New directory structure:
[gem5.git] / src / dev / etherdump.cc
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
33 #include <sys/time.h>
34
35 #include <algorithm>
36 #include <string>
37
38 #include "base/misc.hh"
39 #include "base/output.hh"
40 #include "dev/etherdump.hh"
41 #include "sim/builder.hh"
42 #include "sim/root.hh"
43
44 using std::string;
45
46 EtherDump::EtherDump(const string &name, const string &file, int max)
47 : SimObject(name), stream(file.c_str()), maxlen(max)
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 curtime = time(NULL);
77 struct pcap_file_header hdr;
78 hdr.magic = TCPDUMP_MAGIC;
79 hdr.version_major = PCAP_VERSION_MAJOR;
80 hdr.version_minor = PCAP_VERSION_MINOR;
81
82 hdr.thiszone = -5 * 3600;
83 hdr.snaplen = 1500;
84 hdr.sigfigs = 0;
85 hdr.linktype = DLT_EN10MB;
86
87 stream.write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
88
89 /*
90 * output an empty packet with the current time so that we know
91 * when the simulation began. This allows us to correlate packets
92 * to sim_cycles.
93 */
94 pcap_pkthdr pkthdr;
95 pkthdr.seconds = curtime;
96 pkthdr.microseconds = 0;
97 pkthdr.caplen = 0;
98 pkthdr.len = 0;
99 stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
100
101 stream.flush();
102 }
103
104 void
105 EtherDump::dumpPacket(EthPacketPtr &packet)
106 {
107 pcap_pkthdr pkthdr;
108 pkthdr.seconds = curtime + (curTick / Clock::Int::s);
109 pkthdr.microseconds = (curTick / Clock::Int::us) % ULL(1000000);
110 pkthdr.caplen = std::min(packet->length, maxlen);
111 pkthdr.len = packet->length;
112 stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
113 stream.write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
114 stream.flush();
115 }
116
117 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherDump)
118
119 Param<string> file;
120 Param<int> maxlen;
121
122 END_DECLARE_SIM_OBJECT_PARAMS(EtherDump)
123
124 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherDump)
125
126 INIT_PARAM(file, "file to dump packets to"),
127 INIT_PARAM(maxlen, "max portion of packet data to dump")
128
129 END_INIT_SIM_OBJECT_PARAMS(EtherDump)
130
131 CREATE_SIM_OBJECT(EtherDump)
132 {
133 return new EtherDump(getInstanceName(), simout.resolve(file), maxlen);
134 }
135
136 REGISTER_SIM_OBJECT("EtherDump", EtherDump)