Mininet-wifi custom topology - python

At first I want wanted to create a complex topology (more switches connected to each other) but even this simple one is not working. I am not able to get a working connection eg. 'h1 ping h2'
Here is the topology:
He is the script that should create the equivalent topology:
!/usr/bin/python
"""
Setting the position of Nodes (only for Stations and Access Points) and providing mobility.
"""
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSKernelAP
from mininet.link import TCLink
from mininet.cli import CLI
from mininet.log import setLogLevel
def topology():
"Create a network."
net = Mininet( controller=Controller, link=TCLink, accessPoint=OVSKernelAP )
print "*** Creating nodes"
h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/8' )
h2 = net.addHost( 'h2', mac='00:00:00:00:00:11', ip='10.0.1.1/8' )
sta1 = net.addStation( 'sta1', mac='00:00:00:00:00:02', ip='10.0.0.2/8', position='50,50,0' )
sta2 = net.addStation( 'sta2', mac='00:00:00:00:00:03', ip='10.0.0.3/8', position='40,50,0')
sta3 = net.addStation( 'sta3', mac='00:00:00:00:00:04', ip='10.0.0.4/8', position='20,50,0' )
ap1 = net.addAccessPoint( 'ap1', ssid= 'new-ssid', mode= 'g', channel= '5', position='25,50,0', range='35' )
ap2 = net.addAccessPoint( 'ap2', ssid= 'new-ssid', mode= 'g', channel= '5', position='75,50,0', range='35' )
c1 = net.addController( 'c1' )
s1 = net.addSwitch('s1')
#net.runAlternativeModule('../module/mac80211_hwsim.ko')
print "*** Configuring wifi nodes"
net.configureWifiNodes()
print "*** Associating and Creating links"
net.addLink(ap1, s1)
net.addLink(ap2, s1)
net.addLink(s1, c1)
#net.addLink(ap1, ap2)
net.addLink(s1, h1)
net.addLink(s1, h2)
net.addLink(ap1, sta1)
net.addLink(ap1, sta2)
net.addLink(ap1, sta3)
print "*** Starting network"
net.build()
c1.start()
ap1.start( [c1] )
ap2.start( [c1] )
"""uncomment to plot graph"""
net.plotGraph(max_x=100, max_y=100)
net.startMobility(startTime=0)
net.mobility(sta1, 'start', time=1, position='0.0,50.0,0.0')
net.mobility(sta1, 'stop', time=30, position='100.0,50.0,0.0')
net.stopMobility(stopTime=31)
print "*** Running CLI"
CLI( net )
print "*** Stopping network"
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
topology()
As already said, I am not able to even ping h1 and h2 :(
I even tried to use RemoteController but got the same result.
Any idea what might be wrong?

Please consider the changes below:
from mininet.node import Controller, RemoteController, OVSKernelAP, OVSSwitch
net = Mininet( controller=Controller, link=TCLink, accessPoint=OVSKernelAP, switch=OVSSwitch )
c1 = net.addController( 'c1', controller=Controller )
s3 = net.addSwitch('s3')
Remove:
net.addLink(s1, c1)
Add:
s3.start( [c1] )
Please note that if you set both ap and switch with the same ID (e.g. s1, ap1), they will have the same DPID by default. So, I had to rename s1 to s3. On the other hand you may define the DPID as a parameter when you add the node whether you want.

Related

trouble in connecting to opendaylight using python API

I tried this code in python to connect to controller:
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSKernelSwitch, UserSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Link, TCLink
def topology():
net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch )
# Add hosts and switches
h1 = net.addHost( 'h1', ip="10.0.1.10/24", mac="00:00:00:00:00:01" )
h2 = net.addHost( 'h2', ip="10.0.2.10/24", mac="00:00:00:00:00:02" )
r1 = net.addHost( 'r1')
s1 = net.addSwitch( 's1')
s2 = net.addSwitch( 's2')
c0 = net.addController( 'c0', controller=RemoteController, ip='192.168.19.132', port=6633 )
net.addLink( r1, s1 )
net.addLink( r1, s2 )
net.addLink( h1, s1 )
net.addLink( h2, s2 )
net.build()
c0.start()
s1.start( [c0] )
s2.start( [c0] )
r1.cmd("ifconfig r1-eth0 0")
r1.cmd("ifconfig r1-eth1 0")
r1.cmd("ifconfig r1-eth0 hw ether 00:00:00:00:01:01")
r1.cmd("ifconfig r1-eth1 hw ether 00:00:00:00:01:02")
r1.cmd("ip addr add 10.0.1.1/24 brd + dev r1-eth0")
r1.cmd("ip addr add 10.0.2.1/24 brd + dev r1-eth1")
r1.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
h1.cmd("ip route add default via 10.0.1.1")
h2.cmd("ip route add default via 10.0.2.1")
s1.cmd("ovs-ofctl add-flow s1 priority=1,arp,actions=flood")
s1.cmd("ovs-ofctl add-flow s1 priority=65535,ip,dl_dst=00:00:00:00:01:01,actions=output:1")
s1.cmd("ovs-ofctl add-flow s1 priority=10,ip,nw_dst=10.0.1.0/24,actions=output:2")
s2.cmd("ovs-ofctl add-flow s2 priority=1,arp,actions=flood")
s2.cmd("ovs-ofctl add-flow s2 priority=65535,ip,dl_dst=00:00:00:00:01:02,actions=output:1")
s2.cmd("ovs-ofctl add-flow s2 priority=10,ip,nw_dst=10.0.2.0/24,actions=output:2")
print ("*** Running CLI")
CLI( net )
print ("*** Stopping network")
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
topology()
but when I check dlux in browser it doesn't show any nodes.
if I use CLI and set the topology using sudo mn and give it the ip of my controller then it works.
why doesn't it work with python?
I used ubuntu 20.04 and my opendaylight version is nitrogen.
thanks in advance
You need to be more specific with defining your remote controller. If you have the controller installed on the same Linux host as Mininet had been installed on, then you need to define you controller IP and port like below. I used this script with ODL Lithium version and it worked with no problem.
#!/usr/bin/env python
from mininet.net import Mininet
from mininet.link import TCIntf
from mininet.node import CPULimitedHost
from mininet.topolib import TreeTopo
from mininet.util import custom, quietRun
from mininet.log import setLogLevel, info
from mininet.cli import CLI
from mininet.node import RemoteController
from mininet.link import TCLink
setLogLevel( 'info' )
net = Mininet( link=TCLink )
c0 = net.addController(name='c0' ,
controller=RemoteController ,
ip='localhost',
port=6653) // # the port for newer versions might be different
s1 = net.addSwitch('s1', protocols='OpenFlow13')
s2 = net.addSwitch('s2', protocols='OpenFlow13')
net.addLink(s1, s2, delay='50ms')
net.addLink(s1, s2, bw=0.2)
net.addLink(s1, s2)
net.start()
CLI(net)

