domingo, 7 de abril de 2013

Topologías y ruteos

Para esta semana la tarea es hacer una topología, probar los modos de ruteo y hacer un generador de topologías.

Primero la topología. Elegí la siguiente topología ya que tenía que pensar en alguna que tuviera alguna aplicación real, así que me di a la tarea de hacer un dibujo muy aproximado a la topología que utilizan en el ciber de mi colonia. El encargado me dijo lo que usaban y me pareció sencillo de hacerlo y además aprovechar que tiene un verdadero uso, por eso me pareció bien basarme en esto.


Como es posible ver es una topología estrella, que tiene bastantes ventajas y que es por eso que se usa en el ciber, por ejemplo cuando algún cable se daña, solo afecta al terminal relacionado y no a todos los demás terminales además que es muy fácil agregar o eliminar terminales.
Entonces empecé a diseñarlo en ns-3.

Código en python:

def main(argv):
cmd = ns3.CommandLine()
cmd.Parse (argv)
# Configuration.
# Build nodes
term_0 = ns3.NodeContainer()
term_0.Create (1)
term_1 = ns3.NodeContainer()
term_1.Create (1)
term_2 = ns3.NodeContainer()
term_2.Create (1)
term_3 = ns3.NodeContainer()
term_3.Create (1)
router_0 = ns3.NodeContainer()
router_0.Create (1)
term_5 = ns3.NodeContainer()
term_5.Create (1)
term_6 = ns3.NodeContainer()
term_6.Create (1)
term_7 = ns3.NodeContainer()
term_7.Create (1)
# Build link.
csma_hub_0 = ns3.CsmaHelper()
csma_hub_0.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_0.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(2)))
csma_hub_1 = ns3.CsmaHelper()
csma_hub_1.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_1.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(2)))
# Build link net device container.
all_hub_0 = ns3.NodeContainer()
all_hub_0.Add (router_0)
all_hub_0.Add (term_1)
all_hub_0.Add (term_0)
all_hub_0.Add (term_2)
all_hub_0.Add (term_3)
ndc_hub_0 = csma_hub_0.Install(all_hub_0)
all_hub_1 = ns3.NodeContainer()
all_hub_1.Add (router_0)
all_hub_1.Add (term_7)
all_hub_1.Add (term_6)
all_hub_1.Add (term_5)
ndc_hub_1 = csma_hub_1.Install(all_hub_1)
# Install the IP stack.
internetStackH = ns3.InternetStackHelper()
internetStackH.Install (term_0)
internetStackH.Install (term_1)
internetStackH.Install (term_2)
internetStackH.Install (term_3)
internetStackH.Install (router_0)
internetStackH.Install (term_5)
internetStackH.Install (term_6)
internetStackH.Install (term_7)
# IP assign.
ipv4 = ns3.Ipv4AddressHelper()
ipv4.SetBase (ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0)
ipv4.SetBase (ns3.Ipv4Address("10.0.1.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1)
# Generate Route.
ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables ()
# Generate Application.
print "Create Applications."
port = 9
######################
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_0.GetAddress(2))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_0.Get(0))
app.Start(ns3.Seconds(0.0))
#####################
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_0.GetAddress(1))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_0.Get(0))
# empieza la aplicacion
app.Start(ns3.Seconds(0.0))
####################
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_0.GetAddress(3))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_0.Get(0))
# empieza la aplicacion
app.Start(ns3.Seconds(0.0))
####################
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_0.GetAddress(0))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_0.Get(0))
# empieza la aplicacion
app.Start(ns3.Seconds(0.0))
####################
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_0.GetAddress(4))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_0.Get(0))
# empieza la aplicacion
app.Start(ns3.Seconds(0.0))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_1.GetAddress(2))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_1.Get(0))
# empieza la aplicacion
app.Start(ns3.Seconds(0.0))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_1.GetAddress(1))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_1.Get(0))
# empieza la aplicacion
app.Start(ns3.Seconds(0.0))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~
dst_ping_0 = ns3.InetSocketAddress(iface_ndc_hub_1.GetAddress(3))
onoff = ns3.OnOffHelper("ns3::UdpSocketFactory",
dst_ping_0)
onoff.SetConstantRate(ns3.DataRate("800kb/s"))
app = onoff.Install(all_hub_1.Get(0))
# empieza la aplicacion
app.Start(ns3.Seconds(0.0))
ns3.Simulator.Run()
ns3.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)
view raw gistfile1.py hosted with ❤ by GitHub


