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

📄 packet_format.tex

📁 柯老师网站上找到的
💻 TEX
📖 第 1 页 / 共 2 页
字号:
%% personal commentary:%        DRAFT DRAFT DRAFT%        - KFALL%\section{\shdr{Packet Headers and Formats}{packet.h}{sec:pformat}}Objects of the class \code{Packet} are the fundamental unit ofexchange between objects in the simulation.The \code{Packet} class provides enough information tolink a packet on to a list (i.e. in a \code{PacketQueue} or on a freelist of packets), refer to a buffer containing packet headerswhich are defined on a per-protocol basis, and to refer to a bufferof packet data.New protocols may define their own packet headers or may extendexisting headers with additional fields.New packet headers are introduced into the simulatorby defining a C++ structure with the neededfields, defining a static class to provideOTcl linkage, and then modifying some of the simulator initializationcode to assign a byte offset in each packet where the new headeris to be located relative to others.When the simulator is initialized through OTcl,a user may choose to enableonly a subset of the compiled-in packet formats, resulting ina modest savings of memory during the execution of the simulation.Presently, all configured-in packet formats are enabled.The management of which packet formats are currently enabledin a simulation is handled by a special packet header managerobject described below.This object supports an OTcl method used to specifywhich packet headers will be used in a simulation.If an object in the simulator makes use of a field in a headerwhich has not been enabled, a run-time fatal program abort occurs.\subsection{\shdr{A Protocol-Specific Packet Header}{rtp.h}{sec:ppackethdr}}Protocol developerswill often wish to provide a specific header type to be used in packets.Doing so allows a new protocol implementationto avoid overloading already-existing header fields.We consider a simplified version of RTP as an example.The RTP header will require a sequence number fields and a sourceidentifier field.The following classes create the needed header(see \code{rtp.h} and \code{rtp.cc}):\begin{small}\begin{verbatim}From rtp.h:	/* rtp packet.  For now, just have srcid + seqno. */	struct hdr_rtp { 		u_int32_t srcid_;		int seqno_;		/* per-field member functions */		u_int32_t& srcid() { return (srcid_); }		int& seqno() { return (seqno_); }	};   From rtp.cc:	class RTPAgent: public CBR_Agent {	...		int off_rtp_;	};	class RTPHeaderClass : public PacketHeaderClass {	public: 		RTPHeaderClass() : PacketHeaderClass("PacketHeader/RTP",						     sizeof(hdr_rtp)) {}	} class_rtphdr;	void RTPAgent::sendpkt()	{		Packet* p = allocpkt();		hdr_rtp *rh = (hdr_rtp*)p->access(off_rtp_);		lastpkttime_ = Scheduler::instance().clock();		/* Fill in srcid_ and seqno */		rh->seqno() = seqno_++;		rh->srcid() = session_->srcid();		target_->recv(p, 0);	}	RTPAgent::RTPAgent()		: session_(0), lastpkttime_(-1e6)	{		type_ = PT_RTP;		bind("seqno_", &seqno_);		bind("off_rtp_", &off_rtp_);	}\end{verbatim}\end{small}The first structure defines the layout (in terms of words and theirplacement): which fields are needed and how big they are.This structure definition is only used by thecompiler to compute byte offsets of fields;no objects of this structure type are ever directly allocated.The structure also provides member functionswhich in turnprovide a layer of data hiding for objects wishing to reador modify header fields of packets.Note that the variable \code{off_rtp_} is usedto find the byte offset at which the rtp header is locatedin an arbitrary packet.To access any packet header other than the ``common'' header(see below, section\ref{sec:commonhdr}), the accessing codemust obtain the appropriate header offset.This is accomplished by declaring and bindingthe integer variable \code{off_<hdrname>_}where \code{<hdrname>} refers to a shorthand nameof the header of interest which must match thename assigned in \code{tcl/lib/ns-packet.tcl}.This is performed above by the RTPAgent's constructor.Generally, one header object for each type of headerin the simulation is instantiated at simulator run-time.A particular header is enabled via OTcl in the simulation duringsimulator configuration time (see \ref{sec:configpacket}).The static object \code{class_rtphdr} of class \code{RTPHeaderClass}is used to provide linkage to OTcl when the RTP header isenabled at configuration time.When the simulator executes, this static object callsthe \code{PacketHeaderClass} constructor with arguments\code{"PacketHeader/RTP"} and \code{sizeof(hdr_rtp}.This causes the size of the RTP header to be storedand made available to the packet header managerat configuration time (see below, section\ref{sec:packethdrmgr}).The sample member function \code{sendpkt()}of \code{RTPAgent} creates a new packetto send by calling \code{allocpkt()}, which handles assignmentof all the network-layer packet header fields (in this case, IP).Headers other than IP are handled separately.In this case, the agent uses the \code{RTPHeader} defined above.The \code{Packet::access()} member function returns the addressof the first byte in a buffer used to hold header information (see below).Its return value is cast as a pointer to the header of interest,after which member functions of the \code{RTPHeader}object are used to access individual fields.\subsubsection{adding a new packet header type}Assuming we wish to create a new header called \code{newhdr}the following steps are performed:\begin{enumerate}        \item create a new structure defining the raw fields (called \code{hdr_newhdr})        \item define member functions for needed fields	\item create a static class to perform OTcl linkage (defines \code{PacketHeader/Newhdr})        \item edit \code{tcl/lib/ns-packet.tcl} to enable new packet format (see \ref{sec:configpacket})\end{enumerate}\subsection{\shdr{Packet Classes}{packet.h}{sec:packetclasses}}There are three C++ classes relevant to the handling of packetsand packet headers in general: \code{Packet},\code{PacketHeader}, and \code{PacketHeaderManager}.The \code{Packet} class defines the type for all packets in thesimulation; it is a subclass of \code{Event} so that packets maybe scheduled (e.g.~for later arrival at some queue).The \code{PacketHeader} class provides a base class forany packet header configured into the simulation.It essentially provides enough internal state to locate any particular packetheader in the collection of packet headers present in any given packet.The \code{PacketHeaderManager} defines a class used to collectand manage currently-configured headers.It is invoked by a method available to OTcl at simulation configurationtime to enable some subset of the compiled-in packet headers.\begin{figure}[h]\centerline{\psfig{figure=packet.eps,width=4in}}\caption{\label{pic:packet}A Packet Object}\end{figure}\subsubsection{\shdr{the Packet class}{packet.h}{sec:packetclass}}The \code{Packet} class defines the structure of apacket and provides member functions to handle afree list for objects of this type.It is illustrated in Figure~\ref{pic:packet} anddefined as follows in \code{packet.h}:\begin{small}\begin{verbatim}	class Packet : public Event {	private:		friend class PacketQueue;		u_char* bits_;  		u_char* data_;  // variable size buffer for 'data'		u_int datalen_; // length of variable size buffer	protected:		static Packet* free_;	public: 		Packet* next_;  // for queues and the free list		static int hdrlen_;		Packet() : bits_(0), datalen_(0), next_(0) { }		u_char* const bits() { return (bits_); }		Packet* copy() const;		static Packet* alloc();		static Packet* alloc(int);		inline void allocdata(int);		static void free(Packet*);		inline u_char* access(int off) { if (off < 0) abort(); return (&bits_[of	f]); }  		inline u_char* accessdata() {return data_;}	};\end{verbatim}\end{small}This class holds a pointer to a generic array of unsignedcharacters (commonly called the ``bag of bits'' or BOB for short)where packet header fields are stored.It also holds a pointer to packet ``data'' (which is often not used insimulations).The \code{bits_} variable contains the address ofthe first byte of the BOB.Effectively BOB is (currently implemented as) a concatenationof all the structures defined for each packet header (by convention,the structures with names beginning \code{hdr_<something>}) that havebeen configured in.BOB generally remains a fixed size throughout a simulation, andthe size is recorded in the \code{Packet::hdrlen_} membervariable.This size is updated during simulator configuration byOTcl.\footnote{It is not intended to be updated after configurationtime.  Doing so {\em should} be possible, but is currently untested.}The other methods of the \code{Packet} class are for creating newpackets and storing old (unused) ones on a private free list.Such allocation and deallocation is performed by thefollowing code (in \code{packet.h}):\begin{small}

⌨️ 快捷键说明

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