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