Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[ns3] remove unused function
[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(const char *src_name,
30                                  const char *dst_name, 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 void parse_ns3_add_host(void)
56 {
57         XBT_DEBUG("NS3_ADD_HOST '%s'",A_surfxml_host_id);
58         xbt_lib_set(host_lib,
59                                 A_surfxml_host_id,
60                                 NS3_HOST_LEVEL,
61                                 ns3_add_host(A_surfxml_host_id)
62                                 );
63 }
64
65 static void ns3_free_dynar(void * elmts){
66         free(elmts);
67         return;
68 }
69
70 void parse_ns3_add_link(void)
71 {
72         XBT_DEBUG("NS3_ADD_LINK '%s'",A_surfxml_link_id);
73
74         if(!IPV4addr) IPV4addr = xbt_dynar_new(sizeof(char*),ns3_free_dynar);
75
76         tmgr_trace_t bw_trace;
77         tmgr_trace_t state_trace;
78         tmgr_trace_t lat_trace;
79
80         bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
81         lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
82         state_trace = tmgr_trace_new(A_surfxml_link_state_file);
83
84         if (bw_trace)
85                 XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
86         if (lat_trace)
87                 XBT_INFO("The NS3 network model doesn't support latency state traces");
88         if (state_trace)
89                 XBT_INFO("The NS3 network model doesn't support link state traces");
90
91         ns3_link_t link_ns3 = xbt_new0(s_ns3_link_t,1);;
92         link_ns3->id = xbt_strdup(A_surfxml_link_id);
93         link_ns3->bdw = xbt_strdup(A_surfxml_link_bandwidth);
94         link_ns3->lat = xbt_strdup(A_surfxml_link_latency);
95
96         surf_ns3_link_t link = xbt_new0(s_surf_ns3_link_t,1);
97         link->generic_resource.name = xbt_strdup(A_surfxml_link_id);
98         link->generic_resource.properties = current_property_set;
99         link->data = link_ns3;
100         link->created = 1;
101
102         xbt_lib_set(link_lib,A_surfxml_link_id,NS3_LINK_LEVEL,link_ns3);
103         xbt_lib_set(link_lib,A_surfxml_link_id,SURF_LINK_LEVEL,link);
104 }
105 void parse_ns3_add_router(void)
106 {
107         XBT_DEBUG("NS3_ADD_ROUTER '%s'",A_surfxml_router_id);
108         xbt_lib_set(as_router_lib,
109                                 A_surfxml_router_id,
110                                 NS3_ASR_LEVEL,
111                                 ns3_add_router(A_surfxml_router_id)
112                                 );
113 }
114 void parse_ns3_add_AS(void)
115 {
116         XBT_DEBUG("NS3_ADD_AS '%s'",A_surfxml_AS_id);
117         xbt_lib_set(as_router_lib,
118                                 A_surfxml_AS_id,
119                                 NS3_ASR_LEVEL,
120                                 ns3_add_AS(A_surfxml_AS_id)
121                                 );
122 }
123 void parse_ns3_add_cluster(void)
124 {
125         char *cluster_prefix = A_surfxml_cluster_prefix;
126         char *cluster_suffix = A_surfxml_cluster_suffix;
127         char *cluster_radical = A_surfxml_cluster_radical;
128         char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
129         char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
130         char *cluster_bw = A_surfxml_cluster_bw;
131         char *cluster_lat = A_surfxml_cluster_lat;
132         char *groups = NULL;
133
134         int start, end, i;
135         unsigned int iter;
136
137         xbt_dynar_t radical_elements;
138         xbt_dynar_t radical_ends;
139         xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
140
141         char *router_id,*host_id;
142
143         radical_elements = xbt_str_split(cluster_radical, ",");
144         xbt_dynar_foreach(radical_elements, iter, groups) {
145                 radical_ends = xbt_str_split(groups, "-");
146
147                 switch (xbt_dynar_length(radical_ends)) {
148                 case 1:
149                   surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
150                   xbt_dynar_push_as(tab_elements_num, int, start);
151                   router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
152                   xbt_lib_set(host_lib,
153                                                 router_id,
154                                                 NS3_HOST_LEVEL,
155                                                 ns3_add_host_cluster(router_id)
156                                                 );
157                   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
158                   free(router_id);
159                   break;
160
161                 case 2:
162                   surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
163                   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
164                   for (i = start; i <= end; i++){
165                         xbt_dynar_push_as(tab_elements_num, int, i);
166                         router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
167                         xbt_lib_set(host_lib,
168                                                 router_id,
169                                                 NS3_HOST_LEVEL,
170                                                 ns3_add_host_cluster(router_id)
171                                                 );
172                         XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
173                         free(router_id);
174                   }
175                   break;
176
177                 default:
178                   XBT_DEBUG("Malformed radical");
179                 }
180         }
181
182         //Create links
183         unsigned int cpt;
184         int elmts;
185         char * lat = xbt_strdup(cluster_lat);
186         char * bw =  xbt_strdup(cluster_bw);
187         replace_lat_ns3(&lat);
188         replace_bdw_ns3(&bw);
189
190         xbt_dynar_foreach(tab_elements_num,cpt,elmts)
191         {
192                 host_id   = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
193                 router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
194                 XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
195
196                 ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,host_id,  NS3_HOST_LEVEL);
197                 ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL);
198
199                 if(host_src && host_dst){}
200                 else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
201
202                 ns3_add_link(host_src->node_num,host_src->type,
203                                          host_dst->node_num,host_dst->type,
204                                          bw,lat);
205
206                 free(router_id);
207                 free(host_id);
208         }
209         xbt_dynar_free(&tab_elements_num);
210
211
212         //Create link backbone
213         lat = xbt_strdup(cluster_bb_lat);
214         bw =  xbt_strdup(cluster_bb_bw);
215         replace_lat_ns3(&lat);
216         replace_bdw_ns3(&bw);
217         ns3_add_cluster(bw,lat,A_surfxml_cluster_id);
218         xbt_free(lat);
219         xbt_free(bw);   
220 }
221
222 double ns3_get_link_latency (const void *link)
223 {
224         double lat;
225         //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);
226         sscanf(((surf_ns3_link_t)link)->data->lat,"%lg",&lat);
227         return lat;
228 }
229 double ns3_get_link_bandwidth (const void *link)
230 {
231         double bdw;
232         //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);
233         sscanf(((surf_ns3_link_t)link)->data->bdw,"%lg",&bdw);
234         return bdw;
235 }
236
237 static xbt_dynar_t ns3_get_route(const char *src, const char *dst)
238 {
239   return global_routing->get_route(src, dst);
240 }
241
242 void parse_ns3_end_platform(void)
243 {
244         ns3_end_platform();
245 }
246
247 /* Create the ns3 topology based on routing strategy */
248 void create_ns3_topology()
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 = onelink->src;
264      char *dst = onelink->dst;
265      void *link = onelink->link_ptr;
266
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   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_ns3_add_host);       //HOST
301   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_ns3_add_router);       //ROUTER
302   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_ns3_add_link);       //LINK
303   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_ns3_add_AS);                   //AS
304   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_ns3_add_cluster); //CLUSTER
305
306   surfxml_add_callback(ETag_surfxml_platform_cb_list, &create_ns3_topology);    //get_one_link_routes
307   surfxml_add_callback(ETag_surfxml_platform_cb_list, &parse_ns3_end_platform); //InitializeRoutes
308 }
309
310 static void free_ns3_link(void * elmts)
311 {
312         ns3_link_t link = elmts;
313         free(link->id);
314         free(link->bdw);
315         free(link->lat);
316         free(link);
317 }
318
319 static void free_ns3_host(void * elmts)
320 {
321         ns3_nodes_t host = elmts;
322         free(host);
323 }
324
325 #ifdef HAVE_LATENCY_BOUND_TRACKING
326 static int ns3_get_link_latency_limited(surf_action_t action)
327 {
328   return 0;
329 }
330 #endif
331
332 #ifdef HAVE_TRACING
333 static void ns3_action_set_category(surf_action_t action, const char *category)
334 {
335   action->category = xbt_strdup (category);
336 }
337 #endif
338
339 void surf_network_model_init_NS3()
340 {
341         if (surf_network_model)
342                 return;
343
344         surf_network_model = surf_model_init();
345         surf_network_model->name = "network NS3";
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(_surf_cfg_set,"ns3/TcpModel"))) {
367         xbt_die("Impossible to initialize NS3 interface");
368         }
369
370         routing_model_create(sizeof(s_surf_ns3_link_t), NULL, 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         update_model_description(surf_network_model_description,
379                     "NS3", surf_network_model);
380
381 #ifdef HAVE_LATENCY_BOUND_TRACKING
382         surf_network_model->get_latency_limited = ns3_get_link_latency_limited;
383 #endif
384 }
385
386 static void finalize(void)
387 {
388         ns3_finalize();
389         xbt_dynar_free_container(&IPV4addr);
390         xbt_dict_free(&dict_socket);
391 }
392
393 static double ns3_share_resources(double min)
394 {
395         XBT_DEBUG("ns3_share_resources");
396
397         xbt_swag_t running_actions =
398           surf_network_model->states.running_action_set;
399
400         //get the first relevant value from the running_actions list
401         if (!xbt_swag_size(running_actions) || min == 0.0)
402           return -1.0;
403         else
404          do {
405            ns3_simulator(min);
406            time_to_next_flow_completion = ns3_time() - surf_get_clock();
407          } while(double_equals(time_to_next_flow_completion,0));
408
409         XBT_DEBUG("min       : %f",min);
410         XBT_DEBUG("ns3  time : %f",ns3_time());
411         XBT_DEBUG("surf time : %f",surf_get_clock());
412         XBT_DEBUG("Next completion %f :",time_to_next_flow_completion);
413
414         return time_to_next_flow_completion;
415 }
416
417 static void ns3_update_actions_state(double now, double delta)
418 {
419           xbt_dict_cursor_t cursor = NULL;
420           char *key;
421           void *data;
422
423           static xbt_dynar_t socket_to_destroy = NULL;
424     if(!socket_to_destroy) socket_to_destroy = xbt_dynar_new(sizeof(char*),NULL);
425
426           surf_action_network_ns3_t action = NULL;
427           xbt_swag_t running_actions =
428               surf_network_model->states.running_action_set;
429
430           /* If there are no running flows, just return */
431           if (!xbt_swag_size(running_actions)) {
432             while(double_positive(now-ns3_time())) {
433               ns3_simulator(now-ns3_time());
434             }
435             return;
436           }
437
438           xbt_dict_foreach(dict_socket,cursor,key,data){
439             action = (surf_action_network_ns3_t)ns3_get_socket_action(data);
440             XBT_DEBUG("Processing socket %p (action %p)",data,action);
441             action->generic_action.remains = action->generic_action.cost - ns3_get_socket_sent(data);
442
443 #ifdef HAVE_TRACING
444             if (TRACE_is_enabled() &&
445                 surf_action_state_get(&(action->generic_action)) == SURF_ACTION_RUNNING){
446               double data_sent = ns3_get_socket_sent(data);
447               double data_delta_sent = data_sent - action->last_sent;
448
449               xbt_dynar_t route = global_routing->get_route(action->src_name, action->dst_name);
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                     action->generic_action.data,
455                     (surf_action_t) action,
456                     (data_delta_sent)/delta,
457                     now-delta,
458                     delta);
459               }
460               action->last_sent = data_sent;
461             }
462 #endif
463
464             if(ns3_get_socket_is_finished(data) == 1){
465               xbt_dynar_push(socket_to_destroy,&key);
466               XBT_DEBUG("Destroy socket %p of action %p", key, action);
467               action->generic_action.finish = now;
468               surf_action_state_set(&(action->generic_action), SURF_ACTION_DONE);
469             }
470           }
471
472           while (!xbt_dynar_is_empty(socket_to_destroy)){
473             xbt_dynar_pop(socket_to_destroy,&key);
474
475             void *data = xbt_dict_get (dict_socket, key);
476             surf_action_network_ns3_t action = (surf_action_network_ns3_t)ns3_get_socket_action(data);
477             XBT_DEBUG ("Removing socket %p of action %p", key, action);
478             xbt_dict_remove(dict_socket,key);
479           }
480           return;
481 }
482
483 /* Max durations are not supported */
484 static surf_action_t ns3_communicate(const char *src_name,
485                                  const char *dst_name, double size, double rate)
486 {
487   surf_action_network_ns3_t action = NULL;
488
489   XBT_DEBUG("Communicate from %s to %s",src_name,dst_name);
490   action = surf_action_new(sizeof(s_surf_action_network_ns3_t), size, surf_network_model, 0);
491
492   ns3_create_flow(src_name, dst_name, surf_get_clock(), size, action);
493
494 #ifdef HAVE_TRACING
495   action->last_sent = 0;
496   action->src_name = xbt_strdup (src_name);
497   action->dst_name = xbt_strdup (dst_name);
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(((surf_action_network_ns3_t)action)->src_name);
529     xbt_free(((surf_action_network_ns3_t)action)->dst_name);
530     xbt_free(action->category);
531 #endif
532     XBT_DEBUG ("Removing action %p", action);
533     surf_action_free(&action);
534     return 1;
535   }
536   return 0;
537 }