Mininet-wifi Cannot import name OVSKernelAP with Python

I am building a SDN network using Mininet-Wifi and I have this smallnetwork as a test just to learn more about Python, so I this code to run
!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSKernelAP
from mininet.link import TCLink
from mininet.cli import CLI
from mininet.log import setLogLevel
def topology():
"Create a network."
net = Mininet( controller=Controller, link=TCLink, accessPoint=OVSKernelAP )
print "*** Creating nodes"
h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/8' )
h2 = net.addHost( 'h2', mac='00:00:00:00:00:11', ip='10.0.1.1/8' )
sta1 = net.addStation( 'sta1', mac='00:00:00:00:00:02', ip='10.0.0.2/8', position='50,50,0' )
sta2 = net.addStation( 'sta2', mac='00:00:00:00:00:03', ip='10.0.0.3/8', position='40,50,0')
sta3 = net.addStation( 'sta3', mac='00:00:00:00:00:04', ip='10.0.0.4/8', position='20,50,0' )
ap1 = net.addAccessPoint( 'ap1', ssid= 'new-ssid', mode= 'g', channel= '5',
position='25,50,0', range='35' )
ap2 = net.addAccessPoint( 'ap2', ssid= 'new-ssid', mode= 'g', channel= '5',
position='75,50,0', range='35' )
c1 = net.addController( 'c1' )
s1 = net.addSwitch('s1')
#net.runAlternativeModule('../module/mac80211_hwsim.ko')
print "*** Configuring wifi nodes"
net.configureWifiNodes()
print "*** Associating and Creating links"
net.addLink(ap1, s1)
net.addLink(ap2, s1)
net.addLink(s1, c1)
#net.addLink(ap1, ap2)
net.addLink(s1, h1)
net.addLink(s1, h2)
net.addLink(ap1, sta1)
net.addLink(ap1, sta2)
net.addLink(ap1, sta3)
print "*** Starting network"
net.build()
c1.start()
ap1.start( [c1] )
ap2.start( [c1] )
"""uncomment to plot graph"""
net.plotGraph(max_x=100, max_y=100)
net.startMobility(startTime=0)
net.mobility(sta1, 'start', time=1, position='0.0,50.0,0.0')
net.mobility(sta1, 'stop', time=30, position='100.0,50.0,0.0')
net.stopMobility(stopTime=31)
print "*** Running CLI"
CLI( net )
print "*** Stopping network"
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
topology()
And after runing "sudo mn --custom test.py" (My file name is test.py ) I get the error.
Caught Excepcion. Cleaning up... ImportError: cannot import name OVSKernelAP
What Can i do? I have mininet and mininet-wifi already installed.
The main source of your error is in lines 3 to 7. With Mininet-wifi you just need to import the following:
from mininet.log import setLogLevel, info
from mn_wifi import CLI
from mn_wifi import Mininet_wifi
That's all you need to import. You might have a few more errors such as the minimum range of your access points should be 116m and s1 and ap1 will have the same DPID so you should change s1 to s3

