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/maxmin.h"
9 #include "surf/ns3/ns3_interface.h"
11 #include "surf/network_ns3_private.h"
14 extern xbt_lib_t host_lib;
15 extern xbt_lib_t link_lib;
16 extern xbt_lib_t as_router_lib;
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_ns3, surf,
19 "Logging specific to the SURF network NS3 module");
21 #define MAX_LENGHT_IPV4 16 //255.255.255.255\0
23 extern routing_global_t global_routing;
24 extern xbt_dict_t dict_socket;
26 static double time_to_next_flow_completion = -1;
28 static double ns3_share_resources(double min);
29 static void ns3_update_actions_state(double now, double delta);
30 static void finalize(void);
31 static surf_action_t ns3_communicate(const char *src_name,
32 const char *dst_name, double size, double rate);
33 static void action_suspend(surf_action_t action);
34 static void action_resume(surf_action_t action);
35 static int action_is_suspended(surf_action_t action);
36 static int action_unref(surf_action_t action);
40 static void replace_str(char *str, const char *orig, const char *rep)
45 if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
48 strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
51 sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
53 str = xbt_strdup(buffer);
56 static void replace_bdw_ns3(char * bdw)
58 char *temp = xbt_strdup(bdw);
60 bdw = bprintf("%fBps",atof(temp));
65 static void replace_lat_ns3(char * lat)
67 char *temp = xbt_strdup(lat);
69 lat = bprintf("%fs",atof(temp));
73 void parse_ns3_add_host(void)
75 XBT_DEBUG("NS3_ADD_HOST '%s'",A_surfxml_host_id);
79 ns3_add_host(A_surfxml_host_id)
83 static void ns3_free_dynar(void * elmts){
89 void parse_ns3_add_link(void)
91 XBT_DEBUG("NS3_ADD_LINK '%s'",A_surfxml_link_id);
93 if(!IPV4addr) IPV4addr = xbt_dynar_new(MAX_LENGHT_IPV4*sizeof(char),ns3_free_dynar);
95 tmgr_trace_t bw_trace;
96 tmgr_trace_t state_trace;
97 tmgr_trace_t lat_trace;
99 bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
100 lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
101 state_trace = tmgr_trace_new(A_surfxml_link_state_file);
104 XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
106 XBT_INFO("The NS3 network model doesn't support latency state traces");
108 XBT_INFO("The NS3 network model doesn't support link state traces");
110 ns3_link_t link_ns3 = xbt_new0(s_ns3_link_t,1);;
111 link_ns3->id = xbt_strdup(A_surfxml_link_id);
112 link_ns3->bdw = xbt_strdup(A_surfxml_link_bandwidth);
113 link_ns3->lat = xbt_strdup(A_surfxml_link_latency);
115 surf_ns3_link_t link = xbt_new0(s_surf_ns3_link_t,1);
116 link->generic_resource.name = xbt_strdup(A_surfxml_link_id);
117 link->generic_resource.properties = current_property_set;
118 link->data = link_ns3;
121 xbt_lib_set(link_lib,A_surfxml_link_id,NS3_LINK_LEVEL,link_ns3);
122 xbt_lib_set(link_lib,A_surfxml_link_id,SURF_LINK_LEVEL,link);
124 void parse_ns3_add_router(void)
126 XBT_DEBUG("NS3_ADD_ROUTER '%s'",A_surfxml_router_id);
127 xbt_lib_set(as_router_lib,
130 ns3_add_router(A_surfxml_router_id)
133 void parse_ns3_add_AS(void)
135 XBT_DEBUG("NS3_ADD_AS '%s'",A_surfxml_AS_id);
136 xbt_lib_set(as_router_lib,
139 ns3_add_AS(A_surfxml_AS_id)
142 void parse_ns3_add_cluster(void)
144 char *cluster_prefix = A_surfxml_cluster_prefix;
145 char *cluster_suffix = A_surfxml_cluster_suffix;
146 char *cluster_radical = A_surfxml_cluster_radical;
147 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
148 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
149 char *cluster_bw = A_surfxml_cluster_bw;
150 char *cluster_lat = A_surfxml_cluster_lat;
156 xbt_dynar_t radical_elements;
157 xbt_dynar_t radical_ends;
158 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
160 char *router_id,*host_id;
162 radical_elements = xbt_str_split(cluster_radical, ",");
163 xbt_dynar_foreach(radical_elements, iter, groups) {
164 radical_ends = xbt_str_split(groups, "-");
166 switch (xbt_dynar_length(radical_ends)) {
168 surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
169 xbt_dynar_push_as(tab_elements_num, int, start);
170 router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
171 xbt_lib_set(host_lib,
174 ns3_add_host_cluster(router_id)
176 XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
181 surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
182 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
183 for (i = start; i <= end; i++){
184 xbt_dynar_push_as(tab_elements_num, int, i);
185 router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
186 xbt_lib_set(host_lib,
189 ns3_add_host_cluster(router_id)
191 XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
197 XBT_DEBUG("Malformed radical");
204 char * lat = xbt_strdup(cluster_lat);
205 char * bw = xbt_strdup(cluster_bw);
206 replace_lat_ns3(lat);
209 xbt_dynar_foreach(tab_elements_num,cpt,elmts)
211 host_id = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
212 router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
213 XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
215 ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,host_id, NS3_HOST_LEVEL);
216 ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL);
218 if(host_src && host_dst){}
219 else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
221 ns3_add_link(host_src->node_num,host_dst->node_num,bw,lat);
226 xbt_dynar_free(&tab_elements_num);
229 //Create link backbone
230 lat = xbt_strdup(cluster_bb_lat);
231 bw = xbt_strdup(cluster_bb_bw);
232 replace_lat_ns3(lat);
234 ns3_add_cluster(bw,lat,A_surfxml_cluster_id);
239 double ns3_get_link_latency (const void *link)
242 //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);
243 sscanf(((surf_ns3_link_t)link)->data->lat,"%lg",&lat);
246 double ns3_get_link_bandwidth (const void *link)
249 //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);
250 sscanf(((surf_ns3_link_t)link)->data->bdw,"%lg",&bdw);
254 static xbt_dynar_t ns3_get_route(const char *src, const char *dst)
256 return global_routing->get_route(src, dst);
259 void parse_ns3_end_platform(void)
264 /* Create the ns3 topology based on routing strategy */
265 void create_ns3_topology()
267 XBT_DEBUG("Starting topology generation");
269 xbt_dynar_shrink(IPV4addr,0);
271 //get the onelinks from the parsed platform
272 xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
274 xbt_die("There is no routes!");
275 XBT_DEBUG("Have get_onelink_routes, found %ld routes",onelink_routes->used);
276 //save them in trace file
279 xbt_dynar_foreach(onelink_routes, iter, onelink) {
280 char *src = onelink->src;
281 char *dst = onelink->dst;
282 void *link = onelink->link_ptr;
284 if( strcmp(src,dst) && ((surf_ns3_link_t)link)->created){
285 XBT_DEBUG("Route from '%s' to '%s' with link '%s'",src,dst,((surf_ns3_link_t)link)->data->id);
286 char * link_bdw = bprintf("%s",((surf_ns3_link_t)link)->data->bdw);
287 char * link_lat = bprintf("%s",(((surf_ns3_link_t)link)->data->lat));
288 replace_lat_ns3(link_lat);
289 replace_bdw_ns3(link_bdw);
290 ((surf_ns3_link_t)link)->created = 0;
292 // XBT_DEBUG("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
293 XBT_DEBUG("\tLink (%s) bdw:%s lat:%s",((surf_ns3_link_t)link)->data->id,
299 ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,src,NS3_HOST_LEVEL);
300 if(!host_src) host_src = xbt_lib_get_or_null(as_router_lib,src,NS3_ASR_LEVEL);
301 ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,dst,NS3_HOST_LEVEL);
302 if(!host_dst) host_dst = xbt_lib_get_or_null(as_router_lib,dst,NS3_ASR_LEVEL);
304 if(host_src && host_dst){}
305 else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
307 ns3_add_link(host_src->node_num,host_dst->node_num,link_bdw,link_lat);
315 static void define_callbacks_ns3(const char *filename)
317 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_ns3_add_host); //HOST
318 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_ns3_add_router); //ROUTER
319 surfxml_add_callback(STag_surfxml_link_cb_list, &parse_ns3_add_link); //LINK
320 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_ns3_add_AS); //AS
321 surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_ns3_add_cluster); //CLUSTER
323 surfxml_add_callback(ETag_surfxml_platform_cb_list, &create_ns3_topology); //get_one_link_routes
324 surfxml_add_callback(ETag_surfxml_platform_cb_list, &parse_ns3_end_platform); //InitializeRoutes
327 static void free_ns3_elmts(void * elmts)
331 static void free_ns3_link(void * elmts)
333 ns3_link_t link = elmts;
340 static void free_ns3_host(void * elmts)
342 ns3_nodes_t host = elmts;
346 #ifdef HAVE_LATENCY_BOUND_TRACKING
347 static int ns3_get_link_latency_limited(surf_action_t action)
354 static void ns3_action_set_category(surf_action_t action, const char *category)
356 action->category = xbt_strdup (category);
360 void surf_network_model_init_NS3(const char *filename)
362 if (surf_network_model)
365 surf_network_model = surf_model_init();
366 surf_network_model->name = "network NS3";
367 surf_network_model->extension.network.get_link_latency = ns3_get_link_latency;
368 surf_network_model->extension.network.get_link_bandwidth = ns3_get_link_bandwidth;
369 surf_network_model->extension.network.get_route = ns3_get_route;
371 surf_network_model->model_private->share_resources = ns3_share_resources;
372 surf_network_model->model_private->update_actions_state = ns3_update_actions_state;
373 surf_network_model->model_private->finalize = finalize;
375 surf_network_model->suspend = action_suspend;
376 surf_network_model->resume = action_resume;
377 surf_network_model->is_suspended = action_is_suspended;
378 surf_network_model->action_unref = action_unref;
379 surf_network_model->extension.network.communicate = ns3_communicate;
382 surf_network_model->set_category = ns3_action_set_category;
385 /* Added the initialization for NS3 interface */
387 if (ns3_initialize(xbt_cfg_get_string(_surf_cfg_set,"ns3/TcpModel"))) {
388 xbt_die("Impossible to initialize NS3 interface");
391 routing_model_create(sizeof(s_surf_ns3_link_t), NULL, NULL);
392 define_callbacks_ns3(filename);
394 NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
395 NS3_ASR_LEVEL = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
396 NS3_LINK_LEVEL = xbt_lib_add_level(link_lib,(void_f_pvoid_t)free_ns3_link);
398 xbt_dynar_push(model_list, &surf_network_model);
399 update_model_description(surf_network_model_description,
400 "NS3", surf_network_model);
402 #ifdef HAVE_LATENCY_BOUND_TRACKING
403 surf_network_model->get_latency_limited = ns3_get_link_latency_limited;
407 static void finalize(void)
410 xbt_dynar_free_container(&IPV4addr);
413 static double ns3_share_resources(double min)
415 XBT_DEBUG("ns3_share_resources");
417 xbt_swag_t running_actions =
418 surf_network_model->states.running_action_set;
420 //get the first relevant value from the running_actions list
421 if (!xbt_swag_size(running_actions) || min == 0.0)
426 time_to_next_flow_completion = ns3_time() - surf_get_clock();
427 } while(double_equals(time_to_next_flow_completion,0));
429 XBT_DEBUG("min : %f",min);
430 XBT_DEBUG("ns3 time : %f",ns3_time());
431 XBT_DEBUG("surf time : %f",surf_get_clock());
432 XBT_DEBUG("Next completion %f :",time_to_next_flow_completion);
434 return time_to_next_flow_completion;
437 static void ns3_update_actions_state(double now, double delta)
439 xbt_dict_cursor_t cursor = NULL;
443 surf_action_network_ns3_t action = NULL;
444 xbt_swag_t running_actions =
445 surf_network_model->states.running_action_set;
447 /* If there are no running flows, just return */
448 if (!xbt_swag_size(running_actions)) {
449 while(double_positive(now-ns3_time())) {
450 ns3_simulator(now-ns3_time());
455 xbt_dict_foreach(dict_socket,cursor,key,data){
456 action = (surf_action_network_ns3_t)ns3_get_socket_action(data);
457 action->generic_action.remains = action->generic_action.cost - ns3_get_socket_sent(data);
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;
465 xbt_dynar_t route = global_routing->get_route(action->src_name, action->dst_name);
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,
476 action->last_sent = data_sent;
480 if(ns3_get_socket_is_finished(data) == 1){
481 action->generic_action.finish = now;
482 surf_action_state_set(&(action->generic_action), SURF_ACTION_DONE);
488 /* Max durations are not supported */
489 static surf_action_t ns3_communicate(const char *src_name,
490 const char *dst_name, double size, double rate)
492 surf_action_network_ns3_t action = NULL;
494 XBT_DEBUG("Communicate from %s to %s",src_name,dst_name);
495 action = surf_action_new(sizeof(s_surf_action_network_ns3_t), size, surf_network_model, 0);
497 ns3_create_flow(src_name, dst_name, surf_get_clock(), size, action);
500 action->last_sent = 0;
501 action->src_name = xbt_strdup (src_name);
502 action->dst_name = xbt_strdup (dst_name);
505 return (surf_action_t) action;
508 /* Suspend a flow() */
509 static void action_suspend(surf_action_t action)
514 /* Resume a flow() */
515 static void action_resume(surf_action_t action)
520 /* Test whether a flow is suspended */
521 static int action_is_suspended(surf_action_t action)
526 static int action_unref(surf_action_t action)
529 if (!action->refcount) {
530 xbt_swag_remove(action, action->state_set);
533 xbt_free(((surf_action_network_ns3_t)action)->src_name);
534 xbt_free(((surf_action_network_ns3_t)action)->dst_name);
535 if (action->category)
536 xbt_free(action->category);
539 surf_action_free(&action);