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 congestion control to dctcp when both hosts are
0008  * in the same datacenter (as deteremined 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_cong(struct bpf_sock_ops *skops)
0026 {
0027     char cong[] = "dctcp";
0028     int rv = 0;
0029     int op;
0030 
0031     /* For testing purposes, only execute rest of BPF program
0032      * if neither port numberis 55601
0033      */
0034     if (bpf_ntohl(skops->remote_port) != 55601 &&
0035         skops->local_port != 55601) {
0036         skops->reply = -1;
0037         return 1;
0038     }
0039 
0040     op = (int) skops->op;
0041 
0042 #ifdef DEBUG
0043     bpf_printk("BPF command: %d\n", op);
0044 #endif
0045 
0046     /* Check if both hosts are in the same datacenter. For this
0047      * example they are if the 1st 5.5 bytes in the IPv6 address
0048      * are the same.
0049      */
0050     if (skops->family == AF_INET6 &&
0051         skops->local_ip6[0] == skops->remote_ip6[0] &&
0052         (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
0053         (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
0054         switch (op) {
0055         case BPF_SOCK_OPS_NEEDS_ECN:
0056             rv = 1;
0057             break;
0058         case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
0059             rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION,
0060                         cong, sizeof(cong));
0061             break;
0062         case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
0063             rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION,
0064                         cong, sizeof(cong));
0065             break;
0066         default:
0067             rv = -1;
0068         }
0069     } else {
0070         rv = -1;
0071     }
0072 #ifdef DEBUG
0073     bpf_printk("Returning %d\n", rv);
0074 #endif
0075     skops->reply = rv;
0076     return 1;
0077 }
0078 char _license[] SEC("license") = "GPL";