ipmac vlan

Macvlan vs Bridge

The macvlan is a trivial bridge that doesn’t need to do learning as it knows every mac address it can receive, so it doesn’t need to implement learning or stp. Which makes it simple stupid and and fast.

Use Macvlan:

Use Bridge:

  • When you need to connect VMs or containers on the same host.
    • 当您需要连接同一主机上的虚拟机或容器时。
  • For complex topologies with multiple bridges and hybrid environments (hosts in the same Layer2 domain both on the same host and outside the host).
  • You need to apply advanced flood control, FDB manipulation, etc.

 

Comparing ipvlan to macvlan

  ipvlan should be used in cases where some switches restrict the maximum number of mac address per physical port due to port security configuration.  

macvlan needs to be used in cases where common dhcp server is used since dhcp server would need unique mac address which ipvlan does not have.

 

 

  macvlan 允许你在主机的一个网络接口上配置多个虚拟的网络接口,这些网络 interface 有自己独立的 mac 地址,也可以配置上 ip 地址进行通信。

macvlan 下的虚拟机或者容器网络和主机在同一个网段中,共享同一个广播域。macvlan 和 bridge 比较相似,但因为它省去了 bridge 的存在,所以配置和调试起来比较简单,而且效率也相对高。

除此之外,macvlan 自身也完美支持 VLAN。

 

IPVlan 和 macvlan 类似,都是从一个主机接口虚拟出多个虚拟网络接口。一个重要的区别就是所有的虚拟接口都有相同的 macv 地址,而拥有不同的 ip 地址。因为所有的虚拟接口要共享 mac 地址,所有有些需要注意的地方:

  • DHCP 协议分配 ip 的时候一般会用 mac 地址作为机器的标识。这个情况下,客户端动态获取 ip 的时候需要配置唯一的 ClientID 字段,并且 DHCP server 也要正确配置使用该字段作为机器标识,而不是使用 mac 地址

 

两种模式

ipvlan 有两种不同的模式:L2 和 L3。一个父接口只能选择一种模式,依附于它的所有虚拟接口都运行在这个模式下,不能混用模式。

L2 模式

ipvlan L2 模式和 macvlan bridge 模式工作原理很相似,父接口作为交换机来转发子接口的数据。同一个网络的子接口可以通过父接口来转发数据,而如果想发送到其他网络,报文则会通过父接口的路由转发出去。

L3 模式

L3 模式下,ipvlan 有点像路由器的功能,它在各个虚拟网络和主机网络之间进行不同网络报文的路由转发工作。只要父接口相同,即使虚拟机/容器不在同一个网络,也可以互相 ping 通对方,因为 ipvlan 会在中间做报文的转发工作。

 

L3 模式下的虚拟接口不会接收到多播或者广播的报文,为什么呢?这个模式下,所有的网络都会发送给父接口,所有的 ARP 过程或者其他多播报文都是在底层的父接口完成的。需要注意的是:外部网络默认情况下是不知道 ipvlan 虚拟出来的网络的,如果不在外部路由器上配置好对应的路由规则,ipvlan 的网络是不能被外部直接访问的。

ip link add ipv1 link eth0 type ipvlan mode l3
ip link add ipv2 link eth0 type ipvlan mode l3
ip link set ipv1 netns net1
ip link set ipv2 netns net2
ip netns exec net1 ip link set ipv1 up
ip netns exec net2 ip link set ipv2 up

ip netns exec net1 ip addr add 10.0.1.10/24 dev ipv1
ip netns exec net2 ip addr add 192.168.1.10/24 dev ipv2
ip netns exec net1 ip route add default dev ipv1
 ip netns exec net2 ip route add default dev ipv2
测试两个网络的连通性:ping

 

ipvlan 还是 macvlan?

ipvlan 和 macvlan 两个虚拟网络模型提供的功能,看起来差距并不大,那么什么时候需要用到 ipvlan 呢?要回答这个问题,我们先来看看 macvlan 存在的不足:

  • 需要大量 mac 地址。每个虚拟接口都有自己的 mac 地址,而网络接口和交换机支持的 mac 地址有支持的上限
  • 无法和 802.11(wireless) 网络一起工作

对应的,如果你遇到一下的情况,请考虑使用 ipvlan:

  • 父接口对 mac 地址数目有限制,或者在 mac 地址过多的情况下会造成严重的性能损失
  • 工作在无线网络中
  • 希望搭建比较复杂的网络拓扑(不是简单的二层网络和 VLAN),比如要和 BGP 网络一起工作

 

 

参考:

bridge-vs-macvlan

macvlan-and-ipvlan

posted @ 2024-01-17 11:34  codestacklinuxer  阅读(3)  评论(0编辑  收藏  举报