Logo AND Algorithmique Numérique Distribuée

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