// SPDX-License-Identifier: GPL-2.01/* Copyright (c) 2019 Facebook2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of version 2 of the GNU General Public5* License as published by the Free Software Foundation.6*7* Sample Host Bandwidth Manager (HBM) BPF program.8*9* A cgroup skb BPF egress program to limit cgroup output bandwidth.10* It uses a modified virtual token bucket queue to limit average11* egress bandwidth. The implementation uses credits instead of tokens.12* Negative credits imply that queueing would have happened (this is13* a virtual queue, so no queueing is done by it. However, queueing may14* occur at the actual qdisc (which is not used for rate limiting).15*16* This implementation uses 3 thresholds, one to start marking packets and17* the other two to drop packets:18* CREDIT19* - <--------------------------|------------------------> +20* | | | 021* | Large pkt |22* | drop thresh |23* Small pkt drop Mark threshold24* thresh25*26* The effect of marking depends on the type of packet:27* a) If the packet is ECN enabled and it is a TCP packet, then the packet28* is ECN marked.29* b) If the packet is a TCP packet, then we probabilistically call tcp_cwr30* to reduce the congestion window. The current implementation uses a linear31* distribution (0% probability at marking threshold, 100% probability32* at drop threshold).33* c) If the packet is not a TCP packet, then it is dropped.34*35* If the credit is below the drop threshold, the packet is dropped. If it36* is a TCP packet, then it also calls tcp_cwr since packets dropped by37* a cgroup skb BPF program do not automatically trigger a call to38* tcp_cwr in the current kernel code.39*40* This BPF program actually uses 2 drop thresholds, one threshold41* for larger packets (>= 120 bytes) and another for smaller packets. This42* protects smaller packets such as SYNs, ACKs, etc.43*44* The default bandwidth limit is set at 1Gbps but this can be changed by45* a user program through a shared BPF map. In addition, by default this BPF46* program does not limit connections using loopback. This behavior can be47* overwritten by the user program. There is also an option to calculate48* some statistics, such as percent of packets marked or dropped, which49* a user program, such as hbm, can access.50*/5152#include "hbm_kern.h"5354SEC("cgroup_skb/egress")55int _hbm_out_cg(struct __sk_buff *skb)56{57long long delta = 0, delta_send;58unsigned long long curtime, sendtime;59struct hbm_queue_stats *qsp = NULL;60unsigned int queue_index = 0;61bool congestion_flag = false;62bool ecn_ce_flag = false;63struct hbm_pkt_info pkti = {};64struct hbm_vqueue *qdp;65bool drop_flag = false;66bool cwr_flag = false;67int len = skb->len;68int rv = ALLOW_PKT;6970qsp = bpf_map_lookup_elem(&queue_stats, &queue_index);7172// Check if we should ignore loopback traffic73if (qsp != NULL && !qsp->loopback && (skb->ifindex == 1))74return ALLOW_PKT;7576hbm_get_pkt_info(skb, &pkti);7778// We may want to account for the length of headers in len79// calculation, like ETH header + overhead, specially if it80// is a gso packet. But I am not doing it right now.8182qdp = bpf_get_local_storage(&queue_state, 0);83if (!qdp)84return ALLOW_PKT;85if (qdp->lasttime == 0)86hbm_init_edt_vqueue(qdp, 1024);8788curtime = bpf_ktime_get_ns();8990// Begin critical section91bpf_spin_lock(&qdp->lock);92delta = qdp->lasttime - curtime;93// bound bursts to 100us94if (delta < -BURST_SIZE_NS) {95// negative delta is a credit that allows bursts96qdp->lasttime = curtime - BURST_SIZE_NS;97delta = -BURST_SIZE_NS;98}99sendtime = qdp->lasttime;100delta_send = BYTES_TO_NS(len, qdp->rate);101__sync_add_and_fetch(&(qdp->lasttime), delta_send);102bpf_spin_unlock(&qdp->lock);103// End critical section104105// Set EDT of packet106skb->tstamp = sendtime;107108// Check if we should update rate109if (qsp != NULL && (qsp->rate * 128) != qdp->rate)110qdp->rate = qsp->rate * 128;111112// Set flags (drop, congestion, cwr)113// last packet will be sent in the future, bound latency114if (delta > DROP_THRESH_NS || (delta > LARGE_PKT_DROP_THRESH_NS &&115len > LARGE_PKT_THRESH)) {116drop_flag = true;117if (pkti.is_tcp && pkti.ecn == 0)118cwr_flag = true;119} else if (delta > MARK_THRESH_NS) {120if (pkti.is_tcp)121congestion_flag = true;122else123drop_flag = true;124}125126if (congestion_flag) {127if (bpf_skb_ecn_set_ce(skb)) {128ecn_ce_flag = true;129} else {130if (pkti.is_tcp) {131unsigned int rand = bpf_get_prandom_u32();132133if (delta >= MARK_THRESH_NS +134(rand % MARK_REGION_SIZE_NS)) {135// Do congestion control136cwr_flag = true;137}138} else if (len > LARGE_PKT_THRESH) {139// Problem if too many small packets?140drop_flag = true;141congestion_flag = false;142}143}144}145146if (pkti.is_tcp && drop_flag && pkti.packets_out <= 1) {147drop_flag = false;148cwr_flag = true;149congestion_flag = false;150}151152if (qsp != NULL && qsp->no_cn)153cwr_flag = false;154155hbm_update_stats(qsp, len, curtime, congestion_flag, drop_flag,156cwr_flag, ecn_ce_flag, &pkti, (int) delta);157158if (drop_flag) {159__sync_add_and_fetch(&(qdp->lasttime), -delta_send);160rv = DROP_PKT;161}162163if (cwr_flag)164rv |= CWR;165return rv;166}167char _license[] SEC("license") = "GPL";168169170