Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2017 Facebook
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  *
0007  * BPF program to set base_rtt to 80us when host is running TCP-NV and
0008  * both hosts are in the same datacenter (as determined by IPv6 prefix).
0009  *
0010  * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
0011  */
0012 
0013 #include <uapi/linux/bpf.h>
0014 #include <uapi/linux/tcp.h>
0015 #include <uapi/linux/if_ether.h>
0016 #include <uapi/linux/if_packet.h>
0017 #include <uapi/linux/ip.h>
0018 #include <linux/socket.h>
0019 #include <bpf/bpf_helpers.h>
0020 #include <bpf/bpf_endian.h>
0021 
0022 #define DEBUG 1
0023 
0024 SEC("sockops")
0025 int bpf_basertt(struct bpf_sock_ops *skops)
0026 {
0027     char cong[20];
0028     char nv[] = "nv";
0029     int rv = 0, n;
0030     int op;
0031 
0032     op = (int) skops->op;
0033 
0034 #ifdef DEBUG
0035     bpf_printk("BPF command: %d\n", op);
0036 #endif
0037 
0038     /* Check if both hosts are in the same datacenter. For this
0039      * example they are if the 1st 5.5 bytes in the IPv6 address
0040      * are the same.
0041      */
0042     if (skops->family == AF_INET6 &&
0043         skops->local_ip6[0] == skops->remote_ip6[0] &&
0044         (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
0045         (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
0046         switch (op) {
0047         case BPF_SOCK_OPS_BASE_RTT:
0048             n = bpf_getsockopt(skops, SOL_TCP, TCP_CONGESTION,
0049                        cong, sizeof(cong));
0050             if (!n && !__builtin_memcmp(cong, nv, sizeof(nv)+1)) {
0051                 /* Set base_rtt to 80us */
0052                 rv = 80;
0053             } else if (n) {
0054                 rv = n;
0055             } else {
0056                 rv = -1;
0057             }
0058             break;
0059         default:
0060             rv = -1;
0061         }
0062     } else {
0063         rv = -1;
0064     }
0065 #ifdef DEBUG
0066     bpf_printk("Returning %d\n", rv);
0067 #endif
0068     skops->reply = rv;
0069     return 1;
0070 }
0071 char _license[] SEC("license") = "GPL";