Logo AND Algorithmique Numérique Distribuée

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