10b4deb148fed866095c055cf9f554b36905fa38
[nepi.git] / examples / ccn_emu_live / planetlab.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 from nepi.execution.ec import ExperimentController 
22 from nepi.execution.runner import ExperimentRunner
23 from nepi.util.netgraph import NetGraph, TopologyType
24 import nepi.data.processing.ccn.parser as ccn_parser
25
26 import networkx
27 import socket
28 import os
29
30 PL_NODES = dict({
31     0: "iraplab1.iralab.uni-karlsruhe.de",
32     1: "planetvs2.informatik.uni-stuttgart.de",
33     2: "dfn-ple1.x-win.dfn.de",
34     3: "planetlab2.extern.kuleuven.be",
35     4: "mars.planetlab.haw-hamburg.de",
36     5: "planetlab-node3.it-sudparis.eu",
37     6: "node2pl.planet-lab.telecom-lille1.eu",
38     7: "planetlab1.informatik.uni-wuerzburg.de",
39     8: "planet1.l3s.uni-hannover.de",
40     9: "planetlab1.wiwi.hu-berlin.de",
41     10: "pl2.uni-rostock.de", 
42     11: "planetlab1.u-strasbg.fr",
43     12: "peeramidion.irisa.fr",
44     13: "planetlab2.unineuchatel.ch", 
45     })
46
47 pl_slice = os.environ.get("PL_SLICE")
48 pl_user = os.environ.get("PL_USER")
49 pl_password = os.environ.get("PL_PASS")
50 pl_ssh_key = os.environ.get("PL_SSHKEY")
51
52 content_name = "ccnx:/test/bunny.ts"
53
54 pipeline = 4 # Default value for ccncat
55
56 operating_system = "f14"
57
58 country = "germany"
59
60 repofile = os.path.join(
61         os.path.dirname(os.path.realpath(__file__)), "repoFile1.0.8.2")
62
63 def add_collector(ec, trace_name, subdir, newname = None):
64     collector = ec.register_resource("Collector")
65     ec.set(collector, "traceName", trace_name)
66     ec.set(collector, "subDir", subdir)
67     if newname:
68         ec.set(collector, "rename", newname)
69
70     return collector
71
72 def add_pl_host(ec, nid):
73     hostname = PL_NODES[nid]
74
75     # Add a planetlab host to the experiment description
76     host = ec.register_resource("planetlab::Node")
77     ec.set(host, "hostname", hostname)
78     ec.set(host, "username", pl_slice)
79     ec.set(host, "identity", pl_ssh_key)
80     #ec.set(host, "pluser", pl_user)
81     #ec.set(host, "plpassword", pl_password)
82     #ec.set(host, "country", country)
83     #ec.set(host, "operatingSystem", operating_system)
84     ec.set(host, "cleanExperiment", True)
85     ec.set(host, "cleanProcesses", True)
86
87     # Annotate the graph
88     ec.netgraph.annotate_node(nid, "hostname", hostname)
89     ec.netgraph.annotate_node(nid, "host", host)
90     
91     # Annotate the graph node with an ip address
92     ip = socket.gethostbyname(hostname)
93     ec.netgraph.annotate_node_ip(nid, ip)
94
95 def add_pl_ccnd(ec, nid):
96     # Retrieve annotation from netgraph
97     host = ec.netgraph.node_annotation(nid, "host")
98     
99     # Add a CCN daemon to the planetlab node
100     ccnd = ec.register_resource("linux::CCND")
101     ec.set(ccnd, "debug", 7)
102     ec.register_connection(ccnd, host)
103     
104     # Collector to retrieve ccnd log
105     collector = add_collector(ec, "stderr", nid, "log")
106     ec.register_connection(collector, ccnd)
107
108     # Annotate the graph
109     ec.netgraph.annotate_node(nid, "ccnd", ccnd)
110
111 def add_pl_ccnr(ec, nid):
112     # Retrieve annotation from netgraph
113     ccnd = ec.netgraph.node_annotation(nid, "ccnd")
114     
115     # Add a CCN content repository to the planetlab node
116     ccnr = ec.register_resource("linux::CCNR")
117
118     ec.set(ccnr, "repoFile1", repofile)
119     ec.register_connection(ccnr, ccnd)
120
121 def add_pl_ccncat(ec, nid):
122     # Retrieve annotation from netgraph
123     ccnd = ec.netgraph.node_annotation(nid, "ccnd")
124     
125     # Add a CCN cat application to the planetlab node
126     ccncat = ec.register_resource("linux::CCNCat")
127     ec.set(ccncat, "pipeline", pipeline)
128     ec.set(ccncat, "contentName", content_name)
129     ec.register_connection(ccncat, ccnd)
130
131 def add_pl_fib_entry(ec, nid1, nid2):
132     # Retrieve annotations from netgraph
133     ccnd1 = ec.netgraph.node_annotation(nid1, "ccnd")
134     hostname2 = ec.netgraph.node_annotation(nid2, "hostname")
135     
136     # Add a FIB entry between one planetlab node and its peer
137     entry = ec.register_resource("linux::FIBEntry")
138     ec.set(entry, "host", hostname2)
139     ec.register_connection(entry, ccnd1)
140
141     # Collector to retrieve peering ping output (to measure neighbors delay)
142     ec.enable_trace(entry, "ping")
143     collector = add_collector(ec, "ping", nid1)
144     ec.register_connection(collector, entry)
145
146     return entry
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                 parse_ping_logs = True)
161
162     shortest_path = networkx.shortest_path(graph, 
163             source = ec.netgraph.sources()[0], 
164             target = ec.netgraph.targets()[0])
165
166     ### Compute metric: Avg number of Interests seen per content name
167     ###                 normalized by the number of nodes in the shortest path
168     content_name_count = len(content_names.values())
169     nodes_in_shortest_path = len(shortest_path) - 1
170     metric = interest_count / (float(content_name_count) * float(nodes_in_shortest_path))
171
172     # TODO: DUMP RESULTS TO FILE
173     # TODO: DUMP GRAPH DELAYS!
174     f = open("/tmp/metric", "a+")
175     f.write("%.2f\n" % metric)
176     f.close()
177     print " METRIC", metric
178
179     return metric
180
181 def add_pl_edge(ec, nid1, nid2):
182     #### Add connections between CCN nodes
183     add_pl_fib_entry(ec, nid1, nid2)
184     add_pl_fib_entry(ec, nid2, nid1)
185
186 def add_pl_node(ec, nid):
187     ### Add CCN nodes (ec.netgraph holds the topology graph)
188     add_pl_host(ec, nid)
189     add_pl_ccnd(ec, nid)
190         
191     if nid == ec.netgraph.targets()[0]:
192         add_pl_ccnr(ec, nid)
193
194     if nid == ec.netgraph.sources()[0]:
195         add_pl_ccncat(ec, nid)
196
197 if __name__ == '__main__':
198
199     #### Create NEPI Experiment Description with LINEAR topology 
200     ec = ExperimentController("pl_ccn", 
201             topo_type = TopologyType.LINEAR, 
202             node_count = 4, 
203             #assign_ips = True,
204             assign_st = True,
205             add_node_callback = add_pl_node, 
206             add_edge_callback = add_pl_edge)
207     
208     print "Results stored at", ec.exp_dir
209
210     #### Retrieve the content producing resource to wait for ot to finish
211     ccncat = ec.filter_resources("linux::CCNCat")
212    
213     #### Run experiment until metric convergences
214     rnr = ExperimentRunner()
215     runs = rnr.run(ec, min_runs = 10, max_runs = 300, 
216             compute_metric_callback = avg_interests,
217             wait_guids = ccncat,
218             wait_time = 0)
219