Logo AND Algorithmique Numérique Distribuée

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