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/NetCard.hpp"
10 #include "src/kernel/routing/NetZoneImpl.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include "src/surf/network_interface.hpp"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
22 explicit BypassRoute(NetCard* gwSrc, NetCard* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
23 const NetCard* gw_src;
24 const NetCard* gw_dst;
25 std::vector<Link*> links;
28 NetZoneImpl::NetZoneImpl(NetZone* father, const char* name) : NetZone(father, name)
30 xbt_assert(nullptr == xbt_dict_get_or_null(netcards_dict, name), "Refusing to create a second NetZone called '%s'.",
33 netcard_ = new NetCard(name, NetCard::Type::NetZone, static_cast<NetZoneImpl*>(father));
34 xbt_dict_set(netcards_dict, name, static_cast<void*>(netcard_), nullptr);
35 XBT_DEBUG("NetZone '%s' created with the id '%d'", name, netcard_->id());
37 NetZoneImpl::~NetZoneImpl()
39 for (auto& kv : bypassRoutes_)
43 simgrid::s4u::Host* NetZoneImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount)
45 simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
47 if (hierarchy_ == RoutingMode::unset)
48 hierarchy_ = RoutingMode::base;
50 res->pimpl_netcard = new NetCard(name, NetCard::Type::Host, this);
52 surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
57 void NetZoneImpl::addBypassRoute(sg_platf_route_cbarg_t e_route)
59 /* Argument validity checks */
60 if (e_route->gw_dst) {
61 XBT_DEBUG("Load bypassASroute from %s@%s to %s@%s", e_route->src->cname(), e_route->gw_src->cname(),
62 e_route->dst->cname(), e_route->gw_dst->cname());
63 xbt_assert(!e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
64 e_route->src->cname(), e_route->gw_src->cname(), e_route->dst->cname(), e_route->gw_dst->cname());
65 xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
66 "The bypass route between %s@%s and %s@%s already exists.", e_route->src->cname(),
67 e_route->gw_src->cname(), e_route->dst->cname(), e_route->gw_dst->cname());
69 XBT_DEBUG("Load bypassRoute from %s to %s", e_route->src->cname(), e_route->dst->cname());
70 xbt_assert(!e_route->link_list->empty(), "Bypass route between %s and %s cannot be empty.", e_route->src->cname(),
71 e_route->dst->cname());
72 xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
73 "The bypass route between %s and %s already exists.", e_route->src->cname(), e_route->dst->cname());
76 /* Build a copy that will be stored in the dict */
77 kernel::routing::BypassRoute* newRoute = new kernel::routing::BypassRoute(e_route->gw_src, e_route->gw_dst);
78 for (auto link : *e_route->link_list)
79 newRoute->links.push_back(link);
82 bypassRoutes_.insert({{e_route->src, e_route->dst}, newRoute});
85 /** @brief Get the common ancestor and its first children in each line leading to src and dst
87 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
91 * ... <- possibly long path
99 * src_ancestor dst_ancestor <- must be different in the recursive case
101 * ... ... <-- possibly long paths (one hop or more)
106 * In the base case (when src and dst are in the same AS), things are as follows:
110 * ... <-- possibly long path
112 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
114 * / \ <-- direct links (exactly one hop)
119 * A specific recursive case occurs when src is the ancestor of dst. In this case,
120 * the base case routing should be used so the common_ancestor is specifically set
121 * to src_ancestor==dst_ancestor.
122 * Naturally, things are completely symmetrical if dst is the ancestor of src.
126 * ... <-- possibly long path
128 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
130 * ... <-- possibly long path (one hop or more)
135 static void find_common_ancestors(NetCard* src, NetCard* dst,
136 /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
137 NetZoneImpl** dst_ancestor)
139 /* Deal with the easy base case */
140 if (src->netzone() == dst->netzone()) {
141 *common_ancestor = src->netzone();
142 *src_ancestor = *common_ancestor;
143 *dst_ancestor = *common_ancestor;
147 /* engage the full recursive search */
149 /* (1) find the path to root of src and dst*/
150 NetZoneImpl* src_as = src->netzone();
151 NetZoneImpl* dst_as = dst->netzone();
153 xbt_assert(src_as, "Host %s must be in an AS", src->cname());
154 xbt_assert(dst_as, "Host %s must be in an AS", dst->cname());
156 /* (2) find the path to the root routing component */
157 std::vector<NetZoneImpl*> path_src;
158 NetZoneImpl* current = src->netzone();
159 while (current != nullptr) {
160 path_src.push_back(current);
161 current = static_cast<NetZoneImpl*>(current->father());
163 std::vector<NetZoneImpl*> path_dst;
164 current = dst->netzone();
165 while (current != nullptr) {
166 path_dst.push_back(current);
167 current = static_cast<NetZoneImpl*>(current->father());
170 /* (3) find the common father.
171 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
172 * So we move them down simultaneously as long as they point to the same content.
174 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
176 NetZoneImpl* father = nullptr; // the netzone we dropped on the previous loop iteration
177 while (path_src.size() > 1 && path_dst.size() > 1 &&
178 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
179 father = path_src.at(path_src.size() - 1);
184 /* (4) we found the difference at least. Finalize the returned values */
185 *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
186 *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
187 if (*src_ancestor == *dst_ancestor) { // src is the ancestor of dst, or the contrary
188 *common_ancestor = *src_ancestor;
190 *common_ancestor = father;
194 /* PRECONDITION: this is the common ancestor of src and dst */
195 bool NetZoneImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
196 /* OUT */ std::vector<surf::Link*>* links, double* latency)
198 // If never set a bypass route return nullptr without any further computations
199 if (bypassRoutes_.empty())
202 /* Base case, no recursion is needed */
203 if (dst->netzone() == this && src->netzone() == this) {
204 if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
205 BypassRoute* bypassedRoute = bypassRoutes_.at({src, dst});
206 for (surf::Link* link : bypassedRoute->links) {
207 links->push_back(link);
209 *latency += link->latency();
211 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->cname(), dst->cname(),
212 bypassedRoute->links.size());
218 /* Engage recursive search */
220 /* (1) find the path to the root routing component */
221 std::vector<NetZoneImpl*> path_src;
222 NetZone* current = src->netzone();
223 while (current != nullptr) {
224 path_src.push_back(static_cast<NetZoneImpl*>(current));
225 current = current->father_;
228 std::vector<NetZoneImpl*> path_dst;
229 current = dst->netzone();
230 while (current != nullptr) {
231 path_dst.push_back(static_cast<NetZoneImpl*>(current));
232 current = current->father_;
235 /* (2) find the common father */
236 while (path_src.size() > 1 && path_dst.size() > 1 &&
237 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
242 int max_index_src = path_src.size() - 1;
243 int max_index_dst = path_dst.size() - 1;
245 int max_index = std::max(max_index_src, max_index_dst);
247 /* (3) Search for a bypass making the path up to the ancestor useless */
248 BypassRoute* bypassedRoute = nullptr;
249 std::pair<kernel::routing::NetCard*, kernel::routing::NetCard*> key;
250 for (int max = 0; max <= max_index; max++) {
251 for (int i = 0; i < max; i++) {
252 if (i <= max_index_src && max <= max_index_dst) {
253 key = {path_src.at(i)->netcard_, path_dst.at(max)->netcard_};
254 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
255 bypassedRoute = bypassRoutes_.at(key);
259 if (max <= max_index_src && i <= max_index_dst) {
260 key = {path_src.at(max)->netcard_, path_dst.at(i)->netcard_};
261 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
262 bypassedRoute = bypassRoutes_.at(key);
271 if (max <= max_index_src && max <= max_index_dst) {
272 key = {path_src.at(max)->netcard_, path_dst.at(max)->netcard_};
273 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
274 bypassedRoute = bypassRoutes_.at(key);
280 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
282 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
284 src->cname(), dst->cname(), bypassedRoute->links.size());
285 if (src != key.first)
286 getGlobalRoute(src, const_cast<NetCard*>(bypassedRoute->gw_src), links, latency);
287 for (surf::Link* link : bypassedRoute->links) {
288 links->push_back(link);
290 *latency += link->latency();
292 if (dst != key.second)
293 getGlobalRoute(const_cast<NetCard*>(bypassedRoute->gw_dst), dst, links, latency);
296 XBT_DEBUG("No bypass route from '%s' to '%s'.", src->cname(), dst->cname());
300 void NetZoneImpl::getGlobalRoute(routing::NetCard* src, routing::NetCard* dst,
301 /* OUT */ std::vector<surf::Link*>* links, double* latency)
303 s_sg_platf_route_cbarg_t route;
304 memset(&route, 0, sizeof(route));
306 XBT_DEBUG("Resolve route from '%s' to '%s'", src->cname(), dst->cname());
308 /* Find how src and dst are interconnected */
309 NetZoneImpl *common_ancestor, *src_ancestor, *dst_ancestor;
310 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
311 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'", common_ancestor->name(),
312 src_ancestor->name(), dst_ancestor->name());
314 /* Check whether a direct bypass is defined. If so, use it and bail out */
315 if (common_ancestor->getBypassRoute(src, dst, links, latency))
318 /* If src and dst are in the same AS, life is good */
319 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
320 route.link_list = links;
321 common_ancestor->getLocalRoute(src, dst, &route, latency);
325 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
327 route.link_list = new std::vector<surf::Link*>();
329 common_ancestor->getLocalRoute(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
330 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
331 src->cname(), dst->cname());
333 /* If source gateway is not our source, we have to recursively find our way up to this point */
334 if (src != route.gw_src)
335 getGlobalRoute(src, route.gw_src, links, latency);
336 for (auto link : *route.link_list)
337 links->push_back(link);
338 delete route.link_list;
340 /* If dest gateway is not our destination, we have to recursively find our way from this point */
341 if (route.gw_dst != dst)
342 getGlobalRoute(route.gw_dst, dst, links, latency);