tcp_iw_kern.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * BPF program to set initial congestion window and initial receive
  8. * window to 40 packets and send and receive buffers to 1.5MB. This
  9. * would usually be done after doing appropriate checks that indicate
  10. * the hosts are far enough away (i.e. large RTT).
  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_iw(struct bpf_sock_ops *skops)
  30. {
  31. int bufsize = 1500000;
  32. int rwnd_init = 40;
  33. int iw = 40;
  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 &&
  40. skops->local_port != 55601) {
  41. skops->reply = -1;
  42. return 1;
  43. }
  44. op = (int) skops->op;
  45. #ifdef DEBUG
  46. bpf_printk("BPF command: %d\n", op);
  47. #endif
  48. /* Usually there would be a check to insure the hosts are far
  49. * from each other so it makes sense to increase buffer sizes
  50. */
  51. switch (op) {
  52. case BPF_SOCK_OPS_RWND_INIT:
  53. rv = rwnd_init;
  54. break;
  55. case BPF_SOCK_OPS_TCP_CONNECT_CB:
  56. /* Set sndbuf and rcvbuf of active connections */
  57. rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
  58. sizeof(bufsize));
  59. rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
  60. &bufsize, sizeof(bufsize));
  61. break;
  62. case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
  63. rv = bpf_setsockopt(skops, SOL_TCP, TCP_BPF_IW, &iw,
  64. sizeof(iw));
  65. break;
  66. case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
  67. /* Set sndbuf and rcvbuf of passive connections */
  68. rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
  69. sizeof(bufsize));
  70. rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
  71. &bufsize, sizeof(bufsize));
  72. break;
  73. default:
  74. rv = -1;
  75. }
  76. #ifdef DEBUG
  77. bpf_printk("Returning %d\n", rv);
  78. #endif
  79. skops->reply = rv;
  80. return 1;
  81. }
  82. char _license[] SEC("license") = "GPL";