NS- Manual - Simulation Chapter, Reference Translation

xiaoxiao2021-03-06  41

Part Viii

simulation

Chapter 39 Simulation

This chapter mainly tells the simulation function of NS. The simulation refers to connecting the simulator to a real network. There are special objects in the simulator, which have the ability to introduce the real network packet into the simulator and can send the handled data to the real network.

About the attention point of the emulator:

● Although the user does not want the following interface descriptions frequently change, the simulation function of NS is constantly being developed, which is constantly experimenting and improvement, so the interface will definitely change.

● The simulation function of NS is developed on FreeBSD 2.2.5, and we don't test on other systems.

● Due to the limited portability of the current simulation function, the simulation function is compiled into NSE (using the "Make NSE" compile option) instead of standard NS.

39.1 Overview

The simulation function can be divided into two modes:

1. The opaque mode - The simulator believes that the network packet is an opaque overall.

2. Protocol mode - The simulator can explain and generate network packets.

In an opaque mode, the simulator believes that the network packet is not understandable. In particular, the protocol field of the data packet in the real network cannot be operated directly by the simulator. In an opaque mode, the real network packet may be discarded, delayed, reordered or replicated by the simulator. However, since the protocol processing process is not executed, some protocols are related to the operation of the operation network packets may not be executed. In the protocol mode, the simulator can identify and / or can generate a network packet that can include any related fields. Until now (1998.3), NS only realizes the simulation function of opaque mode.

The collection of some objects in NS includes TAP Agents and Network Objects object sets to provide interfaces between simulators and real network environments. TAP Agents Inserts the real network packet into the simulator's analog packet, or extracts the real network packet from the simulator's analog packet. Network Objects is installed in TAP Agents, which provides an entry for sending and receiving real network data. All TAP Agents and Network Objects are told in the following sections.

When the NS works in the simulation mode, a special version of the system scheduler is used: RealTime Scheduler. This scheduler uses the same underlying structure as the standard Calendar-Queue, but it can perform events in real time. The real-time scheduler is as follows:

39.2 Real-time scheduler

The real-time scheduler implements a software scheduler that is running on real time. If the speed of the CPU is fast enough, you can synchronize with the package, then the simulator's virtual time is close to real time and very close. If the simulator is too slow, the simulator's virtual time has a deviation with the real time. When this deviation is greater than a preset constant "overflow coefficient (Slop Factor, current is 10ms), the simulator will continue to have a warning.

The main loop of the scheduler distributing events in the RealTimescheduler :: run () function in the file scheduler.cc. It essentially follows the algorithm:

_

As long as the simulator does not stop, execute the following:

--- Get the current real time ("now").

--- Distribute all timestamps before the current time before the current time before the unprocessed simulator event.

--- If there is a next (future) incident, it takes this event.

--- Delay until the next simulator event is ready or has a TCL event.

--- If there is a TCL event that occurs, the next event inserts the next event into the simulator event queue and returns to the main loop to continue.

- Otherwise, the simulator event is distributed back to the main loop start office.

--- If there is no future event, check the TCL event and return to the main loop to continue.

Real-time scheduler must be used with the simulation function. Otherwise, it will cause the simulator's virtual time to be faster than true time. In this case, the network packet is not delayed when the network is simulated. Using real-time scheduler needs to do the following instructions in the beginning of the simulation: SET NS [New Simulator]

$ NS Use-Scheduler Realtime

39.3 TAP Agents

The TapAgent class is a simple class derived from the base class Agent. In this way, it can generate a data packet of the simulator, and contain any specified value in the NS packet public head. TAP Agent Processing Packet Size fields and package type fields in public headers. It specifies the type field inserted into the package in the simulator as PT_LIVE. Each Tab Agent can only have a related network object, but a node of a simulator can have multiple Tab Agent objects.

The TAP Agent object can receive packets from a related network object, and you can also send data to a related network object. Suppose $ Netobj represents a network object, a TAP Agent requires the configuration of the class's NetWork method:

SET A0 [New Agent / TAP]

$ A0 Network $ Netobj

$ A0 set FID_ 26

$ A0 Set PRIO_ 2

$ NS Connect $ A0 $ A1

