# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

#
# This is an Emulab disk image.  Be sure to understand the actions this
# file triggers before you edit it!
#
# When we boot this image in Emulab, we don't know a priori which device
# is the control network.  So we need to DHCP on all Ethernet
# interfaces, selecting the one on which we get a DHCP response as the
# control network, and bringing down all the others.  (Emulab will
# configure some subset of those others as experiment NICs, if the
# experimenter requested.)  Prior to the Ubuntu 16 systemd-udevd, our
# strategy was to enumerate a bunch of eth* devices in this file as
# 'auto', and then use the mapping script below run dhclient manually
# and discover our control net iface via dhclient enter/exit hooks, and
# the mapping script returns the logical mapped name 'cnet'.  (Note that
# the mapping script locks so that only one copy of it runs at a time,
# despite systemd/ifup trying to run them all in parallel; thus the
# first invocation that gets the lock is the first one to run dhclient.)
# This worked because all wired ethernet devices would be named eth*.
#
# Sadly, since systemd-udevd introduced the new predictable naming
# scheme, where devices are named by buss location, or firmware hint,
# etc, we no longer can be sure to have at least one 'auto foo' device
# in this file, where foo actually exists on the system we're booting.
# So, our new strategy is to add udev rules
# (/etc/udev/rules.d/99-emulab-contron-network.rules) that populates the
# /run/emulab-interfaces.d-auto-added dir (listed as a source dir for
# the ifup/ifdown/ifquery commands below) with files that contain merely
# 'auto <IFACE>'.  Those rules are careful to do only that for certain
# valid wired Ethernet devices (and deliberately not wireless devices!).
# Then, once we've got 'auto ...' stanzas for each possible Ethernet
# device, we can continue to utilize the mapping stanzas below like
# previous versions of this file did.  And we don't have anything to
# clean out on reboot or on image capture, because /run is automatically
# cleared.  ifup/ifdown/ifquery are not bothered by the absence of the
# sourced directory in /run, if that didn't exist for any reason.
# 

source-directory /etc/interfaces.d
source-directory /run/emulab-interfaces.d-auto-added

#
# XXX abuse of mapping function follows.
# The findcnet script DHCPs on all interfaces the first time it is invoked
# and uses the results of that for every other invocation.  The script
# returns "cnet" for the interface that is the control net, and the physical
# interface name for all others.
#
# NB: notice that there are no 'auto' lines in this file for ethernet
# devices.  We depend on the udev rule referenced above to add them for us!
#
mapping eth*
    script /usr/local/etc/emulab/findcnet
mapping en*
    script /usr/local/etc/emulab/findcnet
mapping sl*
    script /usr/local/etc/emulab/findcnet
# NB: don't listen on wireless devices by default
#mapping ww*
#    script /usr/local/etc/emulab/findcnet
#mapping wl*
#    script /usr/local/etc/emulab/findcnet

# The control network has been identified and configured indirectly
# via the mapping above.  Here we just make sure that if shutdown, we
# remove the indicator file so that we will re-DHCP next time.
#
iface cnet inet manual
    up echo "Emulab control net is $IFACE"
    down rm -f /var/run/cnet
    down ifconfig $IFACE down
