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 initial receive window to 40 packets and send
0008  * and receive buffers to 1.5MB. This would usually be done after
0009  * doing appropriate checks that indicate the hosts are far enough
0010  * away (i.e. large RTT).
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_bufs(struct bpf_sock_ops *skops)
0027 {
0028     int bufsize = 1500000;
0029     int rwnd_init = 40;
0030     int rv = 0;
0031     int op;
0032 
0033     /* For testing purposes, only execute rest of BPF program
0034      * if neither port numberis 55601
0035      */
0036     if (bpf_ntohl(skops->remote_port) != 55601 &&
0037         skops->local_port != 55601) {
0038         skops->reply = -1;
0039         return 1;
0040     }
0041 
0042     op = (int) skops->op;
0043 
0044 #ifdef DEBUG
0045     bpf_printk("Returning %d\n", rv);
0046 #endif
0047 
0048     /* Usually there would be a check to insure the hosts are far
0049      * from each other so it makes sense to increase buffer sizes
0050      */
0051     switch (op) {
0052     case BPF_SOCK_OPS_RWND_INIT:
0053         rv = rwnd_init;
0054         break;
0055     case BPF_SOCK_OPS_TCP_CONNECT_CB:
0056         /* Set sndbuf and rcvbuf of active connections */
0057         rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
0058                     sizeof(bufsize));
0059         rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
0060                      &bufsize, sizeof(bufsize));
0061         break;
0062     case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
0063         /* Nothing to do */
0064         break;
0065     case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
0066         /* Set sndbuf and rcvbuf of passive connections */
0067         rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
0068                     sizeof(bufsize));
0069         rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
0070                      &bufsize, sizeof(bufsize));
0071         break;
0072     default:
0073         rv = -1;
0074     }
0075 #ifdef DEBUG
0076     bpf_printk("Returning %d\n", rv);
0077 #endif
0078     skops->reply = rv;
0079     return 1;
0080 }
0081 char _license[] SEC("license") = "GPL";