逻辑上,系统整体拓扑包括三部分:
实现上,系统的各个部分:
参考帖子:Mininet主机与真实网络互通方案实现
本质上是利用mininet/examples/hwintf.py来做这个事情,代码很短,贴在下面:
#!/usr/bin/python
"""
This example shows how to add an interface (for example a real
hardware interface) to a network after the network is created.
"""
import re
import sys
from mininet.cli import CLI
from mininet.log import setLogLevel, info, error
from mininet.net import Mininet
from mininet.link import Intf
from mininet.topolib import TreeTopo
from mininet.util import quietRun
def checkIntf( intf ):
"Make sure intf exists and is not configured."
config = quietRun( 'ifconfig %s 2>/dev/null' % intf, shell=True )
if not config:
error( 'Error:', intf, 'does not exist!n' )
exit( 1 )
ips = re.findall( r'd+.d+.d+.d+', config )
if ips:
error( 'Error:', intf, 'has an IP address,'
'and is probably in use!n' )
exit( 1 )
if __name__ == '__main__':
setLogLevel( 'info' )
# 命令行参数传入要加载的物理端口名(默认是eth1)
intfName = sys.argv[ 1 ] if len( sys.argv ) > 1 else 'eth1'
info( '*** Connecting to hw intf: %s' % intfName )
info( '*** Checking', intfName, 'n' )
checkIntf( intfName )
info( '*** Creating networkn' )
net = Mininet( topo=TreeTopo( depth=1, fanout=2 ) )
switch = net.switches[ 0 ]
info( '*** Adding hardware interface', intfName, 'to switch',
switch.name, 'n' )
# 关键代码:将物理端口给sdn交换机设置上
_intf = Intf( intfName, node=switch )
info( '*** Note: you may need to reconfigure the interfaces for '
'the Mininet hosts:n', net.hosts, 'n' )
net.start()
CLI( net )
net.stop()
整段代码关键部分就两:检查网卡是否可用和把网卡设置到ovs交换机上。
要将“物理网卡”(比如我用的是enp0s3)设置在ovs交换机上,必须保证这个端口没有被ubuntu系统本身使用,也就是要先停用该端口,停用的命令(其实就是清除ip):
sudo ifconfig enp0s3 0.0.0.0
注:这个“物理网卡”实际上一点也不物理。因为Ubuntu虚拟机的网卡是桥接在windows的网卡上的,并且开启了混杂模式使得可以抓取到宿主机网卡的流量;而这个宿主机的网卡是开无线热点而虚拟出来的网卡,供thinkpad-t430连接。所以这个“物理网卡”仅仅对Ubuntu系统是物理网卡,而从整体上看是虚拟中的虚拟。
为了让mininet网络内部的主机能访问外网,需要给其配置对应的ip和路由等。例如:桥接的宿主机网卡ip地址为 192.168.137.1/24
,那么可以这样设置mininet网络的主机(在mininet命令行模式下):
# 配置ip地址
h1 ifconfig h1-eth0 192.168.137.231 netmask 255.255.255.0
# 配置默认网关
h1 route add default gw 192.168.137.1