问题
If the kernel decides to change the interface index for some reason, our userspace code cached interface index does not gets updated and it starts dropping the packet because their is mismatch between interface index cached in userspace and what is actually present in kernel.
I've used if_indextoname(index, interfaceName) to solve this problem. Basically kernel knows which index maps to correct interface name.
Now comes my requirement: I need to test my code. Is there a way by which I can simulate index change so that I can verify my code? I have a gut feeling that since index is something which is very much particular to kernel thus it can only be accessed by calls(if_nametoindex()/etc.) but we can't change it. So how do I validate my changeset?
回答1:
sysctl can't do what you are suggesting.
udev takes care of naming interfaces and even though there are a few naming schemes there is nothing to change the index.
If you can read code have a look here:
http://cgit.freedesktop.org/systemd/systemd/tree/src/udev/udev-builtin-net_id.c#n20
回答2:
If you can force the netdev to unregister, it will choose a new index when it re-registers. For example if the interface is RNDIS (over USB) you can simply disconnect and reconnect the USB. To make sure the index changed you can add a print in the netdev probe...
来源:https://stackoverflow.com/questions/32859179/is-it-possible-to-modify-interface-index