Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improvements of pcap handling
When writing a Header to pcap, it should not be modified and thus be
passed as a constant.

This commits adds a sink to export Header along the packet. This is
done for performance reasons, to prevent the (costly) creation of a new packet with the header.

I've also modified some tests to remove unnecessary data copy via using
the overload that accepts a packet as a parameter.
  • Loading branch information
teto committed Jul 21, 2015
commit 17edea9701a0a32bf854cbcec63a82b3d5be9ae7
7 changes: 7 additions & 0 deletions src/network/helper/trace-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ PcapHelper::DefaultSink (Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
file->Write (Simulator::Now (), p);
}

void
PcapHelper::SinkWithHeader (Ptr<PcapFileWrapper> file, const Header &header, Ptr<const Packet> p)
{
NS_LOG_FUNCTION (file << p);
file->Write (Simulator::Now (), header, p);
}

AsciiTraceHelper::AsciiTraceHelper ()
{
NS_LOG_FUNCTION_NOARGS ();
Expand Down
16 changes: 15 additions & 1 deletion src/network/helper/trace-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ class PcapHelper
DLT_PPP = 9,
DLT_RAW = 101,
DLT_IEEE802_11 = 105,
DLT_LINUX_SSL = 113,
DLT_PRISM_HEADER = 119,
DLT_IEEE802_11_RADIO = 127,
DLT_IEEE802_15_4 = 195
DLT_IEEE802_15_4 = 195,
DLT_NETLINK = 253
};

/**
Expand Down Expand Up @@ -121,6 +123,18 @@ class PcapHelper
* @param p the packet to write
*/
static void DefaultSink (Ptr<PcapFileWrapper> file, Ptr<const Packet> p);

/**
* This trace sink passes a header separately from the packet to prevent creating a new packet
* (for performance reasons)
*
* @param file the file to write to
* @param header header of the packet
* @param p the packet to write
*
* @see DefaultSink
*/
static void SinkWithHeader (Ptr<PcapFileWrapper> file, const Header& header, Ptr<const Packet> p);
};

template <typename T> void
Expand Down
2 changes: 1 addition & 1 deletion src/network/utils/pcap-file-wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ PcapFileWrapper::Write (Time t, Ptr<const Packet> p)
}

void
PcapFileWrapper::Write (Time t, Header &header, Ptr<const Packet> p)
PcapFileWrapper::Write (Time t, const Header &header, Ptr<const Packet> p)
{
NS_LOG_FUNCTION (this << t << &header << p);
uint64_t current = t.GetMicroSeconds ();
Expand Down
2 changes: 1 addition & 1 deletion src/network/utils/pcap-file-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class PcapFileWrapper : public Object
* \param p Packet to write to the pcap file.
*
*/
void Write (Time t, Header &header, Ptr<const Packet> p);
void Write (Time t, const Header &header, Ptr<const Packet> p);

/**
* \brief Write the provided data buffer to the pcap file.
Expand Down
2 changes: 1 addition & 1 deletion src/network/utils/pcap-file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ PcapFile::Write (uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p)
}

void
PcapFile::Write (uint32_t tsSec, uint32_t tsUsec, Header &header, Ptr<const Packet> p)
PcapFile::Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr<const Packet> p)
{
NS_LOG_FUNCTION (this << tsSec << tsUsec << &header << p);
uint32_t headerSize = header.GetSerializedSize ();
Expand Down
8 changes: 6 additions & 2 deletions src/network/utils/pcap-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PcapFile
* PCAP_PPP, PCAP_80211, etc. If you are storing different kinds of packet
* data, such as naked TCP headers, you are at liberty to locally define your
* own data link types. According to the pcap-linktype man page, "well-known"
* pcap linktypes range from 0 to 177. If you use a large random number for
* pcap linktypes range from 0 to 263. If you use a large random number for
* your type, chances are small for a collision.
*
* \param snapLen An optional maximum size for packets written to the file.
Expand Down Expand Up @@ -147,7 +147,7 @@ class PcapFile
* \param p Packet to write
*
*/
void Write (uint32_t tsSec, uint32_t tsUsec, Header &header, Ptr<const Packet> p);
void Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr<const Packet> p);


/**
Expand Down Expand Up @@ -337,6 +337,10 @@ class PcapFile
void WriteFileHeader (void);
/**
* \brief Write a Pcap packet header
*
* The pcap header has a fixed length of 24 bytes. The last 4 bytes
* represent the link-layer type
*
* \param tsSec Time stamp (seconds part)
* \param tsUsec Time stamp (microseconds part)
* \param totalLen total packet length
Expand Down
9 changes: 2 additions & 7 deletions src/test/ns3tcp/ns3tcp-interop-test-suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,10 @@ Ns3TcpInteroperabilityTestCase::Ipv4L3Tx (std::string context, Ptr<const Packet>
Time tNow = Simulator::Now ();
int64_t tMicroSeconds = tNow.GetMicroSeconds ();

uint32_t size = p->GetSize ();
uint8_t *buf = new uint8_t[size];
p->CopyData (buf, size);

m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000),
uint32_t (tMicroSeconds % 1000000),
buf,
size);
delete [] buf;
p
);
}
else
{
Expand Down
8 changes: 2 additions & 6 deletions src/test/ns3tcp/ns3tcp-loss-test-suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,11 @@ Ns3TcpLossTestCase::Ipv4L3Tx (std::string context, Ptr<const Packet> packet, Ptr
Time tNow = Simulator::Now ();
int64_t tMicroSeconds = tNow.GetMicroSeconds ();

uint32_t size = p->GetSize ();
uint8_t *buf = new uint8_t[size];
p->CopyData (buf, size);

m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000),
uint32_t (tMicroSeconds % 1000000),
buf,
size);
delete [] buf;
p
);
}
else
{
Expand Down
8 changes: 1 addition & 7 deletions src/test/ns3tcp/ns3tcp-state-test-suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,9 @@ Ns3TcpStateTestCase::Ipv4L3Tx (std::string context, Ptr<const Packet> packet, Pt
Time tNow = Simulator::Now ();
int64_t tMicroSeconds = tNow.GetMicroSeconds ();

uint32_t size = p->GetSize ();
uint8_t *buf = new uint8_t[size];
p->CopyData (buf, size);

m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000),
uint32_t (tMicroSeconds % 1000000),
buf,
size);
delete [] buf;
p);
}
else
{
Expand Down