Basic packet
forwarding in
     NS2
   N S 2   U l t i m a t e . c o m
                b y
T e e r a w a t I s s a r i y a k u l
OUTLINE
Before you begin...

NsObject

Packet forwarding

Example

    Class Connector

    Class Queue

Questions?
                       www.ns2ultimate.com
Before you Begin ...


              In NS2:

  • NO such thing as transmission
  • RECEIVE ONLY



                                    www.ns2ultimate.com
What we are going to
      do here
Suppose you’d like to send a packet from one
NsObject to another

What’s NsObject? ➠ See [ this link ]

                   packet
NsObject                          NsObject




                                       www.ns2ultimate.com
NSObjects
Inherited Functionalities:

    Class TclObject ➠ OTcl interface

    Class Handler ➠ Default actions

New Functionalities:

    Receive packet ➠ function recv(p,h)



                                 www.ns2ultimate.com
This section gives an overview of C++ class hierarchies. The entire hierarchy

     NSObjects: Inherited
consists of over 100 C++ classes and struct data types. Here, we only show
a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the

       functionalities
complete class hierarchy.


                  OTcl Interface                                     Default Action


                                  TclObject                Handler



     Simulator                PacketQueue      NsObject      AtHandler       QueueHandler

           RoutingModule
                                                      Network Component


                 Classifier                    Connector                    LanRouter


                          Uni-directional Point-to-
                          point Object Connector


                  Queue           Agent       ErrorModel       LinkDelay          Trace

                                                              www.ns2ultimate.com
Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes
NSObject: New
     functionality
class NsObject : public TclObject, public Handler {

public:

   NsObject();

   virtual void recv(Packet*, Handler*) = 0;
};




                                        www.ns2ultimate.com
NSObject: New
     functionality
class NsObject : public TclObject, public Handler {

public:

   NsObject();

   virtual void recv(Packet*, Handler*) = 0;
};



          TclObject
                            NsObject
         Handler

                                        www.ns2ultimate.com
NSObject: New
     functionality
class NsObject : public TclObject, public Handler {

public:

   NsObject();

   virtual void recv(Packet*, Handler*) = 0;
};



packet reception function
Input = a pointer to a Packet object
Input = a pointer to a Handler object
abstract function
                                        www.ns2ultimate.com
packet forwarding
NS2 refers to most objects using pointers

Including NsObjects and Packets

Example

   “p” = a pointer

    “*p” = a place where the pointer “p” pointer
   to


                                      www.ns2ultimate.com
packet forwarding
Task: An object “*s” sends packet “*p” to an
object “*d”


 s                      p                  d

                   packet
NsObject                           NsObject



                                      www.ns2ultimate.com
C++ Statement
From within “*s”, execute one of the following
two C++ Statements:

     Given a handler *h: “d->recv(p,h)”
     Handler does not exists: “d->recv(p)”
 s                      p                  d

                   packet
NsObject                           NsObject
                                      www.ns2ultimate.com
Chapter 15.



                      Examples
5.1.2 C++ Class Hierarchy

This section gives an overview of C++ class hierarchies. The entire hierarchy
consists of over 100 C++ classes and struct data types. Here, we only show
a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the
Six main categories
complete class hierarchy.


                  OTcl Interface                                     Default Action


                                  TclObject                Handler



     Simulator                PacketQueue      NsObject      AtHandler       QueueHandler

           RoutingModule
                                                      Network Component


                 Classifier                    Connector                    LanRouter


                          Uni-directional Point-to-
                          point Object Connector


                  Queue           Agent       ErrorModel       LinkDelay          Trace

Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes
in boxes with thick solid lines).                              www.ns2ultimate.com
Example:
     Class Connector
class Connector : public NsObject {

public:


    Connector();


    NsObject* target_;

     NsObject* drop_;

};                        • Class variable
                          • A pointer to NsObject
                                     www.ns2ultimate.com
Class Connector
Example configuration
 target_ points to the next NsObject
 drop_ points to an NsObject responsible for
 dropping a packet
   100 5 Network Objects

                               Connector

      NsObject                                                        NsObject
                                 target_
         Upstream                           Packet forwarding path      Downstream
         NsObject                                                        NsObject
                                 drop_


                      Packet
                    dropping
                        path

                        NsObject
                          Packet Dropping
                             NsObject                                www.ns2ultimate.com
