Packet, Packet Header, and Header Format - UBC ECEteerawat/publications/NS2/15-Packets.pdf ·...

100
Packet, Packet Header, and Packet, Packet Header, and Packet, Packet Header, and Packet, Packet Header, and Header Format Header Format Header Format Header Format Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1

Transcript of Packet, Packet Header, and Header Format - UBC ECEteerawat/publications/NS2/15-Packets.pdf ·...

Packet, Packet Header, and Packet, Packet Header, and Packet, Packet Header, and Packet, Packet Header, and Packet, Packet Header, and Packet, Packet Header, and Header FormatHeader Format

Packet, Packet Header, and Packet, Packet Header, and Header FormatHeader Format

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1

O liOutline• OverviewOverview• Packet Allocation and Deallocation• Packet Header• Packet Header• Data Payload

C t i i P k t• Customizing Packets• Summary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 2

O i O liOverview: Outline• Packet ArchitecturePacket Architecture• Packets As Events, and Delayed

Packet ReceptionPacket Reception• A Link List of Packets• A Free Packet List• A Free Packet List

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 3

O iTh i bj ti S d k t

Overview• The main objective: Send packets

from one node to another.I NS2 • In NS2, – NO packet is actually sent.

T ll N d 2 th t k t i– Tell Node n2 that a packet arrives.• How does it actually work?

n1 n2Packet

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

n1 n2

4

O iA k t i t f

OverviewPacket

• A packet consists of– Packet Header– Data Payload

Header Data Payload

Data Payload• In most case, you do not need to send the

entire packet. p• You may need to tell NS2

– Packet length: Tell n2 when the packet (i.e., hdr cmn::size ) is receivedhdr_cmn::size_) is received.

– Bit error rate (BER): Simulate error and send packet only if it is not in error.

• No need to send entire packet

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

No need to send entire packet

5

P k t A hit t• 5 main parts:

Packet Architecturep

1. Actual packet2. Class Packet3. Protocol specific header3. Protocol specific header4. Packet header manager5. Data payload

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 6

A l P kActual Packet• The actual memory where the packet is The actual memory where the packet is

stored.• Two main parts

– bits_: Store headers– data_: Store payload Again, not being used in most cases

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 7

Cl P k V i bl• Main packet class

Class Packet: Variablesp

//~/ns/common/packet.hclass Packet : public Event { private:

unsigned char* bits_; AppData* data_; bool fflag_;

protected: static Packet* free_; int ref_count_;

public: Packet* next ;Packet next_; static int hdrlen_;

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

}

8

Cl P k V i blClass Packet: VariablesVariable MeaningVariable Meaningbits_ A bag of bits which contain packet header

data_ Pointer to an AppData object which contains data payload

fflag_ True if the packet is in use.

free_ A pointer to the head of the packet free list

ref count Number of objects which are using the packetref_count_ Number of objects which are using the packet

next_ A pointer the next packet in the linked list of packets

hdrlen_ Length of packet header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 9

Cl P k F iClass Packet: Functions//~/ns/common/packet.hclass Packet : public Event { private:

static void init(Packet*) {bzero(p->bits_, hdrlen_);}public:p

//Packet Allocation and Deallocation Packet() : bits_(0), data_(0), ref_count_(0), next_(0) { } inline unsigned char* const bits() { return (bits_); } inline Packet* copy() const;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); static inline void free(Packet*);

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

//Packet Access inline unsigned char* access(int off){return &bits_[off]);};

} 10

Cl P k F iClass Packet: FunctionsVariable MeaningVariable Meaninginit(p) Clears the packet header bits_ of the input packet p.

copy() Returns a pointer to a duplicated packet.

h b f bj hi h f h refcopy() Increases the number of objects, which refer to the packet, by one.

alloc() Creates a new packet and returns a pointer to the created packet.

k h “ ” b f d l d d alloc(n) Creates a new packet with “n” bytes of data payload and returns a pointer to the created packet.

allocdata(n) Allocates “n” bytes of data payload to the variable data_.

free(p) Deallocates packet pfree(p) Deallocates packet p.

access(off) Retrieves a reference to a certain point of the variable bits_ (i.e., packet header).

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 11

P k t A hit t• 5 main parts:

Packet Architecturep

1. Actual packet2. Class Packet3. Protocol specific header3. Protocol specific header4. Packet header manager5. Data payload

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 12

P l S ifi H dProtocol Specific Header• Packet header classified based on typesPacket header classified based on types.

– Common packet header– IP packet header

k h d

Q. What fn. do we use to bind these variables?– Class = TclObject

– TCP packet header

• Defined on a portion f k t h d

j- Fn = bind(…)

of packet header or*bits_

Protocol s

determined

...

specific header sduring the com

The header length is stored in C++

Packet::hdrlen_, which is bound to

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

...

size is pilation.

OTcl: PacketHeaderManager::hdrlen_

