162306a36Sopenharmony_ci/* Copyright (c) 2017 Facebook 262306a36Sopenharmony_ci * 362306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or 462306a36Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public 562306a36Sopenharmony_ci * License as published by the Free Software Foundation. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * BPF program to set initial congestion window and initial receive 862306a36Sopenharmony_ci * window to 40 packets and send and receive buffers to 1.5MB. This 962306a36Sopenharmony_ci * would usually be done after doing appropriate checks that indicate 1062306a36Sopenharmony_ci * the hosts are far enough away (i.e. large RTT). 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program. 1362306a36Sopenharmony_ci */ 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <uapi/linux/bpf.h> 1662306a36Sopenharmony_ci#include <uapi/linux/if_ether.h> 1762306a36Sopenharmony_ci#include <uapi/linux/if_packet.h> 1862306a36Sopenharmony_ci#include <uapi/linux/ip.h> 1962306a36Sopenharmony_ci#include <linux/socket.h> 2062306a36Sopenharmony_ci#include <bpf/bpf_helpers.h> 2162306a36Sopenharmony_ci#include <bpf/bpf_endian.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#define DEBUG 1 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ciSEC("sockops") 2662306a36Sopenharmony_ciint bpf_iw(struct bpf_sock_ops *skops) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci int bufsize = 1500000; 2962306a36Sopenharmony_ci int rwnd_init = 40; 3062306a36Sopenharmony_ci int iw = 40; 3162306a36Sopenharmony_ci int rv = 0; 3262306a36Sopenharmony_ci int op; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci /* For testing purposes, only execute rest of BPF program 3562306a36Sopenharmony_ci * if neither port numberis 55601 3662306a36Sopenharmony_ci */ 3762306a36Sopenharmony_ci if (bpf_ntohl(skops->remote_port) != 55601 && 3862306a36Sopenharmony_ci skops->local_port != 55601) { 3962306a36Sopenharmony_ci skops->reply = -1; 4062306a36Sopenharmony_ci return 1; 4162306a36Sopenharmony_ci } 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci op = (int) skops->op; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci#ifdef DEBUG 4662306a36Sopenharmony_ci bpf_printk("BPF command: %d\n", op); 4762306a36Sopenharmony_ci#endif 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci /* Usually there would be a check to insure the hosts are far 5062306a36Sopenharmony_ci * from each other so it makes sense to increase buffer sizes 5162306a36Sopenharmony_ci */ 5262306a36Sopenharmony_ci switch (op) { 5362306a36Sopenharmony_ci case BPF_SOCK_OPS_RWND_INIT: 5462306a36Sopenharmony_ci rv = rwnd_init; 5562306a36Sopenharmony_ci break; 5662306a36Sopenharmony_ci case BPF_SOCK_OPS_TCP_CONNECT_CB: 5762306a36Sopenharmony_ci /* Set sndbuf and rcvbuf of active connections */ 5862306a36Sopenharmony_ci rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize, 5962306a36Sopenharmony_ci sizeof(bufsize)); 6062306a36Sopenharmony_ci rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF, 6162306a36Sopenharmony_ci &bufsize, sizeof(bufsize)); 6262306a36Sopenharmony_ci break; 6362306a36Sopenharmony_ci case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: 6462306a36Sopenharmony_ci rv = bpf_setsockopt(skops, SOL_TCP, TCP_BPF_IW, &iw, 6562306a36Sopenharmony_ci sizeof(iw)); 6662306a36Sopenharmony_ci break; 6762306a36Sopenharmony_ci case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: 6862306a36Sopenharmony_ci /* Set sndbuf and rcvbuf of passive connections */ 6962306a36Sopenharmony_ci rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize, 7062306a36Sopenharmony_ci sizeof(bufsize)); 7162306a36Sopenharmony_ci rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF, 7262306a36Sopenharmony_ci &bufsize, sizeof(bufsize)); 7362306a36Sopenharmony_ci break; 7462306a36Sopenharmony_ci default: 7562306a36Sopenharmony_ci rv = -1; 7662306a36Sopenharmony_ci } 7762306a36Sopenharmony_ci#ifdef DEBUG 7862306a36Sopenharmony_ci bpf_printk("Returning %d\n", rv); 7962306a36Sopenharmony_ci#endif 8062306a36Sopenharmony_ci skops->reply = rv; 8162306a36Sopenharmony_ci return 1; 8262306a36Sopenharmony_ci} 8362306a36Sopenharmony_cichar _license[] SEC("license") = "GPL"; 84