ns3::Packet Class Reference
[Packet]

network packets More...

#include <packet.h>

Collaboration diagram for ns3::Packet:
Collaboration graph
[legend]

List of all members.

Public Member Functions

void Ref (void) const
void Unref (void) const
Ptr< PacketCopy (void) const
 Packet ()
 Packet (const Packet &o)
Packetoperator= (const Packet &o)
 Packet (uint32_t size)
 Packet (uint8_t const *buffer, uint32_t size)
Ptr< PacketCreateFragment (uint32_t start, uint32_t length) const
uint32_t GetSize (void) const
void AddHeader (const Header &header)
uint32_t RemoveHeader (Header &header)
uint32_t PeekHeader (Header &header) const
void AddTrailer (const Trailer &trailer)
uint32_t RemoveTrailer (Trailer &trailer)
uint32_t PeekTrailer (Trailer &trailer)
void PrintTags (std::ostream &os) const
void AddAtEnd (Ptr< const Packet > packet)
void AddPaddingAtEnd (uint32_t size)
void RemoveAtEnd (uint32_t size)
void RemoveAtStart (uint32_t size)
uint8_t const * PeekData (void) const
uint32_t CopyData (uint8_t *buffer, uint32_t size) const
uint32_t GetUid (void) const
void Print (std::ostream &os) const
PacketMetadata::ItemIterator BeginItem (void) const
Buffer Serialize (void) const
void Deserialize (Buffer buffer)
void AddTag (const Tag &tag) const
TagIterator GetTagIterator (void) const
bool FindFirstMatchingTag (Tag &tag) const
void RemoveAllTags (void)

Static Public Member Functions

static void EnableMetadata (void) NS_DEPRECATED
static void EnablePrinting (void)
static void EnableChecking (void)

Private Member Functions

 Packet (const Buffer &buffer, const TagList &tagList, const PacketMetadata &metadata)

Private Attributes

Buffer m_buffer
TagList m_tagList
PacketMetadata m_metadata
uint32_t m_refCount

Static Private Attributes

static uint32_t m_globalUid = 0

Detailed Description

network packets

Each network packet contains a byte buffer, a set of tags, and metadata.

Implementing a new type of Header or Trailer for a new protocol is pretty easy and is a matter of creating a subclass of the ns3::Header or of the ns3::Trailer base class, and implementing the methods described in their respective API documentation.

Implementing a new type of Tag requires roughly the same amount of work and this work is described in the ns3::Tag API documentation.

The performance aspects of the Packet API are discussed in Packet Performance

Definition at line 140 of file packet.h.


Constructor & Destructor Documentation

ns3::Packet::Packet (  ) 

Create an empty packet with a new uid (as returned by getUid).

Definition at line 103 of file packet.cc.

References m_globalUid.

Referenced by Copy(), and CreateFragment().

ns3::Packet::Packet ( const Packet o  ) 

Definition at line 112 of file packet.cc.

ns3::Packet::Packet ( uint32_t  size  ) 

Create a packet with a zero-filled payload. The memory necessary for the payload is not allocated: it will be allocated at any later point if you attempt to fragment this packet or to access the zero-filled bytes. The packet is allocated with a new uid (as returned by getUid).

Parameters:
size the size of the zero-filled payload

Definition at line 132 of file packet.cc.

References m_globalUid.

ns3::Packet::Packet ( uint8_t const *  buffer,
uint32_t  size 
)

Create a packet with payload filled with the content of this buffer. The input data is copied: the input buffer is untouched.

Parameters:
buffer the data to store in the packet.
size the size of the input buffer.

Definition at line 140 of file packet.cc.

References ns3::Buffer::AddAtStart(), ns3::Buffer::Begin(), m_buffer, m_globalUid, and ns3::Buffer::Iterator::Write().

ns3::Packet::Packet ( const Buffer buffer,
const TagList tagList,
const PacketMetadata metadata 
) [private]

Definition at line 152 of file packet.cc.


Member Function Documentation

void ns3::Packet::AddAtEnd ( Ptr< const Packet packet  ) 

Concatenate the input packet at the end of the current packet. This does not alter the uid of either packet.

Parameters:
packet packet to concatenate

Definition at line 243 of file packet.cc.

References ns3::TagList::Add(), ns3::PacketMetadata::AddAtEnd(), ns3::TagList::AddAtEnd(), ns3::Buffer::AddAtEnd(), ns3::TagList::AddAtStart(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), m_buffer, m_metadata, m_tagList, and NS_LOG_FUNCTION.

