2525 */
2626
2727#include <sys/time.h>
28+ #include <time.h>
2829#include <unistd.h>
2930#include <errno.h>
3031
3132#include "ff_dpdk_pcap.h"
3233#define FILE_PATH_LEN 64
3334#define PCAP_FILE_NUM 10
35+ #define PCAP_MAGIC_USEC 0xA1B2C3D4
36+ #define PCAP_MAGIC_NSEC 0xA1B23C4D
3437
3538struct pcap_file_header {
3639 uint32_t magic ;
@@ -44,7 +47,7 @@ struct pcap_file_header {
4447
4548struct pcap_pkthdr {
4649 uint32_t sec ; /* time stamp */
47- uint32_t usec ; /* struct timeval time_t, in linux64: 8*2=16, in cap: 4 */
50+ uint32_t usec_or_nsec ; /* usec when magic=0xA1B2C3D4, nsec when magic=0xA1B23C4D */
4851 uint32_t caplen ; /* length of portion present */
4952 uint32_t len ; /* length this packet (off wire) */
5053};
@@ -53,7 +56,7 @@ static __thread FILE* g_pcap_fp = NULL;
5356static __thread uint32_t seq = 0 ;
5457static __thread uint32_t g_flen = 0 ;
5558
56- int ff_enable_pcap (const char * dump_path , uint16_t snap_len )
59+ int ff_enable_pcap (const char * dump_path , uint16_t snap_len , uint8_t timestamp_precision )
5760{
5861 char pcap_f_path [FILE_PATH_LEN ] = {0 };
5962
@@ -68,7 +71,7 @@ int ff_enable_pcap(const char* dump_path, uint16_t snap_len)
6871 struct pcap_file_header pcap_file_hdr ;
6972 void * file_hdr = & pcap_file_hdr ;
7073
71- pcap_file_hdr .magic = 0xA1B2C3D4 ;
74+ pcap_file_hdr .magic = timestamp_precision ? PCAP_MAGIC_NSEC : PCAP_MAGIC_USEC ;
7275 pcap_file_hdr .version_major = 0x0002 ;
7376 pcap_file_hdr .version_minor = 0x0004 ;
7477 pcap_file_hdr .thiszone = 0x00000000 ;
@@ -83,21 +86,30 @@ int ff_enable_pcap(const char* dump_path, uint16_t snap_len)
8386}
8487
8588int
86- ff_dump_packets (const char * dump_path , struct rte_mbuf * pkt , uint16_t snap_len , uint32_t f_maxlen )
89+ ff_dump_packets (const char * dump_path , struct rte_mbuf * pkt , uint16_t snap_len , uint32_t f_maxlen , uint8_t timestamp_precision )
8790{
8891 unsigned int out_len = 0 , wr_len = 0 ;
8992 struct pcap_pkthdr pcap_hdr ;
9093 void * hdr = & pcap_hdr ;
91- struct timeval ts ;
9294 char pcap_f_path [FILE_PATH_LEN ] = {0 };
9395
9496 if (g_pcap_fp == NULL ) {
9597 return -1 ;
9698 }
9799 snap_len = pkt -> pkt_len < snap_len ? pkt -> pkt_len : snap_len ;
98- gettimeofday (& ts , NULL );
99- pcap_hdr .sec = ts .tv_sec ;
100- pcap_hdr .usec = ts .tv_usec ;
100+
101+ if (timestamp_precision ) {
102+ struct timespec ts ;
103+ clock_gettime (CLOCK_REALTIME , & ts );
104+ pcap_hdr .sec = ts .tv_sec ;
105+ pcap_hdr .usec_or_nsec = ts .tv_nsec ;
106+ } else {
107+ struct timeval ts ;
108+ gettimeofday (& ts , NULL );
109+ pcap_hdr .sec = ts .tv_sec ;
110+ pcap_hdr .usec_or_nsec = ts .tv_usec ;
111+ }
112+
101113 pcap_hdr .caplen = snap_len ;
102114 pcap_hdr .len = pkt -> pkt_len ;
103115 fwrite (hdr , sizeof (struct pcap_pkthdr ), 1 , g_pcap_fp );
@@ -117,7 +129,7 @@ ff_dump_packets(const char* dump_path, struct rte_mbuf* pkt, uint16_t snap_len,
117129 if ( ++ seq >= PCAP_FILE_NUM )
118130 seq = 0 ;
119131
120- ff_enable_pcap (dump_path , snap_len );
132+ ff_enable_pcap (dump_path , snap_len , timestamp_precision );
121133 }
122134
123135 return 0 ;
0 commit comments