etherdump.cc revision 599
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 "base/misc.hh" 38#include "dev/etherdump.hh" 39#include "sim/builder.hh" 40#include "sim/universe.hh" 41 42using std::string; 43 44EtherDump::EtherDump(const string &name, const string &file) 45 : SimObject(name) 46{ 47 if (!file.empty()) 48 stream.open(file.c_str()); 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 56struct 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 66struct 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 73void 74EtherDump::init() 75{ 76 if (!stream.is_open()) 77 return; 78 79 curtime = time(NULL); 80 s_freq = ticksPerSecond; 81 us_freq = ticksPerSecond / ULL(1000000); 82 83 struct pcap_file_header hdr; 84 hdr.magic = TCPDUMP_MAGIC; 85 hdr.version_major = PCAP_VERSION_MAJOR; 86 hdr.version_minor = PCAP_VERSION_MINOR; 87 88 hdr.thiszone = -5 * 3600; 89 hdr.snaplen = 1500; 90 hdr.sigfigs = 0; 91 hdr.linktype = DLT_EN10MB; 92 93 stream.write(reinterpret_cast<char *>(&hdr), sizeof(hdr)); 94 95 /* 96 * output an empty packet with the current time so that we know 97 * when the simulation began. This allows us to correlate packets 98 * to sim_cycles. 99 */ 100 pcap_pkthdr pkthdr; 101 pkthdr.seconds = curtime; 102 pkthdr.microseconds = 0; 103 pkthdr.caplen = 0; 104 pkthdr.len = 0; 105 stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr)); 106 107 stream.flush(); 108} 109 110void 111EtherDump::dumpPacket(PacketPtr &packet) 112{ 113 pcap_pkthdr pkthdr; 114 pkthdr.seconds = curtime + (curTick / s_freq); 115 pkthdr.microseconds = (curTick / us_freq) % ULL(1000000); 116 pkthdr.caplen = packet->length; 117 pkthdr.len = packet->length; 118 stream.write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr)); 119 stream.write(reinterpret_cast<char *>(packet->data), packet->length); 120 stream.flush(); 121} 122 123BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherDump) 124 125 Param<string> file; 126 127END_DECLARE_SIM_OBJECT_PARAMS(EtherDump) 128 129BEGIN_INIT_SIM_OBJECT_PARAMS(EtherDump) 130 131 INIT_PARAM(file, "file to dump packets to") 132 133END_INIT_SIM_OBJECT_PARAMS(EtherDump) 134 135CREATE_SIM_OBJECT(EtherDump) 136{ 137 string filename; 138 if (file.isValid()) { 139 filename = file; 140 141 if (filename[0] != '/' && !outputDirectory.empty()) 142 filename = outputDirectory + filename; 143 } else { 144 if (outputDirectory.empty()) { 145 filename = "etherdump"; 146 } else { 147 filename = outputDirectory + "etherdump"; 148 } 149 } 150 151 return new EtherDump(getInstanceName(), filename); 152} 153 154REGISTER_SIM_OBJECT("EtherDump", EtherDump) 155