tcp_basertt_kern.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 base_rtt to 80us when host is running TCP-NV and
  8. * both hosts are in the same datacenter (as determined by IPv6 prefix).
  9. *
  10. * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
  11. */
  12. #include <uapi/linux/bpf.h>
  13. #include <uapi/linux/tcp.h>
  14. #include <uapi/linux/if_ether.h>
  15. #include <uapi/linux/if_packet.h>
  16. #include <uapi/linux/ip.h>
  17. #include <linux/socket.h>
  18. #include <bpf/bpf_helpers.h>
  19. #include <bpf/bpf_endian.h>
  20. #define DEBUG 1
  21. SEC("sockops")
  22. int bpf_basertt(struct bpf_sock_ops *skops)
  23. {
  24. char cong[20];
  25. char nv[] = "nv";
  26. int rv = 0, n;
  27. int op;
  28. op = (int) skops->op;
  29. #ifdef DEBUG
  30. bpf_printk("BPF command: %d\n", op);
  31. #endif
  32. /* Check if both hosts are in the same datacenter. For this
  33. * example they are if the 1st 5.5 bytes in the IPv6 address
  34. * are the same.
  35. */
  36. if (skops->family == AF_INET6 &&
  37. skops->local_ip6[0] == skops->remote_ip6[0] &&
  38. (bpf_ntohl(skops->local_ip6[1]) & 0xfff00000) ==
  39. (bpf_ntohl(skops->remote_ip6[1]) & 0xfff00000)) {
  40. switch (op) {
  41. case BPF_SOCK_OPS_BASE_RTT:
  42. n = bpf_getsockopt(skops, SOL_TCP, TCP_CONGESTION,
  43. cong, sizeof(cong));
  44. if (!n && !__builtin_memcmp(cong, nv, sizeof(nv)+1)) {
  45. /* Set base_rtt to 80us */
  46. rv = 80;
  47. } else if (n) {
  48. rv = n;
  49. } else {
  50. rv = -1;
  51. }
  52. break;
  53. default:
  54. rv = -1;
  55. }
  56. } else {
  57. rv = -1;
  58. }
  59. #ifdef DEBUG
  60. bpf_printk("Returning %d\n", rv);
  61. #endif
  62. skops->reply = rv;
  63. return 1;
  64. }
  65. char _license[] SEC("license") = "GPL";