Exemple #1
0
func (v *veth) create(n *network, nspid int) (err error) {
	tmpName, err := v.generateTempPeerName()
	if err != nil {
		return err
	}
	n.TempVethPeerName = tmpName
	defer func() {
		if err != nil {
			netlink.NetworkLinkDel(n.HostInterfaceName)
			netlink.NetworkLinkDel(n.TempVethPeerName)
		}
	}()
	if n.Bridge == "" {
		return fmt.Errorf("bridge is not specified")
	}
	if err := netlink.NetworkCreateVethPair(n.HostInterfaceName, n.TempVethPeerName, n.TxQueueLen); err != nil {
		return err
	}
	if err := v.attach(&n.Network); err != nil {
		return err
	}
	child, err := net.InterfaceByName(n.TempVethPeerName)
	if err != nil {
		return err
	}
	return netlink.NetworkSetNsPid(child, nspid)
}
Exemple #2
0
// NewVethPair creates a pair of veth network links.
//
// It is equivalent of running:
// 		ip link add name veth${RANDOM STRING} type veth peer name veth${RANDOM STRING}.
// NewVethPair returns Vether which is initialized to a pointer of type VethPair if the
// veth link was successfully created on Linux host. Newly created pair of veth links
// are assigned random names starting with "veth".
// NewVethPair returns error if the veth pair could not be created.
func NewVethPair() (Vether, error) {
	ifcName := makeNetInterfaceName("veth")
	peerName := makeNetInterfaceName("veth")

	if err := netlink.NetworkCreateVethPair(ifcName, peerName, 0); err != nil {
		return nil, err
	}

	newIfc, err := net.InterfaceByName(ifcName)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	peerIfc, err := net.InterfaceByName(peerName)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	return &VethPair{
		Link: Link{
			ifc: newIfc,
		},
		peerIfc: peerIfc,
	}, nil
}
Exemple #3
0
// NewVethPairWithOptions creates a pair of veth network links.
//
// It is equivalent of running:
// 		ip link add name ${first device name} type veth peer name ${second device name}
// NewVethPairWithOptions returns Vether which is initialized to a pointer of type VethPair if the
// veth link was successfully created on the Linux host. It accepts VethOptions which allow you to set
// peer interface name. It returns error if the veth pair could not be created.
func NewVethPairWithOptions(ifcName string, opts VethOptions) (Vether, error) {
	peerName := opts.PeerName
	txQLen := opts.TxQueueLen

	if ok, err := NetInterfaceNameValid(ifcName); !ok {
		return nil, err
	}

	if _, err := net.InterfaceByName(ifcName); err == nil {
		return nil, fmt.Errorf("Interface name %s already assigned on the host", ifcName)
	}

	if peerName != "" {
		if ok, err := NetInterfaceNameValid(peerName); !ok {
			return nil, err
		}

		if _, err := net.InterfaceByName(peerName); err == nil {
			return nil, fmt.Errorf("Interface name %s already assigned on the host", peerName)
		}
	} else {
		peerName = makeNetInterfaceName("veth")
	}

	if txQLen < 0 {
		return nil, fmt.Errorf("TX queue length must be a positive integer: %d", txQLen)
	}

	if err := netlink.NetworkCreateVethPair(ifcName, peerName, txQLen); err != nil {
		return nil, err
	}

	newIfc, err := net.InterfaceByName(ifcName)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	peerIfc, err := net.InterfaceByName(peerName)
	if err != nil {
		return nil, fmt.Errorf("Could not find the new interface: %s", err)
	}

	return &VethPair{
		Link: Link{
			ifc: newIfc,
		},
		peerIfc: peerIfc,
	}, nil
}
func (v *veth) create(n *network, nspid int) (err error) {
	tmpName, err := v.generateTempPeerName()
	if err != nil {
		return err
	}
	n.TempVethPeerName = tmpName
	defer func() {
		if err != nil {
			netlink.NetworkLinkDel(n.HostInterfaceName)
			netlink.NetworkLinkDel(n.TempVethPeerName)
		}
	}()
	if n.Bridge == "" {
		return fmt.Errorf("bridge is not specified")
	}
	bridge, err := net.InterfaceByName(n.Bridge)
	if err != nil {
		return err
	}
	if err := netlink.NetworkCreateVethPair(n.HostInterfaceName, n.TempVethPeerName, n.TxQueueLen); err != nil {
		return err
	}
	host, err := net.InterfaceByName(n.HostInterfaceName)
	if err != nil {
		return err
	}
	if err := netlink.AddToBridge(host, bridge); err != nil {
		return err
	}
	if err := netlink.NetworkSetMTU(host, n.Mtu); err != nil {
		return err
	}
	if n.HairpinMode {
		if err := netlink.SetHairpinMode(host, true); err != nil {
			return err
		}
	}
	if err := netlink.NetworkLinkUp(host); err != nil {
		return err
	}
	child, err := net.InterfaceByName(n.TempVethPeerName)
	if err != nil {
		return err
	}
	return netlink.NetworkSetNsPid(child, nspid)
}
Exemple #5
0
func (VethCreator) Create(hostIfcName, containerIfcName string) (host, container *net.Interface, err error) {
	netlinkMu.Lock()
	defer netlinkMu.Unlock()

	if err := netlink.NetworkCreateVethPair(hostIfcName, containerIfcName, 1); err != nil {
		return nil, nil, fmt.Errorf("devices: create veth pair: %v", err)
	}

	if host, err = net.InterfaceByName(hostIfcName); err != nil {
		return nil, nil, fmt.Errorf("devices: look up created host interface: %v", err)
	}

	if container, err = net.InterfaceByName(containerIfcName); err != nil {
		return nil, nil, fmt.Errorf("devices: look up created container interface: %v", err)
	}

	return host, container, nil
}
Exemple #6
0
func CreateVethPair(name1, name2 string, txQueueLen int) error {
	return netlink.NetworkCreateVethPair(name1, name2, txQueueLen)
}
Exemple #7
0
func CreateVethPair(name1, name2 string) error {
	return netlink.NetworkCreateVethPair(name1, name2)
}