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/surf/cpu_interface.hpp"
11 #include "src/surf/network_interface.hpp" // Link FIXME: move to proper header
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(AsImpl,surf, "Implementation of S4U autonomous systems");
19 AsImpl::AsImpl(As* father, const char* name) : As(father, name)
21 xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL),
22 "Refusing to create a second AS called '%s'.", name);
24 netcard_ = new NetCardImpl(name, NetCard::Type::As, static_cast<AsImpl*>(father));
25 xbt_lib_set(as_router_lib, name, ROUTING_ASR_LEVEL, static_cast<void*>(netcard_));
26 XBT_DEBUG("AS '%s' created with the id '%d'", name, netcard_->id());
29 simgrid::s4u::Host* AsImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount)
31 simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
33 if (hierarchy_ == RoutingMode::unset)
34 hierarchy_ = RoutingMode::base;
36 res->pimpl_netcard = new NetCardImpl(name, NetCard::Type::Host, this);
38 surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
43 void AsImpl::getOneLinkRoutes(std::vector<Onelink*>* accumulator)
45 // recursing only. I have no route myself :)
47 xbt_dict_cursor_t cursor = nullptr;
49 xbt_dict_foreach (children(), cursor, key, rc_child) {
50 rc_child->getOneLinkRoutes(accumulator);
54 /** @brief Get the common ancestor and its first children in each line leading to src and dst
56 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
60 * ... <- possibly long path
68 * src_ancestor dst_ancestor <- must be different in the recursive case
70 * ... ... <-- possibly long pathes (one hop or more)
75 * In the base case (when src and dst are in the same AS), things are as follows:
79 * ... <-- possibly long path
81 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
83 * / \ <-- direct links (exactly one hop)
88 * A specific recursive case occurs when src is the ancestor of dst. In this case,
89 * the base case routing should be used so the common_ancestor is specifically set
90 * to src_ancestor==dst_ancestor.
91 * Naturally, things are completely symmetrical if dst is the ancestor of src.
95 * ... <-- possibly long path
97 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
99 * ... <-- possibly long path (one hop or more)
104 static void find_common_ancestors(NetCard* src, NetCard* dst,
105 /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
107 /* Deal with the easy base case */
108 if (src->containingAS() == dst->containingAS()) {
109 *common_ancestor = src->containingAS();
110 *src_ancestor = *common_ancestor;
111 *dst_ancestor = *common_ancestor;
115 /* engage the full recursive search */
117 /* (1) find the path to root of src and dst*/
118 AsImpl* src_as = src->containingAS();
119 AsImpl* dst_as = dst->containingAS();
121 xbt_assert(src_as, "Host %s must be in an AS", src->name().c_str());
122 xbt_assert(dst_as, "Host %s must be in an AS", dst->name().c_str());
124 /* (2) find the path to the root routing component */
125 std::vector<AsImpl*> path_src;
126 AsImpl* current = src->containingAS();
127 while (current != nullptr) {
128 path_src.push_back(current);
129 current = static_cast<AsImpl*>(current->father());
131 std::vector<AsImpl*> path_dst;
132 current = dst->containingAS();
133 while (current != nullptr) {
134 path_dst.push_back(current);
135 current = static_cast<AsImpl*>(current->father());
138 /* (3) find the common father.
139 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
140 * So we move them down simultaneously as long as they point to the same content.
142 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
144 AsImpl* father = nullptr; // the AS we dropped on the previous loop iteration
145 while (path_src.size() > 1 && path_dst.size() > 1 &&
146 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
147 father = path_src.at(path_src.size() - 1);
152 /* (4) we found the difference at least. Finalize the returned values */
153 *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
154 *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
155 if (*src_ancestor == *dst_ancestor) { // src is the ancestor of dst, or the contrary
156 *common_ancestor = *src_ancestor;
158 *common_ancestor = father;
162 /* PRECONDITION: this is the common ancestor of src and dst */
163 bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
164 /* OUT */ std::vector<surf::Link*>* links, double* latency)
166 // If never set a bypass route return nullptr without any further computations
167 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name().c_str(), dst->name().c_str());
168 if (bypassRoutes_.empty())
171 /* Base case, no recursion is needed */
172 if (dst->containingAS() == this && src->containingAS() == this) {
173 if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
174 AsRoute* bypassedRoute = bypassRoutes_.at({src, dst});
175 for (surf::Link* link : bypassedRoute->links) {
176 links->push_back(link);
178 *latency += link->latency();
180 XBT_DEBUG("Found a bypass route with %zu links", bypassedRoute->links.size());
186 /* Engage recursive search */
189 /* (1) find the path to the root routing component */
190 std::vector<AsImpl*> path_src;
191 As* current = src->containingAS();
192 while (current != nullptr) {
193 path_src.push_back(static_cast<AsImpl*>(current));
194 current = current->father_;
197 std::vector<AsImpl*> path_dst;
198 current = dst->containingAS();
199 while (current != nullptr) {
200 path_dst.push_back(static_cast<AsImpl*>(current));
201 current = current->father_;
204 /* (2) find the common father */
205 while (path_src.size() > 1 && path_dst.size() > 1 &&
206 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
211 int max_index_src = path_src.size() - 1;
212 int max_index_dst = path_dst.size() - 1;
214 int max_index = std::max(max_index_src, max_index_dst);
216 /* (3) Search for a bypass making the path up to the ancestor useless */
217 AsRoute* bypassedRoute = nullptr;
218 std::pair<kernel::routing::NetCard*, kernel::routing::NetCard*> key;
219 for (int max = 0; max <= max_index; max++) {
220 for (int i = 0; i < max; i++) {
221 if (i <= max_index_src && max <= max_index_dst) {
222 key = {path_src.at(i)->netcard_, path_dst.at(max)->netcard_};
223 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
224 bypassedRoute = bypassRoutes_.at(key);
228 if (max <= max_index_src && i <= max_index_dst) {
229 key = {path_src.at(max)->netcard_, path_dst.at(i)->netcard_};
230 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
231 bypassedRoute = bypassRoutes_.at(key);
240 if (max <= max_index_src && max <= max_index_dst) {
241 key = {path_src.at(max)->netcard_, path_dst.at(max)->netcard_};
242 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
243 bypassedRoute = bypassRoutes_.at(key);
249 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
251 if (src != key.first)
252 getRouteRecursive(src, const_cast<NetCard*>(bypassedRoute->gw_src), links, latency);
253 for (surf::Link* link : bypassedRoute->links) {
254 links->push_back(link);
256 *latency += link->latency();
258 if (dst != key.second)
259 getRouteRecursive(const_cast<NetCard*>(bypassedRoute->gw_dst), dst, links, latency);
266 * \brief Recursive function for getRouteAndLatency
268 * \param src the source host
269 * \param dst the destination host
270 * \param links Where to store the links and the gw information
271 * \param latency If not nullptr, the latency of all links will be added in it
273 void AsImpl::getRouteRecursive(routing::NetCard *src, routing::NetCard *dst,
274 /* OUT */ std::vector<surf::Link*> * links, double *latency)
276 s_sg_platf_route_cbarg_t route;
277 memset(&route,0,sizeof(route));
279 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name().c_str(), dst->name().c_str());
281 /* Find how src and dst are interconnected */
282 AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
283 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
284 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
285 common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
287 /* Check whether a direct bypass is defined. If so, use it and bail out */
288 if (common_ancestor->getBypassRoute(src, dst, links, latency))
291 /* If src and dst are in the same AS, life is good */
292 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
293 route.link_list = links;
294 common_ancestor->getLocalRoute(src, dst, &route, latency);
298 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
300 route.link_list = new std::vector<surf::Link*>();
302 common_ancestor->getLocalRoute(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
303 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
304 src->name().c_str(), dst->name().c_str());
306 /* If source gateway is not our source, we have to recursively find our way up to this point */
307 if (src != route.gw_src)
308 getRouteRecursive(src, route.gw_src, links, latency);
309 for (auto link: *route.link_list)
310 links->push_back(link);
311 delete route.link_list;
313 /* If dest gateway is not our destination, we have to recursively find our way from this point */
314 if (route.gw_dst != dst)
315 getRouteRecursive(route.gw_dst, dst, links, latency);