$ sudo ip link add link eno1 name macvtap1 type macvtap mode bridge $ sudo ip link add link eno1 name macvtap2 type macvtap mode bridge $ sudo ip linkset dev macvtap1 up $ sudo ip linkset dev macvtap2 up
但是需要注意的是,即使是 bridge 模式也对虚机和该虚机所在宿主机之间的网络连通没有帮助(虽然虚机与外部的其它宿主机之间的网络是连通的),这是 macvtap 与 linux bridge 在用户体验上最大的差异。
This situation is actually not an error — it is the defined behavior of macvtap. Due to the way in which the host’s physical Ethernet is attached to the macvtap bridge, traffic into that bridge from the guests that is forwarded to the physical interface cannot be bounced back up to the host’s IP stack. Additionally, traffic from the host’s IP stack that is sent to the physical interface cannot be bounced back up to the macvtap bridge for forwarding to the guests.
macvtap_newlink macvlan_common_newlink lowerdev = __dev_get_by_index(nla_get_u32(tb[IFLA_LINK])); macvlan_port_create(lowerdev); // only once for all macvlan ifaces netdev_rx_handler_register(dev /* i.e., lowerdev */, macvlan_handle_frame); register_netdevice(dev);
macvlan_handle_frame if(is_multicast_ether_addr(eth->h_dest)) { if (macvlan_forward_source(skb, port, eth->h_source)) { kfree_skb(skb); return RX_HANDLER_CONSUMED; } src = macvlan_hash_lookup(port, eth->h_source); if (src && src->mode != MACVLAN_MODE_VEPA && src->mode != MACVLAN_MODE_BRIDGE) { /* forward to original port. */ vlan = src; ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?: __netif_rx(skb); handle_res = RX_HANDLER_CONSUMED; goto out; }
hash = mc_hash(NULL, eth->h_dest); if (test_bit(hash, port->mc_filter)) macvlan_broadcast_enqueue(port, src, skb);
在相同的物理接口上创建 macvlan 或 macvtap 接口并配置 IP 就可以实现虚机和该虚机所在宿主机之间的通信:
1 2 3
$ sudo ip link add link eno1 name macvlan1 type macvlan mode bridge $ sudo ip linkset dev macvlan1 up $ sudo ip a add dev macvlan1 192.168.0.5/24
One possible method of eliminating this problem would be to create a separate macvtap interface for host use, and give it the IP configuration previously on the physical ethernet - in this way, the host would be an equal peer attached to the macvlap bridge, and thus guest and host could communicate directly.