A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
neighbor-cache-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 ZHIHENG DONG
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Zhiheng Dong <dzh2077@gmail.com>
18 */
19
20/**
21 * This example shows how to generate neighbor caches on the scope of
22 * a channel, a NetDeviceContainer, an InterfaceContainer and globally.
23 * The example also shows a scenario that auto-generated neighbor caches
24 * can avoid dropping packets because of ARP message or Neighbor Discover
25 * message.
26 * If the user runs the example without any command line option, NeighborCacheHelper
27 * will generate neighbor cache for all devices and the output is their
28 * ARP table or NDISC table, the user can also generate neighbor cache on different
29 * scope by adding command line option --useChannel (on specific channel),
30 * --useNetDeviceContainer (on specific netDeviceContainer) and --useInterfaceContainer
31 * (on specific netDeviceContainer), --noGenerate (don't generate neighbor cache).
32 * The user can also enable --sendTraffic flag to send a data stream from n0 to n1,
33 * the output will be the information of received packets, which shows the packets loss
34 * are reduced after pre-generate neighbor caches.
35 *
36 * IPv4 Network Topology
37 * \verbatim
38
39 LAN 10.1.1.0/24
40 n0 --------------- n1 n2 n3
41 | | |
42 ===============
43 LAN 10.1.2.0/24
44 \endverbatim
45 *
46 *IPv6 Network Topology
47 * \verbatim
48
49 LAN 2001:1::/64
50 n0 --------------- n1 n2 n3
51 | | |
52 ===============
53 LAN 2001:2::/64
54 \endverbatim
55 *
56 * Expected Outputs:
57 * IPv4 (default):
58 * Generate ARP caches for all deveices (default) :
59 * \verbatim
60 ARP Cache of node 0 at time 0
61 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
62 ARP Cache of node 1 at time 0
63 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
64 10.1.2.2 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
65 10.1.2.3 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
66 ARP Cache of node 2 at time 0
67 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
68 10.1.2.3 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
69 ARP Cache of node 3 at time 0
70 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
71 10.1.2.2 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
72 \endverbatim
73 *
74 * Generate ARP caches for the left channel (--useChannel):
75 * \verbatim
76 ARP Cache of node 0 at time 0
77 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
78 ARP Cache of node 1 at time 0
79 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
80 ARP Cache of node 2 at time 0
81 ARP Cache of node 3 at time 0
82 \endverbatim
83 *
84 * Generate ARP caches for devices on the right side (--useNetDeviceContainer):
85 * \verbatim
86 ARP Cache of node 0 at time 0
87 ARP Cache of node 1 at time 0
88 10.1.2.2 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
89 10.1.2.3 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
90 ARP Cache of node 2 at time 0
91 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
92 10.1.2.3 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
93 ARP Cache of node 3 at time 0
94 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
95 10.1.2.2 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
96 \endverbatim
97 *
98 * Generate ARP caches for specific interfaces (--useInterfaceContainer):
99 * \verbatim
100 ARP Cache of node 0 at time 0
101 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
102 ARP Cache of node 1 at time 0
103 ARP Cache of node 2 at time 0
104 ARP Cache of node 3 at time 0
105 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
106 10.1.2.2 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
107 \endverbatim
108 *
109 * Do not generate neighbor cache (--noGenerate):
110 * \verbatim
111 ARP Cache of node 0 at time 0
112 ARP Cache of node 1 at time 0
113 ARP Cache of node 2 at time 0
114 ARP Cache of node 3 at time 0
115 \endverbatim
116 *
117 * sending packet with pre-generated neighbor caches(--sendTraffic)
118 * \verbatim
119 Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=0
120 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND
121 (seq=1 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512)
122 AND (seq=2 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 ->
123 (size=512) AND (seq=3 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to
124 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=4 time=+1s) Rx pkt from
125 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=5 time=+1s) Rx pkt
126 from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=6 time=+1s) Rx
127 pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=7
128 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND
129 (seq=8 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512)
130 AND (seq=9 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 ->
131 (size=512) AND (seq=10 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to
132 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=11 time=+1s) Rx pkt from
133 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=12 time=+1.00001s)
134 \endverbatim
135 *
136 * sending packet without pre-generated neighbor caches(--sendTraffic --noGenerate)
137 * \verbatim
138 Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=0
139 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND
140 (seq=1 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512)
141 AND (seq=2 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 ->
142 (size=512) AND (seq=8 time=+1s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to
143 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=9 time=+1s) Rx pkt from
144 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=10 time=+1s) Rx pkt
145 from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=11 time=+1s)
146 Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512) AND (seq=12
147 time=+1.00001s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 -> (size=512)
148 AND (seq=13 time=+1.00001s) Rx pkt from 03-07-0a:01:01:01:01:c0:00 to 03-07-0a:01:01:02:09:00:00 ->
149 (size=512) AND (seq=14 time=+1.00001s) \endverbatim
150 *
151 * IPv6 (--useIPv6):
152 * Generate NDISC caches for all deveices (default) :
153 * \verbatim
154 NDISC Cache of node 0 at time +0s
155 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
156 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
157 NDISC Cache of node 1 at time +0s
158 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
159 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
160 2001:2::200:ff:fe00:4 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
161 2001:2::200:ff:fe00:5 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
162 fe80::200:ff:fe00:4 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
163 fe80::200:ff:fe00:5 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
164 NDISC Cache of node 2 at time +0s
165 2001:2::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
166 2001:2::200:ff:fe00:5 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
167 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
168 fe80::200:ff:fe00:5 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
169 NDISC Cache of node 3 at time +0s
170 2001:2::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
171 2001:2::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
172 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
173 fe80::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
174 \endverbatim
175 *
176 * Generate NDISC caches for the left channel (--useChannel):
177 * \verbatim
178 NDISC Cache of node 0 at time +0s
179 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
180 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
181 NDISC Cache of node 1 at time +0s
182 2001:1::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
183 fe80::200:ff:fe00:1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
184 NDISC Cache of node 2 at time +0s
185 NDISC Cache of node 3 at time +0s
186 \endverbatim
187 *
188 * Generate NDISC caches for devices on the right side (--useNetDeviceContainer):
189 * \verbatim
190 NDISC Cache of node 0 at time +0s
191 NDISC Cache of node 1 at time +0s
192 2001:2::200:ff:fe00:4 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
193 2001:2::200:ff:fe00:5 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
194 fe80::200:ff:fe00:4 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
195 fe80::200:ff:fe00:5 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
196 NDISC Cache of node 2 at time +0s
197 2001:2::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
198 2001:2::200:ff:fe00:5 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
199 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
200 fe80::200:ff:fe00:5 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
201 NDISC Cache of node 3 at time +0s
202 2001:2::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
203 2001:2::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
204 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
205 fe80::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
206 \endverbatim
207 *
208 * Generate NDISC caches for specific interfaces (--useInterfaceContainer):
209 * \verbatim
210 NDISC Cache of node 0 at time +0s
211 2001:1::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
212 fe80::200:ff:fe00:2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
213 NDISC Cache of node 1 at time +0s
214 NDISC Cache of node 2 at time +0s
215 NDISC Cache of node 3 at time +0s
216 2001:2::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
217 2001:2::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
218 fe80::200:ff:fe00:3 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
219 fe80::200:ff:fe00:4 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
220 \endverbatim
221 *
222 * Do not generate neighbor cache (--noGenerate):
223 * \verbatim
224 NDISC Cache of node 0 at time +0s
225 NDISC Cache of node 1 at time +0s
226 NDISC Cache of node 2 at time +0s
227 NDISC Cache of node 3 at time +0s
228 \endverbatim
229 *
230 * sending packet with pre-generated neighbor caches(--sendTraffic)
231 * \verbatim
232 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
233 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=0 time=+1s) Rx
234 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
235 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=1 time=+1s) Rx
236 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
237 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=2 time=+1s) Rx
238 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
239 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=3 time=+1s) Rx
240 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
241 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=4 time=+1s) Rx
242 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
243 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=5 time=+1s) Rx
244 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
245 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=6 time=+1s) Rx
246 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
247 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=7 time=+1s) Rx
248 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
249 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=8 time=+1s) Rx
250 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
251 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=9 time=+1s) Rx
252 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
253 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=10 time=+1s) Rx
254 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
255 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=11 time=+1s)
256 \endverbatim
257 *
258 * sending packet without pre-generated neighbor caches(--sendTraffic --noGenerate)
259 * \verbatim
260 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
261 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=8 time=+1s) Rx
262 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
263 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=9 time=+1s) Rx
264 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
265 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=10 time=+1s) Rx
266 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
267 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=11 time=+1s) Rx
268 pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
269 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=12
270 time=+1.00001s) Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
271 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=13
272 time=+1.00001s) Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
273 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=14
274 time=+1.00001s) Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
275 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=15
276 time=+1.00001s) Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to
277 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:02:09:00 -> (size=512) AND (seq=16
278 time=+1.00001s) \endverbatim
279*/
280
281#include "ns3/applications-module.h"
282#include "ns3/core-module.h"
283#include "ns3/csma-module.h"
284#include "ns3/internet-module.h"
285#include "ns3/network-module.h"
286
287using namespace ns3;
288
289NS_LOG_COMPONENT_DEFINE("NeighborCacheExample");
290
291/**
292 * neighbor Cache example class.
293 *
294 * It handles the creation and run of an example.
295 */
297{
298 public:
300
301 /**
302 * Run the example.
303 */
304 void Run();
305
306 /**
307 * \brief description the command-line parameters.
308 * \param argc The argument count.
309 * \param argv The argument vector.
310 */
311 void CommandSetup(int argc, char** argv);
312
313 private:
314 /**
315 * \brief Print the information of receive data.
316 * \param pkt The received packet.
317 * \param from The sender.
318 * \param dst The receiver.
319 * \param header The header with a sequence, a timestamp, and a "size" attribute
320 */
322 const Address& from,
323 const Address& dst,
324 const SeqTsSizeHeader& header);
325
326 bool m_useIpv6{false}; //!< Use IPv6 instead of IPv4
327 bool m_enableLog{false}; //!< Enable ArpL3Protocol and Icmpv6L4Protocol logging
328 bool m_useChannel{false}; //!< Generate neighbor cache for specific Channel
330 false}; //!< Generate neighbor cache for specific netDeviceContainer
332 false}; //!< Generate neighbor cache for specific interfaceContainer
333 bool m_noGenerate{false}; //!< do not generate neighbor cache automatically
334 bool m_sendTraffic{false}; //!< send data stream from n0 to n1
335};
336
338{
339 NS_LOG_FUNCTION(this);
340}
341
342void
344 const Address& from,
345 const Address& dst,
346 const SeqTsSizeHeader& header)
347{
348 std::cout << "Rx pkt from " << from << " to " << dst << " -> " << header << std::endl;
349}
350
351void
353{
354 CommandLine cmd(__FILE__);
355 cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", m_useIpv6);
356 cmd.AddValue("enableLog", "Enable ArpL3Protocol and Icmpv6L4Protocol logging", m_enableLog);
357 cmd.AddValue("useChannel", "Generate neighbor cache for specific Channel", m_useChannel);
358 cmd.AddValue("useNetDeviceContainer",
359 "Generate neighbor cache for specific netDeviceContainer",
361 cmd.AddValue("useInterfaceContainer",
362 "Generate neighbor cache for specific interfaceContainer",
364 cmd.AddValue("noGenerate", "do not generate neighbor cache automatically", m_noGenerate);
365 cmd.AddValue("sendTraffic", "send data stream from n0 to n1", m_sendTraffic);
366
367 cmd.Parse(argc, argv);
368}
369
370int
371main(int argc, char* argv[])
372{
373 NeighborCacheExample example;
374 example.CommandSetup(argc, argv);
375 example.Run();
376 return 0;
377}
378
379void
381{
382 if (m_enableLog)
383 {
384 LogComponentEnable("ArpL3Protocol", LOG_LEVEL_LOGIC);
385 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_LOGIC);
386 }
387 uint32_t nCsmaLeft = 2;
388 uint32_t nCsmaRight = 2;
389
390 NodeContainer csmaNodesLeft;
391 csmaNodesLeft.Create(nCsmaLeft);
392 NodeContainer csmaNodesRight;
393 csmaNodesRight.Add(csmaNodesLeft.Get(1));
394 csmaNodesRight.Create(nCsmaRight);
395
396 CsmaHelper csmaLeft;
397 csmaLeft.SetChannelAttribute("DataRate", StringValue("20Gbps"));
398 // The 1 microSeconds delay is only for showing packets dropped effect without generating
399 // neighbor caches.
400 csmaLeft.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
401 CsmaHelper csmaRight;
402 csmaRight.SetChannelAttribute("DataRate", StringValue("20Gbps"));
403 csmaRight.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
404
405 NetDeviceContainer csmaDevicesLeft;
406 csmaDevicesLeft = csmaLeft.Install(csmaNodesLeft);
407 NetDeviceContainer csmaDevicesRight;
408 csmaDevicesRight = csmaRight.Install(csmaNodesRight);
409
411 if (!m_useIpv6)
412 {
413 stack.SetIpv6StackInstall(false);
414 }
415 else
416 {
417 stack.SetIpv4StackInstall(false);
418 }
419 // disabled Ipv4ArpJitter and Ipv6NsRsJitter to avoid the influence on packet dropped
420 stack.SetIpv4ArpJitter(false);
421 stack.SetIpv6NsRsJitter(false);
422 stack.Install(csmaNodesLeft.Get(0));
423 stack.Install(csmaNodesRight);
424
425 if (!m_useIpv6)
426 {
427 Ipv4AddressHelper address;
428 address.SetBase("10.1.1.0", "255.255.255.0");
429 Ipv4InterfaceContainer csmaInterfacesLeft;
430 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
431 address.SetBase("10.1.2.0", "255.255.255.0");
432 Ipv4InterfaceContainer csmaInterfacesRight;
433 csmaInterfacesRight = address.Assign(csmaDevicesRight);
434
435 // Populate ARP caches
436 NeighborCacheHelper neighborCache;
437 if (m_useChannel)
438 {
439 // Populate ARP caches for given channel
440 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
441 neighborCache.PopulateNeighborCache(csmaChannel);
442 }
444 {
445 // Populate ARP caches for given netDeviceContainer
446 neighborCache.PopulateNeighborCache(csmaDevicesRight);
447 }
449 {
450 std::pair<Ptr<Ipv4>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
451 Ptr<Ipv4> ipv41 = txInterface.first;
452 uint32_t index1 = txInterface.second;
453 std::pair<Ptr<Ipv4>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
454 Ptr<Ipv4> ipv42 = rxInterface.first;
455 uint32_t index2 = rxInterface.second;
456
457 // Populate ARP caches for given interfaceContainer
458 Ipv4InterfaceContainer interfaces;
459 interfaces.Add(ipv41, index1);
460 interfaces.Add(ipv42, index2);
461 neighborCache.PopulateNeighborCache(interfaces);
462 }
463 else if (!m_noGenerate)
464 {
465 // Populate ARP caches for all devices
466 neighborCache.PopulateNeighborCache();
467 }
468
469 if (m_sendTraffic)
470 {
471 // send Packet from n0 to n1
472 uint16_t port = 9; // Discard port (RFC 863)
473 OnOffHelper onoff("ns3::UdpSocketFactory",
474 Address(InetSocketAddress(csmaInterfacesLeft.GetAddress(1), port)));
475 onoff.SetConstantRate(DataRate("10Gbps"));
476 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
477 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
478 apps.Start(Seconds(1.0));
479
480 // Create a packet sink to receive these packets
481 PacketSinkHelper sink("ns3::UdpSocketFactory",
483 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
484 apps = sink.Install(csmaNodesLeft);
486 "RxWithSeqTsSize",
488 AsciiTraceHelper ascii;
489 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
490 csmaLeft.EnableAsciiAll(stream);
491 csmaLeft.EnablePcapAll("neighbor-cache-example");
492 }
493 else
494 {
495 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
497 }
498 }
499 else
500 {
501 Ipv6AddressHelper address;
502 address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
503 Ipv6InterfaceContainer csmaInterfacesLeft;
504 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
505 csmaInterfacesLeft.SetForwarding(1, true);
506 csmaInterfacesLeft.SetDefaultRouteInAllNodes(1);
507
508 address.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
509 Ipv6InterfaceContainer csmaInterfacesRight;
510 csmaInterfacesRight = address.Assign(csmaDevicesRight);
511 csmaInterfacesRight.SetForwarding(0, true);
512 csmaInterfacesRight.SetDefaultRouteInAllNodes(0);
513
514 // Populate neighbor NDISC caches
515 NeighborCacheHelper neighborCache;
516 if (m_useChannel)
517 {
518 // Populate NDISC caches for given channel
519 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
520 neighborCache.PopulateNeighborCache(csmaChannel);
521 }
523 {
524 // Populate NDISC caches for given netDeviceContainer
525 neighborCache.PopulateNeighborCache(csmaDevicesRight);
526 }
528 {
529 std::pair<Ptr<Ipv6>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
530 Ptr<Ipv6> ipv61 = txInterface.first;
531 uint32_t index1 = txInterface.second;
532 std::pair<Ptr<Ipv6>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
533 Ptr<Ipv6> ipv62 = rxInterface.first;
534 uint32_t index2 = rxInterface.second;
535
536 // Populate NDISC caches for given interfaceContainer
537 Ipv6InterfaceContainer interfaces;
538 interfaces.Add(ipv61, index1);
539 interfaces.Add(ipv62, index2);
540 neighborCache.PopulateNeighborCache(interfaces);
541 }
542 else if (!m_noGenerate)
543 {
544 // Populate NDISC caches for all devices
545 neighborCache.PopulateNeighborCache();
546 }
547
548 if (m_sendTraffic)
549 {
550 // send Packet from n0 to n1
551 uint16_t port = 9; // Discard port (RFC 863)
552 OnOffHelper onoff(
553 "ns3::UdpSocketFactory",
554 Address(Inet6SocketAddress(csmaInterfacesLeft.GetAddress(1, 1), port)));
555 onoff.SetConstantRate(DataRate("10Gbps"));
556 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
557 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
558 apps.Start(Seconds(1.0));
559
560 // Create a packet sink to receive these packets
561 PacketSinkHelper sink("ns3::UdpSocketFactory",
563 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
564 apps = sink.Install(csmaNodesLeft);
566 "RxWithSeqTsSize",
568 AsciiTraceHelper ascii;
569 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
570 csmaLeft.EnableAsciiAll(stream);
571 csmaLeft.EnablePcapAll("neighbor-cache-example");
572 }
573 else
574 {
575 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
577 }
578 }
579 Simulator::Stop(Seconds(1.00002));
582}
neighbor Cache example class.
void CommandSetup(int argc, char **argv)
description the command-line parameters.
bool m_sendTraffic
send data stream from n0 to n1
bool m_noGenerate
do not generate neighbor cache automatically
void ReceivePacket(Ptr< const Packet > pkt, const Address &from, const Address &dst, const SeqTsSizeHeader &header)
Print the information of receive data.
bool m_useNetDeviceContainer
Generate neighbor cache for specific netDeviceContainer.
bool m_useChannel
Generate neighbor cache for specific Channel.
void Run()
Run the example.
bool m_useIpv6
Use IPv6 instead of IPv4.
bool m_enableLog
Enable ArpL3Protocol and Icmpv6L4Protocol logging.
bool m_useInterfaceContainer
Generate neighbor cache for specific interfaceContainer.
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:56
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:226
Class for representing data rates.
Definition: data-rate.h:89
An Inet6 address class.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
static void PrintNeighborCacheAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the neighbor cache of all nodes at a particular time.
Helper class to auto-assign global IPv6 unicast addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
std::pair< Ptr< Ipv6 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv6> and interface stored at the location specified by the index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
static void PrintNeighborCacheAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the neighbor cache of all nodes at a particular time.
A helper class to populate neighbor cache.
void PopulateNeighborCache()
Populate neighbor ARP and NDISC caches for all devices.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:322
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Header with a sequence, a timestamp, and a "size" attribute.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1413
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:110
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55