tcp_bufs_kern.c 2.2 KB

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