b22d66804d2d88002ca5883e56da5ac5fe97c545
[nepi.git] / examples / dce / custom_dce_ccn.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
21 from nepi.execution.ec import ExperimentController 
22
23 import os
24
25 def add_ns3_node(ec, simu):
26     node = ec.register_resource("ns3::Node")
27     ec.register_connection(node, simu)
28
29     ipv4 = ec.register_resource("ns3::Ipv4L3Protocol")
30     ec.register_connection(node, ipv4)
31
32     arp = ec.register_resource("ns3::ArpL3Protocol")
33     ec.register_connection(node, arp)
34     
35     icmp = ec.register_resource("ns3::Icmpv4L4Protocol")
36     ec.register_connection(node, icmp)
37
38     udp = ec.register_resource("ns3::UdpL4Protocol")
39     ec.register_connection(node, udp)
40
41     tcp = ec.register_resource("ns3::TcpL4Protocol")
42     ec.register_connection(node, tcp)
43
44     return node
45
46 def add_point2point_device(ec, node, ip,  prefix):
47     dev = ec.register_resource("ns3::PointToPointNetDevice")
48     ec.set(dev, "ip", ip)
49     ec.set(dev, "prefix", prefix)
50     ec.register_connection(node, dev)
51
52     queue = ec.register_resource("ns3::DropTailQueue")
53     ec.register_connection(dev, queue)
54
55     return dev
56
57 ec = ExperimentController(exp_id = "dce-custom-ccn")
58
59 node = ec.register_resource("LinuxNode")
60 ec.set(node, "hostname", "localhost")
61 ec.set(node, "cleanProcesses", True)
62 #ec.set(node, "cleanHome", True)
63
64 simu = ec.register_resource("LinuxNS3Simulation")
65 ec.set(simu, "verbose", True)
66 ec.set(simu, "nsLog", "DceApplication")
67 ec.set(simu, "enableDump", True)
68 ec.register_connection(simu, node)
69
70 nsnode1 = add_ns3_node(ec, simu)
71 p2p1 = add_point2point_device(ec, nsnode1, "10.0.0.1", "30")
72 ec.set(p2p1, "DataRate", "5Mbps")
73
74 nsnode2 = add_ns3_node(ec, simu)
75 p2p2 = add_point2point_device(ec, nsnode2, "10.0.0.2", "30")
76 ec.set(p2p2, "DataRate", "5Mbps")
77
78 # Create channel
79 chan = ec.register_resource("ns3::PointToPointChannel")
80 ec.set(chan, "Delay", "2ms")
81
82 ec.register_connection(chan, p2p1)
83 ec.register_connection(chan, p2p2)
84
85 ### create applications
86
87 # Add a LinuxCCNDceApplication to ns-3 node 1 to install custom ccnx sources
88 # and run a CCNx daemon
89 ccnd1 = ec.register_resource("ns3::LinuxCCNDceApplication")
90 # NOTE THAT INSTALLATION MIGHT FAIL IF openjdk-6-jdk is not installed
91 ec.set(ccnd1, "depends", "libpcap0.8-dev openjdk-6-jdk ant1.8 autoconf "
92     "libssl-dev libexpat-dev libpcap-dev libecryptfs0 libxml2-utils auto"
93     "make gawk gcc g++ git-core pkg-config libpcre3-dev openjdk-6-jre-lib")
94 ec.set (ccnd1, "sources", "http://www.ccnx.org/releases/ccnx-0.7.2.tar.gz")
95 ec.set (ccnd1, "build", "tar zxf ${SRC}/ccnx-0.7.2.tar.gz && "
96         "cd ccnx-0.7.2 && "
97         " INSTALL_BASE=${BIN_DCE}/.. ./configure && "
98         " make MORE_LDLIBS='-pie -rdynamic' && "
99         " make install && "
100         " cp ${BIN_DCE}/../bin/ccn* ${BIN_DCE} && "
101         " cd -")
102 ec.set (ccnd1, "binary", "ccnd")
103 ec.set (ccnd1, "stackSize", 1<<20)
104 ec.set (ccnd1, "environment", "CCND_CAP=50000; CCND_DEBUG=7")
105 ec.set (ccnd1, "StartTime", "1s")
106 ec.set (ccnd1, "StopTime", "20s")
107 ec.register_connection(ccnd1, nsnode1)
108
109 # Add a CCN repository to ns-3 node1 manually configuring all
110 # parameters
111 repofile = os.path.join(
112     os.path.dirname(os.path.realpath(__file__)), 
113     "..", "..", "..",
114     "test", "resources", "linux", "ns3", "ccn", "repoFile1")
115
116 ccnr = ec.register_resource("ns3::LinuxCCNDceApplication")
117 ec.set (ccnr, "binary", "ccnr")
118 ec.set (ccnr, "environment", "CCNR_DIRECTORY=/REPO/")
119 ec.set (ccnr, "files", "%s=/REPO/repoFile1" % repofile) 
120 ec.set (ccnr, "stackSize", 1<<20)
121 ec.set (ccnr, "StartTime", "2s")
122 ec.set (ccnr, "StopTime", "120s")
123 ec.register_connection(ccnr, nsnode1)
124
125 # Add a LinuxCCNDceApplication to ns-3 node 1 to run a CCN
126 # daemon. Note that the CCNx sources and build instructions 
127 # do not need to be specified again (NEPI will take the 
128 # instructions from the first definition).
129 ccnd2 = ec.register_resource("ns3::LinuxCCNDceApplication")
130 ec.set (ccnd2, "binary", "ccnd")
131 ec.set (ccnd2, "stackSize", 1<<20)
132 ec.set (ccnd2, "environment", "CCND_CAP=50000; CCND_DEBUG=7")
133 ec.set (ccnd2, "StartTime", "1s")
134 ec.set (ccnd2, "StopTime", "120s")
135 ec.register_connection(ccnd2, nsnode2)
136
137 # Add DCE application to configure peer CCN faces between
138 # nodes
139 ccndc1 = ec.register_resource("ns3::LinuxCCNDceApplication")
140 ec.set (ccndc1, "binary", "ccndc")
141 ec.set (ccndc1, "arguments", "-v;add;ccnx:/;udp;10.0.0.2")
142 ec.set (ccndc1, "stackSize", 1<<20)
143 ec.set (ccndc1, "StartTime", "2s")
144 ec.set (ccndc1, "StopTime", "120s")
145 ec.register_connection(ccndc1, nsnode1)
146
147 ccndc2 = ec.register_resource("ns3::LinuxCCNDceApplication")
148 ec.set (ccndc2, "binary", "ccndc")
149 ec.set (ccndc2, "arguments", "-v;add;ccnx:/;udp;10.0.0.1")
150 ec.set (ccndc2, "stackSize", 1<<20)
151 ec.set (ccndc2, "StartTime", "2s")
152 ec.set (ccndc2, "StopTime", "120s")
153 ec.register_connection(ccndc2, nsnode2)
154
155 # Add a DCE application to perform a ccncat and retrieve content
156 ccncat = ec.register_resource("ns3::LinuxCCNDceApplication")
157 ec.set (ccncat, "binary", "ccncat")
158 ec.set (ccncat, "arguments", "ccnx:/test/bunny.ts")
159 ec.set (ccncat, "stdinFile", "")
160 ec.set (ccncat, "stackSize", 1<<20)
161 ec.set (ccncat, "StartTime", "4s")
162 ec.set (ccncat, "StopTime", "120s")
163 ec.register_connection(ccncat, nsnode2)
164
165 ec.deploy()
166
167 ec.wait_finished([ccncat])
168
169 stdout = ec.trace(ccncat, "stdout")
170 # convert from bytes to MB
171 print "%0.2f MBytes received" % (len(stdout) / 1024.0 / 1024.0 )
172
173 ec.shutdown()
174
175