Logo AND Algorithmique Numérique Distribuée

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