summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsongyanchao <[email protected]>2023-12-01 10:01:26 +0000
committersongyanchao <[email protected]>2023-12-01 10:12:35 +0000
commite0db337fa6b45820eda3401d958e4a00a116069d (patch)
tree049578759a2e9acb602ecd80c7e50a58eafa4817
parent2f838a5fa685c3a6d248729ef1289b1dd2106489 (diff)
✨ feat: mrpdump tool supports saving pcapng files outside the containerv4.6.62-20231201
mrpdump tool supports saving pcapng files outside the container.
-rw-r--r--service/include/sc_pdump.h3
-rw-r--r--service/src/pdump.c57
-rw-r--r--tools/tcpdump/pdump.c64
3 files changed, 55 insertions, 69 deletions
diff --git a/service/include/sc_pdump.h b/service/include/sc_pdump.h
index 4c3d826..61b02f8 100644
--- a/service/include/sc_pdump.h
+++ b/service/include/sc_pdump.h
@@ -43,7 +43,7 @@ enum mr_pdump_operation
{
DISABLE = 1,
ENABLE = 2,
- CREATE_PCAPNG = 3,
+ WRITE_PCAPNG_HEADER = 3,
};
/* Request struct */
@@ -57,7 +57,6 @@ struct mr_pdump_request
uint32_t ring_size;
uint32_t snaplen;
char dev_symbol[MR_SYMBOL_MAX];
- char * dumpfile_path;
struct rte_bpf_prm * prm;
};
diff --git a/service/src/pdump.c b/service/src/pdump.c
index 8f21eff..eea109d 100644
--- a/service/src/pdump.c
+++ b/service/src/pdump.c
@@ -327,46 +327,22 @@ static char * get_os_info(void)
return os_name;
}
-/* Mrpdump create */
-int mr_pdump_pcapng_create(const struct mr_pdump_request * _cli_req, struct mr_pdump_response * _out_response,
- struct rte_mp_msg * _out_mp_resp, int * _out_pcapng_fd)
+/* Pcapng header write */
+void mr_pdump_pcapng_header_write(int pcapng_fd, struct mr_pdump_response * _out_response)
{
- int32_t err_value = MR_PDUMP_SUCCESS;
-
- /* Check dumpfile whether exist, if exist, delete it */
- if (access(_cli_req->dumpfile_path, F_OK) == 0)
+ /* Write the pcapng header for the pcapng fd */
+ _out_response->res_op = WRITE_PCAPNG_HEADER;
+ rte_pcapng_t * _pcapng_item = rte_pcapng_fdopen(pcapng_fd, get_os_info(), NULL, "mrpdump v1.0", NULL);
+ if (_pcapng_item == NULL)
{
- if (remove(_cli_req->dumpfile_path) != 0)
- {
- MR_ERROR("mrpdump remove %s err.", _cli_req->dumpfile_path);
- err_value = MR_PDUMP_ERR_FOR_PCAPNG_CREATE;
- rte_free(_cli_req->dumpfile_path);
- return RT_ERR;
- }
+ _out_response->err_value = MR_PDUMP_ERR_FOR_PCAPNG_CREATE;
+ MR_ERROR("mrpdump write the pcapng header err.");
}
-
- /* Create pcapng file */
- char * os = get_os_info();
- int pcapng_fd = open(_cli_req->dumpfile_path, O_WRONLY | O_CREAT, 0640);
- rte_pcapng_t * _pcapng_item = rte_pcapng_fdopen(pcapng_fd, os, NULL, "mrpdump v1.0", NULL);
- if (_pcapng_item == NULL)
+ else
{
- MR_ERROR("mrpdump create %s err.", _cli_req->dumpfile_path);
- err_value = MR_PDUMP_ERR_FOR_PCAPNG_CREATE;
- rte_free(_cli_req->dumpfile_path);
- return RT_ERR;
+ _out_response->err_value = MR_PDUMP_SUCCESS;
+ MR_INFO("mrpdump write the pcapng header success.");
}
-
- /* Fill the response for public */
- _out_response->res_op = _cli_req->op;
- _out_response->err_value = err_value;
- _out_mp_resp->num_fds = 1;
- _out_mp_resp->fds[0] = pcapng_fd;
-
- MR_INFO("mrpdump create '%s' .", _cli_req->dumpfile_path);
- rte_free(_cli_req->dumpfile_path);
- *_out_pcapng_fd = pcapng_fd;
- return RT_SUCCESS;
}
/* Pcapng server */
@@ -389,7 +365,7 @@ static int mr_pdump_server(const struct rte_mp_msg * mp_msg, const void * peer)
goto done;
}
- if (_cli_req->op != CREATE_PCAPNG)
+ if (_cli_req->op != WRITE_PCAPNG_HEADER)
{
/* Check devices name */
dev_desc = mr_dev_desc_lookup(sc->devmgr_main, _cli_req->dev_symbol);
@@ -402,7 +378,6 @@ static int mr_pdump_server(const struct rte_mp_msg * mp_msg, const void * peer)
}
/* Check res op */
- int pcapng_fd = -1;
if (_cli_req->op == ENABLE)
{
mr_pdump_enable(_cli_req, &response, dev_desc);
@@ -411,9 +386,9 @@ static int mr_pdump_server(const struct rte_mp_msg * mp_msg, const void * peer)
{
mr_pdump_disable(_cli_req, &response, dev_desc);
}
- else if (_cli_req->op == CREATE_PCAPNG)
+ else if (_cli_req->op == WRITE_PCAPNG_HEADER)
{
- mr_pdump_pcapng_create(_cli_req, &response, &mp_resp, &pcapng_fd);
+ mr_pdump_pcapng_header_write(mp_msg->fds[0], &response);
}
done:
@@ -421,10 +396,6 @@ done:
memcpy(_resp, &response, sizeof(struct mr_pdump_response));
int ret = rte_mp_reply(&mp_resp, peer);
- /* close pcapng fd */
- if (_cli_req->op == CREATE_PCAPNG)
- close(pcapng_fd);
-
/* Check response */
if (ret < 0)
{
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, &current_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)
{