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.linux.ns3.ns3client import LinuxNS3Client
41 class LinuxNS3ClientTest(unittest.TestCase):
43 self.socket_name = os.path.join("/", "tmp", "NS3WrapperServer.sock")
44 if os.path.exists(self.socket_name):
45 os.remove(self.socket_name)
48 os.remove(self.socket_name)
50 def test_runtime_attr_modify(self):
51 thread = threading.Thread(target = run_server,
52 args = [self.socket_name])
54 thread.setDaemon(True)
59 # Verify that the communication socket was created
60 self.assertTrue(os.path.exists(self.socket_name))
62 # Instantiate the NS3 client
63 client = LinuxNS3Client(self.socket_name)
65 # Define a real time simulation
66 stype = client.create("StringValue", "ns3::RealtimeSimulatorImpl")
67 client.invoke("singleton::GlobalValue", "Bind", "SimulatorImplementationType", stype)
68 btrue = client.create("BooleanValue", True)
69 client.invoke("singleton::GlobalValue", "Bind", "ChecksumEnabled", btrue)
72 n1 = client.create("Node")
73 self.assertTrue(n1.startswith("uuid"))
75 ## Install internet stack
76 ipv41 = client.create("Ipv4L3Protocol")
77 client.invoke(n1, "AggregateObject", ipv41)
79 arp1 = client.create("ArpL3Protocol")
80 client.invoke(n1, "AggregateObject", arp1)
82 icmp1 = client.create("Icmpv4L4Protocol")
83 client.invoke(n1, "AggregateObject", icmp1)
86 lr1 = client.create("Ipv4ListRouting")
87 client.invoke(ipv41, "SetRoutingProtocol", lr1)
88 sr1 = client.create("Ipv4StaticRouting")
89 client.invoke(lr1, "AddRoutingProtocol", sr1, 1)
92 n2 = client.create("Node")
94 ## Install internet stack
95 ipv42 = client.create("Ipv4L3Protocol")
96 client.invoke(n2, "AggregateObject", ipv42)
98 arp2 = client.create("ArpL3Protocol")
99 client.invoke(n2, "AggregateObject", arp2)
101 icmp2 = client.create("Icmpv4L4Protocol")
102 client.invoke(n2, "AggregateObject", icmp2)
105 lr2 = client.create("Ipv4ListRouting")
106 client.invoke(ipv42, "SetRoutingProtocol", lr2)
107 sr2 = client.create("Ipv4StaticRouting")
108 client.invoke(lr2, "AddRoutingProtocol", sr2, 1)
110 ##### Create p2p device and enable ascii tracing
111 p2pHelper = client.create("PointToPointHelper")
112 asciiHelper = client.create("AsciiTraceHelper")
115 p1 = client.create("PointToPointNetDevice")
116 client.invoke(n1, "AddDevice", p1)
117 q1 = client.create("DropTailQueue")
118 client.invoke(p1, "SetQueue", q1)
121 ifindex1 = client.invoke(ipv41, "AddInterface", p1)
122 mask1 = client.create("Ipv4Mask", "/30")
123 addr1 = client.create("Ipv4Address", "10.0.0.1")
124 inaddr1 = client.create("Ipv4InterfaceAddress", addr1, mask1)
125 client.invoke(ipv41, "AddAddress", ifindex1, inaddr1)
126 client.invoke(ipv41, "SetMetric", ifindex1, 1)
127 client.invoke(ipv41, "SetUp", ifindex1)
129 # Enable collection of Ascii format to a specific file
130 filepath1 = "trace-p2p-1.tr"
131 stream1 = client.invoke(asciiHelper, "CreateFileStream", filepath1)
132 client.invoke(p2pHelper, "EnableAscii", stream1, p1)
135 p2 = client.create("PointToPointNetDevice")
136 client.invoke(n2, "AddDevice", p2)
137 q2 = client.create("DropTailQueue")
138 client.invoke(p2, "SetQueue", q2)
141 ifindex2 = client.invoke(ipv42, "AddInterface", p2)
142 mask2 = client.create("Ipv4Mask", "/30")
143 addr2 = client.create("Ipv4Address", "10.0.0.2")
144 inaddr2 = client.create("Ipv4InterfaceAddress", addr2, mask2)
145 client.invoke(ipv42, "AddAddress", ifindex2, inaddr2)
146 client.invoke(ipv42, "SetMetric", ifindex2, 1)
147 client.invoke(ipv42, "SetUp", ifindex2)
149 # Enable collection of Ascii format to a specific file
150 filepath2 = "trace-p2p-2.tr"
151 stream2 = client.invoke(asciiHelper, "CreateFileStream", filepath2)
152 client.invoke(p2pHelper, "EnableAscii", stream2, p2)
155 chan = client.create("PointToPointChannel")
156 client.set(chan, "Delay", "0s")
157 client.invoke(p1, "Attach", chan)
158 client.invoke(p2, "Attach", chan)
161 ping = client.create("V4Ping")
162 client.invoke(n1, "AddApplication", ping)
163 client.set (ping, "Remote", "10.0.0.2")
164 client.set (ping, "Interval", "1s")
165 client.set (ping, "Verbose", True)
166 client.set (ping, "StartTime", "0s")
167 client.set (ping, "StopTime", "20s")
170 client.stop(time = "21s")
175 client.set(chan, "Delay", "5s")
179 client.set(chan, "Delay", "0s")
181 # wait until simulation is over
185 if __name__ == '__main__':