Analysis of the process of configuring a simple network environment based on Tcl language

Analysis of the process of configuring a simple network environment based on Tcl language

1. Tcl script file circle.tcl code comments

#Set some properties needed for simulation set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
#After setting the protocol to DSR, set the queue to CMUPriQueue
set val(ifq) CMUPriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(ifqlen) 50
#Preset the number of nodes to 0 and wait for user input. This item requires user input, otherwise the simulation will not be performed.
set val(nn) 0
set val(rp) DSR
#The default value of the scene size is 1000*1000
set val(x) 1000
set val(y) 1000
#The default radius of the circle is 400
set val(r) 400
#This procedure is used to print on the screen. Type ns circle.tcl in the terminal and then add the parameter format proc usage {} {
  global argv0
  puts "\nusage: $argv0 \[-nn nodes\] \[-rr\] \[-xx\] \[-yy\]\n"
  puts "note: \[-nn nodes\] is essential, and the others are optional.\n"
}
#This procedure is used to change the values ​​of some preset parameters according to user input. proc getval {argc argv} {
  global val
  lappend vallist nn rxyz
  #argc is the number of parameters, argv is the string consisting of the entire parameter for {set i 0} {$i < $argc} {incr i} {
    #The variable arg is the i-th part of argv, separated by spaces. set arg [lindex $argv $i]
    #Skip the string without the character "-", which is usually a number typed by the user. #string range $arg mn means taking the mth character to the nth character of the string $arg if {[string range $arg 0 0] != "-"} continue
    set name [string range $arg 1 end]
    #Change preset variables (number of nodes, radius, scene size)
    set val($name) [lindex $argv [expr $i+1]]
  }
}
#Call getval procedure getval $argc $argv
#The user did not enter any parameters, but only typed ns circle.Tcl, so the number of nodes is assumed to be 0
if { $val(nn) == 0 } {
  #Print usage
  exit
}

#Create a simulation instance set ns [new Simulator]

#Set the record file set tracefd [open circle.tr w]
$ns trace-all $tracefd
set namtracefd [open circle.nam w]
$ns namtrace-all-wireless $namtracefd $val(x) $val(y)

#Close the trace file and call the nam program to demonstrate the animation proc finish { } {
	global ns tracefd namtracefd
	$ ns flush-trace
	close $tracefd
	close $namtracefd
	exec nam circle.nam &
	exit 0
}

set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)
#Node configuration. Due to version reasons, addressType is set to def
$ns node-config -addressType def\
-adhocRouting $val(rp) \
    -llType $val(ll) \
    -macType $val(mac)\
    -ifqType $val(ifq) \
    -ifqLen $val(ifqlen) \
    -antType $val(ant) \
    -propType $val(prop) \
    -phyType $val(netif) \
    -channelType $val(chan) \
    -topoInstance $topo \
    -agenttrace ON \
    -routertrace ON \
    -mactrace OFF \
    -movementtrace OFF

# Initialize the node for {set i 0} {$i < $val(nn)} {incr i} {
#Create a node set node_($i) [$ns node]
$node_($i) random-motion 0
#Calculate the node position and set it, using trigonometric functions $node_($i) set X_ [expr $val(r) * cos($i * 2 * 3.14159 / $val(nn))]
  $node_($i) set Y_ [expr $val(r) * sin($i * 2 * 3.14159 / $val(nn))]
$node_($i) set Z_ 0
#Set the display size of the mobile node in nam, otherwise, the node cannot be displayed in nam $ns initial_node_pos $node_($i) [expr $val(x) / 10]
}

#Create a UDP agent on node_(0) set tcp [new Agent/UDP]
$ns attach-agent $node_(0) $tcp
#Create a data receiver on the node opposite to node(0) along the diameter set null [new Agent/Null]
$ns attach-agent $node_([expr $val(nn)/2]) $null
#Create a new CBR traffic generator, with a packet size of 500B and an interval of 0.05s
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 5000
$cbr set interval_ 0.05
#Connect UDP and Null
$cbr attach-agent $tcp
$ns connect $tcp $null
#Send data at 0.1s, stop sending data at 3.0s, and call the finish process at 5.0s $ns at 0.1 "$cbr start"
$ns at 3.0 "$cbr stop"
$ns at 5.0 "finish"
$ns run

2. gawk script code analysis.awk comments