13

Cl P k V i bl• Main packet class

Class Packet: Variablesp

//~/ns/common/packet.hclass Packet : public Event { private:

unsigned char* bits_; AppData* data_; bool fflag_;

protected: static Packet* free_; int ref_count_;

public: Packet* next ;Packet next_; static int hdrlen_;

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

} Q: What does it mean?A: Every packet has the same header length.

14

P l S ifi H dProtocol Specific Header• Protocol specific header consists of 3 Protocol specific header consists of 3

classes:1.A C++ class: stores packet attributesp2.An OTcl class: acts as an interface to the OTcl

domain3 A i l bi d h b C d OT l 3.A mapping class: binds the above C++ and OTcl

classes

E.G.,

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

hdr_cmn CommonHeaderClass PacketHeader/Common

OTclC++ Mapping15

Packet Header Manager and Data Payload

• Packet Header ManagerPacket Header Manager– Maintain the list of active protocol

Create header based on the list– Create header based on the list• Data Payload

!!– Try not to use it!!– Store actual data (i.e., *data_)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 16

O i O liOverview: Outline• Packet ArchitecturePacket Architecture• Packets As Events, and Delayed

Packet ReceptionPacket Reception• A Link List of Packets• A Free Packet List• A Free Packet List

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 17

P k E• Class Packet is a child class of Class Event

Packets as EventsClass Packet is a child class of Class Event.

Event ID 1 2 3 4 5 6

Type Arrival Arrival Arrival Arrival Arrival Arrival

Time 0.8 1.5 5.2 7.4 9.4 11.5

Event ID 7 8 9

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Event ID 7 8 9Type Departure Departure DepartureTime 2.4 5.5 10.5

18

P k E• Recall:

Packets as EventsRecall:Event = AtEvent; Handler = AtHandler

AtHandlerAtEvent

handle(Event *e){AtEvent* at = (AtEvent*)e; Tcl::instance().eval(at->proc_);delete at;

uid_time_ handler_

puts “this is test”

next_

proc

• For Packets:E t P k t; H dl N Obj t

}pproc_

Event = Packet; Handler = NsObjectNsobject

handle(Event* e){

Packet

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

handle(Event e){recv((Packet*)e);

}

uid_time_ handler_next_

bits_ data_ …

19

D l d P k R i• Suppose we have would like to

Delayed Packet ReceptionSuppose we have would like to – Send a Packet whose pointer is p– To an NsObject whose pointer is targetj p g _

• Two types of packet forwarding1. Immediate: ( target_->recv(p) )_

2. Delayed by d seconds: s.schedule(target_, p, d)• What is s? How do we obtain it?

– s is a Scheduler object– Scheduler& s = Scheduler::instance();

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 20

O i O liOverview: Outline• Packet ArchitecturePacket Architecture• Packets As Events, and Delayed

Packet ReceptionPacket Reception• A Link List of Packets• A Free Packet List• A Free Packet List

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 21

Cl P k V i bl• Main packet class

Class Packet: Variablesp

//~/ns/common/packet.hclass Packet : public Event { private:

unsigned char* bits_; AppData* data_; bool fflag_;

protected: static Packet* free_; int ref_count_;

public: Packet* next ;Packet* next_; static int hdrlen_;

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

}

22

A Li k Li f P k• A class pointer variable next provide a

A Link List of PacketsA class pointer variable next_ provide a support to create link list

• Class PacketQueue form a link list of packets:Class PacketQueue form a link list of packets– head_: a pointer to the first packet– tail_: a pointer to the last packet

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 23

A Li k Li f P k• Two main functions of class PacketQueue

A Link List of PacketsTwo main functions of class PacketQueue– enque(p): Put a packet *p in the PacketQueue– deque(): Take the “head” (i.e., first) packet of q ( , ) p

the PacketQueue

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 24

O i O liOverview: Outline• Packet ArchitecturePacket Architecture• Packets As Events, and Delayed

Packet ReceptionPacket Reception• A Link List of Packets• A Free Packet List• A Free Packet List

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 25

A F P k LiA Free Packet List• What would NS2 do after a packet is no longer in What would NS2 do after a packet is no longer in

use (e.g., reaches the destination) ?– Destroy it!, or

K it d s it!– Keep it and reuse it!• NS2 keeps packets not in use in a “free packet

list”.• Advantage: No need to allocate/deallocate

memory for several timesDi d W f • Disadvantage: Waste of memory

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 26

A F P k Li• When NS2 need a new packet

A Free Packet ListWhen NS2 need a new packet 1. Take a packet from the free packet list, or (if (1) is not

possible) 2 Create a new packet2. Create a new packet

• When a packet is no longer needed– Put it in a free packet list

• When do we return the memory to the system?When the simulati n terminatesWhen the simulation terminates

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 27

Cl P k V i bl• Main packet class