Attention The configuration of stream ID and priority is processed by the base class Agent. The purpose of setting the stream ID in the packet public head is to identify a packet belonging to its specific stream of real data. Special processing can be performed for these packets, such as discard or reordering, and the like. The Connect method requires that the agent $ A0 is sent to the agent $ A1.

39.4 Network object

Network objects can access true networks. The network object is divided into several different categories based on the different levels of the protocol layer that requests the real network. To use a network object, you need to have special access rights, which must be set in advance. In general, in a specific protocol layer of a real network (for example, a link layer, IP layer, UDP, etc.), the network object provides an entry point and a special access mode (read-only, written, readable write).

Network objects provide some special features, such as filtering packets or receiving network packets (eg, PCAP / BPF network objects) or inter-set nodes (such as UDP / IP multicast) Function. C Class NetWork is the base class of all derived network objects. Current NS supports three network objects: PCAP / BPF, RAW IP and UDP / IP.

39.4.1 PCAP / BPF NetWork Objects

These objects provide an extension interface for the LBNL package capture library (more information, please refer to ftp://ftp.ee.lbl.gov/libpcap.tar). This library can capture the frame of the link layer from the network interface driver in the mix mode (for example: using libpcap), which is replicated from the network card driver.). It also provides the ability to read and write packet tracking files in "TCPDump" format. If the network interface driver allows the expansion interface provided by the NS, the frame can be written to a real network through the network interface driver. System administrators may limit the use of this library to capture and generate network packets. If you use this library, you must at least have permissions to read the system package filtering service, which must be system administrators to specify.

This package capture library can run well in multiple UNIX-based platforms. It is specifically optimized for the Berkeley packet filter (BPF) [26], and the package capture library also provides a filter compiler of the BPF pseudo machine code. Since most systems support BPF, a group of resolved kernel's BPF code processing filters condition, and the received frames match. The frame that matches the filter condition is received by BPF, and the frame does not meet the conditions is not affected. BPF can also send a frame of the link layer. But it is generally not recommended, because a frame that a correct initialization must be created before being passed to the BPF. This will result in the correct link layer header information that cannot be specified for the next target address. In general, it is better to send IP packets with "Raw IP" network object because the system routing function can specify the correct information for the link layer header. Configuration: The PCAP network object can be configured to be related to the real network or related to the trace file. If you are related to the real network, you must specify the specific network interface that will be used, but also specify the mixed mode marker. For all network objects, you can open the network object in an read or written. E.g:

SET me [exec hostname]

Set Pf1 [New Network / PCAP / LIVE]

$ PF1 Set Promisc_ true

SET INTF [$ PF1 Open ReadOnly]

PUTS "PF1 Configured on Interface $ INTF"

SET FILT "(IP SRC Host Foobar) and (Not Ether Broadcast)

Set nbytes [$ PF1 FILTER $ FILT]

Puts "Filter Compiled to $ NBYTES BYTES"

PUTS "DROPS: [$ PF1 PDROPS], PKTS: [$ PF1 PKTS]"

The first line local computer name is taken out as a condition for constructing a BPF / libpcap filter. The second line of New Network / PCAP / LIVE generates a PCAP network object that can capture a real network packet. The third line PROMISC logo tells the packet filter to set the network interface into a mixed mode (if the underlying network interface supports mixing mode). The fourth line calls the network object $ PF1 Open activation package filter. Open's parameters can be specified as Readonly, Writeonly or ReadWrite. The Open method returns the name of the network interface related to the package filter. The OPEN method can also pass additional parameters (we don't show), indicate the name of the network interface you want to use. This is used in a case where there is a plurality of interfaces.

The Filter method creates a BPF-compatible package condition that is loaded into the BPF module of the underlying layer. The Filter method returns the number of bytes used by the filter. PDROPS and PKTS methods are used to count the number of packages. They return to the total number of packets discarded due to the filter queue overflow and the total number of packets to the filter (not the number of packages that satisfy the filter condition).

39.4.2 IP network object

