Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add functions for ns3
[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 void parse_ns3_add_host(void)
23 {
24         XBT_DEBUG("NS3_ADD_HOST '%s'",A_surfxml_host_id);
25         xbt_lib_set(host_lib,
26                                 A_surfxml_host_id,
27                                 NS3_HOST_LEVEL,
28                                 ns3_add_host(A_surfxml_host_id)
29                                 );
30 }
31 void parse_ns3_add_link(void)
32 {
33         XBT_DEBUG("NS3_ADD_LINK '%s'",A_surfxml_link_id);
34
35         tmgr_trace_t bw_trace;
36         tmgr_trace_t state_trace;
37         tmgr_trace_t lat_trace;
38
39         bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
40         lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
41         state_trace = tmgr_trace_new(A_surfxml_link_state_file);
42
43         if (bw_trace)
44                 XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
45         if (lat_trace)
46                 XBT_INFO("The NS3 network model doesn't support latency state traces");
47         if (state_trace)
48                 XBT_INFO("The NS3 network model doesn't support link state traces");
49
50         ns3_link_t link_ns3 = xbt_new0(s_ns3_link_t,1);;
51         link_ns3->id = xbt_strdup(A_surfxml_link_id);
52         link_ns3->bdw = xbt_strdup(A_surfxml_link_bandwidth);
53         link_ns3->lat = xbt_strdup(A_surfxml_link_latency);
54
55         surf_ns3_link_t link = xbt_new0(s_surf_ns3_link_t,1);
56         link->generic_resource.name = xbt_strdup(A_surfxml_link_id);
57         link->generic_resource.properties = current_property_set;
58         link->data = link_ns3;
59
60         xbt_lib_set(link_lib,A_surfxml_link_id,NS3_LINK_LEVEL,link_ns3);
61         xbt_lib_set(link_lib,A_surfxml_link_id,SURF_LINK_LEVEL,link);
62 }
63 void parse_ns3_add_router(void)
64 {
65         XBT_DEBUG("NS3_ADD_ROUTER '%s'",A_surfxml_router_id);
66         xbt_lib_set(as_router_lib,
67                                 A_surfxml_router_id,
68                                 NS3_ASR_LEVEL,
69                                 ns3_add_router(A_surfxml_router_id)
70                                 );
71 }
72 void parse_ns3_add_AS(void)
73 {
74         XBT_DEBUG("NS3_ADD_AS '%s'",A_surfxml_AS_id);
75         xbt_lib_set(as_router_lib,
76                                 A_surfxml_AS_id,
77                                 NS3_ASR_LEVEL,
78                                 ns3_add_AS(A_surfxml_AS_id)
79                                 );
80 }
81 void parse_ns3_add_cluster(void)
82 {
83         XBT_DEBUG("NS3_ADD_CLUSTER '%s'",A_surfxml_cluster_id);
84         routing_parse_Scluster();
85 }
86
87 double ns3_get_link_latency (const void *link)
88 {
89         double lat;
90         XBT_INFO("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);
91         sscanf(((surf_ns3_link_t)link)->data->lat,"%lg",&lat);
92         return lat;
93 }
94 double ns3_get_link_bandwidth (const void *link)
95 {
96         double bdw;
97         XBT_INFO("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);
98         sscanf(((surf_ns3_link_t)link)->data->bdw,"%lg",&bdw);
99         return bdw;
100 }
101
102 static xbt_dynar_t ns3_get_route(const char *src, const char *dst)
103 {
104   return global_routing->get_route(src, dst);
105 }
106
107 void parse_ns3_end_platform(void)
108 {
109           xbt_lib_cursor_t cursor = NULL;
110           char *name = NULL;
111           void **data = NULL;
112           XBT_INFO("link_lib");
113           xbt_lib_foreach(link_lib, cursor, name, data) {
114                         XBT_INFO("\tSee link '%s'\t--> NS3_LEVEL %p",
115                                         name,
116                                         data[NS3_LINK_LEVEL]);
117           }
118           XBT_INFO(" ");
119           XBT_INFO("host_lib");
120           xbt_lib_foreach(host_lib, cursor, name, data) {
121                         XBT_INFO("\tSee host '%s'\t--> NS3_LEVEL %p",
122                                         name,
123                                         data[NS3_HOST_LEVEL]);
124           }
125           XBT_INFO(" ");
126           XBT_INFO("as_router_lib");
127           xbt_lib_foreach(as_router_lib, cursor, name, data) {
128                         XBT_INFO("\tSee ASR '%s'\t--> NS3_LEVEL %p",
129                                         name,
130                                         data[NS3_ASR_LEVEL]);
131           }
132 }
133
134 static void replace_str(char *str, const char *orig, const char *rep)
135 {
136   char buffer[30];
137   char *p;
138
139   if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
140     return;
141
142   strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
143   buffer[p-str] = '\0';
144
145   sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
146   xbt_free(str);
147   str = xbt_strdup(buffer);
148 }
149
150 static void replace_bdw_ns3(char * bdw)
151 {
152         char *temp = xbt_strdup(bdw);
153
154         replace_str(bdw,"000000000","Gbps");
155         if(strcmp(bdw,temp)) {xbt_free(temp);return;}
156         replace_str(bdw,"000000","Mbps");
157         if(strcmp(bdw,temp)) {xbt_free(temp);return;}
158         replace_str(bdw,"000","Kbps");
159         if(strcmp(bdw,temp)) {xbt_free(temp);return;}
160
161         xbt_free(bdw);
162         bdw = bprintf("%s%s",temp,"bps");
163         xbt_free(temp);
164 }
165
166 static void replace_lat_ns3(char * lat)
167 {
168         char *temp = xbt_strdup(lat);
169
170         replace_str(lat,"E-1","00ms");
171         if(strcmp(lat,temp)) {xbt_free(temp);return;}
172         replace_str(lat,"E-2","0ms");
173         if(strcmp(lat,temp)) {xbt_free(temp);return;}
174         replace_str(lat,"E-3","ms");
175         if(strcmp(lat,temp)) {xbt_free(temp);return;}
176         replace_str(lat,"E-4","00us");
177         if(strcmp(lat,temp)) {xbt_free(temp);return;}
178         replace_str(lat,"E-5","0us");
179         if(strcmp(lat,temp)) {xbt_free(temp);return;}
180         replace_str(lat,"E-6","us");
181         if(strcmp(lat,temp)) {xbt_free(temp);return;}
182         replace_str(lat,"E-7","00ns");
183         if(strcmp(lat,temp)) {xbt_free(temp);return;}
184         replace_str(lat,"E-8","0ns");
185         if(strcmp(lat,temp)) {xbt_free(temp);return;}
186         replace_str(lat,"E-9","ns");
187         if(strcmp(lat,temp)) {xbt_free(temp);return;}
188
189         xbt_free(lat);
190         lat = bprintf("%s%s",temp,"s");
191         xbt_free(temp);
192 }
193
194 /* Create the ns3 topology based on routing strategy */
195 void create_ns3_topology()
196 {
197    int src_id,dst_id;
198
199    XBT_DEBUG("Starting topology generation");
200
201    //get the onelinks from the parsed platform
202    xbt_dynar_t onelink_routes = global_routing->get_onelink_routes();
203    if (!onelink_routes)
204      xbt_die("There is no routes!");
205
206    //save them in trace file
207    onelink_t onelink;
208    unsigned int iter;
209    xbt_dynar_foreach(onelink_routes, iter, onelink) {
210      char *src = onelink->src;
211      char *dst = onelink->dst;
212      void *link = onelink->link_ptr;
213      src_id = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,src));
214      dst_id = *((int *) xbt_dict_get_or_null(global_routing->root->to_index,dst));
215
216    if(src_id != dst_id){
217          char * link_bdw = xbt_strdup(((surf_ns3_link_t)link)->data->bdw);
218          char * link_lat = xbt_strdup(((surf_ns3_link_t)link)->data->lat);
219          //     replace_bdw_ns3(link_bdw);
220          //     replace_lat_ns3(link_lat);
221          XBT_INFO("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
222      XBT_INFO("\tLink (%s) bdw:%s->%s lat:%s->%s",((surf_ns3_link_t)link)->data->id,
223                  ((surf_ns3_link_t)link)->data->bdw,link_bdw,
224                  ((surf_ns3_link_t)link)->data->lat,link_lat
225                  );
226      xbt_free(link_bdw);
227      xbt_free(link_lat);
228      }
229    }
230 }
231
232 static void define_callbacks_ns3(const char *filename)
233 {
234   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_ns3_add_host); //HOST
235   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_ns3_add_router);     //ROUTER
236   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_ns3_add_link); //LINK
237   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_ns3_add_AS);             //AS
238   //surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_ns3_add_cluster); //CLUSTER
239
240   surfxml_add_callback(ETag_surfxml_platform_cb_list, &parse_ns3_end_platform); //DEBUG
241   surfxml_add_callback(ETag_surfxml_platform_cb_list, &create_ns3_topology);
242 }
243
244 static void free_ns3_elmts(void * elmts)
245 {
246 }
247
248 static void free_ns3_link(void * elmts)
249 {
250         ns3_link_t link = elmts;
251         free(link->id);
252         free(link->bdw);
253         free(link->lat);
254         free(link);
255 }
256
257 static void free_ns3_host(void * elmts)
258 {
259         ns3_nodes_t host = elmts;
260         free(host);
261 }
262
263 void surf_network_model_init_NS3(const char *filename)
264 {
265         define_callbacks_ns3(filename);
266         surf_network_model = surf_model_init();
267         surf_network_model->name = "network NS3";
268         surf_network_model->extension.network.get_link_latency = ns3_get_link_latency;
269         surf_network_model->extension.network.get_link_bandwidth = ns3_get_link_bandwidth;
270         surf_network_model->extension.network.get_route = ns3_get_route;
271         routing_model_create(sizeof(s_surf_ns3_link_t), NULL, NULL);
272
273         NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
274         NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
275         NS3_LINK_LEVEL = xbt_lib_add_level(link_lib,(void_f_pvoid_t)free_ns3_link);
276
277         update_model_description(surf_network_model_description,
278                     "NS3", surf_network_model);
279 }