From patchwork Wed Apr 21 13:48:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Maximets X-Patchwork-Id: 1468729 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=smtp4.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FQMMf5CHtz9t0G for ; Wed, 21 Apr 2021 23:48:30 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 8B04140EE2; Wed, 21 Apr 2021 13:48:27 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id g3RLpi6LlrLf; Wed, 21 Apr 2021 13:48:24 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp4.osuosl.org (Postfix) with ESMTP id CB73F40ED3; Wed, 21 Apr 2021 13:48:23 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 9D94DC000F; Wed, 21 Apr 2021 13:48:23 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id EDACFC000B for ; Wed, 21 Apr 2021 13:48:22 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id C750E83E12 for ; Wed, 21 Apr 2021 13:48:22 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HaiHSAlYKMXl for ; Wed, 21 Apr 2021 13:48:21 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from relay10.mail.gandi.net (relay10.mail.gandi.net [217.70.178.230]) by smtp1.osuosl.org (Postfix) with ESMTPS id 72C2A83D60 for ; Wed, 21 Apr 2021 13:48:21 +0000 (UTC) Received: from im-t490s.redhat.com (ip-78-45-89-65.net.upcbroadband.cz [78.45.89.65]) (Authenticated sender: i.maximets@ovn.org) by relay10.mail.gandi.net (Postfix) with ESMTPSA id C5F32240008; Wed, 21 Apr 2021 13:48:18 +0000 (UTC) From: Ilya Maximets To: ovs-dev@openvswitch.org Date: Wed, 21 Apr 2021 15:48:16 +0200 Message-Id: <20210421134816.311584-1-i.maximets@ovn.org> X-Mailer: git-send-email 2.26.3 MIME-Version: 1.0 Cc: "jean . tourrilhes @ hpe . com" , Ilya Maximets Subject: [ovs-dev] [PATCH] dpif-netdev: Remove meter rate from the bucket size calculation. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Implementation of meters supposed to be a classic token bucket with 2 typical parameters: rate and burst size. Burst size in this schema is the maximum number of bytes/packets that could pass without being rate limited. Recent changes to userspace datapath made meter implementation to be in line with the kernel one, and this uncovered several issues. The main problem is that maximum bucket size for unknown reason accounts not only burst size, but also the numerical value of rate. This creates a lot of confusion around behavior of meters. For example, if rate is configured as 1000 pps and burst size set to 1, this should mean that meter will tolerate bursts of 1 packet at most, i.e. not a single packet above the rate should pass the meter. However, current implementation calculates maximum bucket size as (rate + burst size), so the effective bucket size will be 1001. This means that first 1000 packets will not be rate limited and average rate might be twice as high as the configured rate. This also makes it practically impossible to configure meter that will have burst size lower than the rate, which might be a desirable configuration if the rate is high. Inability to configure low values of a burst size and overall inability for a user to predict what will be a maximum and average rate from the configured parameters of a meter without looking at the OVS and kernel code might be also classified as a security issue, because drop meters are frequently used as a way of protection from DoS attacks. This change removes rate from the calculation of a bucket size, making it in line with the classic token bucket algorithm and essentially making the rate and burst tolerance being predictable from a users' perspective. Same change will be proposed for the kernel implementation. Unit tests changed back to their correct version and enhanced. Signed-off-by: Ilya Maximets Acked-by: Eelco Chaudron Reviewed-by: Tonghao Zhang --- lib/dpif-netdev.c | 5 ++-- tests/dpif-netdev.at | 53 +++++++++++++++++++++++++++++++------------ tests/ofproto-dpif.at | 2 +- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 251788b04..650e67ab3 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -6229,7 +6229,7 @@ dp_netdev_run_meter(struct dp_netdev *dp, struct dp_packet_batch *packets_, uint64_t max_bucket_size; band = &meter->bands[m]; - max_bucket_size = (band->rate + band->burst_size) * 1000ULL; + max_bucket_size = band->burst_size * 1000ULL; /* Update band's bucket. */ band->bucket += (uint64_t) delta_t * band->rate; if (band->bucket > max_bucket_size) { @@ -6357,8 +6357,7 @@ dpif_netdev_meter_set(struct dpif *dpif, ofproto_meter_id meter_id, meter->bands[i].rate = config->bands[i].rate; meter->bands[i].burst_size = config->bands[i].burst_size; /* Start with a full bucket. */ - meter->bands[i].bucket = - (meter->bands[i].burst_size + meter->bands[i].rate) * 1000ULL; + meter->bands[i].bucket = meter->bands[i].burst_size * 1000ULL; /* Figure out max delta_t that is enough to fill any bucket. */ band_max_delta_t diff --git a/tests/dpif-netdev.at b/tests/dpif-netdev.at index 57cae383f..16402ebae 100644 --- a/tests/dpif-netdev.at +++ b/tests/dpif-netdev.at @@ -314,21 +314,21 @@ done sleep 1 # wait for forwarders process packets # Meter 1 is measuring packets, allowing one packet per second with -# bursts of one packet, so 3 out of 5 packets should hit the drop -# band. -# Meter 2 is measuring kbps, with burst size 2 (== 3000 bits). 6 packets -# (360 bytes == 2880 bits) pass, but the last packet should hit the drop band. +# bursts of one packet, so 4 out of 5 packets should hit the drop band. +# Meter 2 is measuring kbps, with burst size 2 (== 2000 bits). 4 packets +# (240 bytes == 1920 bits) pass, but the last three packets should hit the +# drop band. There should be 80 bits remaining for the next packets. AT_CHECK([ovs-ofctl -O OpenFlow13 meter-stats br0 | strip_timers], [0], [dnl OFPST_METER reply (OF1.3) (xid=0x2): meter:1 flow_count:1 packet_in_count:5 byte_in_count:300 duration:0.0s bands: -0: packet_count:3 byte_count:180 +0: packet_count:4 byte_count:240 meter:2 flow_count:1 packet_in_count:7 byte_in_count:420 duration:0.0s bands: -0: packet_count:1 byte_count:60 +0: packet_count:3 byte_count:180 ]) -# Advance time by 1/2 second -ovs-appctl time/warp 500 +# Advance time by 870 ms +ovs-appctl time/warp 870 for i in `seq 1 5`; do AT_CHECK( @@ -345,16 +345,41 @@ sleep 1 # wait for forwarders process packets # Meter 1 is measuring packets, allowing one packet per second with # bursts of one packet, so all 5 of the new packets should hit the drop # band. -# Meter 2 is measuring kbps, with burst size 2 (== 3000 bits). After 500ms -# there should be space for 120 + 500 bits, so one new 60 byte (480 bit) packet -# should pass, remaining 4 should hit the drop band. +# Meter 2 is measuring kbps, with burst size 2 (== 2000 bits). After 870ms +# there should be space for 80 + 870 = 950 bits, so one new 60 byte (480 bit) +# packet should pass, remaining 4 should hit the drop band. There should be +# 470 bits left. AT_CHECK([ovs-ofctl -O OpenFlow13 meter-stats br0 | strip_timers], [0], [dnl OFPST_METER reply (OF1.3) (xid=0x2): meter:1 flow_count:1 packet_in_count:10 byte_in_count:600 duration:0.0s bands: -0: packet_count:8 byte_count:480 +0: packet_count:9 byte_count:540 meter:2 flow_count:1 packet_in_count:12 byte_in_count:720 duration:0.0s bands: -0: packet_count:5 byte_count:300 +0: packet_count:7 byte_count:420 +]) + +# Advance time by 10 ms +ovs-appctl time/warp 10 + +for i in `seq 1 5`; do + AT_CHECK( + [ovs-appctl netdev-dummy/receive p7 \ + 'in_port(7),packet_type(ns=0,id=0),eth(src=50:54:00:00:00:09,dst=50:54:00:00:00:0a),eth_type(0x0800),ipv4(src=10.0.0.2,dst=10.0.0.1,proto=1,tos=0,ttl=64,frag=no),icmp(type=8,code=0)' --len 60]) +done + +sleep 1 # wait for forwarders process packets + +# Meter 1 should remain the same as we didn't send anything that should hit it. +# Meter 2 is measuring kbps, with burst size 2 (== 2000 bits). After 10ms +# there should be space for 470 + 10 = 480 bits, so one new 60 byte (480 bit) +# packet should pass, remaining 4 should hit the drop band. +AT_CHECK([ovs-ofctl -O OpenFlow13 meter-stats br0 | strip_timers], [0], [dnl +OFPST_METER reply (OF1.3) (xid=0x2): +meter:1 flow_count:1 packet_in_count:10 byte_in_count:600 duration:0.0s bands: +0: packet_count:9 byte_count:540 + +meter:2 flow_count:1 packet_in_count:17 byte_in_count:1020 duration:0.0s bands: +0: packet_count:11 byte_count:660 ]) ovs-appctl time/warp 5000 @@ -362,7 +387,7 @@ ovs-appctl time/warp 5000 AT_CHECK([ ovs-appctl coverage/read-counter datapath_drop_meter ], [0], [dnl -13 +20 ]) AT_CHECK([cat ovs-vswitchd.log | filter_flow_install | strip_xout_keep_actions], [0], [dnl diff --git a/tests/ofproto-dpif.at b/tests/ofproto-dpif.at index 24bbd884c..31064ed95 100644 --- a/tests/ofproto-dpif.at +++ b/tests/ofproto-dpif.at @@ -2123,7 +2123,7 @@ AT_CHECK([ovs-appctl revalidator/purge]) AT_CHECK([ovs-ofctl monitor br0 65534 invalid_ttl -P nxt_packet_in --detach --no-chdir --pidfile 2> ofctl_monitor.log]) dnl Add a controller meter. -AT_CHECK([ovs-ofctl -O OpenFlow13 add-meter br0 'meter=controller pktps burst stats bands=type=drop rate=1 burst_size=1']) +AT_CHECK([ovs-ofctl -O OpenFlow13 add-meter br0 'meter=controller pktps stats bands=type=drop rate=2']) dnl Advance time by 1 second. AT_CHECK([ovs-appctl time/warp 1000], [0], [ignore])