Logo AND Algorithmique Numérique Distribuée

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