ns-3 复盘思考¶
做了几个月的计算机网络实验,感觉之前对ns-3的理解并不到位,于是我花了点时间从源理解整个ns-3包的构建&运行原理
前置技能¶
Git¶
Complex software systems need some way to manage the organization and changes to the underlying code and documentation. There are many ways to perform this feat, and you may have heard of some of the systems that are currently used to do this. Until recently, the ns-3 project used Mercurial as its source code management system, but in December 2018, switch to using Git. Although you do not need to know much about Git in order to complete this tutorial, we recommend becoming familiar with Git and using it to access the source code.
CMake工作原理¶
Once you have source code downloaded to your local system, you will need to compile that source to produce usable programs. Just as in the case of source code management, there are many tools available to perform this function. Probably the most well known of these tools is make
. Along with being the most well known, make
is probably the most difficult to use in a very large and highly configurable system. Because of this, many alternatives have been developed.
The build system CMake is used on the ns-3 project.
C++¶
As mentioned above, scripting in ns-3 is done in C++ or Python. Most of the ns-3 API is available in Python, but the models are written in C++ in either case. A working knowledge of C++ and object-oriented concepts is assumed in this document.
Socket Programming¶
We will assume a basic facility with the Berkeley Sockets API in the examples used in this tutorial. If you are new to sockets, we recommend reviewing the API and some common usage cases. For a good overview of programming TCP/IP sockets we recommend TCP/IP Sockets in C, Donahoo and Calvert.
You can get an online version on Google Library at TCP / IP Sockets in C
ns-3的运行原理¶
ns-3 is built as a system of software libraries that work together. User programs can be written that links with (or imports from) these libraries. User programs are written in either the C++ or Python programming languages.
ns-3 is distributed as source code, meaning that the target system needs to have a software development environment to build the libraries first, then build the user program. ns-3 could in principle be distributed as pre-built libraries for selected systems, and in the future it may be distributed that way.
构建命令¶
To build a specific target such as test-runner
we use the following ns3 command:
Bash | |
---|---|
1 |
|
corresponding to :
Bash | |
---|---|
1 2 |
|
运行命令¶
Bash | |
---|---|
1 |
|
corresponding to :
Bash | |
---|---|
1 2 3 4 5 6 |
|
Note:
the command above would fail if
./ns3 build
was not executed first, since the examples won’t be built by the test-runner target.
运行脚本¶
We typically run scripts under the control of ns3. This allows the build system to ensure that the shared library paths are set correctly and that the libraries are available at run time. To run a program, simply use the --run
option in ns3. Let’s run the ns-3 equivalent of the ubiquitous hello world program by typing the following:
提供运行参数¶
To feed command line arguments to an ns-3 program use this pattern:
Bash | |
---|---|
1 |
|
Debug¶
To run ns-3 programs under the control of another utility, such as a debugger (e.g. gdb
) or memory checker (e.g. valgrind
), you use a similar --command-template="..."
form.
For example, to run your ns-3 program hello-simulator
with the arguments <args>
under the gdb
debugger:
Bash | |
---|---|
1 |
|
设计理念¶
In this section, we’ll review some terms that are commonly used in networking, but have a specific meaning in ns-3.
Node¶
In Internet jargon, a computing device that connects to a network is called a host or sometimes an end system. Because ns-3 is a network simulator, not specifically an Internet simulator, we intentionally do not use the term host since it is closely associated with the Internet and its protocols. Instead, we use a more generic term also used by other simulators that originates in Graph Theory — the node.
In ns-3 the basic computing device abstraction is called the node. This abstraction is represented in C++ by the class Node
. The Node
class provides methods for managing the representations of computing devices in simulations.
You should think of a Node
as a computer to which you will add functionality. One adds things like applications, protocol stacks and peripheral cards with their associated drivers to enable the computer to do useful work. We use the same basic model in ns-3.
Application¶
Typically, computer software is divided into two broad classes. System Software organizes various computer resources such as memory, processor cycles, disk, network, etc., according to some computing model. System software usually does not use those resources to complete tasks that directly benefit a user. A user would typically run an application that acquires and uses the resources controlled by the system software to accomplish some goal.
Often, the line of separation between system and application software is made at the privilege level change that happens in operating system traps. In ns-3 there is no real concept of operating system and especially no concept of privilege levels or system calls. We do, however, have the idea of an application. Just as software applications run on computers to perform tasks in the “real world,” ns-3 applications run on ns-3 Nodes
to drive simulations in the simulated world.
In ns-3 the basic abstraction for a user program that generates some activity to be simulated is the application. This abstraction is represented in C++ by the class Application
. The Application
class provides methods for managing the representations of our version of user-level applications in simulations. Developers are expected to specialize the Application
class in the object-oriented programming sense to create new applications. In this tutorial, we will use specializations of class Application
called UdpEchoClientApplication
and UdpEchoServerApplication
. As you might expect, these applications compose a client/server application set used to generate and echo simulated network packets
Channel¶
In the real world, one can connect a computer to a network. Often the media over which data flows in these networks are called channels. When you connect your Ethernet cable to the plug in the wall, you are connecting your computer to an Ethernet communication channel. In the simulated world of ns-3, one connects a Node
to an object representing a communication channel. Here the basic communication subnetwork abstraction is called the channel and is represented in C++ by the class Channel
.
The Channel
class provides methods for managing communication subnetwork objects and connecting nodes to them. Channels
may also be specialized by developers in the object oriented programming sense. A Channel
specialization may model something as simple as a wire. The specialized Channel
can also model things as complicated as a large Ethernet switch, or three-dimensional space full of obstructions in the case of wireless networks.
We will use specialized versions of the Channel
called CsmaChannel
, PointToPointChannel
and WifiChannel
in this tutorial. The CsmaChannel
, for example, models a version of a communication subnetwork that implements a carrier sense multiple access communication medium. This gives us Ethernet-like functionality.
Net Device¶
It used to be the case that if you wanted to connect a computer to a network, you had to buy a specific kind of network cable and a hardware device called (in PC terminology) a peripheral card that needed to be installed in your computer. If the peripheral card implemented some networking function, they were called Network Interface Cards, or NICs. Today most computers come with the network interface hardware built in and users don’t see these building blocks.
A NIC will not work without a software driver to control the hardware. In Unix (or Linux), a piece of peripheral hardware is classified as a device. Devices are controlled using device drivers, and network devices (NICs) are controlled using network device drivers collectively known as net devices. In Unix and Linux you refer to these net devices by names such as eth0.
In ns-3 the net device abstraction covers both the software driver and the simulated hardware. A net device is “installed” in a Node
in order to enable the Node
to communicate with other Nodes
in the simulation via Channels
. Just as in a real computer, a Node
may be connected to more than one Channel
via multiple NetDevices
.
The net device abstraction is represented in C++ by the class NetDevice
. The NetDevice
class provides methods for managing connections to Node
and Channel
objects; and may be specialized by developers in the object-oriented programming sense. We will use the several specialized versions of the NetDevice
called CsmaNetDevice
, PointToPointNetDevice
, and WifiNetDevice
in this tutorial. Just as an Ethernet NIC is designed to work with an Ethernet network, the CsmaNetDevice
is designed to work with a CsmaChannel
; the PointToPointNetDevice
is designed to work with a PointToPointChannel
and a WifiNetDevice
is designed to work with a WifiChannel
.
Topology Helpers¶
In a real network, you will find host computers with added (or built-in) NICs. In ns-3 we would say that you will find Nodes
with attached NetDevices
. In a large simulated network you will need to arrange many connections between Nodes
, NetDevices
and Channels
.
Since connecting NetDevices
to Nodes
, NetDevices
to Channels
, assigning IP addresses, etc., are such common tasks in ns-3, we provide what we call topology helpers to make this as easy as possible. For example, it may take many distinct ns-3 core operations to create a NetDevice, add a MAC address, install that net device on a Node
, configure the node’s protocol stack, and then connect the NetDevice
to a Channel
. Even more operations would be required to connect multiple devices onto multipoint channels and then to connect individual networks together into internetworks. We provide topology helper objects that combine those many distinct operations into an easy to use model for your convenience.
实例分析¶
- Here we take first.cc as an example.
- Pay attention to the differences between Helper and Container !
C++ | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|