A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
80 {
81 public:
82 
86  struct Item
87  {
88  enum {
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 
257  ItemIterator BeginItem (Buffer buffer) const;
258 
265  uint32_t Serialize (uint8_t* buffer, uint32_t maxSize) const;
272  uint32_t Deserialize (const uint8_t* buffer, uint32_t size);
273 
274 private:
284  static uint8_t* AddToRawU8 (const uint8_t& data,
285  uint8_t* start,
286  uint8_t* current,
287  uint32_t maxSize);
288 
298  static uint8_t* AddToRawU16 (const uint16_t& data,
299  uint8_t* start,
300  uint8_t* current,
301  uint32_t maxSize);
302 
312  static uint8_t* AddToRawU32 (const uint32_t& data,
313  uint8_t* start,
314  uint8_t* current,
315  uint32_t maxSize);
316 
326  static uint8_t* AddToRawU64 (const uint64_t& data,
327  uint8_t* start,
328  uint8_t* current,
329  uint32_t maxSize);
330 
341  static uint8_t* AddToRaw (const uint8_t* data,
342  uint32_t dataSize,
343  uint8_t* start,
344  uint8_t* current,
345  uint32_t maxSize);
346 
356  static uint8_t* ReadFromRawU8 (uint8_t& data,
357  const uint8_t* start,
358  const uint8_t* current,
359  uint32_t maxSize);
360 
370  static uint8_t* ReadFromRawU16 (uint16_t& data,
371  const uint8_t* start,
372  const uint8_t* current,
373  uint32_t maxSize);
374 
384  static uint8_t* ReadFromRawU32 (uint32_t& data,
385  const uint8_t* start,
386  const uint8_t* current,
387  uint32_t maxSize);
388 
398  static uint8_t* ReadFromRawU64 (uint64_t& data,
399  const uint8_t* start,
400  const uint8_t* current,
401  uint32_t maxSize);
402 
407 #define PACKET_METADATA_DATA_M_DATA_SIZE 8
408 
412  struct Data {
414  uint32_t m_count;
416  uint16_t m_size;
419  uint16_t m_dirtyEnd;
422  };
423  /* Note that since the next and prev fields are 16 bit integers
424  and since the value 0xffff is reserved to identify the
425  fact that the end or the start of the list is reached,
426  only a limited number of elements can be stored in
427  a m_data byte buffer.
428  */
432  struct SmallItem {
438  uint16_t next;
444  uint16_t prev;
452  uint32_t typeUid;
457  uint32_t size;
470  uint16_t chunkUid;
471  };
472 
476  struct ExtraItem {
481  uint32_t fragmentStart;
486  uint32_t fragmentEnd;
492  uint64_t packetUid;
493  };
494 
498  class DataFreeList : public std::vector<struct Data *>
499  {
500 public:
501  ~DataFreeList ();
502  };
503 
504  friend DataFreeList::~DataFreeList ();
505  friend class ItemIterator;
506 
507  PacketMetadata ();
508 
514  inline uint16_t AddSmall (const PacketMetadata::SmallItem *item);
523  uint16_t AddBig (uint32_t head, uint32_t tail,
524  const PacketMetadata::SmallItem *item,
525  const PacketMetadata::ExtraItem *extraItem);
534  PacketMetadata::ExtraItem *extraItem,
535  uint32_t available);
540  inline void UpdateHead (uint16_t written);
545  inline void UpdateTail (uint16_t written);
546 
552  inline uint32_t GetUleb128Size (uint32_t value) const;
558  uint32_t ReadUleb128 (const uint8_t **pBuffer) const;
564  inline void Append16 (uint16_t value, uint8_t *buffer);
570  inline void Append32 (uint32_t value, uint8_t *buffer);
576  inline void AppendValue (uint32_t value, uint8_t *buffer);
585  void AppendValueExtra (uint32_t value, uint8_t *buffer);
586 
591  inline void Reserve (uint32_t n);
596  void ReserveCopy (uint32_t n);
597 
601  uint32_t GetTotalSize (void) const;
602 
610  uint32_t ReadItems (uint16_t current,
611  struct PacketMetadata::SmallItem *item,
612  struct PacketMetadata::ExtraItem *extraItem) const;
618  void DoAddHeader (uint32_t uid, uint32_t size);
623  bool IsStateOk (void) const;
629  bool IsPointerOk (uint16_t pointer) const;
635  bool IsSharedPointerOk (uint16_t pointer) const;
636 
641  static void Recycle (struct PacketMetadata::Data *data);
647  static struct PacketMetadata::Data *Create (uint32_t size);
653  static struct PacketMetadata::Data *Allocate (uint32_t n);
658  static void Deallocate (struct PacketMetadata::Data *data);
659 
661  static bool m_enable;
662  static bool m_enableChecking;
663 
669  static bool m_metadataSkipped;
670 
671  static uint32_t m_maxSize;
672  static uint16_t m_chunkUid;
673 
674  struct Data *m_data;
675  /*
676  head -(next)-> tail
677  ^ |
678  \---(prev)---|
679  */
680  uint16_t m_head;
681  uint16_t m_tail;
682  uint16_t m_used;
683  uint64_t m_packetUid;
684 };
685 
686 } // namespace ns3
687 
688 namespace ns3 {
689 
690 PacketMetadata::PacketMetadata (uint64_t uid, uint32_t size)
691  : m_data (PacketMetadata::Create (10)),
692  m_head (0xffff),
693  m_tail (0xffff),
694  m_used (0),
695  m_packetUid (uid)
696 {
697  memset (m_data->m_data, 0xff, 4);
698  if (size > 0)
699  {
700  DoAddHeader (0, size);
701  }
702 }
704  : m_data (o.m_data),
705  m_head (o.m_head),
706  m_tail (o.m_tail),
707  m_used (o.m_used),
708  m_packetUid (o.m_packetUid)
709 {
710  NS_ASSERT (m_data != 0);
711  NS_ASSERT (m_data->m_count < std::numeric_limits<uint32_t>::max());
712  m_data->m_count++;
713 }
716 {
717  if (m_data != o.m_data)
718  {
719  // not self assignment
720  NS_ASSERT (m_data != 0);
721  m_data->m_count--;
722  if (m_data->m_count == 0)
723  {
725  }
726  m_data = o.m_data;
727  NS_ASSERT (m_data != 0);
728  m_data->m_count++;
729  }
730  m_head = o.m_head;
731  m_tail = o.m_tail;
732  m_used = o.m_used;
734  return *this;
735 }
737 {
738  NS_ASSERT (m_data != 0);
739  m_data->m_count--;
740  if (m_data->m_count == 0)
741  {
743  }
744 }
745 
746 } // namespace ns3
747 
748 
749 #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
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:61
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.
enum ns3::PacketMetadata::Item::@81 type
metadata type
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.
PacketMetadata CreateFragment(uint32_t start, uint32_t end) const
Creates a fragment.
Ptr< T > Create(void)
Definition: ptr.h:232
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.
Buffer m_buffer
buffer the metadata refers to
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.
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.
Time current
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.
uint8_t m_data[PACKET_METADATA_DATA_M_DATA_SIZE]
variable-sized buffer of bytes
a unique identifier for an interface.
Definition: type-id.h:49
uint32_t ReadUleb128(const uint8_t **pBuffer) const
Read a ULEB128 (Unsigned Little Endian Base 128) coded number.
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
uint32_t ReadItems(uint16_t current, struct PacketMetadata::SmallItem *item, struct PacketMetadata::ExtraItem *extraItem) const
Read items.
#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.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
Handle packet metadata about packet headers and trailers.