Logo AND Algorithmique Numérique Distribuée

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