Network simulation techniques
OUTLINE
• Theoretical aspects
– why do we need network simulation?
– structure of a simulator
– discrete-event simulation
– data collection and analysis
– simulation with a given accuracy
– variance reduction techniques
• Practical aspects: ns2
– ns2 overview;
– ns2 architecture;
– simulation procedure;
– support tools: nam and xgraph;
– examples: bottleneck, routing and 802.11 WLANs.
D.Moltchanov, TUT, 2012
Lecture: Network simulation 2
Network simulation techniques D.Moltchanov, TUT, 2012
Analyze communication networks: two ways
• analytical analysis;
• simulation studies.
Example: analysis of queuing system:
• modeling of arrival process as a stochastic process;
• modeling of service time are a stochastic process;
• representation of interactions of these processes as an another process.
What is important for analytical analysis:
• often analytical analysis is too complicated or even not possible;
• even if possible, it requires restrictive assumptions.
Lecture: Network simulation 3
Network simulation techniques
GI/G/1 queuing system.
D.Moltchanov, TUT, 2012
...
...
unlimited # of waiting positions server arrivals
Figure 1: Graphical representation of GI/G/1 queuing system.
System is characterized by:
• arbitrary i.i.d. distributed interarrival times (GI);
• arbitrary i.i.d. distributed service times (G);
• single server;
• FCFS queuing discipline.
Lecture: Network simulation 4
Network simulation techniques
What should be noted:
• there are no analytical solution for GI/G/1 queue;
• what should we do?
D.Moltchanov, TUT, 2012
Approximate solution can be derived for:
• overloaded GI/G/1 system ( λ → µ );
• unloaded GI/G/1 system ( λ << µ );
• results in terms of bounds.
Notes on GI/G/1 queue:
• GI/G/1 does not capture correlation between arrivals;
• arrival processes in real networks are often correlated;
• simulation is the only suitable way in this case.
Lecture: Network simulation 5
Network simulation techniques
vs.
Simulation of a queue consists of:
• representation of the arrival process as a stochastic one;
• representation of service time as a stochastic process;
• representation of interoperation as a stochastic process;
• simulation run;
• collection of data;
• statistical analysis of obtained data;
• drawing conclusions.
D.Moltchanov, TUT, 2012
Differences between analytical analysis and simulations:
• incorporation of simulation execution;
• incorporation of data collection and analysis.
Lecture: Network simulation 6
Network simulation techniques D.Moltchanov, TUT, 2012
Advantages:
• does not require restrictive assumption or require less of them;
• model structure, algorithms and variable may be changed quickly;
• models of traffic and service times can be used as is just from measurements;
• may provide results which are not obtainable using analytical techniques.
Shortcomings:
• time consuming, it may take much more time that analytic;
• computationally expensive;
• validation of results take additional time;
• results may be inaccurate when time of analysis is not sufficient;
• simulations may be of higher complexity than required;
• relationship between variables is hard to visualize and explain;
• sensitivity analysis is difficult.
Lecture: Network simulation 7
Network simulation techniques
We have the following components:
• simulation engine (core);
• a collection of models (link, buffer, etc.);
• frontend (interface);
• preprocessing tools (hidden for a user);
• data postprocessing tools.
Engine is the most important:
• specifies how to handle simulations;
• everything is handled in uniform way;
• does not matter what you simulate.
Lecture: Network simulation
D.Moltchanov, TUT, 2012
8
Network simulation techniques D.Moltchanov, TUT, 2012
Continuous simulations:
• real time is used: time increments as fine as possible: to capture all state changes.
Discrete simulations:
• system is observed at discrete times t
0
, t
0
+ ∆ t, t
0
+ 2∆ t, . . .
.
Discrete event simulations:
• we focus on system changes only at event times;
• after processing the current event, the system clock is forwarded to another event
• simulation moves from the current system state to the event occurring next;
• processing of the event may create additional events;
• event list which is updated for the system.
Lecture: Network simulation 9
Network simulation techniques
t
0
t
1
D.Moltchanov, TUT, 2012 t
3 t
0 t
1
t
2 t
3 t
4 t
5
...
t
0 t
1 t
2 t
3 t
4 t
5 t
6 t
7
Figure 2: Illustration of different types of simulations.
Lecture: Network simulation 10
Network simulation techniques
Lecture: Network simulation
D.Moltchanov, TUT, 2012
11
Network simulation techniques D.Moltchanov, TUT, 2012
Simulation program can be implemented using:
• general purpose programming languages:
– C: suitable for one purpose simulations;
– C++: suitable for writing multiple purposes simulations.
+: very flexible languages;
− : there are no simulation specific functions and error detection.
• simulation oriented programming languages:
– OPNET: all networks;
– OMNET++: all networks;
– Qualnet: very good for cellular networks;
– ns2: all networks:
+: reliable approach;
− : sometimes not fast (incorporate a lot of functionality;)
− : sometimes limited to specific functionality;
− : one more language to learn.
Lecture: Network simulation 12
Network simulation techniques
The basic idea:
• only events change the state of the system;
• no need to track state of the system between events.
D.Moltchanov, TUT, 2012
The whole system consists of the following components:
• system under consideration:
– process under considerations: product of other processes and RVs.
• system state:
– state of the stochastic process.
• simulation clock:
– tells the occurrence time of the next event.
• event list:
– holder for events: consider it as a two dimensional vector: time and event.
Lecture: Network simulation 13
Network simulation techniques D.Moltchanov, TUT, 2012
The idea of the event list is illustrated in the following figure:
• T i
, i = 1 , 2 , . . .
, are event times;
• E i
, i = 1 , 2 , . . .
, are corresponding events.
E
1
T
1
E
2
T
2
E
3
T
3
...
E i
T i
...
Figure 3: The idea of the event list.
Events are identified by event time and event type .
There are two general types of events:
• basic events:
– these events modify the state of the system : arrivals/departures of customers.
• additional events:
– these are events needed by additional tasks of simulation: run, stoppage, collection of data.
Lecture: Network simulation 14
Network simulation techniques D.Moltchanov, TUT, 2012
General algorithm:
• initialization procedure:
– system clock should be set to zero;
– system state is assigned an initial value;
– generate list of the event and place the next event to this list.
• handling of events during a simulation:
– system clock should be set to the occurrence time of the first (next) event in event list;
– handle the event making all appropriate actions associated with the event;
– update the system state.
• stop of simulation.
Note that the following is not included:
• storing of statistical data;
• statistical analysis of obtained data.
Lecture: Network simulation 15
Network simulation techniques D.Moltchanov, TUT, 2012
Time advance methods in discrete-event simulations:
• event-advance method;
• unit-advance method.
unit-time advance: t
0 t
1 event advance: t
2 t
3 t
4 t
5 t
0 t
1 t
3
Figure 4: Time advance techniques in discrete-event simulations.
Lecture: Network simulation 16
Network simulation techniques D.Moltchanov, TUT, 2012
Future event list: collection of events that occur in the future.
What type of operation are performed in a list:
• locating the next event and its type:
– required when advancing the time.
• deleting the event:
– required when the events has already been treated.
• inserting the event in a list:
– required when generating new event;
– required when basic event generates conditional events.
Basically, there are two ways of organizing a list:
• using sequential array;
• using linked list.
Lecture: Network simulation 17
Network simulation techniques D.Moltchanov, TUT, 2012
The approach: all future event times are stored sequentially in array.
How to implement:
• associate each event type with a certain integer i ;
• clock associated with this event is stored in the i th position in array.
Example: we need N positions in array:
• clock value of the type 1 event is stored in 1st position;
• clock value of the type 2 event is stored in 2nd position;
• . . .
• clock value of the type N event is store in N th position;
Figure 5: Using sequential array as a future event list.
Lecture: Network simulation 18
Network simulation techniques
How to find the next event:
• locate the smallest value in array of N elements.
D.Moltchanov, TUT, 2012
Example: find the smallest element and its index in array E [ i ], i = 0 , 1 , . . . , N − 1:
• variable smallest returns the smallest element;
• variable index returns the index of the smallest element.
smallest = E[0]; index = 0;
} for (i=1; i<N; i++) { if (E[i] < smallest) { smallest = E[i]; index = i;
}
Lecture: Network simulation 19
Network simulation techniques D.Moltchanov, TUT, 2012
How to deal with other functions:
• deletion: set the value of the clock associated with event of type i to very big one;
– not deleted physically!
• insertion: update the value of the clock associated with event of type i .
– not inserted physically!
Advantages:
• insertion and deletion are made very easily;
• location of the next event depend on the number of event types:
– complexity is linear in time.
Shortcoming:
• if the number of event types is large location is time consuming;
• in this case different organization of the future event list should be considered.
Lecture: Network simulation 20
Network simulation techniques D.Moltchanov, TUT, 2012
The idea: to access data elements in correct order
• we store data element;
• we store pointer to the next element.
Definitions:
• pointer is referred to as link ;
• data element and the link(s) are referred to as node .
Example:
• node consists of two elements: one is data elements, the next one is link;
• link F points to the first element; link of the last node is set to zero.
Figure 6: Using linked list as a future event list.
Lecture: Network simulation 21
Network simulation techniques D.Moltchanov, TUT, 2012
Implementation example: arrange integer numbers in T in ascending order:
• create new array P of the same dimension;
• the content of P ( i ) is the link to location in T containing element larger than T ( i );
• example: P (1) = 7, next larger after T (1) is T (7)...
Notes:
• F (= 5) is the first element;
• node: location in T and corresponding location in P .
Figure 7: Locating values in a singly linked list.
Lecture: Network simulation 22
Network simulation techniques D.Moltchanov, TUT, 2012
Locating element: we want to locate number 10 in T :
• using pointer F we check the value stored in the first node: ( T (5) , P (5));
• using pointer P (5) we locate the second node: ( T (3) , P (3));
• using pointer P (3) we locate the third node: ( T (1) , P (1));
• pointer P (1) contains the address we are looking for;
• general: if we know address of the first node we can locate any element.
Note: we can only move forward using singly linked link!
Figure 8: Deletion of elements in a singly linked list.
Lecture: Network simulation 23
Network simulation techniques D.Moltchanov, TUT, 2012
Deletion: we want to delete number 10:
• change the value of the pointer of the previous node;
• previous node: ( T (1) , P (1)) = (5 , 4);
• node containing 10 has the pointer P (7) = 4;
• to delete: set P (1) = P (7) = 4.
Note: information is not physically deleted, just not accessible!
Figure 9: Deletion of elements in a singly linked list.
Lecture: Network simulation 24
Network simulation techniques D.Moltchanov, TUT, 2012
Insertion: we want to insert number 6:
• locate two nodes between which we should put 6;
• starting from the first node we find them as: (5 , 7) and (10 , 4) (see previous slide);
• get unused location in T : T (2);
• set P (1) = 2 to go from T (1) = 5 to T (2) = 6;
• set T (2) = 6, P (2) = 7 to go from T (2) = 6 to T (7) = 10.
Figure 10: Insertion of elements in a singly linked list.
Lecture: Network simulation 25
Network simulation techniques
We have to be able:
• organize data elements and pointers into nodes;
• access node using pointer;
• create and delete nodes.
D.Moltchanov, TUT, 2012
There are two ways:
• use built-in commands to carry these operations (if any);
• set-up your own storage scheme.
Notes:
• must be long enough to accommodate all events that might be generated;
• all unused nodes must be linked together to form a pool of free nodes.
Lecture: Network simulation 26
Network simulation techniques D.Moltchanov, TUT, 2012
General notes:
• nodes are organized such that CL i
< CL j
< · · · < CL n
;
• the next event is given by pointer F (first node);
• when the event occurred and processed it should be deleted;
• if conditional events are generated they are place in linked list.
Advantages:
• location and deletion is made easily:
– using linked lists in simulation we have to delete only the first node!
Shortcomings
• we have to use location procedure to insert an event in a linked list;
• search of the linked lists might be time consuming if the number of event types is large;
• solution: use better location procedure (e.g. use pointer pointing to the middle of the list)!
Lecture: Network simulation 27
Network simulation techniques
The main problem of the singly linked lists:
• we can go only forward!
Doubly linked lists:
• link two successive node with two pointers;
• we can go forward and backward!
Note: there are some advantages for specific applications.
D.Moltchanov, TUT, 2012
Figure 11: Example of the doubly linked list.
Lecture: Network simulation 28
Network simulation techniques
How to carry out a simulation:
• set the initial state;
• start the simulation;
• obtain simulation results for some time...
We distinguish between:
• transient simulations (results for warm-up period);
• steady-state simulation (results for steady-state period).
N ( t ) warm-up
D.Moltchanov, TUT, 2012 steady-state
T
W
T t
Lecture: Network simulation 29
Network simulation techniques D.Moltchanov, TUT, 2012
What is the reason:
• analyze problems associated with a specific initial state;
– T
W is a function of the initial conditions;
– this might give some hints how to start a certain system...
– example: which initial state faster leads to steady-state.
• analyze transient state itself:
– a certain system may not have a steady-state at all;
– example: M/M/1 queuing system with λ/µ = ρ > 1.
M / M /1
0
Lecture: Network simulation
1 lambda/mu
30
Network simulation techniques D.Moltchanov, TUT, 2012
What is the reason:
• typical application of simulations;
• simulation has to run long enough to get away from the transient state ( T
W
).
Note: T
W is a function of the initial conditions.
How to choose initial condition:
• assume no activities in the system prior to time 0;
– no particular reasons for that;
– as good as any other choice of the initial state.
• set to some typical state of the system:
– should be as representative as possible;
– a-priori knowledge of the system is required;
– advantage: may significantly shorten the length of the transient period, T
W
.
Lecture: Network simulation 31
Network simulation techniques D.Moltchanov, TUT, 2012
Transient period: may deteriorate statistics:
• how to remove transient period;
• how to determine the length of the transient period.
What are approaches to deal with:
• use very long simulation runs such that (meanwhile, how long is ’long enough’ ?):
– the amount of data in transient period is small relative to those of steady-state;
– time-consuming approach.
• no data collection during transient period:
– simulate till the steady-state is reached;
– clear all data accumulated up to this point;
– continue to get enough statistical data.
Hint for both approaches: set the initial state to what is expected in the long run.
Lecture: Network simulation 32
Network simulation techniques D.Moltchanov, TUT, 2012
There are three methods to detect T
W
:
• method of trials;
• moving-average method;
• blind guess using time-series...
Method of trials:
• try out different transient periods: T
1
, T
2
, . . . , T k
, T
1
< T
2
< . . . , < T k
;
• compile steady-state statistics for each run;
• choose T i such that for all j > i statistics do not change significantly.
Moving-average method:
• compute moving average as time progresses: R i
= P i j = i − r
N j
;
– where N j is some metric of interest, r is the range of moving averages.
• steady-state when R i no longer changes significantly over time.
Lecture: Network simulation 33
Network simulation techniques D.Moltchanov, TUT, 2012
How to estimate the mean:
• use point estimator;
• estimate confidence intervals.
Confidence interval for the mean:
P r
ˆ
[ X ] − z
α/ 2
√ s
N
≤ µ ≤
ˆ
[ X ] + z
α/ 2
√ s
N
≈ 1 − α,
Note: interval [ ˆ [ X ] − z
α/ 2
√ s
N
,
ˆ
[ X ] + z
α/ 2
√ s
N
] is 100(1 − α )% confidence interval for µ .
(1) a
2 a
2
Lecture: Network simulation 34
Network simulation techniques D.Moltchanov, TUT, 2012
What is the problem with the following expression?
P r
ˆ
[ X ] − z
α/ 2
√ s
N
≤ µ ≤
ˆ
[ X ] + z
α/ 2
√ s
N
• we have to estimate sample mean and sample variance;
• point estimator of the variance is given by: s
2
=
1
N − 1
N
X
( X i
−
ˆ
[ X ])
2
, i =0
ˆ
[ X ] =
1
N
N
X
X i
, i =0
≈ 1 − α,
• X i
, i = 1 , 2 , . . . , N must be independent for ˆ 2 to be unbiased!
• quite often observations are dependent (correlated, in particular):
– example: waiting time of the packet i depends on the waiting time of packet ( i − 1)!
(2)
(3)
The main problem!
how to remove dependence between observations?
Lecture: Network simulation 35
Network simulation techniques
The following approaches have been proposed:
• use of autocorrelation function:
– tries to explicitly take correlation into account.
• batch means method;
– tries to remove correlation from observations.
• replication method;
– tries to remove correlation from observations.
• regenerative method.
– tries to remove correlation from observations.
D.Moltchanov, TUT, 2012
Note the following:
• when sample size is really large the effect of dependence can be negligible;
• use any of above methods when sample size is not really huge.
Lecture: Network simulation 36
Network simulation techniques D.Moltchanov, TUT, 2012
How to estimate the variance:
• idea: take into account the effect of correlation explicitly ( σ 2
X + Y
= σ 2
X
+ σ 2
Y
+ 2 Cov
XY
);
• get the sample X
1
, X
2
, . . . , X
N
;
• compute autocorrelation coefficients R ( i ), i = 0 , 1 , . . . , k ;
• the variance can now be estimated as: s
2 s
2
X
N/ 4
1 + 2
X k =1
1 − k
N
R ( k )
.
(4)
• ˆ
2
X is the point estimator of the variance assuming no serial correlation: s
2
X
=
1
N − 1
N
X
( X i i =1
−
ˆ
[ X ])
2
.
(5)
Lecture: Network simulation 37
Network simulation techniques
N
t
D.Moltchanov, TUT, 2012
...
T
W
Figure 12: Correlation between successive batches.
Do the following:
• let ˆ [ X i
] be the sample mean of the batch i , i = 1 , 2 , . . . , k ;
• if b is large: ˆ [ X
1
] ,
ˆ
[ X
2
] , . . . ,
ˆ
[ X k
] are approximately uncorrelated and normally distributed.
Lecture: Network simulation 38
Network simulation techniques D.Moltchanov, TUT, 2012
Given that
ˆ
[ X
1
] ,
ˆ
[ X
2
] , . . . ,
ˆ
[ X k
] are uncorrelated and normally distributed:
ˆ
[ X ] =
1 k k
X i =1
ˆ
[ X i
] , s
2
=
1 k − 1 k
X
( ˆ [ X i
] −
ˆ
[ X ])
2
.
i =1
Thus, for large k , k > 30 we get the following confidential intervals:
ˆ
[ X ] − 1 .
96 s
√
ˆ k
,
ˆ
[ X ] + 1 .
96 s
√ k
• construct confidential intervals using t distribution.
.
Important notes:
• if b is not large enough, this computation gives biased estimates;
• estimate of b : plot ACF of X
1
, X
2
, . . . , X
N
:
– b should be approximately 5 times greater than the last significant correlation.
(6)
(7)
Lecture: Network simulation 39
Network simulation techniques D.Moltchanov, TUT, 2012
The idea: replicate simulation run several times:
• make n runs each resulting in m observations:
( X
11
, X
12
, . . . , X
1 m
) , ( X
21
, X
22
, . . . , X
2 m
) , . . . , ( X n 1
, X n 2
, . . . , X nm
) .
• for each sample compute the sample mean using point estimator of the mean:
ˆ
[ X i
] =
1 m n
X x j =1
• treat sample means as a sample of independent observations:
ˆ
[ X ] =
1 n n
X i =1
ˆ
[ X i
] , ˆ
2
=
1 n − 1 n
X
( ˆ [ X i
] −
ˆ
[ X ])
2
.
i =1
Classic problems:
• determine the length of each simulation run;
• determine the length of the transient period.
(8)
(9)
(10)
Lecture: Network simulation 40
Network simulation techniques D.Moltchanov, TUT, 2012
Comparison of methods:
• batch means:
– obtain approximately independent samples from a single run.
• method of replications:
– obtain strictly independent samples from multiples runs;
• regenerative method:
– obtain strictly independent samples from a single run;
– application is limited to systems with specific behavior.
Where it is used:
• simulation of queue-related problems.
Lecture: Network simulation 41
Network simulation techniques D.Moltchanov, TUT, 2012
Two cases are possible:
• trajectories are all random: estimation may have no sense;
• trajectories are somewhat similar.
How to estimate:
• the transient state depends on initial conditions;
• the only way to get independent realizations of X is to use method of replications;
Repeating experiments:
• must start with the same initial condition;
• must use different random numbers (different seeds);
• we have to know the length of transient period.
Lecture: Network simulation 42
Network simulation techniques D.Moltchanov, TUT, 2012
Two inverse tasks:
• we considered: what are the confidence intervals given N observation:
– is it OK to say that mean is 50 ± 30?
• what we are asked: provide a kind of assurance:
– say what would be the values with confidence ± 3...
• question: how many experiments are needed to achieve that?
General notes:
• recall, width of confidence intervals is proportional to 1 /
√
N ;
ˆ
[ X ] − z
α/ 2
√ s
N
,
ˆ
[ X ] + z
α/ 2
√ s
N
– N : number of iid observations;
– the larger N the smaller is the interval.
• to halve the confidence interval: increase N four times.
.
(11)
Lecture: Network simulation 43
Network simulation techniques D.Moltchanov, TUT, 2012
Observe that:
• we do not know how many observations are needed:
– what accuracy may N experiments give?
– e.g. how many observations are needed to get 50 ± 3?
Two techniques:
• Solution 1: use of pilot experiments:
– aim 1: to get overall idea how things go;
– aim 2: obtain rough estimate of N providing required accuracy.
• Solution 2: sequential ’in-simulation’ checking:
– test is carried out periodically to check whether required accuracy is achieved.
Can be applied to:
• batch mean method;
• method of replications.
Lecture: Network simulation 44
Network simulation techniques D.Moltchanov, TUT, 2012
Use of pilot experiments:
• we want to estimate statistics a with the intervals ± 0 .
1ˆ :
• pilot experiments is carried out to collect N
1 replications;
• let ˆ
1 be a point estimator of a ;
• let ∆
1 be the width of confidence intervals;
• check the following:
– if ∆
1
≤ 0 .
1 ˆ
1 we stop;
– if ∆
1
> 0 .
1 ˆ
1 we carry out main simulation with N
2
= (∆
1
/ 0 .
1 ˆ
1
)
2
N
1 replications;
• we obtain new ˆ
2 and ∆
2
:
– they may not be exactly what we wanted (0 .
1 ˆ
2
) due to randomness.
• if ∆
1
> 0 .
1 ˆ
1 carry out new attempt.
Lecture: Network simulation 45
Network simulation techniques D.Moltchanov, TUT, 2012
Sequential ’in-simulations’ checking:
• we want to estimate statistics a with the intervals ± 0 .
1ˆ :
• gather k
?
batches;
• estimate ˆ
1 and ∆
1
;
• check the following:
– if ∆
1
≤ 0 .
1 ˆ
1 we stop;
– if ∆
1
> 0 .
1 ˆ
1 gather k
?
additional batches.
• calculate new ˆ
2 and ∆
2 based on total 2 k ?
batches;
• check the following:
– if ∆
2
≤ 0 .
1 ˆ
2 we stop;
– if ∆
2
> 0 .
1 ˆ
2 gather k
?
additional batches.
• repeat...
Lecture: Network simulation 46
Network simulation techniques D.Moltchanov, TUT, 2012
Suppose we have to estimate mean given N iid observations:
• point estimate of the mean is:
ˆ
[ X ] =
1
N
N
X
X i
, i =1
• confidence intervals for the mean are given by:
ˆ
[ X ] − z
α/ 2
√ s
N
,
ˆ
[ X ] + z
α/ 2
√ s
N
– where ˆ
2 is the estimate of the variance.
.
How to shorten the confidence intervals:
• accuracy of an estimate can be increased by increasing the number of observations;
– shortcoming: may require very long simulations.
• accuracy can also be achieved by reducing variance .
(12)
(13)
Lecture: Network simulation 47
Network simulation techniques D.Moltchanov, TUT, 2012
Lecture: Network simulation 48
Network simulation techniques
Free simulators
• ns2/ns3: www.isi.edu/nsnam/ns;
• OMNET++: www.omnetpp.org
• QualNet: www.scalable-networks.com
• ITGuru: www.opnet.com
Commercial:
• OPNET: www.opnet.com
• a plenty of others....
Note: ITGuru is an academic edition of OPNET.
D.Moltchanov, TUT, 2012
Lecture: Network simulation 49
Network simulation techniques
Characteristics:
• discrete-event engine;
• network simulator;
• basically TCP/IP networks;
• wired and wireless components included.
Intended usage:
• research;
• development;
• education.
Developing:
• research institutes and universities;
• freely distributed and open source.
Lecture: Network simulation
D.Moltchanov, TUT, 2012
50
Network simulation techniques D.Moltchanov, TUT, 2012
Briefly:
• 1989: REAL simulator by UCB;
• 1990: ns1: LBL;
• 1995: ns2 DARPA VINT project (Virtual InterNet Testbed)
• 2011: ns3.
Current status:
• ns2 runs on:
– almost all UNIX and Linux and Win 95/98/2000/XP.
• last stable version: 2.35 released in Nov. 4, 2011
– roughly 1 release in 6 months + daily snapshots.
• http://www.isi.edu/nsnam/ns/ or http://nsnam.isi.edu/nsnam/index.php/Main Page
• > 200000 line of codes; > 1000 institutions; > 10000 users; > 400 pages of short manual;
• ns3 project started in July 2006, http://www.nsnam.org/
Lecture: Network simulation 51
Network simulation techniques
ns2 distribution consists of:
• ns2 itself;
• nam: Network AniMator
– visualizing ns2 output;
– GUI for simple scenarios.
• Mandatory support tools:
– tcl/tk: script language
– otcl: object-oriented tcl;
– TclCL: tcl library.
• Pre-processing tools:
– traffic, topology generators, converters.
• Post-processing tools:
– trace analysis: awk, sed, perl, tcl.
– very simple and not recommended.
Lecture: Network simulation
D.Moltchanov, TUT, 2012
52
Network simulation techniques D.Moltchanov, TUT, 2012
Differs for:
• Unix/Linux;
• Windows.
Two types are available:
• Via compilation;
– get ’all-in-one package’;
– get pieces and then compile.
• Obtaining binaries.
– easiest way to run on Win platform.
Hints and notes:
• to compile on Windows you need Cygwin (Unix emulation);
• Size of ’all-in-one’ is around 320Mb (v2.29).
Lecture: Network simulation 53
Network simulation techniques D.Moltchanov, TUT, 2012
Building from pieces (Unix/Linux):
• get Otcl, TclCL and ns2;
• unpack in some temp folder at the same level;
• cd into the OTcl directory;
• run ./configure;
• run make;
• cd into the TclCL directory;
• run ./configure;
• run make;
• cd into the ns directory;
• run ./configure;
• run make;
• Verify that it built correctly and runs by running ./validate.
Note: ns2 ’all-in-one’ contains ’install’ script (just run it).
Lecture: Network simulation 54
Network simulation techniques D.Moltchanov, TUT, 2012
Documentation:
• mailing list: ns-users@isi.edu
– to get in send message to the address with ”subscribe ns-users” in the body;
– to browse archive go to http://www.isi.edu/nsnam/ns/.
• ns manual:
– available at http://www.isi.edu/nsnam/ns/;
– is only a short reference guide.
• tutorials:
– a number is available on http://www.isi.edu/nsnam/ns/;
– FAQ: http://www.isi.edu/nsnam/ns/ns-faq.html;
– de-facto tutorial: http://www.isi.edu/nsnam/ns/tutorial/index.html;
– installation and bug fixes: http://www.isi.edu/nsnam/ns/ns-problems.html;
– E. Altman, T. Jimenez: http://www-sop.inria.fr/maestro/personnel/Eitan.Altman/ns.htm.
Lecture: Network simulation 55
Network simulation techniques
Object-oriented structure:
• advantage: code reuse;
• shortcomings: performance.
Software structure:
• uses two languages to separate control and processing:
– C++: packet processing;
– Otcl: control.
• Packet processing:
– C++ makes it fast and detailed;
– C++ makes it scalable.
• Control:
– Otcl makes it easy to create scenarios;
– Otcl makes it easy to understand third party scripts.
D.Moltchanov, TUT, 2012
Lecture: Network simulation 56
Network simulation techniques D.Moltchanov, TUT, 2012
Scalability:
• per-packet processing must be fast;
• separating control and packet handling.
Extensibility:
• must be easy to add new objects;
• object trees to understand hierarchy:
– in C++;
– in Otcl.
• C++ and Otcl trees are split:
– if not needed nothing have to be changed at a certain level;
– helps a lot!
• Otcl class hierarchy: http://www-sop.inria.fr/planete/software/ns-doc/ns-current/;
• C++ class hierarchy: http://www.isi.edu/nsnam/nsdoc-classes/index.html.
Lecture: Network simulation 57
Network simulation techniques
D.Moltchanov, TUT, 2012
Lecture: Network simulation 58
Network simulation techniques D.Moltchanov, TUT, 2012
Tcl language:
• semantics is similar to perl;
• one can check tutorial at: http://www.msen.com/%7Eclif/TclTutor.html;
• real programming language to create network topology;
• tcl is used by Otcl to construct advanced objects.
Tcl contains:
• lists, arrays, associative arrays etc.;
• procedures and functions;
• flow controls: if, while, for, etc.
Lecture: Network simulation 59
Network simulation techniques D.Moltchanov, TUT, 2012
Lecture: Network simulation 60
Network simulation techniques
D.Moltchanov, TUT, 2012
• codes can be reused (for example, different version of TCP).
Lecture: Network simulation 61
Network simulation techniques
Where to find:
• C++:
– /whereYouInstalled/ns-(ver)/
– where (ver) is your version of ns2 (e.g. 2.1b9a).
• Otcl:
– /whereYouInstalled/ns-(ver)/tcl/lib/
∗ ns-default.tcl: default values for ns2 objects;
∗ other Otcl definitions.
– /whereYouInstalled/ns-(ver)/tcl/
∗ specialized objects in subdirs.
D.Moltchanov, TUT, 2012
Lecture: Network simulation 62
Network simulation techniques
Basic steps:
• Create the simulation environment;
– event list, scheduler, etc.;
• Create the network:
– nodes and links between them.
• Create connections:
– TCP, UDP (in some sense).
• Create applications:
– CBR flow, FTP, WWW traffic.
• Trace network elements:
– trace queue, trace flow.
Lecture: Network simulation
D.Moltchanov, TUT, 2012
63
Network simulation techniques D.Moltchanov, TUT, 2012
The following are the common commands:
• Create event scheduler:
– set ns [new Simulator];
• Schedule events:
– $ns at < time > < event > ;
• Start scheduler:
– $ns run.
• Stop and close everything:
– $ns at $x ”exit”.
– where $x is some instant of time.
Note: before closing you must take additional actions:
• flush all traces to files;
• close all files.
Lecture: Network simulation 64
Network simulation techniques D.Moltchanov, TUT, 2012
Create new generator:
• set rng [new RNG];
• $rng seed 0.
RNs from other distributions:
• using class RNG:
– uniform: $rng uniform a b.
• using class RandomVariable:
– distributions: uniform, exponential, hyperexponential, Pareto;
Lecture: Network simulation 65
Network simulation techniques D.Moltchanov, TUT, 2012
Nodes:
• set n0 [$ns node].
Links and queuing:
• $ns duplex-link $n0 $n1 < bandwidth > < delay > < queue type > ;
• < queue type > : DropTail, RED, CBQ, FQ, SFQ, DRR;
• example: link with 10 Mbps, 10 ms delay, buffer size 100, RED buffer control
Lecture: Network simulation 66
Network simulation techniques
Creating UDP flow
D.Moltchanov, TUT, 2012
In one command:
• $ns create-connection < src type > < src node > < dst type > < dst node > < packet class > ;
• example: $ns create-connection UDP $n0 Null $n1 1.
Other sources:
• TCP (we will consider);
• RTP source, RTCP source.
• TCP for wireless links.
Lecture: Network simulation 67
Network simulation techniques
Creating TCP flow
D.Moltchanov, TUT, 2012
In one command:
• $ns create-connection < src type > < src node > < dst type > < dst node > < packet class > ;
• example: $ns create-connection TCP $n0 TCPSink $n1 1.
Some included TCP versions:
• TCP: Tahoe TCP (slow start and AIMD);
• TCP/Reno: Reno TCP (... + fast retransmit/fast recovery);
• TCP/NewReno: TCP Reno with improved fast retransmit;
• TCP/Sack: TCP SACK (Selective ACK).
Lecture: Network simulation 68
Network simulation techniques
CBR:
• Constant bit rate;
• set src [new Application/Traffic/CBR].
Exponential or Pareto ON/OFF:
• on/off times are exponentially distributed;
• set src [new Application/Traffic/Exponential];
• set src [new Application/Traffic/Pareto].
Connecting to transport:
• $udp defined earlier;
• $src attach-agent $udp.
Lecture: Network simulation
D.Moltchanov, TUT, 2012
69
Network simulation techniques ns2 includes support for traffic traces:
D.Moltchanov, TUT, 2012
What < file > should look like:
• each record consist of two 32 bit field;
• inter-packet time (msec) and packet size (in bytes).
For example: there is converter for MPEG frame size file to ns2.
Lecture: Network simulation 70
Network simulation techniques
FTP:
• set ftp [new Application/FTP];
• attaching to TCP: $ftp attach-agent $tcp.
Telnet:
• set telnet [new Application/Telnet];
• attaching to TCP: $telnet attach-agent $tcp.
D.Moltchanov, TUT, 2012
Lecture: Network simulation 71
Network simulation techniques D.Moltchanov, TUT, 2012
Starting and stopping times scheduled as events to the scheduler:
• $ns at < time > < event > .
Starting:
• $ns at 1.0 ”$ftp start”;
• sends infinitely long;
• the same for CBR, telnet and on/off sources.
Stopping:
• $ns at 5.0 ”$ftp stop”;
• the same for CBR, telnet and on/off sources.
Sending for example 1000 packets:
• $ns at 7.0 ”$ftp produce 1000”;
• works for FTP only.
Lecture: Network simulation 72
Network simulation techniques
Trace packets on all links of the network:
• $ns trace-all [open test.out w].
Tracing on specific links:
• $ns trace-queue $n0 $n1.
D.Moltchanov, TUT, 2012
Lecture: Network simulation 73
Network simulation techniques D.Moltchanov, TUT, 2012
Queue monitor:
• set qmon [$ns monitor-queue $n0 $n1];
• for packet arrivals and drops:
– set parr [$qmon set parrivals ];
– set drops [$qmon set pdrops ].
Flow monitor:
• enable flow monitoring:
– set fmon [$nssim makeflowmon Fid];
– $nssim attach-fmon [$nssim link $n0 $n1] $fmon.
• count arrivals and drops for flow with id xx:
– set fclassifier [$fmon classifier];
– set flow1 [$fclassifier lookup auto 0 0 xx];
– set parr [$flow1 set parrivals ]; set pdrops [$flow1 set pdrops ].
Lecture: Network simulation 74
Network simulation techniques
D.Moltchanov, TUT, 2012
Lecture: Network simulation 75
Network simulation techniques
ns2 also provides:
• errors on the data-link layer;
• LAN and WLAN;
• Routing;
• Multicasting;
• Mobile IP;
• DiffServ;
• MPLS;
• . . .
D.Moltchanov, TUT, 2012
Lecture: Network simulation 76
Network simulation techniques
Nam (Network AniMator):
• packet-level animation;
• almost integrated with ns2.
D.Moltchanov, TUT, 2012 controls node link packet time
Lecture: Network simulation 77
Network simulation techniques D.Moltchanov, TUT, 2012
Lecture: Network simulation 78
Network simulation techniques
Xgraph allows to make graphs:
• frequently used with ns2;
• simple visualizer.
D.Moltchanov, TUT, 2012
Lecture: Network simulation 79
Network simulation techniques D.Moltchanov, TUT, 2012
Environment is the same!
# Create a simulator object set ns [new Simulator]
# Open the nam trace file set nf [open out.nam w]
$ns namtrace-all $nf
}
# Define a 'finish' procedure proc finish {} { global ns nf
$ns flush-trace close $nf exec nam out.nam & exit 0
BODY
# Call the finish procedure after 5 seconds simulation time
$ns at 5.0 "finish"
# Run the simulation
$ns run
Lecture: Network simulation 80
Network simulation techniques
Let’s create four nodes: set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
D.Moltchanov, TUT, 2012
Duplex links between them with droptail queuing:
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 1Mb 10ms DropTail
Re-layout topology for good-looking in nam:
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
Lecture: Network simulation 81
Network simulation techniques
What we get in nam:
D.Moltchanov, TUT, 2012
What is next:
• two UDP agents with CBR sources at n0 and n1;
• null agent at node n3.
Lecture: Network simulation 82
Network simulation techniques D.Moltchanov, TUT, 2012
We use these commands:
#Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a UDP agent and attach it to node n1 set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
# Create a CBR traffic source and attach it to udp1 set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
# Create a Null agent and attach it to node n3 set null0 [new Agent/Null]
$ns attach-agent $n3 $nul
Lecture: Network simulation 83
Network simulation techniques
Connect two CBR agents to the Null agent:
$ns connect $udp0 $null0
$ns connect $udp1 $null0
...
D.Moltchanov, TUT, 2012
Schedule their sending times:
$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
Classic bottleneck scenario:
• both sources send 200 pps with size 500 bytes;
• n0 to n2: 0 .
8 Mbps, n1 to n2: 0 .
8 Mbps;
• link between n2 and n3 is 1 Mbps.
Questions: which packets get lost? how many? what is the effect of queuing?
Lecture: Network simulation 84
Network simulation techniques
Set class to UDP sources first:
$udp0 set class_ 1
$udp1 set class_ 2
Set colors to flows:
$ns color 1 Blue
$ns color 2 Red
D.Moltchanov, TUT, 2012
Lecture: Network simulation 85
Network simulation techniques D.Moltchanov, TUT, 2012
Add queue monitor:
$ns duplex-link-op $n2 $n3 queuePos 0.5
Everything is droptail on that queue!
Lecture: Network simulation 86
Network simulation techniques D.Moltchanov, TUT, 2012
Why not to add some fairness with stochastic fair queuing (SFQ):
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
Everything is SFQ on that queue!
Example: http://www.isi.edu/nsnam/ns/tutorial/examples/example2.tcl
Lecture: Network simulation 87
Network simulation techniques
Create 8 nodes:
D.Moltchanov, TUT, 2012
} for {set i 0} {$i < 7} {incr i} { set n($i) [$ns node]
Create circular topology:
} for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
Lecture: Network simulation 88
Network simulation techniques D.Moltchanov, TUT, 2012
Lecture: Network simulation 89
Network simulation techniques D.Moltchanov, TUT, 2012
Send some data from n0 to n3:
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0 set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
By default traffic goes over shortest path n0-n1-n2-n3!
Lecture: Network simulation 90
Network simulation techniques D.Moltchanov, TUT, 2012
Introducing link failure:
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
Lecture: Network simulation 91
Network simulation techniques D.Moltchanov, TUT, 2012
Introduce destination vector (DV) routing to tackle the problem:
.
$ns rtproto DV
Example: http://www.isi.edu/nsnam/ns/tutorial/examples/example3.tcl
Lecture: Network simulation 92
Network simulation techniques D.Moltchanov, TUT, 2012
What we consider:
• simple 2-nodes wireless scenario;
• nodes move within the area 500 m × 500 m ;
• nodes start out initially at two opposite ends of the boundary;
• nodes move towards each other and then move away;
• TCP connection is setup between the two nodes;
• packets are exchanged as nodes come within hearing range of each other;
• as nodes move away, packets start getting dropped.
We start with simple template:
• just as usual - some ’must do’ things;
• http://www.isi.edu/nsnam/ns/tutorial/examples/simple-wireless.tcl
Lecture: Network simulation 93
Network simulation techniques D.Moltchanov, TUT, 2012
Array ’val()’ is used to set basic parameters:
• link layer (LL);
• interface queue (IfQ);
• MAC layer;
• wireless channel nodes transmit and receive signals from...
# ======================================================================
# Define options
# ====================================================================== set val(chan) Channel/WirelessChannel ;# channel type set val(prop) set val(ant) set val(ll) set val(ifq) set val(ifqlen)
Propagation/TwoRayGround
Antenna/OmniAntenna
LL
Queue/DropTail/PriQueue
50
;# radio-propagation model
;# Antenna type
;# Link layer type
;# Interface queue type
;# max packet in ifq set val(netif) set val(mac) set val(rp) set val(nn)
Phy/WirelessPhy
Mac/802_11
DSDV
2
;# network interface type
;# MAC type
;# ad-hoc routing protocol
;# number of mobile nodes
Lecture: Network simulation 94
Network simulation techniques D.Moltchanov, TUT, 2012
Create an instance of the simulator: set ns_ [new Simulator]
Setup trace supportand call the procedure ’trace-all’: set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
Create a topology object that keeps track of movements: set topo [new Topography]
Provide the topography object with x and y coordinates:
$topo load_flatgrid 500 500
Next we create the object God (general operations director), as follows: create-god $val(n
• keeps track of wireless environment.
Lecture: Network simulation 95
Network simulation techniques D.Moltchanov, TUT, 2012
We have to configure nodes before creating them providing e.g.:
• type of adressing (flat or hierarchial or...);
• routing protocol, LL, IfQ, MAC, etc.
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# $ns_ node-config -addressingType flat or hierarchical or expanded
# -adhocRouting DSDV or DSR or TORA
-llType
-macType
-propType
-ifqType
LL
Mac/802_11
"Propagation/TwoRayGround"
"Queue/DropTail/PriQueue"
-ifqLen
-phyType
-antType
50
"Phy/WirelessPhy"
"Antenna/OmniAntenna"
-channelType "Channel/WirelessChannel"
-topoInstance $topo
-energyModel "EnergyModel"
-initialEnergy (in Joules)
-rxPower (in W)
-txPower (in W)
-agentTrace ON or OFF
-routerTrace
-macTrace
ON or OFF
ON or OFF
-movementTrace ON or OFF
Lecture: Network simulation 96
Network simulation techniques D.Moltchanov, TUT, 2012
We use the following configuration:
# Configure nodes
$ns_ node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
Then we create two nodes disabling random movement:
} for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node ]
$node_($i) random-motion 0 ;# disable random motion
Lecture: Network simulation 97
Network simulation techniques D.Moltchanov, TUT, 2012
Give initial positions of nodes:
# Provide initial (X,Y, for now Z=0) co-ordinates for node_(0) and node_(1)
#
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0
Setting movement of nodes:
#
# Node_(1) starts to move towards node_(0)
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"
# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0" ns at 50.0 ”node (1) setdest 25.0 20.0 15.0”: at 50.0s, it moves to (x=25,y=20) at 15m/s.
Lecture: Network simulation 98
Network simulation techniques D.Moltchanov, TUT, 2012
Setup traffic flow between nodes: set tcp [new Agent/TCP]
$tcp set class_ 2 set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(1) $sink
$ns_ connect $tcp $sink set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start"
Setting up time when simulation ends and reset to initial states: for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0001 "stop"
$ns_ at 150.0002 "puts \"NS EXITING...\" ; $ns_
} halt" proc stop {} { global ns_ tracefd close $tracefd
Lecture: Network simulation 99
Network simulation techniques D.Moltchanov, TUT, 2012
Example: http://www.isi.edu/nsnam/ns/tutorial/examples/simple-wireless.tcl
What actually happens:
• TCP flow starting at 10.0s from node0;
• no packets are exchanged as nodes too far apart;
• around 81.0s the routing info begins to be exchanged between both the nodes;
• around 100.0s we see the first TCP packet being received by node1;
• the connection breaks down again around time 116.0s.
Note the following:
• all wireless traces starts with WL in their first field;
• we enabled DSDV so we have routing messages.
Lecture: Network simulation 100