NS3網路模擬(6): 匯流排型網路,ns3模擬
快樂蝦
http://blog.csdn.net/lights_joy/
歡迎轉載,但請保留作者資訊
在NS3提供的第一個樣本first.py中,類比了一個點對點的網路,接下來的一個範例程式碼類比了一個匯流排型網路及CSMA協議。
# // Default Network Topology# //# // 10.1.1.0# // n0 -------------- n1 n2 n3 n4# // point-to-point | | | |# // ================# // LAN 10.1.2.0
只不過NS3提供的範例程式碼是C++的,我們用Python改寫它。
和first.py一樣,我們先構造n0-n1之間的點對點連接:
# 構建點對點連接p2pNodes = ns.network.NodeContainer()p2pNodes.Create (2)pointToPoint = ns.point_to_point.PointToPointHelper()pointToPoint.SetDeviceAttribute ("DataRate", ns.core.StringValue ("5Mbps"))pointToPoint.SetChannelAttribute ("Delay", ns.core.StringValue ("2ms"))p2pDevices = pointToPoint.Install (p2pNodes)
再構造n1-n4之間的匯流排型串連:
# 構建匯流排串連nCsma = 3csmaNodes = ns.network.NodeContainer()csmaNodes.Add (p2pNodes.Get (1))csmaNodes.Create (nCsma)csma = ns.csma.CsmaHelper()csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("100Mbps"))csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.NanoSeconds (6560)))csmaDevices = csma.Install (csmaNodes)
接下來為每個節點安裝協議棧,這裡需要注意的是每個節點只能安裝一次,這也是這裡使用p2pNodes.Get(0)的原因:
# 安裝協議棧stack = ns.internet.InternetStackHelper()stack.Install (p2pNodes.Get(0))stack.Install (csmaNodes)
接下來為每個節點配置IP:
# 配置IPaddress = ns.internet.Ipv4AddressHelper()address.SetBase ( ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))p2pInterfaces = address.Assign (p2pDevices)address.SetBase ( ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0"))csmaInterfaces = address.Assign (csmaDevices)
接著配置要類比啟動並執行Echo服務和用戶端:
# 配置應用程式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))
由於我們使用了兩個網段,因而需要配置路由:
# 全域路由管理器根據節點產生 的鏈路通告為每個節點建立路由表ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
此後我們就可以開始模擬了:
# 開始模擬ns.core.Simulator.Run()ns.core.Simulator.Destroy()
此時我們的指令碼還沒有任何輸出,我們讓NS3產生可以在NetAnim中使用的xml檔案,在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)
運行此指令碼後可以在項目路徑下產生second.xml檔案,在NetAnim下開啟:
再讓NS3儲存類比的資料包:
pointToPoint.EnablePcapAll ("second");csma.EnablePcap ("second", csmaDevices.Get (1), True)
用etherreal開啟產生的pcap檔案,有點問題:
產生的資料包校正錯誤,校正碼全為0。這是由於NS3沒有開啟Checksum的緣故,在指令碼中加上一行:
ns.core.GlobalValue.Bind("ChecksumEnabled",ns.core.BooleanValue (True))
再運行此指令碼:
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。