Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / surf / xml / surfxml_sax_cb.cpp
1 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <xbt/file.hpp>
10 #include <xbt/parse_units.hpp>
11
12 #include "simgrid/sg_config.hpp"
13 #include "src/kernel/resource/LinkImpl.hpp"
14 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
15 #include "src/kernel/resource/profile/Profile.hpp"
16 #include "src/surf/surf_interface.hpp"
17 #include "src/surf/xml/platf.hpp"
18 #include "src/surf/xml/platf_private.hpp"
19
20 #include <boost/algorithm/string/classification.hpp>
21 #include <boost/algorithm/string/split.hpp>
22 #include <string>
23 #include <tuple>
24 #include <unordered_map>
25 #include <vector>
26
27 #include "simgrid_dtd.c"
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
30
31 std::string surf_parsed_filename; // Currently parsed file (for the error messages)
32 std::vector<simgrid::s4u::LinkInRoute> parsed_link_list; /* temporary store of current link list of a route */
33
34 /* Helping functions */
35 void surf_parse_assert(bool cond, const std::string& msg)
36 {
37   if (not cond)
38     surf_parse_error(msg);
39 }
40
41 void surf_parse_error(const std::string& msg)
42 {
43   throw simgrid::ParseError(surf_parsed_filename, surf_parse_lineno, msg);
44 }
45
46 void surf_parse_assert_netpoint(const std::string& hostname, const std::string& pre, const std::string& post)
47 {
48   if (simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(hostname) != nullptr) // found
49     return;
50
51   std::string msg = pre + hostname + post + " Existing netpoints:\n";
52
53   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
54       simgrid::s4u::Engine::get_instance()->get_all_netpoints();
55   std::sort(netpoints.begin(), netpoints.end(),
56             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
57               return a->get_name() < b->get_name();
58             });
59   bool first = true;
60   for (auto const& np : netpoints) {
61     if (np->is_netzone())
62       continue;
63
64     if (not first)
65       msg += ",";
66     first = false;
67     msg += "'" + np->get_name() + "'";
68     if (msg.length() > 4096) {
69       msg.pop_back(); // remove trailing quote
70       msg += "...(list truncated)......";
71       break;
72     }
73   }
74   surf_parse_error(msg);
75 }
76
77 double surf_parse_get_double(const std::string& s)
78 {
79   try {
80     return std::stod(s);
81   } catch (const std::invalid_argument&) {
82     surf_parse_error(s + " is not a double");
83   }
84 }
85
86 int surf_parse_get_int(const std::string& s)
87 {
88   try {
89     return std::stoi(s);
90   } catch (const std::invalid_argument&) {
91     surf_parse_error(s + " is not an int");
92   }
93 }
94
95 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
96 static void explodesRadical(const std::string& radicals, std::vector<int>* exploded)
97 {
98   // Make all hosts
99   std::vector<std::string> radical_elements;
100   boost::split(radical_elements, radicals, boost::is_any_of(","));
101   for (auto const& group : radical_elements) {
102     std::vector<std::string> radical_ends;
103     boost::split(radical_ends, group, boost::is_any_of("-"));
104     int start = surf_parse_get_int(radical_ends.front());
105     int end   = 0;
106
107     switch (radical_ends.size()) {
108       case 1:
109         end = start;
110         break;
111       case 2:
112         end = surf_parse_get_int(radical_ends.back());
113         break;
114       default:
115         surf_parse_error("Malformed radical: " + group);
116     }
117     for (int i = start; i <= end; i++)
118       exploded->push_back(i);
119   }
120 }
121
122
123 /*
124  * All the callback lists that can be overridden anywhere.
125  * (this list should probably be reduced to the bare minimum to allow the models to work)
126  */
127
128 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
129
130 std::vector<std::unordered_map<std::string, std::string>> property_sets;
131
132 FILE *surf_file_to_parse = nullptr;
133
134 /* Stuff relative to storage */
135 void STag_surfxml_storage()
136 {
137   xbt_die("<storage> tag was removed in SimGrid v3.27. Please stop using it now.");
138 }
139
140 void ETag_surfxml_storage()
141 {
142   /* Won't happen since <storage> is now removed since v3.27. */
143 }
144 void STag_surfxml_storage___type()
145 {
146   xbt_die("<storage_type> tag was removed in SimGrid v3.27. Please stop using it now.");
147 }
148 void ETag_surfxml_storage___type()
149 {
150   /* Won't happen since <storage_type> is now removed since v3.27. */
151 }
152
153 void STag_surfxml_mount()
154 {
155   xbt_die("<mount> tag was removed in SimGrid v3.27. Please stop using it now.");
156 }
157
158 void ETag_surfxml_mount()
159 {
160   /* Won't happen since <mount> is now removed since v3.27. */
161 }
162
163 void STag_surfxml_include()
164 {
165   xbt_die("<include> tag was removed in SimGrid v3.18. Please stop using it now.");
166 }
167
168 void ETag_surfxml_include()
169 {
170   /* Won't happen since <include> is now removed since v3.18. */
171 }
172
173 /* Stag and Etag parse functions */
174 void STag_surfxml_platform() {
175   /* Use fixed point arithmetic to avoid rounding errors ("4.1" for example cannot be represented exactly as a floating
176    * point number) */
177   const long int version           = lround(100.0 * surf_parse_get_double(A_surfxml_platform_version));
178   const std::string version_string = std::to_string(version / 100) + "." + std::to_string(version % 100);
179
180   surf_parse_assert(version >= 100L, "******* BIG FAT WARNING *********\n "
181                                      "You're using an ancient XML file.\n"
182                                      "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
183                                      "instead of MBytes, MFlops and seconds.\n"
184
185                                      "Use simgrid_update_xml to update your file automatically. "
186                                      "This program is installed automatically with SimGrid, or "
187                                      "available in the tools/ directory of the source archive.\n"
188
189                                      "Please check also out the SURF section of the ChangeLog for "
190                                      "the 3.1 version for more information. \n"
191
192                                      "Last, do not forget to also update your values for "
193                                      "the calls to MSG_task_create (if any).");
194   surf_parse_assert(version >= 300L, "******* BIG FAT WARNING *********\n "
195                                      "You're using an old XML file.\n"
196                                      "Use simgrid_update_xml to update your file automatically. "
197                                      "This program is installed automatically with SimGrid, or "
198                                      "available in the tools/ directory of the source archive.");
199   surf_parse_assert(
200       version >= 400L,
201       "******* THIS FILE IS TOO OLD (v:" + version_string +
202           ") *********\n "
203           "Changes introduced in SimGrid 3.13:\n"
204           "  - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
205           "  - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
206           "  - DOCTYPE now point to the rignt URL.\n"
207           "  - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
208           "\n\n"
209           "Use simgrid_update_xml to update your file automatically. "
210           "This program is installed automatically with SimGrid, or "
211           "available in the tools/ directory of the source archive.");
212   if (version < 410L) {
213     XBT_INFO("You're using a v%s XML file (%s) while the current standard is v4.1 "
214              "That's fine, the new version is backward compatible. \n\n"
215              "Use simgrid_update_xml to update your file automatically to get rid of this warning. "
216              "This program is installed automatically with SimGrid, or "
217              "available in the tools/ directory of the source archive.",
218              version_string.c_str(), surf_parsed_filename.c_str());
219   }
220   surf_parse_assert(version <= 410L, "******* THIS FILE COMES FROM THE FUTURE (v:" + version_string +
221                                          ") *********\n "
222                                          "The most recent formalism that this version of SimGrid understands is v4.1.\n"
223                                          "Please update your code, or use another, more adapted, file.");
224 }
225 void ETag_surfxml_platform(){
226   simgrid::s4u::Engine::on_platform_created();
227 }
228
229 void STag_surfxml_prop()
230 {
231   property_sets.back().try_emplace(A_surfxml_prop_id, A_surfxml_prop_value);
232   XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
233             &(property_sets.back()));
234 }
235
236 void STag_surfxml_host()
237 {
238   simgrid::kernel::routing::HostCreationArgs host;
239   property_sets.emplace_back();
240   host.id = A_surfxml_host_id;
241
242   host.speed_per_pstate = xbt_parse_get_all_speeds(surf_parsed_filename, surf_parse_lineno, A_surfxml_host_speed,
243                                                    "speed of host " + host.id);
244
245   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
246   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
247
248   if (A_surfxml_host_availability___file[0] != '\0') {
249     XBT_WARN("The availability_file attribute in <host> is now deprecated. Please, use 'speed_file' instead.");
250     host.speed_trace = simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_host_availability___file);
251   }
252   if (A_surfxml_host_speed___file[0] != '\0')
253     host.speed_trace = simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_host_speed___file);
254   host.state_trace = A_surfxml_host_state___file[0]
255                          ? simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_host_state___file)
256                          : nullptr;
257   host.coord       = A_surfxml_host_coordinates;
258
259   sg_platf_new_host_begin(&host);
260 }
261
262 void ETag_surfxml_host()
263 {
264   sg_platf_new_host_set_properties(property_sets.back());
265   property_sets.pop_back();
266
267   sg_platf_new_host_seal(surf_parse_get_int(A_surfxml_host_pstate));
268 }
269
270 void STag_surfxml_disk() {
271   property_sets.emplace_back();
272 }
273
274 void ETag_surfxml_disk() {
275   simgrid::kernel::routing::DiskCreationArgs disk;
276   disk.properties = property_sets.back();
277   property_sets.pop_back();
278
279   disk.id       = A_surfxml_disk_id;
280   disk.read_bw  = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_disk_read___bw,
281                                          "read_bw of disk " + disk.id);
282   disk.write_bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_disk_write___bw,
283                                           "write_bw of disk " + disk.id);
284
285   sg_platf_new_disk(&disk);
286 }
287
288 void STag_surfxml_host___link(){
289   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
290   simgrid::kernel::routing::HostLinkCreationArgs host_link;
291
292   host_link.id        = A_surfxml_host___link_id;
293   host_link.link_up   = A_surfxml_host___link_up;
294   host_link.link_down = A_surfxml_host___link_down;
295   sg_platf_new_hostlink(&host_link);
296 }
297
298 void STag_surfxml_router(){
299   sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
300 }
301
302 void ETag_surfxml_cluster(){
303   simgrid::kernel::routing::ClusterCreationArgs cluster;
304   cluster.properties = property_sets.back();
305   property_sets.pop_back();
306
307   cluster.id          = A_surfxml_cluster_id;
308   cluster.prefix      = A_surfxml_cluster_prefix;
309   cluster.suffix      = A_surfxml_cluster_suffix;
310   explodesRadical(A_surfxml_cluster_radical, &cluster.radicals);
311
312   cluster.speeds = xbt_parse_get_all_speeds(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_speed,
313                                             "speed of cluster " + cluster.id);
314   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
315   cluster.bw          = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bw,
316                                        "bw of cluster " + cluster.id);
317   cluster.lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_lat,
318                                    "lat of cluster " + cluster.id);
319   if(strcmp(A_surfxml_cluster_bb___bw,""))
320     cluster.bb_bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bb___bw,
321                                             "bb_bw of cluster " + cluster.id);
322   if(strcmp(A_surfxml_cluster_bb___lat,""))
323     cluster.bb_lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bb___lat,
324                                         "bb_lat of cluster " + cluster.id);
325   if(strcmp(A_surfxml_cluster_limiter___link,""))
326     cluster.limiter_link =
327         xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_limiter___link,
328                                 "limiter_link of cluster " + cluster.id);
329   if(strcmp(A_surfxml_cluster_loopback___bw,""))
330     cluster.loopback_bw =
331         xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_loopback___bw,
332                                 "loopback_bw of cluster " + cluster.id);
333   if(strcmp(A_surfxml_cluster_loopback___lat,""))
334     cluster.loopback_lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_loopback___lat,
335                                               "loopback_lat of cluster " + cluster.id);
336
337   switch(AX_surfxml_cluster_topology){
338   case A_surfxml_cluster_topology_FLAT:
339     cluster.topology = simgrid::kernel::routing::ClusterTopology::FLAT;
340     break;
341   case A_surfxml_cluster_topology_TORUS:
342     cluster.topology = simgrid::kernel::routing::ClusterTopology::TORUS;
343     break;
344   case A_surfxml_cluster_topology_FAT___TREE:
345     cluster.topology = simgrid::kernel::routing::ClusterTopology::FAT_TREE;
346     break;
347   case A_surfxml_cluster_topology_DRAGONFLY:
348     cluster.topology = simgrid::kernel::routing::ClusterTopology::DRAGONFLY;
349     break;
350   default:
351     surf_parse_error("Invalid cluster topology for cluster " + cluster.id);
352   }
353   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
354   cluster.router_id = A_surfxml_cluster_router___id;
355
356   switch (AX_surfxml_cluster_sharing___policy) {
357   case A_surfxml_cluster_sharing___policy_SHARED:
358     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
359     break;
360   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
361     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
362     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
363     break;
364   case A_surfxml_cluster_sharing___policy_SPLITDUPLEX:
365     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
366     break;
367   case A_surfxml_cluster_sharing___policy_FATPIPE:
368     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
369     break;
370   default:
371     surf_parse_error("Invalid cluster sharing policy for cluster " + cluster.id);
372   }
373   switch (AX_surfxml_cluster_bb___sharing___policy) {
374   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
375     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
376     break;
377   case A_surfxml_cluster_bb___sharing___policy_SHARED:
378     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
379     break;
380   default:
381     surf_parse_error("Invalid bb sharing policy in cluster " + cluster.id);
382   }
383
384   sg_platf_new_tag_cluster(&cluster);
385 }
386
387 void STag_surfxml_cluster(){
388   property_sets.emplace_back();
389 }
390
391 void STag_surfxml_cabinet(){
392   simgrid::kernel::routing::CabinetCreationArgs cabinet;
393   cabinet.id      = A_surfxml_cabinet_id;
394   cabinet.prefix  = A_surfxml_cabinet_prefix;
395   cabinet.suffix  = A_surfxml_cabinet_suffix;
396   cabinet.speed   = xbt_parse_get_speed(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_speed,
397                                       "speed of cabinet " + cabinet.id);
398   cabinet.bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_bw,
399                                        "bw of cabinet " + cabinet.id);
400   cabinet.lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_lat,
401                                    "lat of cabinet " + cabinet.id);
402   explodesRadical(A_surfxml_cabinet_radical, &cabinet.radicals);
403
404   sg_platf_new_cabinet(&cabinet);
405 }
406
407 void STag_surfxml_peer(){
408   simgrid::kernel::routing::PeerCreationArgs peer;
409
410   peer.id = A_surfxml_peer_id;
411   peer.speed =
412       xbt_parse_get_speed(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_speed, "speed of peer " + peer.id);
413   peer.bw_in = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_bw___in,
414                                        "bw_in of peer " + peer.id);
415   peer.bw_out = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_bw___out,
416                                         "bw_out of peer " + peer.id);
417   peer.coord       = A_surfxml_peer_coordinates;
418   peer.speed_trace = nullptr;
419   if (A_surfxml_peer_availability___file[0] != '\0') {
420     XBT_WARN("The availability_file attribute in <peer> is now deprecated. Please, use 'speed_file' instead.");
421     peer.speed_trace = simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_peer_availability___file);
422   }
423   if (A_surfxml_peer_speed___file[0] != '\0')
424     peer.speed_trace = simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_peer_speed___file);
425   peer.state_trace = A_surfxml_peer_state___file[0]
426                          ? simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_peer_state___file)
427                          : nullptr;
428
429   if (A_surfxml_peer_lat[0] != '\0')
430     XBT_WARN("The latency attribute in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
431              A_surfxml_peer_lat);
432
433   sg_platf_new_peer(&peer);
434 }
435
436 void STag_surfxml_link(){
437   property_sets.emplace_back();
438 }
439
440 void ETag_surfxml_link(){
441   simgrid::kernel::routing::LinkCreationArgs link;
442
443   link.properties = property_sets.back();
444   property_sets.pop_back();
445
446   link.id                  = A_surfxml_link_id;
447   link.bandwidths          = xbt_parse_get_bandwidths(surf_parsed_filename, surf_parse_lineno, A_surfxml_link_bandwidth,
448                                              "bandwidth of link " + link.id);
449   link.bandwidth_trace     = A_surfxml_link_bandwidth___file[0]
450                              ? simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_link_bandwidth___file)
451                              : nullptr;
452   link.latency =
453       xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_link_latency, "latency of link " + link.id);
454   link.latency_trace       = A_surfxml_link_latency___file[0]
455                            ? simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_link_latency___file)
456                            : nullptr;
457   link.state_trace = A_surfxml_link_state___file[0]
458                          ? simgrid::kernel::profile::ProfileBuilder::from_file(A_surfxml_link_state___file)
459                          : nullptr;
460
461   switch (A_surfxml_link_sharing___policy) {
462   case A_surfxml_link_sharing___policy_SHARED:
463     link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
464     break;
465   case A_surfxml_link_sharing___policy_FATPIPE:
466     link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
467     break;
468   case A_surfxml_link_sharing___policy_FULLDUPLEX:
469     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
470     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
471     break;
472   case A_surfxml_link_sharing___policy_SPLITDUPLEX:
473     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
474     break;
475   case A_surfxml_link_sharing___policy_WIFI:
476     link.policy = simgrid::s4u::Link::SharingPolicy::WIFI;
477     break;
478   default:
479     surf_parse_error("Invalid sharing policy in link " + link.id);
480   }
481
482   sg_platf_new_link(&link);
483 }
484
485 void STag_surfxml_link___ctn()
486 {
487   const auto engine = simgrid::s4u::Engine::get_instance();
488   const simgrid::s4u::Link* link;
489   simgrid::s4u::LinkInRoute::Direction direction = simgrid::s4u::LinkInRoute::Direction::NONE;
490   switch (A_surfxml_link___ctn_direction) {
491   case AU_surfxml_link___ctn_direction:
492   case A_surfxml_link___ctn_direction_NONE:
493     link = engine->link_by_name(A_surfxml_link___ctn_id);
494     break;
495   case A_surfxml_link___ctn_direction_UP:
496     link      = engine->split_duplex_link_by_name(A_surfxml_link___ctn_id);
497     direction = simgrid::s4u::LinkInRoute::Direction::UP;
498     break;
499   case A_surfxml_link___ctn_direction_DOWN:
500     link      = engine->split_duplex_link_by_name(A_surfxml_link___ctn_id);
501     direction = simgrid::s4u::LinkInRoute::Direction::DOWN;
502     break;
503   default:
504     surf_parse_error(std::string("Invalid direction for link ") + A_surfxml_link___ctn_id);
505   }
506
507   const char* dirname;
508   switch (A_surfxml_link___ctn_direction) {
509     case A_surfxml_link___ctn_direction_UP:
510       dirname = " (upward)";
511       break;
512     case A_surfxml_link___ctn_direction_DOWN:
513       dirname = " (downward)";
514       break;
515     default:
516       dirname = "";
517   }
518   surf_parse_assert(link != nullptr, std::string("No such link: '") + A_surfxml_link___ctn_id + "'" + dirname);
519   parsed_link_list.emplace_back(link, direction);
520 }
521
522 void ETag_surfxml_backbone()
523 {
524   auto link = std::make_unique<simgrid::kernel::routing::LinkCreationArgs>();
525
526   link->id = A_surfxml_backbone_id;
527   link->bandwidths.push_back(xbt_parse_get_bandwidth(
528       surf_parsed_filename, surf_parse_lineno, A_surfxml_backbone_bandwidth, "bandwidth of backbone " + link->id));
529   link->latency = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_backbone_latency,
530                                      "latency of backbone " + link->id);
531   link->policy  = simgrid::s4u::Link::SharingPolicy::SHARED;
532
533   routing_cluster_add_backbone(std::move(link));
534 }
535
536 void STag_surfxml_route(){
537   surf_parse_assert_netpoint(A_surfxml_route_src, "Route src='", "' does name a node.");
538   surf_parse_assert_netpoint(A_surfxml_route_dst, "Route dst='", "' does name a node.");
539 }
540
541 void STag_surfxml_ASroute(){
542   surf_parse_assert_netpoint(A_surfxml_ASroute_src, "ASroute src='", "' does name a node.");
543   surf_parse_assert_netpoint(A_surfxml_ASroute_dst, "ASroute dst='", "' does name a node.");
544
545   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___src, "ASroute gw_src='", "' does name a node.");
546   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___dst, "ASroute gw_dst='", "' does name a node.");
547 }
548 void STag_surfxml_zoneRoute(){
549   surf_parse_assert_netpoint(A_surfxml_zoneRoute_src, "zoneRoute src='", "' does name a node.");
550   surf_parse_assert_netpoint(A_surfxml_zoneRoute_dst, "zoneRoute dst='", "' does name a node.");
551   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___src, "zoneRoute gw_src='", "' does name a node.");
552   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___dst, "zoneRoute gw_dst='", "' does name a node.");
553 }
554
555 void STag_surfxml_bypassRoute(){
556   surf_parse_assert_netpoint(A_surfxml_bypassRoute_src, "bypassRoute src='", "' does name a node.");
557   surf_parse_assert_netpoint(A_surfxml_bypassRoute_dst, "bypassRoute dst='", "' does name a node.");
558 }
559
560 void STag_surfxml_bypassASroute(){
561   surf_parse_assert_netpoint(A_surfxml_bypassASroute_src, "bypassASroute src='", "' does name a node.");
562   surf_parse_assert_netpoint(A_surfxml_bypassASroute_dst, "bypassASroute dst='", "' does name a node.");
563   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___src, "bypassASroute gw_src='", "' does name a node.");
564   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___dst, "bypassASroute gw_dst='", "' does name a node.");
565 }
566 void STag_surfxml_bypassZoneRoute(){
567   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_src, "bypassZoneRoute src='", "' does name a node.");
568   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_dst, "bypassZoneRoute dst='", "' does name a node.");
569   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___src, "bypassZoneRoute gw_src='", "' does name a node.");
570   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___dst, "bypassZoneRoute gw_dst='", "' does name a node.");
571 }
572
573 void ETag_surfxml_route(){
574   simgrid::kernel::routing::RouteCreationArgs route;
575
576   route.src         = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
577   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
578   route.symmetrical = (A_surfxml_route_symmetrical == AU_surfxml_route_symmetrical ||
579                        A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES ||
580                        A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_yes);
581
582   route.link_list.swap(parsed_link_list);
583
584   sg_platf_new_route(&route);
585 }
586
587 void ETag_surfxml_ASroute()
588 {
589   AX_surfxml_zoneRoute_src = AX_surfxml_ASroute_src;
590   AX_surfxml_zoneRoute_dst = AX_surfxml_ASroute_dst;
591   AX_surfxml_zoneRoute_gw___src = AX_surfxml_ASroute_gw___src;
592   AX_surfxml_zoneRoute_gw___dst = AX_surfxml_ASroute_gw___dst;
593   AX_surfxml_zoneRoute_symmetrical = (AT_surfxml_zoneRoute_symmetrical)AX_surfxml_ASroute_symmetrical;
594   ETag_surfxml_zoneRoute();
595 }
596 void ETag_surfxml_zoneRoute()
597 {
598   simgrid::kernel::routing::RouteCreationArgs ASroute;
599
600   ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src); // tested to not be nullptr in start tag
601   ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst); // tested to not be nullptr in start tag
602
603   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src); // tested to not be nullptr in start tag
604   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst); // tested to not be nullptr in start tag
605
606   ASroute.link_list.swap(parsed_link_list);
607
608   ASroute.symmetrical = (A_surfxml_zoneRoute_symmetrical == AU_surfxml_zoneRoute_symmetrical ||
609                          A_surfxml_zoneRoute_symmetrical == A_surfxml_zoneRoute_symmetrical_YES ||
610                          A_surfxml_zoneRoute_symmetrical == A_surfxml_zoneRoute_symmetrical_yes);
611
612   sg_platf_new_route(&ASroute);
613 }
614
615 void ETag_surfxml_bypassRoute(){
616   simgrid::kernel::routing::RouteCreationArgs route;
617
618   route.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
619   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
620   route.symmetrical = false;
621
622   route.link_list.swap(parsed_link_list);
623
624   sg_platf_new_bypass_route(&route);
625 }
626
627 void ETag_surfxml_bypassASroute()
628 {
629   AX_surfxml_bypassZoneRoute_src = AX_surfxml_bypassASroute_src;
630   AX_surfxml_bypassZoneRoute_dst = AX_surfxml_bypassASroute_dst;
631   AX_surfxml_bypassZoneRoute_gw___src = AX_surfxml_bypassASroute_gw___src;
632   AX_surfxml_bypassZoneRoute_gw___dst = AX_surfxml_bypassASroute_gw___dst;
633   ETag_surfxml_bypassZoneRoute();
634 }
635 void ETag_surfxml_bypassZoneRoute()
636 {
637   simgrid::kernel::routing::RouteCreationArgs ASroute;
638
639   ASroute.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src);
640   ASroute.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst);
641   ASroute.link_list.swap(parsed_link_list);
642
643   ASroute.symmetrical = false;
644
645   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src);
646   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst);
647
648   sg_platf_new_bypass_route(&ASroute);
649 }
650
651 void ETag_surfxml_trace(){
652   simgrid::kernel::routing::ProfileCreationArgs trace;
653
654   trace.id = A_surfxml_trace_id;
655   trace.file = A_surfxml_trace_file;
656   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
657   trace.pc_data = surfxml_pcdata;
658
659   sg_platf_new_trace(&trace);
660 }
661
662 void STag_surfxml_trace___connect()
663 {
664   simgrid::kernel::routing::TraceConnectCreationArgs trace_connect;
665
666   trace_connect.element = A_surfxml_trace___connect_element;
667   trace_connect.trace   = A_surfxml_trace___connect_trace;
668
669   switch (A_surfxml_trace___connect_kind) {
670     case AU_surfxml_trace___connect_kind:
671     case A_surfxml_trace___connect_kind_SPEED:
672       trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::SPEED;
673       break;
674     case A_surfxml_trace___connect_kind_BANDWIDTH:
675       trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::BANDWIDTH;
676       break;
677     case A_surfxml_trace___connect_kind_HOST___AVAIL:
678       trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL;
679       break;
680     case A_surfxml_trace___connect_kind_LATENCY:
681       trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LATENCY;
682       break;
683     case A_surfxml_trace___connect_kind_LINK___AVAIL:
684       trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL;
685       break;
686     default:
687       surf_parse_error("Invalid trace kind");
688   }
689   sg_platf_trace_connect(&trace_connect);
690 }
691
692 void STag_surfxml_AS()
693 {
694   AX_surfxml_zone_id = AX_surfxml_AS_id;
695   AX_surfxml_zone_routing = AX_surfxml_AS_routing;
696   STag_surfxml_zone();
697 }
698
699 void ETag_surfxml_AS()
700 {
701   ETag_surfxml_zone();
702 }
703
704 void STag_surfxml_zone()
705 {
706   property_sets.emplace_back();
707   simgrid::kernel::routing::ZoneCreationArgs zone;
708   zone.id      = A_surfxml_zone_id;
709   zone.routing = A_surfxml_zone_routing;
710   sg_platf_new_zone_begin(&zone);
711 }
712
713 void ETag_surfxml_zone()
714 {
715   sg_platf_new_zone_set_properties(property_sets.back());
716   property_sets.pop_back();
717   sg_platf_new_zone_seal();
718 }
719
720 void STag_surfxml_config()
721 {
722   property_sets.emplace_back();
723   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
724   if (_sg_cfg_init_status == 2) {
725     surf_parse_error("All <config> tags must be given before any platform elements (such as <zone>, <host>, <cluster>, "
726                      "<link>, etc).");
727   }
728 }
729
730 void ETag_surfxml_config()
731 {
732   // Sort config elements before applying.
733   // That's a little waste of time, but not doing so would break the tests
734   auto current_property_set = property_sets.back();
735
736   std::vector<std::string> keys;
737   for (auto const& [key, _] : current_property_set) {
738     keys.push_back(key);
739   }
740   std::sort(keys.begin(), keys.end());
741   for (const std::string& key : keys) {
742     if (simgrid::config::is_default(key.c_str())) {
743       std::string cfg = key + ":" + current_property_set.at(key);
744       simgrid::config::set_parse(cfg);
745     } else
746       XBT_INFO("The custom configuration '%s' is already defined by user!", key.c_str());
747   }
748   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
749
750   property_sets.pop_back();
751 }
752
753 static std::vector<std::string> arguments;
754
755 void STag_surfxml_process()
756 {
757   AX_surfxml_actor_function = AX_surfxml_process_function;
758   STag_surfxml_actor();
759 }
760
761 void STag_surfxml_actor()
762 {
763   property_sets.emplace_back();
764   arguments.assign(1, A_surfxml_actor_function);
765 }
766
767 void ETag_surfxml_process()
768 {
769   AX_surfxml_actor_host = AX_surfxml_process_host;
770   AX_surfxml_actor_function = AX_surfxml_process_function;
771   AX_surfxml_actor_start___time = AX_surfxml_process_start___time;
772   AX_surfxml_actor_kill___time = AX_surfxml_process_kill___time;
773   AX_surfxml_actor_on___failure = (AT_surfxml_actor_on___failure)AX_surfxml_process_on___failure;
774   ETag_surfxml_actor();
775 }
776
777 void ETag_surfxml_actor()
778 {
779   simgrid::kernel::routing::ActorCreationArgs actor;
780
781   actor.properties = property_sets.back();
782   property_sets.pop_back();
783
784   actor.args.swap(arguments);
785   actor.host       = A_surfxml_actor_host;
786   actor.function   = A_surfxml_actor_function;
787   actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time);
788   actor.kill_time  = surf_parse_get_double(A_surfxml_actor_kill___time);
789
790   switch (A_surfxml_actor_on___failure) {
791   case AU_surfxml_actor_on___failure:
792   case A_surfxml_actor_on___failure_DIE:
793     actor.restart_on_failure = false;
794     break;
795   case A_surfxml_actor_on___failure_RESTART:
796     actor.restart_on_failure = true;
797     break;
798   default:
799     surf_parse_error("Invalid on failure behavior");
800   }
801
802   sg_platf_new_actor(&actor);
803 }
804
805 void STag_surfxml_argument(){
806   arguments.emplace_back(A_surfxml_argument_value);
807 }
808
809 void STag_surfxml_model___prop(){
810   XBT_INFO("Deprecated tag <model_prop> ignored");
811 }
812
813 void ETag_surfxml_prop(){/* Nothing to do */}
814 void STag_surfxml_random(){/* Nothing to do */}
815 void ETag_surfxml_random(){/* Nothing to do */}
816 void ETag_surfxml_trace___connect()
817 { /* Nothing to do */
818 }
819 void STag_surfxml_trace()
820 { /* Nothing to do */
821 }
822 void ETag_surfxml_router(){/*Nothing to do*/}
823 void ETag_surfxml_host___link(){/* Nothing to do */}
824 void ETag_surfxml_cabinet(){/* Nothing to do */}
825 void ETag_surfxml_peer(){/* Nothing to do */}
826 void STag_surfxml_backbone(){/* Nothing to do */}
827 void ETag_surfxml_link___ctn(){/* Nothing to do */}
828 void ETag_surfxml_argument(){/* Nothing to do */}
829 void ETag_surfxml_model___prop(){/* Nothing to do */}
830
831 /* Open and Close parse file */
832 YY_BUFFER_STATE surf_input_buffer;
833
834 void surf_parse_open(const std::string& file)
835 {
836   surf_parsed_filename = file;
837   std::string dir      = simgrid::xbt::Path(file).get_dir_name();
838   surf_path.push_back(dir);
839
840   surf_file_to_parse = surf_fopen(file, "r");
841   if (surf_file_to_parse == nullptr)
842     throw std::invalid_argument("Unable to open '" + file + "' from '" + simgrid::xbt::Path().get_name() +
843                                 "'. Does this file exist?");
844   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
845   surf_parse__switch_to_buffer(surf_input_buffer);
846   surf_parse_lineno = 1;
847 }
848
849 void surf_parse_close()
850 {
851   surf_path.pop_back(); // remove the dirname of the opened file, that was added in surf_parse_open()
852
853   if (surf_file_to_parse) {
854     surf_parse__delete_buffer(surf_input_buffer);
855     fclose(surf_file_to_parse);
856     surf_file_to_parse = nullptr; //Must be reset for Bypass
857   }
858 }
859
860 /* Call the lexer to parse the currently opened file */
861 void surf_parse()
862 {
863   bool err = surf_parse_lex();
864   surf_parse_assert(not err, "Flex returned an error code");
865 }