From patchwork Tue Apr 16 05:15:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13631264 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D472539863 for ; Tue, 16 Apr 2024 05:15:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713244539; cv=none; b=fjcnDty0naE/eySe1BIMlZpu2HDpJKsWrtm5OFnHYoDIYV0rlAHzbO7vD0G2aXd/3dK21kiTwmRXzsX+wBcG2+ukfLwVwP0Nc2ZHlR1vWZcSqfQSVci7L86BEIFAKzL8YEsbvFfMARenDeMADUB9qlWtUPeJh95kcnXe7Ehn3Qc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713244539; c=relaxed/simple; bh=VKGpPryBp1qRxW8NvF0iF4kQbCQ+IZf4h907maxV4eg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XYqxm7V6zi9/R5aMeLPN/7JnnSQ1pomp/gP0vkNQLbMV2f/uvZL2BS3tKXHfkTnqodfF4Myags/oo1WJM+iHtxuSH18MAi+Z8wEZai3FbZmcTZXYlq6eSrzXS/iFH7QPUxY9qc3FQ0VpavA/fDRvHXGCWi84Z4ElU9FQsiSxb7o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk; spf=none smtp.mailfrom=davidwei.uk; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b=qUESjREs; arc=none smtp.client-ip=209.85.167.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b="qUESjREs" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3c70a55988dso1370801b6e.0 for ; Mon, 15 Apr 2024 22:15:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1713244537; x=1713849337; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GatKq/JJKGctfowNtMQbspH1D9Wc9gEWuRSzVkhw1u8=; b=qUESjREsEBk33kdeY+8tc1vA/YwM7bX3OQ9hyTAYRgT1TH09ZVWB3yAtxdpcfz8/sg EiAjyrMHLpd6EuMuryEbtnLU54DROjnOwUMIkkApYbJQ8OosUHIc4+u3L4iT1l7KRDIx qVKVnoIwbHV9l+/ZD9x/i/2KKHeXJfNxBa4BJO2x2L101223j+EU1BOE51Dn+rXSzfQo +8w385mg3QgSBk4726KzigrkZSiQjuHGuiwZfbTqM8cl0DcD1apFs3LBZQMhMj+GKS9y JuLQ9+OQXtQr75YJ5GJkKY4oFx2wKNpyGouH8Hr4h/GMgjuGhBU4fFj7Y/dkjybKNHc/ S4mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713244537; x=1713849337; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GatKq/JJKGctfowNtMQbspH1D9Wc9gEWuRSzVkhw1u8=; b=aQr7IZF+3Ms72a/Us2gUV5PAaduGH2g72iDedr5h/wTE6RHRo8yOV5WOQ7NbGoxrN2 p10TSolmB8QAvaFWmuNCHlg5Vl/FRH0fT8Vze7GF2331YkKqC8Bti1JEpV/MaBpmKnRD tglHlbH4B/bF/OsgpWiKeSKXcaxZSnqmJ5okyilVBgyzjF/qoxgVdG0agksyIs48EM0h v6DXMMm3eDF2x8Y9F0kIGXyNlpWJRI8EyTjexLDJ4pXceHHzIqGRmkidq7/YewDcO6HN 6BGOE7bTThMgx/4Lpu0KS4pDP0pY8N7E+Zb8P3ygzmsX3lcl06t9dUVroe3kjaAGsrbs NyYQ== X-Gm-Message-State: AOJu0Yw1w2RYhmPbzCmoDzoLQ/S7sUsJjYWp/LRpoaRq8pQEw3jJLJ/E SG3InAh0hIJlv1ONYXQb2ZZYIPIA+pOJf8NktrPDMdF1PQUM/3v4DO19u0dw/6rd0NSmr0wPdOB 9 X-Google-Smtp-Source: AGHT+IEwgV5qfdZOobZO/pCnlT9Dhiu6WQbXsaq9GDOkCpKSJo3dNSYezIOeiiyB3HsQcnDH7G6NZQ== X-Received: by 2002:a05:6808:2a4c:b0:3c6:f482:6526 with SMTP id fa12-20020a0568082a4c00b003c6f4826526mr10821031oib.2.1713244536754; Mon, 15 Apr 2024 22:15:36 -0700 (PDT) Received: from localhost (fwdproxy-prn-013.fbsv.net. [2a03:2880:ff:d::face:b00c]) by smtp.gmail.com with ESMTPSA id k69-20020a628448000000b006eab9ef5d4esm8477529pfd.50.2024.04.15.22.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 22:15:36 -0700 (PDT) From: David Wei To: netdev@vger.kernel.org Cc: Jakub Kicinski , "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH v1 1/2] netdevsim: add NAPI support Date: Mon, 15 Apr 2024 22:15:26 -0700 Message-ID: <20240416051527.1657233-2-dw@davidwei.uk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240416051527.1657233-1-dw@davidwei.uk> References: <20240416051527.1657233-1-dw@davidwei.uk> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add NAPI support to netdevim, similar to veth. * Add a nsim_rq rx queue structure to hold a NAPI instance and a skb queue. * During xmit, store the skb in the peer skb queue and schedule NAPI. * During napi_poll(), drain the skb queue and pass up the stack. * Add assoc between rxq and NAPI instance using netif_queue_set_napi(). Signed-off-by: David Wei --- drivers/net/netdevsim/netdev.c | 227 ++++++++++++++++++++++++++++-- drivers/net/netdevsim/netdevsim.h | 7 + 2 files changed, 223 insertions(+), 11 deletions(-) diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index d127856f8f36..c1a99825be91 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -28,11 +28,33 @@ #include "netdevsim.h" +#define NSIM_RING_SIZE 256 + +static int nsim_napi_rx(struct nsim_rq *rq, struct sk_buff *skb) +{ + if (list_count_nodes(&rq->skb_queue) > NSIM_RING_SIZE) { + dev_kfree_skb_any(skb); + return NET_RX_DROP; + } + + list_add_tail(&skb->list, &rq->skb_queue); + return NET_RX_SUCCESS; +} + +static int nsim_forward_skb(struct net_device *dev, struct sk_buff *skb, + struct nsim_rq *rq) +{ + return __dev_forward_skb(dev, skb) ?: nsim_napi_rx(rq, skb); +} + static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) { struct netdevsim *ns = netdev_priv(dev); + struct net_device *peer_dev; unsigned int len = skb->len; struct netdevsim *peer_ns; + struct nsim_rq *rq; + int rxq; rcu_read_lock(); if (!nsim_ipsec_tx(ns, skb)) @@ -42,10 +64,18 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) if (!peer_ns) goto out_drop_free; + peer_dev = peer_ns->netdev; + rxq = skb_get_queue_mapping(skb); + if (rxq >= peer_dev->num_rx_queues) + rxq = rxq % peer_dev->num_rx_queues; + rq = &peer_ns->rq[rxq]; + skb_tx_timestamp(skb); - if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP)) + if (unlikely(nsim_forward_skb(peer_dev, skb, rq) == NET_RX_DROP)) goto out_drop_cnt; + napi_schedule(&rq->napi); + rcu_read_unlock(); u64_stats_update_begin(&ns->syncp); ns->tx_packets++; @@ -300,25 +330,153 @@ static int nsim_get_iflink(const struct net_device *dev) return iflink; } +static int nsim_rcv(struct nsim_rq *rq, int budget) +{ + struct sk_buff *skb; + int i; + + for (i = 0; i < budget; i++) { + if (list_empty(&rq->skb_queue)) + break; + + skb = list_first_entry(&rq->skb_queue, struct sk_buff, list); + list_del(&skb->list); + + netif_receive_skb(skb); + } + + return i; +} + +static int nsim_poll(struct napi_struct *napi, int budget) +{ + struct nsim_rq *rq = container_of(napi, struct nsim_rq, napi); + int done; + + done = nsim_rcv(rq, budget); + + if (done < budget && napi_complete_done(napi, done)) { + if (unlikely(!list_empty(&rq->skb_queue))) + napi_schedule(&rq->napi); + } + + return done; +} + +static int nsim_create_page_pool(struct nsim_rq *rq) +{ + struct page_pool_params p = { + .order = 0, + .pool_size = NSIM_RING_SIZE, + .nid = NUMA_NO_NODE, + .dev = &rq->napi.dev->dev, + .napi = &rq->napi, + .dma_dir = DMA_BIDIRECTIONAL, + .netdev = rq->napi.dev, + }; + + rq->page_pool = page_pool_create(&p); + if (IS_ERR(rq->page_pool)) { + int err = PTR_ERR(rq->page_pool); + + rq->page_pool = NULL; + return err; + } + return 0; +} + +static int nsim_init_napi(struct netdevsim *ns) +{ + struct net_device *dev = ns->netdev; + struct nsim_rq *rq; + int err, i; + + for (i = 0; i < dev->num_rx_queues; i++) { + rq = &ns->rq[i]; + + netif_napi_add(dev, &rq->napi, nsim_poll); + } + + for (i = 0; i < dev->num_rx_queues; i++) { + rq = &ns->rq[i]; + + err = nsim_create_page_pool(rq); + if (err) + goto err_pp_destroy; + } + + return 0; + +err_pp_destroy: + while (i--) { + page_pool_destroy(ns->rq[i].page_pool); + ns->rq[i].page_pool = NULL; + } + + for (i = 0; i < dev->num_rx_queues; i++) + __netif_napi_del(&ns->rq[i].napi); + + return err; +} + +static void nsim_enable_napi(struct netdevsim *ns) +{ + int i; + + for (i = 0; i < ns->netdev->num_rx_queues; i++) { + struct nsim_rq *rq = &ns->rq[i]; + + netif_queue_set_napi(ns->netdev, i, + NETDEV_QUEUE_TYPE_RX, &rq->napi); + napi_enable(&rq->napi); + } +} + static int nsim_open(struct net_device *dev) { struct netdevsim *ns = netdev_priv(dev); - struct page_pool_params pp = { 0 }; + int err; + + err = nsim_init_napi(ns); + if (err) + return err; + + nsim_enable_napi(ns); - pp.pool_size = 128; - pp.dev = &dev->dev; - pp.dma_dir = DMA_BIDIRECTIONAL; - pp.netdev = dev; + netif_carrier_on(dev); - ns->pp = page_pool_create(&pp); - return PTR_ERR_OR_ZERO(ns->pp); + return 0; +} + +static void nsim_del_napi(struct netdevsim *ns) +{ + struct net_device *dev = ns->netdev; + int i; + + for (i = 0; i < dev->num_rx_queues; i++) { + struct nsim_rq *rq = &ns->rq[i]; + + napi_disable(&rq->napi); + __netif_napi_del(&rq->napi); + } + synchronize_net(); + + for (i = 0; i < dev->num_rx_queues; i++) { + page_pool_destroy(ns->rq[i].page_pool); + ns->rq[i].page_pool = NULL; + } } static int nsim_stop(struct net_device *dev) { struct netdevsim *ns = netdev_priv(dev); + struct netdevsim *peer = rtnl_dereference(ns->peer); + + netif_carrier_off(dev); + if (peer) + netif_carrier_off(peer->netdev); - page_pool_destroy(ns->pp); + nsim_del_napi(ns); return 0; } @@ -437,7 +595,7 @@ nsim_pp_hold_write(struct file *file, const char __user *data, if (!netif_running(ns->netdev) && val) { ret = -ENETDOWN; } else if (val) { - ns->page = page_pool_dev_alloc_pages(ns->pp); + ns->page = page_pool_dev_alloc_pages(ns->rq[0].page_pool); if (!ns->page) ret = -ENOMEM; } else { @@ -477,6 +635,46 @@ static void nsim_setup(struct net_device *dev) dev->xdp_features = NETDEV_XDP_ACT_HW_OFFLOAD; } +static int nsim_queue_init(struct netdevsim *ns) +{ + struct net_device *dev = ns->netdev; + int i; + + ns->rq = kvcalloc(dev->num_rx_queues, sizeof(*ns->rq), + GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); + if (!ns->rq) + return -ENOMEM; + + for (i = 0; i < dev->num_rx_queues; i++) + INIT_LIST_HEAD(&ns->rq[i].skb_queue); + + return 0; +} + +static void __nsim_skb_queue_purge(struct list_head *head) +{ + struct sk_buff *skb, *tmp; + + list_for_each_entry_safe(skb, tmp, head, list) { + list_del(&skb->list); + dev_kfree_skb_any(skb); + } +} + +static void nsim_queue_free(struct netdevsim *ns) +{ + struct net_device *dev = ns->netdev; + int i; + + for (i = 0; i < dev->num_rx_queues; i++) { + if (!list_empty(&ns->rq[i].skb_queue)) + __nsim_skb_queue_purge(&ns->rq[i].skb_queue); + } + + kvfree(ns->rq); + ns->rq = NULL; +} + static int nsim_init_netdevsim(struct netdevsim *ns) { struct mock_phc *phc; @@ -495,10 +693,14 @@ static int nsim_init_netdevsim(struct netdevsim *ns) goto err_phc_destroy; rtnl_lock(); - err = nsim_bpf_init(ns); + err = nsim_queue_init(ns); if (err) goto err_utn_destroy; + err = nsim_bpf_init(ns); + if (err) + goto err_rq_destroy; + nsim_macsec_init(ns); nsim_ipsec_init(ns); @@ -512,6 +714,8 @@ static int nsim_init_netdevsim(struct netdevsim *ns) nsim_ipsec_teardown(ns); nsim_macsec_teardown(ns); nsim_bpf_uninit(ns); +err_rq_destroy: + nsim_queue_free(ns); err_utn_destroy: rtnl_unlock(); nsim_udp_tunnels_info_destroy(ns->netdev); @@ -594,6 +798,7 @@ void nsim_destroy(struct netdevsim *ns) nsim_ipsec_teardown(ns); nsim_bpf_uninit(ns); } + nsim_queue_free(ns); rtnl_unlock(); if (nsim_dev_port_is_pf(ns->nsim_dev_port)) nsim_exit_netdevsim(ns); diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h index 7664ab823e29..87bf45ec4dd2 100644 --- a/drivers/net/netdevsim/netdevsim.h +++ b/drivers/net/netdevsim/netdevsim.h @@ -90,11 +90,18 @@ struct nsim_ethtool { struct ethtool_fecparam fec; }; +struct nsim_rq { + struct napi_struct napi; + struct list_head skb_queue; + struct page_pool *page_pool; +}; + struct netdevsim { struct net_device *netdev; struct nsim_dev *nsim_dev; struct nsim_dev_port *nsim_dev_port; struct mock_phc *phc; + struct nsim_rq *rq; u64 tx_packets; u64 tx_bytes; From patchwork Tue Apr 16 05:15:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13631265 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B020E39FD7 for ; Tue, 16 Apr 2024 05:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713244540; cv=none; b=kSK9Rr/civElztpUs7VrJi6L3qsjr8Ab/by1FFqgYIOJGhnRvhePwATC8zaWWagYG3wGTeLcPdhYsYrkbnvweA3KS90EM7v3dmbr350HGlq9JPVuJgqbwwDjQLoAup7FR1Ez/zcPGjH5ju2K20Mwib8JMv9JqkO+BP7T+up5TNU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713244540; c=relaxed/simple; bh=MfwabN8GFP7uKl9lFa9vQpoQpqTIW8cOwoTzIhU/Nbk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YocwnV1qxO039fiIzovKHvb0QzdBKxZnfcfftGTyto99w6PqMC1Y36MWWZiUm1BJe3Xz3oEgEqyd3rHMyiDRsPvGe8NaEID45Nu/WnGGeLn2KGpQalszwuW/l0ubBU5mA9WQRW0DceI1u3aiQ145eKXCZ6YiO5/cZPK+nAabbbQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk; spf=none smtp.mailfrom=davidwei.uk; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b=jO52yfZw; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b="jO52yfZw" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6f00f24f761so1153541b3a.3 for ; Mon, 15 Apr 2024 22:15:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1713244538; x=1713849338; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/PQb3BpYqjMgrRgLh2qbIFgqXM+Ujv0wGjlr43ONtBo=; b=jO52yfZwPz3EY44Tu2Fu2W3hk+SNvFp6CnMCkiwNSk+KPBet2NfJ5oiWazqC/X4mqV n946ORpJ3kBL2F9OgxMNNiQP4PvW6+I1QwJW3lRYo6UeL46u/KWmRlsIjqa1xawg3PJO t4ZUQqRoij5+Gzt5pgds0Ovw3tpgK8yW276J+7DiDSiyXh86rgM30TjGuiC+NnxfU8nF XKdEJpoNbOZfnPW7MSHBoEIavdQv1y8PygL9fLnQHf44FFfHwLx6tZLcylu6+8UWmIb6 pJkvjYbdD7ZgFQp7NHs1HSgxUop3WjhE6d9gewdPBWocpLD3xoc4tGmKz7ThPq/JeXZa GeHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713244538; x=1713849338; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/PQb3BpYqjMgrRgLh2qbIFgqXM+Ujv0wGjlr43ONtBo=; b=Wg6N9+kT5gCEOfQsXv/18Z+/vCfai9dUaZB43fNe6rQMk7XjTOPsk9/xSmrwStE9MP 5G1btPrTvrXXVboTBTTIOQsEA+IUuNybkkr9DMh3Khn2lq7WxX4qanaujiifm9EsOIx6 H0z7na6eyrzWvrmlHQ/cYVfJQStzmBtdbDImjN6lVRu0Tdf98Rt9gwb3kbAcKDtKws/n W+oC4Axrw4GpHn9ByvVkH6LELSeD+eWCCVoCS5v73S5k7XUPEihAZJbNr8VFC+8wHq5A X5H45Tb0LX4pWIVFSteKjbkNS8CK34YWzDvZL4sGSQ11sGHd7kqMqKOAvGOZCDiMQcxB lxTg== X-Gm-Message-State: AOJu0YzlkwZSde0CtDlXoWOkGWwtTTXNDD/ZMAqLq4U8y5+ZzeofqzwS EmkMMaRV93haPPeVzt3t2WTvQCDvY5apLEa+dyYpbyje0mT7ChBD/pQys7HMIGBTAVRqeJuw1U2 Z X-Google-Smtp-Source: AGHT+IF4x9YGe/V9dwrNflwFglCEL0X8iiSf2E99rJwndlrTg0h/IO0SmkktMD7qe4sHhiZmUKJmkA== X-Received: by 2002:a05:6a00:10cc:b0:6ed:6b11:4076 with SMTP id d12-20020a056a0010cc00b006ed6b114076mr11660612pfu.12.1713244537752; Mon, 15 Apr 2024 22:15:37 -0700 (PDT) Received: from localhost (fwdproxy-prn-009.fbsv.net. [2a03:2880:ff:9::face:b00c]) by smtp.gmail.com with ESMTPSA id b22-20020a056a000cd600b006ead792b6f2sm8154944pfv.1.2024.04.15.22.15.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 22:15:37 -0700 (PDT) From: David Wei To: netdev@vger.kernel.org Cc: Jakub Kicinski , "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH v1 2/2] net: selftest: add test for netdev netlink queue-get API Date: Mon, 15 Apr 2024 22:15:27 -0700 Message-ID: <20240416051527.1657233-3-dw@davidwei.uk> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240416051527.1657233-1-dw@davidwei.uk> References: <20240416051527.1657233-1-dw@davidwei.uk> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add a selftest for Netdev Netlink queue-get API. The ground truth is determined by sysfs. The test works with netdevsim by default or with a real device by setting NETIF. Minor changes to NetdevSimDev to accept the number of queues to create per port and pass through NetdevSimDev args in NetDrvEnv ctor. Signed-off-by: David Wei --- tools/testing/selftests/drivers/net/Makefile | 1 + .../selftests/drivers/net/lib/py/env.py | 10 ++- tools/testing/selftests/drivers/net/queues.py | 67 +++++++++++++++++++ tools/testing/selftests/net/lib/py/nsim.py | 4 +- 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100755 tools/testing/selftests/drivers/net/queues.py diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/selftests/drivers/net/Makefile index 379cdb1960a7..118a73650dbc 100644 --- a/tools/testing/selftests/drivers/net/Makefile +++ b/tools/testing/selftests/drivers/net/Makefile @@ -3,5 +3,6 @@ TEST_INCLUDES := $(wildcard lib/py/*.py) TEST_PROGS := stats.py +TEST_PROGS += queues.py include ../../lib.mk diff --git a/tools/testing/selftests/drivers/net/lib/py/env.py b/tools/testing/selftests/drivers/net/lib/py/env.py index e1abe9491daf..7187aca5c516 100644 --- a/tools/testing/selftests/drivers/net/lib/py/env.py +++ b/tools/testing/selftests/drivers/net/lib/py/env.py @@ -7,7 +7,7 @@ from lib.py import ip from lib.py import NetdevSimDev class NetDrvEnv: - def __init__(self, src_path): + def __init__(self, src_path, **kwargs): self._ns = None self.env = os.environ.copy() @@ -16,7 +16,7 @@ class NetDrvEnv: if 'NETIF' in self.env: self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0] else: - self._ns = NetdevSimDev() + self._ns = NetdevSimDev(**kwargs) self.dev = self._ns.nsims[0].dev self.ifindex = self.dev['ifindex'] @@ -50,3 +50,9 @@ class NetDrvEnv: else: self.env[k] = token k = None + + def dev_up(self): + ip(f"link set dev {self.dev['ifname']} up") + + def dev_down(self): + ip(f"link set dev {self.dev['ifname']} down") diff --git a/tools/testing/selftests/drivers/net/queues.py b/tools/testing/selftests/drivers/net/queues.py new file mode 100755 index 000000000000..de2820f2759a --- /dev/null +++ b/tools/testing/selftests/drivers/net/queues.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +from lib.py import ksft_run, ksft_in, ksft_true, ksft_eq, KsftSkipEx, KsftXfailEx +from lib.py import NetdevFamily, NlError +from lib.py import NetDrvEnv +from lib.py import cmd +import glob + +netnl = NetdevFamily() + + +def sys_get_queues(ifname) -> int: + folders = glob.glob(f'/sys/class/net/{ifname}/queues/rx-*') + return len(folders) + + +def nl_get_queues(cfg): + global netnl + queues = netnl.queue_get({'ifindex': cfg.ifindex}, dump=True) + if queues: + return len([q for q in queues if q['type'] == 'rx']) + return None + + +def get_queues(cfg) -> None: + global netnl + + queues = nl_get_queues(cfg) + if not queues: + raise KsftSkipEx("queue-get not supported by device") + + expected = sys_get_queues(cfg.dev['ifname']) + ksft_eq(queues, expected) + + +def addremove_queues(cfg) -> None: + global netnl + + queues = nl_get_queues(cfg) + if not queues: + raise KsftSkipEx("queue-get not supported by device") + + expected = sys_get_queues(cfg.dev['ifname']) + ksft_eq(queues, expected) + + # reduce queue count by 1 + expected = expected - 1 + cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}") + queues = nl_get_queues(cfg) + ksft_eq(queues, expected) + + # increase queue count by 1 + expected = expected + 1 + cmd(f"ethtool -L {cfg.dev['ifname']} combined {expected}") + queues = nl_get_queues(cfg) + ksft_eq(queues, expected) + + +def main() -> None: + with NetDrvEnv(__file__, queue_count=3) as cfg: + cfg.dev_up() + ksft_run([get_queues, addremove_queues], args=(cfg, )) + + +if __name__ == "__main__": + main() diff --git a/tools/testing/selftests/net/lib/py/nsim.py b/tools/testing/selftests/net/lib/py/nsim.py index 06896cdf7c18..f571a8b3139b 100644 --- a/tools/testing/selftests/net/lib/py/nsim.py +++ b/tools/testing/selftests/net/lib/py/nsim.py @@ -49,7 +49,7 @@ class NetdevSimDev: with open(fullpath, "w") as f: f.write(val) - def __init__(self, port_count=1, ns=None): + def __init__(self, port_count=1, queue_count=1, ns=None): # nsim will spawn in init_net, we'll set to actual ns once we switch it there self.ns = None @@ -59,7 +59,7 @@ class NetdevSimDev: addr = random.randrange(1 << 15) while True: try: - self.ctrl_write("new_device", "%u %u" % (addr, port_count)) + self.ctrl_write("new_device", "%u %u %u" % (addr, port_count, queue_count)) except OSError as e: if e.errno == errno.ENOSPC: addr = random.randrange(1 << 15)