Class Packet: Variablesp

//~/ns/common/packet.hclass Packet : public Event { private: True if the packet is in use:

unsigned char* bits_; AppData* data_; bool fflag_;

protected:

You cannot put the packeton the free packet listIf fflag is true.

static Packet* free_; int ref_count_;

public: Packet* next ;

_

Packet* next_; static int hdrlen_;

A pointer to the first packet on the free packet list

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

}

28

A F P k LiA Free Packet List

Q: Why do I write free_ outside the class? Why do all packets instance points to the same free_?

A: free is a static variable

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

_

• How a free packet list is actually used? Packet allocation/deallocation

29

O liOutline• OverviewOverview• Packet Allocation and Deallocation• Packet Header• Packet Header• Data Payload

C t i i P k t• Customizing Packets• Summary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 30

Packet Allocation/Deallocation• AllocationAllocation

– When a packet is needed Take a packet from the free packet list– Take a packet from the free packet list

– If not possible, create a new oneD ll ti• Deallocation– When a packet is no longer needed – Put it in the free packet list

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 31

Packet Allocation• Main C++ function alloc()Main C++ function alloc()

– Two steps: 1) Allocation, and 2) Initialization//~/ns/common/packet.h There is at least one packet inline Packet* Packet::alloc() {

Packet* p = free_; if (p != 0) {

t( >ffl FALSE)

n pin the free packet list

assert(p->fflag_ == FALSE); free_ = p->next_; assert(p->data_ == 0); p->uid_ = 0; p->time_ = 0;

} else { p = new Packet; p->bits_ = new unsigned char[hdrlen_];

}init(p);

0 ffl

Clear the space in bits_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

p->next = 0; p->fflag = TRUE;}

which means this packet is in use.

32

Packet Allocation: Other functions• alloc(int n): creates a packet with alloc(int n): creates a packet with

payload size n• allocdata(int n): creates a data payload allocdata(int n): creates a data payload

with size n//~/ns/common/packet.hinline Packet* Packet::alloc(int n){

Packet* p = alloc(); p->allocdata(n); p ( )return (p);

}inline void Packet::allocdata(int n){

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

{ data_ = new PacketData(n);

}

33

Packet Allocation: Other functions• copy(): Creates a duplicatedcopy(): Creates a duplicated• refcopy(): Increment ref_count_; not actually

create a duplicated packet.//~/ns/common/packet.hinline Packet* Packet::copy(){ {

Packet* p = alloc(); memcpy(p->bits(), bits_, hdrlen_);if (data_)

p->data_ = data_->copy(); return (p);

}

i li P k t* f () { ++ f t t thi }

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

inline Packet* refcopy() { ++ref_count_; return this; }

34

Packet Allocation: ref_count_• The variable ref countThe variable ref_count_

• The number of objects which refers to this packetthis packet

• Several objects may refer to the same objectobject

• Refer = Have its pointer point to this packetpacket

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 35

Cl P k V i bl• Main packet class

Class Packet: Variablesp

//~/ns/common/packet.hclass Packet : public Event { private: True if the packet is in use:

unsigned char* bits_; AppData* data_; bool fflag_;

protected:

You cannot put the packeton the free packet listIf fflag is true.

static Packet* free_; int ref_count_;

public: Packet* next ;

_

Packet* next_; static int hdrlen_;

No. of objects which have its variablepoint to this packet

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

}

36

Packet Deallocation• Main function free() • Only free packet which

is currently in useMain function free()//~/ns/common/packet.hinline void Packet::free(Packet* p){

is currently in use• We do not have to do anything with packets which is not in use

if (p->fflag_) { if (p->ref_count_ == 0) {

assert(p->uid_ <= 0); if (p->data != 0) { • ref count > 0 (p _ ) {

delete p->data_; p->data_ = 0; } init(p); p->fflag_ = FALSE; p->next = free ; free = p;

ref_count_ 0 •means somebody is still usingthe packet

D s

Cleanup the packet

p >next_ free_; free_ p;

} else { --p->ref_count_;

}

• Decrease ref_count_

Put the packet at the head of

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

} }

}

Put the packet at the head of the free packet list

37

Packet Deallocation• Main function free()

Packet

next_bits_ data_ next_p

Packet

Main function free()

Packet H d

Data P l d

NULL

NULL

//~/ns/common/packet.hinline void Packet::free(Packet* p){

(4)(3)

(2)(1)

Header Payload

free_Packet

next_

{ if (p->fflag_) {

if (p->ref_count_ == 0) { assert(p->uid_ <= 0); if (p->data_ != 0) {

delete p->data ; p->data = 0; (2)

(2)

(1)Packet

Before free()Aft f ()

p _; p _ ;} init(p); p->fflag_ = FALSE; p->next_ = free_; free_ = p;

} else { --p->ref_count_;

(4)(3)(2)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

After free() } }

}

38

