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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Zhiheng Dong <dzh2077@gmail.com>
7 */
8
9/**
10 * @file
11 * Generating neighbor caches in various scopes
12 */
13
14/**
15 * @file
16 *
17 * This example shows how to generate neighbor caches in various scopes.
18 *
19 * This example shows how to generate neighbor caches on the scope of
20 * a channel, a NetDeviceContainer, an InterfaceContainer and globally.
21 * The example also shows a scenario that auto-generated neighbor caches
22 * can avoid dropping packets because of ARP message or Neighbor Discover
23 * message.
24 * If the user runs the example without any command line option, NeighborCacheHelper
25 * will generate neighbor cache for all devices and the output is their
26 * ARP table or NDISC table, the user can also generate neighbor cache on different
27 * scope by adding command line option --useChannel (on specific channel),
28 * --useNetDeviceContainer (on specific netDeviceContainer) and --useInterfaceContainer
29 * (on specific netDeviceContainer), --noGenerate (don't generate neighbor cache).
30 * The user can also enable --sendTraffic flag to send a data stream from n0 to n1,
31 * the output will be the information of received packets, which shows the packets loss
32 * are reduced after pre-generate neighbor caches.
33 *
34 * IPv4 Network Topology
35 * @verbatim
36 IPv4 Network Topology
37 LAN 10.1.1.0/24
38 n0 --------------- n1 n2 n3
39 | | |
40 ===============
41 LAN 10.1.2.0/24
42 \endverbatim
43 *
44 * @verbatim
45 IPv6 Network Topology
46 LAN 2001:1::/64
47 n0 --------------- n1 n2 n3
48 | | |
49 ===============
50 LAN 2001:2::/64
51 \endverbatim
52 *
53 * Expected Outputs:
54 * IPv4 (default):
55 * Generate ARP caches for all devices (default) :
56 * @verbatim
57 ARP Cache of node 0 at time 0
58 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
59 ARP Cache of node 1 at time 0
60 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
61 10.1.2.2 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
62 10.1.2.3 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
63 ARP Cache of node 2 at time 0
64 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
65 10.1.2.3 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
66 ARP Cache of node 3 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.2 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
69 \endverbatim
70 *
71 * Generate ARP caches for the left channel (--useChannel):
72 * @verbatim
73 ARP Cache of node 0 at time 0
74 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
75 ARP Cache of node 1 at time 0
76 10.1.1.1 dev 0 lladdr 02-06-00:00:00:00:00:01 STATIC_AUTOGENERATED
77 ARP Cache of node 2 at time 0
78 ARP Cache of node 3 at time 0
79 \endverbatim
80 *
81 * Generate ARP caches for devices on the right side (--useNetDeviceContainer):
82 * @verbatim
83 ARP Cache of node 0 at time 0
84 ARP Cache of node 1 at time 0
85 10.1.2.2 dev 1 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
86 10.1.2.3 dev 1 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
87 ARP Cache of node 2 at time 0
88 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
89 10.1.2.3 dev 0 lladdr 02-06-00:00:00:00:00:05 STATIC_AUTOGENERATED
90 ARP Cache of node 3 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.2 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
93 \endverbatim
94 *
95 * Generate ARP caches for specific interfaces (--useInterfaceContainer):
96 * @verbatim
97 ARP Cache of node 0 at time 0
98 10.1.1.2 dev 0 lladdr 02-06-00:00:00:00:00:02 STATIC_AUTOGENERATED
99 ARP Cache of node 1 at time 0
100 ARP Cache of node 2 at time 0
101 ARP Cache of node 3 at time 0
102 10.1.2.1 dev 0 lladdr 02-06-00:00:00:00:00:03 STATIC_AUTOGENERATED
103 10.1.2.2 dev 0 lladdr 02-06-00:00:00:00:00:04 STATIC_AUTOGENERATED
104 \endverbatim
105 *
106 * Do not generate neighbor cache (--noGenerate):
107 * @verbatim
108 ARP Cache of node 0 at time 0
109 ARP Cache of node 1 at time 0
110 ARP Cache of node 2 at time 0
111 ARP Cache of node 3 at time 0
112 \endverbatim
113 *
114 */
115// clang-format off
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 time=+1s)
120 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=1 time=+1s)
121 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=2 time=+1s)
122 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=3 time=+1s)
123 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=4 time=+1s)
124 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=5 time=+1s)
125 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=6 time=+1s)
126 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=7 time=+1s)
127 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=8 time=+1s)
128 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=9 time=+1s)
129 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=10 time=+1s)
130 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=11 time=+1s)
131 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 time=+1.00001s)
132 \endverbatim
133 *
134 * sending packet without pre-generated neighbor caches(--sendTraffic --noGenerate)
135 * \verbatim
136 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 time=+1s)
137 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=1 time=+1s)
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=2 time=+1s)
139 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=8 time=+1s)
140 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=9 time=+1s)
141 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=10 time=+1s)
142 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=11 time=+1s)
143 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 time=+1.00001s)
144 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=13 time=+1.00001s)
145 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=14 time=+1.00001s)
146 \endverbatim
147 *
148 */
149// clang-format on
150/**
151 * IPv6 (--useIPv6):
152 * Generate NDISC caches for all devices (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 */
231// clang-format off
232/**
233 * sending packet with pre-generated neighbor caches(--sendTraffic)
234 * \verbatim
235 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
236 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
237 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
238 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
239 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
240 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
241 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
242 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
243 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
244 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
245 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
246 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
247 \endverbatim
248 *
249 * sending packet without pre-generated neighbor caches(--sendTraffic --noGenerate)
250 * \verbatim
251 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
252 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
253 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
254 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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)
255 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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 time=+1.00001s)
256 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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 time=+1.00001s)
257 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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 time=+1.00001s)
258 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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 time=+1.00001s)
259 Rx pkt from 04-12-20:01:00:01:00:00:00:00:02:00:00:ff:fe:00:00:01:01:c0 to 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 time=+1.00001s)
260 \endverbatim
261*/
262// clang-format on
263
264#include "ns3/applications-module.h"
265#include "ns3/core-module.h"
266#include "ns3/csma-module.h"
267#include "ns3/internet-module.h"
268#include "ns3/network-module.h"
269
270using namespace ns3;
271
272NS_LOG_COMPONENT_DEFINE("NeighborCacheExample");
273
274/**
275 * neighbor Cache example class.
276 *
277 * It handles the creation and run of an example.
278 */
280{
281 public:
283
284 /**
285 * Run the example.
286 */
287 void Run();
288
289 /**
290 * @brief description the command-line parameters.
291 * @param argc The argument count.
292 * @param argv The argument vector.
293 */
294 void CommandSetup(int argc, char** argv);
295
296 private:
297 /**
298 * @brief Print the information of receive data.
299 * @param pkt The received packet.
300 * @param from The sender.
301 * @param dst The receiver.
302 * @param header The header with a sequence, a timestamp, and a "size" attribute
303 */
305 const Address& from,
306 const Address& dst,
307 const SeqTsSizeHeader& header);
308
309 bool m_useIpv6{false}; //!< Use IPv6 instead of IPv4
310 bool m_enableLog{false}; //!< Enable ArpL3Protocol and Icmpv6L4Protocol logging
311 bool m_useChannel{false}; //!< Generate neighbor cache for specific Channel
313 false}; //!< Generate neighbor cache for specific netDeviceContainer
315 false}; //!< Generate neighbor cache for specific interfaceContainer
316 bool m_noGenerate{false}; //!< do not generate neighbor cache automatically
317 bool m_sendTraffic{false}; //!< send data stream from n0 to n1
318};
319
324
325void
327 const Address& from,
328 const Address& dst,
329 const SeqTsSizeHeader& header)
330{
331 std::cout << "Rx pkt from " << from << " to " << dst << " -> " << header << std::endl;
332}
333
334void
336{
337 CommandLine cmd(__FILE__);
338 cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", m_useIpv6);
339 cmd.AddValue("enableLog", "Enable ArpL3Protocol and Icmpv6L4Protocol logging", m_enableLog);
340 cmd.AddValue("useChannel", "Generate neighbor cache for specific Channel", m_useChannel);
341 cmd.AddValue("useNetDeviceContainer",
342 "Generate neighbor cache for specific netDeviceContainer",
344 cmd.AddValue("useInterfaceContainer",
345 "Generate neighbor cache for specific interfaceContainer",
347 cmd.AddValue("noGenerate", "do not generate neighbor cache automatically", m_noGenerate);
348 cmd.AddValue("sendTraffic", "send data stream from n0 to n1", m_sendTraffic);
349
350 cmd.Parse(argc, argv);
351}
352
353int
354main(int argc, char* argv[])
355{
356 NeighborCacheExample example;
357 example.CommandSetup(argc, argv);
358 example.Run();
359 return 0;
360}
361
362void
364{
365 if (m_enableLog)
366 {
367 LogComponentEnable("ArpL3Protocol", LOG_LEVEL_LOGIC);
368 LogComponentEnable("Icmpv6L4Protocol", LOG_LEVEL_LOGIC);
369 }
370 uint32_t nCsmaLeft = 2;
371 uint32_t nCsmaRight = 2;
372
373 NodeContainer csmaNodesLeft;
374 csmaNodesLeft.Create(nCsmaLeft);
375 NodeContainer csmaNodesRight;
376 csmaNodesRight.Add(csmaNodesLeft.Get(1));
377 csmaNodesRight.Create(nCsmaRight);
378
379 CsmaHelper csmaLeft;
380 csmaLeft.SetChannelAttribute("DataRate", StringValue("20Gbps"));
381 // The 1 microSeconds delay is only for showing packets dropped effect without generating
382 // neighbor caches.
383 csmaLeft.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
384 CsmaHelper csmaRight;
385 csmaRight.SetChannelAttribute("DataRate", StringValue("20Gbps"));
386 csmaRight.SetChannelAttribute("Delay", TimeValue(MicroSeconds(1)));
387
388 NetDeviceContainer csmaDevicesLeft;
389 csmaDevicesLeft = csmaLeft.Install(csmaNodesLeft);
390 NetDeviceContainer csmaDevicesRight;
391 csmaDevicesRight = csmaRight.Install(csmaNodesRight);
392
394 if (!m_useIpv6)
395 {
396 stack.SetIpv6StackInstall(false);
397 }
398 else
399 {
400 stack.SetIpv4StackInstall(false);
401 }
402 // disabled Ipv4ArpJitter and Ipv6NsRsJitter to avoid the influence on packet dropped
403 stack.SetIpv4ArpJitter(false);
404 stack.SetIpv6NsRsJitter(false);
405 stack.Install(csmaNodesLeft.Get(0));
406 stack.Install(csmaNodesRight);
407
408 if (!m_useIpv6)
409 {
410 Ipv4AddressHelper address;
411 address.SetBase("10.1.1.0", "255.255.255.0");
412 Ipv4InterfaceContainer csmaInterfacesLeft;
413 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
414 address.SetBase("10.1.2.0", "255.255.255.0");
415 Ipv4InterfaceContainer csmaInterfacesRight;
416 csmaInterfacesRight = address.Assign(csmaDevicesRight);
417
418 // Populate ARP caches
419 NeighborCacheHelper neighborCache;
420 if (m_useChannel)
421 {
422 // Populate ARP caches for given channel
423 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
424 neighborCache.PopulateNeighborCache(csmaChannel);
425 }
427 {
428 // Populate ARP caches for given netDeviceContainer
429 neighborCache.PopulateNeighborCache(csmaDevicesRight);
430 }
432 {
433 std::pair<Ptr<Ipv4>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
434 Ptr<Ipv4> ipv41 = txInterface.first;
435 uint32_t index1 = txInterface.second;
436 std::pair<Ptr<Ipv4>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
437 Ptr<Ipv4> ipv42 = rxInterface.first;
438 uint32_t index2 = rxInterface.second;
439
440 // Populate ARP caches for given interfaceContainer
441 Ipv4InterfaceContainer interfaces;
442 interfaces.Add(ipv41, index1);
443 interfaces.Add(ipv42, index2);
444 neighborCache.PopulateNeighborCache(interfaces);
445 }
446 else if (!m_noGenerate)
447 {
448 // Populate ARP caches for all devices
449 neighborCache.PopulateNeighborCache();
450 }
451
452 if (m_sendTraffic)
453 {
454 // send Packet from n0 to n1
455 uint16_t port = 9; // Discard port (RFC 863)
456 OnOffHelper onoff("ns3::UdpSocketFactory",
457 Address(InetSocketAddress(csmaInterfacesLeft.GetAddress(1), port)));
458 onoff.SetConstantRate(DataRate("10Gbps"));
459 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
460 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
461 apps.Start(Seconds(1));
462
463 // Create a packet sink to receive these packets
464 PacketSinkHelper sink("ns3::UdpSocketFactory",
466 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
467 apps = sink.Install(csmaNodesLeft);
468 apps.Get(1)->TraceConnectWithoutContext(
469 "RxWithSeqTsSize",
471 AsciiTraceHelper ascii;
472 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
473 csmaLeft.EnableAsciiAll(stream);
474 csmaLeft.EnablePcapAll("neighbor-cache-example");
475 }
476 else
477 {
478 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
480 }
481 }
482 else
483 {
484 Ipv6AddressHelper address;
485 address.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
486 Ipv6InterfaceContainer csmaInterfacesLeft;
487 csmaInterfacesLeft = address.Assign(csmaDevicesLeft);
488 csmaInterfacesLeft.SetForwarding(1, true);
489 csmaInterfacesLeft.SetDefaultRouteInAllNodes(1);
490
491 address.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
492 Ipv6InterfaceContainer csmaInterfacesRight;
493 csmaInterfacesRight = address.Assign(csmaDevicesRight);
494 csmaInterfacesRight.SetForwarding(0, true);
495 csmaInterfacesRight.SetDefaultRouteInAllNodes(0);
496
497 // Populate neighbor NDISC caches
498 NeighborCacheHelper neighborCache;
499 if (m_useChannel)
500 {
501 // Populate NDISC caches for given channel
502 Ptr<Channel> csmaChannel = csmaDevicesLeft.Get(0)->GetChannel();
503 neighborCache.PopulateNeighborCache(csmaChannel);
504 }
506 {
507 // Populate NDISC caches for given netDeviceContainer
508 neighborCache.PopulateNeighborCache(csmaDevicesRight);
509 }
511 {
512 std::pair<Ptr<Ipv6>, uint32_t> txInterface = csmaInterfacesLeft.Get(0);
513 Ptr<Ipv6> ipv61 = txInterface.first;
514 uint32_t index1 = txInterface.second;
515 std::pair<Ptr<Ipv6>, uint32_t> rxInterface = csmaInterfacesRight.Get(nCsmaRight);
516 Ptr<Ipv6> ipv62 = rxInterface.first;
517 uint32_t index2 = rxInterface.second;
518
519 // Populate NDISC caches for given interfaceContainer
520 Ipv6InterfaceContainer interfaces;
521 interfaces.Add(ipv61, index1);
522 interfaces.Add(ipv62, index2);
523 neighborCache.PopulateNeighborCache(interfaces);
524 }
525 else if (!m_noGenerate)
526 {
527 // Populate NDISC caches for all devices
528 neighborCache.PopulateNeighborCache();
529 }
530
531 if (m_sendTraffic)
532 {
533 // send Packet from n0 to n1
534 uint16_t port = 9; // Discard port (RFC 863)
535 OnOffHelper onoff(
536 "ns3::UdpSocketFactory",
537 Address(Inet6SocketAddress(csmaInterfacesLeft.GetAddress(1, 1), port)));
538 onoff.SetConstantRate(DataRate("10Gbps"));
539 onoff.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
540 ApplicationContainer apps = onoff.Install(csmaNodesLeft.Get(0));
541 apps.Start(Seconds(1));
542
543 // Create a packet sink to receive these packets
544 PacketSinkHelper sink("ns3::UdpSocketFactory",
546 sink.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
547 apps = sink.Install(csmaNodesLeft);
548 apps.Get(1)->TraceConnectWithoutContext(
549 "RxWithSeqTsSize",
551 AsciiTraceHelper ascii;
552 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("neighbor-cache-example.tr");
553 csmaLeft.EnableAsciiAll(stream);
554 csmaLeft.EnablePcapAll("neighbor-cache-example");
555 }
556 else
557 {
558 Ptr<OutputStreamWrapper> outputStream = Create<OutputStreamWrapper>(&std::cout);
560 }
561 }
562 Simulator::Stop(Seconds(1.00002));
565}
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:90
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.
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:26
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Class for representing data rates.
Definition data-rate.h:78
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.
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.
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
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.
Header with a sequence, a timestamp, and a "size" attribute.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
AttributeValue implementation for Time.
Definition nstime.h:1431
uint16_t port
Definition dsdv-manet.cc:33
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1368
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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:291
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:684
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition log.h:99
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44