1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Ramon Fried <rfried.dev@gmail.com>
9 #include <linux/errno.h>
12 #define LINKTYPE_ETHERNET 1
14 static bool initialized
;
16 static bool buffer_full
;
18 static unsigned int max_size
;
19 static unsigned int pos
;
21 static unsigned long incoming_count
;
22 static unsigned long outgoing_count
;
34 struct pcap_packet_header
{
41 static struct pcap_header file_header
= {
46 .network
= LINKTYPE_ETHERNET
,
49 int pcap_init(phys_addr_t paddr
, unsigned long size
)
51 buf
= map_physmem(paddr
, size
, 0);
53 printf("Failed mapping PCAP memory\n");
57 printf("PCAP capture initialized: addr: 0x%lx max length: %lu\n",
58 (unsigned long)buf
, size
);
60 memcpy(buf
, &file_header
, sizeof(file_header
));
61 pos
= sizeof(file_header
);
71 int pcap_start_stop(bool start
)
74 printf("error: pcap was not initialized\n");
86 printf("error: pcap was not initialized\n");
90 pos
= sizeof(file_header
);
95 printf("pcap capture cleared\n");
99 int pcap_post(const void *packet
, size_t len
, bool outgoing
)
101 struct pcap_packet_header header
;
102 u64 cur_time
= timer_get_us();
104 if (!initialized
|| !running
|| !buf
)
110 if ((pos
+ len
+ sizeof(header
)) >= max_size
) {
112 printf("\n!!! Buffer is full, consider increasing buffer size !!!\n");
116 header
.ts_sec
= cur_time
/ 1000000;
117 header
.ts_usec
= cur_time
% 1000000;
118 header
.incl_len
= len
;
119 header
.orig_len
= len
;
121 memcpy(buf
+ pos
, &header
, sizeof(header
));
122 pos
+= sizeof(header
);
123 memcpy(buf
+ pos
, packet
, len
);
131 env_set_hex("pcapsize", pos
);
136 int pcap_print_status(void)
139 printf("pcap was not initialized\n");
142 printf("PCAP status:\n");
143 printf("\tInitialized addr: 0x%lx\tmax length: %u\n",
144 (unsigned long)buf
, max_size
);
145 printf("\tStatus: %s.\t file size: %u\n", running
? "Active" : "Idle",
147 printf("\tIncoming packets: %lu Outgoing packets: %lu\n",
148 incoming_count
, outgoing_count
);
153 bool pcap_active(void)