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