1bd4aed0eSJiong Wang // SPDX-License-Identifier: GPL-2.0
2bd4aed0eSJiong Wang #include <stddef.h>
3bd4aed0eSJiong Wang #include <string.h>
45de35e3aSWang YanQing #include <netinet/in.h>
5bd4aed0eSJiong Wang #include <linux/bpf.h>
6bd4aed0eSJiong Wang #include <linux/if_ether.h>
7bd4aed0eSJiong Wang #include <linux/if_packet.h>
8bd4aed0eSJiong Wang #include <linux/ip.h>
9bd4aed0eSJiong Wang #include <linux/ipv6.h>
10bd4aed0eSJiong Wang #include <linux/types.h>
11bd4aed0eSJiong Wang #include <linux/socket.h>
12bd4aed0eSJiong Wang #include <linux/tcp.h>
133e689141SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
143e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
15bd4aed0eSJiong Wang #include "test_tcpnotify.h"
16bd4aed0eSJiong Wang
17df0b7792SAndrii Nakryiko struct {
18bc7430ccSAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
19bc7430ccSAndrii Nakryiko __uint(max_entries, 4);
20bc7430ccSAndrii Nakryiko __type(key, __u32);
21bc7430ccSAndrii Nakryiko __type(value, struct tcpnotify_globals);
22bc7430ccSAndrii Nakryiko } global_map SEC(".maps");
23bd4aed0eSJiong Wang
24df0b7792SAndrii Nakryiko struct {
25bc7430ccSAndrii Nakryiko __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
26bc7430ccSAndrii Nakryiko __uint(max_entries, 2);
27*bd368cb5SHengqi Chen __type(key, int);
28*bd368cb5SHengqi Chen __type(value, __u32);
29bc7430ccSAndrii Nakryiko } perf_event_map SEC(".maps");
30bd4aed0eSJiong Wang
31bd4aed0eSJiong Wang SEC("sockops")
bpf_testcb(struct bpf_sock_ops * skops)32bd4aed0eSJiong Wang int bpf_testcb(struct bpf_sock_ops *skops)
33bd4aed0eSJiong Wang {
34bd4aed0eSJiong Wang int rv = -1;
35bd4aed0eSJiong Wang int op;
36bd4aed0eSJiong Wang
37bd4aed0eSJiong Wang op = (int) skops->op;
38bd4aed0eSJiong Wang
39bd4aed0eSJiong Wang if (bpf_ntohl(skops->remote_port) != TESTPORT) {
40bd4aed0eSJiong Wang skops->reply = -1;
41bd4aed0eSJiong Wang return 0;
42bd4aed0eSJiong Wang }
43bd4aed0eSJiong Wang
44bd4aed0eSJiong Wang switch (op) {
45bd4aed0eSJiong Wang case BPF_SOCK_OPS_TIMEOUT_INIT:
46bd4aed0eSJiong Wang case BPF_SOCK_OPS_RWND_INIT:
47bd4aed0eSJiong Wang case BPF_SOCK_OPS_NEEDS_ECN:
48bd4aed0eSJiong Wang case BPF_SOCK_OPS_BASE_RTT:
49bd4aed0eSJiong Wang case BPF_SOCK_OPS_RTO_CB:
50bd4aed0eSJiong Wang rv = 1;
51bd4aed0eSJiong Wang break;
52bd4aed0eSJiong Wang
53bd4aed0eSJiong Wang case BPF_SOCK_OPS_TCP_CONNECT_CB:
54bd4aed0eSJiong Wang case BPF_SOCK_OPS_TCP_LISTEN_CB:
55bd4aed0eSJiong Wang case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
56bd4aed0eSJiong Wang case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
57bd4aed0eSJiong Wang bpf_sock_ops_cb_flags_set(skops, (BPF_SOCK_OPS_RETRANS_CB_FLAG|
58bd4aed0eSJiong Wang BPF_SOCK_OPS_RTO_CB_FLAG));
59bd4aed0eSJiong Wang rv = 1;
60bd4aed0eSJiong Wang break;
61bd4aed0eSJiong Wang case BPF_SOCK_OPS_RETRANS_CB: {
62bd4aed0eSJiong Wang __u32 key = 0;
63bd4aed0eSJiong Wang struct tcpnotify_globals g, *gp;
64bd4aed0eSJiong Wang struct tcp_notifier msg = {
65bd4aed0eSJiong Wang .type = 0xde,
66bd4aed0eSJiong Wang .subtype = 0xad,
67bd4aed0eSJiong Wang .source = 0xbe,
68bd4aed0eSJiong Wang .hash = 0xef,
69bd4aed0eSJiong Wang };
70bd4aed0eSJiong Wang
71bd4aed0eSJiong Wang rv = 1;
72bd4aed0eSJiong Wang
73bd4aed0eSJiong Wang /* Update results */
74bd4aed0eSJiong Wang gp = bpf_map_lookup_elem(&global_map, &key);
75bd4aed0eSJiong Wang if (!gp)
76bd4aed0eSJiong Wang break;
77bd4aed0eSJiong Wang g = *gp;
78bd4aed0eSJiong Wang g.total_retrans = skops->total_retrans;
79bd4aed0eSJiong Wang g.ncalls++;
80bd4aed0eSJiong Wang bpf_map_update_elem(&global_map, &key, &g,
81bd4aed0eSJiong Wang BPF_ANY);
82bd4aed0eSJiong Wang bpf_perf_event_output(skops, &perf_event_map,
83bd4aed0eSJiong Wang BPF_F_CURRENT_CPU,
84bd4aed0eSJiong Wang &msg, sizeof(msg));
85bd4aed0eSJiong Wang }
86bd4aed0eSJiong Wang break;
87bd4aed0eSJiong Wang default:
88bd4aed0eSJiong Wang rv = -1;
89bd4aed0eSJiong Wang }
90bd4aed0eSJiong Wang skops->reply = rv;
91bd4aed0eSJiong Wang return 1;
92bd4aed0eSJiong Wang }
93bd4aed0eSJiong Wang char _license[] SEC("license") = "GPL";
94