Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc++' into mc-merge
[simgrid.git] / src / surf / network_ns3.cpp
1 /* Copyright (c) 2007-2013. The SimGrid Team.
2  *
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5 #include "network_ns3.hpp"
6 #include "surf_private.h"
7 #include "simgrid/sg_config.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_ns3, surf,
10                                 "Logging specific to the SURF network NS3 module");
11
12 extern xbt_lib_t host_lib;
13 extern xbt_lib_t link_lib;
14 extern xbt_lib_t as_router_lib;
15
16 extern xbt_dict_t dict_socket;
17
18 xbt_dynar_t IPV4addr;
19 static double time_to_next_flow_completion = -1;
20
21 /*************
22  * Callbacks *
23  *************/
24
25 static void replace_bdw_ns3(char ** bdw)
26 {
27   char *temp = xbt_strdup(*bdw);
28   xbt_free(*bdw);
29   *bdw = bprintf("%fBps",atof(temp));
30   xbt_free(temp);
31
32 }
33
34 static void replace_lat_ns3(char ** lat)
35 {
36   char *temp = xbt_strdup(*lat);
37   xbt_free(*lat);
38   *lat = bprintf("%fs",atof(temp));
39   xbt_free(temp);
40 }
41
42 static void parse_ns3_add_host(sg_platf_host_cbarg_t host)
43 {
44   XBT_DEBUG("NS3_ADD_HOST '%s'",host->id);
45   xbt_lib_set(host_lib,
46               host->id,
47               NS3_HOST_LEVEL,
48               ns3_add_host(host->id)
49     );
50 }
51
52 static void parse_ns3_add_link(sg_platf_link_cbarg_t link)
53 {
54   XBT_DEBUG("NS3_ADD_LINK '%s'",link->id);
55
56   if(!IPV4addr) IPV4addr = xbt_dynar_new(sizeof(char*),free);
57
58   NetworkLinkPtr net_link = surf_network_model->createResource(link->id,
59                                      link->bandwidth,
60                                      link->bandwidth_trace,
61                                      link->latency,
62                                      link->latency_trace,
63                                      link->state,
64                                      link->state_trace,
65                                      link->policy,
66                                      link->properties);
67   xbt_lib_set(link_lib, link->id, SURF_LINK_LEVEL, net_link);
68 }
69
70 static void parse_ns3_add_router(sg_platf_router_cbarg_t router)
71 {
72   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router->id);
73   xbt_lib_set(as_router_lib,
74               router->id,
75               NS3_ASR_LEVEL,
76               ns3_add_router(router->id)
77     );
78 }
79
80 static void parse_ns3_add_AS(sg_platf_AS_cbarg_t AS)
81 {
82   XBT_DEBUG("NS3_ADD_AS '%s'",AS->id);
83   xbt_lib_set(as_router_lib,
84               AS->id,
85               NS3_ASR_LEVEL,
86               ns3_add_AS(AS->id)
87     );
88 }
89
90 static void parse_ns3_add_cluster(sg_platf_cluster_cbarg_t cluster)
91 {
92   const char *cluster_prefix = cluster->prefix;
93   const char *cluster_suffix = cluster->suffix;
94   const char *cluster_radical = cluster->radical;
95   const char *cluster_bb_bw = bprintf("%f",cluster->bb_bw);
96   const char *cluster_bb_lat = bprintf("%f",cluster->bb_lat);
97   const char *cluster_bw = bprintf("%f",cluster->bw);
98   const char *cluster_lat = bprintf("%f",cluster->lat);
99   const char *groups = NULL;
100
101   int start, end, i;
102   unsigned int iter;
103
104   xbt_dynar_t radical_elements;
105   xbt_dynar_t radical_ends;
106   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
107
108   char *router_id,*host_id;
109
110   radical_elements = xbt_str_split(cluster_radical, ",");
111   xbt_dynar_foreach(radical_elements, iter, groups) {
112     radical_ends = xbt_str_split(groups, "-");
113
114     switch (xbt_dynar_length(radical_ends)) {
115     case 1:
116       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
117       xbt_dynar_push_as(tab_elements_num, int, start);
118       router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
119       xbt_lib_set(host_lib,
120                   router_id,
121                   NS3_HOST_LEVEL,
122                   ns3_add_host_cluster(router_id)
123         );
124       XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
125       free(router_id);
126       break;
127
128     case 2:
129       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
130       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
131       for (i = start; i <= end; i++){
132         xbt_dynar_push_as(tab_elements_num, int, i);
133         router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
134         xbt_lib_set(host_lib,
135                     router_id,
136                     NS3_HOST_LEVEL,
137                     ns3_add_host_cluster(router_id)
138           );
139         XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
140         free(router_id);
141       }
142       break;
143
144     default:
145       XBT_DEBUG("Malformed radical");
146     }
147   }
148
149   //Create links
150   unsigned int cpt;
151   int elmts;
152   char * lat = xbt_strdup(cluster_lat);
153   char * bw =  xbt_strdup(cluster_bw);
154   replace_lat_ns3(&lat);
155   replace_bdw_ns3(&bw);
156
157   xbt_dynar_foreach(tab_elements_num,cpt,elmts)
158   {
159     host_id   = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
160     router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
161     XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
162
163     ns3_nodes_t host_src = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,host_id,  NS3_HOST_LEVEL));
164     ns3_nodes_t host_dst = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL));
165
166     if(host_src && host_dst){}
167     else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
168
169     ns3_add_link(host_src->node_num,host_src->type,
170                  host_dst->node_num,host_dst->type,
171                  bw,lat);
172
173     free(router_id);
174     free(host_id);
175   }
176   xbt_dynar_free(&tab_elements_num);
177
178
179   //Create link backbone
180   lat = xbt_strdup(cluster_bb_lat);
181   bw =  xbt_strdup(cluster_bb_bw);
182   replace_lat_ns3(&lat);
183   replace_bdw_ns3(&bw);
184   ns3_add_cluster(bw,lat,cluster->id);
185   xbt_free(lat);
186   xbt_free(bw);
187 }
188
189 /* Create the ns3 topology based on routing strategy */
190 static void create_ns3_topology(void)
191 {
192   XBT_DEBUG("Starting topology generation");
193
194   xbt_dynar_shrink(IPV4addr,0);
195
196   //get the onelinks from the parsed platform
197   xbt_dynar_t onelink_routes = routing_platf->getOneLinkRoutes();
198   if (!onelink_routes)
199     xbt_die("There is no routes!");
200   XBT_DEBUG("Have get_onelink_routes, found %ld routes",onelink_routes->used);
201   //save them in trace file
202   OnelinkPtr onelink;
203   unsigned int iter;
204   xbt_dynar_foreach(onelink_routes, iter, onelink) {
205     char *src = onelink->p_src->getName();
206     char *dst = onelink->p_dst->getName();
207     NetworkNS3LinkPtr link = static_cast<NetworkNS3LinkPtr>(onelink->p_link);
208
209     if (strcmp(src,dst) && link->m_created){
210       XBT_DEBUG("Route from '%s' to '%s' with link '%s'", src, dst, link->getName());
211       char * link_bdw = xbt_strdup(link->p_bdw);
212       char * link_lat = xbt_strdup(link->p_lat);
213       replace_lat_ns3(&link_lat);
214       replace_bdw_ns3(&link_bdw);
215       link->m_created = 0;
216
217       //   XBT_DEBUG("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
218       XBT_DEBUG("\tLink (%s) bdw:%s lat:%s", link->getName(), link_bdw, link_lat);
219
220       //create link ns3
221       ns3_nodes_t host_src = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,src,NS3_HOST_LEVEL));
222       if(!host_src) host_src = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(as_router_lib,src,NS3_ASR_LEVEL));
223       ns3_nodes_t host_dst = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,dst,NS3_HOST_LEVEL));
224       if(!host_dst) host_dst = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(as_router_lib,dst,NS3_ASR_LEVEL));
225
226       if(host_src && host_dst){}
227       else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
228
229       ns3_add_link(host_src->node_num,host_src->type,host_dst->node_num,host_dst->type,link_bdw,link_lat);
230
231       xbt_free(link_bdw);
232       xbt_free(link_lat);
233     }
234   }
235 }
236
237 static void parse_ns3_end_platform(void)
238 {
239   ns3_end_platform();
240 }
241
242 static void define_callbacks_ns3(void)
243 {
244   sg_platf_host_add_cb (&parse_ns3_add_host);
245   sg_platf_router_add_cb (&parse_ns3_add_router);
246   sg_platf_link_add_cb (&parse_ns3_add_link);
247   sg_platf_cluster_add_cb (&parse_ns3_add_cluster);
248   sg_platf_AS_begin_add_cb (&parse_ns3_add_AS);
249   sg_platf_postparse_add_cb(&create_ns3_topology); //get_one_link_routes
250   sg_platf_postparse_add_cb(&parse_ns3_end_platform); //InitializeRoutes
251 }
252
253 /*********
254  * Model *
255  *********/
256 static void free_ns3_link(void * elmts)
257 {
258   delete static_cast<NetworkNS3LinkPtr>(elmts);
259 }
260
261 static void free_ns3_host(void * elmts)
262 {
263   ns3_nodes_t host = static_cast<ns3_nodes_t>(elmts);
264   free(host);
265 }
266
267 void surf_network_model_init_NS3()
268 {
269   if (surf_network_model)
270     return;
271
272   surf_network_model = new NetworkNS3Model();
273
274   xbt_dynar_push(model_list, &surf_network_model);
275 }
276
277 NetworkNS3Model::NetworkNS3Model() : NetworkModel("network NS3") {
278   if (ns3_initialize(xbt_cfg_get_string(_sg_cfg_set, "ns3/TcpModel"))) {
279     xbt_die("Impossible to initialize NS3 interface");
280   }
281   routing_model_create(NULL);
282   define_callbacks_ns3();
283
284   NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
285   NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
286   NS3_LINK_LEVEL = xbt_lib_add_level(link_lib,(void_f_pvoid_t)free_ns3_link);
287 }
288
289 NetworkNS3Model::~NetworkNS3Model() {
290   ns3_finalize();
291   xbt_dynar_free_container(&IPV4addr);
292   xbt_dict_free(&dict_socket);
293 }
294
295 NetworkLinkPtr NetworkNS3Model::createResource(const char *name,
296                                          double bw_initial,
297                                          tmgr_trace_t bw_trace,
298                                          double lat_initial,
299                                          tmgr_trace_t lat_trace,
300                                          e_surf_resource_state_t state_initial,
301                                          tmgr_trace_t state_trace,
302                                          e_surf_link_sharing_policy_t policy,
303                                          xbt_dict_t properties){
304   if (bw_trace)
305     XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
306   if (lat_trace)
307     XBT_INFO("The NS3 network model doesn't support latency state traces");
308   if (state_trace)
309     XBT_INFO("The NS3 network model doesn't support link state traces");
310   return new NetworkNS3Link(this, name, properties, bw_initial, lat_initial);
311 }
312
313 xbt_dynar_t NetworkNS3Model::getRoute(RoutingEdgePtr src, RoutingEdgePtr dst)
314 {
315   xbt_dynar_t route = NULL;
316   routing_get_route_and_latency(src, dst, &route, NULL);
317   //routing_platf->getRouteAndLatency(src, dst, &route, NULL);
318   return route;
319 }
320
321 ActionPtr NetworkNS3Model::communicate(RoutingEdgePtr src, RoutingEdgePtr dst,
322                                                double size, double rate)
323 {
324   XBT_DEBUG("Communicate from %s to %s", src->getName(), dst->getName());
325   NetworkNS3ActionPtr action = new NetworkNS3Action(this, size, 0);
326
327   ns3_create_flow(src->getName(), dst->getName(), surf_get_clock(), size, action);
328
329 #ifdef HAVE_TRACING
330   action->m_lastSent = 0;
331   action->p_srcElm = src;
332   action->p_dstElm = dst;
333 #endif
334
335   return (surf_action_t) action;
336 }
337
338 double NetworkNS3Model::shareResources(double now)
339 {
340   XBT_DEBUG("ns3_share_resources");
341
342   //get the first relevant value from the running_actions list
343   if (!getRunningActionSet()->size() || now == 0.0)
344     return -1.0;
345   else
346     do {
347       ns3_simulator(now);
348       time_to_next_flow_completion = ns3_time() - surf_get_clock();//FIXME: use now instead ?
349     } while(double_equals(time_to_next_flow_completion, 0));
350
351   XBT_DEBUG("min       : %f", now);
352   XBT_DEBUG("ns3  time : %f", ns3_time());
353   XBT_DEBUG("surf time : %f", surf_get_clock());
354   XBT_DEBUG("Next completion %f :", time_to_next_flow_completion);
355
356   return time_to_next_flow_completion;
357 }
358
359 void NetworkNS3Model::updateActionsState(double now, double delta)
360 {
361   xbt_dict_cursor_t cursor = NULL;
362   char *key;
363   void *data;
364
365   static xbt_dynar_t socket_to_destroy = NULL;
366   if(!socket_to_destroy) socket_to_destroy = xbt_dynar_new(sizeof(char*),NULL);
367
368   /* If there are no running flows, just return */
369   if (!getRunningActionSet()->size()) {
370     while(double_positive(now-ns3_time())) {
371       ns3_simulator(now-ns3_time());
372     }
373     return;
374   }
375
376   NetworkNS3ActionPtr action;
377   xbt_dict_foreach(dict_socket,cursor,key,data){
378     action = static_cast<NetworkNS3ActionPtr>(ns3_get_socket_action(data));
379     XBT_DEBUG("Processing socket %p (action %p)",data,action);
380     action->setRemains(action->getCost() - ns3_get_socket_sent(data));
381
382     #ifdef HAVE_TRACING
383       if (TRACE_is_enabled() &&
384           action->getState() == SURF_ACTION_RUNNING){
385         double data_sent = ns3_get_socket_sent(data);
386         double data_delta_sent = data_sent - action->m_lastSent;
387
388         xbt_dynar_t route = NULL;
389
390         routing_get_route_and_latency (action->p_srcElm, action->p_dstElm, &route, NULL);
391         unsigned int i;
392         for (i = 0; i < xbt_dynar_length (route); i++){
393                 NetworkNS3LinkPtr link = ((NetworkNS3LinkPtr)xbt_dynar_get_ptr (route, i));
394           TRACE_surf_link_set_utilization (link->getName(),
395                                            action->getCategory(),
396                                            (data_delta_sent)/delta,
397                                            now-delta,
398                                            delta);
399         }
400         action->m_lastSent = data_sent;
401       }
402     #endif
403
404     if(ns3_get_socket_is_finished(data) == 1){
405       xbt_dynar_push(socket_to_destroy,&key);
406       XBT_DEBUG("Destroy socket %p of action %p", key, action);
407       action->finish();
408       action->setState(SURF_ACTION_DONE);
409     }
410   }
411
412   while (!xbt_dynar_is_empty(socket_to_destroy)){
413     xbt_dynar_pop(socket_to_destroy,&key);
414
415     void *data = xbt_dict_get (dict_socket, key);
416     action = static_cast<NetworkNS3ActionPtr>(ns3_get_socket_action(data));
417     XBT_DEBUG ("Removing socket %p of action %p", key, action);
418     xbt_dict_remove(dict_socket, key);
419   }
420   return;
421 }
422
423 /************
424  * Resource *
425  ************/
426
427 NetworkNS3Link::NetworkNS3Link(NetworkNS3ModelPtr model, const char *name, xbt_dict_t props,
428                                        double bw_initial, double lat_initial)
429  : NetworkLink(model, name, props)
430  , p_bdw(bprintf("%f", bw_initial))
431  , p_lat(bprintf("%f", lat_initial))
432  , m_created(1)
433 {
434 }
435
436 NetworkNS3Link::~NetworkNS3Link()
437 {
438 }
439
440 void NetworkNS3Link::updateState(tmgr_trace_event_t event_type, double value, double date)
441 {
442
443 }
444 double NetworkNS3Link::getLatency()
445 {
446
447 }
448 double NetworkNS3Link::getBandwidth()
449 {
450
451 }
452
453 /**********
454  * Action *
455  **********/
456
457 NetworkNS3Action::NetworkNS3Action(ModelPtr model, double cost, bool failed)
458 : NetworkAction(model, cost, failed)
459 {}
460
461 #ifdef HAVE_LATENCY_BOUND_TRACKING
462   int NetworkNS3Action::getLatencyLimited() {
463     return m_latencyLimited;
464   }
465 #endif
466
467  void NetworkNS3Action::suspend()
468 {
469   THROW_UNIMPLEMENTED;
470 }
471
472 void NetworkNS3Action::resume()
473 {
474   THROW_UNIMPLEMENTED;
475 }
476
477   /* Test whether a flow is suspended */
478 bool NetworkNS3Action::isSuspended()
479 {
480   return 0;
481 }
482
483 int NetworkNS3Action::unref()
484 {
485   m_refcount--;
486   if (!m_refcount) {
487         if (actionHook::is_linked())
488           p_stateSet->erase(p_stateSet->iterator_to(*this));
489     XBT_DEBUG ("Removing action %p", this);
490         delete this;
491     return 1;
492   }
493   return 0;
494 }