3 # NEPI, a framework to manage network experiments
4 # Copyright (C) 2013 INRIA
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
22 # Test based on ns-3 csma/examples/csma-ping.cc file
30 # node n0 sends IGMP traffic to node n3
33 from nepi.resources.ns3.ns3server import run_server
34 from nepi.resources.ns3.linuxns3client import LinuxNS3Client
41 class LinuxNS3ClientTest(unittest.TestCase):
43 self.socket_name = os.path.join("/", "tmp", "NS3WrapperServer.sock")
46 os.remove(self.socket_name)
48 def test_runtime_attr_modify(self):
49 thread = threading.Thread(target = run_server,
50 args = [self.socket_name])
52 thread.setDaemon(True)
57 # Verify that the communication socket was created
58 self.assertTrue(os.path.exists(self.socket_name))
60 # Instantiate the NS3 client
61 client = LinuxNS3Client(self.socket_name)
63 # Define a real time simulation
64 stype = client.create("StringValue", "ns3::RealtimeSimulatorImpl")
65 client.invoke("singleton::GlobalValue", "Bind", "SimulatorImplementationType", stype)
66 btrue = client.create("BooleanValue", True)
67 client.invoke("singleton::GlobalValue", "Bind", "ChecksumEnabled", btrue)
70 n1 = client.create("Node")
71 self.assertTrue(n1.startswith("uuid"))
73 ## Install internet stack
74 ipv41 = client.create("Ipv4L3Protocol")
75 client.invoke(n1, "AggregateObject", ipv41)
77 arp1 = client.create("ArpL3Protocol")
78 client.invoke(n1, "AggregateObject", arp1)
80 icmp1 = client.create("Icmpv4L4Protocol")
81 client.invoke(n1, "AggregateObject", icmp1)
84 lr1 = client.create("Ipv4ListRouting")
85 client.invoke(ipv41, "SetRoutingProtocol", lr1)
86 sr1 = client.create("Ipv4StaticRouting")
87 client.invoke(lr1, "AddRoutingProtocol", sr1, 1)
90 n2 = client.create("Node")
92 ## Install internet stack
93 ipv42 = client.create("Ipv4L3Protocol")
94 client.invoke(n2, "AggregateObject", ipv42)
96 arp2 = client.create("ArpL3Protocol")
97 client.invoke(n2, "AggregateObject", arp2)
99 icmp2 = client.create("Icmpv4L4Protocol")
100 client.invoke(n2, "AggregateObject", icmp2)
103 lr2 = client.create("Ipv4ListRouting")
104 client.invoke(ipv42, "SetRoutingProtocol", lr2)
105 sr2 = client.create("Ipv4StaticRouting")
106 client.invoke(lr2, "AddRoutingProtocol", sr2, 1)
108 ##### Create p2p device and enable ascii tracing
109 p2pHelper = client.create("PointToPointHelper")
110 asciiHelper = client.create("AsciiTraceHelper")
113 p1 = client.create("PointToPointNetDevice")
114 client.invoke(n1, "AddDevice", p1)
115 q1 = client.create("DropTailQueue")
116 client.invoke(p1, "SetQueue", q1)
119 ifindex1 = client.invoke(ipv41, "AddInterface", p1)
120 mask1 = client.create("Ipv4Mask", "/30")
121 addr1 = client.create("Ipv4Address", "10.0.0.1")
122 inaddr1 = client.create("Ipv4InterfaceAddress", addr1, mask1)
123 client.invoke(ipv41, "AddAddress", ifindex1, inaddr1)
124 client.invoke(ipv41, "SetMetric", ifindex1, 1)
125 client.invoke(ipv41, "SetUp", ifindex1)
127 # Enable collection of Ascii format to a specific file
128 filepath1 = "trace-p2p-1.tr"
129 stream1 = client.invoke(asciiHelper, "CreateFileStream", filepath1)
130 client.invoke(p2pHelper, "EnableAscii", stream1, p1)
133 p2 = client.create("PointToPointNetDevice")
134 client.invoke(n2, "AddDevice", p2)
135 q2 = client.create("DropTailQueue")
136 client.invoke(p2, "SetQueue", q2)
139 ifindex2 = client.invoke(ipv42, "AddInterface", p2)
140 mask2 = client.create("Ipv4Mask", "/30")
141 addr2 = client.create("Ipv4Address", "10.0.0.2")
142 inaddr2 = client.create("Ipv4InterfaceAddress", addr2, mask2)
143 client.invoke(ipv42, "AddAddress", ifindex2, inaddr2)
144 client.invoke(ipv42, "SetMetric", ifindex2, 1)
145 client.invoke(ipv42, "SetUp", ifindex2)
147 # Enable collection of Ascii format to a specific file
148 filepath2 = "trace-p2p-2.tr"
149 stream2 = client.invoke(asciiHelper, "CreateFileStream", filepath2)
150 client.invoke(p2pHelper, "EnableAscii", stream2, p2)
153 chan = client.create("PointToPointChannel")
154 client.set(chan, "Delay", "0s")
155 client.invoke(p1, "Attach", chan)
156 client.invoke(p2, "Attach", chan)
159 ping = client.create("V4Ping")
160 client.invoke(n1, "AddApplication", ping)
161 client.set (ping, "Remote", "10.0.0.2")
162 client.set (ping, "Interval", "1s")
163 client.set (ping, "Verbose", True)
164 client.set (ping, "StartTime", "0s")
165 client.set (ping, "StopTime", "20s")
168 client.stop(time = "21s")
173 client.set(chan, "Delay", "5s")
177 client.set(chan, "Delay", "0s")
179 # wait until simulation is over
183 if __name__ == '__main__':