From patchwork Tue Jul 12 02:02:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilson Peng X-Patchwork-Id: 1655153 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=vmware.com header.i=@vmware.com header.a=rsa-sha256 header.s=selector2 header.b=qQMJeJiW; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LhkYy1RPVz9s07 for ; Tue, 12 Jul 2022 12:03:33 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 72BC140B7B; Tue, 12 Jul 2022 02:03:31 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 72BC140B7B Authentication-Results: smtp2.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key, unprotected) header.d=vmware.com header.i=@vmware.com header.a=rsa-sha256 header.s=selector2 header.b=qQMJeJiW X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wpeNl-zvEBCt; Tue, 12 Jul 2022 02:03:29 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTPS id 8208240283; Tue, 12 Jul 2022 02:03:28 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 8208240283 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 3E0FBC0033; Tue, 12 Jul 2022 02:03:28 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 2AA31C002D for ; Tue, 12 Jul 2022 02:03:27 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id DC1E140931 for ; Tue, 12 Jul 2022 02:03:26 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org DC1E140931 X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mj01BHWNoLVz for ; Tue, 12 Jul 2022 02:03:24 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.8.0 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 9974E40283 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2073.outbound.protection.outlook.com [40.107.94.73]) by smtp2.osuosl.org (Postfix) with ESMTPS id 9974E40283 for ; Tue, 12 Jul 2022 02:03:24 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gqkLlI9sfdnfbYIHl+087Vg6/BxlYS6GaVoD+MlV9rzmAaZNRzLED1gToEK1fVZFm4UwfwZMry75JRrJbn/p+PZuQrhbgkZktlQYqtxsoUAwaG1hd4HnO+oQCJ/7fyLrOOrklscRnYvfL9pagQlU5J2ILKOfowFCl5xB5SghBQAHnxaACxSYByesbU5cRlU99ob5Dw/si83c88rHLKq0WmvBrze4v2rH+XlloxjG+qvDcW/Vzkv/YhNnxTAVGwLw+T6KSfKqjcxyvb7d964C9rLPeflD2gozlKfely1ZUlmLT8uJxgtyOQXoHuqosdZYD3goUEEsaCmivMONbZFp6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3LuQoaNW850XsUJr9WRkYPsUloZhoq9susjrjoGOBdM=; b=C+M5mS1wIaLcsw8FADJwZ2oL9YpAWAYXonklrbHH0D2JfAT9EvxuahQlYuKyu1cxXrkXEhoFlCBtOrpFImRCT0JKq+R1ETOyqDsBfnLZlrmFx5J01L0nmtnwUCIS+DgwsTk+VBy/vWeR/cS7+oZPmIX3wnZvzCSBP9ugD6FXYr9EUIVNCB70XypDUcAQxKQg/gE5eGM/fQZQ/xwhqThjXofKGNHZlE4nFG5/BcC/zFBKrWhlPZOfX6K3m+ZEZfPO55uDeGif+lr8//rMm1UTksco1UwNPYv8vmuzNEfkCkX5GJOnm2zeTVDd4PoM8hPh1Y6C5I4P2VBiRiBqtw5k/A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vmware.com; dmarc=pass action=none header.from=vmware.com; dkim=pass header.d=vmware.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vmware.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3LuQoaNW850XsUJr9WRkYPsUloZhoq9susjrjoGOBdM=; b=qQMJeJiWx42OhHY5WVRwUOo+OMbezzV4P43HXPrGvKC0j2FG6ld9uDLA0Q1i8x1lIfdBYyoYGuHjCn8IDLQaKlJk8lvBiuVhS9hOtfDR3dMNaNK4w7I6m/7+kxQuysL9cvRwuSN4qsxtsthop4TNik+n1P7Schh8jrOMC+XEWO4= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vmware.com; Received: from SN6PR05MB5935.namprd05.prod.outlook.com (2603:10b6:805:100::18) by BN8PR05MB6211.namprd05.prod.outlook.com (2603:10b6:408:44::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.10; Tue, 12 Jul 2022 02:03:18 +0000 Received: from SN6PR05MB5935.namprd05.prod.outlook.com ([fe80::992a:2e15:7c33:6da6]) by SN6PR05MB5935.namprd05.prod.outlook.com ([fe80::992a:2e15:7c33:6da6%5]) with mapi id 15.20.5438.011; Tue, 12 Jul 2022 02:03:18 +0000 To: dev@openvswitch.org Date: Tue, 12 Jul 2022 10:02:54 +0800 Message-Id: <20220712020254.79404-1-svc.ovs-community@vmware.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) X-ClientProxiedBy: MA1PR0101CA0065.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::27) To SN6PR05MB5935.namprd05.prod.outlook.com (2603:10b6:805:100::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 03fd9b5e-2419-4186-018c-08da63aaafd7 X-MS-TrafficTypeDiagnostic: BN8PR05MB6211:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XdZ+xRZWNM5uMdJftFF4xFkN05ERe+yawG6Hqei4TwGyBBLtPN9EGl8kYGX4FR3siJ7aIx4LNOxZkp1VXOZ4oYNUskgL+c3EvsFv/F2oeVo/J0nUxwKQdnqpMmivn72wnomwgNCAQ/Un2MGb5Almm4O4UKQDL/CeaqUb4T6CbJaWunEhXAe4v+B5GYFsNGQ0sHKTBkyG7l1pIPxoZNIUHDgbcaEC7gX3hLQ4F76Ki5ZEf2vhQaB7KSJMkvhTQkzjO6AwA14yp6ZY7SwTLIVaMjWXtqFb/zkmIJ5LwcwMT6kP9diPuaPM2bdSzA8DnX3HwZcG7BZ4NaExpbYBBmAUP4ubSx/qaBXaw2zckMR1oa5fYTbkV0lJ3JZtiZl3XcLrEtUL2OIxpi57J8eCdqHTs/X9Qi72+QpmUVC7xemYCzK/ls9hYwRyZpCKcCqf5u27EyuQWlPpzQhYKjvlW03i6i8hQBHYybprazTZ01ahUCN8buiKhhEBqtuDt6+yCTHhdCLCAnlA/632K1U+JQKNZaGfdCZHxFEOnjuuySz6aME9qiucKVXZ/1GY7jQODpeajHeQtRViDbUey31+IYLbAvIjoqBWpbxO56XWHdu/gkmRsra2lqMozNdV1IHN/5yZEzhTzHMrj7VTcGlAEAwZuBZVoq31LyfC1j9qDwAEQQcyw7QyzzVPaWBX5kERaBXGpD6yYlkcL0N7kNrCLdm2GfvAA0VjS9IAx9A6JvcgRl3i1G9i1hX+NRbaws1ooFtw6I77zBnhH5KK+i/PZUJz6d9QrNTLjb4H6sv7pfcBEUXhdNK/YT67hsVgQBxKQ+SouOgQm6WViQLCaugzAtC0vGIybfezyifmWSWiKJJ1PAeQFxtgSlvRLB9jxcH29JgHnRw0Xvbmar+7lE1XmcNldwek7ZITDzM9ag+eXngjm1Q= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR05MB5935.namprd05.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(4636009)(376002)(136003)(346002)(366004)(396003)(39860400002)(41300700001)(6512007)(6666004)(186003)(8676002)(83380400001)(66556008)(66946007)(6506007)(66476007)(4326008)(52116002)(26005)(86362001)(478600001)(966005)(6916009)(38100700002)(107886003)(2616005)(1076003)(2906002)(36756003)(6486002)(38350700002)(316002)(30864003)(8936002)(5660300002)(43062005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LOrRdffpZpGTe1hdEny+QP82pr7pc+p2sKCiffRDa9EMReuNIgR65F5n1fpIk5/CQyaOpGKN1yo49nqF6RDLrbma+CKBmckuCQZghNcsN74bQn9t3wTwI1/HolivGSVidlMdqcw3F3QNPOQctySzzIKMpBvAuXHKP8pwsdWQfe5p1cXwzz1YSzuHz4SWgDUcqGG/3YCagBrfuZ8KvUlUxGJoSUhYvq1xNDvm0AdnhtYNnR1kjTwZD2f/kjduUbicqwcsScH6qa/QUwOCHeC4+762YJXn8TSNvK+4RBi1JoPFNhBy38h9VoA/6t8aylxT4USIS44UumDFnpwwJA/krkeASSJ5mN5FeyBtRXq3T5DQIWUs1EC5e+Nou2wMxfoSlhU5YrubMSHvCMcU82fAnSLD48nJIOnK2F7AYdr+MqgpXC2e59SMHURmeIi+e3DjnVfI5+VRBWJush0sly/U0XAzUFAbYNoIKxm7KeqhkNK0fikL8Yh9dHEJjfb+EjT/JTyOAMpxOJfep3dXk2PDeLjHru3LdRpDQg2b4RIaXC5cE1DAGR0ZA9ZfHXOWJaPi9H/xT3FnVsDQmxUOq4JI/OAzVlBhPGPxZBEZrrXiBVEDuopa5hhCYRHK1nKBtlMr0tlT5FgAf9wakrP/TLv6hlKESoMXJ1dzDsvStkTsg/lqtSjyaKGXSvdjr+AXpHc+OGh6bvp++Bnzu6lepo9QsNztBVW8JqoOF9wMbvntGjQafs4xMbbq2y7G4nwcBUZYx85kOaV2SFBKazcb6pTHCaKni6JL5OLGV+O9BjXpSzs2Don3Rg06vlop7Sc8DFp6xJKdqwXuV+kDasUIBvU1ys2421zqgeHUdvr+RsxEp4kK+ffW2wrOezcCIjykJY8Hv3Cx1Zlqx+whNttX0rGVKfCHAYOc/cjBTw3HeZdSnqo34djVuAEGW8VurhlKNNl/nO5wUOVZ8cd8eWbvqKTgrfcpRS6CuAUramOW+1tfBj9ZJ3JiQfQbdpInaziEM/2myXz01IxbzV/27+WvPNjxB9a5lelUWXewh5n6JDoZ8kzKX0PnjCQSylRr0TdStyRW+KqKJMhodMCp/aRhwiUt/rG7k5RY4YXjKCpOfF3BmRRi/+grBzt/GbBXf9BstLDXA0K/V86IF9sHfFX5J2Zgu0E9TGKMJy5FkkUjlwd62m11Ovknubbl0ZkN1vuA7ziKVKdO0iRPnewld12PSjqkjC/Yg4dP6YqzQbankfoP2FrHgKTtYUBk+fhuqsxyZsQdmtH7GomG8u/SFWwWYchn+ZMW3Or5nntIGXKXNS880fW24UC1sLI8YDLjiuHSFnJ9zDLhzBOH72R5SjzQf7k+yrGbRePJwxVE3DeMeZIuedcFX6hGCW68TCO9j4tFF7+oI4aIoofqflovVhPPzo890vF4xyt6oW0JrxNS7DNb0vtzq3WJGcloYq4Q0f0PUWltq9ev2o8yoHwu0ZkptmKXLbxCdoLAz4PmzKsnrT2DX8TM+uEyHJHFxSn3zXe7xAD6L1F4i2s4eg/d2Uy8Z7Iu2Blx0laBiXsdP2FldrK96pGRhnU/3MzrRnsis4stNSnfgHfgfThMogcr+jqlKX11iw== X-OriginatorOrg: vmware.com X-MS-Exchange-CrossTenant-Network-Message-Id: 03fd9b5e-2419-4186-018c-08da63aaafd7 X-MS-Exchange-CrossTenant-AuthSource: SN6PR05MB5935.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jul 2022 02:03:17.9695 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b39138ca-3cee-4b4a-a4d6-cd83d9dd62f0 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uYdT0/GK226ma0Clsu5Ua2t9zrBVLZKQvEImGKoSCUmbmeSvdBWaEriZ7qQL90J2zbOnxawSUu1kNSWwgHNnZB64Vulg/rrq6M6uTQiRwZA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8PR05MB6211 Cc: ldejing Subject: [ovs-dev] [PATCH v1 1/1] datapath-windows: support meter action initial version 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: , X-Patchwork-Original-From: ldejing via dev From: Wilson Peng Reply-To: ldejing Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: ldejing This patch implemented meter action, currently, meter only support drop method and only support one band. The overall implementation is, when a packet comes in, it will first lookup meter according to the meter id, then get the band->rates and delta time since last access the same meter from the meter struct. Add the multiply result(band->rates * delta_time) to bucket, finally bucket minus the packet size, if the result larger than zero, allow the packet go through, otherwise deny the packet go through. Test case: 1. Setting the size meter size 3M, then the bandwidth was limit around 3M; ovs-ofctl -O OpenFlow13 add-meter br-test meter=2,kbps,\ band=type=drop,rate=3000 ovs-ofctl add-flow br-test "table=0,priority=1,ip \ actions=meter:2,normal" -O OpenFlow13 2. Setting the meter size 8M, then the bandwidth was limit around 8M; ovs-ofctl -O OpenFlow13 add-meter br-test meter=2,\ kbps,band=type=drop,rate=8000 ovs-ofctl add-flow br-test "table=0,priority=1,ip\ actions=meter:2,normal" -O OpenFlow13 Signed-off-by: ldejing --- datapath-windows/automake.mk | 2 + datapath-windows/include/OvsDpInterfaceExt.h | 3 + datapath-windows/ovsext/Actions.c | 10 + datapath-windows/ovsext/Datapath.c | 46 +- datapath-windows/ovsext/Meter.c | 538 +++++++++++++++++++ datapath-windows/ovsext/Meter.h | 68 +++ datapath-windows/ovsext/Switch.c | 8 + datapath-windows/ovsext/Util.h | 1 + datapath-windows/ovsext/ovsext.vcxproj | 2 + lib/netlink-socket.c | 5 + 10 files changed, 682 insertions(+), 1 deletion(-) create mode 100644 datapath-windows/ovsext/Meter.c create mode 100644 datapath-windows/ovsext/Meter.h diff --git a/datapath-windows/automake.mk b/datapath-windows/automake.mk index 60b3d6033..2d9f9a40d 100644 --- a/datapath-windows/automake.mk +++ b/datapath-windows/automake.mk @@ -42,6 +42,8 @@ EXTRA_DIST += \ datapath-windows/ovsext/Jhash.c \ datapath-windows/ovsext/Jhash.h \ datapath-windows/ovsext/Mpls.h \ + datapath-windows/ovsext/Meter.c \ + datapath-windows/ovsext/Meter.h \ datapath-windows/ovsext/NetProto.h \ datapath-windows/ovsext/Netlink/Netlink.c \ datapath-windows/ovsext/Netlink/Netlink.h \ diff --git a/datapath-windows/include/OvsDpInterfaceExt.h b/datapath-windows/include/OvsDpInterfaceExt.h index 5fd8000d1..045e4cbd6 100644 --- a/datapath-windows/include/OvsDpInterfaceExt.h +++ b/datapath-windows/include/OvsDpInterfaceExt.h @@ -74,6 +74,9 @@ #define OVS_WIN_NL_CT_FAMILY_ID (NLMSG_MIN_TYPE + 7) #define OVS_WIN_NL_CTLIMIT_FAMILY_ID (NLMSG_MIN_TYPE + 8) +/* Meter Family */ +#define OVS_WIN_NL_METER_FAMILY_ID (NLMSG_MIN_TYPE + 9) + #define OVS_WIN_NL_INVALID_MCGRP_ID 0 #define OVS_WIN_NL_MCGRP_START_ID 100 #define OVS_WIN_NL_VPORT_MCGRP_ID (OVS_WIN_NL_MCGRP_START_ID + 1) diff --git a/datapath-windows/ovsext/Actions.c b/datapath-windows/ovsext/Actions.c index 20de4db4c..63ee574c5 100644 --- a/datapath-windows/ovsext/Actions.c +++ b/datapath-windows/ovsext/Actions.c @@ -23,6 +23,7 @@ #include "Flow.h" #include "Gre.h" #include "Jhash.h" +#include "Meter.h" #include "Mpls.h" #include "NetProto.h" #include "Offload.h" @@ -2502,6 +2503,15 @@ OvsDoExecuteActions(POVS_SWITCH_CONTEXT switchContext, } break; } + case OVS_ACTION_ATTR_METER: { + if (OvsMeterExecute(&ovsFwdCtx, NlAttrGetU32(a))) { + OVS_LOG_INFO("Drop packet"); + dropReason = L"Ovs-meter exceed max rate"; + goto dropit; + } + + break; + } case OVS_ACTION_ATTR_SAMPLE: { if (ovsFwdCtx.destPortsSizeOut > 0 || ovsFwdCtx.tunnelTxNic != NULL diff --git a/datapath-windows/ovsext/Datapath.c b/datapath-windows/ovsext/Datapath.c index fa994840a..37db5bd17 100644 --- a/datapath-windows/ovsext/Datapath.c +++ b/datapath-windows/ovsext/Datapath.c @@ -100,7 +100,11 @@ NetlinkCmdHandler OvsGetNetdevCmdHandler, OvsReadPacketCmdHandler, OvsCtDeleteCmdHandler, OvsCtDumpCmdHandler, - OvsCtLimitHandler; + OvsCtLimitHandler, + OvsMeterFeatureProbe, + OvsNewMeterCmdHandler, + OvsMeterDestroy, + OvsMeterGet; static NTSTATUS HandleGetDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx, UINT32 *replyLen); @@ -307,6 +311,43 @@ NETLINK_FAMILY nlCtFamilyOps = { .opsCount = ARRAY_SIZE(nlCtFamilyCmdOps) }; +/* Netlink Meter family */ +NETLINK_CMD nlMeterFamilyCmdOps[] = { + { .cmd = OVS_METER_CMD_FEATURES, + .handler = OvsMeterFeatureProbe, + .supportedDevOp = OVS_TRANSACTION_DEV_OP | + OVS_WRITE_DEV_OP | OVS_READ_DEV_OP, + .validateDpIndex = FALSE + }, + { .cmd = OVS_METER_CMD_SET, + .handler = OvsNewMeterCmdHandler, + .supportedDevOp = OVS_TRANSACTION_DEV_OP | + OVS_WRITE_DEV_OP | OVS_READ_DEV_OP, + .validateDpIndex = FALSE + }, + { .cmd = OVS_METER_CMD_GET, + .handler = OvsMeterGet, + .supportedDevOp = OVS_TRANSACTION_DEV_OP | + OVS_WRITE_DEV_OP | OVS_READ_DEV_OP, + .validateDpIndex = FALSE + }, + { .cmd = OVS_METER_CMD_DEL, + .handler = OvsMeterDestroy, + .supportedDevOp = OVS_TRANSACTION_DEV_OP | + OVS_WRITE_DEV_OP | OVS_READ_DEV_OP, + .validateDpIndex = FALSE + }, +}; + +NETLINK_FAMILY nlMeterFamilyOps = { + .name = OVS_METER_FAMILY, + .id = OVS_WIN_NL_METER_FAMILY_ID, + .version = OVS_METER_VERSION, + .maxAttr = __OVS_METER_ATTR_MAX, + .cmds = nlMeterFamilyCmdOps, + .opsCount = ARRAY_SIZE(nlMeterFamilyCmdOps) +}; + /* Netlink netdev family. */ NETLINK_CMD nlNetdevFamilyCmdOps[] = { { .cmd = OVS_WIN_NETDEV_CMD_GET, @@ -952,6 +993,9 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject, case NFNL_TYPE_CT_DEL: nlFamilyOps = &nlCtFamilyOps; break; + case OVS_WIN_NL_METER_FAMILY_ID: + nlFamilyOps = &nlMeterFamilyOps; + break; case OVS_WIN_NL_CTRL_FAMILY_ID: nlFamilyOps = &nlControlFamilyOps; break; diff --git a/datapath-windows/ovsext/Meter.c b/datapath-windows/ovsext/Meter.c new file mode 100644 index 000000000..62f5f36c9 --- /dev/null +++ b/datapath-windows/ovsext/Meter.c @@ -0,0 +1,538 @@ +/* + * Copyright (c) 2022 VMware, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Meter.h" +#include "precomp.h" +#include "Switch.h" +#include "User.h" +#include "Datapath.h" +#include "Event.h" +#include "NetProto.h" +#include "Flow.h" +#include "PacketParser.h" +#include "util.h" + +static PNDIS_RW_LOCK_EX meterGlobalTableLock; +PLIST_ENTRY meterGlobalTable; + +const NL_POLICY nlMeterPolicy[OVS_METER_ATTR_MAX + 1] = { + [OVS_METER_ATTR_ID] = { .type = NL_A_U32, }, + [OVS_METER_ATTR_KBPS] = { .type = NL_A_FLAG, .optional = TRUE }, + [OVS_METER_ATTR_STATS] = { .minLen = sizeof(struct ovs_flow_stats), + .maxLen = sizeof(struct ovs_flow_stats), + .optional = TRUE }, + [OVS_METER_ATTR_BANDS] = { .type = NL_A_NESTED, .optional = TRUE }, + [OVS_METER_ATTR_USED] = { .type = NL_A_U64, .optional = TRUE }, + [OVS_METER_ATTR_CLEAR] = { .type = NL_A_FLAG, .optional = TRUE }, + [OVS_METER_ATTR_MAX_METERS] = { .type = NL_A_U32, .optional = TRUE }, + [OVS_METER_ATTR_MAX_BANDS] = { .type = NL_A_U32, .optional = TRUE }, +}; + +const NL_POLICY bandPolicy[OVS_BAND_ATTR_MAX + 1] = { + [OVS_BAND_ATTR_TYPE] = { .type = NL_A_U32, .optional = FALSE }, + [OVS_BAND_ATTR_RATE] = { .type = NL_A_U32, .optional = TRUE }, + [OVS_BAND_ATTR_BURST] = { .type = NL_A_U32, .optional = TRUE }, + [OVS_BAND_ATTR_STATS] = { .minLen = sizeof(struct ovs_flow_stats), + .maxLen = sizeof(struct ovs_flow_stats), + .optional = TRUE }, +}; + +NTSTATUS +OvsInitMeter(POVS_SWITCH_CONTEXT context) +{ + UINT32 maxEntry = METER_HASH_BUCKET_MAX; + + meterGlobalTableLock = NdisAllocateRWLock(context->NdisFilterHandle); + if (meterGlobalTableLock == NULL) { + return STATUS_INSUFFICIENT_RESOURCES; + } + + meterGlobalTable = OvsAllocateMemoryWithTag(sizeof(LIST_ENTRY) * maxEntry, + OVS_METER_TAG); + if (!meterGlobalTable) { + NdisFreeRWLock(meterGlobalTableLock); + return NDIS_STATUS_RESOURCES; + } + + for (UINT32 index = 0; index < maxEntry; index++) { + InitializeListHead(&meterGlobalTable[index]); + } + + return NDIS_STATUS_SUCCESS; +} + +NDIS_STATUS +FillBandIntoMeter(PNL_ATTR meterAttrs[], DpMeter *meter, PNL_MSG_HDR nlMsgHdr) +{ + PNL_ATTR a = NULL; + INT rem = 0; + UINT32 bandMaxDelta = 0; + UINT32 attrOffset = 0; + DpMeterBand *band = NULL; + PNL_ATTR bandAttrs[OVS_BAND_ATTR_MAX + 1]; + UINT16 nBands = 0; + + band = meter->bands; + NL_ATTR_FOR_EACH(a, rem, NlAttrData(meterAttrs[OVS_METER_ATTR_BANDS]), + NlAttrGetSize(meterAttrs[OVS_METER_ATTR_BANDS])) { + RtlZeroMemory(bandAttrs, sizeof(bandAttrs)); + attrOffset = (UINT32)((PCHAR)NlAttrData(a) - (PCHAR)nlMsgHdr); + if (!NlAttrParse(nlMsgHdr, + attrOffset, + NlAttrGetSize(a), + bandPolicy, ARRAY_SIZE(bandPolicy), + bandAttrs, ARRAY_SIZE(bandAttrs))) { + return STATUS_INVALID_PARAMETER; + } + + if (bandAttrs[OVS_BAND_ATTR_TYPE]) { + band->type = NlAttrGetU32(bandAttrs[OVS_BAND_ATTR_TYPE]); + } + + if (bandAttrs[OVS_BAND_ATTR_RATE]) { + band->rate = NlAttrGetU32(bandAttrs[OVS_BAND_ATTR_RATE]); + } + + if (bandAttrs[OVS_BAND_ATTR_BURST]) { + band->burst_size = NlAttrGetU32(bandAttrs[OVS_BAND_ATTR_BURST]); + } + + band->bucket = (band->burst_size + band->rate) * 1000; + bandMaxDelta = (UINT32)((band->bucket / band->rate) / 10); + if (bandMaxDelta > meter->maxDelta) { + meter->maxDelta = bandMaxDelta; + } + + nBands++; + band++; + } + + meter->nBands = nBands; + return NDIS_STATUS_SUCCESS; +} + +NDIS_STATUS +OvsNewMeterCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx, + UINT32 *replyLen) +{ + DpMeter *meter = NULL; + POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer; + PNL_MSG_HDR nlMsgHdr = &(msgIn->nlMsg); + PGENL_MSG_HDR genlMsgHdr = &(msgIn->genlMsg); + POVS_HDR ovsHdr = &(msgIn->ovsHdr); + LOCK_STATE_EX lockState; + PNL_ATTR meterAttrs[ARRAY_SIZE(nlMeterPolicy)]; + ASSERT(usrParamsCtx->inputBuffer != NULL); + PNL_MSG_HDR nlMsgOutHdr = NULL; + NL_BUFFER nlBuf; + NL_ERROR nlError = NL_ERROR_SUCCESS; + + if (!NlAttrParse((PNL_MSG_HDR)msgIn, + NLMSG_HDRLEN + GENL_HDRLEN + OVS_HDRLEN, + NlMsgAttrsLen((PNL_MSG_HDR)msgIn), + nlMeterPolicy, ARRAY_SIZE(nlMeterPolicy), + meterAttrs, ARRAY_SIZE(meterAttrs))) { + nlError = NL_ERROR_NOMSG; + goto Done; + } + + meter = OvsAllocateMemoryWithTag(sizeof(*meter), OVS_METER_TAG); + if (!meter) { + nlError = NL_ERROR_NOMEM; + goto Done; + } + + RtlZeroMemory(meter, sizeof(*meter)); + meter->id = NlAttrGetU32(meterAttrs[OVS_METER_ATTR_ID]); + meter->kbps = meterAttrs[OVS_METER_ATTR_KBPS] ? 1 : 0; + meter->keepStatus = meterAttrs[OVS_METER_ATTR_CLEAR] ? 1 : 0; + if (meter->keepStatus && meterAttrs[OVS_METER_ATTR_STATS]) { + meter->stats = *(struct ovs_flow_stats *)NlAttrData( + meterAttrs[OVS_METER_ATTR_STATS]); + } + + if (FillBandIntoMeter(meterAttrs, meter, nlMsgHdr) != NDIS_STATUS_SUCCESS) { + nlError = NL_ERROR_NOMSG; + OvsFreeMemoryWithTag(meter, OVS_METER_TAG); + goto Done; + } + + NdisAcquireRWLockWrite(meterGlobalTableLock, &lockState, 0); + InsertHeadList(&meterGlobalTable[meter->id & (METER_HASH_BUCKET_MAX - 1)], + &(meter->link)); + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + + NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength); + nlMsgOutHdr = (PNL_MSG_HDR)(NlBufAt(&nlBuf, 0, 0)); + if (!NlFillOvsMsg(&nlBuf, nlMsgHdr->nlmsgType, 0, + nlMsgHdr->nlmsgSeq, nlMsgHdr->nlmsgPid, + genlMsgHdr->cmd, OVS_METER_CMD_GET, + ovsHdr->dp_ifindex)) { + nlError = NL_ERROR_NOMSG; + goto Done; + } + + if (!buildOvsMeterReplyMsg(&nlBuf, meter)) { + nlError = NL_ERROR_NOMEM; + goto Done; + } + + NlMsgSetSize(nlMsgOutHdr, NlBufSize(&nlBuf)); + NlMsgAlignSize(nlMsgOutHdr); + *replyLen += NlMsgSize(nlMsgOutHdr); + +Done: + if (nlError != NL_ERROR_SUCCESS) { + POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR) + usrParamsCtx->outputBuffer; + + ASSERT(msgError); + NlBuildErrorMsg(msgIn, msgError, nlError, replyLen); + ASSERT(*replyLen != 0); + } + + return NDIS_STATUS_SUCCESS; +} + +NDIS_STATUS OvsMeterFeatureProbe(POVS_USER_PARAMS_CONTEXT usrParamsCtx, + UINT32 *replyLen) +{ + POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer; + PNL_MSG_HDR nlMsgHdr = &(msgIn->nlMsg); + POVS_HDR ovsHdr = &(msgIn->ovsHdr); + PNL_MSG_HDR nlMsgOutHdr = NULL; + BOOLEAN ok = FALSE; + PGENL_MSG_HDR genlMsgHdr = &(msgIn->genlMsg); + NL_BUFFER nlBuf; + NL_ERROR nlError = NL_ERROR_SUCCESS; + UINT32 bandsOffset = 0; + UINT32 bandAttrOffset = 0; + + NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength); + nlMsgOutHdr = (PNL_MSG_HDR)(NlBufAt(&nlBuf, 0, 0)); + ok = NlFillOvsMsg(&nlBuf, nlMsgHdr->nlmsgType, 0, + nlMsgHdr->nlmsgSeq, nlMsgHdr->nlmsgPid, + genlMsgHdr->cmd, OVS_METER_CMD_FEATURES, + ovsHdr->dp_ifindex); + if (!ok) { + nlError = NL_ERROR_NOMSG; + goto Done; + } + + if (!NlMsgPutTailU32(&nlBuf, OVS_METER_ATTR_MAX_METERS, UINT32_MAX)) { + nlError = NL_ERROR_NOMSG; + goto Done; + } + + if (!NlMsgPutTailU32(&nlBuf, OVS_METER_ATTR_MAX_BANDS, OVS_MAX_BANDS)) { + nlError = NL_ERROR_NOMSG; + goto Done; + } + + bandsOffset = NlMsgStartNested(&nlBuf, OVS_METER_ATTR_BANDS); + bandAttrOffset = NlMsgStartNested(&nlBuf, OVS_METER_ATTR_UNSPEC); + if (!NlMsgPutTailU32(&nlBuf, OVS_BAND_ATTR_TYPE, + OVS_METER_BAND_TYPE_DROP)) { + nlError = NL_ERROR_NOMSG; + goto Done; + + } + NlMsgEndNested(&nlBuf, bandAttrOffset); + NlMsgEndNested(&nlBuf, bandsOffset); + + NlMsgSetSize(nlMsgOutHdr, NlBufSize(&nlBuf)); + NlMsgAlignSize(nlMsgOutHdr); + *replyLen += NlMsgSize(nlMsgOutHdr); + +Done: + if (nlError != NL_ERROR_SUCCESS) { + POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR) + usrParamsCtx->outputBuffer; + + ASSERT(msgError); + NlBuildErrorMsg(msgIn, msgError, nlError, replyLen); + ASSERT(*replyLen != 0); + } + + return STATUS_SUCCESS; +} + +BOOLEAN +buildOvsMeterReplyMsg(NL_BUFFER *nlBuf, DpMeter *dpMeter) +{ + BOOLEAN ok = FALSE; + UINT32 bandAttrOffset; + UINT32 bandsOffset; + + /* Add meter element. */ + ok = NlMsgPutTailU32(nlBuf, OVS_METER_ATTR_ID, dpMeter->id); + if (!ok) { + OVS_LOG_ERROR("Could not add meter id %d.", dpMeter->id); + return ok; + } + + ok = NlMsgPutTailUnspec(nlBuf, OVS_METER_ATTR_STATS, + (PCHAR)&(dpMeter->stats), + sizeof(dpMeter->stats)); + if (!ok) { + OVS_LOG_ERROR("Could not add ovs meter stats."); + return ok; + } + + bandsOffset = NlMsgStartNested(nlBuf, OVS_METER_ATTR_BANDS); + for (int index = 0; index < dpMeter->nBands; index++) { + bandAttrOffset = NlMsgStartNested(nlBuf, OVS_BAND_ATTR_UNSPEC); + ok = NlMsgPutTailUnspec(nlBuf, OVS_BAND_ATTR_STATS, + (PCHAR)&(dpMeter->bands[index].stats), + sizeof(dpMeter->bands[index].stats)); + NlMsgEndNested(nlBuf, bandAttrOffset); + if (!ok) { + break; + } + } + + NlMsgEndNested(nlBuf, bandsOffset); + return ok; +} + +NDIS_STATUS +OvsMeterGet(POVS_USER_PARAMS_CONTEXT usrParamsCtx, + UINT32 *replyLen) +{ + POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer; + PNL_MSG_HDR nlMsgHdr = &(msgIn->nlMsg); + POVS_HDR ovsHdr = &(msgIn->ovsHdr); + PNL_MSG_HDR nlMsgOutHdr = NULL; + PNL_ATTR meterAttrs[ARRAY_SIZE(nlMeterPolicy)]; + UINT32 meterId = 0; + DpMeter *dpMeter = NULL; + BOOLEAN ok = FALSE; + PGENL_MSG_HDR genlMsgHdr = &(msgIn->genlMsg); + NL_BUFFER nlBuf; + NL_ERROR nlError = NL_ERROR_SUCCESS; + LOCK_STATE_EX lockState; + + if (!NlAttrParse((PNL_MSG_HDR)msgIn, + NLMSG_HDRLEN + GENL_HDRLEN + OVS_HDRLEN, + NlMsgAttrsLen((PNL_MSG_HDR)msgIn), + nlMeterPolicy, ARRAY_SIZE(nlMeterPolicy), + meterAttrs, ARRAY_SIZE(meterAttrs))) { + nlError = NL_ERROR_NOMSG; + goto Done; + } + + NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength); + meterId = NlAttrGetU32(meterAttrs[OVS_METER_ATTR_ID]); + + /* Reply message header */ + nlMsgOutHdr = (PNL_MSG_HDR)(NlBufAt(&nlBuf, 0, 0)); + ok = NlFillOvsMsg(&nlBuf, nlMsgHdr->nlmsgType, 0, + nlMsgHdr->nlmsgSeq, nlMsgHdr->nlmsgPid, + genlMsgHdr->cmd, OVS_METER_CMD_GET, + ovsHdr->dp_ifindex); + if (!ok) { + nlError = NL_ERROR_NOMSG; + goto Done; + } + + NdisAcquireRWLockRead(meterGlobalTableLock, &lockState, 0); + dpMeter = OvsMeterLookup(meterId); + if (!dpMeter) { + OVS_LOG_WARN("Has not find %d associated meter", meterId); + nlError = NL_ERROR_EXIST; + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + goto Done; + } + + if (!buildOvsMeterReplyMsg(&nlBuf, dpMeter)) { + nlError = NL_ERROR_NOMEM; + OVS_LOG_ERROR("Could not build ovs meter reply msg."); + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + goto Done; + } + + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + NlMsgSetSize(nlMsgOutHdr, NlBufSize(&nlBuf)); + NlMsgAlignSize(nlMsgOutHdr); + *replyLen += NlMsgSize(nlMsgOutHdr); + +Done: + if (nlError != NL_ERROR_SUCCESS) { + POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR) + usrParamsCtx->outputBuffer; + + ASSERT(msgError); + NlBuildErrorMsg(msgIn, msgError, nlError, replyLen); + ASSERT(*replyLen != 0); + } + + return NDIS_STATUS_SUCCESS; +} + +NDIS_STATUS +OvsMeterDestroy(POVS_USER_PARAMS_CONTEXT usrParamsCtx, + UINT32 *replyLen) +{ + POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer; + PNL_MSG_HDR nlMsgHdr = &(msgIn->nlMsg); + POVS_HDR ovsHdr = &(msgIn->ovsHdr); + PNL_MSG_HDR nlMsgOutHdr = NULL; + PNL_ATTR meterAttrs[ARRAY_SIZE(nlMeterPolicy)]; + PGENL_MSG_HDR genlMsgHdr = &(msgIn->genlMsg); + LOCK_STATE_EX lockState; + UINT32 meterId = 0; + BOOLEAN ok; + NL_BUFFER nlBuf; + NL_ERROR nlError = NL_ERROR_SUCCESS; + + if (!NlAttrParse((PNL_MSG_HDR)msgIn, + NLMSG_HDRLEN + GENL_HDRLEN + OVS_HDRLEN, + NlMsgAttrsLen((PNL_MSG_HDR)msgIn), + nlMeterPolicy, ARRAY_SIZE(nlMeterPolicy), + meterAttrs, ARRAY_SIZE(meterAttrs))) { + return STATUS_INVALID_PARAMETER; + } + + NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength); + + meterId = NlAttrGetU32(meterAttrs[OVS_METER_ATTR_ID]); + nlMsgOutHdr = (PNL_MSG_HDR)(NlBufAt(&nlBuf, 0, 0)); + ok = NlFillOvsMsg(&nlBuf, nlMsgHdr->nlmsgType, 0, + nlMsgHdr->nlmsgSeq, nlMsgHdr->nlmsgPid, + genlMsgHdr->cmd, OVS_METER_CMD_DEL, + ovsHdr->dp_ifindex); + if (!ok) { + nlError = NL_ERROR_NOMEM; + goto Done; + } + + NdisAcquireRWLockWrite(meterGlobalTableLock, &lockState, 0); + PLIST_ENTRY head = &meterGlobalTable[meterId & (METER_HASH_BUCKET_MAX - 1)]; + PLIST_ENTRY link, next; + DpMeter *entry = NULL; + + LIST_FORALL_SAFE(head, link, next) { + entry = CONTAINING_RECORD(link, DpMeter, link); + if (entry->id == meterId) { + if (!buildOvsMeterReplyMsg(&nlBuf, entry)) { + nlError = NL_ERROR_NOMEM; + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + goto Done; + } + RemoveEntryList(&entry->link); + OvsFreeMemoryWithTag(entry, OVS_METER_TAG); + break; + } + } + + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + NlMsgSetSize(nlMsgOutHdr, NlBufSize(&nlBuf)); + NlMsgAlignSize(nlMsgOutHdr); + *replyLen += NlMsgSize(nlMsgOutHdr); + +Done: + if (nlError != NL_ERROR_SUCCESS) { + POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR) + usrParamsCtx->outputBuffer; + + ASSERT(msgError); + NlBuildErrorMsg(msgIn, msgError, nlError, replyLen); + ASSERT(*replyLen != 0); + } + + return NDIS_STATUS_SUCCESS; +} + + + +DpMeter* +OvsMeterLookup(UINT32 meterId) +{ + PLIST_ENTRY head = &meterGlobalTable[meterId & (METER_HASH_BUCKET_MAX - 1)]; + PLIST_ENTRY link, next; + DpMeter *entry = NULL; + + LIST_FORALL_SAFE(head, link, next) { + entry = CONTAINING_RECORD(link, DpMeter, link); + if (entry->id == meterId) { + return entry; + } + } + + return NULL; +} + +BOOLEAN +OvsMeterExecute(OvsForwardingContext *fwdCtx, UINT32 meterId) +{ + DpMeter *dpMeter; + DpMeterBand *band; + UINT32 longDeltaMs; + UINT32 deltaMs; + UINT64 currentTime; + LOCK_STATE_EX lockState; + UINT32 cost; + UINT32 bandExceededRate = 0; + INT32 bandExceedIndex = -1; + UINT64 maxBucketSize = 0; + + NdisAcquireRWLockRead(meterGlobalTableLock, &lockState, 0); + dpMeter = OvsMeterLookup(meterId); + if (!dpMeter) { + OVS_LOG_ERROR("Not found meter id %d associated meter.", meterId); + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + return FALSE; + } + + NdisGetCurrentSystemTime((LARGE_INTEGER *)¤tTime); + longDeltaMs = (UINT32)((currentTime - dpMeter->used) / 10000); + deltaMs = longDeltaMs > dpMeter->maxDelta ? dpMeter->maxDelta : + longDeltaMs; + dpMeter->used = currentTime; + dpMeter->stats.n_packets += 1; + dpMeter->stats.n_bytes += OvsPacketLenNBL(fwdCtx->curNbl); + cost = dpMeter->kbps ? OvsPacketLenNBL(fwdCtx->curNbl) * 8 : 1000; + for (int index = 0; index < dpMeter->nBands; index++) { + band = &(dpMeter->bands[index]); + maxBucketSize = (band->burst_size + band->rate) * 1000LL; + band->bucket += deltaMs * band->rate; + if (band->bucket > maxBucketSize) { + band->bucket = maxBucketSize; + } + + if (band->bucket >= cost) { + band->bucket -= cost; + band->stats.n_packets += 1; + band->stats.n_bytes += OvsPacketLenNBL(fwdCtx->curNbl); + } else if (band->rate > bandExceededRate) { + bandExceededRate = band->rate; + bandExceedIndex = index; + } + } + + if (bandExceedIndex >= 0) { + band = &(dpMeter->bands[bandExceedIndex]); + band->stats.n_packets += 1; + band->stats.n_bytes += OvsPacketLenNBL(fwdCtx->curNbl); + if (band->type == OVS_METER_BAND_TYPE_DROP) { + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + return TRUE; + } + } + + NdisReleaseRWLock(meterGlobalTableLock, &lockState); + return FALSE; +} \ No newline at end of file diff --git a/datapath-windows/ovsext/Meter.h b/datapath-windows/ovsext/Meter.h new file mode 100644 index 000000000..bc94489a5 --- /dev/null +++ b/datapath-windows/ovsext/Meter.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022 VMware, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OVS_METER_H +#define OVS_METER_H + +#include "precomp.h" +#include "Switch.h" +#include "User.h" +#include "Datapath.h" +#include "Event.h" +#include "NetProto.h" +#include "Netlink/Netlink.h" +#include "Flow.h" + +#define OVS_MAX_BANDS 1 +#define OVS_MAX_METERS 32 +#define METER_HASH_BUCKET_MAX 1024 + +typedef struct _DpMeterBand { + UINT32 type; + UINT32 rate; + UINT32 burst_size; + UINT64 bucket; + struct ovs_flow_stats stats; +} DpMeterBand; + +typedef struct _DpMeter { + LIST_ENTRY link; + UINT32 id; + UINT16 kbps:1; + UINT16 keepStatus:1; + UINT16 nBands; + UINT32 maxDelta; + UINT64 used; + struct ovs_flow_stats stats; + DpMeterBand bands[OVS_MAX_BANDS]; +} DpMeter; + +NTSTATUS OvsInitMeter(POVS_SWITCH_CONTEXT context); +NDIS_STATUS OvsNewMeterCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx, + UINT32 *replyLen); +NDIS_STATUS OvsMeterFeatureProbe(POVS_USER_PARAMS_CONTEXT usrParamsCtx, +UINT32 *replyLen); + +NDIS_STATUS OvsMeterDestroy(POVS_USER_PARAMS_CONTEXT usrParamsCtx, + UINT32 *replyLen); +DpMeter* OvsMeterLookup(UINT32 meterId); +BOOLEAN +OvsMeterExecute(OvsForwardingContext *fwdCtx, UINT32 meterId); +BOOLEAN +buildOvsMeterReplyMsg(NL_BUFFER *nlBuf, DpMeter *dpMeter); + + +#endif //OVS_METER_H diff --git a/datapath-windows/ovsext/Switch.c b/datapath-windows/ovsext/Switch.c index 1ac4fa77c..088aee21a 100644 --- a/datapath-windows/ovsext/Switch.c +++ b/datapath-windows/ovsext/Switch.c @@ -24,6 +24,7 @@ #include "Switch.h" #include "Vport.h" #include "Event.h" +#include "Meter.h" #include "Flow.h" #include "IpHelper.h" #include "Oid.h" @@ -239,6 +240,13 @@ OvsCreateSwitch(NDIS_HANDLE ndisFilterHandle, goto create_switch_done; } + status = OvsInitMeter(switchContext); + if (status != STATUS_SUCCESS) { + OvsUninitSwitchContext(switchContext); + OVS_LOG_ERROR("Exit: Failed to initialize Ovs meter."); + goto create_switch_done; + } + *switchContextOut = switchContext; create_switch_done: diff --git a/datapath-windows/ovsext/Util.h b/datapath-windows/ovsext/Util.h index f63a885a9..16903ed46 100644 --- a/datapath-windows/ovsext/Util.h +++ b/datapath-windows/ovsext/Util.h @@ -38,6 +38,7 @@ #define OVS_TUNFLT_POOL_TAG 'WSVO' #define OVS_RECIRC_POOL_TAG 'CSVO' #define OVS_CT_POOL_TAG 'CTVO' +#define OVS_METER_TAG 'MEVO' #define OVS_GENEVE_POOL_TAG 'GNVO' #define OVS_IPFRAG_POOL_TAG 'FGVO' diff --git a/datapath-windows/ovsext/ovsext.vcxproj b/datapath-windows/ovsext/ovsext.vcxproj index 7a2cbd2de..6e54b71f9 100644 --- a/datapath-windows/ovsext/ovsext.vcxproj +++ b/datapath-windows/ovsext/ovsext.vcxproj @@ -164,6 +164,7 @@ + @@ -408,6 +409,7 @@ + diff --git a/lib/netlink-socket.c b/lib/netlink-socket.c index 93c1fa561..80da20d9f 100644 --- a/lib/netlink-socket.c +++ b/lib/netlink-socket.c @@ -1574,6 +1574,11 @@ do_lookup_genl_family(const char *name, struct nlattr **attrs, family_name = OVS_FLOW_FAMILY; family_version = OVS_FLOW_VERSION; family_attrmax = OVS_FLOW_ATTR_MAX; + } else if (!strcmp(name, OVS_METER_FAMILY)) { + family_id = OVS_WIN_NL_METER_FAMILY_ID; + family_name = OVS_METER_FAMILY; + family_version = OVS_METER_VERSION; + family_attrmax = __OVS_METER_ATTR_MAX; } else if (!strcmp(name, OVS_WIN_NETDEV_FAMILY)) { family_id = OVS_WIN_NL_NETDEV_FAMILY_ID; family_name = OVS_WIN_NETDEV_FAMILY;