84c28d0b24f74f24ddff22964fd9310c44e77841
[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 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 #         x.x.x.1          x.x.x.2
24 #         Switch1 -------- Switch2         
25 #            /                \           
26 #           /                  \          
27 #          /                    \         
28 #       Host1                  Host2      
29 #       x.x.x.3                x.x.x.4
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.register_connection(tap, node)
82     return tap
83
84 def add_tunnel(ec, port0, tap):
85     tunnel = ec.register_resource("linux::UdpTunnel")
86     ec.register_connection(port0, tunnel)
87     ec.register_connection(tunnel, tap)
88     return tunnel
89
90 def add_app(ec, command, node):
91     app = ec.register_resource("linux::Application")
92     ec.set(app, "command", command)
93     ec.register_connection(app, node)
94     return app
95
96 def parse_args():
97     pl_slice = os.environ.get("PL_SLICE")
98     pl_user = os.environ.get("PL_USER")
99     pl_pass = os.environ.get("PL_PASS")
100     identity = os.environ.get("PL_SSHKEY")
101
102     usage = ("usage: %prog -a <host1> -b <host2> -c <switch1> -d <switch2> "
103             "-n <vsys_vnet> -C <controller> -s <slicename> -u <pl-user> " 
104             "-p <pl-password> -i <ssh-key> ")
105
106     switch1 = "planetlab2.virtues.fi"
107     switch2 = "planetlab2.upc.es"
108     host1 = "planetlab2.s3.kth.se"
109     host2 = "iraplab2.iralab.uni-karlsruhe.de"
110
111     parser = OptionParser(usage = usage)
112     parser.add_option("-a", "--host1", dest="host1", 
113         help="Hostname for PlanetLab host 1", default=host1)
114     parser.add_option("-b", "--host2", dest="host2", 
115         help="Hostname for PlanetLab host 2", default=host2)
116     parser.add_option("-c", "--switch1", dest="switch1", 
117         help="Hostname for PlanetLab switch 1", default=switch1)
118     parser.add_option("-d", "--switch2", dest="switch2", 
119         help="Hostname for PlanetLab switch 2", default=switch2)
120     parser.add_option("-n", "--vsys_vnet", dest="vsys_vnet", 
121         help="Overlay network address of the form x.x.x.x/yy. "
122             "Must correspond to the vsys_vnet tag on the PlanetLab slice")
123     parser.add_option("-C", "--controller", dest="controller", 
124         help="IP address for the OpenFlow controller, if one has been deployed",
125         default="1.1.1.1")
126     parser.add_option("-s", "--slicename", dest="slicename", 
127         help="Name of PlanetLab slice", 
128         default=pl_slice)
129     parser.add_option("-u", "--pl_user", dest="pl_user", 
130         help="PlanetLab user (email address)", 
131         default=pl_user)
132     parser.add_option("-p", "--pl_pass", dest="pl_pass", 
133         help="PlanetLab password", 
134         default=pl_pass)
135     parser.add_option("-i", "--identity", dest="identity", 
136         help="Path to SSH key", 
137         default=identity)
138
139     (options, args) = parser.parse_args()
140
141     return (options.host1, options.host2, options.switch1, options.switch2, 
142             options.vsys_vnet, options.controller, options.slicename, 
143             options.pl_user, options.pl_pass, identity)
144
145 (host1, host2, switch1, switch2, vsys_vnet, controller, slicename, 
146         pl_user, pl_pass, identity) = parse_args()
147
148 # Create the EC
149 ec = ExperimentController(exp_id = "ovs_2_switch")
150
151 net = vsys_vnet.split("/")
152 prefix = net[-1]
153 network = net[0]
154 net_segs = network.split(".")
155 ip1 = "%s.1" % ".".join(net_segs[:-1]) # x.x.x.1
156 ip2 = "%s.2" % ".".join(net_segs[:-1]) # x.x.x.2
157 ip3 = "%s.3" % ".".join(net_segs[:-1]) # x.x.x.3
158 ip4 = "%s.4" % ".".join(net_segs[:-1]) # x.x.x.4
159
160 ### Define topology
161
162 # Add switches
163 s1_node = add_node(ec, switch1, slicename, pl_user, pl_pass, identity)
164 s2_node = add_node(ec, switch2, slicename, pl_user, pl_pass, identity)
165
166 # Add OVS switches 
167 addr1 = "%s/%s" % (ip1, prefix) # x.x.x.1/prefix
168 addr2 = "%s/%s" % (ip2, prefix) # x.x.x.2/prefix
169 ovs1 = add_ovs(ec, "nepi_bridge_1", addr1, controller, "6633", s1_node)
170 ovs2 = add_ovs(ec, "nepi_bridge_2", addr2, controller, "6633", s2_node)
171
172 # Add ports on OVS
173 port1 = add_port(ec, "nepi_port1", network, ovs1)
174 port3 = add_port(ec, "nepi_port3", network, ovs1)
175 port2 = add_port(ec, "nepi_port2", network, ovs2)
176 port4 = add_port(ec, "nepi_port4", network, ovs2)
177
178 # Add hosts
179 h1_node = add_node(ec, host1, slicename, pl_user, pl_pass, identity)
180 h2_node = add_node(ec, host2, slicename, pl_user, pl_pass, identity)
181
182 ### Add overlay
183 # Add tap devices
184 tap1 = add_tap(ec, ip3, prefix, ip1, h1_node)
185 tap2 = add_tap(ec, ip4, prefix, ip2, h2_node)
186
187 # Connect the nodes
188 tunnel1 = add_tunnel(ec, port1, tap1)
189 tunnel2 = add_tunnel(ec, port2, tap2)
190 tunnel3 = add_tunnel(ec, port3, port4)
191
192 ### Add Ping
193 app = add_app(ec, "ping -c5 %s" % ip4, h1_node)
194
195 ec.deploy()
196
197 ec.wait_finished([app])
198
199 # Retreive ping results and save them in a file
200 stdout = ec.trace(app, "stdout")
201
202 # Delete the overlay network
203 ec.shutdown()
204
205 print stdout
206