etherdump.cc revision 1388
11046SN/A/*
21046SN/A * Copyright (c) 2002-2004 The Regents of The University of Michigan
31762SN/A * All rights reserved.
41046SN/A *
51046SN/A * Redistribution and use in source and binary forms, with or without
61046SN/A * modification, are permitted provided that the following conditions are
71046SN/A * met: redistributions of source code must retain the above copyright
81046SN/A * notice, this list of conditions and the following disclaimer;
91046SN/A * redistributions in binary form must reproduce the above copyright
101046SN/A * notice, this list of conditions and the following disclaimer in the
111046SN/A * documentation and/or other materials provided with the distribution;
121046SN/A * neither the name of the copyright holders nor the names of its
131046SN/A * contributors may be used to endorse or promote products derived from
141046SN/A * this software without specific prior written permission.
151046SN/A *
161046SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171046SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181046SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191046SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201046SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211046SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221046SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231046SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241046SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251046SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261046SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271046SN/A */
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu/* @file
302665Ssaidi@eecs.umich.edu * Simple object for creating a simple pcap style packet trace
311046SN/A */
321530SN/A
332655Sstever@eecs.umich.edu#include <sys/time.h>
342655Sstever@eecs.umich.edu
352655Sstever@eecs.umich.edu#include <algorithm>
362655Sstever@eecs.umich.edu#include <string>
372655Sstever@eecs.umich.edu
381530SN/A#include "base/misc.hh"
391530SN/A#include "base/output.hh"
401530SN/A#include "dev/etherdump.hh"
412667Sstever@eecs.umich.edu#include "sim/builder.hh"
422667Sstever@eecs.umich.edu#include "sim/universe.hh"
431046SN/A
442655Sstever@eecs.umich.eduusing std::string;
452655Sstever@eecs.umich.edu
462655Sstever@eecs.umich.eduEtherDump::EtherDump(const string &name, const string &file, int max)
472655Sstever@eecs.umich.edu    : SimObject(name), stream(file.c_str()), maxlen(max)
482655Sstever@eecs.umich.edu{
491046SN/A}
502655Sstever@eecs.umich.edu
512655Sstever@eecs.umich.edu#define DLT_EN10MB		1       	// Ethernet (10Mb)
522655Sstever@eecs.umich.edu#define TCPDUMP_MAGIC		0xa1b2c3d4
532655Sstever@eecs.umich.edu#define PCAP_VERSION_MAJOR	2
541046SN/A#define PCAP_VERSION_MINOR	4
552655Sstever@eecs.umich.edu
562655Sstever@eecs.umich.edustruct pcap_file_header {
572655Sstever@eecs.umich.edu    uint32_t magic;
582667Sstever@eecs.umich.edu    uint16_t version_major;
592655Sstever@eecs.umich.edu    uint16_t version_minor;
602655Sstever@eecs.umich.edu    int32_t thiszone;		// gmt to local correction
612655Sstever@eecs.umich.edu    uint32_t sigfigs;		// accuracy of timestamps
622655Sstever@eecs.umich.edu    uint32_t snaplen;		// max length saved portion of each pkt
632655Sstever@eecs.umich.edu    uint32_t linktype;		// data link type (DLT_*)
642655Sstever@eecs.umich.edu};
652655Sstever@eecs.umich.edu
662655Sstever@eecs.umich.edustruct pcap_pkthdr {
671046SN/A    uint32_t seconds;
682655Sstever@eecs.umich.edu    uint32_t microseconds;
692655Sstever@eecs.umich.edu    uint32_t caplen;		// length of portion present
702667Sstever@eecs.umich.edu    uint32_t len;		// length this packet (off wire)
711046SN/A};
722655Sstever@eecs.umich.edu
731046SN/Avoid
742655Sstever@eecs.umich.eduEtherDump::init()
752655Sstever@eecs.umich.edu{
761439SN/A    curtime = time(NULL);
771530SN/A    s_freq = ticksPerSecond;
782889Sbinkertn@umich.edu    us_freq = ticksPerSecond / ULL(1000000);
791530SN/A
801439SN/A    struct pcap_file_header hdr;
811858SN/A    hdr.magic = TCPDUMP_MAGIC;
822667Sstever@eecs.umich.edu    hdr.version_major = PCAP_VERSION_MAJOR;
831858SN/A    hdr.version_minor = PCAP_VERSION_MINOR;
842889Sbinkertn@umich.edu
852889Sbinkertn@umich.edu    hdr.thiszone = -5 * 3600;
862889Sbinkertn@umich.edu    hdr.snaplen = 1500;
872889Sbinkertn@umich.edu    hdr.sigfigs = 0;
882889Sbinkertn@umich.edu    hdr.linktype = DLT_EN10MB;
892889Sbinkertn@umich.edu
902889Sbinkertn@umich.edu    stream.write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
912889Sbinkertn@umich.edu
922889Sbinkertn@umich.edu    /*
932889Sbinkertn@umich.edu     * output an empty packet with the current time so that we know
942889Sbinkertn@umich.edu     * when the simulation began.  This allows us to correlate packets
952655Sstever@eecs.umich.edu     * to sim_cycles.
962655Sstever@eecs.umich.edu     */
972667Sstever@eecs.umich.edu    pcap_pkthdr pkthdr;
982889Sbinkertn@umich.edu    pkthdr.seconds = curtime;
992655Sstever@eecs.umich.edu    pkthdr.microseconds = 0;
1002655Sstever@eecs.umich.edu    pkthdr.caplen = 0;
1013869Sbinkertn@umich.edu    pkthdr.len = 0;
1023869Sbinkertn@umich.edu    stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
1033869Sbinkertn@umich.edu
1043869Sbinkertn@umich.edu    stream.flush();
1053869Sbinkertn@umich.edu}
1063869Sbinkertn@umich.edu
1073645Sbinkertn@umich.eduvoid
1083869Sbinkertn@umich.eduEtherDump::dumpPacket(PacketPtr &packet)
1093869Sbinkertn@umich.edu{
1103871Sbinkertn@umich.edu    pcap_pkthdr pkthdr;
1114045Sbinkertn@umich.edu    pkthdr.seconds = curtime + (curTick / s_freq);
1124042Sbinkertn@umich.edu    pkthdr.microseconds = (curTick / us_freq) % ULL(1000000);
1132667Sstever@eecs.umich.edu    pkthdr.caplen = std::min(packet->length, maxlen);
1142655Sstever@eecs.umich.edu    pkthdr.len = packet->length;
1152655Sstever@eecs.umich.edu    stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
1162655Sstever@eecs.umich.edu    stream.write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
1172655Sstever@eecs.umich.edu    stream.flush();
1182655Sstever@eecs.umich.edu}
1192655Sstever@eecs.umich.edu
1202655Sstever@eecs.umich.eduBEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherDump)
1212655Sstever@eecs.umich.edu
1222655Sstever@eecs.umich.edu    Param<string> file;
1232655Sstever@eecs.umich.edu    Param<int> maxlen;
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", "etherdump"),
130    INIT_PARAM_DFLT(maxlen, "max portion of packet data to dump", 96)
131
132END_INIT_SIM_OBJECT_PARAMS(EtherDump)
133
134CREATE_SIM_OBJECT(EtherDump)
135{
136    return new EtherDump(getInstanceName(), simout.resolve(file), maxlen);
137}
138
139REGISTER_SIM_OBJECT("EtherDump", EtherDump)
140