Files
unison/examples/wireless/wifi-ht-network.cc

330 lines
13 KiB
C++
Raw Normal View History

2013-08-13 22:05:25 -07:00
/*
* Copyright (c) 2009 MIRKO BANCHI
*
* This program is free software; you can redistribute it and/or modify
2015-09-03 22:16:49 +02:00
* it under the terms of the GNU General Public License version 2 as
2013-08-13 22:05:25 -07:00
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
2015-01-31 17:36:14 -08:00
* Authors: Mirko Banchi <mk.banchi@gmail.com>
* Sebastien Deronne <sebastien.deronne@gmail.com>
2013-08-13 22:05:25 -07:00
*/
2022-10-07 20:08:35 +00:00
#include "ns3/boolean.h"
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/double.h"
#include "ns3/enum.h"
#include "ns3/ht-phy.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
2022-10-07 20:08:35 +00:00
#include "ns3/log.h"
#include "ns3/mobility-helper.h"
#include "ns3/on-off-helper.h"
#include "ns3/packet-sink-helper.h"
#include "ns3/packet-sink.h"
2022-10-07 20:08:35 +00:00
#include "ns3/ssid.h"
#include "ns3/string.h"
#include "ns3/tuple.h"
#include "ns3/udp-client-server-helper.h"
#include "ns3/uinteger.h"
#include "ns3/yans-wifi-channel.h"
2022-10-07 20:08:35 +00:00
#include "ns3/yans-wifi-helper.h"
2013-08-13 22:05:25 -07:00
// This is a simple example in order to show how to configure an IEEE 802.11n Wi-Fi network.
2013-08-13 22:05:25 -07:00
//
2022-10-07 20:08:35 +00:00
// It outputs the UDP or TCP goodput for every HT MCS value, which depends on the MCS value (0 to
// 7), the channel width (20 or 40 MHz) and the guard interval (long or short). The PHY bitrate is
// constant over all the simulation run. The user can also specify the distance between the access
// point and the station: the larger the distance the smaller the goodput.
2015-01-31 17:36:14 -08:00
//
// The simulation assumes a single station in an infrastructure network:
2015-01-31 17:36:14 -08:00
//
// STA AP
// * *
// | |
// n1 n2
2013-08-13 22:05:25 -07:00
//
2022-10-07 20:08:35 +00:00
// Packets in this simulation belong to BestEffort Access Class (AC_BE).
2013-08-13 22:05:25 -07:00
using namespace ns3;
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("ht-wifi-network");
2013-08-13 22:05:25 -07:00
2022-10-07 20:08:35 +00:00
int
main(int argc, char* argv[])
2015-01-31 17:36:14 -08:00
{
2022-10-07 20:08:35 +00:00
bool udp = true;
bool useRts = false;
double simulationTime = 10; // seconds
double distance = 1.0; // meters
double frequency = 5.0; // whether 2.4 or 5.0 GHz
int mcs = -1; // -1 indicates an unset value
double minExpectedThroughput = 0;
double maxExpectedThroughput = 0;
CommandLine cmd(__FILE__);
cmd.AddValue("frequency",
"Whether working in the 2.4 or 5.0 GHz band (other values gets rejected)",
frequency);
cmd.AddValue("distance",
"Distance in meters between the station and the access point",
distance);
cmd.AddValue("simulationTime", "Simulation time in seconds", simulationTime);
cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
cmd.AddValue("mcs", "if set, limit testing to a specific MCS (0-7)", mcs);
cmd.AddValue("minExpectedThroughput",
"if set, simulation fails if the lowest throughput is below this value",
minExpectedThroughput);
cmd.AddValue("maxExpectedThroughput",
"if set, simulation fails if the highest throughput is above this value",
maxExpectedThroughput);
cmd.Parse(argc, argv);
if (useRts)
{
2022-10-07 20:08:35 +00:00
Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
}
double prevThroughput[8] = {0};
2022-10-07 20:08:35 +00:00
std::cout << "MCS value"
<< "\t\t"
<< "Channel width"
<< "\t\t"
<< "short GI"
<< "\t\t"
<< "Throughput" << '\n';
int minMcs = 0;
int maxMcs = 7;
if (mcs >= 0 && mcs <= 7)
{
2022-10-07 20:08:35 +00:00
minMcs = mcs;
maxMcs = mcs;
}
2022-10-07 20:08:35 +00:00
for (int mcs = minMcs; mcs <= maxMcs; mcs++)
2015-01-31 17:36:14 -08:00
{
2022-10-07 20:08:35 +00:00
uint8_t index = 0;
double previous = 0;
for (int channelWidth = 20; channelWidth <= 40;)
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
for (int sgi = 0; sgi < 2; sgi++)
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
uint32_t payloadSize; // 1500 byte IP packet
if (udp)
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
payloadSize = 1472; // bytes
2015-09-03 22:16:49 +02:00
}
2022-10-07 20:08:35 +00:00
else
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
payloadSize = 1448; // bytes
Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
2015-09-03 22:16:49 +02:00
}
2022-10-07 20:08:35 +00:00
NodeContainer wifiStaNode;
wifiStaNode.Create(1);
NodeContainer wifiApNode;
wifiApNode.Create(1);
2015-09-03 22:16:49 +02:00
2022-10-07 20:08:35 +00:00
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy;
phy.SetChannel(channel.Create());
2015-09-03 22:16:49 +02:00
2022-10-07 20:08:35 +00:00
WifiMacHelper mac;
WifiHelper wifi;
std::ostringstream ossControlMode;
2022-10-07 20:08:35 +00:00
if (frequency == 5.0)
{
ossControlMode << "OfdmRate";
2022-10-07 20:08:35 +00:00
wifi.SetStandard(WIFI_STANDARD_80211n);
}
2022-10-07 20:08:35 +00:00
else if (frequency == 2.4)
{
2022-10-07 20:08:35 +00:00
wifi.SetStandard(WIFI_STANDARD_80211n);
ossControlMode << "ErpOfdmRate";
2022-10-07 20:08:35 +00:00
Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
DoubleValue(40.046));
}
2022-10-07 20:08:35 +00:00
else
{
2022-10-07 20:08:35 +00:00
std::cout << "Wrong frequency value!" << std::endl;
return 0;
}
2015-09-03 22:16:49 +02:00
auto nonHtRefRateMbps = HtPhy::GetNonHtReferenceRate(mcs) / 1e6;
ossControlMode << nonHtRefRateMbps << "Mbps";
std::ostringstream ossDataMode;
ossDataMode << "HtMcs" << mcs;
2022-10-07 20:08:35 +00:00
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
"DataMode",
StringValue(ossDataMode.str()),
2022-10-07 20:08:35 +00:00
"ControlMode",
StringValue(ossControlMode.str()));
2022-10-07 20:08:35 +00:00
// Set guard interval
wifi.ConfigHtOptions("ShortGuardIntervalSupported", BooleanValue(sgi));
Ssid ssid = Ssid("ns3-80211n");
TupleValue<UintegerValue, UintegerValue, EnumValue, UintegerValue> channelValue;
WifiPhyBand band = (frequency == 5.0 ? WIFI_PHY_BAND_5GHZ : WIFI_PHY_BAND_2_4GHZ);
channelValue.Set(WifiPhy::ChannelTuple{0, channelWidth, band, 0});
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
phy.Set("ChannelSettings", channelValue);
NetDeviceContainer staDevice;
staDevice = wifi.Install(phy, mac, wifiStaNode);
mac.SetType("ns3::ApWifiMac",
"EnableBeaconJitter",
BooleanValue(false),
"Ssid",
SsidValue(ssid));
NetDeviceContainer apDevice;
apDevice = wifi.Install(phy, mac, wifiApNode);
// mobility.
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
positionAlloc->Add(Vector(0.0, 0.0, 0.0));
positionAlloc->Add(Vector(distance, 0.0, 0.0));
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(wifiApNode);
mobility.Install(wifiStaNode);
/* Internet stack*/
InternetStackHelper stack;
stack.Install(wifiApNode);
stack.Install(wifiStaNode);
Ipv4AddressHelper address;
address.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer staNodeInterface;
Ipv4InterfaceContainer apNodeInterface;
staNodeInterface = address.Assign(staDevice);
apNodeInterface = address.Assign(apDevice);
/* Setting applications */
ApplicationContainer serverApp;
if (udp)
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
// 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));
2015-09-03 22:16:49 +02:00
}
2022-10-07 20:08:35 +00:00
else
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
// 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(200000000)); // 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));
2015-09-03 22:16:49 +02:00
}
2022-10-07 20:08:35 +00:00
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
2015-09-03 22:16:49 +02:00
2022-10-07 20:08:35 +00:00
Simulator::Stop(Seconds(simulationTime + 1));
Simulator::Run();
2015-09-03 22:16:49 +02:00
2022-10-07 20:08:35 +00:00
uint64_t rxBytes = 0;
if (udp)
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
2015-09-03 22:16:49 +02:00
}
2022-10-07 20:08:35 +00:00
else
2015-09-03 22:16:49 +02:00
{
2022-10-07 20:08:35 +00:00
rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
2015-09-03 22:16:49 +02:00
}
2022-10-07 20:08:35 +00:00
double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); // Mbit/s
2022-10-07 20:08:35 +00:00
Simulator::Destroy();
2022-10-07 20:08:35 +00:00
std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << sgi << "\t\t\t"
<< throughput << " Mbit/s" << std::endl;
2022-10-07 20:08:35 +00:00
// test first element
if (mcs == 0 && channelWidth == 20 && sgi == 0)
{
2022-10-07 20:08:35 +00:00
if (throughput < minExpectedThroughput)
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
}
}
2022-10-07 20:08:35 +00:00
// test last element
if (mcs == 7 && channelWidth == 40 && sgi == 1)
{
2022-10-07 20:08:35 +00:00
if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
}
}
2022-10-07 20:08:35 +00:00
// test previous throughput is smaller (for the same mcs)
if (throughput > previous)
{
2022-10-07 20:08:35 +00:00
previous = throughput;
}
2022-10-07 20:08:35 +00:00
else
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
}
2022-10-07 20:08:35 +00:00
// test previous throughput is smaller (for the same channel width and GI)
if (throughput > prevThroughput[index])
{
2022-10-07 20:08:35 +00:00
prevThroughput[index] = throughput;
}
2022-10-07 20:08:35 +00:00
else
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
}
2022-10-07 20:08:35 +00:00
index++;
2015-09-03 22:16:49 +02:00
}
2022-10-07 20:08:35 +00:00
channelWidth *= 2;
2015-01-31 17:36:14 -08:00
}
}
2022-10-07 20:08:35 +00:00
return 0;
2015-01-31 17:36:14 -08:00
}