Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into 'master'
[simgrid.git] / src / surf / ns3 / ns3_simulator.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/surf/ns3/ns3_simulator.hpp"
7 #include "xbt/log.h"
8 #include "xbt/sysdep.h"
9
10 #include <ns3/ipv4-address-helper.h>
11 #include <ns3/point-to-point-helper.h>
12 #include <ns3/application-container.h>
13 #include <ns3/ptr.h>
14 #include <ns3/callback.h>
15 #include <ns3/packet-sink.h>
16
17 #include <algorithm>
18
19 std::map<std::string, SgFlow*> flow_from_sock; // ns3::sock -> SgFlow
20 std::map<std::string, ns3::ApplicationContainer> sink_from_sock; // ns3::sock -> ns3::PacketSink
21
22 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
23 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
24
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
26
27 SgFlow::SgFlow(uint32_t totalBytes, simgrid::kernel::resource::NetworkNS3Action* action)
28 {
29   total_bytes_ = totalBytes;
30   remaining_  = totalBytes;
31   action_     = action;
32 }
33
34 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
35 {
36   auto it = flow_from_sock.find(transform_socket_ptr(socket));
37   return (it == flow_from_sock.end()) ? nullptr : it->second;
38 }
39
40 static ns3::ApplicationContainer* getSinkFromSocket(ns3::Ptr<ns3::Socket> socket)
41 {
42   auto it = sink_from_sock.find(transform_socket_ptr(socket));
43   return (it == sink_from_sock.end()) ? nullptr : &(it->second);
44 }
45
46 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
47 {
48   SgFlow* flow = getFlowFromSocket(socket);
49   XBT_DEBUG("received on F[%p, total: %u, remain: %u]", flow, flow->total_bytes_, flow->remaining_);
50
51   if (flow->finished_ == false) {
52     flow->finished_ = true;
53     XBT_DEBUG("recv_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
54     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
55     ns3::Simulator::Stop(ns3::Seconds(0.0));
56     ns3::Simulator::Run();
57   }
58 }
59
60 static void send_cb(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
61 {
62   SgFlow* flow = getFlowFromSocket(sock);
63   ns3::ApplicationContainer* sink = getSinkFromSocket(sock);
64   XBT_DEBUG("Asked to write on F[%p, total: %u, remain: %u]", flow, flow->total_bytes_, flow->remaining_);
65
66   if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
67     return;
68
69   /* While not all is buffered and there remain space in the buffers */
70   while (flow->buffered_bytes_ < flow->total_bytes_ && sock->GetTxAvailable() > 0) {
71
72     // Send at most 1040 bytes (data size in a TCP packet), as ns-3 seems to not split correctly by itself
73     uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable(), std::uint32_t(1040)});
74     if (toWrite == 0) { // buffer full
75       XBT_DEBUG("%f: buffer full on flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), flow,
76                 flow->remaining_);
77       return;
78     }
79     int amountSent = sock->Send(0, toWrite, 0);
80
81     xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
82     flow->buffered_bytes_ += amountSent;
83     flow->remaining_ -= amountSent;
84
85     XBT_DEBUG("%f: sent %d bytes over flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), amountSent, flow,
86               flow->remaining_);
87   }
88
89   if (flow->buffered_bytes_ >= flow->total_bytes_){
90     XBT_DEBUG("Closing Sockets of flow %p", flow);
91     // Closing the sockets of the receiving application
92     ns3::Ptr<ns3::PacketSink> app = ns3::DynamicCast<ns3::PacketSink, ns3::Application>(sink->Get(0));
93     ns3::Ptr<ns3::Socket> listening_sock = app->GetListeningSocket();
94     listening_sock->Close();
95     listening_sock->SetRecvCallback(ns3::MakeNullCallback<void, ns3::Ptr<ns3::Socket>>());
96     for(ns3::Ptr<ns3::Socket> accepted_sock : app->GetAcceptedSockets())
97       accepted_sock->Close();
98     // Closing the socket of the sender
99     sock->Close();
100   }
101 }
102
103 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
104 {
105   /* The tracing wants to know */
106   SgFlow* flow = getFlowFromSocket(socket);
107   flow->sent_bytes_ += dataSent;
108   XBT_DEBUG("datasent_cb of F[%p, %p, %u] %u sent (%u total)", flow, flow->action_, flow->total_bytes_, dataSent,
109             flow->sent_bytes_);
110 }
111
112 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
113 {
114   SgFlow* flow = getFlowFromSocket(socket);
115   XBT_DEBUG("normalClose_cb of F[%p, %p] total: %u; sent: %u", flow, flow->action_, flow->total_bytes_,
116             flow->sent_bytes_);
117   // xbt_assert(flow->total_bytes_ == flow->sent_bytes_,
118   //    "total_bytes (=%u) is not sent_bytes(=%u)", flow->total_bytes_ , flow->sent_bytes_);
119   // xbt_assert(flow->remaining_ == 0, "Remaining is not 0 but %u", flow->remaining_);
120   receive_callback(socket);
121 }
122
123 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
124 {
125   SgFlow* flow = getFlowFromSocket(socket);
126   XBT_DEBUG("errorClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
127   xbt_die("ns-3: a socket was closed anormally");
128 }
129
130 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
131 {
132   SgFlow* flow = getFlowFromSocket(socket);
133   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
134 }
135
136 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
137 {
138   SgFlow* mysocket = getFlowFromSocket(socket);
139   XBT_DEBUG("failedConnect_cb of F[%p, %p, %u]", mysocket, mysocket->action_, mysocket->total_bytes_);
140   xbt_die("ns-3: a socket failed to connect");
141 }
142
143 void start_flow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
144 {
145   SgFlow* flow = getFlowFromSocket(sock);
146   ns3::InetSocketAddress serverAddr(to, port_number);
147
148   sock->Connect(serverAddr);
149   // tell the tcp implementation to call send_cb again
150   // if we blocked and new tx buffer space becomes available
151   sock->SetSendCallback(MakeCallback(&send_cb));
152   // Notice when we actually sent some data (mostly for the TRACING module)
153   sock->SetDataSentCallback(MakeCallback(&datasent_cb));
154
155   XBT_DEBUG("startFlow of F[%p, %p, %u] dest=%s port=%d", flow, flow->action_, flow->total_bytes_, to, port_number);
156
157   sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
158   sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
159 }