hbm_edt_kern.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * Sample Host Bandwidth Manager (HBM) BPF program.
  9. *
  10. * A cgroup skb BPF egress program to limit cgroup output bandwidth.
  11. * It uses a modified virtual token bucket queue to limit average
  12. * egress bandwidth. The implementation uses credits instead of tokens.
  13. * Negative credits imply that queueing would have happened (this is
  14. * a virtual queue, so no queueing is done by it. However, queueing may
  15. * occur at the actual qdisc (which is not used for rate limiting).
  16. *
  17. * This implementation uses 3 thresholds, one to start marking packets and
  18. * the other two to drop packets:
  19. * CREDIT
  20. * - <--------------------------|------------------------> +
  21. * | | | 0
  22. * | Large pkt |
  23. * | drop thresh |
  24. * Small pkt drop Mark threshold
  25. * thresh
  26. *
  27. * The effect of marking depends on the type of packet:
  28. * a) If the packet is ECN enabled and it is a TCP packet, then the packet
  29. * is ECN marked.
  30. * b) If the packet is a TCP packet, then we probabilistically call tcp_cwr
  31. * to reduce the congestion window. The current implementation uses a linear
  32. * distribution (0% probability at marking threshold, 100% probability
  33. * at drop threshold).
  34. * c) If the packet is not a TCP packet, then it is dropped.
  35. *
  36. * If the credit is below the drop threshold, the packet is dropped. If it
  37. * is a TCP packet, then it also calls tcp_cwr since packets dropped by
  38. * by a cgroup skb BPF program do not automatically trigger a call to
  39. * tcp_cwr in the current kernel code.
  40. *
  41. * This BPF program actually uses 2 drop thresholds, one threshold
  42. * for larger packets (>= 120 bytes) and another for smaller packets. This
  43. * protects smaller packets such as SYNs, ACKs, etc.
  44. *
  45. * The default bandwidth limit is set at 1Gbps but this can be changed by
  46. * a user program through a shared BPF map. In addition, by default this BPF
  47. * program does not limit connections using loopback. This behavior can be
  48. * overwritten by the user program. There is also an option to calculate
  49. * some statistics, such as percent of packets marked or dropped, which
  50. * a user program, such as hbm, can access.
  51. */
  52. #include "hbm_kern.h"
  53. SEC("cgroup_skb/egress")
  54. int _hbm_out_cg(struct __sk_buff *skb)
  55. {
  56. long long delta = 0, delta_send;
  57. unsigned long long curtime, sendtime;
  58. struct hbm_queue_stats *qsp = NULL;
  59. unsigned int queue_index = 0;
  60. bool congestion_flag = false;
  61. bool ecn_ce_flag = false;
  62. struct hbm_pkt_info pkti = {};
  63. struct hbm_vqueue *qdp;
  64. bool drop_flag = false;
  65. bool cwr_flag = false;
  66. int len = skb->len;
  67. int rv = ALLOW_PKT;
  68. qsp = bpf_map_lookup_elem(&queue_stats, &queue_index);
  69. // Check if we should ignore loopback traffic
  70. if (qsp != NULL && !qsp->loopback && (skb->ifindex == 1))
  71. return ALLOW_PKT;
  72. hbm_get_pkt_info(skb, &pkti);
  73. // We may want to account for the length of headers in len
  74. // calculation, like ETH header + overhead, specially if it
  75. // is a gso packet. But I am not doing it right now.
  76. qdp = bpf_get_local_storage(&queue_state, 0);
  77. if (!qdp)
  78. return ALLOW_PKT;
  79. if (qdp->lasttime == 0)
  80. hbm_init_edt_vqueue(qdp, 1024);
  81. curtime = bpf_ktime_get_ns();
  82. // Begin critical section
  83. bpf_spin_lock(&qdp->lock);
  84. delta = qdp->lasttime - curtime;
  85. // bound bursts to 100us
  86. if (delta < -BURST_SIZE_NS) {
  87. // negative delta is a credit that allows bursts
  88. qdp->lasttime = curtime - BURST_SIZE_NS;
  89. delta = -BURST_SIZE_NS;
  90. }
  91. sendtime = qdp->lasttime;
  92. delta_send = BYTES_TO_NS(len, qdp->rate);
  93. __sync_add_and_fetch(&(qdp->lasttime), delta_send);
  94. bpf_spin_unlock(&qdp->lock);
  95. // End critical section
  96. // Set EDT of packet
  97. skb->tstamp = sendtime;
  98. // Check if we should update rate
  99. if (qsp != NULL && (qsp->rate * 128) != qdp->rate)
  100. qdp->rate = qsp->rate * 128;
  101. // Set flags (drop, congestion, cwr)
  102. // last packet will be sent in the future, bound latency
  103. if (delta > DROP_THRESH_NS || (delta > LARGE_PKT_DROP_THRESH_NS &&
  104. len > LARGE_PKT_THRESH)) {
  105. drop_flag = true;
  106. if (pkti.is_tcp && pkti.ecn == 0)
  107. cwr_flag = true;
  108. } else if (delta > MARK_THRESH_NS) {
  109. if (pkti.is_tcp)
  110. congestion_flag = true;
  111. else
  112. drop_flag = true;
  113. }
  114. if (congestion_flag) {
  115. if (bpf_skb_ecn_set_ce(skb)) {
  116. ecn_ce_flag = true;
  117. } else {
  118. if (pkti.is_tcp) {
  119. unsigned int rand = bpf_get_prandom_u32();
  120. if (delta >= MARK_THRESH_NS +
  121. (rand % MARK_REGION_SIZE_NS)) {
  122. // Do congestion control
  123. cwr_flag = true;
  124. }
  125. } else if (len > LARGE_PKT_THRESH) {
  126. // Problem if too many small packets?
  127. drop_flag = true;
  128. congestion_flag = false;
  129. }
  130. }
  131. }
  132. if (pkti.is_tcp && drop_flag && pkti.packets_out <= 1) {
  133. drop_flag = false;
  134. cwr_flag = true;
  135. congestion_flag = false;
  136. }
  137. if (qsp != NULL && qsp->no_cn)
  138. cwr_flag = false;
  139. hbm_update_stats(qsp, len, curtime, congestion_flag, drop_flag,
  140. cwr_flag, ecn_ce_flag, &pkti, (int) delta);
  141. if (drop_flag) {
  142. __sync_add_and_fetch(&(qdp->lasttime), -delta_send);
  143. rv = DROP_PKT;
  144. }
  145. if (cwr_flag)
  146. rv |= CWR;
  147. return rv;
  148. }
  149. char _license[] SEC("license") = "GPL";