O liOutline• OverviewOverview• Packet Allocation and Deallocation• Packet Header• Packet Header• Data Payload

C t i i P k t• Customizing Packets• Summary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 39

P k H d O liPacket Header: Outline• Protocol Specific Header (PSH) Protocol Specific Header (PSH)

Architecture• Packet TypePacket Type• Protocol Specific Header• Packet Header Access MechanismPacket Header Access Mechanism• Packet Header Manager• Construction of Packet Header• Construction of Packet Header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 40

PSH A hi

PSH = protocol specific header

PSH Architecture• Examples of PSH: Common, IP, TCP, etc.Examples of PSH Common, IP, TCP, etc.• Stored in variable bits_ of a Packet object• Two-level packet header architecture1. Composition of PSH2. C++ struct data type containing PSH

Q Th i f h i Q. The size of the entire packet header is stored in Packet::hdrlen_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 41

PSH A hi

PSH = protocol specific header

PSH Architecture• Variable bits is a “bag of bits”Variable bits_ is a bag of bits• 1st level:

– Allocate spaces for PSHsp– Use an “offset” concept

*bits_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 42

PSH A hi Off

PSH = protocol specific header

PSH Architecture: Offset• 2nd Level: An attribute of a PSH2 Level An attribute of a PSH• Use C++ struct data type• Main field = offset_ = No. of bytes between

Th b i i f d – The beginning of Packet::bits_, and – The beginning of the space allocated to a PSH

*bits_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 43

A E l f PSH

PSH = protocol specific header

An Example of PSHs• A common PSHA common PSH• Contain common attributes

Attribute variablePacket size size_

Packet type ptype_

Unique ID uidUnique ID _

Offseting value offset_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 44

C H d C C d

PSH = protocol specific header

Common Header: C++ Code//~/ns/common/packet.hstruct hdr_cmn {

packet_t ptype_; // packet type int size_; // simulated packet size int uid ; // unique idint uid_; // unique id dir_t direction_; // direction: 0=none, 1=up, -1static int offset_; // offset for this header

i li t ti hd * ( t P k t* ) {inline static hdr_cmn* access(const Packet* p) {return (hdr_cmn*) p->access(offset_);

} inline static int& offset() { return offset_; } inline packet_t& ptype() { return (ptype_); } inline int& size() { return (size_); } inline int& uid() { return (uid_); }

};

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

};

45

PSH = protocol specific header

Common Header: Offseting• The variable offset is staticThe variable offset_ is static

– Only one offset_ instance– Every packet has the same value of hdr_cmn::offset_

• Function access(Packet* p)– Static can be invoked from anywhere

R t i t t h d f th k t – Return a pointer to common header of the packet *p

• E.g., To set the size of Packet *p to be my_size

hdr_cmn* chdr = hdr_cmn::access(p);chdr->size_ = my_size;

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 46

C H d C C d

PSH = protocol specific header

Common Header: C++ Code//~/ns/common/packet.hstruct hdr_cmn {

packet_t ptype_; // packet type int size_; // simulated packet size int uid ; // unique idint uid_; // unique id dir_t direction_; // direction: 0=none, 1=up, -1static int offset_; // offset for this header

i li t ti hd * ( t P k t* ) {inline static hdr_cmn* access(const Packet* p) {return (hdr_cmn*) p->access(offset_);

} inline static int& offset() { return offset_; } inline packet_t& ptype() { return (ptype_); } inline int& size() { return (size_); } inline int& uid() { return (uid_); }

};

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

};

The type of ptype_ is packet_t. What is packet_t ?47

P k H d O liPacket Header: Outline• Protocol Specific Header ArchitectureProtocol Specific Header Architecture• Packet Type• Protocol Specific Header• Protocol Specific Header• Packet Header Access Mechanism• Packet Header Manager• Packet Header Manager• Construction of Packet Header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 48

P k T

PSH = protocol specific header

Packet Type• Defined in Common Header not in Defined in Common Header, not in

TCP or IP• Define in k t t below:• Define in enum packet_t below:

//~/ns/common/packet.henum packet t {enum packet_t {

PT_TCP, PT_UDP, PT_CBR,

Q: What is its actual value?a. stringb. integer

PT_AUDIO, PT_VIDEO, PT_ACK,

gc. floating pointd. boolean

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

... PT_NTYPE // This MUST be the LAST one

}49

P k T

PSH = protocol specific header

Packet Type• How do we show the type of packet?How do we show the type of packet? class p_info (i.e., packet info.)A t l i bl • An external variable packet_info of class p_info

• An array member variable name_– Index = an element of packet_t_– Value = The corresponding string

• A function name(packet t p)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

A function name(packet_t p)– Return a string corresponding to p

50

P k T

PSH = protocol specific header

