Skip to contentSkip to conversation

net: introduce packet capture support

PATCH Superseded Ramon Fried 2 participants 4 messages Jun 21, 2019 Networking

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: introduce packet capture support

Add support for capturing ethernet packets and storing them in memory in PCAP(2.4) format, later to be analyzed by any PCAP viewer software (IE. Wireshark)

This feature greatly assist debugging network issues such as detecting dropped packets, packet corruption etc.

---
v2: Fix type assignmnet to header.ts_sec

 include/net.h | 29 ++++++++++++++++++
 net/Kconfig   | 22 ++++++++++++++
 net/Makefile  |  1 +
 net/net.c     | 11 +++++++
 net/pcap.c    | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 146 insertions(+)
 create mode 100644 net/pcap.c
include/net.h +29 -0
index 44b32385c4..d1dc864896 100644
@@ -630,6 +630,31 @@ bool arp_is_waiting(void);		/* Waiting for ARP reply? */
 void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
 void net_set_timeout_handler(ulong, thand_f *);/* Set timeout handler */
 
+/* PCAP extension */
+
+/**
+ * pcap_init() - Initialize PCAP memory buffer
+ *
+ * @return	0 on success, -ERROR on error
+ */
+int pcap_init(void);
+
+/**
+ * pcap_post() - Post a packet to PCAP file
+ *
+ * @packet:	packet to post
+ * @len:	packet length in bytes
+ * @return	0 on success, -ERROR on error
+ */
+int pcap_post(const void *packet, size_t len);
+
+/**
+ * pcap_size() - get size of PCAP file
+ *
+ * @return	size of PCAP file in bytes
+ */
+unsigned int pcap_size(void);
+
 /* Network loop state */
 enum net_loop_state {
 	NETLOOP_CONTINUE,
@@ -658,6 +683,10 @@ static inline void net_send_packet(uchar *pkt, int len)
 {
 	/* Currently no way to return errors from eth_send() */
 	(void) eth_send(pkt, len);
+
+#if defined(CONFIG_NET_PCAP)
+	pcap_post(pkt, len);
+#endif
 }
 
 /*
net/Kconfig +22 -0
index 68cecf75a2..f1d3719b04 100644
@@ -8,6 +8,28 @@ menuconfig NET
 
 if NET
 
+config NET_PCAP
+	bool "PCAP network capture"
+	help
+	  Selecting this will capture all Ethernet packets and store
+	  them in a PCAP formated file later to be analyzed by PCAP
+	  reader application (IE. WireShark).
+
+config NET_PCAP_ADDR
+	hex "Address of PCAP file in memory"
+	depends on NET_PCAP
+	help
+	  Sets the address of the PCAP file in physical memory.
+
+config NET_PCAP_SIZE
+	hex "Size of PCAP file"
+	depends on NET_PCAP
+	default 0x100000
+	help
+	  Sets the maximum size of PCAP file in bytes.
+	  When it fills up completely, it will stop capturing
+	  packets silently.
+
 config NET_RANDOM_ETHADDR
 	bool "Random ethaddr if unset"
 	help
net/Makefile +1 -0
index ce36362168..85103fee93 100644
@@ -18,6 +18,7 @@ endif
 obj-$(CONFIG_NET)      += eth_common.o
 obj-$(CONFIG_CMD_LINK_LOCAL) += link_local.o
 obj-$(CONFIG_NET)      += net.o
+obj-$(CONFIG_NET_PCAP) += pcap.o
 obj-$(CONFIG_CMD_NFS)  += nfs.o
 obj-$(CONFIG_CMD_PING) += ping.o
 obj-$(CONFIG_CMD_RARP) += rarp.o
net/net.c +11 -0
index 58b0417cbe..d0bdf8fe20 100644
@@ -387,6 +387,10 @@ void net_init(void)
 
 		/* Only need to setup buffer pointers once. */
 		first_call = 0;
+
+#if defined(CONFIG_NET_PCAP)
+		pcap_init();
+#endif
 	}
 
 	net_init_loop();
@@ -671,6 +675,10 @@ done:
 	net_set_icmp_handler(NULL);
 #endif
 	net_set_state(prev_net_state);