Creating a topology using python

I want to build a mininet topology using python API. My topology is supposed to have 2 controllers, 3 switches and each switch will have several hosts connected to it (number of connected hosts is 2, 4, 1 respectively). Here is the code:
#!/usr/bin/python
import time
from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
def net():
net = Mininet(controller=RemoteController, switch=OVSKernelSwitch)
c1 = net.addController('c1', controller=RemoteController, ip="127.0.0.1", port=6653)
c2 = net.addController('c2', controller=RemoteController, ip="127.0.0.1", port=7753)
s_n = 3 #number of switches
c_n = 2 #number of controllers
hosts = []
amount = [2, 4, 1] # number of hosts that each switch has
info( "*** Creating switches\n" )
switches = [ net.addSwitch( 's%s' % s ) for s in range( 1, s_n ) ]
index1 = 0
L_in = 0
index2 = 0
info( "*** Creating hosts\n" )
for s in switches:
L_in = (L_in + 1)
if L_in <= len(amount):
index1 = (index2 + 1)
index2 = (index1 + amount[L_in]) - 1
hosts = [ net.addHost( 'h%s' % n ) for n in ( index1, index2 ) ]
for h in hosts:
net.addLink( s, h )
else:
break
# Wire up switches
last = None
for switch in switches:
if last:
net.addLink( last, switch )
last = switch
net.build()
c1.start()
c2.start()
for sw in switches[:c_n]:
sw.start( [c1] )
for sw in switches[c_n:]:
sw.start( [c2] )
net.start()
CLI( net )
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
net()
But when I run the code, it is not working correctly. For example, I expect 3 switches, but 2 switches are created. I expect 7 hosts, but 4 hosts are created. The connection between hosts and switches is not correct, too. Here is the output:
*** Creating switches
*** Creating hosts
*** Configuring hosts
h1 h4 h5 h5
*** Starting controller
c1 c2
*** Starting 2 switches
s1 s2 ...
*** Starting CLI:
mininet> net
h1 h1-eth0:s1-eth1
h4 h4-eth0:s1-eth2
h5 h5-eth0:s2-eth2
h5 h5-eth0:s2-eth2
s1 lo: s1-eth1:h1-eth0 s1-eth2:h4-eth0 s1-eth3:s2-eth3
s2 lo: s2-eth1:h5-eth0 s2-eth2:h5-eth0 s2-eth3:s1-eth3
c1
c2
A number of problems here.
range(1, n) produces numbers from 1 no n-1, not to n.
You define function net that will shadow the previous (imported?) definition of module net. Call it make_net or something.
Explicit loop indices like L_in is almost always a bad idea, same as non-descriptive names as index2.
Something like this should give you the idea:
n_switches = 3
hosts_per_switch = [2, 4, 1]
switches = [addSwitch("s%d" % n + 1) for n in range(n_switches)]
for (switch, number_of_hosts) in zip(switches, hosts_per_switch): # Pair them.
hosts = [addHost("h%d" % n + 1) for n in range(number_of_hosts)]
for host in hosts:
addLink(switch, host)

ryu (SDN) traffic controller from (example) Host 1

