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.h"
9 #include "xbt/sysdep.h"
13 xbt_dict_t flowFromSock = xbt_dict_new_homogeneous([](void *p) {
15 }); // ns3::sock -> SgFlow
17 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
18 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
22 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action* action)
24 totalBytes_ = totalBytes;
25 remaining_ = totalBytes;
29 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
31 return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
34 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
36 SgFlow* flow = getFlowFromSocket(socket);
38 if (flow->finished_ == false) {
39 flow->finished_ = true;
40 XBT_DEBUG("recv_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
41 XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
42 ns3::Simulator::Stop(ns3::Seconds(0.0));
43 ns3::Simulator::Run();
47 static void WriteUntilBufferFull(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
49 SgFlow* flow = getFlowFromSocket(sock);
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->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) {
57 uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable()});
58 if (toWrite == 0) // buffer full
60 int amountSent = sock->Send(0, toWrite, 0);
62 xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
63 flow->bufferedBytes_ += amountSent;
64 flow->remaining_ -= amountSent;
66 XBT_DEBUG("%f: sent %d bytes over flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), amountSent, flow,
70 if (flow->bufferedBytes_ >= flow->totalBytes_)
74 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
76 /* The tracing wants to know */
77 SgFlow* flow = getFlowFromSocket(socket);
78 flow->sentBytes_ += dataSent;
79 XBT_DEBUG("datasent_cb of F[%p, %p, %u] %u sent (%u total)", flow, flow->action_, flow->totalBytes_, dataSent,
83 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
85 SgFlow* flow = getFlowFromSocket(socket);
86 XBT_DEBUG("normalClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
87 receive_callback(socket);
90 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
92 SgFlow* flow = getFlowFromSocket(socket);
93 XBT_DEBUG("errorClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
94 xbt_die("NS3: a socket was closed anormally");
97 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
99 SgFlow* flow = getFlowFromSocket(socket);
100 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %u]", flow, flow->action_, flow->totalBytes_);
103 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
105 SgFlow* mysocket = getFlowFromSocket(socket);
106 XBT_DEBUG("failedConnect_cb of F[%p, %p, %u]", mysocket, mysocket->action_, mysocket->totalBytes_);
107 xbt_die("NS3: a socket failed to connect");
110 void StartFlow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
112 SgFlow* flow = getFlowFromSocket(sock);
113 ns3::InetSocketAddress serverAddr(to, port_number);
115 sock->Connect(serverAddr);
116 // tell the tcp implementation to call WriteUntilBufferFull again
117 // if we blocked and new tx buffer space becomes available
118 sock->SetSendCallback (MakeCallback(&WriteUntilBufferFull));
119 // Note when the send is over
120 sock->SetRecvCallback(MakeCallback(&receive_callback));
121 // Keep track of what was used (for the TRACING module)
122 sock->SetDataSentCallback(MakeCallback(&datasent_callback));
123 XBT_DEBUG("startFlow of F[%p, %p, %u] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);
125 //WriteUntilBufferFull (sock, sock->GetTxAvailable ());
127 sock->SetSendCallback(MakeCallback(&send_callback));
128 sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
129 sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
130 send_callback(sock, sock->GetTxAvailable ());