
# ip link set wlan0 netns 1
RTNETLINK answers: Invalid argument
它適用於普通乙太網路。它也適用於專有的 Broadcom“wl”驅動程式。
對於通常的基於 mac80211 的驅動程式如何做到這一點?
答案1
您需要移動 PHY:
iw phy phy0 set netns 666
其中 666 是在該網路命名空間中執行的某個程序的 pid;對於由iproute2
工具(使用ip netns
)創建的網路空間,您可以在其中建立短期進程並取得其 pid:
iw phy phy0 set netns "$(ip netns exec mynetns sh -c '
sleep 1 >&- & echo "$!"')"
若要將其移回根命名空間:
ip netns exec mynetns iw phy phy0 set netns 1
答案2
對於 wifi,請先取得介面的實體位址(如果您有超過 1 張無線卡):
iw dev
結果會是這樣的:
phy#0
Interface wlp58s0
ifindex 3
wdev 0x1
...
所以名字是phy0
現在用它來將你的 wifi 分配給命名空間:
# iw phy phy0 set netns name <MyNamespace>
以下人員的幫助iw
:
phy <phyname> set netns { <pid> | name <nsname> }
Put this wireless device into a different network namespace:
<pid> - change network namespace by process id
<nsname> - change network namespace by name from /var/run/netns
or by absolute path (man ip-netns)
答案3
實施了我自己的解決方法:驗證
- 建立一對虛擬接口,將其中一個(例如 veth1)移至其他命名空間
ip link add type veth
:ip link set veth1 netns <some_pid>
; - 啟動 wlan0 介面和 veth0,但不新增任何位址;
vethify wlan0 veth0
在主機網路命名空間上啟動。它將把 wlan0 上捕獲的所有內容複製到 veth0 並返回;- 在veth1上配置其他命名空間的IP位址和路由;
- 停用 veth1: 的校驗和卸載
ethtool --offload veth1 rx off tx off && ethtool -K veth1 gso off
。
注意:vethify 可以看到網路命名空間中的所有資料包,而不僅僅是 wlan0 和 veth0。它將增加延遲並導致額外的開銷。