1187d0738Sbrakmo // SPDX-License-Identifier: GPL-2.0
2187d0738Sbrakmo /* Copyright (c) 2019 Facebook
3187d0738Sbrakmo *
4187d0738Sbrakmo * This program is free software; you can redistribute it and/or
5187d0738Sbrakmo * modify it under the terms of version 2 of the GNU General Public
6187d0738Sbrakmo * License as published by the Free Software Foundation.
7187d0738Sbrakmo *
8187d0738Sbrakmo * Sample Host Bandwidth Manager (HBM) BPF program.
9187d0738Sbrakmo *
10187d0738Sbrakmo * A cgroup skb BPF egress program to limit cgroup output bandwidth.
11187d0738Sbrakmo * It uses a modified virtual token bucket queue to limit average
12187d0738Sbrakmo * egress bandwidth. The implementation uses credits instead of tokens.
13187d0738Sbrakmo * Negative credits imply that queueing would have happened (this is
14187d0738Sbrakmo * a virtual queue, so no queueing is done by it. However, queueing may
15187d0738Sbrakmo * occur at the actual qdisc (which is not used for rate limiting).
16187d0738Sbrakmo *
17187d0738Sbrakmo * This implementation uses 3 thresholds, one to start marking packets and
18187d0738Sbrakmo * the other two to drop packets:
19187d0738Sbrakmo * CREDIT
20187d0738Sbrakmo * - <--------------------------|------------------------> +
21187d0738Sbrakmo * | | | 0
22187d0738Sbrakmo * | Large pkt |
23187d0738Sbrakmo * | drop thresh |
24187d0738Sbrakmo * Small pkt drop Mark threshold
25187d0738Sbrakmo * thresh
26187d0738Sbrakmo *
27187d0738Sbrakmo * The effect of marking depends on the type of packet:
28187d0738Sbrakmo * a) If the packet is ECN enabled and it is a TCP packet, then the packet
29187d0738Sbrakmo * is ECN marked.
30187d0738Sbrakmo * b) If the packet is a TCP packet, then we probabilistically call tcp_cwr
31187d0738Sbrakmo * to reduce the congestion window. The current implementation uses a linear
32187d0738Sbrakmo * distribution (0% probability at marking threshold, 100% probability
33187d0738Sbrakmo * at drop threshold).
34187d0738Sbrakmo * c) If the packet is not a TCP packet, then it is dropped.
35187d0738Sbrakmo *
36187d0738Sbrakmo * If the credit is below the drop threshold, the packet is dropped. If it
37187d0738Sbrakmo * is a TCP packet, then it also calls tcp_cwr since packets dropped by
38187d0738Sbrakmo * by a cgroup skb BPF program do not automatically trigger a call to
39187d0738Sbrakmo * tcp_cwr in the current kernel code.
40187d0738Sbrakmo *
41187d0738Sbrakmo * This BPF program actually uses 2 drop thresholds, one threshold
42187d0738Sbrakmo * for larger packets (>= 120 bytes) and another for smaller packets. This
43187d0738Sbrakmo * protects smaller packets such as SYNs, ACKs, etc.
44187d0738Sbrakmo *
45187d0738Sbrakmo * The default bandwidth limit is set at 1Gbps but this can be changed by
46187d0738Sbrakmo * a user program through a shared BPF map. In addition, by default this BPF
47187d0738Sbrakmo * program does not limit connections using loopback. This behavior can be
48187d0738Sbrakmo * overwritten by the user program. There is also an option to calculate
49187d0738Sbrakmo * some statistics, such as percent of packets marked or dropped, which
50187d0738Sbrakmo * the user program can access.
51187d0738Sbrakmo *
52187d0738Sbrakmo * A latter patch provides such a program (hbm.c)
53187d0738Sbrakmo */
54187d0738Sbrakmo
55187d0738Sbrakmo #include "hbm_kern.h"
56187d0738Sbrakmo
57187d0738Sbrakmo SEC("cgroup_skb/egress")
_hbm_out_cg(struct __sk_buff * skb)58187d0738Sbrakmo int _hbm_out_cg(struct __sk_buff *skb)
59187d0738Sbrakmo {
60187d0738Sbrakmo struct hbm_pkt_info pkti;
61187d0738Sbrakmo int len = skb->len;
62187d0738Sbrakmo unsigned int queue_index = 0;
63187d0738Sbrakmo unsigned long long curtime;
64187d0738Sbrakmo int credit;
65*d58c6f72Sbrakmo signed long long delta = 0, new_credit;
66187d0738Sbrakmo int max_credit = MAX_CREDIT;
67187d0738Sbrakmo bool congestion_flag = false;
68187d0738Sbrakmo bool drop_flag = false;
69187d0738Sbrakmo bool cwr_flag = false;
70*d58c6f72Sbrakmo bool ecn_ce_flag = false;
71187d0738Sbrakmo struct hbm_vqueue *qdp;
72187d0738Sbrakmo struct hbm_queue_stats *qsp = NULL;
73187d0738Sbrakmo int rv = ALLOW_PKT;
74187d0738Sbrakmo
75187d0738Sbrakmo qsp = bpf_map_lookup_elem(&queue_stats, &queue_index);
76187d0738Sbrakmo if (qsp != NULL && !qsp->loopback && (skb->ifindex == 1))
77187d0738Sbrakmo return ALLOW_PKT;
78187d0738Sbrakmo
79187d0738Sbrakmo hbm_get_pkt_info(skb, &pkti);
80187d0738Sbrakmo
81187d0738Sbrakmo // We may want to account for the length of headers in len
82187d0738Sbrakmo // calculation, like ETH header + overhead, specially if it
83187d0738Sbrakmo // is a gso packet. But I am not doing it right now.
84187d0738Sbrakmo
85187d0738Sbrakmo qdp = bpf_get_local_storage(&queue_state, 0);
86187d0738Sbrakmo if (!qdp)
87187d0738Sbrakmo return ALLOW_PKT;
88187d0738Sbrakmo else if (qdp->lasttime == 0)
89187d0738Sbrakmo hbm_init_vqueue(qdp, 1024);
90187d0738Sbrakmo
91187d0738Sbrakmo curtime = bpf_ktime_get_ns();
92187d0738Sbrakmo
93187d0738Sbrakmo // Begin critical section
94187d0738Sbrakmo bpf_spin_lock(&qdp->lock);
95187d0738Sbrakmo credit = qdp->credit;
96187d0738Sbrakmo delta = curtime - qdp->lasttime;
97187d0738Sbrakmo /* delta < 0 implies that another process with a curtime greater
98187d0738Sbrakmo * than ours beat us to the critical section and already added
99187d0738Sbrakmo * the new credit, so we should not add it ourselves
100187d0738Sbrakmo */
101187d0738Sbrakmo if (delta > 0) {
102187d0738Sbrakmo qdp->lasttime = curtime;
103*d58c6f72Sbrakmo new_credit = credit + CREDIT_PER_NS(delta, qdp->rate);
104*d58c6f72Sbrakmo if (new_credit > MAX_CREDIT)
105187d0738Sbrakmo credit = MAX_CREDIT;
106*d58c6f72Sbrakmo else
107*d58c6f72Sbrakmo credit = new_credit;
108187d0738Sbrakmo }
109187d0738Sbrakmo credit -= len;
110187d0738Sbrakmo qdp->credit = credit;
111187d0738Sbrakmo bpf_spin_unlock(&qdp->lock);
112187d0738Sbrakmo // End critical section
113187d0738Sbrakmo
114187d0738Sbrakmo // Check if we should update rate
115187d0738Sbrakmo if (qsp != NULL && (qsp->rate * 128) != qdp->rate) {
116187d0738Sbrakmo qdp->rate = qsp->rate * 128;
117187d0738Sbrakmo bpf_printk("Updating rate: %d (1sec:%llu bits)\n",
118187d0738Sbrakmo (int)qdp->rate,
119187d0738Sbrakmo CREDIT_PER_NS(1000000000, qdp->rate) * 8);
120187d0738Sbrakmo }
121187d0738Sbrakmo
122187d0738Sbrakmo // Set flags (drop, congestion, cwr)
123187d0738Sbrakmo // Dropping => we are congested, so ignore congestion flag
124187d0738Sbrakmo if (credit < -DROP_THRESH ||
125ffd81558Sbrakmo (len > LARGE_PKT_THRESH && credit < -LARGE_PKT_DROP_THRESH)) {
126ffd81558Sbrakmo // Very congested, set drop packet
127187d0738Sbrakmo drop_flag = true;
128ffd81558Sbrakmo if (pkti.ecn)
129ffd81558Sbrakmo congestion_flag = true;
130ffd81558Sbrakmo else if (pkti.is_tcp)
131ffd81558Sbrakmo cwr_flag = true;
132187d0738Sbrakmo } else if (credit < 0) {
133187d0738Sbrakmo // Congested, set congestion flag
134ffd81558Sbrakmo if (pkti.ecn || pkti.is_tcp) {
135187d0738Sbrakmo if (credit < -MARK_THRESH)
136187d0738Sbrakmo congestion_flag = true;
137187d0738Sbrakmo else
138187d0738Sbrakmo congestion_flag = false;
139187d0738Sbrakmo } else {
140187d0738Sbrakmo congestion_flag = true;
141187d0738Sbrakmo }
142187d0738Sbrakmo }
143187d0738Sbrakmo
144187d0738Sbrakmo if (congestion_flag) {
145*d58c6f72Sbrakmo if (bpf_skb_ecn_set_ce(skb)) {
146*d58c6f72Sbrakmo ecn_ce_flag = true;
147*d58c6f72Sbrakmo } else {
148ffd81558Sbrakmo if (pkti.is_tcp) {
149ffd81558Sbrakmo unsigned int rand = bpf_get_prandom_u32();
150ffd81558Sbrakmo
151ffd81558Sbrakmo if (-credit >= MARK_THRESH +
152ffd81558Sbrakmo (rand % MARK_REGION_SIZE)) {
153ffd81558Sbrakmo // Do congestion control
154ffd81558Sbrakmo cwr_flag = true;
155ffd81558Sbrakmo }
156ffd81558Sbrakmo } else if (len > LARGE_PKT_THRESH) {
157187d0738Sbrakmo // Problem if too many small packets?
158187d0738Sbrakmo drop_flag = true;
159187d0738Sbrakmo }
160187d0738Sbrakmo }
161187d0738Sbrakmo }
162187d0738Sbrakmo
163ffd81558Sbrakmo if (qsp != NULL)
164ffd81558Sbrakmo if (qsp->no_cn)
165ffd81558Sbrakmo cwr_flag = false;
166187d0738Sbrakmo
167*d58c6f72Sbrakmo hbm_update_stats(qsp, len, curtime, congestion_flag, drop_flag,
168*d58c6f72Sbrakmo cwr_flag, ecn_ce_flag, &pkti, credit);
169187d0738Sbrakmo
170*d58c6f72Sbrakmo if (drop_flag) {
171187d0738Sbrakmo __sync_add_and_fetch(&(qdp->credit), len);
172*d58c6f72Sbrakmo rv = DROP_PKT;
173*d58c6f72Sbrakmo }
174187d0738Sbrakmo
175ffd81558Sbrakmo if (cwr_flag)
176ffd81558Sbrakmo rv |= 2;
177187d0738Sbrakmo return rv;
178187d0738Sbrakmo }
179187d0738Sbrakmo char _license[] SEC("license") = "GPL";
180