NS-3 Study notes 2

Source: Internet
Author: User
Tags get ip time interval port number

Today begins with the 4th chapter of Ns-3 Tutorial. 4. Basic Concepts

Before reading or writing a ns-3 program, you need to understand some concepts and abstract questions.
It's being recommended taking the time to read through this section just to ensure you ' re starting on a firm foundation. 4.1 Critical Concepts (key abstractions)

Introduce some common words in several networks, they have specific meanings in NS3.
1.node (node)
NS3 is not limited to Internet impersonation, so node is used to represent a host or terminal in a network, rather than using host directly. node is similar to the concept of "point" in graph theory. The basic computing device is called node in NS-3, and the abstract concept is described in C + + class node. There are several methods and properties in the node class that govern the behavior of computing devices.
You may already be thinking about how to give node applications, stacks, peripherals so that the device works, but this is something you can think about later, but it's all based on the node model.
2.Application (application)
Computer software is usually divided into: system software and application software. The direct segmentation lines are often based on changes in system privilege levels. There is no concept of OS in NS-3, there is no concept of privilege level or system invocation, but there is the concept of application application. Just as an application running on a computer in the real world performs a task, the applications running on NS3 nodes in the simulated world also performs a simulation.
In NS-3, a user program that generates a simulation activity is abstracted as a application. This abstraction is described by the C + + class application. The application class contains user-level actions to perform impersonation activities. In this paper, we use special application class--udpechoclientapplication and Udpechoserverapplication class to form a C/S analog network.
3.Channel (channel)
The network medium for transmitting data (cable/wireless channel, etc.) is called channels. In NS-3, connect a node to a communication channel just as you would in reality connect your computer's network cable to a wall Ethernet slot. In NS-3, the basic communication subnet (physical layer and link layer) is abstracted into channel and described by C + + channel.
The Channel class provides a series of methods for managing communication subnet objects and connection nodes. Users can write their own specific channel classes, such as a cable, based on the basic channel class. The specific channel can also be a large Ethernet switch or a barrier-free three-dimensional channel space for wireless networks.
This article uses specific channel classes Csmachannel, Pointtopointchannel, and Wifichannel, which are typical dynamic access-sharing channels, point-to-point channels, and WiFi channels that use Ethernet technology.
4.Net Device (network devices)
Usually connected to a host to the network, we need to buy cable and network card (NICS), network cards such devices called Device,device need to work under the driver driver support. In NS-3, the concept of net device includes software dirver and hardware device. A net device is installed on node so that node can communicate with other node via channel.
NS3. NET device is abstracted as a C + + class Netdevice, which provides methods to manage the connection of a node object to a channel object.
This article uses a special network device class Csmanetdevice,pointtopointnetdevice and Wifinetdevice (that is, the Ethernet card class/Point-to-dot Nic class/wifi Nic Class).
5.Topology Helpers (topology tool)
In the real world, network devices are installed in the host, and node is connected to the net device in NS-3. In larger network simulations, many connections to nodes, netdevices, and channels may need to be scheduled, which requires a topology tool to plan the network form.
In NS-3, connecting node with Netdevice, Netdevice and channel, specifying IP addresses, and so on, requires invoking the topology tool topology helpers, which makes the task easier. For example: Build network device, add MAC address, install Device to node, configure node protocol stack, connect Netdevice to channel, etc.

In addition, the above C + + classes are in Ns-allinone-3.26/ns-3.26/src/network/model. 4.2 First NS-3 script

After downloading ns-allinone-3.26, you will see the following in ns-3.26:

Switch work environment to examples/tutorial, you will see a file called first.cc. This file is a simple peer-to network simulation program that connects two nodes and sends a packet (ECHO) between nodes. Now let's learn first.cc.

