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 extern routing_global_t global_routing;
22 extern xbt_dict_t dict_socket;
24 static double time_to_next_flow_completion = -1;
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);
38 static void replace_bdw_ns3(char ** bdw)
40 char *temp = xbt_strdup(*bdw);
42 *bdw = bprintf("%fBps",atof(temp));
47 static void replace_lat_ns3(char ** lat)
49 char *temp = xbt_strdup(*lat);
51 *lat = bprintf("%fs",atof(temp));
55 static void parse_ns3_add_host(sg_platf_host_cbarg_t host)
57 XBT_DEBUG("NS3_ADD_HOST '%s'",host->id);
61 ns3_add_host(host->id)
65 static void ns3_free_dynar(void * elmts)
71 static void parse_ns3_add_link(sg_platf_link_cbarg_t link)
73 XBT_DEBUG("NS3_ADD_LINK '%s'",link->id);
75 if(!IPV4addr) IPV4addr = xbt_dynar_new(sizeof(char*),ns3_free_dynar);
77 tmgr_trace_t bw_trace;
78 tmgr_trace_t state_trace;
79 tmgr_trace_t lat_trace;
81 bw_trace = link->bandwidth_trace;
82 lat_trace = link->latency_trace;
83 state_trace = link->state_trace;
86 XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
88 XBT_INFO("The NS3 network model doesn't support latency state traces");
90 XBT_INFO("The NS3 network model doesn't support link state traces");
92 ns3_link_t link_ns3 = xbt_new0(s_ns3_link_t,1);;
93 link_ns3->id = (char*)(link->id);
94 link_ns3->bdw = bprintf("%f",link->bandwidth);
95 link_ns3->lat = bprintf("%f",link->latency);
97 surf_ns3_link_t l = xbt_new0(s_surf_ns3_link_t,1);
98 l->generic_resource.name = xbt_strdup(link->id);
99 l->generic_resource.properties = current_property_set;
103 xbt_lib_set(link_lib,link->id,NS3_LINK_LEVEL,link_ns3);
104 xbt_lib_set(link_lib,link->id,SURF_LINK_LEVEL,l);
107 static void parse_ns3_add_router(sg_platf_router_cbarg_t router)
109 XBT_DEBUG("NS3_ADD_ROUTER '%s'",router->id);
110 xbt_lib_set(as_router_lib,
113 ns3_add_router(router->id)
117 static void parse_ns3_add_AS(const char*id, const char*routing)
119 XBT_DEBUG("NS3_ADD_AS '%s'",id);
120 xbt_lib_set(as_router_lib,
127 static void parse_ns3_add_cluster(sg_platf_cluster_cbarg_t cluster)
129 const char *cluster_prefix = cluster->prefix;
130 const char *cluster_suffix = cluster->suffix;
131 const char *cluster_radical = cluster->radical;
132 const char *cluster_bb_bw = bprintf("%f",cluster->bb_bw);
133 const char *cluster_bb_lat = bprintf("%f",cluster->bb_lat);
134 const char *cluster_bw = bprintf("%f",cluster->bw);
135 const char *cluster_lat = bprintf("%f",cluster->lat);
136 const char *groups = NULL;
141 xbt_dynar_t radical_elements;
142 xbt_dynar_t radical_ends;
143 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
145 char *router_id,*host_id;
147 radical_elements = xbt_str_split(cluster_radical, ",");
148 xbt_dynar_foreach(radical_elements, iter, groups) {
149 radical_ends = xbt_str_split(groups, "-");
151 switch (xbt_dynar_length(radical_ends)) {
153 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
154 xbt_dynar_push_as(tab_elements_num, int, start);
155 router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
156 xbt_lib_set(host_lib,
159 ns3_add_host_cluster(router_id)
161 XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
166 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
167 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
168 for (i = start; i <= end; i++){
169 xbt_dynar_push_as(tab_elements_num, int, i);
170 router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
171 xbt_lib_set(host_lib,
174 ns3_add_host_cluster(router_id)
176 XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
182 XBT_DEBUG("Malformed radical");
189 char * lat = xbt_strdup(cluster_lat);
190 char * bw = xbt_strdup(cluster_bw);
191 replace_lat_ns3(&lat);
192 replace_bdw_ns3(&bw);
194 xbt_dynar_foreach(tab_elements_num,cpt,elmts)
196 host_id = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
197 router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
198 XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
200 ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,host_id, NS3_HOST_LEVEL);
201 ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL);
203 if(host_src && host_dst){}
204 else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
206 ns3_add_link(host_src->node_num,host_src->type,
207 host_dst->node_num,host_dst->type,
213 xbt_dynar_free(&tab_elements_num);
216 //Create link backbone
217 lat = xbt_strdup(cluster_bb_lat);
218 bw = xbt_strdup(cluster_bb_bw);
219 replace_lat_ns3(&lat);
220 replace_bdw_ns3(&bw);
221 ns3_add_cluster(bw,lat,cluster->id);
226 static double ns3_get_link_latency (const void *link)
229 //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);
230 sscanf(((surf_ns3_link_t)link)->data->lat,"%lg",&lat);
233 static double ns3_get_link_bandwidth (const void *link)
236 //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);
237 sscanf(((surf_ns3_link_t)link)->data->bdw,"%lg",&bdw);
241 static xbt_dynar_t ns3_get_route(const char *src, const char *dst)
243 xbt_dynar_t route = NULL;
244 routing_get_route_and_latency(src, dst, &route, NULL);
248 static void parse_ns3_end_platform(void)
253 /* Create the ns3 topology based on routing strategy */
254 static void create_ns3_topology(void)
256 XBT_DEBUG("Starting topology generation");
258 xbt_dynar_shrink(IPV4addr,0);
260 //get the onelinks from the parsed platform
261 xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
263 xbt_die("There is no routes!");
264 XBT_DEBUG("Have get_onelink_routes, found %ld routes",onelink_routes->used);
265 //save them in trace file
268 xbt_dynar_foreach(onelink_routes, iter, onelink) {
269 char *src = onelink->src;
270 char *dst = onelink->dst;
271 void *link = onelink->link_ptr;
273 if( strcmp(src,dst) && ((surf_ns3_link_t)link)->created){
274 XBT_DEBUG("Route from '%s' to '%s' with link '%s'",src,dst,((surf_ns3_link_t)link)->data->id);
275 char * link_bdw = xbt_strdup(((surf_ns3_link_t)link)->data->bdw);
276 char * link_lat = xbt_strdup(((surf_ns3_link_t)link)->data->lat);
277 replace_lat_ns3(&link_lat);
278 replace_bdw_ns3(&link_bdw);
279 ((surf_ns3_link_t)link)->created = 0;
281 // XBT_DEBUG("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
282 XBT_DEBUG("\tLink (%s) bdw:%s lat:%s",((surf_ns3_link_t)link)->data->id,
288 ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,src,NS3_HOST_LEVEL);
289 if(!host_src) host_src = xbt_lib_get_or_null(as_router_lib,src,NS3_ASR_LEVEL);
290 ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,dst,NS3_HOST_LEVEL);
291 if(!host_dst) host_dst = xbt_lib_get_or_null(as_router_lib,dst,NS3_ASR_LEVEL);
293 if(host_src && host_dst){}
294 else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
296 ns3_add_link(host_src->node_num,host_src->type,host_dst->node_num,host_dst->type,link_bdw,link_lat);
304 static void define_callbacks_ns3(void)
306 sg_platf_host_add_cb (&parse_ns3_add_host);
307 sg_platf_router_add_cb (&parse_ns3_add_router);
308 sg_platf_link_add_cb (&parse_ns3_add_link);
309 sg_platf_cluster_add_cb (&parse_ns3_add_cluster);
310 sg_platf_AS_begin_add_cb (&parse_ns3_add_AS);
311 sg_platf_postparse_add_cb(&create_ns3_topology); //get_one_link_routes
312 sg_platf_postparse_add_cb(&parse_ns3_end_platform); //InitializeRoutes
315 static void free_ns3_link(void * elmts)
317 ns3_link_t link = elmts;
324 static void free_ns3_host(void * elmts)
326 ns3_nodes_t host = elmts;
330 #ifdef HAVE_LATENCY_BOUND_TRACKING
331 static int ns3_get_link_latency_limited(surf_action_t action)
338 static void ns3_action_set_category(surf_action_t action, const char *category)
340 action->category = xbt_strdup (category);
344 void surf_network_model_init_NS3()
346 if (surf_network_model)
349 surf_network_model = surf_model_init();
350 surf_network_model->name = "network NS3";
351 surf_network_model->extension.network.get_link_latency = ns3_get_link_latency;
352 surf_network_model->extension.network.get_link_bandwidth = ns3_get_link_bandwidth;
353 surf_network_model->extension.network.get_route = ns3_get_route;
355 surf_network_model->model_private->share_resources = ns3_share_resources;
356 surf_network_model->model_private->update_actions_state = ns3_update_actions_state;
357 surf_network_model->model_private->finalize = finalize;
359 surf_network_model->suspend = action_suspend;
360 surf_network_model->resume = action_resume;
361 surf_network_model->is_suspended = action_is_suspended;
362 surf_network_model->action_unref = action_unref;
363 surf_network_model->extension.network.communicate = ns3_communicate;
366 surf_network_model->set_category = ns3_action_set_category;
369 /* Added the initialization for NS3 interface */
371 if (ns3_initialize(xbt_cfg_get_string(_surf_cfg_set,"ns3/TcpModel"))) {
372 xbt_die("Impossible to initialize NS3 interface");
375 routing_model_create(sizeof(s_surf_ns3_link_t), NULL);
376 define_callbacks_ns3();
378 NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
379 NS3_ASR_LEVEL = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
380 NS3_LINK_LEVEL = xbt_lib_add_level(link_lib,(void_f_pvoid_t)free_ns3_link);
382 xbt_dynar_push(model_list, &surf_network_model);
384 #ifdef HAVE_LATENCY_BOUND_TRACKING
385 surf_network_model->get_latency_limited = ns3_get_link_latency_limited;
389 static void finalize(void)
392 xbt_dynar_free_container(&IPV4addr);
393 xbt_dict_free(&dict_socket);
396 static double ns3_share_resources(double min)
398 XBT_DEBUG("ns3_share_resources");
400 xbt_swag_t running_actions =
401 surf_network_model->states.running_action_set;
403 //get the first relevant value from the running_actions list
404 if (!xbt_swag_size(running_actions) || min == 0.0)
409 time_to_next_flow_completion = ns3_time() - surf_get_clock();
410 } while(double_equals(time_to_next_flow_completion,0));
412 XBT_DEBUG("min : %f",min);
413 XBT_DEBUG("ns3 time : %f",ns3_time());
414 XBT_DEBUG("surf time : %f",surf_get_clock());
415 XBT_DEBUG("Next completion %f :",time_to_next_flow_completion);
417 return time_to_next_flow_completion;
420 static void ns3_update_actions_state(double now, double delta)
422 xbt_dict_cursor_t cursor = NULL;
426 static xbt_dynar_t socket_to_destroy = NULL;
427 if(!socket_to_destroy) socket_to_destroy = xbt_dynar_new(sizeof(char*),NULL);
429 surf_action_network_ns3_t action = NULL;
430 xbt_swag_t running_actions =
431 surf_network_model->states.running_action_set;
433 /* If there are no running flows, just return */
434 if (!xbt_swag_size(running_actions)) {
435 while(double_positive(now-ns3_time())) {
436 ns3_simulator(now-ns3_time());
441 xbt_dict_foreach(dict_socket,cursor,key,data){
442 action = (surf_action_network_ns3_t)ns3_get_socket_action(data);
443 XBT_DEBUG("Processing socket %p (action %p)",data,action);
444 action->generic_action.remains = action->generic_action.cost - ns3_get_socket_sent(data);
447 if (TRACE_is_enabled() &&
448 surf_action_state_get(&(action->generic_action)) == SURF_ACTION_RUNNING){
449 double data_sent = ns3_get_socket_sent(data);
450 double data_delta_sent = data_sent - action->last_sent;
452 xbt_dynar_t route = NULL;
453 routing_get_route_and_latency (action->src_name, action->dst_name, &route, NULL);
455 for (i = 0; i < xbt_dynar_length (route); i++){
456 surf_ns3_link_t *link = ((surf_ns3_link_t*)xbt_dynar_get_ptr (route, i));
457 TRACE_surf_link_set_utilization ((*link)->generic_resource.name,
458 ((surf_action_t) action)->category,
459 (data_delta_sent)/delta,
463 action->last_sent = data_sent;
467 if(ns3_get_socket_is_finished(data) == 1){
468 xbt_dynar_push(socket_to_destroy,&key);
469 XBT_DEBUG("Destroy socket %p of action %p", key, action);
470 action->generic_action.finish = now;
471 surf_action_state_set(&(action->generic_action), SURF_ACTION_DONE);
475 while (!xbt_dynar_is_empty(socket_to_destroy)){
476 xbt_dynar_pop(socket_to_destroy,&key);
478 void *data = xbt_dict_get (dict_socket, key);
479 surf_action_network_ns3_t action = (surf_action_network_ns3_t)ns3_get_socket_action(data);
480 XBT_DEBUG ("Removing socket %p of action %p", key, action);
481 xbt_dict_remove(dict_socket,key);
486 /* Max durations are not supported */
487 static surf_action_t ns3_communicate(const char *src_name,
488 const char *dst_name, double size, double rate)
490 surf_action_network_ns3_t action = NULL;
492 XBT_DEBUG("Communicate from %s to %s",src_name,dst_name);
493 action = surf_action_new(sizeof(s_surf_action_network_ns3_t), size, surf_network_model, 0);
495 ns3_create_flow(src_name, dst_name, surf_get_clock(), size, action);
498 action->last_sent = 0;
499 action->src_name = xbt_strdup (src_name);
500 action->dst_name = xbt_strdup (dst_name);
503 return (surf_action_t) action;
506 /* Suspend a flow() */
507 static void action_suspend(surf_action_t action)
512 /* Resume a flow() */
513 static void action_resume(surf_action_t action)
518 /* Test whether a flow is suspended */
519 static int action_is_suspended(surf_action_t action)
524 static int action_unref(surf_action_t action)
527 if (!action->refcount) {
528 xbt_swag_remove(action, action->state_set);
531 xbt_free(((surf_action_network_ns3_t)action)->src_name);
532 xbt_free(((surf_action_network_ns3_t)action)->dst_name);
533 xbt_free(action->category);
535 XBT_DEBUG ("Removing action %p", action);
536 surf_action_free(&action);