1, in the NS simulation network, the Grouping (Packet) is the basic unit of interaction between objects. A grouping is a series of grouping headers and an optional data space composition. The structure of the packet header is initialized when the simulator object is created, and the offset of each packet header relative to the starting address of the packet is also recorded. By default, most NS built-in packet headers are enabled (including common headers, IP headers, TCP headers, RTP headers, trace headers, and so on). This means that, by default, it will be initialized regardless of whether a packet header will be used. In general, groupings contain only a series of packet headers, and pointers to data spaces are null.
Users can define their own packet headers for the new protocol, or they can extend the existing packet header by increasing the domain, adding a new packet header, in summary, defining the structure containing the desired domain in C + +, and then defining a static class to provide a connection to OTCL. Then modify some of the simulated initialization code to specify the byte offset of the new packet header in the grouping.
2. Protocol and Packet Header
In general, we need to define the Protocol's own packet header for the new protocol, which will ensure that the new protocol is used without affecting the existing packet headers. For example, the RTP header contains a sequence number field and a Source identification field, and the following code defines the packet header for the RTP protocol, see ~/NS/APPS/RTP. (CC,H):
In the Rtp.h
struct HDR_RTP {
u_int32_t srcid_; Source Identification Field
int seqno_; Serial Number field
RTP flags indicating significant event (begining of Talkspurt)
u_int16_t Flags_;
static int offset_; The byte offset of the RTP header in the NS packet, which accesses the RTP header of the P packet, hdr_rtp::access (p). The class Packheadermanger is responsible for binding the position of the header in the offset_ and the group.
inline static int& offset () {return offset_;}
Inline static hdr_rtp* access (const packet* p) {
Return (hdr_rtp*) p->access (offset_);
}
/* Per-field member functions */
u_int32_t& Srcid () {return (SRCID_);}
int& seqno () {return (seqno_);}
u_int16_t& flags () {return (FLAGS_);}
};
The main function of HDR_RTP is to calculate the byte offset of a field, and in the structure, it also provides an interface that the member variable function uses to provide external access to internal variables.
In the rtp.cc
Class Rtpheaderclass:public Packetheaderclass {
Public
Calling the Packetheaderclass constructor will make the RTP header size stored and used by the Packheadermanger object
Rtpheaderclass (): Packetheaderclass ("PACKETHEADER/RTP",
sizeof (HDR_RTP)) {
Bind_offset (&hdr_rtp::offset_);//packheadermanger know the offset of this head is stored there.
}
CLASS_RTPHDR//CLASS_RTPHDR is a static object of the Rtpheaderclass class, providing a connection to OTCL
void Rtpagent::sendpkt ()
{
packet* p = allocpkt (); Constructs a new grouping that needs to be sent, ALLOCPKT () Handles assignment of fields for all network layer packet headers.
Lastpkttime_ = Scheduler::instance (). clock ();
MAKEPKT (P);
TARGET_->RECV (P, (handler*) 0);
}
void Rtpagent::makepkt (packet* p)
{
HDR_RTP *rh = hdr_rtp::access (p); Returns the address of the first byte of the buffer used to store the header information, which is cast to the corresponding header pointer.
/* Fill in Srcid_ and seqno */
Rh->seqno () = seqno_++;
Rh->srcid () = Session_? Session_->srcid (): 0;
}
3. Add new group header
If we want to add a new packet header called NEWHDR, we need:
(1) Create a new structure in C + + to define the required fields, according to the naming convention, this structure should be called HDR_NEWHDR, which defines the offset_ field and the method that accesses the field.
(2) Define the required member functions to access other fields.
(3) Create a static class to complete the OTCL connection (PCKETHEADER/NEWDHR), Bind_offset () in its constructor.
(4) Edit ~NS/TCL/LIB/NS-PACKET.TCL to activate the new packet header.
If you need to find the name of the packet header, you can view the file ~ns/tcl/lib/ns-packet.tcl or execute the following code in NS:
Foreach cl [packetheader Info Subclass] {
Puts $CL
}
1. Classes related to packet headers
(1) Packet class: All groupings in NS are objects of the packet class, which are subclasses of the event class, so groupings can be dispatched as events.
Class Packet:public Event {
Private
unsigned char* bits_; The pointed character array stores all the packet Headers (Bob), which contains the address of Bob's first Byte, which is formed together with all the structure connections defined in each of the active packet headers. Bob maintains a fixed size in the simulation process, the size is recorded in Packet::hdrlen_, and the Hdrlen_ value is set by the Packheadermanger class in the simulation configuration phase.
appdata* Data_; Variable size buffer for ' data ' infrequently used
static void Init (packet*); Initialize PKT HDR
BOOL Fflag_;
Protected
Static packet* Free_; The packet free list, which stores the previously used groupings that have been recycled, gives preference to the space occupied by using these recycled groupings when generating new groupings.
int Ref_count_; Free the PKT until count to 0
Public
Packet* Next_; For queues and the free list
static int hdrlen_;
Packet (): Bits_ (0), Data_ (0), Ref_count_ (0), Next_ (0) {}
inline unsigned char* bits () {return (BITS_);}
Inline packet* copy () const;
Inline packet* refcopy () {++ref_count_; return this;}
Inline int& Ref_count () {return (REF_COUNT_);}
Static inline packet* alloc ();
Static inline packet* alloc (int);
inline void allocdata (int);
Dirty hack for diffusion data
inline void InitData () {data_ = 0;}
static inline void free (packet*);
Inline unsigned char* access (int off) const {
if (Off < 0)
Abort ();
Return (&bits_[off]);
}
This was used for backward compatibility, i.e., assuming user data
is packetdata and return its pointer.
inline unsigned char* accessdata () const {
if (Data_ = = 0)
return 0;
ASSERT (data_->type () = = Packet_data);
Return (((packetdata*) Data_)->data ());
}
u_int8_t incoming;
}
(2) P_info class: A textual representation of the names of all grouping types is saved.
This class is equivalent to "glue", used to bind each grouping type value and their name, when a new grouping type is defined, its numeric code should be added to the enumeration type packet_t, it should be noted that PT_NTPE must be the last element of this enumeration type, At the same time, the name of this grouping type should be added to the constructor of the P_info class.
(3) Packetheaderclass class: Anyone a packet header is based on its base class, which provides an internal state variable
You can locate a specific grouping in a group. Set the Hdrlen_ and offset_ variables, Hdrlen_ is set in the constructor, and Offset_ is set by calling Bind_offset ().
(4) Packheadermanger class: Manages each packet header, which can be called from OTCL to activate part of all inline packet headers during the simulation configuration phase. The only offsets that are assigned to them in Bob.
In PACKET.TCL
Packetheadermanager Set Hdrlen_ 0
Packetheadermanager set Tab_ (Common) 1//common The head is always enabled
Proc Add-packet-header args {
foreach cl $args {
Packetheadermanager set Tab_ (packetheader/$cl) 1
}
}
Set Protolist {//contains all the names of the group headers that need to be activated
# Common:
Common
Flags
IP # IP
.....
}
foreach prot $allhdrs {
Add-packet-header $prot//The group header to be activated is added to the Otcl class array tab_, and all the active packet header values in Tab_ are 1.
}
Simulator instproc Create_packetformat {} {//is called during the configuration phase of the simulation
Packetheadermanager Instvar Tab_
Set PM [New Packetheadermanager]//Create Packetheadermanager Class object
foreach cl [packetheader info Subclass] {
If [info exists tab_ ($CL)] {
set off [$pm ALLOCHDR $CL]
$CL Offset $off
}
}
$self Set Packetmanager_ $pm
}
Packetheadermanager Instproc ALLOCHDR CL {//give the position of the packet header
Set size [$CL set Hdrlen_]
$self Instvar Hdrlen_
Set Ns_align 8
# round up to nearest ns_align bytes
# (needed on Sparc/solaris)
Set incr [Expr ($size + ($NS _align-1)) & ~ ($NS _align-1)]
Set Base $hdrlen _
INCR Hdrlen_ $INCR
Return $base
}
(5) HDR_CMN structure
Each grouping in the simulation contains a common header, and the HDR_CMN structure defines the fields that are used to track commonly used metrics such as packet flow, measure time, and length.
struct HDR_CMN {
Enum dir_t {down=-1, none= 0, up= 1};
packet_t Ptype_; Packet type (see above)
int size_; Simulated packet size
Size_ is often used to calculate the time required to submit a packet along a network link, with the value of the length of the application data and the sum of the IP layer, transport layer, and application layer grouping length.
int uid_; Unique ID
int error_; Error flag
int Errbitcnt_; # of corrupted Bits Jahn
int fecsize_;
Double Ts_; Measure the queuing delay at the switching node
int iface_; Completes the multicast distribution tree calculation, which indicates that the group was received on the link
dir_t Direction_; Direction:0=none, 1=up, -1=down
Source Routing
Char Src_rt_valid;
Double ts_arr_; Required by Marker of JOBS
Http://www.cnblogs.com/xyl-share-happy/archive/2012/05/30/2526791.html
Management of packet headers