From: Fan Zhang <roy.fan.zhang@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH v3 3/4] lib/librte_port: add packet dumping to PCAP file support in sink port
Date: Wed, 9 Mar 2016 16:07:31 +0000 [thread overview]
Message-ID: <1457539652-17337-4-git-send-email-roy.fan.zhang@intel.com> (raw)
In-Reply-To: <1457539652-17337-1-git-send-email-roy.fan.zhang@intel.com>
Originally, sink ports in librte_port releases received mbufs back to
mempool. This patch adds optional packet dumping to PCAP feature in sink
port: the packets will be dumped to user defined PCAP file for storage or
debugging. The user may also choose the sink port's activity: either it
continuously dump the packets to the file, or stops at certain dumping
This feature shares same CONFIG_RTE_PORT_PCAP compiler option as source
port PCAP file support feature. Users can enable or disable this feature
by setting CONFIG_RTE_PORT_PCAP compiler option "y" or "n".
Signed-off-by: Fan Zhang <roy.fan.zhang@intel.com>
Acked-by: Cristian Dumitrescu <cristian.dumitrescu@intel.com>
---
lib/librte_port/rte_port_source_sink.c | 248 ++++++++++++++++++++++++++++++++-
lib/librte_port/rte_port_source_sink.h | 11 +-
2 files changed, 256 insertions(+), 3 deletions(-)
diff --git a/lib/librte_port/rte_port_source_sink.c b/lib/librte_port/rte_port_source_sink.c
index 3d4e8d9..6a7ba64 100644
--- a/lib/librte_port/rte_port_source_sink.c
+++ b/lib/librte_port/rte_port_source_sink.c
@@ -40,6 +40,7 @@
#ifdef RTE_NEXT_ABI
#include <rte_memcpy.h>
+#include <rte_ether.h>
#ifdef RTE_PORT_PCAP
#include <pcap.h>
@@ -400,12 +401,183 @@ rte_port_source_stats_read(void *port,
struct rte_port_sink {
struct rte_port_out_stats stats;
+
+ /* PCAP dumper handle and pkts number */
+ void *dumper;
+ uint32_t max_pkts;
+ uint32_t pkt_index;
+ uint32_t dump_finish;
};
+#ifdef RTE_PORT_PCAP
+
+/**
+ * Open PCAP file for dumping packets to the file later
+ *
+ * @param port
+ * Handle to sink port
+ * @param p
+ * Sink port parameter
+ * @return
+ * 0 on SUCCESS
+ * error code otherwise
+ */
+static int
+pcap_sink_open(struct rte_port_sink *port,
+ __rte_unused struct rte_port_sink_params *p)
+{
+ pcap_t *tx_pcap;
+ pcap_dumper_t *pcap_dumper;
+
+ if (p->file_name == NULL) {
+ port->dumper = NULL;
+ port->max_pkts = 0;
+ port->pkt_index = 0;
+ port->dump_finish = 0;
+ return 0;
+ }
+
+ /** Open a dead pcap handler for opening dumper file */
+ tx_pcap = pcap_open_dead(DLT_EN10MB, 65535);
+ if (tx_pcap == NULL)
+ return -ENOENT;
+
+ /* The dumper is created using the previous pcap_t reference */
+ pcap_dumper = pcap_dump_open(tx_pcap, p->file_name);
+ if (pcap_dumper == NULL)
+ return -ENOENT;
+
+ port->dumper = pcap_dumper;
+ port->max_pkts = p->max_n_pkts;
+ port->pkt_index = 0;
+ port->dump_finish = 0;
+
+ return 0;
+}
+
+uint8_t jumbo_pkt_buf[ETHER_MAX_JUMBO_FRAME_LEN];
+
+/**
+ * Dump a packet to PCAP dumper
+ *
+ * @param p
+ * Handle to sink port
+ * @param mbuf
+ * Handle to mbuf structure holding the packet
+ */
+static void
+pcap_sink_dump_pkt(struct rte_port_sink *port, struct rte_mbuf *mbuf)
+{
+ uint8_t *pcap_dumper = (uint8_t *)(port->dumper);
+ struct pcap_pkthdr pcap_hdr;
+ uint8_t *pkt;
+
+ /* Maximum num packets already reached */
+ if (port->dump_finish)
+ return;
+
+ pkt = rte_pktmbuf_mtod(mbuf, uint8_t *);
+
+ pcap_hdr.len = mbuf->pkt_len;
+ pcap_hdr.caplen = pcap_hdr.len;
+ gettimeofday(&(pcap_hdr.ts), NULL);
+
+ if (mbuf->nb_segs > 1) {
+ struct rte_mbuf *jumbo_mbuf;
+ uint32_t pkt_index = 0;
+
+ /* if packet size longer than ETHER_MAX_JUMBO_FRAME_LEN,
+ * ignore it.
+ */
+ if (mbuf->pkt_len > ETHER_MAX_JUMBO_FRAME_LEN)
+ return;
+
+ for (jumbo_mbuf = mbuf; jumbo_mbuf != NULL;
+ jumbo_mbuf = jumbo_mbuf->next) {
+ rte_memcpy(&jumbo_pkt_buf[pkt_index],
+ rte_pktmbuf_mtod(jumbo_mbuf, uint8_t *),
+ jumbo_mbuf->data_len);
+ pkt_index += jumbo_mbuf->data_len;
+ }
+
+ jumbo_pkt_buf[pkt_index] = '\0';
+
+ pkt = jumbo_pkt_buf;
+ }
+
+ pcap_dump(pcap_dumper, &pcap_hdr, pkt);
+
+ port->pkt_index++;
+
+ if ((port->max_pkts != 0) && (port->pkt_index >= port->max_pkts)) {
+ port->dump_finish = 1;
+ RTE_LOG(INFO, PORT, "Dumped %u packets to file\n",
+ port->pkt_index);
+ }
+
+}
+
+/**
+ * Flush pcap dumper
+ *
+ * @param dumper
+ * Handle to pcap dumper
+ */
+
+static void
+pcap_sink_flush_pkt(void *dumper)
+{
+ pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
+
+ pcap_dump_flush(pcap_dumper);
+}
+
+/**
+ * Close a PCAP dumper handle
+ *
+ * @param dumper
+ * Handle to pcap dumper
+ */
+static void
+pcap_sink_close(void *dumper)
+{
+ pcap_dumper_t *pcap_dumper = (pcap_dumper_t *)dumper;
+
+ pcap_dump_close(pcap_dumper);
+}
+
+#else
+
+static int
+pcap_sink_open(struct rte_port_sink *port,
+ __rte_unused struct rte_port_sink_params *p)
+{
+ port->dumper = NULL;
+ port->max_pkts = 0;
+ port->pkt_index = 0;
+ port->dump_finish = 0;
+
+ return -ENOTSUP;
+}
+
+static void
+pcap_sink_dump_pkt(__rte_unused struct rte_port_sink *port,
+ __rte_unused struct rte_mbuf *mbuf) {}
+
+static void
+pcap_sink_flush_pkt(__rte_unused void *dumper) {}
+
+static void
+pcap_sink_close(__rte_unused void *dumper) {}
+
+#endif
+
static void *
rte_port_sink_create(__rte_unused void *params, int socket_id)
{
struct rte_port_sink *port;
+ struct rte_port_sink_params *p = params;
+ int status;
/* Memory allocation */
port = rte_zmalloc_socket("PORT", sizeof(*port),
@@ -415,6 +587,26 @@ rte_port_sink_create(__rte_unused void *params, int socket_id)
return NULL;
}
+ /* Try to open PCAP file for dumping, if possible */
+ status = pcap_sink_open(port, p);
+ if (status == 0) {
+ if (port->dumper != NULL)
+ RTE_LOG(INFO, PORT, "Ready to dump packets "
+ "to file %s\n", p->file_name);
+
+ } else if (status != -ENOTSUP) {
+ if (status == -ENOENT)
+ RTE_LOG(ERR, PORT, "%s: Failed to open pcap file "
+ "%s for writing\n", __func__,
+ p->file_name);
+ else
+ RTE_LOG(ERR, PORT, "%s: Failed to enable pcap "
+ "support for unknown reason\n", __func__);
+
+ rte_free(port);
+ port = NULL;
+ }
+
return port;
}
@@ -424,6 +616,8 @@ rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
__rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
+ if (p->dumper != NULL)
+ pcap_sink_dump_pkt(p, pkt);
rte_pktmbuf_free(pkt);
RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
@@ -442,12 +636,34 @@ rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
+
+ if (p->dumper) {
+ for (i = 0; i < n_pkts; i++) {
+ struct rte_mbuf *pkt = pkts[i];
+
+ pcap_sink_dump_pkt(p, pkt);
+ }
+ }
+
for (i = 0; i < n_pkts; i++) {
struct rte_mbuf *pkt = pkts[i];
rte_pktmbuf_free(pkt);
}
+
} else {
+ if (p->dumper) {
+ uint64_t dump_pkts_mask = pkts_mask;
+ uint32_t pkt_index;
+
+ for ( ; dump_pkts_mask; ) {
+ pkt_index = __builtin_ctzll(
+ dump_pkts_mask);
+ pcap_sink_dump_pkt(p, pkts[pkt_index]);
+ dump_pkts_mask &= ~(1LLU << pkt_index);
+ }
+ }
+
for ( ; pkts_mask; ) {
uint32_t pkt_index = __builtin_ctzll(pkts_mask);
uint64_t pkt_mask = 1LLU << pkt_index;
@@ -464,6 +680,34 @@ rte_port_sink_tx_bulk(void *port, struct rte_mbuf **pkts,
}
static int
+rte_port_sink_flush(void *port)
+{
+ struct rte_port_sink *p = (struct rte_port_sink *)port;
+
+ if (p->dumper != NULL)
+ pcap_sink_flush_pkt(p->dumper);
+
+ return 0;
+}
+
+static int
+rte_port_sink_free(void *port)
+{
+ struct rte_port_sink *p =
+ (struct rte_port_sink *)port;
+ /* Check input parameters */
+ if (p == NULL)
+ return 0;
+
+ if (p->dumper != NULL)
+ pcap_sink_close(p->dumper);
+
+ rte_free(p);
+
+ return 0;
+}
+
+static int
rte_port_sink_stats_read(void *port, struct rte_port_out_stats *stats,
int clear)
{
@@ -491,9 +735,9 @@ struct rte_port_in_ops rte_port_source_ops = {
struct rte_port_out_ops rte_port_sink_ops = {
.f_create = rte_port_sink_create,
- .f_free = NULL,
+ .f_free = rte_port_sink_free,
.f_tx = rte_port_sink_tx,
.f_tx_bulk = rte_port_sink_tx_bulk,
- .f_flush = NULL,
+ .f_flush = rte_port_sink_flush,
.f_stats = rte_port_sink_stats_read,
};
diff --git a/lib/librte_port/rte_port_source_sink.h b/lib/librte_port/rte_port_source_sink.h
index 522d10d..917abe4 100644
--- a/lib/librte_port/rte_port_source_sink.h
+++ b/lib/librte_port/rte_port_source_sink.h
@@ -69,7 +69,16 @@ struct rte_port_source_params {
/** source port operations */
extern struct rte_port_in_ops rte_port_source_ops;
-/** sink port parameters: NONE */
+/** sink port parameters */
+struct rte_port_sink_params {
+ /** The full path of the pcap file to write the packets to */
+ char *file_name;
+ /** The maximum number of packets write to the pcap file.
+ * If this value is 0, the "infinite" write will be carried
+ * out.
+ */
+ uint32_t max_n_pkts;
+};
/** sink port operations */
extern struct rte_port_out_ops rte_port_sink_ops;
--
2.5.0
next prev parent reply other threads:[~2016-03-09 16:08 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-17 11:11 [dpdk-dev] [PATCH v2 0/4] Add PCAP support to source and " Fan Zhang
2016-02-17 11:11 ` [dpdk-dev] [PATCH v2 1/4] lib/librte_port: add PCAP file support to source port Fan Zhang
2016-03-07 11:17 ` Thomas Monjalon
2016-03-08 8:36 ` Dumitrescu, Cristian
2016-03-08 9:06 ` Panu Matilainen
2016-03-08 10:14 ` Thomas Monjalon
2016-02-17 11:11 ` [dpdk-dev] [PATCH v2 2/4] example/ip_pipeline: add PCAP file support Fan Zhang
2016-02-17 11:11 ` [dpdk-dev] [PATCH v2 3/4] lib/librte_port: add packet dumping to PCAP file support in sink port Fan Zhang
2016-02-17 11:11 ` [dpdk-dev] [PATCH v2 4/4] examples/ip_pipeline: add packets dumping to PCAP file support Fan Zhang
2016-03-09 16:07 ` [dpdk-dev] [PATCH v3 0/4] Add PCAP support to source and sink port Fan Zhang
2016-03-09 16:07 ` [dpdk-dev] [PATCH v3 1/4] lib/librte_port: add PCAP file support to source port Fan Zhang
2016-03-09 16:07 ` [dpdk-dev] [PATCH v3 2/4] example/ip_pipeline: add PCAP file support Fan Zhang
2016-03-11 11:10 ` Thomas Monjalon
2016-03-25 12:00 ` Zhang, Roy Fan
2016-03-09 16:07 ` Fan Zhang [this message]
2016-03-09 16:07 ` [dpdk-dev] [PATCH v3 4/4] examples/ip_pipeline: add packets dumping to " Fan Zhang
2016-03-11 17:08 ` [dpdk-dev] [PATCH v4 0/4] Add PCAP support to source and sink port Fan Zhang
2016-03-11 17:08 ` [dpdk-dev] [PATCH v4 1/4] lib/librte_port: add PCAP file support to source port Fan Zhang
2016-03-11 17:08 ` [dpdk-dev] [PATCH v4 2/4] example/ip_pipeline: add PCAP file support Fan Zhang
2016-03-11 17:08 ` [dpdk-dev] [PATCH v4 3/4] lib/librte_port: add packet dumping to PCAP file support in sink port Fan Zhang
2016-03-11 17:08 ` [dpdk-dev] [PATCH v4 4/4] examples/ip_pipeline: add packets dumping to PCAP file support Fan Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1457539652-17337-4-git-send-email-roy.fan.zhang@intel.com \
--to=roy.fan.zhang@intel.com \
--cc=dev@dpdk.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).