Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3064e7dc78c67de67c49786c9d7e4a6d17aeb77f
[simgrid.git] / src / surf / network_ns3.cpp
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/surf/network_ns3.hpp"
8
9 #include "src/surf/HostImpl.hpp"
10 #include "src/surf/surf_private.h"
11 #include "simgrid/sg_config.h"
12 #include "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
13
14 #include "simgrid/s4u/As.hpp"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
17
18 int NS3_EXTENSION_ID;
19
20 xbt_dynar_t IPV4addr = xbt_dynar_new(sizeof(char*),free);
21 static double time_to_next_flow_completion = -1;
22
23 extern xbt_dict_t dict_socket;
24
25 /*************
26  * Callbacks *
27  *************/
28
29 static void simgrid_ns3_add_host(simgrid::s4u::Host& host)
30 {
31   const char* id = host.name().c_str();
32   XBT_DEBUG("NS3_ADD_HOST '%s'", id);
33   host.extension_set(NS3_EXTENSION_ID, ns3_add_host(id));
34 }
35
36 static void parse_ns3_add_link(sg_platf_link_cbarg_t link)
37 {
38   XBT_DEBUG("NS3_ADD_LINK '%s'",link->id);
39
40   Link *l = surf_network_model->createLink(link->id, link->bandwidth, link->latency, link->policy, link->properties);
41   if (link->bandwidth_trace)
42     l->setBandwidthTrace(link->latency_trace);
43   if (link->latency_trace)
44     l->setLatencyTrace(link->latency_trace);
45   if (link->state_trace)
46     l->setStateTrace(link->state_trace);
47 }
48 static void simgrid_ns3_add_router(simgrid::surf::NetCard* router)
49 {
50   const char* router_id = router->name();
51   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
52   xbt_lib_set(as_router_lib,
53               router_id,
54               NS3_ASR_LEVEL,
55               ns3_add_router(router_id)
56     );
57 }
58
59 static void parse_ns3_add_AS(simgrid::s4u::As* as)
60 {
61   const char* as_id = as->name();
62   XBT_DEBUG("NS3_ADD_AS '%s'", as_id);
63   xbt_lib_set(as_router_lib, as_id, NS3_ASR_LEVEL, ns3_add_AS(as_id) );
64 }
65
66 #include "src/surf/xml/platf.hpp" // FIXME: move that back to the parsing area
67 static void parse_ns3_add_cluster(sg_platf_cluster_cbarg_t cluster)
68 {
69   const char *groups = NULL;
70
71   int start, end, i;
72   unsigned int iter;
73
74   xbt_dynar_t radical_elements;
75   xbt_dynar_t radical_ends;
76   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
77
78   char *router_id,*host_id;
79
80   radical_elements = xbt_str_split(cluster->radical, ",");
81   xbt_dynar_foreach(radical_elements, iter, groups) {
82     radical_ends = xbt_str_split(groups, "-");
83
84     switch (xbt_dynar_length(radical_ends)) {
85     case 1:
86       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
87       xbt_dynar_push_as(tab_elements_num, int, start);
88       router_id = bprintf("ns3_%s%d%s", cluster->prefix, start, cluster->suffix);
89       simgrid::s4u::Host::by_name_or_create(router_id)
90         ->extension_set(NS3_EXTENSION_ID, ns3_add_host_cluster(router_id));
91       XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
92       free(router_id);
93       break;
94
95     case 2:
96       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
97       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
98       for (i = start; i <= end; i++){
99         xbt_dynar_push_as(tab_elements_num, int, i);
100         router_id = bprintf("ns3_%s%d%s", cluster->prefix, i, cluster->suffix);
101         simgrid::s4u::Host::by_name_or_create(router_id)
102           ->extension_set(NS3_EXTENSION_ID, ns3_add_host_cluster(router_id));
103         XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
104         free(router_id);
105       }
106       break;
107
108     default:
109       XBT_DEBUG("Malformed radical");
110     }
111   }
112
113   //Create links
114   unsigned int cpt;
115   int elmts;
116   char * lat = bprintf("%fs", cluster->lat);
117   char * bw =  bprintf("%fBps", cluster->bw);
118
119   xbt_dynar_foreach(tab_elements_num,cpt,elmts)
120   {
121     host_id   = bprintf("%s%d%s", cluster->prefix, elmts, cluster->suffix);
122     router_id = bprintf("ns3_%s%d%s", cluster->prefix, elmts, cluster->suffix);
123     XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
124
125     ns3_nodes_t host_src = ns3_find_host(host_id);
126     ns3_nodes_t host_dst = ns3_find_host(router_id);
127
128     if(host_src && host_dst){}
129     else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
130
131     ns3_add_link(host_src->node_num,host_src->type,
132                  host_dst->node_num,host_dst->type,
133                  bw,lat);
134
135     free(router_id);
136     free(host_id);
137   }
138   xbt_free(lat);
139   xbt_free(bw);
140   xbt_dynar_free(&tab_elements_num);
141
142
143   //Create link backbone
144   lat = bprintf("%fs", cluster->bb_lat);
145   bw =  bprintf("%fBps", cluster->bb_bw);
146   ns3_add_cluster(bw,lat,cluster->id);
147   xbt_free(lat);
148   xbt_free(bw);
149 }
150
151 /* Create the ns3 topology based on routing strategy */
152 static void create_ns3_topology(void)
153 {
154   XBT_DEBUG("Starting topology generation");
155
156   xbt_dynar_shrink(IPV4addr,0);
157
158   //get the onelinks from the parsed platform
159   xbt_dynar_t onelink_routes = routing_platf->getOneLinkRoutes();
160   if (!onelink_routes)
161     xbt_die("There is no routes!");
162   XBT_DEBUG("Have get_onelink_routes, found %ld routes",onelink_routes->used);
163   //save them in trace file
164   simgrid::surf::Onelink *onelink;
165   unsigned int iter;
166   xbt_dynar_foreach(onelink_routes, iter, onelink) {
167     char *src = onelink->src_->name();
168     char *dst = onelink->dst_->name();
169     simgrid::surf::LinkNS3 *link =
170       static_cast<simgrid::surf::LinkNS3 *>(onelink->link_);
171
172     if (strcmp(src,dst) && link->m_created){
173       XBT_DEBUG("Route from '%s' to '%s' with link '%s'", src, dst, link->getName());
174       char * link_bdw = bprintf("%fBps", link->getBandwidth());
175       char * link_lat = bprintf("%fs", link->getLatency());
176       link->m_created = 0;
177
178       //   XBT_DEBUG("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
179       XBT_DEBUG("\tLink (%s) bdw:%s lat:%s", link->getName(), link_bdw, link_lat);
180
181       //create link ns3
182       ns3_nodes_t host_src = ns3_find_host(src);
183       if (!host_src)
184         host_src = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(as_router_lib,src,NS3_ASR_LEVEL));
185       ns3_nodes_t host_dst = ns3_find_host(dst);
186       if(!host_dst)
187         host_dst = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(as_router_lib,dst,NS3_ASR_LEVEL));
188
189       if (!host_src || !host_dst)
190           xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
191
192       ns3_add_link(host_src->node_num,host_src->type,host_dst->node_num,host_dst->type,link_bdw,link_lat);
193
194       xbt_free(link_bdw);
195       xbt_free(link_lat);
196     }
197   }
198 }
199
200 static void parse_ns3_end_platform(void)
201 {
202   ns3_end_platform();
203 }
204
205 static void define_callbacks_ns3(void)
206 {
207   simgrid::s4u::Host::onCreation.connect(simgrid_ns3_add_host);
208   simgrid::surf::netcardCreatedCallbacks.connect(simgrid_ns3_add_router);
209   simgrid::surf::on_link.connect (parse_ns3_add_link);
210   simgrid::surf::on_cluster.connect (&parse_ns3_add_cluster);
211   simgrid::surf::asCreatedCallbacks.connect(parse_ns3_add_AS);
212   simgrid::surf::on_postparse.connect(&create_ns3_topology); //get_one_link_routes
213   simgrid::surf::on_postparse.connect(&parse_ns3_end_platform); //InitializeRoutes
214 }
215
216 /*********
217  * Model *
218  *********/
219 static void free_ns3_link(void * elmts)
220 {
221   delete static_cast<simgrid::surf::LinkNS3*>(elmts);
222 }
223
224 static void free_ns3_host(void * elmts)
225 {
226   ns3_nodes_t host = static_cast<ns3_nodes_t>(elmts);
227   free(host);
228 }
229
230 void surf_network_model_init_NS3()
231 {
232   if (surf_network_model)
233     return;
234
235   surf_network_model = new simgrid::surf::NetworkNS3Model();
236
237   xbt_dynar_push(all_existing_models, &surf_network_model);
238 }
239
240 namespace simgrid {
241 namespace surf {
242
243 NetworkNS3Model::NetworkNS3Model() : NetworkModel() {
244   if (ns3_initialize(xbt_cfg_get_string(_sg_cfg_set, "ns3/TcpModel"))) {
245     xbt_die("Impossible to initialize NS3 interface");
246   }
247   routing_model_create(NULL);
248   define_callbacks_ns3();
249
250   NS3_EXTENSION_ID = simgrid::s4u::Host::extension_create(free_ns3_host);
251   NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib, free_ns3_host);
252 }
253
254 NetworkNS3Model::~NetworkNS3Model() {
255   ns3_finalize();
256   xbt_dynar_free_container(&IPV4addr);
257   xbt_dict_free(&dict_socket);
258 }
259
260 Link* NetworkNS3Model::createLink(const char *name, double bandwidth, double latency, e_surf_link_sharing_policy_t policy,
261     xbt_dict_t properties){
262
263   Link* link = new LinkNS3(this, name, properties, bandwidth, latency);
264   Link::onCreation(link);
265   return link;
266 }
267
268 Action *NetworkNS3Model::communicate(NetCard *src, NetCard *dst,
269                                    double size, double rate)
270 {
271   XBT_DEBUG("Communicate from %s to %s", src->name(), dst->name());
272   NetworkNS3Action *action = new NetworkNS3Action(this, size, 0);
273
274   ns3_create_flow(src->name(), dst->name(), surf_get_clock(), size, action);
275
276   action->m_lastSent = 0;
277   action->p_srcElm = src;
278   action->p_dstElm = dst;
279   networkCommunicateCallbacks(action, src, dst, size, rate);
280
281   return (surf_action_t) action;
282 }
283
284 double NetworkNS3Model::next_occuring_event(double now)
285 {
286   XBT_DEBUG("ns3_next_occuring_event");
287
288   //get the first relevant value from the running_actions list
289   if (!getRunningActionSet()->size() || now == 0.0)
290     return -1.0;
291   else
292     do {
293       ns3_simulator(now);
294       time_to_next_flow_completion = ns3_time() - surf_get_clock();//FIXME: use now instead ?
295     } while(double_equals(time_to_next_flow_completion, 0, sg_surf_precision));
296
297   XBT_DEBUG("min       : %f", now);
298   XBT_DEBUG("ns3  time : %f", ns3_time());
299   XBT_DEBUG("surf time : %f", surf_get_clock());
300   XBT_DEBUG("Next completion %f :", time_to_next_flow_completion);
301
302   return time_to_next_flow_completion;
303 }
304
305 void NetworkNS3Model::updateActionsState(double now, double delta)
306 {
307   xbt_dict_cursor_t cursor = NULL;
308   char *key;
309   void *data;
310
311   static xbt_dynar_t socket_to_destroy = NULL;
312   if(!socket_to_destroy) socket_to_destroy = xbt_dynar_new(sizeof(char*),NULL);
313
314   /* If there are no running flows, just return */
315   if (!getRunningActionSet()->size()) {
316     while(double_positive(now-ns3_time(), sg_surf_precision)) {
317       ns3_simulator(now-ns3_time());
318     }
319     return;
320   }
321
322   NetworkNS3Action *action;
323   xbt_dict_foreach(dict_socket,cursor,key,data){
324     action = static_cast<NetworkNS3Action*>(ns3_get_socket_action(data));
325     XBT_DEBUG("Processing socket %p (action %p)",data,action);
326     action->setRemains(action->getCost() - ns3_get_socket_sent(data));
327
328     if (TRACE_is_enabled() &&
329         action->getState() == SURF_ACTION_RUNNING){
330       double data_sent = ns3_get_socket_sent(data);
331       double data_delta_sent = data_sent - action->m_lastSent;
332
333       std::vector<Link*> *route = new std::vector<Link*>();
334
335       routing_platf->getRouteAndLatency (action->p_srcElm, action->p_dstElm, route, NULL);
336       for (auto link : *route)
337         TRACE_surf_link_set_utilization (link->getName(), action->getCategory(), (data_delta_sent)/delta, now-delta, delta);
338       delete route;
339
340       action->m_lastSent = data_sent;
341     }
342
343     if(ns3_get_socket_is_finished(data) == 1){
344       xbt_dynar_push(socket_to_destroy,&key);
345       XBT_DEBUG("Destroy socket %p of action %p", key, action);
346       action->finish();
347       action->setState(SURF_ACTION_DONE);
348     }
349   }
350
351   while (!xbt_dynar_is_empty(socket_to_destroy)){
352     xbt_dynar_pop(socket_to_destroy,&key);
353
354     void *data = xbt_dict_get (dict_socket, key);
355     action = static_cast<NetworkNS3Action*>(ns3_get_socket_action(data));
356     XBT_DEBUG ("Removing socket %p of action %p", key, action);
357     xbt_dict_remove(dict_socket, key);
358   }
359   return;
360 }
361
362 /************
363  * Resource *
364  ************/
365
366 LinkNS3::LinkNS3(NetworkNS3Model *model, const char *name, xbt_dict_t props, double bandwidth, double latency)
367  : Link(model, name, props)
368  , m_created(1)
369 {
370   m_bandwidth.peak = bandwidth;
371   m_latency.peak = latency;
372 }
373
374 LinkNS3::~LinkNS3()
375 {
376 }
377
378 void LinkNS3::apply_event(tmgr_trace_iterator_t event, double value)
379 {
380   THROW_UNIMPLEMENTED;
381 }
382 void LinkNS3::setBandwidthTrace(tmgr_trace_t trace) {
383   xbt_die("The NS3 network model doesn't support latency state traces");
384 }
385 void LinkNS3::setLatencyTrace(tmgr_trace_t trace) {
386   xbt_die("The NS3 network model doesn't support latency state traces");
387 }
388
389 /**********
390  * Action *
391  **********/
392
393 NetworkNS3Action::NetworkNS3Action(Model *model, double cost, bool failed)
394 : NetworkAction(model, cost, failed)
395 {}
396
397 void NetworkNS3Action::suspend()
398 {
399   THROW_UNIMPLEMENTED;
400 }
401
402 void NetworkNS3Action::resume()
403 {
404   THROW_UNIMPLEMENTED;
405 }
406
407   /* Test whether a flow is suspended */
408 bool NetworkNS3Action::isSuspended()
409 {
410   return 0;
411 }
412
413 int NetworkNS3Action::unref()
414 {
415   m_refcount--;
416   if (!m_refcount) {
417   if (action_hook.is_linked())
418     p_stateSet->erase(p_stateSet->iterator_to(*this));
419     XBT_DEBUG ("Removing action %p", this);
420   delete this;
421     return 1;
422   }
423   return 0;
424 }
425
426 }
427 }