18c2ecf20Sopenharmony_ci/* Copyright (c) 2017 Facebook
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
48c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public
58c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Sample BPF program to set send and receive buffers to 150KB, sndcwnd clamp
88c2ecf20Sopenharmony_ci * to 100 packets and SYN and SYN_ACK RTOs to 10ms when both hosts are within
98c2ecf20Sopenharmony_ci * the same datacenter. For his example, we assume they are within the same
108c2ecf20Sopenharmony_ci * datacenter when the first 5.5 bytes of their IPv6 addresses are the same.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
168c2ecf20Sopenharmony_ci#include <uapi/linux/if_ether.h>
178c2ecf20Sopenharmony_ci#include <uapi/linux/if_packet.h>
188c2ecf20Sopenharmony_ci#include <uapi/linux/ip.h>
198c2ecf20Sopenharmony_ci#include <linux/socket.h>
208c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
218c2ecf20Sopenharmony_ci#include <bpf/bpf_endian.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define DEBUG 1
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciSEC("sockops")
268c2ecf20Sopenharmony_ciint bpf_clamp(struct bpf_sock_ops *skops)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	int bufsize = 150000;
298c2ecf20Sopenharmony_ci	int to_init = 10;
308c2ecf20Sopenharmony_ci	int clamp = 100;
318c2ecf20Sopenharmony_ci	int rv = 0;
328c2ecf20Sopenharmony_ci	int op;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	/* For testing purposes, only execute rest of BPF program
358c2ecf20Sopenharmony_ci	 * if neither port numberis 55601
368c2ecf20Sopenharmony_ci	 */
378c2ecf20Sopenharmony_ci	if (bpf_ntohl(skops->remote_port) != 55601 && skops->local_port != 55601) {
388c2ecf20Sopenharmony_ci		skops->reply = -1;
398c2ecf20Sopenharmony_ci		return 0;
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	op = (int) skops->op;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#ifdef DEBUG
458c2ecf20Sopenharmony_ci	bpf_printk("BPF command: %d\n", op);
468c2ecf20Sopenharmony_ci#endif
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/* Check that both hosts are within same datacenter. For this example
498c2ecf20Sopenharmony_ci	 * it is the case when the first 5.5 bytes of their IPv6 addresses are
508c2ecf20Sopenharmony_ci	 * the same.
518c2ecf20Sopenharmony_ci	 */
528c2ecf20Sopenharmony_ci	if (skops->family == AF_INET6 &&
538c2ecf20Sopenharmony_ci	    skops->local_ip6[0] == skops->remote_ip6[0] &&
548c2ecf20Sopenharmony_ci	    (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
558c2ecf20Sopenharmony_ci	    (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
568c2ecf20Sopenharmony_ci		switch (op) {
578c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_TIMEOUT_INIT:
588c2ecf20Sopenharmony_ci			rv = to_init;
598c2ecf20Sopenharmony_ci			break;
608c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_TCP_CONNECT_CB:
618c2ecf20Sopenharmony_ci			/* Set sndbuf and rcvbuf of active connections */
628c2ecf20Sopenharmony_ci			rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF,
638c2ecf20Sopenharmony_ci					    &bufsize, sizeof(bufsize));
648c2ecf20Sopenharmony_ci			rv += bpf_setsockopt(skops, SOL_SOCKET,
658c2ecf20Sopenharmony_ci					     SO_RCVBUF, &bufsize,
668c2ecf20Sopenharmony_ci					     sizeof(bufsize));
678c2ecf20Sopenharmony_ci			break;
688c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
698c2ecf20Sopenharmony_ci			rv = bpf_setsockopt(skops, SOL_TCP,
708c2ecf20Sopenharmony_ci					    TCP_BPF_SNDCWND_CLAMP,
718c2ecf20Sopenharmony_ci					    &clamp, sizeof(clamp));
728c2ecf20Sopenharmony_ci			break;
738c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
748c2ecf20Sopenharmony_ci			/* Set sndbuf and rcvbuf of passive connections */
758c2ecf20Sopenharmony_ci			rv = bpf_setsockopt(skops, SOL_TCP,
768c2ecf20Sopenharmony_ci					    TCP_BPF_SNDCWND_CLAMP,
778c2ecf20Sopenharmony_ci					    &clamp, sizeof(clamp));
788c2ecf20Sopenharmony_ci			rv += bpf_setsockopt(skops, SOL_SOCKET,
798c2ecf20Sopenharmony_ci					     SO_SNDBUF, &bufsize,
808c2ecf20Sopenharmony_ci					     sizeof(bufsize));
818c2ecf20Sopenharmony_ci			rv += bpf_setsockopt(skops, SOL_SOCKET,
828c2ecf20Sopenharmony_ci					     SO_RCVBUF, &bufsize,
838c2ecf20Sopenharmony_ci					     sizeof(bufsize));
848c2ecf20Sopenharmony_ci			break;
858c2ecf20Sopenharmony_ci		default:
868c2ecf20Sopenharmony_ci			rv = -1;
878c2ecf20Sopenharmony_ci		}
888c2ecf20Sopenharmony_ci	} else {
898c2ecf20Sopenharmony_ci		rv = -1;
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci#ifdef DEBUG
928c2ecf20Sopenharmony_ci	bpf_printk("Returning %d\n", rv);
938c2ecf20Sopenharmony_ci#endif
948c2ecf20Sopenharmony_ci	skops->reply = rv;
958c2ecf20Sopenharmony_ci	return 1;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
98