These objects have the original access capability of the IP protocol, and can handle the full-specific IP packets (including the data packet header). These objects are implemented using the RAW Socket. On most UNIX systems, to access these sockets, you need to have permissions for superuser. In addition, the Raw Sockets interface is not as good as other types of Sockets standards. Class Network / IP implements the function of the original IP and has the functions of the NetWork base class. All network objects that implement higher level protocols are derived from the NetWork base class.

Configuration: The configuration of the original IP network object is quite simple. It does not have to be associated with any particular network interface. You can use the IP routing function of the system when you need a packet. The physical address of the network interface of the destination address is included in the head of these packets. Below is an example of configuring an IP object: SET IPNET [New Network / IP]

$ ipnet open writeonly

...

$ i 网 close

IP network objects only support Open and Close methods.

39.4.3 IP / UDP network object

These objects encapsulate the functionality of the system UDP protocol, and also provides IP multicast. This object is being developed.

39.5 An example

The following code shows a small but very complete analog script, using BPF and IP network objects in the script, you can use this script to make simulation tests. Ns runs on a multi-interface host, which has routing capabilities, which can read packets from an interface and make these packets through analog network, and finally use the original IP network object to send them to a real network. .

SET ME "10.0.1.1"

SET NS [New Simulator]

$ NS Use-Scheduler Realtime

#

# w w t Test Machine To Have IP Forwarding Disabled, SO

# Check this (this is how to do so under freebsd at least)

#

Set ipforw [exec system sysctl -n net.inet.ip.forwarding]

IF $ ipforw

PUTS "Can Not Run with IP Forwarding Enabled"

EXIT 1

#

# Allocate a BPF Type Network Object and a RAW-IP Object

#

SET BPF0 [New Network / PCAP / LIVE]

SET BPF1 [New Network / PCAP / LIVE]

$ bpf0 set propisc_ true

$ bpf1 set promisc_ true

Set ipnet [new network / ip]

Set nd0 [$ bpf0 open ready fxp0]

Set nd1 [$ bpf1 open ready fxp1]

$ ipnet open writeonly

#

# Try to filter out Weird Stuff Like Netbios Pkts, ARP Requests, DNS,

# Also, don't catch stuff to / from myself or broadcasted

#

SET NOTME "(Not IP Host $ ME)"

Set notbcast "(Not ether big broadcast"

Set ftp "and port ftp-data"

Set F0len [$ BPF0 Filter "(IP DST HOST BIS) and $ Notme and $ Notbcast"]

Set F1len [$ BPF1 Filter "(IP SRC Host Bit) and $ notme and $ notbcast"]

PUTS "Filter Length": $ f0len (bpf0), $ f1len (bpf1) "

PUTS "DEV $ ND0 HAS Address [$ BPF0 LINKADDR]"

PUTS "DEV $ ND1 HAS Address [$ BPF1 LINKADDR]"

SET A0 [New Agent / TAP]

SET A1 [New Agent / TAP]

SET A2 [New Agent / TAP] PUTS "Install Nets INTO TAPS ..."

$ A0 Network $ bpf0

$ A1 Network $ bpf1

$ A2 Network $ ipnet

Set node0 [$ ns node]

Set node1 [$ ns node]

Set node2 [$ ns node]

$ NS Simplex-Link $ Node0 $ Node2 10MB 10ms Droptail

$ ns simplex-link $ node1 $ node2 10MB 10ms Droptail

$ ns attach-agent $ node0 $ A0

$ ns attach-agent $ node1 $ a1

$ ns attach-agent $ node2 $ a2

$ NS Connect $ A0 $ A2

$ NS Connect $ A1 $ A2

PUTS "OKEY"

$ NS RUN

39.6 related orders

Here are some simulation related commands:

$ ns_ use-scheduler realtime

This command specifies the system's event scheduler as a real-time scheduler. Note that the real-time scheduler should always be used with the simulation function, otherwise it will cause the virtual time of the analog network to be fast than true time.

Set Netob [New Network / ]

This command creates a network object. We use the network object to access the real network. Several network objects are available in NS, namely NetWork / PCAP / LIVE, NETWORK / IP, Network / IP / UDP. For details on the network object, please refer to Section 39.4.

转载请注明原文地址:https://www.9cbs.com/read-78165.html

New Post(0)