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