From patchwork Sun Mar 13 11:26:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianli Lai X-Patchwork-Id: 108699 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C04DAA00BE; Sun, 13 Mar 2022 04:28:52 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4F75840698; Sun, 13 Mar 2022 04:28:52 +0100 (CET) Received: from smtp.tom.com (smtprz15.163.net [106.3.154.248]) by mails.dpdk.org (Postfix) with ESMTP id C6CD24068A for ; Sun, 13 Mar 2022 04:28:49 +0100 (CET) Received: from my-app02.tom.com (my-app02.tom.com [127.0.0.1]) by freemail02.tom.com (Postfix) with ESMTP id 4E317B00D31 for ; Sun, 13 Mar 2022 11:28:48 +0800 (CST) Received: from my-app02.tom.com (HELO smtp.tom.com) ([127.0.0.1]) by my-app02 (TOM SMTP Server) with SMTP ID -129513559 for ; Sun, 13 Mar 2022 11:28:48 +0800 (CST) Received: from antispam1.tom.com (unknown [172.25.16.55]) by freemail02.tom.com (Postfix) with ESMTP id 39AC6B00CBF for ; Sun, 13 Mar 2022 11:28:46 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tom.com; s=201807; t=1647142128; bh=gLAFB90Ctjzln59NUq12DlpMKVi/UXnZMaiRgn9HoNs=; h=From:To:Cc:Subject:Date:From; b=q9Q4Pw2MVQovzfZNCRvnUM4lxSLZH8L7mwy+lTxI/SNAO6V/u4RNVAp2XRx5v9Jp7 ASFT1vO9CStUnoLnqjo8WitYXEsTkXXAlbIBXH85crEoIu4Vxia0ShUCaH+KHNHqcT i6irPzGDAWg+ounAr/BbrO2FYYJZ3uhRdvgbILNA= Received: from antispam1.tom.com (antispam1.tom.com [127.0.0.1]) by antispam1.tom.com (Postfix) with ESMTP id 70E90D41626 for ; Sun, 13 Mar 2022 11:28:46 +0800 (CST) X-Virus-Scanned: Debian amavisd-new at antispam1.tom.com Received: from antispam1.tom.com ([127.0.0.1]) by antispam1.tom.com (antispam1.tom.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2gG0asB9pPNv for ; Sun, 13 Mar 2022 11:28:44 +0800 (CST) Received: from localhost.localdomain (unknown [59.41.75.211]) by antispam1.tom.com (Postfix) with ESMTPA id 52EE4D412D4; Sun, 13 Mar 2022 11:28:42 +0800 (CST) From: Tianli Lai To: dev@dpdk.org Cc: Ferruh Yigit , Bruce Richardson Subject: [PATCH] net/pcap: add snaplen argument Date: Sun, 13 Mar 2022 19:26:38 +0800 Message-Id: <20220313112638.3945-1-laitianli@tom.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org snaplen argument would set the length of each packet that will save to pcap file. Signed-off-by: Tianli Lai --- doc/guides/nics/pcap_ring.rst | 16 +++++++++- drivers/net/pcap/pcap_ethdev.c | 58 +++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/doc/guides/nics/pcap_ring.rst b/doc/guides/nics/pcap_ring.rst index acb1f00e30..8fb309212f 100644 --- a/doc/guides/nics/pcap_ring.rst +++ b/doc/guides/nics/pcap_ring.rst @@ -94,6 +94,13 @@ The different stream types are: iface=eth0 +* snaplen: Defines the length of one packet that will save pcap file. + the length of each packets can be snap by set this value. + this value between 64 and 65535. + + snaplen= + + Runtime Config Options ^^^^^^^^^^^^^^^^^^^^^^ @@ -132,6 +139,13 @@ Runtime Config Options In this case, one dummy rx queue is created for each tx queue argument passed + - Receive packets on Tx and set the length of packet, for example:: + + --vdev 'net_pcap0,tx_pcap=file_tx.pcap,snaplen=100' + +In this case, one dummy rx queue is created for each tx queue argument passed and the length of each packet would not over 100 byte. + + Examples of Usage ^^^^^^^^^^^^^^^^^ @@ -140,7 +154,7 @@ Read packets from one pcap file and write them to another: .. code-block:: console .//app/dpdk-testpmd -l 0-3 -n 4 \ - --vdev 'net_pcap0,rx_pcap=file_rx.pcap,tx_pcap=file_tx.pcap' \ + --vdev 'net_pcap0,rx_pcap=file_rx.pcap,tx_pcap=file_tx.pcap,snaplen=100' \ -- --port-topology=chained Read packets from a network interface and write them to a pcap file: diff --git a/drivers/net/pcap/pcap_ethdev.c b/drivers/net/pcap/pcap_ethdev.c index ec29fd6bc5..6e111518e7 100644 --- a/drivers/net/pcap/pcap_ethdev.c +++ b/drivers/net/pcap/pcap_ethdev.c @@ -33,10 +33,12 @@ #define ETH_PCAP_IFACE_ARG "iface" #define ETH_PCAP_PHY_MAC_ARG "phy_mac" #define ETH_PCAP_INFINITE_RX_ARG "infinite_rx" +#define ETH_PCAP_SNAPLEN_ARG "snaplen" #define ETH_PCAP_ARG_MAXLEN 64 #define RTE_PMD_PCAP_MAX_QUEUES 16 +#define RTE_PCAP_MIN_SNAPLEN 64 static char errbuf[PCAP_ERRBUF_SIZE]; static struct timespec start_time; @@ -93,6 +95,7 @@ struct pmd_internals { int single_iface; int phy_mac; unsigned int infinite_rx; + int snaplen; }; struct pmd_process_private { @@ -121,6 +124,14 @@ struct pmd_devargs_all { unsigned int is_rx_pcap; unsigned int is_rx_iface; unsigned int infinite_rx; + int snaplen; +}; +struct pmd_devargs_all devargs_all = { + .single_iface = 0, + .is_tx_pcap = 0, + .is_tx_iface = 0, + .infinite_rx = 0, + .snaplen = RTE_ETH_PCAP_SNAPSHOT_LEN, }; static const char *valid_arguments[] = { @@ -132,6 +143,7 @@ static const char *valid_arguments[] = { ETH_PCAP_IFACE_ARG, ETH_PCAP_PHY_MAC_ARG, ETH_PCAP_INFINITE_RX_ARG, + ETH_PCAP_SNAPLEN_ARG, NULL }; @@ -384,8 +396,10 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) pcap_dumper_t *dumper; unsigned char temp_data[RTE_ETH_PCAP_SNAPLEN]; size_t len, caplen; - - pp = rte_eth_devices[dumper_q->port_id].process_private; + struct pmd_internals *internals; + struct rte_eth_dev *dev = &rte_eth_devices[dumper_q->port_id]; + internals = dev->data->dev_private; + pp = dev->process_private; dumper = pp->tx_dumper[dumper_q->queue_id]; if (dumper == NULL || nb_pkts == 0) @@ -402,6 +416,8 @@ eth_pcap_tx_dumper(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) } calculate_timestamp(&header.ts); + if ((typeof(internals->snaplen))caplen > internals->snaplen) + caplen = internals->snaplen; header.len = len; header.caplen = caplen; /* rte_pktmbuf_read() returns a pointer to the data directly @@ -536,7 +552,7 @@ open_single_iface(const char *iface, pcap_t **pcap) } static int -open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper) +open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper, int snaplen) { pcap_t *tx_pcap; @@ -546,7 +562,7 @@ open_single_tx_pcap(const char *pcap_filename, pcap_dumper_t **dumper) * pcap holder. */ tx_pcap = pcap_open_dead_with_tstamp_precision(DLT_EN10MB, - RTE_ETH_PCAP_SNAPSHOT_LEN, PCAP_TSTAMP_PRECISION_NANO); + snaplen, PCAP_TSTAMP_PRECISION_NANO); if (tx_pcap == NULL) { PMD_LOG(ERR, "Couldn't create dead pcap"); return -1; @@ -627,7 +643,7 @@ eth_dev_start(struct rte_eth_dev *dev) if (!pp->tx_dumper[i] && strcmp(tx->type, ETH_PCAP_TX_PCAP_ARG) == 0) { if (open_single_tx_pcap(tx->name, - &pp->tx_dumper[i]) < 0) + &pp->tx_dumper[i], internals->snaplen) < 0) return -1; } else if (!pp->tx_pcap[i] && strcmp(tx->type, ETH_PCAP_TX_IFACE_ARG) == 0) { @@ -1055,7 +1071,7 @@ open_tx_pcap(const char *key, const char *value, void *extra_args) struct pmd_devargs *dumpers = extra_args; pcap_dumper_t *dumper; - if (open_single_tx_pcap(pcap_filename, &dumper) < 0) + if (open_single_tx_pcap(pcap_filename, &dumper, devargs_all.snaplen) < 0) return -1; if (add_queue(dumpers, pcap_filename, key, NULL, dumper) < 0) { @@ -1066,6 +1082,15 @@ open_tx_pcap(const char *key, const char *value, void *extra_args) return 0; } +static int +parse_uint_value(const char *key __rte_unused, const char *value, void *extra_args) +{ + char *end; + int *val = extra_args; + *val = strtoul(value, &end, 10); + return 0; +} + /* * Opens an interface for reading and writing */ @@ -1325,10 +1350,10 @@ eth_from_pcaps(struct rte_vdev_device *vdev, int ret; ret = eth_from_pcaps_common(vdev, devargs_all, &internals, ð_dev); - if (ret < 0) return ret; + internals->snaplen = devargs_all->snaplen; /* store weather we are using a single interface for rx/tx or not */ internals->single_iface = single_iface; @@ -1404,13 +1429,6 @@ pmd_pcap_probe(struct rte_vdev_device *dev) struct pmd_internals *internal; int ret = 0; - struct pmd_devargs_all devargs_all = { - .single_iface = 0, - .is_tx_pcap = 0, - .is_tx_iface = 0, - .infinite_rx = 0, - }; - name = rte_vdev_device_name(dev); PMD_LOG(INFO, "Initializing pmd_pcap for %s", name); @@ -1444,6 +1462,16 @@ pmd_pcap_probe(struct rte_vdev_device *dev) return -1; } + if (rte_kvargs_count(kvlist, ETH_PCAP_SNAPLEN_ARG) == 1) { + ret = rte_kvargs_process(kvlist, ETH_PCAP_SNAPLEN_ARG, + &parse_uint_value, &devargs_all.snaplen); + if (ret < 0) + goto free_kvlist; + if (devargs_all.snaplen < RTE_PCAP_MIN_SNAPLEN || + devargs_all.snaplen >= RTE_ETH_PCAP_SNAPSHOT_LEN) + devargs_all.snaplen = RTE_PCAP_MIN_SNAPLEN; + } + /* * If iface argument is passed we open the NICs and use them for * reading / writing @@ -1593,7 +1621,7 @@ pmd_pcap_probe(struct rte_vdev_device *dev) pp->tx_dumper[i] = dumpers.queue[i].dumper; pp->tx_pcap[i] = dumpers.queue[i].pcap; } - + internal->snaplen = devargs_all.snaplen; eth_dev->process_private = pp; eth_dev->rx_pkt_burst = eth_pcap_rx; if (devargs_all.is_tx_pcap)