Logo AND Algorithmique Numérique Distribuée

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