Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fc3cb25fd9f654b2c7f978e0e8a38caeebbf4262
[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(simgrid::surf::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 simgrid_ns3_add_router(simgrid::surf::RoutingEdge* router)
72 {
73   const char* router_id = router->getName();
74   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
75   xbt_lib_set(as_router_lib,
76               router_id,
77               NS3_ASR_LEVEL,
78               ns3_add_router(router_id)
79     );
80 }
81
82 static void parse_ns3_add_AS(simgrid::surf::As* as)
83 {
84   const char* as_id = as->p_name;
85   XBT_DEBUG("NS3_ADD_AS '%s'", as_id);
86   xbt_lib_set(as_router_lib, as_id, NS3_ASR_LEVEL, ns3_add_AS(as_id) );
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   simgrid::surf::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     simgrid::surf::NetworkNS3Link *link =
207       static_cast<simgrid::surf::NetworkNS3Link *>(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   simgrid::surf::hostCreatedCallbacks.connect(simgrid_ns3_add_host);
245   simgrid::surf::routingEdgeCreatedCallbacks.connect(simgrid_ns3_add_router);
246   sg_platf_link_add_cb (&parse_ns3_add_link);
247   sg_platf_cluster_add_cb (&parse_ns3_add_cluster);
248   simgrid::surf::asCreatedCallbacks.connect(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<simgrid::surf::NetworkNS3Link*>(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 simgrid::surf::NetworkNS3Model();
273
274   xbt_dynar_push(all_existing_models, &surf_network_model);
275 }
276
277 namespace simgrid {
278 namespace surf {
279
280 NetworkNS3Model::NetworkNS3Model() : NetworkModel() {
281   if (ns3_initialize(xbt_cfg_get_string(_sg_cfg_set, "ns3/TcpModel"))) {
282     xbt_die("Impossible to initialize NS3 interface");
283   }
284   routing_model_create(NULL);
285   define_callbacks_ns3();
286
287   NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
288   NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
289 }
290
291 NetworkNS3Model::~NetworkNS3Model() {
292   ns3_finalize();
293   xbt_dynar_free_container(&IPV4addr);
294   xbt_dict_free(&dict_socket);
295 }
296
297 Link* NetworkNS3Model::createLink(const char *name,
298                                          double bw_initial,
299                                          tmgr_trace_t bw_trace,
300                                          double lat_initial,
301                                          tmgr_trace_t lat_trace,
302                                          e_surf_resource_state_t state_initial,
303                                          tmgr_trace_t state_trace,
304                                          e_surf_link_sharing_policy_t policy,
305                                          xbt_dict_t properties){
306   if (bw_trace)
307     XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
308   if (lat_trace)
309     XBT_INFO("The NS3 network model doesn't support latency state traces");
310   if (state_trace)
311     XBT_INFO("The NS3 network model doesn't support link state traces");
312   Link* link = new NetworkNS3Link(this, name, properties, bw_initial, lat_initial);
313   surf_callback_emit(networkLinkCreatedCallbacks, link);
314   return link;
315 }
316
317 xbt_dynar_t NetworkNS3Model::getRoute(RoutingEdge *src, RoutingEdge *dst)
318 {
319   xbt_dynar_t route = NULL;
320   routing_get_route_and_latency(src, dst, &route, NULL);
321   //routing_platf->getRouteAndLatency(src, dst, &route, NULL);
322   return route;
323 }
324
325 Action *NetworkNS3Model::communicate(RoutingEdge *src, RoutingEdge *dst,
326                                                double size, double rate)
327 {
328   XBT_DEBUG("Communicate from %s to %s", src->getName(), dst->getName());
329   NetworkNS3Action *action = new NetworkNS3Action(this, size, 0);
330
331   ns3_create_flow(src->getName(), dst->getName(), surf_get_clock(), size, action);
332
333   action->m_lastSent = 0;
334   action->p_srcElm = src;
335   action->p_dstElm = dst;
336   surf_callback_emit(networkCommunicateCallbacks, action, src, dst, size, rate);
337
338   return (surf_action_t) action;
339 }
340
341 double NetworkNS3Model::shareResources(double now)
342 {
343   XBT_DEBUG("ns3_share_resources");
344
345   //get the first relevant value from the running_actions list
346   if (!getRunningActionSet()->size() || now == 0.0)
347     return -1.0;
348   else
349     do {
350       ns3_simulator(now);
351       time_to_next_flow_completion = ns3_time() - surf_get_clock();//FIXME: use now instead ?
352     } while(double_equals(time_to_next_flow_completion, 0, sg_surf_precision));
353
354   XBT_DEBUG("min       : %f", now);
355   XBT_DEBUG("ns3  time : %f", ns3_time());
356   XBT_DEBUG("surf time : %f", surf_get_clock());
357   XBT_DEBUG("Next completion %f :", time_to_next_flow_completion);
358
359   return time_to_next_flow_completion;
360 }
361
362 void NetworkNS3Model::updateActionsState(double now, double delta)
363 {
364   xbt_dict_cursor_t cursor = NULL;
365   char *key;
366   void *data;
367
368   static xbt_dynar_t socket_to_destroy = NULL;
369   if(!socket_to_destroy) socket_to_destroy = xbt_dynar_new(sizeof(char*),NULL);
370
371   /* If there are no running flows, just return */
372   if (!getRunningActionSet()->size()) {
373     while(double_positive(now-ns3_time(), sg_surf_precision)) {
374       ns3_simulator(now-ns3_time());
375     }
376     return;
377   }
378
379   NetworkNS3Action *action;
380   xbt_dict_foreach(dict_socket,cursor,key,data){
381     action = static_cast<NetworkNS3Action*>(ns3_get_socket_action(data));
382     XBT_DEBUG("Processing socket %p (action %p)",data,action);
383     action->setRemains(action->getCost() - ns3_get_socket_sent(data));
384
385     if (TRACE_is_enabled() &&
386                 action->getState() == SURF_ACTION_RUNNING){
387         double data_sent = ns3_get_socket_sent(data);
388         double data_delta_sent = data_sent - action->m_lastSent;
389
390         xbt_dynar_t route = NULL;
391
392         routing_get_route_and_latency (action->p_srcElm, action->p_dstElm, &route, NULL);
393         unsigned int i;
394         for (i = 0; i < xbt_dynar_length (route); i++){
395                 NetworkNS3Link* link = ((NetworkNS3Link*)xbt_dynar_get_ptr(route, i));
396                 TRACE_surf_link_set_utilization (link->getName(),
397                                 action->getCategory(),
398                                         (data_delta_sent)/delta,
399                                         now-delta,
400                                         delta);
401         }
402         action->m_lastSent = data_sent;
403     }
404
405     if(ns3_get_socket_is_finished(data) == 1){
406       xbt_dynar_push(socket_to_destroy,&key);
407       XBT_DEBUG("Destroy socket %p of action %p", key, action);
408       action->finish();
409       action->setState(SURF_ACTION_DONE);
410     }
411   }
412
413   while (!xbt_dynar_is_empty(socket_to_destroy)){
414     xbt_dynar_pop(socket_to_destroy,&key);
415
416     void *data = xbt_dict_get (dict_socket, key);
417     action = static_cast<NetworkNS3Action*>(ns3_get_socket_action(data));
418     XBT_DEBUG ("Removing socket %p of action %p", key, action);
419     xbt_dict_remove(dict_socket, key);
420   }
421   return;
422 }
423
424 /************
425  * Resource *
426  ************/
427
428 NetworkNS3Link::NetworkNS3Link(NetworkNS3Model *model, const char *name, xbt_dict_t props,
429                                        double bw_initial, double lat_initial)
430  : Link(model, name, props)
431  , p_lat(bprintf("%f", lat_initial))
432  , p_bdw(bprintf("%f", bw_initial))
433  , m_created(1)
434 {
435 }
436
437 NetworkNS3Link::~NetworkNS3Link()
438 {
439 }
440
441 void NetworkNS3Link::updateState(tmgr_trace_event_t event_type, double value, double date)
442 {
443
444 }
445
446 /**********
447  * Action *
448  **********/
449
450 NetworkNS3Action::NetworkNS3Action(Model *model, double cost, bool failed)
451 : NetworkAction(model, cost, failed)
452 {}
453
454 #ifdef HAVE_LATENCY_BOUND_TRACKING
455   int NetworkNS3Action::getLatencyLimited() {
456     return m_latencyLimited;
457   }
458 #endif
459
460  void NetworkNS3Action::suspend()
461 {
462   THROW_UNIMPLEMENTED;
463 }
464
465 void NetworkNS3Action::resume()
466 {
467   THROW_UNIMPLEMENTED;
468 }
469
470   /* Test whether a flow is suspended */
471 bool NetworkNS3Action::isSuspended()
472 {
473   return 0;
474 }
475
476 int NetworkNS3Action::unref()
477 {
478   m_refcount--;
479   if (!m_refcount) {
480         if (action_hook.is_linked())
481           p_stateSet->erase(p_stateSet->iterator_to(*this));
482     XBT_DEBUG ("Removing action %p", this);
483         delete this;
484     return 1;
485   }
486   return 0;
487 }
488
489 }
490 }