Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
95ca5f8216b792547150bdc6bb0eba295c4f3ece
[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 void* 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,void * action)
56 {
57         ns3_nodes_t node1 = ns3_find_host(a);
58         ns3_nodes_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_nodes_t host  = xbt_new0(s_ns3_nodes_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         host->data = GetPointer(node);
133         number_of_nodes++;
134         return host;
135 }
136
137 void * ns3_add_host_cluster(const char * id)
138 {
139         ns3_nodes_t host  = xbt_new0(s_ns3_nodes_t,1);
140         XBT_DEBUG("Interface ns3 add host[%d] '%s'",number_of_nodes,id);
141         Ptr<Node> node =  CreateObject<Node> (0);
142         stack.Install(node);
143         Cluster_nodes.Add(node);
144         nodes.Add(node);
145         host->node_num = number_of_nodes;
146         host->type = NS3_NETWORK_ELEMENT_HOST;
147         host->data = node;
148         number_of_nodes++;
149         return host;
150 }
151
152 void * ns3_add_router(const char * id)
153 {
154         ns3_nodes_t router  = xbt_new0(s_ns3_nodes_t,1);
155         XBT_DEBUG("Interface ns3 add router[%d] '%s'",number_of_nodes,id);
156         Ptr<Node> node =  CreateObject<Node> (0);
157         stack.Install(node);
158         nodes.Add(node);
159         router->node_num = number_of_nodes;
160         router->type = NS3_NETWORK_ELEMENT_ROUTER;
161         router->data = node;
162         number_of_nodes++;
163         return router;
164 }
165
166 void ns3_add_cluster(char * bw,char * lat,const char *id)
167 {
168
169         XBT_DEBUG("cluster_id: %s",id);
170         XBT_DEBUG("bw: %s lat: %s",bw,lat);
171         XBT_DEBUG("Number of %s nodes: %d",id,Cluster_nodes.GetN() - number_of_clusters_nodes);
172
173         NodeContainer Nodes;
174         unsigned int i;
175
176         for(i = number_of_clusters_nodes; i < Cluster_nodes.GetN() ; i++){
177                 Nodes.Add(Cluster_nodes.Get(i));
178                 XBT_DEBUG("Add node %d to cluster",i);
179         }
180         number_of_clusters_nodes = Cluster_nodes.GetN();
181
182         XBT_DEBUG("Add router %d to cluster",nodes.GetN()-Nodes.GetN()-1);
183         Nodes.Add(nodes.Get(nodes.GetN()-Nodes.GetN()-1));
184
185         if(Nodes.GetN() > 65000)
186                 xbt_die("Cluster with NS3 is limited to 65000 nodes");
187         CsmaHelper csma;
188         csma.SetChannelAttribute ("DataRate", StringValue (bw));
189         csma.SetChannelAttribute ("Delay", StringValue (lat));
190         NetDeviceContainer devices = csma.Install (Nodes);
191         XBT_DEBUG("Create CSMA");
192
193         char * adr = bprintf("%d.%d.0.0",number_of_networks,number_of_links);
194         XBT_DEBUG("Assign IP Addresses %s to CSMA.",adr);
195         Ipv4AddressHelper ipv4;
196         ipv4.SetBase (adr, "255.255.0.0");
197         free(adr);
198         interfaces.Add(ipv4.Assign (devices));
199
200         if(number_of_links == 255){
201                 if(number_of_networks == 255)
202                         xbt_die("Number of links and networks exceed 255*255");
203                 number_of_links = 1;
204                 number_of_networks++;
205         }else{
206                 number_of_links++;
207         }
208         XBT_DEBUG("Number of nodes in Cluster_nodes: %d",Cluster_nodes.GetN());
209 }
210
211 void * ns3_add_AS(const char * id)
212 {
213         XBT_DEBUG("Interface ns3 add AS '%s'",id);
214         return NULL;
215 }
216
217 static char* transformIpv4Address (Ipv4Address from){
218         std::stringstream sstream;
219                 sstream << from ;
220                 std::string s = sstream.str();
221                 return bprintf("%s",s.c_str());
222 }
223
224 void ns3_add_link(int src, e_ns3_network_element_type_t type_src,
225                                         int dst, e_ns3_network_element_type_t type_dst,
226                                         char *bw, char *lat)
227 {
228         if(number_of_links == 1 ) {
229                 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
230                 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
231         }
232
233         MyPointToPointHelper pointToPoint;
234
235         NetDeviceContainer netA;
236         Ipv4AddressHelper address;
237
238         Ptr<Node> a = nodes.Get(src);
239         Ptr<Node> b = nodes.Get(dst);
240
241         XBT_DEBUG("\tAdd PTP from %d to %d bw:'%s' lat:'%s'",src,dst,bw,lat);
242         pointToPoint.SetDeviceAttribute ("DataRate", StringValue (bw));
243         pointToPoint.SetChannelAttribute ("Delay", StringValue (lat));
244         //pointToPoint.EnablePcapAll("test_ns3_trace"); //DEBUG
245
246         netA.Add(pointToPoint.Install (a, type_src, b, type_dst));
247
248         char * adr = bprintf("%d.%d.0.0",number_of_networks,number_of_links);
249         address.SetBase (adr, "255.255.0.0");
250         XBT_DEBUG("\tInterface stack '%s'",adr);
251         free(adr);
252         interfaces.Add(address.Assign (netA));
253
254         char *tmp = transformIpv4Address(interfaces.GetAddress(interfaces.GetN()-2));
255         xbt_dynar_set_as(IPV4addr,src,char*,tmp);
256         XBT_DEBUG("Have write '%s' for Node '%d'",(char*)xbt_dynar_get_as(IPV4addr,src,char*),src);
257
258         tmp = transformIpv4Address(interfaces.GetAddress(interfaces.GetN()-1));
259         xbt_dynar_set_as(IPV4addr,dst,char*,tmp);
260         XBT_DEBUG("Have write '%s' for Node '%d'",(char*)xbt_dynar_get_as(IPV4addr,dst,char*),dst);
261
262         if(number_of_links == 255){
263                 if(number_of_networks == 255)
264                         xbt_die("Number of links and networks exceed 255*255");
265                 number_of_links = 1;
266                 number_of_networks++;
267         }else{
268                 number_of_links++;
269         }
270 }
271
272 void ns3_end_platform(void)
273 {
274         XBT_DEBUG("InitializeRoutes");
275         GlobalRouteManager::BuildGlobalRoutingDatabase();
276         GlobalRouteManager::InitializeRoutes();
277 }