/*-*-mode:c++; C-file-style: "GNU"; Indent-tabs-mode:nil; -*-*/* * This program was free software;  Can redistribute it and/or modify * it under the terms of the GNU general public License version 2 as * published by
 The free software Foundation; * * This program was distributed in the hope that it'll be useful, * but without any WARRANTY;  Without even the implied warranty of * merchantability or FITNESS for A particular PURPOSE.
 See the * GNU general public License for more details. * * You should has received a copy of the GNU general public License * along with this program; If not, write to the free software * Foundation, Inc., Temple Place, Suite, Boston, MA 02111-1307 USA */#incl Ude "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/

Point-to-point-module.h "#include" ns3/applications-module.h "using namespace ns3;

Ns_log_component_define ("Firstscriptexample");
int main (int argc, char *argv[]) {  CommandLine cmd; Cmd.

  Parse (argc, argv);
  Time::setresolution (Time::ns);
  Logcomponentenable ("Udpechoclientapplication", log_level_info);

  Logcomponentenable ("Udpechoserverapplication", log_level_info);
  Nodecontainer nodes; Nodes.

  Create (2);
  Pointtopointhelper Pointtopoint;
  Pointtopoint.setdeviceattribute ("Datarate", StringValue ("5Mbps"));

  Pointtopoint.setchannelattribute ("Delay", StringValue ("2ms"));
  Netdevicecontainer devices;

  devices = Pointtopoint.install (nodes);
  Internetstackhelper Stack; Stack.

  Install (nodes);
  Ipv4addresshelper address; Address.

  SetBase ("10.1.1.0", "255.255.255.0"); Ipv4interfacecontainer interfaces = address.

  Assign (Devices);

  Udpechoserverhelper Echoserver (9); Applicationcontainer Serverapps = Echoserver.install (nodes.
  Get (1));
  Serverapps.start (Seconds (1.0));

  Serverapps.stop (Seconds (10.0)); Udpechoclienthelper echoclient (interfaces.
  GetAddress (1), 9); Echoclient.setattribute ("Maxpackets", UinteGervalue (1));
  Echoclient.setattribute ("Interval", TimeValue (Seconds (1.0)));

  Echoclient.setattribute ("PacketSize", Uintegervalue (1024)); Applicationcontainer ClientApps = Echoclient.install (nodes.
  Get (0));
  Clientapps.start (Seconds (2.0));

  Clientapps.stop (Seconds (10.0));
  Simulator::run ();
  Simulator::D Estroy ();
return 0; }

1. The program template (boilerplate)
Line 1th is an Emacs style line that tells the information about coding styles such as Emacs format conversion,
/*-*-mode:c++; C-file-style: "GNU"; Indent-tabs-mode:nil; -*-*/
If you want to participate in the development of NS-3, you have to follow the relevant coding style specification (specifically in doc/codingstd.txt). This is recommended in all NS-3 programs, especially with the Emacs editor. The
Line 2nd to 15th is a description of the GNU General Public License used by NS-3.
2. The module refers to the
第17-21 line, which is a C + + reference to the Include statement for other modules, indicating that these libraries are used in first.cc: Core-module, Network-module, Internet-module, Point-to-point-module, Application-module. By referencing these existing libraries, we can quickly build our own programs without having to reinvent the wheel. These header files are placed under build/ns3/, and if we open one of the above network-module.h take a look, you'll see something like this:

#ifdef ns3_module_compilation
# error "Do not include NS3 MODULE aggregator headers from other modules; These is meant only for 
end user scripts. "
#endif
#ifndef ns3_module_network  
//MODULE headers:
#include "address-utils.h"
#include "address.h "
#include" application-container.h "
#include" application.h "
#include" ascii-file.h "
#include" Ascii-test.h "
#include" buffer.h "
#include" byte-tag-list.h "
#include" channel-list.h "
# Include "Channel.h"
#include "chunk.h"
#include "crc32.h"
#include "data-rate.h
" ...

