273eb89aa72a64b6e93dc9566e23f959c91b34e5
[nepi.git] / examples / dce / custom_local_wifi_ping.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
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;
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 from nepi.execution.ec import ExperimentController 
21
22 def add_ns3_node(ec, simu):
23     node = ec.register_resource("ns3::Node")
24     ec.register_connection(node, simu)
25
26     ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
27     ec.register_connection(node, ipv4)
28
29     arp = ec.register_resource("ns3::ArpL3Protocol")
30     ec.register_connection(node, arp)
31     
32     icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
33     ec.register_connection(node, icmp)
34
35     udp = ec.register_resource("ns3::UdpL4Protocol")
36     ec.register_connection(node, udp)
37
38     tcp = ec.register_resource("ns3::TcpL4Protocol")
39     ec.register_connection(node, tcp)
40
41     return node
42
43 def add_device(ec, node, ip, prefix, access_point = False):
44     dev = ec.register_resource("ns3::WifiNetDevice")
45     ec.set(dev, "ip", ip)
46     ec.set(dev, "prefix", prefix)
47     ec.register_connection(node, dev)
48
49     phy = ec.register_resource("ns3::YansWifiPhy")
50     ec.set(phy, "Standard", "WIFI_PHY_STANDARD_80211a")
51     ec.register_connection(dev, phy)
52
53     error = ec.register_resource("ns3::NistErrorRateModel")
54     ec.register_connection(phy, error)
55
56     manager = ec.register_resource("ns3::ArfWifiManager")
57     ec.register_connection(dev, manager)
58
59     if access_point:
60         mac = ec.register_resource("ns3::ApWifiMac")
61     else:
62         mac = ec.register_resource("ns3::StaWifiMac")
63
64     ec.set(mac, "Standard", "WIFI_PHY_STANDARD_80211a")
65     ec.register_connection(dev, mac)
66
67     return dev, phy
68
69 def add_constant_mobility(ec, node, x, y, z):
70     mobility = ec.register_resource("ns3::ConstantPositionMobilityModel") 
71     position = "%d:%d:%d" % (x, y, z)
72     ec.set(mobility, "Position", position)
73     ec.register_connection(node, mobility)
74     return mobility
75
76 def add_wifi_channel(ec):
77     channel = ec.register_resource("ns3::YansWifiChannel")
78     delay = ec.register_resource("ns3::ConstantSpeedPropagationDelayModel")
79     ec.register_connection(channel, delay)
80
81     loss  = ec.register_resource("ns3::LogDistancePropagationLossModel")
82     ec.register_connection(channel, loss)
83
84     return channel
85
86 ec = ExperimentController(exp_id = "dce-custom-wifi-ping")
87
88 node = ec.register_resource("linux::Node")
89 ec.set(node, "hostname", "localhost")
90 ec.set(node, "cleanProcesses", True)
91
92 simu = ec.register_resource("linux::ns3::Simulation")
93 ec.set(simu, "verbose", True)
94 ec.register_connection(simu, node)
95
96 nsnode1 = add_ns3_node(ec, simu)
97 add_constant_mobility(ec, nsnode1, 0, 0, 0)
98 dev1, phy1 = add_device(ec, nsnode1, "10.0.0.1", "30")
99
100 nsnode2 = add_ns3_node(ec, simu)
101 add_constant_mobility(ec, nsnode2, 50, 50, 0)
102 dev2, phy2 = add_device(ec, nsnode2, "10.0.0.2", "30", access_point = True)
103
104 # Create channel
105 chan = add_wifi_channel(ec)
106 ec.register_connection(chan, phy1)
107 ec.register_connection(chan, phy2)
108
109 ### create applications
110 ping = ec.register_resource("linux::ns3::dce::Application")
111 ec.set (ping, "sources", "http://www.skbuff.net/iputils/iputils-s20101006.tar.bz2")
112 ec.set (ping, "build", "tar xvjf ${SRC}/iputils-s20101006.tar.bz2 && "
113         "cd iputils-s20101006/ && "
114         "sed -i 's/CFLAGS=/CFLAGS+=/g' Makefile && "
115         "make CFLAGS=-fPIC LDFLAGS='-pie -rdynamic' ping && "
116         "cp ping ${BIN_DCE} && cd - ")
117 ec.set (ping, "binary", "ping")
118 ec.set (ping, "stackSize", 1<<20)
119 ec.set (ping, "arguments", "-c 10;-s 1000;10.0.0.2")
120 ec.set (ping, "StartTime", "1s")
121 ec.set (ping, "StopTime", "20s")
122 ec.register_connection(ping, nsnode1)
123
124 ec.deploy()
125
126 ec.wait_finished([ping])
127
128 stdout = ec.trace(ping, "stdout") 
129
130 ec.shutdown()
131
132 print "PING OUTPUT", stdout
133