SDN期末做業

1、項目描述

負載均衡程序node

2、我的分工

  • 代碼實現:林俊辰、黃逸飛
  • 文檔收集:郭圳源
  • 視頻拍攝:葉文滔

3、代碼演示及過程描述

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的時候進行擇路。json

  • 流圖:
    cookie

  • 視頻:
    https://pan.baidu.com/s/1ggNw1WJ網絡

4、課程感想

計算機的各個領域是相互貫通的,要聯繫學習,加強自主學習能力和查閱資料能力、動手能力。app

  • 瞭解了
    什麼是 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驗證負載均衡的實現學習

相關文章
相關標籤/搜索