2015初赛练习题

本文详细介绍如何使用Floodlight控制器及Mininet搭建简单的网络拓扑,包括基本和高级实验,涉及流表配置、访问限制和代理访问等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搭建环境

  • sudo apt-get install build-essential default-jdk ant python-dev
  • sudo apt-get install git
  • git clone git://github.com/floodlight/floodlight.git
  • cd floodlight
  • ant # 根据build.xml会生成target目录
  • java -jar target/floodlight.jar
  • http://localhost:8080/ui/index.html
  • ssh登陆mininet
  • sudo mn –custom ~/topo-2sw-2host.py –topo mytopo –controller=remote,ip=192.168.56.103,port=6653

关于sudo apt-get install build-essential default-jdk ant python-dev eclipse

  • 这是配置Floodlight是首先用到的一句话,安装了四个包
  • dpkg -s build-essential default-jdk ant python-dev eclipse 可以查看各个包的信息
  • sudo apt-cache depends build-essential 可以查看这个包的依赖信息,表示这些依赖的都已经装好了
  • apt-get安装的jdk就不用配置环境变量了如果自动安装的就不需要配置环境变量了,配置环境变量的目的是告诉系统
  • 有这样一些资源,典型的是你使用tab键能够找到可以运行的程序,如果这个程序的位置没有设置在path环境变量里面就无法自动提示
  • linux发行版通常会把类库的头文件和相关的pkg-config分拆成一个单独的xxx-dev(el)包.
  • 以python为例, 以下情况你是需要python-dev的
  • 你需要自己安装一个源外的python类库, 而这个类库内含需要编译的调用python api的c/c++文件 //如:安装使用WiringpisPi库需要python-dev
  • 你自己写的一个程序编译需要链接libpythonXX.(a|so)
  • (注:以上不含使用ctypes/ffi或者裸dlsym方式直接调用libpython.so)
  • 其他正常使用python或者通过安装源内的python类库的不需要python-dev.

第一题:基础题

第1小题:简单网络

  • 搭建自己的拓扑
  • 我所有的拓扑文件都放到了~/目录下
    //topo-2sw-2host.py
    """Custom topology example
    Two directly connected switches plus a host for each switch:
       host --- switch --- switch --- host
    Adding the 'topos' dict with a key/value pair to generate our newly defined
    topology enables one to pass in '--topo=mytopo' from the command line.
    """
    from mininet.topo import Topo
    class MyTopo( Topo ):
        "Simple topology example."
        def __init__( self ):
            "Create custom topo."
            # Initialize topology
            Topo.__init__( self )
            # Add hosts and switches
            leftHost = self.addHost( 'h1' )
            rightHost = self.addHost( 'h2' )
            leftSwitch = self.addSwitch( 's3' )
            rightSwitch = self.addSwitch( 's4' )
            # Add links
            self.addLink( leftHost, leftSwitch )
            self.addLink( leftSwitch, rightSwitch )
            self.addLink( rightSwitch, rightHost )
    topos = { 'mytopo': ( lambda: MyTopo() ) }
  • sudo mn –custom ~/topo-2sw-2host.py –topo mytopo –controller=remote,ip=192.168.56.103,port=6653
  • 修改默认流表转发路径:Ubuntu ~/floodlight/src/main/resources/floodlightdefault.properties
  • cp floodlightdefault.properties floodlightdefault.properties.noforwarding
  • cp floodlightdefault.properties floodlightdefault.properties.forwarding
  • cp floodlightdefault.properties.noforwarding floodlightdefault.properties
  • ant一下(这个不用),会根据属性文件自动加载模块
  • 将第一行forwarding那句话加#注释,重新ant一下


  • 手动下发流表

    curl -d '{"switch":"00:00:00:00:00:00:00:03","name":"flow-mod-1","cookie":"0","priority":"32767","in_port":"1","active":"true","actions":"output=2"}' http://192.168.56.103:8080/wm/staticflowpusher/json
    curl -d '{"switch":"00:00:00:00:00:00:00:03","name":"flow-mod-2","cookie":"0","priority":"32767","in_port":"2","active":"true","actions":"output=1"}' http://192.168.56.103:8080/wm/staticflowpusher/json
    curl -d '{"switch":"00:00:00:00:00:00:00:04","name":"flow-mod-3","cookie":"0","priority":"32767","in_port":"1","active":"true","actions":"output=2"}' http://192.168.56.103:8080/wm/staticflowpusher/json
    curl -d '{"switch":"00:00:00:00:00:00:00:04","name":"flow-mod-4","cookie":"0","priority":"32767","in_port":"2","active":"true","actions":"output=1"}' http://192.168.56.103:8080/wm/staticflowpusher/json
{
    "switch": "00:00:00:00:00:00:00:03",
    "name": "flow-mod-1",
    "cookie": "0",
    "priority": "32767",
    "in_port": "1",
    "active": "true",
    "actions": "output=2"
}
{
    "switch": "00:00:00:00:00:00:00:03",
    "name": "flow-mod-2",
    "cookie": "0",
    "priority": "32767",
    "in_port": "2",
    "active": "true",
    "actions": "output=1"
}
{
    "switch": "00:00:00:00:00:00:00:04",
    "name": "flow-mod-3",
    "cookie": "0",
    "priority": "32767",
    "in_port": "1",
    "active": "true",
    "actions": "output=2"
}
{
    "switch": "00:00:00:00:00:00:00:04",
    "name": "flow-mod-4",
    "cookie": "0",
    "priority": "32767",
    "in_port": "2",
    "active": "true",
    "actions": "output=1"
}

