如何在 NS-3 中使用 TCP 获取丢失的数据包数? (80211n + MIMO 示例)
How to get the number of lost packets using TCP in NS-3 ? (80211n + MIMO example)
我正在使用这个例子:https://github.com/nsnam/ns-3-dev-git/blob/master/examples/wireless/80211n-mimo.cc。
对于 UDP,在 UdpServer 中有一个名为 "GetLost()" 的函数,它 returns 丢失数据包的数量,但对于 TCP 则没有。我问是否有任何解决方案可以解决这个问题。谢谢
/* Setting applications */
ApplicationContainer serverApp;
if (udp)
{
//UDP flow
uint16_t port = 9;
UdpServerHelper server (port);
serverApp = server.Install (wifiStaNode.Get (0));
serverApp.Start (Seconds (0.0));
serverApp.Stop (Seconds (simulationTime + 1));
UdpClientHelper client (staNodeInterface.GetAddress (0), port);
client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
clientApp.Start (Seconds (1.0));
clientApp.Stop (Seconds (simulationTime + 1));
}
else
{
//TCP flow
uint16_t port = 50000;
Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
serverApp = packetSinkHelper.Install (wifiStaNode.Get (0));
serverApp.Start (Seconds (0.0));
serverApp.Stop (Seconds (simulationTime + 1));
OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
onoff.SetAttribute ("Remote", remoteAddress);
ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
clientApp.Start (Seconds (1.0));
clientApp.Stop (Seconds (simulationTime + 1));
}
Simulator::Stop (Seconds (simulationTime + 1));
Simulator::Run ();
uint64_t nb = 0;
uint64_t lost = 0;
if (udp)
{
//UDP
nb = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
lost = DynamicCast<UdpServer> (serverApp.Get (0))->GetLost();
double window = DynamicCast<UdpServer> (serverApp.Get (0))->GetPacketWindowSize();
std::cout << "Window Size :" << window << std::endl;
}
else
{
//TCP
uint64_t totalPacketsThrough = DynamicCast<PacketSink> (serverApp.Get (0))->GetTotalRx ();
nb = totalPacketsThrough / payloadSize ;
/*need help here to get the number of lost packets using TCP Thanks*/
}
dataset1.Add (d, nb);
dataset2.Add (d, lost);
std::cout << nb << " Received packets" << std::endl;
std::cout << lost << " Lost packets" << std::endl;
d += step;
Simulator::Destroy ();
}
plot1.AddDataset (dataset1);
plot2.AddDataset (dataset2);
}
每次传输数据包时,使用 "Tx" 跟踪 OnOffApplication 调用函数 -
void SourceUdpTrace(Ptr<const Packet> pkt)
{
nPacketsSent++;
}
在 main() 内部,使用以下 Config::Connect 语句设置跟踪并 link 使用 SourceUdpTrace
。此语句必须放在 Simulator::Run()
之前
Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::ManualOnOffApp/Tx",MakeCallback(&SourceUdpTrace));
一旦知道已发送数据包的数量,就可以通过将其与 nb
的值进行比较来轻松确定丢失的数据包数量,正如您的代码行 - nb = totalPacketsThrough / payloadSize;
我正在使用这个例子:https://github.com/nsnam/ns-3-dev-git/blob/master/examples/wireless/80211n-mimo.cc。 对于 UDP,在 UdpServer 中有一个名为 "GetLost()" 的函数,它 returns 丢失数据包的数量,但对于 TCP 则没有。我问是否有任何解决方案可以解决这个问题。谢谢
/* Setting applications */
ApplicationContainer serverApp;
if (udp)
{
//UDP flow
uint16_t port = 9;
UdpServerHelper server (port);
serverApp = server.Install (wifiStaNode.Get (0));
serverApp.Start (Seconds (0.0));
serverApp.Stop (Seconds (simulationTime + 1));
UdpClientHelper client (staNodeInterface.GetAddress (0), port);
client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
clientApp.Start (Seconds (1.0));
clientApp.Stop (Seconds (simulationTime + 1));
}
else
{
//TCP flow
uint16_t port = 50000;
Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
serverApp = packetSinkHelper.Install (wifiStaNode.Get (0));
serverApp.Start (Seconds (0.0));
serverApp.Stop (Seconds (simulationTime + 1));
OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
onoff.SetAttribute ("Remote", remoteAddress);
ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
clientApp.Start (Seconds (1.0));
clientApp.Stop (Seconds (simulationTime + 1));
}
Simulator::Stop (Seconds (simulationTime + 1));
Simulator::Run ();
uint64_t nb = 0;
uint64_t lost = 0;
if (udp)
{
//UDP
nb = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
lost = DynamicCast<UdpServer> (serverApp.Get (0))->GetLost();
double window = DynamicCast<UdpServer> (serverApp.Get (0))->GetPacketWindowSize();
std::cout << "Window Size :" << window << std::endl;
}
else
{
//TCP
uint64_t totalPacketsThrough = DynamicCast<PacketSink> (serverApp.Get (0))->GetTotalRx ();
nb = totalPacketsThrough / payloadSize ;
/*need help here to get the number of lost packets using TCP Thanks*/
}
dataset1.Add (d, nb);
dataset2.Add (d, lost);
std::cout << nb << " Received packets" << std::endl;
std::cout << lost << " Lost packets" << std::endl;
d += step;
Simulator::Destroy ();
}
plot1.AddDataset (dataset1);
plot2.AddDataset (dataset2);
}
每次传输数据包时,使用 "Tx" 跟踪 OnOffApplication 调用函数 -
void SourceUdpTrace(Ptr<const Packet> pkt)
{
nPacketsSent++;
}
在 main() 内部,使用以下 Config::Connect 语句设置跟踪并 link 使用 SourceUdpTrace
。此语句必须放在 Simulator::Run()
Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::ManualOnOffApp/Tx",MakeCallback(&SourceUdpTrace));
一旦知道已发送数据包的数量,就可以通过将其与 nb
的值进行比较来轻松确定丢失的数据包数量,正如您的代码行 - nb = totalPacketsThrough / payloadSize;