Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new model for cluster tag.
[simgrid.git] / src / surf / surf_routing_cluster.c
1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "surf_routing_private.h"
7
8 /* Global vars */
9 extern routing_global_t global_routing;
10 extern routing_component_t current_routing;
11 extern model_type_t current_routing_model;
12 extern xbt_dynar_t link_list;
13 extern xbt_dict_t cluster_host_link;
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster, surf, "Routing part of surf");
16
17 /* Routing model structure */
18
19 typedef struct {
20   s_routing_component_t generic_routing;
21   xbt_dict_t dict_processing_units;
22   xbt_dict_t dict_autonomous_systems;
23 } s_routing_component_cluster_t, *routing_component_cluster_t;
24
25 /* Parse routing model functions */
26
27 static void model_cluster_set_processing_unit(routing_component_t rc,
28                                                 const char *name)
29 {
30   routing_component_cluster_t routing =
31       (routing_component_cluster_t) rc;
32   xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
33 }
34
35 static void model_cluster_set_autonomous_system(routing_component_t rc,
36                                                   const char *name)
37 {
38   routing_component_cluster_t routing =
39       (routing_component_cluster_t) rc;
40   xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
41                NULL);
42 }
43
44 static void model_cluster_set_bypassroute(routing_component_t rc,
45                                             const char *src,
46                                             const char *dst,
47                                             route_extended_t e_route)
48 {
49   xbt_die("bypass routing not supported for Route-Based model");
50 }
51
52 static void model_cluster_set_route(routing_component_t rc,
53                                       const char *src, const char *dst,
54                                       name_route_extended_t route)
55 {
56         xbt_die("routing not supported for Route-Based model");
57 }
58
59 static void model_cluster_set_ASroute(routing_component_t rc,
60                                         const char *src, const char *dst,
61                                         name_route_extended_t route)
62 {
63         xbt_die("AS routing not supported for Route-Based model");
64 }
65
66 #define BUFFER_SIZE 4096        /* result buffer size */
67 #define OVECCOUNT 30            /* should be a multiple of 3 */
68
69 static route_extended_t cluster_get_route(routing_component_t rc,
70                                             const char *src,
71                                             const char *dst);
72 static xbt_dynar_t cluster_get_onelink_routes(routing_component_t rc)
73 {
74   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
75
76   //We have already bypass cluster routes with network NS3
77   if(!strcmp(surf_network_model->name,"network NS3"))
78         return ret;
79
80   routing_component_cluster_t routing = (routing_component_cluster_t)rc;
81
82   xbt_dict_cursor_t c1 = NULL;
83   char *k1, *d1;
84
85   //find router
86   char *router = NULL;
87   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
88     if (rc->get_network_element_type(k1) == SURF_NETWORK_ELEMENT_ROUTER){
89       router = k1;
90     }
91   }
92
93   if (!router){
94     xbt_die ("cluster_get_onelink_routes works only if the AS is a cluster, sorry.");
95   }
96
97   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
98     route_extended_t route = cluster_get_route (rc, router, k1);
99
100     int number_of_links = xbt_dynar_length(route->generic_route.link_list);
101
102     if(number_of_links == 1) {
103                 //loopback
104     }
105     else{
106                 if (number_of_links != 2) {
107                   xbt_die ("cluster_get_onelink_routes works only if the AS is a cluster, sorry.");
108                 }
109
110                 void *link_ptr;
111                 xbt_dynar_get_cpy (route->generic_route.link_list, 1, &link_ptr);
112                 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
113                 onelink->src = xbt_strdup (k1);
114                 onelink->dst = xbt_strdup (router);
115                 onelink->link_ptr = link_ptr;
116                 xbt_dynar_push (ret, &onelink);
117     }
118   }
119   return ret;
120 }
121
122 /* Business methods */
123 static route_extended_t cluster_get_route(routing_component_t rc,
124                                             const char *src,
125                                             const char *dst)
126 {
127           xbt_assert(rc && src
128                       && dst,
129                       "Invalid params for \"get_route\" function at AS \"%s\"",
130                       rc->name);
131
132
133           xbt_dynar_t links_list =
134               xbt_dynar_new(global_routing->size_of_link, NULL);
135
136           char *cluster_is_fd = xbt_dict_get_or_null(cluster_host_link,rc->name);
137           char *link_src,*link_bb,*link_dst,*link_src_up,*link_dst_down;
138
139           if(!cluster_is_fd){ //        NOT FULLDUPLEX
140                   link_src = xbt_dict_get_or_null(cluster_host_link,src);
141                   if( !link_src && (global_routing->get_network_element_type(src) != SURF_NETWORK_ELEMENT_ROUTER) )
142                           xbt_die("No link for '%s' found!",src);
143                   if(link_src) xbt_dynar_push_as(links_list,void*,xbt_lib_get_or_null(link_lib, link_src, SURF_LINK_LEVEL)); //link_up
144
145                   link_bb = bprintf("%s_backbone",rc->name);
146                   xbt_dynar_push_as(links_list,void*,xbt_lib_get_or_null(link_lib, link_bb, SURF_LINK_LEVEL)); //link_bb
147                   free(link_bb);
148
149                   link_dst = xbt_dict_get_or_null(cluster_host_link,dst);
150                   if( !link_dst && (global_routing->get_network_element_type(dst) != SURF_NETWORK_ELEMENT_ROUTER) )
151                           xbt_die("No link for '%s' found!",dst);
152                   if(link_dst) xbt_dynar_push_as(links_list,void*,xbt_lib_get_or_null(link_lib, link_dst, SURF_LINK_LEVEL)); //link_down
153           }
154           else //       FULLDUPLEX
155           {
156                   link_src = xbt_dict_get_or_null(cluster_host_link,src);
157                   if( !link_src  && (global_routing->get_network_element_type(src) != SURF_NETWORK_ELEMENT_ROUTER) )
158                           xbt_die("No link for '%s' found!",src);
159                   link_src_up = bprintf("%s_UP",link_src);
160                   if(link_src) xbt_dynar_push_as(links_list,void*,xbt_lib_get_or_null(link_lib, link_src_up, SURF_LINK_LEVEL)); //link_up
161                   free(link_src_up);
162
163                   link_bb = bprintf("%s_backbone",rc->name);
164                   if(link_bb)  xbt_dynar_push_as(links_list,void*,xbt_lib_get_or_null(link_lib, link_bb, SURF_LINK_LEVEL)); //link_bb
165                   free(link_bb);
166
167                   link_dst = xbt_dict_get_or_null(cluster_host_link,dst);
168                   if(!link_dst  && (global_routing->get_network_element_type(dst) != SURF_NETWORK_ELEMENT_ROUTER))
169                           xbt_die("No link for '%s' found!",dst);
170                   link_dst_down = bprintf("%s_DOWN",link_dst);
171                   if(link_dst) xbt_dynar_push_as(links_list,void*,xbt_lib_get_or_null(link_lib, link_dst_down, SURF_LINK_LEVEL)); //link_down
172                   free(link_dst_down);
173           }
174
175           route_extended_t new_e_route = NULL;
176           new_e_route = xbt_new0(s_route_extended_t, 1);
177           new_e_route->generic_route.link_list = links_list;
178
179           return new_e_route;
180 }
181
182 static route_extended_t cluster_get_bypass_route(routing_component_t rc,
183                                                    const char *src,
184                                                    const char *dst)
185 {
186   return NULL;
187 }
188
189 static void cluster_finalize(routing_component_t rc)
190 {
191   routing_component_cluster_t routing =
192       (routing_component_cluster_t) rc;
193   if (routing) {
194     xbt_dict_free(&routing->dict_processing_units);
195     xbt_dict_free(&routing->dict_autonomous_systems);
196     /* Delete structure */
197     xbt_free(routing);
198   }
199 }
200
201 /* Creation routing model functions */
202 void *model_cluster_create(void)
203 {
204   routing_component_cluster_t new_component =
205       xbt_new0(s_routing_component_cluster_t, 1);
206   new_component->generic_routing.set_processing_unit =
207       model_cluster_set_processing_unit;
208   new_component->generic_routing.set_autonomous_system =
209       model_cluster_set_autonomous_system;
210   new_component->generic_routing.set_route = model_cluster_set_route;
211   new_component->generic_routing.set_ASroute = model_cluster_set_ASroute;
212   new_component->generic_routing.set_bypassroute = model_cluster_set_bypassroute;
213   new_component->generic_routing.get_onelink_routes = cluster_get_onelink_routes;
214   new_component->generic_routing.get_route = cluster_get_route;
215   new_component->generic_routing.get_latency = generic_get_link_latency;
216   new_component->generic_routing.get_bypass_route = cluster_get_bypass_route;
217   new_component->generic_routing.finalize = cluster_finalize;
218   new_component->generic_routing.get_network_element_type = get_network_element_type;
219   /* initialization of internal structures */
220   new_component->dict_processing_units = xbt_dict_new();
221   new_component->dict_autonomous_systems = xbt_dict_new();
222
223   return new_component;
224 }
225
226 void model_cluster_load(void)
227 {
228   /* use "surfxml_add_callback" to add a parse function call */
229 }
230
231 void model_cluster_unload(void)
232 {
233   /* use "surfxml_del_callback" to remove a parse function call */
234 }
235
236 void model_cluster_end(void)
237 {
238 }