23-Openwrt switch vlan配置

路由器上面基本都是用switch芯片來實現(xiàn)lan/wan的劃分,但是有的switch只有一個GMAC,有的有多個GMAC,對于內(nèi)核驅(qū)動設(shè)備來說就是有的switch生成的只有eth0,有的switch生成的有eth0和eth1。

1、基本概念

1.1、wan lan
  • LAN:Local Area Network 的英文簡稱,即局域網(wǎng)
    我們計算機和路由連在一起就是接這個口
  • WAN: Wide Area Network 的英文簡稱,即廣域網(wǎng)
    運營商拉進來的網(wǎng)線就是和這個口連在一起
  • VLAN( Virtual Local Area Network)的中文名為"虛擬局域網(wǎng)"
    VLAN通常是在局域網(wǎng)中邏輯地再劃分為幾個網(wǎng)段來構(gòu)成VLAN。一個局域網(wǎng)中可以劃分為N多個VLAN,使VLAN之間不可互相通信(通常這是為了安全起見)。LAN和WAN都屬于VLAN
  • VLAN ID:每個VLAN都有一個ID,范圍為0-4095之間,但是0和4095僅僅限于系統(tǒng)使用,用戶不能查看和使用。所以我們可以使用的范圍為1-4994
  • ACCESS口:在OpenWRT里面為未標(biāo)記,只屬于一個VLAN通過
  • TRUNK口:在OpenWRT里面為已標(biāo)記,允許多個VLAN通過
  • ethX.X:在大部分Linux發(fā)行版中,第一個X為實際的物理網(wǎng)卡ID,第二個X為VLAN ID
1.2、phy switch
  • MAC(介質(zhì)訪問控制),可以理解成數(shù)據(jù)鏈路層即可
  • PHY(物理性),簡單理解層轉(zhuǎn)成物理層的連接組件即可
  • 控制接口(MDC/DMIO,I2C,SPI),數(shù)據(jù)接口(RGMII / GMII/MII)
  • 網(wǎng)卡: 可以理解成phy 和mac 組成的一個芯片,直接可以通過各種接口和cpu對接
  • PHY: 單純的物理層芯片,通常是和SOC 或是MCU對接,部分soc和mcu 都會集成mac
  • switch : 多網(wǎng)口設(shè)備,內(nèi)部結(jié)構(gòu)就是mac+phy,主要功能是將數(shù)據(jù)在不同端口之間轉(zhuǎn)發(fā)。也會留有數(shù)據(jù)接口以便和SOC 等設(shè)備對接。

嵌入式設(shè)備的switch 以及PHY 芯片調(diào)試和選型 :https://blog.csdn.net/noheike/article/details/105037362

2、 openwrt官方配置swconfig

openwrt官方使用的是swconfig工具,它屬于package下面的一個包
https://oldwiki.archive.openwrt.org/doc/techref/swconfig

  • 可以使用swconfig命令來查看一些網(wǎng)卡的信息,如swconfig list 、swconfig dev eth0 show等。

swconfig 結(jié)構(gòu)框架是應(yīng)用層與內(nèi)核驅(qū)動通信的一種框架,主要實通過應(yīng)用層命令去配置交換機芯片的底層驅(qū)動,應(yīng)用層與內(nèi)核層采用netlink通信機制.
首先看到

  • package/network/config/swconfig/cli.c文件,跳到main()函數(shù),里面主要做了檢查參數(shù),根據(jù)傳進來的參數(shù)操作底層驅(qū)動。
    然后看到
  • target/linux/generic/files/drivers/net/phy/swconfig.c
  • target/linux/generic/files/include/linux/switch.h

