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 receive window to 40 packets when using IPv6
88c2ecf20Sopenharmony_ci * and the first 5.5 bytes of the IPv6 addresses are not the same (in this
98c2ecf20Sopenharmony_ci * example that means both hosts are not the same datacenter).
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
158c2ecf20Sopenharmony_ci#include <uapi/linux/if_ether.h>
168c2ecf20Sopenharmony_ci#include <uapi/linux/if_packet.h>
178c2ecf20Sopenharmony_ci#include <uapi/linux/ip.h>
188c2ecf20Sopenharmony_ci#include <linux/socket.h>
198c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
208c2ecf20Sopenharmony_ci#include <bpf/bpf_endian.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define DEBUG 1
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciSEC("sockops")
258c2ecf20Sopenharmony_ciint bpf_rwnd(struct bpf_sock_ops *skops)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	int rv = -1;
288c2ecf20Sopenharmony_ci	int op;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	/* For testing purposes, only execute rest of BPF program
318c2ecf20Sopenharmony_ci	 * if neither port numberis 55601
328c2ecf20Sopenharmony_ci	 */
338c2ecf20Sopenharmony_ci	if (bpf_ntohl(skops->remote_port) !=
348c2ecf20Sopenharmony_ci	    55601 && skops->local_port != 55601) {
358c2ecf20Sopenharmony_ci		skops->reply = -1;
368c2ecf20Sopenharmony_ci		return 1;
378c2ecf20Sopenharmony_ci	}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	op = (int) skops->op;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#ifdef DEBUG
428c2ecf20Sopenharmony_ci	bpf_printk("BPF command: %d\n", op);
438c2ecf20Sopenharmony_ci#endif
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* Check for RWND_INIT operation and IPv6 addresses */
468c2ecf20Sopenharmony_ci	if (op == BPF_SOCK_OPS_RWND_INIT &&
478c2ecf20Sopenharmony_ci		skops->family == AF_INET6) {
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci		/* If the first 5.5 bytes of the IPv6 address are not the same
508c2ecf20Sopenharmony_ci		 * then both hosts are not in the same datacenter
518c2ecf20Sopenharmony_ci		 * so use a larger initial advertized window (40 packets)
528c2ecf20Sopenharmony_ci		 */
538c2ecf20Sopenharmony_ci		if (skops->local_ip6[0] != skops->remote_ip6[0] ||
548c2ecf20Sopenharmony_ci		    (bpf_ntohl(skops->local_ip6[1]) & 0xfffff000) !=
558c2ecf20Sopenharmony_ci		    (bpf_ntohl(skops->remote_ip6[1]) & 0xfffff000))
568c2ecf20Sopenharmony_ci			rv = 40;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci#ifdef DEBUG
598c2ecf20Sopenharmony_ci	bpf_printk("Returning %d\n", rv);
608c2ecf20Sopenharmony_ci#endif
618c2ecf20Sopenharmony_ci	skops->reply = rv;
628c2ecf20Sopenharmony_ci	return 1;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
65