1 /* Copyright (c) 2007-2018. 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/surf/ns3/ns3_simulator.hpp"
8 #include "xbt/sysdep.h"
10 #include <ns3/ipv4-address-helper.h>
11 #include <ns3/point-to-point-helper.h>
15 std::map<std::string, SgFlow*> flowFromSock; // ns3::sock -> SgFlow
17 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
18 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
22 SgFlow::SgFlow(uint32_t totalBytes, simgrid::kernel::resource::NetworkNS3Action* action)
24 totalBytes_ = totalBytes;
25 remaining_ = totalBytes;
29 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
31 auto it = flowFromSock.find(transformSocketPtr(socket));
32 return (it == flowFromSock.end()) ? nullptr : it->second;
35 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
37 SgFlow* flow = getFlowFromSocket(socket);
38 XBT_DEBUG("received on F[%p, total: %u, remain: %u]", flow, flow->totalBytes_, flow->remaining_);
40 if (flow->finished_ == false) {
41 flow->finished_ = true;
42 XBT_DEBUG("recv_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
43 XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
44 ns3::Simulator::Stop(ns3::Seconds(0.0));
45 ns3::Simulator::Run();
49 static void send_cb(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
51 SgFlow* flow = getFlowFromSocket(sock);
52 XBT_DEBUG("Asked to write on F[%p, total: %u, remain: %u]", flow, flow->totalBytes_, flow->remaining_);
54 if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
57 /* While not all is buffered and there remain space in the buffers */
58 while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) {
60 uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable()});
61 if (toWrite == 0) { // buffer full
62 XBT_DEBUG("%f: buffer full on flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), flow,
66 int amountSent = sock->Send(0, toWrite, 0);
68 xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
69 flow->bufferedBytes_ += amountSent;
70 flow->remaining_ -= amountSent;
72 XBT_DEBUG("%f: sent %d bytes over flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), amountSent, flow,
76 if (flow->bufferedBytes_ >= flow->totalBytes_)
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->sentBytes_ += dataSent;
85 XBT_DEBUG("datasent_cb of F[%p, %p, %u] %u sent (%u total)", flow, flow->action_, flow->totalBytes_, 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, %u]", flow, flow->action_, flow->totalBytes_);
93 receive_callback(socket);
96 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
98 SgFlow* flow = getFlowFromSocket(socket);
99 XBT_DEBUG("errorClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
100 xbt_die("NS3: a socket was closed anormally");
103 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
105 SgFlow* flow = getFlowFromSocket(socket);
106 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
109 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
111 SgFlow* mysocket = getFlowFromSocket(socket);
112 XBT_DEBUG("failedConnect_cb of F[%p, %p, %u]", mysocket, mysocket->action_, mysocket->totalBytes_);
113 xbt_die("NS3: a socket failed to connect");
116 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
118 SgFlow* flow = getFlowFromSocket(sock);
119 ns3::InetSocketAddress serverAddr(to, port_number);
121 sock->Connect(serverAddr);
122 // tell the tcp implementation to call send_cb again
123 // if we blocked and new tx buffer space becomes available
124 sock->SetSendCallback(MakeCallback(&send_cb));
125 // Notice when the send is over
126 sock->SetRecvCallback(MakeCallback(&receive_callback));
127 // Notice when we actually sent some data (mostly for the TRACING module)
128 sock->SetDataSentCallback(MakeCallback(&datasent_cb));
130 XBT_DEBUG("startFlow of F[%p, %p, %u] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
132 sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
133 sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));