• mininet搭建SDN环境访问互联网【C4】


    主机访问外网

    Vmware+Ubuntu14.04+mininet中的host如何访问外网

    1.虚拟机设置

    第一步:两个网络适配器,NAT桥接模式
    在这里插入图片描述

    第二步 打开虚拟网络编辑器,设置子网IP
    在这里插入图片描述

    在这里插入图片描述
    第三步:NAT设置:设置网关IP
    在这里插入图片描述
    第四步 DHCP设置:起始ip和结束ip随便取一个范围
    在这里插入图片描述

    以上配置已经为Ubuntu配置好了网络。

    2.启动Ubuntu,查看网卡信息

    在这里插入图片描述

    测试ping通互联网

    在这里插入图片描述

    3.路由

    在这里插入图片描述ping -I ens38 baidu.com

    4.配置ens38(第二个网卡)

    我们把ens38的IP设置为:0.0.0.0,这样这个闲置的网卡资源就可以被用来桥接到mininet网络中的交换机上
    在这里插入图片描述

    查看IP地址时ens38已经看不到IP地址了

    5.打开控制器

    ryu控制器:
    先运行simple_monitor_13.py(ryu/ryu/app)
    :ryu-manager simple_monitor_13.py
    再运行gui_topology.py(ryu/ryu/app/gui_topology)
    :ryu-manager gui_topology.py

    floodlight控制器:java -jar target/floodlight.jar

    6.编写python脚本创建mininet网络

    mininet/mininet/examples:xffTopo.py

    在这里插入图片描述

    #!/usr/bin/python
    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
    from mininet.node import OVSSwitch, OVSController, Controller, RemoteController
    from mininet.topo import Topo
    class MyTopo( Topo ):
    #    "this topo is used for Scheme_1"
        
        def __init__( self ):
            "Create custom topo."
     
            # Initialize topology
            Topo.__init__( self )
     
            # Add hosts 
            h1 = self.addHost( 'h1' , ip="10.0.0.1/24", mac="00:00:00:00:00:01", defaultRoute="via 10.0.0.254")
            h2 = self.addHost( 'h2' , ip="10.0.0.2/24", mac="00:00:00:00:00:02", defaultRoute="via 10.0.0.254")
            h3 = self.addHost( 'h3' , ip="10.0.0.3/24", mac="00:00:00:00:00:03", defaultRoute="via 10.0.0.254")
            h4 = self.addHost( 'h4' , ip="10.0.0.4/24", mac="00:00:00:00:00:04", defaultRoute="via 10.0.0.254")
            
            # Add switches
            s1 = self.addSwitch( 's1' )
            s2 = self.addSwitch( 's2' )
            s3 = self.addSwitch( 's3' )
     
            # Add links
            self.addLink( s1, s2 )
            self.addLink( s1, s3 )
            self.addLink( s2, h1 )
            self.addLink( s2, h2 )
            self.addLink( s3, h3 )
            self.addLink( s3, h4 )
    # 检查eth1或者其他指定的网卡资源是不是已经被占用
    def checkIntf( intf ):
        "Make sure intf exists and is not configured."
        if ( ' %s:' % intf ) not in quietRun( 'ip link show' ):
            error( 'Error:', intf, 'does not exist!\n' )
            exit( 1 )
        ips = re.findall( r'\d+\.\d+\.\d+\.\d+', quietRun( 'ifconfig ' + intf ) )
        if ips:
            error( 'Error:', intf, 'has an IP address,'
                   'and is probably in use!\n' )
            exit( 1 )
     
    if __name__ == '__main__':
        setLogLevel( 'info' )
     
        # try to get hw intf from the command line; by default, use eth1
        intfName = sys.argv[ 1 ] if len( sys.argv ) > 1 else 'ens38'
        info( '*** Connecting to hw intf: %s' % intfName )
     
        info( '*** Checking', intfName, '\n' )
        checkIntf( intfName )
     
        info( '*** Creating network\n' )
        net = Mininet( topo=MyTopo(),controller=None) 
        #关键函数,创建mininet网络,指定拓扑和控制器。这里的控制器在后面添加进去
        switch = net.switches[ 0 ] # 取第一个交换机与eth1桥接
        info( '*** Adding hardware interface', intfName, 'to switch', switch.name, '\n' )
        _intf = Intf( intfName, node=switch )
         # 最关键的函数,用作把一个网卡与一个交换机桥接
     
        info( '*** Note: you may need to reconfigure the interfaces for '
              'the Mininet hosts:\n', net.hosts, '\n' )
        c0 = RemoteController( 'c0', ip='127.0.0.1', port=6653 )
        net.addController(c0)
        net.start()
        CLI( net )
        net.stop()
    topos = {'mytopo':(lambda:MyTopo())}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    sudo mn --controller=remote --custom ./xffTopo.py --topo mytopo
    
    • 1

    在这里插入图片描述

    通过上面的脚本运行后,在floodlight web UI中可以看到创建了如下拓扑

    在这里插入图片描述

    在这里插入图片描述

    打开h1的独立窗口

    在这里插入图片描述

    论文学习

    搭建拓扑

    在这里插入图片描述
    c0:127.0.0.1:6653
    s1:0000000000000001
    h1:10.0.0.1
    h2:10.0.0.2
    h3:10.0.0.3(作为传输媒介与互联网直接相连)

    文件保存在:home/mininet/examples:xff_net_topo.mn

    ryu控制器

    先运行simple_monitor_13.py

    zxd@zxd-virtual-machine:~/ryu/ryu/app$ ryu-manager simple_monitor_13.py
    
    • 1

    新增一台主机连接互联网的python代码

    from mininet.cli import CLI
    from mininet.log import lg, info
    from mininet.topolib import TreeNet
    
    
    if __name__ == '__main__':
        lg.setLogLevel( 'info')
        net = TreeNet( depth=1, fanout=4, waitConnected=True )
        # Add NAT connectivity
        net.addNAT().configDefault()
        net.start()
        info( "*** Hosts are running and should have internet connectivity\n" )
        info( "*** Type 'exit' or control-D to shut down network\n" )
        CLI( net )
        # Shut down NAT
        net.stop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    kubernetes(5)Controller的一些核心概念
    # 设计模式 #5.6 Memento备忘录,行为型模式
    Springboot Security 前后端分离模式自由接口最小工作模型
    C++智能指针,强制类型转换
    数字信号处理-9-离散余弦变换
    leetcode 13
    【Qt】如何在麒麟操作系统上配置开发环境
    应用层协议 -- HTTPS 协议
    【C语言】-程序环境和预处理指令
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
  • 原文地址:https://blog.csdn.net/weixin_47229654/article/details/125564008