1 /* Copyright (c) 2007-2016. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "src/surf/ns3/ns3_simulator.h"
10 #include "xbt/sysdep.h"
12 static void delete_mysocket(void *p)
16 xbt_dict_t flowFromSock = xbt_dict_new_homogeneous(delete_mysocket);; // ns3::sock -> SgFlow
20 static void receive_callback(ns3::Ptr<ns3::Socket> socket);
21 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace);
22 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent);
23 static void StartFlow(ns3::Ptr<ns3::Socket> sock, const char *to, uint16_t port_number);
25 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3);
30 static inline const char *transformSocketPtr (ns3::Ptr<ns3::Socket> localSocket)
33 std::stringstream sstream;
34 sstream << localSocket ;
35 sprintf(key,"%s",sstream.str().c_str());
40 SgFlow::SgFlow(uint32_t totalBytes, simgrid::surf::NetworkNS3Action * action) {
41 totalBytes_ = totalBytes;
42 remaining_ = totalBytes;
46 * This function creates a flow from src to dst
50 * dst: node destination
51 * port_number: The port number to use
52 * start: the time the communication start
54 * totalBytes: number of bytes to transmit
56 void NS3Sim::create_flow_NS3(ns3::Ptr<ns3::Node> src, ns3::Ptr<ns3::Node> dst, uint16_t port_number,
57 double startTime, const char *ipAddr, uint32_t totalBytes,
58 simgrid::surf::NetworkNS3Action * action)
60 ns3::PacketSinkHelper sink("ns3::TcpSocketFactory", ns3::InetSocketAddress (ns3::Ipv4Address::GetAny(), port_number));
63 ns3::Ptr<ns3::Socket> sock = ns3::Socket::CreateSocket (src, ns3::TcpSocketFactory::GetTypeId());
65 xbt_dict_set(flowFromSock, transformSocketPtr(sock), new SgFlow(totalBytes, action), NULL);
67 sock->Bind(ns3::InetSocketAddress(port_number));
68 XBT_DEBUG("Create flow starting to %fs + %fs = %fs",
69 startTime-ns3::Simulator::Now().GetSeconds(), ns3::Simulator::Now().GetSeconds(), startTime);
71 ns3::Simulator::Schedule (ns3::Seconds(startTime-ns3::Simulator::Now().GetSeconds()),
72 &StartFlow, sock, ipAddr, port_number);
75 void NS3Sim::simulator_start(double min){
77 ns3::Simulator::Stop(ns3::Seconds(min));
78 XBT_DEBUG("Start simulator '%f'",min);
79 ns3::Simulator::Run ();
82 static SgFlow* getFlowFromSocket(ns3::Ptr<ns3::Socket> socket) {
83 return (SgFlow*)xbt_dict_get_or_null(flowFromSock, transformSocketPtr(socket));
86 static void receive_callback(ns3::Ptr<ns3::Socket> socket){
87 SgFlow* flow = getFlowFromSocket(socket);
89 if (flow->finished_ == false){
90 flow->finished_ = true;
91 XBT_DEBUG("recv_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
92 XBT_DEBUG("Stop simulator at %f seconds", ns3::Simulator::Now().GetSeconds());
93 ns3::Simulator::Stop(ns3::Seconds(0.0));
94 ns3::Simulator::Run();
98 static void send_callback(ns3::Ptr<ns3::Socket> sock, uint32_t txSpace){
99 SgFlow* flow = getFlowFromSocket(sock);
101 if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed)
104 uint8_t *data = (uint8_t*)malloc(sizeof(uint8_t)*txSpace);
106 while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable () > 0) {
108 uint32_t toWrite = std::min ({flow->remaining_, txSpace, sock->GetTxAvailable ()});
109 int amountSent = sock->Send (data, toWrite, 0);
113 flow->bufferedBytes_ += amountSent;
114 flow->remaining_ -= amountSent;
116 XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", flow, flow->action_, flow->totalBytes_,
117 flow->remaining_, flow->totalBytes_, amountSent);
121 if ((flow->bufferedBytes_) >= flow->totalBytes_)
125 static void datasent_callback(ns3::Ptr<ns3::Socket> socket, uint32_t dataSent){
126 SgFlow* flow = getFlowFromSocket(socket);
127 flow->sentBytes_ += dataSent;
128 XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", flow, flow->action_, flow->totalBytes_, dataSent);
131 static void normalClose_callback(ns3::Ptr<ns3::Socket> socket){
132 SgFlow* flow = getFlowFromSocket(socket);
133 XBT_DEBUG("normalClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
134 receive_callback (socket);
137 static void errorClose_callback(ns3::Ptr<ns3::Socket> socket){
138 SgFlow* flow = getFlowFromSocket(socket);
139 XBT_DEBUG("errorClose_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
140 xbt_die("NS3: a socket was closed anormally");
143 static void succeededConnect_callback(ns3::Ptr<ns3::Socket> socket){
144 SgFlow* flow = getFlowFromSocket(socket);
145 XBT_DEBUG("succeededConnect_cb of F[%p, %p, %d]", flow, flow->action_, flow->totalBytes_);
148 static void failedConnect_callback(ns3::Ptr<ns3::Socket> socket){
149 SgFlow* mysocket = getFlowFromSocket(socket);
150 XBT_DEBUG("failedConnect_cb of F[%p, %p, %d]", mysocket, mysocket->action_, mysocket->totalBytes_);
151 xbt_die("NS3: a socket failed to connect");
154 static void StartFlow(ns3::Ptr<ns3::Socket> sock, const char *to, uint16_t port_number)
156 ns3::InetSocketAddress serverAddr (to, port_number);
158 sock->Connect(serverAddr);
159 sock->SetSendCallback (MakeCallback (&send_callback));
160 sock->SetRecvCallback (MakeCallback (&receive_callback));
161 sock->SetDataSentCallback (MakeCallback (&datasent_callback));
162 sock->SetConnectCallback (MakeCallback (&succeededConnect_callback), MakeCallback (&failedConnect_callback));
163 sock->SetCloseCallbacks (MakeCallback (&normalClose_callback), MakeCallback (&errorClose_callback));
165 SgFlow* flow = getFlowFromSocket(sock);
166 XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number);