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>
51 #include "rte_pdump.h"
53 #define SOCKET_PATH_VAR_RUN "/var/run"
54 #define SOCKET_PATH_HOME "HOME"
55 #define DPDK_DIR "/.dpdk"
56 #define SOCKET_DIR "/pdump_sockets"
57 #define SERVER_SOCKET "%s/pdump_server_socket"
58 #define CLIENT_SOCKET "%s/pdump_client_socket_%d_%u"
59 #define DEVICE_ID_SIZE 64
60 /* Macros for printing using RTE_LOG */
61 #define RTE_LOGTYPE_PDUMP RTE_LOGTYPE_USER1
63 enum pdump_operation {
72 static pthread_t pdump_thread;
73 static int pdump_socket_fd;
74 static char server_socket_dir[PATH_MAX];
75 static char client_socket_dir[PATH_MAX];
77 struct pdump_request {
83 char device[DEVICE_ID_SIZE];
85 struct rte_ring *ring;
86 struct rte_mempool *mp;
90 char device[DEVICE_ID_SIZE];
92 struct rte_ring *ring;
93 struct rte_mempool *mp;
99 struct pdump_response {
105 static struct pdump_rxtx_cbs {
106 struct rte_ring *ring;
107 struct rte_mempool *mp;
108 struct rte_eth_rxtx_callback *cb;
110 } rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT],
111 tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
114 pdump_pktmbuf_copy_data(struct rte_mbuf *seg, const struct rte_mbuf *m)
116 if (rte_pktmbuf_tailroom(seg) < m->data_len) {
118 "User mempool: insufficient data_len of mbuf\n");
123 seg->vlan_tci = m->vlan_tci;
125 seg->tx_offload = m->tx_offload;
126 seg->ol_flags = m->ol_flags;
127 seg->packet_type = m->packet_type;
128 seg->vlan_tci_outer = m->vlan_tci_outer;
129 seg->data_len = m->data_len;
130 seg->pkt_len = seg->data_len;
131 rte_memcpy(rte_pktmbuf_mtod(seg, void *),
132 rte_pktmbuf_mtod(m, void *),
133 rte_pktmbuf_data_len(seg));
138 static inline struct rte_mbuf *
139 pdump_pktmbuf_copy(struct rte_mbuf *m, struct rte_mempool *mp)
141 struct rte_mbuf *m_dup, *seg, **prev;
145 m_dup = rte_pktmbuf_alloc(mp);
146 if (unlikely(m_dup == NULL))
156 if (pdump_pktmbuf_copy_data(seg, m) < 0) {
157 rte_pktmbuf_free(m_dup);
162 } while ((m = m->next) != NULL &&
163 (seg = rte_pktmbuf_alloc(mp)) != NULL);
166 m_dup->nb_segs = nseg;
167 m_dup->pkt_len = pktlen;
169 /* Allocation of new indirect segment failed */
170 if (unlikely(seg == NULL)) {
171 rte_pktmbuf_free(m_dup);
175 __rte_mbuf_sanity_check(m_dup, 1);
180 pdump_copy(struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
185 struct rte_mbuf *dup_bufs[nb_pkts];
186 struct pdump_rxtx_cbs *cbs;
187 struct rte_ring *ring;
188 struct rte_mempool *mp;
194 for (i = 0; i < nb_pkts; i++) {
195 p = pdump_pktmbuf_copy(pkts[i], mp);
197 dup_bufs[d_pkts++] = p;
200 ring_enq = rte_ring_enqueue_burst(ring, (void *)dup_bufs, d_pkts);
201 if (unlikely(ring_enq < d_pkts)) {
202 RTE_LOG(DEBUG, PDUMP,
203 "only %d of packets enqueued to ring\n", ring_enq);
205 rte_pktmbuf_free(dup_bufs[ring_enq]);
206 } while (++ring_enq < d_pkts);
211 pdump_rx(uint8_t port __rte_unused, uint16_t qidx __rte_unused,
212 struct rte_mbuf **pkts, uint16_t nb_pkts,
213 uint16_t max_pkts __rte_unused,
216 pdump_copy(pkts, nb_pkts, user_params);
221 pdump_tx(uint8_t port __rte_unused, uint16_t qidx __rte_unused,
222 struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
224 pdump_copy(pkts, nb_pkts, user_params);
229 pdump_get_dombdf(char *device_id, char *domBDF, size_t len)
232 struct rte_pci_addr dev_addr = {0};
234 /* identify if device_id is pci address or name */
235 ret = eal_parse_pci_DomBDF(device_id, &dev_addr);
240 ret = snprintf(domBDF, len, "%u:%u:%u.%u", dev_addr.domain,
241 dev_addr.bus, dev_addr.devid,
244 ret = snprintf(domBDF, len, "%u:%u.%u", dev_addr.bus,
252 pdump_regitser_rx_callbacks(uint16_t end_q, uint8_t port, uint16_t queue,
253 struct rte_ring *ring, struct rte_mempool *mp,
257 struct pdump_rxtx_cbs *cbs = NULL;
259 qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
260 for (; qid < end_q; qid++) {
261 cbs = &rx_cbs[port][qid];
262 if (cbs && operation == ENABLE) {
265 "failed to add rx callback for port=%d "
266 "and queue=%d, callback already exists\n",
272 cbs->cb = rte_eth_add_first_rx_callback(port, qid,
274 if (cbs->cb == NULL) {
276 "failed to add rx callback, errno=%d\n",
281 if (cbs && operation == DISABLE) {
284 if (cbs->cb == NULL) {
286 "failed to delete non existing rx "
287 "callback for port=%d and queue=%d\n",
291 ret = rte_eth_remove_rx_callback(port, qid, cbs->cb);
294 "failed to remove rx callback, errno=%d\n",
306 pdump_regitser_tx_callbacks(uint16_t end_q, uint8_t port, uint16_t queue,
307 struct rte_ring *ring, struct rte_mempool *mp,
312 struct pdump_rxtx_cbs *cbs = NULL;
314 qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
315 for (; qid < end_q; qid++) {
316 cbs = &tx_cbs[port][qid];
317 if (cbs && operation == ENABLE) {
320 "failed to add tx callback for port=%d "
321 "and queue=%d, callback already exists\n",
327 cbs->cb = rte_eth_add_tx_callback(port, qid, pdump_tx,
329 if (cbs->cb == NULL) {
331 "failed to add tx callback, errno=%d\n",
336 if (cbs && operation == DISABLE) {
339 if (cbs->cb == NULL) {
341 "failed to delete non existing tx "
342 "callback for port=%d and queue=%d\n",
346 ret = rte_eth_remove_tx_callback(port, qid, cbs->cb);
349 "failed to remove tx callback, errno=%d\n",
361 set_pdump_rxtx_cbs(struct pdump_request *p)
363 uint16_t nb_rx_q, nb_tx_q = 0, end_q, queue;
368 struct rte_ring *ring;
369 struct rte_mempool *mp;
373 if (operation == ENABLE) {
374 ret = rte_eth_dev_get_port_by_name(p->data.en_v1.device,
378 "failed to get potid for device id=%s\n",
379 p->data.en_v1.device);
382 queue = p->data.en_v1.queue;
383 ring = p->data.en_v1.ring;
384 mp = p->data.en_v1.mp;
386 ret = rte_eth_dev_get_port_by_name(p->data.dis_v1.device,
390 "failed to get potid for device id=%s\n",
391 p->data.dis_v1.device);
394 queue = p->data.dis_v1.queue;
395 ring = p->data.dis_v1.ring;
396 mp = p->data.dis_v1.mp;
399 /* validation if packet capture is for all queues */
400 if (queue == RTE_PDUMP_ALL_QUEUES) {
401 struct rte_eth_dev_info dev_info;
403 rte_eth_dev_info_get(port, &dev_info);
404 nb_rx_q = dev_info.nb_rx_queues;
405 nb_tx_q = dev_info.nb_tx_queues;
406 if (nb_rx_q == 0 && flags & RTE_PDUMP_FLAG_RX) {
408 "number of rx queues cannot be 0\n");
411 if (nb_tx_q == 0 && flags & RTE_PDUMP_FLAG_TX) {
413 "number of tx queues cannot be 0\n");
416 if ((nb_tx_q == 0 || nb_rx_q == 0) &&
417 flags == RTE_PDUMP_FLAG_RXTX) {
419 "both tx&rx queues must be non zero\n");
424 /* register RX callback */
425 if (flags & RTE_PDUMP_FLAG_RX) {
426 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1;
427 ret = pdump_regitser_rx_callbacks(end_q, port, queue, ring, mp,
433 /* register TX callback */
434 if (flags & RTE_PDUMP_FLAG_TX) {
435 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1;
436 ret = pdump_regitser_tx_callbacks(end_q, port, queue, ring, mp,
445 /* get socket path (/var/run if root, $HOME otherwise) */
447 pdump_get_socket_path(char *buffer, int bufsz, enum rte_pdump_socktype type)
449 char dpdk_dir[PATH_MAX] = {0};
450 char dir[PATH_MAX] = {0};
451 char *dir_home = NULL;
453 if (type == RTE_PDUMP_SOCKET_SERVER && server_socket_dir[0] != 0)
454 snprintf(dir, sizeof(dir), "%s", server_socket_dir);
455 else if (type == RTE_PDUMP_SOCKET_CLIENT && client_socket_dir[0] != 0)
456 snprintf(dir, sizeof(dir), "%s", client_socket_dir);
459 dir_home = getenv(SOCKET_PATH_HOME);
462 "Failed to get environment variable"
463 " value for %s, %s:%d\n",
464 SOCKET_PATH_HOME, __func__, __LINE__);
467 snprintf(dpdk_dir, sizeof(dpdk_dir), "%s%s",
470 snprintf(dpdk_dir, sizeof(dpdk_dir), "%s%s",
471 SOCKET_PATH_VAR_RUN, DPDK_DIR);
473 mkdir(dpdk_dir, 700);
474 snprintf(dir, sizeof(dir), "%s%s",
475 dpdk_dir, SOCKET_DIR);
479 if (type == RTE_PDUMP_SOCKET_SERVER)
480 snprintf(buffer, bufsz, SERVER_SOCKET, dir);
482 snprintf(buffer, bufsz, CLIENT_SOCKET, dir, getpid(),
489 pdump_create_server_socket(void)
492 struct sockaddr_un addr;
495 ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
496 RTE_PDUMP_SOCKET_SERVER);
499 "Failed to get server socket path: %s:%d\n",
503 addr.sun_family = AF_UNIX;
505 /* remove if file already exists */
506 unlink(addr.sun_path);
508 /* set up a server socket */
509 socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
512 "Failed to create server socket: %s, %s:%d\n",
513 strerror(errno), __func__, __LINE__);
517 addr_len = sizeof(struct sockaddr_un);
518 ret = bind(socket_fd, (struct sockaddr *) &addr, addr_len);
521 "Failed to bind to server socket: %s, %s:%d\n",
522 strerror(errno), __func__, __LINE__);
527 /* save the socket in local configuration */
528 pdump_socket_fd = socket_fd;
533 static __attribute__((noreturn)) void *
534 pdump_thread_main(__rte_unused void *arg)
536 struct sockaddr_un cli_addr;
538 struct pdump_request cli_req;
539 struct pdump_response resp;
543 /* host thread, never break out */
545 /* recv client requests */
546 cli_len = sizeof(cli_addr);
547 n = recvfrom(pdump_socket_fd, &cli_req,
548 sizeof(struct pdump_request), 0,
549 (struct sockaddr *)&cli_addr, &cli_len);
552 "failed to recv from client:%s, %s:%d\n",
553 strerror(errno), __func__, __LINE__);
557 ret = set_pdump_rxtx_cbs(&cli_req);
559 resp.ver = cli_req.ver;
560 resp.res_op = cli_req.op;
561 resp.err_value = ret;
562 n = sendto(pdump_socket_fd, &resp,
563 sizeof(struct pdump_response),
564 0, (struct sockaddr *)&cli_addr, cli_len);
567 "failed to send to client:%s, %s:%d\n",
568 strerror(errno), __func__, __LINE__);
574 rte_pdump_init(const char *path)
577 char thread_name[RTE_MAX_THREAD_NAME_LEN];
579 ret = rte_pdump_set_socket_dir(path, RTE_PDUMP_SOCKET_SERVER);
583 ret = pdump_create_server_socket();
586 "Failed to create server socket:%s:%d\n",
591 /* create the host thread to wait/handle pdump requests */
592 ret = pthread_create(&pdump_thread, NULL, pdump_thread_main, NULL);
595 "Failed to create the pdump thread:%s, %s:%d\n",
596 strerror(errno), __func__, __LINE__);
599 /* Set thread_name for aid in debugging. */
600 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "pdump-thread");
601 ret = rte_thread_setname(pdump_thread, thread_name);
603 RTE_LOG(DEBUG, PDUMP,
604 "Failed to set thread name for pdump handling\n");
611 rte_pdump_uninit(void)
615 ret = pthread_cancel(pdump_thread);
618 "Failed to cancel the pdump thread:%s, %s:%d\n",
619 strerror(errno), __func__, __LINE__);
623 ret = close(pdump_socket_fd);
626 "Failed to close server socket: %s, %s:%d\n",
627 strerror(errno), __func__, __LINE__);
631 struct sockaddr_un addr;
633 ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
634 RTE_PDUMP_SOCKET_SERVER);
637 "Failed to get server socket path: %s:%d\n",
641 ret = unlink(addr.sun_path);
644 "Failed to remove server socket addr: %s, %s:%d\n",
645 strerror(errno), __func__, __LINE__);
653 pdump_create_client_socket(struct pdump_request *p)
658 struct pdump_response server_resp;
659 struct sockaddr_un addr, serv_addr, from;
660 socklen_t addr_len, serv_len;
664 socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
667 "client socket(): %s:pid(%d):tid(%u), %s:%d\n",
668 strerror(errno), pid, rte_sys_gettid(),
674 ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
675 RTE_PDUMP_SOCKET_CLIENT);
678 "Failed to get client socket path: %s:%d\n",
682 addr.sun_family = AF_UNIX;
683 addr_len = sizeof(struct sockaddr_un);
686 ret = bind(socket_fd, (struct sockaddr *) &addr, addr_len);
689 "client bind(): %s, %s:%d\n",
690 strerror(errno), __func__, __LINE__);
695 serv_len = sizeof(struct sockaddr_un);
696 memset(&serv_addr, 0, sizeof(serv_addr));
697 ret = pdump_get_socket_path(serv_addr.sun_path,
698 sizeof(serv_addr.sun_path),
699 RTE_PDUMP_SOCKET_SERVER);
702 "Failed to get server socket path: %s:%d\n",
706 serv_addr.sun_family = AF_UNIX;
708 n = sendto(socket_fd, p, sizeof(struct pdump_request), 0,
709 (struct sockaddr *)&serv_addr, serv_len);
712 "failed to send to server:%s, %s:%d\n",
713 strerror(errno), __func__, __LINE__);
718 n = recvfrom(socket_fd, &server_resp,
719 sizeof(struct pdump_response), 0,
720 (struct sockaddr *)&from, &serv_len);
723 "failed to recv from server:%s, %s:%d\n",
724 strerror(errno), __func__, __LINE__);
728 ret = server_resp.err_value;
732 unlink(addr.sun_path);
737 pdump_validate_ring_mp(struct rte_ring *ring, struct rte_mempool *mp)
739 if (ring == NULL || mp == NULL) {
740 RTE_LOG(ERR, PDUMP, "NULL ring or mempool are passed %s:%d\n",
745 if (mp->flags & MEMPOOL_F_SP_PUT || mp->flags & MEMPOOL_F_SC_GET) {
746 RTE_LOG(ERR, PDUMP, "mempool with either SP or SC settings"
747 " is not valid for pdump, should have MP and MC settings\n");
751 if (ring->prod.sp_enqueue || ring->cons.sc_dequeue) {
752 RTE_LOG(ERR, PDUMP, "ring with either SP or SC settings"
753 " is not valid for pdump, should have MP and MC settings\n");
762 pdump_validate_flags(uint32_t flags)
764 if (flags != RTE_PDUMP_FLAG_RX && flags != RTE_PDUMP_FLAG_TX &&
765 flags != RTE_PDUMP_FLAG_RXTX) {
767 "invalid flags, should be either rx/tx/rxtx\n");
776 pdump_validate_port(uint8_t port, char *name)
780 if (port >= RTE_MAX_ETHPORTS) {
781 RTE_LOG(ERR, PDUMP, "Invalid port id %u, %s:%d\n", port,
787 ret = rte_eth_dev_get_name_by_port(port, name);
790 "port id to name mapping failed for port id=%u, %s:%d\n",
791 port, __func__, __LINE__);
800 pdump_prepare_client_request(char *device, uint16_t queue,
803 struct rte_ring *ring,
804 struct rte_mempool *mp,
808 struct pdump_request req = {.ver = 1,};
812 if ((operation & ENABLE) != 0) {
813 snprintf(req.data.en_v1.device, sizeof(req.data.en_v1.device),
815 req.data.en_v1.queue = queue;
816 req.data.en_v1.ring = ring;
817 req.data.en_v1.mp = mp;
818 req.data.en_v1.filter = filter;
820 snprintf(req.data.dis_v1.device, sizeof(req.data.dis_v1.device),
822 req.data.dis_v1.queue = queue;
823 req.data.dis_v1.ring = NULL;
824 req.data.dis_v1.mp = NULL;
825 req.data.dis_v1.filter = NULL;
828 ret = pdump_create_client_socket(&req);
831 "client request for pdump enable/disable failed\n");
840 rte_pdump_enable(uint8_t port, uint16_t queue, uint32_t flags,
841 struct rte_ring *ring,
842 struct rte_mempool *mp,
847 char name[DEVICE_ID_SIZE];
849 ret = pdump_validate_port(port, name);
852 ret = pdump_validate_ring_mp(ring, mp);
855 ret = pdump_validate_flags(flags);
859 ret = pdump_prepare_client_request(name, queue, flags,
860 ENABLE, ring, mp, filter);
866 rte_pdump_enable_by_deviceid(char *device_id, uint16_t queue,
868 struct rte_ring *ring,
869 struct rte_mempool *mp,
873 char domBDF[DEVICE_ID_SIZE];
875 ret = pdump_validate_ring_mp(ring, mp);
878 ret = pdump_validate_flags(flags);
882 if (pdump_get_dombdf(device_id, domBDF, sizeof(domBDF)) > 0)
883 ret = pdump_prepare_client_request(domBDF, queue, flags,
884 ENABLE, ring, mp, filter);
886 ret = pdump_prepare_client_request(device_id, queue, flags,
887 ENABLE, ring, mp, filter);
893 rte_pdump_disable(uint8_t port, uint16_t queue, uint32_t flags)
896 char name[DEVICE_ID_SIZE];
898 ret = pdump_validate_port(port, name);
901 ret = pdump_validate_flags(flags);
905 ret = pdump_prepare_client_request(name, queue, flags,
906 DISABLE, NULL, NULL, NULL);
912 rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue,
916 char domBDF[DEVICE_ID_SIZE];
918 ret = pdump_validate_flags(flags);
922 if (pdump_get_dombdf(device_id, domBDF, sizeof(domBDF)) > 0)
923 ret = pdump_prepare_client_request(domBDF, queue, flags,
924 DISABLE, NULL, NULL, NULL);
926 ret = pdump_prepare_client_request(device_id, queue, flags,
927 DISABLE, NULL, NULL, NULL);
933 rte_pdump_set_socket_dir(const char *path, enum rte_pdump_socktype type)
938 if (type == RTE_PDUMP_SOCKET_SERVER) {
939 count = sizeof(server_socket_dir);
940 ret = snprintf(server_socket_dir, count, "%s", path);
942 count = sizeof(client_socket_dir);
943 ret = snprintf(client_socket_dir, count, "%s", path);
946 if (ret < 0 || ret >= count) {
948 "Invalid socket path:%s:%d\n",
950 if (type == RTE_PDUMP_SOCKET_SERVER)
951 server_socket_dir[0] = 0;
953 client_socket_dir[0] = 0;