xref: /OK3568_Linux_fs/kernel/samples/bpf/tcp_rwnd_kern.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* Copyright (c) 2017 Facebook
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
4*4882a593Smuzhiyun  * modify it under the terms of version 2 of the GNU General Public
5*4882a593Smuzhiyun  * License as published by the Free Software Foundation.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * BPF program to set initial receive window to 40 packets when using IPv6
8*4882a593Smuzhiyun  * and the first 5.5 bytes of the IPv6 addresses are not the same (in this
9*4882a593Smuzhiyun  * example that means both hosts are not the same datacenter).
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <uapi/linux/bpf.h>
15*4882a593Smuzhiyun #include <uapi/linux/if_ether.h>
16*4882a593Smuzhiyun #include <uapi/linux/if_packet.h>
17*4882a593Smuzhiyun #include <uapi/linux/ip.h>
18*4882a593Smuzhiyun #include <linux/socket.h>
19*4882a593Smuzhiyun #include <bpf/bpf_helpers.h>
20*4882a593Smuzhiyun #include <bpf/bpf_endian.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define DEBUG 1
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun SEC("sockops")
bpf_rwnd(struct bpf_sock_ops * skops)25*4882a593Smuzhiyun int bpf_rwnd(struct bpf_sock_ops *skops)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	int rv = -1;
28*4882a593Smuzhiyun 	int op;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	/* For testing purposes, only execute rest of BPF program
31*4882a593Smuzhiyun 	 * if neither port numberis 55601
32*4882a593Smuzhiyun 	 */
33*4882a593Smuzhiyun 	if (bpf_ntohl(skops->remote_port) !=
34*4882a593Smuzhiyun 	    55601 && skops->local_port != 55601) {
35*4882a593Smuzhiyun 		skops->reply = -1;
36*4882a593Smuzhiyun 		return 1;
37*4882a593Smuzhiyun 	}
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	op = (int) skops->op;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #ifdef DEBUG
42*4882a593Smuzhiyun 	bpf_printk("BPF command: %d\n", op);
43*4882a593Smuzhiyun #endif
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	/* Check for RWND_INIT operation and IPv6 addresses */
46*4882a593Smuzhiyun 	if (op == BPF_SOCK_OPS_RWND_INIT &&
47*4882a593Smuzhiyun 		skops->family == AF_INET6) {
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 		/* If the first 5.5 bytes of the IPv6 address are not the same
50*4882a593Smuzhiyun 		 * then both hosts are not in the same datacenter
51*4882a593Smuzhiyun 		 * so use a larger initial advertized window (40 packets)
52*4882a593Smuzhiyun 		 */
53*4882a593Smuzhiyun 		if (skops->local_ip6[0] != skops->remote_ip6[0] ||
54*4882a593Smuzhiyun 		    (bpf_ntohl(skops->local_ip6[1]) & 0xfffff000) !=
55*4882a593Smuzhiyun 		    (bpf_ntohl(skops->remote_ip6[1]) & 0xfffff000))
56*4882a593Smuzhiyun 			rv = 40;
57*4882a593Smuzhiyun 	}
58*4882a593Smuzhiyun #ifdef DEBUG
59*4882a593Smuzhiyun 	bpf_printk("Returning %d\n", rv);
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun 	skops->reply = rv;
62*4882a593Smuzhiyun 	return 1;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun char _license[] SEC("license") = "GPL";
65