naccyde / rpms / iproute

Forked from rpms/iproute 5 months ago
Clone

Blame SOURCES/0005-police-add-support-for-packet-per-second-rate-limiti.patch

74a1de
From b061aeba93b1c730b7dafeece6b90aad2e7afce8 Mon Sep 17 00:00:00 2001
74a1de
Message-Id: <b061aeba93b1c730b7dafeece6b90aad2e7afce8.1628790091.git.aclaudi@redhat.com>
74a1de
In-Reply-To: <650694eb0120722499207078f965442ef7343bb1.1628790091.git.aclaudi@redhat.com>
74a1de
References: <650694eb0120722499207078f965442ef7343bb1.1628790091.git.aclaudi@redhat.com>
74a1de
From: Andrea Claudi <aclaudi@redhat.com>
74a1de
Date: Wed, 11 Aug 2021 12:55:14 +0200
74a1de
Subject: [PATCH] police: add support for packet-per-second rate limiting
74a1de
74a1de
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1981393
74a1de
Upstream Status: iproute2.git commit cf9ae1bd
74a1de
74a1de
commit cf9ae1bd31187d8ae62bc1bb408e443dbc8bd6a0
74a1de
Author: Baowen Zheng <baowen.zheng@corigine.com>
74a1de
Date:   Fri Mar 26 13:50:18 2021 +0100
74a1de
74a1de
    police: add support for packet-per-second rate limiting
74a1de
74a1de
    Allow a policer action to enforce a rate-limit based on packets-per-second,
74a1de
    configurable using a packet-per-second rate and burst parameters.
74a1de
74a1de
    e.g.
74a1de
     # $TC actions add action police pkts_rate 1000 pkts_burst 200 index 1
74a1de
     # $TC actions ls action police
74a1de
     total acts 1
74a1de
74a1de
            action order 0:  police 0x1 rate 0bit burst 0b mtu 4096Mb pkts_rate 1000 pkts_burst 200
74a1de
            ref 1 bind 0
74a1de
74a1de
    Signed-off-by: Baowen Zheng <baowen.zheng@corigine.com>
74a1de
    Signed-off-by: Simon Horman <simon.horman@netronome.com>
74a1de
    Signed-off-by: Louis Peens <louis.peens@netronome.com>
74a1de
    Signed-off-by: David Ahern <dsahern@kernel.org>
74a1de
74a1de
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
74a1de
---
74a1de
 man/man8/tc-police.8 | 35 ++++++++++++++++++++++++-------
74a1de
 tc/m_police.c        | 50 +++++++++++++++++++++++++++++++++++++++++---
74a1de
 2 files changed, 75 insertions(+), 10 deletions(-)
74a1de
74a1de
diff --git a/man/man8/tc-police.8 b/man/man8/tc-police.8
74a1de
index 52279755..86e263bb 100644
74a1de
--- a/man/man8/tc-police.8
74a1de
+++ b/man/man8/tc-police.8
74a1de
@@ -5,9 +5,11 @@ police - policing action
74a1de
 .SH SYNOPSIS
74a1de
 .in +8
74a1de
 .ti -8
74a1de
-.BR tc " ... " "action police"
74a1de
+.BR tc " ... " "action police ["
74a1de
 .BI rate " RATE " burst
