Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Please sonar, just a little
[simgrid.git] / src / kernel / routing / StarZone.cpp
1 /* Copyright (c) 2009-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/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
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_star, surf, "Routing part of surf");
13
14 namespace simgrid {
15 namespace kernel {
16 namespace routing {
17 StarZone::StarZone(const std::string& name) : NetZoneImpl(name) {}
18
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) const
21 {
22   for (auto* link : links) {
23     /* do not add duplicated links */
24     if (added_links.find(link) != added_links.end())
25       continue;
26     added_links.insert(link);
27     if (latency)
28       *latency += link->get_latency();
29     route->link_list.push_back(link);
30   }
31 }
32
33 void StarZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
34 {
35   XBT_VERB("StarZone getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
36            dst->id());
37
38   xbt_assert(((src == dst) and (routes_[src->id()].has_loopback())) or (routes_[src->id()].has_links_up()),
39              "StarZone routing (%s - %s): no link UP from source node. Did you use add_route() to set it?",
40              src->get_cname(), dst->get_cname());
41   xbt_assert(((src == dst) and (routes_[dst->id()].has_loopback())) or (routes_[dst->id()].has_links_down()),
42              "StarZone routing (%s - %s): no link DOWN to destination node. Did you use add_route() to set it?",
43              src->get_cname(), dst->get_cname());
44
45   std::unordered_set<resource::LinkImpl*> added_links;
46   /* loopback */
47   if ((src == dst) and (routes_[src->id()].has_loopback())) {
48     add_links_to_route(routes_[src->id()].loopback, route, latency, added_links);
49     return;
50   }
51
52   /* going UP */
53   add_links_to_route(routes_[src->id()].links_up, route, latency, added_links);
54
55   /* going DOWN */
56   add_links_to_route(routes_[dst->id()].links_down, route, latency, added_links);
57   /* gateways */
58   route->gw_src = routes_[src->id()].gateway;
59   route->gw_dst = routes_[dst->id()].gateway;
60 }
61
62 void StarZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
63                          std::map<std::string, xbt_edge_t, std::less<>>* edges)
64 {
65   xbt_node_t star_node = new_xbt_graph_node(graph, get_cname(), nodes);
66
67   for (auto const& src : get_vertices()) {
68     /* going up */
69     xbt_node_t src_node = new_xbt_graph_node(graph, src->get_cname(), nodes);
70     xbt_node_t previous = src_node;
71     for (auto const* link : routes_[src->id()].links_up) {
72       xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
73       new_xbt_graph_edge(graph, previous, current, edges);
74       previous = current;
75     }
76     new_xbt_graph_edge(graph, previous, star_node, edges);
77     /* going down */
78     previous = star_node;
79     for (auto const* link : routes_[src->id()].links_down) {
80       xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
81       new_xbt_graph_edge(graph, previous, current, edges);
82       previous = current;
83     }
84     new_xbt_graph_edge(graph, previous, src_node, edges);
85   }
86 }
87
88 void StarZone::check_add_route_param(const NetPoint* src, const NetPoint* dst, const NetPoint* gw_src,
89                                      const NetPoint* gw_dst, bool symmetrical) const
90 {
91   const char* src_name = src ? src->get_cname() : "nullptr";
92   const char* dst_name = dst ? dst->get_cname() : "nullptr";
93
94   xbt_assert((src == dst) or (not src and dst) or (src and not dst),
95              "Cannot add route from %s to %s. In a StarZone, route must be:  i) from source host to everyone, ii) from "
96              "everyone to a single host or iii) loopback, same source and destination",
97              src_name, dst_name);
98   xbt_assert((not symmetrical) or (symmetrical and src),
99              "Cannot add route from %s to %s. In a StarZone, symmetrical routes must be set from source to everyone "
100              "(not the contrary).",
101              src_name, dst_name);
102
103   if (src and src->is_netzone()) {
104     xbt_assert(gw_src, "add_route(): source %s is a netzone but gw_src isn't configured", src->get_cname());
105     xbt_assert(not gw_src->is_netzone(), "add_route(): src(%s) is a netzone, gw_src(%s) cannot be a netzone",
106                src->get_cname(), gw_src->get_cname());
107   }
108
109   if (dst and dst->is_netzone()) {
110     xbt_assert(gw_dst, "add_route(): destination %s is a netzone but gw_dst isn't configured", dst->get_cname());
111     xbt_assert(not gw_dst->is_netzone(), "add_route(): dst(%s) is a netzone, gw_dst(%s) cannot be a netzone",
112                dst->get_cname(), gw_dst->get_cname());
113   }
114 }
115
116 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
117                          const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical)
118 {
119   check_add_route_param(src, dst, gw_src, gw_dst, symmetrical);
120
121   s4u::NetZone::on_route_creation(symmetrical, gw_src, gw_dst, gw_src, gw_dst, link_list);
122
123   /* loopback */
124   if (src == dst) {
125     routes_[src->id()].loopback = link_list;
126   } else {
127     /* src to everyone */
128     if (src) {
129       routes_[src->id()].links_up     = link_list;
130       routes_[src->id()].gateway      = gw_src;
131       routes_[src->id()].links_up_set = true;
132       if (symmetrical) {
133         /* reverse it for down/symmetrical links */
134         routes_[src->id()].links_down.assign(link_list.rbegin(), link_list.rend());
135         routes_[src->id()].links_down_set = true;
136       }
137     }
138     /* dst to everyone */
139     if (dst) {
140       routes_[dst->id()].links_down     = link_list;
141       routes_[dst->id()].gateway        = gw_dst;
142       routes_[dst->id()].links_down_set = true;
143     }
144   }
145 }
146
147 void StarZone::do_seal()
148 {
149   /* add default empty links if nothing was configured by user */
150   for (auto const& node : get_vertices()) {
151     if (routes_.find(node->id()) == routes_.end()) {
152       routes_[node->id()]                = {};
153       routes_[node->id()].links_down_set = true;
154       routes_[node->id()].links_up_set   = true;
155     }
156   }
157 }
158
159 } // namespace routing
160 } // namespace kernel
161
162 namespace s4u {
163 NetZone* create_star_zone(const std::string& name)
164 {
165   return (new kernel::routing::StarZone(name))->get_iface();
166 }
167 } // namespace s4u
168
169 } // namespace simgrid