NS3 Network Simulation (6): Bus Network, ns3 Simulation
Happy shrimp
Http://blog.csdn.net/lights_joy/
Reprinted, but keep the author information
In the first example first. py provided by NS3, a point-to-point network is simulated, and the following sample code simulates a bus-type network and CSMA protocol.
# // Default Network Topology# //# // 10.1.1.0# // n0 -------------- n1 n2 n3 n4# // point-to-point | | | |# // ================# // LAN 10.1.2.0
The sample code provided by NS3 is C ++. We can rewrite it using Python.
Like first. py, we first construct a point-to-point connection between n0-n1:
# Construct a point-to-point connection p2pNodes = ns. network. nodeContainer () p2pNodes. create (2) pointToPoint = ns. point_to_point.PointToPointHelper () pointToPoint. setDeviceAttribute ("DataRate", ns. core. stringValue ("5 Mbps") pointToPoint. setChannelAttribute ("Delay", ns. core. stringValue ("2 ms") p2pDevices = pointToPoint. install (p2pNodes)
Then construct the bus connection between n1-n4:
# Build a bus connection nCsma = 3 csmaNodes = ns. network. nodeContainer () csmaNodes. add (p2pNodes. get (1) csmaNodes. create (nCsma) csma = ns. csma. csmaHelper () csma. setChannelAttribute ("DataRate", ns. core. stringValue ("100 Mbps") csma. setChannelAttribute ("Delay", ns. core. timeValue (ns. core. nanoSeconds (6560) csal evices = csma. install (csmaNodes)
Next, install the protocol stack for each node. Note that each node can only be installed once. This is why p2pNodes. Get (0) is used here:
# Install protocol stack = ns. internet. InternetStackHelper () stack. Install (p2pNodes. Get (0) stack. Install (csmaNodes)
Next, configure the IP address for each node:
# Configure IPaddress = ns. internet. ipv4AddressHelper () address. setBase (ns. network. listen 4address ("10.1.1.0"), ns. network. ipv4Mask ("255.255.255.0") p2pInterfaces = address. assign (p2pDevices) address. setBase (ns. network. listen 4address ("10.1.2.0"), ns. network. ipv4Mask ("255.255.255.0") csmaInterfaces = address. assign (csal evices)
Configure the Echo service and client to be simulated:
# Configure the application echoServer = ns. applications. udpEchoServerHelper (9) serverApps = echoServer. install (csmaNodes. get (nCsma) serverApps. start (ns. core. seconds (1.0) serverApps. stop (ns. core. seconds (20.0) echoClient = ns. applications. udpEchoClientHelper (csmaInterfaces. getAddress (nCsma), 9) echoClient. setAttribute ("MaxPackets", ns. core. uintegerValue (5) echoClient. setAttribute ("Interval", ns. core. timeValue (ns. core. seconds (1.0) echoClient. setAttribute ("PacketSize", ns. core. uintegerValue (1024) clientApps = echoClient. install (p2pNodes. get (0) clientApps. start (ns. core. seconds (2.0) clientApps. stop (ns. core. seconds (20.0 ))
Because we use two network segments, We need to configure the route:
# The global routing manager creates a route table ns. internet. Ipv4GlobalRoutingHelper. PopulateRoutingTables () for each node based on the link announcement generated by the node ()
Then we can start the simulation:
# Start to simulate ns. core. Simulator. Run () ns. core. Simulator. Destroy ()
At this time, our script has no output. We asked NS3 to generate an xml file that can be used in NetAnim and add the following script before run:
anim = ns.netanim.AnimationInterface('second.xml')anim.SetConstantPosition(p2pNodes.Get(0), 10, 10)anim.SetConstantPosition(csmaNodes.Get(0), 30, 10)anim.SetConstantPosition(csmaNodes.Get(1), 40, 10)anim.SetConstantPosition(csmaNodes.Get(2), 50, 10)anim.SetConstantPosition(csmaNodes.Get(3), 60, 10)
After running this script, you can generate the second. xml file in the project path and open it in NetAnim:
Then let NS3 Save the simulated data packet:
pointToPoint.EnablePcapAll ("second");csma.EnablePcap ("second", csmaDevices.Get (1), True)
Use etherreal to open the generated pcap file:
The generated packet verification error. The verification code is 0. This is because NS3 does not enable Checksum. Add the following line to the script:
Ns. core. GlobalValue. Bind ("ChecksumEnabled", ns. core. BooleanValue (True ))
Run the script again:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.