Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[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::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t res, double* lat)
63 {
64   XBT_DEBUG("full getLocalRoute from %s[%d] to %s[%d]", src->name().c_str(), src->id(), dst->name().c_str(), dst->id());
65
66   size_t table_size = vertices_.size();
67   sg_platf_route_cbarg_t e_route = TO_ROUTE_FULL(src->id(), dst->id());
68
69   if (e_route != nullptr) {
70     res->gw_src = e_route->gw_src;
71     res->gw_dst = e_route->gw_dst;
72     for (auto link : *e_route->link_list) {
73       res->link_list->push_back(link);
74       if (lat)
75         *lat += link->latency();
76     }
77   }
78 }
79
80 void AsFull::addRoute(sg_platf_route_cbarg_t route)
81 {
82   NetCard* src        = route->src;
83   NetCard* dst        = route->dst;
84   const char* srcName = src->name().c_str();
85   const char* dstName = dst->name().c_str();
86
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).", srcName,
98                route->gw_src->name().c_str(), dstName, route->gw_dst->name().c_str());
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).", srcName, dstName);
102
103   /* Add the route to the base */
104   TO_ROUTE_FULL(src->id(), dst->id()) = newExtendedRoute(hierarchy_, route, 1);
105   TO_ROUTE_FULL(src->id(), dst->id())->link_list->shrink_to_fit();
106
107   if (route->symmetrical == true && src != dst) {
108     if (route->gw_dst && route->gw_src) {
109       NetCard* gw_tmp = route->gw_src;
110       route->gw_src = route->gw_dst;
111       route->gw_dst = gw_tmp;
112     }
113     if (route->gw_dst) // AS route (to adapt the error message, if any)
114       xbt_assert(
115           nullptr == TO_ROUTE_FULL(dst->id(), src->id()),
116           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
117           dstName, route->gw_dst->name().c_str(), srcName, route->gw_src->name().c_str());
118     else
119       xbt_assert(nullptr == TO_ROUTE_FULL(dst->id(), src->id()),
120                  "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.",
121                  dstName, srcName);
122
123     TO_ROUTE_FULL(dst->id(), src->id()) = newExtendedRoute(hierarchy_, route, 0);
124     TO_ROUTE_FULL(dst->id(), src->id())->link_list->shrink_to_fit();
125   }
126 }
127
128 }}} // namespace