etherdump.cc revision 11359
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A *
282665SN/A * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A/* @file
322SN/A * Simple object for creating a simple pcap style packet trace
332SN/A */
3411263Sandreas.sandberg@arm.com#include "dev/net/etherdump.hh"
352SN/A
362SN/A#include <sys/time.h>
372SN/A
381015SN/A#include <algorithm>
392SN/A#include <string>
402SN/A
41599SN/A#include "base/misc.hh"
421388SN/A#include "base/output.hh"
434167SN/A#include "sim/core.hh"
442SN/A
452SN/Ausing std::string;
462SN/A
475034SN/AEtherDump::EtherDump(const Params *p)
4811359Sandreas@sandberg.pp.se    : SimObject(p), stream(simout.create(p->file, true)->stream()),
495034SN/A      maxlen(p->maxlen)
502SN/A{
512SN/A}
522SN/A
535543SN/A#define DLT_EN10MB              1               // Ethernet (10Mb)
545543SN/A#define TCPDUMP_MAGIC           0xa1b2c3d4
555543SN/A#define PCAP_VERSION_MAJOR      2
565543SN/A#define PCAP_VERSION_MINOR      4
572SN/A
582SN/Astruct pcap_file_header {
592SN/A    uint32_t magic;
602SN/A    uint16_t version_major;
612SN/A    uint16_t version_minor;
625543SN/A    int32_t thiszone;           // gmt to local correction
635543SN/A    uint32_t sigfigs;           // accuracy of timestamps
645543SN/A    uint32_t snaplen;           // max length saved portion of each pkt
655543SN/A    uint32_t linktype;          // data link type (DLT_*)
662SN/A};
672SN/A
682SN/Astruct pcap_pkthdr {
69599SN/A    uint32_t seconds;
70599SN/A    uint32_t microseconds;
715543SN/A    uint32_t caplen;            // length of portion present
725543SN/A    uint32_t len;               // length this packet (off wire)
732SN/A};
742SN/A
752SN/Avoid
762SN/AEtherDump::init()
772SN/A{
782SN/A    struct pcap_file_header hdr;
792SN/A    hdr.magic = TCPDUMP_MAGIC;
802SN/A    hdr.version_major = PCAP_VERSION_MAJOR;
812SN/A    hdr.version_minor = PCAP_VERSION_MINOR;
822SN/A
835878SN/A    hdr.thiszone = 0;
842SN/A    hdr.snaplen = 1500;
852SN/A    hdr.sigfigs = 0;
862SN/A    hdr.linktype = DLT_EN10MB;
872SN/A
885402SN/A    stream->write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
892SN/A
905402SN/A    stream->flush();
912SN/A}
922SN/A
932SN/Avoid
942566SN/AEtherDump::dumpPacket(EthPacketPtr &packet)
952SN/A{
962SN/A    pcap_pkthdr pkthdr;
977823SN/A    pkthdr.seconds = curTick() / SimClock::Int::s;
987823SN/A    pkthdr.microseconds = (curTick() / SimClock::Int::us) % ULL(1000000);
991015SN/A    pkthdr.caplen = std::min(packet->length, maxlen);
1002SN/A    pkthdr.len = packet->length;
1015402SN/A    stream->write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
1025402SN/A    stream->write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
1035402SN/A    stream->flush();
1042SN/A}
1052SN/A
1064762SN/AEtherDump *
1074762SN/AEtherDumpParams::create()
1082SN/A{
1095034SN/A    return new EtherDump(this);
1102SN/A}
111