第2小题:访问限制

  • 搭建自己的拓扑
    //AccesslimitTopo.py
    from mininet.topo import Topo
    class MyTopo( Topo ):
        "Simple topology example."
        def __init__( self ):
            "Create custom topo."
            # Initialize topology
            Topo.__init__( self )
            # Add hosts and switches
            Host1 = self.addHost( 'Host1' )
            Server1 = self.addHost( 'Server1' )
            Switch1 = self.addSwitch( 'Switch1' )
            Switch2 = self.addSwitch( 'Switch2' )
            # Add links
            self.addLink( Host1,Switch1 )
            self.addLink( Switch1, Switch2 )
            self.addLink( Switch2, Server1 )
    topos = { 'mytopo': ( lambda: MyTopo() ) }
    //Serer1.py
    import SimpleHTTPServer
    import SocketServer
    class SETHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
        def createHTML(self):
            html = file("/home/mininet/Server1.html", "r")
            for line in html:
                self.wfile.write(line)
        def do_GET(self):
            print "GET"
            print self.headers;
            self.createHTML()
        def do_POST(self):
            print "POST"
            print self.headers;
            length = int(self.headers.getheader('content-length'))
            qs = self.rfile.read(length)
            url=urldecode(qs)
            print "url="
            print url
            self.createHTML()
    Handler = SETHandler
    PORT = 80
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    print "Start Server1......"
    print "serving at port", PORT
    httpd.serve_forever()
//Serer1.html
<html>
    <head><title>Server1</title></head>
    <body bgcolor="red"><p align="center">
            Hello , this is Server1!</p></body>
</html>
  • 把forwarding的模块加上
  • mininet> xterm Server1 && python Serer1/Serer1.py
  • mininet> xterm Host1 && wget -O - 10.0.0.2 或者 wget -O http://10.0.0.2 O参数是显示在命令行
  • 注意查看一下Server1.py中的html文件的位置
  • mininet> Server1 firefox localhost 访问Server1提供的静态网页
  • 限制访问服务器流表 mininet
    Switch1 dpctl add-flow tcp:127.0.0.1:6634 in_port=1,idle_timeout=60,actions=output:2
    Switch1 dpctl add-flow tcp:127.0.0.1:6634 in_port=2,idle_timeout=60,actions=output:1
    Switch2 dpctl add-flow tcp:127.0.0.1:6634 in_port=1,idle_timeout=60,actions=output:2
    Switch2 dpctl add-flow tcp:127.0.0.1:6634 in_port=2,idle_timeout=60,actions=output:1
{
    "switch": "00:00:00:00:00:00:00:01",
    "name": "flow-mod-1",
    "cookie": "0",
    "priority": "32767",
    "in_port": "1",
    "idle_timeout": "60",
    "active": "true",
    "actions": "output=2"
}
{
    "switch": "00:00:00:00:00:00:00:01",
    "name": "flow-mod-2",
    "cookie": "0",
    "priority": "32767",
    "in_port": "2",
    "idle_timeout": "60",
    "active": "true",
    "actions": "output=1"
}
{
    "switch": "00:00:00:00:00:00:00:02",
    "name": "flow-mod-3",
    "cookie": "0",
    "priority": "32767",
    "in_port": "1",
    "idle_timeout": "60",
    "active": "true",
    "actions": "output=2"
}
{
    "switch": "00:00:00:00:00:00:00:02",
    "name": "flow-mod-4",
    "cookie": "0",
    "priority": "32767",
    "in_port": "2",
    "idle_timeout": "60",
    "active": "true",
    "actions": "output=1"
}

