Minor changes to examples/ccn_flooding/dce.py
[nepi.git] / examples / ccn_flooding / dce.py
1 #!/usr/bin/env python
2
3 ###############################################################################
4 #
5 #    NEPI, a framework to manage network experiments
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 #
22 ###############################################################################
23
24 from nepi.execution.ec import ExperimentController 
25 from nepi.execution.runner import ExperimentRunner
26 from nepi.util.netgraph import NetGraph, TopologyType
27 import nepi.data.processing.ccn.parser as ccn_parser
28
29 import networkx
30 import socket
31 import os
32
33 content_name = "ccnx:/test/bunny.ts"
34
35 STOP_TIME = "5000s"
36
37 repofile = os.path.join(
38         os.path.dirname(os.path.realpath(__file__)), "repoFile1.0.8.2")
39
40 def get_simulator(ec):
41     simulator = ec.filter_resources("LinuxNS3Simulation")
42
43     if not simulator:
44         node = ec.register_resource("LinuxNode")
45         ec.set(node, "hostname", "localhost")
46
47         simu = ec.register_resource("LinuxNS3Simulation")
48         ec.register_connection(simu, node)
49         return simu
50
51     return simulator[0]
52
53 def add_collector(ec, trace_name, subdir, newname = None):
54     collector = ec.register_resource("Collector")
55     ec.set(collector, "traceName", trace_name)
56     ec.set(collector, "subDir", subdir)
57     if newname:
58         ec.set(collector, "rename", newname)
59
60     return collector
61
62 def add_dce_host(ec, nid):
63     simu = get_simulator(ec)
64     
65     host = ec.register_resource("ns3::Node")
66     ec.set(host, "enableStack", True)
67     ec.register_connection(host, simu)
68
69     # Annotate the graph
70     ec.netgraph.annotate_node(nid, "host", host)
71     
72 def add_dce_ccnd(ec, nid):
73     # Retrieve annotation from netgraph
74     host = ec.netgraph.node_annotation(nid, "host")
75     
76     # Add dce ccnd to the dce node
77     ccnd = ec.register_resource("ns3::LinuxDceCCND")
78     ec.set (ccnd, "stackSize", 1<<20)
79     ec.set (ccnd, "debug", 7)
80     ec.set (ccnd, "capacity", 50000)
81     ec.set (ccnd, "StartTime", "1s")
82     ec.set (ccnd, "StopTime", STOP_TIME)
83     ec.register_connection(ccnd, host)
84
85     # Collector to retrieve ccnd log
86     collector = add_collector(ec, "stderr", nid, "log")
87     ec.register_connection(collector, ccnd)
88
89     # Annotate the graph
90     ec.netgraph.annotate_node(nid, "ccnd", ccnd)
91
92 def add_dce_ccnr(ec, nid):
93     # Retrieve annotation from netgraph
94     host = ec.netgraph.node_annotation(nid, "host")
95     
96     # Add a CCN content repository to the dce node
97     ccnr = ec.register_resource("ns3::LinuxDceCCNR")
98     ec.set (ccnr, "repoFile1", repofile) 
99     ec.set (ccnr, "stackSize", 1<<20)
100     ec.set (ccnr, "StartTime", "2s")
101     ec.set (ccnr, "StopTime", STOP_TIME)
102     ec.register_connection(ccnr, host)
103
104 def add_dce_ccncat(ec, nid):
105     # Retrieve annotation from netgraph
106     host = ec.netgraph.node_annotation(nid, "host")
107    
108     ccnpeek = ec.register_resource("ns3::LinuxDceCCNPeek")
109     #ec.set (ccnpeek, "contentName", "ccnx:/chunk0")
110     ec.set (ccnpeek, "contentName", content_name)
111     ec.set (ccnpeek, "stackSize", 1<<20)
112     ec.set (ccnpeek, "StartTime", "5s")
113     ec.set (ccnpeek, "StopTime", STOP_TIME)
114     ec.register_connection(ccnpeek, host)
115
116     collector = add_collector(ec, "stdout", nid, "peek")
117     ec.register_connection(collector, ccnpeek)
118
119     # Add a ccncat application to the dce host
120     ccncat = ec.register_resource("ns3::LinuxDceCCNCat")
121     ec.set (ccncat, "contentName", content_name)
122     ec.set (ccncat, "stackSize", 1<<20)
123     ec.set (ccncat, "StartTime", "8s")
124     ec.set (ccncat, "StopTime", STOP_TIME)
125     ec.register_connection(ccncat, host)
126
127 def add_dce_fib_entry(ec, nid1, nid2):
128     # Retrieve annotations from netgraph
129     host1 = ec.netgraph.node_annotation(nid1, "host")
130     net = ec.netgraph.edge_net_annotation(nid1, nid2)
131     ip2 = net[nid2]
132
133     # Add FIB entry between peer hosts
134     ccndc = ec.register_resource("ns3::LinuxDceFIBEntry")
135     ec.set (ccndc, "protocol", "udp") 
136     ec.set (ccndc, "uri", "ccnx:/") 
137     ec.set (ccndc, "host", ip2)
138     ec.set (ccndc, "stackSize", 1<<20)
139     ec.set (ccndc, "StartTime", "2s")
140     ec.set (ccndc, "StopTime", STOP_TIME)
141     ec.register_connection(ccndc, host1)
142
143 def add_dce_net_iface(ec, nid1, nid2):
144     # Retrieve annotations from netgraph
145     host = ec.netgraph.node_annotation(nid1, "host")
146     net = ec.netgraph.edge_net_annotation(nid1, nid2)
147     ip1 = net[nid1]
148     prefix = net["prefix"]
149
150     dev = ec.register_resource("ns3::PointToPointNetDevice")
151     ec.set(dev,"DataRate", "5Mbps")
152     ec.set(dev, "ip", ip1)
153     ec.set(dev, "prefix", prefix)
154     ec.register_connection(host, dev)
155
156     queue = ec.register_resource("ns3::DropTailQueue")
157     ec.register_connection(dev, queue)
158
159     return dev
160
161 def avg_interests(ec, run):
162     ## Process logs
163     logs_dir = ec.run_dir
164
165     (graph,
166         content_names,
167         interest_expiry_count,
168         interest_dupnonce_count,
169         interest_count,
170         content_count) = ccn_parser.process_content_history_logs(
171                 logs_dir, 
172                 ec.netgraph.topology)
173
174     shortest_path = networkx.shortest_path(graph, 
175             source = ec.netgraph.sources()[0], 
176             target = ec.netgraph.targets()[0])
177
178     ### Compute metric: Avg number of Interests seen per content name
179     ###                 normalized by the number of nodes in the shortest path
180     content_name_count = len(content_names.values())
181     nodes_in_shortest_path = len(shortest_path) - 1
182     metric = interest_count / (float(content_name_count) * float(nodes_in_shortest_path))
183
184     # TODO: DUMP RESULTS TO FILE
185     # TODO: DUMP GRAPH DELAYS!
186     f = open("/tmp/metric", "w+")
187     f.write("%.2f\n" % metric)
188     f.close()
189     print " METRIC", metric
190
191     return metric
192
193 def add_dce_edge(ec, nid1, nid2):
194     ### Add network interfaces to hosts
195     p2p1 = add_dce_net_iface(ec, nid1, nid2)
196     p2p2 = add_dce_net_iface(ec, nid2, nid1)
197
198     # Create point to point link between interfaces
199     chan = ec.register_resource("ns3::PointToPointChannel")
200     ec.set(chan, "Delay", "0ms")
201
202     ec.register_connection(chan, p2p1)
203     ec.register_connection(chan, p2p2)
204
205     #### Add routing between CCN nodes
206     add_dce_fib_entry(ec, nid1, nid2)
207     add_dce_fib_entry(ec, nid2, nid1)
208
209 def add_dce_node(ec, nid):
210     ### Add CCN nodes (ec.netgraph holds the topology graph)
211     add_dce_host(ec, nid)
212     add_dce_ccnd(ec, nid)
213         
214     if nid == ec.netgraph.targets()[0]:
215         add_dce_ccnr(ec, nid)
216
217     if nid == ec.netgraph.sources()[0]:
218         add_dce_ccncat(ec, nid)
219
220 if __name__ == '__main__':
221
222     #### Create NEPI Experiment Description with LINEAR topology 
223     ec = ExperimentController("dce_ccn", 
224             topo_type = TopologyType.LINEAR, 
225             node_count = 4,
226             assign_st = True,
227             assign_ips = True,
228             add_node_callback = add_dce_node, 
229             add_edge_callback = add_dce_edge)
230     
231     print "Results stored at", ec.exp_dir
232
233     #### Retrieve the consumer to wait for ot to finish
234     ccncat = ec.filter_resources("ns3::LinuxDceCCNCat")
235    
236     #### Run experiment until metric convergences
237     rnr = ExperimentRunner()
238     runs = rnr.run(ec, min_runs = 1, max_runs = 1, 
239             compute_metric_callback = avg_interests,
240             wait_guids = ccncat,
241             wait_time = 0)
242