xref: /linux/samples/bpf/tcp_cong_kern.c (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
1bb56d444SLawrence Brakmo /* Copyright (c) 2017 Facebook
2bb56d444SLawrence Brakmo  *
3bb56d444SLawrence Brakmo  * This program is free software; you can redistribute it and/or
4bb56d444SLawrence Brakmo  * modify it under the terms of version 2 of the GNU General Public
5bb56d444SLawrence Brakmo  * License as published by the Free Software Foundation.
6bb56d444SLawrence Brakmo  *
7bb56d444SLawrence Brakmo  * BPF program to set congestion control to dctcp when both hosts are
8bb56d444SLawrence Brakmo  * in the same datacenter (as deteremined by IPv6 prefix).
9bb56d444SLawrence Brakmo  *
10ea9b6362SJakub Kicinski  * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
11bb56d444SLawrence Brakmo  */
12bb56d444SLawrence Brakmo 
13bb56d444SLawrence Brakmo #include <uapi/linux/bpf.h>
14bb56d444SLawrence Brakmo #include <uapi/linux/tcp.h>
15bb56d444SLawrence Brakmo #include <uapi/linux/if_ether.h>
16bb56d444SLawrence Brakmo #include <uapi/linux/if_packet.h>
17bb56d444SLawrence Brakmo #include <uapi/linux/ip.h>
18bb56d444SLawrence Brakmo #include <linux/socket.h>
19*7cf245a3SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
20*7cf245a3SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
21bb56d444SLawrence Brakmo 
22bb56d444SLawrence Brakmo #define DEBUG 1
23bb56d444SLawrence Brakmo 
24bb56d444SLawrence Brakmo SEC("sockops")
bpf_cong(struct bpf_sock_ops * skops)25bb56d444SLawrence Brakmo int bpf_cong(struct bpf_sock_ops *skops)
26bb56d444SLawrence Brakmo {
27bb56d444SLawrence Brakmo 	char cong[] = "dctcp";
28bb56d444SLawrence Brakmo 	int rv = 0;
29bb56d444SLawrence Brakmo 	int op;
30bb56d444SLawrence Brakmo 
31bb56d444SLawrence Brakmo 	/* For testing purposes, only execute rest of BPF program
32bb56d444SLawrence Brakmo 	 * if neither port numberis 55601
33bb56d444SLawrence Brakmo 	 */
34bb56d444SLawrence Brakmo 	if (bpf_ntohl(skops->remote_port) != 55601 &&
352ff969fbSLawrence Brakmo 	    skops->local_port != 55601) {
362ff969fbSLawrence Brakmo 		skops->reply = -1;
372ff969fbSLawrence Brakmo 		return 1;
382ff969fbSLawrence Brakmo 	}
39bb56d444SLawrence Brakmo 
40bb56d444SLawrence Brakmo 	op = (int) skops->op;
41bb56d444SLawrence Brakmo 
42bb56d444SLawrence Brakmo #ifdef DEBUG
43bb56d444SLawrence Brakmo 	bpf_printk("BPF command: %d\n", op);
44bb56d444SLawrence Brakmo #endif
45bb56d444SLawrence Brakmo 
46bb56d444SLawrence Brakmo 	/* Check if both hosts are in the same datacenter. For this
47bb56d444SLawrence Brakmo 	 * example they are if the 1st 5.5 bytes in the IPv6 address
48bb56d444SLawrence Brakmo 	 * are the same.
49bb56d444SLawrence Brakmo 	 */
50bb56d444SLawrence Brakmo 	if (skops->family == AF_INET6 &&
51bb56d444SLawrence Brakmo 	    skops->local_ip6[0] == skops->remote_ip6[0] &&
52bb56d444SLawrence Brakmo 	    (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
53bb56d444SLawrence Brakmo 	    (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
54bb56d444SLawrence Brakmo 		switch (op) {
55bb56d444SLawrence Brakmo 		case BPF_SOCK_OPS_NEEDS_ECN:
56bb56d444SLawrence Brakmo 			rv = 1;
57bb56d444SLawrence Brakmo 			break;
58bb56d444SLawrence Brakmo 		case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
59bb56d444SLawrence Brakmo 			rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION,
60bb56d444SLawrence Brakmo 					    cong, sizeof(cong));
61bb56d444SLawrence Brakmo 			break;
62bb56d444SLawrence Brakmo 		case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
63bb56d444SLawrence Brakmo 			rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION,
64bb56d444SLawrence Brakmo 					    cong, sizeof(cong));
65bb56d444SLawrence Brakmo 			break;
66bb56d444SLawrence Brakmo 		default:
67bb56d444SLawrence Brakmo 			rv = -1;
68bb56d444SLawrence Brakmo 		}
69bb56d444SLawrence Brakmo 	} else {
70bb56d444SLawrence Brakmo 		rv = -1;
71bb56d444SLawrence Brakmo 	}
72bb56d444SLawrence Brakmo #ifdef DEBUG
73bb56d444SLawrence Brakmo 	bpf_printk("Returning %d\n", rv);
74bb56d444SLawrence Brakmo #endif
75bb56d444SLawrence Brakmo 	skops->reply = rv;
76bb56d444SLawrence Brakmo 	return 1;
77bb56d444SLawrence Brakmo }
78bb56d444SLawrence Brakmo char _license[] SEC("license") = "GPL";
79