Logo AND Algorithmique Numérique Distribuée

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