這是內(nèi)核層的代碼,因為交換機芯片種類繁多,需要有統(tǒng)一接口去兼容所有交換機驅(qū)動接口,swconfig.c就是一套定義接口。應(yīng)用層的命令首先會跳到swconig.c去選擇底層驅(qū)動函數(shù)。
交換機芯片驅(qū)動位置

  • target/linux/generic/files/drivers/net/phy/
    交換機是總線設(shè)備驅(qū)動類型的,在swconfig.c已經(jīng)注冊了交換機設(shè)備register_switch,在驅(qū)動里面要注冊交換機驅(qū)動,然后匹配設(shè)備和驅(qū)動,調(diào)用probe.

所以使用應(yīng)用層swconfig的package包時,內(nèi)核也需要有相應(yīng)的CONFIG_SWCONFIG=y配置開啟。

3、network啟動配置switch過程

/etc/init.d/network start的時候會調(diào)用setup_switch函數(shù),該函數(shù)位于/lib/network/switch.sh中

init_switch() {
    setup_switch() { return 0; }

    include /lib/network
    setup_switch
}

start_service() {
    init_switch

    procd_open_instance
    procd_set_param command /sbin/netifd
    procd_set_param respawn
    procd_set_param watch network.interface
    [ -e /proc/sys/kernel/core_pattern ] && {
        procd_set_param limits core="unlimited"
    }   
    procd_close_instance
}

/lib/network/switch.sh的代碼如下,所以最終是調(diào)用swconfig來配置switch參數(shù)

#!/bin/sh
# Copyright (C) 2009 OpenWrt.org

setup_switch_dev() {
    local name
    config_get name "$1" name
    name="${name:-$1}"
    [ -d "/sys/class/net/$name" ] && ip link set dev "$name" up
    swconfig dev "$name" load network
}

setup_switch() {
    config_load network
    config_foreach setup_switch_dev switch
}

4、/etc/config/network配置vlan

4.1、實例1:switch只有一個eth0口

這邊配置的name為switch0是驅(qū)動查出來的

root@OpenWrt:/# swconfig list
Found: switch0 - rt305x

如下配置信息,lan配置為eth0.1則下面的vlan1為lan口的信息,wan配置為eth0.2則下面的vlan2為wan口的信息。

root@OpenWrt:/# cat /etc/config/network

config interface 'lan'  //配置LAN口
    option type 'bridge' //橋接方式
    option ifname 'eth0.1' // 代表vlan1,這個很重要,下面配置會用到
    option proto 'static' //靜態(tài)IP

config device 'lan_dev' //配置LAN硬件信息
       option macaddr           //設(shè)置MAC地址

config interface 'wan'  //配置WAN口
        option ifname 'eth0.2' // 代表vlan2,這個很重要,下面配置會用到 
        option type 'dhcp' //dhcp方式

config switch  
  //switch中文意思就開關(guān),所以下面就是使能vlan口
        option name 'switch0'
        option reset '1'
        option enable_vlan  '1'    // 1表示開啟vlan口

config switch_vlan 
    option name 'switch0'
    option vlan '1'  //VLAN1, 和上面的option ifname 'eth0.1'相匹配,所以是配置LAN口
    option ports '0 1 2 3 6t'   //0~3都是LAN口,RT5350有5個端口

config switch_vlan
    option name 'switch0'
    option vlan '2'  //VLAN2, 和上面的option ifname 'eth0.2'相匹配,所以是配置WAN口
    option ports '4 6t'   //4是WAN口

swconfig dev switch0 show可以查看具體信息

swconfig其他命令可以查看官網(wǎng):http://wiki.openwrt.org/doc/techref/swconfig
或者使用swconfig dev switch0 help命令就可以列出全部支持的命令

root@OpenWrt:/# swconfig dev switch0 show
Global attributes:
        enable_vlan: 1
        alternate_vlan_disable: 0
        bc_storm_protect: 0
        led_frequency: 0
Port 0:
        disable: 0
        doubletag: 0
        untag: 1
        led: 5
        lan: 0
        recv_bad: 0
        recv_good: 587
        tr_bad: 0
        tr_good: 246
        pvid: 2
        link: port:0 link:up speed:100baseT full-duplex 
