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/NetCard.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include "src/surf/network_interface.hpp"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
20 AsImpl::AsImpl(As* father, const char* name) : As(father, name)
22 xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL),
23 "Refusing to create a second AS called '%s'.", name);
25 netcard_ = new NetCard(name, NetCard::Type::As, static_cast<AsImpl*>(father));
26 xbt_lib_set(as_router_lib, name, ROUTING_ASR_LEVEL, static_cast<void*>(netcard_));
27 XBT_DEBUG("AS '%s' created with the id '%d'", name, netcard_->id());
30 simgrid::s4u::Host* AsImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount)
32 simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
34 if (hierarchy_ == RoutingMode::unset)
35 hierarchy_ = RoutingMode::base;
37 res->pimpl_netcard = new NetCard(name, NetCard::Type::Host, this);
39 surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
44 /** @brief Get the common ancestor and its first children in each line leading to src and dst
46 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
50 * ... <- possibly long path
58 * src_ancestor dst_ancestor <- must be different in the recursive case
60 * ... ... <-- possibly long pathes (one hop or more)
65 * In the base case (when src and dst are in the same AS), things are as follows:
69 * ... <-- possibly long path
71 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
73 * / \ <-- direct links (exactly one hop)
78 * A specific recursive case occurs when src is the ancestor of dst. In this case,
79 * the base case routing should be used so the common_ancestor is specifically set
80 * to src_ancestor==dst_ancestor.
81 * Naturally, things are completely symmetrical if dst is the ancestor of src.
85 * ... <-- possibly long path
87 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
89 * ... <-- possibly long path (one hop or more)
94 static void find_common_ancestors(NetCard* src, NetCard* dst,
95 /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
97 /* Deal with the easy base case */
98 if (src->containingAS() == dst->containingAS()) {
99 *common_ancestor = src->containingAS();
100 *src_ancestor = *common_ancestor;
101 *dst_ancestor = *common_ancestor;
105 /* engage the full recursive search */
107 /* (1) find the path to root of src and dst*/
108 AsImpl* src_as = src->containingAS();
109 AsImpl* dst_as = dst->containingAS();
111 xbt_assert(src_as, "Host %s must be in an AS", src->cname());
112 xbt_assert(dst_as, "Host %s must be in an AS", dst->cname());
114 /* (2) find the path to the root routing component */
115 std::vector<AsImpl*> path_src;
116 AsImpl* current = src->containingAS();
117 while (current != nullptr) {
118 path_src.push_back(current);
119 current = static_cast<AsImpl*>(current->father());
121 std::vector<AsImpl*> path_dst;
122 current = dst->containingAS();
123 while (current != nullptr) {
124 path_dst.push_back(current);
125 current = static_cast<AsImpl*>(current->father());
128 /* (3) find the common father.
129 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
130 * So we move them down simultaneously as long as they point to the same content.
132 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
134 AsImpl* father = nullptr; // the AS we dropped on the previous loop iteration
135 while (path_src.size() > 1 && path_dst.size() > 1 &&
136 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
137 father = path_src.at(path_src.size() - 1);
142 /* (4) we found the difference at least. Finalize the returned values */
143 *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
144 *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
145 if (*src_ancestor == *dst_ancestor) { // src is the ancestor of dst, or the contrary
146 *common_ancestor = *src_ancestor;
148 *common_ancestor = father;
152 /* PRECONDITION: this is the common ancestor of src and dst */
153 bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
154 /* OUT */ std::vector<surf::Link*>* links, double* latency)
156 // If never set a bypass route return nullptr without any further computations
157 if (bypassRoutes_.empty())
160 /* Base case, no recursion is needed */
161 if (dst->containingAS() == this && src->containingAS() == this) {
162 if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
163 AsRoute* bypassedRoute = bypassRoutes_.at({src, dst});
164 for (surf::Link* link : bypassedRoute->links) {
165 links->push_back(link);
167 *latency += link->latency();
169 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->cname(), dst->cname(),
170 bypassedRoute->links.size());
176 /* Engage recursive search */
179 /* (1) find the path to the root routing component */
180 std::vector<AsImpl*> path_src;
181 As* current = src->containingAS();
182 while (current != nullptr) {
183 path_src.push_back(static_cast<AsImpl*>(current));
184 current = current->father_;
187 std::vector<AsImpl*> path_dst;
188 current = dst->containingAS();
189 while (current != nullptr) {
190 path_dst.push_back(static_cast<AsImpl*>(current));
191 current = current->father_;
194 /* (2) find the common father */
195 while (path_src.size() > 1 && path_dst.size() > 1 &&
196 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
201 int max_index_src = path_src.size() - 1;
202 int max_index_dst = path_dst.size() - 1;
204 int max_index = std::max(max_index_src, max_index_dst);
206 /* (3) Search for a bypass making the path up to the ancestor useless */
207 AsRoute* bypassedRoute = nullptr;
208 std::pair<kernel::routing::NetCard*, kernel::routing::NetCard*> key;
209 for (int max = 0; max <= max_index; max++) {
210 for (int i = 0; i < max; i++) {
211 if (i <= max_index_src && max <= max_index_dst) {
212 key = {path_src.at(i)->netcard_, path_dst.at(max)->netcard_};
213 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
214 bypassedRoute = bypassRoutes_.at(key);
218 if (max <= max_index_src && i <= max_index_dst) {
219 key = {path_src.at(max)->netcard_, path_dst.at(i)->netcard_};
220 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
221 bypassedRoute = bypassRoutes_.at(key);
230 if (max <= max_index_src && max <= max_index_dst) {
231 key = {path_src.at(max)->netcard_, path_dst.at(max)->netcard_};
232 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
233 bypassedRoute = bypassRoutes_.at(key);
239 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
241 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
243 src->cname(), dst->cname(), bypassedRoute->links.size());
244 if (src != key.first)
245 getGlobalRoute(src, const_cast<NetCard*>(bypassedRoute->gw_src), links, latency);
246 for (surf::Link* link : bypassedRoute->links) {
247 links->push_back(link);
249 *latency += link->latency();
251 if (dst != key.second)
252 getGlobalRoute(const_cast<NetCard*>(bypassedRoute->gw_dst), dst, links, latency);
255 XBT_DEBUG("No bypass route from '%s' to '%s'.", src->cname(), dst->cname());
259 void AsImpl::getGlobalRoute(routing::NetCard* src, routing::NetCard* dst,
260 /* OUT */ std::vector<surf::Link*>* links, double* latency)
262 s_sg_platf_route_cbarg_t route;
263 memset(&route,0,sizeof(route));
265 XBT_DEBUG("Resolve route from '%s' to '%s'", src->cname(), dst->cname());
267 /* Find how src and dst are interconnected */
268 AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
269 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
270 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
271 common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
273 /* Check whether a direct bypass is defined. If so, use it and bail out */
274 if (common_ancestor->getBypassRoute(src, dst, links, latency))
277 /* If src and dst are in the same AS, life is good */
278 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
279 route.link_list = links;
280 common_ancestor->getLocalRoute(src, dst, &route, latency);
284 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
286 route.link_list = new std::vector<surf::Link*>();
288 common_ancestor->getLocalRoute(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
289 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
290 src->cname(), dst->cname());
292 /* If source gateway is not our source, we have to recursively find our way up to this point */
293 if (src != route.gw_src)
294 getGlobalRoute(src, route.gw_src, links, latency);
295 for (auto link: *route.link_list)
296 links->push_back(link);
297 delete route.link_list;
299 /* If dest gateway is not our destination, we have to recursively find our way from this point */
300 if (route.gw_dst != dst)
301 getGlobalRoute(route.gw_dst, dst, links, latency);