BEGIN {
#Set initial variables num_D = 0; #Number of lost packets num_s = 0; #Number of sent packets num_r = 0; #Number of received packets rate_drop = 0; #Packet loss rate sum_delay = 0; #Total delay time average_delay = 0; #Average delay time}
{
	#Read the trace file record event = $1; #The first column is the packet operation (s is the sending packet, r is the receiving packet)
	time = $2; #The second column is the operation time node = $3; #The third column is the node number trace_type = $4; #The fourth column is the operation layer flag = $5; #The fifth column is the flag uid = $6; #The sixth column is the node identifier pkt_type = $7; #The seventh column is the packet type pkt_size = $8; #The eighth column is the packet size #Operation if (event == "s" && trace_type == "AGT" && pkt_type == "cbr")
	{ send_time[uid] = time; #Create an array to record the time of sending packets num_s++; #Record the total number of packets sent }
	if (event == "r" && trace_type == "AGT" && pkt_type == "cbr")
	{ delay[uid] = time - send_time[uid]; #Create an array to record the delay time num_r++; #Record the total number of packets received }
	if (event == "D" && pkt_type == "cbr")
		delay[uid] = -1; #-1 means the packet is lost and the packet will not be recorded in the delay time}

END {
	#Calculate the number of packet losses and packet loss rate num_D = num_s-num_r; #Total number of packet losses rate_drop = num_D / num_s * 100.0; #Calculate the packet loss rate	
	#Calculate delay for ( i = 0; i < num_s; i++)
		{if (delay[i] >= 0)
			sum_delay += delay[i];
		}#Total delay time average_delay = sum_delay / num_r; #Average delay time #Print results printf("number of packets dropped: %d \n",num_D);
	printf("number of packets sent: %d \n",num_s);
	printf("drop rate: %.3f%% \n",rate_drop);
	printf("average delay time: %.9f \n",average_delay);
}

3. Experimental Results

(1)

The number of network nodes is set to 12. The running results are as follows, and two record files, nam file and trace file, are generated.


At this time, the trace file size is 91.8kb and the nam file size is 76.0kb.



Next, use the gawk tool to analyze the generated trace file. If the gawk tool is not installed, run the sudo apt-get install gawk command to install it.
The results obtained are the number of packet losses, number of packets sent, packet loss rate and average delay during the network simulation process.


(2) Change the number of nodes to 8 again. The running results are as follows. However, no additional circle.nam and circle.tr files are added to the folder.


Checking the properties of the two files, we found that their sizes had changed, indicating that the record files of the newly created network simulation environment had been overwritten.



Gawk can also be used to analyze the generated trace files. It was found that as the number of network nodes increased, the packet loss rate and average delay increased.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • CentOS7 network configuration under VMware virtual machine (host wireless Internet access)
  • Tutorial on how to modify the IP address of a Linux virtual machine, check the gateway, and configure the network environment
  • Implementation of CentOS8.0 network configuration
  • VMWare virtual machine 15.X LAN network configuration tutorial diagram
  • Detailed explanation of the steps for configuring the Centos7 bridge network under VMware
  • Linux beginners in virtual machines configure IP and restart the network
  • Detailed explanation of NAT configuration in Linux network
  • How to configure DNS in Docker's default bridge network

<<:  MySQL 8.0.21 free installation version configuration method graphic tutorial

>>:  Summary of Vue's cross-domain problem handling and solutions

Recommend

Linux file systems explained: ext4 and beyond

Today I will take you through the history of ext4...

Solution to nginx not jumping to the upstream address

Preface Today I encountered a very strange proble...

MySQL database index order by sorting detailed explanation

Table of contents The cause of the incident Anato...

How to control the proportion of Flex child elements on the main axis

background Flex layout achieves alignment and spa...

MySQL 5.7 installation and configuration tutorial under CentOS7 64 bit

Installation environment: CentOS7 64-bit MINI ver...

Issues with Rancher deployment and importing K8S clusters

Rancher deployment can have three architectures: ...

js learning notes: class, super and extends keywords

Table of contents Preface 1. Create objects befor...

MySQL 8.0.23 installation super detailed tutorial

Table of contents Preface 1. Download MySQL from ...

Complete steps to implement location punch-in using MySQL spatial functions

Preface The project requirement is to determine w...

Thumbnail hover effect implemented with CSS3

Achieve resultsImplementation Code html <heade...

Analysis and solutions to problems encountered in the use of label tags

I used the label tag when I was doing something re...

Sample code for deploying Spring-boot project with Docker

1. Basic Spring-boot Quick Start 1.1 Quick start ...

Install and configure MySQL under Linux

System: Ubuntu 16.04LTS 1\Download mysql-5.7.18-l...

MySQL sorting feature details

Table of contents 1. Problem scenario 2. Cause An...