Skip to content

Commit

Permalink
examples: add sockops example
Browse files Browse the repository at this point in the history
The example uses sockops interface to:
1. Track passive and active established TCP connections.
2. Extract sRTT value every time TCP socket recalculates it.
3. Log 4-tuple and RTT in user space.
  • Loading branch information
gmichelo authored Aug 30, 2022
1 parent 27ffd5d commit 63a1b9e
Show file tree
Hide file tree
Showing 7 changed files with 687 additions and 0 deletions.
144 changes: 144 additions & 0 deletions examples/tcprtt_sockops/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/tcprtt_sockops/bpf_bpfeb.o
Binary file not shown.
144 changes: 144 additions & 0 deletions examples/tcprtt_sockops/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/tcprtt_sockops/bpf_bpfel.o
Binary file not shown.
87 changes: 87 additions & 0 deletions examples/tcprtt_sockops/bpf_sockops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Note that this header file contains a subset of kernel
* definitions needed for the tcprtt_sockops example.
*/
#ifndef BPF_SOCKOPS_H
#define BPF_SOCKOPS_H

/*
* Copy of TCP states.
* See: https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/bpf.h#L6347.
*/
enum {
TCP_ESTABLISHED = 1,
TCP_SYN_SENT = 2,
TCP_SYN_RECV = 3,
TCP_FIN_WAIT1 = 4,
TCP_FIN_WAIT2 = 5,
TCP_TIME_WAIT = 6,
TCP_CLOSE = 7,
TCP_CLOSE_WAIT = 8,
TCP_LAST_ACK = 9,
TCP_LISTEN = 10,
TCP_CLOSING = 11,
TCP_NEW_SYN_RECV = 12,
TCP_MAX_STATES = 13,
};

/*
* Copy of sock_ops operations.
* See: https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/bpf.h#L6233.
*/
enum {
BPF_SOCK_OPS_VOID = 0,
BPF_SOCK_OPS_TIMEOUT_INIT = 1,
BPF_SOCK_OPS_RWND_INIT = 2,
BPF_SOCK_OPS_TCP_CONNECT_CB = 3,
BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB = 4,
BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB = 5,
BPF_SOCK_OPS_NEEDS_ECN = 6,
BPF_SOCK_OPS_BASE_RTT = 7,
BPF_SOCK_OPS_RTO_CB = 8,
BPF_SOCK_OPS_RETRANS_CB = 9,
BPF_SOCK_OPS_STATE_CB = 10,
BPF_SOCK_OPS_TCP_LISTEN_CB = 11,
BPF_SOCK_OPS_RTT_CB = 12,
BPF_SOCK_OPS_PARSE_HDR_OPT_CB = 13,
BPF_SOCK_OPS_HDR_OPT_LEN_CB = 14,
BPF_SOCK_OPS_WRITE_HDR_OPT_CB = 15,
};

/*
* Copy of definitions for bpf_sock_ops_cb_flags.
* See: https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/bpf.h#L6178.
*/
enum {
BPF_SOCK_OPS_RTO_CB_FLAG = 1,
BPF_SOCK_OPS_RETRANS_CB_FLAG = 2,
BPF_SOCK_OPS_STATE_CB_FLAG = 4,
BPF_SOCK_OPS_RTT_CB_FLAG = 8,
BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG = 16,
BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG = 32,
BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = 64,
BPF_SOCK_OPS_ALL_CB_FLAGS = 127,
};

/*
* Copy of bpf.h's bpf_sock_ops with minimal subset
* of fields used by the tcprtt_sockops example.
* See: https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/bpf.h#L6101.
*/
struct bpf_sock_ops {
__u32 op;
union {
__u32 args[4];
__u32 reply;
__u32 replylong[4];
};
__u32 family;
__u32 remote_ip4;
__u32 local_ip4;
__u32 remote_port;
__u32 local_port;
__u32 srtt_us;
__u32 bpf_sock_ops_cb_flags;
} __attribute__((preserve_access_index));

#endif
Loading

0 comments on commit 63a1b9e

Please sign in to comment.