tcp_bufs_kern.c 2.1 KB

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