tcp_bufs_kern.c 2.2 KB

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