Skip to contentSkip to conversation

Atomically changing NIC dev name + namespace ?

DISCUSSION Daniel P. Berrange 2 participants 2 messages Jul 27, 2012 – Aug 6, 2012 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Another feature I'd like to provide in libvirt LXC is "hotplug" of NIC devices to containers.

eg, I have a NIC eth1 in the host and I want to make it appear as eth0 in the container. There are two ways I can do this

  # ip link set eth1 name eth0
  # ip link set eth0 ns $PID

Or

  # ip link set eth1 ns $PID
  # setns --net $PID  (assume it opens /proc/$PID/ns/net & does setns(fd))
  # ip link set eth1 name eth0

Both these approaches suffer from flaws though. The first approach fails on step one, if there is already another NIC in the host, that has the same name as the target we want to use in the container. The second approach fails on step 1 if there is already a NIC in the container which has the same name as the original device on the host.

To get around this, we'd basically need to temporarily use a completely random NIC name eg

  # ip link set eth1 name $RANDOMNAME
  # ip link set $RANDOMNAME ns $PID
  # setns --net $PID  (assume it opens /proc/$PID/ns/net & does setns(fd))
  # ip link set $RANDOMNAME name eth0

This is workable, but feels slightly unpleasant. So IMHO if would be desirable if the kernel allowed us to atomically change the NIC device name and namespace at the same time. eg

  # ip link set eth1 name eth0 ns $PID

Although that 'ip' CLI syntax is allowed, it appears to implement it in two steps, doing the rename first and then setting the namespace, so you still hit the NIC name clash problem.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#2 lore raw
In reply to a message not in the archive
Quoted 25 lines · Daniel P. Berrange

Another feature I'd like to provide in libvirt LXC is "hotplug" of NIC devices to containers.

eg, I have a NIC eth1 in the host and I want to make it appear as eth0 in the container. There are two ways I can do this

  # ip link set eth1 name eth0
  # ip link set eth0 ns $PID

Or

  # ip link set eth1 ns $PID
  # setns --net $PID  (assume it opens /proc/$PID/ns/net & does setns(fd))
  # ip link set eth1 name eth0

Both these approaches suffer from flaws though. The first approach fails on step one, if there is already another NIC in the host, that has the same name as the target we want to use in the container. The second approach fails on step 1 if there is already a NIC in the container which has the same name as the original device on the host.

To get around this, we'd basically need to temporarily use a completely random NIC name eg

Quoted 6 lines
  # ip link set eth1 name $RANDOMNAME
  # ip link set $RANDOMNAME ns $PID
  # setns --net $PID  (assume it opens /proc/$PID/ns/net & does setns(fd))
  # ip link set $RANDOMNAME name eth0

This is workable, but feels slightly unpleasant. So IMHO if would be

Yes, it feels racy. I assume 'ip' will give a helpful error if RANDOMNAME happens to be already taken though.

Quoted 4 lines

desirable if the kernel allowed us to atomically change the NIC device name and namespace at the same time. eg

  # ip link set eth1 name eth0 ns $PID

An alternative would be to encode the above logic in the ip program itself, but not the kernel. I think that's the way to go. We don't want every user of this to have to do the ugly steps, but I don't know that this quite justifies kernel help.

Quoted 15 lines

Although that 'ip' CLI syntax is allowed, it appears to implement it in two steps, doing the rename first and then setting the namespace, so you still hit the NIC name clash problem.

Regards,
Daniel

Signature · 8 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
_______________________________________________
Containers mailing list
Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linuxfoundation.org/mailman/listinfo/containers