7fbb74b4709c4a4726aa88d811ea6fb9cb448a35
[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("PlanetlabNode")
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, "cleanHome", 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("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, ovs):
59     port = ec.register_resource("OVSPort")
60     ec.set(port, "port_name", port_name)
61     ec.register_connection(port, ovs)
62     return port
63
64 def add_tap(ec, ip4, prefix4, pointopoint, node):
65     tap = ec.register_resource("PlanetlabTap")
66     ec.set(tap, "ip4", ip4)
67     ec.set(tap, "prefix4", prefix4)
68     ec.set(tap, "pointopoint", pointopoint)
69     ec.set(tap, "up", True)
70     ec.register_connection(tap, node)
71     return tap
72
73 def add_tunnel(ec, network, port0, tap):
74     tunnel = ec.register_resource("OVSTunnel")
75     ec.set(tunnel, "network", network)
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("LinuxApplication")
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 switch1 = "planetlab2.virtues.fi"
90 switch2 = "planetlab2.upc.es"
91 host1 = "planetlab2.ionio.gr"
92 host2 = "iraplab2.iralab.uni-karlsruhe.de"
93
94 ip_controller = "194.254.215.12"
95 network = "192.168.3.0"
96
97 slicename = "inria_nepi"
98
99 pl_user = os.environ.get("PL_USER")
100 pl_password = os.environ.get("PL_PASS")
101
102 s1_node = add_node(ec, switch1, slicename, pl_user, pl_password)
103 s2_node = add_node(ec, switch2, slicename, pl_user, pl_password)
104
105 # Add switches 
106 ovs1 = add_ovs(ec, "nepi_bridge_1", "192.168.3.1/24", ip_controller, "6633", s1_node)
107 ovs2 = add_ovs(ec, "nepi_bridge_2", "192.168.3.2/24", ip_controller, "6633", s2_node)
108
109 # Add ports on ovs
110 port1 = add_port(ec, "nepi_port1", ovs1)
111 port3 = add_port(ec, "nepi_port3", ovs1)
112 port2 = add_port(ec, "nepi_port2", ovs2)
113 port4 = add_port(ec, "nepi_port4", ovs2)
114
115 h1_node = add_node(ec, host1, slicename, pl_user, pl_password)
116 h2_node = add_node(ec, host2, slicename, pl_user, pl_password)
117
118 # Add tap devices
119 tap1 = add_tap(ec, "192.168.3.3", 24, "192.168.3.1", h1_node)
120 tap2 = add_tap(ec, "192.168.3.4", 24, "192.168.3.2", h2_node)
121
122 # Connect the nodes
123 tunnel1 = add_tunnel(ec, network, port1, tap1)
124 tunnel2 = add_tunnel(ec, network, port2, tap2)
125 tunnel3 = add_tunnel(ec, network, port3, port4)
126
127 # Add ping commands
128 app1 = add_app(ec, "ping -c5 192.168.3.2", s1_node)
129 app2 = add_app(ec, "ping -c5 192.168.3.3", s1_node)
130 app3 = add_app(ec, "ping -c5 192.168.3.4", s1_node)
131 app4 = add_app(ec, "ping -c5 192.168.3.1", s2_node)
132 app5 = add_app(ec, "ping -c5 192.168.3.3", s2_node)
133 app6 = add_app(ec, "ping -c5 192.168.3.4", s2_node)
134 app7 = add_app(ec, "ping -c5 192.168.3.1", h1_node)
135 app8 = add_app(ec, "ping -c5 192.168.3.2", h1_node)
136 app9 = add_app(ec, "ping -c5 192.168.3.4", h1_node)
137 app10 = add_app(ec, "ping -c5 192.168.3.1", h2_node)
138 app11 = add_app(ec, "ping -c5 192.168.3.2", h2_node)
139 app12 = add_app(ec, "ping -c5 192.168.3.3", h2_node)
140
141 ec.deploy()
142
143 ec.wait_finished([app1, app2, app3, app4, app5, app6, app7, app8, app9, app10, app11, app12])
144
145 # Retreive ping results and save
146 # them in a file
147 ping1 = ec.trace(app1, 'stdout')
148 ping2 = ec.trace(app2, 'stdout')
149 ping3 = ec.trace(app3, 'stdout')
150 ping4 = ec.trace(app4, 'stdout')
151 ping5 = ec.trace(app5, 'stdout')
152 ping6 = ec.trace(app6, 'stdout')
153 ping7 = ec.trace(app7, 'stdout')
154 ping8 = ec.trace(app8, 'stdout')
155 ping9 = ec.trace(app9, 'stdout')
156 ping10 = ec.trace(app10, 'stdout')
157 ping11 = ec.trace(app11, 'stdout')
158 ping12 = ec.trace(app12, 'stdout')
159
160 if not ping12:
161   ec.shutdown()
162   sys.exit("No ping found")
163
164 f = open("examples/openvswitch/ovs_ping_2switches.txt", 'w')
165 f.write("************ Ping From Switch 1 : 192.168.3.1 ********************\n\n")
166 f.write(ping1)
167 f.write("----------------------------------------\n\n")
168 f.write(ping2)
169 f.write("----------------------------------------\n\n")
170 f.write(ping3)
171 f.write("************ Ping From Switch 2 : 192.168.3.2 ********************\n\n")
172 f.write(ping4)
173 f.write("----------------------------------------\n\n")
174 f.write(ping5)
175 f.write("----------------------------------------\n\n")
176 f.write(ping6)
177 f.write("************ Ping From Host 1 : 192.168.3.3 ********************\n\n")
178 f.write(ping7)
179 f.write("----------------------------------------\n\n")
180 f.write(ping8)
181 f.write("----------------------------------------\n\n")
182 f.write(ping9)
183 f.write("************ Ping From Host 2 : 192.168.3.4 ********************\n\n")
184 f.write(ping10)
185 f.write("----------------------------------------\n\n")
186 f.write(ping11)
187 f.write("----------------------------------------\n\n")
188 f.write(ping12)
189
190 f.close()
191
192 # Delete the overlay network
193 ec.shutdown()
194
195
196
197
198