void ns3::Packet::AddHeader ( const Header header  ) 

Add header to this packet. This method invokes the Header::GetSerializedSize and Header::Serialize methods to reserve space in the buffer and request the header to serialize itself in the packet buffer.

Parameters:
header a reference to the header to add to this packet.

Definition at line 179 of file packet.cc.

References ns3::TagList::AddAtStart(), ns3::Buffer::AddAtStart(), ns3::PacketMetadata::AddHeader(), ns3::Buffer::Begin(), ns3::Buffer::GetCurrentStartOffset(), ns3::Header::GetSerializedSize(), m_buffer, m_metadata, m_tagList, NS_LOG_FUNCTION, and ns3::Header::Serialize().

Referenced by ns3::OlsrHeaderTest::RunTests().

void ns3::Packet::AddPaddingAtEnd ( uint32_t  size  ) 
void ns3::Packet::AddTag ( const Tag tag  )  const
Parameters:
tag the new tag to add to this packet

Tag each byte included in this packet with the new tag.

Note that adding a tag is a const operation which is pretty un-intuitive. The rationale is that the content and behavior of a packet is _not_ changed when a tag is added to a packet: any code which was not aware of the new tag is going to work just the same if the new tag is added. The real reason why adding a tag was made a const operation is to allow a trace sink which gets a packet to tag the packet, even if the packet is const (and most trace sources should use const packets because it would be totally evil to allow a trace sink to modify the content of a packet).

Definition at line 547 of file packet.cc.

References ns3::TagList::Add(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), ns3::ObjectBase::GetInstanceTypeId(), ns3::Tag::GetSerializedSize(), m_buffer, m_tagList, NS_LOG_FUNCTION, and ns3::Tag::Serialize().

Referenced by ns3::PacketTest::RunTests().

void ns3::Packet::AddTrailer ( const Trailer trailer  ) 

Add trailer to this packet. This method invokes the Trailer::GetSerializedSize and Trailer::Serialize methods to reserve space in the buffer and request the trailer to serialize itself in the packet buffer.

Parameters:
trailer a reference to the trailer to add to this packet.

Definition at line 210 of file packet.cc.

References ns3::TagList::AddAtEnd(), ns3::Buffer::AddAtEnd(), ns3::PacketMetadata::AddTrailer(), ns3::Buffer::End(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), ns3::Trailer::GetSerializedSize(), m_buffer, m_metadata, m_tagList, NS_LOG_FUNCTION, and ns3::Trailer::Serialize().

PacketMetadata::ItemIterator ns3::Packet::BeginItem ( void   )  const

Definition at line 466 of file packet.cc.

References ns3::PacketMetadata::BeginItem(), m_buffer, and m_metadata.

Ptr< Packet > ns3::Packet::Copy ( void   )  const

Definition at line 95 of file packet.cc.

References Packet().

uint32_t ns3::Packet::CopyData ( uint8_t *  buffer,
uint32_t  size 
) const
Parameters:
buffer a pointer to a byte buffer where the packet data should be copied.
size the size of the byte buffer.
Returns:
the number of bytes read from the packet

No more than size bytes will be copied by this function.

Definition at line 300 of file packet.cc.

References ns3::Buffer::Begin(), ns3::Buffer::Iterator::IsEnd(), m_buffer, and ns3::Buffer::Iterator::ReadU8().

Ptr< Packet > ns3::Packet::CreateFragment ( uint32_t  start,
uint32_t  length 
) const

Create a new packet which contains a fragment of the original packet. The returned packet shares the same uid as this packet.

Parameters:
start offset from start of packet to start of fragment to create
length length of fragment to create
Returns:
a fragment of the original packet

Definition at line 160 of file packet.cc.

References ns3::PacketMetadata::CreateFragment(), ns3::Buffer::CreateFragment(), ns3::Buffer::GetSize(), m_buffer, m_metadata, m_tagList, NS_ASSERT, NS_LOG_FUNCTION, and Packet().

void ns3::Packet::Deserialize ( Buffer  buffer  ) 
Parameters:
buffer a byte buffer

This method reads a byte buffer as created by Packet::Serialize and restores the state of the Packet to what it was prior to calling Serialize.

This method will trigger calls to the Deserialize method of each tag stored in this packet.

This method will typically be used by parallel simulations where the simulated system is partitioned and each partition runs on a different CPU.

Definition at line 521 of file packet.cc.

