Featured image of post 在Pica8 SDN交换机上配置流表

在Pica8 SDN交换机上配置流表

使用Ryu控制器连接在Pica8 SDN交换机上配置流表,或直接通过OVS命令在Pica8 SDN交换机上配置流表

部署环境

创建三个虚拟交换机:

  • br0:物理端口1、2、3、4
  • br1:物理端口9、10、11、12
  • br2:物理端口13、14、15、16
  • br3:物理端口21、22、23、24

部署两台主机:

  • 主机1:IP地址为192.168.100.100
  • 主机2:IP地址为192.168.100.200

链路连接情况:

  • br0端口4 <~> br1端口9
  • br1端口12 <~> br2端口13
  • br2端口16 <~> br3端口21
  • 主机1连接至br0的端口3
  • 主机2连接至br3的端口23

方式一:Ryu控制器

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types

class SimpleSwitch13(app_manager.RyuApp):
    OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]

    def __init__(self, *args, **kwargs):
        super(SimpleSwitch13, self).__init__(*args, **kwargs)

    # 处理交换机连接
    @set_ev_cls(ofp_event.EventOFPStateChange, MAIN_DISPATCHER)
    def switch_features_handler(self, ev):
        datapath = ev.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        # 交换机新连接
        if ev.state == MAIN_DISPATCHER:
            # 断言dpid存在
            assert datapath.id is not None
            print("Switch Coming", datapath.id)
            # 为交换机的流表新增Table-miss Flow Entry项 用于在无法匹配到流表项后与之匹配
            # match表示匹配约束 为空则说明可以匹配所有封包
            # action表示匹配成功后执行的操作为向Controller端口发送最大数据长度的封包
            # OFPCML_NO_BUFFER表示不需要在交换机上缓存封包将封包整体发给controller
            # 优先级为0 即最低的优先权
            match = parser.OFPMatch()
            actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
                                              ofproto.OFPCML_NO_BUFFER)]
            self.add_flow(datapath, 0, match, actions)
            # 预下发静态路由
            self.download_routing(datapath)

    # 预下发静态路由表
    def download_routing(self, datapath):
        dpid = datapath.id
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        if dpid == 1:
            # [100-1] 从3端口输入的流量 从4端口输出
            in_port, out_port = 3, 4
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)
            # [100-2] 从4端口输入的流量 从3端口输出
            in_port, out_port = 4, 3
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)
        if dpid == 2:
            # [200-1] 从9端口输入的流量 从12端口输出
            in_port, out_port = 9, 12
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)
            # [200-2] 从12端口输入的流量 从9端口输出
            in_port, out_port = 12, 9
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)
        if dpid == 3:
            # [300-1] 从13端口输入的流量 从16端口输出
            in_port, out_port = 13, 16
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)
            # [300-2] 从16端口输入的流量 从13端口输出
            in_port, out_port = 16, 13
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)
        if dpid == 4:
            # [400-1] 从21端口输入的流量 从23端口输出
            in_port, out_port = 21, 23
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)
            # [400-2] 从23端口输入的流量 从21端口输出
            in_port, out_port = 23, 21
            match = parser.OFPMatch(in_port=in_port)
            actions = [parser.OFPActionOutput(out_port)]
            self.add_flow(datapath, 1, match, actions)

    # 用于添加流表项的工具函数
    def add_flow(self, datapath, priority, match, actions, buffer_id=None):
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                             actions)]
        if buffer_id:
            mod = parser.OFPFlowMod(datapath=datapath,
                                    buffer_id=buffer_id,
                                    priority=priority,
                                    match=match,
                                    instructions=inst)
        else:
            mod = parser.OFPFlowMod(datapath=datapath,
                                    priority=priority,
                                    match=match,
                                    instructions=inst)
        datapath.send_msg(mod)

    # 控制器接收到Packet-In消息
    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
    def packet_in_handler(self, ev):
        msg = ev.msg
        datapath = msg.datapath
        in_port = msg.match['in_port']
        print("Recv PACKET_IN", datapath.id,"from",in_port)

        pkt = packet.Packet(msg.data)
        eth = pkt.get_protocols(ethernet.ethernet)[0]
        print("Eth pkt: {0}".format(eth))

使用Iperf进行速率测试,由于主机2使用的外置网卡速率为百兆,导致最终测速被限制在100Mbps

方式二:OVS命令

PICOS Documentation

1
2
3
4
5
6
7
8
ovs-ofctl add-flow br0 in_port=3,actions=output:4
ovs-ofctl add-flow br0 in_port=4,actions=output:3
ovs-ofctl add-flow br1 in_port=9,actions=output:12
ovs-ofctl add-flow br1 in_port=12,actions=output:9
ovs-ofctl add-flow br2 in_port=13,actions=output:16
ovs-ofctl add-flow br2 in_port=16,actions=output:13
ovs-ofctl add-flow br3 in_port=21,actions=output:23
ovs-ofctl add-flow br3 in_port=23,actions=output:21
1
2
3
4
ovs-ofctl dump-flows br0
ovs-ofctl dump-flows br1
ovs-ofctl dump-flows br2
ovs-ofctl dump-flows br3

Licensed under CC BY-NC-SA 4.0
皖ICP备2025083746号-1
公安备案 陕公网安备61019002003315号



使用 Hugo 构建
主题 StackJimmy 设计