Logo AND Algorithmique Numérique Distribuée

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