tcp_clamp_kern.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 load_sock_ops to load this BPF program.
  13. */
  14. #include <uapi/linux/bpf.h>
  15. #include <uapi/linux/if_ether.h>
  16. #include <uapi/linux/if_packet.h>
  17. #include <uapi/linux/ip.h>
  18. #include <linux/socket.h>
  19. #include "bpf_helpers.h"
  20. #include "bpf_endian.h"
  21. #define DEBUG 1
  22. #define bpf_printk(fmt, ...) \
  23. ({ \
  24. char ____fmt[] = fmt; \
  25. bpf_trace_printk(____fmt, sizeof(____fmt), \
  26. ##__VA_ARGS__); \
  27. })
  28. SEC("sockops")
  29. int bpf_clamp(struct bpf_sock_ops *skops)
  30. {
  31. int bufsize = 150000;
  32. int to_init = 10;
  33. int clamp = 100;
  34. int rv = 0;
  35. int op;
  36. /* For testing purposes, only execute rest of BPF program
  37. * if neither port numberis 55601
  38. */
  39. if (bpf_ntohl(skops->remote_port) != 55601 && skops->local_port != 55601) {
  40. skops->reply = -1;
  41. return 0;
  42. }
  43. op = (int) skops->op;
  44. #ifdef DEBUG
  45. bpf_printk("BPF command: %d\n", op);
  46. #endif
  47. /* Check that both hosts are within same datacenter. For this example
  48. * it is the case when the first 5.5 bytes of their IPv6 addresses are
  49. * the same.
  50. */
  51. if (skops->family == AF_INET6 &&
  52. skops->local_ip6[0] == skops->remote_ip6[0] &&
  53. (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
  54. (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
  55. switch (op) {
  56. case BPF_SOCK_OPS_TIMEOUT_INIT:
  57. rv = to_init;
  58. break;
  59. case BPF_SOCK_OPS_TCP_CONNECT_CB:
  60. /* Set sndbuf and rcvbuf of active connections */
  61. rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF,
  62. &bufsize, sizeof(bufsize));
  63. rv += bpf_setsockopt(skops, SOL_SOCKET,
  64. SO_RCVBUF, &bufsize,
  65. sizeof(bufsize));
  66. break;
  67. case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
  68. rv = bpf_setsockopt(skops, SOL_TCP,
  69. TCP_BPF_SNDCWND_CLAMP,
  70. &clamp, sizeof(clamp));
  71. break;
  72. case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
  73. /* Set sndbuf and rcvbuf of passive connections */
  74. rv = bpf_setsockopt(skops, SOL_TCP,
  75. TCP_BPF_SNDCWND_CLAMP,
  76. &clamp, sizeof(clamp));
  77. rv += bpf_setsockopt(skops, SOL_SOCKET,
  78. SO_SNDBUF, &bufsize,
  79. sizeof(bufsize));
  80. rv += bpf_setsockopt(skops, SOL_SOCKET,
  81. SO_RCVBUF, &bufsize,
  82. sizeof(bufsize));
  83. break;
  84. default:
  85. rv = -1;
  86. }
  87. } else {
  88. rv = -1;
  89. }
  90. #ifdef DEBUG
  91. bpf_printk("Returning %d\n", rv);
  92. #endif
  93. skops->reply = rv;
  94. return 1;
  95. }
  96. char _license[] SEC("license") = "GPL";