1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Intel Corporation
5 #include <rte_memcpy.h>
7 #include <rte_ethdev.h>
10 #include <rte_errno.h>
11 #include <rte_string_fns.h>
13 #include "rte_pdump.h"
15 #define DEVICE_ID_SIZE 64
17 /* Macro for printing using RTE_LOG */
18 static int pdump_logtype;
19 #define PDUMP_LOG(level, fmt, args...) \
20 rte_log(RTE_LOG_ ## level, pdump_logtype, "%s(): " fmt, \
23 /* Used for the multi-process communication */
24 #define PDUMP_MP "mp_pdump"
26 enum pdump_operation {
35 struct pdump_request {
41 char device[DEVICE_ID_SIZE];
43 struct rte_ring *ring;
44 struct rte_mempool *mp;
48 char device[DEVICE_ID_SIZE];
50 struct rte_ring *ring;
51 struct rte_mempool *mp;
57 struct pdump_response {
63 static struct pdump_rxtx_cbs {
64 struct rte_ring *ring;
65 struct rte_mempool *mp;
66 const struct rte_eth_rxtx_callback *cb;
68 } rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT],
69 tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
73 pdump_copy(struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
78 struct rte_mbuf *dup_bufs[nb_pkts];
79 struct pdump_rxtx_cbs *cbs;
80 struct rte_ring *ring;
81 struct rte_mempool *mp;
87 for (i = 0; i < nb_pkts; i++) {
88 p = rte_pktmbuf_copy(pkts[i], mp, 0, UINT32_MAX);
90 dup_bufs[d_pkts++] = p;
93 ring_enq = rte_ring_enqueue_burst(ring, (void *)dup_bufs, d_pkts, NULL);
94 if (unlikely(ring_enq < d_pkts)) {
96 "only %d of packets enqueued to ring\n", ring_enq);
98 rte_pktmbuf_free(dup_bufs[ring_enq]);
99 } while (++ring_enq < d_pkts);
104 pdump_rx(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
105 struct rte_mbuf **pkts, uint16_t nb_pkts,
106 uint16_t max_pkts __rte_unused,
109 pdump_copy(pkts, nb_pkts, user_params);
114 pdump_tx(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
115 struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
117 pdump_copy(pkts, nb_pkts, user_params);
122 pdump_register_rx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
123 struct rte_ring *ring, struct rte_mempool *mp,
127 struct pdump_rxtx_cbs *cbs = NULL;
129 qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
130 for (; qid < end_q; qid++) {
131 cbs = &rx_cbs[port][qid];
132 if (cbs && operation == ENABLE) {
135 "failed to add rx callback for port=%d "
136 "and queue=%d, callback already exists\n",
142 cbs->cb = rte_eth_add_first_rx_callback(port, qid,
144 if (cbs->cb == NULL) {
146 "failed to add rx callback, errno=%d\n",
151 if (cbs && operation == DISABLE) {
154 if (cbs->cb == NULL) {
156 "failed to delete non existing rx "
157 "callback for port=%d and queue=%d\n",
161 ret = rte_eth_remove_rx_callback(port, qid, cbs->cb);
164 "failed to remove rx callback, errno=%d\n",
176 pdump_register_tx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
177 struct rte_ring *ring, struct rte_mempool *mp,
182 struct pdump_rxtx_cbs *cbs = NULL;
184 qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
185 for (; qid < end_q; qid++) {
186 cbs = &tx_cbs[port][qid];
187 if (cbs && operation == ENABLE) {
190 "failed to add tx callback for port=%d "
191 "and queue=%d, callback already exists\n",
197 cbs->cb = rte_eth_add_tx_callback(port, qid, pdump_tx,
199 if (cbs->cb == NULL) {
201 "failed to add tx callback, errno=%d\n",
206 if (cbs && operation == DISABLE) {
209 if (cbs->cb == NULL) {
211 "failed to delete non existing tx "
212 "callback for port=%d and queue=%d\n",
216 ret = rte_eth_remove_tx_callback(port, qid, cbs->cb);
219 "failed to remove tx callback, errno=%d\n",
231 set_pdump_rxtx_cbs(const struct pdump_request *p)
233 uint16_t nb_rx_q = 0, nb_tx_q = 0, end_q, queue;
238 struct rte_ring *ring;
239 struct rte_mempool *mp;
243 if (operation == ENABLE) {
244 ret = rte_eth_dev_get_port_by_name(p->data.en_v1.device,
248 "failed to get port id for device id=%s\n",
249 p->data.en_v1.device);
252 queue = p->data.en_v1.queue;
253 ring = p->data.en_v1.ring;
254 mp = p->data.en_v1.mp;
256 ret = rte_eth_dev_get_port_by_name(p->data.dis_v1.device,
260 "failed to get port id for device id=%s\n",
261 p->data.dis_v1.device);
264 queue = p->data.dis_v1.queue;
265 ring = p->data.dis_v1.ring;
266 mp = p->data.dis_v1.mp;
269 /* validation if packet capture is for all queues */
270 if (queue == RTE_PDUMP_ALL_QUEUES) {
271 struct rte_eth_dev_info dev_info;
273 ret = rte_eth_dev_info_get(port, &dev_info);
276 "Error during getting device (port %u) info: %s\n",
277 port, strerror(-ret));
281 nb_rx_q = dev_info.nb_rx_queues;
282 nb_tx_q = dev_info.nb_tx_queues;
283 if (nb_rx_q == 0 && flags & RTE_PDUMP_FLAG_RX) {
285 "number of rx queues cannot be 0\n");
288 if (nb_tx_q == 0 && flags & RTE_PDUMP_FLAG_TX) {
290 "number of tx queues cannot be 0\n");
293 if ((nb_tx_q == 0 || nb_rx_q == 0) &&
294 flags == RTE_PDUMP_FLAG_RXTX) {
296 "both tx&rx queues must be non zero\n");
301 /* register RX callback */
302 if (flags & RTE_PDUMP_FLAG_RX) {
303 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1;
304 ret = pdump_register_rx_callbacks(end_q, port, queue, ring, mp,
310 /* register TX callback */
311 if (flags & RTE_PDUMP_FLAG_TX) {
312 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1;
313 ret = pdump_register_tx_callbacks(end_q, port, queue, ring, mp,
323 pdump_server(const struct rte_mp_msg *mp_msg, const void *peer)
325 struct rte_mp_msg mp_resp;
326 const struct pdump_request *cli_req;
327 struct pdump_response *resp = (struct pdump_response *)&mp_resp.param;
329 /* recv client requests */
330 if (mp_msg->len_param != sizeof(*cli_req)) {
331 PDUMP_LOG(ERR, "failed to recv from client\n");
332 resp->err_value = -EINVAL;
334 cli_req = (const struct pdump_request *)mp_msg->param;
335 resp->ver = cli_req->ver;
336 resp->res_op = cli_req->op;
337 resp->err_value = set_pdump_rxtx_cbs(cli_req);
340 strlcpy(mp_resp.name, PDUMP_MP, RTE_MP_MAX_NAME_LEN);
341 mp_resp.len_param = sizeof(*resp);
343 if (rte_mp_reply(&mp_resp, peer) < 0) {
344 PDUMP_LOG(ERR, "failed to send to client:%s\n",
345 strerror(rte_errno));
355 int ret = rte_mp_action_register(PDUMP_MP, pdump_server);
356 if (ret && rte_errno != ENOTSUP)
362 rte_pdump_uninit(void)
364 rte_mp_action_unregister(PDUMP_MP);
370 pdump_validate_ring_mp(struct rte_ring *ring, struct rte_mempool *mp)
372 if (ring == NULL || mp == NULL) {
373 PDUMP_LOG(ERR, "NULL ring or mempool\n");
377 if (mp->flags & MEMPOOL_F_SP_PUT || mp->flags & MEMPOOL_F_SC_GET) {
378 PDUMP_LOG(ERR, "mempool with either SP or SC settings"
379 " is not valid for pdump, should have MP and MC settings\n");
383 if (ring->prod.single || ring->cons.single) {
384 PDUMP_LOG(ERR, "ring with either SP or SC settings"
385 " is not valid for pdump, should have MP and MC settings\n");
394 pdump_validate_flags(uint32_t flags)
396 if (flags != RTE_PDUMP_FLAG_RX && flags != RTE_PDUMP_FLAG_TX &&
397 flags != RTE_PDUMP_FLAG_RXTX) {
399 "invalid flags, should be either rx/tx/rxtx\n");
408 pdump_validate_port(uint16_t port, char *name)
412 if (port >= RTE_MAX_ETHPORTS) {
413 PDUMP_LOG(ERR, "Invalid port id %u\n", port);
418 ret = rte_eth_dev_get_name_by_port(port, name);
420 PDUMP_LOG(ERR, "port %u to name mapping failed\n",
430 pdump_prepare_client_request(char *device, uint16_t queue,
433 struct rte_ring *ring,
434 struct rte_mempool *mp,
438 struct rte_mp_msg mp_req, *mp_rep;
439 struct rte_mp_reply mp_reply;
440 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
441 struct pdump_request *req = (struct pdump_request *)mp_req.param;
442 struct pdump_response *resp;
447 if ((operation & ENABLE) != 0) {
448 strlcpy(req->data.en_v1.device, device,
449 sizeof(req->data.en_v1.device));
450 req->data.en_v1.queue = queue;
451 req->data.en_v1.ring = ring;
452 req->data.en_v1.mp = mp;
453 req->data.en_v1.filter = filter;
455 strlcpy(req->data.dis_v1.device, device,
456 sizeof(req->data.dis_v1.device));
457 req->data.dis_v1.queue = queue;
458 req->data.dis_v1.ring = NULL;
459 req->data.dis_v1.mp = NULL;
460 req->data.dis_v1.filter = NULL;
463 strlcpy(mp_req.name, PDUMP_MP, RTE_MP_MAX_NAME_LEN);
464 mp_req.len_param = sizeof(*req);
466 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0) {
467 mp_rep = &mp_reply.msgs[0];
468 resp = (struct pdump_response *)mp_rep->param;
469 rte_errno = resp->err_value;
470 if (!resp->err_value)
477 "client request for pdump enable/disable failed\n");
482 rte_pdump_enable(uint16_t port, uint16_t queue, uint32_t flags,
483 struct rte_ring *ring,
484 struct rte_mempool *mp,
489 char name[DEVICE_ID_SIZE];
491 ret = pdump_validate_port(port, name);
494 ret = pdump_validate_ring_mp(ring, mp);
497 ret = pdump_validate_flags(flags);
501 ret = pdump_prepare_client_request(name, queue, flags,
502 ENABLE, ring, mp, filter);
508 rte_pdump_enable_by_deviceid(char *device_id, uint16_t queue,
510 struct rte_ring *ring,
511 struct rte_mempool *mp,
516 ret = pdump_validate_ring_mp(ring, mp);
519 ret = pdump_validate_flags(flags);
523 ret = pdump_prepare_client_request(device_id, queue, flags,
524 ENABLE, ring, mp, filter);
530 rte_pdump_disable(uint16_t port, uint16_t queue, uint32_t flags)
533 char name[DEVICE_ID_SIZE];
535 ret = pdump_validate_port(port, name);
538 ret = pdump_validate_flags(flags);
542 ret = pdump_prepare_client_request(name, queue, flags,
543 DISABLE, NULL, NULL, NULL);
549 rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue,
554 ret = pdump_validate_flags(flags);
558 ret = pdump_prepare_client_request(device_id, queue, flags,
559 DISABLE, NULL, NULL, NULL);
566 pdump_logtype = rte_log_register("lib.pdump");
567 if (pdump_logtype >= 0)
568 rte_log_set_level(pdump_logtype, RTE_LOG_NOTICE);