1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <uapi/linux/bpf.h>
16#include <uapi/linux/if_ether.h>
17#include <uapi/linux/if_packet.h>
18#include <uapi/linux/ip.h>
19#include <linux/socket.h>
20#include "bpf_helpers.h"
21#include "bpf_endian.h"
22
23#define DEBUG 1
24
25#define bpf_printk(fmt, ...) \
26({ \
27 char ____fmt[] = fmt; \
28 bpf_trace_printk(____fmt, sizeof(____fmt), \
29 ##__VA_ARGS__); \
30})
31
32SEC("sockops")
33int bpf_clamp(struct bpf_sock_ops *skops)
34{
35 int bufsize = 150000;
36 int to_init = 10;
37 int clamp = 100;
38 int rv = 0;
39 int op;
40
41
42
43
44 if (bpf_ntohl(skops->remote_port) != 55601 && skops->local_port != 55601)
45 return -1;
46
47 op = (int) skops->op;
48
49#ifdef DEBUG
50 bpf_printk("BPF command: %d\n", op);
51#endif
52
53
54
55
56
57 if (skops->family == AF_INET6 &&
58 skops->local_ip6[0] == skops->remote_ip6[0] &&
59 (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
60 (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
61 switch (op) {
62 case BPF_SOCK_OPS_TIMEOUT_INIT:
63 rv = to_init;
64 break;
65 case BPF_SOCK_OPS_TCP_CONNECT_CB:
66
67 rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF,
68 &bufsize, sizeof(bufsize));
69 rv = rv*100 + bpf_setsockopt(skops, SOL_SOCKET,
70 SO_RCVBUF, &bufsize,
71 sizeof(bufsize));
72 break;
73 case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
74 rv = bpf_setsockopt(skops, SOL_TCP,
75 TCP_BPF_SNDCWND_CLAMP,
76 &clamp, sizeof(clamp));
77 break;
78 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
79
80 rv = bpf_setsockopt(skops, SOL_TCP,
81 TCP_BPF_SNDCWND_CLAMP,
82 &clamp, sizeof(clamp));
83 rv = rv*100 + bpf_setsockopt(skops, SOL_SOCKET,
84 SO_SNDBUF, &bufsize,
85 sizeof(bufsize));
86 rv = rv*100 + bpf_setsockopt(skops, SOL_SOCKET,
87 SO_RCVBUF, &bufsize,
88 sizeof(bufsize));
89 break;
90 default:
91 rv = -1;
92 }
93 } else {
94 rv = -1;
95 }
96#ifdef DEBUG
97 bpf_printk("Returning %d\n", rv);
98#endif
99 skops->reply = rv;
100 return 1;
101}
102char _license[] SEC("license") = "GPL";
103