net: introduce packet capture support
PATCH v4 1/3 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.
---
Changes in v4:
* Move implementation to pcap.c/pcap.h
* Display error message when memory is exhausted.
* Populate $pcapsize environment variable with actual capture size.
* move outgoing pcap_post to eth_send()
Changes in v3:
* Change to implementation to command based.
* Added counters for incoming/outgoing packets.
* Support clearing the capture for multiple captures.
* Added documentation (separate patch).
Changes in v2:
Fix type assignmnet to header.ts_sec
cmd/Kconfig | 7 ++ cmd/Makefile | 1 + cmd/pcap.c | 71 +++++++++++++++++++++ include/net/pcap.h | 55 ++++++++++++++++ net/Makefile | 1 + net/eth-uclass.c | 7 ++ net/eth_legacy.c | 12 +++- net/net.c | 11 ++++ net/pcap.c | 156 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 cmd/pcap.c create mode 100644 include/net/pcap.h create mode 100644 net/pcap.c
cmd/Kconfig +7 -0
@@ -1263,6 +1263,13 @@ config BOOTP_NTPSERVER bool "Request & store 'ntpserverip' from BOOTP/DHCP server" depends on CMD_BOOTP +config CMD_PCAP + bool "pcap capture" + help + Selecting this will allow capturing all Ethernet packets and store + them in physical memory in a PCAP formated file, + later to be analyzed by PCAP reader application (IE. WireShark). + config BOOTP_PXE bool "Send PXE client arch to BOOTP/DHCP server" default y
cmd/Makefile +1 -0
@@ -102,6 +102,7 @@ obj-$(CONFIG_CMD_NVEDIT_EFI) += nvedit_efi.o obj-$(CONFIG_CMD_ONENAND) += onenand.o obj-$(CONFIG_CMD_OSD) += osd.o obj-$(CONFIG_CMD_PART) += part.o +obj-$(CONFIG_CMD_PCAP) += pcap.o ifdef CONFIG_PCI obj-$(CONFIG_CMD_PCI) += pci.o endif
cmd/pcap.c new file +71 -0
71 lines not on this page · raw message
include/net/pcap.h new file +55 -0
55 lines not on this page · raw message
net/Makefile +1 -0
@@ -21,6 +21,7 @@ obj-$(CONFIG_CMD_LINK_LOCAL) += link_local.o obj-$(CONFIG_NET) += net.o obj-$(CONFIG_CMD_NFS) += nfs.o obj-$(CONFIG_CMD_PING) += ping.o +obj-$(CONFIG_CMD_PCAP) += pcap.o obj-$(CONFIG_CMD_RARP) += rarp.o obj-$(CONFIG_CMD_SNTP) += sntp.o obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o
net/eth-uclass.c +7 -0
@@ -9,6 +9,9 @@#include <dm.h> #include <environment.h> #include <net.h> +#if defined(CONFIG_CMD_PCAP ) +#include <net/pcap.h> +#endif #include <dm/device-internal.h> #include <dm/uclass-internal.h> #include "eth_internal.h"
@@ -344,6 +347,10 @@ int eth_send(void *packet, int length)/* We cannot completely return the error at present */ debug ("%s: send() returned error %d\n" , __func__, ret); } +#if defined(CONFIG_CMD_PCAP ) +if (ret >=0 ) +pcap_post (packet, length, true); +#endif return ret; }
net/eth_legacy.c +11 -1
@@ -9,6 +9,9 @@#include <command.h> #include <environment.h> #include <net.h> +#if defined(CONFIG_CMD_PCAP ) +#include <net/pcap.h> +#endif #include <phy.h> #include <linux/errno.h> #include "eth_internal.h"
@@ -352,10 +355,17 @@ int eth_is_active(struct eth_device *dev)int eth_send (void *packet,int length) { +int ret; +if (!eth_current)return -ENODEV ; -return eth_current->send (eth_current, packet, length); + ret = eth_current->send (eth_current, packet, length); +#if defined(CONFIG_CMD_PCAP ) +if (ret >=0 ) +pcap_post (packet, lengeth, true); +#endif +return ret; }int eth_rx (void )
net/net.c +11 -0
@@ -95,6 +95,9 @@#include <net.h> #include <net/fastboot.h> #include <net/tftp.h> +#if defined(CONFIG_CMD_PCAP ) +#include <net/pcap.h> +#endif #if defined(CONFIG_LED_STATUS )#include <miiphy.h> #include <status_led.h>
@@ -671,6 +674,11 @@ done:net_set_icmp_handler (NULL );#endif net_set_state (prev_net_state); + +#if defined(CONFIG_CMD_PCAP ) +if (pcap_active ()) +pcap_print_status (); +#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_CMD_PCAP ) +pcap_post (in_packet, len, false); +#endif net_rx_packet = in_packet; net_rx_packet_len = len; et = (struct ethernet_hdr *)in_packet;
net/pcap.c new file +156 -0
156 lines not on this page · raw message
Signature · 1 line
2.22.0
PATCH v4 2/3 doc: pcap: add pcap cmd documentation
Add documentation for new "pcap" command.
---
Changes in v4:
* Document the new $pcapsize environment variable.
* update doc regarding error message when there's not enough memory left.
Changes in v3: None
Changes in v2: None
doc/README.pcap | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 doc/README.pcap
doc/README.pcap new file +62 -0
@@ -0,0 +1,62 @@ +PCAP: + +U-boot supports live Ethernet packet capture in PCAP(2.4) format. +This is enabled by CONFIG_CMD_PCAP. + +The capture is stored on physical memory, and should be copied to +a machine capable of parsing and displaying PCAP files (IE. wireshark) +If networking works properly one can copy the capture file from physical memory +using tftpput, or save it to local storage with (sf write, mmc write, fatwrite, etc) + +the pcap capturing requires maximum buffer size. +when the buffer is full an error message will be displayed and then packets +will silently drop. +the actual capture file size is populate in the environment variable "pcapsize". + +Usage example: + +# Initialize pcap capture to physical address (0x100000) with maximum size of +# 100000 bytes. + +# Start capture +pcap start + +# Initialize network activity +env set ipaddr 10.0.2.15; env set serverip 10.0.2.2; tftp uImage64 + +# Stop capture +pcap stop + +# pcap init 0x100000 100000 +PCAP capture initialized: addr: 0xffffffff80100000 max length: 100000 + +# pcap start +# env set ipaddr 10.0.2.15; env set serverip 10.0.2.2; tftp uImage64 +eth0 at 10000000: PHY present at 0 +eth0 at 10000000: link up, 1000Mbps full-duplex (lpa: 0x7c00) +Using eth0 at 10000000 device +TFTP from server 10.0.2.2; our IP address is 10.0.2.15 +Filename 'uImage64'. +Load address: 0xffffffff88000000 +Loading: ################################################################# + ################################################################# + ################################################################# + ################################################################# +!!! Buffer is full, consider increasing buffer size !!! + ################################################################# + ################################################################# + ################################################################# + ################################################################# + ################################################################# + # + 18.2 MiB/s +done +Bytes transferred = 8359376 (7f8dd0 hex) +PCAP status: + Initialized addr: 0xffffffff80100000 max length: 100000 + Status: Active. file size: 99991 + Incoming packets: 66 Outgoing packets: 67 + +# pcap stop +# tftpput 0xffffffff80100000 $pcapsize 10.0.2.2:capture.pcap +
Signature · 1 line
2.22.0
PATCH v4 2/3 doc: pcap: add pcap cmd documentation
Quoted 7 lines · Ramon Fried
Add documentation for new "pcap" command.
Signed-off-by Ramon Fried <[email protected]>---
Quoted 7 lines
Changes in v4:
* Document the new $pcapsize environment variable.
* update doc regarding error message when there's not enough memory left.Changes in v3: None
Changes in v2: None
I guess we can figure out the sandbox testing later.
[U-Boot] doc: pcap: add pcap cmd documentation
In reply to Ramon FriedHi Ramon,
https://patchwork.ozlabs.org/patch/1133815/ was applied to http://git.denx.de/?p=u-boot/u-boot-net.git
Thanks!
-Joe
PATCH v4 3/3 configs: sandbox: enable PCAP capture cmd
In reply to Ramon FriedEnable CONFIG_CMD_PCAP for testing PCAP capture.
---
Changes in v4:
* Patch added to patchset
Changes in v3: None
Changes in v2: None
configs/sandbox_defconfig | 1 + 1 file changed, 1 insertion(+)
configs/sandbox_defconfig +1 -0
@@ -47,6 +47,7 @@ CONFIG_CMD_REMOTEPROC=y CONFIG_CMD_SPI=y CONFIG_CMD_USB=y CONFIG_CMD_AXI=y +CONFIG_CMD_PCAP=y CONFIG_CMD_TFTPPUT=y CONFIG_CMD_TFTPSRV=y CONFIG_CMD_RARP=y
Signature · 1 line
2.22.0
PATCH v4 3/3 configs: sandbox: enable PCAP capture cmd
Quoted 4 lines · Ramon Fried
Enable
CONFIG_CMD_PCAPfor testing PCAP capture.Signed-off-by Ramon Fried <[email protected]>
[U-Boot] configs: sandbox: enable PCAP capture cmd
In reply to Ramon FriedHi Ramon,
https://patchwork.ozlabs.org/patch/1133816/ was applied to http://git.denx.de/?p=u-boot/u-boot-net.git
Thanks!
-Joe
PATCH v4 1/3 net: introduce packet capture support
In reply to Ramon FriedQuoted 11 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.
Signed-off-by Ramon Fried <[email protected]>Reviewed-by Alex Marginean <[email protected]>Tested-by Alex Marginean <[email protected]>
PATCH v4 1/3 net: introduce packet capture support
In reply to Ramon FriedQuoted 29 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.
Signed-off-by Ramon Fried <[email protected]>Reviewed-by Alex Marginean <[email protected]>Tested-by Alex Marginean <[email protected]>---
Changes in v4:
* Move implementation topcap.c/pcap.h
* Display error message when memory is exhausted.
* Populate $pcapsize environment variable with actual capture size.
* move outgoingpcap_posttoeth_send()Changes in v3:
* Change to implementation to command based.
* Added counters for incoming/outgoing packets.
* Support clearing the capture for multiple captures.
* Added documentation (separate patch).Changes in v2:
Fix type assignmnet to header.ts_sec
[ ... ]
Quoted 47 lines
net/pcap.c new file +41
-0@@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2019 Ramon Fried <[email protected]> +*/ + +#include <common.h> +#include <net.h> +#include <net/pcap.h> +#include <time.h> +#include <asm/io.h> + +#define LINKTYPE_ETHERNET 1 + +static bool initialized; +static bool running; +static bool buffer_full; +static void *buf; +static unsigned int max_size; +static unsigned int pos; + +static unsigned long incoming_count; +static unsigned long outgoing_count; + +struct pcap_header { +u32 magic; +u16 version_major; +u16 version_minor; +s32 thiszone; +u32 sigfigs; +u32 snaplen; +u32 network; +}; + +struct pcap_packet_header { +u32 ts_sec; +u32 ts_usec; +u32 incl_len; +u32 orig_len; +}; + +static struct pcap_header file_header = {
net/pcap.c line 51
Quoted 10 lines
net/pcap.c new file +10
-0+ .magic = 0xa1b2c3d4, + .version_major = 2, + .version_minor = 4, + .snaplen = 65535, + .network = LINKTYPE_ETHERNET, +}; + +int pcap_init (phys_addr_t paddr,unsigned long size) +{ + buf =map_physmem (paddr, size,0 );
Since this is mapping to physmem, and apparently from your documentation, you need to use the physical memory when writing to a file or tftp send, how is the mapping typically handled? Maybe we should be writing to a variable like "pcapaddr" so it can be used in the write as part of a script.
Quoted 112 lines
net/pcap.c new file +105
-1+if (!buf) { +printf ("Failed mapping PCAP memory\n" ); +return -ENOMEM ; + } + +printf ("PCAP capture initialized: addr: 0x%lx max length: %lu\n" , + (unsigned long )buf, size); + +memcpy (buf, &file_header,sizeof (file_header)); + pos =sizeof (file_header); + max_size = size; + initialized = true; + running = false; + buffer_full = false; + incoming_count =0 ; + outgoing_count =0 ; +return 0 ; +} + +int pcap_start_stop (bool start) +{ +if (!initialized) { +printf ("error: pcap was not initialized\n" ); +return -ENODEV ; + } + + running = start; + +return 0 ; +} + +int pcap_clear (void ) +{ +if (!initialized) { +printf ("error: pcap was not initialized\n" ); +return -ENODEV ; + } + + pos =sizeof (file_header); + incoming_count =0 ; + outgoing_count =0 ; + buffer_full = false; + +printf ("pcap capture cleared\n" ); +return 0 ; +} + +int pcap_post (const void *packet,size_t len,bool outgoing) +{ +struct pcap_packet_header header; +u64 cur_time =timer_get_us (); + +if (!initialized || !running || !buf) +return -ENODEV ; + +if (buffer_full) +return -ENOMEM ; + +if ((pos + len +sizeof (header)) >= max_size) { + buffer_full = true; +printf ("\n!!! Buffer is full, consider increasing buffer size !!!\n" ); +return -ENOMEM ; + } + + header.ts_sec = cur_time /1000000 ; + header.ts_usec = cur_time %1000000 ; + header.incl_len = len; + header.orig_len = len; + +memcpy (buf + pos, &header,sizeof (header)); + pos +=sizeof (header); +memcpy (buf + pos, packet, len); + pos += len; + +if (outgoing) + outgoing_count++; +else + incoming_count++; + +env_set_hex ("pcapsize" , pos); + +return 0 ; +} + +int pcap_print_status (void ) +{ +if (!initialized) { +printf ("pcap was not initialized\n" ); +return -ENODEV ; + } +printf ("PCAP status:\n" ); +printf ("\tInitialized addr: 0x%lx\tmax length: %u\n" , + (unsigned long )buf, max_size); +printf ("\tStatus: %s.\t file size: %u\n" , running ?"Active" :"Idle" , + pos); +printf ("\tIncoming packets: %lu Outgoing packets: %lu\n" , + incoming_count, outgoing_count); + +return 0 ; +} + +bool pcap_active (void ) +{ +return running; +} --2.22.0
_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot
Hi Ramon,
https://patchwork.ozlabs.org/patch/1133814/ was applied to http://git.denx.de/?p=u-boot/u-boot-net.git
Thanks!
-Joe