This header file contains a number of specific network key files, which is not to say that first.cc will use all the libraries, just to write programs, although not very economical. Core-module.h contains all header files in Src/core, Network-module.h contains all header files in Src/network internet-module.h contains all header files in src/internet Point-to-point-module.h contains all header files in Src/point-to-point application-module.h contains all header files in src/application
After compiling, these header files will be placed in the BUILD/NS3 or Build/debug or build/optimized directory, depending on the version and your configuration, I am here in build/ns3.

3.NS3 Namespaces
Line 23rd declares the namespace using NS3. The NS-3 program is within this space. This ns3 space is separate from the global namespace and helps to integrate with other code. Here, the using statement is used to introduce NS3 into the current global declaration domain.
4. Log
Line 25th Sets the log file parameters. Ns_log_component_define ("xxx") sets the relevant parameters, NS3 will create a log component named Firstscriptexample to record console messages.
5. Main function
From 27 lines to the end, it is the main function content.

int main (int argc, char *argv[]) declares the function
Time::setresolution (Time::ns), set the setresolution (timing precision) to nanosecond, nanosecond is the smallest unit of time in NS3, you can change the set to a larger unit of timing. If not set, the default is nanoseconds.

The next two lines of script open two log components, respectively, in the Echo client and echo Server application:

Logcomponentenable ("Udpechoclientapplication", log_level_info);
Logcomponentenable ("Udpechoserverapplication", log_level_info);

6. Topology Tools
The following two lines are used to generate the Ns-3 node object, which represents the computer for this simulation.

Nodecontainer nodes;
Nodes. Create (2);

(1) The Nodecontainer class is a topology tool class that provides a convenient way to build, manage, and Access node objects. The first sentence above is to declare a Nodecontainer object nodes, and then use its Creat method to generate two node objects. Now that node is not working, we need to connect them to a network, the simplest of which is a point-to-point network of two machines.

(2) The Pointtopointhelper class is also a topology tool class, which is used to build a point-to-point network. Recall the concept of net device and channel, where we need to use point-to-point net device and channel. Defined as follows:

Pointtopointhelper Pointtopoint;
Pointtopoint.setdeviceattribute ("Datarate", StringValue ("5Mbps"));
Pointtopoint.setchannelattribute ("Delay", StringValue ("2ms"));

Property Datarate is a property in the Pointtopointnetdevice object, which can be found in the help, and there are several device properties that can be set. For example: mac48address, ptr< pointtopointchannel >, ptr< Packet >, ptr< Node > and many more.

(3) Netdevicecontainer is one of the third topology tool classes. Its objects are used to manage network devices, which are generated, configured, and installed (installed on node) by Pointtopointhelper objects.

Netdevicecontainer devices;
devices = Pointtopoint.install (nodes); This method takes the Nodecontainer object as a parameter, and for each node that the Nodcontainer object nodes contains, a point-to-point network device is generated and saved in the Netdevicecontainer object devices. A Pointtopointchannel object is generated, and two pointtopointdevices are connected.

After the Pointtopoint.install () method is executed, we will have two nodes, each with a point-to-dot network card and a point-to-line channel connecting them, and the device and channel will be configured with the parameters configured above.
(4) Internetstackhelper is a tool class for easy installation and configuration of the management Internet Protocol stack.

Internetstackhelper Stack;
Stack. Install (nodes);//After execution, the Internet stack with protocols such as TCP, IP, and UDP will be installed on each node in the nodes.

(5) Ipv4addresshelper is a tool class that helps us set IP addresses.

Ipv4addresshelper address;
Address. SetBase ("10.1.1.0", "255.255.255.0");//Set an IP network number and subnet mask, which means that the IP address of this network is from 10.1.1.0~10.1.1.255.

The underlying implementation of NS3 will record all assigned IP addresses, and if duplicate generation of the same IP address segment (using the SetBase method), a fatal error will be reported, which would be difficult to debug the discovery.
(6) Ipv4interfacecontainer is a tool class for assigning and managing IP addresses. The Ipv4addresshelper object address assigns IP addresses to devices, which are assigned to Ipv4interfacecontainer object management. This facilitates uniform invocation when all interfaces need to be listed.

