tcp_clamp_kern.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. return -1;
  41. op = (int) skops->op;
  42. #ifdef DEBUG
  43. bpf_printk("BPF command: %d\n", op);
  44. #endif
  45. /* Check that both hosts are within same datacenter. For this example
  46. * it is the case when the first 5.5 bytes of their IPv6 addresses are
  47. * the same.
  48. */
  49. if (skops->family == AF_INET6 &&
  50. skops->local_ip6[0] == skops->remote_ip6[0] &&
  51. (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
  52. (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
  53. switch (op) {
  54. case BPF_SOCK_OPS_TIMEOUT_INIT:
  55. rv = to_init;
  56. break;
  57. case BPF_SOCK_OPS_TCP_CONNECT_CB:
  58. /* Set sndbuf and rcvbuf of active connections */
  59. rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF,
  60. &bufsize, sizeof(bufsize));
  61. rv = rv*100 + bpf_setsockopt(skops, SOL_SOCKET,
  62. SO_RCVBUF, &bufsize,
  63. sizeof(bufsize));
  64. break;
  65. case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
  66. rv = bpf_setsockopt(skops, SOL_TCP,
  67. TCP_BPF_SNDCWND_CLAMP,
  68. &clamp, sizeof(clamp));
  69. break;
  70. case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
  71. /* Set sndbuf and rcvbuf of passive connections */
  72. rv = bpf_setsockopt(skops, SOL_TCP,
  73. TCP_BPF_SNDCWND_CLAMP,
  74. &clamp, sizeof(clamp));
  75. rv = rv*100 + bpf_setsockopt(skops, SOL_SOCKET,
  76. SO_SNDBUF, &bufsize,
  77. sizeof(bufsize));
  78. rv = rv*100 + bpf_setsockopt(skops, SOL_SOCKET,
  79. SO_RCVBUF, &bufsize,
  80. sizeof(bufsize));
  81. break;
  82. default:
  83. rv = -1;
  84. }
  85. } else {
  86. rv = -1;
  87. }
  88. #ifdef DEBUG
  89. bpf_printk("Returning %d\n", rv);
  90. #endif
  91. skops->reply = rv;
  92. return 1;
  93. }
  94. char _license[] SEC("license") = "GPL";