Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 #include <string.h>
0004 #include <linux/tcp.h>
0005 #include <netinet/in.h>
0006 #include <linux/bpf.h>
0007 #include <bpf/bpf_helpers.h>
0008 #include "bpf_tcp_helpers.h"
0009 
0010 char _license[] SEC("license") = "GPL";
0011 
0012 SEC("cgroup/setsockopt")
0013 int sockopt_qos_to_cc(struct bpf_sockopt *ctx)
0014 {
0015     void *optval_end = ctx->optval_end;
0016     int *optval = ctx->optval;
0017     char buf[TCP_CA_NAME_MAX];
0018     char cc_reno[TCP_CA_NAME_MAX] = "reno";
0019     char cc_cubic[TCP_CA_NAME_MAX] = "cubic";
0020 
0021     if (ctx->level != SOL_IPV6 || ctx->optname != IPV6_TCLASS)
0022         return 1;
0023 
0024     if (optval + 1 > optval_end)
0025         return 0; /* EPERM, bounds check */
0026 
0027     if (bpf_getsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf)))
0028         return 0;
0029 
0030     if (!tcp_cc_eq(buf, cc_cubic))
0031         return 0;
0032 
0033     if (*optval == 0x2d) {
0034         if (bpf_setsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &cc_reno,
0035                 sizeof(cc_reno)))
0036             return 0;
0037     }
0038     return 1;
0039 }