gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
44using std::string;
45
46namespace 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
79void
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
97void
99{
100 pcap_pkthdr pkthdr;
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
std::ostream * stream
Definition etherdump.hh:51
void dumpPacket(EthPacketPtr &packet)
Definition etherdump.cc:98
void init()
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition etherdump.cc:80
const unsigned maxlen
Definition etherdump.hh:52
EtherDump(const Params &p)
Definition etherdump.cc:49
EtherDumpParams Params
Definition etherdump.hh:57
Abstract superclass for simulation objects.
#define DLT_EN10MB
Definition etherdump.cc:55
#define PCAP_VERSION_MINOR
Definition etherdump.cc:58
#define PCAP_VERSION_MAJOR
Definition etherdump.cc:57
#define TCPDUMP_MAGIC
Definition etherdump.cc:56
Bitfield< 0 > p
Tick us
microsecond
Definition core.cc:67
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Tick curTick()
The universal simulation clock.
Definition cur_tick.hh:46
OutputDirectory simout
Definition output.cc:62
std::shared_ptr< EthPacketData > EthPacketPtr
Definition etherpkt.hh:90
uint32_t microseconds
Definition etherdump.cc:74

Generated on Tue Jun 18 2024 16:24:03 for gem5 by doxygen 1.11.0