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  * Sample BPF program to set send and receive buffers to 150KB, sndcwnd clamp
0008  * to 100 packets and SYN and SYN_ACK RTOs to 10ms when both hosts are within
0009  * the same datacenter. For his example, we assume they are within the same
0010  * datacenter when the first 5.5 bytes of their IPv6 addresses are the same.
0011  *
0012  * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
0013  */
0014 
0015 #include <uapi/linux/bpf.h>
0016 #include <uapi/linux/if_ether.h>
0017 #include <uapi/linux/if_packet.h>
0018 #include <uapi/linux/ip.h>
0019 #include <linux/socket.h>
0020 #include <bpf/bpf_helpers.h>
0021 #include <bpf/bpf_endian.h>
0022 
0023 #define DEBUG 1
0024 
0025 SEC("sockops")
0026 int bpf_clamp(struct bpf_sock_ops *skops)
0027 {
0028     int bufsize = 150000;
0029     int to_init = 10;
0030     int clamp = 100;
0031     int rv = 0;
0032     int op;
0033 
0034     /* For testing purposes, only execute rest of BPF program
0035      * if neither port numberis 55601
0036      */
0037     if (bpf_ntohl(skops->remote_port) != 55601 && skops->local_port != 55601) {
0038         skops->reply = -1;
0039         return 0;
0040     }
0041 
0042     op = (int) skops->op;
0043 
0044 #ifdef DEBUG
0045     bpf_printk("BPF command: %d\n", op);
0046 #endif
0047 
0048     /* Check that both hosts are within same datacenter. For this example
0049      * it is the case when the first 5.5 bytes of their IPv6 addresses are
0050      * the same.
0051      */
0052     if (skops->family == AF_INET6 &&
0053         skops->local_ip6[0] == skops->remote_ip6[0] &&
0054         (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
0055         (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
0056         switch (op) {
0057         case BPF_SOCK_OPS_TIMEOUT_INIT:
0058             rv = to_init;
0059             break;
0060         case BPF_SOCK_OPS_TCP_CONNECT_CB:
0061             /* Set sndbuf and rcvbuf of active connections */
0062             rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF,
0063                         &bufsize, sizeof(bufsize));
0064             rv += bpf_setsockopt(skops, SOL_SOCKET,
0065                          SO_RCVBUF, &bufsize,
0066                          sizeof(bufsize));
0067             break;
0068         case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
0069             rv = bpf_setsockopt(skops, SOL_TCP,
0070                         TCP_BPF_SNDCWND_CLAMP,
0071                         &clamp, sizeof(clamp));
0072             break;
0073         case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
0074             /* Set sndbuf and rcvbuf of passive connections */
0075             rv = bpf_setsockopt(skops, SOL_TCP,
0076                         TCP_BPF_SNDCWND_CLAMP,
0077                         &clamp, sizeof(clamp));
0078             rv += bpf_setsockopt(skops, SOL_SOCKET,
0079                          SO_SNDBUF, &bufsize,
0080                          sizeof(bufsize));
0081             rv += bpf_setsockopt(skops, SOL_SOCKET,
0082                          SO_RCVBUF, &bufsize,
0083                          sizeof(bufsize));
0084             break;
0085         default:
0086             rv = -1;
0087         }
0088     } else {
0089         rv = -1;
0090     }
0091 #ifdef DEBUG
0092     bpf_printk("Returning %d\n", rv);
0093 #endif
0094     skops->reply = rv;
0095     return 1;
0096 }
0097 char _license[] SEC("license") = "GPL";