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 congestion control to dctcp when both hosts are
88c2ecf20Sopenharmony_ci * in the same datacenter (as deteremined 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_cong(struct bpf_sock_ops *skops)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	char cong[] = "dctcp";
288c2ecf20Sopenharmony_ci	int rv = 0;
298c2ecf20Sopenharmony_ci	int op;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	/* For testing purposes, only execute rest of BPF program
328c2ecf20Sopenharmony_ci	 * if neither port numberis 55601
338c2ecf20Sopenharmony_ci	 */
348c2ecf20Sopenharmony_ci	if (bpf_ntohl(skops->remote_port) != 55601 &&
358c2ecf20Sopenharmony_ci	    skops->local_port != 55601) {
368c2ecf20Sopenharmony_ci		skops->reply = -1;
378c2ecf20Sopenharmony_ci		return 1;
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	op = (int) skops->op;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#ifdef DEBUG
438c2ecf20Sopenharmony_ci	bpf_printk("BPF command: %d\n", op);
448c2ecf20Sopenharmony_ci#endif
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* Check if both hosts are in the same datacenter. For this
478c2ecf20Sopenharmony_ci	 * example they are if the 1st 5.5 bytes in the IPv6 address
488c2ecf20Sopenharmony_ci	 * are the same.
498c2ecf20Sopenharmony_ci	 */
508c2ecf20Sopenharmony_ci	if (skops->family == AF_INET6 &&
518c2ecf20Sopenharmony_ci	    skops->local_ip6[0] == skops->remote_ip6[0] &&
528c2ecf20Sopenharmony_ci	    (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
538c2ecf20Sopenharmony_ci	    (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
548c2ecf20Sopenharmony_ci		switch (op) {
558c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_NEEDS_ECN:
568c2ecf20Sopenharmony_ci			rv = 1;
578c2ecf20Sopenharmony_ci			break;
588c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
598c2ecf20Sopenharmony_ci			rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION,
608c2ecf20Sopenharmony_ci					    cong, sizeof(cong));
618c2ecf20Sopenharmony_ci			break;
628c2ecf20Sopenharmony_ci		case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
638c2ecf20Sopenharmony_ci			rv = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION,
648c2ecf20Sopenharmony_ci					    cong, sizeof(cong));
658c2ecf20Sopenharmony_ci			break;
668c2ecf20Sopenharmony_ci		default:
678c2ecf20Sopenharmony_ci			rv = -1;
688c2ecf20Sopenharmony_ci		}
698c2ecf20Sopenharmony_ci	} else {
708c2ecf20Sopenharmony_ci		rv = -1;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci#ifdef DEBUG
738c2ecf20Sopenharmony_ci	bpf_printk("Returning %d\n", rv);
748c2ecf20Sopenharmony_ci#endif
758c2ecf20Sopenharmony_ci	skops->reply = rv;
768c2ecf20Sopenharmony_ci	return 1;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
79