Packet Typeclass p_info { public:

p_info() { name_[PT_TCP]= "tcp"; name [PT UDP] " dp"

constructor

name_[PT_UDP]= "udp"; ... name_[PT_NTYPE]= "undefined";

} }const char* name(packet_t p) const {

if ( p <= PT_NTYPE ) return name_[p];return 0;

} private:

static char* name_[PT_NTYPE+1]; };

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

}; extern p_info packet_info; /* map PT_* to string name */

51

P k H d O liPacket Header: Outline• Protocol Specific Header ArchitectureProtocol Specific Header Architecture• Packet Type• Protocol Specific Header• Protocol Specific Header• Packet Header Access Mechanism• Packet Header Manager• Packet Header Manager• Construction of Packet Header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 52

P l S ifi H d

PSH = protocol specific header

Protocol Specific Header• Store attributes specific to a certain protocols. Store attributes specific to a certain protocols. • 48 built-in protocol specific headers• Each protocol involves 3 following parts: 1. C++ class:

- struct data type- The place where packet info is storedThe place where packet info is stored- Format: hdr_<XXX> (e.g., hdr_cmn)

2. OTcl class: - OTcl interfaceOTcl interface- Format: PacketHeader/<XXX> (e.g., PacketHeader/Common)

3. Mapping class: M C l t OT l l

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

- Map a C++ class to an OTcl class- Format: <XXX>HeaderClass (e.g., CommonHeaderClass)

53

P l S ifi H d

PSH = protocol specific header

Protocol Specific Header• Examples:Examples:

C++ Class OTcl classMapping class

TcpAgent TcpClass Agent/TCP

DropTail DropTailClass Queue/DropTail

hdr_cmn CommonHeaderClass PacketHeader/Common

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 54

C Cl S

PSH = protocol specific header

C++ Class: Struct//~/ns/common/packet.hstruct hdr_cmn {

packet_t ptype_; // packet type int size_; // simulated packet size int uid ; // unique idint uid_; // unique id dir_t direction_; // direction: 0=none, 1=up, -1static int offset_; // offset for this header

i li t ti hd * ( t P k t* ) {inline static hdr_cmn* access(const Packet* p) {return (hdr_cmn*) p->access(offset_);

} inline static int& offset() { return offset_; } inline packet_t& ptype() { return (ptype_); } inline int& size() { return (size_); } inline int& uid() { return (uid_); }

};

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

};

55

M i Cl

PSH = protocol specific header

Mapping Class• The base class is PacketHeaderClass

//~/ns/common/packet.hclass PacketHeaderClass : public TclClass {class PacketHeaderClass : public TclClass { protected:

PacketHeaderClass(const char* classname, int hdrlen) : TclClass(classname), hdrlen_(hdrlen), offset_(0);{};

inline void bind_offset(int* off) { offset_ = off; }; inline void offset(int* off) {offset_= off;}; int hdrlen_; // # of bytes for this header i * ff // ff f hi h dint* offset_; // offset for this header

public: TclObject* create(int argc,const char*const* argv){return 0;};virtual void bind { }; Q Wh d d

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

virtual void bind { … }; };

Q: What does create(…) do? A: Create a shadow object

56

M i Cl

PSH = protocol specific header

Mapping Class• The constructor of class PacketHeaderClass

PacketHeaderClass(const char* classname, int hdrlen) : TclClass(classname) hdrlen (hdrlen) offset (0);{};TclClass(classname), hdrlen_(hdrlen), offset_(0);{};

• Two input arguments:– classname: Feed it to class TclClass– hdrlen: Set hdrlen_ to this value

• Also set offset to NULLAlso set offset_ to NULL

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 57

M i Cl

PSH = protocol specific header

Mapping Class• An example: class CommonHeaderClassp

//~/ns/common/packet.ccclass CommonHeaderClass : public PacketHeaderClass {class CommonHeaderClass : public PacketHeaderClass { public:

CommonHeaderClass() : PacketHeaderClass( “P k tH d /C ” i f(hd )) {“PacketHeader/Common”,sizeof(hdr_cmn)) {

bind_offset(&hdr_cmn::offset_); }

} class_cmnhdr;

Q: What does this line do?Bind this class to OTcl class “PacketHeader/Common”

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

- Bind this class to OTcl class PacketHeader/Common- put the size of hdr_cmn in CommonHeaderClass::hdrlen_

58

M i Cl

PSH = protocol specific header

• Class CommonHeaderClass:

Mapping Classbind_offset(&hdr_cmn::offset_);

