From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by dpdk.org (Postfix) with ESMTP id 1423F37AA for ; Fri, 27 May 2016 15:39:22 +0200 (CEST) Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP; 27 May 2016 06:39:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.26,374,1459839600"; d="scan'208";a="816090725" Received: from irsmsx108.ger.corp.intel.com ([163.33.3.3]) by orsmga003.jf.intel.com with ESMTP; 27 May 2016 06:39:21 -0700 Received: from irsmsx111.ger.corp.intel.com (10.108.20.4) by IRSMSX108.ger.corp.intel.com (163.33.3.3) with Microsoft SMTP Server (TLS) id 14.3.248.2; Fri, 27 May 2016 14:39:19 +0100 Received: from irsmsx105.ger.corp.intel.com ([169.254.7.51]) by irsmsx111.ger.corp.intel.com ([169.254.2.182]) with mapi id 14.03.0248.002; Fri, 27 May 2016 14:39:19 +0100 From: "Ananyev, Konstantin" To: "Pattan, Reshma" , "dev@dpdk.org" CC: "Pattan, Reshma" Thread-Topic: [dpdk-dev] [PATCH v4 5/9] lib/librte_pdump: add new library for packet capturing support Thread-Index: AQHRtTzJlCFb+xkZBk+bNKGIAEzkqZ/MwIAQ Date: Fri, 27 May 2016 13:39:18 +0000 Message-ID: <2601191342CEEE43887BDE71AB97725836B67FBC@irsmsx105.ger.corp.intel.com> References: <1463503030-10318-1-git-send-email-reshma.pattan@intel.com> <1464039512-2683-1-git-send-email-reshma.pattan@intel.com> <1464039512-2683-6-git-send-email-reshma.pattan@intel.com> In-Reply-To: <1464039512-2683-6-git-send-email-reshma.pattan@intel.com> Accept-Language: en-IE, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [163.33.239.182] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH v4 5/9] lib/librte_pdump: add new library for packet capturing support X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 May 2016 13:39:24 -0000 Hi Reshma, >=20 > Added new library for packet capturing support. >=20 > Added public api rte_pdump_init, applications should call > this as part of their application setup to have packet > capturing framework ready. >=20 > Added public api rte_pdump_uninit to un initialize the packet > capturing framework. >=20 > Added public apis rte_pdump_enable and rte_pdump_disable to > enable and disable packet capturing on specific port and queue. >=20 > Added public apis rte_pdump_enable_by_deviceid and > rte_pdump_disable_by_deviceid to enable and disable packet > capturing on a specific device (pci address or name) and queue. >=20 > Signed-off-by: Reshma Pattan > --- > MAINTAINERS | 4 + > config/common_base | 5 + > lib/Makefile | 1 + > lib/librte_pdump/Makefile | 55 +++ > lib/librte_pdump/rte_pdump.c | 816 +++++++++++++++++++++++++++= ++++++ > lib/librte_pdump/rte_pdump.h | 186 ++++++++ > lib/librte_pdump/rte_pdump_version.map | 12 + > mk/rte.app.mk | 1 + > 8 files changed, 1080 insertions(+) > create mode 100644 lib/librte_pdump/Makefile > create mode 100644 lib/librte_pdump/rte_pdump.c > create mode 100644 lib/librte_pdump/rte_pdump.h > create mode 100644 lib/librte_pdump/rte_pdump_version.map >=20 > diff --git a/MAINTAINERS b/MAINTAINERS > index 9dd0738..8656239 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -433,6 +433,10 @@ F: app/test/test_reorder* > F: examples/packet_ordering/ > F: doc/guides/sample_app_ug/packet_ordering.rst >=20 > +Pdump > +M: Reshma Pattan > +F: lib/librte_pdump/ > + > Hierarchical scheduler > M: Cristian Dumitrescu > F: lib/librte_sched/ > diff --git a/config/common_base b/config/common_base > index 3535c6e..259bf0a 100644 > --- a/config/common_base > +++ b/config/common_base > @@ -484,6 +484,11 @@ CONFIG_RTE_LIBRTE_DISTRIBUTOR=3Dy > CONFIG_RTE_LIBRTE_REORDER=3Dy >=20 > # > +# Compile the pdump library > +# > +CONFIG_RTE_LIBRTE_PDUMP=3Dy > + > +# > # Compile librte_port > # > CONFIG_RTE_LIBRTE_PORT=3Dy > diff --git a/lib/Makefile b/lib/Makefile > index f254dba..ca7c02f 100644 > --- a/lib/Makefile > +++ b/lib/Makefile > @@ -57,6 +57,7 @@ DIRS-$(CONFIG_RTE_LIBRTE_PORT) +=3D librte_port > DIRS-$(CONFIG_RTE_LIBRTE_TABLE) +=3D librte_table > DIRS-$(CONFIG_RTE_LIBRTE_PIPELINE) +=3D librte_pipeline > DIRS-$(CONFIG_RTE_LIBRTE_REORDER) +=3D librte_reorder > +DIRS-$(CONFIG_RTE_LIBRTE_PDUMP) +=3D librte_pdump >=20 > ifeq ($(CONFIG_RTE_EXEC_ENV_LINUXAPP),y) > DIRS-$(CONFIG_RTE_LIBRTE_KNI) +=3D librte_kni > diff --git a/lib/librte_pdump/Makefile b/lib/librte_pdump/Makefile > new file mode 100644 > index 0000000..af81a28 > --- /dev/null > +++ b/lib/librte_pdump/Makefile > @@ -0,0 +1,55 @@ > +# BSD LICENSE > +# > +# Copyright(c) 2016 Intel Corporation. All rights reserved. > +# All rights reserved. > +# > +# Redistribution and use in source and binary forms, with or without > +# modification, are permitted provided that the following conditions > +# are met: > +# > +# * Redistributions of source code must retain the above copyright > +# notice, this list of conditions and the following disclaimer. > +# * Redistributions in binary form must reproduce the above copyrigh= t > +# notice, this list of conditions and the following disclaimer in > +# the documentation and/or other materials provided with the > +# distribution. > +# * Neither the name of Intel Corporation nor the names of its > +# contributors may be used to endorse or promote products derived > +# from this software without specific prior written permission. > +# > +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS > +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT > +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FO= R > +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT > +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL= , > +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE= , > +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON AN= Y > +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT > +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE US= E > +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. > + > +include $(RTE_SDK)/mk/rte.vars.mk > + > +# library name > +LIB =3D librte_pdump.a > + > +CFLAGS +=3D $(WERROR_FLAGS) -I$(SRCDIR) -O3 > +CFLAGS +=3D -D_GNU_SOURCE > + > +EXPORT_MAP :=3D rte_pdump_version.map > + > +LIBABIVER :=3D 1 > + > +# all source are stored in SRCS-y > +SRCS-$(CONFIG_RTE_LIBRTE_PDUMP) :=3D rte_pdump.c > + > +# install this header file > +SYMLINK-$(CONFIG_RTE_LIBRTE_PDUMP)-include :=3D rte_pdump.h > + > +# this lib depends upon: > +DEPDIRS-$(CONFIG_RTE_LIBRTE_PDUMP) +=3D lib/librte_mbuf > +DEPDIRS-$(CONFIG_RTE_LIBRTE_PDUMP) +=3D lib/librte_eal > +DEPDIRS-$(CONFIG_RTE_LIBRTE_PDUMP) +=3D lib/librte_ether > + > +include $(RTE_SDK)/mk/rte.lib.mk > diff --git a/lib/librte_pdump/rte_pdump.c b/lib/librte_pdump/rte_pdump.c > new file mode 100644 > index 0000000..915cd37 > --- /dev/null > +++ b/lib/librte_pdump/rte_pdump.c > @@ -0,0 +1,816 @@ > +/*- > + * BSD LICENSE > + * > + * Copyright(c) 2016 Intel Corporation. All rights reserved. > + * All rights reserved. > + * > + * Redistribution and use in source and binary forms, with or without > + * modification, are permitted provided that the following conditions > + * are met: > + * > + * * Redistributions of source code must retain the above copyright > + * notice, this list of conditions and the following disclaimer. > + * * Redistributions in binary form must reproduce the above copyrig= ht > + * notice, this list of conditions and the following disclaimer in > + * the documentation and/or other materials provided with the > + * distribution. > + * * Neither the name of Intel Corporation nor the names of its > + * contributors may be used to endorse or promote products derived > + * from this software without specific prior written permission. > + * > + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS > + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT > + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS F= OR > + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGH= T > + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTA= L, > + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF US= E, > + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A= NY > + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT > + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE U= SE > + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE= . > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "rte_pdump.h" > + > +#define SOCKET_PATH_VAR_RUN "/var/run/pdump_sockets" > +#define SOCKET_PATH_HOME "HOME/pdump_sockets" > +#define SERVER_SOCKET "%s/pdump_server_socket" > +#define CLIENT_SOCKET "%s/pdump_client_socket_%d_%u" > +#define DEVICE_ID_SIZE 64 > +/* Macros for printing using RTE_LOG */ > +#define RTE_LOGTYPE_PDUMP RTE_LOGTYPE_USER1 > + > +enum pdump_operation { > + DISABLE =3D 1, > + ENABLE =3D 2 > +}; > + > +enum pdump_socktype { > + SERVER =3D 1, > + CLIENT =3D 2 > +}; > + > +enum pdump_version { > + V1 =3D 1 > +}; > + > +static pthread_t pdump_thread; > +static int pdump_socket_fd; > + > +struct pdump_request { > + uint16_t ver; > + uint16_t op; > + uint32_t dir; I understand why you named it 'dir' (as right now it could be only RX & TX)= , but I think we better name it in a more generic way: flags or so. There are 29 unused bits right now, and probably in future will use it For something more than just traffic direction indication.=20 > + union pdump_data { > + struct enable_v1 { > + char device[DEVICE_ID_SIZE]; > + uint16_t queue; > + struct rte_ring *ring; > + struct rte_mempool *mp; > + void *filter; > + bool is_pci_or_name; > + } en_v1; > + struct disable_v1 { > + char device[DEVICE_ID_SIZE]; > + uint16_t queue; > + struct rte_ring *ring; > + struct rte_mempool *mp; > + void *filter; > + bool is_pci_or_name; > + } dis_v1; > + } data; > +}; > + > +struct pdump_response { > + uint16_t ver; > + uint16_t res_op; > + int32_t err_value; > +}; > + > +static struct pdump_rxtx_cbs { > + struct rte_ring *ring; > + struct rte_mempool *mp; > + struct rte_eth_rxtx_callback *cb; > + void *filter; > +} rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT], > +tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT]; > + > +static inline int > +pdump_pktmbuf_copy_data(struct rte_mbuf *seg, const struct rte_mbuf *m) > +{ > + if (rte_pktmbuf_tailroom(seg) < m->data_len) { > + RTE_LOG(ERR, PDUMP, "User mempool: insufficient data_len of mbuf\n"); > + return -EINVAL; > + } > + > + seg->port =3D m->port; > + seg->vlan_tci =3D m->vlan_tci; > + seg->hash =3D m->hash; > + seg->tx_offload =3D m->tx_offload; > + seg->ol_flags =3D m->ol_flags; > + seg->packet_type =3D m->packet_type; > + seg->vlan_tci_outer =3D m->vlan_tci_outer; > + seg->data_len =3D m->data_len; > + seg->pkt_len =3D seg->data_len; > + rte_memcpy(rte_pktmbuf_mtod(seg, void *), > + rte_pktmbuf_mtod(m, void *), > + rte_pktmbuf_data_len(seg)); > + > + __rte_mbuf_sanity_check(seg, 1); And I don't think you need that sanity_check() here at all, as you call it = anyway in pdump_pktmbuf_copy(). But if you still like to have it there, then _sanity_check(seg, 0) seems en= ough here. > + > + return 0; > +} > + > +static inline struct rte_mbuf * > +pdump_pktmbuf_copy(struct rte_mbuf *m, struct rte_mempool *mp) > +{ > + struct rte_mbuf *m_dup, *seg, **prev; > + uint32_t pktlen; > + uint8_t nseg; > + > + m_dup =3D rte_pktmbuf_alloc(mp); > + if (unlikely(m_dup =3D=3D NULL)) > + return NULL; > + > + seg =3D m_dup; > + prev =3D &seg->next; > + pktlen =3D m->pkt_len; > + nseg =3D 0; > + > + do { > + nseg++; > + if (pdump_pktmbuf_copy_data(seg, m) < 0) { > + rte_pktmbuf_free(m_dup); > + return NULL; > + } > + *prev =3D seg; > + prev =3D &seg->next; > + } while ((m =3D m->next) !=3D NULL && > + (seg =3D rte_pktmbuf_alloc(mp)) !=3D NULL); > + > + *prev =3D NULL; > + m_dup->nb_segs =3D nseg; > + m_dup->pkt_len =3D pktlen; > + > + /* Allocation of new indirect segment failed */ > + if (unlikely(seg =3D=3D NULL)) { > + rte_pktmbuf_free(m_dup); > + return NULL; > + } > + > + __rte_mbuf_sanity_check(m_dup, 1); > + return m_dup; > +} > + > +static inline void > +pdump_copy(struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params) > +{ > + unsigned i; > + int ring_enq; > + uint16_t d_pkts =3D 0; > + struct rte_mbuf *dup_bufs[nb_pkts]; > + struct pdump_rxtx_cbs *cbs; > + struct rte_ring *ring; > + struct rte_mempool *mp; > + struct rte_mbuf *p; > + > + cbs =3D user_params; > + ring =3D cbs->ring; > + mp =3D cbs->mp; > + for (i =3D 0; i < nb_pkts; i++) { > + p =3D pdump_pktmbuf_copy(pkts[i], mp); > + if (p) > + dup_bufs[d_pkts++] =3D p; > + } > + > + ring_enq =3D rte_ring_enqueue_burst(ring, (void *)dup_bufs, d_pkts); > + if (unlikely(ring_enq < d_pkts)) { > + RTE_LOG(DEBUG, PDUMP, "only %d of packets enqueued to ring\n", ring_en= q); > + do { > + rte_pktmbuf_free(dup_bufs[ring_enq]); > + } while (++ring_enq < d_pkts); > + } > +} > + > +static uint16_t > +pdump_rx(uint8_t port __rte_unused, uint16_t qidx __rte_unused, > + struct rte_mbuf **pkts, uint16_t nb_pkts, uint16_t max_pkts __rte_unuse= d, > + void *user_params) > +{ > + pdump_copy(pkts, nb_pkts, user_params); > + return nb_pkts; > +} > + > +static uint16_t > +pdump_tx(uint8_t port __rte_unused, uint16_t qidx __rte_unused, > + struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params) > +{ > + pdump_copy(pkts, nb_pkts, user_params); > + return nb_pkts; > +} > + > +static int > +pdump_get_dombdf(char *device_id, char *domBDF) > +{ > + int ret; > + struct rte_pci_addr dev_addr =3D {0}; > + > + ret =3D eal_parse_pci_DomBDF(device_id, &dev_addr); > + if (ret < 0) > + return -1; > + > + if (dev_addr.domain) > + snprintf(domBDF, DEVICE_ID_SIZE, "%u:%u:%u.%u", dev_addr.domain, > + dev_addr.bus, dev_addr.devid, dev_addr.function); > + else > + snprintf(domBDF, DEVICE_ID_SIZE, "%u:%u.%u", dev_addr.bus, dev_addr.de= vid, > + dev_addr.function); Instead of implicitly assume that domBDF is at least DEVICE_ID_SIZE bytes l= ong, why just not pass its size as an extra parameter?=20 > + > + return 0; > +} > + > +static int > +pdump_regitser_callbacks(uint32_t dir, uint16_t end_q, > + uint8_t port, uint16_t queue, > + struct rte_ring *ring, struct rte_mempool *mp, > + uint16_t operation) > +{ > + > + uint16_t qid; > + struct pdump_rxtx_cbs *cbs =3D NULL; > + > + qid =3D (queue =3D=3D RTE_PDUMP_ALL_QUEUES) ? 0 : queue; > + for (; qid < end_q; qid++) { > + if ((dir & RTE_PDUMP_FLAG_RX) !=3D 0) > + cbs =3D &rx_cbs[port][qid]; > + if ((dir & RTE_PDUMP_FLAG_TX) !=3D 0) > + cbs =3D &tx_cbs[port][qid]; In case you have dir =3D=3D (RTE_PDUMP_FLAG_RX | RTE_PDUMP_FLAG_TX) you'll overwrite your rx_cbs pointer with tx_cbs pointer. I suppose you need 2 local vars: cbs_rx and cbs_tx here. Again probably worth to have 2 helper functions: pdump_regitser_rx_callback() and pdump_regitser_tx_callback() and call them from that one.=20 Or you'll never invoke that function with dir =3D=3D(RTE_PDUMP_FLAG_RX | RT= E_PDUMP_FLAG_TX)? If so, it porbably worth to put it into comments, though if it would be me,= I still think it would be good to split it in a way I mentioned above.=20 > + if (cbs && operation =3D=3D ENABLE) { > + if (cbs->cb) { > + RTE_LOG(ERR, PDUMP, > + "failed to add callback for port=3D%d and " > + "queue=3D%d, callback already exists\n", > + port, qid); > + return -EEXIST; > + } > + cbs->ring =3D ring; > + cbs->mp =3D mp; > + if ((dir & RTE_PDUMP_FLAG_RX) !=3D 0) { > + cbs->cb =3D rte_eth_add_first_rx_callback(port, qid, > + pdump_rx, cbs); > + if (cbs->cb =3D=3D NULL) { > + RTE_LOG(ERR, PDUMP, > + "failed to add rx callback, errno=3D%d\n", > + rte_errno); > + return rte_errno; > + } > + } > + if ((dir & RTE_PDUMP_FLAG_TX) !=3D 0) { > + cbs->cb =3D rte_eth_add_tx_callback(port, qid, pdump_tx, > + cbs); > + if (cbs->cb =3D=3D NULL) { > + RTE_LOG(ERR, PDUMP, > + "failed to add tx callback, errno=3D%d\n", > + rte_errno); > + return rte_errno; > + } > + } > + } > + if (cbs && operation =3D=3D DISABLE) { > + int ret; > + > + if (cbs->cb =3D=3D NULL) { > + RTE_LOG(ERR, PDUMP, > + "failed to delete non existing callback " > + "for port=3D%d and queue=3D%d\n", port, qid); > + return -EINVAL; > + } > + if ((dir & RTE_PDUMP_FLAG_RX) !=3D 0) { > + ret =3D rte_eth_remove_rx_callback(port, qid, cbs->cb); > + if (ret < 0) { > + RTE_LOG(ERR, PDUMP, > + "failed to remove rx callback, errno=3D%d\n", > + rte_errno); > + return ret; > + } > + } > + if ((dir & RTE_PDUMP_FLAG_TX) !=3D 0) { > + ret =3D rte_eth_remove_tx_callback(port, qid, cbs->cb); > + if (ret < 0) { > + RTE_LOG(ERR, PDUMP, > + "failed to remove tx callback, errno=3D%d\n", > + rte_errno); > + return ret; > + } > + } > + cbs->cb =3D NULL; > + } > + } > + > + return 0; > +} > + > +static int > +set_pdump_rxtx_cbs(struct pdump_request *p) > +{ > + uint16_t nb_rx_q, nb_tx_q =3D 0, end_q, queue; > + uint8_t port; > + int ret =3D 0; > + uint32_t dir; > + uint16_t operation; > + struct rte_ring *ring; > + struct rte_mempool *mp; > + char domBDF[DEVICE_ID_SIZE]; > + > + dir =3D p->dir; > + operation =3D p->op; > + if (operation =3D=3D ENABLE) { > + if (p->data.en_v1.is_pci_or_name =3D=3D true) { > + /* check if device is pci address or name */ > + if (pdump_get_dombdf(p->data.en_v1.device, domBDF) =3D=3D 0) > + ret =3D rte_eth_dev_get_port_by_name(domBDF, &port); > + else > + ret =3D rte_eth_dev_get_port_by_name(p->data.en_v1.device, > + &port); Why we can't force client to have device name in predefined format? Then you woudn't need that name conversion here. > + if (ret < 0) { > + RTE_LOG(ERR, PDUMP, > + "failed to get potid for device id=3D%s\n", > + p->data.en_v1.device); > + return -EINVAL; > + } > + } else /* if device is port id */ > + port =3D atoi(p->data.en_v1.device); Hmm, again why not make server to accept requests only by device id? Then it would be client responsibility to do port to device id, and you can get rid of some duplicated code here. > + queue =3D p->data.en_v1.queue; > + ring =3D p->data.en_v1.ring; > + mp =3D p->data.en_v1.mp; > + } else { > + if (p->data.dis_v1.is_pci_or_name =3D=3D true) { > + /* check if device is pci address or name */ > + if (pdump_get_dombdf(p->data.dis_v1.device, domBDF) =3D=3D 0) > + ret =3D rte_eth_dev_get_port_by_name(domBDF, &port); > + else > + ret =3D rte_eth_dev_get_port_by_name(p->data.dis_v1.device, > + &port); > + if (ret < 0) { > + RTE_LOG(ERR, PDUMP, > + "failed to get potid for device id=3D%s\n", > + p->data.dis_v1.device); > + return -EINVAL; > + } > + } else /* if device is port id */ > + port =3D atoi(p->data.dis_v1.device); > + queue =3D p->data.dis_v1.queue; > + ring =3D p->data.dis_v1.ring; > + mp =3D p->data.dis_v1.mp; > + } > + > + /* validation if packet capture is for all queues */ > + if (queue =3D=3D RTE_PDUMP_ALL_QUEUES) { > + struct rte_eth_dev_info dev_info; > + > + rte_eth_dev_info_get(port, &dev_info); > + nb_rx_q =3D dev_info.nb_rx_queues; > + nb_tx_q =3D dev_info.nb_tx_queues; > + if (nb_rx_q =3D=3D 0 && dir =3D=3D RTE_PDUMP_FLAG_RX) { > + RTE_LOG(ERR, PDUMP, "number of rx queues cannot be 0\n"); > + return -EINVAL; > + } > + if (nb_tx_q =3D=3D 0 && dir =3D=3D RTE_PDUMP_FLAG_TX) { > + RTE_LOG(ERR, PDUMP, "number of tx queues cannot be 0\n"); > + return -EINVAL; > + } > + if ((nb_tx_q =3D=3D 0 || nb_rx_q =3D=3D 0) && dir =3D=3D RTE_PDUMP_FLA= G_RXTX) { > + RTE_LOG(ERR, PDUMP, "both tx&rx queues must be non zero\n"); > + return -EINVAL; > + } > + } > + > + /* register RX callback for dir rx/rxtx */ > + if (dir =3D=3D RTE_PDUMP_FLAG_RX || dir =3D=3D RTE_PDUMP_FLAG_RXTX) { just if (dir & RTE_PDUMP_FLAG_RX) ? > + end_q =3D (queue =3D=3D RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1; > + ret =3D pdump_regitser_callbacks(RTE_PDUMP_FLAG_RX, end_q, port, queue= , > + ring, mp, operation); > + if (ret < 0) > + return ret; > + } > + > + /* register TX callback for dir tx/rxtx */ > + if (dir =3D=3D RTE_PDUMP_FLAG_TX || dir =3D=3D RTE_PDUMP_FLAG_RXTX) { > + end_q =3D (queue =3D=3D RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1; > + ret =3D pdump_regitser_callbacks(RTE_PDUMP_FLAG_TX, end_q, port, queue= , > + ring, mp, operation); > + if (ret < 0) > + return ret; > + } > + > + return ret; > +} > + > +/* get socket path (/var/run if root, $HOME otherwise) */ > +static void > +pdump_get_socket_path(char *buffer, int bufsz, enum pdump_socktype type) > +{ > + const char *dir =3D SOCKET_PATH_VAR_RUN; > + const char *home_dir =3D getenv(SOCKET_PATH_HOME); > + > + if (getuid() !=3D 0 && home_dir !=3D NULL) > + dir =3D home_dir; > + > + mkdir(dir, 700); > + if (type =3D=3D SERVER) > + snprintf(buffer, bufsz, SERVER_SOCKET, dir); > + else > + snprintf(buffer, bufsz, CLIENT_SOCKET, dir, getpid(), > + rte_sys_gettid()); Probably add internal_config.hugefile_prefix into the name too, in case there is multiple primary DPDK procs running?=20 Konstantin > +} > + > +static int > +pdump_create_server_socket(void) > +{ > + int ret, socket_fd; > + struct sockaddr_un addr; > + socklen_t addr_len; > + > + pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path), SERVER); > + addr.sun_family =3D AF_UNIX; > + > + /* remove if file already exists */ > + unlink(addr.sun_path); > + > + /* set up a server socket */ > + socket_fd =3D socket(AF_UNIX, SOCK_DGRAM, 0); > + if (socket_fd < 0) { > + RTE_LOG(ERR, PDUMP, "Failed to create server socket: %s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + return -1; > + } > + > + addr_len =3D sizeof(struct sockaddr_un); > + ret =3D bind(socket_fd, (struct sockaddr *) &addr, addr_len); > + if (ret) { > + RTE_LOG(ERR, PDUMP, "Failed to bind to server socket: %s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + close(socket_fd); > + return -1; > + } > + > + /* save the socket in local configuration */ > + pdump_socket_fd =3D socket_fd; > + > + return 0; > +} > + > +static __attribute__((noreturn)) void * > +pdump_thread_main(__rte_unused void *arg) > +{ > + struct sockaddr_un cli_addr; > + socklen_t cli_len; > + struct pdump_request cli_req; > + struct pdump_response resp; > + int n; > + int ret =3D 0; > + > + /* host thread, never break out */ > + for (;;) { > + /* recv client requests */ > + cli_len =3D sizeof(cli_addr); > + n =3D recvfrom(pdump_socket_fd, &cli_req, sizeof(struct pdump_request)= , 0, > + (struct sockaddr *)&cli_addr, &cli_len); > + if (n < 0) { > + RTE_LOG(ERR, PDUMP, "failed to recv from client:%s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + continue; > + } > + > + ret =3D set_pdump_rxtx_cbs(&cli_req); > + > + resp.ver =3D cli_req.ver; > + resp.res_op =3D cli_req.op; > + resp.err_value =3D ret; > + n =3D sendto(pdump_socket_fd, &resp, sizeof(struct pdump_response), > + 0, (struct sockaddr *)&cli_addr, cli_len); > + if (n < 0) { > + RTE_LOG(ERR, PDUMP, "failed to send to client:%s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + } > + } > +} > + > +int > +rte_pdump_init(void) > +{ > + int ret =3D 0; > + char thread_name[RTE_MAX_THREAD_NAME_LEN]; > + > + ret =3D pdump_create_server_socket(); > + if (ret !=3D 0) { > + RTE_LOG(ERR, PDUMP, "Failed to create server socket:%s:%d\n", > + __func__, __LINE__); > + return -1; > + } > + > + /* create the host thread to wait/handle pdump requests */ > + ret =3D pthread_create(&pdump_thread, NULL, pdump_thread_main, NULL); > + if (ret !=3D 0) { > + RTE_LOG(ERR, PDUMP, "Failed to create the pdump thread:%s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + return -1; > + } > + /* Set thread_name for aid in debugging. */ > + snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "pdump-thread"); > + ret =3D rte_thread_setname(pdump_thread, thread_name); > + if (ret !=3D 0) { > + RTE_LOG(DEBUG, PDUMP, > + "Failed to set thread name for pdump handling\n"); > + } > + > + return 0; > +} > + > +int > +rte_pdump_uninit(void) > +{ > + int ret; > + > + ret =3D pthread_cancel(pdump_thread); > + if (ret !=3D 0) { > + RTE_LOG(ERR, PDUMP, "Failed to cancel the pdump thread:%s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + return -1; > + } > + > + ret =3D close(pdump_socket_fd); > + if (ret !=3D 0) { > + RTE_LOG(ERR, PDUMP, "Failed to close server socket: %s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + return -1; > + } > + > + struct sockaddr_un addr; > + > + pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path), SERVER); > + ret =3D unlink(addr.sun_path); > + if (ret !=3D 0) { > + RTE_LOG(ERR, PDUMP, "Failed to remove server socket addr: %s, %s:%d\n"= , > + strerror(errno), __func__, __LINE__); > + return -1; > + } > + > + return 0; > +} > + > +static int > +pdump_create_client_socket(struct pdump_request *p) > +{ > + int ret, socket_fd; > + int pid; > + int n; > + struct pdump_response server_resp; > + struct sockaddr_un addr, serv_addr, from; > + socklen_t addr_len, serv_len; > + > + pid =3D getpid(); > + > + socket_fd =3D socket(AF_UNIX, SOCK_DGRAM, 0); > + if (socket_fd < 0) { > + RTE_LOG(ERR, PDUMP, "client socket(): %s:pid(%d):tid(%u), %s:%d\n", > + strerror(errno), pid, rte_sys_gettid(), __func__, __LINE__); > + ret =3D errno; > + return ret; > + } > + > + pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path), CLIENT); > + addr.sun_family =3D AF_UNIX; > + addr_len =3D sizeof(struct sockaddr_un); > + > + do { > + ret =3D bind(socket_fd, (struct sockaddr *) &addr, addr_len); > + if (ret) { > + RTE_LOG(ERR, PDUMP, "client bind(): %s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + ret =3D errno; > + break; > + } > + > + serv_len =3D sizeof(struct sockaddr_un); > + memset(&serv_addr, 0, sizeof(serv_addr)); > + pdump_get_socket_path(serv_addr.sun_path, sizeof(serv_addr.sun_path), > + SERVER); > + serv_addr.sun_family =3D AF_UNIX; > + > + n =3D sendto(socket_fd, p, sizeof(struct pdump_request), 0, > + (struct sockaddr *)&serv_addr, serv_len); > + if (n < 0) { > + RTE_LOG(ERR, PDUMP, "failed to send to server:%s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + ret =3D errno; > + break; > + } > + > + n =3D recvfrom(socket_fd, &server_resp, sizeof(struct pdump_response),= 0, > + (struct sockaddr *)&from, &serv_len); > + if (n < 0) { > + RTE_LOG(ERR, PDUMP, "failed to recv from server:%s, %s:%d\n", > + strerror(errno), __func__, __LINE__); > + ret =3D errno; > + break; > + } > + ret =3D server_resp.err_value; > + } while (0); > + > + close(socket_fd); > + unlink(addr.sun_path); > + return ret; > +} > + > +static int > +pdump_validate_ring_mp(struct rte_ring *ring, struct rte_mempool *mp) > +{ > + if (ring =3D=3D NULL || mp =3D=3D NULL) { > + RTE_LOG(ERR, PDUMP, "NULL ring or mempool are passed %s:%d\n", > + __func__, __LINE__); > + rte_errno =3D EINVAL; > + return -1; > + } > + if (mp->flags & MEMPOOL_F_SP_PUT || mp->flags & MEMPOOL_F_SC_GET) { > + RTE_LOG(ERR, PDUMP, "mempool with either SP or SC settings" > + " is not valid for pdump, should have MP and MC settings\n"); > + rte_errno =3D EINVAL; > + return -1; > + } > + if (ring->prod.sp_enqueue || ring->cons.sc_dequeue) { > + RTE_LOG(ERR, PDUMP, "ring with either SP or SC settings" > + " is not valid for pdump, should have MP and MC settings\n"); > + rte_errno =3D EINVAL; > + return -1; > + } > + > + return 0; > +} > + > +static int > +pdump_validate_dir(uint32_t dir) > +{ > + if (dir !=3D RTE_PDUMP_FLAG_RX && dir !=3D RTE_PDUMP_FLAG_TX && > + dir !=3D RTE_PDUMP_FLAG_RXTX) { > + RTE_LOG(ERR, PDUMP, "invalid direction, should be either rx/tx/rxtx\n"= ); > + rte_errno =3D EINVAL; > + return -1; > + } > + > + return 0; > +} > + > +static int > +pdump_validate_port(uint8_t port) > +{ > + if (port >=3D RTE_MAX_ETHPORTS) { > + RTE_LOG(ERR, PDUMP, "Invalid port id %u, %s:%d\n", port, > + __func__, __LINE__); > + rte_errno =3D EINVAL; > + return -1; > + } > + > + return 0; > +} > + > +static int > +pdump_prepare_client_request(char *device, bool is_pci_or_name, uint16_t= queue, > + uint32_t dir, > + uint16_t operation, > + struct rte_ring *ring, > + struct rte_mempool *mp, > + void *filter) > +{ > + int ret; > + struct pdump_request req =3D {.ver =3D 1,}; > + > + req.dir =3D dir; > + req.op =3D operation; > + if ((operation & ENABLE) !=3D 0) { > + strncpy(req.data.en_v1.device, device, strlen(device)); > + req.data.en_v1.is_pci_or_name =3D is_pci_or_name; > + req.data.en_v1.queue =3D queue; > + req.data.en_v1.ring =3D ring; > + req.data.en_v1.mp =3D mp; > + req.data.en_v1.filter =3D filter; > + } else { > + strncpy(req.data.dis_v1.device, device, strlen(device)); > + req.data.dis_v1.is_pci_or_name =3D is_pci_or_name; > + req.data.dis_v1.queue =3D queue; > + req.data.dis_v1.ring =3D NULL; > + req.data.dis_v1.mp =3D NULL; > + req.data.dis_v1.filter =3D NULL; > + } > + > + ret =3D pdump_create_client_socket(&req); > + if (ret < 0) { > + RTE_LOG(ERR, PDUMP, "client request for pdump enable/disable failed\n"= ); > + rte_errno =3D ret; > + return -1; > + } > + > + return 0; > +} > + > +int > +rte_pdump_enable(uint8_t port, uint16_t queue, uint32_t dir, > + struct rte_ring *ring, > + struct rte_mempool *mp, > + void *filter) > +{ > + > + int ret =3D 0; > + char device[DEVICE_ID_SIZE]; > + > + ret =3D pdump_validate_port(port); > + if (ret < 0) > + return ret; > + ret =3D pdump_validate_ring_mp(ring, mp); > + if (ret < 0) > + return ret; > + pdump_validate_dir(dir); > + if (ret < 0) > + return ret; > + > + snprintf(device, sizeof(device), "%u", port); > + ret =3D pdump_prepare_client_request(device, false, queue, dir, > + ENABLE, ring, mp, filter); > + > + return ret; > +} > + > +int > +rte_pdump_enable_by_deviceid(char *device_id, uint16_t queue, > + uint32_t dir, > + struct rte_ring *ring, > + struct rte_mempool *mp, > + void *filter) > +{ > + int ret =3D 0; > + > + ret =3D pdump_validate_ring_mp(ring, mp); > + if (ret < 0) > + return ret; > + ret =3D pdump_validate_dir(dir); > + if (ret < 0) > + return ret; > + > + ret =3D pdump_prepare_client_request(device_id, true, queue, dir, > + ENABLE, ring, mp, filter); > + > + return ret; > +} > + > +int > +rte_pdump_disable(uint8_t port, uint16_t queue, uint32_t dir) > +{ > + int ret =3D 0; > + char device[DEVICE_ID_SIZE]; > + > + ret =3D pdump_validate_port(port); > + if (ret < 0) > + return ret; > + ret =3D pdump_validate_dir(dir); > + if (ret < 0) > + return ret; > + > + snprintf(device, sizeof(device), "%u", port); > + ret =3D pdump_prepare_client_request(device, false, queue, dir, > + DISABLE, NULL, NULL, NULL); > + > + return ret; > +} > + > +int > +rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue, > + uint32_t dir) > +{ > + int ret =3D 0; > + > + ret =3D pdump_validate_dir(dir); > + if (ret < 0) > + return ret; > + > + ret =3D pdump_prepare_client_request(device_id, true, queue, dir, > + DISABLE, NULL, NULL, NULL); > + > + return ret; > +} > diff --git a/lib/librte_pdump/rte_pdump.h b/lib/librte_pdump/rte_pdump.h > new file mode 100644 > index 0000000..fcd02de > --- /dev/null > +++ b/lib/librte_pdump/rte_pdump.h > @@ -0,0 +1,186 @@ > +/*- > + * BSD LICENSE > + * > + * Copyright(c) 2016 Intel Corporation. All rights reserved. > + * All rights reserved. > + * > + * Redistribution and use in source and binary forms, with or without > + * modification, are permitted provided that the following conditions > + * are met: > + * > + * * Redistributions of source code must retain the above copyright > + * notice, this list of conditions and the following disclaimer. > + * * Redistributions in binary form must reproduce the above copyrig= ht > + * notice, this list of conditions and the following disclaimer in > + * the documentation and/or other materials provided with the > + * distribution. > + * * Neither the name of Intel Corporation nor the names of its > + * contributors may be used to endorse or promote products derived > + * from this software without specific prior written permission. > + * > + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS > + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT > + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS F= OR > + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGH= T > + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTA= L, > + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT > + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF US= E, > + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A= NY > + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT > + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE U= SE > + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE= . > + */ > + > +#ifndef _RTE_PDUMP_H_ > +#define _RTE_PDUMP_H_ > + > +/** > + * @file > + * RTE pdump > + * > + * packet dump library to provide packet capturing support on dpdk. > + */ > + > +#ifdef __cplusplus > +extern "C" { > +#endif > + > +#define RTE_PDUMP_ALL_QUEUES UINT16_MAX > + > +enum { > + RTE_PDUMP_FLAG_RX =3D 1, /* receive direction */ > + RTE_PDUMP_FLAG_TX =3D 2, /* transmit direction */ > + /* both receive and transmit directions */ > + RTE_PDUMP_FLAG_RXTX =3D (RTE_PDUMP_FLAG_RX|RTE_PDUMP_FLAG_TX) > +}; > + > +/** > + * Initialize packet capturing handling > + * > + * Creates pthread and server socket for handling clients > + * requests to enable/disable rxtx callbacks. > + * > + * @return > + * 0 on success, -1 on error > + */ > +int > +rte_pdump_init(void); > + > +/** > + * Un initialize packet capturing handling > + * > + * Cancels pthread, close server socket, removes server socket address. > + * > + * @return > + * 0 on success, -1 on error > + */ > +int > +rte_pdump_uninit(void); > + > +/** > + * Enables packet capturing on given port and queue. > + * > + * @param port > + * port on which packet capturing should be enabled. > + * @param queue > + * queue of a given port on which packet capturing should be enabled. > + * users should pass on value UINT16_MAX to enable packet capturing on = all > + * queues of a given port. > + * @param dir > + * dir specifies RTE_PDUMP_FLAG_RX/RTE_PDUMP_FLAG_TX/RTE_PDUMP_FLAG_RXT= X > + * on which packet capturing should be enabled for a given port and que= ue. > + * @param ring > + * ring on which captured packets will be enqueued for user. > + * @param mp > + * mempool on to which original packets will be mirrored or duplicated. > + * @param filter > + * place holder for packet filtering. > + * > + * @return > + * 0 on success, -1 on error, rte_errno is set accordingly. > + */ > + > +int > +rte_pdump_enable(uint8_t port, uint16_t queue, uint32_t dir, > + struct rte_ring *ring, > + struct rte_mempool *mp, > + void *filter); > + > +/** > + * Disables packet capturing on given port and queue. > + * > + * @param port > + * port on which packet capturing should be disabled. > + * @param queue > + * queue of a given port on which packet capturing should be disabled. > + * users should pass on value UINT16_MAX to disable packet capturing on= all > + * queues of a given port. > + * @param dir > + * dir specifies RTE_PDUMP_FLAG_RX/RTE_PDUMP_FLAG_TX/RTE_PDUMP_FLAG_RXT= X > + * on which packet capturing should be enabled for a given port and que= ue. > + * > + * @return > + * 0 on success, -1 on error, rte_errno is set accordingly. > + */ > + > +int > +rte_pdump_disable(uint8_t port, uint16_t queue, uint32_t dir); > + > +/** > + * Enables packet capturing on given device id and queue. > + * device_id can be name or pci address of device. > + * > + * @param device_id > + * device id on which packet capturing should be enabled. > + * @param queue > + * queue of a given device id on which packet capturing should be enabl= ed. > + * users should pass on value UINT16_MAX to enable packet capturing on = all > + * queues of a given device id. > + * @param dir > + * dir specifies RTE_PDUMP_FLAG_RX/RTE_PDUMP_FLAG_TX/RTE_PDUMP_FLAG_RXT= X > + * on which packet capturing should be enabled for a given port and que= ue. > + * @param ring > + * ring on which captured packets will be enqueued for user. > + * @param mp > + * mempool on to which original packets will be mirrored or duplicated. > + * @param filter > + * place holder for packet filtering. > + * > + * @return > + * 0 on success, -1 on error, rte_errno is set accordingly. > + */ > + > +int > +rte_pdump_enable_by_deviceid(char *device_id, uint16_t queue, > + uint32_t dir, > + struct rte_ring *ring, > + struct rte_mempool *mp, > + void *filter); > + > +/** > + * Disables packet capturing on given device_id and queue. > + * device_id can be name or pci address of device. > + * > + * @param device_id > + * pci address or name of the device on which packet capturing > + * should be disabled. > + * @param queue > + * queue of a given device on which packet capturing should be disabled= . > + * users should pass on value UINT16_MAX to disable packet capturing on= all > + * queues of a given device id. > + * @param dir > + * dir specifies RTE_PDUMP_FLAG_RX/RTE_PDUMP_FLAG_TX/RTE_PDUMP_FLAG_RXT= X > + * on which packet capturing should be enabled for a given port and que= ue. > + * > + * @return > + * 0 on success, -1 on error, rte_errno is set accordingly. > + */ > +int > +rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue, > + uint32_t dir); > + > +#ifdef __cplusplus > +} > +#endif > + > +#endif /* _RTE_PDUMP_H_ */ > diff --git a/lib/librte_pdump/rte_pdump_version.map b/lib/librte_pdump/rt= e_pdump_version.map > new file mode 100644 > index 0000000..3e744f3 > --- /dev/null > +++ b/lib/librte_pdump/rte_pdump_version.map > @@ -0,0 +1,12 @@ > +DPDK_16.07 { > + global: > + > + rte_pdump_disable; > + rte_pdump_disable_by_deviceid; > + rte_pdump_enable; > + rte_pdump_enable_by_deviceid; > + rte_pdump_init; > + rte_pdump_uninit; > + > + local: *; > +}; > diff --git a/mk/rte.app.mk b/mk/rte.app.mk > index b84b56d..f792f2a 100644 > --- a/mk/rte.app.mk > +++ b/mk/rte.app.mk > @@ -61,6 +61,7 @@ _LDLIBS-y +=3D --whole-archive >=20 > _LDLIBS-$(CONFIG_RTE_LIBRTE_DISTRIBUTOR) +=3D -lrte_distributor > _LDLIBS-$(CONFIG_RTE_LIBRTE_REORDER) +=3D -lrte_reorder > +_LDLIBS-$(CONFIG_RTE_LIBRTE_PDUMP) +=3D -lrte_pdump >=20 > ifeq ($(CONFIG_RTE_EXEC_ENV_LINUXAPP),y) > _LDLIBS-$(CONFIG_RTE_LIBRTE_KNI) +=3D -lrte_kni > -- > 2.5.0