创建 veth 对后无法直接为 veth 分配 ip 地址

创建 veth 对后无法直接为 veth 分配 ip 地址

如果我运行以下命令,则 IP 地址实际上不会被添加:

ip link add veth0 type veth peer name veth1; ip address add 10.0.1.2/24 dev veth1

但是,如果我在执行命令之间睡着了,那么它会被添加:

ip link add veth0 type veth peer name veth1; sleep 0.1; ip address add 10.0.1.2/24 dev veth1

我重复了多次,结果一致。

除了半夜睡觉以外,还有没有更好的方法吗?

更新:

使用 ip montitor,似乎在创建 veth 对时正在执行大量操作(不知道是什么),并且当两个命令在同一行上运行时(不带 sleep),ip 地址创建会停止执行大量操作,并且 ip 地址会被自动删除。

有谁了解创建 veth 对时发生的所有事情是什么吗?

veth 创建的 ip 监视器输出:

ipv4 dev veth1 forwarding on rp_filter strict mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
ipv6 dev veth1 forwarding off mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
372: veth1@NONE: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default 
    link/ether da:a6:13:1a:29:31 brd ff:ff:ff:ff:ff:ff
ipv4 dev veth0 forwarding on rp_filter strict mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
ipv6 dev veth0 forwarding off mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
373: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default 
    link/ether 4a:9e:6c:13:13:34 brd ff:ff:ff:ff:ff:ff
372: veth1@veth0: <NO-CARRIER,BROADCAST,MULTICAST,UP,M-DOWN> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default 
    link/ether da:a6:13:1a:29:31 brd ff:ff:ff:ff:ff:ff
373: veth0@veth1: <NO-CARRIER,BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default 
    link/ether 4a:9e:6c:13:13:34 brd ff:ff:ff:ff:ff:ff
ff00::/8 dev veth0 table local metric 256 pref medium
373: veth0@veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 4a:9e:6c:13:13:34 brd ff:ff:ff:ff:ff:ff
ff00::/8 dev veth1 table local metric 256 pref medium
372: veth1@veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether da:a6:13:1a:29:31 brd ff:ff:ff:ff:ff:ff
Deleted ff00::/8 dev veth0 table local metric 256 pref medium
372: veth1@veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether da:a6:13:1a:29:31 brd ff:ff:ff:ff:ff:ff
Deleted ff00::/8 dev veth1 table local metric 256 pref medium
ff00::/8 dev veth1 table local metric 256 pref medium
373: veth0@veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 4a:9e:6c:13:13:34 brd ff:ff:ff:ff:ff:ff
ff00::/8 dev veth0 table local metric 256 pref medium
372: veth1@veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether da:a6:13:1a:29:31 brd ff:ff:ff:ff:ff:ff
Deleted ff00::/8 dev veth1 table local metric 256 pref medium
ff00::/8 dev veth1 table local metric 256 pref medium
fe80::/64 dev veth1 proto kernel metric 256 pref medium
373: veth0@veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 4a:9e:6c:13:13:34 brd ff:ff:ff:ff:ff:ff
Deleted ff00::/8 dev veth0 table local metric 256 pref medium
ff00::/8 dev veth0 table local metric 256 pref medium
fe80::/64 dev veth0 proto kernel metric 256 pref medium

用于添加 ip 地址的 ip 监视器输出:

372: veth1    inet 10.0.1.2/24 scope global veth1
       valid_lft forever preferred_lft forever
local 10.0.1.2 dev veth1 table local proto kernel scope host src 10.0.1.2
10.0.1.0/24 dev veth1 proto kernel scope link src 10.0.1.2
broadcast 10.0.1.0 dev veth1 table local proto kernel scope link src 10.0.1.2
broadcast 10.0.1.255 dev veth1 table local proto kernel scope link src 10.0.1.2

在同一行上运行时(不休眠)的 ip 监视器输出。* 表示开始创建 ip 地址的行:

ipv4 dev veth1 forwarding on rp_filter strict mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
ipv6 dev veth1 forwarding off mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
374: veth1@NONE: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default 
    link/ether 72:94:ec:5a:d7:76 brd ff:ff:ff:ff:ff:ff
