1 /* Copyright (c) 2017 Facebook
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.
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.
12 * Use load_sock_ops to load this BPF program.
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"
25 #define bpf_printk(fmt, ...) \
27 char ____fmt[] = fmt; \
28 bpf_trace_printk(____fmt, sizeof(____fmt), \
33 int bpf_clamp(struct bpf_sock_ops
*skops
)
41 /* For testing purposes, only execute rest of BPF program
42 * if neither port numberis 55601
44 if (bpf_ntohl(skops
->remote_port
) != 55601 && skops
->local_port
!= 55601)
50 bpf_printk("BPF command: %d\n", op
);
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
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)) {
62 case BPF_SOCK_OPS_TIMEOUT_INIT
:
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
,
73 case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB
:
74 rv
= bpf_setsockopt(skops
, SOL_TCP
,
75 TCP_BPF_SNDCWND_CLAMP
,
76 &clamp
, sizeof(clamp
));
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
,
86 rv
= rv
*100 + bpf_setsockopt(skops
, SOL_SOCKET
,
97 bpf_printk("Returning %d\n", rv
);
102 char _license
[] SEC("license") = "GPL";