Vídeo del funcionamiento:



Construcción de otras topologías:

Anillo


mport ns3
def main(argv):
cmd = ns3.CommandLine()
cmd.Parse (argv)
# Configuration.
# Build nodes
term_0 = ns3.NodeContainer()
term_0.Create (1)
term_1 = ns3.NodeContainer()
term_1.Create (1)
term_2 = ns3.NodeContainer()
term_2.Create (1)
term_3 = ns3.NodeContainer()
term_3.Create (1)
term_4 = ns3.NodeContainer()
term_4.Create (1)
term_5 = ns3.NodeContainer()
term_5.Create (1)
# Build link.
csma_hub_0 = ns3.CsmaHelper()
csma_hub_0.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_0.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_1 = ns3.CsmaHelper()
csma_hub_1.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_1.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_2 = ns3.CsmaHelper()
csma_hub_2.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_2.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_3 = ns3.CsmaHelper()
csma_hub_3.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_3.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_4 = ns3.CsmaHelper()
csma_hub_4.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_4.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_5 = ns3.CsmaHelper()
csma_hub_5.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_5.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
# Build link net device container.
all_hub_0 = ns3.NodeContainer()
all_hub_0.Add (term_2)
all_hub_0.Add (term_3)
ndc_hub_0 = csma_hub_0.Install(all_hub_0)
all_hub_1 = ns3.NodeContainer()
all_hub_1.Add (term_3)
all_hub_1.Add (term_0)
ndc_hub_1 = csma_hub_1.Install(all_hub_1)
all_hub_2 = ns3.NodeContainer()
all_hub_2.Add (term_0)
all_hub_2.Add (term_4)
ndc_hub_2 = csma_hub_2.Install(all_hub_2)
all_hub_3 = ns3.NodeContainer()
all_hub_3.Add (term_4)
all_hub_3.Add (term_5)
ndc_hub_3 = csma_hub_3.Install(all_hub_3)
all_hub_4 = ns3.NodeContainer()
all_hub_4.Add (term_1)
all_hub_4.Add (term_5)
ndc_hub_4 = csma_hub_4.Install(all_hub_4)
all_hub_5 = ns3.NodeContainer()
all_hub_5.Add (term_2)
all_hub_5.Add (term_1)
ndc_hub_5 = csma_hub_5.Install(all_hub_5)
# Install the IP stack.
internetStackH = ns3.InternetStackHelper()
internetStackH.Install (term_0)
internetStackH.Install (term_1)
internetStackH.Install (term_2)
internetStackH.Install (term_3)
internetStackH.Install (term_4)
internetStackH.Install (term_5)
# IP assign.
ipv4 = ns3.Ipv4AddressHelper()
ipv4.SetBase (ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0)
ipv4.SetBase (ns3.Ipv4Address("10.0.1.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1)
ipv4.SetBase (ns3.Ipv4Address("10.0.2.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_2 = ipv4.Assign (ndc_hub_2)
ipv4.SetBase (ns3.Ipv4Address("10.0.3.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_3 = ipv4.Assign (ndc_hub_3)
ipv4.SetBase (ns3.Ipv4Address("10.0.4.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_4 = ipv4.Assign (ndc_hub_4)
ipv4.SetBase (ns3.Ipv4Address("10.0.5.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_5 = ipv4.Assign (ndc_hub_5)
# Generate Route.
ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables ()
# Generate Application.
# Simulation.
# Pcap output.
# Stop the simulation after x seconds.
stopTime = 1
ns3.Simulator.Stop (ns3.Seconds(stopTime))
# Start and clean simulation.
ns3.Simulator.Run()
ns3.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)
view raw gistfile1.py hosted with ❤ by GitHub


Árbol


import ns3
def main(argv):
cmd = ns3.CommandLine()
cmd.Parse (argv)
# Configuration.
# Build nodes
term_0 = ns3.NodeContainer()
term_0.Create (1)
term_1 = ns3.NodeContainer()
term_1.Create (1)
term_2 = ns3.NodeContainer()
term_2.Create (1)
term_3 = ns3.NodeContainer()
term_3.Create (1)
term_4 = ns3.NodeContainer()
term_4.Create (1)
term_5 = ns3.NodeContainer()
term_5.Create (1)
term_6 = ns3.NodeContainer()
term_6.Create (1)
# Build link.
csma_hub_0 = ns3.CsmaHelper()
csma_hub_0.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_0.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_1 = ns3.CsmaHelper()
csma_hub_1.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_1.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_2 = ns3.CsmaHelper()
csma_hub_2.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_2.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_3 = ns3.CsmaHelper()
csma_hub_3.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_3.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
csma_hub_4 = ns3.CsmaHelper()
csma_hub_4.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
csma_hub_4.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
# Build link net device container.
all_hub_0 = ns3.NodeContainer()
all_hub_0.Add (term_0)
ndc_hub_0 = csma_hub_0.Install(all_hub_0)
all_hub_1 = ns3.NodeContainer()
all_hub_1.Add (term_6)
all_hub_1.Add (term_5)
all_hub_1.Add (term_4)
ndc_hub_1 = csma_hub_1.Install(all_hub_1)
all_hub_2 = ns3.NodeContainer()
all_hub_2.Add (term_2)
all_hub_2.Add (term_1)
all_hub_2.Add (term_3)
ndc_hub_2 = csma_hub_2.Install(all_hub_2)
all_hub_3 = ns3.NodeContainer()
all_hub_3.Add (hub_0)
all_hub_3.Add (hub_1)
ndc_hub_3 = csma_hub_3.Install(all_hub_3)
all_hub_4 = ns3.NodeContainer()
all_hub_4.Add (hub_0)
all_hub_4.Add (hub_2)
ndc_hub_4 = csma_hub_4.Install(all_hub_4)
# Install the IP stack.
internetStackH = ns3.InternetStackHelper()
internetStackH.Install (term_0)
internetStackH.Install (term_1)
internetStackH.Install (term_2)
internetStackH.Install (term_3)
internetStackH.Install (term_4)
internetStackH.Install (term_5)
internetStackH.Install (term_6)
# IP assign.
ipv4 = ns3.Ipv4AddressHelper()
ipv4.SetBase (ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0)
ipv4.SetBase (ns3.Ipv4Address("10.0.1.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1)
ipv4.SetBase (ns3.Ipv4Address("10.0.2.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_2 = ipv4.Assign (ndc_hub_2)
ipv4.SetBase (ns3.Ipv4Address("10.0.3.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_3 = ipv4.Assign (ndc_hub_3)
ipv4.SetBase (ns3.Ipv4Address("10.0.4.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_4 = ipv4.Assign (ndc_hub_4)
# Generate Route.
ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables ()
# Generate Application.
# Simulation.
# Pcap output.
# Stop the simulation after x seconds.
stopTime = 1
ns3.Simulator.Stop (ns3.Seconds(stopTime))
# Start and clean simulation.
ns3.Simulator.Run()
ns3.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)
view raw gistfile1.py hosted with ❤ by GitHub



Modos de ruteo

En NS-3 la manera más sencilla de crear un nuevo modo de ruteo es modificando alguno ya existente, por ejemplo el OLSR, tiene las dos funciones básicas que hay que leer RouteOutput y RouteInput. Y así poder utilizar la tabla de ruteo.
Por ejemplo:



Y se establecen de esta manera:

print "Enabling OLSR routing on all backbone nodes"
internet = ns.internet.InternetStackHelper()
olsr = ns.olsr.OlsrHelper()
internet.SetRoutingHelper(olsr); # has effect on the next Install ()
internet.Install(backbone);
view raw gistfile1.py hosted with ❤ by GitHub


Ese es el olsr, y el ejemplo esta ya hecho en la carpeta de wireless/mixed-wireless.py el que yo hice implementa el ipv4, y la manera en que lo llamo podemos verla en el código que ya mostré pero aquí esta más específicamente, en la línea 69:

# Generate Route.
ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables ()
view raw gistfile1.py hosted with ❤ by GitHub



Tabla de ruteo

Generador de topologías

El generador que hice es un programa en python que genera a su vez código en python de varios tipos de topologías, el usuario puede elegir si quiere hacer una topología aleatoria, de estrella o de anillo, el programa toma como primer parámetro el tipo de topología y el segundo parámetro es el numero de nodos y regresa un código en python, que se corre en ns-3 para poder verlo.
Para poder verlo es necesario que movamos el archivo generado a la carpeta de ns-3 y así usar todas las funciones de red.
Código:

#!/usr/bin/python
import sys
import random
def inicia_archivo():
f = open("nuevo.py", "w")
f.write("import ns3\n")
f.write("def main(argv):\n")
f.write(" cmd = ns3.CommandLine()\n")
f.write(" cmd.Parse (argv)\n")
return f
def termina_archivo(f):
f.write("if __name__ == '__main__':\n")
f.write(" import sys\n")
f.write(" main(sys.argv)\n")
return f
def anillo(f, nodos):
#escribe los nodos
f.write("\n")
f.write(" #nodes\n")
for i in range(nodos):
f.write(" nodo_%s=ns3.NodeContainer()\n" %i)
f.write(" nodo_%s.Create(1)\n" %i)
#links
f.write("\n")
f.write(" #links\n")
for i in range(nodos):
f.write(" hub_%s = ns3.CsmaHelper()\n" %i)
f.write(" hub_%s.SetChannelAttribute(\"DataRate\", ns3.DataRateValue(ns3.DataRate(100000000)))\n" %i)
f.write(" hub_%s.SetChannelAttribute(\"Delay\", ns3.TimeValue(ns3.MilliSeconds(10000)))\n" %i)
#links
f.write("\n")
f.write(" #links\n")
for i in range(nodos-1):
if i != nodos-1:
f.write(" all_hub_%s = ns3.NodeContainer()\n" %i)
f.write(" all_hub_%s.Add (term_%s)\n" %(i,i))
f.write(" all_hub_%s.Add (term_%s)\n" %(i,i+1))
f.write(" ndc_hub_%s=hub_%s.Install(all_hub_%s)\n" %(i,i,i))
else:
f.write(" all_hub_%s = ns3.NodeContainer()\n" %i)
f.write(" all_hub_%s.Add (term_%s)\n" %(i,i))
f.write(" all_hub_%s.Add (term_%s)\n" %(i,0))
f.write(" ndc_hub_%s=hub_%s.Install(all_hub_%s)\n" %(i,i,i))
#ip stack
f.write("\n")
f.write(" #ip stack\n")
f.write(" internetStackH = ns3.InternetStackHelper()\n")
for i in range(nodos):
f.write(" internetStackH.Install(nodo_%s)\n" %i)
#ip
f.write("\n")
f.write(" #ip\n")
for i in range(nodos-1):
f.write(" ipv4.SetBase(ns3.Ipv4Address(\"10.0.%s.0\"), ns3.Ipv4Mask(\"255.255.255.0\"))\n"%i)
f.write(" iface_ndc_hub_%s = ipv4.Assign (ndc_hub_%s)\n" %(i,i))
#generate route
f.write("\n")
f.write(" #generate route\n")
f.write(" ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables()\n")
#simulation
#aqui debe cada quien escribir la simulcion
f.write(" stopTime = 1\n")
f.write(" ns3.Simulator.Stop (ns3.Seconds(stopTime))\n")
#inicia la simulacion
f.write("\n")
f.write(" #inicial simulacion\n")
f.write(" ns3.Simulator.Run()\n")
f.write(" ns3.Simulator.Destroy()\n")
return f
def estrella(f, nodos):
#escribe los nodos
f.write("\n")
f.write(" #nodes\n")
for i in range(nodos):
f.write(" nodo_%s=ns3.NodeContainer()\n" %i)
f.write(" nodo_%s.Create(1)\n" %i)
#links
f.write("\n")
f.write(" #links\n")
for i in range(nodos-1):
f.write(" hub_%s = ns3.CsmaHelper()\n" %i)
f.write(" hub_%s.SetChannelAttribute(\"DataRate\", ns3.DataRateValue(ns3.DataRate(100000000)))\n" %i)
f.write(" hub_%s.SetChannelAttribute(\"Delay\", ns3.TimeValue(ns3.MilliSeconds(10000)))\n" %i)
#links
f.write("\n")
f.write(" #links\n")
for i in range(nodos-1):
if i != nodos-1:
f.write(" all_hub_%s = ns3.NodeContainer()\n" %i)
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,0))
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,i+1))
f.write(" ndc_hub_%s=hub_%s.Install(all_hub_%s)\n" %(i,i,i))
else:
f.write(" all_hub_%s = ns3.NodeContainer()\n" %i)
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,0))
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,i+1))
f.write(" ndc_hub_%s=hub_%s.Install(all_hub_%s)\n" %(i,i,i))
#ip stack
f.write("\n")
f.write(" #ip stack\n")
f.write(" internetStackH = ns3.InternetStackHelper()\n")
for i in range(nodos):
f.write(" internetStackH.Install(nodo_%s)\n" %i)
#ip
f.write("\n")
f.write(" #ip\n")
for i in range(nodos-1):
f.write(" ipv4.SetBase(ns3.Ipv4Address(\"10.0.%s.0\"), ns3.Ipv4Mask(\"255.255.255.0\"))\n"%i)
f.write(" iface_ndc_hub_%s = ipv4.Assign (ndc_hub_%s)\n" %(i,i))
#generate route
f.write("\n")
f.write(" #generate route\n")
f.write(" ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables()\n")
#simulation
#aqui debe cada quien escribir la simulcion
f.write(" stopTime = 1\n")
f.write(" ns3.Simulator.Stop (ns3.Seconds(stopTime))\n")
#inicia la simulacion
f.write("\n")
f.write(" #inicial simulacion\n")
f.write(" ns3.Simulator.Run()\n")
f.write(" ns3.Simulator.Destroy()\n")
return f
def aleatoria(f, nodos):
#escribe los nodos
f.write("\n")
f.write(" #nodes\n")
for i in range(nodos):
f.write(" nodo_%s=ns3.NodeContainer()\n" %i)
f.write(" nodo_%s.Create(1)\n" %i)
#links
f.write("\n")
f.write(" #links\n")
for i in range(nodos-1):
f.write(" hub_%s = ns3.CsmaHelper()\n" %i)
f.write(" hub_%s.SetChannelAttribute(\"DataRate\", ns3.DataRateValue(ns3.DataRate(100000000)))\n" %i)
f.write(" hub_%s.SetChannelAttribute(\"Delay\", ns3.TimeValue(ns3.MilliSeconds(10000)))\n" %i)
#links
f.write("\n")
f.write(" #links\n")
for i in range(nodos-1):
if i != nodos-1:
f.write(" all_hub_%s = ns3.NodeContainer()\n" %i)
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,0))
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,random.randint(0, nodos-1)))
f.write(" ndc_hub_%s=hub_%s.Install(all_hub_%s)\n" %(i,i,i))
else:
f.write(" all_hub_%s = ns3.NodeContainer()\n" %i)
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,0))
f.write(" all_hub_%s.Add (nodo_%s)\n" %(i,random.randint(0, nodos-1)))
f.write(" ndc_hub_%s=hub_%s.Install(all_hub_%s)\n" %(i,i,i))
#ip stack
f.write("\n")
f.write(" #ip stack\n")
f.write(" internetStackH = ns3.InternetStackHelper()\n")
for i in range(nodos):
f.write(" internetStackH.Install(nodo_%s)\n" %i)
#ip
f.write("\n")
f.write(" #ip\n")
for i in range(nodos-1):
f.write(" ipv4.SetBase(ns3.Ipv4Address(\"10.0.%s.0\"), ns3.Ipv4Mask(\"255.255.255.0\"))\n"%i)
f.write(" iface_ndc_hub_%s = ipv4.Assign (ndc_hub_%s)\n" %(i,i))
#generate route
f.write("\n")
f.write(" #generate route\n")
f.write(" ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables()\n")
#simulation
#aqui debe cada quien escribir la simulcion
f.write(" stopTime = 1\n")
f.write(" ns3.Simulator.Stop (ns3.Seconds(stopTime))\n")
#inicia la simulacion
f.write("\n")
f.write(" #inicial simulacion\n")
f.write(" ns3.Simulator.Run()\n")
f.write(" ns3.Simulator.Destroy()\n")
return f
def main():
"""funcion principal
"""
try:
tipo = sys.argv[1]
nodos = sys.argv[2]
except:
print "Falta datos"
return
print tipo
print nodos
f = inicia_archivo()
if tipo == "anillo":
f = anillo(f,int(nodos))
elif tipo == "estrella":
f = estrella(f, int(nodos))
elif tipo == "aleatoria":
f = aleatoria(f, int(nodos))
else:
pass
termina_archivo(f)
if __name__ == "__main__":
main()
view raw gistfile1.py hosted with ❤ by GitHub


Ejemplos
Estrella 8 nodos
Código generado:

import ns3
def main(argv):
cmd = ns3.CommandLine()
cmd.Parse (argv)
#nodes
nodo_0=ns3.NodeContainer()
nodo_0.Create(1)
nodo_1=ns3.NodeContainer()
nodo_1.Create(1)
nodo_2=ns3.NodeContainer()
nodo_2.Create(1)
nodo_3=ns3.NodeContainer()
nodo_3.Create(1)
nodo_4=ns3.NodeContainer()
nodo_4.Create(1)
nodo_5=ns3.NodeContainer()
nodo_5.Create(1)
nodo_6=ns3.NodeContainer()
nodo_6.Create(1)
nodo_7=ns3.NodeContainer()
nodo_7.Create(1)
#links
hub_0 = ns3.CsmaHelper()
hub_0.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_0.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_1 = ns3.CsmaHelper()
hub_1.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_1.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_2 = ns3.CsmaHelper()
hub_2.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_2.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_3 = ns3.CsmaHelper()
hub_3.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_3.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_4 = ns3.CsmaHelper()
hub_4.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_4.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_5 = ns3.CsmaHelper()
hub_5.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_5.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_6 = ns3.CsmaHelper()
hub_6.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_6.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
#links
all_hub_0 = ns3.NodeContainer()
all_hub_0.Add (nodo_0)
all_hub_0.Add (nodo_1)
ndc_hub_0=hub_0.Install(all_hub_0)
all_hub_1 = ns3.NodeContainer()
all_hub_1.Add (nodo_0)
all_hub_1.Add (nodo_2)
ndc_hub_1=hub_1.Install(all_hub_1)
all_hub_2 = ns3.NodeContainer()
all_hub_2.Add (nodo_0)
all_hub_2.Add (nodo_3)
ndc_hub_2=hub_2.Install(all_hub_2)
all_hub_3 = ns3.NodeContainer()
all_hub_3.Add (nodo_0)
all_hub_3.Add (nodo_4)
ndc_hub_3=hub_3.Install(all_hub_3)
all_hub_4 = ns3.NodeContainer()
all_hub_4.Add (nodo_0)
all_hub_4.Add (nodo_5)
ndc_hub_4=hub_4.Install(all_hub_4)
all_hub_5 = ns3.NodeContainer()
all_hub_5.Add (nodo_0)
all_hub_5.Add (nodo_6)
ndc_hub_5=hub_5.Install(all_hub_5)
all_hub_6 = ns3.NodeContainer()
all_hub_6.Add (nodo_0)
all_hub_6.Add (nodo_7)
ndc_hub_6=hub_6.Install(all_hub_6)
#ip stack
internetStackH = ns3.InternetStackHelper()
internetStackH.Install(nodo_0)
internetStackH.Install(nodo_1)
internetStackH.Install(nodo_2)
internetStackH.Install(nodo_3)
internetStackH.Install(nodo_4)
internetStackH.Install(nodo_5)
internetStackH.Install(nodo_6)
internetStackH.Install(nodo_7)
#ip
ipv4.SetBase(ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0)
ipv4.SetBase(ns3.Ipv4Address("10.0.1.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1)
ipv4.SetBase(ns3.Ipv4Address("10.0.2.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_2 = ipv4.Assign (ndc_hub_2)
ipv4.SetBase(ns3.Ipv4Address("10.0.3.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_3 = ipv4.Assign (ndc_hub_3)
ipv4.SetBase(ns3.Ipv4Address("10.0.4.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_4 = ipv4.Assign (ndc_hub_4)
ipv4.SetBase(ns3.Ipv4Address("10.0.5.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_5 = ipv4.Assign (ndc_hub_5)
ipv4.SetBase(ns3.Ipv4Address("10.0.6.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_6 = ipv4.Assign (ndc_hub_6)
#generate route
ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
stopTime = 1
ns3.Simulator.Stop (ns3.Seconds(stopTime))
#inicial simulacion
ns3.Simulator.Run()
ns3.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)
view raw gistfile1.py hosted with ❤ by GitHub


Captura de pantalla:



Anillo 4 nodos
Código generado:

import ns3
def main(argv):
cmd = ns3.CommandLine()
cmd.Parse (argv)
#nodes
nodo_0=ns3.NodeContainer()
nodo_0.Create(1)
nodo_1=ns3.NodeContainer()
nodo_1.Create(1)
nodo_2=ns3.NodeContainer()
nodo_2.Create(1)
nodo_3=ns3.NodeContainer()
nodo_3.Create(1)
#links
hub_0 = ns3.CsmaHelper()
hub_0.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_0.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_1 = ns3.CsmaHelper()
hub_1.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_1.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_2 = ns3.CsmaHelper()
hub_2.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_2.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_3 = ns3.CsmaHelper()
hub_3.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_3.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
#links
all_hub_0 = ns3.NodeContainer()
all_hub_0.Add (nodo_0)
all_hub_0.Add (nodo_1)
ndc_hub_0=hub_0.Install(all_hub_0)
all_hub_1 = ns3.NodeContainer()
all_hub_1.Add (nodo_1)
all_hub_1.Add (nodo_2)
ndc_hub_1=hub_1.Install(all_hub_1)
all_hub_2 = ns3.NodeContainer()
all_hub_2.Add (nodo_2)
all_hub_2.Add (nodo_3)
ndc_hub_2=hub_2.Install(all_hub_2)
#ip stack
internetStackH = ns3.InternetStackHelper()
internetStackH.Install(nodo_0)
internetStackH.Install(nodo_1)
internetStackH.Install(nodo_2)
internetStackH.Install(nodo_3)
#ip
ipv4.SetBase(ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0)
ipv4.SetBase(ns3.Ipv4Address("10.0.1.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1)
ipv4.SetBase(ns3.Ipv4Address("10.0.2.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_2 = ipv4.Assign (ndc_hub_2)
#generate route
ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
stopTime = 1
ns3.Simulator.Stop (ns3.Seconds(stopTime))
#inicial simulacion
ns3.Simulator.Run()
ns3.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)
view raw gistfile1.py hosted with ❤ by GitHub


Captura de pantalla:



Aleatoria 5 nodos
Código generado:

mport ns3
def main(argv):
cmd = ns3.CommandLine()
cmd.Parse (argv)
#nodes
nodo_0=ns3.NodeContainer()
nodo_0.Create(1)
nodo_1=ns3.NodeContainer()
nodo_1.Create(1)
nodo_2=ns3.NodeContainer()
nodo_2.Create(1)
nodo_3=ns3.NodeContainer()
nodo_3.Create(1)
nodo_4=ns3.NodeContainer()
nodo_4.Create(1)
#links
hub_0 = ns3.CsmaHelper()
hub_0.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_0.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_1 = ns3.CsmaHelper()
hub_1.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_1.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_2 = ns3.CsmaHelper()
hub_2.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_2.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
hub_3 = ns3.CsmaHelper()
hub_3.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
hub_3.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(10000)))
#links
all_hub_0 = ns3.NodeContainer()
all_hub_0.Add (nodo_0)
all_hub_0.Add (nodo_1)
ndc_hub_0=hub_0.Install(all_hub_0)
all_hub_1 = ns3.NodeContainer()
all_hub_1.Add (nodo_0)
all_hub_1.Add (nodo_3)
ndc_hub_1=hub_1.Install(all_hub_1)
all_hub_2 = ns3.NodeContainer()
all_hub_2.Add (nodo_0)
all_hub_2.Add (nodo_0)
ndc_hub_2=hub_2.Install(all_hub_2)
all_hub_3 = ns3.NodeContainer()
all_hub_3.Add (nodo_0)
all_hub_3.Add (nodo_0)
ndc_hub_3=hub_3.Install(all_hub_3)
#ip stack
internetStackH = ns3.InternetStackHelper()
internetStackH.Install(nodo_0)
internetStackH.Install(nodo_1)
internetStackH.Install(nodo_2)
internetStackH.Install(nodo_3)
internetStackH.Install(nodo_4)
#ip
ipv4.SetBase(ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0)
ipv4.SetBase(ns3.Ipv4Address("10.0.1.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1)
ipv4.SetBase(ns3.Ipv4Address("10.0.2.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_2 = ipv4.Assign (ndc_hub_2)
ipv4.SetBase(ns3.Ipv4Address("10.0.3.0"), ns3.Ipv4Mask("255.255.255.0"))
iface_ndc_hub_3 = ipv4.Assign (ndc_hub_3)
#generate route
ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
stopTime = 1
ns3.Simulator.Stop (ns3.Seconds(stopTime))
#inicial simulacion
ns3.Simulator.Run()
ns3.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)
view raw gistfile1.py hosted with ❤ by GitHub


Captura de pantalla:



Referencias

3 comentarios:

  1. Hola Cecy, me gusta tu blog, te podrías poner en contacto conmigo para conversar un par de cosas de proyectos que espero te agraden

    Saludos!

    ResponderEliminar
  2. La gráfica de árbol es un anillo :P 6 pts.

    ResponderEliminar