// 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); 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 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 err := netlink.NetworkCreateVethPair(ifcName, peerName); 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 }