Packet, Packet Header, and Header Format Packet ... - UBC ECE

91 downloads 22356 Views 660KB Size Report
Customizing Packets. • Summary ... Variable Meaning bits_ ... Meaning. Variable. Meaning init(p). Clears the packet header bits_ of the input packet p. copy().
Packet, Packet Header, and Header Format

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

1

O li Outline • • • • • •

Overview Packet Allocation and Deallocation Packet Header Data Payload C t i i P Customizing Packets k t Summary

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

2

O Overview: i O Outline li • Packet Architecture • Packets As Events, and Delayed Packet Reception • A Link List of Packets • A Free Packet List

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

3

O Overview i • Th The main i objective: bj ti S Send d packets k t from one node to another. • In I NS2 NS2, – NO packet is actually sent. – Tell T ll Node N d n2 2 that th t a packet k t arrives. i

• How does it actually work? n1

Packet

n2

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

4

O Overview i • A packet k t consists i t of f – Packet Header – Data Payload

Packet

Header

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 received. hdr_cmn::size_) 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.

5

P k t Architecture Packet A hit t • 5 main p parts: 1. 2. 3. 4. 5.

Actual packet Class Packet Protocol specific header Packet header manager Data payload

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

6

A Actual lP Packet k • 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 Class Packet: P k Variables V i bl • Main p packet class //~/ns/common/packet.h class 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 Class Packet: P k Variables V i bl Variable Meaning bits_

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 ref_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 Class Packet: P k Functions F i //~/ns/common/packet.h class Packet : public Event { private: static void init(Packet*) {bzero(p->bits_, hdrlen_);} p public: //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* 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*); //Packet Access inline unsigned char* access(int off){return &bits_[off]);}; }

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

10

Cl Class Packet: P k Functions F i Variable

Meaning

init(p)

Clears the packet header bits_ of the input packet p.

copy()

Returns a pointer to a duplicated packet.

refcopy()

allocdata(n)

h number b of f objects, bj which hi h refer f to the h Increases the packet, by one. Creates a new packet and returns a pointer to the created packet. Creates a new packet k with h “n” “ ” bytes b of f data d payload l d and d returns a pointer to the created packet. Allocates “n” bytes of data payload to the variable data_.

free(p)

p Deallocates packet p.

access(off)

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

alloc() alloc(n)

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

11

P k t Architecture Packet A hit t • 5 main p parts: 1. 2. 3. 4. 5.

Actual packet Class Packet Protocol specific header Packet header manager Data payload

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

12

P Protocol l Specific S ifi H Header d • Packet header classified based on types. types – Common packet header – IP packet header – TCP packet k header h d

Q. What fn. do we use to bind these variables? – Class = TclObject j - Fn = bind(…)

...

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.

...

OTcl: PacketHeaderManager::hdrlen_

Protocol specific s header size s is determined during the compilation.

• Defined on a portion of f packet k t header h d or *bits_

13

Cl Class Packet: P k Variables V i bl • Main p packet class //~/ns/common/packet.h class 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_; … }

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

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

14

P Protocol l Specific S ifi H Header d • Protocol specific header consists of 3 classes: 1.A C++ class: stores p packet attributes 2.An OTcl class: acts as an interface to the OTcl domain 3 A mapping 3.A i class: l bi d the binds h above b C++ C and d OTcl OT l classes

E.G., hdr_cmn

CommonHeaderClass

PacketHeader/Common

C++

Mapping

OTcl

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

15

Packet Header Manager and Data Payload • Packet Header Manager – Maintain the list of active protocol – 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 Overview: i O Outline li • Packet Architecture • Packets As Events, and Delayed Packet Reception • A Link List of Packets • A Free Packet List

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

17

P k Packets as Events E • Class Packet is a child class of Class Event. 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

Type

Departure

Departure

Departure

Time

2.4

5.5

10.5

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

18

P k Packets as Events E • Recall: Event = AtEvent;

Handler = AtHandler

AtEvent

time_ proc proc_

AtHandler

uid_

handler_

next_

puts “this is test” p

• For Packets: E Event t = Packet P k t;

H dl = NsObject Handler N Obj t

Packet

time_ bits_

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

Nsobject

uid_

next_ data_

handler_ …

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

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

19

D l Delayed d Packet P k Reception R i • Suppose we have would like to – Send a Packet whose pointer is p – To an NsObject j whose p pointer is target g _

