author | Tom Henderson <tomh@tomh.org> |
Thu, 02 Jul 2009 21:57:00 -0700 | |
changeset 4646 | c25ca2e38845 |
parent 4032 | 2b675a0b3b94 |
permissions | -rw-r--r-- |
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 |
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
124 |
via a helper function @code{InternetStackHelper::Install ()} which does the |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
125 |
following to all nodes passed in as arguments: |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
126 |
@verbatim |
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
127 |
void |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
128 |
InternetStackHelper::Install (Ptr<Node> node) const |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
129 |
{ |
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
130 |
if (node->GetObject<Ipv4> () != 0) |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
131 |
{ |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
132 |
NS_FATAL_ERROR ("InternetStackHelper::Install(): Aggregating " |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
133 |
"an InternetStack to a node with an existing Ipv4 object"); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
134 |
return; |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
135 |
} |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
136 |
|
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
137 |
CreateAndAggregateObjectFromTypeId (node, "ns3::ArpL3Protocol"); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
138 |
CreateAndAggregateObjectFromTypeId (node, "ns3::Ipv4L3Protocol"); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
139 |
CreateAndAggregateObjectFromTypeId (node, "ns3::Icmpv4L4Protocol"); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
140 |
CreateAndAggregateObjectFromTypeId (node, "ns3::UdpL4Protocol"); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
141 |
node->AggregateObject (m_tcpFactory.Create<Object> ()); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
142 |
Ptr<PacketSocketFactory> factory = CreateObject<PacketSocketFactory> (); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
143 |
node->AggregateObject (factory); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
144 |
// Set routing |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
145 |
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> (); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
146 |
Ptr<Ipv4RoutingProtocol> ipv4Routing = m_routing->Create (node); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
147 |
ipv4->SetRoutingProtocol (ipv4Routing); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
148 |
} |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
149 |
@end verbatim |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
150 |
|
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
151 |
Note that the Ipv4 routing protocol is configured and set outside this |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
152 |
function. By default, the following protocols are added to Ipv4: |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
153 |
@verbatim |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
154 |
InternetStackHelper::InternetStackHelper () |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
155 |
{ |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
156 |
SetTcp ("ns3::TcpL4Protocol"); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
157 |
static Ipv4StaticRoutingHelper staticRouting; |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
158 |
static Ipv4GlobalRoutingHelper globalRouting; |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
159 |
static Ipv4ListRoutingHelper listRouting; |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
160 |
listRouting.Add (staticRouting, 0); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
161 |
listRouting.Add (globalRouting, -10); |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
162 |
SetRoutingHelper (listRouting); |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
163 |
} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
164 |
@end verbatim |
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 |
@subsection Internet Node structure |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
167 |
|
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
168 |
An IPv4-capable Node (an ns-3 Node augmented by aggregation to have one or more |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
169 |
IP stacks) has the following internal structure. |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
170 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
171 |
@subsubsection Layer-3 protocols |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
172 |
At the lowest layer, sitting above the NetDevices, are the "layer 3" |
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
173 |
protocols, including IPv4, IPv6 (in the future), and ARP. The |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
174 |
@code{class Ipv4L3Protocol} is an |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
175 |
implementation class whose public interface is |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
176 |
typically @code{class Ipv4} (found in src/node directory), but the |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
177 |
Ipv4L3Protocol public API is also used internally in the |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
178 |
src/internet-stack directory at present. |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
179 |
|
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
180 |
In class Ipv4L3Protocol, one method described below is @code{Receive ()}: |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
181 |
@verbatim |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
182 |
/** |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
183 |
* Lower layer calls this method after calling L3Demux::Lookup |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
184 |
* The ARP subclass needs to know from which NetDevice this |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
185 |
* packet is coming to: |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
186 |
* - implement a per-NetDevice ARP cache |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
187 |
* - send back arp replies on the right device |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
188 |
*/ |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
189 |
void Receive( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from, |
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
190 |
const Address &to, NetDevice::PacketType packetType); |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
191 |
@end verbatim |
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 |
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
|
194 |
to the ReceiveCallback in the @code{class Node}. This function pointer |
4032
2b675a0b3b94
Fix misprints in tutorial (bug 453)
Tom Henderson <tomh@tomh.org>
parents:
3362
diff
changeset
|
195 |
is inserted into the Node's protocol handler when |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
196 |
@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
|
197 |
with a statement such as: |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
198 |
follows: |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
199 |
@verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
200 |
RegisterProtocolHandler ( MakeCallback (&Ipv4Protocol::Receive, ipv4), |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
201 |
Ipv4L3Protocol::PROT_NUMBER, 0); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
202 |
@end verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
203 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
@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
|
208 |
@verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
209 |
Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> (); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
210 |
if (ipv4 != 0) |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
211 |
{ |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
212 |
ipv4->Send (packet, saddr, daddr, PROT_NUMBER); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
213 |
} |
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 |
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
|
217 |
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
|
218 |
|
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
219 |
Once IPv4 routing has determined that a packet is for the local node, it |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
220 |
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
|
221 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
222 |
@verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
223 |
void |
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
224 |
Ipv4L3Protocol::LocalDeliver (Ptr<const Packet> packet, Ipv4Header const&ip, uint32_t iif) |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
225 |
@end verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
226 |
|
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
227 |
The first step is to find the right Ipv4L4Protocol object , based on IP protocol |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
228 |
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
|
229 |
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
|
230 |
@code{TcpL4Protocol::Receive} is called. |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
231 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
238 |
@subsubsection Layer-4 protocols and sockets |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
239 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
244 |
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
|
245 |
snippet such as the following: |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
246 |
@verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
247 |
Ptr<Udp> udpSocketFactory = GetNode ()->GetObject<Udp> (); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
248 |
Ptr<Socket> m_socket = socketFactory->CreateSocket (); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
249 |
m_socket->Bind (m_local_address); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
250 |
... |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
251 |
@end verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
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
|
256 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
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
|
267 |
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
|
268 |
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
|
269 |
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
|
270 |
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
|
271 |
function registered by the socket. |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
272 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
This callback is specified as follows: |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
280 |
@verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
281 |
void Socket::SetRecvCallback (Callback<void, Ptr<Socket>, |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
282 |
Ptr<Packet>, const Address&> receivedData); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
283 |
@end verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
284 |
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
|
285 |
usage is in @code{class PacketSink}: |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
286 |
@verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
287 |
m_socket->SetRecvCallback (MakeCallback(&PacketSink::HandleRead, this)); |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
288 |
@end verbatim |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
289 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
290 |
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
|
291 |
@itemize @bullet |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
292 |
@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
|
293 |
functionality |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
294 |
@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
|
295 |
that is socket-independent |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
296 |
@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
|
297 |
of UDP |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
298 |
@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
|
299 |
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
|
300 |
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
|
301 |
@end itemize |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
302 |
|
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
303 |
@subsection Ipv4-capable node interfaces |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
304 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
305 |
Many of the implementation details, or internal objects themselves, |
4646
c25ca2e38845
some fixes to the manual for IPv4 refactoring
Tom Henderson <tomh@tomh.org>
parents:
4032
diff
changeset
|
306 |
of Ipv4-capable Node objects are not exposed at the simulator public |
3362
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
307 |
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
|
308 |
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
|
309 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
310 |
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
|
311 |
@code{src/node} directory, including principally: |
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 @code{socket.h} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
314 |
@item @code{tcp.h} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
315 |
@item @code{udp.h} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
316 |
@item @code{ipv4.h} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
317 |
@end itemize |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
exposed to clients such as @code{CreateSocket}. |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
322 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
323 |
@subsection Example path of a packet |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
324 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
325 |
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
|
326 |
through the Internet Node objects. |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
327 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
328 |
@float Figure,fig:internet-node-send |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
329 |
@caption{Send path of a packet.} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
330 |
@image{figures/internet-node-send,5in} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
331 |
@end float |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
332 |
|
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
333 |
@float Figure,fig:internet-node-recv |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
334 |
@caption{Receive path of a packet.} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
335 |
@image{figures/internet-node-recv,5in} |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
336 |
@end float |
9a6f1b3c6e0b
chapter on Node and internet stack, for manual
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
337 |