Logo AND Algorithmique Numérique Distribuée

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