Logo AND Algorithmique Numérique Distribuée

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