I created a simple network in the mininet environment with the following command:
$ sudo mn --topo single,3 --mac --controller remote --switch ovsk
I want use RYU CONTROLLER to calculate bandwidth traffic on special port of switch. I'm thinking of using an OFPEVENTPacketIn event for the incoming packet, but I do not any idea for out coming packet form port.
My code:
from operator import attrgetter
from ryu.app import simple_switch_13
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.lib import hub
class SimpleMonitor(simple_switch_13.SimpleSwitch13):
def __init__(self, *args, **kwargs):
super(SimpleMonitor, self).__init__(*args, **kwargs)
self.datapaths = {}
self.monitor_thread = hub.spawn(self._monitor)
#set_ev_cls(ofp_event.EventOFPStateChange,
[MAIN_DISPATCHER, DEAD_DISPATCHER])
def _state_change_handler1(self, ev):
datapath = ev.datapath
if ev.state == MAIN_DISPATCHER:
if not datapath.id in self.datapaths:
self.logger.debug('register datapath: %016x', datapath.id)
self.datapaths[datapath.id] = datapath
elif ev.state == DEAD_DISPATCHER:
if datapath.id in self.datapaths:
self.logger.debug('unregister datapath: %016x', datapath.id)
del self.datapaths[datapath.id]
def _monitor(self):
while True:
for dp in self.datapaths.values():
self._request_stats(dp)
hub.sleep(10)
def _request_stats(self, datapath):
self.logger.debug('send stats request: %016x', datapath.id)
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
req = parser.OFPFlowStatsRequest(datapath)
datapath.send_msg(req)
req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY)
datapath.send_msg(req)
#set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def _flow_stats_reply_handler(self, ev):
body = ev.msg.body
self.logger.info('datapath '
'in-port eth-dst '
'out-port packets bytes')
self.logger.info('---------------- '
'-------- ----------------- '
'-------- -------- --------')
for stat in sorted([flow for flow in body if flow.priority == 1],
key=lambda flow: (flow.match['in_port'],
flow.match['eth_dst'])):
# self.logger.info('%016x %8x %17s %8x %8d %8d',
if stat.match['in_port']==1:
self.logger.info('%x %x %s %x %d %d',
ev.msg.datapath.id,
stat.match['in_port'], stat.match['eth_dst'],
stat.instructions[0].actions[0].port,
stat.packet_count, stat.byte_count)
#for stat in [1234]#sorted([flow for flow in body if flow.priority == 1],
# key=lambda flow: (flow.match['in_port'],
# flow.match['eth_dst'])):
# self.logger.info('%016x %8x %17s %8x %8d %8d',
# if stat.match['in_port']==1:
# self.logger.info('%x %x %s %x %d %d',
# ev.msg.datapath.id,
# stat.match['in_port'], stat.match['eth_dst'],
# stat.instructions[0].actions[0].port,
# stat.packet_count, stat.byte_count)
#set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
def _port_stats_reply_handler(self, ev):
body = ev.msg.body
self.logger.info('datapath port '
'rx-pkts rx-bytes rx-error '
'tx-pkts tx-bytes tx-error')
self.logger.info('---------------- -------- '
'-------- -------- -------- '
'-------- -------- --------')
for stat in sorted(body, key=attrgetter('port_no')):
if stat.port_no==1:
self.logger.info('%016x %8x %8d %8d %8d %8d %8d %8d',
ev.msg.datapath.id, stat.port_no,
stat.rx_packets, stat.rx_bytes, stat.rx_errors,
stat.tx_packets, stat.tx_bytes, stat.tx_errors)
Please help me edit this code to answer my question. Thanks.
You have to add two method in your controller:
1) a method that requests stats
2) a method that captures the answers from switches.
The method for answering stats is the following:
def send_flow_stats_request(self, datapath):
ofp = datapath.ofproto
ofp_parser = datapath.ofproto_parser
cookie = cookie_mask = 0
match = ofp_parser.OFPMatch(in_port=1)
req = ofp_parser.OFPFlowStatsRequest(datapath, 0,
ofp.OFPTT_ALL,
ofp.OFPP_ANY, ofp.OFPG_ANY,
cookie, cookie_mask,
match)
datapath.send_msg(req)
The method for capturing answers is this one:
#set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def flow_stats_reply_handler(self, ev):
flows = []
for stat in ev.msg.body:
flows.append('table_id=%s '
'duration_sec=%d duration_nsec=%d '
'priority=%d '
'idle_timeout=%d hard_timeout=%d flags=0x%04x '
'cookie=%d packet_count=%d byte_count=%d '
'match=%s instructions=%s' %
(stat.table_id,
stat.duration_sec, stat.duration_nsec,
stat.priority,
stat.idle_timeout, stat.hard_timeout, stat.flags,
stat.cookie, stat.packet_count, stat.byte_count,
stat.match, stat.instructions))
self.logger.debug('FlowStats: %s', flows)
Once you capture stats you can compute bandwidth usage.
However you have also other kind of requests you can perform so i suggest you to read the rye doc.
http://ryu.readthedocs.io/en/latest/

