Sunday, August 9, 2009

Packet Forwarding Mechanism

In NS2, objects that are able to pass packets around are called NsObject. They are the objects derived from class NsObject. Class NsObject declares function recv(p,h) as pure virtual, forcing its derived class to specify how a packet is received. For example,
  • Once a connector receives a packet, it immediately passes the packet to its forwarding object.
  • Once a queue receives a packet, it puts the packet in its buffer. The queue will send out only the head-of-the-line packet.
As you might guess from the name of the function, NS2 models packet forwarding by "receiving" rather than "sending". In most cases, a packet forwarder asks a packet receiver to receives a packet by calling the function recv(p,h) of the packet receiver For example, as a packet forwarder, a Connector contains a pointer "target_" to a packet receiver. When a connect needs to send a packet "*p" to a packet receiver, it executes target_->(p,h), where "h" is a pointer to a handler.

In general, an NsObject forwards packets in two following ways:
  1. Immediate packet forwarding: A packet forwarder forwards a packet as soon as it receives the packet. The packet receiver receives the packet at the same time as the packet forwarder does. For example, a Connector forwards a packet to its downstream object by invoking target_->recv(p,h). In this case, the packet receiver (i.e., "*target_") receives the packet "*p" at the same time as the Connector does.
  2. Delayed packet forwarding: To delay packet forwarding, the packet "*p" is cast to be an Event object, associated with a packet receiving NsObject (i.e., handler "*h"), and placed on the simulation timeline at a given simulation time"t". This is to schedule a packet reception event at time "t". When the simulation runs to time "t", function handle of the NsObject "*h" is invoked. From within function NsObject::handle, function recv is executed, and the packet "*p" is received by the NsObject "*h".
In case of delayed packet forwarding, the input argument "h" (i.e., a pointer to the handler) is mandatory. The handler is a reference to the NsObject which will receive the packet at a certain time "t". Without handler, NS2 will not know where it should send the packet to.

The handler is optional for immediate packet forwarding. It is not used when executing recv(p,h). We can simply provide the "h" as a null pointer. However, in some cases, the handler will be passed to the downstream objects. At the place where a delayed packet forwarding is invoked, the handler "h" will be used as an input argument.

For more information about NsObject and packet forwarding, see the
following book from Springer:

T. Issaraiyakul and E. Hossain, "Introduction to Network Simulator
NS2", Springer 2008.
http://www.springer.com/engineering/signals/book/978-0-387-71759-3

You may also find the following website useful:
http://www.ece.ubc.ca/~teerawat/NS2.htm

1 comment:

Anonymous said...

Dear Teerawat,

I tried getting your email address but got no luck. This looks like the only way I can get in touch with you.

I have your 2008 ns2 book. I have a question about the figure on page 169.

Should the IP header be to the left of the TCP header? In reality, the IP header is at the outer layer and the TCP header is at the inner layer.

Also, where are the static vairables offset_ (of different headers) assigned a value?

Thanks a lot and best regards.