gem5
v20.1.0.0
dev
net
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
43
using
std::string;
44
45
EtherDump::EtherDump
(
const
Params
*
p
)
46
:
SimObject
(
p
), stream(
simout
.create(
p
->file, true)->stream()),
47
maxlen(
p
->maxlen)
48
{
49
}
50
51
#define DLT_EN10MB 1 // Ethernet (10Mb)
52
#define TCPDUMP_MAGIC 0xa1b2c3d4
53
#define PCAP_VERSION_MAJOR 2
54
#define PCAP_VERSION_MINOR 4
55
56
struct
pcap_file_header
{
57
uint32_t
magic
;
58
uint16_t
version_major
;
59
uint16_t
version_minor
;
60
int32_t
thiszone
;
// gmt to local correction
61
uint32_t
sigfigs
;
// accuracy of timestamps
62
uint32_t
snaplen
;
// max length saved portion of each pkt
63
uint32_t
linktype
;
// data link type (DLT_*)
64
};
65
66
struct
pcap_pkthdr
{
67
uint32_t
seconds
;
68
uint32_t
microseconds
;
69
uint32_t
caplen
;
// length of portion present
70
uint32_t
len
;
// length this packet (off wire)
71
};
72
73
void
74
EtherDump::init
()
75
{
76
struct
pcap_file_header
hdr;
77
hdr.
magic
=
TCPDUMP_MAGIC
;
78
hdr.
version_major
=
PCAP_VERSION_MAJOR
;
79
hdr.
version_minor
=
PCAP_VERSION_MINOR
;
80
81
hdr.
thiszone
= 0;
82
hdr.
snaplen
= 1500;
83
hdr.
sigfigs
= 0;
84
hdr.
linktype
=
DLT_EN10MB
;
85
86
stream
->write(
reinterpret_cast<
char
*
>
(&hdr),
sizeof
(hdr));
87
88
stream
->flush();
89
}
90
91
void
92
EtherDump::dumpPacket
(
EthPacketPtr
&packet)
93
{
94
pcap_pkthdr
pkthdr;
95
pkthdr.
seconds
=
curTick
() /
SimClock::Int::s
;
96
pkthdr.
microseconds
= (
curTick
() /
SimClock::Int::us
) %
ULL
(1000000);
97
pkthdr.
caplen
= std::min(packet->length,
maxlen
);
98
pkthdr.
len
= packet->length;
99
stream
->write(
reinterpret_cast<
char
*
>
(&pkthdr),
sizeof
(pkthdr));
100
stream
->write(
reinterpret_cast<
char
*
>
(packet->data), pkthdr.
caplen
);
101
stream
->flush();
102
}
103
104
EtherDump
*
105
EtherDumpParams::create()
106
{
107
return
new
EtherDump
(
this
);
108
}
DLT_EN10MB
#define DLT_EN10MB
Definition:
etherdump.cc:51
TCPDUMP_MAGIC
#define TCPDUMP_MAGIC
Definition:
etherdump.cc:52
EtherDump::init
void init()
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition:
etherdump.cc:74
PCAP_VERSION_MAJOR
#define PCAP_VERSION_MAJOR
Definition:
etherdump.cc:53
pcap_pkthdr::len
uint32_t len
Definition:
etherdump.cc:70
etherdump.hh
SimClock::Int::us
Tick us
microsecond
Definition:
core.cc:64
pcap_file_header::sigfigs
uint32_t sigfigs
Definition:
etherdump.cc:61
output.hh
pcap_pkthdr
Definition:
etherdump.cc:66
EtherDump::stream
std::ostream * stream
Definition:
etherdump.hh:48
pcap_file_header::magic
uint32_t magic
Definition:
etherdump.cc:57
pcap_file_header::version_major
uint16_t version_major
Definition:
etherdump.cc:58
pcap_file_header::thiszone
int32_t thiszone
Definition:
etherdump.cc:60
SimClock::Int::s
Tick s
second
Definition:
core.cc:62
pcap_pkthdr::seconds
uint32_t seconds
Definition:
etherdump.cc:67
pcap_file_header
Definition:
etherdump.cc:56
EtherDump::dumpPacket
void dumpPacket(EthPacketPtr &packet)
Definition:
etherdump.cc:92
pcap_file_header::linktype
uint32_t linktype
Definition:
etherdump.cc:63
pcap_file_header::version_minor
uint16_t version_minor
Definition:
etherdump.cc:59
core.hh
pcap_pkthdr::microseconds
uint32_t microseconds
Definition:
etherdump.cc:68
EthPacketPtr
std::shared_ptr< EthPacketData > EthPacketPtr
Definition:
etherpkt.hh:87
EtherDump::maxlen
const unsigned maxlen
Definition:
etherdump.hh:49
pcap_file_header::snaplen
uint32_t snaplen
Definition:
etherdump.cc:62
pcap_pkthdr::caplen
uint32_t caplen
Definition:
etherdump.cc:69
logging.hh
EtherDump
Definition:
etherdump.hh:45
PCAP_VERSION_MINOR
#define PCAP_VERSION_MINOR
Definition:
etherdump.cc:54
simout
OutputDirectory simout
Definition:
output.cc:61
MipsISA::p
Bitfield< 0 > p
Definition:
pra_constants.hh:323
ULL
#define ULL(N)
uint64_t constant
Definition:
types.hh:50
EtherDump::Params
EtherDumpParams Params
Definition:
etherdump.hh:54
EtherDump::EtherDump
EtherDump(const Params *p)
Definition:
etherdump.cc:45
curTick
Tick curTick()
The current simulated tick.
Definition:
core.hh:45
SimObject
Abstract superclass for simulation objects.
Definition:
sim_object.hh:92
Generated on Wed Sep 30 2020 14:02:11 for gem5 by
doxygen
1.8.17