Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into hypervisor
[simgrid.git] / src / surf / surfxml_parse.c
1 /* Copyright (c) 2006, 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 <stdarg.h> /* va_arg */
8
9 #include "xbt/misc.h"
10 #include "xbt/log.h"
11 #include "xbt/str.h"
12 #include "xbt/dict.h"
13 #include "surf/surfxml_parse.h"
14 #include "surf/surf_private.h"
15 #include "simgrid/sg_config.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
18                                 "Logging specific to the SURF parsing module");
19 #undef CLEANUP
20 int ETag_surfxml_include_state(void);
21
22 #include "simgrid_dtd.c"
23
24 char* surf_parsed_filename = NULL; // to locate parse error messages
25
26 xbt_dynar_t parsed_link_list = NULL;   /* temporary store of current list link of a route */
27 extern AS_t current_routing;
28 /*
29  * Helping functions
30  */
31 void surf_parse_error(const char *fmt, ...) {
32   va_list va;
33   va_start(va,fmt);
34   char *msg = bvprintf(fmt,va);
35   va_end(va);
36   xbt_die("Parse error at %s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
37 }
38 void surf_parse_warn(const char *fmt, ...) {
39   va_list va;
40   va_start(va,fmt);
41   char *msg = bvprintf(fmt,va);
42   va_end(va);
43     XBT_WARN("%s:%d: %s", surf_parsed_filename, surf_parse_lineno, msg);
44     free(msg);
45 }
46
47 double surf_parse_get_double(const char *string) {
48   double res;
49   int ret = sscanf(string, "%lg", &res);
50   if (ret != 1)
51     surf_parse_error("%s is not a double", string);
52   return res;
53 }
54
55 int surf_parse_get_int(const char *string) {
56   int res;
57   int ret = sscanf(string, "%d", &res);
58   if (ret != 1)
59     surf_parse_error("%s is not an integer", string);
60   return res;
61 }
62
63
64 /*
65  * All the callback lists that can be overridden anywhere.
66  * (this list should probably be reduced to the bare minimum to allow the models to work)
67  */
68
69 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
70
71 /* The default current property receiver. Setup in the corresponding opening callbacks. */
72 xbt_dict_t current_property_set = NULL;
73 xbt_dict_t as_current_property_set = NULL;
74 int AS_TAG = 0;
75 char* as_name_tab[1024];
76 void* as_dict_tab[1024];
77 int as_prop_nb = 0;
78
79
80 /* dictionary of random generator data */
81 xbt_dict_t random_data_list = NULL;
82
83 YY_BUFFER_STATE surf_input_buffer;
84 FILE *surf_file_to_parse = NULL;
85
86 static void init_randomness(void);
87 static void add_randomness(void);
88
89 /*
90  * Stuff relative to storage
91  */
92 void STag_surfxml_storage(void)
93 {
94   AS_TAG = 0;
95   XBT_DEBUG("STag_surfxml_storage");
96   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
97 }
98 void ETag_surfxml_storage(void)
99 {
100   s_sg_platf_storage_cbarg_t storage;
101   memset(&storage,0,sizeof(storage));
102
103   storage.id = A_surfxml_storage_id;
104   storage.type_id = A_surfxml_storage_typeId;
105   storage.content = A_surfxml_storage_content;
106   storage.properties = current_property_set;
107   sg_platf_new_storage(&storage);
108   current_property_set = NULL;
109 }
110 void STag_surfxml_storage___type(void)
111 {
112   AS_TAG = 0;
113   XBT_DEBUG("STag_surfxml_storage___type");
114   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
115 }
116 void ETag_surfxml_storage___type(void)
117 {
118   s_sg_platf_storage_type_cbarg_t storage_type;
119   memset(&storage_type,0,sizeof(storage_type));
120
121   storage_type.content = A_surfxml_storage___type_content;
122   storage_type.id = A_surfxml_storage___type_id;
123   storage_type.model = A_surfxml_storage___type_model;
124   storage_type.properties = current_property_set;
125   storage_type.size = surf_parse_get_int(A_surfxml_storage___type_size);
126   sg_platf_new_storage_type(&storage_type);
127   current_property_set = NULL;
128 }
129 void STag_surfxml_mstorage(void)
130 {
131   XBT_DEBUG("STag_surfxml_mstorage");
132 }
133 void ETag_surfxml_mstorage(void)
134 {
135   s_sg_platf_mstorage_cbarg_t mstorage;
136   memset(&mstorage,0,sizeof(mstorage));
137
138   mstorage.name = A_surfxml_mstorage_name;
139   mstorage.type_id = A_surfxml_mstorage_typeId;
140   sg_platf_new_mstorage(&mstorage);
141 }
142 void STag_surfxml_mount(void)
143 {
144   XBT_DEBUG("STag_surfxml_mount");
145 }
146 void ETag_surfxml_mount(void)
147 {
148   s_sg_platf_mount_cbarg_t mount;
149   memset(&mount,0,sizeof(mount));
150
151   mount.name = A_surfxml_mount_name;
152   mount.id = A_surfxml_mount_id;
153   sg_platf_new_mount(&mount);
154 }
155
156 /*
157  * Stuff relative to the <include> tag
158  */
159 static xbt_dynar_t surf_input_buffer_stack = NULL;
160 static xbt_dynar_t surf_file_to_parse_stack = NULL;
161 static xbt_dynar_t surf_parsed_filename_stack = NULL;
162
163 void STag_surfxml_include(void)
164 {
165   XBT_DEBUG("STag_surfxml_include '%s'",A_surfxml_include_file);
166   xbt_dynar_push(surf_parsed_filename_stack,&surf_parsed_filename); // save old file name
167   surf_parsed_filename = xbt_strdup(A_surfxml_include_file);
168
169   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
170
171   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
172   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
173               A_surfxml_include_file);
174   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
175   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
176   surf_parse_push_buffer_state(surf_input_buffer);
177
178   fflush(NULL);
179 }
180
181 void ETag_surfxml_include(void) {
182 /* Nothing to do when done with reading the include tag.
183  * Instead, the handling should be deferred until the EOF of current buffer -- see below */
184 }
185
186 /** @brief When reaching EOF, check whether we are in an include tag, and behave accordingly if yes
187  *
188  * This function is called automatically by sedding the parser in buildtools/Cmake/MaintainerMode.cmake
189  * Every FAIL on "Premature EOF" is preceded by a call to this function, which role is to restore the
190  * previous buffer if we reached the EOF /of an include file/. Its return code is used to avoid the
191  * error message in that case.
192  *
193  * Yeah, that's terribly hackish, but it works. A better solution should be dealed with in flexml
194  * directly: a command line flag could instruct it to do the correct thing when #include is encountered
195  * on a line. One day maybe, if the maya allow it.
196  */
197 int ETag_surfxml_include_state(void)
198 {
199   fflush(NULL);
200   XBT_DEBUG("ETag_surfxml_include_state '%s'",A_surfxml_include_file);
201
202   if(xbt_dynar_is_empty(surf_input_buffer_stack)) // nope, that's a true premature EOF. Let the parser die verbosely.
203     return 0;
204
205   // Yeah, we were in an <include> Restore state and proceed.
206   fclose(surf_file_to_parse);
207   xbt_dynar_pop(surf_file_to_parse_stack, &surf_file_to_parse);
208   surf_parse_pop_buffer_state();
209   xbt_dynar_pop(surf_input_buffer_stack,&surf_input_buffer);
210
211   // Restore the filename for error messages
212   free(surf_parsed_filename);
213   xbt_dynar_pop(surf_parsed_filename_stack,&surf_parsed_filename);
214
215   return 1;
216 }
217
218
219 void surf_parse_init_callbacks(void)
220 {
221     sg_platf_init();
222 }
223
224 void surf_parse_reset_callbacks(void)
225 {
226   surf_parse_free_callbacks();
227   surf_parse_init_callbacks();
228 }
229
230 void surf_parse_free_callbacks(void)
231 {
232   sg_platf_exit();
233 }
234
235 /* Stag and Etag parse functions */
236
237 void STag_surfxml_platform(void) {
238   _XBT_GNUC_UNUSED double version = surf_parse_get_double(A_surfxml_platform_version);
239
240   xbt_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
241       "You're using an ancient XML file.\n"
242       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
243       "instead of MBytes, MFlops and seconds.\n"
244
245       "Use simgrid_update_xml to update your file automatically. "
246       "This program is installed automatically with SimGrid, or "
247       "available in the tools/ directory of the source archive.\n"
248
249       "Please check also out the SURF section of the ChangeLog for "
250       "the 3.1 version for more information. \n"
251
252       "Last, do not forget to also update your values for "
253       "the calls to MSG_task_create (if any).");
254   xbt_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
255       "You're using an old XML file.\n"
256       "Use simgrid_update_xml to update your file automatically. "
257       "This program is installed automatically with SimGrid, or "
258       "available in the tools/ directory of the source archive.");
259
260   sg_platf_begin();
261 }
262 void ETag_surfxml_platform(void){
263   sg_platf_end();
264 }
265
266 void STag_surfxml_host(void){
267   AS_TAG = 0;
268   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
269 }
270
271 void STag_surfxml_prop(void)
272 {
273   if(AS_TAG){
274     if (!as_current_property_set){
275       xbt_assert(as_prop_nb < 1024, "Number of AS property reach the limit!!!");
276       as_current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
277       as_name_tab[as_prop_nb] = xbt_strdup(A_surfxml_AS_id);
278       as_dict_tab[as_prop_nb] = as_current_property_set;
279       XBT_DEBUG("PUSH prop %p for AS '%s'",as_dict_tab[as_prop_nb],as_name_tab[as_prop_nb]);
280       as_prop_nb++;
281     }
282     xbt_dict_set(as_current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
283   }
284   else{
285     if (!current_property_set)
286       current_property_set = xbt_dict_new_homogeneous(xbt_free_f); // Maybe, it should raise an error
287     xbt_dict_set(current_property_set, A_surfxml_prop_id, xbt_strdup(A_surfxml_prop_value), NULL);
288   }
289 }
290
291 void ETag_surfxml_host(void)    {
292   s_sg_platf_host_cbarg_t host;
293   memset(&host,0,sizeof(host));
294
295   host.properties = current_property_set;
296
297   host.id = A_surfxml_host_id;
298   host.power_peak = get_cpu_power(A_surfxml_host_power);
299   host.power_scale = surf_parse_get_double( A_surfxml_host_availability);
300   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
301   host.power_trace = tmgr_trace_new_from_file(A_surfxml_host_availability___file);
302   host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state___file);
303   xbt_assert((A_surfxml_host_state == A_surfxml_host_state_ON) ||
304         (A_surfxml_host_state == A_surfxml_host_state_OFF), "Invalid state");
305   if (A_surfxml_host_state == A_surfxml_host_state_ON)
306     host.initial_state = SURF_RESOURCE_ON;
307   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
308     host.initial_state = SURF_RESOURCE_OFF;
309   host.coord = A_surfxml_host_coordinates;
310
311   sg_platf_new_host(&host);
312   current_property_set = NULL;
313 }
314
315 void STag_surfxml_host___link(void){
316   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
317   s_sg_platf_host_link_cbarg_t host_link;
318   memset(&host_link,0,sizeof(host_link));
319
320   host_link.id = A_surfxml_host___link_id;
321   host_link.link_up = A_surfxml_host___link_up;
322   host_link.link_down = A_surfxml_host___link_down;
323   sg_platf_new_host_link(&host_link);
324 }
325
326 void STag_surfxml_router(void){
327   s_sg_platf_router_cbarg_t router;
328   memset(&router, 0, sizeof(router));
329
330   router.id = A_surfxml_router_id;
331   router.coord = A_surfxml_router_coordinates;
332
333   sg_platf_new_router(&router);
334 }
335
336 void STag_surfxml_cluster(void){
337   s_sg_platf_cluster_cbarg_t cluster;
338   memset(&cluster,0,sizeof(cluster));
339   cluster.id = A_surfxml_cluster_id;
340   cluster.prefix = A_surfxml_cluster_prefix;
341   cluster.suffix = A_surfxml_cluster_suffix;
342   cluster.radical = A_surfxml_cluster_radical;
343   cluster.power= surf_parse_get_double(A_surfxml_cluster_power);
344   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
345   cluster.bw =   surf_parse_get_double(A_surfxml_cluster_bw);
346   cluster.lat =  surf_parse_get_double(A_surfxml_cluster_lat);
347   if(strcmp(A_surfxml_cluster_bb___bw,""))
348     cluster.bb_bw = surf_parse_get_double(A_surfxml_cluster_bb___bw);
349   if(strcmp(A_surfxml_cluster_bb___lat,""))
350     cluster.bb_lat = surf_parse_get_double(A_surfxml_cluster_bb___lat);
351   if(strcmp(A_surfxml_cluster_limiter___link,""))
352     cluster.limiter_link = surf_parse_get_double(A_surfxml_cluster_limiter___link);
353   if(strcmp(A_surfxml_cluster_loopback___bw,""))
354     cluster.loopback_bw = surf_parse_get_double(A_surfxml_cluster_loopback___bw);
355   if(strcmp(A_surfxml_cluster_loopback___lat,""))
356     cluster.loopback_lat = surf_parse_get_double(A_surfxml_cluster_loopback___lat);
357   cluster.router_id = A_surfxml_cluster_router___id;
358
359   switch (AX_surfxml_cluster_sharing___policy) {
360   case A_surfxml_cluster_sharing___policy_SHARED:
361     cluster.sharing_policy = SURF_LINK_SHARED;
362     break;
363   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
364     cluster.sharing_policy = SURF_LINK_FULLDUPLEX;
365     break;
366   case A_surfxml_cluster_sharing___policy_FATPIPE:
367     cluster.sharing_policy = SURF_LINK_FATPIPE;
368     break;
369   default:
370     surf_parse_error("Invalid cluster sharing policy for cluster %s",
371                      cluster.id);
372     break;
373   }
374   switch (AX_surfxml_cluster_bb___sharing___policy) {
375   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
376     cluster.bb_sharing_policy = SURF_LINK_FATPIPE;
377     break;
378   case A_surfxml_cluster_bb___sharing___policy_SHARED:
379     cluster.bb_sharing_policy = SURF_LINK_SHARED;
380     break;
381   default:
382     surf_parse_error("Invalid bb sharing policy in cluster %s",
383                      cluster.id);
384     break;
385   }
386
387   cluster.availability_trace = A_surfxml_cluster_availability___file;
388   cluster.state_trace = A_surfxml_cluster_state___file;
389   sg_platf_new_cluster(&cluster);
390 }
391
392 void STag_surfxml_cabinet(void){
393   s_sg_platf_cabinet_cbarg_t cabinet;
394   memset(&cabinet,0,sizeof(cabinet));
395   cabinet.id = A_surfxml_cabinet_id;
396   cabinet.prefix = A_surfxml_cabinet_prefix;
397   cabinet.suffix = A_surfxml_cabinet_suffix;
398   cabinet.power = surf_parse_get_double(A_surfxml_cabinet_power);
399   cabinet.bw = surf_parse_get_double(A_surfxml_cabinet_bw);
400   cabinet.lat = surf_parse_get_double(A_surfxml_cabinet_lat);
401   cabinet.radical = A_surfxml_cabinet_radical;
402
403   sg_platf_new_cabinet(&cabinet);
404 }
405
406 void STag_surfxml_peer(void){
407   s_sg_platf_peer_cbarg_t peer;
408   memset(&peer,0,sizeof(peer));
409   peer.id = A_surfxml_peer_id;
410   peer.power = surf_parse_get_double(A_surfxml_peer_power);
411   peer.bw_in = surf_parse_get_double(A_surfxml_peer_bw___in);
412   peer.bw_out = surf_parse_get_double(A_surfxml_peer_bw___out);
413   peer.lat = surf_parse_get_double(A_surfxml_peer_lat);
414   peer.coord = A_surfxml_peer_coordinates;
415   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file);
416   peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state___file);
417
418   sg_platf_new_peer(&peer);
419 }
420
421 void STag_surfxml_link(void){
422   AS_TAG = 0;
423   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
424 }
425
426 void ETag_surfxml_link(void){
427   s_sg_platf_link_cbarg_t link;
428   memset(&link,0,sizeof(link));
429
430   link.properties = current_property_set;
431
432   link.id = A_surfxml_link_id;
433   link.bandwidth = surf_parse_get_double(A_surfxml_link_bandwidth);
434   link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file);
435   link.latency = surf_parse_get_double(A_surfxml_link_latency);
436   link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency___file);
437
438   switch (A_surfxml_link_state) {
439   case A_surfxml_link_state_ON:
440     link.state = SURF_RESOURCE_ON;
441     break;
442   case A_surfxml_link_state_OFF:
443     link.state = SURF_RESOURCE_OFF;
444     break;
445   default:
446     surf_parse_error("invalid state for link %s", link.id);
447     break;
448   }
449   link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state___file);
450
451   switch (A_surfxml_link_sharing___policy) {
452   case A_surfxml_link_sharing___policy_SHARED:
453     link.policy = SURF_LINK_SHARED;
454     break;
455   case A_surfxml_link_sharing___policy_FATPIPE:
456      link.policy = SURF_LINK_FATPIPE;
457      break;
458   case A_surfxml_link_sharing___policy_FULLDUPLEX:
459      link.policy = SURF_LINK_FULLDUPLEX;
460      break;
461   default:
462     surf_parse_error("Invalid sharing policy in link %s", link.id);
463     break;
464   }
465
466   sg_platf_new_link(&link);
467
468   current_property_set = NULL;
469 }
470
471 void STag_surfxml_link___ctn(void){
472
473   char *link_id;
474   switch (A_surfxml_link___ctn_direction) {
475   case AU_surfxml_link___ctn_direction:
476   case A_surfxml_link___ctn_direction_NONE:
477     link_id = xbt_strdup(A_surfxml_link___ctn_id);
478     break;
479   case A_surfxml_link___ctn_direction_UP:
480     link_id = bprintf("%s_UP", A_surfxml_link___ctn_id);
481     break;
482   case A_surfxml_link___ctn_direction_DOWN:
483     link_id = bprintf("%s_DOWN", A_surfxml_link___ctn_id);
484     break;
485   }
486
487   // FIXME we should push the surf link object but it don't
488   // work because of model rulebased
489   xbt_dynar_push(parsed_link_list, &link_id);
490 }
491
492 void ETag_surfxml_backbone(void){
493   s_sg_platf_link_cbarg_t link;
494   memset(&link,0,sizeof(link));
495
496   link.properties = NULL;
497
498   link.id = A_surfxml_backbone_id;
499   link.bandwidth = surf_parse_get_double(A_surfxml_backbone_bandwidth);
500   link.latency = surf_parse_get_double(A_surfxml_backbone_latency);
501   link.state = SURF_RESOURCE_ON;
502   link.policy = SURF_LINK_SHARED;
503
504   sg_platf_new_link(&link);
505   routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, A_surfxml_backbone_id, SURF_LINK_LEVEL));
506 }
507
508 void STag_surfxml_route(void){
509   xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
510       "Missing end-points while defining route \"%s\"->\"%s\"",
511       A_surfxml_route_src, A_surfxml_route_dst);
512   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
513 }
514
515 void STag_surfxml_ASroute(void){
516   xbt_assert(strlen(A_surfxml_ASroute_src) > 0
517              || strlen(A_surfxml_ASroute_dst) > 0
518              || strlen(A_surfxml_ASroute_gw___src) > 0
519              || strlen(A_surfxml_ASroute_gw___dst) > 0,
520              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
521              A_surfxml_ASroute_src, A_surfxml_ASroute_dst,
522              A_surfxml_ASroute_gw___src, A_surfxml_ASroute_gw___dst);
523   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
524 }
525
526 void STag_surfxml_bypassRoute(void){
527   xbt_assert(strlen(A_surfxml_bypassRoute_src) > 0
528              || strlen(A_surfxml_bypassRoute_dst) > 0,
529              "Missing end-points while defining bupass route \"%s\"->\"%s\"",
530              A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
531   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
532 }
533
534 void STag_surfxml_bypassASroute(void){
535   xbt_assert(strlen(A_surfxml_bypassASroute_src) > 0
536              || strlen(A_surfxml_bypassASroute_dst) > 0
537              || strlen(A_surfxml_bypassASroute_gw___src) > 0
538              || strlen(A_surfxml_bypassASroute_gw___dst) > 0,
539              "Missing end-points while defining route \"%s\"->\"%s\" (with %s and %s as gateways)",
540              A_surfxml_bypassASroute_src, A_surfxml_bypassASroute_dst,
541              A_surfxml_bypassASroute_gw___src,A_surfxml_bypassASroute_gw___dst);
542   parsed_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
543 }
544
545 void ETag_surfxml_route(void){
546   s_sg_platf_route_cbarg_t route;
547   memset(&route,0,sizeof(route));
548
549   route.src = A_surfxml_route_src;
550   route.dst = A_surfxml_route_dst;
551   route.gw_src = NULL;
552   route.gw_dst = NULL;
553   route.link_list = parsed_link_list;
554
555   switch (A_surfxml_route_symmetrical) {
556   case AU_surfxml_route_symmetrical:
557   case A_surfxml_route_symmetrical_YES:
558     route.symmetrical = TRUE;
559     break;
560   case A_surfxml_route_symmetrical_NO:
561     route.symmetrical = FALSE;;
562     break;
563   }
564
565   sg_platf_new_route(&route);
566   parsed_link_list = NULL;
567 }
568
569 void ETag_surfxml_ASroute(void){
570   s_sg_platf_route_cbarg_t ASroute;
571   memset(&ASroute,0,sizeof(ASroute));
572
573   ASroute.src = A_surfxml_ASroute_src;
574   ASroute.dst = A_surfxml_ASroute_dst;
575
576   if (!strcmp(current_routing->model_desc->name,"RuleBased")) {
577     // DIRTY PERL HACK AHEAD: with the rulebased routing, the {src,dst}_gateway fields
578     // store the provided name instead of the entity directly (model_rulebased_parse_ASroute knows)
579     //
580     // This is because the user will provide something like "^AS_(.*)$" instead of the proper name of a given entity
581     ASroute.gw_src = (sg_routing_edge_t) A_surfxml_ASroute_gw___src;
582     ASroute.gw_dst = (sg_routing_edge_t) A_surfxml_ASroute_gw___dst;
583   } else {
584     ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___src);
585     ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_ASroute_gw___dst);
586   }
587
588   ASroute.link_list = parsed_link_list;
589
590   switch (A_surfxml_ASroute_symmetrical) {
591   case AU_surfxml_ASroute_symmetrical:
592   case A_surfxml_ASroute_symmetrical_YES:
593     ASroute.symmetrical = TRUE;
594     break;
595   case A_surfxml_ASroute_symmetrical_NO:
596     ASroute.symmetrical = FALSE;
597     break;
598   }
599
600   sg_platf_new_ASroute(&ASroute);
601   parsed_link_list = NULL;
602 }
603
604 void ETag_surfxml_bypassRoute(void){
605   s_sg_platf_route_cbarg_t route;
606   memset(&route,0,sizeof(route));
607
608   route.src = A_surfxml_bypassRoute_src;
609   route.dst = A_surfxml_bypassRoute_dst;
610   route.gw_src = NULL;
611   route.gw_dst = NULL;
612   route.link_list = parsed_link_list;
613   route.symmetrical = FALSE;
614
615   sg_platf_new_bypassRoute(&route);
616   parsed_link_list = NULL;
617 }
618
619 void ETag_surfxml_bypassASroute(void){
620   s_sg_platf_route_cbarg_t ASroute;
621   memset(&ASroute,0,sizeof(ASroute));
622
623   ASroute.src = A_surfxml_bypassASroute_src;
624   ASroute.dst = A_surfxml_bypassASroute_dst;
625   ASroute.link_list = parsed_link_list;
626   ASroute.symmetrical = FALSE;
627
628   if (!strcmp(current_routing->model_desc->name,"RuleBased")) {
629     // DIRTY PERL HACK AHEAD: with the rulebased routing, the {src,dst}_gateway fields
630     // store the provided name instead of the entity directly (model_rulebased_parse_ASroute knows)
631     //
632     // This is because the user will provide something like "^AS_(.*)$" instead of the proper name of a given entity
633     ASroute.gw_src = (sg_routing_edge_t) A_surfxml_bypassASroute_gw___src;
634     ASroute.gw_dst = (sg_routing_edge_t) A_surfxml_bypassASroute_gw___dst;
635   } else {
636     ASroute.gw_src = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___src);
637     ASroute.gw_dst = sg_routing_edge_by_name_or_null(A_surfxml_bypassASroute_gw___dst);
638   }
639
640   sg_platf_new_bypassASroute(&ASroute);
641   parsed_link_list = NULL;
642 }
643
644 void ETag_surfxml_trace(void){
645   s_sg_platf_trace_cbarg_t trace;
646   memset(&trace,0,sizeof(trace));
647
648   trace.id = A_surfxml_trace_id;
649   trace.file = A_surfxml_trace_file;
650   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
651   trace.pc_data = surfxml_pcdata;
652
653   sg_platf_new_trace(&trace);
654 }
655
656 void STag_surfxml_trace___connect(void){
657   s_sg_platf_trace_connect_cbarg_t trace_connect;
658   memset(&trace_connect,0,sizeof(trace_connect));
659
660   trace_connect.element = A_surfxml_trace___connect_element;
661   trace_connect.trace = A_surfxml_trace___connect_trace;
662
663   switch (A_surfxml_trace___connect_kind) {
664   case AU_surfxml_trace___connect_kind:
665   case A_surfxml_trace___connect_kind_POWER:
666     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_POWER;
667     break;
668   case A_surfxml_trace___connect_kind_BANDWIDTH:
669     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_BANDWIDTH;
670     break;
671   case A_surfxml_trace___connect_kind_HOST___AVAIL:
672     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_HOST_AVAIL;
673     break;
674   case A_surfxml_trace___connect_kind_LATENCY:
675     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LATENCY;
676     break;
677   case A_surfxml_trace___connect_kind_LINK___AVAIL:
678     trace_connect.kind =  SURF_TRACE_CONNECT_KIND_LINK_AVAIL;
679     break;
680   }
681   sg_platf_trace_connect(&trace_connect);
682 }
683
684 void STag_surfxml_AS(void){
685   AS_TAG = 1;
686   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
687   AS.id = A_surfxml_AS_id;
688   AS.routing = (int)A_surfxml_AS_routing;
689
690   as_current_property_set = NULL;
691
692   sg_platf_new_AS_begin(&AS);
693 }
694 void ETag_surfxml_AS(void){
695   if(as_prop_nb){
696     char *name = as_name_tab[as_prop_nb-1];
697     xbt_dict_t dict = as_dict_tab[as_prop_nb-1];
698     as_prop_nb--;
699     XBT_DEBUG("POP prop %p for AS '%s'",dict,name);
700     xbt_lib_set(as_router_lib,
701         name,
702       ROUTING_PROP_ASR_LEVEL,
703       dict);
704     xbt_free(name);
705   }
706   sg_platf_new_AS_end();
707 }
708
709 extern int _sg_init_status; /* FIXME: find a proper way to export this at some point */
710
711 void STag_surfxml_config(void){
712   AS_TAG = 0;
713   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
714   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
715   if (_sg_init_status == 2) {
716     surf_parse_error("All <config> tags must be given before any platform elements (such as <AS>, <host>, <cluster>, <link>, etc).");
717   }
718 }
719 void ETag_surfxml_config(void){
720   xbt_dict_cursor_t cursor = NULL;
721   char *key;
722   char *elem;
723   char *cfg;
724   xbt_dict_foreach(current_property_set, cursor, key, elem) {
725     cfg = bprintf("%s:%s",key,elem);
726     if(xbt_cfg_is_default_value(_sg_cfg_set, key))
727       xbt_cfg_set_parse(_sg_cfg_set, cfg);
728     else
729       XBT_INFO("The custom configuration '%s' is already defined by user!",key);
730     free(cfg);
731   }
732   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
733   xbt_dict_free(&current_property_set);
734   current_property_set = NULL;
735 }
736
737 static int argc;
738 static char **argv;
739
740 void STag_surfxml_process(void){
741   AS_TAG = 0;
742   argc = 1;
743   argv = xbt_new(char *, 1);
744   argv[0] = xbt_strdup(A_surfxml_process_function);
745   xbt_assert(current_property_set == NULL, "Someone forgot to reset the property set to NULL in its closing tag (or XML malformed)");
746 }
747
748 void ETag_surfxml_process(void){
749   s_sg_platf_process_cbarg_t process;
750   memset(&process,0,sizeof(process));
751
752   process.argc = argc;
753   process.argv = (const char **)argv;
754   process.properties = current_property_set;
755   process.host = A_surfxml_process_host;
756   process.function = A_surfxml_process_function;
757   process.start_time = surf_parse_get_double(A_surfxml_process_start___time);
758   process.kill_time = surf_parse_get_double(A_surfxml_process_kill___time);
759
760   switch (A_surfxml_process_on___failure) {
761   case AU_surfxml_process_on___failure:
762   case A_surfxml_process_on___failure_DIE:
763     process.on_failure =  SURF_PROCESS_ON_FAILURE_DIE;
764     break;
765   case A_surfxml_process_on___failure_RESTART:
766     process.on_failure =  SURF_PROCESS_ON_FAILURE_RESTART;
767     break;
768   }
769
770   sg_platf_new_process(&process);
771   current_property_set = NULL;
772 }
773
774 void STag_surfxml_argument(void){
775   argc++;
776   argv = xbt_realloc(argv, (argc) * sizeof(char *));
777   argv[(argc) - 1] = xbt_strdup(A_surfxml_argument_value);
778 }
779
780 /* ***************************************** */
781 /* TUTORIAL: New TAG                         */
782 void STag_surfxml_gpu(void)
783 {
784   XBT_DEBUG("STag_surfxml_gpu");
785 }
786 void ETag_surfxml_gpu(void)
787 {
788   s_sg_platf_gpu_cbarg_t gpu;
789   memset(&gpu,0,sizeof(gpu));
790
791   gpu.name = A_surfxml_gpu_name;
792
793   sg_platf_new_gpu(&gpu);
794 }
795 /* ***************************************** */
796
797 /* nothing to do in those functions */
798 void ETag_surfxml_prop(void){}
799 void STag_surfxml_random(void){}
800 void ETag_surfxml_random(void){}
801 void ETag_surfxml_trace___connect(void){}
802 void STag_surfxml_trace(void){}
803 void ETag_surfxml_router(void){}
804 void ETag_surfxml_host___link(void){}
805 void ETag_surfxml_cluster(void){}
806 void ETag_surfxml_cabinet(void){}
807 void ETag_surfxml_peer(void){}
808 void STag_surfxml_backbone(void){}
809 void ETag_surfxml_link___ctn(void){}
810 void ETag_surfxml_argument(void){}
811
812 /* Open and Close parse file */
813
814 void surf_parse_open(const char *file)
815 {
816   static int warned = 0;        /* warn only once */
817   if (!file) {
818     if (!warned) {
819       XBT_WARN
820           ("Bypassing the XML parser since surf_parse_open received a NULL pointer. "
821               "If it is not what you want, go fix your code.");
822       warned = 1;
823     }
824     return;
825   }
826
827   if (!surf_input_buffer_stack)
828     surf_input_buffer_stack = xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
829   if (!surf_file_to_parse_stack)
830     surf_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
831
832   if (!surf_parsed_filename_stack)
833     surf_parsed_filename_stack = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
834   surf_parsed_filename = xbt_strdup(file);
835
836   surf_file_to_parse = surf_fopen(file, "r");
837   xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", file);
838   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
839   surf_parse__switch_to_buffer(surf_input_buffer);
840   surf_parse_lineno = 1;
841 }
842
843 void surf_parse_close(void)
844 {
845   xbt_dynar_free(&surf_input_buffer_stack);
846   xbt_dynar_free(&surf_file_to_parse_stack);
847   xbt_dynar_free(&surf_parsed_filename_stack);
848
849   free(surf_parsed_filename);
850   surf_parsed_filename = NULL;
851
852   if (surf_file_to_parse) {
853     surf_parse__delete_buffer(surf_input_buffer);
854     fclose(surf_file_to_parse);
855     surf_file_to_parse = NULL; //Must be reset for Bypass
856   }
857 }
858
859 /* Call the lexer to parse the currently opened file. This pointer to function enables bypassing of the parser */
860
861 static int _surf_parse(void) {
862   return surf_parse_lex();
863 }
864 int_f_void_t surf_parse = _surf_parse;
865
866
867 /* Prop tag functions */
868
869 /**
870  * With XML parser
871  */
872
873 /* Random tag functions */
874
875 double get_cpu_power(const char *power)
876 {
877   double power_scale = 0.0;
878   const char *p, *q;
879   char *generator;
880   random_data_t random = NULL;
881   /* randomness is inserted like this: power="$rand(my_random)" */
882   if (((p = strstr(power, "$rand(")) != NULL)
883       && ((q = strstr(power, ")")) != NULL)) {
884     if (p < q) {
885       generator = xbt_malloc(q - (p + 6) + 1);
886       memcpy(generator, p + 6, q - (p + 6));
887       generator[q - (p + 6)] = '\0';
888       random = xbt_dict_get_or_null(random_data_list, generator);
889       xbt_assert(random, "Random generator %s undefined", generator);
890       power_scale = random_generate(random);
891     }
892   } else {
893     power_scale = surf_parse_get_double(power);
894   }
895   return power_scale;
896 }
897
898 double random_min, random_max, random_mean, random_std_deviation;
899 e_random_generator_t random_generator;
900 char *random_id;
901
902 static void init_randomness(void)
903 {
904   random_id = A_surfxml_random_id;
905   random_min = surf_parse_get_double(A_surfxml_random_min);
906   random_max = surf_parse_get_double(A_surfxml_random_max);
907   random_mean = surf_parse_get_double(A_surfxml_random_mean);
908   random_std_deviation = surf_parse_get_double(A_surfxml_random_std___deviation);
909   switch (A_surfxml_random_generator) {
910   case AU_surfxml_random_generator:
911   case A_surfxml_random_generator_NONE:
912     random_generator = NONE;
913     break;
914   case A_surfxml_random_generator_DRAND48:
915     random_generator = DRAND48;
916     break;
917   case A_surfxml_random_generator_RAND:
918     random_generator = RAND;
919     break;
920   case A_surfxml_random_generator_RNGSTREAM:
921     random_generator = RNGSTREAM;
922     break;
923   default:
924     surf_parse_error("Invalid random generator");
925     break;
926   }
927 }
928
929 static void add_randomness(void)
930 {
931   /* If needed aditional properties can be added by using the prop tag */
932   random_data_t random =
933       random_new(random_generator, 0, random_min, random_max, random_mean,
934                  random_std_deviation);
935   xbt_dict_set(random_data_list, random_id, (void *) random,
936                &xbt_free_ref);
937 }
938
939
940 xbt_dict_t get_as_router_properties(const char* name)
941 {
942   return xbt_lib_get_or_null(as_router_lib, name, ROUTING_PROP_ASR_LEVEL);
943 }
944