18c2f158fe3775122e6e3c293c4ef934a48237aa
[nepi.git] / examples / planetlab / openvswitch / ovs_ping_exp.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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 #         Alexandros Kouvakas <alexandros.kouvakas@gmail.com>
21 #
22 #         Switch1 ------- Switch2         
23 #            /                \           
24 #           /                  \          
25 #          /                    \         
26 #       Host1                  Host2      
27
28
29
30 from nepi.execution.ec import ExperimentController
31 import os, time
32
33 def add_node(ec, host, user, pl_user, pl_password):
34     node = ec.register_resource("PlanetlabNode")
35     ec.set(node, "hostname", host)
36     ec.set(node, "username", user)
37     if pl_user:
38         ec.set(node, "pluser", pl_user)
39     if pl_password:
40         ec.set(node, "plpassword", pl_password)
41     ec.set(node, "cleanHome", True)
42     ec.set(node, "cleanProcesses", True)
43
44     return node
45
46 def add_ovs(ec, bridge_name, virtual_ip_pref, controller_ip, controller_port, node):
47     ovs = ec.register_resource("OVSWitch")
48     ec.set(ovs, "bridge_name", bridge_name)
49     ec.set(ovs, "virtual_ip_pref", virtual_ip_pref)
50     ec.set(ovs, "controller_ip", controller_ip)
51     ec.set(ovs, "controller_port", controller_port)
52     ec.register_connection(ovs, node)
53     return ovs
54
55 def add_port(ec, port_name, ovs):
56     port = ec.register_resource("OVSPort")
57     ec.set(port, "port_name", port_name)
58     ec.register_connection(port, ovs)
59     return port
60
61 def add_tap(ec, ip4, prefix4, pointopoint, node):
62     tap = ec.register_resource("PlanetlabTap")
63     ec.set(tap, "ip4", ip4)
64     ec.set(tap, "prefix4", prefix4)
65     ec.set(tap, "pointopoint", pointopoint)
66     ec.set(tap, "up", True)
67     ec.register_connection(tap, node)
68     return tap
69
70 def add_tunnel(ec, network, port0, tap):
71     tunnel = ec.register_resource("OVSTunnel")
72     ec.set(tunnel, "network", network)
73     ec.register_connection(port0, tunnel)
74     ec.register_connection(tunnel, tap)
75     return tunnel
76
77 def add_app(ec, command, node):
78     app = ec.register_resource("LinuxApplication")
79     ec.set(app, "command", command)
80     ec.register_connection(app, node)
81     return app
82
83 # Create the EC
84 ec = ExperimentController(exp_id = "test")
85
86 switch1 = "planetlab2.virtues.fi"
87 switch2 = "planetlab2.upc.es"
88 host1 = "planetlab2.ionio.gr"
89 host2 = "iraplab2.iralab.uni-karlsruhe.de"
90
91 network = "192.168.3.0"
92
93 slicename = "inria_nepi"
94
95 pl_user = os.environ.get("PL_USER")
96 pl_password = os.environ.get("PL_PASS")
97
98 s1_node = add_node(ec, switch1, slicename, pl_user, pl_password)
99 s2_node = add_node(ec, switch2, slicename, pl_user, pl_password)
100
101 # Add switches 
102 ovs1 = add_ovs(ec, "nepi_bridge", "192.168.3.1/24", "85.23.168.77", "6633", s1_node)
103 ovs2 = add_ovs(ec, "nepi_bridge", "192.168.3.2/24", "85.23.168.77", "6633", s2_node)
104
105 # Add ports on ovs
106 port1 = add_port(ec, "nepi_port1", ovs1)
107 port3 = add_port(ec, "nepi_port3", ovs1)
108 port2 = add_port(ec, "nepi_port2", ovs2)
109 port4 = add_port(ec, "nepi_port4", ovs2)
110
111 h1_node = add_node(ec, host1, slicename, pl_user, pl_password)
112 h2_node = add_node(ec, host2, slicename, pl_user, pl_password)
113
114 # Add tap devices
115 tap1 = add_tap(ec, "192.168.3.3", 24, "192.168.3.1", h1_node)
116 tap2 = add_tap(ec, "192.168.3.4", 24, "192.168.3.2", h2_node)
117
118 # Connect the nodes
119 tunnel1 = add_tunnel(ec, network, port1, tap1)
120 tunnel2 = add_tunnel(ec, network, port2, tap2)
121 tunnel3 = add_tunnel(ec, network, port3, port4)
122
123 # Add ping commands
124 app1 = add_app(ec, "ping -c5 192.168.3.2", s1_node)
125 app2 = add_app(ec, "ping -c5 192.168.3.3", s1_node)
126 app3 = add_app(ec, "ping -c5 192.168.3.4", s1_node)
127 app4 = add_app(ec, "ping -c5 192.168.3.1", s2_node)
128 app5 = add_app(ec, "ping -c5 192.168.3.3", s2_node)
129 app6 = add_app(ec, "ping -c5 192.168.3.4", s2_node)
130 app7 = add_app(ec, "ping -c5 192.168.3.1", h1_node)
131 app8 = add_app(ec, "ping -c5 192.168.3.2", h1_node)
132 app9 = add_app(ec, "ping -c5 192.168.3.4", h1_node)
133 app10 = add_app(ec, "ping -c5 192.168.3.1", h2_node)
134 app11 = add_app(ec, "ping -c5 192.168.3.2", h2_node)
135 app12 = add_app(ec, "ping -c5 192.168.3.3", h2_node)
136
137 ec.deploy()
138
139 ec.wait_finished([app1, app2, app3, app4, app5, app6, app7, app8, app9, app10, app11, app12])
140
141 # Retreive ping results and save
142 # them in a file
143 ping1 = ec.trace(app1, 'stdout')
144 ping2 = ec.trace(app2, 'stdout')
145 ping3 = ec.trace(app3, 'stdout')
146 ping4 = ec.trace(app4, 'stdout')
147 ping5 = ec.trace(app5, 'stdout')
148 ping6 = ec.trace(app6, 'stdout')
149 ping7 = ec.trace(app7, 'stdout')
150 ping8 = ec.trace(app8, 'stdout')
151 ping9 = ec.trace(app9, 'stdout')
152 ping10 = ec.trace(app10, 'stdout')
153 ping11 = ec.trace(app11, 'stdout')
154 ping12 = ec.trace(app12, 'stdout')
155
156
157 f = open("examples/openvswitch/ping_res.txt", 'w')
158
159 if not ping12:
160   ec.shutdown()
161
162 f.write(ping1)
163 f.write(ping2)
164 f.write(ping3)
165 f.write(ping4)
166 f.write(ping5)
167 f.write(ping6)
168 f.write(ping7)
169 f.write(ping8)
170 f.write(ping9)
171 f.write(ping10)
172 f.write(ping11)
173 f.write(ping12)
174 f.close()
175
176 # Delete the overlay network
177 ec.shutdown()
178
179
180
181
182