1 /* Copyright (c) 2006-2016. 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. */
8 #include "simgrid/s4u/host.hpp"
9 #include "src/kernel/routing/AsImpl.hpp"
10 #include "src/kernel/routing/BypassRoute.hpp"
11 #include "src/kernel/routing/NetCard.hpp"
12 #include "src/surf/cpu_interface.hpp"
13 #include "src/surf/network_interface.hpp"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
21 AsImpl::AsImpl(As* father, const char* name) : As(father, name)
23 xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL),
24 "Refusing to create a second AS called '%s'.", name);
26 netcard_ = new NetCard(name, NetCard::Type::As, static_cast<AsImpl*>(father));
27 xbt_lib_set(as_router_lib, name, ROUTING_ASR_LEVEL, static_cast<void*>(netcard_));
28 XBT_DEBUG("AS '%s' created with the id '%d'", name, netcard_->id());
31 simgrid::s4u::Host* AsImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount)
33 simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
35 if (hierarchy_ == RoutingMode::unset)
36 hierarchy_ = RoutingMode::base;
38 res->pimpl_netcard = new NetCard(name, NetCard::Type::Host, this);
40 surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
45 /** @brief Get the common ancestor and its first children in each line leading to src and dst
47 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
51 * ... <- possibly long path
59 * src_ancestor dst_ancestor <- must be different in the recursive case
61 * ... ... <-- possibly long pathes (one hop or more)
66 * In the base case (when src and dst are in the same AS), things are as follows:
70 * ... <-- possibly long path
72 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
74 * / \ <-- direct links (exactly one hop)
79 * A specific recursive case occurs when src is the ancestor of dst. In this case,
80 * the base case routing should be used so the common_ancestor is specifically set
81 * to src_ancestor==dst_ancestor.
82 * Naturally, things are completely symmetrical if dst is the ancestor of src.
86 * ... <-- possibly long path
88 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
90 * ... <-- possibly long path (one hop or more)
95 static void find_common_ancestors(NetCard* src, NetCard* dst,
96 /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
98 /* Deal with the easy base case */
99 if (src->containingAS() == dst->containingAS()) {
100 *common_ancestor = src->containingAS();
101 *src_ancestor = *common_ancestor;
102 *dst_ancestor = *common_ancestor;
106 /* engage the full recursive search */
108 /* (1) find the path to root of src and dst*/
109 AsImpl* src_as = src->containingAS();
110 AsImpl* dst_as = dst->containingAS();
112 xbt_assert(src_as, "Host %s must be in an AS", src->cname());
113 xbt_assert(dst_as, "Host %s must be in an AS", dst->cname());
115 /* (2) find the path to the root routing component */
116 std::vector<AsImpl*> path_src;
117 AsImpl* current = src->containingAS();
118 while (current != nullptr) {
119 path_src.push_back(current);
120 current = static_cast<AsImpl*>(current->father());
122 std::vector<AsImpl*> path_dst;
123 current = dst->containingAS();
124 while (current != nullptr) {
125 path_dst.push_back(current);
126 current = static_cast<AsImpl*>(current->father());
129 /* (3) find the common father.
130 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
131 * So we move them down simultaneously as long as they point to the same content.
133 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
135 AsImpl* father = nullptr; // the AS we dropped on the previous loop iteration
136 while (path_src.size() > 1 && path_dst.size() > 1 &&
137 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
138 father = path_src.at(path_src.size() - 1);
143 /* (4) we found the difference at least. Finalize the returned values */
144 *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
145 *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
146 if (*src_ancestor == *dst_ancestor) { // src is the ancestor of dst, or the contrary
147 *common_ancestor = *src_ancestor;
149 *common_ancestor = father;
153 /* PRECONDITION: this is the common ancestor of src and dst */
154 bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
155 /* OUT */ std::vector<surf::Link*>* links, double* latency)
157 // If never set a bypass route return nullptr without any further computations
158 if (bypassRoutes_.empty())
161 /* Base case, no recursion is needed */
162 if (dst->containingAS() == this && src->containingAS() == this) {
163 if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
164 BypassRoute* bypassedRoute = bypassRoutes_.at({src, dst});
165 for (surf::Link* link : bypassedRoute->links) {
166 links->push_back(link);
168 *latency += link->latency();
170 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->cname(), dst->cname(),
171 bypassedRoute->links.size());
177 /* Engage recursive search */
180 /* (1) find the path to the root routing component */
181 std::vector<AsImpl*> path_src;
182 As* current = src->containingAS();
183 while (current != nullptr) {
184 path_src.push_back(static_cast<AsImpl*>(current));
185 current = current->father_;
188 std::vector<AsImpl*> path_dst;
189 current = dst->containingAS();
190 while (current != nullptr) {
191 path_dst.push_back(static_cast<AsImpl*>(current));
192 current = current->father_;
195 /* (2) find the common father */
196 while (path_src.size() > 1 && path_dst.size() > 1 &&
197 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
202 int max_index_src = path_src.size() - 1;
203 int max_index_dst = path_dst.size() - 1;
205 int max_index = std::max(max_index_src, max_index_dst);
207 /* (3) Search for a bypass making the path up to the ancestor useless */
208 BypassRoute* bypassedRoute = nullptr;
209 std::pair<kernel::routing::NetCard*, kernel::routing::NetCard*> key;
210 for (int max = 0; max <= max_index; max++) {
211 for (int i = 0; i < max; i++) {
212 if (i <= max_index_src && max <= max_index_dst) {
213 key = {path_src.at(i)->netcard_, path_dst.at(max)->netcard_};
214 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
215 bypassedRoute = bypassRoutes_.at(key);
219 if (max <= max_index_src && i <= max_index_dst) {
220 key = {path_src.at(max)->netcard_, path_dst.at(i)->netcard_};
221 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
222 bypassedRoute = bypassRoutes_.at(key);
231 if (max <= max_index_src && max <= max_index_dst) {
232 key = {path_src.at(max)->netcard_, path_dst.at(max)->netcard_};
233 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
234 bypassedRoute = bypassRoutes_.at(key);
240 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
242 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
244 src->cname(), dst->cname(), bypassedRoute->links.size());
245 if (src != key.first)
246 getGlobalRoute(src, const_cast<NetCard*>(bypassedRoute->gw_src), links, latency);
247 for (surf::Link* link : bypassedRoute->links) {
248 links->push_back(link);
250 *latency += link->latency();
252 if (dst != key.second)
253 getGlobalRoute(const_cast<NetCard*>(bypassedRoute->gw_dst), dst, links, latency);
256 XBT_DEBUG("No bypass route from '%s' to '%s'.", src->cname(), dst->cname());
260 void AsImpl::getGlobalRoute(routing::NetCard* src, routing::NetCard* dst,
261 /* OUT */ std::vector<surf::Link*>* links, double* latency)
263 s_sg_platf_route_cbarg_t route;
264 memset(&route,0,sizeof(route));
266 XBT_DEBUG("Resolve route from '%s' to '%s'", src->cname(), dst->cname());
268 /* Find how src and dst are interconnected */
269 AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
270 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
271 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
272 common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
274 /* Check whether a direct bypass is defined. If so, use it and bail out */
275 if (common_ancestor->getBypassRoute(src, dst, links, latency))
278 /* If src and dst are in the same AS, life is good */
279 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
280 route.link_list = links;
281 common_ancestor->getLocalRoute(src, dst, &route, latency);
285 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
287 route.link_list = new std::vector<surf::Link*>();
289 common_ancestor->getLocalRoute(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
290 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
291 src->cname(), dst->cname());
293 /* If source gateway is not our source, we have to recursively find our way up to this point */
294 if (src != route.gw_src)
295 getGlobalRoute(src, route.gw_src, links, latency);
296 for (auto link: *route.link_list)
297 links->push_back(link);
298 delete route.link_list;
300 /* If dest gateway is not our destination, we have to recursively find our way from this point */
301 if (route.gw_dst != dst)
302 getGlobalRoute(route.gw_dst, dst, links, latency);