• Two types of packet forwarding 1. 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 Overview: i O Outline li • Packet Architecture • Packets As Events, and Delayed Packet Reception • A Link List of Packets • A Free Packet List

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

21

Cl Class Packet: P k Variables V i bl • Main p packet class //~/ns/common/packet.h class Packet : public Event { private: unsigned char* bits_; AppData* data_; bool fflag_; protected: static Packet* free_; int ref_count_; public: Packet* next next_; ; static int hdrlen_; … }

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

22

A Link Li k List Li of f Packets P k • A class pointer variable next_ next provide a support to create link list • Class PacketQueue form a link list of packets 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 Link Li k List Li of f Packets P k • Two main functions of class PacketQueue – enque(p): Put a packet *p in the PacketQueue – deque(): q Take the “head” ((i.e.,, first)) p packet of the PacketQueue

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

24

O Overview: i O Outline li • Packet Architecture • Packets As Events, and Delayed Packet Reception • A Link List of Packets • A Free Packet List

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

25

A Free F Packet P k List Li • What would NS2 do after a packet is no longer in use (e.g., reaches the destination) ? – Destroy it!, or – Keep K it and d reuse s it!

• NS2 keeps packets not in use in a “free packet list”. • Advantage: No need to allocate/deallocate memory for several times • Disadvantage: Di d W Waste of f memory

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

26

A Free F Packet P k List Li • When NS2 need a new packet 1.

Take a packet from the free packet list, or (if (1) is not possible) 2. Create a new packet 2

• 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 simulation n terminates

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

27

Cl Class Packet: P k Variables V i bl • Main p packet class //~/ns/common/packet.h class Packet : public Event { True if the packet is in use: private: unsigned char* bits_; You cannot put the packet AppData* data_; on the free packet list bool fflag_; If fflag_ is true. protected: static Packet* free_; int ref_count_; public: Packet* next 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 Free F Packet P k List Li

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 • How a free packet list is actually used?  Packet allocation/deallocation

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

29

O li Outline • • • • • •

Overview Packet Allocation and Deallocation Packet Header Data Payload C t i i P Customizing Packets k t Summary

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

30

Packet Allocation/Deallocation • Allocation – When a packet is needed – Take a packet from the free packet list – If not possible, create a new one

• Deallocation D ll ti – 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() – Two steps: 1) Allocation, and 2) Initialization //~/ns/common/packet.h There is at least one n p packet inline Packet* Packet::alloc() in the free packet list { Packet* p = free_; if (p != 0) { assert(p->fflag_ t( >ffl == FALSE); FALSE) free_ = p->next_; assert(p->data_ == 0); p->uid_ = 0; p->time_ = 0; } else { p = new Packet; p->bits_ = new unsigned char[hdrlen_]; } Clear the space in bits_ init(p); 0 p->fflag ffl = TRUE; p->next = 0; }

which means this packet is in use.

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

32

Packet Allocation: Other functions • alloc(int n): creates a packet with payload size n • allocdata(int n): creates a data payload with size n //~/ns/common/packet.h inline Packet* Packet::alloc(int n) { Packet* p = alloc(); p p->allocdata(n); ( ) return (p); } inline void Packet::allocdata(int n) { data_ = new PacketData(n); } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

33

Packet Allocation: Other functions • copy(): Creates a duplicated • refcopy(): Increment ref_count_; not actually create a duplicated packet. //~/ns/common/packet.h inline Packet* Packet::copy() { Packet* p = alloc(); memcpy(p->bits(), bits_, hdrlen_); if (data_) p->data_ = data_->copy(); return (p); }

i li inline P Packet* k t* refcopy() f () { ++ ++ref_count_; f t return t thi this; }

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

34

Packet Allocation: ref_count_ • The variable ref_count_ ref count • The number of objects which refers to this packet • Several objects may refer to the same object • Refer = Have its pointer point to this packet

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

35

Cl Class Packet: P k Variables V i bl • Main p packet class //~/ns/common/packet.h class Packet : public Event { True if the packet is in use: private: unsigned char* bits_; You cannot put the packet AppData* data_; on the free packet list bool fflag_; If fflag_ is true. protected: static Packet* free_; int ref_count_; public: Packet* next next_; ; static int hdrlen_; No. of objects which have its variable

point 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 use • We do not have to do anything with packets which is not in use

