Logo AND Algorithmique Numérique Distribuée

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