aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorLawrence Brakmo <brakmo@fb.com>2017-06-30 23:02:54 -0400
committerDavid S. Miller <davem@davemloft.net>2017-07-01 19:15:14 -0400
commit6c4a01b27852347cd3c4d2bf0dadf157ea6c3c40 (patch)
treeb6b16a42bee5779e1ca658061ea6f97b4b8e6011 /samples
parent13bf96411ad2bd162a4f9470d58c6bb579c96e21 (diff)
bpf: Sample bpf program to set sndcwnd clamp
Sample BPF program, tcp_clamp_kern.c, to demostrate the use of setting the sndcwnd clamp. This program assumes that if the first 5.5 bytes of the host's IPv6 addresses are the same, then the hosts are in the same datacenter and sets sndcwnd clamp to 100 packets, SYN and SYN-ACK RTOs to 10ms and send/receive buffer sizes to 150KB. Signed-off-by: Lawrence Brakmo <brakmo@fb.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/Makefile1
-rw-r--r--samples/bpf/tcp_clamp_kern.c102
2 files changed, 103 insertions, 0 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 242d76e84a4b..9c650589e80f 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -118,6 +118,7 @@ always += tcp_rwnd_kern.o
118always += tcp_bufs_kern.o 118always += tcp_bufs_kern.o
119always += tcp_cong_kern.o 119always += tcp_cong_kern.o
120always += tcp_iw_kern.o 120always += tcp_iw_kern.o
121always += tcp_clamp_kern.o
121 122
122HOSTCFLAGS += -I$(objtree)/usr/include 123HOSTCFLAGS += -I$(objtree)/usr/include
123HOSTCFLAGS += -I$(srctree)/tools/lib/ 124HOSTCFLAGS += -I$(srctree)/tools/lib/
diff --git a/samples/bpf/tcp_clamp_kern.c b/samples/bpf/tcp_clamp_kern.c
new file mode 100644
index 000000000000..d68eadd9ca2d
--- /dev/null
+++ b/samples/bpf/tcp_clamp_kern.c
@@ -0,0 +1,102 @@
1/* Copyright (c) 2017 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * Sample BPF program to set send and receive buffers to 150KB, sndcwnd clamp
8 * to 100 packets and SYN and SYN_ACK RTOs to 10ms when both hosts are within
9 * the same datacenter. For his example, we assume they are within the same
10 * datacenter when the first 5.5 bytes of their IPv6 addresses are the same.
11 *
12 * Use load_sock_ops to load this BPF program.
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 /* For testing purposes, only execute rest of BPF program
42 * if neither port numberis 55601
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 /* Check that both hosts are within same datacenter. For this example
54 * it is the case when the first 5.5 bytes of their IPv6 addresses are
55 * the same.
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 /* Set sndbuf and rcvbuf of active connections */
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 /* Set sndbuf and rcvbuf of passive connections */
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";