[RFC,6/8] pdump: add packet header truncation
diff mbox series

Message ID 20191007165232.14535-7-stephen@networkplumber.org
State RFC
Headers show
Series
  • Packet Capture enhancements
Related show

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Stephen Hemminger Oct. 7, 2019, 4:52 p.m. UTC
Add support for pcap style header truncation (called snap length).
This optimizes packet capture by not copying the whole packet.

Since API is already versioned (in previous patch),
it is ok to add one new argument for the length.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/pdump/main.c             | 16 ++++++-------
 app/test/test_pdump.c        |  6 +++--
 lib/librte_pdump/rte_pdump.c | 46 +++++++++++++++++++++++-------------
 lib/librte_pdump/rte_pdump.h | 29 ++++++++++++++---------
 4 files changed, 60 insertions(+), 37 deletions(-)

Patch
diff mbox series

diff --git a/app/pdump/main.c b/app/pdump/main.c
index c3eb554ef28b..e55a94c0f10e 100644
--- a/app/pdump/main.c
+++ b/app/pdump/main.c
@@ -825,45 +825,45 @@  enable_pdump(void)
 			if (pt->dump_by_type == DEVICE_ID) {
 				ret = rte_pdump_enable_by_deviceid(
 						pt->device_id,
-						pt->queue,
+						pt->queue, UINT16_MAX,
 						RTE_PDUMP_FLAG_RX,
 						pt->rx_ring,
 						pt->mp, NULL, 0);
 				ret1 = rte_pdump_enable_by_deviceid(
 						pt->device_id,
-						pt->queue,
+						pt->queue, UINT16_MAX,
 						RTE_PDUMP_FLAG_TX,
 						pt->tx_ring,
 						pt->mp, NULL, 0);
 			} else if (pt->dump_by_type == PORT_ID) {
 				ret = rte_pdump_enable(pt->port, pt->queue,
-						RTE_PDUMP_FLAG_RX,
+						UINT16_MAX, RTE_PDUMP_FLAG_RX,
 						pt->rx_ring, pt->mp, NULL, 0);
 				ret1 = rte_pdump_enable(pt->port, pt->queue,
-						RTE_PDUMP_FLAG_TX,
+						UINT16_MAX, RTE_PDUMP_FLAG_TX,
 						pt->tx_ring, pt->mp, NULL, 0);
 			}
 		} else if (pt->dir == RTE_PDUMP_FLAG_RX) {
 			if (pt->dump_by_type == DEVICE_ID)
 				ret = rte_pdump_enable_by_deviceid(
 						pt->device_id,
-						pt->queue,
+						pt->queue, UINT16_MAX,
 						pt->dir, pt->rx_ring,
 						pt->mp, NULL, 0);
 			else if (pt->dump_by_type == PORT_ID)
 				ret = rte_pdump_enable(pt->port, pt->queue,
-						pt->dir,
+						UINT16_MAX, pt->dir,
 						pt->rx_ring, pt->mp, NULL, 0);
 		} else if (pt->dir == RTE_PDUMP_FLAG_TX) {
 			if (pt->dump_by_type == DEVICE_ID)
 				ret = rte_pdump_enable_by_deviceid(
 						pt->device_id,
-						pt->queue,
+						pt->queue, UINT16_MAX,
 						pt->dir,
 						pt->tx_ring, pt->mp, NULL, 0);
 			else if (pt->dump_by_type == PORT_ID)
 				ret = rte_pdump_enable(pt->port, pt->queue,
-						pt->dir,
+						UINT16_MAX, pt->dir,
 						pt->tx_ring, pt->mp, NULL, 0);
 		}
 		if (ret < 0 || ret1 < 0) {
diff --git a/app/test/test_pdump.c b/app/test/test_pdump.c
index f0187a4cd279..c2468b968247 100644
--- a/app/test/test_pdump.c
+++ b/app/test/test_pdump.c
@@ -78,7 +78,8 @@  run_pdump_client_tests(void)
 	printf("\n***** flags = RTE_PDUMP_FLAG_TX *****\n");
 
 	for (itr = 0; itr < NUM_ITR; itr++) {
-		ret = rte_pdump_enable(portid, QUEUE_ID, flags, ring_client,
+		ret = rte_pdump_enable(portid, QUEUE_ID, flags,
+				       UINT16_MAX, ring_client,
 				       mp, NULL, 0);
 		if (ret < 0) {
 			printf("rte_pdump_enable failed\n");
@@ -93,7 +94,8 @@  run_pdump_client_tests(void)
 		}
 		printf("pdump_disable success\n");
 
-		ret = rte_pdump_enable_by_deviceid(deviceid, QUEUE_ID, flags,
+		ret = rte_pdump_enable_by_deviceid(deviceid, QUEUE_ID,
+						   flags, UINT16_MAX,
 						   ring_client, mp, NULL, 0);
 		if (ret < 0) {
 			printf("rte_pdump_enable_by_deviceid failed\n");
diff --git a/lib/librte_pdump/rte_pdump.c b/lib/librte_pdump/rte_pdump.c
index 1206671c6f60..c3636bcdd029 100644
--- a/lib/librte_pdump/rte_pdump.c
+++ b/lib/librte_pdump/rte_pdump.c
@@ -44,6 +44,7 @@  struct pdump_request {
 		struct enable_v1 {
 			char device[DEVICE_ID_SIZE];
 			uint16_t queue;
+			uint16_t snaplen;
 			struct rte_ring *ring;
 			struct rte_mempool *mp;
 			const void *filter;
@@ -51,6 +52,7 @@  struct pdump_request {
 		struct disable_v1 {
 			char device[DEVICE_ID_SIZE];
 			uint16_t queue;
+			uint16_t snaplen;
 			struct rte_ring *ring;
 			struct rte_mempool *mp;
 			const void *filter;
@@ -69,6 +71,7 @@  static struct pdump_rxtx_cbs {
 	struct rte_mempool *mp;
 	const struct rte_eth_rxtx_callback *cb;
 	const void *filter;
+	uint16_t snaplen;
 } rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT],
 tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
 
@@ -93,7 +96,7 @@  pdump_copy(uint16_t port, struct rte_mbuf **pkts,
 		if (rte_pcap_filter(cbs->filter, pkts[i]) == 0)
 			continue;
 
-		p = rte_pktmbuf_copy(pkts[i], mp, 0, UINT32_MAX);
+		p = rte_pktmbuf_copy(pkts[i], mp, 0, cbs->snaplen);
 		if (p) {
 			p->port = port;
 			p->timestamp = now;
@@ -132,7 +135,8 @@  pdump_tx(uint16_t port, uint16_t qidx __rte_unused,
 static int
 pdump_register_rx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
 			    struct rte_ring *ring, struct rte_mempool *mp,
-			    uint16_t operation, const void *filter)
+			    uint16_t operation, uint16_t snaplen,
+			    const void *filter)
 {
 	uint16_t qid;
 	struct pdump_rxtx_cbs *cbs = NULL;
@@ -151,6 +155,7 @@  pdump_register_rx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
 			cbs->ring = ring;
 			cbs->mp = mp;
 			cbs->filter = filter;
+			cbs->snaplen = snaplen;
 			cbs->cb = rte_eth_add_first_rx_callback(port, qid,
 								pdump_rx, cbs);
 			if (cbs->cb == NULL) {
@@ -187,7 +192,8 @@  pdump_register_rx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
 static int
 pdump_register_tx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
 			    struct rte_ring *ring, struct rte_mempool *mp,
-			    uint16_t operation, const void *filter)
+			    uint16_t operation, uint16_t snaplen,
+			    const void *filter)
 {
 
 	uint16_t qid;
@@ -207,6 +213,7 @@  pdump_register_tx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
 			cbs->ring = ring;
 			cbs->mp = mp;
 			cbs->filter = filter;
+			cbs->snaplen = snaplen;
 			cbs->cb = rte_eth_add_tx_callback(port, qid, pdump_tx,
 								cbs);
 			if (cbs->cb == NULL) {
@@ -244,7 +251,7 @@  static int
 set_pdump_rxtx_cbs(const struct pdump_request *p)
 {
 	uint16_t nb_rx_q = 0, nb_tx_q = 0, end_q, queue;
-	uint16_t port;
+	uint16_t port, snaplen = 0;
 	int ret = 0;
 	uint32_t flags;
 	uint16_t operation;
@@ -266,6 +273,7 @@  set_pdump_rxtx_cbs(const struct pdump_request *p)
 		queue = p->data.en_v1.queue;
 		ring = p->data.en_v1.ring;
 		mp = p->data.en_v1.mp;
+		snaplen = p->data.en_v1.snaplen;
 		filter = p->data.en_v1.filter;
 	} else {
 		ret = rte_eth_dev_get_port_by_name(p->data.dis_v1.device,
@@ -310,7 +318,7 @@  set_pdump_rxtx_cbs(const struct pdump_request *p)
 	if (flags & RTE_PDUMP_FLAG_RX) {
 		end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1;
 		ret = pdump_register_rx_callbacks(end_q, port, queue, ring, mp,
-						  operation, filter);
+						  operation, snaplen, filter);
 		if (ret < 0)
 			return ret;
 	}
@@ -319,7 +327,7 @@  set_pdump_rxtx_cbs(const struct pdump_request *p)
 	if (flags & RTE_PDUMP_FLAG_TX) {
 		end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1;
 		ret = pdump_register_tx_callbacks(end_q, port, queue, ring, mp,
-						  operation, filter);
+						  operation, snaplen, filter);
 		if (ret < 0)
 			return ret;
 	}
@@ -465,7 +473,7 @@  pdump_validate_filter(const void *filter, unsigned int len)
 
 static int
 pdump_prepare_client_request(const char *device, uint16_t queue,
-			     uint32_t flags,
+			     uint32_t flags, uint16_t snaplen,
 			     uint16_t operation,
 			     struct rte_ring *ring,
 			     struct rte_mempool *mp,
@@ -485,6 +493,7 @@  pdump_prepare_client_request(const char *device, uint16_t queue,
 		strlcpy(req->data.en_v1.device, device,
 			sizeof(req->data.en_v1.device));
 		req->data.en_v1.queue = queue;
+		req->data.en_v1.snaplen = snaplen;
 		req->data.en_v1.ring = ring;
 		req->data.en_v1.mp = mp;
 		req->data.en_v1.filter = filter;
@@ -492,6 +501,7 @@  pdump_prepare_client_request(const char *device, uint16_t queue,
 		strlcpy(req->data.dis_v1.device, device,
 			sizeof(req->data.dis_v1.device));
 		req->data.dis_v1.queue = queue;
+		req->data.en_v1.snaplen = snaplen;
 		req->data.dis_v1.ring = NULL;
 		req->data.dis_v1.mp = NULL;
 		req->data.dis_v1.filter = NULL;
@@ -517,6 +527,7 @@  pdump_prepare_client_request(const char *device, uint16_t queue,
 
 int
 rte_pdump_enable_v1911(uint16_t port, uint16_t queue, uint32_t flags,
+		       uint16_t snap_len,
 		       struct rte_ring *ring, struct rte_mempool *mp,
 		       const void *filter, uint32_t filter_len)
 {
@@ -537,14 +548,15 @@  rte_pdump_enable_v1911(uint16_t port, uint16_t queue, uint32_t flags,
 	if (ret < 0)
 		return ret;
 
-	ret = pdump_prepare_client_request(name, queue, flags,
+	ret = pdump_prepare_client_request(name, queue, flags, snap_len,
 					   ENABLE, ring, mp, filter);
 
 	return ret;
 }
 BIND_DEFAULT_SYMBOL(rte_pdump_enable, _v1911, 19.11);
 MAP_STATIC_SYMBOL(int rte_pdump_enable(uint16_t port, uint16_t queue,
-				       uint32_t flags, struct rte_ring *ring,
+				       uint32_t flags, uint16_t snap_len,
+				       struct rte_ring *ring,
 				       struct rte_mempool *mp,
 				       const void *filter, uint32_t len),
 		  rte_pdump_enable_v1911);
@@ -558,14 +570,14 @@  rte_pdump_enable_v1607(uint16_t port, uint16_t queue, uint32_t flags,
 	if (filter != NULL)
 		PDUMP_LOG(WARNING, "filter not supported in this version\n");
 
-	return rte_pdump_enable_v1911(port, queue, flags, ring, mp,
-				      NULL, 0);
+	return rte_pdump_enable_v1911(port, queue, flags, UINT16_MAX,
+				      ring, mp, NULL, 0);
 }
 VERSION_SYMBOL(rte_pdump_enable, _v1607, 16.07);
 
 int
 rte_pdump_enable_by_deviceid_v1911(const char *device_id, uint16_t queue,
-				   uint32_t flags,
+				   uint32_t flags, uint16_t snap_len,
 				   struct rte_ring *ring,
 				   struct rte_mempool *mp,
 				   const void *filter, uint32_t filter_len)
@@ -582,7 +594,7 @@  rte_pdump_enable_by_deviceid_v1911(const char *device_id, uint16_t queue,
 	if (ret < 0)
 		return ret;
 
-	ret = pdump_prepare_client_request(device_id, queue, flags,
+	ret = pdump_prepare_client_request(device_id, queue, flags, snap_len,
 					   ENABLE, ring, mp, filter);
 
 	return ret;
@@ -591,6 +603,7 @@  BIND_DEFAULT_SYMBOL(rte_pdump_enable_by_deviceid, _v1911, 19.11);
 MAP_STATIC_SYMBOL(int rte_pdump_enable_by_deviceid(const char *device_id,
 						   uint16_t queue,
 						   uint32_t flags,
+						   uint16_t snap_len,
 						   struct rte_ring *ring,
 						   struct rte_mempool *mp,
 						   const void *filter,
@@ -607,7 +620,8 @@  rte_pdump_enable_by_deviceid_v1607(char *device_id, uint16_t queue,
 	if (filter != NULL)
 		PDUMP_LOG(WARNING, "filter not supported in this version\n");
 
-	return rte_pdump_enable_by_deviceid_v1911(device_id, queue, flags,
+	return rte_pdump_enable_by_deviceid_v1911(device_id, queue,
+						  flags, UINT16_MAX,
 						  ring, mp, NULL, 0);
 }
 VERSION_SYMBOL(rte_pdump_enable_by_deviceid, _v1607, 16.07);
@@ -625,7 +639,7 @@  rte_pdump_disable(uint16_t port, uint16_t queue, uint32_t flags)
 	if (ret < 0)
 		return ret;
 
-	ret = pdump_prepare_client_request(name, queue, flags,
+	ret = pdump_prepare_client_request(name, queue, flags, 0,
 						DISABLE, NULL, NULL, NULL);
 
 	return ret;
@@ -641,7 +655,7 @@  rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue,
 	if (ret < 0)
 		return ret;
 
-	ret = pdump_prepare_client_request(device_id, queue, flags,
+	ret = pdump_prepare_client_request(device_id, queue, flags, 0,
 						DISABLE, NULL, NULL, NULL);
 
 	return ret;
diff --git a/lib/librte_pdump/rte_pdump.h b/lib/librte_pdump/rte_pdump.h
index 12cb46f8b0e9..e04ef4c1933b 100644
--- a/lib/librte_pdump/rte_pdump.h
+++ b/lib/librte_pdump/rte_pdump.h
@@ -63,13 +63,15 @@  rte_pdump_uninit(void);
  * @param flags
  *  flags specifies RTE_PDUMP_FLAG_RX/RTE_PDUMP_FLAG_TX/RTE_PDUMP_FLAG_RXTX
  *  on which packet capturing should be enabled for a given port and queue.
+ * @param snap_len
+ *  only the first snap_len bytes of packet will be captured.
  * @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.
+ *  mempool on to which original packets will be duplicated.
  * @param filter
  *  filter to apply to incoming packet (classic BPF)
- * @param len
+ * @param filter_len
  *  length of filter (in BPF instructions)
  *
  * @return
@@ -77,15 +79,17 @@  rte_pdump_uninit(void);
  */
 int
 rte_pdump_enable(uint16_t port, uint16_t queue, uint32_t flags,
-		 struct rte_ring *ring, struct rte_mempool *mp,
-		 const void *filter, uint32_t len);
+		 uint16_t snap_len, struct rte_ring *ring,
+		 struct rte_mempool *mp,
+		 const void *filter, uint32_t filter_len);
 int
 rte_pdump_enable_v1607(uint16_t port, uint16_t queue, uint32_t flags,
 		       struct rte_ring *ring, struct rte_mempool *mp,
 		       void *filter);
 int
 rte_pdump_enable_v1911(uint16_t port, uint16_t queue, uint32_t flags,
-		       struct rte_ring *ring, struct rte_mempool *mp,
+		       uint16_t snap_len, struct rte_ring *ring,
+		       struct rte_mempool *mp,
 		       const void *filter, uint32_t len);
 
 /**
@@ -121,13 +125,15 @@  rte_pdump_disable(uint16_t port, uint16_t queue, uint32_t flags);
  * @param flags
  *  flags specifies RTE_PDUMP_FLAG_RX/RTE_PDUMP_FLAG_TX/RTE_PDUMP_FLAG_RXTX
  *  on which packet capturing should be enabled for a given port and queue.
+ * @param snap_len
+ *  only the first snap_len bytes of packet will be captured.
  * @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.
+ *  mempool on to which original packets will be duplicated.
  * @param filter
  *  filter to apply to incoming packet (classic BPF)
- * @param len
+ * @param filter_len
  *  length of filter (in BPF instructions)
  *
  * @return
@@ -135,10 +141,10 @@  rte_pdump_disable(uint16_t port, uint16_t queue, uint32_t flags);
  */
 int
 rte_pdump_enable_by_deviceid(const char *device_id, uint16_t queue,
-			     uint32_t flags,
+			     uint32_t flags, uint16_t snap_len,
 			     struct rte_ring *ring,
 			     struct rte_mempool *mp,
-			     const void *filter, uint32_t len);
+			     const void *filter, uint32_t filter_len);
 int
 rte_pdump_enable_by_deviceid_v1607(char *device_id, uint16_t queue,
 				   uint32_t flags, struct rte_ring *ring,
@@ -146,9 +152,10 @@  rte_pdump_enable_by_deviceid_v1607(char *device_id, uint16_t queue,
 				   void *filter);
 int
 rte_pdump_enable_by_deviceid_v1911(const char *device_id, uint16_t queue,
-				   uint32_t flags, struct rte_ring *ring,
+				   uint32_t flags, uint16_t snap_len,
+				   struct rte_ring *ring,
 				   struct rte_mempool *mp,
-				   const void *filter, uint32_t len);
+				   const void *filter, uint32_t filter_len);
 
 /**
  * Disables packet capturing on given device_id and queue.