Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ns3: comment asserts that fail on clusters
[simgrid.git] / src / surf / ns3 / ns3_simulator.cpp
1 /* Copyright (c) 2007-2018. 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/surf/ns3/ns3_simulator.hpp"
7 #include "xbt/log.h"
8 #include "xbt/sysdep.h"
9
10 #include <ns3/ipv4-address-helper.h>
11 #include <ns3/point-to-point-helper.h>
12
13 #include <algorithm>
14
15 std::map<std::string, SgFlow*> flow_from_sock; // ns3::sock -> SgFlow
16
17 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
18 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
21
22 SgFlow::SgFlow(uint32_t totalBytes, simgrid::kernel::resource::NetworkNS3Action* action)
23 {
24   total_bytes_ = totalBytes;
25   remaining_  = totalBytes;
26   action_     = action;
27 }
28
29 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket)
30 {
31   auto it = flow_from_sock.find(transform_socket_ptr(socket));
32   return (it == flow_from_sock.end()) ? nullptr : it->second;
33 }
34
35 static void receive_callback(ns3::Ptr<ns3::Socket> socket)
36 {
37   SgFlow* flow = getFlowFromSocket(socket);
38   XBT_DEBUG("received on F[%p, total: %u, remain: %u]", flow, flow->total_bytes_, flow->remaining_);
39
40   if (flow->finished_ == false) {
41     flow->finished_ = true;
42     XBT_DEBUG("recv_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
43     XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
44     ns3::Simulator::Stop(ns3::Seconds(0.0));
45     ns3::Simulator::Run();
46   }
47 }
48
49 static void send_cb(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace)
50 {
51   SgFlow* flow = getFlowFromSocket(sock);
52   XBT_DEBUG("Asked to write on F[%p, total: %u, remain: %u]", flow, flow->total_bytes_, flow->remaining_);
53
54   if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
55     return;
56
57   /* While not all is buffered and there remain space in the buffers */
58   while (flow->buffered_bytes_ < flow->total_bytes_ && sock->GetTxAvailable() > 0) {
59
60     // Send at most 1040 bytes (data size in a TCP packet), as NS3 seems to not split correctly by itself
61     uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable(), std::uint32_t(1040)});
62     if (toWrite == 0) { // buffer full
63       XBT_DEBUG("%f: buffer full on flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), flow,
64                 flow->remaining_);
65       return;
66     }
67     int amountSent = sock->Send(0, toWrite, 0);
68
69     xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0");
70     flow->buffered_bytes_ += amountSent;
71     flow->remaining_ -= amountSent;
72
73     XBT_DEBUG("%f: sent %d bytes over flow %p (still %u to go)", ns3::Simulator::Now().GetSeconds(), amountSent, flow,
74               flow->remaining_);
75   }
76
77   if (flow->buffered_bytes_ >= flow->total_bytes_)
78     sock->Close();
79 }
80
81 static void datasent_cb(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent)
82 {
83   /* The tracing wants to know */
84   SgFlow* flow = getFlowFromSocket(socket);
85   flow->sent_bytes_ += dataSent;
86   XBT_DEBUG("datasent_cb of F[%p, %p, %u] %u sent (%u total)", flow, flow->action_, flow->total_bytes_, dataSent,
87             flow->sent_bytes_);
88 }
89
90 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket)
91 {
92   SgFlow* flow = getFlowFromSocket(socket);
93   XBT_DEBUG("normalClose_cb of F[%p, %p] total: %u; sent: %u", flow, flow->action_, flow->total_bytes_,
94             flow->sent_bytes_);
95   // xbt_assert(flow->total_bytes_ == flow->sent_bytes_,
96   //    "total_bytes (=%u) is not sent_bytes(=%u)", flow->total_bytes_ , flow->sent_bytes_);
97   // xbt_assert(flow->remaining_ == 0, "Remaining is not 0 but %u", flow->remaining_);
98   receive_callback(socket);
99 }
100
101 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket)
102 {
103   SgFlow* flow = getFlowFromSocket(socket);
104   XBT_DEBUG("errorClose_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
105   xbt_die("NS3: a socket was closed anormally");
106 }
107
108 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket)
109 {
110   SgFlow* flow = getFlowFromSocket(socket);
111   XBT_DEBUG("succeededConnect_cb of F[%p, %p, %u]", flow, flow->action_, flow->total_bytes_);
112 }
113
114 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket)
115 {
116   SgFlow* mysocket = getFlowFromSocket(socket);
117   XBT_DEBUG("failedConnect_cb of F[%p, %p, %u]", mysocket, mysocket->action_, mysocket->total_bytes_);
118   xbt_die("NS3: a socket failed to connect");
119 }
120
121 void start_flow(ns3::Ptr<ns3::Socket> sock, const char* to, uint16_t port_number)
122 {
123   SgFlow* flow = getFlowFromSocket(sock);
124   ns3::InetSocketAddress serverAddr(to, port_number);
125
126   sock->Connect(serverAddr);
127   // tell the tcp 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 the send is over
131   sock->SetRecvCallback(MakeCallback(&receive_callback));
132   // Notice when we actually sent some data (mostly for the TRACING module)
133   sock->SetDataSentCallback(MakeCallback(&datasent_cb));
134
135   XBT_DEBUG("startFlow of F[%p, %p, %u] dest=%s port=%d", flow, flow->action_, flow->total_bytes_, to, port_number);
136
137   sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback));
138   sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback));
139 }