第二题:提高题

第1小题:代理访问

  • 要添加forwarding模块
  • flt && sshxm && sudomn ProxyaccessTopo.py && pingall
    //ProxyaccessTopo.py
    from mininet.topo import Topo
    class MyTopo( Topo ):
        def __init__( self ):
            Topo.__init__( self )
            host1 = self.addHost('Host1')
            host2 = self.addHost('Host2')
            server1 = self.addHost('Server1')
            proxy1 = self.addHost('Proxy1')
            switch1 = self.addSwitch('Switch1')
            switch2 = self.addSwitch('Switch2')
            switch3 = self.addSwitch('Switch3')
            self.addLink( host1,switch1 )
            self.addLink( host2,switch1 )
            self.addLink( switch1,switch2)
            self.addLink( switch2,switch3)
            self.addLink( server1,switch2)
            self.addLink(proxy1,switch3)
    topos={'mytopo':(lambda:MyTopo())}
  • curl -X PUT http://localhost:8080/wm/firewall/module/enable/json
  • 在交换机上启用防火墙。由于默认情况下防火墙拒绝所有流量,只有明确的允许规则可以允许流量通过,因此目前防火墙阻隔所有数据包的通行
  • curl -X POST -d ‘{“switchid”: “00:00:00:00:00:00:00:01”}’ http://localhost:8080/wm/firewall/rules/json
  • curl -X POST -d ‘{“switchid”: “00:00:00:00:00:00:00:02”}’ http://localhost:8080/wm/firewall/rules/json
  • curl -X POST -d ‘{“switchid”: “00:00:00:00:00:00:00:03”}’ http://localhost:8080/wm/firewall/rules/json
  • 防火墙默认阻隔一切数据流量,因此需首先允许拓扑内设备间的数据交换,即添加允许规则,使流量能够在Switch1、Switch2和Switch3之间流通
  • curl -X POST -d ‘{“src-ip”: “10.0.0.1/32”, “dst-ip”: “10.0.0.3/32”, “action”:”DENY”}’ http://localhost:8080/wm/firewall/rules/json
  • 由于Host1为普通用户,因此原则上丌允许其访问代理服务器Proxy1。但是在第四步中由于允许交换机间的包交换而打开了Proxy1不Host1乊间的通路,因此必须单独指定deny劢作,阻隔Host1和Proxy1乊间的数据流劢。由于Host2(代理用户)未受影响,因此Host2仍然能够访问Proxy1

第2小题:流表管理

### CSP初赛基础知识练习题 对于准备参加CSP-J考试的学生来说,掌握计算机科学基础理论和编程技能至关重要。以下是几类适合用于备考的基础知识练习题目: #### 数据表示与运算 1. **二进制转换** - 将十进制数`97`转化为二进制形式。 - 解析不同进制之间的相互转化方法[^1]。 2. **位操作** - 设计一段程序来判断给定整数是否为偶数,仅使用按位逻辑运算符实现。 ```cpp bool isEven(int num){ return (num & 1) == 0; } ``` #### 编程语言特性理解 3. **变量作用域** - 下列代码片段执行后会打印什么? ```cpp int main(){ int a=5; { int b=a+3; cout<<b; // 输出8 } cout<<a+b; // 错误:'b'未定义 } ``` 4. **函数调用机制** - 考虑如下递归函数: ```cpp void recurse(int n){ if(n>0){ printf("%d ",n); recurse(n-1); } } ``` 当传入参数`recurse(3)`时,屏幕上的输出顺序是什么? #### 数学概念应用 5. **最大公约数计算** - 实现求两个正整数的最大公因数(GCD)的欧几里得算法。 ```cpp int gcd(int m, int n){ while(n != 0){ int temp = m % n; m = n; n = temp; } return m; } ``` 6. **排列组合问题** - 如果有三个不同的字母A,B,C,可以组成多少种长度为2的不同序列?列举所有可能的结果。 这些问题旨在帮助考生巩固对基本概念和技术细节的理解,同时也鼓励通过实际编写代码加深印象。为了更好地应对更复杂的挑战,在日常学习过程中应当注重培养解决问题的能力以及良好的编码习惯[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值