Logo AND Algorithmique Numérique Distribuée

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