Logo AND Algorithmique Numérique Distribuée

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