2019 SDN上机第6次作业

2019 SDN上机第6次作业

实验拓扑

  • 实验拓扑
    2019 SDN上机第6次作业_第1张图片
  • 要求
    使用Python脚本完成拓扑搭建,并连接ryu控制器。

python脚本创建如下

class MyTopo(Topo):

    def __init__(self):

        # initilaize topology
        Topo.__init__(self)

        # add hosts and switches
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')
        h3 = self.addHost('h3')
        h4 = self.addHost('h4')
        h5 = self.addHost('h5')
        h6 = self.addHost('h6')
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        # add links
        self.addLink(h1, s1, 1, 1)
        self.addLink(h2, s1, 1, 2)
        self.addLink(h3, s1, 1, 3)
        self.addLink(s1, s2, 4, 4)
        self.addLink(h4, s2, 1, 1)
        self.addLink(h5, s2, 1, 2)
        self.addLink(h6, s2, 1, 3)
        
topos = {'mytopo': (lambda: MyTopo())}

使用如下命令生成拓扑

sudo mn --custom a.py --topo mytopo --controller=remote,ip=127.0.0.1,port=6653 --switch ovsk,protocols=OpenFlow13

这边这个图片是第一次错误脚本的截图,对应端口是错误的

2019 SDN上机第6次作业_第2张图片

使用pingall命令测试连通性

2019 SDN上机第6次作业_第3张图片

打开控制器

ryu-manager gui_topology.py

2019 SDN上机第6次作业_第4张图片

使用Ryu的REST API下发流表实现和第2次实验同样的VLAN

按照实验二编写脚本m.sh如下

2019 SDN上机第6次作业_第5张图片

#端口号1发来数据
curl -X POST -d '{
    "dpid": 1,
    "priority":1,
    "match":{
        "in_port":1
    },
    "actions":[
        {
            "type": "PUSH_VLAN",     # s1将从主机发来的数据包打上vlan_tag
            "ethertype": 33024       # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",     # 设置VLAN ID
            "value": 4096            # 设置vlan_id的值
        },
        {
            "type": "OUTPUT",
            "port": 4
        }
    ]
}' http://127.0.0.1:8080/stats/flowentry/add

#端口号2发来数据
curl -X POST -d '{
    "dpid": 1,
    "priority":1,
    "match":{
        "in_port":2
    },
    "actions":[
        {
            "type": "PUSH_VLAN",     # s1将从主机发来的数据包打上vlan_tag
            "ethertype": 33024       # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",     # 设置VLAN ID
            "value": 4097            # 设置vlan_id的值
        },
        {
            "type": "OUTPUT",
            "port": 4
        }
    ]
}' http://127.0.0.1:8080/stats/flowentry/add

#端口号3发来数据
curl -X POST -d '{
    "dpid": 1,
    "priority":1,
    "match":{
        "in_port":3
    },
    "actions":[
        {
            "type": "PUSH_VLAN",     # s1将从主机发来的数据包打上vlan_tag
            "ethertype": 33024       # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",     # 设置VLAN ID
            "value": 4098            # 设置vlan_id的值
        },
        {
            "type": "OUTPUT",
            "port": 4
        }
    ]
}' http://127.0.0.1:8080/stats/flowentry/add



#向端口1转发
curl -X POST -d '{
    "dpid": 1,
    "priority":1,
    "match":{
        "dl_vlan": "0"
    },
    "actions":[
        {
            "type": "POP_VLAN",     # 给进入交换机的包去除 vlan_tag
        },
        {
            "type": "OUTPUT",
            "port": 1
        }
    ]
}' http://localhost:8080/stats/flowentry/add

#向端口2转发
curl -X POST -d '{
    "dpid": 1,
    "priority":1,
    "match":{
        "dl_vlan": "1"
    },
    "actions":[
        {
            "type": "POP_VLAN",     # 给进入交换机的包去除 vlan_tag
        },
        {
            "type": "OUTPUT",
            "port": 2
        }
    ]
}' http://localhost:8080/stats/flowentry/add

