update OF code and examples
[nepi.git] / examples / 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, port0, tap):
71     tunnel = ec.register_resource("OVSTunnel")
72     ec.register_connection(port0, tunnel)
73     ec.register_connection(tunnel, tap)
74     return tunnel
75
76 def add_app(ec, command, node):
77     app = ec.register_resource("LinuxApplication")
78     ec.set(app, "command", command)
79     ec.register_connection(app, node)
80     return app
81
82 # Create the EC
83 ec = ExperimentController(exp_id = "test")
84
85 switch1 = "planetlab2.virtues.fi"
86 switch2 = "planetlab2.upc.es"
87 host1 = "planetlab2.ionio.gr"
88 host2 = "iraplab2.iralab.uni-karlsruhe.de"
89
90 slicename = "inria_nepi"
91
92 pl_user = os.environ.get("PL_USER")
93 pl_password = os.environ.get("PL_PASS")
94
95 s1_node = add_node(ec, switch1, slicename, pl_user, pl_password)
96 s2_node = add_node(ec, switch2, slicename, pl_user, pl_password)
97
98 # Add switches 
99 ovs1 = add_ovs(ec, "nepi_bridge", "192.168.3.1/24", "85.23.168.77", "6633", s1_node)
100 ovs2 = add_ovs(ec, "nepi_bridge", "192.168.3.2/24", "85.23.168.77", "6633", s2_node)
101
102 # Add ports on ovs
103 port1 = add_port(ec, "nepi_port1", ovs1)
104 port3 = add_port(ec, "nepi_port3", ovs1)
105 port2 = add_port(ec, "nepi_port2", ovs2)
106 port4 = add_port(ec, "nepi_port4", ovs2)
107
108 h1_node = add_node(ec, host1, slicename, pl_user, pl_password)
109 h2_node = add_node(ec, host2, slicename, pl_user, pl_password)
110
111 # Add tap devices
112 tap1 = add_tap(ec, "192.168.3.3", 24, "192.168.3.1", h1_node)
113 tap2 = add_tap(ec, "192.168.3.4", 24, "192.168.3.2", h2_node)
114
115 # Connect the nodes
116 tunnel1 = add_tunnel(ec, port1, tap1)
117 tunnel2 = add_tunnel(ec, port2, tap2)
118 tunnel3 = add_tunnel(ec, port3, port4)
119
120 # Add ping commands
121 app1 = add_app(ec, "ping -c3 192.168.3.3", s1_node)
122 app2 = add_app(ec, "ping -c3 192.168.3.4", s2_node)
123
124 ec.deploy()
125
126 ec.wait_finished([app2])
127
128 # Retreive ping results and save
129 # them in a file
130 ping1 = ec.trace(app1, 'stdout')
131 ping2 = ec.trace(app2, 'stdout')
132 f = open("examples/openvswitch/ping_res.txt", 'w').close()
133 f = open("examples/openvswitch/ping_res.txt", 'a')
134 f.write(ping1)
135 f.write(ping2)
136 f.close()
137
138 # Delete the overlay network
139 ec.shutdown()
140
141
142
143
144