Parse PDB Symbol and Resolve Address

Using a python based disassembler + debugger I've found below instructions(example). Now I want to parse Microsoft provided public symbols to find exact functions its calling.
I want to know what are the available options/ modules to do the same. Can we just simply get the info from a static PDB files or its required to load that in memory while debugging ?
call ntdll!0x33dec
call ntdll!0x22280
call ntdll!0x2df40
call ntdll!0x33cdb
call ntdll!0x2df29
call ntdll!0x325a0
call ntdll!0x32a96
call ntdll!0x32a79
call ntdll!0x220a4
A sample that uses capstone for dis-assembly and dbghelp apis for symbol resolving of an immediate E8 call
import sys #for argv[]
import binascii #for hexlify repr() spits out ugly mix like
#'\xe8y\xff\' instead of '\xe8\x79\xff' :(
from ctypes import *
from capstone import *
class SYMBOL_INFO(Structure):
_fields_ = [
( 'SizeOfStruct', c_ulong ),
( 'TypeIndex', c_ulong ),
( 'Reserved', c_ulonglong * 2 ),
( 'Index', c_ulong ),
( 'Size', c_ulong ),
( 'ModBase', c_ulonglong ),
( 'Flags', c_ulong ),
( 'Value', c_ulonglong ),
( 'Address', c_ulonglong ),
( 'Register', c_ulong ),
( 'Scope', c_ulong ),
( 'Tag' , c_ulong ),
( 'NameLen', c_ulong ),
( 'MaxNameLen', c_ulong ),
( 'Name', c_char * 2001 )
]
modname = sys.argv[1]
offset = long(sys.argv[2],16)
sympath = "xxx:\\yyyyy" # substitute actual path
base = windll.LoadLibrary(modname)._handle
symaddr = c_ulonglong(base + offset)
print "Module name = %s\nModule Base = %s\nSymFromAddr = %s" % \
(modname,hex(base),hex(symaddr.value))
dbghelp = windll.dbghelp
k32 = windll.kernel32
hproc = k32.GetCurrentProcess()
dbghelp.SymInitialize(hproc,sympath,1)
sinfo = SYMBOL_INFO()
sinfo.SizeOfStruct = sizeof(SYMBOL_INFO) - 2000
sinfo.MaxNameLen = 2000
Displacement = c_ulonglong()
dbghelp.SymFromAddr(hproc,symaddr,addressof(Displacement),addressof(sinfo))
print "Sym At Addr = %s + %s" % (sinfo.Name,str(hex(Displacement.value)))
opcodebuff = create_string_buffer(16)
memmove(opcodebuff,symaddr.value,16)
for i in range(0,16,1):
print binascii.hexlify(opcodebuff.raw[i]),
print
MyDisasm = Cs(CS_ARCH_X86, CS_MODE_32)
for i in MyDisasm.disasm(opcodebuff, symaddr,1):
print "0x%x: %s %s %s" % ( i.address ,binascii.hexlify(i.bytes),
i.mnemonic, i.op_str)
if(i.mnemonic == 'call'):
try:
symaddr = c_ulonglong(long(i.op_str,16))
dbghelp.SymFromAddr(hproc,symaddr,addressof(Displacement),addressof(sinfo))
print "(%s+%s)" % (sinfo.Name,str(hex(Displacement.value))),
print "(%s+0x%X)"% (modname ,long(i.op_str,16)-base)
except:
print "Indirect/register Calls Not Handled Yet"
pass
usage as follows
python dumpsym.py ntdll 1041
first argument is a string that represents a module
second argument is a string that represents an offset in the module
so if module is loaded at 0xxxxxxxxx offset 1041 will point to the address
0xxxxxxxxx+0x1041
output
Module name = ntdll
Module Base = 0x7c900000
SymFromAddr = 0x7c901041L
Sym At Addr = RtlEnterCriticalSection + 0x41L
e8 79 a1 01 00 64 8b 0d 18 00 00 00 8b 54 24 04
0x7c901041: e879a10100 call 0x7c91b1bf
(RtlpWaitForCriticalSection+0x0L) (ntdll+0x1B1BF)
You can get symbols from PDB file using DbgHelp API. This is the same API WinDbg uses, and you can use it to debug live processes as well as simply extract symbols based on offset of the loaded module. In your case SymFromAddr function would be easiest to use to get symbol from an address.
One non-automated but quick way to do that would be :
Load ntdll.dll in the debugger as a dump file.
Load the symbols using the public symbol path - or local cache.
Use the ln command. Like: ln ntdll!0x33dec

Categories

Resources