Logo AND Algorithmique Numérique Distribuée

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