func createVethPair(handle string, wshdPID int, containerIP string, containerIPAddressMask int, containerMTU int) (string, error) {
	hostIfName := fmt.Sprintf("o-%s-0", handle)
	containerIfName := fmt.Sprintf("o-%s-1", handle)

	vethCreator := devices.VethCreator{}
	hostIf, containerIf, err := vethCreator.Create(hostIfName, containerIfName)
	if err != nil {
		if !strings.Contains(err.Error(), "Network interface already exists") {
			return "", err
		}

		hostIf, err = net.InterfaceByName(hostIfName)
		if err != nil {
			return "", err
		}

		containerIf, err = net.InterfaceByName(containerIfName)
		if err != nil && !strings.Contains(err.Error(), "no such network interface") {
			return "", err
		}
	}

	link := devices.Link{}

	if containerIf != nil {
		err = link.SetNs(containerIf, wshdPID)
		if err != nil {
			return "", err
		}
		fmt.Printf("container if: %+v\n", containerIf)
	}

	if hostIf != nil {
		fmt.Printf("host if: %+v\n", hostIf)
	}

	err = runInNamespace(handle, fmt.Sprintf("ip addr add %s/%d dev %s", containerIP, containerIPAddressMask, containerIfName))
	if err != nil && !strings.Contains(err.Error(), "RTNETLINK answers: File exists") {
		return "", err
	}

	err = runInNamespace(handle, fmt.Sprintf("ip link set %s up", containerIfName))
	if err != nil {
		return "", err
	}

	err = runInNamespace(handle, fmt.Sprintf("ip link set dev %s mtu %d", containerIfName, containerMTU))
	if err != nil {
		return "", err
	}

	return hostIfName, nil
}
Example #2
0
	"net"
	"os/exec"
	"strconv"
	"strings"

	"github.com/cloudfoundry-incubator/garden-linux/network/devices"
	"github.com/docker/libcontainer/netlink"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Link Management", func() {
	var (
		l    devices.Link
		name string
		intf *net.Interface
	)

	BeforeEach(func() {
		cmd, err := gexec.Start(exec.Command("sh", "-c", "mountpoint /sys || mount -t sysfs sysfs /sys"), GinkgoWriter, GinkgoWriter)
		Expect(err).ToNot(HaveOccurred())
		Eventually(cmd).Should(gexec.Exit(0))

		name = fmt.Sprintf("gdn-test-%d", GinkgoParallelNode())
		Expect(netlink.NetworkLinkAdd(name, "dummy")).To(Succeed())
		intf, _ = net.InterfaceByName(name)
	})

	AfterEach(func() {
		cleanup(name)