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 base_rtt to 80us when host is running TCP-NV and 8*4882a593Smuzhiyun * both hosts are in the same datacenter (as determined by IPv6 prefix). 9*4882a593Smuzhiyun * 10*4882a593Smuzhiyun * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program. 11*4882a593Smuzhiyun */ 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun #include <uapi/linux/bpf.h> 14*4882a593Smuzhiyun #include <uapi/linux/tcp.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_basertt(struct bpf_sock_ops * skops)25*4882a593Smuzhiyunint bpf_basertt(struct bpf_sock_ops *skops) 26*4882a593Smuzhiyun { 27*4882a593Smuzhiyun char cong[20]; 28*4882a593Smuzhiyun char nv[] = "nv"; 29*4882a593Smuzhiyun int rv = 0, n; 30*4882a593Smuzhiyun int op; 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun op = (int) skops->op; 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun #ifdef DEBUG 35*4882a593Smuzhiyun bpf_printk("BPF command: %d\n", op); 36*4882a593Smuzhiyun #endif 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun /* Check if both hosts are in the same datacenter. For this 39*4882a593Smuzhiyun * example they are if the 1st 5.5 bytes in the IPv6 address 40*4882a593Smuzhiyun * are the same. 41*4882a593Smuzhiyun */ 42*4882a593Smuzhiyun if (skops->family == AF_INET6 && 43*4882a593Smuzhiyun skops->local_ip6[0] == skops->remote_ip6[0] && 44*4882a593Smuzhiyun (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) == 45*4882a593Smuzhiyun (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) { 46*4882a593Smuzhiyun switch (op) { 47*4882a593Smuzhiyun case BPF_SOCK_OPS_BASE_RTT: 48*4882a593Smuzhiyun n = bpf_getsockopt(skops, SOL_TCP, TCP_CONGESTION, 49*4882a593Smuzhiyun cong, sizeof(cong)); 50*4882a593Smuzhiyun if (!n && !__builtin_memcmp(cong, nv, sizeof(nv)+1)) { 51*4882a593Smuzhiyun /* Set base_rtt to 80us */ 52*4882a593Smuzhiyun rv = 80; 53*4882a593Smuzhiyun } else if (n) { 54*4882a593Smuzhiyun rv = n; 55*4882a593Smuzhiyun } else { 56*4882a593Smuzhiyun rv = -1; 57*4882a593Smuzhiyun } 58*4882a593Smuzhiyun break; 59*4882a593Smuzhiyun default: 60*4882a593Smuzhiyun rv = -1; 61*4882a593Smuzhiyun } 62*4882a593Smuzhiyun } else { 63*4882a593Smuzhiyun rv = -1; 64*4882a593Smuzhiyun } 65*4882a593Smuzhiyun #ifdef DEBUG 66*4882a593Smuzhiyun bpf_printk("Returning %d\n", rv); 67*4882a593Smuzhiyun #endif 68*4882a593Smuzhiyun skops->reply = rv; 69*4882a593Smuzhiyun return 1; 70*4882a593Smuzhiyun } 71*4882a593Smuzhiyun char _license[] SEC("license") = "GPL"; 72