Serious refactoring of TUN/TAP and tunnel code. Linux udp/gre tunnels not yet functional
[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 #         x.x.x.1          x.x.x.2
25 #         Switch1 -------- Switch2         
26 #            /                \           
27 #           /                  \          
28 #          /                    \         
29 #       Host1                  Host2      
30 #       x.x.x.3                x.x.x.4
31 #
32 # Execution example:  
33
34 # $ 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
35 #
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
49     if identity:
50         ec.set(node, "identity", identity)
51     if pl_user:
52         ec.set(node, "pluser", pl_user)
53     if pl_password:
54         ec.set(node, "plpassword", pl_password)
55
56     ec.set(node, "cleanExperiment", True)
57     ec.set(node, "cleanProcesses", True)
58
59     return node
60
61 def add_ovs(ec, bridge_name, virtual_ip_pref, controller_ip, controller_port, node):
62     ovs = ec.register_resource("planetlab::OVSSwitch")
63     ec.set(ovs, "bridge_name", bridge_name)
64     ec.set(ovs, "virtual_ip_pref", virtual_ip_pref)
65     ec.set(ovs, "controller_ip", controller_ip)
66     ec.set(ovs, "controller_port", controller_port)
67     ec.register_connection(ovs, node)
68     return ovs
69
70 def add_port(ec, port_name, network, ovs):
71     port = ec.register_resource("planetlab::OVSPort")
72     ec.set(port, "port_name", port_name)
73     ec.set(port, "network", network)
74     ec.register_connection(port, ovs)
75     return port
76
77 def add_tap(ec, ip, prefix, pointopoint, node):
78     tap = ec.register_resource("planetlab::Tap")
79     ec.set(tap, "ip", ip)
80     ec.set(tap, "prefix", prefix)
81     ec.set(tap, "pointopoint", pointopoint)
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.s3.kth.se"
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 ### Define topology
162
163 # Add switches
164 s1_node = add_node(ec, switch1, slicename, pl_user, pl_pass, identity)
165 s2_node = add_node(ec, switch2, slicename, pl_user, pl_pass, identity)
166
167 # Add OVS switches 
168 addr1 = "%s/%s" % (ip1, prefix) # x.x.x.1/prefix
169 addr2 = "%s/%s" % (ip2, prefix) # x.x.x.2/prefix
170 ovs1 = add_ovs(ec, "nepi_bridge_1", addr1, controller, "6633", s1_node)
171 ovs2 = add_ovs(ec, "nepi_bridge_2", addr2, controller, "6633", s2_node)
172
173 # Add ports on OVS
174 port1 = add_port(ec, "nepi_port1", network, ovs1)
175 port3 = add_port(ec, "nepi_port3", network, ovs1)
176 port2 = add_port(ec, "nepi_port2", network, ovs2)
177 port4 = add_port(ec, "nepi_port4", network, ovs2)
178
179 # Add hosts
180 h1_node = add_node(ec, host1, slicename, pl_user, pl_pass, identity)
181 h2_node = add_node(ec, host2, slicename, pl_user, pl_pass, identity)
182
183 ### Add overlay
184 # Add tap devices
185 tap1 = add_tap(ec, ip3, prefix, ip1, h1_node)
186 tap2 = add_tap(ec, ip4, prefix, ip2, h2_node)
187
188 # Connect the nodes
189 tunnel1 = add_tunnel(ec, port1, tap1)
190 tunnel2 = add_tunnel(ec, port2, tap2)
191 tunnel3 = add_tunnel(ec, port3, port4)
192
193 ### Add Ping
194 app = add_app(ec, "ping -c5 %s" % ip4, h1_node)
195
196 ec.deploy()
197
198 ec.wait_finished([app])
199
200 # Retreive ping results and save them in a file
201 stdout = ec.trace(app, "stdout")
202
203 # Delete the overlay network
204 ec.shutdown()
205
206 print stdout
207