Changed the endpoint_ip and endpoint_prefix attributes of TAP/TUN endpoints for ip...
[nepi.git] / examples / openvswitch / ovs_ping_2switches.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 -------- Switch2         
25 #            /                \           
26 #           /                  \          
27 #          /                    \         
28 #       Host1                  Host2      
29
30
31
32 from nepi.execution.ec import ExperimentController
33 import os, time
34 import sys
35
36 def add_node(ec, host, user, pl_user, pl_password):
37     node = ec.register_resource("planetlab::Node")
38     ec.set(node, "hostname", host)
39     ec.set(node, "username", user)
40     if pl_user:
41         ec.set(node, "pluser", pl_user)
42     if pl_password:
43         ec.set(node, "plpassword", pl_password)
44     ec.set(node, "cleanExperiment", True)
45     ec.set(node, "cleanProcesses", True)
46
47     return node
48
49 def add_ovs(ec, bridge_name, virtual_ip_pref, controller_ip, controller_port, node):
50     ovs = ec.register_resource("planetlab::OVSSwitch")
51     ec.set(ovs, "bridge_name", bridge_name)
52     ec.set(ovs, "virtual_ip_pref", virtual_ip_pref)
53     ec.set(ovs, "controller_ip", controller_ip)
54     ec.set(ovs, "controller_port", controller_port)
55     ec.register_connection(ovs, node)
56     return ovs
57
58 def add_port(ec, port_name, network, ovs):
59     port = ec.register_resource("planetlab::OVSPort")
60     ec.set(port, "port_name", port_name)
61     ec.set(port, "network", network)
62     ec.register_connection(port, ovs)
63     return port
64
65 def add_tap(ec, ip, prefix, pointopoint, node):
66     tap = ec.register_resource("planetlab::Tap")
67     ec.set(tap, "ip", ip)
68     ec.set(tap, "prefix", prefix)
69     ec.set(tap, "pointopoint", pointopoint)
70     ec.set(tap, "up", True)
71     ec.register_connection(tap, node)
72     return tap
73
74 def add_tunnel(ec, port0, tap):
75     tunnel = ec.register_resource("linux::UdpTunnel")
76     ec.register_connection(port0, tunnel)
77     ec.register_connection(tunnel, tap)
78     return tunnel
79
80 def add_app(ec, command, node):
81     app = ec.register_resource("linux::Application")
82     ec.set(app, "command", command)
83     ec.register_connection(app, node)
84     return app
85
86 # Create the EC
87 ec = ExperimentController(exp_id = "test")
88
89 #XXX : Need to put 4 working nodes or to let Nepi find for you
90 switch1 = "planetlab2.virtues.fi"
91 switch2 = "planetlab2.upc.es"
92 host1 = "planetlab2.ionio.gr"
93 host2 = "iraplab2.iralab.uni-karlsruhe.de"
94
95 ip_controller = "1.1.1.1"
96
97 #XXX : Depends on the Vsys_tag of your slice
98 network = "192.168.3.0"
99
100 #XXX : Name of your slice
101 slicename = "inria_nepi"
102
103 pl_user = os.environ.get("PL_USER")
104 pl_password = os.environ.get("PL_PASS")
105
106 s1_node = add_node(ec, switch1, slicename, pl_user, pl_password)
107 s2_node = add_node(ec, switch2, slicename, pl_user, pl_password)
108
109 # Add switches 
110 ovs1 = add_ovs(ec, "nepi_bridge_1", "192.168.3.1/24", ip_controller, "6633", s1_node)
111 ovs2 = add_ovs(ec, "nepi_bridge_2", "192.168.3.2/24", ip_controller, "6633", s2_node)
112
113 # Add ports on ovs
114 port1 = add_port(ec, "nepi_port1", network, ovs1)
115 port3 = add_port(ec, "nepi_port3", network, ovs1)
116 port2 = add_port(ec, "nepi_port2", network, ovs2)
117 port4 = add_port(ec, "nepi_port4", network, ovs2)
118
119 h1_node = add_node(ec, host1, slicename, pl_user, pl_password)
120 h2_node = add_node(ec, host2, slicename, pl_user, pl_password)
121
122 # Add tap devices
123 tap1 = add_tap(ec, "192.168.3.3", "24", "192.168.3.1", h1_node)
124 tap2 = add_tap(ec, "192.168.3.4", "24", "192.168.3.2", h2_node)
125
126 # Connect the nodes
127 tunnel1 = add_tunnel(ec, port1, tap1)
128 tunnel2 = add_tunnel(ec, port2, tap2)
129 tunnel3 = add_tunnel(ec, port3, port4)
130
131 # Add ping commands
132 app1 = add_app(ec, "ping -c5 192.168.3.2", s1_node)
133 app2 = add_app(ec, "ping -c5 192.168.3.3", s1_node)
134 app3 = add_app(ec, "ping -c5 192.168.3.4", s1_node)
135 app4 = add_app(ec, "ping -c5 192.168.3.1", s2_node)
136 app5 = add_app(ec, "ping -c5 192.168.3.3", s2_node)
137 app6 = add_app(ec, "ping -c5 192.168.3.4", s2_node)
138 app7 = add_app(ec, "ping -c5 192.168.3.1", h1_node)
139 app8 = add_app(ec, "ping -c5 192.168.3.2", h1_node)
140 app9 = add_app(ec, "ping -c5 192.168.3.4", h1_node)
141 app10 = add_app(ec, "ping -c5 192.168.3.1", h2_node)
142 app11 = add_app(ec, "ping -c5 192.168.3.2", h2_node)
143 app12 = add_app(ec, "ping -c5 192.168.3.3", h2_node)
144
145 ec.deploy()
146
147 ec.wait_finished([app1, app2, app3, app4, app5, app6, app7, app8, app9, app10, app11, app12])
148
149 # Retreive ping results and save
150 # them in a file
151 ping1 = ec.trace(app1, 'stdout')
152 ping2 = ec.trace(app2, 'stdout')
153 ping3 = ec.trace(app3, 'stdout')
154 ping4 = ec.trace(app4, 'stdout')
155 ping5 = ec.trace(app5, 'stdout')
156 ping6 = ec.trace(app6, 'stdout')
157 ping7 = ec.trace(app7, 'stdout')
158 ping8 = ec.trace(app8, 'stdout')
159 ping9 = ec.trace(app9, 'stdout')
160 ping10 = ec.trace(app10, 'stdout')
161 ping11 = ec.trace(app11, 'stdout')
162 ping12 = ec.trace(app12, 'stdout')
163
164 if not ping12:
165   ec.shutdown()
166   sys.exit("No ping found")
167
168 f = open("examples/openvswitch/ovs_ping_2switches.txt", 'w')
169 f.write("************ Ping From Switch 1 : 192.168.3.1 ********************\n\n")
170 f.write(ping1)
171 f.write("----------------------------------------\n\n")
172 f.write(ping2)
173 f.write("----------------------------------------\n\n")
174 f.write(ping3)
175 f.write("************ Ping From Switch 2 : 192.168.3.2 ********************\n\n")
176 f.write(ping4)
177 f.write("----------------------------------------\n\n")
178 f.write(ping5)
179 f.write("----------------------------------------\n\n")
180 f.write(ping6)
181 f.write("************ Ping From Host 1 : 192.168.3.3 ********************\n\n")
182 f.write(ping7)
183 f.write("----------------------------------------\n\n")
184 f.write(ping8)
185 f.write("----------------------------------------\n\n")
186 f.write(ping9)
187 f.write("************ Ping From Host 2 : 192.168.3.4 ********************\n\n")
188 f.write(ping10)
189 f.write("----------------------------------------\n\n")
190 f.write(ping11)
191 f.write("----------------------------------------\n\n")
192 f.write(ping12)
193
194 f.close()
195
196 # Delete the overlay network
197 ec.shutdown()
198
199