doc/manual/node.texi
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Tue, 02 Sep 2008 12:00:11 -0700
changeset 3596 6c39b712a535
parent 3362 9a6f1b3c6e0b
child 4032 2b675a0b3b94
permissions -rw-r--r--
move Address and ack timeout down to MacLow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3362
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     1
@node Node and Internet Stack
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     2
@chapter Node and Internet Stack
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     3
@anchor{chap:Node}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     4
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     5
This chapter describes how ns-3 nodes are put together, and provides
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     6
a walk-through of how packets traverse an internet-based Node.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     7
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     8
@float Figure,fig:node
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     9
@caption{High-level node architecture.}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    10
@image{figures/node,5in}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    11
@end float
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    12
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    13
In ns-3, nodes are instances of @code{class Node}.  This class
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    14
may be subclassed, but instead, the conceptual model is that 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    15
we @emph{aggregate} or insert objects to it rather than define 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    16
subclasses.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    17
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    18
One might think of a bare ns-3 node as a shell of a computer,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    19
to which one may add NetDevices (cards) and other innards including
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    20
the protocols and applications.  @ref{fig:node} illustrates
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    21
that Node objects contain a list of Applications (initially,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    22
the list is empty), a list of NetDevices (initially, the list
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    23
is empty), a unique integer ID, and a system ID (for
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    24
distributed simulation).
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    25
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    26
The design tries to avoid putting too many dependencies on the
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    27
base class Node, Application, or NetDevice for the following:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    28
@itemize @bullet
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    29
@item IP version, or whether IP is at all even used in the Node.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    30
@item implementation details of the IP stack 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    31
@end itemize 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    32
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    33
From a software perspective, the lower interface of applications
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    34
corresponds to the C-based sockets API.  The upper interface
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    35
of NetDevice objects corresponds to the device independent
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    36
sublayer of the Linux stack.  Everything in between can be
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    37
aggregated and plumbed together as needed.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    38
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    39
Let's look more closely at the protocol demultiplexer.  We want
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    40
incoming frames at layer-2 to be delivered to the right layer-3
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    41
protocol such as Ipv4.  The
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
function of this demultiplexer is to register callbacks for
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    43
receiving packets.  The callbacks are indexed based on the 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    44
@uref{http://en.wikipedia.org/wiki/EtherType,,EtherType}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    45
in the layer-2 frame.   
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    46
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    47
Many different types of higher-layer protocols may be 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    48
connected to the NetDevice, such as IPv4, IPv6, ARP,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    49
MPLS, IEEE 802.1x, and packet sockets.  Therefore, the
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    50
use of a callback-based demultiplexer avoids the need to
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    51
use a common base class for all of these protocols, which
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    52
is problematic because of the different types of objects
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    53
(including packet sockets) expected to be registered there.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    54
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    55
Each NetDevice delivers packets to a callback with the following
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    56
signature:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    57
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    58
  /**
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    59
   * \param device a pointer to the net device which is calling this callback
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    60
   * \param packet the packet received
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    61
   * \param protocol the 16 bit protocol number associated with this packet.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    62
   *        This protocol number is expected to be the same protocol number
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    63
   *        given to the Send method by the user on the sender side.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    64
   * \param address the address of the sender
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    65
   * \returns true if the callback could handle the packet successfully, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    66
   *        false otherwise.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    67
   */
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    68
  typedef Callback<bool, Ptr<NetDevice>, Ptr<Packet>, uint16_t,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    69
    const Address &> ReceiveCallback;
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    70
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    71
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    72
There is a function in class Node that matches that signature:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    73
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    74
private:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    75
  bool ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet>,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    76
                          uint16_t protocol, const Address &from);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    77
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    78
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    79
However, users do not need to access this function directly.  Instead,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    80
when users call @code{uint32_t AddDevice (Ptr<NetDevice> device)}, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    81
the implementation of this function sets the callback (and the
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    82
function returns the ifIndex of the NetDevice on that Node).
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    83
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    84
But what does the ReceiveFromDevice function do?  Here, it looks
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    85
up another callback, in its list of callbacks, corresponding to the
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    86
matching EtherType.  This callback is called a ProtocolHandler, and
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    87
is specified as follows:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    88
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    89
  typedef Callback<void, Ptr<NetDevice>, Ptr<Packet>, uint16_t,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    90
    const Address &> ProtocolHandler;
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    91
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    92
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    93
Upper-layer protocols or objects are expected to provide such a function.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    94
and register it with the list of ProtocolHandlers by calling
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    95
@code{Node::RegisterProtocolHandler ();}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    96
For instance, if Ipv4 is aggregated to a Node, then the Ipv4 receive
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    97
function can be registered with the protocol handler by calling:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    98
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    99
  RegisterProtocolHandler (
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   100
    MakeCallback (&Ipv4L3Protocol::Receive, ipv4), 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   101
    Ipv4L3Protocol::PROT_NUMBER, 0);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   102
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   103
and likewise for Ipv6, Arp, etc.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   104
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   105
@section NodeList
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   106
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   107
Every Node created is automatically added to the ns-3 @code{NodeList}.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   108
The NodeList class provides an @code{Add()} method and C++ iterators
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   109
to allow one to walk the node list or fetch a Node pointer by
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   110
its integer identifier.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   111
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   112
@section Internet stack aggregation
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   113
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   114
The above @code{class Node} is not very useful as-is; other objects
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   115
must be aggregated to it to provide useful node functionality.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   116
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   117
The ns-3 source code directory @code{src/internet-stack} provides
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   118
implmentation of TCP/IPv4-related components.  These include IPv4,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   119
ARP, UDP, TCP, and other related protocols.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   120
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   121
Internet Nodes are not subclasses of class Node; they are simply Nodes 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   122
that have had a bunch of IPv4-related
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   123
objects aggregated to them.  They can be put together by hand, or
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   124
via a helper function @code{AddInternetStack ()} which does the 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   125
following:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   126
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   127
void AddInternetStack (Ptr<Node> node)
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   128
{
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   129
  // Create layer-3 protocols 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   130
  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   131
  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   132
  ipv4->SetNode (node);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   133
  arp->SetNode (node);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   134
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   135
  // Create an L4 demux 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   136
  Ptr<Ipv4L4Demux> ipv4L4Demux = CreateObject<Ipv4L4Demux> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   137
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   138
  // Create transport protocols and insert them into the demux
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   139
  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   140
  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   141
  
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   142
  ipv4L4Demux->SetNode (node);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   143
  udp->SetNode (node);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   144
  tcp->SetNode (node);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   145
  
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   146
  ipv4L4Demux->Insert (udp);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   147
  ipv4L4Demux->Insert (tcp);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   148
  
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   149
  // Add factories for instantiating transport protocol sockets
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   150
  Ptr<UdpSocketFactoryImpl> udpFactory = CreateObject<UdpSocketFactoryImpl> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   151
  Ptr<TcpSocketFactoryImpl> tcpFactory = CreateObject<TcpSocketFactoryImpl> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   152
  Ptr<Ipv4Impl> ipv4Impl = CreateObject<Ipv4Impl> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   153
  
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   154
  udpFactory->SetUdp (udp);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   155
  tcpFactory->SetTcp (tcp);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   156
  ipv4Impl->SetIpv4 (ipv4);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   157
  
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   158
  // Aggregate all of these new objects to the node
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   159
  node->AggregateObject (ipv4);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   160
  node->AggregateObject (arp);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   161
  node->AggregateObject (ipv4Impl);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   162
  node->AggregateObject (udpFactory);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   163
  node->AggregateObject (tcpFactory);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   164
  node->AggregateObject (ipv4L4Demux);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   165
}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   166
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   167
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   168
@subsection Internet Node structure
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   169
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   170
The Internet Node (an ns-3 Node augmented by aggregation to have one or more
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   171
IP stacks) has the following internal structure.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   172
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   173
@subsubsection Layer-3 protocols
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   174
At the lowest layer, sitting above the NetDevices, are the "layer 3" 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   175
protocols, including IPv4, IPv6, and ARP.  These protocols provide 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   176
the following key methods and data members:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   177
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   178
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   179
class Ipv4L3Protocol : public Object
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   180
{
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   181
public: 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   182
  // Add an Ipv4 interface corresponding to the provided NetDevice
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   183
  uint32_t AddInterface (Ptr<NetDevice> device);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   184
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   185
  // Receive function that can be bound to a callback, for receiving
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   186
  // packets up the stack
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   187
  void Receive( Ptr<NetDevice> device, Ptr<Packet> p, uint16_t protocol, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   188
    const Address &from);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   189
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   190
  // Higher-level layers call this method to send a packet
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   191
  // down the stack to the MAC and PHY layers
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   192
  // 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   193
  void Send (Ptr<Packet> packet, Ipv4Address source,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   194
             Ipv4Address destination, uint8_t protocol);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   195
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   196
private:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   197
  Ipv4InterfaceList m_interfaces;
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   198
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   199
  // Protocol handlers
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   200
}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   201
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   202
There are many more functions (such as @code{Forward ()}) but we will
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   203
focus on the above four items from an architectural perspective.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   204
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   205
First, note that the @code{Receive ()} function has a matching signature
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   206
to the ReceiveCallback in the @code{class Node}.  This function pointer
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   207
is inserted into the the Node's protocol handler when 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   208
@code{AddInterface ()} is called.  The actual registration is done
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   209
with a statement such as:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   210
follows:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   211
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   212
 RegisterProtocolHandler ( MakeCallback (&Ipv4Protocol::Receive, ipv4),
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   213
    Ipv4L3Protocol::PROT_NUMBER, 0);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   214
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   215
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   216
The Ipv4L3Protocol object is aggregated to the Node; there is only one
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   217
such Ipv4L3Protocol object.  Higher-layer protocols that have a packet
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   218
to send down to the Ipv4L3Protocol object can call 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   219
@code{GetObject<Ipv4L3Protocol> ()} to obtain a pointer, as follows:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   220
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   221
  Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   222
  if (ipv4 != 0)
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   223
    {
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   224
      ipv4->Send (packet, saddr, daddr, PROT_NUMBER);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   225
    }
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   226
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   227
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   228
This class nicely demonstrates two techniques we exploit in
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   229
ns-3 to bind objects together:  callbacks, and object aggregation.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   230
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   231
Once IPv4 has determined that a packet is for the local node, it 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   232
forwards it up the stack.  This is done with the following function:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   233
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   234
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   235
void
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   236
Ipv4L3Protocol::ForwardUp (Ptr<Packet> p, Ipv4Header const&ip,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   237
                           Ptr<Ipv4Interface> incomingInterface)
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   238
{
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   239
  NS_LOG_FUNCTION (this << p << &ip);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   240
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   241
  Ptr<Ipv4L4Demux> demux = m_node->GetObject<Ipv4L4Demux> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   242
  Ptr<Ipv4L4Protocol> protocol = demux->GetProtocol (ip.GetProtocol ());
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   243
  protocol->Receive (p, ip.GetSource (), ip.GetDestination (), incomingInterface);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   244
}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   245
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   246
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   247
The first step is to find the aggregated Ipv4L4Demux object.  Then, this
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   248
object is consulted to look up the right Ipv4L4Protocol, based on IP protocol
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   249
number.  For instance, TCP is registered in the demux as protocol number 6.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   250
Finally, the @code{Receive()} function on the Ipv4L4Protocol (such as
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   251
@code{TcpL4Protocol::Receive} is called.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   252
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   253
We have not yet introduced the class Ipv4Interface.  Basically,
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   254
each NetDevice is paired with an IPv4 representation of such device.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   255
In Linux, this @code{class Ipv4Interface} roughly corresponds to
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   256
the @code{struct in_device}; the main purpose is to provide 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   257
address-family specific information (addresses) about an interface.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   258
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   259
@subsubsection Layer-4 protocols and sockets
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   260
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   261
We next describe how the transport protocols, sockets, and applications
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   262
tie together.  In summary, each transport protocol implementation is
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   263
a socket factory.  An application that needs a new socket 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   264
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   265
For instance, to create a UDP socket, an application would use a code
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   266
snippet such as the following:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   267
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   268
      Ptr<Udp> udpSocketFactory = GetNode ()->GetObject<Udp> ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   269
      Ptr<Socket> m_socket = socketFactory->CreateSocket ();
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   270
      m_socket->Bind (m_local_address);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   271
      ...
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   272
@end verbatim 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   273
The above will query the node to get a pointer to its UDP socket
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   274
factory, will create one such socket, and will use the socket with
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   275
an API similar to the C-based sockets API, such as @code{Connect ()}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   276
and @code{Send ()}.  See the chapter on ns-3 sockets for more information.  
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   277
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   278
We have described so far a socket factory (e.g. @code{class Udp}) and
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   279
a socket, which may be specialized (e.g., @code{class UdpSocket}).
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   280
There are a few more key objects that relate to the specialized
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   281
task of demultiplexing a packet to one or more receiving sockets.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   282
The key object in this task is @code{class Ipv4EndPointDemux}.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   283
This demultiplexer stores objects of @code{class Ipv4EndPoint}.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   284
This class holds the addressing/port tuple (local port, local address, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   285
destination port, destination address) associated with the socket, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   286
and a receive callback.  This receive callback has a receive
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   287
function registered by the socket.  The @code{Lookup ()} function to
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   288
Ipv4EndPointDemux returns a list of Ipv4EndPoint objects (there may
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   289
be a list since more than one socket may match the packet).  The
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   290
layer-4 protocol copies the packet to each Ipv4EndPoint and calls
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   291
its @code{ForwardUp ()} method, which then calls the @code{Receive ()}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   292
function registered by the socket.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   293
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   294
An issue that arises when working with the sockets API on real
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   295
systems is the need to manage the reading from a socket, using 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   296
some type of I/O (e.g., blocking, non-blocking, asynchronous, ...).
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   297
ns-3 implements an asynchronous model for socket I/O; the application
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   298
sets a callback to be notified of received data ready to be read, and the 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   299
callback is invoked by the transport protocol when data is available.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   300
This callback is specified as follows:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   301
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   302
  void Socket::SetRecvCallback (Callback<void, Ptr<Socket>, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   303
    Ptr<Packet>, const Address&> receivedData);
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   304
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   305
The data being received is conveyed in the Packet data buffer.  An example
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   306
usage is in @code{class PacketSink}:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   307
@verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   308
  m_socket->SetRecvCallback (MakeCallback(&PacketSink::HandleRead, this));
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   309
@end verbatim
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   310
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   311
To summarize, internally, the UDP implementation is organized as follows:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   312
@itemize @bullet
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   313
@item a @code{UdpImpl} class that implements the Udp socket factory
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   314
functionality
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   315
@item a @code{UdpL4Protocol} class that implements the protocol logic
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   316
that is socket-independent 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   317
@item a @code{UdpSocketImpl} class that implements socket-specific aspects
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   318
of UDP
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   319
@item a class called @code{Ipv4EndPoint} that stores the
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   320
addressing tuple (local port, local address, destination port, destination
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   321
address) associated with the socket, and a receive callback for the socket.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   322
@end itemize
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   323
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   324
@subsection Internet Node interfaces
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   325
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   326
Many of the implementation details, or internal objects themselves, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   327
of Internet Node objects are not exposed at the simulator public
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   328
API.  This allows for different implementations; for instance, 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   329
replacing the native ns-3 models with ported TCP/IP stack code. 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   330
 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   331
The C++ public APIs of all of these objects is found in the
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   332
@code{src/node} directory, including principally:
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   333
@itemize @bullet
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   334
@item @code{socket.h}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   335
@item @code{tcp.h}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   336
@item @code{udp.h}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   337
@item @code{ipv4.h}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   338
@end itemize 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   339
These are typically base class objects that implement the default
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   340
values used in the implementation, implement access methods to get/set
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   341
state variables, host attributes, and implement publicly-available methods 
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   342
exposed to clients such as @code{CreateSocket}.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   343
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   344
@subsection Example path of a packet
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   345
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   346
These two figures show an example stack trace of how packets flow
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   347
through the Internet Node objects.
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   348
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   349
@float Figure,fig:internet-node-send
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   350
@caption{Send path of a packet.}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   351
@image{figures/internet-node-send,5in}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   352
@end float
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   353
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   354
@float Figure,fig:internet-node-recv
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   355
@caption{Receive path of a packet.}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   356
@image{figures/internet-node-recv,5in}
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   357
@end float
9a6f1b3c6e0b chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   358