+
+#if defined(CONFIG_NET_PCAP)
+	printf("PCAP len: 0x%x\n", pcap_size());
+#endif
 	return ret;
 }
 
@@ -1083,6 +1091,9 @@ void net_process_received_packet(uchar *in_packet, int len)
 
 	debug_cond(DEBUG_NET_PKT, "packet received\n");
 
+#if defined(CONFIG_NET_PCAP)
+	pcap_post(in_packet, len);
+#endif
 	net_rx_packet = in_packet;
 	net_rx_packet_len = len;
 	et = (struct ethernet_hdr *)in_packet;
net/pcap.c new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.22.0
#1 lore raw

PATCH v2 1/1 net: introduce packet capture support

Hi Ramon,

Quoted 5 lines · Ramon Fried

Add support for capturing ethernet packets and storing them in memory in PCAP(2.4) format, later to be analyzed by any PCAP viewer software (IE. Wireshark)

Quoted 8 lines

This feature greatly assist debugging network issues such as detecting dropped packets, packet corruption etc.

---
v2: Fix type assignmnet to header.ts_sec

Could you please add functionality as Alex suggested? Besides what Aleix mentioned, can you also add the sub-function in the pcap command to store the captured packets from memory to file system?

Quoted 8 lines
 include/net.h | 29 ++++++++++++++++++
 net/Kconfig   | 22 ++++++++++++++
 net/Makefile  |  1 +
 net/net.c     | 11 +++++++
 net/pcap.c    | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 146 insertions(+)
 create mode 100644 net/pcap.c

Regards,
Bin

#2 lore raw

PATCH v2 1/1 net: introduce packet capture support

Quoted 18 lines · Bin Meng

Hi Ramon,

Quoted 5 lines · Ramon Fried

Add support for capturing ethernet packets and storing them in memory in PCAP(2.4) format, later to be analyzed by any PCAP viewer software (IE. Wireshark)

Quoted 8 lines

This feature greatly assist debugging network issues such as detecting dropped packets, packet corruption etc.

---
v2: Fix type assignmnet to header.ts_sec

Quoted 3 lines

Could you please add functionality as Alex suggested? Besides what Aleix mentioned, can you also add the sub-function in the pcap command to store the captured packets from memory to file system?

Sure, I started working on it, but it's a different patch Regarding command to store it on file system, I don't think it's a wise idea, There's plenty of options on how to store it, not only fatwrite, it could be sf write, mmc write, etc.
I think it's better to document the feature and explain the different options users has to store the data.
Basically it's just typing another command.
Thanks,
Ramon.

Quoted 12 lines
Quoted 9 lines
 include/net.h | 29 ++++++++++++++++++
 net/Kconfig   | 22 ++++++++++++++
 net/Makefile  |  1 +
 net/net.c     | 11 +++++++
 net/pcap.c    | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 146 insertions(+)
 create mode 100644 net/pcap.c

Regards,
Bin

#3 lore raw

PATCH v2 1/1 net: introduce packet capture support

Hi Ramon,

Quoted 20 lines · Ramon Fried
Quoted 18 lines · Bin Meng

Hi Ramon,

Quoted 13 lines · Ramon Fried

Add support for capturing ethernet packets and storing them in memory in PCAP(2.4) format, later to be analyzed by any PCAP viewer software (IE. Wireshark)

This feature greatly assist debugging network issues such as detecting dropped packets, packet corruption etc.

---
v2: Fix type assignmnet to header.ts_sec

Quoted 4 lines
Quoted 3 lines

Could you please add functionality as Alex suggested? Besides what Aleix mentioned, can you also add the sub-function in the pcap command to store the captured packets from memory to file system?

Sure, I started working on it, but it's a different patch

Thanks, please include all pcap related patches in a series.

Quoted 5 lines

Regarding command to store it on file system, I don't think it's a wise idea, There's plenty of options on how to store it, not only fatwrite, it could be sf write, mmc write, etc.
I think it's better to document the feature and explain the different options users has to store the data.

Sure, a document explaining how to use this feature is definitely helpful.

Quoted 1 line

Basically it's just typing another command.

Regards,
Bin