etherdump.cc revision 2
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2003 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com */
2812855Sgabeblack@google.com
2912855Sgabeblack@google.com/* @file
3012855Sgabeblack@google.com * Simple object for creating a simple pcap style packet trace
3112855Sgabeblack@google.com */
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com#include <sys/time.h>
3412855Sgabeblack@google.com
3512855Sgabeblack@google.com#include <string>
3612855Sgabeblack@google.com
3712855Sgabeblack@google.com#include "universe.hh"
3812855Sgabeblack@google.com#include "etherdump.hh"
3912855Sgabeblack@google.com#include "universe.hh"
4012855Sgabeblack@google.com
4112855Sgabeblack@google.comusing std::string;
4212855Sgabeblack@google.com
4312855Sgabeblack@google.comEtherDump::EtherDump(const string &name, const string &file)
4412855Sgabeblack@google.com    : SimObject(name)
4512855Sgabeblack@google.com{
4612855Sgabeblack@google.com    if (!file.empty()) {
4712855Sgabeblack@google.com        stream.open(file.c_str());
4812855Sgabeblack@google.com        if (stream.is_open())
4912855Sgabeblack@google.com            init();
5012855Sgabeblack@google.com    }
5112855Sgabeblack@google.com}
5212855Sgabeblack@google.com
5312855Sgabeblack@google.com#define DLT_EN10MB		1       	// Ethernet (10Mb)
5412855Sgabeblack@google.com#define TCPDUMP_MAGIC		0xa1b2c3d4
5512855Sgabeblack@google.com#define PCAP_VERSION_MAJOR	2
5612855Sgabeblack@google.com#define PCAP_VERSION_MINOR	4
5712855Sgabeblack@google.com
5812855Sgabeblack@google.comstruct pcap_file_header {
5912855Sgabeblack@google.com    uint32_t magic;
6012855Sgabeblack@google.com    uint16_t version_major;
6112855Sgabeblack@google.com    uint16_t version_minor;
6212855Sgabeblack@google.com    int32_t thiszone;		// gmt to local correction
6312855Sgabeblack@google.com    uint32_t sigfigs;		// accuracy of timestamps
6412855Sgabeblack@google.com    uint32_t snaplen;		// max length saved portion of each pkt
6512855Sgabeblack@google.com    uint32_t linktype;		// data link type (DLT_*)
6612855Sgabeblack@google.com};
6712855Sgabeblack@google.com
6812855Sgabeblack@google.comstruct pcap_pkthdr {
6912855Sgabeblack@google.com    struct timeval ts;		// time stamp
7012855Sgabeblack@google.com    uint32_t caplen;		// length of portion present
7112855Sgabeblack@google.com    uint32_t len;		// length this packet (off wire)
7212855Sgabeblack@google.com};
7312855Sgabeblack@google.com
7412855Sgabeblack@google.comvoid
7512855Sgabeblack@google.comEtherDump::init()
7612855Sgabeblack@google.com{
7712855Sgabeblack@google.com    curtime = time(NULL);
7812855Sgabeblack@google.com    s_freq = ticksPerSecond;
7912855Sgabeblack@google.com    us_freq = ticksPerSecond / ULL(1000000);
8012855Sgabeblack@google.com
8112855Sgabeblack@google.com    struct pcap_file_header hdr;
8212855Sgabeblack@google.com    hdr.magic = TCPDUMP_MAGIC;
8312855Sgabeblack@google.com    hdr.version_major = PCAP_VERSION_MAJOR;
8412855Sgabeblack@google.com    hdr.version_minor = PCAP_VERSION_MINOR;
8512855Sgabeblack@google.com
8612855Sgabeblack@google.com    hdr.thiszone = -5 * 3600;
8712855Sgabeblack@google.com    hdr.snaplen = 1500;
8812855Sgabeblack@google.com    hdr.sigfigs = 0;
8912855Sgabeblack@google.com    hdr.linktype = DLT_EN10MB;
9012855Sgabeblack@google.com
9112855Sgabeblack@google.com    stream.write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
9212855Sgabeblack@google.com
9312855Sgabeblack@google.com    /*
9412855Sgabeblack@google.com     * output an empty packet with the current time so that we know
9512855Sgabeblack@google.com     * when the simulation began.  This allows us to correlate packets
9612855Sgabeblack@google.com     * to sim_cycles.
9712855Sgabeblack@google.com     */
9812855Sgabeblack@google.com    pcap_pkthdr pkthdr;
9912855Sgabeblack@google.com    pkthdr.ts.tv_sec = curtime;
10012855Sgabeblack@google.com    pkthdr.ts.tv_usec = 0;
10112855Sgabeblack@google.com    pkthdr.caplen = 0;
10212855Sgabeblack@google.com    pkthdr.len = 0;
10312855Sgabeblack@google.com    stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
10412855Sgabeblack@google.com
10512855Sgabeblack@google.com    stream.flush();
10612855Sgabeblack@google.com}
107
108void
109EtherDump::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
121BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherDump)
122
123    Param<string> file;
124
125END_DECLARE_SIM_OBJECT_PARAMS(EtherDump)
126
127BEGIN_INIT_SIM_OBJECT_PARAMS(EtherDump)
128
129    INIT_PARAM_DFLT(file, "file to dump packets to", "")
130
131END_INIT_SIM_OBJECT_PARAMS(EtherDump)
132
133CREATE_SIM_OBJECT(EtherDump)
134{
135    return new EtherDump(getInstanceName(), file);
136}
137
138REGISTER_SIM_OBJECT("EtherDump", EtherDump)
139