# VLAN 在 WiFi AP 硬體上的實現

主要內容來自於: <https://openwrt.org/docs/guide-user/network/vlan/switch>

## VLAN 的實現架構

一般來說，一個 WiFi AP 會有兩個晶片，分別處理有線網路的封包，以及無線網路的封包，對於較複雜的 WiFi AP 而言，則可能會有多個處理無線網路的晶片，例如: 對應於 802.11n 和 802.11ac 有各自的晶片負責處理所接收到的封包。如果想要查詢所使用的 WiFi AP 硬體架構，可以到以下網站上查詢: <https://wikidevi.com/wiki/Main_Page>

以我們所使用的 WiFi AP 為例，可以看到: [https://wikidevi.com/wiki/GL.iNet\_GL-AR750](https://wikidevi.com/wiki/GL.iNet_GL-AR750S)。其中，我們可以看到，處理有線網路封包的晶片 (ETH chip1) 為 QCA8337，整合在 QCA9563 之中，無線晶片有兩組，QCA9563 負責 2.4 GHz 的通訊，而 QCA9887 負責 5 GHz 上的通訊。在此架構下，會有一個主要用以運算的 CPU，以所使用的 AP 為例，為 QCA9563 晶片負責。

在了解大致上的硬體架構之後，我們就可以把 WiFi AP 的系統架構圖畫出來，並標註 VLAN 是如何在此系統中實現。下圖為 OpenWRT 官網上的說明，以 Asus WL-500g 為例:

![來自: https://openwrt.org/docs/guide-user/network/vlan/switch](/files/-LrYZw4kn7a_6lKqhpIJ)

對應的 VLAN 設置為:

| VLAN ID | <p>Upstream:<br>HW switch<br>↑↓<br>eth0 driver</p> | <p>Downstream:<br>HW switch<br>↑↓<br>phy. ports</p> | <p>Downstream:<br>HW switch<br>↑↓<br>phy. ports</p> | <p>Downstream:<br>HW switch<br>↑↓<br>phy. ports</p> | <p>Downstream:<br>HW switch<br>↑↓<br>phy. ports</p> | <p>Downstream:<br>HW switch<br>↑↓<br>phy. ports</p> |
| ------- | -------------------------------------------------- | --------------------------------------------------- | --------------------------------------------------- | --------------------------------------------------- | --------------------------------------------------- | --------------------------------------------------- |
|         | CPU (eth0)                                         | LAN 1                                               | LAN 2                                               | LAN 3                                               | LAN4                                                | WAN                                                 |
| 1       | tagged                                             | untagged                                            | untagged                                            | untagged                                            | untagged                                            | off                                                 |
| 2       | tagged                                             | off                                                 | off                                                 | off                                                 | off                                                 | untagged                                            |

對應於`/etc/config/network`的設定為 (配合上圖，有做部分修改):

```
config 'switch' 'eth0'
	option 'enable' '1'

config 'switch_vlan' 'eth0_1'
	option 'device' 'eth0'
	option 'vlan' '1'
	option 'ports' '0 5t' #wan

config 'switch_vlan' 'eth0_0'
	option 'device' 'eth0'
	option 'vlan' '0'
	option 'ports' '1 2 3 4 5t' #lan

#note that to use a particular port in an interface the ifname
#should be 'devicename.vlan' . So for example ifname 'eth0.1'
```

對應於這樣的設定，事實上並沒有指定哪一個實體埠為 WAN 和 LAN，而可以藉由更改`option port`中的對應關係，來修改 WAN port 和 LAN port 的位置。另一方面，雖然只有 5 個實體的網路孔，卻有對應到 6 個 port，多出來的 port 對應到 CPU 的運算，而數字旁邊的 "t" 則是標記在 VLAN 中 tag 的狀態。在 VLAN 設定中，如果通訊埠狀態為 "untagged"，即該通訊埠作為 VLAN 成員，但會把 VLAN 的 tag 標記去除，用以連接一般的電腦裝置。 若標記為 "tagged"，則代表體實體埠只能接收相同 VLAN 的封包，同時，也必須連接支援 VLAN 的裝置。

因此，根據上述的設定，就會得到兩組網路設定: LAN 對應到實體埠 1, 2, 3, 4 以及 CPU 埠 5，WAN 對應到實體埠 0 以及 CPU 埠 5。同時，我們也可以看到 WLAN (圖中的 WiFi) 和 VLAN 的關係，兩者是透過 bridge 串接在一起，因此， WLAN 的封包雖然沒有上 VLAN 的 tag，卻可以和 LAN 的裝置互通。我們可以透過`brctl`來看這些介面 (interface) 的相互關係:

```
root@GL-AR750S:~# brctl show
bridge name     bridge id               STP enabled     interfaces
br-lan          7fff.e4956e45a0d8       no              eth0.1
                                                        wlan0
                                                        wlan1
```

可以看到`wlan0`和`wlan1`兩個介面都透過`br-lan`，和`eth0.1`橋接在一起。在此網路設定下，所有的封包都是 untagged，除非是送往 CPU 埠。這樣的設定是因為假設對於此 WiFi AP 而言，往外接出去的裝置 (WAN)，以及向下相連的裝置 (LAN、WLAN) 都看不懂 VLAN 的設定。

## 網卡對 VLAN 的支援

基本上，WiFi AP 的有線網路卡有兩種不同的實作方式，第一種是以 VLAN 虛擬網路，或是實際有多張網卡。我們可以用`ls -l /sys/class/net/`指令來觀察，舉例來說，對於一支援 VLAN 的網卡來說，會看到以下的結果:

```
lrwxrwxrwx    1 root     root             0 Oct 29 00:41 br-lan -> ../../devices/virtual/net/br-lan
lrwxrwxrwx    1 root     root             0 Jan  1  1970 eth0 -> ../../devices/platform/ag71xx.0/net/eth0
lrwxrwxrwx    1 root     root             0 Oct 29 00:41 eth0.1 -> ../../devices/virtual/net/eth0.1
lrwxrwxrwx    1 root     root             0 Oct 29 00:41 eth0.2 -> ../../devices/virtual/net/eth0.2
lrwxrwxrwx    1 root     root             0 Jan  1  1970 lo -> ../../devices/virtual/net/lo
```

在此架構下，我們可以看到一張實體網卡`eth0`，以及兩張虛擬網卡 (在`virtual`資料夾下) 對應於 WAN 和 LAN，分別為: `eth0.1`和`eth0.2`。

另一種介面，WAN 和 LAN 則對應於兩張的實體網卡，如以下結果顯示:

```
lrwxrwxrwx    1 root     root             0 Mar 16 09:06 br-lan -> ../../devices/virtual/net/br-lan
lrwxrwxrwx    1 root     root             0 Jan  1  1970 eth0 -> ../../devices/platform/ag71xx.0/net/eth0
lrwxrwxrwx    1 root     root             0 Jan  1  1970 eth1 -> ../../devices/platform/ag71xx.1/net/eth1
lrwxrwxrwx    1 root     root             0 Jan  1  1970 lo -> ../../devices/virtual/net/l
```

在此架構下，對應於 WAN 和 LAN 的兩張網卡為`eth0`和`eth1`，


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openwrt-nctu.gitbook.io/project/experiment-sdn/openwrt-vlan-framework.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