Class Connector
To drop a packet, “send the packet to the
dropping NsObject”

      void Connector::drop(Packet* p)
      {
      
 if (drop_ != 0)
      
 
 drop_->recv(p);
      
 else
      
 
 Packet::free(p);
      }

                      Send a packet *p to the
                      NsObject *drop_
                                        www.ns2ultimate.com
5.1.2 C++ Class Hierarchy

          Another example:
This section gives an overview of C++ class hierarchies. The entire hierarchy
consists of over 100 C++ classes and struct data types. Here, we only show

            Class Queue
a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the
complete class hierarchy.


                  OTcl Interface                                     Default Action


                                  TclObject                Handler



     Simulator                PacketQueue      NsObject      AtHandler       QueueHandler

           RoutingModule
                                                      Network Component


                 Classifier                    Connector                    LanRouter


                          Uni-directional Point-to-
                          point Object Connector
                                                      Derive from class Connector

                  Queue           Agent       ErrorModel       LinkDelay          Trace

Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes
                 Contain pointers target_ and drop_.
in boxes with thick solid lines).

                                                                                          www.ns2ultimate.com
Another example:
   Class Queue
void Queue::recv(Packet* p, Handler*)
{

 double now = Scheduler::instance().clock();

 enque(p);

 if (!blocked_) {

 
 p = deque();

 
 if (p != 0) {

 
 
 blocked_ = 1;

 
 
 target_->recv(p, &qh_);

 
 }

 }
}

     Send a packet *p to the NsObject
     *target_ with a handler qh_ www.ns2ultimate.com
Questions?
How doNetwork Objectsa topology like this?
 100 5 we setup

                                   Connector

         NsObject                                                        NsObject
                                     target_
            Upstream                            Packet forwarding path     Downstream
            NsObject                                                        NsObject
                                     drop_


                          Packet
                        dropping
                            path

                            NsObject
                              Packet Dropping
                                 NsObject

 Fig. 5.2. Diagram of a connector: The solid arrows represent pointers, while the
What about handler? What is it? What are its
 dotted arrows show packet forwarding and dropping paths.

