Logo AND Algorithmique Numérique Distribuée

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