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 }
package devices_test import ( "fmt" "net" "github.com/cloudfoundry-incubator/garden-linux/network/devices" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Veth Pair Creation", func() { var ( v devices.VethCreator hostName, containerName string ) f := func(i *net.Interface, _ error) *net.Interface { return i } l := func(_, _ interface{}, e error) error { return e } BeforeEach(func() { hostName = fmt.Sprintf("doesntexist-h-%d", GinkgoParallelNode()) containerName = fmt.Sprintf("doesntexist-c-%d", GinkgoParallelNode()) }) AfterEach(func() {