implications?
 Program 5.3 Declaration and function recv(p,h) of class Connector
         //~/ns/common/connector.h
     1   class Connector : public NsObject {                              www.ns2ultimate.com
Stay Tune!
I’ll discuss these
 in the following
       posts!
For more
 information
  about NS2

   P l e a s        e   s e e
    t h i s         b o o k
         f r        o m
     S p r i        n g e r
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009

 or visit www.ns2ultimate.com

Basic Packet Forwarding in NS2

  • 1.
    Basic packet forwarding in NS2 N S 2 U l t i m a t e . c o m b y T e e r a w a t I s s a r i y a k u l
  • 2.
    OUTLINE Before you begin... NsObject Packetforwarding Example Class Connector Class Queue Questions? www.ns2ultimate.com
  • 3.
    Before you Begin... In NS2: • NO such thing as transmission • RECEIVE ONLY www.ns2ultimate.com
  • 4.
    What we aregoing to do here Suppose you’d like to send a packet from one NsObject to another What’s NsObject? ➠ See [ this link ] packet NsObject NsObject www.ns2ultimate.com
  • 5.
    NSObjects Inherited Functionalities: Class TclObject ➠ OTcl interface Class Handler ➠ Default actions New Functionalities: Receive packet ➠ function recv(p,h) www.ns2ultimate.com
  • 6.
    This section givesan overview of C++ class hierarchies. The entire hierarchy NSObjects: Inherited consists of over 100 C++ classes and struct data types. Here, we only show a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the functionalities complete class hierarchy. OTcl Interface Default Action TclObject Handler Simulator PacketQueue NsObject AtHandler QueueHandler RoutingModule Network Component Classifier Connector LanRouter Uni-directional Point-to- point Object Connector Queue Agent ErrorModel LinkDelay Trace www.ns2ultimate.com Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes
  • 7.
    NSObject: New functionality class NsObject : public TclObject, public Handler { public: NsObject(); virtual void recv(Packet*, Handler*) = 0; }; www.ns2ultimate.com
  • 8.
    NSObject: New functionality class NsObject : public TclObject, public Handler { public: NsObject(); virtual void recv(Packet*, Handler*) = 0; }; TclObject NsObject Handler www.ns2ultimate.com
  • 9.
    NSObject: New functionality class NsObject : public TclObject, public Handler { public: NsObject(); virtual void recv(Packet*, Handler*) = 0; }; packet reception function Input = a pointer to a Packet object Input = a pointer to a Handler object abstract function www.ns2ultimate.com
  • 10.
    packet forwarding NS2 refersto most objects using pointers Including NsObjects and Packets Example “p” = a pointer “*p” = a place where the pointer “p” pointer to www.ns2ultimate.com
  • 11.
    packet forwarding Task: Anobject “*s” sends packet “*p” to an object “*d” s p d packet NsObject NsObject www.ns2ultimate.com
  • 12.
    C++ Statement From within“*s”, execute one of the following two C++ Statements: Given a handler *h: “d->recv(p,h)” Handler does not exists: “d->recv(p)” s p d packet NsObject NsObject www.ns2ultimate.com
  • 13.
    Chapter 15. Examples 5.1.2 C++ Class Hierarchy This section gives an overview of C++ class hierarchies. The entire hierarchy consists of over 100 C++ classes and struct data types. Here, we only show a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the Six main categories complete class hierarchy. OTcl Interface Default Action TclObject Handler Simulator PacketQueue NsObject AtHandler QueueHandler RoutingModule Network Component Classifier Connector LanRouter Uni-directional Point-to- point Object Connector Queue Agent ErrorModel LinkDelay Trace Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes in boxes with thick solid lines). www.ns2ultimate.com
  • 14.
    Example: Class Connector class Connector : public NsObject { public: Connector(); NsObject* target_; NsObject* drop_; }; • Class variable • A pointer to NsObject www.ns2ultimate.com
  • 15.
    Class Connector Example configuration target_ points to the next NsObject drop_ points to an NsObject responsible for dropping a packet 100 5 Network Objects Connector NsObject NsObject target_ Upstream Packet forwarding path Downstream NsObject NsObject drop_ Packet dropping path NsObject Packet Dropping NsObject www.ns2ultimate.com
  • 16.
    Class Connector To dropa packet, “send the packet to the dropping NsObject” void Connector::drop(Packet* p) { if (drop_ != 0) drop_->recv(p); else Packet::free(p); } Send a packet *p to the NsObject *drop_ www.ns2ultimate.com
  • 17.
    5.1.2 C++ ClassHierarchy Another example: This section gives an overview of C++ class hierarchies. The entire hierarchy consists of over 100 C++ classes and struct data types. Here, we only show Class Queue a part of the hierarchy (in Fig. 5.1). The readers are referred to [18] for the complete class hierarchy. OTcl Interface Default Action TclObject Handler Simulator PacketQueue NsObject AtHandler QueueHandler RoutingModule Network Component Classifier Connector LanRouter Uni-directional Point-to- point Object Connector Derive from class Connector Queue Agent ErrorModel LinkDelay Trace Fig. 5.1. A part of NS2 C++ class hierarchy (this chapter emphasizes on classes Contain pointers target_ and drop_. in boxes with thick solid lines). www.ns2ultimate.com
  • 18.
    Another example: Class Queue void Queue::recv(Packet* p, Handler*) { double now = Scheduler::instance().clock(); enque(p); if (!blocked_) { p = deque(); if (p != 0) { blocked_ = 1; target_->recv(p, &qh_); } } } Send a packet *p to the NsObject *target_ with a handler qh_ www.ns2ultimate.com
  • 19.
    Questions? How doNetwork Objectsatopology like this? 100 5 we setup Connector NsObject NsObject target_ Upstream Packet forwarding path Downstream NsObject NsObject drop_ Packet dropping path NsObject Packet Dropping NsObject Fig. 5.2. Diagram of a connector: The solid arrows represent pointers, while the What about handler? What is it? What are its dotted arrows show packet forwarding and dropping paths. implications? Program 5.3 Declaration and function recv(p,h) of class Connector //~/ns/common/connector.h 1 class Connector : public NsObject { www.ns2ultimate.com
  • 20.
    Stay Tune! I’ll discussthese in the following posts!
  • 21.
    For more information about NS2 P l e a s e s e e t h i s b o o k f r o m S p r i n g e r T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009 or visit www.ns2ultimate.com