Don't schedule tickEvent if it's already been scheduled.
[gem5.git] / dev / etherdump.cc
1 /*
2 * Copyright (c) 2003 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
33 #include <sys/time.h>
34
35 #include <string>
36
37 #include "dev/etherdump.hh"
38 #include "sim/builder.hh"
39 #include "sim/universe.hh"
40
41 using std::string;
42
43 EtherDump::EtherDump(const string &name, const string &file)
44 : SimObject(name)
45 {
46 if (!file.empty()) {
47 stream.open(file.c_str());
48 if (stream.is_open())
49 init();
50 }
51 }
52
53 #define DLT_EN10MB 1 // Ethernet (10Mb)
54 #define TCPDUMP_MAGIC 0xa1b2c3d4
55 #define PCAP_VERSION_MAJOR 2
56 #define PCAP_VERSION_MINOR 4
57
58 struct pcap_file_header {
59 uint32_t magic;
60 uint16_t version_major;
61 uint16_t version_minor;
62 int32_t thiszone; // gmt to local correction
63 uint32_t sigfigs; // accuracy of timestamps
64 uint32_t snaplen; // max length saved portion of each pkt
65 uint32_t linktype; // data link type (DLT_*)
66 };
67
68 struct pcap_pkthdr {
69 struct timeval ts; // time stamp
70 uint32_t caplen; // length of portion present
71 uint32_t len; // length this packet (off wire)
72 };
73
74 void
75 EtherDump::init()
76 {
77 curtime = time(NULL);
78 s_freq = ticksPerSecond;
79 us_freq = ticksPerSecond / ULL(1000000);
80
81 struct pcap_file_header hdr;
82 hdr.magic = TCPDUMP_MAGIC;
83 hdr.version_major = PCAP_VERSION_MAJOR;
84 hdr.version_minor = PCAP_VERSION_MINOR;
85
86 hdr.thiszone = -5 * 3600;
87 hdr.snaplen = 1500;
88 hdr.sigfigs = 0;
89 hdr.linktype = DLT_EN10MB;
90
91 stream.write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
92
93 /*
94 * output an empty packet with the current time so that we know
95 * when the simulation began. This allows us to correlate packets
96 * to sim_cycles.
97 */
98 pcap_pkthdr pkthdr;
99 pkthdr.ts.tv_sec = curtime;
100 pkthdr.ts.tv_usec = 0;
101 pkthdr.caplen = 0;
102 pkthdr.len = 0;
103 stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
104
105 stream.flush();
106 }
107
108 void
109 EtherDump::dumpPacket(PacketPtr packet)
110 {
111 pcap_pkthdr pkthdr;
112 pkthdr.ts.tv_sec = curtime + (curTick / s_freq);
113 pkthdr.ts.tv_usec = (curTick / us_freq) % ULL(1000000);
114 pkthdr.caplen = packet->length;
115 pkthdr.len = packet->length;
116 stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
117 stream.write(reinterpret_cast<char *>(packet->data), packet->length);
118 stream.flush();
119 }
120
121 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherDump)
122
123 Param<string> file;
124
125 END_DECLARE_SIM_OBJECT_PARAMS(EtherDump)
126
127 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherDump)
128
129 INIT_PARAM_DFLT(file, "file to dump packets to", "")
130
131 END_INIT_SIM_OBJECT_PARAMS(EtherDump)
132
133 CREATE_SIM_OBJECT(EtherDump)
134 {
135 return new EtherDump(getInstanceName(), file);
136 }
137
138 REGISTER_SIM_OBJECT("EtherDump", EtherDump)