⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packet_format.tex

📁 柯老师网站上找到的
💻 TEX
📖 第 1 页 / 共 2 页
字号:
        inline Packet* Packet::alloc(int n)        \{                Packet* p = alloc();                if (n > 0)                       p->allocdata(n);                return (p);        \}                        /* {\cf allocate an n byte data buffer to an existing packet} */        inline void Packet::allocdata(int n)        \{                       datalen_ = n;                 data_ = new u_char[n];                if (data_ == 0)                        abort();                 \}               inline void Packet::free(Packet* p)        \{                p->next_ = free_;                free_ = p;                if (p->datalen_) \{                        delete p->data_;                        p->datalen_ = 0;                \}        \}                        inline Packet* Packet::copy() const        \{                               Packet* p = alloc();                memcpy(p->bits(), bits_, hdrlen_);                  if (datalen_) \{                         p->datalen_ = datalen_;                        p->data_ = new u_char[datalen_];                        memcpy(p->data_, data_, datalen_);                \}                return (p);        \}\end{program}The \fcn[]{alloc} method is a support function commonlyused to create new packets.It is called by \fcn[]{Agent::allocpkt} method onbehalf of agents and is thus not normally invoked directly by most objects.It first attempts to locate an old packet on the free list andif this fails allocates a new one using the C++ \code{new} operator.Note that \code{Packet} class objects and BOBs areallocated separately.The \fcn[]{free} method frees a packet by returning it to the freelist.Note that \emph{packets are never returned to the system's memory allocator}.Instead, they are stored on a free list when \fcn[]{Packet::free} is called.The \fcn[]{copy} member creates a new, identical copy of a packetwith the exception of the \code{uid_} field, which is unique.This function is used by \code{Replicator} objects to supportmulticast distribution and LANs.\subsection{p\_info Class}\label{sec:pinfoclass}This class is used as a ``glue'' to bind numeric packet type valueswith their symbolic names.  When a new packet type is defined, itsnumeric code should be added to the enumeration \code{packet_t} (see\nsf{packet.h}) \footnote{Note: \code{PT\_NTYPE} should remain the last element of thisenumeration.} and its symbolic name should be added to the constructorof \code{p_info}:\begin{program}enum packet_t \{	PT_TCP,	...	PT_NTYPE // This MUST be the LAST one\};class p_info \{public:	p_info() \{		name_[PT_TCP]= "tcp";		...        \}\}\end{program}\subsection{The hdr\_cmn Class}\label{sec:commonhdr}Each packet in the simulator has a ``common''header which is defined in \nsf{packet.h} as follows:\begin{program}        struct hdr_cmn \{                double    ts_;            \* timestamp: for q-delay measurement */                packet_t  ptype_;         \* packet type (see above) */                int       uid_;           \* unique id */                int       size_;          \* simulated packet size */                int       iface_;         \* receiving interface (label) */                         /* {\cf per-field member functions} */                int& ptype() \{ return (ptype_); \}                int& uid() \{ return (uid_); \}                int& size() \{ return (size_); \}                int& iface() \{ return (iface_); \}                double& timestamp() \{ return (ts_); \}        \};\end{program}This structure primarily defines fields used for tracingthe flow of packets or measuring other quantities.The time stamp field is used to measure queuing delayat switch nodes.The \code{ptype_} field is used to identify thetype of packets, which makes reading traces simpler.The \code{uid_} field is used by the scheduler in schedulingpacket arrivals.The \code{size_} field is of general use and gives thesimulated packet's size in bytes.Note that the actual number of bytes consumed in the simulationmay not relate to the value of this field  (i.e., \code{size_} has \emph{no} relationship  to \code{sizeof(struct hdr_cmn)} or other ns structures).Rather, it is used most often in computing the time required for a packetto be delivered along a network link.As such it should be set to the sum of the  application data size  and IP-, transport-, and application-level headers  for the simulated packet.The \code{iface_} field is used by the simulator when performingmulticast distribution tree computations.It is a label indicating (typically) on which link a packet was received.\subsection{The PacketHeaderManager Class}\label{sec:packethdrmgr}An object of the \clsref{PacketHeaderManager}{../ns-2/packet.h} is usedto manage the set of currently-active packet header types andassign each of them unique offsets in the BOB.It is defined in both the C++ and OTcl code:\begin{program}{\rm From tcl/lib/ns-packet.tcl:}        PacketHeaderManager set hdrlen_ 0        #XXX could potentially get rid of this by searching having a more        # uniform offset concept...        foreach pair \{                        \{ Common off_cmn_ \}                        \{ Mac off_mac_ \}                        \{ LL off_ll_ \}                        \{ Snoop off_snoop_ \}                        \{ IP off_ip_ \}                        \{ TCP off_tcp_ \}                        \{ TCPA off_tcpasym_ \}                        \{ Flags off_flags_ \}                        \{ RTP off_rtp_ \}                        \{ Message off_msg_ \}                        \{ IVS off_ivs_ \}                        \{ rtProtoDV off_DV_ \}                        \{ CtrMcast off_CtrMcast_ \}                        \{ Prune off_prune_ \}                        \{ Tap off_tap_ \}                        \{ aSRM off_asrm_ \}                        \{ SRM off_srm_ \}\} \{                set cl [lindex $pair 0]                set var [lindex $pair 1]                PacketHeaderManager set vartab_($cl) $var        \}            Simulator instproc create_packetformat \{ \} \{                set pm [new PacketHeaderManager]                foreach oclass [PacketHeader info subclass] \{                        set L [split $oclass /]                        set cl [lindex $L 1]                        set var [PacketHeaderManager set vartab_($cl)]                        set off [$pm allochdr $cl]                        TclObject set $var $off                \}                       $self set packetManager_ $pm        \}            PacketHeaderManager instproc allochdr cl \{                set size [PacketHeader/$cl set hdrlen_]                             $self instvar hdrlen_                 set NS_ALIGN 8                # round up to nearest NS_ALIGN bytes                set incr [expr ($size + ($NS_ALIGN-1)) & ~($NS_ALIGN-1)]                set base $hdrlen_                incr hdrlen_ $incr                return $base        \}{\rm From packet.cc:}        /* {\cf manages active packet header types} */        class PacketHeaderManager : public TclObject \{        public:                PacketHeaderManager() \{                        bind("hdrlen_", &Packet::hdrlen_);                \}        \};\end{program}The code in \nsf{tcl/lib/ns-packet.tcl} is executedwhen the simulator initializes.Thus, the {\tt foreach} statement is executed before thesimulation begins, and initializes the OTcl class array\code{vartab_} to contain the mapping between classthe name and the name of the variable used to containthat class's header in a packet (which is initialized later).For example, the value of \code{vartab_(IP)} is set to\code{off_ip_}.The \proc[]{create\_packetformat} instance procedure is part of thebasic Simulator class and is called one time during simulatorconfiguration.It first creates a single \code{PacketHeaderManager} object.The C++ constructor links the OTcl instancevariable \code{hdrlen_} (of class \code{PacketHeaderManager})to the C++ variable \code{Packet::hdrlen_} (a staticmember of the \code{Packet} class).This has the effect of setting \code{Packet::hdrlen_} tozero.Note that binding across class types in this fashion isunusual.\label{sec:configpacket}After creating the packet manager, the \code{foreach}loop enables each of the packet headers of interest.This loop iterates through the list of definedpacket headers of the form$(h_i, o_i)$ where $h_i$ is the name of the  $i$th headerand $o_i$ is the name of the variable containing thelocation of the $h_i$ header in BOB.The placement of headers is performed by the \code{allochdr}instproc of the \code{PacketHeaderManager} OTcl class.The procedure keeps a running variable \code{hdrlen_} withthe current length of BOB as new packet headers are enabled.It also arranges for 8-byte alignment for any newly-enabled packetheader.This is needed to ensure that when double-world length quantitiesare used in packet headers on machines where double-word alignmentis required, access faults are not produced.\footnote{Insome processer architectures, including theSparc and HP-PA, double-word access must be performed on a double-wordboundary (i.e. addresses ending in 0 mod 8).  Attempting to performunaligned accesses result in an abnormal program termination.}.\section{Commands at a glance}\label{sec:pktcommand}Following is a list of packet-header related procedures:\begin{flushleft}\code{$ns_ create_packetformat}\\This is an internal simulator procedure and is called once during the simulatorconfiguration to setup a packetHeaderManager object.\code{$pktHdrManager allochdr}\\This is another internal procedure of Class PacketHeaderManager that keeps trackof a variable called \code{hdrlen_} as new packet-headers are enabled. It alsoallows 8-byte allignment for any newly-enabled pkt header.\end{flushleft}\endinput

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -