becd61cfd6fea93fc050027bd274c51ec68871ff
[nepi.git] / examples / openvswitch / ovs_ping.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  
24 #            /               
25 #           /            
26 #          /               
27 #       Host1           
28 #
29 #
30 # Execution example:  
31
32 # $ PYTHONPATH=$PYTHONPATH:src/ python examples/openvswitch/ovs_ping.py -n "192.168.3.0/24" -s <slicename> -i /~/.ssh/id_rsa
33 #
34
35 from nepi.execution.ec import ExperimentController
36
37 import os
38 from optparse import OptionParser
39 import sys
40 import time
41
42 def parse_args():
43     pl_slice = os.environ.get("PL_SLICE")
44     pl_user = os.environ.get("PL_USER")
45     pl_pass = os.environ.get("PL_PASS")
46     identity = os.environ.get("PL_SSHKEY")
47
48     usage = ("usage: %prog -a <host> -b <switch>"
49             "-n <vsys_vnet> -C <controller> -s <slicename> -u <pl-user> " 
50             "-p <pl-password> -i <ssh-key> ")
51
52     switch = "planetlab2.virtues.fi"
53     host = "planetlab2.ionio.gr"
54
55     parser = OptionParser(usage = usage)
56     parser.add_option("-a", "--host", dest="host", 
57         help="Hostname for PlanetLab host", default=host)
58     parser.add_option("-b", "--switch", dest="switch", 
59         help="Hostname for PlanetLab switch", default=switch)
60     parser.add_option("-n", "--vsys_vnet", dest="vsys_vnet", 
61         help="Overlay network address of the form x.x.x.x/yy. "
62             "Must correspond to the vsys_vnet tag on the PlanetLab slice")
63     parser.add_option("-C", "--controller", dest="controller", 
64         help="IP address for the OpenFlow controller, if one has been deployed")
65     parser.add_option("-s", "--slicename", dest="slicename", 
66         help="Name of PlanetLab slice", 
67         default=pl_slice)
68     parser.add_option("-u", "--pl_user", dest="pl_user", 
69         help="PlanetLab user (email address)", 
70         default=pl_user)
71     parser.add_option("-p", "--pl_pass", dest="pl_pass", 
72         help="PlanetLab password", 
73         default=pl_pass)
74     parser.add_option("-i", "--identity", dest="identity", 
75         help="Path to SSH key", 
76         default=identity)
77
78     (options, args) = parser.parse_args()
79
80     return (options.host, options.switch,  
81             options.vsys_vnet, options.controller, options.slicename, 
82             options.pl_user, options.pl_pass, identity)
83
84 (host, switch, vsys_vnet, controller, slicename, 
85         pl_user, pl_pass, identity) = parse_args()
86
87 # Create the EC
88 ec = ExperimentController(exp_id = "ovs_ping")
89
90 net = vsys_vnet.split("/")
91 prefix = net[-1]
92 network = net[0]
93 net_segs = network.split(".")
94 ip1 = "%s.1" % ".".join(net_segs[:-1]) # x.x.x.1
95 ip2 = "%s.2" % ".".join(net_segs[:-1]) # x.x.x.2
96
97 # Add OVS switch 
98 node1 = ec.register_resource("planetlab::Node")
99 ec.set(node1, "hostname", switch)
100 ec.set(node1, "username", slicename)
101 ec.set(node1, "identity", identity)
102 ec.set(node1, "pluser", pl_user)
103 ec.set(node1, "plpassword", pl_pass)
104 ec.set(node1, "cleanExperiment", True)
105 ec.set(node1, "cleanProcesses", True)
106
107 addr1 = "%s/%s" % (ip1, prefix) # x.x.x.1/prefix
108 ovs = ec.register_resource("planetlab::OVSSwitch")
109 ec.set(ovs, "bridge_name", "nepi_bridge")
110 ec.set(ovs, "virtual_ip_pref", addr1)
111 ec.set(ovs, "controller_ip", controller)
112 ec.set(ovs, "controller_port", "6633")
113 ec.register_connection(ovs, node1)
114
115 # Add host
116 node2 = ec.register_resource("planetlab::Node")
117 ec.set(node2, "hostname", host)
118 ec.set(node2, "username", slicename)
119 ec.set(node2, "identity", identity)
120 ec.set(node2, "pluser", pl_user)
121 ec.set(node2, "plpassword", pl_pass)
122 ec.set(node2, "cleanExperiment", True)
123 ec.set(node2, "cleanProcesses", True)
124
125 # Creating overlay
126 # Add tap device
127 tap = ec.register_resource("planetlab::Tap")
128 ec.set(tap, "ip", ip2)
129 ec.set(tap, "prefix", prefix)
130 ec.set(tap, "pointopoint", ip1)
131 ec.register_connection(tap, node2)
132
133 # Add ports on OVS
134 port = ec.register_resource("planetlab::OVSPort")
135 ec.set(port, "port_name", "nepi_port")
136 ec.set(port, "network", network)
137 ec.register_connection(port, ovs)
138
139 # Add tunnel between host and switch
140 tunnel = ec.register_resource("linux::UdpTunnel")
141 ec.register_connection(port, tunnel)
142 ec.register_connection(tunnel, tap)
143
144 ## Ping switch from host
145 app = ec.register_resource("linux::Application")
146 ec.set(app, "command", "ping -c5 %s" % ip1)
147 ec.register_connection(app, node2)
148
149 ec.deploy()
150
151 ec.wait_finished([app])
152
153 # Retreive ping results and save them in a file
154 ping = ec.trace(app, "stdout")
155
156 print ping
157
158 # Delete the overlay network
159 ec.shutdown()
160
161