Logo AND Algorithmique Numérique Distribuée

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