class PacketHeaderClass : public TclClass {class PacketHeaderClass : public TclClass { protected:

inline void bind_offset(int* off) { offset_ = off; };…

};

struct hdr cmn {

Q: Suppose &hdr_cmn::offset_=30. What would be the value of offset_? A: 0xd6f9c0

struct hdr_cmn { …static int offset_; // offset for this header…

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

};

59

P k H d O liPacket Header: Outline• Protocol Specific Header ArchitectureProtocol Specific Header Architecture• Packet Type• Protocol Specific Header• Protocol Specific Header• Packet Header Access Mechanism• Packet Header Manager• Packet Header Manager• Construction of Packet Header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 60

Packet Header Access Mechanism• PSH consists of three componentsPSH consists of three components

C++ Class OTcl classMapping class

/hdr_cmn CommonHeaderClass PacketHeader/Common

A structure to store packetattributes

Bind C++ and OTcl classes

OTcl Interface

• How do we access packet attributes ?– Retrieve, and

M dif

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

– Modify

61

Packet Header Access Mechanism• Two step process:Two step process

1. Retrieve the ref. to the C++ class2. Access attributes using the structure of the

C lC++ class• Let’s have a look at how a packet is create!• When we create a packet we need to • When we create a packet, we need to

initialize the packet, e.g., setting packet size.

Q: Which NS2 component is responsible to creating packets?A: Agents

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Q: How does it create a packet?A: Function Agent::allocpkt()

62

Packet Creation Example

• Function allocpkt() of class Agent

//~/ns/common/agent.ccPacket* Agent::allocpkt(){

Packet* p = Packet::alloc();initpkt(p); return (p);

}

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 63

Packet Creation ExamplePacket* Agent::initpkt(Packet* p){

hdr_cmn* ch = hdr_cmn::access(p); ch->uid() = uidcnt ++;

1. Ref. Retrieval

_ch->ptype() = type_; ch->size() = size_; ...

2. Attrib. access

hdr_ip* iph = hdr_ip::access(p); iph->saddr() = here_.addr_; iph->sport() = here .port ;

1. Ref. Retrieval

p spo t() e e_.po t_;iph->daddr() = dst_.addr_; iph->dport() = dst_.port_;...

2. Attrib. access

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

... }

- Step 2 is quite straghtforward.- Let’s have a look at Step 1.

64

Step 1: Retrieve a Ref. to PSHhdr_cmn* ch = hdr_cmn::access(p);

inline static hdr_cmn* access(const Packet* p) {return (hdr_cmn*) p->access(offset_);

}

t_of

f = h

dr_c

mn:

:offs

et

...

Packo

...

ket Header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008..

65

P k H d O liPacket Header: Outline• Protocol Specific Header ArchitectureProtocol Specific Header Architecture• Packet Type• Protocol Specific Header• Protocol Specific Header• Packet Header Access Mechanism• Packet Header Manager• Packet Header Manager• Construction of Packet Header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 66

P k H d MPacket Header Manager• Responsibilities:Responsibilities:

– Keep the list of active protocols– Use at the initialization to create a packet formatp

• Related classes:

C++ Class

Mapping class

PacketHeaderManager

PacketHeaderManagerClass

OTcl class PacketHeaderManager

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 67

P k H d MPacket Header Manager//~/ns/common/packet.ccclass PacketHeaderManager : public TclObject { public:

PacketHeaderManager() {bind("hdrlen_",&Packet::hdrlen_);}

}; Can you see the difference in this statement when compared to class TcpAgent? Binding to class Packet

static class PacketHeaderManagerClass : public TclClass { public:

PacketHeaderManagerClass() :

p p g g

PacketHeaderManagerClass() : TclClass("PacketHeaderManager") {}

TclObject* create(int, const char*const*) { return (new PacketHeaderManager);

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

} } class_packethdr_mgr;

68

P k H d MPacket Header Manager• OTcl class has two main variables• OTcl class has two main variables• hdrlen_ : The length of the entire packet header• tab : An associative arraytab_ An assoc at ve array

– Index: PSH OTcl class name

V l 1 ti – Value : 1 = active; N/A = inactive

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 69

P k H d O liPacket Header: Outline• Protocol Specific Header ArchitectureProtocol Specific Header Architecture• Packet Type• Protocol Specific Header• Protocol Specific Header• Packet Header Access Mechanism• Packet Header Manager• Packet Header Manager• Construction of Packet Header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 70

P k C iPacket Construction• Three step process: Three step process: 1. At the compilation

1.1 Construct mapping variables of PSHpp1.2 Construct mapping variables of the packet header

manager1 3 Invoke TclClass::bind()1.3 Invoke TclClass::bind()1.4 Setting up active protocol list

2. During the network configuration phaseg g p Create the packet format

3. During the simulation phase C t k t

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Create packets

71

1 A h C il i1. At the Compilation• C++ and OTcl code translation EXEC and OTcl code translation EXETask 1.1: Construct mapping variables of PSHs

l C H d Cl bli P k tH d Cl {class CommonHeaderClass : public PacketHeaderClass { public:

CommonHeaderClass() : PacketHeaderClass( “PacketHeader/Common” sizeof(hdr cmn)) {“PacketHeader/Common”,sizeof(hdr_cmn)) {

bind_offset(&hdr_cmn::offset_); }

} l hd} class_cmnhdr;

