A Discrete-Event Network Simulator
API
packet-metadata.h
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
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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  */
20 #ifndef PACKET_METADATA_H
21 #define PACKET_METADATA_H
22 
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"
30 
31 namespace ns3 {
32 
33 class Chunk;
34 class Buffer;
35 class Header;
36 class Trailer;
37 
79 {
80 public:
81 
85  struct Item
86  {
88  enum ItemType {
92  } type;
93 
97  bool isFragment;
107  uint32_t currentSize;
123  };
124 
129  {
130 public:
136  ItemIterator (const PacketMetadata *metadata, Buffer buffer);
141  bool HasNext (void) const;
146  Item Next (void);
147 private:
150  uint16_t m_current;
151  uint32_t m_offset;
153  };
154 
158  static void Enable (void);
162  static void EnableChecking (void);
163 
169  inline PacketMetadata (uint64_t uid, uint32_t size);
174  inline PacketMetadata (PacketMetadata const &o);
180  inline PacketMetadata &operator = (PacketMetadata const& o);
181  inline ~PacketMetadata ();
182 
188  void AddHeader (Header const &header, uint32_t size);
194  void RemoveHeader (Header const &header, uint32_t size);
195 
201  void AddTrailer (Trailer const &trailer, uint32_t size);
207  void RemoveTrailer (Trailer const &trailer, uint32_t size);
208 
219  PacketMetadata CreateFragment (uint32_t start, uint32_t end) const;
220 
225  void AddAtEnd (PacketMetadata const&o);
230  void AddPaddingAtEnd (uint32_t end);
235  void RemoveAtStart (uint32_t start);
240  void RemoveAtEnd (uint32_t end);
241 
246  uint64_t GetUid (void) const;
247 
252  uint32_t GetSerializedSize (void) const;
253 
259  ItemIterator BeginItem (Buffer buffer) const;
260 
267  uint32_t Serialize (uint8_t* buffer, uint32_t maxSize) const;
274  uint32_t Deserialize (const uint8_t* buffer, uint32_t size);
275 
276 private:
286  static uint8_t* AddToRawU8 (const uint8_t& data,
287  uint8_t* start,
288  uint8_t* current,
289  uint32_t maxSize);
290 
300  static uint8_t* AddToRawU16 (const uint16_t& data,
301  uint8_t* start,
302  uint8_t* current,
303  uint32_t maxSize);
304 
314  static uint8_t* AddToRawU32 (const uint32_t& data,
315  uint8_t* start,
316  uint8_t* current,
317  uint32_t maxSize);
318 
328  static uint8_t* AddToRawU64 (const uint64_t& data,
329  uint8_t* start,
330  uint8_t* current,
331  uint32_t maxSize);
332 
343  static uint8_t* AddToRaw (const uint8_t* data,
344  uint32_t dataSize,
345  uint8_t* start,
346  uint8_t* current,
347  uint32_t maxSize);
348 
358  static uint8_t* ReadFromRawU8 (uint8_t& data,
359  const uint8_t* start,
360  const uint8_t* current,
361  uint32_t maxSize);
362 
372  static uint8_t* ReadFromRawU16 (uint16_t& data,
373  const uint8_t* start,
374  const uint8_t* current,
375  uint32_t maxSize);
376 
386  static uint8_t* ReadFromRawU32 (uint32_t& data,
387  const uint8_t* start,
388  const uint8_t* current,
389  uint32_t maxSize);
390 
400  static uint8_t* ReadFromRawU64 (uint64_t& data,
401  const uint8_t* start,
402  const uint8_t* current,
403  uint32_t maxSize);
404 
409 #define PACKET_METADATA_DATA_M_DATA_SIZE 8
410 
414  struct Data {
416  uint32_t m_count;
418  uint16_t m_size;
421  uint16_t m_dirtyEnd;
424  };
425  /* Note that since the next and prev fields are 16 bit integers
426  and since the value 0xffff is reserved to identify the
427  fact that the end or the start of the list is reached,
428  only a limited number of elements can be stored in
429  a m_data byte buffer.
430  */
434  struct SmallItem {
440  uint16_t next;
446  uint16_t prev;
454  uint32_t typeUid;
459  uint32_t size;
472  uint16_t chunkUid;
473  };
474 
478  struct ExtraItem {
483  uint32_t fragmentStart;
488  uint32_t fragmentEnd;
494  uint64_t packetUid;
495  };
496 
500  class DataFreeList : public std::vector<struct Data *>
501  {
502 public:
503  ~DataFreeList ();
504  };
505 
506  friend DataFreeList::~DataFreeList ();
508  friend class ItemIterator;
509 
510  PacketMetadata ();
511 
517  inline uint16_t AddSmall (const PacketMetadata::SmallItem *item);
526  uint16_t AddBig (uint32_t head, uint32_t tail,
527  const PacketMetadata::SmallItem *item,
528  const PacketMetadata::ExtraItem *extraItem);
537  PacketMetadata::ExtraItem *extraItem,
538  uint32_t available);
543  inline void UpdateHead (uint16_t written);
548  inline void UpdateTail (uint16_t written);
549 
555  inline uint32_t GetUleb128Size (uint32_t value) const;
561  uint32_t ReadUleb128 (const uint8_t **pBuffer) const;
567  inline void Append16 (uint16_t value, uint8_t *buffer);
573  inline void Append32 (uint32_t value, uint8_t *buffer);
579  inline void AppendValue (uint32_t value, uint8_t *buffer);
588  void AppendValueExtra (uint32_t value, uint8_t *buffer);
589 
594  inline void Reserve (uint32_t n);
599  void ReserveCopy (uint32_t n);
600 
605  uint32_t GetTotalSize (void) const;
606 
614  uint32_t ReadItems (uint16_t current,
615  struct PacketMetadata::SmallItem *item,
616  struct PacketMetadata::ExtraItem *extraItem) const;
622  void DoAddHeader (uint32_t uid, uint32_t size);
627  bool IsStateOk (void) const;
633  bool IsPointerOk (uint16_t pointer) const;
639  bool IsSharedPointerOk (uint16_t pointer) const;
640 
645  static void Recycle (struct PacketMetadata::Data *data);
651  static struct PacketMetadata::Data *Create (uint32_t size);
657  static struct PacketMetadata::Data *Allocate (uint32_t n);
662  static void Deallocate (struct PacketMetadata::Data *data);
663 
665  static bool m_enable;
666  static bool m_enableChecking;
667 
673  static bool m_metadataSkipped;
674 
675  static uint32_t m_maxSize;
676  static uint16_t m_chunkUid;
677 
678  struct Data *m_data;
679  /*
680  head -(next)-> tail
681  ^ |
682  \---(prev)---|
683  */
684  uint16_t m_head;
685  uint16_t m_tail;
686  uint16_t m_used;
687  uint64_t m_packetUid;
688 };
689 
690 } // namespace ns3
691 
692 namespace ns3 {
693 
694 PacketMetadata::PacketMetadata (uint64_t uid, uint32_t size)
695  : m_data (PacketMetadata::Create (10)),
696  m_head (0xffff),
697  m_tail (0xffff),
698  m_used (0),
699  m_packetUid (uid)
700 {
701  memset (m_data->m_data, 0xff, 4);
702  if (size > 0)
703  {
704  DoAddHeader (0, size);
705  }
706 }
708  : m_data (o.m_data),
709  m_head (o.m_head),
710  m_tail (o.m_tail),
711  m_used (o.m_used),
712  m_packetUid (o.m_packetUid)
713 {
714  NS_ASSERT (m_data != 0);
716  m_data->m_count++;
717 }
720 {
721  if (m_data != o.m_data)
722  {
723  // not self assignment
724  NS_ASSERT (m_data != 0);
725  m_data->m_count--;
726  if (m_data->m_count == 0)
727  {
729  }
730  m_data = o.m_data;
731  NS_ASSERT (m_data != 0);
732  m_data->m_count++;
733  }
734  m_head = o.m_head;
735  m_tail = o.m_tail;
736  m_used = o.m_used;
738  return *this;
739 }
741 {
742  NS_ASSERT (m_data != 0);
743  m_data->m_count--;
744  if (m_data->m_count == 0)
745  {
747  }
748 }
749 
750 } // namespace ns3
751 
752 
753 #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.
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:1844
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.
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 trimmed from the end of a fragment.
Class to hold all the metadata.
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.
uint32_t ReadUleb128(const uint8_t **pBuffer) const
Read a ULEB128 (Unsigned Little Endian Base 128) coded number.
const PacketMetadata * m_metadata
pointer to the metadata
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
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.
void AddTrailer(Trailer const &trailer, uint32_t size)
Add a trailer.
void AddPaddingAtEnd(uint32_t end)
Add some padding at the end.
uint32_t ReadItems(uint16_t current, struct PacketMetadata::SmallItem *item, struct PacketMetadata::ExtraItem *extraItem) const
Read items.
#define max(a, b)
Definition: 80211b.c:43
ItemIterator BeginItem(Buffer buffer) const
Initialize the item iterator to the buffer begin.
uint32_t GetTotalSize(void) const
Get the total size used by the metadata.
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.
enum ns3::PacketMetadata::Item::ItemType type
metadata type
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 IsPointerOk(uint16_t pointer) const
Check if the position is valid.
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.
bool HasNext(void) const
Checks if there is another metadata item.
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
uint64_t m_packetUid
packet Uid
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
PacketMetadata CreateFragment(uint32_t start, uint32_t end) const
Creates a fragment.
uint32_t size
the size (in bytes) of the header or trailer represented by this element.
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:516
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
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 GetUleb128Size(uint32_t value) const
Get the ULEB128 (Unsigned Little Endian Base 128) size.
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.
bool IsStateOk(void) const
Check if the metadata state is ok.
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 ...
uint64_t GetUid(void) const
Get the packet Uid.
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
uint32_t GetSerializedSize(void) const
Get the metadata serialized size.
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 trimmed from the start of a fragment.
uint16_t m_current
current position
ItemType
Type of data in the packet.
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.
uint8_t m_data[PACKET_METADATA_DATA_M_DATA_SIZE]
variable-sized buffer of bytes
bool IsSharedPointerOk(uint16_t pointer) const
Check if the position is valid.
a unique identifier for an interface.
Definition: type-id.h:58
ItemIterator(const PacketMetadata *metadata, Buffer buffer)
Constructor.
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
#define PACKET_METADATA_DATA_M_DATA_SIZE
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.
Handle packet metadata about packet headers and trailers.