From 0181f52e5cd129a30bb94432bb8540d571aacd9f Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 12 Jun 2017 18:24:52 +0200 Subject: [PATCH] Simplify a bit the NS3/SimGrid interactions --- src/surf/network_ns3.cpp | 72 +++++++++++++++-------------------- src/surf/ns3/ns3_interface.h | 6 +-- src/surf/ns3/ns3_simulator.cc | 42 ++++++++++++-------- 3 files changed, 59 insertions(+), 61 deletions(-) diff --git a/src/surf/network_ns3.cpp b/src/surf/network_ns3.cpp index 928f3da55f..92cc0a5007 100644 --- a/src/surf/network_ns3.cpp +++ b/src/surf/network_ns3.cpp @@ -56,9 +56,6 @@ NetPointNs3::NetPointNs3() static void clusterCreation_cb(sg_platf_cluster_cbarg_t cluster) { - char* lat = bprintf("%fs", cluster->lat); - char* bw = bprintf("%fbps", cluster->bw * 8); - for (int i : *cluster->radicals) { // Routers don't create a router on the other end of the private link by themselves. // We just need this router to be given an ID so we create a temporary NetPointNS3 so that it gets one @@ -70,20 +67,14 @@ static void clusterCreation_cb(sg_platf_cluster_cbarg_t cluster) xbt_assert(host_src, "Cannot find a NS3 host of name %s", host_id); // Any NS3 route is symmetrical - ns3_add_link(host_src, host_dst, bw, lat); + ns3_add_link(host_src, host_dst, cluster->bw, cluster->lat); delete host_dst; free(host_id); } - xbt_free(lat); - xbt_free(bw); //Create link backbone - lat = bprintf("%fs", cluster->bb_lat); - bw = bprintf("%fBps", cluster->bb_bw); - ns3_add_cluster(cluster->id, bw, lat); - xbt_free(lat); - xbt_free(bw); + ns3_add_cluster(cluster->id, cluster->bb_bw, cluster->bb_lat); } static void routeCreation_cb(bool symmetrical, simgrid::kernel::routing::NetPoint* src, @@ -96,11 +87,10 @@ static void routeCreation_cb(bool symmetrical, simgrid::kernel::routing::NetPoin XBT_DEBUG("Route from '%s' to '%s' with link '%s' %s", src->cname(), dst->cname(), link->cname(), (symmetrical ? "(symmetrical)" : "(not symmetrical)")); - char* link_bdw = bprintf("%fbps", link->bandwidth() * 8); - char* link_lat = bprintf("%fs", link->latency()); // XBT_DEBUG("src (%s), dst (%s), src_id = %d, dst_id = %d",src,dst, src_id, dst_id); - XBT_DEBUG("\tLink (%s) bdw:%s lat:%s", link->cname(), link_bdw, link_lat); + XBT_DEBUG("\tLink (%s) bw:%fbps lat:%fs", link->cname(), link->bandwidth(), + link->latency()); // create link ns3 NetPointNs3* host_src = src->extension(); @@ -109,11 +99,7 @@ static void routeCreation_cb(bool symmetrical, simgrid::kernel::routing::NetPoin xbt_assert(host_src != nullptr, "Network element %s does not seem to be NS3-ready", src->cname()); xbt_assert(host_dst != nullptr, "Network element %s does not seem to be NS3-ready", dst->cname()); - // Any NS3 route is symmetrical - ns3_add_link(host_src, host_dst, link_bdw, link_lat); - - xbt_free(link_bdw); - xbt_free(link_lat); + ns3_add_link(host_src, host_dst, link->bandwidth(), link->latency()); } else { static bool warned_about_long_routes = false; @@ -150,8 +136,7 @@ void surf_network_model_init_NS3() } static simgrid::config::Flag ns3_tcp_model("ns3/TcpModel", - "The ns3 tcp model can be : NewReno or Reno or Tahoe", - "default"); + "The ns3 tcp model can be : NewReno or Reno or Tahoe", "NewReno"); namespace simgrid { namespace surf { @@ -169,6 +154,7 @@ NetworkNS3Model::NetworkNS3Model() : NetworkModel() { simgrid::s4u::onPlatformCreated.connect(&postparse_cb); simgrid::s4u::NetZone::onRouteCreation.connect(&routeCreation_cb); + LogComponentEnable("PacketSink", ns3::LOG_LEVEL_INFO); LogComponentEnable("UdpEchoClientApplication", ns3::LOG_LEVEL_INFO); LogComponentEnable("UdpEchoServerApplication", ns3::LOG_LEVEL_INFO); } @@ -304,7 +290,7 @@ NetworkNS3Action::NetworkNS3Action(Model* model, double size, s4u::Host* src, s4 src_ = src; dst_ = dst; - ns3_create_flow(src, dst, surf_get_clock(), size, this); + ns3_create_flow(src, dst, size, this); s4u::Link::onCommunicate(this, src, dst); } @@ -352,9 +338,9 @@ void ns3_simulator(double maxSeconds) ns3::Simulator::Run (); } -void ns3_create_flow(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, double startTime, u_int32_t TotalBytes, - simgrid::surf::NetworkNS3Action* action) -{ +void ns3_create_flow(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, + u_int32_t TotalBytes, + simgrid::surf::NetworkNS3Action* action) { unsigned int node1 = src->pimpl_netpoint->extension()->node_num; unsigned int node2 = dst->pimpl_netpoint->extension()->node_num; @@ -371,16 +357,13 @@ void ns3_create_flow(simgrid::s4u::Host* src, simgrid::s4u::Host* dst, double st ns3::PacketSinkHelper sink("ns3::TcpSocketFactory", ns3::InetSocketAddress (ns3::Ipv4Address::GetAny(), port_number)); sink.Install (dst_node); - ns3::Ptr sock = ns3::Socket::CreateSocket (src_node, ns3::TcpSocketFactory::GetTypeId()); + ns3::Ptr sock = ns3::Socket::CreateSocket(src_node, ns3::TcpSocketFactory::GetTypeId()); xbt_dict_set(flowFromSock, transformSocketPtr(sock), new SgFlow(TotalBytes, action), nullptr); sock->Bind(ns3::InetSocketAddress(port_number)); - XBT_DEBUG("Create flow starting to %fs + %fs = %fs", - startTime-ns3::Simulator::Now().GetSeconds(), ns3::Simulator::Now().GetSeconds(), startTime); - - ns3::Simulator::Schedule (ns3::Seconds(startTime-ns3::Simulator::Now().GetSeconds()), - &StartFlow, sock, addr, port_number); + + ns3::Simulator::ScheduleNow(&StartFlow, sock, addr, port_number); port_number++; xbt_assert(port_number <= 65000, "Too many connections! Port number is saturated."); @@ -393,8 +376,9 @@ void ns3_initialize(const char* TcpProtocol){ // "ns3::TcpReno" // "ns3::TcpTahoe" - ns3::Config::SetDefault ("ns3::TcpSocket::SegmentSize", ns3::UintegerValue (1024)); // 1024-byte packet for easier reading + ns3::Config::SetDefault ("ns3::TcpSocket::SegmentSize", ns3::UintegerValue (1000)); ns3::Config::SetDefault ("ns3::TcpSocket::DelAckCount", ns3::UintegerValue (1)); + ns3::Config::SetDefault ("ns3::TcpSocketBase::Timestamp", ns3::BooleanValue (false)); if (not strcmp(TcpProtocol, "default")) return; @@ -418,8 +402,7 @@ void ns3_initialize(const char* TcpProtocol){ xbt_die("The ns3/TcpModel must be : NewReno or Reno or Tahoe"); } -void ns3_add_cluster(const char* id, char* bw, char* lat) -{ +void ns3_add_cluster(const char* id, double bw, double lat) { ns3::NodeContainer Nodes; for (unsigned int i = number_of_clusters_nodes; i < Cluster_nodes.GetN(); i++) { @@ -433,9 +416,9 @@ void ns3_add_cluster(const char* id, char* bw, char* lat) xbt_assert(Nodes.GetN() <= 65000, "Cluster with NS3 is limited to 65000 nodes"); ns3::CsmaHelper csma; - csma.SetChannelAttribute ("DataRate", ns3::StringValue (bw)); - csma.SetChannelAttribute ("Delay", ns3::StringValue (lat)); - ns3::NetDeviceContainer devices = csma.Install (Nodes); + csma.SetDeviceAttribute("DataRate", ns3::DataRateValue(ns3::DataRate(bw*8)));// NS3 takes bps, but we provide Bps + csma.SetChannelAttribute("Delay", ns3::TimeValue(ns3::Seconds(lat))); + ns3::NetDeviceContainer devices = csma.Install(Nodes); XBT_DEBUG("Create CSMA"); char * adr = bprintf("%d.%d.0.0",number_of_networks,number_of_links); @@ -462,8 +445,7 @@ static char* transformIpv4Address (ns3::Ipv4Address from){ return bprintf("%s",s.c_str()); } -void ns3_add_link(NetPointNs3* src, NetPointNs3* dst, char* bw, char* lat) -{ +void ns3_add_link(NetPointNs3* src, NetPointNs3* dst, double bw, double lat) { ns3::PointToPointHelper pointToPoint; ns3::Ipv4AddressHelper address; @@ -474,9 +456,15 @@ void ns3_add_link(NetPointNs3* src, NetPointNs3* dst, char* bw, char* lat) ns3::Ptr a = nodes.Get(srcNum); ns3::Ptr b = nodes.Get(dstNum); - XBT_DEBUG("\tAdd PTP from %d to %d bw:'%s' lat:'%s'", srcNum, dstNum, bw, lat); - pointToPoint.SetDeviceAttribute ("DataRate", ns3::StringValue (bw)); - pointToPoint.SetChannelAttribute ("Delay", ns3::StringValue (lat)); + XBT_DEBUG("\tAdd PTP from %d to %d bw:'%f Bps' lat:'%fs'", srcNum, dstNum, bw, lat); + pointToPoint.SetDeviceAttribute("DataRate", ns3::DataRateValue(ns3::DataRate(bw*8)));// NS3 takes bps, but we provide Bps + pointToPoint.SetChannelAttribute("Delay", ns3::TimeValue(ns3::Seconds(lat))); + + char *filename = bprintf("link-%d-%d.tr", srcNum, dstNum); + ns3::AsciiTraceHelper ascii; + pointToPoint.EnableAsciiAll (ascii.CreateFileStream (filename)); + pointToPoint.EnablePcapAll ("tcp-bulk-send", false); + xbt_free(filename); ns3::NetDeviceContainer netA; netA.Add(pointToPoint.Install (a, b)); diff --git a/src/surf/ns3/ns3_interface.h b/src/surf/ns3/ns3_interface.h index b35d8680e7..9479f656f9 100644 --- a/src/surf/ns3/ns3_interface.h +++ b/src/surf/ns3/ns3_interface.h @@ -26,12 +26,12 @@ SG_BEGIN_DECL() XBT_PUBLIC(void) ns3_initialize(const char* TcpProtocol); XBT_PUBLIC(void) -ns3_create_flow(sg_host_t src, sg_host_t dst, double start, u_int32_t TotalBytes, +ns3_create_flow(sg_host_t src, sg_host_t dst, u_int32_t TotalBytes, simgrid::surf::NetworkNS3Action* action); XBT_PUBLIC(void) ns3_simulator(double maxSeconds); XBT_PUBLIC(void*) ns3_add_router(const char* id); -XBT_PUBLIC(void) ns3_add_link(NetPointNs3* src, NetPointNs3* dst, char* bw, char* lat); -XBT_PUBLIC(void) ns3_add_cluster(const char* id, char* bw, char* lat); +XBT_PUBLIC(void) ns3_add_link(NetPointNs3* src, NetPointNs3* dst, double bw, double lat); +XBT_PUBLIC(void) ns3_add_cluster(const char* id, double bw, double lat); SG_END_DECL() diff --git a/src/surf/ns3/ns3_simulator.cc b/src/surf/ns3/ns3_simulator.cc index 7aa706d211..b27b852bdd 100644 --- a/src/surf/ns3/ns3_simulator.cc +++ b/src/surf/ns3/ns3_simulator.cc @@ -15,7 +15,6 @@ xbt_dict_t flowFromSock = xbt_dict_new_homogeneous([](void *p) { }); // ns3::sock -> SgFlow static void receive_callback(ns3::Ptr socket); -static void send_callback(ns3::Ptr sock, uint32_t txSpace); static void datasent_callback(ns3::Ptr socket, uint32_t dataSent); XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ns3); @@ -45,39 +44,40 @@ static void receive_callback(ns3::Ptr socket) } } -static void send_callback(ns3::Ptr sock, uint32_t txSpace) +static void WriteUntilBufferFull(ns3::Ptr sock, uint32_t txSpace) { SgFlow* flow = getFlowFromSocket(sock); if (flow->remaining_ == 0) // all data was already buffered (and socket was already closed) return; - uint8_t* data = (uint8_t*)malloc(sizeof(uint8_t) * txSpace); - + /* While not all is buffered and there remain space in the buffers */ while (flow->bufferedBytes_ < flow->totalBytes_ && sock->GetTxAvailable() > 0) { - uint32_t toWrite = std::min({flow->remaining_, txSpace, sock->GetTxAvailable()}); - int amountSent = sock->Send(data, toWrite, 0); - - if (amountSent < 0) + uint32_t toWrite = std::min({flow->remaining_, sock->GetTxAvailable()}); + if (toWrite == 0) // buffer full return; + int amountSent = sock->Send(0, toWrite, 0); + + xbt_assert(amountSent > 0, "Since TxAvailable>0, amountSent should also >0"); flow->bufferedBytes_ += amountSent; flow->remaining_ -= amountSent; - XBT_DEBUG("send_cb of F[%p, %p, %d] (%d/%d) %d buffered", flow, flow->action_, flow->totalBytes_, flow->remaining_, - flow->totalBytes_, amountSent); + XBT_DEBUG("%f: sent %d bytes over flow %p (still %d to go)", + ns3::Simulator::Now().GetSeconds(), amountSent, flow, flow->remaining_); } - free(data); - if ((flow->bufferedBytes_) >= flow->totalBytes_) + if (flow->bufferedBytes_ >= flow->totalBytes_) sock->Close(); } static void datasent_callback(ns3::Ptr socket, uint32_t dataSent) { + /* The tracing wants to know */ SgFlow* flow = getFlowFromSocket(socket); flow->sentBytes_ += dataSent; - XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent", flow, flow->action_, flow->totalBytes_, dataSent); + XBT_DEBUG("datasent_cb of F[%p, %p, %d] %d sent (%d total)", + flow, flow->action_, flow->totalBytes_, dataSent, flow->sentBytes_); } static void normalClose_callback(ns3::Ptr socket) @@ -109,15 +109,25 @@ static void failedConnect_callback(ns3::Ptr socket) void StartFlow(ns3::Ptr sock, const char* to, uint16_t port_number) { + SgFlow* flow = getFlowFromSocket(sock); ns3::InetSocketAddress serverAddr(to, port_number); sock->Connect(serverAddr); - sock->SetSendCallback(MakeCallback(&send_callback)); + // tell the tcp implementation to call WriteUntilBufferFull again + // if we blocked and new tx buffer space becomes available + sock->SetSendCallback (MakeCallback(&WriteUntilBufferFull)); + // Note when the send is over sock->SetRecvCallback(MakeCallback(&receive_callback)); + // Keep track of what was used (for the TRACING module) sock->SetDataSentCallback(MakeCallback(&datasent_callback)); + XBT_DEBUG("startFlow of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number); + + //WriteUntilBufferFull (sock, sock->GetTxAvailable ()); + /* + sock->SetSendCallback(MakeCallback(&send_callback)); sock->SetConnectCallback(MakeCallback(&succeededConnect_callback), MakeCallback(&failedConnect_callback)); sock->SetCloseCallbacks(MakeCallback(&normalClose_callback), MakeCallback(&errorClose_callback)); + send_callback(sock, sock->GetTxAvailable ()); + */ - SgFlow* flow = getFlowFromSocket(sock); - XBT_DEBUG("startFlow_cb of F[%p, %p, %d] dest=%s port=%d", flow, flow->action_, flow->totalBytes_, to, port_number); } -- 2.20.1