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 * Sample BPF program to set send and receive buffers to 150KB, sndcwnd clamp 862306a36Sopenharmony_ci * to 100 packets and SYN and SYN_ACK RTOs to 10ms when both hosts are within 962306a36Sopenharmony_ci * the same datacenter. For his example, we assume they are within the same 1062306a36Sopenharmony_ci * datacenter when the first 5.5 bytes of their IPv6 addresses are the same. 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_clamp(struct bpf_sock_ops *skops) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci int bufsize = 150000; 2962306a36Sopenharmony_ci int to_init = 10; 3062306a36Sopenharmony_ci int clamp = 100; 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 && skops->local_port != 55601) { 3862306a36Sopenharmony_ci skops->reply = -1; 3962306a36Sopenharmony_ci return 0; 4062306a36Sopenharmony_ci } 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci op = (int) skops->op; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#ifdef DEBUG 4562306a36Sopenharmony_ci bpf_printk("BPF command: %d\n", op); 4662306a36Sopenharmony_ci#endif 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci /* Check that both hosts are within same datacenter. For this example 4962306a36Sopenharmony_ci * it is the case when the first 5.5 bytes of their IPv6 addresses are 5062306a36Sopenharmony_ci * the same. 5162306a36Sopenharmony_ci */ 5262306a36Sopenharmony_ci if (skops->family == AF_INET6 && 5362306a36Sopenharmony_ci skops->local_ip6[0] == skops->remote_ip6[0] && 5462306a36Sopenharmony_ci (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) == 5562306a36Sopenharmony_ci (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) { 5662306a36Sopenharmony_ci switch (op) { 5762306a36Sopenharmony_ci case BPF_SOCK_OPS_TIMEOUT_INIT: 5862306a36Sopenharmony_ci rv = to_init; 5962306a36Sopenharmony_ci break; 6062306a36Sopenharmony_ci case BPF_SOCK_OPS_TCP_CONNECT_CB: 6162306a36Sopenharmony_ci /* Set sndbuf and rcvbuf of active connections */ 6262306a36Sopenharmony_ci rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, 6362306a36Sopenharmony_ci &bufsize, sizeof(bufsize)); 6462306a36Sopenharmony_ci rv += bpf_setsockopt(skops, SOL_SOCKET, 6562306a36Sopenharmony_ci SO_RCVBUF, &bufsize, 6662306a36Sopenharmony_ci sizeof(bufsize)); 6762306a36Sopenharmony_ci break; 6862306a36Sopenharmony_ci case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: 6962306a36Sopenharmony_ci rv = bpf_setsockopt(skops, SOL_TCP, 7062306a36Sopenharmony_ci TCP_BPF_SNDCWND_CLAMP, 7162306a36Sopenharmony_ci &clamp, sizeof(clamp)); 7262306a36Sopenharmony_ci break; 7362306a36Sopenharmony_ci case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: 7462306a36Sopenharmony_ci /* Set sndbuf and rcvbuf of passive connections */ 7562306a36Sopenharmony_ci rv = bpf_setsockopt(skops, SOL_TCP, 7662306a36Sopenharmony_ci TCP_BPF_SNDCWND_CLAMP, 7762306a36Sopenharmony_ci &clamp, sizeof(clamp)); 7862306a36Sopenharmony_ci rv += bpf_setsockopt(skops, SOL_SOCKET, 7962306a36Sopenharmony_ci SO_SNDBUF, &bufsize, 8062306a36Sopenharmony_ci sizeof(bufsize)); 8162306a36Sopenharmony_ci rv += bpf_setsockopt(skops, SOL_SOCKET, 8262306a36Sopenharmony_ci SO_RCVBUF, &bufsize, 8362306a36Sopenharmony_ci sizeof(bufsize)); 8462306a36Sopenharmony_ci break; 8562306a36Sopenharmony_ci default: 8662306a36Sopenharmony_ci rv = -1; 8762306a36Sopenharmony_ci } 8862306a36Sopenharmony_ci } else { 8962306a36Sopenharmony_ci rv = -1; 9062306a36Sopenharmony_ci } 9162306a36Sopenharmony_ci#ifdef DEBUG 9262306a36Sopenharmony_ci bpf_printk("Returning %d\n", rv); 9362306a36Sopenharmony_ci#endif 9462306a36Sopenharmony_ci skops->reply = rv; 9562306a36Sopenharmony_ci return 1; 9662306a36Sopenharmony_ci} 9762306a36Sopenharmony_cichar _license[] SEC("license") = "GPL"; 98