A Discrete-Event Network Simulator
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
23 #include <stdint.h>
24 #include <vector>
25 #include <limits>
26 #include "ns3/callback.h"
27 #include "ns3/assert.h"
28 #include "ns3/type-id.h"
29 #include "buffer.h"
31 namespace ns3 {
33 class Chunk;
34 class Buffer;
35 class Header;
36 class Trailer;
79 {
80 public:
85  struct Item
86  {
87  enum {
91  } type;
96  bool isFragment;
106  uint32_t currentSize;
122  };
128  {
129 public:
135  ItemIterator (const PacketMetadata *metadata, Buffer buffer);
140  bool HasNext (void) const;
145  Item Next (void);
146 private:
149  uint16_t m_current;
150  uint32_t m_offset;
152  };
157  static void Enable (void);
161  static void EnableChecking (void);
168  inline PacketMetadata (uint64_t uid, uint32_t size);
173  inline PacketMetadata (PacketMetadata const &o);
179  inline PacketMetadata &operator = (PacketMetadata const& o);
180  inline ~PacketMetadata ();
187  void AddHeader (Header const &header, uint32_t size);
193  void RemoveHeader (Header const &header, uint32_t size);
200  void AddTrailer (Trailer const &trailer, uint32_t size);
206  void RemoveTrailer (Trailer const &trailer, uint32_t size);
218  PacketMetadata CreateFragment (uint32_t start, uint32_t end) const;
224  void AddAtEnd (PacketMetadata const&o);
229  void AddPaddingAtEnd (uint32_t end);
234  void RemoveAtStart (uint32_t start);
239  void RemoveAtEnd (uint32_t end);
245  uint64_t GetUid (void) const;
251  uint32_t GetSerializedSize (void) const;
256  ItemIterator BeginItem (Buffer buffer) const;
264  uint32_t Serialize (uint8_t* buffer, uint32_t maxSize) const;
271  uint32_t Deserialize (const uint8_t* buffer, uint32_t size);
273 private:
283  static uint8_t* AddToRawU8 (const uint8_t& data,
284  uint8_t* start,
285  uint8_t* current,
286  uint32_t maxSize);
297  static uint8_t* AddToRawU16 (const uint16_t& data,
298  uint8_t* start,
299  uint8_t* current,
300  uint32_t maxSize);
311  static uint8_t* AddToRawU32 (const uint32_t& data,
312  uint8_t* start,
313  uint8_t* current,
314  uint32_t maxSize);
325  static uint8_t* AddToRawU64 (const uint64_t& data,
326  uint8_t* start,
327  uint8_t* current,
328  uint32_t maxSize);
340  static uint8_t* AddToRaw (const uint8_t* data,
341  uint32_t dataSize,
342  uint8_t* start,
343  uint8_t* current,
344  uint32_t maxSize);
355  static uint8_t* ReadFromRawU8 (uint8_t& data,
356  const uint8_t* start,
357  const uint8_t* current,
358  uint32_t maxSize);
369  static uint8_t* ReadFromRawU16 (uint16_t& data,
370  const uint8_t* start,
371  const uint8_t* current,
372  uint32_t maxSize);
383  static uint8_t* ReadFromRawU32 (uint32_t& data,
384  const uint8_t* start,
385  const uint8_t* current,
386  uint32_t maxSize);
397  static uint8_t* ReadFromRawU64 (uint64_t& data,
398  const uint8_t* start,
399  const uint8_t* current,
400  uint32_t maxSize);
411  struct Data {
413  uint32_t m_count;
415  uint16_t m_size;
418  uint16_t m_dirtyEnd;
421  };
422  /* Note that since the next and prev fields are 16 bit integers
423  and since the value 0xffff is reserved to identify the
424  fact that the end or the start of the list is reached,
425  only a limited number of elements can be stored in
426  a m_data byte buffer.
427  */
431  struct SmallItem {
437  uint16_t next;
443  uint16_t prev;
451  uint32_t typeUid;
456  uint32_t size;
469  uint16_t chunkUid;
470  };
475  struct ExtraItem {
480  uint32_t fragmentStart;
485  uint32_t fragmentEnd;
491  uint64_t packetUid;
492  };
497  class DataFreeList : public std::vector<struct Data *>
498  {
499 public:
500  ~DataFreeList ();
501  };
503  friend DataFreeList::~DataFreeList ();
504  friend class ItemIterator;
506  PacketMetadata ();
513  inline uint16_t AddSmall (const PacketMetadata::SmallItem *item);
522  uint16_t AddBig (uint32_t head, uint32_t tail,
523  const PacketMetadata::SmallItem *item,
524  const PacketMetadata::ExtraItem *extraItem);
533  PacketMetadata::ExtraItem *extraItem,
534  uint32_t available);
539  inline void UpdateHead (uint16_t written);
544  inline void UpdateTail (uint16_t written);
551  inline uint32_t GetUleb128Size (uint32_t value) const;
557  uint32_t ReadUleb128 (const uint8_t **pBuffer) const;
563  inline void Append16 (uint16_t value, uint8_t *buffer);
569  inline void Append32 (uint32_t value, uint8_t *buffer);
575  inline void AppendValue (uint32_t value, uint8_t *buffer);
584  void AppendValueExtra (uint32_t value, uint8_t *buffer);
590  inline void Reserve (uint32_t n);
595  void ReserveCopy (uint32_t n);
600  uint32_t GetTotalSize (void) const;
609  uint32_t ReadItems (uint16_t current,
610  struct PacketMetadata::SmallItem *item,
611  struct PacketMetadata::ExtraItem *extraItem) const;
617  void DoAddHeader (uint32_t uid, uint32_t size);
622  bool IsStateOk (void) const;
628  bool IsPointerOk (uint16_t pointer) const;
634  bool IsSharedPointerOk (uint16_t pointer) const;
640  static void Recycle (struct PacketMetadata::Data *data);
646  static struct PacketMetadata::Data *Create (uint32_t size);
652  static struct PacketMetadata::Data *Allocate (uint32_t n);
657  static void Deallocate (struct PacketMetadata::Data *data);
660  static bool m_enable;
661  static bool m_enableChecking;
668  static bool m_metadataSkipped;
670  static uint32_t m_maxSize;
671  static uint16_t m_chunkUid;
673  struct Data *m_data;
674  /*
675  head -(next)-> tail
676  ^ |
677  \---(prev)---|
678  */
679  uint16_t m_head;
680  uint16_t m_tail;
681  uint16_t m_used;
682  uint64_t m_packetUid;
683 };
685 } // namespace ns3
687 namespace ns3 {
689 PacketMetadata::PacketMetadata (uint64_t uid, uint32_t size)
690  : m_data (PacketMetadata::Create (10)),
691  m_head (0xffff),
692  m_tail (0xffff),
693  m_used (0),
694  m_packetUid (uid)
695 {
696  memset (m_data->m_data, 0xff, 4);
697  if (size > 0)
698  {
699  DoAddHeader (0, size);
700  }
701 }
703  : m_data (o.m_data),
704  m_head (o.m_head),
705  m_tail (o.m_tail),
706  m_used (o.m_used),
707  m_packetUid (o.m_packetUid)
708 {
709  NS_ASSERT (m_data != 0);
711  m_data->m_count++;
712 }
715 {
716  if (m_data != o.m_data)
717  {
718  // not self assignment
719  NS_ASSERT (m_data != 0);
720  m_data->m_count--;
721  if (m_data->m_count == 0)
722  {
724  }
725  m_data = o.m_data;
726  NS_ASSERT (m_data != 0);
727  m_data->m_count++;
728  }
729  m_head = o.m_head;
730  m_tail = o.m_tail;
731  m_used = o.m_used;
733  return *this;
734 }
736 {
737  NS_ASSERT (m_data != 0);
738  m_data->m_count--;
739  if (m_data->m_count == 0)
740  {
742  }
743 }
745 } // namespace ns3
748 #endif /* PACKET_METADATA_H */
Protocol header serialization and deserialization.
Definition: header.h:42
void RemoveAtEnd(uint32_t end)
Remove a chunk of metadata at the metadata end.
static void Recycle(struct PacketMetadata::Data *data)
Recycle the buffer memory.
Buffer::Iterator current
an iterator which can be fed to Deserialize.
static uint8_t * ReadFromRawU8(uint8_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
static bool m_enableChecking
Enable the packet metadata checking.
static bool m_enable
Enable the packet metadata.
uint64_t GetUid(void) const
Get the packet Uid.
void ReserveCopy(uint32_t n)
Reserve space and make a metadata copy.
Item Next(void)
Retrieve the next metadata item.
automatically resized byte buffer
Definition: buffer.h:92
def start()
Definition: core.py:1482
structure describing a packet metadata item
bool isFragment
true: this is a fragmented header, trailer, or, payload.
static void Deallocate(struct PacketMetadata::Data *data)
Deallocate the buffer memory.
TypeId tid
TypeId of Header or Trailer.
static void EnableChecking(void)
Enable the packet metadata checking.
static uint8_t * ReadFromRawU16(uint16_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
struct Data * m_data
Metadata storage.
uint32_t GetSerializedSize(void) const
Get the metadata serialized size.
uint16_t AddSmall(const PacketMetadata::SmallItem *item)
Add a SmallItem.
void UpdateHead(uint16_t written)
Update the head.
uint32_t currentTrimedFromEnd
how many bytes were trimed from the end of a fragment.
Class to hold all the metadata.
bool IsSharedPointerOk(uint16_t pointer) const
Check if the position is valid.
void AddHeader(Header const &header, uint32_t size)
Add an header.
void AppendValueExtra(uint32_t value, uint8_t *buffer)
Append a value to the buffer - extra.
iterator in a Buffer instance
Definition: buffer.h:98
void DoAddHeader(uint32_t uid, uint32_t size)
Add an header.
const PacketMetadata * m_metadata
pointer to the metadata
static uint8_t * AddToRaw(const uint8_t *data, uint32_t dataSize, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
bool IsPointerOk(uint16_t pointer) const
Check if the position is valid.
void AddTrailer(Trailer const &trailer, uint32_t size)
Add a trailer.
void AddPaddingAtEnd(uint32_t end)
Add some padding at the end.
#define max(a, b)
Definition: 80211b.c:45
PacketMetadata CreateFragment(uint32_t start, uint32_t end) const
Creates a fragment.
uint8_t data[writeSize]
static uint8_t * AddToRawU16(const uint16_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
Iterator class for metadata items.
uint16_t next
offset (in bytes) from start of m_data buffer to next element in linked list.
static DataFreeList m_freeList
the metadata data storage
bool HasNext(void) const
Checks if there is another metadata item.
void AppendValue(uint32_t value, uint8_t *buffer)
Append a value to the buffer.
static struct PacketMetadata::Data * Allocate(uint32_t n)
Allocate a buffer data storage.
void AddAtEnd(PacketMetadata const &o)
Add a metadata at the metadata start.
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
uint64_t m_packetUid
packet Uid
uint32_t GetUleb128Size(uint32_t value) const
Get the ULEB128 (Unsigned Little Endian Base 128) size.
static void Enable(void)
Enable the packet metadata.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Buffer m_buffer
buffer the metadata refers to
uint32_t size
the size (in bytes) of the header or trailer represented by this element.
enum ns3::PacketMetadata::Item::@79 type
metadata type
uint16_t m_tail
list tail
static uint8_t * AddToRawU64(const uint64_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
void Append32(uint32_t value, uint8_t *buffer)
Append a 32-bit value to the buffer.
Ptr< T > Create(void)
Create class instances by constructors with varying numbers of arguments and return them by Ptr...
Definition: ptr.h:514
uint16_t m_size
size (in bytes) of m_data buffer below
void Append16(uint16_t value, uint8_t *buffer)
Append a 16-bit value to the buffer.
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Deserialization from raw uint8_t*.
static uint32_t m_maxSize
maximum metadata size
bool IsStateOk(void) const
Check if the metadata state is ok.
void RemoveHeader(Header const &header, uint32_t size)
Remove an header.
static uint8_t * AddToRawU32(const uint32_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
uint16_t prev
offset (in bytes) from start of m_data buffer to previous element in linked list. ...
static uint16_t m_chunkUid
Chunk Uid.
uint16_t m_used
used portion
uint32_t currentSize
size of item.
uint32_t GetTotalSize(void) const
Get the total size used by the metadata.
uint16_t AddBig(uint32_t head, uint32_t tail, const PacketMetadata::SmallItem *item, const PacketMetadata::ExtraItem *extraItem)
Add a "Big" Item (a SmallItem plus an ExtraItem)
uint32_t m_count
number of references to this struct Data instance.
static struct PacketMetadata::Data * Create(uint32_t size)
Create a buffer data storage.
void RemoveTrailer(Trailer const &trailer, uint32_t size)
Remove a trailer.
PacketMetadata & operator=(PacketMetadata const &o)
Basic assignment.
ItemIterator BeginItem(Buffer buffer) const
Initialize the item iterator to the buffer begin.
static uint8_t * ReadFromRawU64(uint64_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
uint16_t m_dirtyEnd
max of the m_used field over all objects which reference this struct Data instance ...
static bool m_metadataSkipped
Set to true when adding metadata to a packet is skipped because m_enable is false; used to detect ena...
uint32_t fragmentEnd
offset (in bytes) from start of original header to the end of the fragment still present.
uint32_t typeUid
the high 31 bits of this field identify the type of the header or trailer represented by this item: t...
uint16_t chunkUid
this field tries to uniquely identify each header or trailer instance while the typeUid field uniquel...
bool m_hasReadTail
true if the metadata tail has been read
void UpdateTail(uint16_t written)
Update the tail.
uint64_t packetUid
the packetUid of the packet in which this header or trailer was first added.
void RemoveAtStart(uint32_t start)
Remove a chunk of metadata at the metadata start.
uint32_t currentTrimedFromStart
how many bytes were trimed from the start of a fragment.
uint16_t m_current
current position
void Reserve(uint32_t n)
Reserve space.
void ReplaceTail(PacketMetadata::SmallItem *item, PacketMetadata::ExtraItem *extraItem, uint32_t available)
Replace the tail.
static uint8_t * AddToRawU8(const uint8_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
variable-sized buffer of bytes
a unique identifier for an interface.
Definition: type-id.h:58
uint32_t ReadUleb128(const uint8_t **pBuffer) const
Read a ULEB128 (Unsigned Little Endian Base 128) coded number.
ItemIterator(const PacketMetadata *metadata, Buffer buffer)
uint32_t fragmentStart
offset (in bytes) from start of original header to the start of the fragment still present...
uint16_t m_head
list head
uint32_t ReadItems(uint16_t current, struct PacketMetadata::SmallItem *item, struct PacketMetadata::ExtraItem *extraItem) const
Read items.
the size of PacketMetadata::Data::m_data such that the total size of PacketMetadata::Data is 16 bytes...
static uint8_t * ReadFromRawU32(uint32_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
Handle packet metadata about packet headers and trailers.