dcbdf9785136b709851b907c98fe83ae49fac9f7
[nepi.git] / examples / ccn_emu_live / 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("linux::ns3::Simulation")
42
43     if not simulator:
44         node = ec.register_resource("linux::Node")
45         ec.set(node, "hostname", "localhost")
46
47         simu = ec.register_resource("linux::ns3::Simulation")
48         ec.set(simu, "enableDump", True)
49         ec.set(simu, "StopTime", STOP_TIME)
50         ec.register_connection(simu, node)
51         return simu
52
53     return simulator[0]
54
55 def add_collector(ec, trace_name, subdir, newname = None):
56     collector = ec.register_resource("Collector")
57     ec.set(collector, "traceName", trace_name)
58     ec.set(collector, "subDir", subdir)
59     if newname:
60         ec.set(collector, "rename", newname)
61
62     return collector
63
64 def add_dce_host(ec, nid):
65     simu = get_simulator(ec)
66     
67     host = ec.register_resource("ns3::Node")
68     ec.set(host, "enableStack", True)
69     ec.register_connection(host, simu)
70
71     # Annotate the graph
72     ec.netgraph.annotate_node(nid, "host", host)
73     
74 def add_dce_ccnd(ec, nid):
75     # Retrieve annotation from netgraph
76     host = ec.netgraph.node_annotation(nid, "host")
77     
78     # Add dce ccnd to the dce node
79     ccnd = ec.register_resource("linux::ns3::dce::CCND")
80     ec.set (ccnd, "stackSize", 1<<20)
81     ec.set (ccnd, "debug", 7)
82     ec.set (ccnd, "capacity", 50000)
83     ec.set (ccnd, "StartTime", "1s")
84     ec.register_connection(ccnd, host)
85
86     # Collector to retrieve ccnd log
87     collector = add_collector(ec, "stderr", nid, "log")
88     ec.register_connection(collector, ccnd)
89
90     # Annotate the graph
91     ec.netgraph.annotate_node(nid, "ccnd", ccnd)
92
93 def add_dce_ccnr(ec, nid):
94     # Retrieve annotation from netgraph
95     host = ec.netgraph.node_annotation(nid, "host")
96     
97     # Add a CCN content repository to the dce node
98     ccnr = ec.register_resource("linux::ns3::dce::CCNR")
99     ec.set (ccnr, "repoFile1", repofile) 
100     ec.set (ccnr, "stackSize", 1<<20)
101     ec.set (ccnr, "StartTime", "2s")
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     # Add a ccncat application to the dce host
109     ccncat = ec.register_resource("linux::ns3::dce::CCNCat")
110     ec.set (ccncat, "contentName", content_name)
111     ec.set (ccncat, "stackSize", 1<<20)
112     ec.set (ccncat, "StartTime", "8s")
113     ec.register_connection(ccncat, host)
114
115 def add_dce_fib_entry(ec, nid1, nid2):
116     # Retrieve annotations from netgraph
117     host1 = ec.netgraph.node_annotation(nid1, "host")
118     net = ec.netgraph.edge_net_annotation(nid1, nid2)
119     ip2 = net[nid2]
120
121     # Add FIB entry between peer hosts
122     ccndc = ec.register_resource("linux::ns3::dce::FIBEntry")
123     ec.set (ccndc, "protocol", "udp") 
124     ec.set (ccndc, "uri", "ccnx:/") 
125     ec.set (ccndc, "host", ip2)
126     ec.set (ccndc, "stackSize", 1<<20)
127     ec.set (ccndc, "StartTime", "4s")
128     ec.register_connection(ccndc, host1)
129
130 def add_dce_net_iface(ec, nid1, nid2):
131     # Retrieve annotations from netgraph
132     host = ec.netgraph.node_annotation(nid1, "host")
133     net = ec.netgraph.edge_net_annotation(nid1, nid2)
134     ip1 = net[nid1]
135     prefix = net["prefix"]
136
137     dev = ec.register_resource("ns3::PointToPointNetDevice")
138     ec.set(dev,"DataRate", "5Mbps")
139     ec.set(dev, "ip", ip1)
140     ec.set(dev, "prefix", prefix)
141     ec.register_connection(host, dev)
142
143     queue = ec.register_resource("ns3::DropTailQueue")
144     ec.register_connection(dev, queue)
145
146     return dev
147
148 def avg_interests(ec, run):
149     ## Process logs
150     logs_dir = ec.run_dir
151
152     (graph,
153         content_names,
154         interest_expiry_count,
155         interest_dupnonce_count,
156         interest_count,
157         content_count) = ccn_parser.process_content_history_logs(
158                 logs_dir, 
159                 ec.netgraph.topology)
160
161     shortest_path = networkx.shortest_path(graph, 
162             source = ec.netgraph.sources()[0], 
163             target = ec.netgraph.targets()[0])
164
165     ### Compute metric: Avg number of Interests seen per content name
166     ###                 normalized by the number of nodes in the shortest path
167     content_name_count = len(content_names.values())
168     nodes_in_shortest_path = len(shortest_path) - 1
169     metric = interest_count / (float(content_name_count) * float(nodes_in_shortest_path))
170
171     # TODO: DUMP RESULTS TO FILE
172     # TODO: DUMP GRAPH DELAYS!
173     f = open("/tmp/metric", "a+")
174     f.write("%.2f\n" % metric)
175     f.close()
176     print " METRIC", metric
177
178     return metric
179
180 def add_dce_edge(ec, nid1, nid2):
181     ### Add network interfaces to hosts
182     p2p1 = add_dce_net_iface(ec, nid1, nid2)
183     p2p2 = add_dce_net_iface(ec, nid2, nid1)
184
185     # Create point to point link between interfaces
186     chan = ec.register_resource("ns3::PointToPointChannel")
187     ec.set(chan, "Delay", "0ms")
188
189     ec.register_connection(chan, p2p1)
190     ec.register_connection(chan, p2p2)
191
192     #### Add routing between CCN nodes
193     add_dce_fib_entry(ec, nid1, nid2)
194     add_dce_fib_entry(ec, nid2, nid1)
195
196 def add_dce_node(ec, nid):
197     ### Add CCN nodes (ec.netgraph holds the topology graph)
198     add_dce_host(ec, nid)
199     add_dce_ccnd(ec, nid)
200         
201     if nid == ec.netgraph.targets()[0]:
202         add_dce_ccnr(ec, nid)
203
204     if nid == ec.netgraph.sources()[0]:
205         add_dce_ccncat(ec, nid)
206
207 if __name__ == '__main__':
208
209     #### Create NEPI Experiment Description with LINEAR topology 
210     ec = ExperimentController("dce_ccn", 
211             topo_type = TopologyType.LINEAR, 
212             node_count = 2,
213             assign_st = True,
214             assign_ips = True,
215             add_node_callback = add_dce_node, 
216             add_edge_callback = add_dce_edge)
217     
218     print "Results stored at", ec.exp_dir
219
220     #### Retrieve the consumer to wait for ot to finish
221     ccncat = ec.filter_resources("linux::ns3::dce::CCNCat")
222    
223     #### Run experiment until metric convergences
224     rnr = ExperimentRunner()
225     runs = rnr.run(ec, min_runs = 1, max_runs = 1, 
226             compute_metric_callback = avg_interests,
227             wait_guids = ccncat,
228             wait_time = 0)
229