Port 1:
        ...
        pvid: 1
        link: port:1 link:down
Port 2:
        ...
        pvid: 1
        link: port:2 link:down
Port 3:
        ...
        pvid: 1
        link: port:3 link:up speed:100baseT full-duplex 
Port 4:
        ...
        pvid: 1
        link: port:4 link:down
Port 5:
        ...
        tr_good: 0
        pvid: 0
        link: port:5 link:down
Port 6:
        ...
        pvid: 0
        link: port:6 link:up speed:1000baseT full-duplex 
VLAN 1:
        ports: 1 2 3 4 6t 
VLAN 2:
        ports: 0 6t 
image.png

圖片來自官網(wǎng):https://oldwiki.archive.openwrt.org/doc/uci/network/switch

https://oldwiki.archive.openwrt.org/doc/uci/network

  • eth0是一塊物理網(wǎng)卡,如MT7688就一個switch芯片,這個switch芯片可以實現(xiàn)5個Port口,所以我們可以通過vlan技術(shù),虛擬成多種端口
  • eth0.1 eth0.2都是從此設(shè)備上虛擬出來的。
  • eth0.1 是vlan1分出的lan口.
  • eth0.2 是vlan分出的wan口。
  • br-lan 虛擬設(shè)備,用于LAN口設(shè)備橋接.

br-lan = eth0.1 + rai0 + ra0,即將有線LAN口和無線網(wǎng)統(tǒng)一劃分為 LAN,便于管理,可以用brctl show查看使用情況。

root@Openwrt:/# brctl show
bridge name     bridge id               STP enabled     interfaces
br-lan          7fff.008811225577       no              eth0.1
                                                        ra0
                                                        ra1
4.2、實例2:switch有兩個eth0、eth1口

如mt7531就是雙GMAC的switch,datasheet上面也有標(biāo)注

1-port SGMII MAC(P6), and -1-port RGMII/SGMII MAC(P5)
image.png

這就相當(dāng)于P6會生成eth0給lan口使用,P5會生成eth1給wan口使用

root@OpenWrt:/# swconfig list
Found: switch0 - mt763x

如下配置信息,lan配置為eth0則下面的vlan1為lan口的信息,wan配置為eth01則下面的vlan2為wan口的信息。

root@OpenWrt:/# cat /etc/config/network
config interface 'lan'
    option type 'bridge'
    option ifname 'eth0'
    option proto 'static'
    option ipaddr '192.168.1.1'
    option netmask '255.255.255.0'
    option ip6assign '60'

config interface 'wan'
    option ifname 'eth1'
    option proto 'dhcp'

config switch
    option name 'switch0'
    option reset '1'
    option enable_vlan '1'

config switch_vlan
    option device 'switch0'
    option vlan '1'
    option ports '1 2 3 4 6'

config switch_vlan
    option device 'switch0'
    option vlan '2'
    option ports '0 5'
root@Openwrt:~# swconfig dev switch0 show
Global attributes:
        enable_vlan: 1
Port 0:
        mib: Port 0 MIB counters
TxDrop     : 0
TxCRC      : 0
TxUni      : 236162
TxMulti    : 0
TxBroad    : 224
TxCollision: 0
TxSingleCol: 0
TxMultiCol : 0
TxDefer    : 0
TxLateCol  : 0
TxExcCol   : 0
TxPause    : 0
Tx64Byte   : 56862
Tx65Byte   : 120468
Tx128Byte  : 24152
Tx256Byte  : 9256
Tx512Byte  : 5214
Tx1024Byte : 20434
TxByte     : 54175070
RxDrop     : 0
RxFiltered : 28
RxUni      : 1341775
RxMulti    : 4705
RxBroad    : 47850
RxAlignErr : 0
RxCRC      : 0
RxUnderSize: 0
RxFragment : 0
RxOverSize : 0
RxJabber   : 0
RxPause    : 2
Rx64Byte   : 244418
Rx65Byte   : 741681
Rx128Byte  : 134996
Rx256Byte  : 80427
Rx512Byte  : 71055
Rx1024Byte : 121755
RxByte     : 351255018
RxCtrlDrop : 0
RxIngDrop  : 0
RxARLDrop  : 0

        pvid: 2
        link: port:0 link:up speed:1000baseT full-duplex 
