tcp_clamp_kern.c 2.7 KB

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