1 /* Copyright (c) 2007-2017. 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"
12 std::map<std::string, SgFlow*> flowFromSock; // ns3::sock -> SgFlow
14 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
15 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
19 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action* action)
21 totalBytes_ = totalBytes;
22 remaining_ = totalBytes;
26 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
28 auto it = flowFromSock.find(transformSocketPtr(socket));
29 return (it == flowFromSock.end()) ? nullptr : it->second;
32 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
34 SgFlow* flow = getFlowFromSocket(socket);
36 if (flow->finished_ == false) {
37 flow->finished_ = true;
38 XBT_DEBUG("recv_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
39 XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
40 ns3::Simulator::Stop(ns3::Seconds(0.0));
41 ns3::Simulator::Run();
45 static void WriteUntilBufferFull(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
47 SgFlow* flow = getFlowFromSocket(sock);
49 if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
52 /* While not all is buffered and there remain space in the buffers */
53 while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) {
55 uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable()});
56 if (toWrite == 0) // buffer full
58 int amountSent = sock->Send(0, toWrite, 0);
60 xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
61 flow->bufferedBytes_ += amountSent;
62 flow->remaining_ -= amountSent;
64 XBT_DEBUG("%f: sent %d bytes over flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), amountSent, flow,
68 if (flow->bufferedBytes_ >= flow->totalBytes_)
72 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
74 /* The tracing wants to know */
75 SgFlow* flow = getFlowFromSocket(socket);
76 flow->sentBytes_ += dataSent;
77 XBT_DEBUG("datasent_cb of F[%p, %p, %u] %u sent (%u total)", flow, flow->action_, flow->totalBytes_, dataSent,
81 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
83 SgFlow* flow = getFlowFromSocket(socket);
84 XBT_DEBUG("normalClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
85 receive_callback(socket);
88 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
90 SgFlow* flow = getFlowFromSocket(socket);
91 XBT_DEBUG("errorClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
92 xbt_die("NS3: a socket was closed anormally");
95 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
97 SgFlow* flow = getFlowFromSocket(socket);
98 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
101 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
103 SgFlow* mysocket = getFlowFromSocket(socket);
104 XBT_DEBUG("failedConnect_cb of F[%p, %p, %u]", mysocket, mysocket->action_, mysocket->totalBytes_);
105 xbt_die("NS3: a socket failed to connect");
108 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
110 SgFlow* flow = getFlowFromSocket(sock);
111 ns3::InetSocketAddress serverAddr(to, port_number);
113 sock->Connect(serverAddr);
114 // tell the tcp implementation to call WriteUntilBufferFull again
115 // if we blocked and new tx buffer space becomes available
116 sock->SetSendCallback(MakeCallback(&WriteUntilBufferFull));
117 // Note when the send is over
118 sock->SetRecvCallback(MakeCallback(&receive_callback));
119 // Keep track of what was used (for the TRACING module)
120 sock->SetDataSentCallback(MakeCallback(&datasent_callback));
121 XBT_DEBUG("startFlow of F[%p, %p, %u] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
123 // WriteUntilBufferFull (sock, sock->GetTxAvailable ());
125 sock->SetSendCallback(MakeCallback(&send_callback));
126 sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
127 sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
128 send_callback(sock, sock->GetTxAvailable ());