# macvlan

macvlan 是一种网卡虚拟化的技术，由 linux 内核提供，支持的版本有 v3.9-3.19 和 4.0+，比较稳定的版本推荐 4.0+。有时候我们希望网卡可以有多个 ip 和 mac 地址。这时候就可以使用 macvlan 。其工作原理图如下：

![图1 macvlan 的工作原理](/files/-M6NOkrTrvRLE-zzOa--)

从物理网卡进来的二层帧，根据目的 mac 地址，转发到不同的 macvlan 上。然后由 macvlan 转发到网络协议栈。

### 四种模式

macvlan 有四种工作模式：

* private 模式
* vepa(virtual ethernet port aggregator) 模式
* bridge 模式
* passthru 模式

#### private 模式

同一物理接口下的子接口无法互相同样，即使是从外部的物理交换机进来的帧。

![](/files/-M6NXtlftWPqQLzFANok)

**vepa**

这种模式下，子接口之间的通信流量需要导到外部支持 802.1Qbg/VPEA 功能的交换机上(可以是物理的或者虚拟的)，经由外部交换机转发，再绕回来。

注： 802.1Qbg/VPEA 功能简单说就是交换机要支持 发夹(hairpin) 功能，也就是数据包从一个接口上收上来之后还能再扔回去。

![](/files/-M6NYIfKHtJwigviFUD0)

#### **### bridge**

这种模式下，模拟的是 Linux bridge 的功能，但比 bridge 要好的一点是每个接口的 MAC 地址是已知的，不用学习。所以，这种模式下，子接口之间就是直接可以通信的。

![](/files/-M6NYalIOtPqvTsD_BBo)

**passthru**

这种模式，只允许单个子接口连接主接口，且必须设置成混杂模式，一般用于子接口桥接和创建 VLAN 子接口的场景。

![](/files/-M6NYpevZLOJJkyDimjc)

### 参考资料

* \[网络虚拟化技术（二）: TUN/TAP MACVLAN MACVTAP]\(<https://blog.kghost.info/2013/03/27/linux-network-tun/>)
* <https://virtual.51cto.com/art/201904/595168.htm>
*


---

# 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://jiangpengfei.gitbook.io/open-vswitch/linux-wang-luo/macvlan.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.
