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 version 2 as
8 # published by the Free Software Foundation;
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 # Test based on ns-3 csma/examples/csma-ping.cc file
29 # node n0 sends IGMP traffic to node n3
32 from nepi.resources.ns3.ns3server import run_server
33 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
40 class DummySimulation(object):
41 def __init__(self, socket_name):
42 self.socket_name = socket_name
43 self.node = dict({'hostname': 'localhost'})
46 def remote_socket(self):
47 return self.socket_name
49 class LinuxNS3ClientTest(unittest.TestCase):
51 self.socket_name = os.path.join("/", "tmp", "NS3WrapperServer.sock")
52 if os.path.exists(self.socket_name):
53 os.remove(self.socket_name)
56 os.remove(self.socket_name)
58 def test_runtime_attr_modify(self):
59 thread = threading.Thread(target = run_server,
60 args = [self.socket_name])
62 thread.setDaemon(True)
67 # Verify that the communication socket was created
68 self.assertTrue(os.path.exists(self.socket_name))
70 # Create a dummy simulation object
71 simulation = DummySimulation(self.socket_name)
73 # Instantiate the NS3 client
74 client = LinuxNS3Client(simulation)
76 # Define a real time simulation
77 stype = client.create("StringValue", "ns3::RealtimeSimulatorImpl")
78 client.invoke("singleton::GlobalValue", "Bind", "SimulatorImplementationType", stype)
79 btrue = client.create("BooleanValue", True)
80 client.invoke("singleton::GlobalValue", "Bind", "ChecksumEnabled", btrue)
83 n1 = client.create("Node")
84 self.assertTrue(n1.startswith("uuid"))
86 ## Install internet stack
87 ipv41 = client.create("Ipv4L3Protocol")
88 client.invoke(n1, "AggregateObject", ipv41)
90 arp1 = client.create("ArpL3Protocol")
91 client.invoke(n1, "AggregateObject", arp1)
93 icmp1 = client.create("Icmpv4L4Protocol")
94 client.invoke(n1, "AggregateObject", icmp1)
97 lr1 = client.create("Ipv4ListRouting")
98 client.invoke(ipv41, "SetRoutingProtocol", lr1)
99 sr1 = client.create("Ipv4StaticRouting")
100 client.invoke(lr1, "AddRoutingProtocol", sr1, 1)
103 n2 = client.create("Node")
105 ## Install internet stack
106 ipv42 = client.create("Ipv4L3Protocol")
107 client.invoke(n2, "AggregateObject", ipv42)
109 arp2 = client.create("ArpL3Protocol")
110 client.invoke(n2, "AggregateObject", arp2)
112 icmp2 = client.create("Icmpv4L4Protocol")
113 client.invoke(n2, "AggregateObject", icmp2)
116 lr2 = client.create("Ipv4ListRouting")
117 client.invoke(ipv42, "SetRoutingProtocol", lr2)
118 sr2 = client.create("Ipv4StaticRouting")
119 client.invoke(lr2, "AddRoutingProtocol", sr2, 1)
121 ##### Create p2p device and enable ascii tracing
122 p2pHelper = client.create("PointToPointHelper")
123 asciiHelper = client.create("AsciiTraceHelper")
126 p1 = client.create("PointToPointNetDevice")
127 client.invoke(n1, "AddDevice", p1)
128 q1 = client.create("DropTailQueue")
129 client.invoke(p1, "SetQueue", q1)
132 ifindex1 = client.invoke(ipv41, "AddInterface", p1)
133 mask1 = client.create("Ipv4Mask", "/30")
134 addr1 = client.create("Ipv4Address", "10.0.0.1")
135 inaddr1 = client.create("Ipv4InterfaceAddress", addr1, mask1)
136 client.invoke(ipv41, "AddAddress", ifindex1, inaddr1)
137 client.invoke(ipv41, "SetMetric", ifindex1, 1)
138 client.invoke(ipv41, "SetUp", ifindex1)
140 # Enable collection of Ascii format to a specific file
141 filepath1 = "trace-p2p-1.tr"
142 stream1 = client.invoke(asciiHelper, "CreateFileStream", filepath1)
143 client.invoke(p2pHelper, "EnableAscii", stream1, p1)
146 p2 = client.create("PointToPointNetDevice")
147 client.invoke(n2, "AddDevice", p2)
148 q2 = client.create("DropTailQueue")
149 client.invoke(p2, "SetQueue", q2)
152 ifindex2 = client.invoke(ipv42, "AddInterface", p2)
153 mask2 = client.create("Ipv4Mask", "/30")
154 addr2 = client.create("Ipv4Address", "10.0.0.2")
155 inaddr2 = client.create("Ipv4InterfaceAddress", addr2, mask2)
156 client.invoke(ipv42, "AddAddress", ifindex2, inaddr2)
157 client.invoke(ipv42, "SetMetric", ifindex2, 1)
158 client.invoke(ipv42, "SetUp", ifindex2)
160 # Enable collection of Ascii format to a specific file
161 filepath2 = "trace-p2p-2.tr"
162 stream2 = client.invoke(asciiHelper, "CreateFileStream", filepath2)
163 client.invoke(p2pHelper, "EnableAscii", stream2, p2)
166 chan = client.create("PointToPointChannel")
167 client.set(chan, "Delay", "0s")
168 client.invoke(p1, "Attach", chan)
169 client.invoke(p2, "Attach", chan)
172 ping = client.create("V4Ping")
173 client.invoke(n1, "AddApplication", ping)
174 client.set (ping, "Remote", "10.0.0.2")
175 client.set (ping, "Interval", "1s")
176 client.set (ping, "Verbose", True)
177 client.set (ping, "StartTime", "0s")
178 client.set (ping, "StopTime", "20s")
181 client.stop(time = "21s")
186 client.set(chan, "Delay", "5s")
190 client.set(chan, "Delay", "0s")
192 # wait until simulation is over
195 ## TODO: Add assertions !!
197 if __name__ == '__main__':