# SDN期末作业

SDN期末作业

SDN期末作业
一、项目描述
负载均衡程序
二、个人分工
代码实现:林俊辰、黄逸飞
文档收集:郭圳源
视频拍摄:叶文滔
三、代码演示及过程描述

from mininet.topo import Topo
 
class MyTopo( Topo ):
    "Simple topology example."
 
    def __init__( self ):
 
        # Initialize topology
        Topo.__init__( self )
        
        sw1 = self.addSwitch('s1')
        sw2 = self.addSwitch('s2')
        sw3 = self.addSwitch('s3')
        sw4 = self.addSwitch('s4')

        h1 = self.addHost('h1')
        h2 = self.addHost('h2')
        h3 = self.addHost('h3')
        h4 = self.addHost('h4')

        self.addLink(h1,sw1,1)
        self.addLink(sw1,sw2,2,1)
        self.addLink(sw1,sw4,3,2)
        self.addLink(sw1,sw3,4,1)
        self.addLink(sw2,sw4,2,1)
        self.addLink(sw3,sw4,2,3)
        self.addLink(sw4,h2,4)
        self.addLink(sw4,h3,5)
        self.addLink(sw4,h4,6)

topos = { 'mytopo': ( lambda: MyTopo() ) }
//拓扑


import httplib2
import time

class OdlUtil:
    url = ''
    def __init__(self, host, port):
        self.url = 'http://' + host + ':' + str(port)
    def install_flow(self, container_name='default',username="admin", password="admin"):
        http = httplib2.Http()  
        http.add_credentials(username, password)
        headers = {'Accept': 'application/json'}    
        flow_name = 'flow_' + str(int(time.time()*1000))
        h1h4body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'   
                '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "4"},"order": "0"}]}}]},'
                '"priority": "101","cookie": "1","table_id": "0"}]}'    
        mh1h4body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "4"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "1","table_id": "0"}]}'
    h1h4body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'   
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "2"},"order": "0"}]}}]},'
                '"priority": "100","cookie": "5","table_id": "0"}]}'
    mh1h4body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "2"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "5","table_id": "0"}]}'
    h1h4body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'           
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "3"},"order": "0"}]}}]},'
                '"priority": "99","cookie": "6","table_id": "0"}]}'
    mh1h4body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "3"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "6","table_id": "0"}]}'        
        h1h2body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'   
                '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "4"},"order": "0"}]}}]},'
                '"priority": "101","cookie": "1","table_id": "0"}]}'    
        mh1h2body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "4"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "1","table_id": "0"}]}'
    h1h2body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'   
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "2"},"order": "0"}]}}]},'
                '"priority": "100","cookie": "5","table_id": "0"}]}'
    mh1h2body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "2"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "5","table_id": "0"}]}'
    h1h2body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'           
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "3"},"order": "0"}]}}]},'
                '"priority": "99","cookie": "6","table_id": "0"}]}'
    mh1h2body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "3"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "6","table_id": "0"}]}'
        h1h3body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'   
                '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "4"},"order": "0"}]}}]},'
                '"priority": "101","cookie": "1","table_id": "0"}]}'    
        mh1h3body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "4"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "1","table_id": "0"}]}'
    h1h3body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'   
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "2"},"order": "0"}]}}]},'
                '"priority": "100","cookie": "5","table_id": "0"}]}'
    mh1h3body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "2"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "5","table_id": "0"}]}'
    h1h3body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'           
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "3"},"order": "0"}]}}]},'
                '"priority": "99","cookie": "6","table_id": "0"}]}'
    mh1h3body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'  
               '{"ethernet-type": {"type": "2048"}},'
    '"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
                '"instructions": {"instruction": [{"order": "0",'
                '"apply-actions": {"action": [{"output-action": {'
                '"output-node-connector": "3"},"order": "0"}]}}]},'
                '"priority": "102","cookie": "6","table_id": "0"}]}'
        headers = {'Content-type': 'application/json'}
    num=0
    while num < 4 :
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=mh1h4body1, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=mh1h3body1, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=mh1h2body1, method='PUT',headers=headers)

 

        time.sleep(0.1)

            response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=h1h4body1, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=mh1h4body2, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=h1h3body1, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=mh1h3body2, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=h1h2body1, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=mh1h2body2, method='PUT',headers=headers)



        time.sleep(0.1)

            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=h1h4body2, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=mh1h4body3, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=h1h3body2, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=mh1h3body3, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=h1h2body2, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=mh1h2body3, method='PUT',headers=headers)


        time.sleep(0.1)

            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=h1h4body3, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=h1h3body3, method='PUT',headers=headers)
            response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=h1h2body3, method='PUT',headers=headers)


        print(content.decode())

odl = OdlUtil('127.0.0.1', '8181')
odl.install_flow()

  • 代码大概思路:

主机h1发往主机h2h3h4,只在途中经过交换机123的时候进行择路。

四、课程感想
计算机的各个领域是相互贯通的,要联系学习,增强自主学习能力和查阅资料能力、动手能力。
了解了
什么是 SDN ,SDN 的基本思想、特点
有哪些控制器,控制层和数据层的通信标准协议
分布式控制器的几种扩展方式
ODL设计的三个层次
SDN 的应用领域
openflow多级流表机制的优点
ryu控制器
onos控制器
opendaylight控制器
接触了
SDNLAB 等几个优质的学习网站
网络相关知识
学会了
安装mininet
用字符命令生成拓扑并测试连通性
用可视化界面生成拓扑并测试连通性
用Python脚本生成Fat-tree型的拓扑
安装floodlight
生成拓扑并连接控制器floodlight,利用控制器floodlight查看图形拓扑
利用字符界面下发流表,通过测试联通性,来验证openflow的hardtime机制
利用OVS命令下发流表,实现VLAN功能
利用OVS命令查看流表
Wireshark抓包验证
借助Postman通过ODL的北向接口下发流表,再利用ODL北向接口查看已下发的流表
利用ODL下发组表、流表,实现建议负载均衡
利用Wireshark验证负载均衡的实现
学会了p4语言

posted @ 2018-01-24 19:21  yksrz  阅读(371)  评论(0编辑  收藏  举报