Port 1:
        mib: Port 1 MIB counters
...

        pvid: 1
        link: port:1 link:down
Port 2:
        mib: Port 2 MIB counters
...

        pvid: 1
        link: port:2 link:up speed:1000baseT full-duplex 
Port 3:
        mib: Port 3 MIB counters
...

        pvid: 1
        link: port:3 link:up speed:1000baseT full-duplex 
Port 4:
        mib: Port 4 MIB counters
...

        pvid: 1
        link: port:4 link:up speed:1000baseT full-duplex 
Port 5:
        mib: Port 5 MIB counters
...

        pvid: 2
        link: port:5 link:up speed:1000baseT full-duplex 
Port 6:
        mib: Port 6 MIB counters
...
        pvid: 1
        link: port:6 link:up speed:1000baseT full-duplex 
VLAN 1:
        vid: 1
        ports: 1 2 3 4 6 
VLAN 2:
        vid: 2
        ports: 0 5 

5、 mtk提供的switch命令

mtk提供了一個switch的應(yīng)用層package,可以直接配置switch的vlan,寄存器等信息,如下:

switch
Usage:
 switch acl etype add [ethtype] [portmap]              - drop etherytype packets
 switch acl dip add [dip] [portmap]                    - drop dip packets
 switch acl dip meter [dip] [portmap][meter:kbps]      - rate limit dip packets
 switch acl dip trtcm [dip] [portmap][CIR:kbps][CBS][PIR][PBS] - TrTCM dip packets
 switch acl port add [sport] [portmap]           - drop src port packets
 switch acl L4 add [2byes] [portmap]             - drop L4 packets with 2bytes payload
 switch add [mac] [portmap]                  - add an entry to switch table
 switch add [mac] [portmap] [vlan id]        - add an entry to switch table
 switch add [mac] [portmap] [vlan id] [age]  - add an entry to switch table
 switch clear                                - clear switch table
 switch del [mac]                            - delete an entry from switch table
 switch del [mac] [fid]                  - delete an entry from switch table
 switch search [mac] [vlan id]           - search an entry with specific mac and vlan id
 switch dip add [dip] [portmap]                  - add a dip entry to switch table
 switch dip del [dip]                        - del a dip entry to switch table
 switch dip dump                                 - dump switch dip table
 switch dip clear                                - clear switch dip table
 switch dump            - dump switch table
 switch ingress-rate on [port] [Kbps]        - set ingress rate limit on port 0~4
 switch egress-rate on [port] [Kbps]         - set egress rate limit on port 0~4
 switch ingress-rate off [port]              - del ingress rate limit on port 0~4
 switch egress-rate off [port]               - del egress rate limit on port 0~4
 switch filt [mac]                           - add a SA filtering entry (with portmap 1111111) to switch table
 switch filt [mac] [portmap]                 - add a SA filtering entry to switch table
 switch filt [mac] [portmap] [vlan id]       - add a SA filtering entry to switch table
 switch filt [mac] [portmap] [vlan id] [age] - add a SA filtering entry to switch table
 switch igmpsnoop on [Query Interval] [default router portmap] - turn on IGMP snoop and  router port learning (Query Interval 1~255)
 switch igmpsnoop off                                  - turn off IGMP snoop and router port learning
 switch igmpsnoop enable [port#]                       - enable IGMP HW leave/join/Squery/Gquery
 switch igmpsnoop disable [port#]                      - disable IGMP HW leave/join/Squery/Gquery
 switch mymac [mac] [portmap]                  - add a mymac entry to switch table
 switch mirror monitor [portnumber]            - enable port mirror and indicate monitor port number
 switch mirror target [portnumber] [0:off, 1:rx, 2:tx, 3:all]  - set port mirror target
 switch phy                                      - dump all phy registers
 switch phy [phy_addr]                   - dump phy register of specific port
 switch phy mt7530                               - dump mt7530 phy registers
 switch crossover [port] [auto/mdi/mdix]         - switch auto or force mdi/mdix mode for crossover cable
 switch pvid [port] [pvid]                - set pvid on port 0~4
 switch reg r [offset]                       - register read from offset
 switch reg w [offset] [value]               - register write value to offset
 switch reg d [offset]                       - register dump
 switch sip add [sip] [dip] [portmap]            - add a sip entry to switch table
 switch sip del [sip] [dip]                          - del a sip entry to switch table
 switch sip dump                                 - dump switch sip table
 switch sip clear                                - clear switch sip table
 switch tag on [port]                        - keep vlan tag for egress packet on prot 0~4
 switch tag off [port]                       - remove vlan tag for egress packet on port 0~4
 switch vlan dump                            - dump switch table
 switch vlan set [vlan idx (NULL)][vid] [portmap]  - set vlan id and associated member
 switch port [port] [10half|10full|100half|100full|auto]    - get/set port media
 switch phy r [phy_id] [reg]                - get phy reg
 switch phy w [phy_id] [reg] [value]        - set phy reg

如我們可以使用switch vlan dump命令查看目前的配置,可以看到

  • vid=1,portmap是1234 6被置位,就是我們上面的vlan1配置
  • vid=2,portmap是0 5被置位,就是我們上面的vlan2配置
root@Openwrt:/# switch vlan dump
  vid  fid  portmap    s-tag
    1    0  -1111-1-       0
    2    0  1----1--       0
    3    0  invalid
    4    0  invalid
    5    0  invalid
    6    0  invalid
    7    0  invalid
    8    0  invalid
    9    0  invalid
   10    0  invalid
   11    0  invalid
   12    0  invalid
   13    0  invalid
   14    0  invalid
   15    0  invalid
   16    0  invalid

根據(jù)switch的寄存器可以設(shè)置對于的寄存器信息

image.png

image.png
for i in $(seq 0 5)
do
    # set LAN/WAN ports as security mode, egress mode = untagged
    switch reg w "2${i}04" ff0003

    # set LAN/WAN ports as transparent mode
    switch reg w "2${i}10" 810000c0
done

for i in $(seq 6 7)
do
    # set CPU/P7 port as user port
    switch reg w "2${i}10" 81000000

    # set CPU/P7 port as security mode, egress mode = tagged
    switch reg w "2${i}04" 20ff0003
done

# clear mac table if vlan configuration changed
switch clear
switch vlan clear

case "$1" in
"LLLLL")
    echo "nothing for eth0/eth1"
    ;;
"LLLLW")
    # set LAN/WAN ports as security mode
    for i in $(seq 0 7)
    do
            switch reg w "2${i}04" ff0003
    done
    switch vlan set 1 1 11110011
    switch vlan set 2 2 00001100
    # set PVID
    switch pvid 4 2
    switch pvid 5 2
    switch reg w 240c fff10
    switch reg w 250c fff10
    ;;
"WLLLL")
    # set LAN/WAN ports as security mode
    for i in $(seq 0 7)
    do
            switch reg w "2${i}04" ff0003
    done
    # set VLAN member port
    switch vlan set 1 1 01111011
    switch vlan set 2 2 10000100
    # set PVID
    switch pvid 0 2
    switch pvid 5 2
    switch reg w 200c fff10
    switch reg w 250c fff10
    ;;
esac

OpenWRT 中 vlan 的使用:https://blog.csdn.net/qq_36741413/article/details/124612442?spm=1001.2014.3001.5502

openwrt使用VLAN實現(xiàn)簡單的單線復(fù)用:https://www.wunote.cn/article/3906/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容