etherdump.cc (11263:8dcc6b40f164) etherdump.cc (11359:b0b976a1ceda)
1/*
2 * Copyright (c) 2002-2005 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 * Authors: Nathan Binkert
29 */
30
31/* @file
32 * Simple object for creating a simple pcap style packet trace
33 */
34#include "dev/net/etherdump.hh"
35
36#include <sys/time.h>
37
38#include <algorithm>
39#include <string>
40
41#include "base/misc.hh"
42#include "base/output.hh"
43#include "sim/core.hh"
44
45using std::string;
46
47EtherDump::EtherDump(const Params *p)
1/*
2 * Copyright (c) 2002-2005 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 * Authors: Nathan Binkert
29 */
30
31/* @file
32 * Simple object for creating a simple pcap style packet trace
33 */
34#include "dev/net/etherdump.hh"
35
36#include <sys/time.h>
37
38#include <algorithm>
39#include <string>
40
41#include "base/misc.hh"
42#include "base/output.hh"
43#include "sim/core.hh"
44
45using std::string;
46
47EtherDump::EtherDump(const Params *p)
48 : SimObject(p), stream(simout.create(p->file, true)),
48 : SimObject(p), stream(simout.create(p->file, true)->stream()),
49 maxlen(p->maxlen)
50{
51}
52
53#define DLT_EN10MB 1 // Ethernet (10Mb)
54#define TCPDUMP_MAGIC 0xa1b2c3d4
55#define PCAP_VERSION_MAJOR 2
56#define PCAP_VERSION_MINOR 4
57
58struct pcap_file_header {
59 uint32_t magic;
60 uint16_t version_major;
61 uint16_t version_minor;
62 int32_t thiszone; // gmt to local correction
63 uint32_t sigfigs; // accuracy of timestamps
64 uint32_t snaplen; // max length saved portion of each pkt
65 uint32_t linktype; // data link type (DLT_*)
66};
67
68struct pcap_pkthdr {
69 uint32_t seconds;
70 uint32_t microseconds;
71 uint32_t caplen; // length of portion present
72 uint32_t len; // length this packet (off wire)
73};
74
75void
76EtherDump::init()
77{
78 struct pcap_file_header hdr;
79 hdr.magic = TCPDUMP_MAGIC;
80 hdr.version_major = PCAP_VERSION_MAJOR;
81 hdr.version_minor = PCAP_VERSION_MINOR;
82
83 hdr.thiszone = 0;
84 hdr.snaplen = 1500;
85 hdr.sigfigs = 0;
86 hdr.linktype = DLT_EN10MB;
87
88 stream->write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
89
90 stream->flush();
91}
92
93void
94EtherDump::dumpPacket(EthPacketPtr &packet)
95{
96 pcap_pkthdr pkthdr;
97 pkthdr.seconds = curTick() / SimClock::Int::s;
98 pkthdr.microseconds = (curTick() / SimClock::Int::us) % ULL(1000000);
99 pkthdr.caplen = std::min(packet->length, maxlen);
100 pkthdr.len = packet->length;
101 stream->write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
102 stream->write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
103 stream->flush();
104}
105
106EtherDump *
107EtherDumpParams::create()
108{
109 return new EtherDump(this);
110}
49 maxlen(p->maxlen)
50{
51}
52
53#define DLT_EN10MB 1 // Ethernet (10Mb)
54#define TCPDUMP_MAGIC 0xa1b2c3d4
55#define PCAP_VERSION_MAJOR 2
56#define PCAP_VERSION_MINOR 4
57
58struct pcap_file_header {
59 uint32_t magic;
60 uint16_t version_major;
61 uint16_t version_minor;
62 int32_t thiszone; // gmt to local correction
63 uint32_t sigfigs; // accuracy of timestamps
64 uint32_t snaplen; // max length saved portion of each pkt
65 uint32_t linktype; // data link type (DLT_*)
66};
67
68struct pcap_pkthdr {
69 uint32_t seconds;
70 uint32_t microseconds;
71 uint32_t caplen; // length of portion present
72 uint32_t len; // length this packet (off wire)
73};
74
75void
76EtherDump::init()
77{
78 struct pcap_file_header hdr;
79 hdr.magic = TCPDUMP_MAGIC;
80 hdr.version_major = PCAP_VERSION_MAJOR;
81 hdr.version_minor = PCAP_VERSION_MINOR;
82
83 hdr.thiszone = 0;
84 hdr.snaplen = 1500;
85 hdr.sigfigs = 0;
86 hdr.linktype = DLT_EN10MB;
87
88 stream->write(reinterpret_cast<char *>(&hdr), sizeof(hdr));
89
90 stream->flush();
91}
92
93void
94EtherDump::dumpPacket(EthPacketPtr &packet)
95{
96 pcap_pkthdr pkthdr;
97 pkthdr.seconds = curTick() / SimClock::Int::s;
98 pkthdr.microseconds = (curTick() / SimClock::Int::us) % ULL(1000000);
99 pkthdr.caplen = std::min(packet->length, maxlen);
100 pkthdr.len = packet->length;
101 stream->write(reinterpret_cast<char *>(&pkthdr), sizeof(pkthdr));
102 stream->write(reinterpret_cast<char *>(packet->data), pkthdr.caplen);
103 stream->flush();
104}
105
106EtherDump *
107EtherDumpParams::create()
108{
109 return new EtherDump(this);
110}