6754
|
1 |
.. include:: replace.txt
|
|
2 |
|
|
3 |
|
|
4 |
Tracing
|
|
5 |
-------
|
|
6 |
|
|
7 |
Background
|
|
8 |
**********
|
|
9 |
|
|
10 |
As mentioned in the Using the Tracing System section, the whole point of running
|
|
11 |
an |ns3| simulation is to generate output for study. You have two basic
|
|
12 |
strategies to work with in |ns3|: using generic pre-defined bulk output
|
|
13 |
mechanisms and parsing their content to extract interesting information; or
|
|
14 |
somehow developing an output mechanism that conveys exactly (and perhaps only)
|
|
15 |
the information wanted.
|
|
16 |
|
|
17 |
Using pre-defined bulk output mechanisms has the advantage of not requiring any
|
|
18 |
changes to |ns3|, but it does require programming. Often, pcap or NS_LOG
|
|
19 |
output messages are gathered during simulation runs and separately run through
|
|
20 |
scripts that use grep, sed or awk to parse the messages and reduce and transform
|
|
21 |
the data to a manageable form. Programs must be written to do the
|
|
22 |
transformation, so this does not come for free. Of course, if the information
|
|
23 |
of interest in does not exist in any of the pre-defined output mechanisms,
|
|
24 |
this approach fails.
|
|
25 |
|
|
26 |
If you need to add some tidbit of information to the pre-defined bulk mechanisms,
|
|
27 |
this can certainly be done; and if you use one of the |ns3| mechanisms,
|
|
28 |
you may get your code added as a contribution.
|
|
29 |
|
|
30 |
|ns3| provides another mechanism, called Tracing, that avoids some of the
|
|
31 |
problems inherent in the bulk output mechanisms. It has several important
|
|
32 |
advantages. First, you can reduce the amount of data you have to manage by only
|
|
33 |
tracing the events of interest to you (for large simulations, dumping everything
|
|
34 |
to disk for post-processing can create I/O bottlenecks). Second, if you use this
|
|
35 |
method, you can control the format of the output directly so you avoid the
|
|
36 |
postprocessing step with sed or awk script. If you desire, your output can be
|
|
37 |
formatted directly into a form acceptable by gnuplot, for example. You can add
|
|
38 |
hooks in the core which can then be accessed by other users, but which will
|
|
39 |
produce no information unless explicitly asked to do so. For these reasons, we
|
|
40 |
believe that the |ns3| tracing system is the best way to get information
|
|
41 |
out of a simulation and is also therefore one of the most important mechanisms
|
|
42 |
to understand in |ns3|.
|
|
43 |
|
|
44 |
Blunt Instruments
|
|
45 |
+++++++++++++++++
|
|
46 |
There are many ways to get information out of a program. The most
|
|
47 |
straightforward way is to just directly print the information to the standard
|
|
48 |
output, as in,
|
|
49 |
|
|
50 |
::
|
|
51 |
|
|
52 |
#include <iostream>
|
|
53 |
...
|
|
54 |
void
|
|
55 |
SomeFunction (void)
|
|
56 |
{
|
|
57 |
uint32_t x = SOME_INTERESTING_VALUE;
|
|
58 |
...
|
|
59 |
std::cout << "The value of x is " << x << std::endl;
|
|
60 |
...
|
|
61 |
}
|
|
62 |
|
|
63 |
Nobody is going to prevent you from going deep into the core of |ns3| and
|
|
64 |
adding print statements. This is insanely easy to do and, after all, you have
|
|
65 |
complete control of your own |ns3| branch. This will probably not turn
|
|
66 |
out to be very satisfactory in the long term, though.
|
|
67 |
|
|
68 |
As the number of print statements increases in your programs, the task of
|
|
69 |
dealing with the large number of outputs will become more and more complicated.
|
|
70 |
Eventually, you may feel the need to control what information is being printed
|
|
71 |
in some way; perhaps by turning on and off certain categories of prints, or
|
|
72 |
increasing or decreasing the amount of information you want. If you continue
|
|
73 |
down this path you may discover that you have re-implemented the ``NS_LOG``
|
|
74 |
mechanism. In order to avoid that, one of the first things you might consider
|
|
75 |
is using ``NS_LOG`` itself.
|
|
76 |
|
|
77 |
We mentioned above that one way to get information out of |ns3| is to
|
|
78 |
parse existing NS_LOG output for interesting information. If you discover that
|
|
79 |
some tidbit of information you need is not present in existing log output, you
|
|
80 |
could edit the core of |ns3| and simply add your interesting information
|
|
81 |
to the output stream. Now, this is certainly better than adding your own
|
|
82 |
print statements since it follows |ns3| coding conventions and could
|
|
83 |
potentially be useful to other people as a patch to the existing core.
|
|
84 |
|
|
85 |
Let's pick a random example. If you wanted to add more logging to the
|
|
86 |
|ns3| TCP socket (``tcp-socket-base.cc``) you could just add a new
|
|
87 |
message down in the implementation. Notice that in TcpSocketBase::ReceivedAck()
|
|
88 |
there is no log message for the no ack case. You could simply add one,
|
|
89 |
changing the code from:
|
|
90 |
|
|
91 |
::
|
|
92 |
|
|
93 |
/** Process the newly received ACK */
|
|
94 |
void
|
|
95 |
TcpSocketBase::ReceivedAck (Ptr<Packet> packet, const TcpHeader& tcpHeader)
|
|
96 |
{
|
|
97 |
NS_LOG_FUNCTION (this << tcpHeader);
|
|
98 |
|
|
99 |
// Received ACK. Compare the ACK number against highest unacked seqno
|
|
100 |
if (0 == (tcpHeader.GetFlags () & TcpHeader::ACK))
|
|
101 |
{ // Ignore if no ACK flag
|
|
102 |
}
|
|
103 |
...
|
|
104 |
|
|
105 |
to add a new ``NS_LOG_LOGIC`` in the appropriate statement:
|
|
106 |
|
|
107 |
::
|
|
108 |
|
|
109 |
/** Process the newly received ACK */
|
|
110 |
void
|
|
111 |
TcpSocketBase::ReceivedAck (Ptr<Packet> packet, const TcpHeader& tcpHeader)
|
|
112 |
{
|
|
113 |
NS_LOG_FUNCTION (this << tcpHeader);
|
|
114 |
|
|
115 |
// Received ACK. Compare the ACK number against highest unacked seqno
|
|
116 |
if (0 == (tcpHeader.GetFlags () & TcpHeader::ACK))
|
|
117 |
{ // Ignore if no ACK flag
|
|
118 |
NS_LOG_LOGIC ("TcpSocketBase " << this << " no ACK flag");
|
|
119 |
}
|
|
120 |
...
|
|
121 |
|
|
122 |
This may seem fairly simple and satisfying at first glance, but something to
|
|
123 |
consider is that you will be writing code to add the ``NS_LOG`` statement
|
|
124 |
and you will also have to write code (as in grep, sed or awk scripts) to parse
|
|
125 |
the log output in order to isolate your information. This is because even
|
|
126 |
though you have some control over what is output by the logging system, you
|
|
127 |
only have control down to the log component level.
|
|
128 |
|
|
129 |
If you are adding code to an existing module, you will also have to live with the
|
|
130 |
output that every other developer has found interesting. You may find that in
|
|
131 |
order to get the small amount of information you need, you may have to wade
|
|
132 |
through huge amounts of extraneous messages that are of no interest to you. You
|
|
133 |
may be forced to save huge log files to disk and process them down to a few lines
|
|
134 |
whenever you want to do anything.
|
|
135 |
|
|
136 |
Since there are no guarantees in |ns3| about the stability of ``NS_LOG``
|
|
137 |
output, you may also discover that pieces of log output on which you depend
|
|
138 |
disappear or change between releases. If you depend on the structure of the
|
|
139 |
output, you may find other messages being added or deleted which may affect your
|
|
140 |
parsing code.
|
|
141 |
|
|
142 |
For these reasons, we consider prints to ``std::cout`` and NS_LOG messages
|
|
143 |
to be quick and dirty ways to get more information out of |ns3|.
|
|
144 |
|
|
145 |
It is desirable to have a stable facility using stable APIs that allow one to
|
|
146 |
reach into the core system and only get the information required. It is
|
|
147 |
desirable to be able to do this without having to change and recompile the
|
|
148 |
core system. Even better would be a system that notified the user when an item
|
|
149 |
of interest changed or an interesting event happened so the user doesn't have
|
|
150 |
to actively poke around in the system looking for things.
|
|
151 |
|
|
152 |
The |ns3| tracing system is designed to work along those lines and is
|
|
153 |
well-integrated with the Attribute and Config subsystems allowing for relatively
|
|
154 |
simple use scenarios.
|
|
155 |
|
|
156 |
Overview
|
|
157 |
********
|
|
158 |
|
|
159 |
The ns-3 tracing system is built on the concepts of independent tracing sources
|
|
160 |
and tracing sinks; along with a uniform mechanism for connecting sources to sinks.
|
|
161 |
|
|
162 |
Trace sources are entities that can signal events that happen in a simulation and
|
|
163 |
provide access to interesting underlying data. For example, a trace source could
|
|
164 |
indicate when a packet is received by a net device and provide access to the
|
|
165 |
packet contents for interested trace sinks. A trace source might also indicate
|
|
166 |
when an interesting state change happens in a model. For example, the congestion
|
|
167 |
window of a TCP model is a prime candidate for a trace source.
|
|
168 |
|
|
169 |
Trace sources are not useful by themselves; they must be connected to other pieces
|
|
170 |
of code that actually do something useful with the information provided by the source.
|
|
171 |
The entities that consume trace information are called trace sinks. Trace sources
|
|
172 |
are generators of events and trace sinks are consumers. This explicit division
|
|
173 |
allows for large numbers of trace sources to be scattered around the system in
|
|
174 |
places which model authors believe might be useful.
|
|
175 |
|
|
176 |
There can be zero or more consumers of trace events generated by a trace source.
|
|
177 |
One can think of a trace source as a kind of point-to-multipoint information link.
|
|
178 |
Your code looking for trace events from a particular piece of core code could
|
|
179 |
happily coexist with other code doing something entirely different from the same
|
|
180 |
information.
|
|
181 |
|
|
182 |
Unless a user connects a trace sink to one of these sources, nothing is output. By
|
|
183 |
using the tracing system, both you and other people at the same trace source are
|
|
184 |
getting exactly what they want and only what they want out of the system. Neither
|
|
185 |
of you are impacting any other user by changing what information is output by the
|
|
186 |
system. If you happen to add a trace source, your work as a good open-source
|
|
187 |
citizen may allow other users to provide new utilities that are perhaps very useful
|
|
188 |
overall, without making any changes to the |ns3| core.
|
|
189 |
|
|
190 |
A Simple Low-Level Example
|
|
191 |
++++++++++++++++++++++++++
|
|
192 |
|
|
193 |
Let's take a few minutes and walk through a simple tracing example. We are going
|
|
194 |
to need a little background on Callbacks to understand what is happening in the
|
|
195 |
example, so we have to take a small detour right away.
|
|
196 |
|
|
197 |
Callbacks
|
|
198 |
~~~~~~~~~
|
|
199 |
|
|
200 |
The goal of the Callback system in |ns3| is to allow one piece of code to
|
|
201 |
call a function (or method in C++) without any specific inter-module dependency.
|
|
202 |
This ultimately means you need some kind of indirection -- you treat the address
|
|
203 |
of the called function as a variable. This variable is called a pointer-to-function
|
|
204 |
variable. The relationship between function and pointer-to-function pointer is
|
|
205 |
really no different that that of object and pointer-to-object.
|
|
206 |
|
|
207 |
In C the canonical example of a pointer-to-function is a
|
|
208 |
pointer-to-function-returning-integer (PFI). For a PFI taking one int parameter,
|
|
209 |
this could be declared like,
|
|
210 |
|
|
211 |
::
|
|
212 |
|
|
213 |
int (*pfi)(int arg) = 0;
|
|
214 |
|
|
215 |
What you get from this is a variable named simply "pfi" that is initialized
|
|
216 |
to the value 0. If you want to initialize this pointer to something meaningful,
|
|
217 |
you have to have a function with a matching signature. In this case, you could
|
|
218 |
provide a function that looks like,
|
|
219 |
|
|
220 |
::
|
|
221 |
|
|
222 |
int MyFunction (int arg) {}
|
|
223 |
|
|
224 |
If you have this target, you can initialize the variable to point to your
|
|
225 |
function:
|
|
226 |
|
|
227 |
::
|
|
228 |
|
|
229 |
pfi = MyFunction;
|
|
230 |
|
|
231 |
You can then call MyFunction indirectly using the more suggestive form of
|
|
232 |
the call,
|
|
233 |
|
|
234 |
::
|
|
235 |
|
|
236 |
int result = (*pfi) (1234);
|
|
237 |
|
|
238 |
This is suggestive since it looks like you are dereferencing the function
|
|
239 |
pointer just like you would dereference any pointer. Typically, however,
|
|
240 |
people take advantage of the fact that the compiler knows what is going on
|
|
241 |
and will just use a shorter form,
|
|
242 |
|
|
243 |
::
|
|
244 |
|
|
245 |
int result = pfi (1234);
|
|
246 |
|
|
247 |
This looks like you are calling a function named "pfi," but the compiler is
|
|
248 |
smart enough to know to call through the variable ``pfi`` indirectly to
|
|
249 |
the function ``MyFunction``.
|
|
250 |
|
|
251 |
Conceptually, this is almost exactly how the tracing system will work.
|
|
252 |
Basically, a trace source *is* a callback. When a trace sink expresses
|
|
253 |
interest in receiving trace events, it adds a Callback to a list of Callbacks
|
|
254 |
internally held by the trace source. When an interesting event happens, the
|
|
255 |
trace source invokes its ``operator()`` providing zero or more parameters.
|
|
256 |
The ``operator()`` eventually wanders down into the system and does something
|
|
257 |
remarkably like the indirect call you just saw. It provides zero or more
|
|
258 |
parameters (the call to "pfi" above passed one parameter to the target function
|
|
259 |
``MyFunction``.
|
|
260 |
|
|
261 |
The important difference that the tracing system adds is that for each trace
|
|
262 |
source there is an internal list of Callbacks. Instead of just making one
|
|
263 |
indirect call, a trace source may invoke any number of Callbacks. When a trace
|
|
264 |
sink expresses interest in notifications from a trace source, it basically just
|
|
265 |
arranges to add its own function to the callback list.
|
|
266 |
|
|
267 |
If you are interested in more details about how this is actually arranged in
|
|
268 |
|ns3|, feel free to peruse the Callback section of the manual.
|
|
269 |
|
|
270 |
Example Code
|
|
271 |
~~~~~~~~~~~~
|
|
272 |
|
|
273 |
We have provided some code to implement what is really the simplest example
|
|
274 |
of tracing that can be assembled. You can find this code in the tutorial
|
|
275 |
directory as ``fourth.cc``. Let's walk through it.
|
|
276 |
|
|
277 |
::
|
|
278 |
|
|
279 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
280 |
/*
|
|
281 |
* This program is free software; you can redistribute it and/or modify
|
|
282 |
* it under the terms of the GNU General Public License version 2 as
|
|
283 |
* published by the Free Software Foundation;
|
|
284 |
*
|
|
285 |
* This program is distributed in the hope that it will be useful,
|
|
286 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
287 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
288 |
* GNU General Public License for more details.
|
|
289 |
*
|
|
290 |
* You should have received a copy of the GNU General Public License
|
|
291 |
* along with this program; if not, write to the Free Software
|
|
292 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
293 |
*/
|
|
294 |
|
|
295 |
#include "ns3/object.h"
|
|
296 |
#include "ns3/uinteger.h"
|
|
297 |
#include "ns3/traced-value.h"
|
|
298 |
#include "ns3/trace-source-accessor.h"
|
|
299 |
|
|
300 |
#include <iostream>
|
|
301 |
|
|
302 |
using namespace ns3;
|
|
303 |
|
|
304 |
Most of this code should be quite familiar to you. As mentioned above, the
|
|
305 |
trace system makes heavy use of the Object and Attribute systems, so you will
|
|
306 |
need to include them. The first two includes above bring in the declarations
|
|
307 |
for those systems explicitly. You could use the core module header, but this
|
|
308 |
illustrates how simple this all really is.
|
|
309 |
|
|
310 |
The file, ``traced-value.h`` brings in the required declarations for tracing
|
|
311 |
of data that obeys value semantics. In general, value semantics just means that
|
|
312 |
you can pass the object around, not an address. In order to use value semantics
|
|
313 |
at all you have to have an object with an associated copy constructor and
|
|
314 |
assignment operator available. We extend the requirements to talk about the set
|
|
315 |
of operators that are pre-defined for plain-old-data (POD) types. Operator=,
|
|
316 |
operator++, operator---, operator+, operator==, etc.
|
|
317 |
|
|
318 |
What this all really means is that you will be able to trace changes to a C++
|
|
319 |
object made using those operators.
|
|
320 |
|
|
321 |
Since the tracing system is integrated with Attributes, and Attributes work
|
|
322 |
with Objects, there must be an |ns3| ``Object`` for the trace source
|
|
323 |
to live in. The next code snippet declares and defines a simple Object we can
|
|
324 |
work with.
|
|
325 |
|
|
326 |
::
|
|
327 |
|
|
328 |
class MyObject : public Object
|
|
329 |
{
|
|
330 |
public:
|
|
331 |
static TypeId GetTypeId (void)
|
|
332 |
{
|
|
333 |
static TypeId tid = TypeId ("MyObject")
|
|
334 |
.SetParent (Object::GetTypeId ())
|
|
335 |
.AddConstructor<MyObject> ()
|
|
336 |
.AddTraceSource ("MyInteger",
|
|
337 |
"An integer value to trace.",
|
|
338 |
MakeTraceSourceAccessor (&MyObject::m_myInt))
|
|
339 |
;
|
|
340 |
return tid;
|
|
341 |
}
|
|
342 |
|
|
343 |
MyObject () {}
|
|
344 |
TracedValue<int32_t> m_myInt;
|
|
345 |
};
|
|
346 |
|
|
347 |
The two important lines of code, above, with respect to tracing are the
|
|
348 |
``.AddTraceSource`` and the ``TracedValue`` declaration of ``m_myInt``.
|
|
349 |
|
|
350 |
The ``.AddTraceSource`` provides the "hooks" used for connecting the trace
|
|
351 |
source to the outside world through the config system. The ``TracedValue``
|
|
352 |
declaration provides the infrastructure that overloads the operators mentioned
|
|
353 |
above and drives the callback process.
|
|
354 |
|
|
355 |
::
|
|
356 |
|
|
357 |
void
|
|
358 |
IntTrace (int32_t oldValue, int32_t newValue)
|
|
359 |
{
|
|
360 |
std::cout << "Traced " << oldValue << " to " << newValue << std::endl;
|
|
361 |
}
|
|
362 |
|
|
363 |
This is the definition of the trace sink. It corresponds directly to a callback
|
|
364 |
function. Once it is connected, this function will be called whenever one of the
|
|
365 |
overloaded operators of the ``TracedValue`` is executed.
|
|
366 |
|
|
367 |
We have now seen the trace source and the trace sink. What remains is code to
|
|
368 |
connect the source to the sink.
|
|
369 |
|
|
370 |
::
|
|
371 |
|
|
372 |
int
|
|
373 |
main (int argc, char *argv[])
|
|
374 |
{
|
|
375 |
Ptr<MyObject> myObject = CreateObject<MyObject> ();
|
|
376 |
myObject->TraceConnectWithoutContext ("MyInteger", MakeCallback(&IntTrace));
|
|
377 |
|
|
378 |
myObject->m_myInt = 1234;
|
|
379 |
}
|
|
380 |
|
|
381 |
Here we first create the Object in which the trace source lives.
|
|
382 |
|
|
383 |
The next step, the ``TraceConnectWithoutContext``, forms the connection
|
|
384 |
between the trace source and the trace sink. Notice the ``MakeCallback``
|
|
385 |
template function. This function does the magic required to create the
|
|
386 |
underlying |ns3| Callback object and associate it with the function
|
|
387 |
``IntTrace``. TraceConnect makes the association between your provided
|
|
388 |
function and the overloaded ``operator()`` in the traced variable referred
|
|
389 |
to by the "MyInteger" Attribute. After this association is made, the trace
|
|
390 |
source will "fire" your provided callback function.
|
|
391 |
|
|
392 |
The code to make all of this happen is, of course, non-trivial, but the essence
|
|
393 |
is that you are arranging for something that looks just like the ``pfi()``
|
|
394 |
example above to be called by the trace source. The declaration of the
|
|
395 |
``TracedValue<int32_t> m_myInt;`` in the Object itself performs the magic
|
|
396 |
needed to provide the overloaded operators (++, ---, etc.) that will use the
|
|
397 |
``operator()`` to actually invoke the Callback with the desired parameters.
|
|
398 |
The ``.AddTraceSource`` performs the magic to connect the Callback to the
|
|
399 |
Config system, and ``TraceConnectWithoutContext`` performs the magic to
|
|
400 |
connect your function to the trace source, which is specified by Attribute
|
|
401 |
name.
|
|
402 |
|
|
403 |
Let's ignore the bit about context for now.
|
|
404 |
|
|
405 |
Finally, the line,
|
|
406 |
|
|
407 |
::
|
|
408 |
|
|
409 |
myObject->m_myInt = 1234;
|
|
410 |
|
|
411 |
should be interpreted as an invocation of ``operator=`` on the member
|
|
412 |
variable ``m_myInt`` with the integer ``1234`` passed as a parameter.
|
|
413 |
|
|
414 |
It turns out that this operator is defined (by ``TracedValue``) to execute
|
|
415 |
a callback that returns void and takes two integer values as parameters ---
|
|
416 |
an old value and a new value for the integer in question. That is exactly
|
|
417 |
the function signature for the callback function we provided --- ``IntTrace``.
|
|
418 |
|
|
419 |
To summarize, a trace source is, in essence, a variable that holds a list of
|
|
420 |
callbacks. A trace sink is a function used as the target of a callback. The
|
|
421 |
Attribute and object type information systems are used to provide a way to
|
|
422 |
connect trace sources to trace sinks. The act of "hitting" a trace source
|
|
423 |
is executing an operator on the trace source which fires callbacks. This
|
|
424 |
results in the trace sink callbacks registering interest in the source being
|
|
425 |
called with the parameters provided by the source.
|
|
426 |
|
|
427 |
If you now build and run this example,
|
|
428 |
|
|
429 |
::
|
|
430 |
|
|
431 |
./waf --run fourth
|
|
432 |
|
|
433 |
you will see the output from the ``IntTrace`` function execute as soon as the
|
|
434 |
trace source is hit:
|
|
435 |
|
|
436 |
::
|
|
437 |
|
|
438 |
Traced 0 to 1234
|
|
439 |
|
|
440 |
When we executed the code, ``myObject->m_myInt = 1234;``, the trace source
|
|
441 |
fired and automatically provided the before and after values to the trace sink.
|
|
442 |
The function ``IntTrace`` then printed this to the standard output. No
|
|
443 |
problem.
|
|
444 |
|
|
445 |
Using the Config Subsystem to Connect to Trace Sources
|
|
446 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
447 |
|
|
448 |
The ``TraceConnectWithoutContext`` call shown above in the simple example is
|
|
449 |
actually very rarely used in the system. More typically, the ``Config``
|
|
450 |
subsystem is used to allow selecting a trace source in the system using what is
|
|
451 |
called a *config path*. We saw an example of this in the previous section
|
|
452 |
where we hooked the "CourseChange" event when we were playing with
|
|
453 |
``third.cc``.
|
|
454 |
|
|
455 |
Recall that we defined a trace sink to print course change information from the
|
|
456 |
mobility models of our simulation. It should now be a lot more clear to you
|
|
457 |
what this function is doing.
|
|
458 |
|
|
459 |
::
|
|
460 |
|
|
461 |
void
|
|
462 |
CourseChange (std::string context, Ptr<const MobilityModel> model)
|
|
463 |
{
|
|
464 |
Vector position = model->GetPosition ();
|
|
465 |
NS_LOG_UNCOND (context <<
|
|
466 |
" x = " << position.x << ", y = " << position.y);
|
|
467 |
}
|
|
468 |
|
|
469 |
When we connected the "CourseChange" trace source to the above trace sink,
|
|
470 |
we used what is called a "Config Path" to specify the source when we
|
|
471 |
arranged a connection between the pre-defined trace source and the new trace
|
|
472 |
sink:
|
|
473 |
|
|
474 |
::
|
|
475 |
|
|
476 |
std::ostringstream oss;
|
|
477 |
oss <<
|
|
478 |
"/NodeList/" << wifiStaNodes.Get (nWifi - 1)->GetId () <<
|
|
479 |
"/$ns3::MobilityModel/CourseChange";
|
|
480 |
|
|
481 |
Config::Connect (oss.str (), MakeCallback (&CourseChange));
|
|
482 |
|
|
483 |
Let's try and make some sense of what is sometimes considered relatively
|
|
484 |
mysterious code. For the purposes of discussion, assume that the node
|
|
485 |
number returned by the ``GetId()`` is "7". In this case, the path
|
|
486 |
above turns out to be,
|
|
487 |
|
|
488 |
::
|
|
489 |
|
|
490 |
"/NodeList/7/$ns3::MobilityModel/CourseChange"
|
|
491 |
|
|
492 |
The last segment of a config path must be an ``Attribute`` of an
|
|
493 |
``Object``. In fact, if you had a pointer to the ``Object`` that has the
|
|
494 |
"CourseChange" ``Attribute`` handy, you could write this just like we did
|
|
495 |
in the previous example. You know by now that we typically store pointers to
|
|
496 |
our nodes in a NodeContainer. In the ``third.cc`` example, the Nodes of
|
|
497 |
interest are stored in the ``wifiStaNodes`` NodeContainer. In fact, while
|
|
498 |
putting the path together, we used this container to get a Ptr<Node> which we
|
|
499 |
used to call GetId() on. We could have used this Ptr<Node> directly to call
|
|
500 |
a connect method directly:
|
|
501 |
|
|
502 |
::
|
|
503 |
|
|
504 |
Ptr<Object> theObject = wifiStaNodes.Get (nWifi - 1);
|
|
505 |
theObject->TraceConnectWithoutContext ("CourseChange", MakeCallback (&CourseChange));
|
|
506 |
|
|
507 |
In the ``third.cc`` example, we actually want an additional "context" to
|
|
508 |
be delivered along with the Callback parameters (which will be explained below) so we
|
|
509 |
could actually use the following equivalent code,
|
|
510 |
|
|
511 |
::
|
|
512 |
|
|
513 |
Ptr<Object> theObject = wifiStaNodes.Get (nWifi - 1);
|
|
514 |
theObject->TraceConnect ("CourseChange", MakeCallback (&CourseChange));
|
|
515 |
|
|
516 |
It turns out that the internal code for ``Config::ConnectWithoutContext`` and
|
|
517 |
``Config::Connect`` actually do find a Ptr<Object> and call the appropriate
|
|
518 |
TraceConnect method at the lowest level.
|
|
519 |
|
|
520 |
The ``Config`` functions take a path that represents a chain of ``Object``
|
|
521 |
pointers. Each segment of a path corresponds to an Object Attribute. The last
|
|
522 |
segment is the Attribute of interest, and prior segments must be typed to contain
|
|
523 |
or find Objects. The ``Config`` code parses and "walks" this path until it
|
|
524 |
gets to the final segment of the path. It then interprets the last segment as
|
|
525 |
an ``Attribute`` on the last Object it found while walking the path. The
|
|
526 |
``Config`` functions then call the appropriate ``TraceConnect`` or
|
|
527 |
``TraceConnectWithoutContext`` method on the final Object. Let's see what
|
|
528 |
happens in a bit more detail when the above path is walked.
|
|
529 |
|
|
530 |
The leading "/" character in the path refers to a so-called namespace. One
|
|
531 |
of the predefined namespaces in the config system is "NodeList" which is a
|
|
532 |
list of all of the nodes in the simulation. Items in the list are referred to
|
|
533 |
by indices into the list, so "/NodeList/7" refers to the eighth node in the
|
|
534 |
list of nodes created during the simulation. This reference is actually a
|
|
535 |
``Ptr<Node>`` and so is a subclass of an ``ns3::Object``.
|
|
536 |
|
|
537 |
As described in the Object Model section of the |ns3| manual, we support
|
|
538 |
Object Aggregation. This allows us to form an association between different
|
|
539 |
Objects without any programming. Each Object in an Aggregation can be reached
|
|
540 |
from the other Objects.
|
|
541 |
|
|
542 |
The next path segment being walked begins with the "$" character. This
|
|
543 |
indicates to the config system that a ``GetObject`` call should be made
|
|
544 |
looking for the type that follows. It turns out that the MobilityHelper used in
|
|
545 |
``third.cc`` arranges to Aggregate, or associate, a mobility model to each of
|
|
546 |
the wireless Nodes. When you add the "$" you are asking for another Object that
|
|
547 |
has presumably been previously aggregated. You can think of this as switching
|
|
548 |
pointers from the original Ptr<Node> as specified by "/NodeList/7" to its
|
|
549 |
associated mobility model --- which is of type "$ns3::MobilityModel". If you
|
|
550 |
are familiar with ``GetObject``, we have asked the system to do the following:
|
|
551 |
|
|
552 |
::
|
|
553 |
|
|
554 |
Ptr<MobilityModel> mobilityModel = node->GetObject<MobilityModel> ()
|
|
555 |
|
|
556 |
We are now at the last Object in the path, so we turn our attention to the
|
|
557 |
Attributes of that Object. The ``MobilityModel`` class defines an Attribute
|
|
558 |
called "CourseChange". You can see this by looking at the source code in
|
|
559 |
``src/mobility/mobility-model.cc`` and searching for "CourseChange" in your
|
|
560 |
favorite editor. You should find,
|
|
561 |
|
|
562 |
::
|
|
563 |
|
|
564 |
.AddTraceSource ("CourseChange",
|
|
565 |
"The value of the position and/or velocity vector changed",
|
|
566 |
MakeTraceSourceAccessor (&MobilityModel::m_courseChangeTrace))
|
|
567 |
|
|
568 |
which should look very familiar at this point.
|
|
569 |
|
|
570 |
If you look for the corresponding declaration of the underlying traced variable
|
|
571 |
in ``mobility-model.h`` you will find
|
|
572 |
|
|
573 |
::
|
|
574 |
|
|
575 |
TracedCallback<Ptr<const MobilityModel> > m_courseChangeTrace;
|
|
576 |
|
|
577 |
The type declaration ``TracedCallback`` identifies ``m_courseChangeTrace``
|
|
578 |
as a special list of Callbacks that can be hooked using the Config functions
|
|
579 |
described above.
|
|
580 |
|
|
581 |
The ``MobilityModel`` class is designed to be a base class providing a common
|
|
582 |
interface for all of the specific subclasses. If you search down to the end of
|
|
583 |
the file, you will see a method defined called ``NotifyCourseChange()``:
|
|
584 |
|
|
585 |
::
|
|
586 |
|
|
587 |
void
|
|
588 |
MobilityModel::NotifyCourseChange (void) const
|
|
589 |
{
|
|
590 |
m_courseChangeTrace(this);
|
|
591 |
}
|
|
592 |
|
|
593 |
Derived classes will call into this method whenever they do a course change to
|
|
594 |
support tracing. This method invokes ``operator()`` on the underlying
|
|
595 |
``m_courseChangeTrace``, which will, in turn, invoke all of the registered
|
|
596 |
Callbacks, calling all of the trace sinks that have registered interest in the
|
|
597 |
trace source by calling a Config function.
|
|
598 |
|
|
599 |
So, in the ``third.cc`` example we looked at, whenever a course change is
|
|
600 |
made in one of the ``RandomWalk2dMobilityModel`` instances installed, there
|
|
601 |
will be a ``NotifyCourseChange()`` call which calls up into the
|
|
602 |
``MobilityModel`` base class. As seen above, this invokes ``operator()``
|
|
603 |
on ``m_courseChangeTrace``, which in turn, calls any registered trace sinks.
|
|
604 |
In the example, the only code registering an interest was the code that provided
|
|
605 |
the config path. Therefore, the ``CourseChange`` function that was hooked
|
|
606 |
from Node number seven will be the only Callback called.
|
|
607 |
|
|
608 |
The final piece of the puzzle is the "context". Recall that we saw an output
|
|
609 |
looking something like the following from ``third.cc``:
|
|
610 |
|
|
611 |
::
|
|
612 |
|
|
613 |
/NodeList/7/$ns3::MobilityModel/CourseChange x = 7.27897, y = 2.22677
|
|
614 |
|
|
615 |
The first part of the output is the context. It is simply the path through
|
|
616 |
which the config code located the trace source. In the case we have been looking at
|
|
617 |
there can be any number of trace sources in the system corresponding to any number
|
|
618 |
of nodes with mobility models. There needs to be some way to identify which trace
|
|
619 |
source is actually the one that fired the Callback. An easy way is to request a
|
|
620 |
trace context when you ``Config::Connect``.
|
|
621 |
|
|
622 |
How to Find and Connect Trace Sources, and Discover Callback Signatures
|
|
623 |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
624 |
|
|
625 |
The first question that inevitably comes up for new users of the Tracing system is,
|
|
626 |
"okay, I know that there must be trace sources in the simulation core, but how do
|
|
627 |
I find out what trace sources are available to me"?
|
|
628 |
|
|
629 |
The second question is, "okay, I found a trace source, how do I figure out the
|
|
630 |
config path to use when I connect to it"?
|
|
631 |
|
|
632 |
The third question is, "okay, I found a trace source, how do I figure out what
|
|
633 |
the return type and formal arguments of my callback function need to be"?
|
|
634 |
|
|
635 |
The fourth question is, "okay, I typed that all in and got this incredibly bizarre
|
|
636 |
error message, what in the world does it mean"?
|
|
637 |
|
|
638 |
What Trace Sources are Available?
|
|
639 |
+++++++++++++++++++++++++++++++++
|
|
640 |
|
|
641 |
The answer to this question is found in the |ns3| Doxygen. Go to the
|
|
642 |
|ns3| web site `"here"
|
|
643 |
<http://www.nsnam.org/getting_started.html>`_
|
|
644 |
and select the "Doxygen (stable)" link "Documentation" on the navigation
|
|
645 |
bar to the left side of the page. Expand the "Modules" book in the NS-3
|
|
646 |
documentation tree a the upper left by clicking the "+" box. Now, expand
|
|
647 |
the "Core" book in the tree by clicking its "+" box. You should now
|
|
648 |
see three extremely useful links:
|
|
649 |
|
|
650 |
* The list of all trace sources
|
|
651 |
* The list of all attributes
|
|
652 |
* The list of all global values
|
|
653 |
|
|
654 |
The list of interest to us here is "the list of all trace sources". Go
|
|
655 |
ahead and select that link. You will see, perhaps not too surprisingly, a
|
|
656 |
list of all of the trace sources available in the |ns3| core.
|
|
657 |
|
|
658 |
As an example, scroll down to ``ns3::MobilityModel``. You will find
|
|
659 |
an entry for
|
|
660 |
|
|
661 |
::
|
|
662 |
|
|
663 |
CourseChange: The value of the position and/or velocity vector changed
|
|
664 |
|
|
665 |
You should recognize this as the trace source we used in the ``third.cc``
|
|
666 |
example. Perusing this list will be helpful.
|
|
667 |
|
|
668 |
What String do I use to Connect?
|
|
669 |
++++++++++++++++++++++++++++++++
|
|
670 |
|
|
671 |
The easiest way to do this is to grep around in the |ns3| codebase for someone
|
|
672 |
who has already figured it out, You should always try to copy someone else's
|
|
673 |
working code before you start to write your own. Try something like:
|
|
674 |
|
|
675 |
::
|
|
676 |
|
|
677 |
find . -name '*.cc' | xargs grep CourseChange | grep Connect
|
|
678 |
|
|
679 |
and you may find your answer along with working code. For example, in this
|
|
680 |
case, ``./ns-3-dev/examples/wireless/mixed-wireless.cc`` has something
|
|
681 |
just waiting for you to use:
|
|
682 |
|
|
683 |
::
|
|
684 |
|
|
685 |
Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
|
|
686 |
MakeCallback (&CourseChangeCallback));
|
|
687 |
|
|
688 |
If you cannot find any examples in the distribution, you can find this out
|
|
689 |
from the |ns3| Doxygen. It will probably be simplest just to walk
|
|
690 |
through the "CourseChanged" example.
|
|
691 |
|
|
692 |
Let's assume that you have just found the "CourseChanged" trace source in
|
|
693 |
"The list of all trace sources" and you want to figure out how to connect to
|
|
694 |
it. You know that you are using (again, from the ``third.cc`` example) an
|
|
695 |
``ns3::RandomWalk2dMobilityModel``. So open the "Class List" book in
|
|
696 |
the NS-3 documentation tree by clicking its "+" box. You will now see a
|
|
697 |
list of all of the classes in |ns3|. Scroll down until you see the
|
|
698 |
entry for ``ns3::RandomWalk2dMobilityModel`` and follow that link.
|
|
699 |
You should now be looking at the "ns3::RandomWalk2dMobilityModel Class
|
|
700 |
Reference".
|
|
701 |
|
|
702 |
If you now scroll down to the "Member Function Documentation" section, you
|
|
703 |
will see documentation for the ``GetTypeId`` function. You constructed one
|
|
704 |
of these in the simple tracing example above:
|
|
705 |
|
|
706 |
::
|
|
707 |
|
|
708 |
static TypeId GetTypeId (void)
|
|
709 |
{
|
|
710 |
static TypeId tid = TypeId ("MyObject")
|
|
711 |
.SetParent (Object::GetTypeId ())
|
|
712 |
.AddConstructor<MyObject> ()
|
|
713 |
.AddTraceSource ("MyInteger",
|
|
714 |
"An integer value to trace.",
|
|
715 |
MakeTraceSourceAccessor (&MyObject::m_myInt))
|
|
716 |
;
|
|
717 |
return tid;
|
|
718 |
}
|
|
719 |
|
|
720 |
As mentioned above, this is the bit of code that connected the Config
|
|
721 |
and Attribute systems to the underlying trace source. This is also the
|
|
722 |
place where you should start looking for information about the way to
|
|
723 |
connect.
|
|
724 |
|
|
725 |
You are looking at the same information for the RandomWalk2dMobilityModel; and
|
|
726 |
the information you want is now right there in front of you in the Doxygen:
|
|
727 |
|
|
728 |
::
|
|
729 |
|
|
730 |
This object is accessible through the following paths with Config::Set and Config::Connect:
|
|
731 |
|
|
732 |
/NodeList/[i]/$ns3::MobilityModel/$ns3::RandomWalk2dMobilityModel
|
|
733 |
|
|
734 |
The documentation tells you how to get to the ``RandomWalk2dMobilityModel``
|
|
735 |
Object. Compare the string above with the string we actually used in the
|
|
736 |
example code:
|
|
737 |
|
|
738 |
::
|
|
739 |
|
|
740 |
"/NodeList/7/$ns3::MobilityModel"
|
|
741 |
|
|
742 |
The difference is due to the fact that two ``GetObject`` calls are implied
|
|
743 |
in the string found in the documentation. The first, for ``$ns3::MobilityModel``
|
|
744 |
will query the aggregation for the base class. The second implied
|
|
745 |
``GetObject`` call, for ``$ns3::RandomWalk2dMobilityModel``, is used to "cast"
|
|
746 |
the base class to the concrete implementation class. The documentation shows
|
|
747 |
both of these operations for you. It turns out that the actual Attribute you are
|
|
748 |
going to be looking for is found in the base class as we have seen.
|
|
749 |
|
|
750 |
Look further down in the ``GetTypeId`` doxygen. You will find,
|
|
751 |
|
|
752 |
::
|
|
753 |
|
|
754 |
No TraceSources defined for this type.
|
|
755 |
TraceSources defined in parent class ns3::MobilityModel:
|
|
756 |
|
|
757 |
CourseChange: The value of the position and/or velocity vector changed
|
|
758 |
Reimplemented from ns3::MobilityModel
|
|
759 |
|
|
760 |
This is exactly what you need to know. The trace source of interest is found in
|
|
761 |
``ns3::MobilityModel`` (which you knew anyway). The interesting thing this
|
|
762 |
bit of Doxygen tells you is that you don't need that extra cast in the config
|
|
763 |
path above to get to the concrete class, since the trace source is actually in
|
|
764 |
the base class. Therefore the additional ``GetObject`` is not required and
|
|
765 |
you simply use the path:
|
|
766 |
|
|
767 |
::
|
|
768 |
|
|
769 |
/NodeList/[i]/$ns3::MobilityModel
|
|
770 |
|
|
771 |
which perfectly matches the example path:
|
|
772 |
|
|
773 |
::
|
|
774 |
|
|
775 |
/NodeList/7/$ns3::MobilityModel
|
|
776 |
|
|
777 |
What Return Value and Formal Arguments?
|
|
778 |
+++++++++++++++++++++++++++++++++++++++
|
|
779 |
|
|
780 |
The easiest way to do this is to grep around in the |ns3| codebase for someone
|
|
781 |
who has already figured it out, You should always try to copy someone else's
|
|
782 |
working code. Try something like:
|
|
783 |
|
|
784 |
::
|
|
785 |
|
|
786 |
find . -name '*.cc' | xargs grep CourseChange | grep Connect
|
|
787 |
|
|
788 |
and you may find your answer along with working code. For example, in this
|
|
789 |
case, ``./ns-3-dev/examples/wireless/mixed-wireless.cc`` has something
|
|
790 |
just waiting for you to use. You will find
|
|
791 |
|
|
792 |
::
|
|
793 |
|
|
794 |
Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
|
|
795 |
MakeCallback (&CourseChangeCallback));
|
|
796 |
|
|
797 |
as a result of your grep. The ``MakeCallback`` should indicate to you that
|
|
798 |
there is a callback function there which you can use. Sure enough, there is:
|
|
799 |
|
|
800 |
::
|
|
801 |
|
|
802 |
static void
|
|
803 |
CourseChangeCallback (std::string path, Ptr<const MobilityModel> model)
|
|
804 |
{
|
|
805 |
...
|
|
806 |
}
|
|
807 |
|
|
808 |
Take my Word for It
|
|
809 |
~~~~~~~~~~~~~~~~~~~
|
|
810 |
|
|
811 |
If there are no examples to work from, this can be, well, challenging to
|
|
812 |
actually figure out from the source code.
|
|
813 |
|
|
814 |
Before embarking on a walkthrough of the code, I'll be kind and just tell you
|
|
815 |
a simple way to figure this out: The return value of your callback will always
|
|
816 |
be void. The formal parameter list for a ``TracedCallback`` can be found
|
|
817 |
from the template parameter list in the declaration. Recall that for our
|
|
818 |
current example, this is in ``mobility-model.h``, where we have previously
|
|
819 |
found:
|
|
820 |
|
|
821 |
::
|
|
822 |
|
|
823 |
TracedCallback<Ptr<const MobilityModel> > m_courseChangeTrace;
|
|
824 |
|
|
825 |
There is a one-to-one correspondence between the template parameter list in
|
|
826 |
the declaration and the formal arguments of the callback function. Here,
|
|
827 |
there is one template parameter, which is a ``Ptr<const MobilityModel>``.
|
|
828 |
This tells you that you need a function that returns void and takes a
|
|
829 |
a ``Ptr<const MobilityModel>``. For example,
|
|
830 |
|
|
831 |
::
|
|
832 |
|
|
833 |
void
|
|
834 |
CourseChangeCallback (Ptr<const MobilityModel> model)
|
|
835 |
{
|
|
836 |
...
|
|
837 |
}
|
|
838 |
|
|
839 |
That's all you need if you want to ``Config::ConnectWithoutContext``. If
|
|
840 |
you want a context, you need to ``Config::Connect`` and use a Callback
|
|
841 |
function that takes a string context, then the required argument.
|
|
842 |
|
|
843 |
::
|
|
844 |
|
|
845 |
void
|
|
846 |
CourseChangeCallback (std::string path, Ptr<const MobilityModel> model)
|
|
847 |
{
|
|
848 |
...
|
|
849 |
}
|
|
850 |
|
|
851 |
If you want to ensure that your ``CourseChangeCallback`` is only visible
|
|
852 |
in your local file, you can add the keyword ``static`` and come up with:
|
|
853 |
|
|
854 |
::
|
|
855 |
|
|
856 |
static void
|
|
857 |
CourseChangeCallback (std::string path, Ptr<const MobilityModel> model)
|
|
858 |
{
|
|
859 |
...
|
|
860 |
}
|
|
861 |
|
|
862 |
which is exactly what we used in the ``third.cc`` example.
|
|
863 |
|
|
864 |
The Hard Way
|
|
865 |
~~~~~~~~~~~~
|
|
866 |
|
|
867 |
This section is entirely optional. It is going to be a bumpy ride, especially
|
|
868 |
for those unfamiliar with the details of templates. However, if you get through
|
|
869 |
this, you will have a very good handle on a lot of the |ns3| low level
|
|
870 |
idioms.
|
|
871 |
|
|
872 |
So, again, let's figure out what signature of callback function is required for
|
|
873 |
the "CourseChange" Attribute. This is going to be painful, but you only need
|
|
874 |
to do this once. After you get through this, you will be able to just look at
|
|
875 |
a ``TracedCallback`` and understand it.
|
|
876 |
|
|
877 |
The first thing we need to look at is the declaration of the trace source.
|
|
878 |
Recall that this is in ``mobility-model.h``, where we have previously
|
|
879 |
found:
|
|
880 |
|
|
881 |
::
|
|
882 |
|
|
883 |
TracedCallback<Ptr<const MobilityModel> > m_courseChangeTrace;
|
|
884 |
|
|
885 |
This declaration is for a template. The template parameter is inside the
|
|
886 |
angle-brackets, so we are really interested in finding out what that
|
|
887 |
``TracedCallback<>`` is. If you have absolutely no idea where this might
|
|
888 |
be found, grep is your friend.
|
|
889 |
|
|
890 |
We are probably going to be interested in some kind of declaration in the
|
|
891 |
|ns3| source, so first change into the ``src`` directory. Then,
|
|
892 |
we know this declaration is going to have to be in some kind of header file,
|
|
893 |
so just grep for it using:
|
|
894 |
|
|
895 |
::
|
|
896 |
|
|
897 |
find . -name '*.h' | xargs grep TracedCallback
|
|
898 |
|
|
899 |
You'll see 124 lines fly by (I piped this through wc to see how bad it was).
|
|
900 |
Although that may seem like it, that's not really a lot. Just pipe the output
|
|
901 |
through more and start scanning through it. On the first page, you will see
|
|
902 |
some very suspiciously template-looking stuff.
|
|
903 |
|
|
904 |
::
|
|
905 |
|
|
906 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::TracedCallback ()
|
|
907 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::ConnectWithoutContext (c ...
|
|
908 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Connect (const CallbackB ...
|
|
909 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::DisconnectWithoutContext ...
|
|
910 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Disconnect (const Callba ...
|
|
911 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (void) const ...
|
|
912 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1) const ...
|
|
913 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2 ...
|
|
914 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2 ...
|
|
915 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2 ...
|
|
916 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2 ...
|
|
917 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2 ...
|
|
918 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2 ...
|
|
919 |
|
|
920 |
It turns out that all of this comes from the header file
|
|
921 |
``traced-callback.h`` which sounds very promising. You can then take a
|
|
922 |
look at ``mobility-model.h`` and see that there is a line which confirms
|
|
923 |
this hunch:
|
|
924 |
|
|
925 |
::
|
|
926 |
|
|
927 |
#include "ns3/traced-callback.h"
|
|
928 |
|
|
929 |
Of course, you could have gone at this from the other direction and started
|
|
930 |
by looking at the includes in ``mobility-model.h`` and noticing the
|
|
931 |
include of ``traced-callback.h`` and inferring that this must be the file
|
|
932 |
you want.
|
|
933 |
|
|
934 |
In either case, the next step is to take a look at ``src/core/traced-callback.h``
|
|
935 |
in your favorite editor to see what is happening.
|
|
936 |
|
|
937 |
You will see a comment at the top of the file that should be comforting:
|
|
938 |
|
|
939 |
::
|
|
940 |
|
|
941 |
An ns3::TracedCallback has almost exactly the same API as a normal ns3::Callback but
|
|
942 |
instead of forwarding calls to a single function (as an ns3::Callback normally does),
|
|
943 |
it forwards calls to a chain of ns3::Callback.
|
|
944 |
|
|
945 |
This should sound very familiar and let you know you are on the right track.
|
|
946 |
|
|
947 |
Just after this comment, you will find,
|
|
948 |
|
|
949 |
::
|
|
950 |
|
|
951 |
template<typename T1 = empty, typename T2 = empty,
|
|
952 |
typename T3 = empty, typename T4 = empty,
|
|
953 |
typename T5 = empty, typename T6 = empty,
|
|
954 |
typename T7 = empty, typename T8 = empty>
|
|
955 |
class TracedCallback
|
|
956 |
{
|
|
957 |
...
|
|
958 |
|
|
959 |
This tells you that TracedCallback is a templated class. It has eight possible
|
|
960 |
type parameters with default values. Go back and compare this with the
|
|
961 |
declaration you are trying to understand:
|
|
962 |
|
|
963 |
::
|
|
964 |
|
|
965 |
TracedCallback<Ptr<const MobilityModel> > m_courseChangeTrace;
|
|
966 |
|
|
967 |
The ``typename T1`` in the templated class declaration corresponds to the
|
|
968 |
``Ptr<const MobilityModel>`` in the declaration above. All of the other
|
|
969 |
type parameters are left as defaults. Looking at the constructor really
|
|
970 |
doesn't tell you much. The one place where you have seen a connection made
|
|
971 |
between your Callback function and the tracing system is in the ``Connect``
|
|
972 |
and ``ConnectWithoutContext`` functions. If you scroll down, you will see
|
|
973 |
a ``ConnectWithoutContext`` method here:
|
|
974 |
|
|
975 |
::
|
|
976 |
|
|
977 |
template<typename T1, typename T2,
|
|
978 |
typename T3, typename T4,
|
|
979 |
typename T5, typename T6,
|
|
980 |
typename T7, typename T8>
|
|
981 |
void
|
|
982 |
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::ConnectWithoutContext ...
|
|
983 |
{
|
|
984 |
Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> cb;
|
|
985 |
cb.Assign (callback);
|
|
986 |
m_callbackList.push_back (cb);
|
|
987 |
}
|
|
988 |
|
|
989 |
You are now in the belly of the beast. When the template is instantiated for
|
|
990 |
the declaration above, the compiler will replace ``T1`` with
|
|
991 |
``Ptr<const MobilityModel>``.
|
|
992 |
|
|
993 |
::
|
|
994 |
|
|
995 |
void
|
|
996 |
TracedCallback<Ptr<const MobilityModel>::ConnectWithoutContext ... cb
|
|
997 |
{
|
|
998 |
Callback<void, Ptr<const MobilityModel> > cb;
|
|
999 |
cb.Assign (callback);
|
|
1000 |
m_callbackList.push_back (cb);
|
|
1001 |
}
|
|
1002 |
|
|
1003 |
You can now see the implementation of everything we've been talking about. The
|
|
1004 |
code creates a Callback of the right type and assigns your function to it. This
|
|
1005 |
is the equivalent of the ``pfi = MyFunction`` we discussed at the start of
|
|
1006 |
this section. The code then adds the Callback to the list of Callbacks for
|
|
1007 |
this source. The only thing left is to look at the definition of Callback.
|
|
1008 |
Using the same grep trick as we used to find ``TracedCallback``, you will be
|
|
1009 |
able to find that the file ``./core/callback.h`` is the one we need to look at.
|
|
1010 |
|
|
1011 |
If you look down through the file, you will see a lot of probably almost
|
|
1012 |
incomprehensible template code. You will eventually come to some Doxygen for
|
|
1013 |
the Callback template class, though. Fortunately, there is some English:
|
|
1014 |
|
|
1015 |
::
|
|
1016 |
|
|
1017 |
This class template implements the Functor Design Pattern.
|
|
1018 |
It is used to declare the type of a Callback:
|
|
1019 |
- the first non-optional template argument represents
|
|
1020 |
the return type of the callback.
|
|
1021 |
- the second optional template argument represents
|
|
1022 |
the type of the first argument to the callback.
|
|
1023 |
- the third optional template argument represents
|
|
1024 |
the type of the second argument to the callback.
|
|
1025 |
- the fourth optional template argument represents
|
|
1026 |
the type of the third argument to the callback.
|
|
1027 |
- the fifth optional template argument represents
|
|
1028 |
the type of the fourth argument to the callback.
|
|
1029 |
- the sixth optional template argument represents
|
|
1030 |
the type of the fifth argument to the callback.
|
|
1031 |
|
|
1032 |
We are trying to figure out what the
|
|
1033 |
|
|
1034 |
::
|
|
1035 |
|
|
1036 |
Callback<void, Ptr<const MobilityModel> > cb;
|
|
1037 |
|
|
1038 |
declaration means. Now we are in a position to understand that the first
|
|
1039 |
(non-optional) parameter, ``void``, represents the return type of the
|
|
1040 |
Callback. The second (non-optional) parameter, ``Ptr<const MobilityModel>``
|
|
1041 |
represents the first argument to the callback.
|
|
1042 |
|
|
1043 |
The Callback in question is your function to receive the trace events. From
|
|
1044 |
this you can infer that you need a function that returns ``void`` and takes
|
|
1045 |
a ``Ptr<const MobilityModel>``. For example,
|
|
1046 |
|
|
1047 |
::
|
|
1048 |
|
|
1049 |
void
|
|
1050 |
CourseChangeCallback (Ptr<const MobilityModel> model)
|
|
1051 |
{
|
|
1052 |
...
|
|
1053 |
}
|
|
1054 |
|
|
1055 |
That's all you need if you want to ``Config::ConnectWithoutContext``. If
|
|
1056 |
you want a context, you need to ``Config::Connect`` and use a Callback
|
|
1057 |
function that takes a string context. This is because the ``Connect``
|
|
1058 |
function will provide the context for you. You'll need:
|
|
1059 |
|
|
1060 |
::
|
|
1061 |
|
|
1062 |
void
|
|
1063 |
CourseChangeCallback (std::string path, Ptr<const MobilityModel> model)
|
|
1064 |
{
|
|
1065 |
...
|
|
1066 |
}
|
|
1067 |
|
|
1068 |
If you want to ensure that your ``CourseChangeCallback`` is only visible
|
|
1069 |
in your local file, you can add the keyword ``static`` and come up with:
|
|
1070 |
|
|
1071 |
::
|
|
1072 |
|
|
1073 |
static void
|
|
1074 |
CourseChangeCallback (std::string path, Ptr<const MobilityModel> model)
|
|
1075 |
{
|
|
1076 |
...
|
|
1077 |
}
|
|
1078 |
|
|
1079 |
which is exactly what we used in the ``third.cc`` example. Perhaps you
|
|
1080 |
should now go back and reread the previous section (Take My Word for It).
|
|
1081 |
|
|
1082 |
If you are interested in more details regarding the implementation of
|
|
1083 |
Callbacks, feel free to take a look at the |ns3| manual. They are one
|
|
1084 |
of the most frequently used constructs in the low-level parts of |ns3|.
|
|
1085 |
It is, in my opinion, a quite elegant thing.
|
|
1086 |
|
|
1087 |
What About TracedValue?
|
|
1088 |
+++++++++++++++++++++++
|
|
1089 |
|
|
1090 |
Earlier in this section, we presented a simple piece of code that used a
|
|
1091 |
``TracedValue<int32_t>`` to demonstrate the basics of the tracing code.
|
|
1092 |
We just glossed over the way to find the return type and formal arguments
|
|
1093 |
for the ``TracedValue``. Rather than go through the whole exercise, we
|
|
1094 |
will just point you at the correct file, ``src/core/traced-value.h`` and
|
|
1095 |
to the important piece of code:
|
|
1096 |
|
|
1097 |
::
|
|
1098 |
|
|
1099 |
template <typename T>
|
|
1100 |
class TracedValue
|
|
1101 |
{
|
|
1102 |
public:
|
|
1103 |
...
|
|
1104 |
void Set (const T &v) {
|
|
1105 |
if (m_v != v)
|
|
1106 |
{
|
|
1107 |
m_cb (m_v, v);
|
|
1108 |
m_v = v;
|
|
1109 |
}
|
|
1110 |
}
|
|
1111 |
...
|
|
1112 |
private:
|
|
1113 |
T m_v;
|
|
1114 |
TracedCallback<T,T> m_cb;
|
|
1115 |
};
|
|
1116 |
|
|
1117 |
Here you see that the ``TracedValue`` is templated, of course. In the simple
|
|
1118 |
example case at the start of the section, the typename is int32_t. This means
|
|
1119 |
that the member variable being traced (``m_v`` in the private section of the
|
|
1120 |
class) will be an ``int32_t m_v``. The ``Set`` method will take a
|
|
1121 |
``const int32_t &v`` as a parameter. You should now be able to understand
|
|
1122 |
that the ``Set`` code will fire the ``m_cb`` callback with two parameters:
|
|
1123 |
the first being the current value of the ``TracedValue``; and the second
|
|
1124 |
being the new value being set.
|
|
1125 |
|
|
1126 |
The callback, ``m_cb`` is declared as a ``TracedCallback<T, T>`` which
|
|
1127 |
will correspond to a ``TracedCallback<int32_t, int32_t>`` when the class is
|
|
1128 |
instantiated.
|
|
1129 |
|
|
1130 |
Recall that the callback target of a TracedCallback always returns ``void``.
|
|
1131 |
Further recall that there is a one-to-one correspondence between the template
|
|
1132 |
parameter list in the declaration and the formal arguments of the callback
|
|
1133 |
function. Therefore the callback will need to have a function signature that
|
|
1134 |
looks like:
|
|
1135 |
|
|
1136 |
::
|
|
1137 |
|
|
1138 |
void
|
|
1139 |
MyCallback (int32_t oldValue, int32_t newValue)
|
|
1140 |
{
|
|
1141 |
...
|
|
1142 |
}
|
|
1143 |
|
|
1144 |
It probably won't surprise you that this is exactly what we provided in that
|
|
1145 |
simple example we covered so long ago:
|
|
1146 |
|
|
1147 |
::
|
|
1148 |
|
|
1149 |
void
|
|
1150 |
IntTrace (int32_t oldValue, int32_t newValue)
|
|
1151 |
{
|
|
1152 |
std::cout << "Traced " << oldValue << " to " << newValue << std::endl;
|
|
1153 |
}
|
|
1154 |
|
|
1155 |
A Real Example
|
|
1156 |
**************
|
|
1157 |
|
|
1158 |
Let's do an example taken from one of the best-known books on TCP around.
|
|
1159 |
"TCP/IP Illustrated, Volume 1: The Protocols," by W. Richard Stevens is a
|
|
1160 |
classic. I just flipped the book open and ran across a nice plot of both the
|
|
1161 |
congestion window and sequence numbers versus time on page 366. Stevens calls
|
|
1162 |
this, "Figure 21.10. Value of cwnd and send sequence number while data is being
|
|
1163 |
transmitted." Let's just recreate the cwnd part of that plot in |ns3|
|
|
1164 |
using the tracing system and ``gnuplot``.
|
|
1165 |
|
|
1166 |
Are There Trace Sources Available?
|
|
1167 |
++++++++++++++++++++++++++++++++++
|
|
1168 |
|
|
1169 |
The first thing to think about is how we want to get the data out. What is it
|
|
1170 |
that we need to trace? The first thing to do is to consult "The list of all
|
|
1171 |
trace sources" to see what we have to work with. Recall that this is found
|
|
1172 |
in the |ns3| Doxygen in the "Core" Module section. If you scroll
|
|
1173 |
through the list, you will eventually find:
|
|
1174 |
|
|
1175 |
::
|
|
1176 |
|
|
1177 |
ns3::TcpNewReno
|
|
1178 |
CongestionWindow: The TCP connection's congestion window
|
|
1179 |
|
|
1180 |
It turns out that the |ns3| TCP implementation lives (mostly) in the
|
|
1181 |
file ``src/internet-stack/tcp-socket-base.cc`` while congestion control
|
|
1182 |
variants are in files such as ``src/internet-stack/tcp-newreno.cc``.
|
|
1183 |
If you don't know this a priori, you can use the recursive grep trick:
|
|
1184 |
|
|
1185 |
::
|
|
1186 |
|
|
1187 |
find . -name '*.cc' | xargs grep -i tcp
|
|
1188 |
|
|
1189 |
You will find page after page of instances of tcp pointing you to that file.
|
|
1190 |
|
|
1191 |
If you open ``src/internet-stack/tcp-newreno.cc`` in your favorite
|
|
1192 |
editor, you will see right up at the top of the file, the following declarations:
|
|
1193 |
|
|
1194 |
::
|
|
1195 |
|
|
1196 |
TypeId
|
|
1197 |
TcpNewReno::GetTypeId ()
|
|
1198 |
{
|
|
1199 |
static TypeId tid = TypeId("ns3::TcpNewReno")
|
|
1200 |
.SetParent<TcpSocketBase> ()
|
|
1201 |
.AddConstructor<TcpNewReno> ()
|
|
1202 |
.AddTraceSource ("CongestionWindow",
|
|
1203 |
"The TCP connection's congestion window",
|
|
1204 |
MakeTraceSourceAccessor (&TcpNewReno::m_cWnd))
|
|
1205 |
;
|
|
1206 |
return tid;
|
|
1207 |
}
|
|
1208 |
|
|
1209 |
This should tell you to look for the declaration of ``m_cWnd`` in the header
|
|
1210 |
file ``src/internet-stack/tcp-newreno.h``. If you open this file in your
|
|
1211 |
favorite editor, you will find:
|
|
1212 |
|
|
1213 |
::
|
|
1214 |
|
|
1215 |
TracedValue<uint32_t> m_cWnd; //Congestion window
|
|
1216 |
|
|
1217 |
You should now understand this code completely. If we have a pointer to the
|
|
1218 |
``TcpNewReno``, we can ``TraceConnect`` to the "CongestionWindow" trace
|
|
1219 |
source if we provide an appropriate callback target. This is the same kind of
|
|
1220 |
trace source that we saw in the simple example at the start of this section,
|
|
1221 |
except that we are talking about ``uint32_t`` instead of ``int32_t``.
|
|
1222 |
|
|
1223 |
We now know that we need to provide a callback that returns void and takes
|
|
1224 |
two ``uint32_t`` parameters, the first being the old value and the second
|
|
1225 |
being the new value:
|
|
1226 |
|
|
1227 |
::
|
|
1228 |
|
|
1229 |
void
|
|
1230 |
CwndTrace (uint32_t oldValue, uint32_t newValue)
|
|
1231 |
{
|
|
1232 |
...
|
|
1233 |
}
|
|
1234 |
|
|
1235 |
What Script to Use?
|
|
1236 |
+++++++++++++++++++
|
|
1237 |
|
|
1238 |
It's always best to try and find working code laying around that you can
|
|
1239 |
modify, rather than starting from scratch. So the first order of business now
|
|
1240 |
is to find some code that already hooks the "CongestionWindow" trace source
|
|
1241 |
and see if we can modify it. As usual, grep is your friend:
|
|
1242 |
|
|
1243 |
::
|
|
1244 |
|
|
1245 |
find . -name '*.cc' | xargs grep CongestionWindow
|
|
1246 |
|
|
1247 |
This will point out a couple of promising candidates:
|
|
1248 |
``examples/tcp/tcp-large-transfer.cc`` and
|
|
1249 |
``src/test/ns3tcp/ns3tcp-cwnd-test-suite.cc``.
|
|
1250 |
|
|
1251 |
We haven't visited any of the test code yet, so let's take a look there. You
|
|
1252 |
will typically find that test code is fairly minimal, so this is probably a
|
|
1253 |
very good bet. Open ``src/test/ns3tcp/ns3tcp-cwnd-test-suite.cc`` in your
|
|
1254 |
favorite editor and search for "CongestionWindow". You will find,
|
|
1255 |
|
|
1256 |
::
|
|
1257 |
|
|
1258 |
ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow",
|
|
1259 |
MakeCallback (&Ns3TcpCwndTestCase1::CwndChange, this));
|
|
1260 |
|
|
1261 |
This should look very familiar to you. We mentioned above that if we had a
|
|
1262 |
pointer to the ``TcpNewReno``, we could ``TraceConnect`` to the
|
|
1263 |
"CongestionWindow" trace source. That's exactly what we have here; so it
|
|
1264 |
turns out that this line of code does exactly what we want. Let's go ahead
|
|
1265 |
and extract the code we need from this function
|
|
1266 |
(``Ns3TcpCwndTestCase1::DoRun (void)``). If you look at this function,
|
|
1267 |
you will find that it looks just like an |ns3| script. It turns out that
|
|
1268 |
is exactly what it is. It is a script run by the test framework, so we can just
|
|
1269 |
pull it out and wrap it in ``main`` instead of in ``DoRun``. Rather than
|
|
1270 |
walk through this, step, by step, we have provided the file that results from
|
|
1271 |
porting this test back to a native |ns3| script --
|
|
1272 |
``examples/tutorial/fifth.cc``.
|
|
1273 |
|
|
1274 |
A Common Problem and Solution
|
|
1275 |
+++++++++++++++++++++++++++++
|
|
1276 |
|
|
1277 |
The ``fifth.cc`` example demonstrates an extremely important rule that you
|
|
1278 |
must understand before using any kind of ``Attribute``: you must ensure
|
|
1279 |
that the target of a ``Config`` command exists before trying to use it.
|
|
1280 |
This is no different than saying an object must be instantiated before trying
|
|
1281 |
to call it. Although this may seem obvious when stated this way, it does
|
|
1282 |
trip up many people trying to use the system for the first time.
|
|
1283 |
|
|
1284 |
Let's return to basics for a moment. There are three basic time periods that
|
|
1285 |
exist in any |ns3| script. The first time period is sometimes called
|
|
1286 |
"Configuration Time" or "Setup Time," and is in force during the period
|
|
1287 |
when the ``main`` function of your script is running, but before
|
|
1288 |
``Simulator::Run`` is called. The second time period is sometimes called
|
|
1289 |
"Simulation Time" and is in force during the time period when
|
|
1290 |
``Simulator::Run`` is actively executing its events. After it completes
|
|
1291 |
executing the simulation, ``Simulator::Run`` will return control back to
|
|
1292 |
the ``main`` function. When this happens, the script enters what can be
|
|
1293 |
called "Teardown Time," which is when the structures and objects created
|
|
1294 |
during setup and taken apart and released.
|
|
1295 |
|
|
1296 |
Perhaps the most common mistake made in trying to use the tracing system is
|
|
1297 |
assuming that entities constructed dynamically during simulation time are
|
|
1298 |
available during configuration time. In particular, an |ns3|
|
|
1299 |
``Socket`` is a dynamic object often created by ``Applications`` to
|
|
1300 |
communicate between ``Nodes``. An |ns3| ``Application``
|
|
1301 |
always has a "Start Time" and a "Stop Time" associated with it. In the
|
|
1302 |
vast majority of cases, an ``Application`` will not attempt to create
|
|
1303 |
a dynamic object until its ``StartApplication`` method is called at some
|
|
1304 |
"Start Time". This is to ensure that the simulation is completely
|
|
1305 |
configured before the app tries to do anything (what would happen if it tried
|
|
1306 |
to connect to a node that didn't exist yet during configuration time). The
|
|
1307 |
answer to this issue is to 1) create a simulator event that is run after the
|
|
1308 |
dynamic object is created and hook the trace when that event is executed; or
|
|
1309 |
2) create the dynamic object at configuration time, hook it then, and give
|
|
1310 |
the object to the system to use during simulation time. We took the second
|
|
1311 |
approach in the ``fifth.cc`` example. This decision required us to create
|
|
1312 |
the ``MyApp`` ``Application``, the entire purpose of which is to take
|
|
1313 |
a ``Socket`` as a parameter.
|
|
1314 |
|
|
1315 |
A fifth.cc Walkthrough
|
|
1316 |
++++++++++++++++++++++
|
|
1317 |
|
|
1318 |
Now, let's take a look at the example program we constructed by dissecting
|
|
1319 |
the congestion window test. Open ``examples/tutorial/fifth.cc`` in your
|
|
1320 |
favorite editor. You should see some familiar looking code:
|
|
1321 |
|
|
1322 |
::
|
|
1323 |
|
|
1324 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
1325 |
/*
|
|
1326 |
* This program is free software; you can redistribute it and/or modify
|
|
1327 |
* it under the terms of the GNU General Public License version 2 as
|
|
1328 |
* published by the Free Software Foundation;
|
|
1329 |
*
|
|
1330 |
* This program is distributed in the hope that it will be useful,
|
|
1331 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
1332 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
1333 |
* GNU General Public License for more details.
|
|
1334 |
*
|
|
1335 |
* You should have received a copy of the GNU General Public License
|
|
1336 |
* along with this program; if not, write to the Free Software
|
|
1337 |
* Foundation, Include., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
1338 |
*/
|
|
1339 |
|
|
1340 |
#include <fstream>
|
|
1341 |
#include "ns3/core-module.h"
|
|
1342 |
#include "ns3/common-module.h"
|
|
1343 |
#include "ns3/simulator-module.h"
|
|
1344 |
#include "ns3/node-module.h"
|
|
1345 |
#include "ns3/helper-module.h"
|
|
1346 |
|
|
1347 |
using namespace ns3;
|
|
1348 |
|
|
1349 |
NS_LOG_COMPONENT_DEFINE ("FifthScriptExample");
|
|
1350 |
|
|
1351 |
This has all been covered, so we won't rehash it. The next lines of source are
|
|
1352 |
the network illustration and a comment addressing the problem described above
|
|
1353 |
with ``Socket``.
|
|
1354 |
|
|
1355 |
::
|
|
1356 |
|
|
1357 |
// ===========================================================================
|
|
1358 |
//
|
|
1359 |
// node 0 node 1
|
|
1360 |
// +----------------+ +----------------+
|
|
1361 |
// | ns-3 TCP | | ns-3 TCP |
|
|
1362 |
// +----------------+ +----------------+
|
|
1363 |
// | 10.1.1.1 | | 10.1.1.2 |
|
|
1364 |
// +----------------+ +----------------+
|
|
1365 |
// | point-to-point | | point-to-point |
|
|
1366 |
// +----------------+ +----------------+
|
|
1367 |
// | |
|
|
1368 |
// +---------------------+
|
|
1369 |
// 5 Mbps, 2 ms
|
|
1370 |
//
|
|
1371 |
//
|
|
1372 |
// We want to look at changes in the ns-3 TCP congestion window. We need
|
|
1373 |
// to crank up a flow and hook the CongestionWindow attribute on the socket
|
|
1374 |
// of the sender. Normally one would use an on-off application to generate a
|
|
1375 |
// flow, but this has a couple of problems. First, the socket of the on-off
|
|
1376 |
// application is not created until Application Start time, so we wouldn't be
|
|
1377 |
// able to hook the socket (now) at configuration time. Second, even if we
|
|
1378 |
// could arrange a call after start time, the socket is not public so we
|
|
1379 |
// couldn't get at it.
|
|
1380 |
//
|
|
1381 |
// So, we can cook up a simple version of the on-off application that does what
|
|
1382 |
// we want. On the plus side we don't need all of the complexity of the on-off
|
|
1383 |
// application. On the minus side, we don't have a helper, so we have to get
|
|
1384 |
// a little more involved in the details, but this is trivial.
|
|
1385 |
//
|
|
1386 |
// So first, we create a socket and do the trace connect on it; then we pass
|
|
1387 |
// this socket into the constructor of our simple application which we then
|
|
1388 |
// install in the source node.
|
|
1389 |
// ===========================================================================
|
|
1390 |
//
|
|
1391 |
|
|
1392 |
This should also be self-explanatory.
|
|
1393 |
|
|
1394 |
The next part is the declaration of the ``MyApp`` ``Application`` that
|
|
1395 |
we put together to allow the ``Socket`` to be created at configuration time.
|
|
1396 |
|
|
1397 |
::
|
|
1398 |
|
|
1399 |
class MyApp : public Application
|
|
1400 |
{
|
|
1401 |
public:
|
|
1402 |
|
|
1403 |
MyApp ();
|
|
1404 |
virtual ~MyApp();
|
|
1405 |
|
|
1406 |
void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize,
|
|
1407 |
uint32_t nPackets, DataRate dataRate);
|
|
1408 |
|
|
1409 |
private:
|
|
1410 |
virtual void StartApplication (void);
|
|
1411 |
virtual void StopApplication (void);
|
|
1412 |
|
|
1413 |
void ScheduleTx (void);
|
|
1414 |
void SendPacket (void);
|
|
1415 |
|
|
1416 |
Ptr<Socket> m_socket;
|
|
1417 |
Address m_peer;
|
|
1418 |
uint32_t m_packetSize;
|
|
1419 |
uint32_t m_nPackets;
|
|
1420 |
DataRate m_dataRate;
|
|
1421 |
EventId m_sendEvent;
|
|
1422 |
bool m_running;
|
|
1423 |
uint32_t m_packetsSent;
|
|
1424 |
};
|
|
1425 |
|
|
1426 |
You can see that this class inherits from the |ns3| ``Application``
|
|
1427 |
class. Take a look at ``src/node/application.h`` if you are interested in
|
|
1428 |
what is inherited. The ``MyApp`` class is obligated to override the
|
|
1429 |
``StartApplication`` and ``StopApplication`` methods. These methods are
|
|
1430 |
automatically called when ``MyApp`` is required to start and stop sending
|
|
1431 |
data during the simulation.
|
|
1432 |
|
|
1433 |
How Applications are Started and Stopped (optional)
|
|
1434 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
1435 |
|
|
1436 |
It is worthwhile to spend a bit of time explaining how events actually get
|
|
1437 |
started in the system. This is another fairly deep explanation, and can be
|
|
1438 |
ignored if you aren't planning on venturing down into the guts of the system.
|
|
1439 |
It is useful, however, in that the discussion touches on how some very important
|
|
1440 |
parts of |ns3| work and exposes some important idioms. If you are
|
|
1441 |
planning on implementing new models, you probably want to understand this
|
|
1442 |
section.
|
|
1443 |
|
|
1444 |
The most common way to start pumping events is to start an ``Application``.
|
|
1445 |
This is done as the result of the following (hopefully) familar lines of an
|
|
1446 |
|ns3| script:
|
|
1447 |
|
|
1448 |
::
|
|
1449 |
|
|
1450 |
ApplicationContainer apps = ...
|
|
1451 |
apps.Start (Seconds (1.0));
|
|
1452 |
apps.Stop (Seconds (10.0));
|
|
1453 |
|
|
1454 |
The application container code (see ``src/helper/application-container.h`` if
|
|
1455 |
you are interested) loops through its contained applications and calls,
|
|
1456 |
|
|
1457 |
::
|
|
1458 |
|
|
1459 |
app->SetStartTime (startTime);
|
|
1460 |
|
|
1461 |
as a result of the ``apps.Start`` call and
|
|
1462 |
|
|
1463 |
::
|
|
1464 |
|
|
1465 |
app->SetStopTime (stopTime);
|
|
1466 |
|
|
1467 |
as a result of the ``apps.Stop`` call.
|
|
1468 |
|
|
1469 |
The ultimate result of these calls is that we want to have the simulator
|
|
1470 |
automatically make calls into our ``Applications`` to tell them when to
|
|
1471 |
start and stop. In the case of ``MyApp``, it inherits from class
|
|
1472 |
``Application`` and overrides ``StartApplication``, and
|
|
1473 |
``StopApplication``. These are the functions that will be called by
|
|
1474 |
the simulator at the appropriate time. In the case of ``MyApp`` you
|
|
1475 |
will find that ``MyApp::StartApplication`` does the initial ``Bind``,
|
|
1476 |
and ``Connect`` on the socket, and then starts data flowing by calling
|
|
1477 |
``MyApp::SendPacket``. ``MyApp::StopApplication`` stops generating
|
|
1478 |
packets by cancelling any pending send events and closing the socket.
|
|
1479 |
|
|
1480 |
One of the nice things about |ns3| is that you can completely
|
|
1481 |
ignore the implementation details of how your ``Application`` is
|
|
1482 |
"automagically" called by the simulator at the correct time. But since
|
|
1483 |
we have already ventured deep into |ns3| already, let's go for it.
|
|
1484 |
|
|
1485 |
If you look at ``src/node/application.cc`` you will find that the
|
|
1486 |
``SetStartTime`` method of an ``Application`` just sets the member
|
|
1487 |
variable ``m_startTime`` and the ``SetStopTime`` method just sets
|
|
1488 |
``m_stopTime``. From there, without some hints, the trail will probably
|
|
1489 |
end.
|
|
1490 |
|
|
1491 |
The key to picking up the trail again is to know that there is a global
|
|
1492 |
list of all of the nodes in the system. Whenever you create a node in
|
|
1493 |
a simulation, a pointer to that node is added to the global ``NodeList``.
|
|
1494 |
|
|
1495 |
Take a look at ``src/node/node-list.cc`` and search for
|
|
1496 |
``NodeList::Add``. The public static implementation calls into a private
|
|
1497 |
implementation called ``NodeListPriv::Add``. This is a relatively common
|
|
1498 |
idom in |ns3|. So, take a look at ``NodeListPriv::Add``. There
|
|
1499 |
you will find,
|
|
1500 |
|
|
1501 |
::
|
|
1502 |
|
|
1503 |
Simulator::ScheduleWithContext (index, TimeStep (0), &Node::Start, node);
|
|
1504 |
|
|
1505 |
This tells you that whenever a ``Node`` is created in a simulation, as
|
|
1506 |
a side-effect, a call to that node's ``Start`` method is scheduled for
|
|
1507 |
you that happens at time zero. Don't read too much into that name, yet.
|
|
1508 |
It doesn't mean that the node is going to start doing anything, it can be
|
|
1509 |
interpreted as an informational call into the ``Node`` telling it that
|
|
1510 |
the simulation has started, not a call for action telling the ``Node``
|
|
1511 |
to start doing something.
|
|
1512 |
|
|
1513 |
So, ``NodeList::Add`` indirectly schedules a call to ``Node::Start``
|
|
1514 |
at time zero to advise a new node that the simulation has started. If you
|
|
1515 |
look in ``src/node/node.h`` you will, however, not find a method called
|
|
1516 |
``Node::Start``. It turns out that the ``Start`` method is inherited
|
|
1517 |
from class ``Object``. All objects in the system can be notified when
|
|
1518 |
the simulation starts, and objects of class ``Node`` are just one kind
|
|
1519 |
of those objects.
|
|
1520 |
|
|
1521 |
Take a look at ``src/core/object.cc`` next and search for ``Object::Start``.
|
|
1522 |
This code is not as straightforward as you might have expected since
|
|
1523 |
|ns3| ``Objects`` support aggregation. The code in
|
|
1524 |
``Object::Start`` then loops through all of the objects that have been
|
|
1525 |
aggregated together and calls their ``DoStart`` method. This is another
|
|
1526 |
idiom that is very common in |ns3|. There is a public API method,
|
|
1527 |
that stays constant across implementations, that calls a private implementation
|
|
1528 |
method that is inherited and implemented by subclasses. The names are typically
|
|
1529 |
something like ``MethodName`` for the public API and ``DoMethodName`` for
|
|
1530 |
the private API.
|
|
1531 |
|
|
1532 |
This tells us that we should look for a ``Node::DoStart`` method in
|
|
1533 |
``src/node/node.cc`` for the method that will continue our trail. If you
|
|
1534 |
locate the code, you will find a method that loops through all of the devices
|
|
1535 |
in the node and then all of the applications in the node calling
|
|
1536 |
``device->Start`` and ``application->Start`` respectively.
|
|
1537 |
|
|
1538 |
You may already know that classes ``Device`` and ``Application`` both
|
|
1539 |
inherit from class ``Object`` and so the next step will be to look at
|
|
1540 |
what happens when ``Application::DoStart`` is called. Take a look at
|
|
1541 |
``src/node/application.cc`` and you will find:
|
|
1542 |
|
|
1543 |
::
|
|
1544 |
|
|
1545 |
void
|
|
1546 |
Application::DoStart (void)
|
|
1547 |
{
|
|
1548 |
m_startEvent = Simulator::Schedule (m_startTime, &Application::StartApplication, this);
|
|
1549 |
if (m_stopTime != TimeStep (0))
|
|
1550 |
{
|
|
1551 |
m_stopEvent = Simulator::Schedule (m_stopTime, &Application::StopApplication, this);
|
|
1552 |
}
|
|
1553 |
Object::DoStart ();
|
|
1554 |
}
|
|
1555 |
|
|
1556 |
Here, we finally come to the end of the trail. If you have kept it all straight,
|
|
1557 |
when you implement an |ns3| ``Application``, your new application
|
|
1558 |
inherits from class ``Application``. You override the ``StartApplication``
|
|
1559 |
and ``StopApplication`` methods and provide mechanisms for starting and
|
|
1560 |
stopping the flow of data out of your new ``Application``. When a ``Node``
|
|
1561 |
is created in the simulation, it is added to a global ``NodeList``. The act
|
|
1562 |
of adding a node to this ``NodeList`` causes a simulator event to be scheduled
|
|
1563 |
for time zero which calls the ``Node::Start`` method of the newly added
|
|
1564 |
``Node`` to be called when the simulation starts. Since a ``Node`` inherits
|
|
1565 |
from ``Object``, this calls the ``Object::Start`` method on the ``Node``
|
|
1566 |
which, in turn, calls the ``DoStart`` methods on all of the ``Objects``
|
|
1567 |
aggregated to the ``Node`` (think mobility models). Since the ``Node``
|
|
1568 |
``Object`` has overridden ``DoStart``, that method is called when the
|
|
1569 |
simulation starts. The ``Node::DoStart`` method calls the ``Start`` methods
|
|
1570 |
of all of the ``Applications`` on the node. Since ``Applications`` are
|
|
1571 |
also ``Objects``, this causes ``Application::DoStart`` to be called. When
|
|
1572 |
``Application::DoStart`` is called, it schedules events for the
|
|
1573 |
``StartApplication`` and ``StopApplication`` calls on the ``Application``.
|
|
1574 |
These calls are designed to start and stop the flow of data from the
|
|
1575 |
``Application``
|
|
1576 |
|
|
1577 |
This has been another fairly long journey, but it only has to be made once, and
|
|
1578 |
you now understand another very deep piece of |ns3|.
|
|
1579 |
|
|
1580 |
The MyApp Application
|
|
1581 |
~~~~~~~~~~~~~~~~~~~~~
|
|
1582 |
|
|
1583 |
The ``MyApp`` ``Application`` needs a constructor and a destructor,
|
|
1584 |
of course:
|
|
1585 |
|
|
1586 |
::
|
|
1587 |
|
|
1588 |
MyApp::MyApp ()
|
|
1589 |
: m_socket (0),
|
|
1590 |
m_peer (),
|
|
1591 |
m_packetSize (0),
|
|
1592 |
m_nPackets (0),
|
|
1593 |
m_dataRate (0),
|
|
1594 |
m_sendEvent (),
|
|
1595 |
m_running (false),
|
|
1596 |
m_packetsSent (0)
|
|
1597 |
{
|
|
1598 |
}
|
|
1599 |
|
|
1600 |
MyApp::~MyApp()
|
|
1601 |
{
|
|
1602 |
m_socket = 0;
|
|
1603 |
}
|
|
1604 |
|
|
1605 |
The existence of the next bit of code is the whole reason why we wrote this
|
|
1606 |
``Application`` in the first place.
|
|
1607 |
|
|
1608 |
::
|
|
1609 |
|
|
1610 |
void
|
|
1611 |
MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize,
|
|
1612 |
uint32_t nPackets, DataRate dataRate)
|
|
1613 |
{
|
|
1614 |
m_socket = socket;
|
|
1615 |
m_peer = address;
|
|
1616 |
m_packetSize = packetSize;
|
|
1617 |
m_nPackets = nPackets;
|
|
1618 |
m_dataRate = dataRate;
|
|
1619 |
}
|
|
1620 |
|
|
1621 |
This code should be pretty self-explanatory. We are just initializing member
|
|
1622 |
variables. The important one from the perspective of tracing is the
|
|
1623 |
``Ptr<Socket> socket`` which we needed to provide to the application
|
|
1624 |
during configuration time. Recall that we are going to create the ``Socket``
|
|
1625 |
as a ``TcpSocket`` (which is implemented by ``TcpNewReno``) and hook
|
|
1626 |
its "CongestionWindow" trace source before passing it to the ``Setup``
|
|
1627 |
method.
|
|
1628 |
|
|
1629 |
::
|
|
1630 |
|
|
1631 |
void
|
|
1632 |
MyApp::StartApplication (void)
|
|
1633 |
{
|
|
1634 |
m_running = true;
|
|
1635 |
m_packetsSent = 0;
|
|
1636 |
m_socket->Bind ();
|
|
1637 |
m_socket->Connect (m_peer);
|
|
1638 |
SendPacket ();
|
|
1639 |
}
|
|
1640 |
|
|
1641 |
The above code is the overridden implementation ``Application::StartApplication``
|
|
1642 |
that will be automatically called by the simulator to start our ``Application``
|
|
1643 |
running at the appropriate time. You can see that it does a ``Socket`` ``Bind``
|
|
1644 |
operation. If you are familiar with Berkeley Sockets this shouldn't be a surprise.
|
|
1645 |
It performs the required work on the local side of the connection just as you might
|
|
1646 |
expect. The following ``Connect`` will do what is required to establish a connection
|
|
1647 |
with the TCP at ``Address`` m_peer. It should now be clear why we need to defer
|
|
1648 |
a lot of this to simulation time, since the ``Connect`` is going to need a fully
|
|
1649 |
functioning network to complete. After the ``Connect``, the ``Application``
|
|
1650 |
then starts creating simulation events by calling ``SendPacket``.
|
|
1651 |
|
|
1652 |
The next bit of code explains to the ``Application`` how to stop creating
|
|
1653 |
simulation events.
|
|
1654 |
|
|
1655 |
::
|
|
1656 |
|
|
1657 |
void
|
|
1658 |
MyApp::StopApplication (void)
|
|
1659 |
{
|
|
1660 |
m_running = false;
|
|
1661 |
|
|
1662 |
if (m_sendEvent.IsRunning ())
|
|
1663 |
{
|
|
1664 |
Simulator::Cancel (m_sendEvent);
|
|
1665 |
}
|
|
1666 |
|
|
1667 |
if (m_socket)
|
|
1668 |
{
|
|
1669 |
m_socket->Close ();
|
|
1670 |
}
|
|
1671 |
}
|
|
1672 |
|
|
1673 |
Every time a simulation event is scheduled, an ``Event`` is created. If the
|
|
1674 |
``Event`` is pending execution or executing, its method ``IsRunning`` will
|
|
1675 |
return ``true``. In this code, if ``IsRunning()`` returns true, we
|
|
1676 |
``Cancel`` the event which removes it from the simulator event queue. By
|
|
1677 |
doing this, we break the chain of events that the ``Application`` is using to
|
|
1678 |
keep sending its ``Packets`` and the ``Application`` goes quiet. After we
|
|
1679 |
quiet the ``Application`` we ``Close`` the socket which tears down the TCP
|
|
1680 |
connection.
|
|
1681 |
|
|
1682 |
The socket is actually deleted in the destructor when the ``m_socket = 0`` is
|
|
1683 |
executed. This removes the last reference to the underlying Ptr<Socket> which
|
|
1684 |
causes the destructor of that Object to be called.
|
|
1685 |
|
|
1686 |
Recall that ``StartApplication`` called ``SendPacket`` to start the
|
|
1687 |
chain of events that describes the ``Application`` behavior.
|
|
1688 |
|
|
1689 |
::
|
|
1690 |
|
|
1691 |
void
|
|
1692 |
MyApp::SendPacket (void)
|
|
1693 |
{
|
|
1694 |
Ptr<Packet> packet = Create<Packet> (m_packetSize);
|
|
1695 |
m_socket->Send (packet);
|
|
1696 |
|
|
1697 |
if (++m_packetsSent < m_nPackets)
|
|
1698 |
{
|
|
1699 |
ScheduleTx ();
|
|
1700 |
}
|
|
1701 |
}
|
|
1702 |
|
|
1703 |
Here, you see that ``SendPacket`` does just that. It creates a ``Packet``
|
|
1704 |
and then does a ``Send`` which, if you know Berkeley Sockets, is probably
|
|
1705 |
just what you expected to see.
|
|
1706 |
|
|
1707 |
It is the responsibility of the ``Application`` to keep scheduling the
|
|
1708 |
chain of events, so the next lines call ``ScheduleTx`` to schedule another
|
|
1709 |
transmit event (a ``SendPacket``) until the ``Application`` decides it
|
|
1710 |
has sent enough.
|
|
1711 |
|
|
1712 |
::
|
|
1713 |
|
|
1714 |
void
|
|
1715 |
MyApp::ScheduleTx (void)
|
|
1716 |
{
|
|
1717 |
if (m_running)
|
|
1718 |
{
|
|
1719 |
Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
|
|
1720 |
m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
|
|
1721 |
}
|
|
1722 |
}
|
|
1723 |
|
|
1724 |
Here, you see that ``ScheduleTx`` does exactly that. If the ``Application``
|
|
1725 |
is running (if ``StopApplication`` has not been called) it will schedule a
|
|
1726 |
new event, which calls ``SendPacket`` again. The alert reader will spot
|
|
1727 |
something that also trips up new users. The data rate of an ``Application`` is
|
|
1728 |
just that. It has nothing to do with the data rate of an underlying ``Channel``.
|
|
1729 |
This is the rate at which the ``Application`` produces bits. It does not take
|
|
1730 |
into account any overhead for the various protocols or channels that it uses to
|
|
1731 |
transport the data. If you set the data rate of an ``Application`` to the same
|
|
1732 |
data rate as your underlying ``Channel`` you will eventually get a buffer overflow.
|
|
1733 |
|
|
1734 |
The Trace Sinks
|
|
1735 |
~~~~~~~~~~~~~~~
|
|
1736 |
|
|
1737 |
The whole point of this exercise is to get trace callbacks from TCP indicating the
|
|
1738 |
congestion window has been updated. The next piece of code implements the
|
|
1739 |
corresponding trace sink:
|
|
1740 |
|
|
1741 |
::
|
|
1742 |
|
|
1743 |
static void
|
|
1744 |
CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
|
|
1745 |
{
|
|
1746 |
NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
|
|
1747 |
}
|
|
1748 |
|
|
1749 |
This should be very familiar to you now, so we won't dwell on the details. This
|
|
1750 |
function just logs the current simulation time and the new value of the
|
|
1751 |
congestion window every time it is changed. You can probably imagine that you
|
|
1752 |
could load the resulting output into a graphics program (gnuplot or Excel) and
|
|
1753 |
immediately see a nice graph of the congestion window behavior over time.
|
|
1754 |
|
|
1755 |
We added a new trace sink to show where packets are dropped. We are going to
|
|
1756 |
add an error model to this code also, so we wanted to demonstrate this working.
|
|
1757 |
|
|
1758 |
::
|
|
1759 |
|
|
1760 |
static void
|
|
1761 |
RxDrop (Ptr<const Packet> p)
|
|
1762 |
{
|
|
1763 |
NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
|
|
1764 |
}
|
|
1765 |
|
|
1766 |
This trace sink will be connected to the "PhyRxDrop" trace source of the
|
|
1767 |
point-to-point NetDevice. This trace source fires when a packet is dropped
|
|
1768 |
by the physical layer of a ``NetDevice``. If you take a small detour to the
|
|
1769 |
source (``src/devices/point-to-point/point-to-point-net-device.cc``) you will
|
|
1770 |
see that this trace source refers to ``PointToPointNetDevice::m_phyRxDropTrace``.
|
|
1771 |
If you then look in ``src/devices/point-to-point/point-to-point-net-device.h``
|
|
1772 |
for this member variable, you will find that it is declared as a
|
|
1773 |
``TracedCallback<Ptr<const Packet> >``. This should tell you that the
|
|
1774 |
callback target should be a function that returns void and takes a single
|
|
1775 |
parameter which is a ``Ptr<const Packet>`` -- just what we have above.
|
|
1776 |
|
|
1777 |
The Main Program
|
|
1778 |
~~~~~~~~~~~~~~~~
|
|
1779 |
|
|
1780 |
The following code should be very familiar to you by now:
|
|
1781 |
|
|
1782 |
::
|
|
1783 |
|
|
1784 |
int
|
|
1785 |
main (int argc, char *argv[])
|
|
1786 |
{
|
|
1787 |
NodeContainer nodes;
|
|
1788 |
nodes.Create (2);
|
|
1789 |
|
|
1790 |
PointToPointHelper pointToPoint;
|
|
1791 |
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
|
|
1792 |
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
|
|
1793 |
|
|
1794 |
NetDeviceContainer devices;
|
|
1795 |
devices = pointToPoint.Install (nodes);
|
|
1796 |
|
|
1797 |
This creates two nodes with a point-to-point channel between them, just as
|
|
1798 |
shown in the illustration at the start of the file.
|
|
1799 |
|
|
1800 |
The next few lines of code show something new. If we trace a connection that
|
|
1801 |
behaves perfectly, we will end up with a monotonically increasing congestion
|
|
1802 |
window. To see any interesting behavior, we really want to introduce link
|
|
1803 |
errors which will drop packets, cause duplicate ACKs and trigger the more
|
|
1804 |
interesting behaviors of the congestion window.
|
|
1805 |
|
|
1806 |
|ns3| provides ``ErrorModel`` objects which can be attached to
|
|
1807 |
``Channels``. We are using the ``RateErrorModel`` which allows us
|
|
1808 |
to introduce errors into a ``Channel`` at a given *rate*.
|
|
1809 |
|
|
1810 |
::
|
|
1811 |
|
|
1812 |
Ptr<RateErrorModel> em = CreateObjectWithAttributes<RateErrorModel> (
|
|
1813 |
"RanVar", RandomVariableValue (UniformVariable (0., 1.)),
|
|
1814 |
"ErrorRate", DoubleValue (0.00001));
|
|
1815 |
devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (em));
|
|
1816 |
|
|
1817 |
The above code instantiates a ``RateErrorModel`` Object. Rather than
|
|
1818 |
using the two-step process of instantiating it and then setting Attributes,
|
|
1819 |
we use the convenience function ``CreateObjectWithAttributes`` which
|
|
1820 |
allows us to do both at the same time. We set the "RanVar"
|
|
1821 |
``Attribute`` to a random variable that generates a uniform distribution
|
|
1822 |
from 0 to 1. We also set the "ErrorRate" ``Attribute``.
|
|
1823 |
We then set the resulting instantiated ``RateErrorModel`` as the error
|
|
1824 |
model used by the point-to-point ``NetDevice``. This will give us some
|
|
1825 |
retransmissions and make our plot a little more interesting.
|
|
1826 |
|
|
1827 |
::
|
|
1828 |
|
|
1829 |
InternetStackHelper stack;
|
|
1830 |
stack.Install (nodes);
|
|
1831 |
|
|
1832 |
Ipv4AddressHelper address;
|
|
1833 |
address.SetBase ("10.1.1.0", "255.255.255.252");
|
|
1834 |
Ipv4InterfaceContainer interfaces = address.Assign (devices);
|
|
1835 |
|
|
1836 |
The above code should be familiar. It installs internet stacks on our two
|
|
1837 |
nodes and creates interfaces and assigns IP addresses for the point-to-point
|
|
1838 |
devices.
|
|
1839 |
|
|
1840 |
Since we are using TCP, we need something on the destination node to receive
|
|
1841 |
TCP connections and data. The ``PacketSink`` ``Application`` is commonly
|
|
1842 |
used in |ns3| for that purpose.
|
|
1843 |
|
|
1844 |
::
|
|
1845 |
|
|
1846 |
uint16_t sinkPort = 8080;
|
|
1847 |
Address sinkAddress (InetSocketAddress(interfaces.GetAddress (1), sinkPort));
|
|
1848 |
PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory",
|
|
1849 |
InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
|
|
1850 |
ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
|
|
1851 |
sinkApps.Start (Seconds (0.));
|
|
1852 |
sinkApps.Stop (Seconds (20.));
|
|
1853 |
|
|
1854 |
This should all be familiar, with the exception of,
|
|
1855 |
|
|
1856 |
::
|
|
1857 |
|
|
1858 |
PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory",
|
|
1859 |
InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
|
|
1860 |
|
|
1861 |
This code instantiates a ``PacketSinkHelper`` and tells it to create sockets
|
|
1862 |
using the class ``ns3::TcpSocketFactory``. This class implements a design
|
|
1863 |
pattern called "object factory" which is a commonly used mechanism for
|
|
1864 |
specifying a class used to create objects in an abstract way. Here, instead of
|
|
1865 |
having to create the objects themselves, you provide the ``PacketSinkHelper``
|
|
1866 |
a string that specifies a ``TypeId`` string used to create an object which
|
|
1867 |
can then be used, in turn, to create instances of the Objects created by the
|
|
1868 |
factory.
|
|
1869 |
|
|
1870 |
The remaining parameter tells the ``Application`` which address and port it
|
|
1871 |
should ``Bind`` to.
|
|
1872 |
|
|
1873 |
The next two lines of code will create the socket and connect the trace source.
|
|
1874 |
|
|
1875 |
::
|
|
1876 |
|
|
1877 |
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (nodes.Get (0),
|
|
1878 |
TcpSocketFactory::GetTypeId ());
|
|
1879 |
ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow",
|
|
1880 |
MakeCallback (&CwndChange));
|
|
1881 |
|
|
1882 |
The first statement calls the static member function ``Socket::CreateSocket``
|
|
1883 |
and provides a ``Node`` and an explicit ``TypeId`` for the object factory
|
|
1884 |
used to create the socket. This is a slightly lower level call than the
|
|
1885 |
``PacketSinkHelper`` call above, and uses an explicit C++ type instead of
|
|
1886 |
one referred to by a string. Otherwise, it is conceptually the same thing.
|
|
1887 |
|
|
1888 |
Once the ``TcpSocket`` is created and attached to the ``Node``, we can
|
|
1889 |
use ``TraceConnectWithoutContext`` to connect the CongestionWindow trace
|
|
1890 |
source to our trace sink.
|
|
1891 |
|
|
1892 |
Recall that we coded an ``Application`` so we could take that ``Socket``
|
|
1893 |
we just made (during configuration time) and use it in simulation time. We now
|
|
1894 |
have to instantiate that ``Application``. We didn't go to any trouble to
|
|
1895 |
create a helper to manage the ``Application`` so we are going to have to
|
|
1896 |
create and install it "manually". This is actually quite easy:
|
|
1897 |
|
|
1898 |
::
|
|
1899 |
|
|
1900 |
Ptr<MyApp> app = CreateObject<MyApp> ();
|
|
1901 |
app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000, DataRate ("1Mbps"));
|
|
1902 |
nodes.Get (0)->AddApplication (app);
|
|
1903 |
app->Start (Seconds (1.));
|
|
1904 |
app->Stop (Seconds (20.));
|
|
1905 |
|
|
1906 |
The first line creates an ``Object`` of type ``MyApp`` -- our
|
|
1907 |
``Application``. The second line tells the ``Application`` what
|
|
1908 |
``Socket`` to use, what address to connect to, how much data to send
|
|
1909 |
at each send event, how many send events to generate and the rate at which
|
|
1910 |
to produce data from those events.
|
|
1911 |
|
|
1912 |
Next, we manually add the ``MyApp Application`` to the source node
|
|
1913 |
and explicitly call the ``Start`` and ``Stop`` methods on the
|
|
1914 |
``Application`` to tell it when to start and stop doing its thing.
|
|
1915 |
|
|
1916 |
We need to actually do the connect from the receiver point-to-point ``NetDevice``
|
|
1917 |
to our callback now.
|
|
1918 |
|
|
1919 |
::
|
|
1920 |
|
|
1921 |
devices.Get (1)->TraceConnectWithoutContext("PhyRxDrop", MakeCallback (&RxDrop));
|
|
1922 |
|
|
1923 |
It should now be obvious that we are getting a reference to the receiving
|
|
1924 |
``Node NetDevice`` from its container and connecting the trace source defined
|
|
1925 |
by the attribute "PhyRxDrop" on that device to the trace sink ``RxDrop``.
|
|
1926 |
|
|
1927 |
Finally, we tell the simulator to override any ``Applications`` and just
|
|
1928 |
stop processing events at 20 seconds into the simulation.
|
|
1929 |
|
|
1930 |
::
|
|
1931 |
|
|
1932 |
Simulator::Stop (Seconds(20));
|
|
1933 |
Simulator::Run ();
|
|
1934 |
Simulator::Destroy ();
|
|
1935 |
|
|
1936 |
return 0;
|
|
1937 |
}
|
|
1938 |
|
|
1939 |
Recall that as soon as ``Simulator::Run`` is called, configuration time
|
|
1940 |
ends, and simulation time begins. All of the work we orchestrated by
|
|
1941 |
creating the ``Application`` and teaching it how to connect and send
|
|
1942 |
data actually happens during this function call.
|
|
1943 |
|
|
1944 |
As soon as ``Simulator::Run`` returns, the simulation is complete and
|
|
1945 |
we enter the teardown phase. In this case, ``Simulator::Destroy`` takes
|
|
1946 |
care of the gory details and we just return a success code after it completes.
|
|
1947 |
|
|
1948 |
Running fifth.cc
|
|
1949 |
++++++++++++++++
|
|
1950 |
|
|
1951 |
Since we have provided the file ``fifth.cc`` for you, if you have built
|
|
1952 |
your distribution (in debug mode since it uses NS_LOG -- recall that optimized
|
|
1953 |
builds optimize out NS_LOGs) it will be waiting for you to run.
|
|
1954 |
|
|
1955 |
::
|
|
1956 |
|
|
1957 |
./waf --run fifth
|
|
1958 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/build
|
|
1959 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone-dev/ns-3-dev/build'
|
|
1960 |
'build' finished successfully (0.684s)
|
|
1961 |
1.20919 1072
|
|
1962 |
1.21511 1608
|
|
1963 |
1.22103 2144
|
|
1964 |
...
|
|
1965 |
1.2471 8040
|
|
1966 |
1.24895 8576
|
|
1967 |
1.2508 9112
|
|
1968 |
RxDrop at 1.25151
|
|
1969 |
...
|
|
1970 |
|
|
1971 |
You can probably see immediately a downside of using prints of any kind in your
|
|
1972 |
traces. We get those extraneous waf messages printed all over our interesting
|
|
1973 |
information along with those RxDrop messages. We will remedy that soon, but I'm
|
|
1974 |
sure you can't wait to see the results of all of this work. Let's redirect that
|
|
1975 |
output to a file called ``cwnd.dat``:
|
|
1976 |
|
|
1977 |
::
|
|
1978 |
|
|
1979 |
./waf --run fifth > cwnd.dat 2>&1
|
|
1980 |
|
|
1981 |
Now edit up "cwnd.dat" in your favorite editor and remove the waf build status
|
|
1982 |
and drop lines, leaving only the traced data (you could also comment out the
|
|
1983 |
``TraceConnectWithoutContext("PhyRxDrop", MakeCallback (&RxDrop));`` in the
|
|
1984 |
script to get rid of the drop prints just as easily.
|
|
1985 |
|
|
1986 |
You can now run gnuplot (if you have it installed) and tell it to generate some
|
|
1987 |
pretty pictures:
|
|
1988 |
|
|
1989 |
::
|
|
1990 |
|
|
1991 |
gnuplot> set terminal png size 640,480
|
|
1992 |
gnuplot> set output "cwnd.png"
|
|
1993 |
gnuplot> plot "cwnd.dat" using 1:2 title 'Congestion Window' with linespoints
|
|
1994 |
gnuplot> exit
|
|
1995 |
|
|
1996 |
You should now have a graph of the congestion window versus time sitting in the
|
|
1997 |
file "cwnd.png" that looks like:
|
|
1998 |
|
|
1999 |
.. figure:: figures/cwnd.png
|
|
2000 |
|
|
2001 |
Using Mid-Level Helpers
|
|
2002 |
+++++++++++++++++++++++
|
|
2003 |
|
|
2004 |
In the previous section, we showed how to hook a trace source and get hopefully
|
|
2005 |
interesting information out of a simulation. Perhaps you will recall that we
|
|
2006 |
called logging to the standard output using ``std::cout`` a "Blunt Instrument"
|
|
2007 |
much earlier in this chapter. We also wrote about how it was a problem having
|
|
2008 |
to parse the log output in order to isolate interesting information. It may
|
|
2009 |
have occurred to you that we just spent a lot of time implementing an example
|
|
2010 |
that exhibits all of the problems we purport to fix with the |ns3| tracing
|
|
2011 |
system! You would be correct. But, bear with us. We're not done yet.
|
|
2012 |
|
|
2013 |
One of the most important things we want to do is to is to have the ability to
|
|
2014 |
easily control the amount of output coming out of the simulation; and we also
|
|
2015 |
want to save those data to a file so we can refer back to it later. We can use
|
|
2016 |
the mid-level trace helpers provided in |ns3| to do just that and complete
|
|
2017 |
the picture.
|
|
2018 |
|
|
2019 |
We provide a script that writes the cwnd change and drop events developed in
|
|
2020 |
the example ``fifth.cc`` to disk in separate files. The cwnd changes are
|
|
2021 |
stored as a tab-separated ASCII file and the drop events are stored in a pcap
|
|
2022 |
file. The changes to make this happen are quite small.
|
|
2023 |
|
|
2024 |
A sixth.cc Walkthrough
|
|
2025 |
~~~~~~~~~~~~~~~~~~~~~~
|
|
2026 |
|
|
2027 |
Let's take a look at the changes required to go from ``fifth.cc`` to
|
|
2028 |
``sixth.cc``. Open ``examples/tutorial/fifth.cc`` in your favorite
|
|
2029 |
editor. You can see the first change by searching for CwndChange. You will
|
|
2030 |
find that we have changed the signatures for the trace sinks and have added
|
|
2031 |
a single line to each sink that writes the traced information to a stream
|
|
2032 |
representing a file.
|
|
2033 |
|
|
2034 |
::
|
|
2035 |
|
|
2036 |
static void
|
|
2037 |
CwndChange (Ptr<OutputStreamWrapper> stream, uint32_t oldCwnd, uint32_t newCwnd)
|
|
2038 |
{
|
|
2039 |
NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
|
|
2040 |
*stream->GetStream () << Simulator::Now ().GetSeconds () << "\t" << oldCwnd << "\t" << newCwnd << std::endl;
|
|
2041 |
}
|
|
2042 |
|
|
2043 |
static void
|
|
2044 |
RxDrop (Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
|
|
2045 |
{
|
|
2046 |
NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
|
|
2047 |
file->Write(Simulator::Now(), p);
|
|
2048 |
}
|
|
2049 |
|
|
2050 |
We have added a "stream" parameter to the ``CwndChange`` trace sink.
|
|
2051 |
This is an object that holds (keeps safely alive) a C++ output stream. It
|
|
2052 |
turns out that this is a very simple object, but one that manages lifetime
|
|
2053 |
issues for the stream and solves a problem that even experienced C++ users
|
|
2054 |
run into. It turns out that the copy constructor for ostream is marked
|
|
2055 |
private. This means that ostreams do not obey value semantics and cannot
|
|
2056 |
be used in any mechanism that requires the stream to be copied. This includes
|
|
2057 |
the |ns3| callback system, which as you may recall, requires objects
|
|
2058 |
that obey value semantics. Further notice that we have added the following
|
|
2059 |
line in the ``CwndChange`` trace sink implementation:
|
|
2060 |
|
|
2061 |
::
|
|
2062 |
|
|
2063 |
*stream->GetStream () << Simulator::Now ().GetSeconds () << "\t" << oldCwnd << "\t" << newCwnd << std::endl;
|
|
2064 |
|
|
2065 |
This would be very familiar code if you replaced ``*stream->GetStream ()``
|
|
2066 |
with ``std::cout``, as in:
|
|
2067 |
|
|
2068 |
::
|
|
2069 |
|
|
2070 |
std::cout << Simulator::Now ().GetSeconds () << "\t" << oldCwnd << "\t" << newCwnd << std::endl;
|
|
2071 |
|
|
2072 |
This illustrates that the ``Ptr<OutputStreamWrapper>`` is really just
|
|
2073 |
carrying around a ``std::ofstream`` for you, and you can use it here like
|
|
2074 |
any other output stream.
|
|
2075 |
|
|
2076 |
A similar situation happens in ``RxDrop`` except that the object being
|
|
2077 |
passed around (a ``Ptr<PcapFileWrapper>``) represents a pcap file. There
|
|
2078 |
is a one-liner in the trace sink to write a timestamp and the contents of the
|
|
2079 |
packet being dropped to the pcap file:
|
|
2080 |
|
|
2081 |
::
|
|
2082 |
|
|
2083 |
file->Write(Simulator::Now(), p);
|
|
2084 |
|
|
2085 |
Of course, if we have objects representing the two files, we need to create
|
|
2086 |
them somewhere and also cause them to be passed to the trace sinks. If you
|
|
2087 |
look in the ``main`` function, you will find new code to do just that:
|
|
2088 |
|
|
2089 |
::
|
|
2090 |
|
|
2091 |
AsciiTraceHelper asciiTraceHelper;
|
|
2092 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("sixth.cwnd");
|
|
2093 |
ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeBoundCallback (&CwndChange, stream));
|
|
2094 |
|
|
2095 |
...
|
|
2096 |
|
|
2097 |
PcapHelper pcapHelper;
|
|
2098 |
Ptr<PcapFileWrapper> file = pcapHelper.CreateFile ("sixth.pcap", std::ios::out, PcapHelper::DLT_PPP);
|
|
2099 |
devices.Get (1)->TraceConnectWithoutContext("PhyRxDrop", MakeBoundCallback (&RxDrop, file));
|
|
2100 |
|
|
2101 |
In the first section of the code snippet above, we are creating the ASCII
|
|
2102 |
trace file, creating an object responsible for managing it and using a
|
|
2103 |
variant of the callback creation function to arrange for the object to be
|
|
2104 |
passed to the sink. Our ASCII trace helpers provide a rich set of
|
|
2105 |
functions to make using text (ASCII) files easy. We are just going to
|
|
2106 |
illustrate the use of the file stream creation function here.
|
|
2107 |
|
|
2108 |
The ``CreateFileStream{}`` function is basically going to instantiate
|
|
2109 |
a std::ofstream object and create a new file (or truncate an existing file).
|
|
2110 |
This ofstream is packaged up in an |ns3| object for lifetime management
|
|
2111 |
and copy constructor issue resolution.
|
|
2112 |
|
|
2113 |
We then take this |ns3| object representing the file and pass it to
|
|
2114 |
``MakeBoundCallback()``. This function creates a callback just like
|
|
2115 |
``MakeCallback()``, but it "binds" a new value to the callback. This
|
|
2116 |
value is added to the callback before it is called.
|
|
2117 |
|
|
2118 |
Essentially, ``MakeBoundCallback(&CwndChange, stream)`` causes the trace
|
|
2119 |
source to add the additional "stream" parameter to the front of the formal
|
|
2120 |
parameter list before invoking the callback. This changes the required
|
|
2121 |
signature of the ``CwndChange`` sink to match the one shown above, which
|
|
2122 |
includes the "extra" parameter ``Ptr<OutputStreamWrapper> stream``.
|
|
2123 |
|
|
2124 |
In the second section of code in the snippet above, we instantiate a
|
|
2125 |
``PcapHelper`` to do the same thing for our pcap trace file that we did
|
|
2126 |
with the ``AsciiTraceHelper``. The line of code,
|
|
2127 |
|
|
2128 |
::
|
|
2129 |
|
|
2130 |
Ptr<PcapFileWrapper> file = pcapHelper.CreateFile ("sixth.pcap", "w", PcapHelper::DLT_PPP);
|
|
2131 |
|
|
2132 |
creates a pcap file named "sixth.pcap" with file mode "w". This means that
|
|
2133 |
the new file is to truncated if an existing file with that name is found. The
|
|
2134 |
final parameter is the "data link type" of the new pcap file. These are
|
|
2135 |
the same as the pcap library data link types defined in ``bpf.h`` if you are
|
|
2136 |
familar with pcap. In this case, ``DLT_PPP`` indicates that the pcap file
|
|
2137 |
is going to contain packets prefixed with point to point headers. This is true
|
|
2138 |
since the packets are coming from our point-to-point device driver. Other
|
|
2139 |
common data link types are DLT_EN10MB (10 MB Ethernet) appropriate for csma
|
|
2140 |
devices and DLT_IEEE802_11 (IEEE 802.11) appropriate for wifi devices. These
|
|
2141 |
are defined in ``src/helper/trace-helper.h"`` if you are interested in seeing
|
|
2142 |
the list. The entries in the list match those in ``bpf.h`` but we duplicate
|
|
2143 |
them to avoid a pcap source dependence.
|
|
2144 |
|
|
2145 |
A |ns3| object representing the pcap file is returned from ``CreateFile``
|
|
2146 |
and used in a bound callback exactly as it was in the ascii case.
|
|
2147 |
|
|
2148 |
An important detour: It is important to notice that even though both of these
|
|
2149 |
objects are declared in very similar ways,
|
|
2150 |
|
|
2151 |
::
|
|
2152 |
|
|
2153 |
Ptr<PcapFileWrapper> file ...
|
|
2154 |
Ptr<OutputStreamWrapper> stream ...
|
|
2155 |
|
|
2156 |
The underlying objects are entirely different. For example, the
|
|
2157 |
Ptr<PcapFileWrapper> is a smart pointer to an |ns3| Object that is a
|
|
2158 |
fairly heaviweight thing that supports ``Attributes`` and is integrated into
|
|
2159 |
the config system. The Ptr<OutputStreamWrapper>, on the other hand, is a smart
|
|
2160 |
pointer to a reference counted object that is a very lightweight thing.
|
|
2161 |
Remember to always look at the object you are referencing before making any
|
|
2162 |
assumptions about the "powers" that object may have.
|
|
2163 |
|
|
2164 |
For example, take a look at ``src/common/pcap-file-object.h`` in the
|
|
2165 |
distribution and notice,
|
|
2166 |
|
|
2167 |
::
|
|
2168 |
|
|
2169 |
class PcapFileWrapper : public Object
|
|
2170 |
|
|
2171 |
that class ``PcapFileWrapper`` is an |ns3| Object by virtue of
|
|
2172 |
its inheritance. Then look at ``src/common/output-stream-wrapper.h`` and
|
|
2173 |
notice,
|
|
2174 |
|
|
2175 |
::
|
|
2176 |
|
|
2177 |
class OutputStreamWrapper : public SimpleRefCount<OutputStreamWrapper>
|
|
2178 |
|
|
2179 |
that this object is not an |ns3| Object at all, it is "merely" a
|
|
2180 |
C++ object that happens to support intrusive reference counting.
|
|
2181 |
|
|
2182 |
The point here is that just because you read Ptr<something> it does not necessarily
|
|
2183 |
mean that "something" is an |ns3| Object on which you can hang |ns3|
|
|
2184 |
``Attributes``, for example.
|
|
2185 |
|
|
2186 |
Now, back to the example. If you now build and run this example,
|
|
2187 |
|
|
2188 |
::
|
|
2189 |
|
|
2190 |
./waf --run sixth
|
|
2191 |
|
|
2192 |
you will see the same messages appear as when you ran "fifth", but two new
|
|
2193 |
files will appear in the top-level directory of your |ns3| distribution.
|
|
2194 |
|
|
2195 |
::
|
|
2196 |
|
|
2197 |
sixth.cwnd sixth.pcap
|
|
2198 |
|
|
2199 |
Since "sixth.cwnd" is an ASCII text file, you can view it with ``cat``
|
|
2200 |
or your favorite file viewer.
|
|
2201 |
|
|
2202 |
::
|
|
2203 |
|
|
2204 |
1.20919 536 1072
|
|
2205 |
1.21511 1072 1608
|
|
2206 |
...
|
|
2207 |
9.30922 8893 8925
|
|
2208 |
9.31754 8925 8957
|
|
2209 |
|
|
2210 |
You have a tab separated file with a timestamp, an old congestion window and a
|
|
2211 |
new congestion window suitable for directly importing into your plot program.
|
|
2212 |
There are no extraneous prints in the file, no parsing or editing is required.
|
|
2213 |
|
|
2214 |
Since "sixth.pcap" is a pcap file, you can fiew it with ``tcpdump``.
|
|
2215 |
|
|
2216 |
::
|
|
2217 |
|
|
2218 |
reading from file ../../sixth.pcap, link-type PPP (PPP)
|
|
2219 |
1.251507 IP 10.1.1.1.49153 > 10.1.1.2.8080: . 17689:18225(536) ack 1 win 65535
|
|
2220 |
1.411478 IP 10.1.1.1.49153 > 10.1.1.2.8080: . 33808:34312(504) ack 1 win 65535
|
|
2221 |
...
|
|
2222 |
7.393557 IP 10.1.1.1.49153 > 10.1.1.2.8080: . 781568:782072(504) ack 1 win 65535
|
|
2223 |
8.141483 IP 10.1.1.1.49153 > 10.1.1.2.8080: . 874632:875168(536) ack 1 win 65535
|
|
2224 |
|
|
2225 |
You have a pcap file with the packets that were dropped in the simulation. There
|
|
2226 |
are no other packets present in the file and there is nothing else present to
|
|
2227 |
make life difficult.
|
|
2228 |
|
|
2229 |
It's been a long journey, but we are now at a point where we can appreciate the
|
|
2230 |
|ns3| tracing system. We have pulled important events out of the middle
|
|
2231 |
of a TCP implementation and a device driver. We stored those events directly in
|
|
2232 |
files usable with commonly known tools. We did this without modifying any of the
|
|
2233 |
core code involved, and we did this in only 18 lines of code:
|
|
2234 |
|
|
2235 |
::
|
|
2236 |
|
|
2237 |
static void
|
|
2238 |
CwndChange (Ptr<OutputStreamWrapper> stream, uint32_t oldCwnd, uint32_t newCwnd)
|
|
2239 |
{
|
|
2240 |
NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
|
|
2241 |
*stream->GetStream () << Simulator::Now ().GetSeconds () << "\t" << oldCwnd << "\t" << newCwnd << std::endl;
|
|
2242 |
}
|
|
2243 |
|
|
2244 |
...
|
|
2245 |
|
|
2246 |
AsciiTraceHelper asciiTraceHelper;
|
|
2247 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("sixth.cwnd");
|
|
2248 |
ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeBoundCallback (&CwndChange, stream));
|
|
2249 |
|
|
2250 |
...
|
|
2251 |
|
|
2252 |
static void
|
|
2253 |
RxDrop (Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
|
|
2254 |
{
|
|
2255 |
NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
|
|
2256 |
file->Write(Simulator::Now(), p);
|
|
2257 |
}
|
|
2258 |
|
|
2259 |
...
|
|
2260 |
|
|
2261 |
PcapHelper pcapHelper;
|
|
2262 |
Ptr<PcapFileWrapper> file = pcapHelper.CreateFile ("sixth.pcap", "w", PcapHelper::DLT_PPP);
|
|
2263 |
devices.Get (1)->TraceConnectWithoutContext("PhyRxDrop", MakeBoundCallback (&RxDrop, file));
|
|
2264 |
|
|
2265 |
Using Trace Helpers
|
|
2266 |
*******************
|
|
2267 |
|
|
2268 |
The |ns3| trace helpers provide a rich environment for configuring and
|
|
2269 |
selecting different trace events and writing them to files. In previous
|
|
2270 |
sections, primarily "Building Topologies," we have seen several varieties
|
|
2271 |
of the trace helper methods designed for use inside other (device) helpers.
|
|
2272 |
|
|
2273 |
Perhaps you will recall seeing some of these variations:
|
|
2274 |
|
|
2275 |
::
|
|
2276 |
|
|
2277 |
pointToPoint.EnablePcapAll ("second");
|
|
2278 |
pointToPoint.EnablePcap ("second", p2pNodes.Get (0)->GetId (), 0);
|
|
2279 |
csma.EnablePcap ("third", csmaDevices.Get (0), true);
|
|
2280 |
pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("myfirst.tr"));
|
|
2281 |
|
|
2282 |
What may not be obvious, though, is that there is a consistent model for all of
|
|
2283 |
the trace-related methods found in the system. We will now take a little time
|
|
2284 |
and take a look at the "big picture".
|
|
2285 |
|
|
2286 |
There are currently two primary use cases of the tracing helpers in |ns3|:
|
|
2287 |
Device helpers and protocol helpers. Device helpers look at the problem
|
|
2288 |
of specifying which traces should be enabled through a node, device pair. For
|
|
2289 |
example, you may want to specify that pcap tracing should be enabled on a
|
|
2290 |
particular device on a specific node. This follows from the |ns3| device
|
|
2291 |
conceptual model, and also the conceptual models of the various device helpers.
|
|
2292 |
Following naturally from this, the files created follow a
|
|
2293 |
<prefix>-<node>-<device> naming convention.
|
|
2294 |
|
|
2295 |
Protocol helpers look at the problem of specifying which traces should be
|
|
2296 |
enabled through a protocol and interface pair. This follows from the |ns3|
|
|
2297 |
protocol stack conceptual model, and also the conceptual models of internet
|
|
2298 |
stack helpers. Naturally, the trace files should follow a
|
|
2299 |
<prefix>-<protocol>-<interface> naming convention.
|
|
2300 |
|
|
2301 |
The trace helpers therefore fall naturally into a two-dimensional taxonomy.
|
|
2302 |
There are subtleties that prevent all four classes from behaving identically,
|
|
2303 |
but we do strive to make them all work as similarly as possible; and whenever
|
|
2304 |
possible there are analogs for all methods in all classes.
|
|
2305 |
|
|
2306 |
::
|
|
2307 |
|
|
2308 |
| pcap | ascii |
|
|
2309 |
-----------------+------+-------|
|
|
2310 |
Device Helper | | |
|
|
2311 |
-----------------+------+-------|
|
|
2312 |
Protocol Helper | | |
|
|
2313 |
-----------------+------+-------|
|
|
2314 |
|
|
2315 |
We use an approach called a ``mixin`` to add tracing functionality to our
|
|
2316 |
helper classes. A ``mixin`` is a class that provides functionality to that
|
|
2317 |
is inherited by a subclass. Inheriting from a mixin is not considered a form
|
|
2318 |
of specialization but is really a way to collect functionality.
|
|
2319 |
|
|
2320 |
Let's take a quick look at all four of these cases and their respective
|
|
2321 |
``mixins``.
|
|
2322 |
|
|
2323 |
Pcap Tracing Device Helpers
|
|
2324 |
+++++++++++++++++++++++++++
|
|
2325 |
|
|
2326 |
The goal of these helpers is to make it easy to add a consistent pcap trace
|
|
2327 |
facility to an |ns3| device. We want all of the various flavors of
|
|
2328 |
pcap tracing to work the same across all devices, so the methods of these
|
|
2329 |
helpers are inherited by device helpers. Take a look at
|
|
2330 |
``src/helper/trace-helper.h`` if you want to follow the discussion while
|
|
2331 |
looking at real code.
|
|
2332 |
|
|
2333 |
The class ``PcapHelperForDevice`` is a ``mixin`` provides the high level
|
|
2334 |
functionality for using pcap tracing in an |ns3| device. Every device
|
|
2335 |
must implement a single virtual method inherited from this class.
|
|
2336 |
|
|
2337 |
::
|
|
2338 |
|
|
2339 |
virtual void EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename) = 0;
|
|
2340 |
|
|
2341 |
The signature of this method reflects the device-centric view of the situation
|
|
2342 |
at this level. All of the public methods inherited from class
|
|
2343 |
2``PcapUserHelperForDevice`` reduce to calling this single device-dependent
|
|
2344 |
implementation method. For example, the lowest level pcap method,
|
|
2345 |
|
|
2346 |
::
|
|
2347 |
|
|
2348 |
void EnablePcap (std::string prefix, Ptr<NetDevice> nd, bool promiscuous = false, bool explicitFilename = false);
|
|
2349 |
|
|
2350 |
will call the device implementation of ``EnablePcapInternal`` directly. All
|
|
2351 |
other public pcap tracing methods build on this implementation to provide
|
|
2352 |
additional user-level functionality. What this means to the user is that all
|
|
2353 |
device helpers in the system will have all of the pcap trace methods available;
|
|
2354 |
and these methods will all work in the same way across devices if the device
|
|
2355 |
implements ``EnablePcapInternal`` correctly.
|
|
2356 |
|
|
2357 |
Pcap Tracing Device Helper Methods
|
|
2358 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2359 |
|
|
2360 |
::
|
|
2361 |
|
|
2362 |
void EnablePcap (std::string prefix, Ptr<NetDevice> nd, bool promiscuous = false, bool explicitFilename = false);
|
|
2363 |
void EnablePcap (std::string prefix, std::string ndName, bool promiscuous = false, bool explicitFilename = false);
|
|
2364 |
void EnablePcap (std::string prefix, NetDeviceContainer d, bool promiscuous = false);
|
|
2365 |
void EnablePcap (std::string prefix, NodeContainer n, bool promiscuous = false);
|
|
2366 |
void EnablePcap (std::string prefix, uint32_t nodeid, uint32_t deviceid, bool promiscuous = false);
|
|
2367 |
void EnablePcapAll (std::string prefix, bool promiscuous = false);
|
|
2368 |
|
|
2369 |
In each of the methods shown above, there is a default parameter called
|
|
2370 |
``promiscuous`` that defaults to false. This parameter indicates that the
|
|
2371 |
trace should not be gathered in promiscuous mode. If you do want your traces
|
|
2372 |
to include all traffic seen by the device (and if the device supports a
|
|
2373 |
promiscuous mode) simply add a true parameter to any of the calls above. For example,
|
|
2374 |
|
|
2375 |
::
|
|
2376 |
|
|
2377 |
Ptr<NetDevice> nd;
|
|
2378 |
...
|
|
2379 |
helper.EnablePcap ("prefix", nd, true);
|
|
2380 |
|
|
2381 |
will enable promiscuous mode captures on the ``NetDevice`` specified by ``nd``.
|
|
2382 |
|
|
2383 |
The first two methods also include a default parameter called ``explicitFilename``
|
|
2384 |
that will be discussed below.
|
|
2385 |
|
|
2386 |
You are encouraged to peruse the Doxygen for class ``PcapHelperForDevice``
|
|
2387 |
to find the details of these methods; but to summarize ...
|
|
2388 |
|
|
2389 |
You can enable pcap tracing on a particular node/net-device pair by providing a
|
|
2390 |
``Ptr<NetDevice>`` to an ``EnablePcap`` method. The ``Ptr<Node>`` is
|
|
2391 |
implicit since the net device must belong to exactly one ``Node``.
|
|
2392 |
For example,
|
|
2393 |
|
|
2394 |
::
|
|
2395 |
|
|
2396 |
Ptr<NetDevice> nd;
|
|
2397 |
...
|
|
2398 |
helper.EnablePcap ("prefix", nd);
|
|
2399 |
|
|
2400 |
You can enable pcap tracing on a particular node/net-device pair by providing a
|
|
2401 |
``std::string`` representing an object name service string to an
|
|
2402 |
``EnablePcap`` method. The ``Ptr<NetDevice>`` is looked up from the name
|
|
2403 |
string. Again, the ``<Node>`` is implicit since the named net device must
|
|
2404 |
belong to exactly one ``Node``. For example,
|
|
2405 |
|
|
2406 |
::
|
|
2407 |
|
|
2408 |
Names::Add ("server" ...);
|
|
2409 |
Names::Add ("server/eth0" ...);
|
|
2410 |
...
|
|
2411 |
helper.EnablePcap ("prefix", "server/ath0");
|
|
2412 |
|
|
2413 |
You can enable pcap tracing on a collection of node/net-device pairs by
|
|
2414 |
providing a ``NetDeviceContainer``. For each ``NetDevice`` in the container
|
|
2415 |
the type is checked. For each device of the proper type (the same type as is
|
|
2416 |
managed by the device helper), tracing is enabled. Again, the ``<Node>`` is
|
|
2417 |
implicit since the found net device must belong to exactly one ``Node``.
|
|
2418 |
For example,
|
|
2419 |
|
|
2420 |
::
|
|
2421 |
|
|
2422 |
NetDeviceContainer d = ...;
|
|
2423 |
...
|
|
2424 |
helper.EnablePcap ("prefix", d);
|
|
2425 |
|
|
2426 |
You can enable pcap tracing on a collection of node/net-device pairs by
|
|
2427 |
providing a ``NodeContainer``. For each ``Node`` in the ``NodeContainer``
|
|
2428 |
its attached ``NetDevices`` are iterated. For each ``NetDevice`` attached
|
|
2429 |
to each node in the container, the type of that device is checked. For each
|
|
2430 |
device of the proper type (the same type as is managed by the device helper),
|
|
2431 |
tracing is enabled.
|
|
2432 |
|
|
2433 |
::
|
|
2434 |
|
|
2435 |
NodeContainer n;
|
|
2436 |
...
|
|
2437 |
helper.EnablePcap ("prefix", n);
|
|
2438 |
|
|
2439 |
You can enable pcap tracing on the basis of node ID and device ID as well as
|
|
2440 |
with explicit ``Ptr``. Each ``Node`` in the system has an integer node ID
|
|
2441 |
and each device connected to a node has an integer device ID.
|
|
2442 |
|
|
2443 |
::
|
|
2444 |
|
|
2445 |
helper.EnablePcap ("prefix", 21, 1);
|
|
2446 |
|
|
2447 |
Finally, you can enable pcap tracing for all devices in the system, with the
|
|
2448 |
same type as that managed by the device helper.
|
|
2449 |
|
|
2450 |
::
|
|
2451 |
|
|
2452 |
helper.EnablePcapAll ("prefix");
|
|
2453 |
|
|
2454 |
Pcap Tracing Device Helper Filename Selection
|
|
2455 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2456 |
|
|
2457 |
Implicit in the method descriptions above is the construction of a complete
|
|
2458 |
filename by the implementation method. By convention, pcap traces in the
|
|
2459 |
|ns3| system are of the form "<prefix>-<node id>-<device id>.pcap"
|
|
2460 |
|
|
2461 |
As previously mentioned, every node in the system will have a system-assigned
|
|
2462 |
node id; and every device will have an interface index (also called a device id)
|
|
2463 |
relative to its node. By default, then, a pcap trace file created as a result
|
|
2464 |
of enabling tracing on the first device of node 21 using the prefix "prefix"
|
|
2465 |
would be "prefix-21-1.pcap".
|
|
2466 |
|
|
2467 |
You can always use the |ns3| object name service to make this more clear.
|
|
2468 |
For example, if you use the object name service to assign the name "server"
|
|
2469 |
to node 21, the resulting pcap trace file name will automatically become,
|
|
2470 |
"prefix-server-1.pcap" and if you also assign the name "eth0" to the
|
|
2471 |
device, your pcap file name will automatically pick this up and be called
|
|
2472 |
"prefix-server-eth0.pcap".
|
|
2473 |
|
|
2474 |
Finally, two of the methods shown above,
|
|
2475 |
|
|
2476 |
::
|
|
2477 |
|
|
2478 |
void EnablePcap (std::string prefix, Ptr<NetDevice> nd, bool promiscuous = false, bool explicitFilename = false);
|
|
2479 |
void EnablePcap (std::string prefix, std::string ndName, bool promiscuous = false, bool explicitFilename = false);
|
|
2480 |
|
|
2481 |
have a default parameter called ``explicitFilename``. When set to true,
|
|
2482 |
this parameter disables the automatic filename completion mechanism and allows
|
|
2483 |
you to create an explicit filename. This option is only available in the
|
|
2484 |
methods which enable pcap tracing on a single device.
|
|
2485 |
|
|
2486 |
For example, in order to arrange for a device helper to create a single
|
|
2487 |
promiscuous pcap capture file of a specific name ("my-pcap-file.pcap") on a
|
|
2488 |
given device, one could:
|
|
2489 |
|
|
2490 |
::
|
|
2491 |
|
|
2492 |
Ptr<NetDevice> nd;
|
|
2493 |
...
|
|
2494 |
helper.EnablePcap ("my-pcap-file.pcap", nd, true, true);
|
|
2495 |
|
|
2496 |
The first ``true`` parameter enables promiscuous mode traces and the second
|
|
2497 |
tells the helper to interpret the ``prefix`` parameter as a complete filename.
|
|
2498 |
|
|
2499 |
Ascii Tracing Device Helpers
|
|
2500 |
++++++++++++++++++++++++++++
|
|
2501 |
|
|
2502 |
The behavior of the ascii trace helper ``mixin`` is substantially similar to
|
|
2503 |
the pcap version. Take a look at ``src/helper/trace-helper.h`` if you want to
|
|
2504 |
follow the discussion while looking at real code.
|
|
2505 |
|
|
2506 |
The class ``AsciiTraceHelperForDevice`` adds the high level functionality for
|
|
2507 |
using ascii tracing to a device helper class. As in the pcap case, every device
|
|
2508 |
must implement a single virtual method inherited from the ascii trace ``mixin``.
|
|
2509 |
|
|
2510 |
::
|
|
2511 |
|
|
2512 |
virtual void EnableAsciiInternal (Ptr<OutputStreamWrapper> stream,
|
|
2513 |
std::string prefix,
|
|
2514 |
Ptr<NetDevice> nd,
|
|
2515 |
bool explicitFilename) = 0;
|
|
2516 |
|
|
2517 |
|
|
2518 |
The signature of this method reflects the device-centric view of the situation
|
|
2519 |
at this level; and also the fact that the helper may be writing to a shared
|
|
2520 |
output stream. All of the public ascii-trace-related methods inherited from
|
|
2521 |
class ``AsciiTraceHelperForDevice`` reduce to calling this single device-
|
|
2522 |
dependent implementation method. For example, the lowest level ascii trace
|
|
2523 |
methods,
|
|
2524 |
|
|
2525 |
::
|
|
2526 |
|
|
2527 |
void EnableAscii (std::string prefix, Ptr<NetDevice> nd, bool explicitFilename = false);
|
|
2528 |
void EnableAscii (Ptr<OutputStreamWrapper> stream, Ptr<NetDevice> nd);
|
|
2529 |
|
|
2530 |
|
|
2531 |
will call the device implementation of ``EnableAsciiInternal`` directly,
|
|
2532 |
providing either a valid prefix or stream. All other public ascii tracing
|
|
2533 |
methods will build on these low-level functions to provide additional user-level
|
|
2534 |
functionality. What this means to the user is that all device helpers in the
|
|
2535 |
system will have all of the ascii trace methods available; and these methods
|
|
2536 |
will all work in the same way across devices if the devices implement
|
|
2537 |
``EnablAsciiInternal`` correctly.
|
|
2538 |
|
|
2539 |
Ascii Tracing Device Helper Methods
|
|
2540 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2541 |
|
|
2542 |
::
|
|
2543 |
|
|
2544 |
void EnableAscii (std::string prefix, Ptr<NetDevice> nd, bool explicitFilename = false);
|
|
2545 |
void EnableAscii (Ptr<OutputStreamWrapper> stream, Ptr<NetDevice> nd);
|
|
2546 |
|
|
2547 |
void EnableAscii (std::string prefix, std::string ndName, bool explicitFilename = false);
|
|
2548 |
void EnableAscii (Ptr<OutputStreamWrapper> stream, std::string ndName);
|
|
2549 |
|
|
2550 |
void EnableAscii (std::string prefix, NetDeviceContainer d);
|
|
2551 |
void EnableAscii (Ptr<OutputStreamWrapper> stream, NetDeviceContainer d);
|
|
2552 |
|
|
2553 |
void EnableAscii (std::string prefix, NodeContainer n);
|
|
2554 |
void EnableAscii (Ptr<OutputStreamWrapper> stream, NodeContainer n);
|
|
2555 |
|
|
2556 |
void EnableAsciiAll (std::string prefix);
|
|
2557 |
void EnableAsciiAll (Ptr<OutputStreamWrapper> stream);
|
|
2558 |
|
|
2559 |
void EnableAscii (std::string prefix, uint32_t nodeid, uint32_t deviceid, bool explicitFilename);
|
|
2560 |
void EnableAscii (Ptr<OutputStreamWrapper> stream, uint32_t nodeid, uint32_t deviceid);
|
|
2561 |
|
|
2562 |
You are encouraged to peruse the Doxygen for class ``TraceHelperForDevice``
|
|
2563 |
to find the details of these methods; but to summarize ...
|
|
2564 |
|
|
2565 |
There are twice as many methods available for ascii tracing as there were for
|
|
2566 |
pcap tracing. This is because, in addition to the pcap-style model where traces
|
|
2567 |
from each unique node/device pair are written to a unique file, we support a model
|
|
2568 |
in which trace information for many node/device pairs is written to a common file.
|
|
2569 |
This means that the <prefix>-<node>-<device> file name generation mechanism is
|
|
2570 |
replaced by a mechanism to refer to a common file; and the number of API methods
|
|
2571 |
is doubled to allow all combinations.
|
|
2572 |
|
|
2573 |
Just as in pcap tracing, you can enable ascii tracing on a particular
|
|
2574 |
node/net-device pair by providing a ``Ptr<NetDevice>`` to an ``EnableAscii``
|
|
2575 |
method. The ``Ptr<Node>`` is implicit since the net device must belong to
|
|
2576 |
exactly one ``Node``. For example,
|
|
2577 |
|
|
2578 |
::
|
|
2579 |
|
|
2580 |
Ptr<NetDevice> nd;
|
|
2581 |
...
|
|
2582 |
helper.EnableAscii ("prefix", nd);
|
|
2583 |
|
|
2584 |
The first four methods also include a default parameter called ``explicitFilename``
|
|
2585 |
that operate similar to equivalent parameters in the pcap case.
|
|
2586 |
|
|
2587 |
In this case, no trace contexts are written to the ascii trace file since they
|
|
2588 |
would be redundant. The system will pick the file name to be created using
|
|
2589 |
the same rules as described in the pcap section, except that the file will
|
|
2590 |
have the suffix ".tr" instead of ".pcap".
|
|
2591 |
|
|
2592 |
If you want to enable ascii tracing on more than one net device and have all
|
|
2593 |
traces sent to a single file, you can do that as well by using an object to
|
|
2594 |
refer to a single file. We have already seen this in the "cwnd" example
|
|
2595 |
above:
|
|
2596 |
|
|
2597 |
::
|
|
2598 |
|
|
2599 |
Ptr<NetDevice> nd1;
|
|
2600 |
Ptr<NetDevice> nd2;
|
|
2601 |
...
|
|
2602 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("trace-file-name.tr");
|
|
2603 |
...
|
|
2604 |
helper.EnableAscii (stream, nd1);
|
|
2605 |
helper.EnableAscii (stream, nd2);
|
|
2606 |
|
|
2607 |
|
|
2608 |
In this case, trace contexts are written to the ascii trace file since they
|
|
2609 |
are required to disambiguate traces from the two devices. Note that since the
|
|
2610 |
user is completely specifying the file name, the string should include the ",tr"
|
|
2611 |
for consistency.
|
|
2612 |
|
|
2613 |
You can enable ascii tracing on a particular node/net-device pair by providing a
|
|
2614 |
``std::string`` representing an object name service string to an
|
|
2615 |
``EnablePcap`` method. The ``Ptr<NetDevice>`` is looked up from the name
|
|
2616 |
string. Again, the ``<Node>`` is implicit since the named net device must
|
|
2617 |
belong to exactly one ``Node``. For example,
|
|
2618 |
|
|
2619 |
::
|
|
2620 |
|
|
2621 |
Names::Add ("client" ...);
|
|
2622 |
Names::Add ("client/eth0" ...);
|
|
2623 |
Names::Add ("server" ...);
|
|
2624 |
Names::Add ("server/eth0" ...);
|
|
2625 |
...
|
|
2626 |
helper.EnableAscii ("prefix", "client/eth0");
|
|
2627 |
helper.EnableAscii ("prefix", "server/eth0");
|
|
2628 |
|
|
2629 |
This would result in two files named "prefix-client-eth0.tr" and
|
|
2630 |
"prefix-server-eth0.tr" with traces for each device in the respective trace
|
|
2631 |
file. Since all of the EnableAscii functions are overloaded to take a stream wrapper,
|
|
2632 |
you can use that form as well:
|
|
2633 |
|
|
2634 |
::
|
|
2635 |
|
|
2636 |
Names::Add ("client" ...);
|
|
2637 |
Names::Add ("client/eth0" ...);
|
|
2638 |
Names::Add ("server" ...);
|
|
2639 |
Names::Add ("server/eth0" ...);
|
|
2640 |
...
|
|
2641 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("trace-file-name.tr");
|
|
2642 |
...
|
|
2643 |
helper.EnableAscii (stream, "client/eth0");
|
|
2644 |
helper.EnableAscii (stream, "server/eth0");
|
|
2645 |
|
|
2646 |
This would result in a single trace file called "trace-file-name.tr" that
|
|
2647 |
contains all of the trace events for both devices. The events would be
|
|
2648 |
disambiguated by trace context strings.
|
|
2649 |
|
|
2650 |
You can enable ascii tracing on a collection of node/net-device pairs by
|
|
2651 |
providing a ``NetDeviceContainer``. For each ``NetDevice`` in the container
|
|
2652 |
the type is checked. For each device of the proper type (the same type as is
|
|
2653 |
managed by the device helper), tracing is enabled. Again, the ``<Node>`` is
|
|
2654 |
implicit since the found net device must belong to exactly one ``Node``.
|
|
2655 |
For example,
|
|
2656 |
|
|
2657 |
::
|
|
2658 |
|
|
2659 |
NetDeviceContainer d = ...;
|
|
2660 |
...
|
|
2661 |
helper.EnableAscii ("prefix", d);
|
|
2662 |
|
|
2663 |
This would result in a number of ascii trace files being created, each of which
|
|
2664 |
follows the <prefix>-<node id>-<device id>.tr convention. Combining all of the
|
|
2665 |
traces into a single file is accomplished similarly to the examples above:
|
|
2666 |
|
|
2667 |
::
|
|
2668 |
|
|
2669 |
NetDeviceContainer d = ...;
|
|
2670 |
...
|
|
2671 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("trace-file-name.tr");
|
|
2672 |
...
|
|
2673 |
helper.EnableAscii (stream, d);
|
|
2674 |
|
|
2675 |
You can enable ascii tracing on a collection of node/net-device pairs by
|
|
2676 |
providing a ``NodeContainer``. For each ``Node`` in the ``NodeContainer``
|
|
2677 |
its attached ``NetDevices`` are iterated. For each ``NetDevice`` attached
|
|
2678 |
to each node in the container, the type of that device is checked. For each
|
|
2679 |
device of the proper type (the same type as is managed by the device helper),
|
|
2680 |
tracing is enabled.
|
|
2681 |
|
|
2682 |
::
|
|
2683 |
|
|
2684 |
NodeContainer n;
|
|
2685 |
...
|
|
2686 |
helper.EnableAscii ("prefix", n);
|
|
2687 |
|
|
2688 |
This would result in a number of ascii trace files being created, each of which
|
|
2689 |
follows the <prefix>-<node id>-<device id>.tr convention. Combining all of the
|
|
2690 |
traces into a single file is accomplished similarly to the examples above:
|
|
2691 |
|
|
2692 |
You can enable pcap tracing on the basis of node ID and device ID as well as
|
|
2693 |
with explicit ``Ptr``. Each ``Node`` in the system has an integer node ID
|
|
2694 |
and each device connected to a node has an integer device ID.
|
|
2695 |
|
|
2696 |
::
|
|
2697 |
|
|
2698 |
helper.EnableAscii ("prefix", 21, 1);
|
|
2699 |
|
|
2700 |
Of course, the traces can be combined into a single file as shown above.
|
|
2701 |
|
|
2702 |
Finally, you can enable pcap tracing for all devices in the system, with the
|
|
2703 |
same type as that managed by the device helper.
|
|
2704 |
|
|
2705 |
::
|
|
2706 |
|
|
2707 |
helper.EnableAsciiAll ("prefix");
|
|
2708 |
|
|
2709 |
This would result in a number of ascii trace files being created, one for
|
|
2710 |
every device in the system of the type managed by the helper. All of these
|
|
2711 |
files will follow the <prefix>-<node id>-<device id>.tr convention. Combining
|
|
2712 |
all of the traces into a single file is accomplished similarly to the examples
|
|
2713 |
above.
|
|
2714 |
|
|
2715 |
Ascii Tracing Device Helper Filename Selection
|
|
2716 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2717 |
|
|
2718 |
Implicit in the prefix-style method descriptions above is the construction of the
|
|
2719 |
complete filenames by the implementation method. By convention, ascii traces
|
|
2720 |
in the |ns3| system are of the form "<prefix>-<node id>-<device id>.tr"
|
|
2721 |
|
|
2722 |
As previously mentioned, every node in the system will have a system-assigned
|
|
2723 |
node id; and every device will have an interface index (also called a device id)
|
|
2724 |
relative to its node. By default, then, an ascii trace file created as a result
|
|
2725 |
of enabling tracing on the first device of node 21, using the prefix "prefix",
|
|
2726 |
would be "prefix-21-1.tr".
|
|
2727 |
|
|
2728 |
You can always use the |ns3| object name service to make this more clear.
|
|
2729 |
For example, if you use the object name service to assign the name "server"
|
|
2730 |
to node 21, the resulting ascii trace file name will automatically become,
|
|
2731 |
"prefix-server-1.tr" and if you also assign the name "eth0" to the
|
|
2732 |
device, your ascii trace file name will automatically pick this up and be called
|
|
2733 |
"prefix-server-eth0.tr".
|
|
2734 |
|
|
2735 |
Several of the methods have a default parameter called ``explicitFilename``.
|
|
2736 |
When set to true, this parameter disables the automatic filename completion
|
|
2737 |
mechanism and allows you to create an explicit filename. This option is only
|
|
2738 |
available in the methods which take a prefix and enable tracing on a single device.
|
|
2739 |
|
|
2740 |
Pcap Tracing Protocol Helpers
|
|
2741 |
+++++++++++++++++++++++++++++
|
|
2742 |
|
|
2743 |
The goal of these ``mixins`` is to make it easy to add a consistent pcap trace
|
|
2744 |
facility to protocols. We want all of the various flavors of pcap tracing to
|
|
2745 |
work the same across all protocols, so the methods of these helpers are
|
|
2746 |
inherited by stack helpers. Take a look at ``src/helper/trace-helper.h``
|
|
2747 |
if you want to follow the discussion while looking at real code.
|
|
2748 |
|
|
2749 |
In this section we will be illustrating the methods as applied to the protocol
|
|
2750 |
``Ipv4``. To specify traces in similar protocols, just substitute the
|
|
2751 |
appropriate type. For example, use a ``Ptr<Ipv6>`` instead of a
|
|
2752 |
``Ptr<Ipv4>`` and call ``EnablePcapIpv6`` instead of ``EnablePcapIpv4``.
|
|
2753 |
|
|
2754 |
The class ``PcapHelperForIpv4`` provides the high level functionality for
|
|
2755 |
using pcap tracing in the ``Ipv4`` protocol. Each protocol helper enabling these
|
|
2756 |
methods must implement a single virtual method inherited from this class. There
|
|
2757 |
will be a separate implementation for ``Ipv6``, for example, but the only
|
|
2758 |
difference will be in the method names and signatures. Different method names
|
|
2759 |
are required to disambiguate class ``Ipv4`` from ``Ipv6`` which are both
|
|
2760 |
derived from class ``Object``, and methods that share the same signature.
|
|
2761 |
|
|
2762 |
::
|
|
2763 |
|
|
2764 |
virtual void EnablePcapIpv4Internal (std::string prefix,
|
|
2765 |
Ptr<Ipv4> ipv4,
|
|
2766 |
uint32_t interface,
|
|
2767 |
bool explicitFilename) = 0;
|
|
2768 |
|
|
2769 |
The signature of this method reflects the protocol and interface-centric view
|
|
2770 |
of the situation at this level. All of the public methods inherited from class
|
|
2771 |
``PcapHelperForIpv4`` reduce to calling this single device-dependent
|
|
2772 |
implementation method. For example, the lowest level pcap method,
|
|
2773 |
|
|
2774 |
::
|
|
2775 |
|
|
2776 |
void EnablePcapIpv4 (std::string prefix, Ptr<Ipv4> ipv4, uint32_t interface, bool explicitFilename = false);
|
|
2777 |
|
|
2778 |
|
|
2779 |
will call the device implementation of ``EnablePcapIpv4Internal`` directly.
|
|
2780 |
All other public pcap tracing methods build on this implementation to provide
|
|
2781 |
additional user-level functionality. What this means to the user is that all
|
|
2782 |
protocol helpers in the system will have all of the pcap trace methods
|
|
2783 |
available; and these methods will all work in the same way across
|
|
2784 |
protocols if the helper implements ``EnablePcapIpv4Internal`` correctly.
|
|
2785 |
|
|
2786 |
Pcap Tracing Protocol Helper Methods
|
|
2787 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2788 |
|
|
2789 |
These methods are designed to be in one-to-one correspondence with the ``Node``-
|
|
2790 |
and ``NetDevice``- centric versions of the device versions. Instead of
|
|
2791 |
``Node`` and ``NetDevice`` pair constraints, we use protocol and interface
|
|
2792 |
constraints.
|
|
2793 |
|
|
2794 |
Note that just like in the device version, there are six methods:
|
|
2795 |
|
|
2796 |
::
|
|
2797 |
|
|
2798 |
void EnablePcapIpv4 (std::string prefix, Ptr<Ipv4> ipv4, uint32_t interface, bool explicitFilename = false);
|
|
2799 |
void EnablePcapIpv4 (std::string prefix, std::string ipv4Name, uint32_t interface, bool explicitFilename = false);
|
|
2800 |
void EnablePcapIpv4 (std::string prefix, Ipv4InterfaceContainer c);
|
|
2801 |
void EnablePcapIpv4 (std::string prefix, NodeContainer n);
|
|
2802 |
void EnablePcapIpv4 (std::string prefix, uint32_t nodeid, uint32_t interface, bool explicitFilename);
|
|
2803 |
void EnablePcapIpv4All (std::string prefix);
|
|
2804 |
|
|
2805 |
You are encouraged to peruse the Doxygen for class ``PcapHelperForIpv4``
|
|
2806 |
to find the details of these methods; but to summarize ...
|
|
2807 |
|
|
2808 |
You can enable pcap tracing on a particular protocol/interface pair by providing a
|
|
2809 |
``Ptr<Ipv4>`` and ``interface`` to an ``EnablePcap`` method. For example,
|
|
2810 |
|
|
2811 |
::
|
|
2812 |
|
|
2813 |
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
|
|
2814 |
...
|
|
2815 |
helper.EnablePcapIpv4 ("prefix", ipv4, 0);
|
|
2816 |
|
|
2817 |
You can enable pcap tracing on a particular node/net-device pair by providing a
|
|
2818 |
``std::string`` representing an object name service string to an
|
|
2819 |
``EnablePcap`` method. The ``Ptr<Ipv4>`` is looked up from the name
|
|
2820 |
string. For example,
|
|
2821 |
|
|
2822 |
::
|
|
2823 |
|
|
2824 |
Names::Add ("serverIPv4" ...);
|
|
2825 |
...
|
|
2826 |
helper.EnablePcapIpv4 ("prefix", "serverIpv4", 1);
|
|
2827 |
|
|
2828 |
You can enable pcap tracing on a collection of protocol/interface pairs by
|
|
2829 |
providing an ``Ipv4InterfaceContainer``. For each ``Ipv4`` / interface
|
|
2830 |
pair in the container the protocol type is checked. For each protocol of the
|
|
2831 |
proper type (the same type as is managed by the device helper), tracing is
|
|
2832 |
enabled for the corresponding interface. For example,
|
|
2833 |
|
|
2834 |
::
|
|
2835 |
|
|
2836 |
NodeContainer nodes;
|
|
2837 |
...
|
|
2838 |
NetDeviceContainer devices = deviceHelper.Install (nodes);
|
|
2839 |
...
|
|
2840 |
Ipv4AddressHelper ipv4;
|
|
2841 |
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
|
|
2842 |
Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);
|
|
2843 |
...
|
|
2844 |
helper.EnablePcapIpv4 ("prefix", interfaces);
|
|
2845 |
|
|
2846 |
You can enable pcap tracing on a collection of protocol/interface pairs by
|
|
2847 |
providing a ``NodeContainer``. For each ``Node`` in the ``NodeContainer``
|
|
2848 |
the appropriate protocol is found. For each protocol, its interfaces are
|
|
2849 |
enumerated and tracing is enabled on the resulting pairs. For example,
|
|
2850 |
|
|
2851 |
::
|
|
2852 |
|
|
2853 |
NodeContainer n;
|
|
2854 |
...
|
|
2855 |
helper.EnablePcapIpv4 ("prefix", n);
|
|
2856 |
|
|
2857 |
You can enable pcap tracing on the basis of node ID and interface as well. In
|
|
2858 |
this case, the node-id is translated to a ``Ptr<Node>`` and the appropriate
|
|
2859 |
protocol is looked up in the node. The resulting protocol and interface are
|
|
2860 |
used to specify the resulting trace source.
|
|
2861 |
|
|
2862 |
::
|
|
2863 |
|
|
2864 |
helper.EnablePcapIpv4 ("prefix", 21, 1);
|
|
2865 |
|
|
2866 |
Finally, you can enable pcap tracing for all interfaces in the system, with
|
|
2867 |
associated protocol being the same type as that managed by the device helper.
|
|
2868 |
|
|
2869 |
::
|
|
2870 |
|
|
2871 |
helper.EnablePcapIpv4All ("prefix");
|
|
2872 |
|
|
2873 |
Pcap Tracing Protocol Helper Filename Selection
|
|
2874 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2875 |
|
|
2876 |
Implicit in all of the method descriptions above is the construction of the
|
|
2877 |
complete filenames by the implementation method. By convention, pcap traces
|
|
2878 |
taken for devices in the |ns3| system are of the form
|
|
2879 |
"<prefix>-<node id>-<device id>.pcap". In the case of protocol traces,
|
|
2880 |
there is a one-to-one correspondence between protocols and ``Nodes``.
|
|
2881 |
This is because protocol ``Objects`` are aggregated to ``Node Objects``.
|
|
2882 |
Since there is no global protocol id in the system, we use the corresponding
|
|
2883 |
node id in file naming. Therefore there is a possibility for file name
|
|
2884 |
collisions in automatically chosen trace file names. For this reason, the
|
|
2885 |
file name convention is changed for protocol traces.
|
|
2886 |
|
|
2887 |
As previously mentioned, every node in the system will have a system-assigned
|
|
2888 |
node id. Since there is a one-to-one correspondence between protocol instances
|
|
2889 |
and node instances we use the node id. Each interface has an interface id
|
|
2890 |
relative to its protocol. We use the convention
|
|
2891 |
"<prefix>-n<node id>-i<interface id>.pcap" for trace file naming in protocol
|
|
2892 |
helpers.
|
|
2893 |
|
|
2894 |
Therefore, by default, a pcap trace file created as a result of enabling tracing
|
|
2895 |
on interface 1 of the Ipv4 protocol of node 21 using the prefix "prefix"
|
|
2896 |
would be "prefix-n21-i1.pcap".
|
|
2897 |
|
|
2898 |
You can always use the |ns3| object name service to make this more clear.
|
|
2899 |
For example, if you use the object name service to assign the name "serverIpv4"
|
|
2900 |
to the Ptr<Ipv4> on node 21, the resulting pcap trace file name will
|
|
2901 |
automatically become, "prefix-nserverIpv4-i1.pcap".
|
|
2902 |
|
|
2903 |
Several of the methods have a default parameter called ``explicitFilename``.
|
|
2904 |
When set to true, this parameter disables the automatic filename completion
|
|
2905 |
mechanism and allows you to create an explicit filename. This option is only
|
|
2906 |
available in the methods which take a prefix and enable tracing on a single device.
|
|
2907 |
|
|
2908 |
Ascii Tracing Protocol Helpers
|
|
2909 |
++++++++++++++++++++++++++++++
|
|
2910 |
|
|
2911 |
The behavior of the ascii trace helpers is substantially similar to the pcap
|
|
2912 |
case. Take a look at ``src/helper/trace-helper.h`` if you want to
|
|
2913 |
follow the discussion while looking at real code.
|
|
2914 |
|
|
2915 |
In this section we will be illustrating the methods as applied to the protocol
|
|
2916 |
``Ipv4``. To specify traces in similar protocols, just substitute the
|
|
2917 |
appropriate type. For example, use a ``Ptr<Ipv6>`` instead of a
|
|
2918 |
``Ptr<Ipv4>`` and call ``EnableAsciiIpv6`` instead of ``EnableAsciiIpv4``.
|
|
2919 |
|
|
2920 |
The class ``AsciiTraceHelperForIpv4`` adds the high level functionality
|
|
2921 |
for using ascii tracing to a protocol helper. Each protocol that enables these
|
|
2922 |
methods must implement a single virtual method inherited from this class.
|
|
2923 |
|
|
2924 |
::
|
|
2925 |
|
|
2926 |
virtual void EnableAsciiIpv4Internal (Ptr<OutputStreamWrapper> stream,
|
|
2927 |
std::string prefix,
|
|
2928 |
Ptr<Ipv4> ipv4,
|
|
2929 |
uint32_t interface,
|
|
2930 |
bool explicitFilename) = 0;
|
|
2931 |
|
|
2932 |
The signature of this method reflects the protocol- and interface-centric view
|
|
2933 |
of the situation at this level; and also the fact that the helper may be writing
|
|
2934 |
to a shared output stream. All of the public methods inherited from class
|
|
2935 |
``PcapAndAsciiTraceHelperForIpv4`` reduce to calling this single device-
|
|
2936 |
dependent implementation method. For example, the lowest level ascii trace
|
|
2937 |
methods,
|
|
2938 |
|
|
2939 |
::
|
|
2940 |
|
|
2941 |
void EnableAsciiIpv4 (std::string prefix, Ptr<Ipv4> ipv4, uint32_t interface, bool explicitFilename = false);
|
|
2942 |
void EnableAsciiIpv4 (Ptr<OutputStreamWrapper> stream, Ptr<Ipv4> ipv4, uint32_t interface);
|
|
2943 |
|
|
2944 |
|
|
2945 |
will call the device implementation of ``EnableAsciiIpv4Internal`` directly,
|
|
2946 |
providing either the prefix or the stream. All other public ascii tracing
|
|
2947 |
methods will build on these low-level functions to provide additional user-level
|
|
2948 |
functionality. What this means to the user is that all device helpers in the
|
|
2949 |
system will have all of the ascii trace methods available; and these methods
|
|
2950 |
will all work in the same way across protocols if the protocols implement
|
|
2951 |
``EnablAsciiIpv4Internal`` correctly.
|
|
2952 |
|
|
2953 |
Ascii Tracing Protocol Helper Methods
|
|
2954 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
2955 |
|
|
2956 |
::
|
|
2957 |
|
|
2958 |
void EnableAsciiIpv4 (std::string prefix, Ptr<Ipv4> ipv4, uint32_t interface, bool explicitFilename = false);
|
|
2959 |
void EnableAsciiIpv4 (Ptr<OutputStreamWrapper> stream, Ptr<Ipv4> ipv4, uint32_t interface);
|
|
2960 |
|
|
2961 |
void EnableAsciiIpv4 (std::string prefix, std::string ipv4Name, uint32_t interface, bool explicitFilename = false);
|
|
2962 |
void EnableAsciiIpv4 (Ptr<OutputStreamWrapper> stream, std::string ipv4Name, uint32_t interface);
|
|
2963 |
|
|
2964 |
void EnableAsciiIpv4 (std::string prefix, Ipv4InterfaceContainer c);
|
|
2965 |
void EnableAsciiIpv4 (Ptr<OutputStreamWrapper> stream, Ipv4InterfaceContainer c);
|
|
2966 |
|
|
2967 |
void EnableAsciiIpv4 (std::string prefix, NodeContainer n);
|
|
2968 |
void EnableAsciiIpv4 (Ptr<OutputStreamWrapper> stream, NodeContainer n);
|
|
2969 |
|
|
2970 |
void EnableAsciiIpv4All (std::string prefix);
|
|
2971 |
void EnableAsciiIpv4All (Ptr<OutputStreamWrapper> stream);
|
|
2972 |
|
|
2973 |
void EnableAsciiIpv4 (std::string prefix, uint32_t nodeid, uint32_t deviceid, bool explicitFilename);
|
|
2974 |
void EnableAsciiIpv4 (Ptr<OutputStreamWrapper> stream, uint32_t nodeid, uint32_t interface);
|
|
2975 |
|
|
2976 |
You are encouraged to peruse the Doxygen for class ``PcapAndAsciiHelperForIpv4``
|
|
2977 |
to find the details of these methods; but to summarize ...
|
|
2978 |
|
|
2979 |
There are twice as many methods available for ascii tracing as there were for
|
|
2980 |
pcap tracing. This is because, in addition to the pcap-style model where traces
|
|
2981 |
from each unique protocol/interface pair are written to a unique file, we
|
|
2982 |
support a model in which trace information for many protocol/interface pairs is
|
|
2983 |
written to a common file. This means that the <prefix>-n<node id>-<interface>
|
|
2984 |
file name generation mechanism is replaced by a mechanism to refer to a common
|
|
2985 |
file; and the number of API methods is doubled to allow all combinations.
|
|
2986 |
|
|
2987 |
Just as in pcap tracing, you can enable ascii tracing on a particular
|
|
2988 |
protocol/interface pair by providing a ``Ptr<Ipv4>`` and an ``interface``
|
|
2989 |
to an ``EnableAscii`` method.
|
|
2990 |
For example,
|
|
2991 |
|
|
2992 |
::
|
|
2993 |
|
|
2994 |
Ptr<Ipv4> ipv4;
|
|
2995 |
...
|
|
2996 |
helper.EnableAsciiIpv4 ("prefix", ipv4, 1);
|
|
2997 |
|
|
2998 |
In this case, no trace contexts are written to the ascii trace file since they
|
|
2999 |
would be redundant. The system will pick the file name to be created using
|
|
3000 |
the same rules as described in the pcap section, except that the file will
|
|
3001 |
have the suffix ".tr" instead of ".pcap".
|
|
3002 |
|
|
3003 |
If you want to enable ascii tracing on more than one interface and have all
|
|
3004 |
traces sent to a single file, you can do that as well by using an object to
|
|
3005 |
refer to a single file. We have already something similar to this in the
|
|
3006 |
"cwnd" example above:
|
|
3007 |
|
|
3008 |
::
|
|
3009 |
|
|
3010 |
Ptr<Ipv4> protocol1 = node1->GetObject<Ipv4> ();
|
|
3011 |
Ptr<Ipv4> protocol2 = node2->GetObject<Ipv4> ();
|
|
3012 |
...
|
|
3013 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("trace-file-name.tr");
|
|
3014 |
...
|
|
3015 |
helper.EnableAsciiIpv4 (stream, protocol1, 1);
|
|
3016 |
helper.EnableAsciiIpv4 (stream, protocol2, 1);
|
|
3017 |
|
|
3018 |
In this case, trace contexts are written to the ascii trace file since they
|
|
3019 |
are required to disambiguate traces from the two interfaces. Note that since
|
|
3020 |
the user is completely specifying the file name, the string should include the
|
|
3021 |
",tr" for consistency.
|
|
3022 |
|
|
3023 |
You can enable ascii tracing on a particular protocol by providing a
|
|
3024 |
``std::string`` representing an object name service string to an
|
|
3025 |
``EnablePcap`` method. The ``Ptr<Ipv4>`` is looked up from the name
|
|
3026 |
string. The ``<Node>`` in the resulting filenames is implicit since there
|
|
3027 |
is a one-to-one correspondence between protocol instances and nodes,
|
|
3028 |
For example,
|
|
3029 |
|
|
3030 |
::
|
|
3031 |
|
|
3032 |
Names::Add ("node1Ipv4" ...);
|
|
3033 |
Names::Add ("node2Ipv4" ...);
|
|
3034 |
...
|
|
3035 |
helper.EnableAsciiIpv4 ("prefix", "node1Ipv4", 1);
|
|
3036 |
helper.EnableAsciiIpv4 ("prefix", "node2Ipv4", 1);
|
|
3037 |
|
|
3038 |
This would result in two files named "prefix-nnode1Ipv4-i1.tr" and
|
|
3039 |
"prefix-nnode2Ipv4-i1.tr" with traces for each interface in the respective
|
|
3040 |
trace file. Since all of the EnableAscii functions are overloaded to take a
|
|
3041 |
stream wrapper, you can use that form as well:
|
|
3042 |
|
|
3043 |
::
|
|
3044 |
|
|
3045 |
Names::Add ("node1Ipv4" ...);
|
|
3046 |
Names::Add ("node2Ipv4" ...);
|
|
3047 |
...
|
|
3048 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("trace-file-name.tr");
|
|
3049 |
...
|
|
3050 |
helper.EnableAsciiIpv4 (stream, "node1Ipv4", 1);
|
|
3051 |
helper.EnableAsciiIpv4 (stream, "node2Ipv4", 1);
|
|
3052 |
|
|
3053 |
This would result in a single trace file called "trace-file-name.tr" that
|
|
3054 |
contains all of the trace events for both interfaces. The events would be
|
|
3055 |
disambiguated by trace context strings.
|
|
3056 |
|
|
3057 |
You can enable ascii tracing on a collection of protocol/interface pairs by
|
|
3058 |
providing an ``Ipv4InterfaceContainer``. For each protocol of the proper
|
|
3059 |
type (the same type as is managed by the device helper), tracing is enabled
|
|
3060 |
for the corresponding interface. Again, the ``<Node>`` is implicit since
|
|
3061 |
there is a one-to-one correspondence between each protocol and its node.
|
|
3062 |
For example,
|
|
3063 |
|
|
3064 |
::
|
|
3065 |
|
|
3066 |
NodeContainer nodes;
|
|
3067 |
...
|
|
3068 |
NetDeviceContainer devices = deviceHelper.Install (nodes);
|
|
3069 |
...
|
|
3070 |
Ipv4AddressHelper ipv4;
|
|
3071 |
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
|
|
3072 |
Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);
|
|
3073 |
...
|
|
3074 |
...
|
|
3075 |
helper.EnableAsciiIpv4 ("prefix", interfaces);
|
|
3076 |
|
|
3077 |
This would result in a number of ascii trace files being created, each of which
|
|
3078 |
follows the <prefix>-n<node id>-i<interface>.tr convention. Combining all of the
|
|
3079 |
traces into a single file is accomplished similarly to the examples above:
|
|
3080 |
|
|
3081 |
::
|
|
3082 |
|
|
3083 |
NodeContainer nodes;
|
|
3084 |
...
|
|
3085 |
NetDeviceContainer devices = deviceHelper.Install (nodes);
|
|
3086 |
...
|
|
3087 |
Ipv4AddressHelper ipv4;
|
|
3088 |
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
|
|
3089 |
Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);
|
|
3090 |
...
|
|
3091 |
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("trace-file-name.tr");
|
|
3092 |
...
|
|
3093 |
helper.EnableAsciiIpv4 (stream, interfaces);
|
|
3094 |
|
|
3095 |
You can enable ascii tracing on a collection of protocol/interface pairs by
|
|
3096 |
providing a ``NodeContainer``. For each ``Node`` in the ``NodeContainer``
|
|
3097 |
the appropriate protocol is found. For each protocol, its interfaces are
|
|
3098 |
enumerated and tracing is enabled on the resulting pairs. For example,
|
|
3099 |
|
|
3100 |
::
|
|
3101 |
|
|
3102 |
NodeContainer n;
|
|
3103 |
...
|
|
3104 |
helper.EnableAsciiIpv4 ("prefix", n);
|
|
3105 |
|
|
3106 |
This would result in a number of ascii trace files being created, each of which
|
|
3107 |
follows the <prefix>-<node id>-<device id>.tr convention. Combining all of the
|
|
3108 |
traces into a single file is accomplished similarly to the examples above:
|
|
3109 |
|
|
3110 |
You can enable pcap tracing on the basis of node ID and device ID as well. In
|
|
3111 |
this case, the node-id is translated to a ``Ptr<Node>`` and the appropriate
|
|
3112 |
protocol is looked up in the node. The resulting protocol and interface are
|
|
3113 |
used to specify the resulting trace source.
|
|
3114 |
|
|
3115 |
::
|
|
3116 |
|
|
3117 |
helper.EnableAsciiIpv4 ("prefix", 21, 1);
|
|
3118 |
|
|
3119 |
Of course, the traces can be combined into a single file as shown above.
|
|
3120 |
|
|
3121 |
Finally, you can enable ascii tracing for all interfaces in the system, with
|
|
3122 |
associated protocol being the same type as that managed by the device helper.
|
|
3123 |
|
|
3124 |
::
|
|
3125 |
|
|
3126 |
helper.EnableAsciiIpv4All ("prefix");
|
|
3127 |
|
|
3128 |
This would result in a number of ascii trace files being created, one for
|
|
3129 |
every interface in the system related to a protocol of the type managed by the
|
|
3130 |
helper. All of these files will follow the <prefix>-n<node id>-i<interface.tr
|
|
3131 |
convention. Combining all of the traces into a single file is accomplished
|
|
3132 |
similarly to the examples above.
|
|
3133 |
|
|
3134 |
Ascii Tracing Protocol Helper Filename Selection
|
|
3135 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
3136 |
|
|
3137 |
Implicit in the prefix-style method descriptions above is the construction of the
|
|
3138 |
complete filenames by the implementation method. By convention, ascii traces
|
|
3139 |
in the |ns3| system are of the form "<prefix>-<node id>-<device id>.tr"
|
|
3140 |
|
|
3141 |
As previously mentioned, every node in the system will have a system-assigned
|
|
3142 |
node id. Since there is a one-to-one correspondence between protocols and nodes
|
|
3143 |
we use to node-id to identify the protocol identity. Every interface on a
|
|
3144 |
given protocol will have an interface index (also called simply an interface)
|
|
3145 |
relative to its protocol. By default, then, an ascii trace file created as a result
|
|
3146 |
of enabling tracing on the first device of node 21, using the prefix "prefix",
|
|
3147 |
would be "prefix-n21-i1.tr". Use the prefix to disambiguate multiple protocols
|
|
3148 |
per node.
|
|
3149 |
|
|
3150 |
You can always use the |ns3| object name service to make this more clear.
|
|
3151 |
For example, if you use the object name service to assign the name "serverIpv4"
|
|
3152 |
to the protocol on node 21, and also specify interface one, the resulting ascii
|
|
3153 |
trace file name will automatically become, "prefix-nserverIpv4-1.tr".
|
|
3154 |
|
|
3155 |
Several of the methods have a default parameter called ``explicitFilename``.
|
|
3156 |
When set to true, this parameter disables the automatic filename completion
|
|
3157 |
mechanism and allows you to create an explicit filename. This option is only
|
|
3158 |
available in the methods which take a prefix and enable tracing on a single device.
|
|
3159 |
|
|
3160 |
Summary
|
|
3161 |
*******
|
|
3162 |
|
|
3163 |
|ns3| includes an extremely rich environment allowing users at several
|
|
3164 |
levels to customize the kinds of information that can be extracted from
|
|
3165 |
simulations.
|
|
3166 |
|
|
3167 |
There are high-level helper functions that allow users to simply control the
|
|
3168 |
collection of pre-defined outputs to a fine granularity. There are mid-level
|
|
3169 |
helper functions to allow more sophisticated users to customize how information
|
|
3170 |
is extracted and saved; and there are low-level core functions to allow expert
|
|
3171 |
users to alter the system to present new and previously unexported information
|
|
3172 |
in a way that will be immediately accessible to users at higher levels.
|
|
3173 |
|
|
3174 |
This is a very comprehensive system, and we realize that it is a lot to
|
|
3175 |
digest, especially for new users or those not intimately familiar with C++
|
|
3176 |
and its idioms. We do consider the tracing system a very important part of
|
|
3177 |
|ns3| and so recommend becoming as familiar as possible with it. It is
|
|
3178 |
probably the case that understanding the rest of the |ns3| system will
|
|
3179 |
be quite simple once you have mastered the tracing system
|