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 base_rtt to 80us when host is running TCP-NV and
88c2ecf20Sopenharmony_ci * both hosts are in the same datacenter (as determined by IPv6 prefix).
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
148c2ecf20Sopenharmony_ci#include <uapi/linux/tcp.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_basertt(struct bpf_sock_ops *skops)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	char cong[20];
288c2ecf20Sopenharmony_ci	char nv[] = "nv";
298c2ecf20Sopenharmony_ci	int rv = 0, n;
308c2ecf20Sopenharmony_ci	int op;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	op = (int) skops->op;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#ifdef DEBUG
358c2ecf20Sopenharmony_ci	bpf_printk("BPF command: %d\n", op);
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* Check if both hosts are in the same datacenter. For this
398c2ecf20Sopenharmony_ci	 * example they are if the 1st 5.5 bytes in the IPv6 address
408c2ecf20Sopenharmony_ci	 * are the same.
418c2ecf20Sopenharmony_ci	 */
428c2ecf20Sopenharmony_ci	if (skops->family == AF_INET6 &&
438c2ecf20Sopenharmony_ci	    skops->local_ip6[0] == skops->remote_ip6[0] &&
448c2ecf20Sopenharmony_ci	    (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
458c2ecf20Sopenharmony_ci	    (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
468c2ecf20Sopenharmony_ci		switch (op) {
478c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_BASE_RTT:
488c2ecf20Sopenharmony_ci			n = bpf_getsockopt(skops, SOL_TCP, TCP_CONGESTION,
498c2ecf20Sopenharmony_ci					   cong, sizeof(cong));
508c2ecf20Sopenharmony_ci			if (!n && !__builtin_memcmp(cong, nv, sizeof(nv))) {
518c2ecf20Sopenharmony_ci				/* Set base_rtt to 80us */
528c2ecf20Sopenharmony_ci				rv = 80;
538c2ecf20Sopenharmony_ci			} else if (n) {
548c2ecf20Sopenharmony_ci				rv = n;
558c2ecf20Sopenharmony_ci			} else {
568c2ecf20Sopenharmony_ci				rv = -1;
578c2ecf20Sopenharmony_ci			}
588c2ecf20Sopenharmony_ci			break;
598c2ecf20Sopenharmony_ci		default:
608c2ecf20Sopenharmony_ci			rv = -1;
618c2ecf20Sopenharmony_ci		}
628c2ecf20Sopenharmony_ci	} else {
638c2ecf20Sopenharmony_ci		rv = -1;
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci#ifdef DEBUG
668c2ecf20Sopenharmony_ci	bpf_printk("Returning %d\n", rv);
678c2ecf20Sopenharmony_ci#endif
688c2ecf20Sopenharmony_ci	skops->reply = rv;
698c2ecf20Sopenharmony_ci	return 1;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
72