1 /* Copyright (c) 2007, 2008, 2009, 2010, 2011. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "surf_private.h"
8 #include "surf/ns3/ns3_interface.h"
10 #include "surf/network_ns3_private.h"
13 extern xbt_lib_t host_lib;
14 extern xbt_lib_t link_lib;
15 extern xbt_lib_t as_router_lib;
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_ns3, surf,
18 "Logging specific to the SURF network NS3 module");
20 extern routing_global_t global_routing;
21 extern xbt_dict_t dict_socket;
23 static double time_to_next_flow_completion = -1;
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);
37 static void replace_str(char *str, const char *orig, const char *rep)
42 if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
45 strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
48 sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
50 str = xbt_strdup(buffer);
53 static void replace_bdw_ns3(char * bdw)
55 char *temp = xbt_strdup(bdw);
57 bdw = bprintf("%fbps",atof(temp));
62 static void replace_lat_ns3(char * lat)
64 char *temp = xbt_strdup(lat);
66 lat = bprintf("%fs",atof(temp));
70 void parse_ns3_add_host(void)
72 XBT_DEBUG("NS3_ADD_HOST '%s'",A_surfxml_host_id);
76 ns3_add_host(A_surfxml_host_id)
79 void parse_ns3_add_link(void)
81 XBT_DEBUG("NS3_ADD_LINK '%s'",A_surfxml_link_id);
83 if(!IPV4addr) IPV4addr = xbt_dynar_new(sizeof(char*),free);
85 tmgr_trace_t bw_trace;
86 tmgr_trace_t state_trace;
87 tmgr_trace_t lat_trace;
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);
94 XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
96 XBT_INFO("The NS3 network model doesn't support latency state traces");
98 XBT_INFO("The NS3 network model doesn't support link state traces");
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);
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;
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);
114 void parse_ns3_add_router(void)
116 XBT_DEBUG("NS3_ADD_ROUTER '%s'",A_surfxml_router_id);
117 xbt_lib_set(as_router_lib,
120 ns3_add_router(A_surfxml_router_id)
123 void parse_ns3_add_AS(void)
125 XBT_DEBUG("NS3_ADD_AS '%s'",A_surfxml_AS_id);
126 xbt_lib_set(as_router_lib,
129 ns3_add_AS(A_surfxml_AS_id)
132 void parse_ns3_add_cluster(void)
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;
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);
150 char *router_id,*host_id;
152 radical_elements = xbt_str_split(cluster_radical, ",");
153 xbt_dynar_foreach(radical_elements, iter, groups) {
154 radical_ends = xbt_str_split(groups, "-");
156 switch (xbt_dynar_length(radical_ends)) {
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,
164 ns3_add_host_cluster(router_id)
166 XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
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,
179 ns3_add_host_cluster(router_id)
181 XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
187 XBT_DEBUG("Malformed radical");
196 char * lat = xbt_strdup(cluster_lat);
197 char * bw = xbt_strdup(cluster_bw);
198 replace_lat_ns3(lat);
201 xbt_dynar_foreach(tab_elements_num,cpt,elmts)
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);
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);
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);
213 ns3_add_link(host_src->node_num,host_dst->node_num,bw,lat);
218 xbt_dynar_free(&tab_elements_num);
221 //Create link backbone
222 lat = xbt_strdup(cluster_bb_lat);
223 bw = xbt_strdup(cluster_bb_bw);
224 replace_lat_ns3(lat);
226 ns3_add_cluster(bw,lat,A_surfxml_cluster_id);
231 double ns3_get_link_latency (const void *link)
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);
238 double ns3_get_link_bandwidth (const void *link)
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);
246 static xbt_dynar_t ns3_get_route(const char *src, const char *dst)
248 return global_routing->get_route(src, dst);
251 void parse_ns3_end_platform(void)
256 /* Create the ns3 topology based on routing strategy */
257 void create_ns3_topology()
259 XBT_DEBUG("Starting topology generation");
261 //get the onelinks from the parsed platform
262 xbt_dynar_t onelink_routes = global_routing->get_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
269 xbt_dynar_foreach(onelink_routes, iter, onelink) {
270 char *src = onelink->src;
271 char *dst = onelink->dst;
272 void *link = onelink->link_ptr;
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;
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,
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);
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);
295 ns3_add_link(host_src->node_num,host_dst->node_num,link_bdw,link_lat);
303 static void define_callbacks_ns3(const char *filename)
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
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
315 static void free_ns3_elmts(void * elmts)
319 static void free_ns3_link(void * elmts)
321 ns3_link_t link = elmts;
328 static void free_ns3_host(void * elmts)
330 ns3_nodes_t host = elmts;
334 #ifdef HAVE_LATENCY_BOUND_TRACKING
335 static int ns3_get_link_latency_limited(surf_action_t action)
341 void surf_network_model_init_NS3(const char *filename)
343 if (surf_network_model)
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;
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;
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;
362 /* Added the initialization for NS3 interface */
363 if (ns3_initialize()) {
364 xbt_die("Impossible to initialize NS3 interface");
367 routing_model_create(sizeof(s_surf_ns3_link_t), NULL, NULL);
368 define_callbacks_ns3(filename);
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);
374 xbt_dynar_push(model_list, &surf_network_model);
375 update_model_description(surf_network_model_description,
376 "NS3", surf_network_model);
378 #ifdef HAVE_LATENCY_BOUND_TRACKING
379 surf_network_model->get_latency_limited = ns3_get_link_latency_limited;
383 static void finalize(void)
386 xbt_dynar_free_container(&IPV4addr);
389 static double ns3_share_resources(double min)
391 XBT_DEBUG("ns3_share_resources");
393 xbt_swag_t running_actions =
394 surf_network_model->states.running_action_set;
396 //get the first relevant value from the running_actions list
397 if (!xbt_swag_size(running_actions))
401 time_to_next_flow_completion = ns3_time() - surf_get_clock();
403 xbt_assert(time_to_next_flow_completion,
404 "Time to next flow completion not initialized!\n");
406 XBT_DEBUG("ns3_share_resources return %f",time_to_next_flow_completion);
407 return time_to_next_flow_completion;
410 static void ns3_update_actions_state(double now, double delta)
412 xbt_dict_cursor_t cursor = NULL;
416 surf_action_t action = NULL;
417 xbt_swag_t running_actions =
418 surf_network_model->states.running_action_set;
420 /* If there are no running flows, just return */
421 if (!xbt_swag_size(running_actions))
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);
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)
439 surf_action_t action = NULL;
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);
444 ns3_create_flow(src_name, dst_name, surf_get_clock(), size, action);
446 return (surf_action_t) action;
449 /* Suspend a flow() */
450 static void action_suspend(surf_action_t action)
455 /* Resume a flow() */
456 static void action_resume(surf_action_t action)
461 /* Test whether a flow is suspended */
462 static int action_is_suspended(surf_action_t action)
467 static int action_unref(surf_action_t action)
470 if (!action->refcount) {
471 xbt_swag_remove(action, action->state_set);
472 surf_action_free(&action);