1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/kernel/resource/models/ns3/ns3_simulator.hpp"
9 #include "xbt/sysdep.h"
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>
20 std::map<std::string, SgFlow*, std::less<>> flow_from_sock; // ns3::sock -> SgFlow
22 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
23 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_ns3);
27 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
29 auto it = flow_from_sock.find(transform_socket_ptr(socket));
30 return (it == flow_from_sock.end()) ? nullptr : it->second;
33 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
35 SgFlow* flow = getFlowFromSocket(socket);
36 XBT_DEBUG("received on F[%p, total: %u, remain: %u]", flow, flow->total_bytes_, flow->remaining_);
38 if (not flow->finished_) {
39 flow->finished_ = true;
40 XBT_DEBUG("recv_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
41 XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
42 ns3::Simulator::Stop();
46 static void send_cb(ns3::Ptr<ns3::Socket> sock, uint32_t /*txSpace*/)
48 SgFlow* flow = getFlowFromSocket(sock);
49 XBT_DEBUG("Asked to write on F[%p, total: %u, remain: %u]", flow, flow->total_bytes_, flow->remaining_);
51 if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
54 /* While not all is buffered and there remain space in the buffers */
55 while (flow->buffered_bytes_ < flow->total_bytes_ && sock->GetTxAvailable() > 0) {
56 // Send at most 1040 bytes (data size in a TCP packet), as ns-3 seems to not split correctly by itself
57 uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable(), std::uint32_t(1040)});
59 if (toWrite == 0) { // buffer full
60 XBT_DEBUG("%f: buffer full on flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), flow,
64 int amountSent = sock->Send(nullptr, toWrite, 0);
66 xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
67 flow->buffered_bytes_ += amountSent;
68 flow->remaining_ -= amountSent;
70 XBT_DEBUG("%f: sent %d bytes over flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), amountSent, flow,
74 if (flow->buffered_bytes_ >= flow->total_bytes_) {
75 XBT_DEBUG("Closing sender's socket of flow %p", flow);
80 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
82 /* The tracing wants to know */
83 SgFlow* flow = getFlowFromSocket(socket);
84 flow->sent_bytes_ += dataSent;
85 XBT_DEBUG("datasent_cb of F[%p, %p, %u] %u sent (%u total)", flow, flow->action_, flow->total_bytes_, dataSent,
89 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
91 SgFlow* flow = getFlowFromSocket(socket);
92 XBT_DEBUG("normalClose_cb of F[%p, %p] total: %u; sent: %u", flow, flow->action_, flow->total_bytes_,
94 xbt_assert(flow->total_bytes_ == flow->sent_bytes_, "total_bytes (=%u) is not sent_bytes(=%u)", flow->total_bytes_,
96 xbt_assert(flow->remaining_ == 0, "Remaining is not 0 but %u", flow->remaining_);
97 receive_callback(socket);
100 XBT_ATTRIB_NORETURN static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
102 SgFlow* flow = getFlowFromSocket(socket);
103 XBT_DEBUG("errorClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
104 xbt_die("ns-3: a socket was closed anormally");
107 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
109 SgFlow* flow = getFlowFromSocket(socket);
110 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
113 XBT_ATTRIB_NORETURN static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
115 SgFlow* mysocket = getFlowFromSocket(socket);
116 XBT_DEBUG("failedConnect_cb of F[%p, %p, %u]", mysocket, mysocket->action_, mysocket->total_bytes_);
117 xbt_die("ns-3: a socket failed to connect");
120 void start_flow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
122 SgFlow* flow = getFlowFromSocket(sock);
124 ns3::InetSocketAddress serverAddr(to, port_number);
125 sock->Connect(serverAddr);
127 // tell the network implementation to call send_cb again
128 // if we blocked and new tx buffer space becomes available
129 sock->SetSendCallback(MakeCallback(&send_cb));
130 // Notice when we actually sent some data (mostly for the TRACING module)
131 sock->SetDataSentCallback(MakeCallback(&datasent_cb));
133 XBT_DEBUG("startFlow of F[%p, %p, %u] dest=%s port=%d", flow, flow->action_, flow->total_bytes_, to, port_number);
135 sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
136 sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));