Logo AND Algorithmique Numérique Distribuée

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