Logo AND Algorithmique Numérique Distribuée

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