1 1 Construct mapping variables

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

1.1 Construct mapping variables

72

1 A h C il i1. At the Compilation

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 73

1 A h C il i• Consequence of Task 1 1

1. At the CompilationConsequence of Task 1.1– Use example of common packet header

The mapping variable class cmnhdr is – The mapping variable class_cmnhdr is created.

– PSH length is stored in class cmnhdr– PSH length is stored in class_cmnhdr ::hdrlen _.

– class cmnhdr::offset points to class_cmnhdr::offset_ points to the variable hdr_cmn::offset_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 74

The Consequence of Tasks 1.1

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 75

1 A h C il iTask 1.2: Construct mapping variables of packet

1. At the CompilationTask 1.2 Construct mapping variables of packet

header manager– The important step is

// / / / k t//~/ns/common/packet.cc class PacketHeaderManager : public TclObject { public:

PacketHeaderManager() {bind("hdrlen ",

– Consequence: the static variable hdrlen is bound to

g () { ( _ ,&Packet::hdrlen_);}

};

onsequence the stat c ar a e _ s ound to the static variable hdrlen_ of class Packet

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 76

The Consequence of Tasks 1.2

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 77

1 A h C il iTask 1.3: Execution of TclClass::bind()

1. At the Compilationa . E cut n f ()

• At NS2 invocation• bind()is overridden by class PacketHeaderClass

//~/ns/common/packet.h class PacketHeaderClass : public TclClass {

… virtual void bind(){

TclClass::bind(); Tcl& tcl = Tcl::instance();Tcl& tcl = Tcl::instance(); tcl.evalf("%s set hdrlen_ %d", classname_, hdrlen_); add_method("offset");

};

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

};

};

78

1 A h C il i• Q: Suppose the length of the common

1. At the CompilationQ: Suppose the length of the common packet header is 104 bytes. What does the following lines do?g

tcl.evalf("%s set hdrlen %d", _

classname_, hdrlen_);

PacketHeader/Common set hdrlen 104PacketHeader/Common set hdrlen_ 104

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 79

The Consequence of Tasks 1.3

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 80

1 A h C il iTask 1.4: Setting up active protocol list

1. At the CompilationTask 1.4 Setting up active protocol list • Sourcing file ~ns/tcl/lib/ns-packet.tcl

PacketHeaderManager set hdrlen_ 0 f h t {foreach prot {

Common IP ...

} { add-packet-header $prot

} proc add-packet-header args {

foreach cl $args { PacketHeaderManager set tab_(PacketHeader/$cl) 1

}

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

}

}

81

P k C iPacket Construction• Three step process: Three step process: 1. At the compilation

1.1 Construct mapping variables of PSHpp1.2 Construct mapping variables of the packet header

manager1 3 Invoke TclClass::bind()1.3 Invoke TclClass::bind()1.4 Setting up active protocol list

2. During the network configuration phaseg g p Create the packet format

3. During the simulation phase C t k t

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Create packets

82

2. During the Network

• Prior to “$ns run”

gConfiguration Phase

Prior to $ns run• Main task: Set the offset for all active PSH. • Q: How do NS2 know which one is active?• Q: How do NS2 know which one is active?

A: PacketHeaderManager::tab_During a creation of the Si l t object• During a creation of the Simulator object

• Invoke instproc create_packetformat of class Simulatorclass Simulator .

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 83

2. During the Network

//~ns/tcl/lib/ns-packet.tcl

gConfiguration Phase

pSimulator instproc create_packetformat { } {

PacketHeaderManager instvar tab_set pm [new PacketHeaderManager] p [ g ]foreach cl [PacketHeader info subclass] {

if [info exists tab_($cl)] { set off [$pm allochdr $cl] [$p $ ]$cl offset $off

} } }$self set packetManager_ $pm

}

Q: tab is an instvar of ??

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Q tab_ is an instvar of ??A: class PacketHeaderManager

84

P k H d MPacket Header Manager• OTcl class has two main variables• OTcl class has two main variables• hdrlen_ : The length of the entire packet header• tab : An associative arraytab_ An assoc at ve array

– Index: PSH OTcl class name

V l 1 ti – Value : 1 = active; N/A = inactive

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 85

2. During the Network

//~ns/tcl/lib/ns-packet.tcl

gConfiguration Phase

pSimulator instproc create_packetformat { } {

PacketHeaderManager instvar tab_set pm [new PacketHeaderManager] p [ g ]foreach cl [PacketHeader info subclass] {

if [info exists tab_($cl)] { set off [$pm allochdr $cl] [$p $ ]$cl offset $off

} } }$self set packetManager_ $pm

}

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 86

2. During the Network

//~ns/tcl/lib/ns-packet tcl

gConfiguration Phase