ipv4 dev veth0 forwarding on rp_filter strict mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
ipv6 dev veth0 forwarding off mc_forwarding off proxy_neigh off ignore_routes_with_linkdown off 
375: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default 
    link/ether b6:7f:f2:e5:69:e6 brd ff:ff:ff:ff:ff:ff
****************************************************************
374: veth1    inet 10.0.1.2/24 scope global veth1
       valid_lft forever preferred_lft forever
****************************************************************
local 10.0.1.2 dev veth1 table local proto kernel scope host src 10.0.1.2 
375: veth0@veth1: <NO-CARRIER,BROADCAST,MULTICAST,UP,M-DOWN> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default 
    link/ether b6:7f:f2:e5:69:e6 brd ff:ff:ff:ff:ff:ff
374: veth1@veth0: <NO-CARRIER,BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default 
    link/ether 72:94:ec:5a:d7:76 brd ff:ff:ff:ff:ff:ff
10.0.1.0/24 dev veth1 proto kernel scope link src 10.0.1.2 
broadcast 10.0.1.0 dev veth1 table local proto kernel scope link src 10.0.1.2 
broadcast 10.0.1.255 dev veth1 table local proto kernel scope link src 10.0.1.2 
ff00::/8 dev veth1 table local metric 256 pref medium
374: veth1@veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 72:94:ec:5a:d7:76 brd ff:ff:ff:ff:ff:ff
ff00::/8 dev veth0 table local metric 256 pref medium
375: veth0@veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether b6:7f:f2:e5:69:e6 brd ff:ff:ff:ff:ff:ff
Deleted 10.0.1.0/24 dev veth1 proto kernel scope link src 10.0.1.2 
Deleted ff00::/8 dev veth1 table local metric 256 pref medium
Deleted 374: veth1    inet 10.0.1.2/24 scope global veth1
       valid_lft forever preferred_lft forever
Deleted broadcast 10.0.1.255 dev veth1 table local proto kernel scope link src 10.0.1.2 
Deleted broadcast 10.0.1.0 dev veth1 table local proto kernel scope link src 10.0.1.2 
Deleted local 10.0.1.2 dev veth1 table local proto kernel scope host src 10.0.1.2 
375: veth0@veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether b6:7f:f2:e5:69:e6 brd ff:ff:ff:ff:ff:ff
Deleted ff00::/8 dev veth0 table local metric 256 pref medium
ff00::/8 dev veth0 table local metric 256 pref medium
374: veth1@veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 72:94:ec:5a:d7:76 brd ff:ff:ff:ff:ff:ff
ff00::/8 dev veth1 table local metric 256 pref medium
375: veth0@veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether b6:7f:f2:e5:69:e6 brd ff:ff:ff:ff:ff:ff
Deleted ff00::/8 dev veth0 table local metric 256 pref medium
ff00::/8 dev veth0 table local metric 256 pref medium
fe80::/64 dev veth0 proto kernel metric 256 pref medium
374: veth1@veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 72:94:ec:5a:d7:76 brd ff:ff:ff:ff:ff:ff
Deleted ff00::/8 dev veth1 table local metric 256 pref medium
ff00::/8 dev veth1 table local metric 256 pref medium
fe80::/64 dev veth1 proto kernel metric 256 pref medium
374: veth1    inet6 fe80::1ab9:60e6:dfe2:5904/64 scope link 
       valid_lft forever preferred_lft forever
local fe80::1ab9:60e6:dfe2:5904 dev veth1 table local proto kernel metric 0 pref medium
374: veth1    inet6 fe80::1ab9:60e6:dfe2:5904/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
Deleted fe80::/64 dev veth1 proto kernel metric 256 pref medium
fe80::/64 dev veth1 proto kernel metric 256 pref medium
fe80::/64 dev veth1 proto kernel metric 102 pref medium
375: veth0    inet6 fe80::aa07:cb3f:757e:f3f4/64 scope link 
       valid_lft forever preferred_lft forever
local fe80::aa07:cb3f:757e:f3f4 dev veth0 table local proto kernel metric 0 pref medium
375: veth0    inet6 fe80::aa07:cb3f:757e:f3f4/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
Deleted fe80::/64 dev veth0 proto kernel metric 256 pref medium
fe80::/64 dev veth0 proto kernel metric 256 pref medium
fe80::/64 dev veth0 proto kernel metric 101 pref medium

相关内容