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