1 /* Copyright (c) 2006-2017. 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. */
6 #include "src/kernel/routing/NetZoneImpl.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/kernel/routing/NetPoint.hpp"
10 #include "src/surf/cpu_interface.hpp"
11 #include "src/surf/network_interface.hpp"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
23 explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
26 std::vector<surf::LinkImpl*> links;
29 NetZoneImpl::NetZoneImpl(NetZone* father, const char* name) : NetZone(father, name)
31 xbt_assert(nullptr == simgrid::s4u::Engine::instance()->netpointByNameOrNull(name),
32 "Refusing to create a second NetZone called '%s'.", name);
34 netpoint_ = new NetPoint(name, NetPoint::Type::NetZone, static_cast<NetZoneImpl*>(father));
35 XBT_DEBUG("NetZone '%s' created with the id '%d'", name, netpoint_->id());
37 NetZoneImpl::~NetZoneImpl()
39 for (auto& kv : bypassRoutes_)
42 simgrid::s4u::Engine::instance()->netpointUnregister(netpoint_);
45 simgrid::s4u::Host* NetZoneImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount,
46 std::unordered_map<std::string, std::string>* props)
48 simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
50 if (hierarchy_ == RoutingMode::unset)
51 hierarchy_ = RoutingMode::base;
53 res->pimpl_netpoint = new NetPoint(name, NetPoint::Type::Host, this);
55 surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
58 for (auto kv : *props)
59 res->setProperty(kv.first.c_str(), kv.second.c_str());
61 simgrid::s4u::Host::onCreation(*res); // notify the signal
66 void NetZoneImpl::addBypassRoute(sg_platf_route_cbarg_t e_route)
68 /* Argument validity checks */
69 if (e_route->gw_dst) {
70 XBT_DEBUG("Load bypassNetzoneRoute from %s@%s to %s@%s", e_route->src->cname(), e_route->gw_src->cname(),
71 e_route->dst->cname(), e_route->gw_dst->cname());
72 xbt_assert(not e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
73 e_route->src->cname(), e_route->gw_src->cname(), e_route->dst->cname(), e_route->gw_dst->cname());
74 xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
75 "The bypass route between %s@%s and %s@%s already exists.", e_route->src->cname(),
76 e_route->gw_src->cname(), e_route->dst->cname(), e_route->gw_dst->cname());
78 XBT_DEBUG("Load bypassRoute from %s to %s", e_route->src->cname(), e_route->dst->cname());
79 xbt_assert(not e_route->link_list->empty(), "Bypass route between %s and %s cannot be empty.",
80 e_route->src->cname(), e_route->dst->cname());
81 xbt_assert(bypassRoutes_.find({e_route->src, e_route->dst}) == bypassRoutes_.end(),
82 "The bypass route between %s and %s already exists.", e_route->src->cname(), e_route->dst->cname());
85 /* Build a copy that will be stored in the dict */
86 kernel::routing::BypassRoute* newRoute = new kernel::routing::BypassRoute(e_route->gw_src, e_route->gw_dst);
87 for (auto link : *e_route->link_list)
88 newRoute->links.push_back(link);
91 bypassRoutes_.insert({{e_route->src, e_route->dst}, newRoute});
94 /** @brief Get the common ancestor and its first children in each line leading to src and dst
96 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
100 * ... <- possibly long path
105 * / \ <- direct links
108 * src_ancestor dst_ancestor <- must be different in the recursive case
110 * ... ... <-- possibly long paths (one hop or more)
115 * In the base case (when src and dst are in the same netzone), things are as follows:
119 * ... <-- possibly long path
121 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
123 * / \ <-- direct links (exactly one hop)
128 * A specific recursive case occurs when src is the ancestor of dst. In this case,
129 * the base case routing should be used so the common_ancestor is specifically set
130 * to src_ancestor==dst_ancestor.
131 * Naturally, things are completely symmetrical if dst is the ancestor of src.
135 * ... <-- possibly long path
137 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
139 * ... <-- possibly long path (one hop or more)
144 static void find_common_ancestors(NetPoint* src, NetPoint* dst,
145 /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
146 NetZoneImpl** dst_ancestor)
148 /* Deal with the easy base case */
149 if (src->netzone() == dst->netzone()) {
150 *common_ancestor = src->netzone();
151 *src_ancestor = *common_ancestor;
152 *dst_ancestor = *common_ancestor;
156 /* engage the full recursive search */
158 /* (1) find the path to root of src and dst*/
159 NetZoneImpl* src_as = src->netzone();
160 NetZoneImpl* dst_as = dst->netzone();
162 xbt_assert(src_as, "Host %s must be in a netzone", src->cname());
163 xbt_assert(dst_as, "Host %s must be in a netzone", dst->cname());
165 /* (2) find the path to the root routing component */
166 std::vector<NetZoneImpl*> path_src;
167 NetZoneImpl* current = src->netzone();
168 while (current != nullptr) {
169 path_src.push_back(current);
170 current = static_cast<NetZoneImpl*>(current->father());
172 std::vector<NetZoneImpl*> path_dst;
173 current = dst->netzone();
174 while (current != nullptr) {
175 path_dst.push_back(current);
176 current = static_cast<NetZoneImpl*>(current->father());
179 /* (3) find the common father.
180 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
181 * So we move them down simultaneously as long as they point to the same content.
183 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
185 NetZoneImpl* father = nullptr; // the netzone we dropped on the previous loop iteration
186 while (path_src.size() > 1 && path_dst.size() > 1 &&
187 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
188 father = path_src.at(path_src.size() - 1);
193 /* (4) we found the difference at least. Finalize the returned values */
194 *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
195 *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
196 if (*src_ancestor == *dst_ancestor) { // src is the ancestor of dst, or the contrary
197 *common_ancestor = *src_ancestor;
199 *common_ancestor = father;
203 /* PRECONDITION: this is the common ancestor of src and dst */
204 bool NetZoneImpl::getBypassRoute(routing::NetPoint* src, routing::NetPoint* dst,
205 /* OUT */ std::vector<surf::LinkImpl*>* links, double* latency)
207 // If never set a bypass route return nullptr without any further computations
208 if (bypassRoutes_.empty())
211 /* Base case, no recursion is needed */
212 if (dst->netzone() == this && src->netzone() == this) {
213 if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
214 BypassRoute* bypassedRoute = bypassRoutes_.at({src, dst});
215 for (surf::LinkImpl* link : bypassedRoute->links) {
216 links->push_back(link);
218 *latency += link->latency();
220 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->cname(), dst->cname(),
221 bypassedRoute->links.size());
227 /* Engage recursive search */
229 /* (1) find the path to the root routing component */
230 std::vector<NetZoneImpl*> path_src;
231 NetZone* current = src->netzone();
232 while (current != nullptr) {
233 path_src.push_back(static_cast<NetZoneImpl*>(current));
234 current = current->father_;
237 std::vector<NetZoneImpl*> path_dst;
238 current = dst->netzone();
239 while (current != nullptr) {
240 path_dst.push_back(static_cast<NetZoneImpl*>(current));
241 current = current->father_;
244 /* (2) find the common father */
245 while (path_src.size() > 1 && path_dst.size() > 1 &&
246 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
251 int max_index_src = path_src.size() - 1;
252 int max_index_dst = path_dst.size() - 1;
254 int max_index = std::max(max_index_src, max_index_dst);
256 /* (3) Search for a bypass making the path up to the ancestor useless */
257 BypassRoute* bypassedRoute = nullptr;
258 std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*> key;
259 for (int max = 0; max <= max_index; max++) {
260 for (int i = 0; i < max; i++) {
261 if (i <= max_index_src && max <= max_index_dst) {
262 key = {path_src.at(i)->netpoint_, path_dst.at(max)->netpoint_};
263 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
264 bypassedRoute = bypassRoutes_.at(key);
268 if (max <= max_index_src && i <= max_index_dst) {
269 key = {path_src.at(max)->netpoint_, path_dst.at(i)->netpoint_};
270 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
271 bypassedRoute = bypassRoutes_.at(key);
280 if (max <= max_index_src && max <= max_index_dst) {
281 key = {path_src.at(max)->netpoint_, path_dst.at(max)->netpoint_};
282 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
283 bypassedRoute = bypassRoutes_.at(key);
289 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
291 XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
293 src->cname(), dst->cname(), bypassedRoute->links.size());
294 if (src != key.first)
295 getGlobalRoute(src, bypassedRoute->gw_src, links, latency);
296 for (surf::LinkImpl* link : bypassedRoute->links) {
297 links->push_back(link);
299 *latency += link->latency();
301 if (dst != key.second)
302 getGlobalRoute(bypassedRoute->gw_dst, dst, links, latency);
305 XBT_DEBUG("No bypass route from '%s' to '%s'.", src->cname(), dst->cname());
309 void NetZoneImpl::getGlobalRoute(routing::NetPoint* src, routing::NetPoint* dst,
310 /* OUT */ std::vector<surf::LinkImpl*>* links, double* latency)
312 s_sg_platf_route_cbarg_t route;
313 memset(&route, 0, sizeof(route));
315 XBT_DEBUG("Resolve route from '%s' to '%s'", src->cname(), dst->cname());
317 /* Find how src and dst are interconnected */
318 NetZoneImpl *common_ancestor;
319 NetZoneImpl *src_ancestor;
320 NetZoneImpl *dst_ancestor;
321 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
322 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'", common_ancestor->name(),
323 src_ancestor->name(), dst_ancestor->name());
325 /* Check whether a direct bypass is defined. If so, use it and bail out */
326 if (common_ancestor->getBypassRoute(src, dst, links, latency))
329 /* If src and dst are in the same netzone, life is good */
330 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
331 route.link_list = links;
332 common_ancestor->getLocalRoute(src, dst, &route, latency);
336 /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
338 route.link_list = new std::vector<surf::LinkImpl*>();
340 common_ancestor->getLocalRoute(src_ancestor->netpoint_, dst_ancestor->netpoint_, &route, latency);
341 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
342 src->cname(), dst->cname());
344 /* If source gateway is not our source, we have to recursively find our way up to this point */
345 if (src != route.gw_src)
346 getGlobalRoute(src, route.gw_src, links, latency);
347 for (auto link : *route.link_list)
348 links->push_back(link);
349 delete route.link_list;
351 /* If dest gateway is not our destination, we have to recursively find our way from this point */
352 if (route.gw_dst != dst)
353 getGlobalRoute(route.gw_dst, dst, links, latency);