1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/kernel/routing/StarZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/kernel/routing/RoutedZone.hpp"
9 #include "src/surf/network_interface.hpp"
10 #include "src/surf/xml/platf_private.hpp" // RouteCreationArgs and friends
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_star, surf, "Routing part of surf");
17 StarZone::StarZone(const std::string& name) : NetZoneImpl(name) {}
19 void StarZone::add_links_to_route(const std::vector<resource::LinkImpl*>& links, RouteCreationArgs* route,
20 double* latency, std::unordered_set<resource::LinkImpl*>& added_links)
22 for (auto* link : links) {
23 /* do not add duplicated links */
24 if (added_links.find(link) != added_links.end())
26 added_links.insert(link);
28 *latency += link->get_latency();
29 route->link_list.push_back(link);
33 void StarZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
35 XBT_VERB("StarZone getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
38 xbt_assert(((src == dst) and (loopback_.find(src->id()) != loopback_.end())) or
39 (links_up_.find(src->id()) != links_up_.end()),
40 "StarZone routing (%s - %s): no link UP from source node. Did you use add_route() to set it?",
41 src->get_cname(), dst->get_cname());
42 xbt_assert(((src == dst) and (loopback_.find(dst->id()) != loopback_.end())) or
43 (links_down_.find(dst->id()) != links_down_.end()),
44 "StarZone routing (%s - %s): no link DOWN to destination node. Did you use add_route() to set it?",
45 src->get_cname(), dst->get_cname());
47 std::unordered_set<resource::LinkImpl*> added_links;
49 if ((src == dst) and (loopback_.find(src->id()) != loopback_.end())) {
50 add_links_to_route(loopback_[src->id()], route, latency, added_links);
55 add_links_to_route(links_up_[src->id()], route, latency, added_links);
58 add_links_to_route(links_down_[dst->id()], route, latency, added_links);
61 void StarZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
62 std::map<std::string, xbt_edge_t, std::less<>>* edges)
66 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
67 const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical)
69 const char* src_name = src ? src->get_cname() : "nullptr";
70 const char* dst_name = dst ? dst->get_cname() : "nullptr";
72 xbt_assert((src == dst) or (not src and dst) or (src and not dst),
73 "Cannot add route from %s to %s. In a StarZone, route must be: i) from source host to everyone, ii) from "
74 "everyone to a single host or iii) loopback, same source and destination",
76 xbt_assert((not symmetrical) or (symmetrical and src),
77 "Cannot add route from %s to %s. In a StarZone, symmetrical routes must be set from source to everyone "
78 "(not the contrary).",
83 loopback_[src->id()] = link_list;
87 links_up_[src->id()] = link_list;
89 links_down_[src->id()] = link_list;
90 /* reverse it for down/symmetrical links */
91 std::reverse(links_down_[src->id()].begin(), links_down_[src->id()].end());
96 links_down_[dst->id()] = link_list;
99 s4u::NetZone::on_route_creation(symmetrical, gw_src, gw_dst, gw_src, gw_dst, link_list);
102 void StarZone::do_seal()
104 /* add default empty links if nothing was configured by user */
105 for (auto const& node : get_vertices()) {
106 if ((links_up_.find(node->id()) == links_up_.end()) and (links_down_.find(node->id()) == links_down_.end())) {
107 links_down_[node->id()] = {};
108 links_up_[node->id()] = {};
113 } // namespace routing
114 } // namespace kernel
117 NetZone* create_star_zone(const std::string& name)
119 return (new kernel::routing::StarZone(name))->get_iface();
123 } // namespace simgrid