//~ns/tcl/lib/ns-packet.tcl1 PacketHeaderManager instproc allochdr cl { 2 set size [$cl set hdrlen_] 3 $self instvar hdrlen3 $self instvar hdrlen_ 4 set NS_ALIGN 8 5 set incr [expr ($size + ($NS_ALIGN-1)) & ~($NS_ALIGN-1)]6 set base $hdrlen

Exam: What does this line do?

R d !!6 set base $hdrlen_ 7 incr hdrlen_ $incr 8 return $base 9 }

Round up!!It sets the last 3 bits to zero, and increase the upper bits by one if the last 3 bits were non-zero.9 } f

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 87

2. During the Network gConfiguration Phase

hdrlen_

PacketHeader/Common

Packet header before invoking allochdr

Line 2size

Line 3PacketHeaderManager::hdrlen_

Lines 4-5incr

Line 6base

Line 7PacketHeaderManager::hdrlen_

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Return (base)Line 8

88

P k C iPacket Construction• Three step process: Three step process: 1. At the compilation

1.1 Construct mapping variables of PSHpp1.2 Construct mapping variables of the packet header

manager1 3 Invoke TclClass::bind()1.3 Invoke TclClass::bind()1.4 Setting up active protocol list

2. During the network configuration phaseg g p Create the packet format

3. During the simulation phase C t k t

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Create packets

89

3 D i h Si l i Ph3. During the Simulation Phase• Packet creationPacket creation• Use the format defined in the former

two stepstwo steps• Most common one: class Agent

– Packet creation: Packet* p = allocpkt()– Packet initialization: initpkt(p)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 90

O liOutline• OverviewOverview• Packet Allocation and Deallocation• Packet Header• Packet Header• Data Payload

C t i i P k t• Customizing Packets• Summary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 91

D P l dData Payload• Generally not being usedGenerally not being used• Main classes:– AppDataTypeAppDataType– AppData– PacketDataPacketData

• Main files– ~ns/common/ns-process hns/common/ns process.h– ~ns/common/packet.h

• See Section 8 4 of the Text

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

See Section 8.4 of the Text

92

Defining a New Packet Header • Define data payload not Define data payload not

recommended• Store necessary information in the

Give me few examples!!

Store necessary information in the packet header.

• How? Ex: My HeaderHow? Ex My HeaderC++ Class OTcl classMapping class

hdr_my MyHeaderClass PacketHeader/My

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 93

Defining a New Packet Header • 4 main steps:4 main steps:1. Define a PSH C++ class hdr_my

– Define the structure to hold packe attributesp– Declare variable offset_– Define function access(p)

D fi k t t (if ) i – Define new packet type (if necessary) in enum packet_t and p_info

2. Define a PSH OTcl class PacketHeader/My. Def ne a PSH O cl class ac et eade / y

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 94

Defining a New Packet Header • 4 main steps:4 main steps:3. Define a mapping C++ class hdr_my

– Derive class MyHeaderClass from class Derive class MyHeaderClass from class PacketHeaderClass

– In the constructor, F d h f ll h • Feed the following two parameters as the input

1. OTcl class name: PacketHeader/My2. Size of the my header: sizeof(hdr_my)• Execute function bind_offset(&hdr_my::offset_)

– Instantiate an instance class_my at the construction

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

the construction

95

Defining a New Packet Header • 4 main steps:4 main steps4. Activate my header using PacketHeaderManager

PacketHeaderManager set hdrlen_ 0 f h t {foreach prot {

Common My...

} { add-packet-header $prot

}

• Put only the suffix xxx (in PacketHeader/xxx)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 96

O liOutline• OverviewOverview• Packet Allocation and Deallocation• Packet Header• Packet Header• Data Payload

C t i i P k t• Customizing Packets• Summary

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 97

SSummary• In most case, NO packet is actually send.p y Put info. in the packet header and send it.

• A packet (class Packet) consists of A packet (class Packet) consists of – Packet header ( *bits_ ): Info. about packet– Data payload ( *data_ ): Actual payload

• Packet allocation– Take one from a free packet list, or– Create a new oneCreate a new one

• Packet deallocationPut the packet in the free packet list

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

– Put the packet in the free packet list.

98

SSummary• Packet header:Packet header

– Variable = *bits_– Contains several Protocol specific header

(PSH)(PSH)– How would you access a PSH ? ( use offset )

• 3 Main parts of PSH– A C++ class: hdr_cmn

l l – An OTcl class: PacketHeader/Common– A mapping class: CommonHeaderClass

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 99

SSummary• Packet creation processp

1. At the compilation: • Construction of PSH and packet header mapping variables, • invoke TclClass::bind(), and • set up active protocol list• set up active protocol list

2. During the network configuration phase: Create packet format

3. During the simulation: Create packetsp

• Defining a new packet header1. Define a PSH C++ classf2. Define a PSH OTcl class3. Define a PSH mapping class4. Activate the header

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 100