Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'model_types_rework_part1' into 'master'
[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 "src/surf/xml/platf_private.hpp"
10 #include "surf/surf.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
13
14 #define TO_ROUTE_FULL(i, j) routing_table_[(i) + (j)*table_size]
15
16 namespace simgrid {
17 namespace kernel {
18 namespace routing {
19 FullZone::FullZone(const std::string& name) : RoutedZone(name) {}
20
21 void FullZone::do_seal()
22 {
23   unsigned int table_size = get_table_size();
24
25   /* Create table if needed */
26   if (routing_table_.empty())
27     routing_table_.resize(table_size * table_size, nullptr);
28
29   /* Add the loopback if needed */
30   if (get_network_model()->loopback_ && hierarchy_ == RoutingMode::base) {
31     for (unsigned int i = 0; i < table_size; i++) {
32       RouteCreationArgs* route = TO_ROUTE_FULL(i, i);
33       if (not route) {
34         route = new RouteCreationArgs();
35         route->link_list.push_back(get_network_model()->loopback_);
36         TO_ROUTE_FULL(i, i) = route;
37       }
38     }
39   }
40 }
41
42 FullZone::~FullZone()
43 {
44   /* Delete routing table */
45   for (auto const* route : routing_table_)
46     delete route;
47 }
48
49 void FullZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* res, double* lat)
50 {
51   XBT_DEBUG("full getLocalRoute from %s[%u] to %s[%u]", src->get_cname(), src->id(), dst->get_cname(), dst->id());
52
53   unsigned int table_size          = get_table_size();
54   const RouteCreationArgs* e_route = TO_ROUTE_FULL(src->id(), dst->id());
55
56   if (e_route != nullptr) {
57     res->gw_src = e_route->gw_src;
58     res->gw_dst = e_route->gw_dst;
59     for (auto const& link : e_route->link_list) {
60       res->link_list.push_back(link);
61       if (lat)
62         *lat += link->get_latency();
63     }
64   }
65 }
66
67 void FullZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
68                          std::vector<resource::LinkImpl*>& link_list, bool symmetrical)
69 {
70   add_route_check_params(src, dst, gw_src, gw_dst, link_list, symmetrical);
71
72   unsigned int table_size = get_table_size();
73
74   if (routing_table_.empty())
75     routing_table_.resize(table_size * table_size, nullptr);
76
77   /* Check that the route does not already exist */
78   if (gw_dst && gw_src) // inter-zone route (to adapt the error message, if any)
79     xbt_assert(nullptr == TO_ROUTE_FULL(src->id(), dst->id()),
80                "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
81                src->get_cname(), gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
82   else
83     xbt_assert(nullptr == TO_ROUTE_FULL(src->id(), dst->id()),
84                "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src->get_cname(),
85                dst->get_cname());
86
87   /* Add the route to the base */
88   TO_ROUTE_FULL(src->id(), dst->id()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, true);
89
90   if (symmetrical && src != dst) {
91     if (gw_dst && gw_src) {
92       NetPoint* gw_tmp = gw_src;
93       gw_src           = gw_dst;
94       gw_dst           = gw_tmp;
95     }
96     if (gw_dst && gw_src) // inter-zone route (to adapt the error message, if any)
97       xbt_assert(
98           nullptr == TO_ROUTE_FULL(dst->id(), src->id()),
99           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
100           dst->get_cname(), gw_dst->get_cname(), src->get_cname(), gw_src->get_cname());
101     else
102       xbt_assert(nullptr == TO_ROUTE_FULL(dst->id(), src->id()),
103                  "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
104                  dst->get_cname(), src->get_cname());
105
106     TO_ROUTE_FULL(dst->id(), src->id()) = new_extended_route(hierarchy_, gw_src, gw_dst, link_list, false);
107   }
108 }
109 } // namespace routing
110 } // namespace kernel
111 } // namespace simgrid