From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013061.outbound.protection.outlook.com [40.107.201.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2ADCD31985F for ; Mon, 27 Oct 2025 17:40:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.201.61 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761586855; cv=fail; b=iWdFlVDZz/SvNvG8Y1SO0LhhuxtPTe/VMXpceOQVWeckUvzEV7CvoiXDL0A3WN2/ktVPYOpadb9YGdKkDKzPsobRbOPnHCiFkvgumf23VHAZS0ZdraMsnkh9f6VUcVxIOf1WQQfsHCv714k7DldsK6HUmh5Zao8EfuRdYJrRWs8= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761586855; c=relaxed/simple; bh=bHiCg/BjkpkZJ6ytoiv75maZNqN4oR7H16GhA5QbUsY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iYTKEl2h1f0C0iWXTD7clATfGh/PAor0YSZfblCAT03jZDuHq+Qs1jAS7QJg8z/HTgAuMDGVYfeN24ug0Gvkc8akXKP8mh3Af/7MYkU3+3Kr5kQDvG1YjbEeKtC/QrsxET83+684QDDYKMaOd7NJm21LpHZD8kEHT6aiiggQyjA= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=IKcmC5WE; arc=fail smtp.client-ip=40.107.201.61 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="IKcmC5WE" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=BddVNcBDPmqNGPyU8pv3YoOnZX/UZLKzc2HRMWZ2VVjIAMwY2A8sF8ng37AkNV4a/sX5WgxqqErYFh3koaZ6D6Cgr5+BtwpU9X/LQsPcMKMPSPgrUPkiKkz3nsZN6ISTdcHsUqzoGgodx4g10TAOOHhqKdUgiRSLz5lgLY+BlAkXw3k6lw3hoKj+uofCTP3Eo2RB+x3bFXM/jVY7BCCnUiuibuP5dhA5pcdE1nJlIWgZ8diMPt+H7H1QLQ+9XJuatAsNwkNSi96hsfy3KoqIiEoCUm0TZaoeguTy6m4IA47HOwIK4CL1Kx8czaRsgexUK6AWwMG/ZX/hZ1hw4Z4fUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=9u98HgZiqSLkr5k/LOV0I2VVciK98yQenTXH8GbBUk8=; b=v0N+zS1g+Hl/EL2mPAbI7KVSHkRlQB+33k2b9p4YM4SeRhE5qofXHuzNtXfKDgxRSOn1P93BUOHIXq3CKfH8XUF0/eeImmVbMIYZI/d0QfQy3eDD5Y0LEoWmumsJLuwc8JPBdvciXT+i075xNgtvlpKdec1pUaGOgVq0NYlAzwqi5Nj+6xabXXISckjS6lqH7GTB1Rilig6DbKVyK0LSaAz2Ip4PTPhPqQphNLX29KVeLpTOo656HPHi+2zM1/g9kMuSw8SJIOvwy5S/PlnS0UHwWNAMJ3qyRWw6sMvOFQwpmD40xXKf1+FgTewexWuW5RtNBW9JWrh8+huYSS57nA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9u98HgZiqSLkr5k/LOV0I2VVciK98yQenTXH8GbBUk8=; b=IKcmC5WErfOZ0RlX4KatG3aWw5gW/+G1o/2NoxCWvQaM+IGBTT/rwljVCmCttjEFADKs7vP5UVQ+UsLOEbe3TdwDDZCrFuW38S/+KayEzAZ1qIarNs6XGCHnQguJXHE+QUKglvAEHRbiWIrEsqdWQ/gMS22yZOMPriWTJRT+4DHr5uouSii1DkMhHy5uMnLkm63ZJSBn2rYuLKswvnl+KKp3czd+ygLpkFWWVM55uHz8YsHkahNrAYcFoFKhP0Z+J+3hkHyss0JVhfUVryxuRl9JtEZhkIW+4doChstPzfAOFyEjtLQ8ffH8KgZvjByvd0HhnHH4Um8bYFlFed5oJw== Received: from DS7P222CA0017.NAMP222.PROD.OUTLOOK.COM (2603:10b6:8:2e::26) by PH7PR12MB5784.namprd12.prod.outlook.com (2603:10b6:510:1d3::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9253.16; Mon, 27 Oct 2025 17:40:42 +0000 Received: from DS1PEPF0001708E.namprd03.prod.outlook.com (2603:10b6:8:2e:cafe::94) by DS7P222CA0017.outlook.office365.com (2603:10b6:8:2e::26) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9253.18 via Frontend Transport; Mon, 27 Oct 2025 17:40:42 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by DS1PEPF0001708E.mail.protection.outlook.com (10.167.17.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9275.10 via Frontend Transport; Mon, 27 Oct 2025 17:40:41 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.34; Mon, 27 Oct 2025 10:40:22 -0700 Received: from rnnvmail203.nvidia.com (10.129.68.9) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.20; Mon, 27 Oct 2025 10:40:22 -0700 Received: from vdi.nvidia.com (10.127.8.14) by mail.nvidia.com (10.129.68.9) with Microsoft SMTP Server id 15.2.2562.20 via Frontend Transport; Mon, 27 Oct 2025 10:40:20 -0700 From: Daniel Jurgens To: , , , , CC: , , , , , , , , , , , , Daniel Jurgens Subject: [PATCH net-next v6 07/12] virtio_net: Implement layer 2 ethtool flow rules Date: Mon, 27 Oct 2025 12:39:52 -0500 Message-ID: <20251027173957.2334-8-danielj@nvidia.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251027173957.2334-1-danielj@nvidia.com> References: <20251027173957.2334-1-danielj@nvidia.com> Precedence: bulk X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0001708E:EE_|PH7PR12MB5784:EE_ X-MS-Office365-Filtering-Correlation-Id: d6bf61e9-e862-459b-6309-08de157ff361 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|36860700013|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?GTU0yqNC1EHCfhTF6AL6oKBcQVyKeXAw2vS/i7DwCwDw+8fB+Ey6/CaHVgD7?= =?us-ascii?Q?Y5om9OooVbH8yg23XTEpNnZU93GMET76jlRQKbRDAe91wolyhG/JG/E57kvc?= =?us-ascii?Q?8JuBw3RmkznFF97Q5wA5FOl4IgbtuYDfQUYT64ICq3r6fpAYwuinj4Twf/z8?= =?us-ascii?Q?fLfPY8WpjLlzi5PTKSNyiXu8usXdOH9desowmQpQlHIbbuVJdNBnrQZzFygB?= =?us-ascii?Q?UbyO8TT2+CYzvwoiu8RDjZjH4a3kC4neAsv4kXC4Wrgzdp5onB9sbOVgJn07?= =?us-ascii?Q?BtGIMPgMf+bOleu5hAtVzBAbjawx9BQh9K0cCs1DcUnaaNWS18qCmeRMs+XN?= =?us-ascii?Q?F12GYTh8WAric1L4MbM/lM+RcjfsTV3eMftJjl2a6XpL0zv7Yz8LP8RCLKz+?= =?us-ascii?Q?JKjMIk426RGpEvGHXcRI/VfRUw5Ilkpxi/kgNsARaDOwUTIBGbwKuH3RSI5G?= =?us-ascii?Q?r10crhDZak/8QsijZNZfUbCG9/hE81D/ZQa7NzHjfZ3xTOG1Fh2WKfxIN08+?= =?us-ascii?Q?+dvYdurJSU+mS6WQ7342tU6zQgAtGDIuUpCITmmceal9/bVbe7X70UF7YsPd?= =?us-ascii?Q?Dp2Y3IgGLS/nZzgne/F6l9ZcAfz5M5RVUUZZrYvY6y1Cyih0aDjgtWh6aehP?= =?us-ascii?Q?gjqBrGo8+Pfwr9odNBo/Jebo4s81tFDRJhuKH1BT91PIC9RlV3gEj1aQXD/y?= =?us-ascii?Q?x6Fno99/8es10cRHwHhgnSrxbB2m26LDLsGG4hokfIO52slgYUCzdA2aPnN/?= =?us-ascii?Q?nRRd8o9SoS5Pmi6LD6AH7rBZvDzsvxziObH9FZTEAW7Edpn+rwY2pf4YFyqq?= =?us-ascii?Q?jW8b9G4yZtFSytT+Po6JhDm7auN8blF9SmzbgX0l9gZSfI0MoXboK/0sHHjo?= =?us-ascii?Q?ucUG6h4beHvWJaNWaci6ZRoeXRNDLpdvpFmDUJ47GVSYSYyBwNNEIXMO1qE6?= =?us-ascii?Q?YwfMAvCnYctark89b59etRMdemIS8dNF4H/4i4BxEw1nO8fjDtDjDchx4CqQ?= =?us-ascii?Q?WrQ2DU7/vULCs2MXnAnIGvCqlsn/tOJW+wyx6t27ib/I6jYt5a+ABSqYtnCB?= =?us-ascii?Q?lp2CKFfPFAq9mwg/7n+jLgSfCC5mXGm6PGfvHc6pZRRRqVWpI79b+Llh0BEa?= =?us-ascii?Q?vSzdSkpYou1X7+VjF+Z/cTFVzYZn1sVSsM3utfEaQoe6nivxlwnRmovbgQBT?= =?us-ascii?Q?ZtzaAK89uAaN95TfWdeU+JWtB5ry8Gr8YcRMmFuHL2Bm/QPKtqf3D4BTvTHs?= =?us-ascii?Q?ulvvddFAotMQ7RkcbwnbwZZv9a21Nt78XGXcybUrkD6n5Bv09OcGqGiJFQIg?= =?us-ascii?Q?4tQl0q/HRmzEP6m7JQw/fCWzcVd88YLAwsxZlXdX2/jghWmDa0udvHOJsxiQ?= =?us-ascii?Q?+aY/Lp4dUTLl0l01e7qF9prYVYdjr7YXYC4TlnzlHcgtCUb9movvP2LPAHCF?= =?us-ascii?Q?TmqJ/RofXjikd+pk1N6FJ6pJBAyjnx/kKrDnmiF1YH/QFXvBMThYWbRHsgqg?= =?us-ascii?Q?tuvKfRBnOQYwTinBUlIiaiq0EbQ6MbsW/PuExXdlEUamvBDv8KOa8nPFYHnz?= =?us-ascii?Q?LM2/vk6vuIhsOfPJEKU=3D?= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(1800799024)(36860700013)(7416014)(376014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Oct 2025 17:40:41.9993 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6bf61e9-e862-459b-6309-08de157ff361 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0001708E.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5784 Filtering a flow requires a classifier to match the packets, and a rule to filter on the matches. A classifier consists of one or more selectors. There is one selector per header type. A selector must only use fields set in the selector capability. If partial matching is supported, the classifier mask for a particular field can be a subset of the mask for that field in the capability. The rule consists of a priority, an action and a key. The key is a byte array containing headers corresponding to the selectors in the classifier. This patch implements ethtool rules for ethernet headers. Example: $ ethtool -U ens9 flow-type ether dst 08:11:22:33:44:54 action 30 Added rule with ID 1 The rule in the example directs received packets with the specified destination MAC address to rq 30. Signed-off-by: Daniel Jurgens Reviewed-by: Parav Pandit Reviewed-by: Shahar Shitrit --- v4: - Fixed double free bug in error flows - Build bug on for classifier struct ordering. - (u8 *) to (void *) casting. - Documentation in UAPI - Answered questions about overflow with no changes. v6: - Fix sparse warning "array of flexible structures" Jakub K/Simon H --- drivers/net/virtio_net.c | 461 +++++++++++++++++++++++++++++ include/uapi/linux/virtio_net_ff.h | 50 ++++ 2 files changed, 511 insertions(+) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 10700e447959..73194b51c318 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -284,6 +284,11 @@ static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = { VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits), }; +struct virtnet_ethtool_ff { + struct xarray rules; + int num_rules; +}; + #define VIRTNET_FF_ETHTOOL_GROUP_PRIORITY 1 #define VIRTNET_FF_MAX_GROUPS 1 @@ -293,8 +298,16 @@ struct virtnet_ff { struct virtio_net_ff_cap_data *ff_caps; struct virtio_net_ff_cap_mask_data *ff_mask; struct virtio_net_ff_actions *ff_actions; + struct xarray classifiers; + int num_classifiers; + struct virtnet_ethtool_ff ethtool; }; +static int virtnet_ethtool_flow_insert(struct virtnet_ff *ff, + struct ethtool_rx_flow_spec *fs, + u16 curr_queue_pairs); +static int virtnet_ethtool_flow_remove(struct virtnet_ff *ff, int location); + #define VIRTNET_Q_TYPE_RX 0 #define VIRTNET_Q_TYPE_TX 1 #define VIRTNET_Q_TYPE_CQ 2 @@ -5632,6 +5645,21 @@ static u32 virtnet_get_rx_ring_count(struct net_device *dev) return vi->curr_queue_pairs; } +static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info) +{ + struct virtnet_info *vi = netdev_priv(dev); + + switch (info->cmd) { + case ETHTOOL_SRXCLSRLINS: + return virtnet_ethtool_flow_insert(&vi->ff, &info->fs, + vi->curr_queue_pairs); + case ETHTOOL_SRXCLSRLDEL: + return virtnet_ethtool_flow_remove(&vi->ff, info->fs.location); + } + + return -EOPNOTSUPP; +} + static const struct ethtool_ops virtnet_ethtool_ops = { .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES | ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX, @@ -5658,6 +5686,7 @@ static const struct ethtool_ops virtnet_ethtool_ops = { .get_rxfh_fields = virtnet_get_hashflow, .set_rxfh_fields = virtnet_set_hashflow, .get_rx_ring_count = virtnet_get_rx_ring_count, + .set_rxnfc = virtnet_set_rxnfc, }; static void virtnet_get_queue_stats_rx(struct net_device *dev, int i, @@ -6769,6 +6798,427 @@ static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = { .xmo_rx_hash = virtnet_xdp_rx_hash, }; +struct virtnet_ethtool_rule { + struct ethtool_rx_flow_spec flow_spec; + u32 classifier_id; +}; + +/* The classifier struct must be the last field in this struct */ +struct virtnet_classifier { + size_t size; + u32 id; + struct virtio_net_resource_obj_ff_classifier classifier; +}; + +static_assert(sizeof(struct virtnet_classifier) == + ALIGN(offsetofend(struct virtnet_classifier, classifier), + __alignof__(struct virtnet_classifier)), + "virtnet_classifier: classifier must be the last member"); + +static bool check_mask_vs_cap(const void *m, const void *c, + u16 len, bool partial) +{ + const u8 *mask = m; + const u8 *cap = c; + int i; + + for (i = 0; i < len; i++) { + if (partial && ((mask[i] & cap[i]) != mask[i])) + return false; + if (!partial && mask[i] != cap[i]) + return false; + } + + return true; +} + +static +struct virtio_net_ff_selector *get_selector_cap(const struct virtnet_ff *ff, + u8 selector_type) +{ + struct virtio_net_ff_selector *sel; + void *buf; + int i; + + buf = &ff->ff_mask->selectors; + sel = buf; + + for (i = 0; i < ff->ff_mask->count; i++) { + if (sel->type == selector_type) + return sel; + + buf += sizeof(struct virtio_net_ff_selector) + sel->length; + sel = buf; + } + + return NULL; +} + +static bool validate_eth_mask(const struct virtnet_ff *ff, + const struct virtio_net_ff_selector *sel, + const struct virtio_net_ff_selector *sel_cap) +{ + bool partial_mask = !!(sel_cap->flags & VIRTIO_NET_FF_MASK_F_PARTIAL_MASK); + struct ethhdr *cap, *mask; + struct ethhdr zeros = {}; + + cap = (struct ethhdr *)&sel_cap->mask; + mask = (struct ethhdr *)&sel->mask; + + if (memcmp(&zeros.h_dest, mask->h_dest, sizeof(zeros.h_dest)) && + !check_mask_vs_cap(mask->h_dest, cap->h_dest, + sizeof(mask->h_dest), partial_mask)) + return false; + + if (memcmp(&zeros.h_source, mask->h_source, sizeof(zeros.h_source)) && + !check_mask_vs_cap(mask->h_source, cap->h_source, + sizeof(mask->h_source), partial_mask)) + return false; + + if (mask->h_proto && + !check_mask_vs_cap(&mask->h_proto, &cap->h_proto, + sizeof(__be16), partial_mask)) + return false; + + return true; +} + +static bool validate_mask(const struct virtnet_ff *ff, + const struct virtio_net_ff_selector *sel) +{ + struct virtio_net_ff_selector *sel_cap = get_selector_cap(ff, sel->type); + + if (!sel_cap) + return false; + + switch (sel->type) { + case VIRTIO_NET_FF_MASK_TYPE_ETH: + return validate_eth_mask(ff, sel, sel_cap); + } + + return false; +} + +static int setup_classifier(struct virtnet_ff *ff, struct virtnet_classifier *c) +{ + int err; + + err = xa_alloc(&ff->classifiers, &c->id, c, + XA_LIMIT(0, le32_to_cpu(ff->ff_caps->classifiers_limit) - 1), + GFP_KERNEL); + if (err) + return err; + + err = virtio_admin_obj_create(ff->vdev, + VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER, + c->id, + VIRTIO_ADMIN_GROUP_TYPE_SELF, + 0, + &c->classifier, + c->size); + if (err) + goto err_xarray; + + return 0; + +err_xarray: + xa_erase(&ff->classifiers, c->id); + + return err; +} + +static void destroy_classifier(struct virtnet_ff *ff, + u32 classifier_id) +{ + struct virtnet_classifier *c; + + c = xa_load(&ff->classifiers, classifier_id); + if (c) { + virtio_admin_obj_destroy(ff->vdev, + VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER, + c->id, + VIRTIO_ADMIN_GROUP_TYPE_SELF, + 0); + + xa_erase(&ff->classifiers, c->id); + kfree(c); + } +} + +static void destroy_ethtool_rule(struct virtnet_ff *ff, + struct virtnet_ethtool_rule *eth_rule) +{ + ff->ethtool.num_rules--; + + virtio_admin_obj_destroy(ff->vdev, + VIRTIO_NET_RESOURCE_OBJ_FF_RULE, + eth_rule->flow_spec.location, + VIRTIO_ADMIN_GROUP_TYPE_SELF, + 0); + + xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location); + destroy_classifier(ff, eth_rule->classifier_id); + kfree(eth_rule); +} + +static int insert_rule(struct virtnet_ff *ff, + struct virtnet_ethtool_rule *eth_rule, + u32 classifier_id, + const u8 *key, + size_t key_size) +{ + struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec; + struct virtio_net_resource_obj_ff_rule *ff_rule; + int err; + + ff_rule = kzalloc(sizeof(*ff_rule) + key_size, GFP_KERNEL); + if (!ff_rule) + return -ENOMEM; + + /* Intentionally leave the priority as 0. All rules have the same + * priority. + */ + ff_rule->group_id = cpu_to_le32(VIRTNET_FF_ETHTOOL_GROUP_PRIORITY); + ff_rule->classifier_id = cpu_to_le32(classifier_id); + ff_rule->key_length = (u8)key_size; + ff_rule->action = fs->ring_cookie == RX_CLS_FLOW_DISC ? + VIRTIO_NET_FF_ACTION_DROP : + VIRTIO_NET_FF_ACTION_RX_VQ; + ff_rule->vq_index = fs->ring_cookie != RX_CLS_FLOW_DISC ? + cpu_to_le16(fs->ring_cookie) : 0; + memcpy(&ff_rule->keys, key, key_size); + + err = virtio_admin_obj_create(ff->vdev, + VIRTIO_NET_RESOURCE_OBJ_FF_RULE, + fs->location, + VIRTIO_ADMIN_GROUP_TYPE_SELF, + 0, + ff_rule, + sizeof(*ff_rule) + key_size); + if (err) + goto err_ff_rule; + + eth_rule->classifier_id = classifier_id; + ff->ethtool.num_rules++; + kfree(ff_rule); + + return 0; + +err_ff_rule: + kfree(ff_rule); + + return err; +} + +static u32 flow_type_mask(u32 flow_type) +{ + return flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS); +} + +static bool supported_flow_type(const struct ethtool_rx_flow_spec *fs) +{ + switch (fs->flow_type) { + case ETHER_FLOW: + return true; + } + + return false; +} + +static int validate_flow_input(struct virtnet_ff *ff, + const struct ethtool_rx_flow_spec *fs, + u16 curr_queue_pairs) +{ + /* Force users to use RX_CLS_LOC_ANY - don't allow specific locations */ + if (fs->location != RX_CLS_LOC_ANY) + return -EOPNOTSUPP; + + if (fs->ring_cookie != RX_CLS_FLOW_DISC && + fs->ring_cookie >= curr_queue_pairs) + return -EINVAL; + + if (fs->flow_type != flow_type_mask(fs->flow_type)) + return -EOPNOTSUPP; + + if (!supported_flow_type(fs)) + return -EOPNOTSUPP; + + return 0; +} + +static void calculate_flow_sizes(struct ethtool_rx_flow_spec *fs, + size_t *key_size, size_t *classifier_size, + int *num_hdrs) +{ + *num_hdrs = 1; + *key_size = sizeof(struct ethhdr); + /* + * The classifier size is the size of the classifier header, a selector + * header for each type of header in the match criteria, and each header + * providing the mask for matching against. + */ + *classifier_size = *key_size + + sizeof(struct virtio_net_resource_obj_ff_classifier) + + sizeof(struct virtio_net_ff_selector) * (*num_hdrs); +} + +static void setup_eth_hdr_key_mask(struct virtio_net_ff_selector *selector, + u8 *key, + const struct ethtool_rx_flow_spec *fs) +{ + struct ethhdr *eth_m = (struct ethhdr *)&selector->mask; + struct ethhdr *eth_k = (struct ethhdr *)key; + + selector->type = VIRTIO_NET_FF_MASK_TYPE_ETH; + selector->length = sizeof(struct ethhdr); + + memcpy(eth_m, &fs->m_u.ether_spec, sizeof(*eth_m)); + memcpy(eth_k, &fs->h_u.ether_spec, sizeof(*eth_k)); +} + +static int +validate_classifier_selectors(struct virtnet_ff *ff, + struct virtio_net_resource_obj_ff_classifier *classifier, + int num_hdrs) +{ + struct virtio_net_ff_selector *selector = (void *)classifier->selectors; + + for (int i = 0; i < num_hdrs; i++) { + if (!validate_mask(ff, selector)) + return -EINVAL; + + selector = (((void *)selector) + sizeof(*selector) + + selector->length); + } + + return 0; +} + +static int build_and_insert(struct virtnet_ff *ff, + struct virtnet_ethtool_rule *eth_rule) +{ + struct virtio_net_resource_obj_ff_classifier *classifier; + struct ethtool_rx_flow_spec *fs = ð_rule->flow_spec; + struct virtio_net_ff_selector *selector; + struct virtnet_classifier *c; + size_t classifier_size; + size_t key_size; + int num_hdrs; + u8 *key; + int err; + + calculate_flow_sizes(fs, &key_size, &classifier_size, &num_hdrs); + + key = kzalloc(key_size, GFP_KERNEL); + if (!key) + return -ENOMEM; + + /* + * virtio_net_ff_obj_ff_classifier is already included in the + * classifier_size. + */ + c = kzalloc(classifier_size + + sizeof(struct virtnet_classifier) - + sizeof(struct virtio_net_resource_obj_ff_classifier), + GFP_KERNEL); + if (!c) { + kfree(key); + return -ENOMEM; + } + + c->size = classifier_size; + classifier = &c->classifier; + classifier->count = num_hdrs; + selector = (void *)&classifier->selectors[0]; + + setup_eth_hdr_key_mask(selector, key, fs); + + err = validate_classifier_selectors(ff, classifier, num_hdrs); + if (err) + goto err_key; + + err = setup_classifier(ff, c); + if (err) + goto err_classifier; + + err = insert_rule(ff, eth_rule, c->id, key, key_size); + if (err) { + /* destroy_classifier will free the classifier */ + destroy_classifier(ff, c->id); + goto err_key; + } + + return 0; + +err_classifier: + kfree(c); +err_key: + kfree(key); + + return err; +} + +static int virtnet_ethtool_flow_insert(struct virtnet_ff *ff, + struct ethtool_rx_flow_spec *fs, + u16 curr_queue_pairs) +{ + struct virtnet_ethtool_rule *eth_rule; + int err; + + if (!ff->ff_supported) + return -EOPNOTSUPP; + + err = validate_flow_input(ff, fs, curr_queue_pairs); + if (err) + return err; + + eth_rule = kzalloc(sizeof(*eth_rule), GFP_KERNEL); + if (!eth_rule) + return -ENOMEM; + + err = xa_alloc(&ff->ethtool.rules, &fs->location, eth_rule, + XA_LIMIT(0, le32_to_cpu(ff->ff_caps->rules_limit) - 1), + GFP_KERNEL); + if (err) + goto err_rule; + + eth_rule->flow_spec = *fs; + + err = build_and_insert(ff, eth_rule); + if (err) + goto err_xa; + + return err; + +err_xa: + xa_erase(&ff->ethtool.rules, eth_rule->flow_spec.location); + +err_rule: + fs->location = RX_CLS_LOC_ANY; + kfree(eth_rule); + + return err; +} + +static int virtnet_ethtool_flow_remove(struct virtnet_ff *ff, int location) +{ + struct virtnet_ethtool_rule *eth_rule; + int err = 0; + + if (!ff->ff_supported) + return -EOPNOTSUPP; + + eth_rule = xa_load(&ff->ethtool.rules, location); + if (!eth_rule) { + err = -ENOENT; + goto out; + } + + destroy_ethtool_rule(ff, eth_rule); +out: + return err; +} + static size_t get_mask_size(u16 type) { switch (type) { @@ -6916,6 +7366,8 @@ static void virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) if (err) goto err_ff_action; + xa_init_flags(&ff->classifiers, XA_FLAGS_ALLOC); + xa_init_flags(&ff->ethtool.rules, XA_FLAGS_ALLOC); ff->vdev = vdev; ff->ff_supported = true; @@ -6935,9 +7387,18 @@ static void virtnet_ff_init(struct virtnet_ff *ff, struct virtio_device *vdev) static void virtnet_ff_cleanup(struct virtnet_ff *ff) { + struct virtnet_ethtool_rule *eth_rule; + unsigned long i; + if (!ff->ff_supported) return; + xa_for_each(&ff->ethtool.rules, i, eth_rule) + destroy_ethtool_rule(ff, eth_rule); + + xa_destroy(&ff->ethtool.rules); + xa_destroy(&ff->classifiers); + virtio_admin_obj_destroy(ff->vdev, VIRTIO_NET_RESOURCE_OBJ_FF_GROUP, VIRTNET_FF_ETHTOOL_GROUP_PRIORITY, diff --git a/include/uapi/linux/virtio_net_ff.h b/include/uapi/linux/virtio_net_ff.h index 6d1f953c2b46..c98aa4942bee 100644 --- a/include/uapi/linux/virtio_net_ff.h +++ b/include/uapi/linux/virtio_net_ff.h @@ -13,6 +13,8 @@ #define VIRTIO_NET_FF_ACTION_CAP 0x802 #define VIRTIO_NET_RESOURCE_OBJ_FF_GROUP 0x0200 +#define VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER 0x0201 +#define VIRTIO_NET_RESOURCE_OBJ_FF_RULE 0x0202 /** * struct virtio_net_ff_cap_data - Flow filter resource capability limits @@ -103,4 +105,52 @@ struct virtio_net_resource_obj_ff_group { __le16 group_priority; }; +/** + * struct virtio_net_resource_obj_ff_classifier - Flow filter classifier object + * @count: number of selector entries in @selectors + * @reserved: must be set to 0 by the driver and ignored by the device + * @selectors: array of selector descriptors that define match masks + * + * Payload for the VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER administrative object. + * Each selector describes a header mask used to match packets + * (see struct virtio_net_ff_selector). Selectors appear in the order they are + * to be applied. + */ +struct virtio_net_resource_obj_ff_classifier { + __u8 count; + __u8 reserved[7]; + __u8 selectors[]; +}; + +/** + * struct virtio_net_resource_obj_ff_rule - Flow filter rule object + * @group_id: identifier of the target flow filter group + * @classifier_id: identifier of the classifier referenced by this rule + * @rule_priority: relative priority of this rule within the group + * @key_length: number of bytes in @keys + * @action: action to perform, one of VIRTIO_NET_FF_ACTION_* + * @reserved: must be set to 0 by the driver and ignored by the device + * @vq_index: RX virtqueue index for VIRTIO_NET_FF_ACTION_RX_VQ, 0 otherwise + * @reserved1: must be set to 0 by the driver and ignored by the device + * @keys: concatenated key bytes matching the classifier's selectors order + * + * Payload for the VIRTIO_NET_RESOURCE_OBJ_FF_RULE administrative object. + * @group_id and @classifier_id refer to previously created objects of types + * VIRTIO_NET_RESOURCE_OBJ_FF_GROUP and VIRTIO_NET_RESOURCE_OBJ_FF_CLASSIFIER + * respectively. The key bytes are compared against packet headers using the + * masks provided by the classifier's selectors. Multi-byte fields are + * little-endian. + */ +struct virtio_net_resource_obj_ff_rule { + __le32 group_id; + __le32 classifier_id; + __u8 rule_priority; + __u8 key_length; /* length of key in bytes */ + __u8 action; + __u8 reserved; + __le16 vq_index; + __u8 reserved1[2]; + __u8 keys[]; +}; + #endif -- 2.50.1