~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/tcp_clamp_kern.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* Copyright (c) 2017 Facebook
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of version 2 of the GNU General Public
  5  * License as published by the Free Software Foundation.
  6  *
  7  * Sample BPF program to set send and receive buffers to 150KB, sndcwnd clamp
  8  * to 100 packets and SYN and SYN_ACK RTOs to 10ms when both hosts are within
  9  * the same datacenter. For his example, we assume they are within the same
 10  * datacenter when the first 5.5 bytes of their IPv6 addresses are the same.
 11  *
 12  * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
 13  */
 14 
 15 #include <uapi/linux/bpf.h>
 16 #include <uapi/linux/if_ether.h>
 17 #include <uapi/linux/if_packet.h>
 18 #include <uapi/linux/ip.h>
 19 #include <linux/socket.h>
 20 #include <bpf/bpf_helpers.h>
 21 #include <bpf/bpf_endian.h>
 22 
 23 #define DEBUG 1
 24 
 25 SEC("sockops")
 26 int bpf_clamp(struct bpf_sock_ops *skops)
 27 {
 28         int bufsize = 150000;
 29         int to_init = 10;
 30         int clamp = 100;
 31         int rv = 0;
 32         int op;
 33 
 34         /* For testing purposes, only execute rest of BPF program
 35          * if neither port numberis 55601
 36          */
 37         if (bpf_ntohl(skops->remote_port) != 55601 && skops->local_port != 55601) {
 38                 skops->reply = -1;
 39                 return 0;
 40         }
 41 
 42         op = (int) skops->op;
 43 
 44 #ifdef DEBUG
 45         bpf_printk("BPF command: %d\n", op);
 46 #endif
 47 
 48         /* Check that both hosts are within same datacenter. For this example
 49          * it is the case when the first 5.5 bytes of their IPv6 addresses are
 50          * the same.
 51          */
 52         if (skops->family == AF_INET6 &&
 53             skops->local_ip6[0] == skops->remote_ip6[0] &&
 54             (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
 55             (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
 56                 switch (op) {
 57                 case BPF_SOCK_OPS_TIMEOUT_INIT:
 58                         rv = to_init;
 59                         break;
 60                 case BPF_SOCK_OPS_TCP_CONNECT_CB:
 61                         /* Set sndbuf and rcvbuf of active connections */
 62                         rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF,
 63                                             &bufsize, sizeof(bufsize));
 64                         rv += bpf_setsockopt(skops, SOL_SOCKET,
 65                                              SO_RCVBUF, &bufsize,
 66                                              sizeof(bufsize));
 67                         break;
 68                 case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
 69                         rv = bpf_setsockopt(skops, SOL_TCP,
 70                                             TCP_BPF_SNDCWND_CLAMP,
 71                                             &clamp, sizeof(clamp));
 72                         break;
 73                 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
 74                         /* Set sndbuf and rcvbuf of passive connections */
 75                         rv = bpf_setsockopt(skops, SOL_TCP,
 76                                             TCP_BPF_SNDCWND_CLAMP,
 77                                             &clamp, sizeof(clamp));
 78                         rv += bpf_setsockopt(skops, SOL_SOCKET,
 79                                              SO_SNDBUF, &bufsize,
 80                                              sizeof(bufsize));
 81                         rv += bpf_setsockopt(skops, SOL_SOCKET,
 82                                              SO_RCVBUF, &bufsize,
 83                                              sizeof(bufsize));
 84                         break;
 85                 default:
 86                         rv = -1;
 87                 }
 88         } else {
 89                 rv = -1;
 90         }
 91 #ifdef DEBUG
 92         bpf_printk("Returning %d\n", rv);
 93 #endif
 94         skops->reply = rv;
 95         return 1;
 96 }
 97 char _license[] SEC("license") = "GPL";
 98 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php