4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/socket.h>
38 #include <sys/types.h>
43 #include <rte_memcpy.h>
45 #include <rte_ethdev.h>
46 #include <rte_lcore.h>
48 #include <rte_errno.h>
50 #include "rte_pdump.h"
52 #define SOCKET_PATH_VAR_RUN "/var/run"
53 #define SOCKET_PATH_HOME "HOME"
54 #define DPDK_DIR "/.dpdk"
55 #define SOCKET_DIR "/pdump_sockets"
56 #define SERVER_SOCKET "%s/pdump_server_socket"
57 #define CLIENT_SOCKET "%s/pdump_client_socket_%d_%u"
58 #define DEVICE_ID_SIZE 64
59 /* Macros for printing using RTE_LOG */
60 #define RTE_LOGTYPE_PDUMP RTE_LOGTYPE_USER1
62 enum pdump_operation {
71 static pthread_t pdump_thread;
72 static int pdump_socket_fd;
73 static char server_socket_dir[PATH_MAX];
74 static char client_socket_dir[PATH_MAX];
76 struct pdump_request {
82 char device[DEVICE_ID_SIZE];
84 struct rte_ring *ring;
85 struct rte_mempool *mp;
89 char device[DEVICE_ID_SIZE];
91 struct rte_ring *ring;
92 struct rte_mempool *mp;
98 struct pdump_response {
104 static struct pdump_rxtx_cbs {
105 struct rte_ring *ring;
106 struct rte_mempool *mp;
107 struct rte_eth_rxtx_callback *cb;
109 } rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT],
110 tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
113 pdump_pktmbuf_copy_data(struct rte_mbuf *seg, const struct rte_mbuf *m)
115 if (rte_pktmbuf_tailroom(seg) < m->data_len) {
117 "User mempool: insufficient data_len of mbuf\n");
122 seg->vlan_tci = m->vlan_tci;
124 seg->tx_offload = m->tx_offload;
125 seg->ol_flags = m->ol_flags;
126 seg->packet_type = m->packet_type;
127 seg->vlan_tci_outer = m->vlan_tci_outer;
128 seg->data_len = m->data_len;
129 seg->pkt_len = seg->data_len;
130 rte_memcpy(rte_pktmbuf_mtod(seg, void *),
131 rte_pktmbuf_mtod(m, void *),
132 rte_pktmbuf_data_len(seg));
137 static inline struct rte_mbuf *
138 pdump_pktmbuf_copy(struct rte_mbuf *m, struct rte_mempool *mp)
140 struct rte_mbuf *m_dup, *seg, **prev;
144 m_dup = rte_pktmbuf_alloc(mp);
145 if (unlikely(m_dup == NULL))
155 if (pdump_pktmbuf_copy_data(seg, m) < 0) {
156 rte_pktmbuf_free(m_dup);
161 } while ((m = m->next) != NULL &&
162 (seg = rte_pktmbuf_alloc(mp)) != NULL);
165 m_dup->nb_segs = nseg;
166 m_dup->pkt_len = pktlen;
168 /* Allocation of new indirect segment failed */
169 if (unlikely(seg == NULL)) {
170 rte_pktmbuf_free(m_dup);
174 __rte_mbuf_sanity_check(m_dup, 1);
179 pdump_copy(struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
184 struct rte_mbuf *dup_bufs[nb_pkts];
185 struct pdump_rxtx_cbs *cbs;
186 struct rte_ring *ring;
187 struct rte_mempool *mp;
193 for (i = 0; i < nb_pkts; i++) {
194 p = pdump_pktmbuf_copy(pkts[i], mp);
196 dup_bufs[d_pkts++] = p;
199 ring_enq = rte_ring_enqueue_burst(ring, (void *)dup_bufs, d_pkts, NULL);
200 if (unlikely(ring_enq < d_pkts)) {
201 RTE_LOG(DEBUG, PDUMP,
202 "only %d of packets enqueued to ring\n", ring_enq);
204 rte_pktmbuf_free(dup_bufs[ring_enq]);
205 } while (++ring_enq < d_pkts);
210 pdump_rx(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
211 struct rte_mbuf **pkts, uint16_t nb_pkts,
212 uint16_t max_pkts __rte_unused,
215 pdump_copy(pkts, nb_pkts, user_params);
220 pdump_tx(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
221 struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
223 pdump_copy(pkts, nb_pkts, user_params);
228 pdump_regitser_rx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
229 struct rte_ring *ring, struct rte_mempool *mp,
233 struct pdump_rxtx_cbs *cbs = NULL;
235 qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
236 for (; qid < end_q; qid++) {
237 cbs = &rx_cbs[port][qid];
238 if (cbs && operation == ENABLE) {
241 "failed to add rx callback for port=%d "
242 "and queue=%d, callback already exists\n",
248 cbs->cb = rte_eth_add_first_rx_callback(port, qid,
250 if (cbs->cb == NULL) {
252 "failed to add rx callback, errno=%d\n",
257 if (cbs && operation == DISABLE) {
260 if (cbs->cb == NULL) {
262 "failed to delete non existing rx "
263 "callback for port=%d and queue=%d\n",
267 ret = rte_eth_remove_rx_callback(port, qid, cbs->cb);
270 "failed to remove rx callback, errno=%d\n",
282 pdump_regitser_tx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
283 struct rte_ring *ring, struct rte_mempool *mp,
288 struct pdump_rxtx_cbs *cbs = NULL;
290 qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
291 for (; qid < end_q; qid++) {
292 cbs = &tx_cbs[port][qid];
293 if (cbs && operation == ENABLE) {
296 "failed to add tx callback for port=%d "
297 "and queue=%d, callback already exists\n",
303 cbs->cb = rte_eth_add_tx_callback(port, qid, pdump_tx,
305 if (cbs->cb == NULL) {
307 "failed to add tx callback, errno=%d\n",
312 if (cbs && operation == DISABLE) {
315 if (cbs->cb == NULL) {
317 "failed to delete non existing tx "
318 "callback for port=%d and queue=%d\n",
322 ret = rte_eth_remove_tx_callback(port, qid, cbs->cb);
325 "failed to remove tx callback, errno=%d\n",
337 set_pdump_rxtx_cbs(struct pdump_request *p)
339 uint16_t nb_rx_q = 0, nb_tx_q = 0, end_q, queue;
344 struct rte_ring *ring;
345 struct rte_mempool *mp;
349 if (operation == ENABLE) {
350 ret = rte_eth_dev_get_port_by_name(p->data.en_v1.device,
354 "failed to get potid for device id=%s\n",
355 p->data.en_v1.device);
358 queue = p->data.en_v1.queue;
359 ring = p->data.en_v1.ring;
360 mp = p->data.en_v1.mp;
362 ret = rte_eth_dev_get_port_by_name(p->data.dis_v1.device,
366 "failed to get potid for device id=%s\n",
367 p->data.dis_v1.device);
370 queue = p->data.dis_v1.queue;
371 ring = p->data.dis_v1.ring;
372 mp = p->data.dis_v1.mp;
375 /* validation if packet capture is for all queues */
376 if (queue == RTE_PDUMP_ALL_QUEUES) {
377 struct rte_eth_dev_info dev_info;
379 rte_eth_dev_info_get(port, &dev_info);
380 nb_rx_q = dev_info.nb_rx_queues;
381 nb_tx_q = dev_info.nb_tx_queues;
382 if (nb_rx_q == 0 && flags & RTE_PDUMP_FLAG_RX) {
384 "number of rx queues cannot be 0\n");
387 if (nb_tx_q == 0 && flags & RTE_PDUMP_FLAG_TX) {
389 "number of tx queues cannot be 0\n");
392 if ((nb_tx_q == 0 || nb_rx_q == 0) &&
393 flags == RTE_PDUMP_FLAG_RXTX) {
395 "both tx&rx queues must be non zero\n");
400 /* register RX callback */
401 if (flags & RTE_PDUMP_FLAG_RX) {
402 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1;
403 ret = pdump_regitser_rx_callbacks(end_q, port, queue, ring, mp,
409 /* register TX callback */
410 if (flags & RTE_PDUMP_FLAG_TX) {
411 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1;
412 ret = pdump_regitser_tx_callbacks(end_q, port, queue, ring, mp,
421 /* get socket path (/var/run if root, $HOME otherwise) */
423 pdump_get_socket_path(char *buffer, int bufsz, enum rte_pdump_socktype type)
425 char dpdk_dir[PATH_MAX] = {0};
426 char dir[PATH_MAX] = {0};
427 char *dir_home = NULL;
430 if (type == RTE_PDUMP_SOCKET_SERVER && server_socket_dir[0] != 0)
431 snprintf(dir, sizeof(dir), "%s", server_socket_dir);
432 else if (type == RTE_PDUMP_SOCKET_CLIENT && client_socket_dir[0] != 0)
433 snprintf(dir, sizeof(dir), "%s", client_socket_dir);
436 dir_home = getenv(SOCKET_PATH_HOME);
439 "Failed to get environment variable"
440 " value for %s, %s:%d\n",
441 SOCKET_PATH_HOME, __func__, __LINE__);
444 snprintf(dpdk_dir, sizeof(dpdk_dir), "%s%s",
447 snprintf(dpdk_dir, sizeof(dpdk_dir), "%s%s",
448 SOCKET_PATH_VAR_RUN, DPDK_DIR);
450 mkdir(dpdk_dir, 0700);
451 snprintf(dir, sizeof(dir), "%s%s",
452 dpdk_dir, SOCKET_DIR);
455 ret = mkdir(dir, 0700);
456 /* if user passed socket path is invalid, return immediately */
457 if (ret < 0 && errno != EEXIST) {
459 "Failed to create dir:%s:%s\n", dir,
465 if (type == RTE_PDUMP_SOCKET_SERVER)
466 snprintf(buffer, bufsz, SERVER_SOCKET, dir);
468 snprintf(buffer, bufsz, CLIENT_SOCKET, dir, getpid(),
475 pdump_create_server_socket(void)
478 struct sockaddr_un addr;
481 ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
482 RTE_PDUMP_SOCKET_SERVER);
485 "Failed to get server socket path: %s:%d\n",
489 addr.sun_family = AF_UNIX;
491 /* remove if file already exists */
492 unlink(addr.sun_path);
494 /* set up a server socket */
495 socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
498 "Failed to create server socket: %s, %s:%d\n",
499 strerror(errno), __func__, __LINE__);
503 addr_len = sizeof(struct sockaddr_un);
504 ret = bind(socket_fd, (struct sockaddr *) &addr, addr_len);
507 "Failed to bind to server socket: %s, %s:%d\n",
508 strerror(errno), __func__, __LINE__);
513 /* save the socket in local configuration */
514 pdump_socket_fd = socket_fd;
519 static __attribute__((noreturn)) void *
520 pdump_thread_main(__rte_unused void *arg)
522 struct sockaddr_un cli_addr;
524 struct pdump_request cli_req;
525 struct pdump_response resp;
529 /* host thread, never break out */
531 /* recv client requests */
532 cli_len = sizeof(cli_addr);
533 n = recvfrom(pdump_socket_fd, &cli_req,
534 sizeof(struct pdump_request), 0,
535 (struct sockaddr *)&cli_addr, &cli_len);
538 "failed to recv from client:%s, %s:%d\n",
539 strerror(errno), __func__, __LINE__);
543 ret = set_pdump_rxtx_cbs(&cli_req);
545 resp.ver = cli_req.ver;
546 resp.res_op = cli_req.op;
547 resp.err_value = ret;
548 n = sendto(pdump_socket_fd, &resp,
549 sizeof(struct pdump_response),
550 0, (struct sockaddr *)&cli_addr, cli_len);
553 "failed to send to client:%s, %s:%d\n",
554 strerror(errno), __func__, __LINE__);
560 rte_pdump_init(const char *path)
563 char thread_name[RTE_MAX_THREAD_NAME_LEN];
565 ret = rte_pdump_set_socket_dir(path, RTE_PDUMP_SOCKET_SERVER);
569 ret = pdump_create_server_socket();
572 "Failed to create server socket:%s:%d\n",
577 /* create the host thread to wait/handle pdump requests */
578 ret = pthread_create(&pdump_thread, NULL, pdump_thread_main, NULL);
581 "Failed to create the pdump thread:%s, %s:%d\n",
582 strerror(errno), __func__, __LINE__);
585 /* Set thread_name for aid in debugging. */
586 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "pdump-thread");
587 ret = rte_thread_setname(pdump_thread, thread_name);
589 RTE_LOG(DEBUG, PDUMP,
590 "Failed to set thread name for pdump handling\n");
597 rte_pdump_uninit(void)
601 ret = pthread_cancel(pdump_thread);
604 "Failed to cancel the pdump thread:%s, %s:%d\n",
605 strerror(errno), __func__, __LINE__);
609 ret = close(pdump_socket_fd);
612 "Failed to close server socket: %s, %s:%d\n",
613 strerror(errno), __func__, __LINE__);
617 struct sockaddr_un addr;
619 ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
620 RTE_PDUMP_SOCKET_SERVER);
623 "Failed to get server socket path: %s:%d\n",
627 ret = unlink(addr.sun_path);
630 "Failed to remove server socket addr: %s, %s:%d\n",
631 strerror(errno), __func__, __LINE__);
639 pdump_create_client_socket(struct pdump_request *p)
644 struct pdump_response server_resp;
645 struct sockaddr_un addr, serv_addr, from;
646 socklen_t addr_len, serv_len;
650 socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
653 "client socket(): %s:pid(%d):tid(%u), %s:%d\n",
654 strerror(errno), pid, rte_sys_gettid(),
660 ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
661 RTE_PDUMP_SOCKET_CLIENT);
664 "Failed to get client socket path: %s:%d\n",
669 addr.sun_family = AF_UNIX;
670 addr_len = sizeof(struct sockaddr_un);
673 ret = bind(socket_fd, (struct sockaddr *) &addr, addr_len);
676 "client bind(): %s, %s:%d\n",
677 strerror(errno), __func__, __LINE__);
682 serv_len = sizeof(struct sockaddr_un);
683 memset(&serv_addr, 0, sizeof(serv_addr));
684 ret = pdump_get_socket_path(serv_addr.sun_path,
685 sizeof(serv_addr.sun_path),
686 RTE_PDUMP_SOCKET_SERVER);
689 "Failed to get server socket path: %s:%d\n",
694 serv_addr.sun_family = AF_UNIX;
696 n = sendto(socket_fd, p, sizeof(struct pdump_request), 0,
697 (struct sockaddr *)&serv_addr, serv_len);
700 "failed to send to server:%s, %s:%d\n",
701 strerror(errno), __func__, __LINE__);
707 n = recvfrom(socket_fd, &server_resp,
708 sizeof(struct pdump_response), 0,
709 (struct sockaddr *)&from, &serv_len);
712 "failed to recv from server:%s, %s:%d\n",
713 strerror(errno), __func__, __LINE__);
718 ret = server_resp.err_value;
723 unlink(addr.sun_path);
728 pdump_validate_ring_mp(struct rte_ring *ring, struct rte_mempool *mp)
730 if (ring == NULL || mp == NULL) {
731 RTE_LOG(ERR, PDUMP, "NULL ring or mempool are passed %s:%d\n",
736 if (mp->flags & MEMPOOL_F_SP_PUT || mp->flags & MEMPOOL_F_SC_GET) {
737 RTE_LOG(ERR, PDUMP, "mempool with either SP or SC settings"
738 " is not valid for pdump, should have MP and MC settings\n");
742 if (ring->prod.single || ring->cons.single) {
743 RTE_LOG(ERR, PDUMP, "ring with either SP or SC settings"
744 " is not valid for pdump, should have MP and MC settings\n");
753 pdump_validate_flags(uint32_t flags)
755 if (flags != RTE_PDUMP_FLAG_RX && flags != RTE_PDUMP_FLAG_TX &&
756 flags != RTE_PDUMP_FLAG_RXTX) {
758 "invalid flags, should be either rx/tx/rxtx\n");
767 pdump_validate_port(uint16_t port, char *name)
771 if (port >= RTE_MAX_ETHPORTS) {
772 RTE_LOG(ERR, PDUMP, "Invalid port id %u, %s:%d\n", port,
778 ret = rte_eth_dev_get_name_by_port(port, name);
781 "port id to name mapping failed for port id=%u, %s:%d\n",
782 port, __func__, __LINE__);
791 pdump_prepare_client_request(char *device, uint16_t queue,
794 struct rte_ring *ring,
795 struct rte_mempool *mp,
799 struct pdump_request req = {.ver = 1,};
803 if ((operation & ENABLE) != 0) {
804 snprintf(req.data.en_v1.device, sizeof(req.data.en_v1.device),
806 req.data.en_v1.queue = queue;
807 req.data.en_v1.ring = ring;
808 req.data.en_v1.mp = mp;
809 req.data.en_v1.filter = filter;
811 snprintf(req.data.dis_v1.device, sizeof(req.data.dis_v1.device),
813 req.data.dis_v1.queue = queue;
814 req.data.dis_v1.ring = NULL;
815 req.data.dis_v1.mp = NULL;
816 req.data.dis_v1.filter = NULL;
819 ret = pdump_create_client_socket(&req);
822 "client request for pdump enable/disable failed\n");
831 rte_pdump_enable(uint16_t port, uint16_t queue, uint32_t flags,
832 struct rte_ring *ring,
833 struct rte_mempool *mp,
838 char name[DEVICE_ID_SIZE];
840 ret = pdump_validate_port(port, name);
843 ret = pdump_validate_ring_mp(ring, mp);
846 ret = pdump_validate_flags(flags);
850 ret = pdump_prepare_client_request(name, queue, flags,
851 ENABLE, ring, mp, filter);
857 rte_pdump_enable_by_deviceid(char *device_id, uint16_t queue,
859 struct rte_ring *ring,
860 struct rte_mempool *mp,
865 ret = pdump_validate_ring_mp(ring, mp);
868 ret = pdump_validate_flags(flags);
872 ret = pdump_prepare_client_request(device_id, queue, flags,
873 ENABLE, ring, mp, filter);
879 rte_pdump_disable(uint16_t port, uint16_t queue, uint32_t flags)
882 char name[DEVICE_ID_SIZE];
884 ret = pdump_validate_port(port, name);
887 ret = pdump_validate_flags(flags);
891 ret = pdump_prepare_client_request(name, queue, flags,
892 DISABLE, NULL, NULL, NULL);
898 rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue,
903 ret = pdump_validate_flags(flags);
907 ret = pdump_prepare_client_request(device_id, queue, flags,
908 DISABLE, NULL, NULL, NULL);
914 rte_pdump_set_socket_dir(const char *path, enum rte_pdump_socktype type)
919 if (type == RTE_PDUMP_SOCKET_SERVER) {
920 count = sizeof(server_socket_dir);
921 ret = snprintf(server_socket_dir, count, "%s", path);
923 count = sizeof(client_socket_dir);
924 ret = snprintf(client_socket_dir, count, "%s", path);
927 if (ret < 0 || ret >= count) {
929 "Invalid socket path:%s:%d\n",
931 if (type == RTE_PDUMP_SOCKET_SERVER)
932 server_socket_dir[0] = 0;
934 client_socket_dir[0] = 0;