//~/ns/common/packet.h inline void Packet::free(Packet* p) { if (p->fflag_) { if (p->ref_count_ == 0) { assert(p->uid_ data_ != 0) ) { • ref_count_ ref count > 0 if (p delete p->data_; p->data_ = 0; •means somebody } is still using init(p); p->fflag_ = FALSE; the packet p->next p >next_ = free_; free ; free free_ = p;

• Decrease D s ref_count_

} else { --p->ref_count_; } }

Cleanup the packet

Put the packet at the head of the free packet list

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

37

Packet Deallocation • Main function free() Packet p

bits_

Packet

data_

NULL NULL

(4)

(1) (2)

Packet Header H d

next_

next_

(3)

Data Payload P l d

next_ free_

Packet

Before free() Aft free() After f ()

//~/ns/common/packet.h inline void Packet::free(Packet* p) { if (p->fflag_) { if (p->ref_count_ == 0) { assert(p->uid_ data_ != 0) { (1) (2) } delete pp->data_;; pp->data_ = 0;; init(p); p->fflag_ = FALSE; (3) p->next_ = free_; free_ = p; } else { (4) --p->ref_count_; } } }

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

38

O li Outline • • • • • •

Overview Packet Allocation and Deallocation Packet Header Data Payload C t i i P Customizing Packets k t Summary

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

39

P k Header: Packet H d Outline O li • Protocol Specific Header (PSH) Architecture • Packet Type • Protocol Specific Header • Packet Header Access Mechanism • Packet Header Manager • Construction of Packet Header

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

40

PSH = protocol specific header

PSH Architecture A hi • Examples of PSH PSH: Common, IP, TCP, etc. • Stored in variable bits_ of a Packet object • Two-level packet header architecture 1. Composition of PSH 2. C++ struct data type containing PSH Q. Th Q The size i of f the h entire i packet header is stored in Packet::hdrlen_

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

41

PSH = protocol specific header

PSH Architecture A hi • Variable bits_ bits is a “bag bag of bits” bits • 1st level: – Allocate spaces p for PSHs – Use an “offset” concept

*bits_

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

42

PSH = protocol specific header

PSH Architecture: A hi Off Offset • 2nd Level Level: An attribute of a PSH • Use C++ struct data type • Main field = offset_ = No. of bytes between – Th The beginning b i i of f Packet::bits_, and d – The beginning of the space allocated to a PSH *bits_

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

43

PSH = protocol specific header

A Example An E l of f PSHs PSH • A common PSH • Contain common attributes Attribute Packet size Packet type Unique ID Offseting value

variable size_ ptype_ uid_ offset_

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

44

PSH = protocol specific header

C Common Header: H d C++ C Code C d //~/ns/common/packet.h struct hdr_cmn { packet_t ptype_; int size_; int uid uid_; ; dir_t direction_; static int offset_;

// // // // //

packet type simulated packet size unique id direction: 0=none, 1=up, -1 offset for this header

