Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e8382479d39019a34295c4df8d4ddadcb89b433c
[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/ns3/ns3_interface.h"
9 #include "xbt/lib.h"
10 #include "surf/network_ns3_private.h"
11 #include "xbt/str.h"
12
13 extern xbt_lib_t host_lib;
14 extern xbt_lib_t link_lib;
15 extern xbt_lib_t as_router_lib;
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_ns3, surf,
18                                 "Logging specific to the SURF network NS3 module");
19
20 #define MAX_LENGHT_IPV4 16 //255.255.255.255\0
21
22 extern routing_global_t global_routing;
23 extern xbt_dict_t dict_socket;
24
25 static double time_to_next_flow_completion = -1;
26
27 static double ns3_share_resources(double min);
28 static void ns3_update_actions_state(double now, double delta);
29 static void finalize(void);
30 static surf_action_t communicate(const char *src_name,
31                                  const char *dst_name, 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_str(char *str, const char *orig, const char *rep)
40 {
41   char buffer[30];
42   char *p;
43
44   if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
45     return;
46
47   strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
48   buffer[p-str] = '\0';
49
50   sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
51   xbt_free(str);
52   str = xbt_strdup(buffer);
53 }
54
55 static void replace_bdw_ns3(char * bdw)
56 {
57         char *temp = xbt_strdup(bdw);
58         xbt_free(bdw);
59         bdw = bprintf("%fBps",atof(temp));
60         xbt_free(temp);
61
62 }
63
64 static void replace_lat_ns3(char * lat)
65 {
66         char *temp = xbt_strdup(lat);
67         xbt_free(lat);
68         lat = bprintf("%fs",atof(temp));
69         xbt_free(temp);
70 }
71
72 void parse_ns3_add_host(void)
73 {
74         XBT_DEBUG("NS3_ADD_HOST '%s'",A_surfxml_host_id);
75         xbt_lib_set(host_lib,
76                                 A_surfxml_host_id,
77                                 NS3_HOST_LEVEL,
78                                 ns3_add_host(A_surfxml_host_id)
79                                 );
80 }
81
82 static void ns3_free_dynar(void * elmts){
83         if(elmts)
84                 free(elmts);
85         return;
86 }
87
88 void parse_ns3_add_link(void)
89 {
90         XBT_DEBUG("NS3_ADD_LINK '%s'",A_surfxml_link_id);
91
92         if(!IPV4addr) IPV4addr = xbt_dynar_new(MAX_LENGHT_IPV4*sizeof(char),ns3_free_dynar);
93
94         tmgr_trace_t bw_trace;
95         tmgr_trace_t state_trace;
96         tmgr_trace_t lat_trace;
97
98         bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
99         lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
100         state_trace = tmgr_trace_new(A_surfxml_link_state_file);
101
102         if (bw_trace)
103                 XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
104         if (lat_trace)
105                 XBT_INFO("The NS3 network model doesn't support latency state traces");
106         if (state_trace)
107                 XBT_INFO("The NS3 network model doesn't support link state traces");
108
109         ns3_link_t link_ns3 = xbt_new0(s_ns3_link_t,1);;
110         link_ns3->id = xbt_strdup(A_surfxml_link_id);
111         link_ns3->bdw = xbt_strdup(A_surfxml_link_bandwidth);
112         link_ns3->lat = xbt_strdup(A_surfxml_link_latency);
113
114         surf_ns3_link_t link = xbt_new0(s_surf_ns3_link_t,1);
115         link->generic_resource.name = xbt_strdup(A_surfxml_link_id);
116         link->generic_resource.properties = current_property_set;
117         link->data = link_ns3;
118         link->created = 1;
119
120         xbt_lib_set(link_lib,A_surfxml_link_id,NS3_LINK_LEVEL,link_ns3);
121         xbt_lib_set(link_lib,A_surfxml_link_id,SURF_LINK_LEVEL,link);
122 }
123 void parse_ns3_add_router(void)
124 {
125         XBT_DEBUG("NS3_ADD_ROUTER '%s'",A_surfxml_router_id);
126         xbt_lib_set(as_router_lib,
127                                 A_surfxml_router_id,
128                                 NS3_ASR_LEVEL,
129                                 ns3_add_router(A_surfxml_router_id)
130                                 );
131 }
132 void parse_ns3_add_AS(void)
133 {
134         XBT_DEBUG("NS3_ADD_AS '%s'",A_surfxml_AS_id);
135         xbt_lib_set(as_router_lib,
136                                 A_surfxml_AS_id,
137                                 NS3_ASR_LEVEL,
138                                 ns3_add_AS(A_surfxml_AS_id)
139                                 );
140 }
141 void parse_ns3_add_cluster(void)
142 {
143         char *cluster_prefix = A_surfxml_cluster_prefix;
144         char *cluster_suffix = A_surfxml_cluster_suffix;
145         char *cluster_radical = A_surfxml_cluster_radical;
146         char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
147         char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
148         char *cluster_bw = A_surfxml_cluster_bw;
149         char *cluster_lat = A_surfxml_cluster_lat;
150         char *groups = NULL;
151
152         int start, end, i;
153         unsigned int iter;
154
155         xbt_dynar_t radical_elements;
156         xbt_dynar_t radical_ends;
157         xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
158
159         char *router_id,*host_id;
160
161         radical_elements = xbt_str_split(cluster_radical, ",");
162         xbt_dynar_foreach(radical_elements, iter, groups) {
163                 radical_ends = xbt_str_split(groups, "-");
164
165                 switch (xbt_dynar_length(radical_ends)) {
166                 case 1:
167                   surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
168                   xbt_dynar_push_as(tab_elements_num, int, start);
169                   router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
170                   xbt_lib_set(host_lib,
171                                                 router_id,
172                                                 NS3_HOST_LEVEL,
173                                                 ns3_add_host_cluster(router_id)
174                                                 );
175                   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
176                   free(router_id);
177                   break;
178
179                 case 2:
180                   surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
181                   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
182                   for (i = start; i <= end; i++){
183                         xbt_dynar_push_as(tab_elements_num, int, i);
184                         router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
185                         xbt_lib_set(host_lib,
186                                                 router_id,
187                                                 NS3_HOST_LEVEL,
188                                                 ns3_add_host_cluster(router_id)
189                                                 );
190                         XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
191                         free(router_id);
192                   }
193                   break;
194
195                 default:
196                   XBT_DEBUG("Malformed radical");
197                 }
198         }
199
200         //Create links
201         unsigned int cpt;
202         int elmts;
203         char * lat = xbt_strdup(cluster_lat);
204         char * bw =  xbt_strdup(cluster_bw);
205         replace_lat_ns3(lat);
206         replace_bdw_ns3(bw);
207
208         xbt_dynar_foreach(tab_elements_num,cpt,elmts)
209         {
210                 host_id   = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
211                 router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
212                 XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
213
214                 ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,host_id,  NS3_HOST_LEVEL);
215                 ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL);
216
217                 if(host_src && host_dst){}
218                 else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
219
220                 ns3_add_link(host_src->node_num,host_dst->node_num,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 = bprintf("%s",((surf_ns3_link_t)link)->data->bdw);
286          char * link_lat = bprintf("%s",(((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_dst->node_num,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(const char *filename)
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_elmts(void * elmts)
327 {
328 }
329
330 static void free_ns3_link(void * elmts)
331 {
332         ns3_link_t link = elmts;
333         free(link->id);
334         free(link->bdw);
335         free(link->lat);
336         free(link);
337 }
338
339 static void free_ns3_host(void * elmts)
340 {
341         ns3_nodes_t host = elmts;
342         free(host);
343 }
344
345 #ifdef HAVE_LATENCY_BOUND_TRACKING
346 static int ns3_get_link_latency_limited(surf_action_t action)
347 {
348   return 0;
349 }
350 #endif
351
352 void surf_network_model_init_NS3(const char *filename)
353 {
354         if (surf_network_model)
355                 return;
356
357         surf_network_model = surf_model_init();
358         surf_network_model->name = "network NS3";
359         surf_network_model->extension.network.get_link_latency = ns3_get_link_latency;
360         surf_network_model->extension.network.get_link_bandwidth = ns3_get_link_bandwidth;
361         surf_network_model->extension.network.get_route = ns3_get_route;
362
363         surf_network_model->model_private->share_resources = ns3_share_resources;
364         surf_network_model->model_private->update_actions_state = ns3_update_actions_state;
365         surf_network_model->model_private->finalize = finalize;
366
367         surf_network_model->suspend = action_suspend;
368         surf_network_model->resume = action_resume;
369         surf_network_model->is_suspended = action_is_suspended;
370         surf_network_model->action_unref = action_unref;
371         surf_network_model->extension.network.communicate = communicate;
372
373         /* Added the initialization for NS3 interface */
374         if (ns3_initialize()) {
375         xbt_die("Impossible to initialize NS3 interface");
376         }
377
378         routing_model_create(sizeof(s_surf_ns3_link_t), NULL, NULL);
379         define_callbacks_ns3(filename);
380
381         NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
382         NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
383         NS3_LINK_LEVEL = xbt_lib_add_level(link_lib,(void_f_pvoid_t)free_ns3_link);
384
385         xbt_dynar_push(model_list, &surf_network_model);
386         update_model_description(surf_network_model_description,
387                     "NS3", surf_network_model);
388
389 #ifdef HAVE_LATENCY_BOUND_TRACKING
390         surf_network_model->get_latency_limited = ns3_get_link_latency_limited;
391 #endif
392 }
393
394 static void finalize(void)
395 {
396         ns3_finalize();
397         xbt_dynar_free_container(&IPV4addr);
398 }
399
400 static double ns3_share_resources(double min)
401 {
402         XBT_DEBUG("ns3_share_resources");
403
404         xbt_swag_t running_actions =
405           surf_network_model->states.running_action_set;
406
407         //get the first relevant value from the running_actions list
408         if (!xbt_swag_size(running_actions))
409         return -1.0;
410
411         ns3_simulator(min);
412         time_to_next_flow_completion = ns3_time() - surf_get_clock();
413
414 //      XBT_INFO("min       : %f",min);
415 //      XBT_INFO("ns3  time : %f",ns3_time());
416 //      XBT_INFO("surf time : %f",surf_get_clock());
417
418         xbt_assert(time_to_next_flow_completion,
419                           "Time to next flow completion not initialized!\n");
420
421         XBT_DEBUG("ns3_share_resources return %f",time_to_next_flow_completion);
422         return time_to_next_flow_completion;
423 }
424
425 static void ns3_update_actions_state(double now, double delta)
426 {
427           xbt_dict_cursor_t cursor = NULL;
428           char *key;
429           void *data;
430
431           surf_action_t action = NULL;
432           xbt_swag_t running_actions =
433               surf_network_model->states.running_action_set;
434
435           /* If there are no running flows, just return */
436           if (!xbt_swag_size(running_actions))
437                 return;
438
439           xbt_dict_foreach(dict_socket,cursor,key,data){
440             action = (surf_action_t)ns3_get_socket_action(data);
441             action->remains = action->cost - ns3_get_socket_sent(data);
442
443             if(ns3_get_socket_is_finished(data) == 1){
444               action->finish = now;
445               surf_action_state_set(action, SURF_ACTION_DONE);
446             }
447           }
448           return;
449 }
450
451 /* Max durations are not supported */
452 static surf_action_t communicate(const char *src_name,
453                                  const char *dst_name, double size, double rate)
454 {
455   surf_action_t action = NULL;
456
457   XBT_DEBUG("Communicate from %s to %s",src_name,dst_name);
458   action = surf_action_new(sizeof(s_surf_action_t), size, surf_network_model, 0);
459
460   ns3_create_flow(src_name, dst_name, surf_get_clock(), size, action);
461
462   return (surf_action_t) action;
463 }
464
465 /* Suspend a flow() */
466 static void action_suspend(surf_action_t action)
467 {
468   THROW_UNIMPLEMENTED;
469 }
470
471 /* Resume a flow() */
472 static void action_resume(surf_action_t action)
473 {
474   THROW_UNIMPLEMENTED;
475 }
476
477 /* Test whether a flow is suspended */
478 static int action_is_suspended(surf_action_t action)
479 {
480   return 0;
481 }
482
483 static int action_unref(surf_action_t action)
484 {
485   action->refcount--;
486   if (!action->refcount) {
487     xbt_swag_remove(action, action->state_set);
488     surf_action_free(&action);
489     return 1;
490   }
491   return 0;
492 }