Logo AND Algorithmique Numérique Distribuée

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