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 * BPF program to set initial congestion window and initial receive
88c2ecf20Sopenharmony_ci * window to 40 packets and send and receive buffers to 1.5MB. This
98c2ecf20Sopenharmony_ci * would usually be done after doing appropriate checks that indicate
108c2ecf20Sopenharmony_ci * the hosts are far enough away (i.e. large RTT).
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_iw(struct bpf_sock_ops *skops)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	int bufsize = 1500000;
298c2ecf20Sopenharmony_ci	int rwnd_init = 40;
308c2ecf20Sopenharmony_ci	int iw = 40;
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 &&
388c2ecf20Sopenharmony_ci	    skops->local_port != 55601) {
398c2ecf20Sopenharmony_ci		skops->reply = -1;
408c2ecf20Sopenharmony_ci		return 1;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	op = (int) skops->op;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#ifdef DEBUG
468c2ecf20Sopenharmony_ci	bpf_printk("BPF command: %d\n", op);
478c2ecf20Sopenharmony_ci#endif
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/* Usually there would be a check to insure the hosts are far
508c2ecf20Sopenharmony_ci	 * from each other so it makes sense to increase buffer sizes
518c2ecf20Sopenharmony_ci	 */
528c2ecf20Sopenharmony_ci	switch (op) {
538c2ecf20Sopenharmony_ci	case BPF_SOCK_OPS_RWND_INIT:
548c2ecf20Sopenharmony_ci		rv = rwnd_init;
558c2ecf20Sopenharmony_ci		break;
568c2ecf20Sopenharmony_ci	case BPF_SOCK_OPS_TCP_CONNECT_CB:
578c2ecf20Sopenharmony_ci		/* Set sndbuf and rcvbuf of active connections */
588c2ecf20Sopenharmony_ci		rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
598c2ecf20Sopenharmony_ci				    sizeof(bufsize));
608c2ecf20Sopenharmony_ci		rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
618c2ecf20Sopenharmony_ci				     &bufsize, sizeof(bufsize));
628c2ecf20Sopenharmony_ci		break;
638c2ecf20Sopenharmony_ci	case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
648c2ecf20Sopenharmony_ci		rv = bpf_setsockopt(skops, SOL_TCP, TCP_BPF_IW, &iw,
658c2ecf20Sopenharmony_ci				    sizeof(iw));
668c2ecf20Sopenharmony_ci		break;
678c2ecf20Sopenharmony_ci	case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
688c2ecf20Sopenharmony_ci		/* Set sndbuf and rcvbuf of passive connections */
698c2ecf20Sopenharmony_ci		rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
708c2ecf20Sopenharmony_ci				    sizeof(bufsize));
718c2ecf20Sopenharmony_ci		rv +=  bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
728c2ecf20Sopenharmony_ci				      &bufsize, sizeof(bufsize));
738c2ecf20Sopenharmony_ci		break;
748c2ecf20Sopenharmony_ci	default:
758c2ecf20Sopenharmony_ci		rv = -1;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci#ifdef DEBUG
788c2ecf20Sopenharmony_ci	bpf_printk("Returning %d\n", rv);
798c2ecf20Sopenharmony_ci#endif
808c2ecf20Sopenharmony_ci	skops->reply = rv;
818c2ecf20Sopenharmony_ci	return 1;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
84