A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 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_H
20#define PACKET_H
21
22#include "buffer.h"
23#include "byte-tag-list.h"
24#include "header.h"
25#include "nix-vector.h"
26#include "packet-metadata.h"
27#include "packet-tag-list.h"
28#include "tag.h"
29#include "trailer.h"
30
31#include "ns3/assert.h"
32#include "ns3/callback.h"
33#include "ns3/mac48-address.h"
34#include "ns3/ptr.h"
35
36#include <stdint.h>
37
38namespace ns3
39{
40
41// Forward declaration
42class Address;
43
44/**
45 * \ingroup network
46 * \defgroup packet Packet
47 */
48
49/**
50 * \ingroup packet
51 * \brief Iterator over the set of byte tags in a packet
52 *
53 * This is a java-style iterator.
54 */
56{
57 public:
58 /**
59 * \brief Identifies a byte tag and a set of bytes within a packet
60 * to which the tag applies.
61 */
62 class Item
63 {
64 public:
65 /**
66 * \returns the ns3::TypeId associated to this tag.
67 */
68 TypeId GetTypeId() const;
69 /**
70 * \returns the index of the first byte tagged by this tag.
71 *
72 * \brief The index is an offset from the start of the packet.
73 */
74 uint32_t GetStart() const;
75 /**
76 * \returns the index of the last byte tagged by this tag.
77 *
78 * \brief The index is an offset from the start of the packet.
79 */
80 uint32_t GetEnd() const;
81 /**
82 * \brief Read the requested tag and store it in the user-provided tag instance.
83 *
84 * \param tag the user tag to which the data should be copied.
85 *
86 * This method will crash if the type of the tag provided
87 * by the user does not match the type of the underlying tag.
88 */
89 void GetTag(Tag& tag) const;
90
91 private:
92 /// Friend class
93 friend class ByteTagIterator;
94 /**
95 * \brief Constructor
96 * \param tid the ns3::TypeId associated to this tag.
97 * \param start the index of the first byte tagged by this tag.
98 * \param end the index of the last byte tagged by this tag.
99 * \param buffer the buffer associated with this tag.
100 */
101 Item(TypeId tid, uint32_t start, uint32_t end, TagBuffer buffer);
102
103 TypeId m_tid; //!< the ns3::TypeId associated to this tag.
104 uint32_t m_start; //!< the index of the first byte tagged by this tag.
105 uint32_t m_end; //!< the index of the last byte tagged by this tag.
106 TagBuffer m_buffer; //!< the buffer associated with this tag.
107 };
108
109 /**
110 * \returns true if calling Next is safe, false otherwise.
111 */
112 bool HasNext() const;
113 /**
114 * \returns the next item found and prepare for the next one.
115 */
116 Item Next();
117
118 private:
119 /// Friend class
120 friend class Packet;
121 /**
122 * Copy Constructor
123 * \param i object to copy
124 */
126 ByteTagList::Iterator m_current; //!< actual position over the set of byte tags in a packet
127};
128
129/**
130 * \ingroup packet
131 * \brief Iterator over the set of packet tags in a packet
132 *
133 * This is a java-style iterator.
134 */
136{
137 public:
138 /**
139 * Identifies a packet tag within a packet.
140 */
141 class Item
142 {
143 public:
144 /**
145 * \returns the ns3::TypeId associated to this tag.
146 */
147 TypeId GetTypeId() const;
148 /**
149 * Read the requested tag and store it in the user-provided tag instance.
150 *
151 * \param tag the user tag to which the data should be copied.
152 *
153 * This method will crash if the type of the tag provided
154 * by the user does not match the type of the underlying tag.
155 */
156 void GetTag(Tag& tag) const;
157
158 private:
159 /// Friend class
160 friend class PacketTagIterator;
161 /**
162 * Constructor
163 * \param data the data to copy.
164 */
166 const PacketTagList::TagData* m_data; //!< the tag data
167 };
168
169 /**
170 * \returns true if calling Next is safe, false otherwise.
171 */
172 bool HasNext() const;
173 /**
174 * \returns the next item found and prepare for the next one.
175 */
176 Item Next();
177
178 private:
179 /// Friend class
180 friend class Packet;
181 /**
182 * Constructor
183 * \param head head of the items
184 */
186 const PacketTagList::TagData* m_current; //!< actual position over the set of tags in a packet
187};
188
189/**
190 * \ingroup packet
191 * \brief network packets
192 *
193 * Each network packet contains a byte buffer, a set of byte tags, a set of
194 * packet tags, and metadata.
195 *
196 * - The byte buffer stores the serialized content of the headers and trailers
197 * added to a packet. The serialized representation of these headers is expected
198 * to match that of real network packets bit for bit (although nothing
199 * forces you to do this) which means that the content of a packet buffer
200 * is expected to be that of a real packet.
201 *
202 * - The metadata describes the type of the headers and trailers which
203 * were serialized in the byte buffer. The maintenance of metadata is
204 * optional and disabled by default. To enable it, you must call
205 * Packet::EnablePrinting and this will allow you to get non-empty
206 * output from Packet::Print. If you wish to only enable
207 * checking of metadata, and do not need any printing capability, you can
208 * call Packet::EnableChecking: its runtime cost is lower than
209 * Packet::EnablePrinting.
210 *
211 * - The set of tags contain simulation-specific information which cannot
212 * be stored in the packet byte buffer because the protocol headers or trailers
213 * have no standard-conformant field for this information. So-called
214 * 'byte' tags are used to tag a subset of the bytes in the packet byte buffer
215 * while 'packet' tags are used to tag the packet itself. The main difference
216 * between these two kinds of tags is what happens when packets are copied,
217 * fragmented, and reassembled: 'byte' tags follow bytes while 'packet' tags
218 * follow packets. Another important difference between these two kinds of tags
219 * is that byte tags cannot be removed and are expected to be written once,
220 * and read many times, while packet tags are expected to be written once,
221 * read many times, and removed exactly once. An example of a 'byte'
222 * tag is a FlowIdTag which contains a flow id and is set by the application
223 * generating traffic. An example of a 'packet' tag is a cross-layer
224 * qos class id set by an application and processed by a lower-level MAC
225 * layer.
226 *
227 * Implementing a new type of Header or Trailer for a new protocol is
228 * pretty easy and is a matter of creating a subclass of the ns3::Header
229 * or of the ns3::Trailer base class, and implementing the methods
230 * described in their respective API documentation.
231 *
232 * Implementing a new type of Tag requires roughly the same amount of
233 * work and this work is described in the ns3::Tag API documentation.
234 *
235 * The performance aspects copy-on-write semantics of the
236 * Packet API are discussed in \ref packetperf
237 */
238class Packet : public SimpleRefCount<Packet>
239{
240 public:
241 /**
242 * \brief Create an empty packet with a new uid (as returned
243 * by getUid).
244 */
245 Packet();
246 /**
247 * \brief Copy constructor
248 * \param o object to copy
249 */
250 Packet(const Packet& o);
251 /**
252 * \brief Basic assignment
253 * \param o object to copy
254 * \return the copied object
255 */
256 Packet& operator=(const Packet& o);
257 /**
258 * \brief Create a packet with a zero-filled payload.
259 *
260 * The memory necessary for the payload is not allocated:
261 * it will be allocated at any later point if you attempt
262 * to fragment this packet or to access the zero-filled
263 * bytes. The packet is allocated with a new uid (as
264 * returned by getUid).
265 *
266 * \param size the size of the zero-filled payload
267 */
268 Packet(uint32_t size);
269 /**
270 * \brief Create a new packet from the serialized buffer.
271 *
272 * This new packet
273 * is identical to the serialized packet contained in the buffer
274 * and is magically deserialized for you
275 *
276 * \param buffer the serialized packet to be created
277 * \param size the size of the packet for deserialization
278 * \param magic allows packet deserialization;
279 * asserts when set to false
280 */
281 Packet(const uint8_t* buffer, uint32_t size, bool magic);
282 /**
283 * \brief Create a packet with payload filled with the content
284 * of this buffer.
285 *
286 * The input data is copied: the input
287 * buffer is untouched.
288 *
289 * \param buffer the data to store in the packet.
290 * \param size the size of the input buffer.
291 */
292 Packet(const uint8_t* buffer, uint32_t size);
293 /**
294 * \brief Create a new packet which contains a fragment of the original
295 * packet.
296 *
297 * The returned packet shares the same uid as this packet.
298 *
299 * \param start offset from start of packet to start of fragment to create
300 * \param length length of fragment to create
301 * \returns a fragment of the original packet
302 */
303 Ptr<Packet> CreateFragment(uint32_t start, uint32_t length) const;
304 /**
305 * \brief Returns the the size in bytes of the packet (including the zero-filled
306 * initial payload).
307 *
308 * \returns the size in bytes of the packet
309 */
310 inline uint32_t GetSize() const;
311 /**
312 * \brief Add header to this packet.
313 *
314 * This method invokes the
315 * Header::GetSerializedSize and Header::Serialize
316 * methods to reserve space in the buffer and request the
317 * header to serialize itself in the packet buffer.
318 *
319 * \param header a reference to the header to add to this packet.
320 */
321 void AddHeader(const Header& header);
322 /**
323 * \brief Deserialize and remove the header from the internal buffer.
324 *
325 * This method invokes Header::Deserialize (begin) and should be used for
326 * fixed-length headers.
327 *
328 * \param header a reference to the header to remove from the internal buffer.
329 * \returns the number of bytes removed from the packet.
330 */
332 /**
333 * \brief Deserialize and remove the header from the internal buffer.
334 *
335 * This method invokes Header::Deserialize (begin, end) and should be
336 * used for variable-length headers (where the size is determined somehow
337 * by the caller).
338 *
339 * \param header a reference to the header to remove from the internal buffer.
340 * \param size number of bytes to deserialize
341 * \returns the number of bytes removed from the packet.
342 */
343 uint32_t RemoveHeader(Header& header, uint32_t size);
344 /**
345 * \brief Deserialize but does _not_ remove the header from the internal buffer.
346 * s
347 * This method invokes Header::Deserialize.
348 *
349 * \param header a reference to the header to read from the internal buffer.
350 * \returns the number of bytes read from the packet.
351 */
352 uint32_t PeekHeader(Header& header) const;
353 /**
354 * \brief Deserialize but does _not_ remove the header from the internal buffer.
355 * s
356 * This method invokes Header::Deserialize (begin, end) and should be used
357 * for variable-length headers (where the size is determined somehow
358 * by the caller).
359 *
360 * \param header a reference to the header to read from the internal buffer.
361 * \param size number of bytes to deserialize
362 * \returns the number of bytes read from the packet.
363 */
364 uint32_t PeekHeader(Header& header, uint32_t size) const;
365 /**
366 * \brief Add trailer to this packet.
367 *
368 * This method invokes the
369 * Trailer::GetSerializedSize and Trailer::Serialize
370 * methods to reserve space in the buffer and request the trailer
371 * to serialize itself in the packet buffer.
372 *
373 * \param trailer a reference to the trailer to add to this packet.
374 */
375 void AddTrailer(const Trailer& trailer);
376 /**
377 * \brief Remove a deserialized trailer from the internal buffer.
378 *
379 * This method invokes the Deserialize method.
380 *
381 * \param trailer a reference to the trailer to remove from the internal buffer.
382 * \returns the number of bytes removed from the end of the packet.
383 */
385 /**
386 * \brief Deserialize but does _not_ remove a trailer from the internal buffer.
387 *
388 * This method invokes the Trailer::Deserialize method.
389 *
390 * \param trailer a reference to the trailer to read from the internal buffer.
391 * \returns the number of bytes read from the end of the packet.
392 */
393 uint32_t PeekTrailer(Trailer& trailer);
394
395 /**
396 * \brief Concatenate the input packet at the end of the current
397 * packet.
398 *
399 * This does not alter the uid of either packet.
400 *
401 * \param packet packet to concatenate
402 */
403 void AddAtEnd(Ptr<const Packet> packet);
404 /**
405 * \brief Add a zero-filled padding to the packet.
406 *
407 * \param size number of padding bytes to add.
408 */
409 void AddPaddingAtEnd(uint32_t size);
410 /**
411 * \brief Remove size bytes from the end of the current packet.
412 *
413 * It is safe to remove more bytes than are present in
414 * the packet.
415 *
416 * \param size number of bytes from remove
417 */
418 void RemoveAtEnd(uint32_t size);
419 /**
420 * \brief Remove size bytes from the start of the current packet.
421 *
422 * It is safe to remove more bytes than are present in
423 * the packet.
424 *
425 * \param size number of bytes from remove
426 */
427 void RemoveAtStart(uint32_t size);
428
429 /**
430 * \brief Copy the packet contents to a byte buffer.
431 *
432 * \param buffer a pointer to a byte buffer where the packet data
433 * should be copied.
434 * \param size the size of the byte buffer.
435 * \returns the number of bytes read from the packet
436 *
437 * No more than \b size bytes will be copied by this function.
438 */
439 uint32_t CopyData(uint8_t* buffer, uint32_t size) const;
440
441 /**
442 * \brief Copy the packet contents to an output stream.
443 *
444 * \param os pointer to output stream in which we want
445 * to write the packet data.
446 * \param size the maximum number of bytes we want to write
447 * in the output stream.
448 */
449 void CopyData(std::ostream* os, uint32_t size) const;
450
451 /**
452 * \brief performs a COW copy of the packet.
453 *
454 * \returns a COW copy of the packet.
455 *
456 * The returns packet will behave like an independent copy of
457 * the original packet, even though they both share the
458 * same datasets internally.
459 */
460 Ptr<Packet> Copy() const;
461
462 /**
463 * \brief Returns the packet's Uid.
464 *
465 * A packet is allocated a new uid when it is created
466 * empty or with zero-filled payload.
467 *
468 * Note: This uid is an internal uid and cannot be counted on to
469 * provide an accurate counter of how many "simulated packets" of a
470 * particular protocol are in the system. It is not trivial to make
471 * this uid into such a counter, because of questions such as what
472 * should the uid be when the packet is sent over broadcast media, or
473 * when fragmentation occurs. If a user wants to trace actual packet
474 * counts, he or she should look at e.g. the IP ID field or transport
475 * sequence numbers, or other packet or frame counters at other
476 * protocol layers.
477 *
478 * \returns an integer identifier which uniquely
479 * identifies this packet.
480 */
481 uint64_t GetUid() const;
482
483 /**
484 * \brief Print the packet contents.
485 *
486 * \param os output stream in which the data should be printed.
487 *
488 * Iterate over the headers and trailers present in this packet,
489 * from the first header to the last trailer and invoke, for
490 * each of them, the user-provided method Header::DoPrint or
491 * Trailer::DoPrint methods.
492 */
493 void Print(std::ostream& os) const;
494
495 /**
496 * \brief Return a string representation of the packet
497 *
498 * An empty string is returned if you haven't called EnablePrinting ()
499 *
500 * \return String representation
501 */
502 std::string ToString() const;
503
504 /**
505 * \brief Returns an iterator which points to the first 'item'
506 * stored in this buffer.
507 *
508 * Note that this iterator will point
509 * to an empty array of items if you don't call EnablePrinting
510 * or EnableChecking before.
511 *
512 * \returns an iterator
513 *
514 * \sa EnablePrinting EnableChecking
515 */
517
518 /**
519 * \brief Enable printing packets metadata.
520 *
521 * By default, packets do not keep around enough metadata to
522 * perform the operations requested by the Print methods. If you
523 * want to be able the Packet::Print method,
524 * you need to invoke this method at least once during the
525 * simulation setup and before any packet is created.
526 */
527 static void EnablePrinting();
528 /**
529 * \brief Enable packets metadata checking.
530 *
531 * The packet metadata is also used to perform extensive
532 * sanity checks at runtime when performing operations on a
533 * Packet. For example, this metadata is used to verify that
534 * when you remove a header from a packet, this same header
535 * was actually present at the front of the packet. These
536 * errors will be detected and will abort the program.
537 */
538 static void EnableChecking();
539
540 /**
541 * \brief Returns number of bytes required for packet
542 * serialization.
543 *
544 * \returns number of bytes required for packet
545 * serialization
546 *
547 * For packet serialization, the total size is checked
548 * in order to determine the size of the buffer
549 * required for serialization
550 */
552
553 /**
554 * \brief Serialize a packet, tags, and metadata into a byte buffer.
555 *
556 * \param buffer a raw byte buffer to which the packet will be serialized
557 * \param maxSize the max size of the buffer for bounds checking
558 *
559 * \returns one if all data were serialized, zero if buffer size was too small.
560 */
561 uint32_t Serialize(uint8_t* buffer, uint32_t maxSize) const;
562
563 /**
564 * \brief Tag each byte included in this packet with a new byte tag.
565 *
566 * \param tag the new tag to add to this packet
567 *
568 * Note that adding a tag is a const operation which is pretty
569 * un-intuitive. The rationale is that the content and behavior of
570 * a packet is _not_ changed when a tag is added to a packet: any
571 * code which was not aware of the new tag is going to work just
572 * the same if the new tag is added. The real reason why adding a
573 * tag was made a const operation is to allow a trace sink which gets
574 * a packet to tag the packet, even if the packet is const (and most
575 * trace sources should use const packets because it would be
576 * totally evil to allow a trace sink to modify the content of a
577 * packet).
578 */
579 void AddByteTag(const Tag& tag) const;
580
581 /**
582 * \brief Tag the indicated byte range of this packet with a new byte tag.
583 *
584 * As parameters for this method, we do not use indexes, but byte position.
585 * Moreover, as there is no 0-th position, the first position is 1.
586 *
587 * As example, if you want to tag the first 10 bytes, you have to call
588 * the method in this way:
589 *
590 * \code{.cpp}
591 Ptr<Packet> p = ... ;
592 SomeTag tag;
593 p->AddByteTag (tag, 1, 10);
594 \endcode
595 *
596 * \param tag the new tag to add to this packet
597 * \param start the position of the first byte tagged by this tag
598 * \param end the position of the last byte tagged by this tag
599 */
600 void AddByteTag(const Tag& tag, uint32_t start, uint32_t end) const;
601 /**
602 * \brief Returns an iterator over the set of byte tags included in this packet
603 *
604 * \returns an iterator over the set of byte tags included in this packet.
605 */
607 /**
608 * \brief Finds the first tag matching the parameter Tag type
609 *
610 * \param tag the byte tag type to search in this packet
611 * \returns true if the requested tag type was found, false otherwise.
612 *
613 * If the requested tag type is found, it is copied in the user's
614 * provided tag instance.
615 */
616 bool FindFirstMatchingByteTag(Tag& tag) const;
617
618 /**
619 * \brief Remove all byte tags stored in this packet.
620 */
621 void RemoveAllByteTags();
622
623 /**
624 * \param os output stream in which the data should be printed.
625 *
626 * \brief Iterate over the byte tags present in this packet, and
627 * invoke the Print method of each tag stored in the packet.
628 */
629 void PrintByteTags(std::ostream& os) const;
630
631 /**
632 * \brief Add a packet tag.
633 *
634 * \param tag the packet tag type to add.
635 *
636 * Note that this method is const, that is, it does not
637 * modify the state of this packet, which is fairly
638 * un-intuitive. See AddByteTag"()" discussion.
639 */
640 void AddPacketTag(const Tag& tag) const;
641 /**
642 * \brief Remove a packet tag.
643 *
644 * \param tag the packet tag type to remove from this packet.
645 * The tag parameter is set to the value of the tag found.
646 * \returns true if the requested tag is found, false
647 * otherwise.
648 */
649 bool RemovePacketTag(Tag& tag);
650 /**
651 * \brief Replace the value of a packet tag.
652 *
653 * \param tag the packet tag type to replace. To get the old
654 * value of the tag, use PeekPacketTag first.
655 * \returns true if the requested tag is found, false otherwise.
656 * If the tag isn't found, Add is performed instead (so
657 * the packet is guaranteed to have the new tag value
658 * either way).
659 */
660 bool ReplacePacketTag(Tag& tag);
661 /**
662 * \brief Search a matching tag and call Tag::Deserialize if it is found.
663 *
664 * \param tag the tag to search in this packet
665 * \returns true if the requested tag is found, false
666 * otherwise.
667 */
668 bool PeekPacketTag(Tag& tag) const;
669 /**
670 * \brief Remove all packet tags.
671 */
672 void RemoveAllPacketTags();
673
674 /**
675 * \brief Print the list of packet tags.
676 *
677 * \param os the stream on which to print the tags.
678 *
679 * \sa Packet::AddPacketTag, Packet::RemovePacketTag, Packet::PeekPacketTag,
680 * Packet::RemoveAllPacketTags
681 */
682 void PrintPacketTags(std::ostream& os) const;
683
684 /**
685 * \brief Returns an object which can be used to iterate over the list of
686 * packet tags.
687 *
688 * \returns an object which can be used to iterate over the list of
689 * packet tags.
690 */
692
693 /**
694 * \brief Set the packet nix-vector.
695 *
696 * \note Note: This function supports a temporary solution
697 * to a specific problem in this generic class, i.e.
698 * how to associate something specific like nix-vector
699 * with a packet. This design methodology
700 * should _not_ be followed, and is only here as an
701 * impetus to fix this general issue.
702 *
703 * \warning For real this function is not const, as it is the
704 * setter for a mutable variable member. The const qualifier
705 * is needed to set a private mutable variable of const objects.
706 *
707 * \param nixVector the nix vector
708 */
709 void SetNixVector(Ptr<NixVector> nixVector) const;
710 /**
711 * \brief Get the packet nix-vector.
712 *
713 * See the comment on SetNixVector
714 *
715 * \returns the Nix vector
716 */
718
719 /**
720 * TracedCallback signature for Ptr<Packet>
721 *
722 * \param [in] packet The packet.
723 */
724 typedef void (*TracedCallback)(Ptr<const Packet> packet);
725
726 /**
727 * TracedCallback signature for packet and Address.
728 *
729 * \param [in] packet The packet.
730 * \param [in] address The address.
731 */
732 typedef void (*AddressTracedCallback)(Ptr<const Packet> packet, const Address& address);
733
734 /**
735 * TracedCallback signature for packet and source/destination addresses.
736 *
737 * \param [in] packet The packet.
738 * \param [in] srcAddress The source address.
739 * \param [in] destAddress The destination address.
740 */
741 typedef void (*TwoAddressTracedCallback)(const Ptr<const Packet> packet,
742 const Address& srcAddress,
743 const Address& destAddress);
744
745 /**
746 * TracedCallback signature for packet and Mac48Address.
747 *
748 * \param [in] packet The packet.
749 * \param [in] mac The Mac48Address.
750 */
752
753 /**
754 * TracedCallback signature for changes in packet size.
755 *
756 * \param [in] oldSize The previous packet's size.
757 * \param [in] newSize The actual packet's size.
758 */
759 typedef void (*SizeTracedCallback)(uint32_t oldSize, uint32_t newSize);
760
761 /**
762 * TracedCallback signature for packet and SINR.
763 *
764 * \param [in] packet The packet.
765 * \param [in] sinr The received SINR.
766 */
767 typedef void (*SinrTracedCallback)(Ptr<const Packet> packet, double sinr);
768
769 private:
770 /**
771 * \brief Constructor
772 * \param buffer the packet buffer
773 * \param byteTagList the ByteTag list
774 * \param packetTagList the packet's Tag list
775 * \param metadata the packet's metadata
776 */
777 Packet(const Buffer& buffer,
778 const ByteTagList& byteTagList,
779 const PacketTagList& packetTagList,
780 const PacketMetadata& metadata);
781
782 /**
783 * \brief Deserializes a packet.
784 * \param [in] buffer the input buffer.
785 * \param [in] size the buffer size.
786 * \returns the number of deserialized bytes.
787 */
788 uint32_t Deserialize(const uint8_t* buffer, uint32_t size);
789
790 Buffer m_buffer; //!< the packet buffer (it's actual contents)
791 ByteTagList m_byteTagList; //!< the ByteTag list
792 PacketTagList m_packetTagList; //!< the packet's Tag list
793 PacketMetadata m_metadata; //!< the packet's metadata
794
795 /* Please see comments above about nix-vector */
796 mutable Ptr<NixVector> m_nixVector; //!< the packet's Nix vector
797
798 static uint32_t m_globalUid; //!< Global counter of packets Uid
799};
800
801/**
802 * \brief Stream insertion operator.
803 *
804 * \param os the stream
805 * \param packet the packet
806 * \returns a reference to the stream
807 */
808std::ostream& operator<<(std::ostream& os, const Packet& packet);
809
810/**
811 * \ingroup network
812 * \defgroup packetperf Packet Performance
813 * The current implementation of the byte buffers and tag list is based
814 * on COW (Copy On Write. An introduction to COW can be found in Scott
815 * Meyer's "More Effective C++", items 17 and 29). What this means is that
816 * copying packets without modifying them is very cheap (in terms of cpu
817 * and memory usage) and modifying them can be also very cheap. What is
818 * key for proper COW implementations is being
819 * able to detect when a given modification of the state of a packet triggers
820 * a full copy of the data prior to the modification: COW systems need
821 * to detect when an operation is "dirty".
822 *
823 * Dirty operations:
824 * - ns3::Packet::AddHeader
825 * - ns3::Packet::AddTrailer
826 * - both versions of ns3::Packet::AddAtEnd
827 * - ns3::Packet::RemovePacketTag
828 * - ns3::Packet::ReplacePacketTag
829 *
830 * Non-dirty operations:
831 * - ns3::Packet::AddPacketTag
832 * - ns3::Packet::PeekPacketTag
833 * - ns3::Packet::RemoveAllPacketTags
834 * - ns3::Packet::AddByteTag
835 * - ns3::Packet::FindFirstMatchingByteTag
836 * - ns3::Packet::RemoveAllByteTags
837 * - ns3::Packet::RemoveHeader
838 * - ns3::Packet::RemoveTrailer
839 * - ns3::Packet::CreateFragment
840 * - ns3::Packet::RemoveAtStart
841 * - ns3::Packet::RemoveAtEnd
842 * - ns3::Packet::CopyData
843 *
844 * Dirty operations will always be slower than non-dirty operations,
845 * sometimes by several orders of magnitude. However, even the
846 * dirty operations have been optimized for common use-cases which
847 * means that most of the time, these operations will not trigger
848 * data copies and will thus be still very fast.
849 */
850
851} // namespace ns3
852
853/****************************************************
854 * Implementation of inline methods for performance
855 ****************************************************/
856
857namespace ns3
858{
859
862{
863 return m_buffer.GetSize();
864}
865
866} // namespace ns3
867
868#endif /* PACKET_H */
a polymophic address class
Definition: address.h:101
automatically resized byte buffer
Definition: buffer.h:94
uint32_t GetSize() const
Definition: buffer.h:1068
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition: packet.h:63
TypeId m_tid
the ns3::TypeId associated to this tag.
Definition: packet.h:103
uint32_t m_start
the index of the first byte tagged by this tag.
Definition: packet.h:104
uint32_t GetEnd() const
The index is an offset from the start of the packet.
Definition: packet.cc:48
TagBuffer m_buffer
the buffer associated with this tag.
Definition: packet.h:106
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:54
uint32_t GetStart() const
The index is an offset from the start of the packet.
Definition: packet.cc:42
uint32_t m_end
the index of the last byte tagged by this tag.
Definition: packet.h:105
TypeId GetTypeId() const
Definition: packet.cc:36
Iterator over the set of byte tags in a packet.
Definition: packet.h:56
bool HasNext() const
Definition: packet.cc:72
ByteTagList::Iterator m_current
actual position over the set of byte tags in a packet
Definition: packet.h:126
An iterator for iterating through a byte tag list.
Definition: byte-tag-list.h:75
keep track of the byte tags stored in a packet.
Definition: byte-tag-list.h:66
Protocol header serialization and deserialization.
Definition: header.h:44
an EUI-48 address
Definition: mac48-address.h:46
network packets
Definition: packet.h:239
void(* SizeTracedCallback)(uint32_t oldSize, uint32_t newSize)
TracedCallback signature for changes in packet size.
Definition: packet.h:759
PacketTagIterator GetPacketTagIterator() const
Returns an object which can be used to iterate over the list of packet tags.
Definition: packet.cc:1020
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:967
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
Buffer m_buffer
the packet buffer (it's actual contents)
Definition: packet.h:790
static void EnableChecking()
Enable packets metadata checking.
Definition: packet.cc:603
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:354
void(* Mac48AddressTracedCallback)(Ptr< const Packet > packet, Mac48Address mac)
TracedCallback signature for packet and Mac48Address.
Definition: packet.h:751
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:336
PacketMetadata::ItemIterator BeginItem() const
Returns an iterator which points to the first 'item' stored in this buffer.
Definition: packet.cc:590
void SetNixVector(Ptr< NixVector > nixVector) const
Set the packet nix-vector.
Definition: packet.cc:256
ByteTagList m_byteTagList
the ByteTag list
Definition: packet.h:791
void(* AddressTracedCallback)(Ptr< const Packet > packet, const Address &address)
TracedCallback signature for packet and Address.
Definition: packet.h:732
void(* TwoAddressTracedCallback)(const Ptr< const Packet > packet, const Address &srcAddress, const Address &destAddress)
TracedCallback signature for packet and source/destination addresses.
Definition: packet.h:741
Ptr< NixVector > GetNixVector() const
Get the packet nix-vector.
Definition: packet.cc:262
void PrintByteTags(std::ostream &os) const
Iterate over the byte tags present in this packet, and invoke the Print method of each tag stored in ...
Definition: packet.cc:418
void(* SinrTracedCallback)(Ptr< const Packet > packet, double sinr)
TracedCallback signature for packet and SINR.
Definition: packet.h:767
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
static uint32_t m_globalUid
Global counter of packets Uid.
Definition: packet.h:798
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:376
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Deserializes a packet.
Definition: packet.cc:805
uint32_t GetSerializedSize() const
Returns number of bytes required for packet serialization.
Definition: packet.cc:610
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:384
PacketTagList m_packetTagList
the packet's Tag list
Definition: packet.h:792
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:997
Packet & operator=(const Packet &o)
Basic assignment.
Definition: packet.cc:165
bool FindFirstMatchingByteTag(Tag &tag) const
Finds the first tag matching the parameter Tag type.
Definition: packet.cc:943
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:663
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
Packet()
Create an empty packet with a new uid (as returned by getUid).
Definition: packet.cc:139
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:960
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:305
void RemoveAllPacketTags()
Remove all packet tags.
Definition: packet.cc:990
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
Definition: packet.cc:238
uint32_t PeekTrailer(Trailer &trailer)
Deserialize but does not remove a trailer from the internal buffer.
Definition: packet.cc:346
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:456
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition: packet.cc:915
std::string ToString() const
Return a string representation of the packet.
Definition: packet.cc:448
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:324
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
Definition: packet.cc:367
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:983
ByteTagIterator GetByteTagIterator() const
Returns an iterator over the set of byte tags included in this packet.
Definition: packet.cc:937
PacketMetadata m_metadata
the packet's metadata
Definition: packet.h:793
Ptr< NixVector > m_nixVector
the packet's Nix vector
Definition: packet.h:796
bool ReplacePacketTag(Tag &tag)
Replace the value of a packet tag.
Definition: packet.cc:975
Iterator class for metadata items.
Handle packet metadata about packet headers and trailers.
Identifies a packet tag within a packet.
Definition: packet.h:142
TypeId GetTypeId() const
Definition: packet.cc:118
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition: packet.cc:124
const PacketTagList::TagData * m_data
the tag data
Definition: packet.h:166
Iterator over the set of packet tags in a packet.
Definition: packet.h:136
const PacketTagList::TagData * m_current
actual position over the set of tags in a packet
Definition: packet.h:186
bool HasNext() const
Definition: packet.cc:98
List of the packet tags stored in a packet.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
A template-based reference counting class.
read and write tag data
Definition: tag-buffer.h:52
tag a set of bytes in a packet
Definition: tag.h:39
Forward calls to a chain of Callback.
Protocol trailer serialization and deserialization.
Definition: trailer.h:41
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
Defines a linked list of Packet tags, including copy-on-write semantics.
uint8_t data[writeSize]
Tree node for sharing serialized tags.