Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ns3: kill an unused void*, qualify another one
[simgrid.git] / src / surf / ns3 / ns3_interface.cc
1 /* Copyright (c) 2007-2014. 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 "ns3_interface.h"
8 #include "ns3_simulator.h"
9 #include "xbt/lib.h"
10 #include "xbt/log.h"
11 #include "xbt/dynar.h"
12 #include "xbt/Extendable.hpp"
13
14
15 using namespace ns3;
16
17 extern xbt_dynar_t IPV4addr;
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ns3, surf,
20                                 "Logging specific to the SURF network NS3 module");
21
22 InternetStackHelper stack;
23 NodeContainer nodes;
24 NodeContainer Cluster_nodes;
25 Ipv4InterfaceContainer interfaces;
26
27 int number_of_nodes = 0;
28 int number_of_clusters_nodes = 0;
29 int number_of_links = 1;
30 int number_of_networks = 1;
31 int port_number = 1025; //Port number is limited from 1025 to 65 000
32
33 static NS3Sim* ns3_sim = 0;
34
35 void ns3_simulator(double min){
36                         ns3_sim->simulator_start(min);
37 }
38
39 simgrid::surf::NetworkNS3Action* ns3_get_socket_action(void *socket){
40   return ((MySocket *)socket)->action;
41 }
42
43 double ns3_get_socket_remains(void *socket){
44   return ((MySocket *)socket)->remaining;
45 }
46
47 double ns3_get_socket_sent(void *socket){
48   return ((MySocket *)socket)->sentBytes;
49 }
50
51 char ns3_get_socket_is_finished(void *socket){
52   return ((MySocket *)socket)->finished;
53 }
54
55 int ns3_create_flow(const char* a,const char *b,double start,u_int32_t TotalBytes,simgrid::surf::NetworkNS3Action * action)
56 {
57         ns3_node_t node1 = ns3_find_host(a);
58         ns3_node_t node2 = ns3_find_host(b);
59
60         Ptr<Node> src_node = nodes.Get(node1->node_num);
61         Ptr<Node> dst_node = nodes.Get(node2->node_num);
62
63         char* addr = (char*)xbt_dynar_get_as(IPV4addr,node2->node_num,char*);
64
65         XBT_DEBUG("ns3_create_flow %d Bytes from %d to %d with Interface %s",TotalBytes, node1->node_num, node2->node_num,addr);
66         ns3_sim->create_flow_NS3(src_node,
67                         dst_node,
68                         port_number,
69                         start,
70                         addr,
71                         TotalBytes,
72                         action);
73
74         port_number++;
75         if(port_number >= 65001 ) xbt_die("Too many connections! Port number is saturated.");
76         return 0;
77 }
78
79 // clean up
80 int ns3_finalize(void){
81         if (!ns3_sim) return -1;
82         delete ns3_sim;
83         ns3_sim = 0;
84         return 0;
85 }
86
87 // initialize the NS3 interface and environment
88 int ns3_initialize(const char* TcpProtocol){
89   xbt_assert(!ns3_sim, "ns3 already initialized");
90   ns3_sim = new NS3Sim();
91
92 //  tcpModel are:
93 //  "ns3::TcpNewReno"
94 //  "ns3::TcpReno"
95 //  "ns3::TcpTahoe"
96
97   Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1024)); // 1024-byte packet for easier reading
98   Config::SetDefault ("ns3::TcpSocket::DelAckCount", UintegerValue (1));
99
100   if(!strcmp(TcpProtocol,"default")){
101           return 0;
102   }
103   if(!strcmp(TcpProtocol,"Reno")){
104           XBT_INFO("Switching Tcp protocol to '%s'",TcpProtocol);
105           Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpReno"));
106           return 0;
107   }
108   if(!strcmp(TcpProtocol,"NewReno")){
109           XBT_INFO("Switching Tcp protocol to '%s'",TcpProtocol);
110           Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpNewReno"));
111           return 0;
112   }
113   if(!strcmp(TcpProtocol,"Tahoe")){
114           XBT_INFO("Switching Tcp protocol to '%s'",TcpProtocol);
115           Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpTahoe"));
116           return 0;
117   }
118
119   XBT_ERROR("The ns3/TcpModel must be : NewReno or Reno or Tahoe");
120   return 0;
121 }
122
123 void * ns3_add_host(const char * id)
124 {
125         ns3_node_t host  = xbt_new0(s_ns3_node_t,1);
126         XBT_DEBUG("Interface ns3 add host[%d] '%s'",number_of_nodes,id);
127         Ptr<Node> node =  CreateObject<Node> (0);
128         stack.Install(node);
129         nodes.Add(node);
130         host->node_num = number_of_nodes;
131         host->type = NS3_NETWORK_ELEMENT_HOST;
132         number_of_nodes++;
133         return host;
134 }
135
136 void * ns3_add_host_cluster(const char * id)
137 {
138         ns3_node_t host  = xbt_new0(s_ns3_node_t,1);
139         XBT_DEBUG("Interface ns3 add host[%d] '%s'",number_of_nodes,id);
140         Ptr<Node> node =  CreateObject<Node> (0);
141         stack.Install(node);
142         Cluster_nodes.Add(node);
143         nodes.Add(node);
144         host->node_num = number_of_nodes;
145         host->type = NS3_NETWORK_ELEMENT_HOST;
146         number_of_nodes++;
147         return host;
148 }
149
150 void * ns3_add_router(const char * id)
151 {
152         ns3_node_t router  = xbt_new0(s_ns3_node_t,1);
153         XBT_DEBUG("Interface ns3 add router[%d] '%s'",number_of_nodes,id);
154         Ptr<Node> node =  CreateObject<Node> (0);
155         stack.Install(node);
156         nodes.Add(node);
157         router->node_num = number_of_nodes;
158         router->type = NS3_NETWORK_ELEMENT_ROUTER;
159         number_of_nodes++;
160         return router;
161 }
162
163 void ns3_add_cluster(char * bw,char * lat,const char *id)
164 {
165
166         XBT_DEBUG("cluster_id: %s",id);
167         XBT_DEBUG("bw: %s lat: %s",bw,lat);
168         XBT_DEBUG("Number of %s nodes: %d",id,Cluster_nodes.GetN() - number_of_clusters_nodes);
169
170         NodeContainer Nodes;
171         unsigned int i;
172
173         for(i = number_of_clusters_nodes; i < Cluster_nodes.GetN() ; i++){
174                 Nodes.Add(Cluster_nodes.Get(i));
175                 XBT_DEBUG("Add node %d to cluster",i);
176         }
177         number_of_clusters_nodes = Cluster_nodes.GetN();
178
179         XBT_DEBUG("Add router %d to cluster",nodes.GetN()-Nodes.GetN()-1);
180         Nodes.Add(nodes.Get(nodes.GetN()-Nodes.GetN()-1));
181
182         if(Nodes.GetN() > 65000)
183                 xbt_die("Cluster with NS3 is limited to 65000 nodes");
184         CsmaHelper csma;
185         csma.SetChannelAttribute ("DataRate", StringValue (bw));
186         csma.SetChannelAttribute ("Delay", StringValue (lat));
187         NetDeviceContainer devices = csma.Install (Nodes);
188         XBT_DEBUG("Create CSMA");
189
190         char * adr = bprintf("%d.%d.0.0",number_of_networks,number_of_links);
191         XBT_DEBUG("Assign IP Addresses %s to CSMA.",adr);
192         Ipv4AddressHelper ipv4;
193         ipv4.SetBase (adr, "255.255.0.0");
194         free(adr);
195         interfaces.Add(ipv4.Assign (devices));
196
197         if(number_of_links == 255){
198                 if(number_of_networks == 255)
199                         xbt_die("Number of links and networks exceed 255*255");
200                 number_of_links = 1;
201                 number_of_networks++;
202         }else{
203                 number_of_links++;
204         }
205         XBT_DEBUG("Number of nodes in Cluster_nodes: %d",Cluster_nodes.GetN());
206 }
207
208 void * ns3_add_AS(const char * id)
209 {
210         XBT_DEBUG("Interface ns3 add AS '%s'",id);
211         return NULL;
212 }
213
214 static char* transformIpv4Address (Ipv4Address from){
215         std::stringstream sstream;
216                 sstream << from ;
217                 std::string s = sstream.str();
218                 return bprintf("%s",s.c_str());
219 }
220
221 void ns3_add_link(int src, e_ns3_network_element_type_t type_src,
222                                         int dst, e_ns3_network_element_type_t type_dst,
223                                         char *bw, char *lat)
224 {
225         if(number_of_links == 1 ) {
226                 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
227                 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
228         }
229
230         MyPointToPointHelper pointToPoint;
231
232         NetDeviceContainer netA;
233         Ipv4AddressHelper address;
234
235         Ptr<Node> a = nodes.Get(src);
236         Ptr<Node> b = nodes.Get(dst);
237
238         XBT_DEBUG("\tAdd PTP from %d to %d bw:'%s' lat:'%s'",src,dst,bw,lat);
239         pointToPoint.SetDeviceAttribute ("DataRate", StringValue (bw));
240         pointToPoint.SetChannelAttribute ("Delay", StringValue (lat));
241         //pointToPoint.EnablePcapAll("test_ns3_trace"); //DEBUG
242
243         netA.Add(pointToPoint.Install (a, type_src, b, type_dst));
244
245         char * adr = bprintf("%d.%d.0.0",number_of_networks,number_of_links);
246         address.SetBase (adr, "255.255.0.0");
247         XBT_DEBUG("\tInterface stack '%s'",adr);
248         free(adr);
249         interfaces.Add(address.Assign (netA));
250
251         char *tmp = transformIpv4Address(interfaces.GetAddress(interfaces.GetN()-2));
252         xbt_dynar_set_as(IPV4addr,src,char*,tmp);
253         XBT_DEBUG("Have write '%s' for Node '%d'",(char*)xbt_dynar_get_as(IPV4addr,src,char*),src);
254
255         tmp = transformIpv4Address(interfaces.GetAddress(interfaces.GetN()-1));
256         xbt_dynar_set_as(IPV4addr,dst,char*,tmp);
257         XBT_DEBUG("Have write '%s' for Node '%d'",(char*)xbt_dynar_get_as(IPV4addr,dst,char*),dst);
258
259         if(number_of_links == 255){
260                 if(number_of_networks == 255)
261                         xbt_die("Number of links and networks exceed 255*255");
262                 number_of_links = 1;
263                 number_of_networks++;
264         }else{
265                 number_of_links++;
266         }
267 }
268
269 void ns3_end_platform(void)
270 {
271         XBT_DEBUG("InitializeRoutes");
272         GlobalRouteManager::BuildGlobalRoutingDatabase();
273         GlobalRouteManager::InitializeRoutes();
274 }