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