gem5  v22.0.0.1
etherdump.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2005 The Regents of The University of Michigan
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
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* @file
30  * Simple object for creating a simple pcap style packet trace
31  */
32 #include "dev/net/etherdump.hh"
33 
34 #include <sys/time.h>
35 
36 #include <algorithm>
37 #include <string>
38 
39 #include "base/logging.hh"
40 #include "base/output.hh"
41 #include "sim/core.hh"
42 #include "sim/cur_tick.hh"
43 
44 using std::string;
45 
46 namespace gem5
47 {
48 
50  : SimObject(p), stream(simout.create(p.file, true)->stream()),
51  maxlen(p.maxlen)
52 {
53 }
54 
55 #define DLT_EN10MB 1 // Ethernet (10Mb)
56 #define TCPDUMP_MAGIC 0xa1b2c3d4
57 #define PCAP_VERSION_MAJOR 2
58 #define PCAP_VERSION_MINOR 4
59 
61 {
62  uint32_t magic;
63  uint16_t version_major;
64  uint16_t version_minor;
65  int32_t thiszone; // gmt to local correction
66  uint32_t sigfigs; // accuracy of timestamps
67  uint32_t snaplen; // max length saved portion of each pkt
68  uint32_t linktype; // data link type (DLT_*)
69 };
70 
72 {
73  uint32_t seconds;
74  uint32_t microseconds;
75  uint32_t caplen; // length of portion present
76  uint32_t len; // length this packet (off wire)
77 };
78 
79 void
81 {
82  struct pcap_file_header hdr;
83  hdr.magic = TCPDUMP_MAGIC;
86 
87  hdr.thiszone = 0;
88  hdr.snaplen = 1500;
89  hdr.sigfigs = 0;
90  hdr.linktype = DLT_EN10MB;
91 
92  stream->write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
93 
94  stream->flush();
95 }
96 
97 void
99 {
100  pcap_pkthdr pkthdr;
101  pkthdr.seconds = curTick() / sim_clock::as_int::s;
102  pkthdr.microseconds = (curTick() / sim_clock::as_int::us) % 1000000ULL;
103  pkthdr.caplen = std::min(packet->length, maxlen);
104  pkthdr.len = packet->length;
105  stream->write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
106  stream->write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
107  stream->flush();
108 }
109 
110 } // namespace gem5
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
DLT_EN10MB
#define DLT_EN10MB
Definition: etherdump.cc:55
gem5::pcap_file_header::magic
uint32_t magic
Definition: etherdump.cc:62
TCPDUMP_MAGIC
#define TCPDUMP_MAGIC
Definition: etherdump.cc:56
gem5::EtherDump::dumpPacket
void dumpPacket(EthPacketPtr &packet)
Definition: etherdump.cc:98
gem5::pcap_file_header::snaplen
uint32_t snaplen
Definition: etherdump.cc:67
PCAP_VERSION_MAJOR
#define PCAP_VERSION_MAJOR
Definition: etherdump.cc:57
cur_tick.hh
etherdump.hh
gem5::simout
OutputDirectory simout
Definition: output.cc:62
gem5::EtherDump::maxlen
const unsigned maxlen
Definition: etherdump.hh:52
gem5::pcap_file_header::sigfigs
uint32_t sigfigs
Definition: etherdump.cc:66
gem5::sim_clock::as_int::us
Tick us
microsecond
Definition: core.cc:70
gem5::sim_clock::as_int::s
Tick s
second
Definition: core.cc:68
gem5::pcap_file_header::version_major
uint16_t version_major
Definition: etherdump.cc:63
output.hh
gem5::EtherDump::stream
std::ostream * stream
Definition: etherdump.hh:51
gem5::pcap_pkthdr::seconds
uint32_t seconds
Definition: etherdump.cc:73
gem5::EtherDump::EtherDump
EtherDump(const Params &p)
Definition: etherdump.cc:49
gem5::VegaISA::p
Bitfield< 54 > p
Definition: pagetable.hh:70
gem5::EthPacketPtr
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90
gem5::pcap_pkthdr::len
uint32_t len
Definition: etherdump.cc:76
gem5::pcap_file_header::thiszone
int32_t thiszone
Definition: etherdump.cc:65
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::EtherDump::init
void init()
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: etherdump.cc:80
gem5::pcap_file_header::linktype
uint32_t linktype
Definition: etherdump.cc:68
gem5::pcap_pkthdr::microseconds
uint32_t microseconds
Definition: etherdump.cc:74
core.hh
gem5::EtherDump::Params
EtherDumpParams Params
Definition: etherdump.hh:57
logging.hh
gem5::pcap_file_header::version_minor
uint16_t version_minor
Definition: etherdump.cc:64
gem5::pcap_file_header
Definition: etherdump.cc:60
PCAP_VERSION_MINOR
#define PCAP_VERSION_MINOR
Definition: etherdump.cc:58
gem5::pcap_pkthdr::caplen
uint32_t caplen
Definition: etherdump.cc:75
gem5::pcap_pkthdr
Definition: etherdump.cc:71
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37

Generated on Sat Jun 18 2022 08:12:24 for gem5 by doxygen 1.8.17