Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Mix create_host and add_route during platf creation.
[simgrid.git] / src / kernel / routing / FullZone.cpp
1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/kernel/routing/FullZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "src/surf/network_interface.hpp"
9 #include "surf/surf.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
12
13 namespace simgrid {
14 namespace kernel {
15 namespace routing {
16
17 void FullZone::check_routing_table()
18 {
19   unsigned int table_size = get_table_size();
20   /* assure routing_table is table_size X table_size */
21   if (routing_table_.size() != table_size) {
22     routing_table_.resize(table_size);
23     for (auto& j : routing_table_) {
24       j.resize(table_size);
25     }
26   }
27 }
28
29 void FullZone::do_seal()
30 {
31   check_routing_table();
32   /* Add the loopback if needed */
33   if (get_network_model()->loopback_ && get_hierarchy() == RoutingMode::base) {
34     for (unsigned int i = 0; i < get_table_size(); i++) {
35       auto& route = routing_table_[i][i];
36       if (not route) {
37         route.reset(new Route());
38         route->link_list_.push_back(get_network_model()->loopback_);
39       }
40     }
41   }
42 }
43
44 void FullZone::get_local_route(NetPoint* src, NetPoint* dst, Route* res, double* lat)
45 {
46   XBT_DEBUG("full getLocalRoute from %s[%u] to %s[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
47
48   const auto& e_route = routing_table_[src->id()][dst->id()];
49
50   if (e_route != nullptr) {
51     res->gw_src_ = e_route->gw_src_;
52     res->gw_dst_ = e_route->gw_dst_;
53     for (auto const& link : e_route->link_list_) {
54       res->link_list_.push_back(link);
55       if (lat)
56         *lat += link->get_latency();
57     }
58   }
59 }
60
61 void FullZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
62                          const std::vector<resource::LinkImpl*>& link_list, bool symmetrical)
63 {
64   add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
65
66   check_routing_table();
67
68   /* Check that the route does not already exist */
69   if (gw_dst && gw_src) // inter-zone route (to adapt the error message, if any)
70     xbt_assert(nullptr == routing_table_[src->id()][dst->id()],
71                "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
72                src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
73   else
74     xbt_assert(nullptr == routing_table_[src->id()][dst->id()],
75                "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
76                dst->get_cname());
77
78   /* Add the route to the base */
79   routing_table_[src->id()][dst->id()] =
80       std::unique_ptr<Route>(new_extended_route(get_hierarchy(), gw_src, gw_dst, link_list, true));
81
82   if (symmetrical && src != dst) {
83     if (gw_dst && gw_src) {
84       NetPoint* gw_tmp = gw_src;
85       gw_src           = gw_dst;
86       gw_dst           = gw_tmp;
87     }
88     if (gw_dst && gw_src) // inter-zone route (to adapt the error message, if any)
89       xbt_assert(
90           nullptr == routing_table_[dst->id()][src->id()],
91           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
92           dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
93     else
94       xbt_assert(nullptr == routing_table_[dst->id()][src->id()],
95                  "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
96                  dst->get_cname(), src->get_cname());
97
98     routing_table_[dst->id()][src->id()] =
99         std::unique_ptr<Route>(new_extended_route(get_hierarchy(), gw_src, gw_dst, link_list, false));
100   }
101 }
102 } // namespace routing
103 } // namespace kernel
104
105 namespace s4u {
106 NetZone* create_full_zone(const std::string& name)
107 {
108   return (new kernel::routing::FullZone(name))->get_iface();
109 }
110 } // namespace s4u
111
112 } // namespace simgrid