systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / examples / openvswitch / ping_over_udpTapTunnel_performance_test_triangleTopo.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 #                  Host3
24 #                    |
25 #                    |
26 #                    |   
27 #                 Switch3                         
28 #                 /    \          
29 #                /      \                
30 #               /        \              
31 #              /          \             
32 #         Switch1 ----- Switch2         
33 #            /              \           
34 #           /                \          
35 #          /                  \         
36 #       Host1                Host2   
37
38 from nepi.execution.ec import ExperimentController 
39
40 import os
41 import time
42
43 ### Useful Method to Create RM ##
44 def add_node(ec, host, user):
45     node = ec.register_resource("planetlab::Node")
46     ec.set(node, "hostname", host)
47     ec.set(node, "username", user)
48     ec.set(node, "cleanExperiment", True)
49     ec.set(node, "cleanProcesses", True)
50     return node
51
52 def add_tap(ec, ip, prefix, pointopoint, node):
53     tap = ec.register_resource("planetlab::Tap")
54     ec.set(tap, "ip", ip)
55     ec.set(tap, "prefix", prefix)
56     ec.set(tap, "pointopoint", pointopoint)
57     ec.set(tap, "up", True)
58     ec.register_connection(tap, node)
59     return tap
60
61 def add_udptun(ec, tap1, tap2):
62     udptun = ec.register_resource("udp::Tunnel")
63     ec.register_connection(tap1, udptun)
64     ec.register_connection(tap2, udptun)
65     return udptun
66
67 def add_vroute(ec, network, prefix, tap):
68     vroute = ec.register_resource("planetlab::Vroute")
69     ec.set(vroute, "network", network)
70     ec.set(vroute, "prefix", prefix)
71     ec.set(vroute, "nexthop", ec.get(tap, "pointopoint"))
72     ec.register_connection(vroute, tap)
73     return vroute
74
75 def add_app(ec, command, node):
76     app = ec.register_resource("linux::Application")
77     ec.set(app, "command", command)
78     ec.register_connection(app, node)
79     return app
80
81 ### Data that can be changed ###
82 user = "inria_nepi"
83
84 hostname_switch1 = "planetlab2.virtues.fi"
85 hostname_switch2 = "planetlab2.upc.es"
86 hostname_switch3 = "planetlab2.cs.aueb.gr"
87 hostname_host1 = "planetlab2.ionio.gr"
88 hostname_host2 = "iraplab2.iralab.uni-karlsruhe.de"
89 hostname_host3 = "planetlab2.diku.dk"
90
91
92 ### Start Experiment ###
93 ec = ExperimentController(exp_id = "test-tap-tunnel")
94      
95 ## Create The topology ##   
96 host1 = add_node(ec, hostname_host1, user)
97 tap1 = add_tap(ec, "192.168.3.1", "24", "192.168.3.2", host1)
98
99 switch1 = add_node(ec, hostname_switch1, user)
100 tap2 = add_tap(ec, "192.168.3.2", "24", "192.168.3.1", switch1)
101 tap102 = add_tap(ec, "192.168.3.102", "29", "192.168.3.104", switch1)
102 tap152 = add_tap(ec, "192.168.3.152", "29", "192.168.3.156", switch1)
103
104 host2 = add_node(ec, hostname_host2, user)
105 tap13 = add_tap(ec, "192.168.3.13", "24", "192.168.3.14", host2)
106
107 switch2 = add_node(ec, hostname_switch2, user)
108 tap14 = add_tap(ec, "192.168.3.14", "24", "192.168.3.13", switch2)
109 tap104 = add_tap(ec, "192.168.3.104", "29", "192.168.3.102", switch2)
110 tap204 = add_tap(ec, "192.168.3.204", "29", "192.168.3.206", switch2)
111
112 host3 = add_node(ec, hostname_host3, user)
113 tap25 = add_tap(ec, "192.168.3.25", "24", "192.168.3.26", host3)
114
115 switch3 = add_node(ec, hostname_switch3, user)
116 tap26 = add_tap(ec, "192.168.3.26", "24", "192.168.3.25", switch3)
117 tap156 = add_tap(ec, "192.168.3.156", "29", "192.168.3.152", switch3)
118 tap206 = add_tap(ec, "192.168.3.206", "29", "192.168.3.204", switch3)
119
120 ## Create the UDP Tunnel ## 
121 udptun1 = add_udptun(ec, tap1, tap2)
122 udptun2 = add_udptun(ec, tap13, tap14)
123 udptun3 = add_udptun(ec, tap25, tap26)
124
125 udptun4 = add_udptun(ec, tap102, tap104)
126 udptun5 = add_udptun(ec, tap152, tap156)
127 udptun6 = add_udptun(ec, tap204, tap206)
128
129 ## Create the PlanetLab Route ## 
130 vroute1 = add_vroute(ec, "192.168.3.0", "24", tap1)
131 vroute2 = add_vroute(ec, "192.168.3.0", "24", tap13)
132 vroute3 = add_vroute(ec, "192.168.3.0", "24", tap25)
133
134 vroute7 = add_vroute(ec, "192.168.3.8", "29", tap102)
135 vroute8 = add_vroute(ec, "192.168.3.0", "29", tap104)
136 vroute9 = add_vroute(ec, "192.168.3.24", "29", tap152)
137 vroute10 = add_vroute(ec, "192.168.3.0", "29", tap156)
138 vroute11 = add_vroute(ec, "192.168.3.24", "29", tap204)
139 vroute12 = add_vroute(ec, "192.168.3.8", "29", tap206)
140
141 ## Create all the Ping ## 
142
143 app1 = add_app(ec, "ping -c8 192.168.3.13", host1)
144 app2 = add_app(ec, "ping -c8 192.168.3.25", host1)
145 app3 = add_app(ec, "ping -c8 192.168.3.104", host1)
146 app4 = add_app(ec, "ping -c8 192.168.3.156", host1)
147 app5 = add_app(ec, "ping -c8 192.168.3.2", host1)
148
149 app11 = add_app(ec, "ping -c8 192.168.3.1", host2)
150 app12 = add_app(ec, "ping -c8 192.168.3.25", host2)
151 app13 = add_app(ec, "ping -c8 192.168.3.102", host2)
152 app14 = add_app(ec, "ping -c8 192.168.3.206", host2)
153 app15 = add_app(ec, "ping -c8 192.168.3.14", host2)
154
155 app21 = add_app(ec, "ping -c8 192.168.3.1", host3)
156 app22 = add_app(ec, "ping -c8 192.168.3.13", host3)
157 app23 = add_app(ec, "ping -c8 192.168.3.152", host3)
158 app24 = add_app(ec, "ping -c8 192.168.3.204", host3)
159 app25 = add_app(ec, "ping -c8 192.168.3.26", host3)
160
161 ec.deploy()
162
163 ec.wait_finished([app1 ,app2, app3, app4, app5, app11 ,app12, app13, app14, app15, app21 ,app22, app23, app24, app25])
164
165 # Retreive ping results and save
166 # them in a file
167 ping1 = ec.trace(app1, 'stdout')
168 ping2 = ec.trace(app2, 'stdout')
169 ping3 = ec.trace(app3, 'stdout')
170 ping4 = ec.trace(app4, 'stdout')
171 ping5 = ec.trace(app5, 'stdout')
172 ping11 = ec.trace(app11, 'stdout')
173 ping12 = ec.trace(app12, 'stdout')
174 ping13 = ec.trace(app13, 'stdout')
175 ping14 = ec.trace(app14, 'stdout')
176 ping15 = ec.trace(app15, 'stdout')
177 ping21 = ec.trace(app21, 'stdout')
178 ping22 = ec.trace(app22, 'stdout')
179 ping23 = ec.trace(app23, 'stdout')
180 ping24 = ec.trace(app24, 'stdout')
181 ping25 = ec.trace(app25, 'stdout')
182
183 with open("examples/openvswitch/ping_over_udpTapTunnel_performance_test.txt", 'w') as f:
184
185     if not ping25:
186         ec.shutdown()
187   
188
189     f.write("************ Ping From Host 1 : 192.168.3.1 ********************\n\n")
190     f.write(ping1)
191     f.write("----------------------------------------\n\n")
192     f.write(ping2)
193     f.write("----------------------------------------\n\n")
194     f.write(ping3)
195     f.write("----------------------------------------\n\n")
196     f.write(ping4)
197     f.write("----------------------------------------\n\n")
198     f.write(ping5)
199     f.write("************ Ping From Host 2 : 192.168.3.13 ********************\n\n")
200     f.write(ping11)
201     f.write("----------------------------------------\n\n")
202     f.write(ping12)
203     f.write("----------------------------------------\n\n")
204     f.write(ping13)
205     f.write("----------------------------------------\n\n")
206     f.write(ping14)
207     f.write("----------------------------------------\n\n")
208     f.write(ping15)
209     f.write("************ Ping From Host 3 : 192.168.3.25 ********************\n\n")
210     f.write(ping21)
211     f.write("----------------------------------------\n\n")
212     f.write(ping22)
213     f.write("----------------------------------------\n\n")
214     f.write(ping23)
215     f.write("----------------------------------------\n\n")
216     f.write(ping24)
217     f.write("----------------------------------------\n\n")
218     f.write(ping25)
219
220 # Delete the overlay network
221 ec.shutdown()
222
223