Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7a82d42ad3c07c23f21425c5c51ce13956b31dc9
[simgrid.git] / src / surf / network_ns3.cpp
1 /* Copyright (c) 2007-2014. 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
7 #include "network_ns3.hpp"
8 #include "surf_private.h"
9 #include "simgrid/sg_config.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
12
13 extern xbt_lib_t host_lib;
14 extern xbt_lib_t link_lib;
15 extern xbt_lib_t as_router_lib;
16
17 extern xbt_dict_t dict_socket;
18
19 xbt_dynar_t IPV4addr;
20 static double time_to_next_flow_completion = -1;
21
22 /*************
23  * Callbacks *
24  *************/
25
26 static void replace_bdw_ns3(char ** bdw)
27 {
28   char *temp = xbt_strdup(*bdw);
29   xbt_free(*bdw);
30   *bdw = bprintf("%fBps",atof(temp));
31   xbt_free(temp);
32
33 }
34
35 static void replace_lat_ns3(char ** lat)
36 {
37   char *temp = xbt_strdup(*lat);
38   xbt_free(*lat);
39   *lat = bprintf("%fs",atof(temp));
40   xbt_free(temp);
41 }
42
43 static void parse_ns3_add_host(sg_platf_host_cbarg_t host)
44 {
45   XBT_DEBUG("NS3_ADD_HOST '%s'",host->id);
46   xbt_lib_set(host_lib,
47               host->id,
48               NS3_HOST_LEVEL,
49               ns3_add_host(host->id)
50     );
51 }
52
53 static void parse_ns3_add_link(sg_platf_link_cbarg_t link)
54 {
55   XBT_DEBUG("NS3_ADD_LINK '%s'",link->id);
56
57   if(!IPV4addr) IPV4addr = xbt_dynar_new(sizeof(char*),free);
58
59   Link* net_link = surf_network_model->createLink(link->id,
60                                      link->bandwidth,
61                                      link->bandwidth_trace,
62                                      link->latency,
63                                      link->latency_trace,
64                                      link->state,
65                                      link->state_trace,
66                                      link->policy,
67                                      link->properties);
68
69   xbt_lib_set(link_lib, link->id, SURF_LINK_LEVEL, net_link);
70 }
71
72 static void parse_ns3_add_router(sg_platf_router_cbarg_t router)
73 {
74   XBT_DEBUG("NS3_ADD_ROUTER '%s'",router->id);
75   xbt_lib_set(as_router_lib,
76               router->id,
77               NS3_ASR_LEVEL,
78               ns3_add_router(router->id)
79     );
80 }
81
82 static void parse_ns3_add_AS(sg_platf_AS_cbarg_t AS)
83 {
84   XBT_DEBUG("NS3_ADD_AS '%s'",AS->id);
85   xbt_lib_set(as_router_lib,
86               AS->id,
87               NS3_ASR_LEVEL,
88               ns3_add_AS(AS->id)
89     );
90 }
91
92 static void parse_ns3_add_cluster(sg_platf_cluster_cbarg_t cluster)
93 {
94   const char *cluster_prefix = cluster->prefix;
95   const char *cluster_suffix = cluster->suffix;
96   const char *cluster_radical = cluster->radical;
97   const char *cluster_bb_bw = bprintf("%f",cluster->bb_bw);
98   const char *cluster_bb_lat = bprintf("%f",cluster->bb_lat);
99   const char *cluster_bw = bprintf("%f",cluster->bw);
100   const char *cluster_lat = bprintf("%f",cluster->lat);
101   const char *groups = NULL;
102
103   int start, end, i;
104   unsigned int iter;
105
106   xbt_dynar_t radical_elements;
107   xbt_dynar_t radical_ends;
108   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
109
110   char *router_id,*host_id;
111
112   radical_elements = xbt_str_split(cluster_radical, ",");
113   xbt_dynar_foreach(radical_elements, iter, groups) {
114     radical_ends = xbt_str_split(groups, "-");
115
116     switch (xbt_dynar_length(radical_ends)) {
117     case 1:
118       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
119       xbt_dynar_push_as(tab_elements_num, int, start);
120       router_id = bprintf("ns3_%s%d%s", cluster_prefix, start, cluster_suffix);
121       xbt_lib_set(host_lib,
122                   router_id,
123                   NS3_HOST_LEVEL,
124                   ns3_add_host_cluster(router_id)
125         );
126       XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
127       free(router_id);
128       break;
129
130     case 2:
131       start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
132       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
133       for (i = start; i <= end; i++){
134         xbt_dynar_push_as(tab_elements_num, int, i);
135         router_id = bprintf("ns3_%s%d%s", cluster_prefix, i, cluster_suffix);
136         xbt_lib_set(host_lib,
137                     router_id,
138                     NS3_HOST_LEVEL,
139                     ns3_add_host_cluster(router_id)
140           );
141         XBT_DEBUG("NS3_ADD_ROUTER '%s'",router_id);
142         free(router_id);
143       }
144       break;
145
146     default:
147       XBT_DEBUG("Malformed radical");
148     }
149   }
150
151   //Create links
152   unsigned int cpt;
153   int elmts;
154   char * lat = xbt_strdup(cluster_lat);
155   char * bw =  xbt_strdup(cluster_bw);
156   replace_lat_ns3(&lat);
157   replace_bdw_ns3(&bw);
158
159   xbt_dynar_foreach(tab_elements_num,cpt,elmts)
160   {
161     host_id   = bprintf("%s%d%s", cluster_prefix, elmts, cluster_suffix);
162     router_id = bprintf("ns3_%s%d%s", cluster_prefix, elmts, cluster_suffix);
163     XBT_DEBUG("Create link from '%s' to '%s'",host_id,router_id);
164
165     ns3_nodes_t host_src = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,host_id,  NS3_HOST_LEVEL));
166     ns3_nodes_t host_dst = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,router_id,NS3_HOST_LEVEL));
167
168     if(host_src && host_dst){}
169     else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
170
171     ns3_add_link(host_src->node_num,host_src->type,
172                  host_dst->node_num,host_dst->type,
173                  bw,lat);
174
175     free(router_id);
176     free(host_id);
177   }
178   xbt_dynar_free(&tab_elements_num);
179
180
181   //Create link backbone
182   lat = xbt_strdup(cluster_bb_lat);
183   bw =  xbt_strdup(cluster_bb_bw);
184   replace_lat_ns3(&lat);
185   replace_bdw_ns3(&bw);
186   ns3_add_cluster(bw,lat,cluster->id);
187   xbt_free(lat);
188   xbt_free(bw);
189 }
190
191 /* Create the ns3 topology based on routing strategy */
192 static void create_ns3_topology(void)
193 {
194   XBT_DEBUG("Starting topology generation");
195
196   xbt_dynar_shrink(IPV4addr,0);
197
198   //get the onelinks from the parsed platform
199   xbt_dynar_t onelink_routes = routing_platf->getOneLinkRoutes();
200   if (!onelink_routes)
201     xbt_die("There is no routes!");
202   XBT_DEBUG("Have get_onelink_routes, found %ld routes",onelink_routes->used);
203   //save them in trace file
204   OnelinkPtr onelink;
205   unsigned int iter;
206   xbt_dynar_foreach(onelink_routes, iter, onelink) {
207     char *src = onelink->p_src->getName();
208     char *dst = onelink->p_dst->getName();
209     NetworkNS3Link *link = static_cast<NetworkNS3Link *>(onelink->p_link);
210
211     if (strcmp(src,dst) && link->m_created){
212       XBT_DEBUG("Route from '%s' to '%s' with link '%s'", src, dst, link->getName());
213       char * link_bdw = xbt_strdup(link->p_bdw);
214       char * link_lat = xbt_strdup(link->p_lat);
215       replace_lat_ns3(&link_lat);
216       replace_bdw_ns3(&link_bdw);
217       link->m_created = 0;
218
219       //   XBT_DEBUG("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id);
220       XBT_DEBUG("\tLink (%s) bdw:%s lat:%s", link->getName(), link_bdw, link_lat);
221
222       //create link ns3
223       ns3_nodes_t host_src = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,src,NS3_HOST_LEVEL));
224       if(!host_src) host_src = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(as_router_lib,src,NS3_ASR_LEVEL));
225       ns3_nodes_t host_dst = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(host_lib,dst,NS3_HOST_LEVEL));
226       if(!host_dst) host_dst = static_cast<ns3_nodes_t>(xbt_lib_get_or_null(as_router_lib,dst,NS3_ASR_LEVEL));
227
228       if(host_src && host_dst){}
229       else xbt_die("\tns3_add_link from %d to %d",host_src->node_num,host_dst->node_num);
230
231       ns3_add_link(host_src->node_num,host_src->type,host_dst->node_num,host_dst->type,link_bdw,link_lat);
232
233       xbt_free(link_bdw);
234       xbt_free(link_lat);
235     }
236   }
237 }
238
239 static void parse_ns3_end_platform(void)
240 {
241   ns3_end_platform();
242 }
243
244 static void define_callbacks_ns3(void)
245 {
246   sg_platf_host_add_cb (&parse_ns3_add_host);
247   sg_platf_router_add_cb (&parse_ns3_add_router);
248   sg_platf_link_add_cb (&parse_ns3_add_link);
249   sg_platf_cluster_add_cb (&parse_ns3_add_cluster);
250   sg_platf_AS_begin_add_cb (&parse_ns3_add_AS);
251   sg_platf_postparse_add_cb(&create_ns3_topology); //get_one_link_routes
252   sg_platf_postparse_add_cb(&parse_ns3_end_platform); //InitializeRoutes
253 }
254
255 /*********
256  * Model *
257  *********/
258 static void free_ns3_link(void * elmts)
259 {
260   delete static_cast<NetworkNS3Link*>(elmts);
261 }
262
263 static void free_ns3_host(void * elmts)
264 {
265   ns3_nodes_t host = static_cast<ns3_nodes_t>(elmts);
266   free(host);
267 }
268
269 void surf_network_model_init_NS3()
270 {
271   if (surf_network_model)
272     return;
273
274   surf_network_model = new NetworkNS3Model();
275
276   xbt_dynar_push(model_list, &surf_network_model);
277 }
278
279 NetworkNS3Model::NetworkNS3Model() : NetworkModel("network NS3") {
280   if (ns3_initialize(xbt_cfg_get_string(_sg_cfg_set, "ns3/TcpModel"))) {
281     xbt_die("Impossible to initialize NS3 interface");
282   }
283   routing_model_create(NULL);
284   define_callbacks_ns3();
285
286   NS3_HOST_LEVEL = xbt_lib_add_level(host_lib,(void_f_pvoid_t)free_ns3_host);
287   NS3_ASR_LEVEL  = xbt_lib_add_level(as_router_lib,(void_f_pvoid_t)free_ns3_host);
288 }
289
290 NetworkNS3Model::~NetworkNS3Model() {
291   ns3_finalize();
292   xbt_dynar_free_container(&IPV4addr);
293   xbt_dict_free(&dict_socket);
294 }
295
296 Link* NetworkNS3Model::createLink(const char *name,
297                                          double bw_initial,
298                                          tmgr_trace_t bw_trace,
299                                          double lat_initial,
300                                          tmgr_trace_t lat_trace,
301                                          e_surf_resource_state_t state_initial,
302                                          tmgr_trace_t state_trace,
303                                          e_surf_link_sharing_policy_t policy,
304                                          xbt_dict_t properties){
305   if (bw_trace)
306     XBT_INFO("The NS3 network model doesn't support bandwidth state traces");
307   if (lat_trace)
308     XBT_INFO("The NS3 network model doesn't support latency state traces");
309   if (state_trace)
310     XBT_INFO("The NS3 network model doesn't support link state traces");
311   return new NetworkNS3Link(this, name, properties, bw_initial, lat_initial);
312 }
313
314 xbt_dynar_t NetworkNS3Model::getRoute(RoutingEdgePtr src, RoutingEdgePtr dst)
315 {
316   xbt_dynar_t route = NULL;
317   routing_get_route_and_latency(src, dst, &route, NULL);
318   //routing_platf->getRouteAndLatency(src, dst, &route, NULL);
319   return route;
320 }
321
322 ActionPtr NetworkNS3Model::communicate(RoutingEdgePtr src, RoutingEdgePtr dst,
323                                                double size, double rate)
324 {
325   XBT_DEBUG("Communicate from %s to %s", src->getName(), dst->getName());
326   NetworkNS3ActionPtr action = new NetworkNS3Action(this, size, 0);
327
328   ns3_create_flow(src->getName(), dst->getName(), surf_get_clock(), size, action);
329
330   action->m_lastSent = 0;
331   action->p_srcElm = src;
332   action->p_dstElm = dst;
333   surf_callback_emit(networkCommunicateCallbacks, action, src, dst, size, rate);
334
335   return (surf_action_t) action;
336 }
337
338 double NetworkNS3Model::shareResources(double now)
339 {
340   XBT_DEBUG("ns3_share_resources");
341
342   //get the first relevant value from the running_actions list
343   if (!getRunningActionSet()->size() || now == 0.0)
344     return -1.0;
345   else
346     do {
347       ns3_simulator(now);
348       time_to_next_flow_completion = ns3_time() - surf_get_clock();//FIXME: use now instead ?
349     } while(double_equals(time_to_next_flow_completion, 0, sg_surf_precision));
350
351   XBT_DEBUG("min       : %f", now);
352   XBT_DEBUG("ns3  time : %f", ns3_time());
353   XBT_DEBUG("surf time : %f", surf_get_clock());
354   XBT_DEBUG("Next completion %f :", time_to_next_flow_completion);
355
356   return time_to_next_flow_completion;
357 }
358
359 void NetworkNS3Model::updateActionsState(double now, double delta)
360 {
361   xbt_dict_cursor_t cursor = NULL;
362   char *key;
363   void *data;
364
365   static xbt_dynar_t socket_to_destroy = NULL;
366   if(!socket_to_destroy) socket_to_destroy = xbt_dynar_new(sizeof(char*),NULL);
367
368   /* If there are no running flows, just return */
369   if (!getRunningActionSet()->size()) {
370     while(double_positive(now-ns3_time(), sg_surf_precision)) {
371       ns3_simulator(now-ns3_time());
372     }
373     return;
374   }
375
376   NetworkNS3ActionPtr action;
377   xbt_dict_foreach(dict_socket,cursor,key,data){
378     action = static_cast<NetworkNS3ActionPtr>(ns3_get_socket_action(data));
379     XBT_DEBUG("Processing socket %p (action %p)",data,action);
380     action->setRemains(action->getCost() - ns3_get_socket_sent(data));
381
382     if (TRACE_is_enabled() &&
383                 action->getState() == SURF_ACTION_RUNNING){
384         double data_sent = ns3_get_socket_sent(data);
385         double data_delta_sent = data_sent - action->m_lastSent;
386
387         xbt_dynar_t route = NULL;
388
389         routing_get_route_and_latency (action->p_srcElm, action->p_dstElm, &route, NULL);
390         unsigned int i;
391         for (i = 0; i < xbt_dynar_length (route); i++){
392                 NetworkNS3Link* link = ((NetworkNS3Link*)xbt_dynar_get_ptr (route, i));
393                 TRACE_surf_link_set_utilization (link->getName(),
394                                 action->getCategory(),
395                                         (data_delta_sent)/delta,
396                                         now-delta,
397                                         delta);
398         }
399         action->m_lastSent = data_sent;
400     }
401
402     if(ns3_get_socket_is_finished(data) == 1){
403       xbt_dynar_push(socket_to_destroy,&key);
404       XBT_DEBUG("Destroy socket %p of action %p", key, action);
405       action->finish();
406       action->setState(SURF_ACTION_DONE);
407     }
408   }
409
410   while (!xbt_dynar_is_empty(socket_to_destroy)){
411     xbt_dynar_pop(socket_to_destroy,&key);
412
413     void *data = xbt_dict_get (dict_socket, key);
414     action = static_cast<NetworkNS3ActionPtr>(ns3_get_socket_action(data));
415     XBT_DEBUG ("Removing socket %p of action %p", key, action);
416     xbt_dict_remove(dict_socket, key);
417   }
418   return;
419 }
420
421 /************
422  * Resource *
423  ************/
424
425 NetworkNS3Link::NetworkNS3Link(NetworkNS3ModelPtr model, const char *name, xbt_dict_t props,
426                                        double bw_initial, double lat_initial)
427  : Link(model, name, props)
428  , p_lat(bprintf("%f", lat_initial))
429  , p_bdw(bprintf("%f", bw_initial))
430  , m_created(1)
431 {
432 }
433
434 NetworkNS3Link::~NetworkNS3Link()
435 {
436 }
437
438 void NetworkNS3Link::updateState(tmgr_trace_event_t event_type, double value, double date)
439 {
440
441 }
442
443 /**********
444  * Action *
445  **********/
446
447 NetworkNS3Action::NetworkNS3Action(ModelPtr model, double cost, bool failed)
448 : NetworkAction(model, cost, failed)
449 {}
450
451 #ifdef HAVE_LATENCY_BOUND_TRACKING
452   int NetworkNS3Action::getLatencyLimited() {
453     return m_latencyLimited;
454   }
455 #endif
456
457  void NetworkNS3Action::suspend()
458 {
459   THROW_UNIMPLEMENTED;
460 }
461
462 void NetworkNS3Action::resume()
463 {
464   THROW_UNIMPLEMENTED;
465 }
466
467   /* Test whether a flow is suspended */
468 bool NetworkNS3Action::isSuspended()
469 {
470   return 0;
471 }
472
473 int NetworkNS3Action::unref()
474 {
475   m_refcount--;
476   if (!m_refcount) {
477         if (actionHook::is_linked())
478           p_stateSet->erase(p_stateSet->iterator_to(*this));
479     XBT_DEBUG ("Removing action %p", this);
480         delete this;
481     return 1;
482   }
483   return 0;
484 }