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) }
// 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 }
// 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 }