replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2005,2006 INRIA
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;
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.
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
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
27 #include "packet-metadata.h"
29 #include "byte-tag-list.h"
30 #include "packet-tag-list.h"
31 #include "nix-vector.h"
32 #include "ns3/callback.h"
33 #include "ns3/assert.h"
40 * \defgroup packet Packet
45 * \brief Iterator over the set of tags in a packet
47 * This is a java-style iterator.
53 * Identifies a tag and a set of bytes within a packet
54 * to which the tag applies.
60 * \returns the ns3::TypeId associated to this tag.
62 TypeId GetTypeId (void) const;
64 * \returns the index of the first byte tagged by this tag.
66 * The index is an offset from the start of the packet.
68 uint32_t GetStart (void) const;
70 * \returns the index of the last byte tagged by this tag.
72 * The index is an offset from the start of the packet.
74 uint32_t GetEnd (void) const;
76 * \param tag the user tag to which the data should be copied.
78 * Read the requested tag and store it in the user-provided
79 * tag instance. This method will crash if the type of the
80 * tag provided by the user does not match the type of
83 void GetTag (Tag &tag) const;
85 friend class ByteTagIterator;
86 Item (TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer);
93 * \returns true if calling Next is safe, false otherwise.
95 bool HasNext (void) const;
97 * \returns the next item found and prepare for the next one.
102 ByteTagIterator (ByteTagList::Iterator i);
103 ByteTagList::Iterator m_current;
108 * \brief Iterator over the set of 'packet' tags in a packet
110 * This is a java-style iterator.
112 class PacketTagIterator
116 * Identifies a tag within a packet.
122 * \returns the ns3::TypeId associated to this tag.
124 TypeId GetTypeId (void) const;
126 * \param tag the user tag to which the data should be copied.
128 * Read the requested tag and store it in the user-provided
129 * tag instance. This method will crash if the type of the
130 * tag provided by the user does not match the type of
131 * the underlying tag.
133 void GetTag (Tag &tag) const;
135 friend class PacketTagIterator;
136 Item (const struct PacketTagList::TagData *data);
137 const struct PacketTagList::TagData *m_data;
140 * \returns true if calling Next is safe, false otherwise.
142 bool HasNext (void) const;
144 * \returns the next item found and prepare for the next one.
149 PacketTagIterator (const struct PacketTagList::TagData *head);
150 const struct PacketTagList::TagData *m_current;
155 * \brief network packets
157 * Each network packet contains a byte buffer, a set of byte tags, a set of
158 * packet tags, and metadata.
160 * - The byte buffer stores the serialized content of the headers and trailers
161 * added to a packet. The serialized representation of these headers is expected
162 * to match that of real network packets bit for bit (although nothing
163 * forces you to do this) which means that the content of a packet buffer
164 * is expected to be that of a real packet.
166 * - The metadata describes the type of the headers and trailers which
167 * were serialized in the byte buffer. The maintenance of metadata is
168 * optional and disabled by default. To enable it, you must call
169 * Packet::EnablePrinting and this will allow you to get non-empty
170 * output from Packet::Print and Packet::Print. If you wish to only enable
171 * checking of metadata, and do not need any printing capability, you can
172 * call Packet::EnableChecking: its runtime cost is lower than Packet::EnablePrinting.
174 * - The set of tags contain simulation-specific information which cannot
175 * be stored in the packet byte buffer because the protocol headers or trailers
176 * have no standard-conformant field for this information. So-called
177 * 'byte' tags are used to tag a subset of the bytes in the packet byte buffer
178 * while 'packet' tags are used to tag the packet itself. The main difference
179 * between these two kinds of tags is what happens when packets are copied,
180 * fragmented, and reassembled: 'byte' tags follow bytes while 'packet' tags
181 * follow packets. Another important difference between these two kinds of tags
182 * is that byte tags cannot be removed and are expected to be written once,
183 * and read many times, while packet tags are expected to be written once,
184 * read many times, and removed exactly once. An example of a 'byte'
185 * tag is a FlowIdTag which contains a flow id and is set by the application
186 * generating traffic. An example of a 'packet' tag is a cross-layer
187 * qos class id set by an application and processed by a lower-level MAC
190 * Implementing a new type of Header or Trailer for a new protocol is
191 * pretty easy and is a matter of creating a subclass of the ns3::Header
192 * or of the ns3::Trailer base class, and implementing the methods
193 * described in their respective API documentation.
195 * Implementing a new type of Tag requires roughly the same amount of
196 * work and this work is described in the ns3::Tag API documentation.
198 * The performance aspects of the Packet API are discussed in
201 class Packet : public SimpleRefCount<Packet>
204 Ptr<Packet> Copy (void) const;
207 * Create an empty packet with a new uid (as returned
211 Packet (const Packet &o);
212 Packet &operator = (const Packet &o);
214 * Create a packet with a zero-filled payload.
215 * The memory necessary for the payload is not allocated:
216 * it will be allocated at any later point if you attempt
217 * to fragment this packet or to access the zero-filled
218 * bytes. The packet is allocated with a new uid (as
219 * returned by getUid).
221 * \param size the size of the zero-filled payload
223 Packet (uint32_t size);
225 * Create a packet with payload filled with the content
226 * of this buffer. The input data is copied: the input
227 * buffer is untouched.
229 * \param buffer the data to store in the packet.
230 * \param size the size of the input buffer.
232 Packet (uint8_t const*buffer, uint32_t size);
234 * Create a new packet which contains a fragment of the original
235 * packet. The returned packet shares the same uid as this packet.
237 * \param start offset from start of packet to start of fragment to create
238 * \param length length of fragment to create
239 * \returns a fragment of the original packet
241 Ptr<Packet> CreateFragment (uint32_t start, uint32_t length) const;
243 * \returns the size in bytes of the packet (including the zero-filled
246 uint32_t GetSize (void) const;
248 * Add header to this packet. This method invokes the
249 * Header::GetSerializedSize and Header::Serialize
250 * methods to reserve space in the buffer and request the
251 * header to serialize itself in the packet buffer.
253 * \param header a reference to the header to add to this packet.
255 void AddHeader (const Header & header);
257 * Deserialize and remove the header from the internal buffer.
258 * This method invokes Header::Deserialize.
260 * \param header a reference to the header to remove from the internal buffer.
261 * \returns the number of bytes removed from the packet.
263 uint32_t RemoveHeader (Header &header);
265 * Deserialize but does _not_ remove the header from the internal buffer.
266 * This method invokes Header::Deserialize.
268 * \param header a reference to the header to read from the internal buffer.
269 * \returns the number of bytes read from the packet.
271 uint32_t PeekHeader (Header &header) const;
273 * Add trailer to this packet. This method invokes the
274 * Trailer::GetSerializedSize and Trailer::Serialize
275 * methods to reserve space in the buffer and request the trailer
276 * to serialize itself in the packet buffer.
278 * \param trailer a reference to the trailer to add to this packet.
280 void AddTrailer (const Trailer &trailer);
282 * Remove a deserialized trailer from the internal buffer.
283 * This method invokes the Deserialize method.
285 * \param trailer a reference to the trailer to remove from the internal buffer.
286 * \returns the number of bytes removed from the end of the packet.
288 uint32_t RemoveTrailer (Trailer &trailer);
290 * Deserialize but does _not_ remove a trailer from the internal buffer.
291 * This method invokes the Trailer::Deserialize method.
293 * \param trailer a reference to the trailer to read from the internal buffer.
294 * \returns the number of bytes read from the end of the packet.
296 uint32_t PeekTrailer (Trailer &trailer);
299 * Concatenate the input packet at the end of the current
300 * packet. This does not alter the uid of either packet.
302 * \param packet packet to concatenate
304 void AddAtEnd (Ptr<const Packet> packet);
306 * \param size number of padding bytes to add.
308 void AddPaddingAtEnd (uint32_t size);
310 * Remove size bytes from the end of the current packet
311 * It is safe to remove more bytes that what is present in
314 * \param size number of bytes from remove
316 void RemoveAtEnd (uint32_t size);
318 * Remove size bytes from the start of the current packet.
319 * It is safe to remove more bytes that what is present in
322 * \param size number of bytes from remove
324 void RemoveAtStart (uint32_t size);
327 * If you try to change the content of the buffer
328 * returned by this method, you will die.
330 * \returns a pointer to the internal buffer of the packet.
332 uint8_t const *PeekData (void) const;
335 * \param buffer a pointer to a byte buffer where the packet data
337 * \param size the size of the byte buffer.
338 * \returns the number of bytes read from the packet
340 * No more than \b size bytes will be copied by this function.
342 uint32_t CopyData (uint8_t *buffer, uint32_t size) const;
344 void CopyData(std::ostream *os, uint32_t size) const;
347 * A packet is allocated a new uid when it is created
348 * empty or with zero-filled payload.
350 * Note: This uid is an internal uid and cannot be counted on to
351 * provide an accurate counter of how many "simulated packets" of a
352 * particular protocol are in the system. It is not trivial to make
353 * this uid into such a counter, because of questions such as what
354 * should the uid be when the packet is sent over broadcast media, or
355 * when fragmentation occurs. If a user wants to trace actual packet
356 * counts, he or she should look at e.g. the IP ID field or transport
357 * sequence numbers, or other packet or frame counters at other
360 * \returns an integer identifier which uniquely
361 * identifies this packet.
363 uint32_t GetUid (void) const;
366 * \param os output stream in which the data should be printed.
368 * Iterate over the headers and trailers present in this packet,
369 * from the first header to the last trailer and invoke, for
370 * each of them, the user-provided method Header::DoPrint or
371 * Trailer::DoPrint methods.
373 void Print (std::ostream &os) const;
375 PacketMetadata::ItemIterator BeginItem (void) const;
378 * By default, packets do not keep around enough metadata to
379 * perform the operations requested by the Print methods. If you
380 * want to be able to invoke any of the two ::Print methods,
381 * you need to invoke this method at least once during the
382 * simulation setup and before any packet is created.
384 static void EnablePrinting (void);
386 * The packet metadata is also used to perform extensive
387 * sanity checks at runtime when performing operations on a
388 * Packet. For example, this metadata is used to verify that
389 * when you remove a header from a packet, this same header
390 * was actually present at the front of the packet. These
391 * errors will be detected and will abort the program.
393 static void EnableChecking (void);
396 * \returns a byte buffer
398 * This method creates a serialized representation of a Packet object
399 * ready to be transmitted over a network to another system. This
400 * serialized representation contains a copy of the packet byte buffer,
401 * the tag list, and the packet metadata (if there is one).
403 * This method will trigger calls to the Serialize and GetSerializedSize
404 * methods of each tag stored in this packet.
406 * This method will typically be used by parallel simulations where
407 * the simulated system is partitioned and each partition runs on
410 Buffer Serialize (void) const;
412 * \param buffer a byte buffer
414 * This method reads a byte buffer as created by Packet::Serialize
415 * and restores the state of the Packet to what it was prior to
418 * This method will trigger calls to the Deserialize method
419 * of each tag stored in this packet.
421 * This method will typically be used by parallel simulations where
422 * the simulated system is partitioned and each partition runs on
425 void Deserialize (Buffer buffer);
428 * \param tag the new tag to add to this packet
430 * Tag each byte included in this packet with the
433 * Note that adding a tag is a const operation which is pretty
434 * un-intuitive. The rationale is that the content and behavior of
435 * a packet is _not_ changed when a tag is added to a packet: any
436 * code which was not aware of the new tag is going to work just
437 * the same if the new tag is added. The real reason why adding a
438 * tag was made a const operation is to allow a trace sink which gets
439 * a packet to tag the packet, even if the packet is const (and most
440 * trace sources should use const packets because it would be
441 * totally evil to allow a trace sink to modify the content of a
444 void AddByteTag (const Tag &tag) const;
446 * \returns an iterator over the set of byte tags included in this packet.
448 ByteTagIterator GetByteTagIterator (void) const;
450 * \param tag the tag to search in this packet
451 * \returns true if the requested tag type was found, false otherwise.
453 * If the requested tag type is found, it is copied in the user's
454 * provided tag instance.
456 bool FindFirstMatchingByteTag (Tag &tag) const;
459 * Remove all the tags stored in this packet.
461 void RemoveAllByteTags (void);
464 * \param os output stream in which the data should be printed.
466 * Iterate over the tags present in this packet, and
467 * invoke the Print method of each tag stored in the packet.
469 void PrintByteTags (std::ostream &os) const;
472 * \param tag the tag to store in this packet
474 * Add a tag to this packet. This method calls the
475 * Tag::GetSerializedSize and, then, Tag::Serialize.
477 * Note that this method is const, that is, it does not
478 * modify the state of this packet, which is fairly
481 void AddPacketTag (const Tag &tag) const;
483 * \param tag the tag to remove from this packet
484 * \returns true if the requested tag is found, false
487 * Remove a tag from this packet. This method calls
488 * Tag::Deserialize if the tag is found.
490 bool RemovePacketTag (Tag &tag);
492 * \param tag the tag to search in this packet
493 * \returns true if the requested tag is found, false
496 * Search a matching tag and call Tag::Deserialize if it is found.
498 bool PeekPacketTag (Tag &tag) const;
500 * Remove all packet tags.
502 void RemoveAllPacketTags (void);
505 * \param os the stream in which we want to print data.
507 * Print the list of 'packet' tags.
509 * \sa Packet::AddPacketTag, Packet::RemovePacketTag, Packet::PeekPacketTag,
510 * Packet::RemoveAllPacketTags
512 void PrintPacketTags (std::ostream &os) const;
515 * \returns an object which can be used to iterate over the list of
518 PacketTagIterator GetPacketTagIterator (void) const;
520 /* Note: These functions support a temporary solution
521 * to a specific problem in this generic class, i.e.
522 * how to associate something specific like nix-vector
523 * with a packet. This design methodology
524 * should _not_ be followed, and is only here as an
525 * impetus to fix this general issue. */
526 void SetNixVector (Ptr<NixVector>);
527 Ptr<NixVector> GetNixVector (void) const;
530 Packet (const Buffer &buffer, const ByteTagList &byteTagList,
531 const PacketTagList &packetTagList, const PacketMetadata &metadata);
533 ByteTagList m_byteTagList;
534 PacketTagList m_packetTagList;
535 PacketMetadata m_metadata;
537 /* Please see comments above about nix-vector */
538 Ptr<NixVector> m_nixVector;
540 static uint32_t m_globalUid;
543 std::ostream& operator<< (std::ostream& os, const Packet &packet);
547 * \defgroup packetperf Packet Performance
548 * The current implementation of the byte buffers and tag list is based
549 * on COW (Copy On Write. An introduction to COW can be found in Scott
550 * Meyer's "More Effective C++", items 17 and 29). What this means is that
551 * copying packets without modifying them is very cheap (in terms of cpu
552 * and memory usage) and modifying them can be also very cheap. What is
553 * key for proper COW implementations is being
554 * able to detect when a given modification of the state of a packet triggers
555 * a full copy of the data prior to the modification: COW systems need
556 * to detect when an operation is "dirty".
559 * - ns3::Packet::AddHeader
560 * - ns3::Packet::AddTrailer
561 * - both versions of ns3::Packet::AddAtEnd
562 * - ns3::Packet::RemovePacketTag
564 * Non-dirty operations:
565 * - ns3::Packet::AddPacketTag
566 * - ns3::Packet::PeekPacketTag
567 * - ns3::Packet::RemoveAllPacketTags
568 * - ns3::Packet::AddByteTag
569 * - ns3::Packet::FindFirstMatchingByteTag
570 * - ns3::Packet::RemoveAllByteTags
571 * - ns3::Packet::RemoveHeader
572 * - ns3::Packet::RemoveTrailer
573 * - ns3::Packet::CreateFragment
574 * - ns3::Packet::RemoveAtStart
575 * - ns3::Packet::RemoveAtEnd
576 * - ns3::Packet::CopyData
578 * Dirty operations will always be slower than non-dirty operations,
579 * sometimes by several orders of magnitude. However, even the
580 * dirty operations have been optimized for common use-cases which
581 * means that most of the time, these operations will not trigger
582 * data copies and will thus be still very fast.
587 #endif /* PACKET_H */