xref: /openbmc/linux/samples/bpf/tcp_basertt_kern.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1c890063eSLawrence Brakmo /* Copyright (c) 2017 Facebook
2c890063eSLawrence Brakmo  *
3c890063eSLawrence Brakmo  * This program is free software; you can redistribute it and/or
4c890063eSLawrence Brakmo  * modify it under the terms of version 2 of the GNU General Public
5c890063eSLawrence Brakmo  * License as published by the Free Software Foundation.
6c890063eSLawrence Brakmo  *
7c890063eSLawrence Brakmo  * BPF program to set base_rtt to 80us when host is running TCP-NV and
8c890063eSLawrence Brakmo  * both hosts are in the same datacenter (as determined by IPv6 prefix).
9c890063eSLawrence Brakmo  *
10ea9b6362SJakub Kicinski  * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
11c890063eSLawrence Brakmo  */
12c890063eSLawrence Brakmo 
13c890063eSLawrence Brakmo #include <uapi/linux/bpf.h>
14c890063eSLawrence Brakmo #include <uapi/linux/tcp.h>
15c890063eSLawrence Brakmo #include <uapi/linux/if_ether.h>
16c890063eSLawrence Brakmo #include <uapi/linux/if_packet.h>
17c890063eSLawrence Brakmo #include <uapi/linux/ip.h>
18c890063eSLawrence Brakmo #include <linux/socket.h>
197cf245a3SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
207cf245a3SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
21c890063eSLawrence Brakmo 
22c890063eSLawrence Brakmo #define DEBUG 1
23c890063eSLawrence Brakmo 
24c890063eSLawrence Brakmo SEC("sockops")
bpf_basertt(struct bpf_sock_ops * skops)25c890063eSLawrence Brakmo int bpf_basertt(struct bpf_sock_ops *skops)
26c890063eSLawrence Brakmo {
27c890063eSLawrence Brakmo 	char cong[20];
28c890063eSLawrence Brakmo 	char nv[] = "nv";
29c890063eSLawrence Brakmo 	int rv = 0, n;
30c890063eSLawrence Brakmo 	int op;
31c890063eSLawrence Brakmo 
32c890063eSLawrence Brakmo 	op = (int) skops->op;
33c890063eSLawrence Brakmo 
34c890063eSLawrence Brakmo #ifdef DEBUG
35c890063eSLawrence Brakmo 	bpf_printk("BPF command: %d\n", op);
36c890063eSLawrence Brakmo #endif
37c890063eSLawrence Brakmo 
38c890063eSLawrence Brakmo 	/* Check if both hosts are in the same datacenter. For this
39c890063eSLawrence Brakmo 	 * example they are if the 1st 5.5 bytes in the IPv6 address
40c890063eSLawrence Brakmo 	 * are the same.
41c890063eSLawrence Brakmo 	 */
42c890063eSLawrence Brakmo 	if (skops->family == AF_INET6 &&
43c890063eSLawrence Brakmo 	    skops->local_ip6[0] == skops->remote_ip6[0] &&
44c890063eSLawrence Brakmo 	    (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
45c890063eSLawrence Brakmo 	    (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
46c890063eSLawrence Brakmo 		switch (op) {
47c890063eSLawrence Brakmo 		case BPF_SOCK_OPS_BASE_RTT:
48c890063eSLawrence Brakmo 			n = bpf_getsockopt(skops, SOL_TCP, TCP_CONGESTION,
49c890063eSLawrence Brakmo 					   cong, sizeof(cong));
50*f4dea968SPengcheng Yang 			if (!n && !__builtin_memcmp(cong, nv, sizeof(nv))) {
51c890063eSLawrence Brakmo 				/* Set base_rtt to 80us */
52c890063eSLawrence Brakmo 				rv = 80;
53c890063eSLawrence Brakmo 			} else if (n) {
54c890063eSLawrence Brakmo 				rv = n;
55c890063eSLawrence Brakmo 			} else {
56c890063eSLawrence Brakmo 				rv = -1;
57c890063eSLawrence Brakmo 			}
58c890063eSLawrence Brakmo 			break;
59c890063eSLawrence Brakmo 		default:
60c890063eSLawrence Brakmo 			rv = -1;
61c890063eSLawrence Brakmo 		}
62c890063eSLawrence Brakmo 	} else {
63c890063eSLawrence Brakmo 		rv = -1;
64c890063eSLawrence Brakmo 	}
65c890063eSLawrence Brakmo #ifdef DEBUG
66c890063eSLawrence Brakmo 	bpf_printk("Returning %d\n", rv);
67c890063eSLawrence Brakmo #endif
68c890063eSLawrence Brakmo 	skops->reply = rv;
69c890063eSLawrence Brakmo 	return 1;
70c890063eSLawrence Brakmo }
71c890063eSLawrence Brakmo char _license[] SEC("license") = "GPL";
72