Logo AND Algorithmique Numérique Distribuée

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