Logo AND Algorithmique Numérique Distribuée

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