Ipv4interfacecontainer interfaces = address. Assign (Devices);//Implement IP address assignment, device in devices will get IP address and subnet mask in ascending order, from 10.1.1.1 to 10.1.1.2

7. Application (Application)
Two special application classes are used in this program: Udpechoserverapplication and Udpechoclientapplication. When managing and configuring application, the tool class is implemented, there are two tool classes: Udpechoserverhelper and Udpechoclienthelper. The application's tool class is not the app itself, it just helps us build a specific application.

Udpechoserverhelper Echoserver (9);//Set the application with port number 9 Echoserver
Applicationcontainer Serverapps = Echoserver.install (nodes. Get (1));//Install the Echoserver program on a node. The Install method also takes the Nodecontainer object as the parameter.
Serverapps.start (Seconds (1.0));//program to set a start time to generate network traffic. The time object in NS3 uses the seconds object.
Serverapps.stop (Seconds (10.0));//program can set a stop time to end the generation of network traffic

Udpechoclienthelper echoclient (interfaces. GetAddress (1), 9);//Generate Echoclient app, and set two properties: Remoteaddress=interfaces. GetAddress (1), i.e. 10.1.1.1,remoteport=9
Echoclient.setattribute ("Maxpackets", Uintegervalue (1));//Set the client in the simulation, sending a packet up to 1
Echoclient.setattribute ("Interval", TimeValue (Seconds (1.0)));//Set Interval, packet sending time interval is 1 seconds
Echoclient.setattribute ("PacketSize", Uintegervalue (1024));//Set the package size to 1024 bytes.
Applicationcontainer ClientApps = Echoclient.install (nodes. Get (0));//Install the application echoclient on the node and establish ClientApps to manage all client applications.

The above sets 5 properties for the Echo client program: "Remoteaddress", "RemotePort", "Maxpackets", "Interval", "PacketSize".
8. Simulator
After setting up nodes, net devices, channels, applications, protocols, and so on, start the simulation below.
The application's startup features have been set up before:

Serverapps.start (Seconds (1.0));//Start server-side All programs in the 1th second
Serverapps.stop (Seconds (10.0));//close server All programs in 10th seconds
Clientapps.start (Seconds (2.0));//Start client All programs in 2nd seconds
Clientapps.stop (Seconds (10.0));//closing client All programs in 10th seconds

After running the Run method, perform the impersonation. According to the schedule in the program, the first 1 seconds to execute the server application Echoserver, the 2nd second client program Echoclient start, send a packet, to the 10th seconds two program end run. After the simulation runs, execute the Destroy method to end the simulation.

Simulator::run ();
Simulator::D Estroy ();

Ns-3 is a discrete event simulator in which each event is associated with the time it is executed, and the simulation process executes each event sequentially in chronological order. When an event executes, he may generate 0, 1, or more other events. When all events are executed sequentially, the simulation process will stop automatically, or the simulation will stop if there is a Stop event (Simulator:stop (StopTime)) in the event execution. Some events are infinite loops, such as Flowmonitor, RIPng, Realtimesimulator, and so on, and these events need to stop using stop.

9. Compiling your script
When you finish writing the script's source code, you can put the script in the scratch directory, and then run the WAF command to compile it automatically. For example, copy the examples/tutorial/first.cc to scratch, rename it to myfirst.cc, and then go back to the ns-3.26 working directory and execute the WAF:

As you can see, build finished successfully. After that, run the Myfirst simulation program:
Command for./waf–run Scracth/myfirst
4.3 NS-3 source code

Want to see NS-3 source code can use mercurial to download the latest, or look at the site Http://code.nsnam.org/ns-3-dev

The 4th chapter is finished, see the 5th chapter tomorrow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.