References ns3::Buffer::Begin(), ns3::PacketMetadata::Deserialize(), ns3::Buffer::GetSize(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::Buffer::Iterator::ReadU32(), ns3::Buffer::RemoveAtEnd(), and ns3::Buffer::RemoveAtStart().

void ns3::Packet::EnableChecking ( void   )  [static]

The packet metadata is also used to perform extensive sanity checks at runtime when performing operations on a Packet. For example, this metadata is used to verify that when you remove a header from a packet, this same header was actually present at the front of the packet. These errors will be detected and will abort the program.

Definition at line 486 of file packet.cc.

References NS_LOG_FUNCTION_NOARGS.

Referenced by EnableMetadata().

void ns3::Packet::EnableMetadata ( void   )  [static]

Definition at line 472 of file packet.cc.

References EnableChecking(), and NS_LOG_FUNCTION_NOARGS.

void ns3::Packet::EnablePrinting ( void   )  [static]

By default, packets do not keep around enough metadata to perform the operations requested by the Print methods. If you want to be able to invoke any of the two Print methods, you need to invoke this method at least once during the simulation setup and before any packet is created.

Definition at line 479 of file packet.cc.

References ns3::PacketMetadata::Enable(), and NS_LOG_FUNCTION_NOARGS.

Referenced by ns3::YansWifiPhyHelper::EnableAscii(), ns3::Ns2ExtWifiPhyHelper::EnableAscii(), and ns3::EmuHelper::EnableAscii().

bool ns3::Packet::FindFirstMatchingTag ( Tag tag  )  const
Parameters:
tag the tag to search in this packet
Returns:
true if the requested tag type was found, false otherwise.

If the requested tag type is found, it is copied in the user's provided tag instance.

Definition at line 563 of file packet.cc.

References ns3::ObjectBase::GetInstanceTypeId(), ns3::TagIterator::Item::GetTag(), GetTagIterator(), ns3::TagIterator::Item::GetTypeId(), ns3::TagIterator::HasNext(), and ns3::TagIterator::Next().

uint32_t ns3::Packet::GetSize ( void   )  const
Returns:
the size in bytes of the packet (including the zero-filled initial payload)

Definition at line 173 of file packet.cc.

References ns3::Buffer::GetSize(), and m_buffer.

Referenced by ns3::OlsrHeaderTest::RunTests().

TagIterator ns3::Packet::GetTagIterator ( void   )  const
Returns:
an iterator over the set of tags included in this packet.

Definition at line 557 of file packet.cc.

References ns3::TagList::Begin(), ns3::Buffer::GetCurrentEndOffset(), ns3::Buffer::GetCurrentStartOffset(), m_buffer, and m_tagList.

Referenced by FindFirstMatchingTag(), and PrintTags().

uint32_t ns3::Packet::GetUid ( void   )  const

A packet is allocated a new uid when it is created empty or with zero-filled payload.

Note: This uid is an internal uid and cannot be counted on to provide an accurate counter of how many "simulated packets" of a particular protocol are in the system. It is not trivial to make this uid into such a counter, because of questions such as what should the uid be when the packet is sent over broadcast media, or when fragmentation occurs. If a user wants to trace actual packet counts, he or she should look at e.g. the IP ID field or transport sequence numbers, or other packet or frame counters at other protocol layers.

Returns:
an integer identifier which uniquely identifies this packet.

Definition at line 313 of file packet.cc.

References ns3::PacketMetadata::GetUid(), and m_metadata.

Packet & ns3::Packet::operator= ( const Packet o  ) 

Definition at line 120 of file packet.cc.

References m_buffer, m_metadata, and m_tagList.

uint8_t const * ns3::Packet::PeekData ( void   )  const

If you try to change the content of the buffer returned by this method, you will die.

Returns:
a pointer to the internal buffer of the packet.

Definition at line 294 of file packet.cc.

References m_buffer, and ns3::Buffer::PeekData().

uint32_t ns3::Packet::PeekHeader ( Header header  )  const

Deserialize but does _not_ remove the header from the internal buffer. This method invokes Header::Deserialize.

Parameters:
header a reference to the header to read from the internal buffer.
Returns:
the number of bytes read from the packet.

Definition at line 203 of file packet.cc.

References ns3::Buffer::Begin(), ns3::Header::Deserialize(), m_buffer, and NS_LOG_FUNCTION.

uint32_t ns3::Packet::PeekTrailer ( Trailer trailer  ) 

Deserialize but does _not_ remove a trailer from the internal buffer. This method invokes the Trailer::Deserialize method.

Parameters:
trailer a reference to the trailer to read from the internal buffer.
Returns:
the number of bytes read from the end of the packet.

Definition at line 235 of file packet.cc.

References ns3::Trailer::Deserialize(), ns3::Buffer::End(), m_buffer, and NS_LOG_FUNCTION.

void ns3::Packet::Print ( std::ostream &  os  )  const
void ns3::Packet::PrintTags ( std::ostream &  os  )  const
Parameters:
os output stream in which the data should be printed.

Iterate over the tags present in this packet, and invoke the Print method of each tag stored in the packet.

Definition at line 319 of file packet.cc.

References ns3::TypeId::GetConstructor(), ns3::TagIterator::Item::GetEnd(), ns3::TypeId::GetName(), ns3::TagIterator::Item::GetStart(), ns3::TagIterator::Item::GetTag(), GetTagIterator(), ns3::TagIterator::Item::GetTypeId(), ns3::TagIterator::HasNext(), ns3::Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 >::IsNull(), ns3::TagIterator::Next(), NS_ASSERT, and ns3::Tag::Print().

void ns3::Packet::Ref ( void   )  const

Definition at line 80 of file packet.cc.

References m_refCount.

void ns3::Packet::RemoveAllTags ( void   ) 

Remove all the tags stored in this packet.

Definition at line 287 of file packet.cc.

References m_tagList, NS_LOG_FUNCTION, and ns3::TagList::RemoveAll().

void ns3::Packet::RemoveAtEnd ( uint32_t  size  ) 

Remove size bytes from the end of the current packet It is safe to remove more bytes that what is present in the packet.

Parameters:
size number of bytes from remove

Definition at line 272 of file packet.cc.

References m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::PacketMetadata::RemoveAtEnd(), and ns3::Buffer::RemoveAtEnd().

void ns3::Packet::RemoveAtStart ( uint32_t  size  ) 

Remove size bytes from the start of the current packet. It is safe to remove more bytes that what is present in the packet.

Parameters:
size number of bytes from remove

Definition at line 279 of file packet.cc.

References m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::PacketMetadata::RemoveAtStart(), and ns3::Buffer::RemoveAtStart().

uint32_t ns3::Packet::RemoveHeader ( Header header  ) 

Deserialize and remove the header from the internal buffer. This method invokes Header::Deserialize.

Parameters:
header a reference to the header to remove from the internal buffer.
Returns:
the number of bytes removed from the packet.

Definition at line 194 of file packet.cc.

References ns3::Buffer::Begin(), ns3::Header::Deserialize(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::Buffer::RemoveAtStart(), and ns3::PacketMetadata::RemoveHeader().

Referenced by ns3::OlsrHeaderTest::RunTests().

uint32_t ns3::Packet::RemoveTrailer ( Trailer trailer  ) 

Remove a deserialized trailer from the internal buffer. This method invokes the Deserialize method.

Parameters:
trailer a reference to the trailer to remove from the internal buffer.
Returns:
the number of bytes removed from the end of the packet.

Definition at line 226 of file packet.cc.

References ns3::Trailer::Deserialize(), ns3::Buffer::End(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::Buffer::RemoveAtEnd(), and ns3::PacketMetadata::RemoveTrailer().

Buffer ns3::Packet::Serialize ( void   )  const
Returns:
a byte buffer

This method creates a serialized representation of a Packet object ready to be transmitted over a network to another system. This serialized representation contains a copy of the packet byte buffer, the tag list, and the packet metadata (if there is one).

This method will trigger calls to the Serialize and GetSerializedSize methods of each tag stored in this packet.

This method will typically be used by parallel simulations where the simulated system is partitioned and each partition runs on a different CPU.

Definition at line 493 of file packet.cc.

References ns3::Buffer::AddAtEnd(), ns3::Buffer::AddAtStart(), ns3::Buffer::Begin(), ns3::Buffer::CreateFullCopy(), ns3::PacketMetadata::GetSerializedSize(), ns3::Buffer::GetSize(), m_buffer, m_metadata, NS_LOG_FUNCTION, ns3::PacketMetadata::Serialize(), and ns3::Buffer::Iterator::WriteU32().

void ns3::Packet::Unref ( void   )  const

Definition at line 85 of file packet.cc.

References m_refCount.


Member Data Documentation

uint32_t ns3::Packet::m_globalUid = 0 [static, private]

Definition at line 418 of file packet.h.

Referenced by Packet().

uint32_t ns3::Packet::m_refCount [mutable, private]

Definition at line 417 of file packet.h.

Referenced by Ref(), and Unref().


The documentation for this class was generated from the following files:
Generated on Thu Dec 3 14:09:59 2009 for NS-3 by  doxygen 1.6.3