diff options
| author | songyanchao <[email protected]> | 2023-12-01 10:01:26 +0000 |
|---|---|---|
| committer | songyanchao <[email protected]> | 2023-12-01 10:12:35 +0000 |
| commit | e0db337fa6b45820eda3401d958e4a00a116069d (patch) | |
| tree | 049578759a2e9acb602ecd80c7e50a58eafa4817 /tools/tcpdump | |
| parent | 2f838a5fa685c3a6d248729ef1289b1dd2106489 (diff) | |
✨ feat: mrpdump tool supports saving pcapng files outside the containerv4.6.62-20231201
mrpdump tool supports saving pcapng files outside the container.
Diffstat (limited to 'tools/tcpdump')
| -rw-r--r-- | tools/tcpdump/pdump.c | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/tools/tcpdump/pdump.c b/tools/tcpdump/pdump.c index 51d3633..9602d06 100644 --- a/tools/tcpdump/pdump.c +++ b/tools/tcpdump/pdump.c @@ -532,7 +532,7 @@ static unsigned int mbuf_burst_segs(struct rte_mbuf * pkts[], unsigned int n) return iovcnt; } -int mr_pcapng_set_opt(struct rte_mbuf * mbuf, uint16_t port_id, uint32_t flags) +int mr_pcapng_set_opt(struct rte_mbuf * mbuf, uint16_t port_id, enum mr_dev_driver drv_type, uint32_t rx_tx_flags) { /* pad the packet to 32 bit boundary */ uint32_t original_length = rte_pktmbuf_pkt_len(mbuf); @@ -557,7 +557,7 @@ int mr_pcapng_set_opt(struct rte_mbuf * mbuf, uint16_t port_id, uint32_t flags) struct mrb_metadata * mrb_metadata = mrbuf_cz_data(mbuf, MR_NODE_CTRLZONE_ID); int len = 0; - if (flags == RTE_PDUMP_FLAG_RX) + if (rx_tx_flags == RTE_PDUMP_FLAG_RX) len = snprintf(str_rx_tx, str_maxlen, "rx"); else len = snprintf(str_rx_tx, str_maxlen, "tx"); @@ -606,7 +606,7 @@ int mr_pcapng_set_opt(struct rte_mbuf * mbuf, uint16_t port_id, uint32_t flags) return RT_ERR; } - /* Get os time */ + /* Set epb info */ struct timespec current_time; clock_gettime(CLOCK_REALTIME, ¤t_time); uint64_t ns = (uint64_t)current_time.tv_sec * 1000000000 + current_time.tv_nsec; @@ -616,7 +616,16 @@ int mr_pcapng_set_opt(struct rte_mbuf * mbuf, uint16_t port_id, uint32_t flags) epb->timestamp_hi = ns >> 32; epb->timestamp_lo = (uint32_t)ns; epb->capture_length = original_length; - epb->original_length = mbuf->tx_offload; + + /* Set the original length based on the driver type */ + if (drv_type == MR_DEV_DRV_TYPE_SHMDEV) + { + epb->original_length = mbuf->tx_offload; + } + else + { + epb->original_length = original_length; + } /* set trailer of block length */ struct pcapng_option * opt_new = pcapng_add_option(opt, PCAPNG_OPT_COMMENT, str_rx_tx, strlen(str_rx_tx)); @@ -627,16 +636,23 @@ int mr_pcapng_set_opt(struct rte_mbuf * mbuf, uint16_t port_id, uint32_t flags) return RT_SUCCESS; } -static inline void pdump_rxtx(struct rte_mempool * mp, uint16_t port_id, struct rte_ring * ring, - struct pdump_stats * stats, uint32_t flags) +static inline void pdump_rxtx(struct pdump_tuples * pt, uint32_t rx_tx_flags) { /* Write input packets of port_id to vdev for pdump */ uint64_t * rxtx_pkts = NULL; + struct rte_ring * ring = NULL; + struct pdump_stats * stats = &pt->stats; - if (flags == RTE_PDUMP_FLAG_RX) + if (rx_tx_flags == RTE_PDUMP_FLAG_RX) + { + ring = pt->rx_ring; rxtx_pkts = &stats->rx_pkts; + } else + { + ring = pt->tx_ring; rxtx_pkts = &stats->tx_pkts; + } /* Set the expected number of dequeued packets based on dumpfile_count */ unsigned int nr_exp_pkts = BURST_SIZE; @@ -675,7 +691,7 @@ static inline void pdump_rxtx(struct rte_mempool * mp, uint16_t port_id, struct for (int i = 0; i < nb_in_deq; i++) { struct rte_mbuf * mbuf = rxtx_bufs[i]; - int ret = mr_pcapng_set_opt(mbuf, port_id, flags); + int ret = mr_pcapng_set_opt(mbuf, pt->port_id, pt->drv_type, rx_tx_flags); if (ret == RT_SUCCESS) { do @@ -693,7 +709,7 @@ static inline void pdump_rxtx(struct rte_mempool * mp, uint16_t port_id, struct if (cnt > 0) { if (writev(pcapng_fd, iov, cnt) <= 0) - perror("pcapng write error"); + perror("Pcapng packets write error."); } /* Pkt free */ @@ -1034,10 +1050,10 @@ static inline void dump_packets(void) { struct pdump_tuples * pt = &pdump_t[i]; if (pt->dir & RTE_PDUMP_FLAG_RX) - pdump_rxtx(pt->mp, pt->port_id, pt->rx_ring, &pt->stats, RTE_PDUMP_FLAG_RX); + pdump_rxtx(pt, RTE_PDUMP_FLAG_RX); if (pt->dir & RTE_PDUMP_FLAG_TX) - pdump_rxtx(pt->mp, pt->port_id, pt->tx_ring, &pt->stats, RTE_PDUMP_FLAG_TX); + pdump_rxtx(pt, RTE_PDUMP_FLAG_TX); } } } @@ -1047,30 +1063,33 @@ static inline void create_pcap_dumper() struct rte_mp_msg mp_req = {}; struct mr_pdump_request * req = (struct mr_pdump_request *)mp_req.param; - /* malloc msg dumpfile path,mrzcpd free this memory */ - char * msg_dumpfile_path = rte_zmalloc(NULL, PATH_MAX, 0); - if (msg_dumpfile_path == NULL) + /* Check dumpfile whether exist, if exist, delete it */ + if (access(dumpfile_path, F_OK) == 0) { - rte_exit(EXIT_FAILURE, "dumpfile path zmalloc err.\n"); + if (remove(dumpfile_path) != 0) + { + rte_exit(EXIT_FAILURE, "Remove %s err.\n", dumpfile_path); + } } + pcapng_fd = open(dumpfile_path, O_WRONLY | O_CREAT, 0640); + /* Fill request struct */ memset(req, 0, sizeof(*req)); - memcpy(msg_dumpfile_path, dumpfile_path, sizeof(dumpfile_path)); - req->op = CREATE_PCAPNG; - req->dumpfile_path = msg_dumpfile_path; + req->op = WRITE_PCAPNG_HEADER; /* Fill request name */ rte_strscpy(mp_req.name, MR_PDUMP_MP, RTE_MP_MAX_NAME_LEN); mp_req.len_param = sizeof(*req); - mp_req.num_fds = 0; + mp_req.num_fds = 1; + mp_req.fds[0] = pcapng_fd; /* Send request and wait reply */ struct rte_mp_reply mp_reply = {}; struct timespec ts = {.tv_sec = 20, .tv_nsec = 0}; if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) != 0) { - rte_exit(EXIT_FAILURE, "create:%s send request faill.\n", dumpfile_path); + rte_exit(EXIT_FAILURE, "Failed to send the request to write the '%s' pcapng header.\n", dumpfile_path); } /* Deal reply */ @@ -1081,12 +1100,9 @@ static inline void create_pcap_dumper() /* Dump error info */ err_value_dump(resp->err_value); free(mp_reply.msgs); - rte_exit(EXIT_FAILURE, "create:%s err.\n", dumpfile_path); + rte_exit(EXIT_FAILURE, "The '%s' pcapng header write err.\n", dumpfile_path); } - /* Save fd */ - pcapng_fd = mp_rep->fds[0]; - /* Set bpf */ if (is_str_bpf_expr_set) { |
