0bb1732ea05553297d3485ebb1572c4c77033a32
[nepi.git] / examples / netns / local_switch_ping.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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 #
20 #
21 # This example must be executed as root: 
22 # $ sudo PYTHONPATH=$PYTHONPATH:src python examples/netns/local_switch_ping.py
23
24 from nepi.execution.ec import ExperimentController 
25
26 ec = ExperimentController(exp_id = "netns-local-p2p-ping")
27
28 # Simulation will executed in the local machine
29 node = ec.register_resource("linux::Node")
30 ec.set(node, "hostname", "localhost")
31
32 # Add an emulation resource
33 emu = ec.register_resource("linux::netns::Emulation")
34 ec.set(emu, "verbose", True)
35 ec.register_connection(emu, node)
36
37 # Add two emulated nodes
38 netnsnode1 = ec.register_resource("netns::Node")
39 ec.register_connection(netnsnode1, emu)
40
41 iface1 = ec.register_resource("netns::NodeInterface")
42 ec.register_connection(iface1, netnsnode1)
43
44 ip1 = ec.register_resource("netns::IPv4Address")
45 ec.set(ip1, "ip", "10.0.0.1")
46 ec.set(ip1, "prefix", "30")
47 ec.register_connection(ip1, iface1)
48
49 netnsnode2 = ec.register_resource("netns::Node")
50 ec.register_connection(netnsnode2, emu)
51
52 iface2 = ec.register_resource("netns::NodeInterface")
53 ec.register_connection(iface2, netnsnode2)
54
55 ip2 = ec.register_resource("netns::IPv4Address")
56 ec.set(ip2, "ip", "10.0.0.2")
57 ec.set(ip2, "prefix", "30")
58 ec.register_connection(ip2, iface2)
59
60 switch = ec.register_resource("netns::Switch")
61 ec.register_connection(iface1, switch)
62 ec.register_connection(iface2, switch)
63
64 ping = ec.register_resource("netns::Application")
65 ec.set(ping, "command", "ping -c20 10.0.0.2")
66 ec.register_connection(ping, netnsnode1)
67
68 ec.deploy()
69
70 ec.wait_finished([ping])
71
72 stdout = ec.trace(ping, "stdout") 
73
74 ec.shutdown()
75
76 print "PING OUTPUT", stdout