A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
byte-tag-list.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef BYTE_TAG_LIST_H
9#define BYTE_TAG_LIST_H
10
11#define __STDC_LIMIT_MACROS
12#include "tag-buffer.h"
13
14#include "ns3/type-id.h"
15
16#include <stdint.h>
17
18namespace ns3
19{
20
21struct ByteTagListData;
22
23/**
24 * @ingroup packet
25 *
26 * @brief keep track of the byte tags stored in a packet.
27 *
28 * This class is mostly private to the Packet implementation and users
29 * should never have to access it directly.
30 *
31 * @internal
32 * The implementation of this class is a bit tricky so, there are a couple
33 * of things to keep in mind here:
34 *
35 * - It stores all tags in a single byte buffer: each tag is stored
36 * as 4 32bit integers (TypeId, tag data size, start, end) followed
37 * by the tag data as generated by Tag::Serialize.
38 *
39 * - The struct ByteTagListData structure which contains the tag byte buffer
40 * is shared and, thus, reference-counted. This data structure is unshared
41 * as-needed to emulate COW semantics.
42 *
43 * - Each tag tags a unique set of bytes identified by the pair of offsets
44 * (start,end). These offsets are relative to the start of the packet
45 * Whenever the origin of the offset changes, the Packet adjusts all
46 * byte tags using ByteTagList::Adjust method.
47 *
48 * - When packet is reduced in size, byte tags that span outside the packet
49 * boundaries remain in ByteTagList. It is not a problem as iterator fixes
50 * the boundaries before returning item. However, when packet is extending,
51 * it calls ByteTagList::AddAtStart or ByteTagList::AddAtEnd to cut byte
52 * tags that will otherwise cover new bytes.
53 */
55{
56 public:
57 /**
58 * @brief An iterator for iterating through a byte tag list
59 *
60 * An iterator for iterating through a byte tag list
61 *
62 */
64 {
65 public:
66 /**
67 * @brief An item specifies an individual tag within a byte buffer
68 *
69 * An item specifies an individual tag within a byte buffer
70 *
71 */
72 struct Item
73 {
74 TypeId tid; //!< type of the tag
75 uint32_t size; //!< size of tag data
76 int32_t start; //!< offset to the start of the tag from the virtual byte buffer
77 int32_t end; //!< offset to the end of the tag from the virtual byte buffer
78 TagBuffer buf; //!< the data for the tag as generated by Tag::Serialize
79
80 /// Constructs an item with the given TagBuffer
81 /// @param buf The Tag Buffer
83
84 private:
85 /// Friend class
86 friend class ByteTagList;
87 /// Friend class
89 };
90
91 /**
92 * @brief Used to determine if the iterator is at the end of the byteTagList
93 *
94 * @returns true if there are more Items in the list
95 */
96 bool HasNext() const;
97
98 /**
99 * @brief Returns the next Item from the ByteTagList
100 *
101 * @returns the next Item in the ByteTagList
102 */
104
105 /**
106 * @brief Returns the offset from the start of the virtual byte buffer to the ByteTagList
107 *
108 * @returns offset to the start of this ByteTagList
109 */
110 uint32_t GetOffsetStart() const;
111
112 private:
113 /// Friend class
114 friend class ByteTagList;
115
116 /**
117 * @brief Constructor
118 * @param start Starting tag
119 * @param end End tag
120 * @param offsetStart offset to the start of the tag from the virtual byte buffer
121 * @param offsetEnd offset to the end of the tag from the virtual byte buffer
122 * @param adjustment adjustment to byte tag offsets
123 */
124 Iterator(uint8_t* start,
125 uint8_t* end,
126 int32_t offsetStart,
127 int32_t offsetEnd,
128 int32_t adjustment);
129
130 /**
131 * @brief Prepare the iterator for the next tag
132 */
133 void PrepareForNext();
134 uint8_t* m_current; //!< Current tag
135 uint8_t* m_end; //!< End tag
136 int32_t m_offsetStart; //!< Offset to the start of the tag from the virtual byte buffer
137 int32_t m_offsetEnd; //!< Offset to the end of the tag from the virtual byte buffer
138 int32_t m_adjustment; //!< Adjustment to byte tag offsets
139 uint32_t m_nextTid; //!< TypeId of the next tag
140 uint32_t m_nextSize; //!< Size of the next tag
141 int32_t m_nextStart; //!< Start of the next tag
142 int32_t m_nextEnd; //!< End of the next tag
143 };
144
145 ByteTagList();
146
147 /**
148 *
149 * Copy constructor, copies the data and increases reference count
150 *
151 * @param o The ByteTagList to copy
152 *
153 */
154 ByteTagList(const ByteTagList& o);
155
156 /**
157 *
158 * Assignment operator, deallocates current data and assigns
159 * value of passed in ByteTagList. Also increases reference count
160 *
161 * @param o reference to the ByteTagList to copy
162 * @returns reference to the assignee
163 *
164 */
166 ~ByteTagList();
167
168 /**
169 * @param tid the typeid of the tag added
170 * @param bufferSize the size of the tag when its serialization will
171 * be completed. Typically, the return value of Tag::GetSerializedSize
172 * @param start offset which uniquely identifies the first byte tagged by this tag.
173 * @param end offset which uniquely identifies the last byte tagged by this tag.
174 * @returns a buffer which can be used to write the tag data.
175 *
176 *
177 */
178 TagBuffer Add(TypeId tid, uint32_t bufferSize, int32_t start, int32_t end);
179
180 /**
181 * @param o the other list of tags to aggregate.
182 *
183 * Aggregate the two lists of tags.
184 */
185 void Add(const ByteTagList& o);
186
187 /**
188 *
189 * Removes all of the tags from the ByteTagList
190 */
191 void RemoveAll();
192
193 /**
194 * @param offsetStart the offset which uniquely identifies the first data byte
195 * present in the byte buffer associated to this ByteTagList.
196 * @param offsetEnd the offset which uniquely identifies the last data byte
197 * present in the byte buffer associated to this ByteTagList.
198 * @returns an iterator
199 *
200 * The returned iterator will allow you to loop through the set of tags present
201 * in this list: the boundaries of each tag as reported by their start and
202 * end offsets will be included within the input offsetStart and offsetEnd.
203 */
204 ByteTagList::Iterator Begin(int32_t offsetStart, int32_t offsetEnd) const;
205
206 /**
207 * Adjust the offsets stored internally by the adjustment delta.
208 *
209 * @param adjustment value to change stored offsets by
210 */
211 inline void Adjust(int32_t adjustment);
212
213 /**
214 * Make sure that all offsets are smaller than appendOffset which represents
215 * the location where new bytes have been added to the byte buffer.
216 *
217 * @param appendOffset maximum offset value
218 *
219 */
220 void AddAtEnd(int32_t appendOffset);
221 /**
222 * Make sure that all offsets are bigger than prependOffset which represents
223 * the location where new bytes have been added to the byte buffer.
224 *
225 * @param prependOffset minimum offset value
226 *
227 */
228 void AddAtStart(int32_t prependOffset);
229 /**
230 * Returns number of bytes required for packet serialization.
231 *
232 * @returns number of bytes required for packet serialization
233 */
235 /**
236 * Serialize the tag list into a byte buffer.
237 *
238 * @param [in,out] buffer The byte buffer to which the tag list will be serialized
239 * @param [in] maxSize Max The max size of the buffer for bounds checking
240 *
241 * @returns zero if complete tag list is not serialized
242 */
243 uint32_t Serialize(uint32_t* buffer, uint32_t maxSize) const;
244 /**
245 * Deserialize tag list from the provided buffer.
246 *
247 * @param [in] buffer The buffer to read from.
248 * @param [in] size The number of bytes to deserialize.
249 *
250 * @returns zero if complete tag list is not deserialized
251 */
252 uint32_t Deserialize(const uint32_t* buffer, uint32_t size);
253
254 private:
255 /**
256 * @brief Returns an iterator pointing to the very first tag in this list.
257 *
258 * @returns an iterator
259 */
261
262 /**
263 * @brief Allocate the memory for the ByteTagListData
264 * @param size the memory to allocate
265 * @returns the ByteTagListData structure
266 */
268
269 /**
270 * @brief Deallocates a ByteTagListData
271 * @param data the ByteTagListData to deallocate
272 */
274
275 int32_t m_minStart; //!< minimal start offset
276 int32_t m_maxEnd; //!< maximal end offset
277 int32_t m_adjustment; //!< adjustment to byte tag offsets
278 uint32_t m_used; //!< the number of used bytes in the buffer
279 ByteTagListData* m_data; //!< the ByteTagListData structure
280};
281
282void
284{
285 m_adjustment += adjustment;
286}
287
288} // namespace ns3
289
290#endif /* BYTE_TAG_LIST_H */
An iterator for iterating through a byte tag list.
uint8_t * m_current
Current tag.
int32_t m_adjustment
Adjustment to byte tag offsets.
int32_t m_offsetEnd
Offset to the end of the tag from the virtual byte buffer.
ByteTagList::Iterator::Item Next()
Returns the next Item from the ByteTagList.
void PrepareForNext()
Prepare the iterator for the next tag.
int32_t m_nextEnd
End of the next tag.
uint32_t GetOffsetStart() const
Returns the offset from the start of the virtual byte buffer to the ByteTagList.
int32_t m_nextStart
Start of the next tag.
int32_t m_offsetStart
Offset to the start of the tag from the virtual byte buffer.
uint32_t m_nextSize
Size of the next tag.
Iterator(uint8_t *start, uint8_t *end, int32_t offsetStart, int32_t offsetEnd, int32_t adjustment)
Constructor.
bool HasNext() const
Used to determine if the iterator is at the end of the byteTagList.
uint32_t m_nextTid
TypeId of the next tag.
keep track of the byte tags stored in a packet.
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Deserialize tag list from the provided buffer.
void AddAtEnd(int32_t appendOffset)
Make sure that all offsets are smaller than appendOffset which represents the location where new byte...
void Deallocate(ByteTagListData *data)
Deallocates a ByteTagListData.
ByteTagList & operator=(const ByteTagList &o)
Assignment operator, deallocates current data and assigns value of passed in ByteTagList.
void Adjust(int32_t adjustment)
Adjust the offsets stored internally by the adjustment delta.
ByteTagList::Iterator Begin(int32_t offsetStart, int32_t offsetEnd) const
int32_t m_minStart
minimal start offset
uint32_t m_used
the number of used bytes in the buffer
ByteTagListData * Allocate(uint32_t size)
Allocate the memory for the ByteTagListData.
ByteTagList::Iterator BeginAll() const
Returns an iterator pointing to the very first tag in this list.
void RemoveAll()
Removes all of the tags from the ByteTagList.
int32_t m_adjustment
adjustment to byte tag offsets
TagBuffer Add(TypeId tid, uint32_t bufferSize, int32_t start, int32_t end)
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Serialize the tag list into a byte buffer.
void AddAtStart(int32_t prependOffset)
Make sure that all offsets are bigger than prependOffset which represents the location where new byte...
int32_t m_maxEnd
maximal end offset
ByteTagListData * m_data
the ByteTagListData structure
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
read and write tag data
Definition tag-buffer.h:41
a unique identifier for an interface.
Definition type-id.h:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
An item specifies an individual tag within a byte buffer.
TagBuffer buf
the data for the tag as generated by Tag::Serialize
uint32_t size
size of tag data
int32_t end
offset to the end of the tag from the virtual byte buffer
Item(TagBuffer buf)
Constructs an item with the given TagBuffer.
int32_t start
offset to the start of the tag from the virtual byte buffer
Internal representation of the byte tags stored in a packet.