Logo AND Algorithmique Numérique Distribuée

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