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