25e9b7f7f0491e5296921daa63732917cb8d5c4a
[nepi.git] / examples / openvswitch / ovs_ping_3_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 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 # Authors :  Julien Tribino <julien.tribino@inria.fr>
19 #          Alina Quereilhac <alina.quereilhac@inria.fr>
20 #
21 # Topology :
22 #
23 #         Switch1 ------ Switch2 ------- Switch2         
24 #            /              |                \           
25 #           /               |                 \          
26 #          /                |                  \         
27 #       Host1             Host3               Host2  
28 #
29 #
30 # Execution example:  
31
32 # $ PYTHONPATH=$PYTHONPATH:src/ python examples/openvswitch/ovs_ping_3_switches.py -n "192.168.3.0/24" -C "1.1.1.1" -s <slicename> -i /~/.ssh/id_rsa
33 #
34
35 from __future__ import print_function
36
37 from nepi.execution.ec import ExperimentController
38
39 import os
40 from optparse import OptionParser
41 import sys
42 import time
43
44 def add_node(ec, host, user, pl_user, pl_password, identity):
45     node = ec.register_resource("planetlab::Node")
46     ec.set(node, "hostname", host)
47     ec.set(node, "username", user)
48     ec.set(node, "identity", identity)
49
50     if pl_user:
51         ec.set(node, "pluser", pl_user)
52     if pl_password:
53         ec.set(node, "plpassword", pl_password)
54     if identity:
55         ec.set(node, "identity", identity)
56
57     ec.set(node, "cleanExperiment", True)
58     ec.set(node, "cleanProcesses", True)
59
60     return node
61
62 def add_ovs(ec, bridge_name, virtual_ip_pref, controller_ip, controller_port, node):
63     ovs = ec.register_resource("planetlab::OVSSwitch")
64     ec.set(ovs, "bridge_name", bridge_name)
65     ec.set(ovs, "virtual_ip_pref", virtual_ip_pref)
66     ec.set(ovs, "controller_ip", controller_ip)
67     ec.set(ovs, "controller_port", controller_port)
68     ec.register_connection(ovs, node)
69     return ovs
70
71 def add_port(ec, port_name, network, ovs):
72     port = ec.register_resource("planetlab::OVSPort")
73     ec.set(port, "port_name", port_name)
74     ec.set(port, "network", network)
75     ec.register_connection(port, ovs)
76     return port
77
78 def add_tap(ec, ip, prefix, pointopoint, node):
79     tap = ec.register_resource("planetlab::Tap")
80     ec.set(tap, "ip", ip)
81     ec.set(tap, "prefix", prefix)
82     ec.set(tap, "pointopoint", pointopoint)
83     ec.set(tap, "up", True)
84     ec.register_connection(tap, node)
85     return tap
86
87 def add_tunnel(ec, port0, tap):
88     tunnel = ec.register_resource("linux::UdpTunnel")
89     ec.register_connection(port0, tunnel)
90     ec.register_connection(tunnel, tap)
91     return tunnel
92
93 def add_app(ec, command, node):
94     app = ec.register_resource("linux::Application")
95     ec.set(app, "command", command)
96     ec.register_connection(app, node)
97
98     return app
99
100 def parse_args():
101     pl_slice = os.environ.get("PL_SLICE")
102     pl_user = os.environ.get("PL_USER")
103     pl_pass = os.environ.get("PL_PASS")
104     identity = os.environ.get("PL_SSHKEY")
105
106     usage = ("usage: %prog -a <host1> -b <host2> -c <host3> "
107             "-d <switch1> -e <switch2> -f <switch3> "
108             "-n <vsys_vnet> -C <controller> -s <slicename> -u <pl-user> " 
109             "-p <pl-password> -i <ssh-key> ")
110
111     switch1 = "planetlab2.virtues.fi"
112     switch2 = "planetlab2.upc.es"
113     switch3 = "inriarennes1.irisa.fr"
114     host1 = "planetlab2.ionio.gr"
115     host2 = "iraplab2.iralab.uni-karlsruhe.de"
116     host3 = "planetlab2.diku.dk"
117
118     parser = OptionParser(usage = usage)
119     parser.add_option("-a", "--host1", dest="host1", 
120         help="Hostname for PlanetLab host 1", default=host1)
121     parser.add_option("-b", "--host2", dest="host2", 
122         help="Hostname for PlanetLab host 2", default=host2)
123     parser.add_option("-c", "--host3", dest="host3", 
124         help="Hostname for PlanetLab host 3", default=host3)
125     parser.add_option("-d", "--switch1", dest="switch1", 
126         help="Hostname for PlanetLab switch 1", default=switch1)
127     parser.add_option("-e", "--switch2", dest="switch2", 
128         help="Hostname for PlanetLab switch 2", default=switch2)
129     parser.add_option("-f", "--switch3", dest="switch3", 
130         help="Hostname for PlanetLab switch 3", default=switch3)
131     parser.add_option("-n", "--vsys_vnet", dest="vsys_vnet", 
132         help="Overlay network address of the form x.x.x.x/yy. "
133             "Must correspond to the vsys_vnet tag on the PlanetLab slice")
134     parser.add_option("-C", "--controller", dest="controller", 
135         help="IP address for the OpenFlow controller, if one has been deployed",
136         default="1.1.1.1")
137     parser.add_option("-s", "--slicename", dest="slicename", 
138         help="Name of PlanetLab slice", 
139         default=pl_slice)
140     parser.add_option("-u", "--pl_user", dest="pl_user", 
141         help="PlanetLab user (email address)", 
142         default=pl_user)
143     parser.add_option("-p", "--pl_pass", dest="pl_pass", 
144         help="PlanetLab password", 
145         default=pl_pass)
146     parser.add_option("-i", "--identity", dest="identity", 
147         help="Path to SSH key", 
148         default=identity)
149
150     (options, args) = parser.parse_args()
151
152     return (options.host1, options.host2, options.host3, options.switch1, 
153             options.switch2, options.switch3, options.vsys_vnet, 
154             options.controller, options.slicename, options.pl_user, 
155             options.pl_pass, identity)
156
157 (host1, host2, host3, switch1, switch2, switch3, vsys_vnet, controller, 
158         slicename, pl_user, pl_pass, identity) = parse_args()
159
160 # Create the EC
161 ec = ExperimentController(exp_id = "ovs_3_switch")
162
163 net = vsys_vnet.split("/")
164 prefix = net[-1]
165 network = net[0]
166 net_segs = network.split(".")
167 ip1 = "%s.1" % ".".join(net_segs[:-1]) # x.x.x.1
168 ip2 = "%s.2" % ".".join(net_segs[:-1]) # x.x.x.2
169 ip3 = "%s.3" % ".".join(net_segs[:-1]) # x.x.x.3
170 ip4 = "%s.4" % ".".join(net_segs[:-1]) # x.x.x.4
171 ip5 = "%s.5" % ".".join(net_segs[:-1]) # x.x.x.5
172 ip6 = "%s.6" % ".".join(net_segs[:-1]) # x.x.x.6
173
174 ### Define topology
175
176 # Add switches
177 s1_node = add_node(ec, switch1, slicename, pl_user, pl_pass, identity)
178 s2_node = add_node(ec, switch2, slicename, pl_user, pl_pass, identity)
179 s3_node = add_node(ec, switch3, slicename, pl_user, pl_pass, identity)
180
181 # Add OVS switches 
182 addr1 = "%s/%s" % (ip1, prefix) # x.x.x.1/prefix
183 addr2 = "%s/%s" % (ip2, prefix) # x.x.x.2/prefix
184 addr3 = "%s/%s" % (ip3, prefix) # x.x.x.3/prefix
185 ovs1 = add_ovs(ec, "nepi_bridge_1", addr1, controller, "6633", s1_node)
186 ovs2 = add_ovs(ec, "nepi_bridge_2", addr2, controller, "6633", s2_node)
187 ovs3 = add_ovs(ec, "nepi_bridge_3", addr3, controller, "6633", s3_node)
188
189 # Add ports on ovs
190 port1 = add_port(ec, "nepi_port1", network, ovs1)
191 port4 = add_port(ec, "nepi_port4", network, ovs1)
192 port7 = add_port(ec, "nepi_port7", network, ovs1)
193 port2 = add_port(ec, "nepi_port2", network, ovs2)
194 port5 = add_port(ec, "nepi_port5", network, ovs2)
195 port3 = add_port(ec, "nepi_port3", network, ovs3)
196 port6 = add_port(ec, "nepi_port6", network, ovs3)
197
198 # Add hosts
199 h1_node = add_node(ec, host1, slicename, pl_user, pl_pass, identity)
200 h2_node = add_node(ec, host2, slicename, pl_user, pl_pass, identity)
201 h3_node = add_node(ec, host3, slicename, pl_user, pl_pass, identity)
202
203 ### Add overlay
204 # Add tap devices
205 tap1 = add_tap(ec, ip3, prefix, ip1, h1_node)
206 tap2 = add_tap(ec, ip4, prefix, ip2, h2_node)
207 tap3 = add_tap(ec, ip5, prefix, ip6, h3_node)
208
209 ### Add Pings
210 # Connect the nodes
211 tunnel1 = add_tunnel(ec, port1, tap1)
212 tunnel2 = add_tunnel(ec, port2, tap2)
213 tunnel3 = add_tunnel(ec, port3, tap3)
214 tunnel4 = add_tunnel(ec, port4, port5)
215 tunnel5 = add_tunnel(ec, port7, port6)
216
217 apps = dict()
218 r2ip = dict({
219     s1_node: ("Switch 1", ip1),  
220     s2_node: ("Switch 2", ip2),
221     s3_node: ("Switch 3", ip3),
222     h1_node: ("Host 1", ip4),
223     h2_node: ("Host 2", ip5),
224     h3_node: ("Host 3", ip6),
225     })
226
227 # Ping from all resources to all other resources
228 for r1, (n1, ip1) in r2ip.items():
229     for r2, (n2, ip2) in r2ip.items():
230         if r1 == r2:
231             continue
232
233         app = add_app(ec, "ping -c5 %s" % ip2, r1)
234         key = "Ping from %s to %s" % (n1, n2)
235         apps[key] = app
236
237 ec.deploy()
238
239 ec.wait_finished(apps.values())
240
241 # collect results
242 for key, app in apps.items():
243     stdout = ec.trace(app, "stdout")
244     print("***************************", key, "************************")
245     print(stdout)
246     print("\n")
247
248 # Delete the overlay network
249 ec.shutdown()