Logo AND Algorithmique Numérique Distribuée

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