74a1de
-.IR BYTES [\fB/ BYTES "] ["
74a1de
+.IR BYTES [\fB/ BYTES "] ] ["
74a1de
+.BI pkts_rate " RATE " pkts_burst
74a1de
+.IR PACKETS "] ["
74a1de
 .B mtu
74a1de
 .IR BYTES [\fB/ BYTES "] ] ["
74a1de
 .BI peakrate " RATE"
74a1de
@@ -34,19 +36,29 @@ police - policing action
74a1de
 .SH DESCRIPTION
74a1de
 The
74a1de
 .B police
74a1de
-action allows to limit bandwidth of traffic matched by the filter it is
74a1de
-attached to. Basically there are two different algorithms available to measure
74a1de
-the packet rate: The first one uses an internal dual token bucket and is
74a1de
-configured using the
74a1de
+action allows limiting of the byte or packet rate of traffic matched by the
74a1de
+filter it is attached to.
74a1de
+.P
74a1de
+There are two different algorithms available to measure the byte rate: The
74a1de
+first one uses an internal dual token bucket and is configured using the
74a1de
 .BR rate ", " burst ", " mtu ", " peakrate ", " overhead " and " linklayer
74a1de
 parameters. The second one uses an in-kernel sampling mechanism. It can be
74a1de
 fine-tuned using the
74a1de
 .B estimator
74a1de
 filter parameter.
74a1de
+.P
74a1de
+There is one algorithm available to measure packet rate and it is similar to
74a1de
+the first algorithm described for byte rate. It is configured using the
74a1de
+.BR pkt_rate " and " pkt_burst
74a1de
+parameters.
74a1de
+.P
74a1de
+At least one of the
74a1de
+.BR rate " and " pkt_rate "
74a1de
+parameters must be configured.
74a1de
 .SH OPTIONS
74a1de
 .TP
74a1de
 .BI rate " RATE"
74a1de
-The maximum traffic rate of packets passing this action. Those exceeding it will
74a1de
+The maximum byte rate of packets passing this action. Those exceeding it will
74a1de
 be treated as defined by the
74a1de
 .B conform-exceed
74a1de
 option.
74a1de
@@ -55,6 +67,15 @@ option.
74a1de
 Set the maximum allowed burst in bytes, optionally followed by a slash ('/')
74a1de
 sign and cell size which must be a power of 2.
74a1de
 .TP
74a1de
+.BI pkt_rate " RATE"
74a1de
+The maximum packet rate or packets passing this action. Those exceeding it will
74a1de
+be treated as defined by the
74a1de
+.B conform-exceed
74a1de
+option.
74a1de
+.TP
74a1de
+.BI pkt_burst " PACKETS"
74a1de
+Set the maximum allowed burst in packets.
74a1de
+.TP
74a1de
 .BI mtu " BYTES\fR[\fB/\fIBYTES\fR]"
74a1de
 This is the maximum packet size handled by the policer (larger ones will be
74a1de
 handled like they exceeded the configured rate). Setting this value correctly
74a1de
diff --git a/tc/m_police.c b/tc/m_police.c
74a1de
index bb51df68..9ef0e40b 100644
74a1de
--- a/tc/m_police.c
74a1de
+++ b/tc/m_police.c
74a1de
@@ -38,7 +38,8 @@ struct action_util police_action_util = {
74a1de
 static void usage(void)
74a1de
 {
74a1de
 	fprintf(stderr,
74a1de
-		"Usage: ... police rate BPS burst BYTES[/BYTES] [ mtu BYTES[/BYTES] ]\n"
74a1de
+		"Usage: ... police [ rate BPS burst BYTES[/BYTES] ] \n"
74a1de
+		"		[ pkts_rate RATE pkts_burst PACKETS ] [ mtu BYTES[/BYTES] ]\n"
74a1de
 		"		[ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n"
74a1de
 		"		[ linklayer TYPE ] [ CONTROL ]\n"
74a1de
 		"Where: CONTROL := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT]\n"
74a1de
@@ -67,6 +68,7 @@ static int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
74a1de
 	int Rcell_log =  -1, Pcell_log = -1;
74a1de
 	struct rtattr *tail;
74a1de
 	__u64 rate64 = 0, prate64 = 0;
74a1de
+	__u64 pps64 = 0, ppsburst64 = 0;
74a1de
 
74a1de
 	if (a) /* new way of doing things */
74a1de
 		NEXT_ARG();
74a1de
@@ -144,6 +146,18 @@ static int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
74a1de
 			NEXT_ARG();
74a1de
 			if (get_linklayer(&linklayer, *argv))
74a1de
 				invarg("linklayer", *argv);
74a1de
+		} else if (matches(*argv, "pkts_rate") == 0) {
74a1de
+			NEXT_ARG();
74a1de
+			if (pps64)
74a1de
+				duparg("pkts_rate", *argv);
74a1de
+			if (get_u64(&pps64, *argv, 10))
74a1de
+				invarg("pkts_rate", *argv);
74a1de
+		} else if (matches(*argv, "pkts_burst") == 0) {
74a1de
+			NEXT_ARG();
74a1de
+			if (ppsburst64)
74a1de
+				duparg("pkts_burst", *argv);
74a1de
+			if (get_u64(&ppsburst64, *argv, 10))
74a1de
+				invarg("pkts_burst", *argv);
74a1de
 		} else if (strcmp(*argv, "help") == 0) {
74a1de
 			usage();
74a1de
 		} else {
74a1de
@@ -161,8 +175,8 @@ action_ctrl_ok:
74a1de
 		return -1;
74a1de
 
74a1de
 	/* Must at least do late binding, use TB or ewma policing */
74a1de
-	if (!rate64 && !avrate && !p.index && !mtu) {
74a1de
-		fprintf(stderr, "'rate' or 'avrate' or 'mtu' MUST be specified.\n");
74a1de
+	if (!rate64 && !avrate && !p.index && !mtu && !pps64) {
74a1de
+		fprintf(stderr, "'rate' or 'avrate' or 'mtu' or 'pkts_rate' MUST be specified.\n");
74a1de
 		return -1;
74a1de
 	}
74a1de
 
74a1de
@@ -172,6 +186,18 @@ action_ctrl_ok:
74a1de
 		return -1;
74a1de
 	}
74a1de
 
74a1de
+	/* When the packets TB policer is used, pkts_burst is required */
74a1de
+	if (pps64 && !ppsburst64) {
74a1de
+		fprintf(stderr, "'pkts_burst' requires 'pkts_rate'.\n");
74a1de
+		return -1;
74a1de
+	}
74a1de
+
74a1de
+	/* forbid rate and pkts_rate in same action */
74a1de
+	if (pps64 && rate64) {
74a1de
+		fprintf(stderr, "'rate' and 'pkts_rate' are not allowed in same action.\n");
74a1de
+		return -1;
74a1de
+	}
74a1de
+
74a1de
 	if (prate64) {
74a1de
 		if (!rate64) {
74a1de
 			fprintf(stderr, "'peakrate' requires 'rate'.\n");
74a1de
@@ -223,6 +249,12 @@ action_ctrl_ok:
74a1de
 	if (presult)
74a1de
 		addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult);
74a1de
 
74a1de
+	if (pps64) {
74a1de
+		addattr64(n, MAX_MSG, TCA_POLICE_PKTRATE64, pps64);
74a1de
+		ppsburst64 = tc_calc_xmittime(pps64, ppsburst64);
74a1de
+		addattr64(n, MAX_MSG, TCA_POLICE_PKTBURST64, ppsburst64);
74a1de
+	}
74a1de
+
74a1de
 	addattr_nest_end(n, tail);
74a1de
 	res = 0;
74a1de
 
74a1de
@@ -244,6 +276,7 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
74a1de
 	unsigned int buffer;
74a1de
 	unsigned int linklayer;
74a1de
 	__u64 rate64, prate64;
74a1de
+	__u64 pps64, ppsburst64;
74a1de
 
74a1de
 	if (arg == NULL)
74a1de
 		return 0;
74a1de
@@ -287,6 +320,17 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
74a1de
 		tc_print_rate(PRINT_FP, NULL, "avrate %s ",
74a1de
 			      rta_getattr_u32(tb[TCA_POLICE_AVRATE]));
74a1de
 
74a1de
+	if ((tb[TCA_POLICE_PKTRATE64] &&
74a1de
+	     RTA_PAYLOAD(tb[TCA_POLICE_PKTRATE64]) >= sizeof(pps64)) &&
74a1de
+	     (tb[TCA_POLICE_PKTBURST64] &&
74a1de
+	      RTA_PAYLOAD(tb[TCA_POLICE_PKTBURST64]) >= sizeof(ppsburst64))) {
74a1de
+		pps64 = rta_getattr_u64(tb[TCA_POLICE_PKTRATE64]);
74a1de
+		ppsburst64 = rta_getattr_u64(tb[TCA_POLICE_PKTBURST64]);
74a1de
+		ppsburst64 = tc_calc_xmitsize(pps64, ppsburst64);
74a1de
+		fprintf(f, "pkts_rate %llu ", pps64);
74a1de
+		fprintf(f, "pkts_burst %llu ", ppsburst64);
74a1de
+	}
74a1de
+
74a1de
 	print_action_control(f, "action ", p->action, "");
74a1de
 
74a1de
 	if (tb[TCA_POLICE_RESULT]) {
74a1de
-- 
74a1de
2.31.1
74a1de