1 /* 2 * Copyright (C) 2017 THL A29 Limited, a Tencent company. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 */ 26 27 #include <sys/time.h> 28 #include <unistd.h> 29 30 #include "ff_dpdk_pcap.h" 31 32 struct pcap_file_header { 33 uint32_t magic; 34 u_short version_major; 35 u_short version_minor; 36 int32_t thiszone; /* gmt to local correction */ 37 uint32_t sigfigs; /* accuracy of timestamps */ 38 uint32_t snaplen; /* max length saved portion of each pkt */ 39 uint32_t linktype; /* data link type (LINKTYPE_*) */ 40 }; 41 42 struct pcap_pkthdr { 43 uint32_t sec; /* time stamp */ 44 uint32_t usec; /* struct timeval time_t, in linux64: 8*2=16, in cap: 4 */ 45 uint32_t caplen; /* length of portion present */ 46 uint32_t len; /* length this packet (off wire) */ 47 }; 48 49 int 50 ff_enable_pcap(const char* dump_path) 51 { 52 FILE* fp = fopen(dump_path, "w"); 53 if (fp == NULL) { 54 rte_exit(EXIT_FAILURE, "Cannot open pcap dump path: %s\n", dump_path); 55 return -1; 56 } 57 58 struct pcap_file_header pcap_file_hdr; 59 void* file_hdr = &pcap_file_hdr; 60 61 pcap_file_hdr.magic = 0xA1B2C3D4; 62 pcap_file_hdr.version_major = 0x0002; 63 pcap_file_hdr.version_minor = 0x0004; 64 pcap_file_hdr.thiszone = 0x00000000; 65 pcap_file_hdr.sigfigs = 0x00000000; 66 pcap_file_hdr.snaplen = 0x0000FFFF; //65535 67 pcap_file_hdr.linktype = 0x00000001; //DLT_EN10MB, Ethernet (10Mb) 68 69 fwrite(file_hdr, sizeof(struct pcap_file_header), 1, fp); 70 fclose(fp); 71 72 return 0; 73 } 74 75 int 76 ff_dump_packets(const char* dump_path, struct rte_mbuf* pkt) 77 { 78 FILE* fp = fopen(dump_path, "a"); 79 if (fp == NULL) { 80 return -1; 81 } 82 83 struct pcap_pkthdr pcap_hdr; 84 void* hdr = &pcap_hdr; 85 86 struct timeval ts; 87 gettimeofday(&ts, NULL); 88 pcap_hdr.sec = ts.tv_sec; 89 pcap_hdr.usec = ts.tv_usec; 90 pcap_hdr.caplen = pkt->pkt_len; 91 pcap_hdr.len = pkt->pkt_len; 92 fwrite(hdr, sizeof(struct pcap_pkthdr), 1, fp); 93 94 while(pkt != NULL) { 95 fwrite(rte_pktmbuf_mtod(pkt, char*), pkt->data_len, 1, fp); 96 pkt = pkt->next; 97 } 98 99 fclose(fp); 100 101 return 0; 102 } 103 104