bpf: Sample NRM BPF program to limit egress bw
Subsystems (3)
PATCH 7/9 bpf: Sample NRM BPF program to limit egress bw
A cgroup skb BPF program to limit cgroup output bandwidth. It uses a modified virtual token bucket queue to limit average egress bandwidth. The implementation uses credits instead of tokens. Negative credits imply that queueing would have happened (this is a virtual queue, so no queueing is done by it. However, queueing may occur at the actual qdisc (which is not used for rate limiting).
This implementation uses 3 thresholds, one to start marking packets and the other two to drop packets:
CREDIT
- <--------------------------|------------------------> +
| | | 0
| Large pkt |
| drop thresh |
Small pkt drop Mark threshold
thresh
The effect of marking depends on the type of packet:
a) If the packet is ECN enabled and it is a TCP packet, then the packet is ECN marked. The current mark threshold is tuned for DCTCP.
b) If the packet is a TCP packet, then we probabilistically call tcp_cwr to reduce the congestion window. The current implementation uses a linear distribution (0% probability at marking threshold, 100% probability at drop threshold).
c) If the packet is not a TCP packet, then it is dropped.
If the credit is below the drop threshold, the packet is dropped. If it is a TCP packet, then it also calls tcp_cwr since packets dropped by by a cgroup skb BPF program do not automatically trigger a call to tcp_cwr in the current kernel code.
This BPF program actually uses 2 drop thresholds, one threshold for larger packets (>= 120 bytes) and another for smaller packets. This protects smaller packets such as SYNs, ACKs, etc.
The default bandwidth limit is set at 1Gbps but this can be changed by a user program through a shared BPF map. In addition, by default this BPF program does not limit connections using loopback. This behavior can be overwritten by the user program. There is also an option to calculate some statistics, such as percent of packets marked or dropped, which the user program can access.
A latter patch provides such a program (nrm.c)
--- samples/bpf/Makefile | 2 + samples/bpf/nrm.h | 31 ++++++ samples/bpf/nrm_kern.h | 109 +++++++++++++++++++ samples/bpf/nrm_out_kern.c | 213 +++++++++++++++++++++++++++++++++++++ 4 files changed, 355 insertions(+) create mode 100644 samples/bpf/nrm.h create mode 100644 samples/bpf/nrm_kern.h create mode 100644 samples/bpf/nrm_out_kern.c
samples/bpf/Makefile +2 -0
@@ -169,6 +169,7 @@ always += xdpsock_kern.o always += xdp_fwd_kern.o always += task_fd_query_kern.o always += xdp_sample_pkts_kern.o +always += nrm_out_kern.o KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -268,6 +269,7 @@ $(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF) $(src)/*.c: verify_target_bpf $(LIBBPF) $(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h +$(obj)/nrm_out_kern.o: $(src)/nrm.h $(src)/nrm_kern.h # asm/sysreg.h - inline assembly used by it is incompatible with llvm. # But, there is no easy way to fix it, so just exclude it since it is
samples/bpf/nrm.h new file +31 -0
@@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* Copyright (c) 2019 Facebook +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of version 2 of the GNU General Public +* License as published by the Free Software Foundation. +* +* Include file for NRM programs +*/ +struct bpf_vqueue { +struct bpf_spin_lock lock; +/* 4 byte hole */ +unsigned long long lasttime;/* In ns */ +int credit;/* In bytes */ +unsigned int rate;/* In bytes per NS << 20 */ +}; + +struct queue_stats { +unsigned long rate;/* in Mbps*/ +unsigned long stats:1 ,/* get NRM stats (marked, dropped,..) */ + loopback:1 ;/* also limit flows using loopback */ +unsigned long long pkts_marked; +unsigned long long bytes_marked; +unsigned long long pkts_dropped; +unsigned long long bytes_dropped; +unsigned long long pkts_total; +unsigned long long bytes_total; +unsigned long long firstPacketTime; +unsigned long long lastPacketTime; +};
samples/bpf/nrm_kern.h new file +109 -0
109 lines not on this page · raw message
samples/bpf/nrm_out_kern.c new file +213 -0
213 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 15 lines · brakmo
samples/bpf/nrm_kern.h new file +14
-0+ +static __always_inline void get_nrm_pkt_info(struct bpf_sock *sk, +struct nrm_pkt_info *pkti) +{ +if (sk->family ==AF_INET6 || sk->family ==AF_INET ) { + pkti->is_ip = true; + pkti->is_tcp = (sk->protocol ==IPPROTO_TCP ); +if (pkti->is_tcp) { +struct bpf_tcp_sock *tp; + + tp =bpf_tcp_sock (sk); +if (tp) + pkti->ecn = tp->ecn_flags &TCP_ECN_OK ; +else
samples/bpf/nrm_kern.h line 101
Quoted 10 lines
samples/bpf/nrm_kern.h new file +10
-0+ pkti->ecn =0 ; + }else { + pkti->ecn =0 ; + } + }else { + pkti->is_ip = false; + pkti->is_tcp = false; + pkti->ecn =0 ; + } +}
This looks very strange.
ECN capability is per packet, and does not need access to the original TCP socket really.
We definitely can use ECN with UDP packets.
IMO this sample looks like a work in progress.
EDT model allows to implement full shaping (not only virtual one)
by twaking/advancing skb->tstamp
(see commit f11216b24219a bpf: add skb->tstamp r/w access from tc clsact and cg skb progs)
Implementing shaping with the need of accessing TCP sockets seems a layering violation, a lot of things can go wrong with this model.
For instance, you wont be able to offload this.
On 2/19/19, 10:29 AM, "[email protected] on behalf of Eric Dumazet" <[email protected] on behalf of [email protected]> wrote:
On 02/18/2019 09:38 PM, brakmo wrote:
> +-
> +-static __always_inline void get_nrm_pkt_info(struct bpf_sock *sk,
> +- struct nrm_pkt_info *pkti)
> +-{
> +- if (sk->family == AF_INET6 || sk->family == AF_INET) {
> +- pkti->is_ip = true;
> +- pkti->is_tcp = (sk->protocol == IPPROTO_TCP);
> +- if (pkti->is_tcp) {
> +- struct bpf_tcp_sock *tp;
> +-
> +- tp = bpf_tcp_sock(sk);
> +- if (tp)
> +- pkti->ecn = tp->ecn_flags & TCP_ECN_OK;
> +- else
> +- pkti->ecn = 0;
> +- } else {
> +- pkti->ecn = 0;
> +- }
> +- } else {
> +- pkti->is_ip = false;
> +- pkti->is_tcp = false;
> +- pkti->ecn = 0;
> +- }
> +-}
>
This looks very strange.
ECN capability is per packet, and does not need access to the original
TCP socket really.
We definitely can use ECN with UDP packets.
IMO this sample looks like a work in progress.This sample NRM program focuses on TCP, I should have made that more explicit. I originally was checking the ECN bits on the packet, but someone pointed out that since I was focusing on TCP, it would be more efficient to just look at the TCP state (saving having to read the packet header). However, your point is correct in that I could be wrongly marking packets that I should not mark, such as pure ACKs. I will go back to the previous version that looks at the ECN bits in the header and not limit ECN marking to just TCP.
EDT model allows to implement full shaping (not only virtual one)
by twaking/advancing skb->tstamp
(see commit f11216b24219a bpf: add skb->tstamp r/w access from tc clsact and cg skb progs)I have a version of an NRM BPF program that uses EDT, so I know what you mean. However, I decided to send it as a separate patch (including an ingress sample program).
Implementing shaping with the need of accessing TCP sockets seems a layering violation, a lot of things can go wrong with this model.
For instance, you wont be able to offload this.
On the other hand, there are also advantages to having access to the TCP socket. For example, one has more tools to affect the TCP rate (like the proposed helper bpf_tcp_enter_cwr() which does not require dropping the packet). We will need more experience to fully understand the tradeoffs between the various implementations of rate limiting and/or shaping.
Thank you for your feedback.