inline i li static t ti hd hdr_cmn* * access(const ( t P Packet* k t* 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_ 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 – Return R t a pointer i t tto common header h d of f th the packet k t *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

PSH = protocol specific header

C Common Header: H d C++ C Code C d //~/ns/common/packet.h struct hdr_cmn { packet_t ptype_; int size_; int uid uid_; ; dir_t direction_; static int offset_;

// // // // //

packet type simulated packet size unique id direction: 0=none, 1=up, -1 offset for this header

inline i li static t ti hd hdr_cmn* * access(const ( t P Packet* k t* 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_); } };

The type of ptype_ is packet_t.  What is packet_t ? Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

47

P k Header: Packet H d Outline O li • • • • • •

Protocol Specific Header Architecture Packet Type Protocol Specific Header Packet Header Access Mechanism Packet Header Manager Construction of Packet Header

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

48

PSH = protocol specific header

P k Type Packet T • Defined in Common Header, Header not in TCP or IP • Define in enum packet_t k t t below: //~/ns/common/packet.h enum packet packet_t t { PT_TCP, Q: What is its actual value? PT_UDP, a. string PT_CBR, b. integer g PT_AUDIO, c. floating point PT_VIDEO, d. boolean PT_ACK, ... PT_NTYPE // This MUST be the LAST one } Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

49

PSH = protocol specific header

P k Type Packet T • How do we show the type of packet?  class p_info (i.e., packet info.) • An A external t l variable i bl 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 name(packet t p) – Return a string corresponding to p Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

50

PSH = protocol specific header

P k Type Packet T class p_info { public: p_info() { constructor name_[PT_TCP]= "tcp"; name [PT UDP] "udp"; name_[PT_UDP]= " dp" ... name_[PT_NTYPE]= "undefined"; } const char* name(packet_t p) const { if ( 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

PSH = protocol specific header

M Mapping i Cl Class • The base class is PacketHeaderClass //~/ns/common/packet.h 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 * offset_; int* ff // offset ff f for this hi h header d public: TclObject* create(int argc,const char*const* argv){return 0;}; virtual void bind { … }; Q Wh Q: What does d create(…) do? d };

A: Create a shadow object

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

56

PSH = protocol specific header

M Mapping i Cl Class • The constructor of class PacketHeaderClass PacketHeaderClass(const char* classname, int hdrlen) : TclClass(classname) hdrlen TclClass(classname), hdrlen_(hdrlen), (hdrlen) offset offset_(0);{}; (0);{};

• Two input arguments:

– classname: Feed it to class TclClass – hdrlen: Set hdrlen_ to this value

• Also set offset_ offset to NULL

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

57

PSH = protocol specific header

M Mapping i Cl Class • An example: p class CommonHeaderClass //~/ns/common/packet.cc class CommonHeaderClass : public PacketHeaderClass { public: CommonHeaderClass() : PacketHeaderClass( “P k tH d /C “PacketHeader/Common”,sizeof(hdr_cmn)) ” i f(hd )) { bind_offset(&hdr_cmn::offset_); } } class_cmnhdr; Q: What does this line do? - Bind this class to OTcl class “PacketHeader/Common” PacketHeader/Common - put the size of hdr_cmn in CommonHeaderClass::hdrlen_ Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

58

PSH = protocol specific header

M Mapping i Cl Class • Class CommonHeaderClass: bind_offset(&hdr_cmn::offset_); class PacketHeaderClass : public TclClass { protected: inline void bind_offset(int* off) { offset_ = off; }; … Q: Suppose &hdr_cmn::offset_=30. }; A: 0xd6f9c0 What would be the value of offset_? struct hdr_cmn 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 Header: Packet H d Outline O li • • • • • •

Protocol Specific Header Architecture Packet Type Protocol Specific Header Packet Header Access Mechanism 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 components C++ Class hdr_cmn

A structure to store packet attributes

Mapping class CommonHeaderClass

Bind C++ and OTcl classes

OTcl class PacketHeader/Common /

OTcl Interface

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

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

61

Packet Header Access Mechanism • Two step process process:

1. Retrieve the ref. to the C++ class 2. Access attributes using the structure of the C class C++ l

• Let’s have a look at how a packet is create! • When we create a packet, packet we need to initialize the packet, e.g., setting packet size. Q: Which NS2 component is responsible to creating packets? A: Agents Q: How does it create a packet? A: Function Agent::allocpkt() Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

62

Packet Creation Example • Function allocpkt() of class Agent //~/ns/common/agent.cc Packet* 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 Example Packet* Agent::initpkt(Packet* p) { 1. Ref. Retrieval hdr_cmn* ch = hdr_cmn::access(p); ch->uid() = uidcnt_++; 2. Attrib. access ch->ptype() = type_; ch->size() = size_; ... hdr_ip* iph = hdr_ip::access(p); 1. Ref. Retrieval iph->saddr() = here_.addr_; iph->sport() p spo t() = here e e_.po .port t_; ; 2. Attrib. access iph->daddr() = dst_.addr_; iph->dport() = dst_.port_; ... - Step 2 is quite straghtforward. } - Let’s have a look at Step 1. Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

64

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

... . ...

Packket Header

off = hdr_cmn::offsett_ o

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

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

65

P k Header: Packet H d Outline O li • • • • • •

Protocol Specific Header Architecture Packet Type Protocol Specific Header Packet Header Access Mechanism Packet Header Manager Construction of Packet Header

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

66

P k Header Packet H d Manager M • Responsibilities: – Keep the list of active protocols – Use at the initialization to create a p packet format

• Related classes: C++ Class Mapping class OTcl class

PacketHeaderManager PacketHeaderManagerClass

PacketHeaderManager

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

67

P k Header Packet H d Manager M //~/ns/common/packet.cc class PacketHeaderManager : public TclObject { public: PacketHeaderManager() {bind("hdrlen_", &Packet::hdrlen_);} }; Can you see the difference in this statement

when compared p to class TcpAgent? p g  Binding g to class Packet

static class PacketHeaderManagerClass : public TclClass { public: PacketHeaderManagerClass() : TclClass("PacketHeaderManager") {} TclObject* create(int, const char*const*) { return (new PacketHeaderManager); } } class_packethdr_mgr; Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

68

P k Header Packet H d Manager M • OTcl class has two main variables

• hdrlen_ : The length of the entire packet header • tab_ tab : An assoc associative at ve array – Index: PSH OTcl class name – Value V l : 1 = active; ti N/A = inactive

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

69

P k Header: Packet H d Outline O li • • • • • •

Protocol Specific Header Architecture Packet Type Protocol Specific Header Packet Header Access Mechanism Packet Header Manager Construction of Packet Header

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

70

P k Construction Packet C i • Three step process: 1. At the compilation 1.1 Construct mapping pp variables of PSH 1.2 Construct mapping variables of the packet header manager 1 3 Invoke TclClass::bind() 1.3 1.4 Setting up active protocol list

2. During g the network configuration g phase p  Create the packet format

3. During the simulation phase  Create C t packets k t Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

71

1 At 1. A the h Compilation C il i • C C++ and OTcl code translation  EXE Task 1.1: Construct mapping variables of PSHs class l C CommonHeaderClass H d Cl : public bli P PacketHeaderClass k tH d Cl { public: CommonHeaderClass() : PacketHeaderClass( “PacketHeader/Common” sizeof(hdr cmn)) { “PacketHeader/Common”,sizeof(hdr_cmn)) bind_offset(&hdr_cmn::offset_); } } class_cmnhdr; l hd

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

72

1 At 1. A the h Compilation C il i

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

73

1 At 1. A the h Compilation C il i • Consequence of Task 1.1 11 – Use example of common packet header – The mapping variable class_cmnhdr class cmnhdr is created. – PSH length is stored in class_cmnhdr class cmnhdr ::hdrlen _. class cmnhdr::offset points to – class_cmnhdr::offset_ 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 At 1. A the h Compilation C il i Task 1.2 1.2: Construct mapping variables of packet header manager – The important step is //~/ns/common/packet.cc // / / / k t class PacketHeaderManager : public TclObject { public: PacketHeaderManager() g () { {bind("hdrlen ( _", , &Packet::hdrlen_);} };

– Consequence: onsequence the stat static c variable ar a e hdrlen_ iss bound 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 At 1. A the h Compilation C il i Task a 1.3: . E Execution cut n of f TclClass::bind() () • 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.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 At 1. A the h Compilation C il i • Q: Suppose the length of the common packet header is 104 bytes. What does the following g lines do? tcl.evalf("%s set hdrlen_ %d", classname_, hdrlen_); PacketHeader/Common set hdrlen 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 At 1. A the h Compilation C il i Task 1.4 1.4: Setting up active protocol list • Sourcing file ~ns/tcl/lib/ns-packet.tcl PacketHeaderManager set hdrlen_ 0 f foreach h prot t { 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 Construction Packet C i • Three step process: 1. At the compilation 1.1 Construct mapping pp variables of PSH 1.2 Construct mapping variables of the packet header manager 1 3 Invoke TclClass::bind() 1.3 1.4 Setting up active protocol list

2. During g the network configuration g phase p  Create the packet format

3. During the simulation phase  Create C t packets k t Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

82

2. During g the Network Configuration Phase • Prior to “$ns $ns run” run • Main task: Set the offset for all active PSH. • Q: How do NS2 know which one is active? A: PacketHeaderManager::tab_ • During a creation of the Simulator Si l t object • Invoke instproc create_packetformat of class Simulator .

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

83

2. During g the Network Configuration Phase //~ns/tcl/lib/ns-packet.tcl p Simulator instproc create_packetformat { } { PacketHeaderManager instvar tab_ set p pm [ [new PacketHeaderManager] g ] foreach cl [PacketHeader info subclass] { if [info exists tab_($cl)] { set off [$p [$pm allochdr $ $cl] ] $cl offset $off } } $self set packetManager_ $pm }

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

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

84

P k Header Packet H d Manager M • OTcl class has two main variables

• hdrlen_ : The length of the entire packet header • tab_ tab : An assoc associative at ve array – Index: PSH OTcl class name – Value V l : 1 = active; ti N/A = inactive

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

85

2. During g the Network Configuration Phase //~ns/tcl/lib/ns-packet.tcl p Simulator instproc create_packetformat { } { PacketHeaderManager instvar tab_ set p pm [ [new PacketHeaderManager] g ] foreach cl [PacketHeader info subclass] { if [info exists tab_($cl)] { set off [$p [$pm allochdr $ $cl] ] $cl offset $off } } $self set packetManager_ $pm }

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

86

2. During g the Network Configuration Phase //~ns/tcl/lib/ns-packet.tcl //~ns/tcl/lib/ns-packet tcl 1 PacketHeaderManager instproc allochdr cl { 2 set size [$cl set hdrlen_] 3 $self instvar hdrlen hdrlen_ Exam: What does this line do? 4 set NS_ALIGN 8 5 set incr [expr ($size + ($NS_ALIGN-1)) & ~($NS_ALIGN-1)] 6 set base $hdrlen $hdrlen_ R Round d up!!!! It sets the last 3 bits to zero, 7 incr hdrlen_ $incr and increase the upper bits by one 8 return $base if f the last 3 bits were non-zero. 9 }

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

87

2. During g the Network Configuration Phase hdrlen_ PacketHeader/Common

Packet header before invoking allochdr size

Line 2 Line 3

PacketHeaderManager::hdrlen_ incr

Lines 4-5 Line 6 Line 7 Line 8

base PacketHeaderManager::hdrlen_ Return (base)

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

88

P k Construction Packet C i • Three step process: 1. At the compilation 1.1 Construct mapping pp variables of PSH 1.2 Construct mapping variables of the packet header manager 1 3 Invoke TclClass::bind() 1.3 1.4 Setting up active protocol list

2. During g the network configuration g phase p  Create the packet format

3. During the simulation phase  Create C t packets k t Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

89

3 During 3. D i the h Simulation Si l i Phase Ph • Packet creation • Use the format defined in the former two 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 li Outline • • • • • •

Overview Packet Allocation and Deallocation Packet Header Data Payload C t i i P Customizing Packets k t Summary

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

91

D Data Payload P l d • Generally not being used • Main classes: – AppDataType – AppData – PacketData

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

• See Section 8.4 8 4 of the Text Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

92

Defining a New Packet Header • Define data payload  not recommended Give me few examples!! • Store necessary information in the packet header. Ex: My Header • How?  Ex C++ Class hdr_my

Mapping class

OTcl class

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:

1. Define a PSH C++ class hdr_my – – – –

Define the structure to hold p packe attributes Declare variable offset_ Define function access(p) D fi new packet Define k t ttype (if necessary)) iin enum packet_t and p_info

2.. Define Def ne a PSH O OTcl cl class PacketHeader/My 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:

3. Define a mapping C++ class hdr_my – Derive class MyHeaderClass from class PacketHeaderClass –



In the constructor,

• Feed F d the h f following ll two parameters as the h input 1. OTcl class name: PacketHeader/My 2. 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.

95

Defining a New Packet Header • 4 main steps steps:

4. Activate my header using PacketHeaderManager PacketHeaderManager set hdrlen_ 0 f foreach h prot t { 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 li Outline • • • • • •

Overview Packet Allocation and Deallocation Packet Header Data Payload C t i i P Customizing Packets k t Summary

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

97

Summary • In most case, NO p packet is actually y send.  Put info. in the packet header and send it. • 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 one

• Packet deallocation

– Put the packet in the free packet list. list

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

98

Summary

• Packet header header:

– Variable = *bits_ – Contains several Protocol specific header (PSH) – How would you access a PSH ? ( use offset )

• 3 Main parts of PSH – A C++ class: – An OTcll class: l – A mapping class:

hdr_cmn PacketHeader/Common CommonHeaderClass

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

99

Summary • Packet creation process p 1.

At the compilation:

• • •

Construction of PSH and packet header mapping variables, invoke TclClass::bind(), and set up active protocol list

2. During the network configuration phase: Create packet format p 3. During the simulation: Create packets

• Defining a new packet header 1. 2. 3. 4.

Define f a PSH C++ class Define a PSH OTcl class Define a PSH mapping class Activate the header

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

100