Skip to content

Commit 3882dcc

Browse files
alan-maguireMartin KaFai Lau
authored and
Martin KaFai Lau
committed
bpf/bpf_get,set_sockopt: add option to set TCP-BPF sock ops flags
Currently the only opportunity to set sock ops flags dictating which callbacks fire for a socket is from within a TCP-BPF sockops program. This is problematic if the connection is already set up as there is no further chance to specify callbacks for that socket. Add TCP_BPF_SOCK_OPS_CB_FLAGS to bpf_setsockopt() and bpf_getsockopt() to allow users to specify callbacks later, either via an iterator over sockets or via a socket-specific program triggered by a setsockopt() on the socket. Previous discussion on this here [1]. [1] https://lore.kernel.org/bpf/[email protected]/ Signed-off-by: Alan Maguire <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 91d516d commit 3882dcc

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

include/uapi/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,7 @@ union bpf_attr {
28512851
* **TCP_SYNCNT**, **TCP_USER_TIMEOUT**, **TCP_NOTSENT_LOWAT**,
28522852
* **TCP_NODELAY**, **TCP_MAXSEG**, **TCP_WINDOW_CLAMP**,
28532853
* **TCP_THIN_LINEAR_TIMEOUTS**, **TCP_BPF_DELACK_MAX**,
2854-
* **TCP_BPF_RTO_MIN**.
2854+
* **TCP_BPF_RTO_MIN**, **TCP_BPF_SOCK_OPS_CB_FLAGS**.
28552855
* * **IPPROTO_IP**, which supports *optname* **IP_TOS**.
28562856
* * **IPPROTO_IPV6**, which supports the following *optname*\ s:
28572857
* **IPV6_TCLASS**, **IPV6_AUTOFLOWLABEL**.
@@ -7080,6 +7080,7 @@ enum {
70807080
TCP_BPF_SYN = 1005, /* Copy the TCP header */
70817081
TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */
70827082
TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */
7083+
TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */
70837084
};
70847085

70857086
enum {

net/core/filter.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5278,6 +5278,11 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
52785278
return -EINVAL;
52795279
inet_csk(sk)->icsk_rto_min = timeout;
52805280
break;
5281+
case TCP_BPF_SOCK_OPS_CB_FLAGS:
5282+
if (val & ~(BPF_SOCK_OPS_ALL_CB_FLAGS))
5283+
return -EINVAL;
5284+
tp->bpf_sock_ops_cb_flags = val;
5285+
break;
52815286
default:
52825287
return -EINVAL;
52835288
}
@@ -5366,6 +5371,17 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
53665371
if (*optlen < 1)
53675372
return -EINVAL;
53685373
break;
5374+
case TCP_BPF_SOCK_OPS_CB_FLAGS:
5375+
if (*optlen != sizeof(int))
5376+
return -EINVAL;
5377+
if (getopt) {
5378+
struct tcp_sock *tp = tcp_sk(sk);
5379+
int cb_flags = tp->bpf_sock_ops_cb_flags;
5380+
5381+
memcpy(optval, &cb_flags, *optlen);
5382+
return 0;
5383+
}
5384+
return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
53695385
default:
53705386
if (getopt)
53715387
return -EINVAL;

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,7 @@ union bpf_attr {
28512851
* **TCP_SYNCNT**, **TCP_USER_TIMEOUT**, **TCP_NOTSENT_LOWAT**,
28522852
* **TCP_NODELAY**, **TCP_MAXSEG**, **TCP_WINDOW_CLAMP**,
28532853
* **TCP_THIN_LINEAR_TIMEOUTS**, **TCP_BPF_DELACK_MAX**,
2854-
* **TCP_BPF_RTO_MIN**.
2854+
* **TCP_BPF_RTO_MIN**, **TCP_BPF_SOCK_OPS_CB_FLAGS**.
28552855
* * **IPPROTO_IP**, which supports *optname* **IP_TOS**.
28562856
* * **IPPROTO_IPV6**, which supports the following *optname*\ s:
28572857
* **IPV6_TCLASS**, **IPV6_AUTOFLOWLABEL**.
@@ -7080,6 +7080,7 @@ enum {
70807080
TCP_BPF_SYN = 1005, /* Copy the TCP header */
70817081
TCP_BPF_SYN_IP = 1006, /* Copy the IP[46] and TCP header */
70827082
TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */
7083+
TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */
70837084
};
70847085

70857086
enum {

0 commit comments

Comments
 (0)