use print() - import print_function - should be fine for both py2 and py3
[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 __future__ import print_function
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 parse_args():
45     pl_slice = os.environ.get("PL_SLICE")
46     pl_user = os.environ.get("PL_USER")
47     pl_pass = os.environ.get("PL_PASS")
48     identity = os.environ.get("PL_SSHKEY")
49
50     usage = ("usage: %prog -a <host> -b <switch>"
51             "-n <vsys_vnet> -C <controller> -s <slicename> -u <pl-user> " 
52             "-p <pl-password> -i <ssh-key> ")
53
54     switch = "planetlab2.virtues.fi"
55     host = "planetlab2.ionio.gr"
56
57     parser = OptionParser(usage = usage)
58     parser.add_option("-a", "--host", dest="host", 
59         help="Hostname for PlanetLab host", default=host)
60     parser.add_option("-b", "--switch", dest="switch", 
61         help="Hostname for PlanetLab switch", default=switch)
62     parser.add_option("-n", "--vsys_vnet", dest="vsys_vnet", 
63         help="Overlay network address of the form x.x.x.x/yy. "
64             "Must correspond to the vsys_vnet tag on the PlanetLab slice")
65     parser.add_option("-C", "--controller", dest="controller", 
66         help="IP address for the OpenFlow controller, if one has been deployed")
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.register_connection(tap, node2)
134
135 # Add ports on OVS
136 port = ec.register_resource("planetlab::OVSPort")
137 ec.set(port, "port_name", "nepi_port")
138 ec.set(port, "network", network)
139 ec.register_connection(port, ovs)
140
141 # Add tunnel between host and switch
142 tunnel = ec.register_resource("linux::UdpTunnel")
143 ec.register_connection(port, tunnel)
144 ec.register_connection(tunnel, tap)
145
146 ## Ping switch from host
147 app = ec.register_resource("linux::Application")
148 ec.set(app, "command", "ping -c5 %s" % ip1)
149 ec.register_connection(app, node2)
150
151 ec.deploy()
152
153 ec.wait_finished([app])
154
155 # Retreive ping results and save them in a file
156 ping = ec.trace(app, "stdout")
157
158 print(ping)
159
160 # Delete the overlay network
161 ec.shutdown()