Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused variables.
[simgrid.git] / src / surf / network_ns3.c
1 /* Copyright (c) 2007-2013. 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 "surf_private.h"
8 #include "surf/maxmin.h"
9 #include "surf/ns3/ns3_interface.h"
10 #include "xbt/lib.h"
11 #include "surf/network_ns3_private.h"
12 #include "xbt/str.h"
13
14 extern xbt_lib_t host_lib;
15 extern xbt_lib_t link_lib;
16 extern xbt_lib_t as_router_lib;
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_ns3, surf,
19                                 "Logging specific to the SURF network NS3 module");
20
21 extern routing_platf_t routing_platf;
22 extern xbt_dict_t dict_socket;
23
24 static double time_to_next_flow_completion = -1;
25
26 static double ns3_share_resources(surf_model_t network_model, double min);
27 static void ns3_update_actions_state(surf_model_t network_model, double now, double delta);
28 static void finalize(surf_model_t network_model);
29 static surf_action_t ns3_communicate(sg_routing_edge_t src_elm,
30                                      sg_routing_edge_t dst_elm,
31                                      double size, double rate);
32 static void action_suspend(surf_action_t action);
33 static void action_resume(surf_action_t action);
34 static int action_is_suspended(surf_action_t action);
35 static int action_unref(surf_action_t action);
36
37 xbt_dynar_t IPV4addr;
38
39 static void replace_bdw_ns3(char ** bdw)
40 {
41   char *temp = xbt_strdup(*bdw);
42   xbt_free(*bdw);
43   *bdw = bprintf("%fBps",atof(temp));
44   xbt_free(temp);
45
46 }
47
48 static void replace_lat_ns3(char ** lat)
49 {
50   char *temp = xbt_strdup(*lat);
51   xbt_free(*lat);
52   *lat = bprintf("%fs",atof(temp));
53   xbt_free(temp);
54 }
55
56 static void parse_ns3_add_host(sg_platf_host_cbarg_t host)
57 {
58   XBT_DEBUG("NS3_ADD_HOST '%s'",host->id);
59   xbt_lib_set(host_lib,
60               host->id,
61               NS3_HOST_LEVEL,
62               ns3_add_host(host->id)
63     );
64 }
65
66 static void parse_ns3_add_link(sg_platf_link_cbarg_t link)
67 {
68   XBT_DEBUG("NS3_ADD_LINK '%s'",link->id);
69
70   if(!IPV4addr) IPV4addr = xbt_dynar_new(sizeof(char*),free);
71
72   tmgr_trace_t bw_trace;
73   tmgr_trace_t state_trace;
74   tmgr_trace_t lat_trace;
75
76   bw_trace = link->bandwidth_trace;
77   lat_trace = link->latency_trace;
78   state_trace = link->state_trace;
79
80   if (bw_trace)
81     XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
82   if (lat_trace)
83     XBT_INFO("The NS3 network model doesn't support latency state traces");
84   if (state_trace)
85     XBT_INFO("The NS3 network model doesn't support link state traces");
86
87   ns3_link_t link_ns3 = xbt_new0(s_ns3_link_t,1);;
88   link_ns3->id = xbt_strdup((char*)(link->id));
89   link_ns3->bdw = bprintf("%f",link->bandwidth);
90   link_ns3->lat = bprintf("%f",link->latency);
91
92   surf_ns3_link_t l = xbt_new0(s_surf_ns3_link_t,1);
93   l->generic_resource.name = xbt_strdup(link->id);
94   l->generic_resource.properties = current_property_set;
95   l->data = link_ns3;
96   l->created = 1;
97
98   xbt_lib_set(link_lib,link->id,NS3_LINK_LEVEL,link_ns3);
99   xbt_lib_set(link_lib,link->id,SURF_LINK_LEVEL,l);
100 }
101
102 static void parse_ns3_add_router(sg_platf_router_cbarg_t router)
103 {
104   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router->id);
105   xbt_lib_set(as_router_lib,
106               router->id,
107               NS3_ASR_LEVEL,
108               ns3_add_router(router->id)
109     );
110 }
111
112 static void parse_ns3_add_AS(sg_platf_AS_cbarg_t AS)
113 {
114   XBT_DEBUG("NS3_ADD_AS '%s'",AS->id);
115   xbt_lib_set(as_router_lib,
116               AS->id,
117               NS3_ASR_LEVEL,
118               ns3_add_AS(AS->id)
119     );
120 }
121
122 static void parse_ns3_add_cluster(sg_platf_cluster_cbarg_t cluster)
123 {
124   const char *cluster_prefix = cluster->prefix;
125   const char *cluster_suffix = cluster->suffix;
126   const char *cluster_radical = cluster->radical;
127   const char *cluster_bb_bw = bprintf("%f",cluster->bb_bw);
128   const char *cluster_bb_lat = bprintf("%f",cluster->bb_lat);
129   const char *cluster_bw = bprintf("%f",cluster->bw);
130   const char *cluster_lat = bprintf("%f",cluster->lat);
131   const char *groups = NULL;
132
133   int start, end, i;
134   unsigned int iter;
135
136   xbt_dynar_t radical_elements;
137   xbt_dynar_t radical_ends;
138   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
139
140   char *router_id,*host_id;
141
142   radical_elements = xbt_str_split(cluster_radical, ",");
143   xbt_dynar_foreach(radical_elements, iter, groups) {
144     radical_ends = xbt_str_split(groups, "-");
145
146     switch (xbt_dynar_length(radical_ends)) {
147     case 1:
148       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
149       xbt_dynar_push_as(tab_elements_num, int, start);
150       router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
151       xbt_lib_set(host_lib,
152                   router_id,
153                   NS3_HOST_LEVEL,
154                   ns3_add_host_cluster(router_id)
155         );
156       XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
157       free(router_id);
158       break;
159
160     case 2:
161       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
162       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
163       for (i = start; i <= end; i++){
164         xbt_dynar_push_as(tab_elements_num, int, i);
165         router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
166         xbt_lib_set(host_lib,
167                     router_id,
168                     NS3_HOST_LEVEL,
169                     ns3_add_host_cluster(router_id)
170           );
171         XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
172         free(router_id);
173       }
174       break;
175
176     default:
177       XBT_DEBUG("Malformed radical");
178     }
179   }
180
181   //Create links
182   unsigned int cpt;
183   int elmts;
184   char * lat = xbt_strdup(cluster_lat);
185   char * bw =  xbt_strdup(cluster_bw);
186   replace_lat_ns3(&lat);
187   replace_bdw_ns3(&bw);
188
189   xbt_dynar_foreach(tab_elements_num,cpt,elmts)
190   {
191     host_id   = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
192     router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
193     XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
194
195     ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,host_id,  NS3_HOST_LEVEL);
196     ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL);
197
198     if(host_src && host_dst){}
199     else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
200
201     ns3_add_link(host_src->node_num,host_src->type,
202                  host_dst->node_num,host_dst->type,
203                  bw,lat);
204
205     free(router_id);
206     free(host_id);
207   }
208   xbt_dynar_free(&tab_elements_num);
209
210
211   //Create link backbone
212   lat = xbt_strdup(cluster_bb_lat);
213   bw =  xbt_strdup(cluster_bb_bw);
214   replace_lat_ns3(&lat);
215   replace_bdw_ns3(&bw);
216   ns3_add_cluster(bw,lat,cluster->id);
217   xbt_free(lat);
218   xbt_free(bw);  
219 }
220
221 static double ns3_get_link_latency (const void *link)
222 {
223   double lat;
224   //XBT_DEBUG("link_id:%s link_lat:%s link_bdw:%s",((surf_ns3_link_t)link)->data->id,((surf_ns3_link_t)link)->data->lat,((surf_ns3_link_t)link)->data->bdw);
225   sscanf(((surf_ns3_link_t)link)->data->lat,"%lg",&lat);
226   return lat;
227 }
228 static double ns3_get_link_bandwidth (const void *link)
229 {
230   double bdw;
231   //XBT_DEBUG("link_id:%s link_lat:%s link_bdw:%s",((surf_ns3_link_t)link)->data->id,((surf_ns3_link_t)link)->data->lat,((surf_ns3_link_t)link)->data->bdw);
232   sscanf(((surf_ns3_link_t)link)->data->bdw,"%lg",&bdw);
233   return bdw;
234 }
235
236 static xbt_dynar_t ns3_get_route(void *src_card, void *dst_card)
237 {
238   xbt_dynar_t route = NULL;
239   routing_get_route_and_latency(src_card, dst_card, &route, NULL);
240   return route;
241 }
242
243 static void parse_ns3_end_platform(void)
244 {
245   ns3_end_platform();
246 }
247
248 /* Create the ns3 topology based on routing strategy */
249 static void create_ns3_topology(void)
250 {
251   XBT_DEBUG("Starting topology generation");
252
253   xbt_dynar_shrink(IPV4addr,0);
254
255   //get the onelinks from the parsed platform
256   xbt_dynar_t onelink_routes = routing_platf->get_onelink_routes();
257   if (!onelink_routes)
258     xbt_die("There is no routes!");
259   XBT_DEBUG("Have get_onelink_routes, found %ld routes",onelink_routes->used);
260   //save them in trace file
261   onelink_t onelink;
262   unsigned int iter;
263   xbt_dynar_foreach(onelink_routes, iter, onelink) {
264     char *src = onelink->src->name;
265     char *dst = onelink->dst->name;
266     void *link = onelink->link_ptr;
267     if( strcmp(src,dst) && ((surf_ns3_link_t)link)->created){
268       XBT_DEBUG("Route from '%s' to '%s' with link '%s'",src,dst,((surf_ns3_link_t)link)->data->id);
269       char * link_bdw = xbt_strdup(((surf_ns3_link_t)link)->data->bdw);
270       char * link_lat = xbt_strdup(((surf_ns3_link_t)link)->data->lat);
271       replace_lat_ns3(&link_lat);
272       replace_bdw_ns3(&link_bdw);
273       ((surf_ns3_link_t)link)->created = 0;
274
275       //   XBT_DEBUG("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
276       XBT_DEBUG("\tLink (%s) bdw:%s lat:%s",((surf_ns3_link_t)link)->data->id,
277                 link_bdw,
278                 link_lat
279         );
280
281       //create link ns3
282       ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,src,NS3_HOST_LEVEL);
283       if(!host_src) host_src = xbt_lib_get_or_null(as_router_lib,src,NS3_ASR_LEVEL);
284       ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,dst,NS3_HOST_LEVEL);
285       if(!host_dst) host_dst = xbt_lib_get_or_null(as_router_lib,dst,NS3_ASR_LEVEL);
286
287       if(host_src && host_dst){}
288       else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
289
290       ns3_add_link(host_src->node_num,host_src->type,host_dst->node_num,host_dst->type,link_bdw,link_lat);
291
292       xbt_free(link_bdw);
293       xbt_free(link_lat);
294     }
295   }
296 }
297
298 static void define_callbacks_ns3(void)
299 {
300   sg_platf_host_add_cb (&parse_ns3_add_host);
301   sg_platf_router_add_cb (&parse_ns3_add_router);
302   sg_platf_link_add_cb (&parse_ns3_add_link);
303   sg_platf_cluster_add_cb (&parse_ns3_add_cluster);
304   sg_platf_AS_begin_add_cb (&parse_ns3_add_AS);
305   sg_platf_postparse_add_cb(&create_ns3_topology); //get_one_link_routes
306   sg_platf_postparse_add_cb(&parse_ns3_end_platform); //InitializeRoutes
307 }
308
309 static void free_ns3_link(void * elmts)
310 {
311   ns3_link_t link = elmts;
312   free(link->id);
313   free(link->bdw);
314   free(link->lat);
315   free(link);
316 }
317
318 static void free_ns3_host(void * elmts)
319 {
320   ns3_nodes_t host = elmts;
321   free(host);
322 }
323
324 #ifdef HAVE_LATENCY_BOUND_TRACKING
325 static int ns3_get_link_latency_limited(surf_action_t action)
326 {
327   return 0;
328 }
329 #endif
330
331 #ifdef HAVE_TRACING
332 static void ns3_action_set_category(surf_action_t action, const char *category)
333 {
334   action->category = xbt_strdup (category);
335 }
336 #endif
337
338 void surf_network_model_init_NS3()
339 {
340   if (surf_network_model)
341     return;
342
343   surf_network_model = surf_model_init();
344   surf_network_model->name = "network NS3";
345   surf_network_model->type = SURF_MODEL_TYPE_NETWORK;
346   surf_network_model->extension.network.get_link_latency = ns3_get_link_latency;
347   surf_network_model->extension.network.get_link_bandwidth = ns3_get_link_bandwidth;
348   surf_network_model->extension.network.get_route = ns3_get_route;
349
350   surf_network_model->model_private->share_resources = ns3_share_resources;
351   surf_network_model->model_private->update_actions_state = ns3_update_actions_state;
352   surf_network_model->model_private->finalize = finalize;
353
354   surf_network_model->suspend = action_suspend;
355   surf_network_model->resume = action_resume;
356   surf_network_model->is_suspended = action_is_suspended;
357   surf_network_model->action_unref = action_unref;
358   surf_network_model->extension.network.communicate = ns3_communicate;
359
360 #ifdef HAVE_TRACING
361   surf_network_model->set_category = ns3_action_set_category;
362 #endif
363
364   /* Added the initialization for NS3 interface */
365
366   if (ns3_initialize(xbt_cfg_get_string(_sg_cfg_set, "ns3/TcpModel"))) {
367     xbt_die("Impossible to initialize NS3 interface");
368   }
369
370   routing_model_create(NULL);
371   define_callbacks_ns3();
372
373   NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
374   NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
375   NS3_LINK_LEVEL = xbt_lib_add_level(link_lib,(void_f_pvoid_t)free_ns3_link);
376
377   xbt_dynar_push(model_list, &surf_network_model);
378
379 #ifdef HAVE_LATENCY_BOUND_TRACKING
380   surf_network_model->get_latency_limited = ns3_get_link_latency_limited;
381 #endif
382 }
383
384 static void finalize(surf_model_t network_model)
385 {
386   ns3_finalize();
387   xbt_dynar_free_container(&IPV4addr);
388   xbt_dict_free(&dict_socket);
389 }
390
391 static double ns3_share_resources(surf_model_t network_model, double min)
392 {
393   XBT_DEBUG("ns3_share_resources");
394
395   xbt_swag_t running_actions =
396     network_model->states.running_action_set;
397
398   //get the first relevant value from the running_actions list
399   if (!xbt_swag_size(running_actions) || min == 0.0)
400     return -1.0;
401   else
402     do {
403       ns3_simulator(min);
404       time_to_next_flow_completion = ns3_time() - surf_get_clock();
405     } while(double_equals(time_to_next_flow_completion,0));
406
407   XBT_DEBUG("min       : %f",min);
408   XBT_DEBUG("ns3  time : %f",ns3_time());
409   XBT_DEBUG("surf time : %f",surf_get_clock());
410   XBT_DEBUG("Next completion %f :",time_to_next_flow_completion);
411
412   return time_to_next_flow_completion;
413 }
414
415 static void ns3_update_actions_state(surf_model_t network_model, double now, double delta)
416 {
417   xbt_dict_cursor_t cursor = NULL;
418   char *key;
419   void *data;
420
421   static xbt_dynar_t socket_to_destroy = NULL;
422   if(!socket_to_destroy) socket_to_destroy = xbt_dynar_new(sizeof(char*),NULL);
423
424   surf_action_network_ns3_t action = NULL;
425   xbt_swag_t running_actions =
426     network_model->states.running_action_set;
427
428   /* If there are no running flows, just return */
429   if (!xbt_swag_size(running_actions)) {
430     while(double_positive(now-ns3_time())) {
431       ns3_simulator(now-ns3_time());
432     }
433     return;
434   }
435
436   xbt_dict_foreach(dict_socket,cursor,key,data){
437     action = (surf_action_network_ns3_t)ns3_get_socket_action(data);
438     XBT_DEBUG("Processing socket %p (action %p)",data,action);
439     action->generic_action.remains = action->generic_action.cost - ns3_get_socket_sent(data);
440
441 #ifdef HAVE_TRACING
442     if (TRACE_is_enabled() &&
443         surf_action_state_get(&(action->generic_action)) == SURF_ACTION_RUNNING){
444       double data_sent = ns3_get_socket_sent(data);
445       double data_delta_sent = data_sent - action->last_sent;
446
447       xbt_dynar_t route = NULL;
448
449       routing_get_route_and_latency (action->src_elm, action->dst_elm, &route, NULL);
450       unsigned int i;
451       for (i = 0; i < xbt_dynar_length (route); i++){
452         surf_ns3_link_t *link = ((surf_ns3_link_t*)xbt_dynar_get_ptr (route, i));
453         TRACE_surf_link_set_utilization ((*link)->generic_resource.name,
454                                          ((surf_action_t) action)->category,
455                                          (data_delta_sent)/delta,
456                                          now-delta,
457                                          delta);
458       }
459       action->last_sent = data_sent;
460     }
461 #endif
462
463     if(ns3_get_socket_is_finished(data) == 1){
464       xbt_dynar_push(socket_to_destroy,&key);
465       XBT_DEBUG("Destroy socket %p of action %p", key, action);
466       action->generic_action.finish = now;
467       surf_action_state_set(&(action->generic_action), SURF_ACTION_DONE);
468     }
469   }
470
471   while (!xbt_dynar_is_empty(socket_to_destroy)){
472     xbt_dynar_pop(socket_to_destroy,&key);
473
474     void *data = xbt_dict_get (dict_socket, key);
475     surf_action_network_ns3_t action = (surf_action_network_ns3_t)ns3_get_socket_action(data);
476     XBT_DEBUG ("Removing socket %p of action %p", key, action);
477     xbt_dict_remove(dict_socket,key);
478   }
479   return;
480 }
481
482 /* Max durations are not supported */
483 static surf_action_t ns3_communicate(sg_routing_edge_t src_elm,
484                                      sg_routing_edge_t dst_elm,
485                                      double size, double rate)
486 {
487   surf_action_network_ns3_t action = NULL;
488
489   XBT_DEBUG("Communicate from %s to %s", src_elm->name, dst_elm->name);
490   action = surf_action_new(sizeof(s_surf_action_network_ns3_t), size, surf_network_model, 0);
491
492   ns3_create_flow(src_elm->name, dst_elm->name, surf_get_clock(), size, action);
493
494 #ifdef HAVE_TRACING
495   action->last_sent = 0;
496   action->src_elm = src_elm;
497   action->dst_elm = dst_elm;
498 #endif
499
500   return (surf_action_t) action;
501 }
502
503 /* Suspend a flow() */
504 static void action_suspend(surf_action_t action)
505 {
506   THROW_UNIMPLEMENTED;
507 }
508
509 /* Resume a flow() */
510 static void action_resume(surf_action_t action)
511 {
512   THROW_UNIMPLEMENTED;
513 }
514
515 /* Test whether a flow is suspended */
516 static int action_is_suspended(surf_action_t action)
517 {
518   return 0;
519 }
520
521 static int action_unref(surf_action_t action)
522 {
523   action->refcount--;
524   if (!action->refcount) {
525     xbt_swag_remove(action, action->state_set);
526
527 #ifdef HAVE_TRACING
528     xbt_free(action->category);
529 #endif
530     XBT_DEBUG ("Removing action %p", action);
531     surf_action_free(&action);
532     return 1;
533   }
534   return 0;
535 }