23-Openwrt switch vlan配置

路由器上面基本都是用switch芯片來實現lan/wan的劃分,但是有的switch只有一個GMAC,有的有多個GMAC,對於內核驅動設備來說就是有的switch生成的只有eth0,有的switch生成的有eth0和eth1。

1、基本概念

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

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

2、 openwrt官方配置swconfig

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

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

swconfig 結構框架是應用層與內核驅動通信的一種框架,主要實通過應用層命令去配置交換機芯片的底層驅動,應用層與內核層採用netlink通信機制.
首先看到

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

這是內核層的代碼,因爲交換機芯片種類繁多,需要有統一接口去兼容所有交換機驅動接口,swconfig.c就是一套定義接口。應用層的命令首先會跳到swconig.c去選擇底層驅動函數。
交換機芯片驅動位置

  • target/linux/generic/files/drivers/net/phy/
    交換機是總線設備驅動類型的,在swconfig.c已經註冊了交換機設備register_switch,在驅動裏面要註冊交換機驅動,然後匹配設備和驅動,調用probe.

所以使用應用層swconfig的package包時,內核也需要有相應的CONFIG_SWCONFIG=y配置開啓。

3、network啓動配置switch過程

/etc/init.d/network start的時候會調用setup_switch函數,該函數位於/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的代碼如下,所以最終是調用swconfig來配置switch參數

#!/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是驅動查出來的

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' //靜態IP

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

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

config switch  
  //switch中文意思就開關,所以下面就是使能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其他命令可以查看官網: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 

圖片來自官網:https://oldwiki.archive.openwrt.org/doc/uci/network/switch

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

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

br-lan = eth0.1 + rai0 + ra0,即將有線LAN口和無線網統一劃分爲 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上面也有標註

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

這就相當於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的應用層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

根據switch的寄存器可以設置對於的寄存器信息


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實現簡單的單線複用:https://www.wunote.cn/article/3906/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章