Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of 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
22 static void replace_str(char *str, const char *orig, const char *rep)
23 {
24   char buffer[30];
25   char *p;
26
27   if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
28     return;
29
30   strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
31   buffer[p-str] = '\0';
32
33   sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
34   xbt_free(str);
35   str = xbt_strdup(buffer);
36 }
37
38 static void replace_bdw_ns3(char * bdw)
39 {
40         char *temp = xbt_strdup(bdw);
41         xbt_free(bdw);
42         bdw = bprintf("%fbps",atof(temp));
43         xbt_free(temp);
44
45 }
46
47 static void replace_lat_ns3(char * lat)
48 {
49         char *temp = xbt_strdup(lat);
50         xbt_free(lat);
51         lat = bprintf("%fs",atof(temp));
52         xbt_free(temp);
53 }
54
55 void parse_ns3_add_host(void)
56 {
57         XBT_DEBUG("NS3_ADD_HOST '%s'",A_surfxml_host_id);
58         xbt_lib_set(host_lib,
59                                 A_surfxml_host_id,
60                                 NS3_HOST_LEVEL,
61                                 ns3_add_host(A_surfxml_host_id)
62                                 );
63 }
64 void parse_ns3_add_link(void)
65 {
66         XBT_DEBUG("NS3_ADD_LINK '%s'",A_surfxml_link_id);
67
68         tmgr_trace_t bw_trace;
69         tmgr_trace_t state_trace;
70         tmgr_trace_t lat_trace;
71
72         bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
73         lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
74         state_trace = tmgr_trace_new(A_surfxml_link_state_file);
75
76         if (bw_trace)
77                 XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
78         if (lat_trace)
79                 XBT_INFO("The NS3 network model doesn't support latency state traces");
80         if (state_trace)
81                 XBT_INFO("The NS3 network model doesn't support link state traces");
82
83         ns3_link_t link_ns3 = xbt_new0(s_ns3_link_t,1);;
84         link_ns3->id = xbt_strdup(A_surfxml_link_id);
85         link_ns3->bdw = xbt_strdup(A_surfxml_link_bandwidth);
86         link_ns3->lat = xbt_strdup(A_surfxml_link_latency);
87
88         surf_ns3_link_t link = xbt_new0(s_surf_ns3_link_t,1);
89         link->generic_resource.name = xbt_strdup(A_surfxml_link_id);
90         link->generic_resource.properties = current_property_set;
91         link->data = link_ns3;
92         link->created = 1;
93
94         xbt_lib_set(link_lib,A_surfxml_link_id,NS3_LINK_LEVEL,link_ns3);
95         xbt_lib_set(link_lib,A_surfxml_link_id,SURF_LINK_LEVEL,link);
96 }
97 void parse_ns3_add_router(void)
98 {
99         XBT_DEBUG("NS3_ADD_ROUTER '%s'",A_surfxml_router_id);
100         xbt_lib_set(as_router_lib,
101                                 A_surfxml_router_id,
102                                 NS3_ASR_LEVEL,
103                                 ns3_add_router(A_surfxml_router_id)
104                                 );
105 }
106 void parse_ns3_add_AS(void)
107 {
108         XBT_DEBUG("NS3_ADD_AS '%s'",A_surfxml_AS_id);
109         xbt_lib_set(as_router_lib,
110                                 A_surfxml_AS_id,
111                                 NS3_ASR_LEVEL,
112                                 ns3_add_AS(A_surfxml_AS_id)
113                                 );
114 }
115 void parse_ns3_add_cluster(void)
116 {
117         char *cluster_prefix = A_surfxml_cluster_prefix;
118         char *cluster_suffix = A_surfxml_cluster_suffix;
119         char *cluster_radical = A_surfxml_cluster_radical;
120         char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
121         char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
122         char *cluster_bw = A_surfxml_cluster_bw;
123         char *cluster_lat = A_surfxml_cluster_lat;
124         char *groups = NULL;
125
126         int start, end, i;
127         unsigned int iter;
128
129         xbt_dynar_t radical_elements;
130         xbt_dynar_t radical_ends;
131         xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
132
133         char *router_id,*host_id;
134
135         radical_elements = xbt_str_split(cluster_radical, ",");
136         xbt_dynar_foreach(radical_elements, iter, groups) {
137                 radical_ends = xbt_str_split(groups, "-");
138
139                 switch (xbt_dynar_length(radical_ends)) {
140                 case 1:
141                   surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
142                   xbt_dynar_push_as(tab_elements_num, int, start);
143                   router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
144                   xbt_lib_set(host_lib,
145                                                 router_id,
146                                                 NS3_HOST_LEVEL,
147                                                 ns3_add_host_cluster(router_id)
148                                                 );
149                   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
150                   free(router_id);
151                   break;
152
153                 case 2:
154                   surf_parse_get_int(&start,xbt_dynar_get_as(radical_ends, 0, char *));
155                   surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
156                   for (i = start; i <= end; i++){
157                         xbt_dynar_push_as(tab_elements_num, int, i);
158                         router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
159                         xbt_lib_set(host_lib,
160                                                 router_id,
161                                                 NS3_HOST_LEVEL,
162                                                 ns3_add_host_cluster(router_id)
163                                                 );
164                         XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
165                         free(router_id);
166                   }
167                   break;
168
169                 default:
170                   XBT_DEBUG("Malformed radical");
171                 }
172         }
173
174
175
176         //Create links
177         unsigned int cpt;
178         int elmts;
179         char * lat = xbt_strdup(cluster_lat);
180         char * bw =  xbt_strdup(cluster_bw);
181         replace_lat_ns3(lat);
182         replace_bdw_ns3(bw);
183
184         xbt_dynar_foreach(tab_elements_num,cpt,elmts)
185         {
186                 host_id   = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
187                 router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
188                 XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
189
190                 ns3_nodes_t host_src = xbt_lib_get_or_null(host_lib,host_id,  NS3_HOST_LEVEL);
191                 ns3_nodes_t host_dst = xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL);
192
193                 if(host_src && host_dst){}
194                 else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
195
196                 ns3_add_link(host_src->node_num,host_dst->node_num,bw,lat);
197
198                 free(router_id);
199                 free(host_id);
200         }
201         xbt_dynar_free(&tab_elements_num);
202
203
204         //Create link backbone
205         lat = xbt_strdup(cluster_bb_lat);
206         bw =  xbt_strdup(cluster_bb_bw);
207         replace_lat_ns3(lat);
208         replace_bdw_ns3(bw);
209         ns3_add_cluster(bw,lat,A_surfxml_cluster_id);
210         xbt_free(lat);
211         xbt_free(bw);   
212 }
213
214 double ns3_get_link_latency (const void *link)
215 {
216         double lat;
217         //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);
218         sscanf(((surf_ns3_link_t)link)->data->lat,"%lg",&lat);
219         return lat;
220 }
221 double ns3_get_link_bandwidth (const void *link)
222 {
223         double bdw;
224         //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);
225         sscanf(((surf_ns3_link_t)link)->data->bdw,"%lg",&bdw);
226         return bdw;
227 }
228
229 static xbt_dynar_t ns3_get_route(const char *src, const char *dst)
230 {
231   return global_routing->get_route(src, dst);
232 }
233
234 void parse_ns3_end_platform(void)
235 {
236         ns3_end_platform();
237
238           xbt_lib_cursor_t cursor = NULL;
239           char *name = NULL;
240           void **data = NULL;
241           XBT_DEBUG("link_lib");
242           xbt_lib_foreach(link_lib, cursor, name, data) {
243                         XBT_DEBUG("\tSee link '%s'\t--> NS3_LEVEL %p",
244                                         name,
245                                         data[NS3_LINK_LEVEL]);
246           }
247           XBT_DEBUG(" ");
248           XBT_DEBUG("host_lib");
249           xbt_lib_foreach(host_lib, cursor, name, data) {
250                         XBT_DEBUG("\tSee host '%s'\t--> NS3_LEVEL %p",
251                                         name,
252                                         data[NS3_HOST_LEVEL]);
253           }
254           XBT_DEBUG(" ");
255           XBT_DEBUG("as_router_lib");
256           xbt_lib_foreach(as_router_lib, cursor, name, data) {
257                         XBT_DEBUG("\tSee ASR '%s'\t--> NS3_LEVEL %p",
258                                         name,
259                                         data[NS3_ASR_LEVEL]);
260           }
261
262           XBT_DEBUG(" ");
263 }
264
265 /* Create the ns3 topology based on routing strategy */
266 void create_ns3_topology()
267 {
268    XBT_INFO("Starting topology generation");
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_INFO("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_INFO("Route from '%s' to '%s' with link '%s'",src,dst,((surf_ns3_link_t)link)->data->id);
285      char * link_bdw = xbt_strdup(((surf_ns3_link_t)link)->data->bdw);
286          char * link_lat = xbt_strdup(((surf_ns3_link_t)link)->data->lat);
287          ((surf_ns3_link_t)link)->created = 0;
288
289          replace_bdw_ns3(link_bdw);
290          replace_lat_ns3(link_lat);
291 //       XBT_INFO("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
292      XBT_INFO("\tLink (%s) bdw:%s->%s lat:%s->%s",((surf_ns3_link_t)link)->data->id,
293                  ((surf_ns3_link_t)link)->data->bdw,link_bdw,
294                  ((surf_ns3_link_t)link)->data->lat,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 void surf_network_model_init_NS3(const char *filename)
346 {
347         surf_network_model = surf_model_init();
348         surf_network_model->name = "network NS3";
349         surf_network_model->extension.network.get_link_latency = ns3_get_link_latency;
350         surf_network_model->extension.network.get_link_bandwidth = ns3_get_link_bandwidth;
351         surf_network_model->extension.network.get_route = ns3_get_route;
352         routing_model_create(sizeof(s_surf_ns3_link_t), NULL, NULL);
353         define_callbacks_ns3(filename);
354
355         NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
356         NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
357         NS3_LINK_LEVEL = xbt_lib_add_level(link_lib,(void_f_pvoid_t)free_ns3_link);
358
359         update_model_description(surf_network_model_description,
360                     "NS3", surf_network_model);
361 }