3fe3d173c5d8b1d451369b936acd7401b28bd686
[nepi.git] / examples / openvswitch / ovs_ping_2_switches.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 as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
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.
15 #
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/>.
18 #
19 # Authors :  Julien Tribino <julien.tribino@inria.fr>
20 #          Alina Quereilhac <alina.quereilhac@inria.fr>
21 #
22 # Topology :
23
24 #         Switch1 -------- Switch2         
25 #            /                \           
26 #           /                  \          
27 #          /                    \         
28 #       Host1                  Host2      
29 #
30 #
31 # Execution example:  
32
33 # $ PYTHONPATH=$PYTHONPATH:src/ python examples/openvswitch/ovs_ping_2_switches.py -n "192.168.3.0/24" -C "1.1.1.1" -s <slicename> -i /~/.ssh/id_rsa
34 #
35
36 from nepi.execution.ec import ExperimentController
37
38 import os
39 from optparse import OptionParser
40 import sys
41 import time
42
43 def add_node(ec, host, user, pl_user, pl_password, identity):
44     node = ec.register_resource("planetlab::Node")
45     ec.set(node, "hostname", host)
46     ec.set(node, "username", user)
47
48     if identity:
49         ec.set(node, "identity", identity)
50     if pl_user:
51         ec.set(node, "pluser", pl_user)
52     if pl_password:
53         ec.set(node, "plpassword", pl_password)
54
55     ec.set(node, "cleanExperiment", True)
56     ec.set(node, "cleanProcesses", True)
57
58     return node
59
60 def add_ovs(ec, bridge_name, virtual_ip_pref, controller_ip, controller_port, node):
61     ovs = ec.register_resource("planetlab::OVSSwitch")
62     ec.set(ovs, "bridge_name", bridge_name)
63     ec.set(ovs, "virtual_ip_pref", virtual_ip_pref)
64     ec.set(ovs, "controller_ip", controller_ip)
65     ec.set(ovs, "controller_port", controller_port)
66     ec.register_connection(ovs, node)
67     return ovs
68
69 def add_port(ec, port_name, network, ovs):
70     port = ec.register_resource("planetlab::OVSPort")
71     ec.set(port, "port_name", port_name)
72     ec.set(port, "network", network)
73     ec.register_connection(port, ovs)
74     return port
75
76 def add_tap(ec, ip, prefix, pointopoint, node):
77     tap = ec.register_resource("planetlab::Tap")
78     ec.set(tap, "ip", ip)
79     ec.set(tap, "prefix", prefix)
80     ec.set(tap, "pointopoint", pointopoint)
81     ec.set(tap, "up", True)
82     ec.register_connection(tap, node)
83     return tap
84
85 def add_tunnel(ec, port0, tap):
86     tunnel = ec.register_resource("linux::UdpTunnel")
87     ec.register_connection(port0, tunnel)
88     ec.register_connection(tunnel, tap)
89     return tunnel
90
91 def add_app(ec, command, node):
92     app = ec.register_resource("linux::Application")
93     ec.set(app, "command", command)
94     ec.register_connection(app, node)
95     return app
96
97 def parse_args():
98     pl_slice = os.environ.get("PL_SLICE")
99     pl_user = os.environ.get("PL_USER")
100     pl_pass = os.environ.get("PL_PASS")
101     identity = os.environ.get("PL_SSHKEY")
102
103     usage = ("usage: %prog -a <host1> -b <host2> -c <switch1> -d <switch2> "
104             "-n <vsys_vnet> -C <controller> -s <slicename> -u <pl-user> " 
105             "-p <pl-password> -i <ssh-key> ")
106
107     switch1 = "planetlab2.virtues.fi"
108     switch2 = "planetlab2.upc.es"
109     host1 = "planetlab2.ionio.gr"
110     host2 = "iraplab2.iralab.uni-karlsruhe.de"
111
112     parser = OptionParser(usage = usage)
113     parser.add_option("-a", "--host1", dest="host1", 
114         help="Hostname for PlanetLab host 1", default=host1)
115     parser.add_option("-b", "--host2", dest="host2", 
116         help="Hostname for PlanetLab host 2", default=host2)
117     parser.add_option("-c", "--switch1", dest="switch1", 
118         help="Hostname for PlanetLab switch 1", default=switch1)
119     parser.add_option("-d", "--switch2", dest="switch2", 
120         help="Hostname for PlanetLab switch 2", default=switch2)
121     parser.add_option("-n", "--vsys_vnet", dest="vsys_vnet", 
122         help="Overlay network address of the form x.x.x.x/yy. "
123             "Must correspond to the vsys_vnet tag on the PlanetLab slice")
124     parser.add_option("-C", "--controller", dest="controller", 
125         help="IP address for the OpenFlow controller, if one has been deployed",
126         default="1.1.1.1")
127     parser.add_option("-s", "--slicename", dest="slicename", 
128         help="Name of PlanetLab slice", 
129         default=pl_slice)
130     parser.add_option("-u", "--pl_user", dest="pl_user", 
131         help="PlanetLab user (email address)", 
132         default=pl_user)
133     parser.add_option("-p", "--pl_pass", dest="pl_pass", 
134         help="PlanetLab password", 
135         default=pl_pass)
136     parser.add_option("-i", "--identity", dest="identity", 
137         help="Path to SSH key", 
138         default=identity)
139
140     (options, args) = parser.parse_args()
141
142     return (options.host1, options.host2, options.switch1, options.switch2, 
143             options.vsys_vnet, options.controller, options.slicename, 
144             options.pl_user, options.pl_pass, identity)
145
146 (host1, host2, switch1, switch2, vsys_vnet, controller, slicename, 
147         pl_user, pl_pass, identity) = parse_args()
148
149 # Create the EC
150 ec = ExperimentController(exp_id = "ovs_2_switch")
151
152 net = vsys_vnet.split("/")
153 prefix = net[-1]
154 network = net[0]
155 net_segs = network.split(".")
156 ip1 = "%s.1" % ".".join(net_segs[:-1]) # x.x.x.1
157 ip2 = "%s.2" % ".".join(net_segs[:-1]) # x.x.x.2
158 ip3 = "%s.3" % ".".join(net_segs[:-1]) # x.x.x.3
159 ip4 = "%s.4" % ".".join(net_segs[:-1]) # x.x.x.4
160
161 s1_node = add_node(ec, switch1, slicename, pl_user, pl_pass, identity)
162 s2_node = add_node(ec, switch2, slicename, pl_user, pl_pass, identity)
163
164 # Add OVS switches 
165 addr1 = "%s/%s" % (ip1, prefix) # x.x.x.1/prefix
166 addr2 = "%s/%s" % (ip2, prefix) # x.x.x.2/prefix
167 ovs1 = add_ovs(ec, "nepi_bridge_1", addr1, controller, "6633", s1_node)
168 ovs2 = add_ovs(ec, "nepi_bridge_2", addr2, controller, "6633", s2_node)
169
170 # Add ports on OVS
171 port1 = add_port(ec, "nepi_port1", network, ovs1)
172 port3 = add_port(ec, "nepi_port3", network, ovs1)
173 port2 = add_port(ec, "nepi_port2", network, ovs2)
174 port4 = add_port(ec, "nepi_port4", network, ovs2)
175
176 h1_node = add_node(ec, host1, slicename, pl_user, pl_pass, identity)
177 h2_node = add_node(ec, host2, slicename, pl_user, pl_pass, identity)
178
179 # Add tap devices
180 tap1 = add_tap(ec, ip3, prefix, ip1, h1_node)
181 tap2 = add_tap(ec, ip4, prefix, ip2, h2_node)
182
183 # Connect the nodes
184 tunnel1 = add_tunnel(ec, port1, tap1)
185 tunnel2 = add_tunnel(ec, port2, tap2)
186 tunnel3 = add_tunnel(ec, port3, port4)
187
188 # Ping switch 1 to switch2
189 app1 = add_app(ec, "ping -c5 %s" % ip2, s1_node)
190 # Ping switch 1 to host1
191 app2 = add_app(ec, "ping -c5 %s" % ip3, s1_node)
192 # Ping switch 1 to host2
193 app3 = add_app(ec, "ping -c5 %s" % ip4, s1_node)
194
195 # Ping switch 2 to switch1
196 app4 = add_app(ec, "ping -c5 %s" % ip1, s2_node)
197 # Ping switch 2 to host1
198 app5 = add_app(ec, "ping -c5 %s" % ip3, s2_node)
199 # Ping switch 2 to host2
200 app6 = add_app(ec, "ping -c5 %s" % ip4, s2_node)
201
202 # Ping host 1 to switch 1
203 app7 = add_app(ec, "ping -c5 %s" % ip1, h1_node)
204 # Ping host 1 to switch 2
205 app8 = add_app(ec, "ping -c5 %s" % ip2, h1_node)
206 # Ping host 1 to host 2
207 app9 = add_app(ec, "ping -c5 %s" % ip4, h1_node)
208
209 # Ping host 2 to switch 1
210 app10 = add_app(ec, "ping -c5 %s" % ip1, h2_node)
211 # Ping host 2 to switch 2
212 app11 = add_app(ec, "ping -c5 %s" % ip2, h2_node)
213 # Ping host 2 to host1
214 app12 = add_app(ec, "ping -c5 %s" % ip3, h2_node)
215
216 ec.deploy()
217
218 ec.wait_finished([app1, app2, app3, app4, app5, app6, 
219     app7, app8, app9, app10, app11, app12])
220
221 # Retreive ping results and save them in a file
222 ping1 = ec.trace(app1, "stdout")
223 ping2 = ec.trace(app2, "stdout")
224 ping3 = ec.trace(app3, "stdout")
225 ping4 = ec.trace(app4, "stdout")
226 ping5 = ec.trace(app5, "stdout")
227 ping6 = ec.trace(app6, "stdout")
228 ping7 = ec.trace(app7, "stdout")
229 ping8 = ec.trace(app8, "stdout")
230 ping9 = ec.trace(app9, "stdout")
231 ping10 = ec.trace(app10, "stdout")
232 ping11 = ec.trace(app11, "stdout")
233 ping12 = ec.trace(app12, "stdout")
234
235 if not ping12:
236   ec.shutdown()
237   sys.exit("No ping found")
238
239 f = open("ovs_ping_2_switches.txt", 'w')
240 f.write("************ Ping From Switch 1 : %s ********************\n\n\n" % ip1)
241 f.write(ping1)
242 f.write("----------------------------------------\n\n")
243 f.write(ping2)
244 f.write("----------------------------------------\n\n")
245 f.write(ping3)
246
247 f.write("************ Ping From Switch 2 : %s ********************\n\n\n" % ip2)
248 f.write(ping4)
249 f.write("----------------------------------------\n\n")
250 f.write(ping5)
251 f.write("----------------------------------------\n\n")
252 f.write(ping6)
253
254 f.write("************ Ping From Host 1 : %s ********************\n\n\n" % ip3)
255 f.write(ping7)
256 f.write("----------------------------------------\n\n")
257 f.write(ping8)
258 f.write("----------------------------------------\n\n")
259 f.write(ping9)
260
261 f.write("************ Ping From Host 2 : %s ********************\n\n\n" % ip4)
262 f.write(ping10)
263 f.write("----------------------------------------\n\n")
264 f.write(ping11)
265 f.write("----------------------------------------\n\n")
266 f.write(ping12)
267
268 f.close()
269
270 # Delete the overlay network
271 ec.shutdown()
272
273