From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 24EC7A00C2; Wed, 4 Jan 2023 04:38:52 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 89AFF42D39; Wed, 4 Jan 2023 04:38:26 +0100 (CET) Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by mails.dpdk.org (Postfix) with ESMTP id 5950E42D2E for ; Wed, 4 Jan 2023 04:38:24 +0100 (CET) Received: by mail-pj1-f41.google.com with SMTP id c8-20020a17090a4d0800b00225c3614161so30616658pjg.5 for ; Tue, 03 Jan 2023 19:38:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; 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=op4SdEQmE2ugtRjm8ESSFLo+a/YIQlx1f0l6KaDiPqQ=; b=HAAyG7wNukF7s8J/7+n4nQ6Ja8PtgAX+Y18XhEcS2FSVP3sd32fD0/D9pQmtUVwKz8 DrEmTZH9zv2OUloIIHnS9U9Gn2QV8Rraq5tLSlNFm4TA+Zz4AyIhLUPWaRUmuCtTCQjb z3s+svFpgsT+k7tMpy43fql/Wy22+JZJ7MH/5uPelGWgPPYLrHsei4WSRgY7v8F5sLOs O4b6pIvpmeEjTSozc9xlfbJQtW0ChT60vLPJ1DiWkbv+5ed5hfNGeMeo4r3OhliozzUE 5XP85AL6D/+4hBpKBCWQiQIw+6LYV6Wq5xG2LhovffhBopb+UwMidkz1KOsG6Aviwk4w e0rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=op4SdEQmE2ugtRjm8ESSFLo+a/YIQlx1f0l6KaDiPqQ=; b=2YuF5b81MMu2Lo4CPK31MOPeaZwdDnu2WkDuha/BBqCfxgNHe9wvjiXsZo27z1NYta RvLhFpiAi/l4dJ6Fvs7+VVY08yBVgIT9G1r4stotbhzMM1gjKEobiO1g03nv9F++S1RW bic/Zl4VqmcuIPPYn+3AtRdi4nXLLJEtyPyjwenIDDqXjmfgmlaLQNXtUE4vIP4lAuLn weQciTi7VCB/vO6KSjty8uL+uPqInyh+j6taR08vqEWdT9ZG6gEqI9dT+rTDL0kCZldQ t6j02VciudoIvT16MXxNNrMS3NMBYikNz0BQ9TsJknODvTPNwIdQ8kn6ER9+MsILYzoF VZqw== X-Gm-Message-State: AFqh2kpOLJvj76HZT5IK8u3kA59fZO+ExB7WghPfPUv8ZPUDhjqfZcuO /qllIe/ojNmCf1VnxjZbRZqkEonUlJFyw4aaRjU= X-Google-Smtp-Source: AMrXdXtljL5HnxpB9PNK7scGMb1sdOSG2K2Df6LHeva0+g5kMIbup4xMaD529OvOmbMM8cj5J3w1Tg== X-Received: by 2002:a17:902:654e:b0:185:441e:4cef with SMTP id d14-20020a170902654e00b00185441e4cefmr46649969pln.31.1672803503562; Tue, 03 Jan 2023 19:38:23 -0800 (PST) Received: from hermes.local (204-195-120-218.wavecable.com. [204.195.120.218]) by smtp.gmail.com with ESMTPSA id c10-20020a170903234a00b0017f73caf588sm23091347plh.218.2023.01.03.19.38.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 19:38:22 -0800 (PST) From: Stephen Hemminger To: Ben Magistro Cc: dev@dpdk.org, Stephen Hemminger Subject: [PATCH v2 5/6] pcapng: require per-interface information Date: Tue, 3 Jan 2023 19:38:14 -0800 Message-Id: <20230104033815.35496-6-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230104033815.35496-1-stephen@networkplumber.org> References: <20230102162441.6205-1-koncept1@gmail.com> <20230104033815.35496-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 This changes the API for how pcapng is used. Before each interface was automatically added to the capture file. Now the application must add each interface. Note: API changes are allowed because this is an experimental interface. This allows application to specify extra meta data like interface name, description and packet filter. Signed-off-by: Stephen Hemminger --- app/test/test_pcapng.c | 7 ++++ lib/pcapng/rte_pcapng.c | 79 ++++++++++++++++++++++++++--------------- lib/pcapng/rte_pcapng.h | 25 +++++++++++++ lib/pcapng/version.map | 1 + 4 files changed, 84 insertions(+), 28 deletions(-) diff --git a/app/test/test_pcapng.c b/app/test/test_pcapng.c index a7acbdc0586d..edba46d1fe0f 100644 --- a/app/test/test_pcapng.c +++ b/app/test/test_pcapng.c @@ -109,6 +109,13 @@ test_setup(void) return -1; } + /* Add interface to the file */ + if (rte_pcapng_add_interface(pcapng, port_id, + NULL, NULL, NULL) != 0) { + fprintf(stderr, "can not add port %u\n", port_id); + return -1; + } + /* Make a pool for cloned packets */ mp = rte_pktmbuf_pool_create_by_ops("pcapng_test_pool", IOV_MAX + NUM_PACKETS, 0, 0, diff --git a/lib/pcapng/rte_pcapng.c b/lib/pcapng/rte_pcapng.c index 80d08e1a3bde..ea004939e63e 100644 --- a/lib/pcapng/rte_pcapng.c +++ b/lib/pcapng/rte_pcapng.c @@ -32,6 +32,9 @@ /* Format of the capture file handle */ struct rte_pcapng { int outfd; /* output file */ + + unsigned int ports; /* number of interfaces added */ + /* DPDK port id to interface index in file */ uint32_t port_index[RTE_MAX_ETHPORTS]; }; @@ -185,8 +188,10 @@ pcapng_section_block(rte_pcapng_t *self, } /* Write an interface block for a DPDK port */ -static int -pcapng_add_interface(rte_pcapng_t *self, uint16_t port) +int +rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, + const char *ifname, const char *ifdescr, + const char *filter) { struct pcapng_interface_block *hdr; struct rte_eth_dev_info dev_info; @@ -197,7 +202,7 @@ pcapng_add_interface(rte_pcapng_t *self, uint16_t port) const uint8_t tsresol = 9; /* nanosecond resolution */ uint32_t len; void *buf; - char ifname[IF_NAMESIZE]; + char ifname_buf[IF_NAMESIZE]; char ifhw[256]; uint64_t speed = 0; @@ -205,8 +210,14 @@ pcapng_add_interface(rte_pcapng_t *self, uint16_t port) return -1; /* make something like an interface name */ - if (if_indextoname(dev_info.if_index, ifname) == NULL) - snprintf(ifname, IF_NAMESIZE, "dpdk:%u", port); + if (ifname == NULL) { + /* Use kernel name if available */ + ifname = if_indextoname(dev_info.if_index, ifname_buf); + if (ifname == NULL) { + snprintf(ifname_buf, IF_NAMESIZE, "dpdk:%u", port); + ifname = ifname_buf; + } + } /* make a useful device hardware string */ dev = dev_info.device; @@ -230,10 +241,14 @@ pcapng_add_interface(rte_pcapng_t *self, uint16_t port) len += pcapng_optlen(sizeof(tsresol)); /* timestamp */ len += pcapng_optlen(strlen(ifname)); /* ifname */ + if (ifdescr) + len += pcapng_optlen(strlen(ifdescr)); if (ea) len += pcapng_optlen(RTE_ETHER_ADDR_LEN); /* macaddr */ if (speed != 0) len += pcapng_optlen(sizeof(uint64_t)); + if (filter) + len += pcapng_optlen(strlen(filter) + 1); if (dev) len += pcapng_optlen(strlen(ifhw)); @@ -256,6 +271,9 @@ pcapng_add_interface(rte_pcapng_t *self, uint16_t port) &tsresol, sizeof(tsresol)); opt = pcapng_add_option(opt, PCAPNG_IFB_NAME, ifname, strlen(ifname)); + if (ifdescr) + opt = pcapng_add_option(opt, PCAPNG_IFB_DESCRIPTION, + ifdescr, strlen(ifdescr)); if (ea) opt = pcapng_add_option(opt, PCAPNG_IFB_MACADDR, ea, RTE_ETHER_ADDR_LEN); @@ -265,31 +283,29 @@ pcapng_add_interface(rte_pcapng_t *self, uint16_t port) if (dev) opt = pcapng_add_option(opt, PCAPNG_IFB_HARDWARE, ifhw, strlen(ifhw)); + if (filter) { + /* Encoding is that the first octet indicates string vs BPF */ + size_t len; + char *buf; + + len = strlen(filter) + 1; + buf = alloca(len); + *buf = '\0'; + memcpy(buf + 1, filter, len); + + opt = pcapng_add_option(opt, PCAPNG_IFB_FILTER, + buf, len); + } + opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0); /* clone block_length after optionsa */ memcpy(opt, &hdr->block_length, sizeof(uint32_t)); - return write(self->outfd, buf, len); -} - -/* - * Write the list of possible interfaces at the start - * of the file. - */ -static int -pcapng_interfaces(rte_pcapng_t *self) -{ - uint16_t port_id; - uint16_t index = 0; + /* remember the file index */ + self->port_index[port] = self->ports++; - RTE_ETH_FOREACH_DEV(port_id) { - /* The list if ports in pcapng needs to be contiguous */ - self->port_index[port_id] = index++; - if (pcapng_add_interface(self, port_id) < 0) - return -1; - } - return 0; + return write(self->outfd, buf, len); } /* @@ -598,6 +614,13 @@ rte_pcapng_write_packets(rte_pcapng_t *self, return -1; } + /* check that this interface was added. */ + epb->interface_id = self->port_index[m->port]; + if (unlikely(epb->interface_id > RTE_MAX_ETHPORTS)) { + rte_errno = EINVAL; + return -1; + } + /* * Handle case of highly fragmented and large burst size * Note: this assumes that max segments per mbuf < IOV_MAX @@ -616,7 +639,6 @@ rte_pcapng_write_packets(rte_pcapng_t *self, * The DPDK port is recorded during pcapng_copy. * Map that to PCAPNG interface in file. */ - epb->interface_id = self->port_index[m->port]; do { iov[cnt].iov_base = rte_pktmbuf_mtod(m, void *); iov[cnt].iov_len = rte_pktmbuf_data_len(m); @@ -638,6 +660,7 @@ rte_pcapng_fdopen(int fd, const char *osname, const char *hardware, const char *appname, const char *comment) { + unsigned int i; rte_pcapng_t *self; self = malloc(sizeof(*self)); @@ -647,13 +670,13 @@ rte_pcapng_fdopen(int fd, } self->outfd = fd; + self->ports = 0; + for (i = 0; i < RTE_MAX_ETHPORTS; i++) + self->port_index[i] = UINT32_MAX; if (pcapng_section_block(self, osname, hardware, appname, comment) < 0) goto fail; - if (pcapng_interfaces(self) < 0) - goto fail; - return self; fail: free(self); diff --git a/lib/pcapng/rte_pcapng.h b/lib/pcapng/rte_pcapng.h index 7d2697c647ef..86b7996e291e 100644 --- a/lib/pcapng/rte_pcapng.h +++ b/lib/pcapng/rte_pcapng.h @@ -70,6 +70,31 @@ __rte_experimental void rte_pcapng_close(rte_pcapng_t *self); +/** + * Add interface information to the capture file + * + * @param self + * The handle to the packet capture file + * @param port + * The Ethernet port to report stats on. + * @param ifname (optional) + * Interface name to record in the file. + * If not specified, name will be constructed from port + * @param ifdescr (optional) + * Interface description to record in the file. + * @param filter + * Capture filter to record in the file. + * + * Interfaces must be added to the output file after opening + * and before any packet record. All ports used in packet capture + * must be added. + */ +__rte_experimental +int +rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, + const char *ifname, const char *ifdescr, + const char *filter); + /** * Direction flag * These should match Enhanced Packet Block flag bits diff --git a/lib/pcapng/version.map b/lib/pcapng/version.map index 05a9c86a7d91..e98e71038ee6 100644 --- a/lib/pcapng/version.map +++ b/lib/pcapng/version.map @@ -7,6 +7,7 @@ EXPERIMENTAL { rte_pcapng_mbuf_size; rte_pcapng_write_packets; rte_pcapng_write_stats; + rte_pcapng_add_interface; local: *; }; -- 2.39.0