SDN第七次上机作业

SDN第七次上机作业

任务一

编写Python程序,调用OpenDaylight的北向接口下发指令删除s1上的流表数据。

#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth

if __name__ == "__main__":
    url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/'
    headers = {'Content-Type': 'application/json'}
    res = requests.delete(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
    print (res.content)

编写Python程序,调用OpenDaylight的北向接口下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。

#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
def http_put(url,jstr):
	url= url
	headers = {'Content-Type':'application/json'}
	resp = requests.put(url,jstr,headers=headers,auth=HTTPBasicAuth('admin', 'admin'))
	return resp
if __name__ == "__main__":
	url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/'
	with open('test1.json') as f:
		jstr = f.read()
	resp = http_put(url,jstr)
	print (resp.content)

很明显3-24s断了

编写Python程序,调用OpenDaylight的北向接口获取s1上活动的流表数。

#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth

if __name__ == "__main__":
    url = 'http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/opendaylight-flow-table-statistics:flow-table-statistics'
    headers = {'Content-Type': 'application/json'}
    res = requests.get(url,headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
    print (res.content)


任务二

编写Python程序,调用Ryu的北向接口,实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。

python

#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
def http_post(url,jstr):
    url= url
    headers = {'Content-Type':'application/json'}
    resp = requests.post(url,jstr,headers=headers)
    return resp
if __name__ == "__main__":
    url='http://127.0.0.1:8080/stats/flowentry/add'
    requests.adapters.DEFAULT_RETRIES = 5
    with open('1.json') as f:
        jstr = f.read()
    resp = http_post(url,jstr)
    print (resp.content)
    s = requests.session()
    s.keep_alive = False

json

{
  "dpid": 1,
  "match": {
    "in-port": "1",
    "dl_dst": "00:00:00:00:00:03"
  },
  "priority": 65535,
  "hard_timeout": 20,
  "cookie": "2",
  "table_id": 0,
  "actions": []
}

利用Mininet平台搭建下图所示网络拓扑,要求支持OpenFlow 1.3协议,主机名、交换机名以及端口对应正确。拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务

整理一个Shell脚本,参考Ryu REST API的文档,利用curl命令,实现和实验2相同的VLAN

curl -X POST -d '{
    "dpid": 1,
    "priority": 1,
    "match":{
        "in_port": 1
    },
    "actions":[
        {
            "type": "PUSH_VLAN",
            "ethertype": 33024
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",
            "value": 4096
        },
        {
            "type": "OUTPUT",
            "port": 3
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

 curl -X POST -d '{
    "dpid": 1,
    "priority": 1,
    "match":{
        "in_port": 2
    },
    "actions":[
        {
            "type": "PUSH_VLAN", 
            "ethertype": 33024 
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",
            "value": 4097
        },
        {
            "type": "OUTPUT",
            "port": 3
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

 curl -X POST -d '{
    "dpid": 1,
    "priority": 1,
    "match":{
        "vlan_vid": 0
    },
    "actions":[
        {
            "type": "POP_VLAN",
            "ethertype": 33024
        },
        {
            "type": "OUTPUT",
            "port": 1
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

 curl -X POST -d '{
    "dpid": 1,
    "priority": 1,
    "match":{
        "vlan_vid": 1
    },
    "actions":[
        {
            "type": "POP_VLAN",
            "ethertype": 33024
        },
        {
            "type": "OUTPUT",
            "port": 2
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

 curl -X POST -d '{
    "dpid": 2,
    "priority": 1,
    "match":{
        "in_port": 1
    },
    "actions":[
        {
            "type": "PUSH_VLAN",
            "ethertype": 33024 
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid", 
            "value": 4096 
        },
        {
            "type": "OUTPUT",
            "port": 3
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

 curl -X POST -d '{
    "dpid": 2,
    "priority": 1,
    "match":{
        "in_port": 2
    },
    "actions":[
        {
            "type": "PUSH_VLAN", 
            "ethertype": 33024
        },
        {
            "type": "SET_FIELD",
            "field": "vlan_vid",
            "value": 4097 
        },
        {
            "type": "OUTPUT",
            "port": 3
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

 curl -X POST -d '{
    "dpid": 2,
    "priority": 1,
    "match":{
        "vlan_vid": 0
    },
    "actions":[
        {
            "type": "POP_VLAN",
            "ethertype": 33024 
        },
        {
            "type": "OUTPUT",
            "port": 1
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

 curl -X POST -d '{
    "dpid": 2,
    "priority": 1,
    "match":{
        "vlan_vid": 1
    },
    "actions":[
        {
            "type": "POP_VLAN", 
            "ethertype": 33024 
        },
        {
            "type": "OUTPUT",
            "port": 2
        }
    ]
 }' http://localhost:8080/stats/flowentry/add

进阶任务

#!/usr/bin/python
import json
import requests

if __name__ == "__main__":
    url = 'http://127.0.0.1:8080/stats/flowentry/add'
    headers = {'Content-Type': 'application/json'}
    flow1 = {
        "dpid": 1,
        "priority": 1,
        "match":{
            "in_port": 1
        },
        "actions":[
            {
                "type": "PUSH_VLAN",
                "ethertype": 33024
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid", 
                "value": 4096 
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow2 = {
        "dpid": 1,
        "priority": 1,
        "match":{
            "in_port": 2
        },
        "actions":[
            {
                "type": "PUSH_VLAN",
                "ethertype": 33024 
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid", 
                "value": 4097 
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow3 = {
        "dpid": 1,
        "priority": 1,
        "match":{
            "vlan_vid": 0
        },
        "actions":[
            {
                "type": "POP_VLAN",
                "ethertype": 33024
            },
            {
                "type": "OUTPUT",
                "port": 1
            }
        ]
    }
    flow4 = {
        "dpid": 1,
        "priority": 1,
        "match": {
            "vlan_vid": 1
        },
        "actions": [
            {
                "type": "POP_VLAN",  # Push a new VLAN tag if a input frame is non-VLAN-tagged
                "ethertype": 33024  # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
            },
            {
                "type": "OUTPUT",
                "port": 2
            }
        ]
    }
    flow5 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "in_port": 1
        },
        "actions": [
            {
                "type": "PUSH_VLAN",  # Push a new VLAN tag if a input frame is non-VLAN-tagged
                "ethertype": 33024  # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid",  # Set VLAN ID
                "value": 4096  # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096)
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow6 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "in_port": 2
        },
        "actions": [
            {
                "type": "PUSH_VLAN",  # Push a new VLAN tag if a input frame is non-VLAN-tagged
                "ethertype": 33024  # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid",  # Set VLAN ID
                "value": 4097  # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096)
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow7 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "vlan_vid": 0
        },
        "actions": [
            {
                "type": "POP_VLAN",  # Push a new VLAN tag if a input frame is non-VLAN-tagged
                "ethertype": 33024  # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
            },
            {
                "type": "OUTPUT",
                "port": 1
            }
        ]
    }
    flow8 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "vlan_vid": 0
        },
        "actions": [
            {
                "type": "POP_VLAN",  # Push a new VLAN tag if a input frame is non-VLAN-tagged
                "ethertype": 33024  # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
            },
            {
                "type": "OUTPUT",
                "port": 1
            }
        ]
    }
    res1 = requests.post(url, json=flow1)
    res2 = requests.post(url, json=flow2)
    res3 = requests.post(url, json=flow3)
    res4 = requests.post(url, json=flow4)
    res5 = requests.post(url, json=flow5)
    res6 = requests.post(url, json=flow6)
    res7 = requests.post(url, json=flow7)
    res8 = requests.post(url, json=flow8)

个人总结

这次实验一开始差点把我心态搞崩,每次使用python命令就会出一大堆的error,幸好在同学的帮助下解决了,这种报错其实就是代码里面缩进和连接符错误导致的,运行成功的代码就会显示b''。在解决了这个问题之后其实都就能很快做完ODL部分。ryu部分每次执行python命令,都会报错Traceback (most recent call last):等一长串莫名其妙的问题,这边要在连接ryu的时候使用ryu-manager ryu.app.simple_switch_13 ryu.app.ofctl_rest ,这样就可以解决问题。细节,细节很重要。虚拟机神一下鬼一下的,属于是很搞人心态了。进阶部分参考了大佬的方法,包括解决那个Traceback (most recent call last):的问题也是在他那边找到的答案,https://www.cnblogs.com/ExcaliburMorgan/p/15426574.html

posted @ 2021-10-24 15:33  Katra丶  阅读(93)  评论(0编辑  收藏  举报