Last active
November 18, 2016 16:04
-
-
Save diegopso/e6337f194483c7d149efdab07a2a54f4 to your computer and use it in GitHub Desktop.
Trabalho de Gerencia de Redes 2016-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# // Sample Command to Run Simulation | |
# // | |
# // ./waf --pyrun "scratch/gerencia.py --trafic=2 --nWifi=5 --verbose=False --seed=3" | |
# // | |
# // nWifi - Number of wifi STA devices | |
# // verbose - Tell echo applications to log if true | |
# // trafic - Tipo de trafico da simulacao: 1 - CBR, 2 - Rajada | |
# // duration - The duration of the simullation in seconds | |
# // seed - The seed to generate random values | |
# // | |
# // Default Network Topology | |
# // | |
# // Wifi 10.1.3.0 | |
# // AP | |
# // * * * * * * | |
# // | | | | | | 10.1.1.0 | |
# // n3 n4 n5 n6 n7 n0 -------------- n1 n2 | |
# // point-to-point | | | |
# // ====== | |
# // LAN 10.1.2.0 | |
import ns3 | |
import ns.core | |
import ns.flow_monitor | |
import ns.network | |
import ns.point_to_point | |
import ns.applications | |
import ns.wifi | |
import ns.mobility | |
import ns.csma | |
import ns.internet | |
import sys | |
totalizer = type('test', (), {})() | |
totalizer.txBytes = 0 | |
totalizer.rxBytes = 0 | |
totalizer.txPackets = 0 | |
totalizer.rxPackets = 0 | |
totalizer.lostPackets = 0 | |
totalizer.Throughput = 0 | |
totalizer.MeanDelay = 0 | |
totalizer.MeanJitter = 0 | |
totalizer.lostNodes = 0 | |
totalizer.disconnectedNodes = 0 | |
# determinar no mais afastado | |
def get_farest_node(apPosition, wifiStaNodes, nWifi): | |
maximum = 0 | |
farest = 0 | |
for i in range(0, nWifi): | |
position = wifiStaNodes.Get(i).GetObject(ns3.MobilityModel.GetTypeId()).GetPosition() | |
sqDistance = (position.x - apPosition.x)**2 + (position.y - apPosition.y)**2 | |
if sqDistance > maximum: | |
maximum = sqDistance | |
farest = i | |
return farest | |
# determinar no mais afastado | |
def get_closest_node(apPosition, wifiStaNodes, nWifi): | |
minimum = 100000 | |
closest = 0 | |
for i in range(0, nWifi): | |
position = wifiStaNodes.Get(i).GetObject(ns3.MobilityModel.GetTypeId()).GetPosition() | |
sqDistance = (position.x - apPosition.x)**2 + (position.y - apPosition.y)**2 | |
if sqDistance < minimum: | |
minimum = sqDistance | |
closest = i | |
return closest | |
# funcao para impressao dos logs do FlowMonitor | |
def print_stats(os, st, duration, nWifi, trafic): | |
totalizer.txBytes += st.txBytes; | |
totalizer.rxBytes += st.rxBytes; | |
totalizer.txPackets += st.txPackets; | |
totalizer.rxPackets += st.rxPackets; | |
totalizer.lostPackets += st.lostPackets; | |
totalizer.Throughput += st.rxBytes / (duration - 2); | |
print >> os, " Flow: ", 'cbr' if trafic == 1 else 'rajada' | |
print >> os, " Mobile Nodes: ", nWifi * 0.2 | |
print >> os, " Total Nodes: ", nWifi | |
print >> os, " Tx Bytes: ", st.txBytes | |
print >> os, " Rx Bytes: ", st.rxBytes | |
print >> os, " Tx Packets: ", st.txPackets | |
print >> os, " Rx Packets: ", st.rxPackets | |
print >> os, " Lost Packets: ", st.lostPackets | |
print >> os, " Throughput: ", st.rxBytes / (duration - 2) | |
if st.rxPackets > 0: | |
totalizer.MeanDelay += st.delaySum.GetSeconds() / st.rxPackets; | |
print >> os, " Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets) | |
else: | |
print >> os, " Mean{Delay}: infinity" | |
if st.rxPackets > 0: | |
totalizer.disconnectedNodes += 1 | |
else: | |
totalizer.lostNodes += 1 | |
if st.rxPackets > 1: | |
totalizer.MeanJitter += st.jitterSum.GetSeconds() / (st.rxPackets - 1) | |
print >> os, " Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets - 1)) | |
else: | |
print >> os, " Mean{Jitter}: infinity" | |
if st.rxPackets > 0: | |
print >> os, " Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1 | |
else: | |
print >> os, " Mean{Hop Count}: infinity" | |
#print >> os, " Delay Histogram" | |
#for i in range(st.delayHistogram.GetNBins()): | |
# print >> os, " ", i, "(", st.delayHistogram.GetBinStart(i), "-", st.delayHistogram.GetBinEnd(i), "): ", st.delayHistogram.GetBinCount(i) | |
#print >> os, " Jitter Histogram" | |
#for i in range(st.jitterHistogram.GetNBins()): | |
# print >> os, " ", i, "(", st.jitterHistogram.GetBinStart(i), "-", st.jitterHistogram.GetBinEnd(i), "): ", st.jitterHistogram.GetBinCount(i) | |
#print >> os, " PacketSize Histogram" | |
#for i in range(st.packetSizeHistogram.GetNBins()): | |
# print >> os, " ", i, "(", st.packetSizeHistogram.GetBinStart(i), "-", st.packetSizeHistogram.GetBinEnd(i), "): ", st.packetSizeHistogram.GetBinCount(i) | |
# definir parametros da simulacao | |
cmd = ns.core.CommandLine() | |
cmd.verbose = "True" | |
cmd.nWifi = 40 | |
cmd.pMoveis = 0.2 | |
cmd.trafic = 1 | |
cmd.duration = 100.0 | |
cmd.seed = 3 | |
cmd.AddValue("pMoveis", "Percentage of mobile devices") | |
cmd.AddValue("nWifi", "Number of wifi STA devices") | |
cmd.AddValue("verbose", "Tell echo applications to log if true") | |
cmd.AddValue("trafic", "Tipo de trafico da simulacao: 1 - CBR, 2 - Rajada") | |
cmd.AddValue("duration", "The duration of the simullation in seconds") | |
cmd.AddValue("seed", "The seed to generate random values") | |
cmd.Parse(sys.argv) | |
nCsma = 1 | |
verbose = cmd.verbose | |
nWifi = int(cmd.nWifi) | |
nMoveis = int(int(cmd.nWifi) * float(cmd.pMoveis)) | |
trafic = int(cmd.trafic) | |
duration = float(cmd.duration) | |
seed = int(cmd.seed) | |
# configurar tamanho padrao do pacote TCP gerado pela aplicacao | |
ns.core.Config.SetDefault("ns3::TcpSocket::SegmentSize", ns.core.UintegerValue(1440)) | |
ns.core.Config.SetDefault("ns3::TcpL4Protocol::SocketType", ns.core.StringValue("ns3::TcpNewReno")) | |
ns3.RngSeedManager.SetSeed(seed) | |
if verbose == "True": | |
ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO) | |
ns.core.LogComponentEnable("OnOffApplication", ns.core.LOG_LEVEL_INFO) | |
ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO) | |
ns.core.LogComponentEnable("PacketSink", ns.core.LOG_LEVEL_INFO) | |
# definir ponte entre as redes | |
p2pNodes = ns.network.NodeContainer() | |
p2pNodes.Create(2) | |
pointToPoint = ns.point_to_point.PointToPointHelper() | |
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps")) | |
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms")) | |
p2pDevices = pointToPoint.Install(p2pNodes) | |
# definir nos estaticos | |
csmaNodes = ns.network.NodeContainer() | |
csmaNodes.Add(p2pNodes.Get(1)) | |
csmaNodes.Create(nCsma) | |
csma = ns.csma.CsmaHelper() | |
csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps")) | |
csma.SetChannelAttribute("Delay", ns.core.StringValue("2ms")) | |
csmaDevices = csma.Install(csmaNodes) | |
# definir nos moveis | |
wifiStaNodes = ns.network.NodeContainer() | |
wifiStaNodes.Create(nWifi) | |
wifiApNode = p2pNodes.Get(0) | |
channel = ns.wifi.YansWifiChannelHelper.Default() | |
phy = ns.wifi.YansWifiPhyHelper.Default() | |
phy.SetChannel(channel.Create()) | |
wifi = ns.wifi.WifiHelper() | |
wifi.SetRemoteStationManager("ns3::AarfWifiManager") | |
mac = ns.wifi.WifiMacHelper() | |
ssid = ns.wifi.Ssid ("ns-3-ssid") | |
mac.SetType ("ns3::StaWifiMac", "Ssid", ns.wifi.SsidValue(ssid), "ActiveProbing", ns.core.BooleanValue(False)) | |
staDevices = wifi.Install(phy, mac, wifiStaNodes) | |
mac.SetType("ns3::ApWifiMac","Ssid", ns.wifi.SsidValue (ssid)) | |
apDevices = wifi.Install(phy, mac, wifiApNode) | |
mobility = ns.mobility.MobilityHelper() | |
mobility.SetPositionAllocator ("ns3::RandomRectanglePositionAllocator", | |
"X", ns.core.StringValue("ns3::UniformRandomVariable[Min=200.0|Max=400.0]"), | |
"Y", ns.core.StringValue("ns3::UniformRandomVariable[Min=200.0|Max=400.0]") | |
) | |
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", | |
"Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle (0, 600, 0, 600)), | |
"Mode", ns.core.StringValue("Time"), | |
"Time", ns.core.StringValue("20s"), | |
"Speed", ns.core.StringValue("ns3::UniformRandomVariable[Min=2.0|Max=3.0]") | |
) | |
for i in range(0, nMoveis): | |
mobility.Install(wifiStaNodes.Get(i)) | |
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel") | |
for i in range(nMoveis, nWifi): | |
mobility.Install(wifiStaNodes.Get(i)) | |
# AP | |
apPosition = type('test', (), {})() | |
apPosition.x = 300.0 | |
apPosition.y = 300.0 | |
mobility.SetPositionAllocator ("ns3::GridPositionAllocator", | |
"MinX", ns.core.DoubleValue(apPosition.x), | |
"MinY", ns.core.DoubleValue (apPosition.y), | |
"DeltaX", ns.core.DoubleValue (10.0), | |
"DeltaY", ns.core.DoubleValue (10.0), | |
"GridWidth", ns.core.UintegerValue(5), | |
"LayoutType", ns.core.StringValue("RowFirst") | |
) | |
mobility.Install(wifiApNode) | |
# identify farest and closest nodes | |
farestNode = get_farest_node(apPosition, wifiStaNodes, nWifi) + 1 # 1-based counting | |
closestNode = get_closest_node(apPosition, wifiStaNodes, nWifi) + 1 # 1-based counting | |
# definir protocolos de comunicacao | |
stack = ns.internet.InternetStackHelper() | |
stack.Install(csmaNodes) | |
stack.Install(wifiApNode) | |
stack.Install(wifiStaNodes) | |
# definir enderecamentos das redes | |
address = ns.internet.Ipv4AddressHelper() | |
address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0")) | |
p2pInterfaces = address.Assign(p2pDevices) | |
address.SetBase(ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0")) | |
csmaInterfaces = address.Assign(csmaDevices) | |
address.SetBase(ns.network.Ipv4Address("10.1.3.0"), ns.network.Ipv4Mask("255.255.255.0")) | |
address.Assign(staDevices) | |
address.Assign(apDevices) | |
# criar aplicacoes e definir conexao | |
if trafic == 1: | |
server = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory", csmaInterfaces.GetAddress(nCsma)) | |
client = ns.applications.OnOffHelper("ns3::UdpSocketFactory", ns3.InetSocketAddress(csmaInterfaces.GetAddress(nCsma), 9)) | |
client.SetAttribute("PacketSize", ns.core.UintegerValue(478)) # +34 header | |
client.SetAttribute("MaxBytes", ns.core.UintegerValue(0)) | |
client.SetAttribute("DataRate", ns3.DataRateValue(ns3.DataRate("512kbps"))) | |
client.SetAttribute("OnTime", ns.core.StringValue("ns3::ConstantRandomVariable[Constant=1]")) | |
client.SetAttribute("OffTime", ns.core.StringValue("ns3::ConstantRandomVariable[Constant=2]")) | |
serverApps = server.Install(csmaNodes.Get(nCsma)) | |
serverApps.Start(ns.core.Seconds(1.0)) | |
serverApps.Stop(ns.core.Seconds(duration)) | |
for i in range(0, nWifi): | |
clientApps = client.Install(wifiStaNodes.Get(i)) | |
clientApps.Start(ns.core.Seconds(2.0)) | |
clientApps.Stop(ns.core.Seconds(duration)) | |
else: | |
client = ns.applications.OnOffHelper("ns3::TcpSocketFactory", csmaInterfaces.GetAddress(nCsma)) | |
client.SetAttribute("PacketSize", ns.core.UintegerValue(1440)) # +60 header | |
client.SetAttribute("MaxBytes", ns.core.UintegerValue(0)) | |
client.SetAttribute("DataRate", ns3.DataRateValue(ns3.DataRate("512kbps"))) | |
client.SetAttribute("OnTime", ns.core.StringValue("ns3::NormalRandomVariable[Mean=5.|Variance=1.|Bound=10.]")) | |
client.SetAttribute("OffTime", ns.core.StringValue("ns3::NormalRandomVariable[Mean=7.|Variance=1.|Bound=10.]")) | |
port = 8080 | |
for i in range(0, nWifi): | |
server = ns.applications.PacketSinkHelper("ns3::TcpSocketFactory", ns3.InetSocketAddress(csmaInterfaces.GetAddress(nCsma), port + i)) | |
serverApps = server.Install(csmaNodes.Get(nCsma)) | |
serverApps.Start(ns.core.Seconds(1.0)) | |
serverApps.Stop(ns.core.Seconds(duration)) | |
client.SetAttribute("Remote", ns3.AddressValue(ns3.InetSocketAddress(csmaInterfaces.GetAddress(1), port + i))) | |
clientApps = client.Install(wifiStaNodes.Get(i)) | |
clientApps.Start(ns.core.Seconds(2.0)) | |
clientApps.Stop(ns.core.Seconds(duration)) | |
# protocolo de roteamento | |
ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables() | |
# definir ponto de parada | |
ns.core.Simulator.Stop(ns.core.Seconds(duration)) | |
# logs PCAP ou Ascii | |
#pointToPoint.EnableAsciiAll ("gerencia") | |
#phy.EnablePcap ("gerencia-node.log", staDevices.Get (1)) | |
#csma.EnablePcap ("gerencia-rot.log", csmaDevices.Get (0), True) | |
#csma.EnablePcap ("gerencia-server.log", csmaDevices.Get (1), True) | |
# definir logs FlowMonitor | |
flowmon_helper = ns.flow_monitor.FlowMonitorHelper() | |
monitor = flowmon_helper.InstallAll() | |
monitor = flowmon_helper.GetMonitor() | |
ns.core.Simulator.Run() | |
ns.core.Simulator.Destroy() | |
monitor.CheckForLostPackets() | |
classifier = flowmon_helper.GetClassifier() | |
# logs FlowMonitor | |
f = open(`seed` + '-' + `nWifi` + '-' + ('cbr' if trafic == 1 else 'rajada') + '.log','w') | |
for flow_id, flow_stats in monitor.GetFlowStats(): | |
if flow_id <= nWifi: | |
t = classifier.FindFlow(flow_id) | |
proto = {6: 'TCP', 17: 'UDP'} [t.protocol] | |
print >> f, "FlowID: %i (%s %s/%s --> %s/%i) %s" % (flow_id, proto, t.sourceAddress, t.sourcePort, t.destinationAddress, t.destinationPort, | |
'Closest' if flow_id == closestNode else ('Farest' if flow_id == farestNode else '')) | |
print_stats(f, flow_stats, duration, nWifi, trafic) | |
print >> f, "Rede Completa: " | |
print >> f, " Flow: ", 'cbr' if trafic == 1 else 'rajada' | |
print >> f, " Tx Bytes: ", totalizer.txBytes | |
print >> f, " Rx Bytes: ", totalizer.rxBytes | |
print >> f, " Tx Packets: ", totalizer.txPackets | |
print >> f, " Rx Packets: ", totalizer.rxPackets | |
print >> f, " Lost Packets: ", totalizer.lostPackets | |
print >> f, " Throughput: ", totalizer.Throughput | |
print >> f, " Mean Delay: ", totalizer.MeanDelay | |
print >> f, " Mean Jitter: ", totalizer.MeanJitter | |
print >> f, " Total Nodes: ", nWifi | |
print >> f, " Mobile Nodes: ", nMoveis | |
print >> f, " Lost Nodes: ", totalizer.lostNodes | |
print >> f, " Disconnected Nodes: ", totalizer.disconnectedNodes | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
for trafego in {1..2}; do | |
for nos in {5,10,15,20,25,30,35,40}; do | |
for i in {0..2}; do | |
for j in {0..9}; do | |
semente=$(( $i * 10 + $j )) | |
./waf --pyrun "scratch/gerencia.py --trafic=$trafego --nWifi=$nos --verbose=False --seed=$semente" & | |
done | |
wait | |
done | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment