Logo AND Algorithmique Numérique Distribuée

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