Logo AND Algorithmique Numérique Distribuée

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