Logo AND Algorithmique Numérique Distribuée

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