From ebb5ad89104633abe176868167cc0009c2afe264 Mon Sep 17 00:00:00 2001 From: rainbow_0206 Date: Wed, 10 Oct 2018 11:42:26 +0800 Subject: Feat: support to log the mbuf to file Change-Id: Id0ce0dd0764db78a2e12bdaee478405b9aba42e7 Signed-off-by: rainbow_0206 --- stacks/lwip_stack/src/io_adpt/dpdk.c | 100 +++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/stacks/lwip_stack/src/io_adpt/dpdk.c b/stacks/lwip_stack/src/io_adpt/dpdk.c index da2cfb6..d199513 100644 --- a/stacks/lwip_stack/src/io_adpt/dpdk.c +++ b/stacks/lwip_stack/src/io_adpt/dpdk.c @@ -190,6 +190,82 @@ NSTACK_STATIC struct rte_eth_conf port_conf_default_bond = { }, }; +/***************************************************************************** +* Prototype : dpdk_mbuf_to_file +* Description : write the packet data into a file +* Input : uint16_t pkt_number +* struct rte_mbuf **pkts +* Output : None +* Return Value : +* Calls : +* Called By : +* +*****************************************************************************/ +NSTACK_STATIC void +dpdk_mbuf_to_file (uint16_t pkt_number, struct rte_mbuf **pkts) +{ + char line[100] = { 0 }; + FILE *f = NULL; + struct rte_mbuf *p = NULL; + uint16_t len = 0, offset, i; + uint16_t pktlen = 0; + uint16_t start = 0; + uint16_t number = 0; + unsigned char *data = NULL; + + f = fopen ("/var/log/nStack/packet.txt", "a+"); + if (f == NULL) + { + NSHAL_LOGERR ("can not open the file:%s", "packet.txt"); + return; + } + + for (i = 0; i < pkt_number; i++) + { + pktlen = 0; + p = pkts[i]; + while (p) + { + len = 0; + data = rte_pktmbuf_mtod (p, unsigned char *); + while (len < p->data_len) + { + start = pktlen % 16; /* start of the line */ + if (start == 0) + { + number = SNPRINTF_S (line, sizeof (line), sizeof (line) - 1, + "%08X", len); + } + + for (offset = 0; + ((offset + start) < 16) && ((len + offset) < p->data_len); + offset++) + { + number += + SNPRINTF_S (line + number, sizeof (line), + sizeof (line) - 1, " %02X", + data[len + offset]); + } + + fprintf (f, "%s", line); + if ((offset + start) == 16) + fprintf (f, "\n"); + + len += offset; + pktlen += offset; + (void) MEMSET_S (line, sizeof (line), 0, sizeof (line)); + } + + p = p->next; + + } + fprintf (f, "\n"); + } + + fclose (f); + return; +} + /***************************************************************************** * Prototype : hal_rte_eth_rx_burst * Description : a copy of rte_eth_rx_burst, because this function invokes @@ -207,13 +283,13 @@ NSTACK_STATIC struct rte_eth_conf port_conf_default_bond = { *****************************************************************************/ NSTACK_STATIC inline uint16_t hal_rte_eth_rx_burst (uint8_t port_id, uint16_t queue_id, - struct rte_mbuf **rx_pkts, const uint16_t nb_pkts) + struct rte_mbuf ** rx_pkts, const uint16_t nb_pkts) { #ifdef RTE_ETHDEV_RXTX_CALLBACKS struct rte_eth_rxtx_callback *cb; #endif int16_t nb_rx; - + char *pst_capture_packet = NULL; struct rte_eth_dev *dev = &rte_eth_devices[port_id]; if (NULL == dev->rx_pkt_burst) @@ -250,6 +326,11 @@ hal_rte_eth_rx_burst (uint8_t port_id, uint16_t queue_id, } #endif + //pst_capture_packet = getenv ("NSTACK_CAPTURE_PACKET"); + if (pst_capture_packet && strcmp (pst_capture_packet, "1") == 0) + { + dpdk_mbuf_to_file (nb_rx, rx_pkts); + } return (uint16_t) nb_rx; } @@ -274,7 +355,8 @@ hal_rte_eth_tx_burst (uint8_t port_id, uint16_t queue_id, #ifdef RTE_ETHDEV_RXTX_CALLBACKS struct rte_eth_rxtx_callback *cb; #endif - + int16_t nb_tx = 0; + char *pst_capture_packet = NULL; struct rte_eth_dev *dev = &rte_eth_devices[port_id]; if (NULL == dev->tx_pkt_burst) @@ -309,8 +391,16 @@ hal_rte_eth_tx_burst (uint8_t port_id, uint16_t queue_id, } #endif - return (*dev->tx_pkt_burst) (dev->data->tx_queues[queue_id], tx_pkts, - nb_pkts); + nb_tx = (*dev->tx_pkt_burst) (dev->data->tx_queues[queue_id], tx_pkts, + nb_pkts); + + //pst_capture_packet = getenv ("NSTACK_CAPTURE_PACKET"); + if (pst_capture_packet && strcmp (pst_capture_packet, "1") == 0) + { + dpdk_mbuf_to_file (nb_tx, tx_pkts); + } + + return nb_tx; } /***************************************************************************** -- cgit 1.2.3-korg