Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2019 Facebook
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of version 2 of the GNU General Public
0006  * License as published by the Free Software Foundation.
0007  *
0008  * Sample Host Bandwidth Manager (HBM) BPF program.
0009  *
0010  * A cgroup skb BPF egress program to limit cgroup output bandwidth.
0011  * It uses a modified virtual token bucket queue to limit average
0012  * egress bandwidth. The implementation uses credits instead of tokens.
0013  * Negative credits imply that queueing would have happened (this is
0014  * a virtual queue, so no queueing is done by it. However, queueing may
0015  * occur at the actual qdisc (which is not used for rate limiting).
0016  *
0017  * This implementation uses 3 thresholds, one to start marking packets and
0018  * the other two to drop packets:
0019  *                                  CREDIT
0020  *        - <--------------------------|------------------------> +
0021  *              |    |          |      0
0022  *              |  Large pkt    |
0023  *              |  drop thresh  |
0024  *   Small pkt drop             Mark threshold
0025  *       thresh
0026  *
0027  * The effect of marking depends on the type of packet:
0028  * a) If the packet is ECN enabled and it is a TCP packet, then the packet
0029  *    is ECN marked.
0030  * b) If the packet is a TCP packet, then we probabilistically call tcp_cwr
0031  *    to reduce the congestion window. The current implementation uses a linear
0032  *    distribution (0% probability at marking threshold, 100% probability
0033  *    at drop threshold).
0034  * c) If the packet is not a TCP packet, then it is dropped.
0035  *
0036  * If the credit is below the drop threshold, the packet is dropped. If it
0037  * is a TCP packet, then it also calls tcp_cwr since packets dropped by
0038  * by a cgroup skb BPF program do not automatically trigger a call to
0039  * tcp_cwr in the current kernel code.
0040  *
0041  * This BPF program actually uses 2 drop thresholds, one threshold
0042  * for larger packets (>= 120 bytes) and another for smaller packets. This
0043  * protects smaller packets such as SYNs, ACKs, etc.
0044  *
0045  * The default bandwidth limit is set at 1Gbps but this can be changed by
0046  * a user program through a shared BPF map. In addition, by default this BPF
0047  * program does not limit connections using loopback. This behavior can be
0048  * overwritten by the user program. There is also an option to calculate
0049  * some statistics, such as percent of packets marked or dropped, which
0050  * a user program, such as hbm, can access.
0051  */
0052 
0053 #include "hbm_kern.h"
0054 
0055 SEC("cgroup_skb/egress")
0056 int _hbm_out_cg(struct __sk_buff *skb)
0057 {
0058     long long delta = 0, delta_send;
0059     unsigned long long curtime, sendtime;
0060     struct hbm_queue_stats *qsp = NULL;
0061     unsigned int queue_index = 0;
0062     bool congestion_flag = false;
0063     bool ecn_ce_flag = false;
0064     struct hbm_pkt_info pkti = {};
0065     struct hbm_vqueue *qdp;
0066     bool drop_flag = false;
0067     bool cwr_flag = false;
0068     int len = skb->len;
0069     int rv = ALLOW_PKT;
0070 
0071     qsp = bpf_map_lookup_elem(&queue_stats, &queue_index);
0072 
0073     // Check if we should ignore loopback traffic
0074     if (qsp != NULL && !qsp->loopback && (skb->ifindex == 1))
0075         return ALLOW_PKT;
0076 
0077     hbm_get_pkt_info(skb, &pkti);
0078 
0079     // We may want to account for the length of headers in len
0080     // calculation, like ETH header + overhead, specially if it
0081     // is a gso packet. But I am not doing it right now.
0082 
0083     qdp = bpf_get_local_storage(&queue_state, 0);
0084     if (!qdp)
0085         return ALLOW_PKT;
0086     if (qdp->lasttime == 0)
0087         hbm_init_edt_vqueue(qdp, 1024);
0088 
0089     curtime = bpf_ktime_get_ns();
0090 
0091     // Begin critical section
0092     bpf_spin_lock(&qdp->lock);
0093     delta = qdp->lasttime - curtime;
0094     // bound bursts to 100us
0095     if (delta < -BURST_SIZE_NS) {
0096         // negative delta is a credit that allows bursts
0097         qdp->lasttime = curtime - BURST_SIZE_NS;
0098         delta = -BURST_SIZE_NS;
0099     }
0100     sendtime = qdp->lasttime;
0101     delta_send = BYTES_TO_NS(len, qdp->rate);
0102     __sync_add_and_fetch(&(qdp->lasttime), delta_send);
0103     bpf_spin_unlock(&qdp->lock);
0104     // End critical section
0105 
0106     // Set EDT of packet
0107     skb->tstamp = sendtime;
0108 
0109     // Check if we should update rate
0110     if (qsp != NULL && (qsp->rate * 128) != qdp->rate)
0111         qdp->rate = qsp->rate * 128;
0112 
0113     // Set flags (drop, congestion, cwr)
0114     // last packet will be sent in the future, bound latency
0115     if (delta > DROP_THRESH_NS || (delta > LARGE_PKT_DROP_THRESH_NS &&
0116                        len > LARGE_PKT_THRESH)) {
0117         drop_flag = true;
0118         if (pkti.is_tcp && pkti.ecn == 0)
0119             cwr_flag = true;
0120     } else if (delta > MARK_THRESH_NS) {
0121         if (pkti.is_tcp)
0122             congestion_flag = true;
0123         else
0124             drop_flag = true;
0125     }
0126 
0127     if (congestion_flag) {
0128         if (bpf_skb_ecn_set_ce(skb)) {
0129             ecn_ce_flag = true;
0130         } else {
0131             if (pkti.is_tcp) {
0132                 unsigned int rand = bpf_get_prandom_u32();
0133 
0134                 if (delta >= MARK_THRESH_NS +
0135                     (rand % MARK_REGION_SIZE_NS)) {
0136                     // Do congestion control
0137                     cwr_flag = true;
0138                 }
0139             } else if (len > LARGE_PKT_THRESH) {
0140                 // Problem if too many small packets?
0141                 drop_flag = true;
0142                 congestion_flag = false;
0143             }
0144         }
0145     }
0146 
0147     if (pkti.is_tcp && drop_flag && pkti.packets_out <= 1) {
0148         drop_flag = false;
0149         cwr_flag = true;
0150         congestion_flag = false;
0151     }
0152 
0153     if (qsp != NULL && qsp->no_cn)
0154             cwr_flag = false;
0155 
0156     hbm_update_stats(qsp, len, curtime, congestion_flag, drop_flag,
0157              cwr_flag, ecn_ce_flag, &pkti, (int) delta);
0158 
0159     if (drop_flag) {
0160         __sync_add_and_fetch(&(qdp->lasttime), -delta_send);
0161         rv = DROP_PKT;
0162     }
0163 
0164     if (cwr_flag)
0165         rv |= CWR;
0166     return rv;
0167 }
0168 char _license[] SEC("license") = "GPL";