Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill a useless element in the NS3 extension of hosts
[simgrid.git] / src / surf / ns3 / ns3_simulator.cc
1 /* Copyright (c) 2007-2016. 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/ns3/ns3_simulator.h"
8 #include "xbt/dict.h"
9 #include "xbt/log.h"
10 #include "xbt/sysdep.h"
11
12 static void delete_mysocket(void *p)
13 {
14   delete (SgFlow *)p;
15 }
16 xbt_dict_t flowFromSock = xbt_dict_new_homogeneous(delete_mysocket);; // ns3::sock -> SgFlow
17
18 NS3Sim SimulatorNS3;
19
20 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
21 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace);
22 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
23 static void StartFlow(ns3::Ptr<ns3::Socket> sock, const char *to, uint16_t port_number);
24
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
26
27 NS3Sim::NS3Sim(){
28 }
29
30 static inline const char *transformSocketPtr (ns3::Ptr<ns3::Socket> localSocket)
31 {
32   static char key[24];
33   std::stringstream sstream;
34   sstream << localSocket ;
35   sprintf(key,"%s",sstream.str().c_str());
36
37   return key;
38 }
39
40 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action * action) {
41   totalBytes_ = totalBytes;
42   remaining_ = totalBytes;
43   action_ = action;
44 }
45 /*
46  * This function creates a flow from src to dst
47  *
48  * Parameters
49  *              src: node source
50  *              dst: node destination
51  *              port_number: The port number to use
52  *              start: the time the communication start
53  *              addr:  ip address
54  *              totalBytes: number of bytes to transmit
55  */
56 void NS3Sim::create_flow_NS3(ns3::Ptr<ns3::Node> src, ns3::Ptr<ns3::Node> dst, uint16_t port_number,
57                 double startTime, const char *ipAddr, uint32_t totalBytes,
58                 simgrid::surf::NetworkNS3Action * action)
59 {
60         ns3::PacketSinkHelper sink("ns3::TcpSocketFactory", ns3::InetSocketAddress (ns3::Ipv4Address::GetAny(), port_number));
61         sink.Install (dst);
62
63         ns3::Ptr<ns3::Socket> sock = ns3::Socket::CreateSocket (src, ns3::TcpSocketFactory::GetTypeId());
64
65         xbt_dict_set(flowFromSock, transformSocketPtr(sock), new SgFlow(totalBytes, action), NULL);
66
67         sock->Bind(ns3::InetSocketAddress(port_number));
68         XBT_DEBUG("Create flow starting to %fs + %fs = %fs",
69             startTime-ns3::Simulator::Now().GetSeconds(), ns3::Simulator::Now().GetSeconds(), startTime);
70
71         ns3::Simulator::Schedule (ns3::Seconds(startTime-ns3::Simulator::Now().GetSeconds()),
72             &StartFlow, sock, ipAddr, port_number);
73 }
74
75 void NS3Sim::simulator_start(double min){
76   if(min > 0.0)
77     ns3::Simulator::Stop(ns3::Seconds(min));
78   XBT_DEBUG("Start simulator '%f'",min);
79   ns3::Simulator::Run ();
80 }
81
82 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket) {
83         return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
84 }
85
86 static void receive_callback(ns3::Ptr<ns3::Socket> socket){
87   SgFlow* flow = getFlowFromSocket(socket);
88
89   if (flow->finished_ == false){
90     flow->finished_ = true;
91     XBT_DEBUG("recv_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
92     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
93     ns3::Simulator::Stop(ns3::Seconds(0.0));
94     ns3::Simulator::Run();
95   }
96 }
97
98 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace){
99         SgFlow* flow = getFlowFromSocket(sock);
100
101         if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
102           return;
103
104         uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
105
106         while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable () > 0) {
107
108       uint32_t toWrite = std::min ({flow->remaining_, txSpace, sock->GetTxAvailable ()});
109       int amountSent = sock->Send (data, toWrite, 0);
110
111       if(amountSent < 0)
112           return;
113       flow->bufferedBytes_ += amountSent;
114       flow->remaining_ -= amountSent;
115
116       XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", flow, flow->action_, flow->totalBytes_,
117           flow->remaining_, flow->totalBytes_, amountSent);
118     }
119         free(data);
120
121         if ((flow->bufferedBytes_) >= flow->totalBytes_)
122                 sock->Close();
123 }
124
125 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent){
126   SgFlow* flow = getFlowFromSocket(socket);
127   flow->sentBytes_ += dataSent;
128   XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", flow, flow->action_, flow->totalBytes_, dataSent);
129 }
130
131 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket){
132   SgFlow* flow = getFlowFromSocket(socket);
133   XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
134   receive_callback (socket);
135 }
136
137 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket){
138   SgFlow* flow = getFlowFromSocket(socket);
139   XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
140   xbt_die("NS3: a socket was closed anormally");
141 }
142
143 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket){
144   SgFlow* flow = getFlowFromSocket(socket);
145   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
146 }
147
148 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket){
149   SgFlow* mysocket = getFlowFromSocket(socket);
150   XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action_, mysocket->totalBytes_);
151   xbt_die("NS3: a socket failed to connect");
152 }
153
154 static void StartFlow(ns3::Ptr<ns3::Socket> sock, const char *to, uint16_t port_number)
155 {
156   ns3::InetSocketAddress serverAddr (to, port_number);
157
158   sock->Connect(serverAddr);
159   sock->SetSendCallback (MakeCallback (&send_callback));
160   sock->SetRecvCallback (MakeCallback (&receive_callback));
161   sock->SetDataSentCallback (MakeCallback (&datasent_callback));
162   sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
163   sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
164
165   SgFlow* flow = getFlowFromSocket(sock);
166   XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
167 }