lxd/container_lxc.go
func (c *containerLXC) createNetworkDevice(name string, m types.Device) (string, error) {
	var dev, n1 string

	if shared.StringInSlice(m["nictype"], []string{"bridged", "p2p", "macvlan"}) {
		// Host Virtual NIC name
		if m["host_name"] != "" {
			n1 = m["host_name"]
		} else {
			n1 = deviceNextVeth()
		}
	}

	if m["nictype"] == "sriov" {
		dev = m["host_name"]
	}

	// Handle bridged and p2p
	if shared.StringInSlice(m["nictype"], []string{"bridged", "p2p"}) {
		n2 := deviceNextVeth()

		_, err := shared.RunCommand("ip", "link", "add", "dev", n1, "type", "veth", "peer", "name", n2)
		if err != nil {
			return "", fmt.Errorf("Failed to create the veth interface: %s", err)
		}

		_, err = shared.RunCommand("ip", "link", "set", "dev", n1, "up")
		if err != nil {
			return "", fmt.Errorf("Failed to bring up the veth interface %s: %s", n1, err)
		}

		if m["nictype"] == "bridged" {
			err = networkAttachInterface(m["parent"], n1)
			if err != nil {
				deviceRemoveInterface(n2)
				return "", fmt.Errorf("Failed to add interface to bridge: %s", err)
			}

			// Attempt to disable router advertisement acceptance
			networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", n1), "0")
		}

		dev = n2
	}

	// Handle physical and macvlan
	if shared.StringInSlice(m["nictype"], []string{"macvlan", "physical"}) {
		// Deal with VLAN
		device := m["parent"]
		if m["vlan"] != "" {
			device = networkGetHostDevice(m["parent"], m["vlan"])
			if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", device)) {
				_, err := shared.RunCommand("ip", "link", "add", "link", m["parent"], "name", device, "up", "type", "vlan", "id", m["vlan"])
				if err != nil {
					return "", err
				}

				// Attempt to disable IPv6 router advertisement acceptance
				networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", device), "0")
			}
		}

		// Handle physical
		if m["nictype"] == "physical" {
			dev = device
		}

		// Handle macvlan
		if m["nictype"] == "macvlan" {
			_, err := shared.RunCommand("ip", "link", "add", "dev", n1, "link", device, "type", "macvlan", "mode", "bridge")
			if err != nil {
				return "", fmt.Errorf("Failed to create the new macvlan interface: %s", err)
			}

			dev = n1
		}
	}

	// Set the MAC address
	if m["hwaddr"] != "" {
		_, err := shared.RunCommand("ip", "link", "set", "dev", dev, "address", m["hwaddr"])
		if err != nil {
			deviceRemoveInterface(dev)
			return "", fmt.Errorf("Failed to set the MAC address: %s", err)
		}
	}

	// Bring the interface up
	_, err := shared.RunCommand("ip", "link", "set", "dev", dev, "up")
	if err != nil {
		deviceRemoveInterface(dev)
		return "", fmt.Errorf("Failed to bring up the interface: %s", err)
	}

	// Set the filter
	if m["nictype"] == "bridged" && shared.IsTrue(m["security.mac_filtering"]) {
		err = c.createNetworkFilter(dev, m["parent"], m["hwaddr"])
		if err != nil {
			return "", err
		}
	}

	return dev, nil
}
func (c *containerLXC) createNetworkDevice(name string, m types.Device) (string, error) {
	var dev, n1 string

	if shared.StringInSlice(m["nictype"], []string{"bridged", "p2p", "macvlan"}) {
		// Host Virtual NIC name
		if m["host_name"] != "" {
			n1 = m["host_name"]
		} else {
			n1 = deviceNextVeth()
		}
	}

	if m["nictype"] == "sriov" {
		dev = m["host_name"]
	}

	// Handle bridged and p2p
	if shared.StringInSlice(m["nictype"], []string{"bridged", "p2p"}) {
		n2 := deviceNextVeth()

		_, err := shared.RunCommand("ip", "link", "add", "dev", n1, "type", "veth", "peer", "name", n2)
		if err != nil {
			return "", fmt.Errorf("Failed to create the veth interface: %s", err)
		}

		_, err = shared.RunCommand("ip", "link", "set", "dev", n1, "up")
		if err != nil {
			return "", fmt.Errorf("Failed to bring up the veth interface %s: %s", n1, err)
		}

		if m["nictype"] == "bridged" {
			err = networkAttachInterface(m["parent"], n1)
			if err != nil {
				deviceRemoveInterface(n2)
				return "", fmt.Errorf("Failed to add interface to bridge: %s", err)
			}

			// Attempt to disable router advertisement acceptance
			networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", n1), "0")
		}

		dev = n2
	}

	// Handle physical and macvlan
	if shared.StringInSlice(m["nictype"], []string{"macvlan", "physical"}) {
		// Deal with VLAN
		device := m["parent"]
		if m["vlan"] != "" {
			device = networkGetHostDevice(m["parent"], m["vlan"])
			if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", device)) {
				_, err := shared.RunCommand("ip", "link", "add", "link", m["parent"], "name", device, "up", "type", "vlan", "id", m["vlan"])
				if err != nil {
					return "", err
				}

				// Attempt to disable IPv6 router advertisement acceptance
				networkSysctlSet(fmt.Sprintf("ipv6/conf/%s/accept_ra", device), "0")
			}
		}

		// Handle physical
		if m["nictype"] == "physical" {
			dev = device
		}

		// Handle macvlan
		if m["nictype"] == "macvlan" {
			_, err := shared.RunCommand("ip", "link", "add", "dev", n1, "link", device, "type", "macvlan", "mode", "bridge")
			if err != nil {
				return "", fmt.Errorf("Failed to create the new macvlan interface: %s", err)
			}

			dev = n1
		}
	}

	// Set the MAC address
	if m["hwaddr"] != "" {
		_, err := shared.RunCommand("ip", "link", "set", "dev", dev, "address", m["hwaddr"])
		if err != nil {
			deviceRemoveInterface(dev)
			return "", fmt.Errorf("Failed to set the MAC address: %s", err)
		}
	}

	// Bring the interface up
	_, err := shared.RunCommand("ip", "link", "set", "dev", dev, "up")
	if err != nil {
		deviceRemoveInterface(dev)
		return "", fmt.Errorf("Failed to bring up the interface: %s", err)
	}

	// Set the filter
	if m["nictype"] == "bridged" && shared.IsTrue(m["security.mac_filtering"]) {
		err = c.createNetworkFilter(dev, m["parent"], m["hwaddr"])
		if err != nil {
			return "", err
		}
	}

	return dev, nil
}
// Network device handling