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 * 10bb56d444SLawrence Brakmo * Use load_sock_ops 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> 19bb56d444SLawrence Brakmo #include "bpf_helpers.h" 20bb56d444SLawrence Brakmo #include "bpf_endian.h" 21bb56d444SLawrence Brakmo 22bb56d444SLawrence Brakmo #define DEBUG 1 23bb56d444SLawrence Brakmo 24bb56d444SLawrence Brakmo #define bpf_printk(fmt, ...) \ 25bb56d444SLawrence Brakmo ({ \ 26bb56d444SLawrence Brakmo char ____fmt[] = fmt; \ 27bb56d444SLawrence Brakmo bpf_trace_printk(____fmt, sizeof(____fmt), \ 28bb56d444SLawrence Brakmo ##__VA_ARGS__); \ 29bb56d444SLawrence Brakmo }) 30bb56d444SLawrence Brakmo 31bb56d444SLawrence Brakmo SEC("sockops") 32bb56d444SLawrence Brakmo int bpf_cong(struct bpf_sock_ops *skops) 33bb56d444SLawrence Brakmo { 34bb56d444SLawrence Brakmo char cong[] = "dctcp"; 35bb56d444SLawrence Brakmo int rv = 0; 36bb56d444SLawrence Brakmo int op; 37bb56d444SLawrence Brakmo 38bb56d444SLawrence Brakmo /* For testing purposes, only execute rest of BPF program 39bb56d444SLawrence Brakmo * if neither port numberis 55601 40bb56d444SLawrence Brakmo */ 41bb56d444SLawrence Brakmo if (bpf_ntohl(skops->remote_port) != 55601 && 42*2ff969fbSLawrence Brakmo skops->local_port != 55601) { 43*2ff969fbSLawrence Brakmo skops->reply = -1; 44*2ff969fbSLawrence Brakmo return 1; 45*2ff969fbSLawrence Brakmo } 46bb56d444SLawrence Brakmo 47bb56d444SLawrence Brakmo op = (int) skops->op; 48bb56d444SLawrence Brakmo 49bb56d444SLawrence Brakmo #ifdef DEBUG 50bb56d444SLawrence Brakmo bpf_printk("BPF command: %d\n", op); 51bb56d444SLawrence Brakmo #endif 52bb56d444SLawrence Brakmo 53bb56d444SLawrence Brakmo /* Check if both hosts are in the same datacenter. For this 54bb56d444SLawrence Brakmo * example they are if the 1st 5.5 bytes in the IPv6 address 55bb56d444SLawrence Brakmo * are the same. 56bb56d444SLawrence Brakmo */ 57bb56d444SLawrence Brakmo if (skops->family == AF_INET6 && 58bb56d444SLawrence Brakmo skops->local_ip6[0] == skops->remote_ip6[0] && 59bb56d444SLawrence Brakmo (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) == 60bb56d444SLawrence Brakmo (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) { 61bb56d444SLawrence Brakmo switch (op) { 62bb56d444SLawrence Brakmo case BPF_SOCK_OPS_NEEDS_ECN: 63bb56d444SLawrence Brakmo rv = 1; 64bb56d444SLawrence Brakmo break; 65bb56d444SLawrence Brakmo case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: 66bb56d444SLawrence Brakmo rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION, 67bb56d444SLawrence Brakmo cong, sizeof(cong)); 68bb56d444SLawrence Brakmo break; 69bb56d444SLawrence Brakmo case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: 70bb56d444SLawrence Brakmo rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION, 71bb56d444SLawrence Brakmo cong, sizeof(cong)); 72bb56d444SLawrence Brakmo break; 73bb56d444SLawrence Brakmo default: 74bb56d444SLawrence Brakmo rv = -1; 75bb56d444SLawrence Brakmo } 76bb56d444SLawrence Brakmo } else { 77bb56d444SLawrence Brakmo rv = -1; 78bb56d444SLawrence Brakmo } 79bb56d444SLawrence Brakmo #ifdef DEBUG 80bb56d444SLawrence Brakmo bpf_printk("Returning %d\n", rv); 81bb56d444SLawrence Brakmo #endif 82bb56d444SLawrence Brakmo skops->reply = rv; 83bb56d444SLawrence Brakmo return 1; 84bb56d444SLawrence Brakmo } 85bb56d444SLawrence Brakmo char _license[] SEC("license") = "GPL"; 86