A Discrete-Event Network Simulator
API
packet-metadata.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006,2007 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef PACKET_METADATA_H
20#define PACKET_METADATA_H
21
22#include "buffer.h"
23
24#include "ns3/assert.h"
25#include "ns3/callback.h"
26#include "ns3/type-id.h"
27
28#include <limits>
29#include <stdint.h>
30#include <vector>
31
32namespace ns3
33{
34
35class Chunk;
36class Buffer;
37class Header;
38class Trailer;
39
81{
82 public:
86 struct Item
87 {
90 {
93 TRAILER
94 } type;
95
126 };
127
132 {
133 public:
139 ItemIterator(const PacketMetadata* metadata, Buffer buffer);
144 bool HasNext() const;
149 Item Next();
150
151 private:
154 uint16_t m_current;
157 };
158
162 static void Enable();
166 static void EnableChecking();
167
173 inline PacketMetadata(uint64_t uid, uint32_t size);
178 inline PacketMetadata(const PacketMetadata& o);
184 inline PacketMetadata& operator=(const PacketMetadata& o);
185 inline ~PacketMetadata();
186
187 // Delete default constructor to avoid misuse
188 PacketMetadata() = delete;
189
195 void AddHeader(const Header& header, uint32_t size);
201 void RemoveHeader(const Header& header, uint32_t size);
202
208 void AddTrailer(const Trailer& trailer, uint32_t size);
214 void RemoveTrailer(const Trailer& trailer, uint32_t size);
215
227
232 void AddAtEnd(const PacketMetadata& o);
237 void AddPaddingAtEnd(uint32_t end);
247 void RemoveAtEnd(uint32_t end);
248
253 uint64_t GetUid() const;
254
260
266 ItemIterator BeginItem(Buffer buffer) const;
267
274 uint32_t Serialize(uint8_t* buffer, uint32_t maxSize) const;
281 uint32_t Deserialize(const uint8_t* buffer, uint32_t size);
282
283 private:
293 static uint8_t* AddToRawU8(const uint8_t& data,
294 uint8_t* start,
295 uint8_t* current,
296 uint32_t maxSize);
297
307 static uint8_t* AddToRawU16(const uint16_t& data,
308 uint8_t* start,
309 uint8_t* current,
310 uint32_t maxSize);
311
321 static uint8_t* AddToRawU32(const uint32_t& data,
322 uint8_t* start,
323 uint8_t* current,
324 uint32_t maxSize);
325
335 static uint8_t* AddToRawU64(const uint64_t& data,
336 uint8_t* start,
337 uint8_t* current,
338 uint32_t maxSize);
339
350 static uint8_t* AddToRaw(const uint8_t* data,
351 uint32_t dataSize,
352 uint8_t* start,
353 uint8_t* current,
354 uint32_t maxSize);
355
365 static uint8_t* ReadFromRawU8(uint8_t& data,
366 const uint8_t* start,
367 const uint8_t* current,
368 uint32_t maxSize);
369
379 static uint8_t* ReadFromRawU16(uint16_t& data,
380 const uint8_t* start,
381 const uint8_t* current,
382 uint32_t maxSize);
383
393 static uint8_t* ReadFromRawU32(uint32_t& data,
394 const uint8_t* start,
395 const uint8_t* current,
396 uint32_t maxSize);
397
407 static uint8_t* ReadFromRawU64(uint64_t& data,
408 const uint8_t* start,
409 const uint8_t* current,
410 uint32_t maxSize);
411
416#define PACKET_METADATA_DATA_M_DATA_SIZE 8
417
421 struct Data
422 {
426 uint16_t m_size;
428 uint16_t m_dirtyEnd;
431 };
432
433 /* Note that since the next and prev fields are 16 bit integers
434 and since the value 0xffff is reserved to identify the
435 fact that the end or the start of the list is reached,
436 only a limited number of elements can be stored in
437 a m_data byte buffer.
438 */
443 {
449 uint16_t next;
455 uint16_t prev;
481 uint16_t chunkUid;
482 };
483
488 {
504 uint64_t packetUid;
505 };
506
510 class DataFreeList : public std::vector<struct Data*>
511 {
512 public:
514 };
515
518 friend class ItemIterator;
519
525 inline uint16_t AddSmall(const PacketMetadata::SmallItem* item);
534 uint16_t AddBig(uint32_t head,
535 uint32_t tail,
536 const PacketMetadata::SmallItem* item,
537 const PacketMetadata::ExtraItem* extraItem);
546 PacketMetadata::ExtraItem* extraItem,
547 uint32_t available);
552 inline void UpdateHead(uint16_t written);
557 inline void UpdateTail(uint16_t written);
558
570 uint32_t ReadUleb128(const uint8_t** pBuffer) const;
576 inline void Append16(uint16_t value, uint8_t* buffer);
582 inline void Append32(uint32_t value, uint8_t* buffer);
588 inline void AppendValue(uint32_t value, uint8_t* buffer);
597 void AppendValueExtra(uint32_t value, uint8_t* buffer);
598
603 inline void Reserve(uint32_t n);
608 void ReserveCopy(uint32_t n);
609
614 uint32_t GetTotalSize() const;
615
623 uint32_t ReadItems(uint16_t current,
624 struct PacketMetadata::SmallItem* item,
625 struct PacketMetadata::ExtraItem* extraItem) const;
631 void DoAddHeader(uint32_t uid, uint32_t size);
636 bool IsStateOk() const;
642 bool IsPointerOk(uint16_t pointer) const;
648 bool IsSharedPointerOk(uint16_t pointer) const;
649
654 static void Recycle(struct PacketMetadata::Data* data);
660 static struct PacketMetadata::Data* Create(uint32_t size);
666 static struct PacketMetadata::Data* Allocate(uint32_t n);
671 static void Deallocate(struct PacketMetadata::Data* data);
672
674 static bool m_enable;
675 static bool m_enableChecking;
676
682 static bool m_metadataSkipped;
683
685 static uint16_t m_chunkUid;
686
687 struct Data* m_data;
688 /*
689 head -(next)-> tail
690 ^ |
691 \---(prev)---|
692 */
693 uint16_t m_head;
694 uint16_t m_tail;
695 uint16_t m_used;
696 uint64_t m_packetUid;
697};
698
699} // namespace ns3
700
701namespace ns3
702{
703
705 : m_data(PacketMetadata::Create(10)),
706 m_head(0xffff),
707 m_tail(0xffff),
708 m_used(0),
709 m_packetUid(uid)
710{
711 memset(m_data->m_data, 0xff, 4);
712 if (size > 0)
713 {
714 DoAddHeader(0, size);
715 }
716}
717
719 : m_data(o.m_data),
720 m_head(o.m_head),
721 m_tail(o.m_tail),
722 m_used(o.m_used),
723 m_packetUid(o.m_packetUid)
724{
725 NS_ASSERT(m_data != nullptr);
727 m_data->m_count++;
728}
729
732{
733 if (m_data != o.m_data)
734 {
735 // not self assignment
736 NS_ASSERT(m_data != nullptr);
737 m_data->m_count--;
738 if (m_data->m_count == 0)
739 {
741 }
742 m_data = o.m_data;
743 NS_ASSERT(m_data != nullptr);
744 m_data->m_count++;
745 }
746 m_head = o.m_head;
747 m_tail = o.m_tail;
748 m_used = o.m_used;
750 return *this;
751}
752
754{
755 NS_ASSERT(m_data != nullptr);
756 m_data->m_count--;
757 if (m_data->m_count == 0)
758 {
760 }
761}
762
763} // namespace ns3
764
765#endif /* PACKET_METADATA_H */
#define max(a, b)
Definition: 80211b.c:43
iterator in a Buffer instance
Definition: buffer.h:100
automatically resized byte buffer
Definition: buffer.h:94
Protocol header serialization and deserialization.
Definition: header.h:44
Class to hold all the metadata.
Iterator class for metadata items.
uint16_t m_current
current position
Buffer m_buffer
buffer the metadata refers to
const PacketMetadata * m_metadata
pointer to the metadata
bool m_hasReadTail
true if the metadata tail has been read
bool HasNext() const
Checks if there is another metadata item.
Item Next()
Retrieve the next metadata item.
ItemIterator(const PacketMetadata *metadata, Buffer buffer)
Constructor.
Handle packet metadata about packet headers and trailers.
static uint8_t * ReadFromRawU64(uint64_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
uint32_t GetSerializedSize() const
Get the metadata serialized size.
void ReplaceTail(PacketMetadata::SmallItem *item, PacketMetadata::ExtraItem *extraItem, uint32_t available)
Replace the tail.
PacketMetadata CreateFragment(uint32_t start, uint32_t end) const
Creates a fragment.
PacketMetadata & operator=(const PacketMetadata &o)
Basic assignment.
void ReserveCopy(uint32_t n)
Reserve space and make a metadata copy.
void AddHeader(const Header &header, uint32_t size)
Add an header.
void AppendValueExtra(uint32_t value, uint8_t *buffer)
Append a value to the buffer - extra.
void Append32(uint32_t value, uint8_t *buffer)
Append a 32-bit value to the buffer.
static uint32_t m_maxSize
maximum metadata size
static bool m_enableChecking
Enable the packet metadata checking.
void UpdateHead(uint16_t written)
Update the head.
uint64_t GetUid() const
Get the packet Uid.
uint16_t m_head
list head
void AppendValue(uint32_t value, uint8_t *buffer)
Append a value to the buffer.
static void Deallocate(struct PacketMetadata::Data *data)
Deallocate the buffer memory.
static uint8_t * ReadFromRawU32(uint32_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
static uint8_t * AddToRawU32(const uint32_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
static struct PacketMetadata::Data * Allocate(uint32_t n)
Allocate a buffer data storage.
static bool m_enable
Enable the packet metadata.
struct Data * m_data
Metadata storage.
void AddAtEnd(const PacketMetadata &o)
Add a metadata at the metadata start.
void Reserve(uint32_t n)
Reserve space.
uint32_t ReadUleb128(const uint8_t **pBuffer) const
Read a ULEB128 (Unsigned Little Endian Base 128) coded number.
ItemIterator BeginItem(Buffer buffer) const
Initialize the item iterator to the buffer begin.
static void EnableChecking()
Enable the packet metadata checking.
void RemoveAtEnd(uint32_t end)
Remove a chunk of metadata at the metadata end.
uint32_t ReadItems(uint16_t current, struct PacketMetadata::SmallItem *item, struct PacketMetadata::ExtraItem *extraItem) const
Read items.
void RemoveHeader(const Header &header, uint32_t size)
Remove an header.
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Deserialization from raw uint8_t*.
void AddPaddingAtEnd(uint32_t end)
Add some padding at the end.
static uint8_t * ReadFromRawU16(uint16_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
static struct PacketMetadata::Data * Create(uint32_t size)
Create a buffer data storage.
static uint16_t m_chunkUid
Chunk Uid.
void RemoveAtStart(uint32_t start)
Remove a chunk of metadata at the metadata start.
uint16_t AddSmall(const PacketMetadata::SmallItem *item)
Add a SmallItem.
static DataFreeList m_freeList
the metadata data storage
uint32_t GetUleb128Size(uint32_t value) const
Get the ULEB128 (Unsigned Little Endian Base 128) size.
uint32_t GetTotalSize() const
Get the total size used by the metadata.
void Append16(uint16_t value, uint8_t *buffer)
Append a 16-bit value to the buffer.
void RemoveTrailer(const Trailer &trailer, uint32_t size)
Remove a trailer.
static bool m_metadataSkipped
Set to true when adding metadata to a packet is skipped because m_enable is false; used to detect ena...
bool IsPointerOk(uint16_t pointer) const
Check if the position is valid.
bool IsStateOk() const
Check if the metadata state is ok.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialization to raw uint8_t*.
bool IsSharedPointerOk(uint16_t pointer) const
Check if the position is valid.
static uint8_t * AddToRawU8(const uint8_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
uint16_t m_tail
list tail
void UpdateTail(uint16_t written)
Update the tail.
uint64_t m_packetUid
packet Uid
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.
static void Recycle(struct PacketMetadata::Data *data)
Recycle the buffer memory.
void DoAddHeader(uint32_t uid, uint32_t size)
Add an header.
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)
static uint8_t * ReadFromRawU8(uint8_t &data, const uint8_t *start, const uint8_t *current, uint32_t maxSize)
Helper for the raw deserialization.
void AddTrailer(const Trailer &trailer, uint32_t size)
Add a trailer.
static uint8_t * AddToRawU16(const uint16_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
static uint8_t * AddToRawU64(const uint64_t &data, uint8_t *start, uint8_t *current, uint32_t maxSize)
Helper for the raw serialization.
static void Enable()
Enable the packet metadata.
uint16_t m_used
used portion
Protocol trailer serialization and deserialization.
Definition: trailer.h:41
a unique identifier for an interface.
Definition: type-id.h:60
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:481
Every class exported by the ns3 library is enclosed in the ns3 namespace.
value
Definition: second.py:41
def start()
Definition: core.py:1861
#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
uint8_t data[writeSize]
uint16_t m_size
size (in bytes) of m_data buffer below
uint8_t m_data[PACKET_METADATA_DATA_M_DATA_SIZE]
variable-sized buffer of bytes
uint32_t m_count
number of references to this struct Data instance.
uint16_t m_dirtyEnd
max of the m_used field over all objects which reference this struct Data instance
uint32_t fragmentEnd
offset (in bytes) from start of original header to the end of the fragment still present.
uint64_t packetUid
the packetUid of the packet in which this header or trailer was first added.
uint32_t fragmentStart
offset (in bytes) from start of original header to the start of the fragment still present.
structure describing a packet metadata item
TypeId tid
TypeId of Header or Trailer.
enum ns3::PacketMetadata::Item::ItemType type
metadata type
bool isFragment
true: this is a fragmented header, trailer, or, payload.
uint32_t currentTrimedFromEnd
how many bytes were trimmed from the end of a fragment.
uint32_t currentTrimedFromStart
how many bytes were trimmed from the start of a fragment.
Buffer::Iterator current
an iterator which can be fed to Deserialize.
ItemType
Type of data in the packet.
uint32_t currentSize
size of item.
uint16_t next
offset (in bytes) from start of m_data buffer to next element in linked list.
uint16_t chunkUid
this field tries to uniquely identify each header or trailer instance while the typeUid field uniquel...
uint32_t typeUid
the high 31 bits of this field identify the type of the header or trailer represented by this item: t...
uint32_t size
the size (in bytes) of the header or trailer represented by this element.
uint16_t prev
offset (in bytes) from start of m_data buffer to previous element in linked list.