#向端口3转发
curl -X POST -d '{
    "dpid": 1,
    "priority":1,
    "match":{
        "dl_vlan": "2"
    },
    "actions":[
        {
            "type": "POP_VLAN",     # 给进入交换机的包去除 vlan_tag
        },
        {
            "type": "OUTPUT",
            "port": 3
        }
    ]
}' http://localhost:8080/stats/flowentry/add


#端口号1发来数据
curl -X POST -d '{
    "dpid": 2,
    "priority":1,
    "match":{
        "in_port":1
    },
    "actions":[
        {
            "type": "PUSH_VLAN",     # s2将从主机发来的数据包打上vlan_tag
            "ethertype": 33024       # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",     # 设置VLAN ID
            "value": 4096            # 设置vlan_id的值
        },
        {
            "type": "OUTPUT",
            "port": 4
        }
    ]
}' http://127.0.0.1:8080/stats/flowentry/add

#端口号2发来数据
curl -X POST -d '{
    "dpid": 2,
    "priority":1,
    "match":{
        "in_port":2
    },
    "actions":[
        {
            "type": "PUSH_VLAN",     # s2将从主机发来的数据包打上vlan_tag
            "ethertype": 33024       # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",     # 设置VLAN ID
            "value": 4097            # 设置vlan_id的值
        },
        {
            "type": "OUTPUT",
            "port": 4
        }
    ]
}' http://127.0.0.1:8080/stats/flowentry/add

#端口号3发来数据
curl -X POST -d '{
    "dpid": 2,
    "priority":1,
    "match":{
        "in_port":3
    },
    "actions":[
        {
            "type": "PUSH_VLAN",     # s2将从主机发来的数据包打上vlan_tag
            "ethertype": 33024       # 帧类型0x8100(=33024): 表示IEEE 802.1Q的VLAN数据帧
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",     # 设置VLAN ID
            "value": 4098            # 设置vlan_id的值
        },
        {
            "type": "OUTPUT",
            "port": 4
        }
    ]
}' http://127.0.0.1:8080/stats/flowentry/add



#向端口1转发
curl -X POST -d '{
    "dpid": 2,
    "priority":1,
    "match":{
        "dl_vlan": "0"
    },
    "actions":[
        {
            "type": "POP_VLAN",     # 给进入交换机的包去除 vlan_tag
        },
        {
            "type": "OUTPUT",
            "port": 1
        }
    ]
}' http://localhost:8080/stats/flowentry/add

#向端口2转发
curl -X POST -d '{
    "dpid": 2,
    "priority":1,
    "match":{
        "dl_vlan": "1"
    },
    "actions":[
        {
            "type": "POP_VLAN",     # 给进入交换机的包去除 vlan_tag
        },
        {
            "type": "OUTPUT",
            "port": 2
        }
    ]
}' http://localhost:8080/stats/flowentry/add

#向端口3转发
curl -X POST -d '{
    "dpid": 2,
    "priority":1,
    "match":{
        "dl_vlan": "2"
    },
    "actions":[
        {
            "type": "POP_VLAN",     # 给进入交换机的包去除 vlan_tag
        },
        {
            "type": "OUTPUT",
            "port": 3
        }
    ]
}' http://localhost:8080/stats/flowentry/add

注意,最后地址前的'必须要有一个空格!

./m.sh

完成所有下发流表后,使用命令查看s1, s2流表

sudo ovs-ofctl -O OpenFlow13 dump-flows s1
sudo ovs-ofctl -O OpenFlow13 dump-flows s2

2019 SDN上机第6次作业_第6张图片

最后使用pingall命令查看其连通性

2019 SDN上机第6次作业_第7张图片

对比两种方法,写出你的实验体会

一开始使用的创建拓扑的py脚本是错误的,h1对应的是s1端口2,h2对应的是s1的端口3,就是从端口2开始,而不是从端口1开始,而shell命令写的又是从端口1开始,就导致实行完shell命令,修改了流表但是pingall却一直显示XXXXX,相互之间都是ping不到的。发现错误之后,修改脚本代码,再次执行就正常了。

对比于第二次实验的方法,显然这一次的方法实用的多。编写shell命令脚本,可以实现一次性完成任务。

你可能感兴趣的:(2019 SDN上机第6次作业)