Logo AND Algorithmique Numérique Distribuée

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