tcp_iw_kern.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "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_iw(struct bpf_sock_ops *skops)
  24. {
  25. int bufsize = 1500000;
  26. int rwnd_init = 40;
  27. int iw = 40;
  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 &&
  34. skops->local_port != 55601) {
  35. skops->reply = -1;
  36. return 1;
  37. }
  38. op = (int) skops->op;
  39. #ifdef DEBUG
  40. bpf_printk("BPF command: %d\n", op);
  41. #endif
  42. /* Usually there would be a check to insure the hosts are far
  43. * from each other so it makes sense to increase buffer sizes
  44. */
  45. switch (op) {
  46. case BPF_SOCK_OPS_RWND_INIT:
  47. rv = rwnd_init;
  48. break;
  49. case BPF_SOCK_OPS_TCP_CONNECT_CB:
  50. /* Set sndbuf and rcvbuf of active connections */
  51. rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
  52. sizeof(bufsize));
  53. rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
  54. &bufsize, sizeof(bufsize));
  55. break;
  56. case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
  57. rv = bpf_setsockopt(skops, SOL_TCP, TCP_BPF_IW, &iw,
  58. sizeof(iw));
  59. break;
  60. case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
  61. /* Set sndbuf and rcvbuf of passive connections */
  62. rv = bpf_setsockopt(skops, SOL_SOCKET, SO_SNDBUF, &bufsize,
  63. sizeof(bufsize));
  64. rv += bpf_setsockopt(skops, SOL_SOCKET, SO_RCVBUF,
  65. &bufsize, sizeof(bufsize));
  66. break;
  67. default:
  68. rv = -1;
  69. }
  70. #ifdef DEBUG
  71. bpf_printk("Returning %d\n", rv);
  72. #endif
  73. skops->reply = rv;
  74. return 1;
  75. }
  76. char _license[] SEC("license") = "GPL";