Пример #1
0
func newVlanInterface(name string) error {

	intSplit := strings.Split(name, ".")
	if len(intSplit) != 2 {
		return errors.New("invalid interface name, ie. eth0")
	}

	vlanID, _ := strconv.Atoi(intSplit[1])

	err := dnetlink.NetworkLinkAddVlan(intSplit[0], name, uint16(vlanID))
	if err != nil && err.Error() == "file exists" {
		return nil
	} else if err != nil {
		return err
	}
	return nil
}
Пример #2
0
// NewVlanLinkWithOptions creates vlan network link and sets some of its network parameters
// to values passed in as VlanOptions
//
// It is equivalent of running:
//		ip link add name ${vlan name} link ${master interface} address ${macaddress} type vlan id ${tag}
// NewVlanLinkWithOptions returns Vlaner which is initialized to a pointer of type VlanLink if the
// vlan link was created successfully on the Linux host. It accepts VlanOptions which allow you to set
// link's options. It returns error if the link could not be created.
func NewVlanLinkWithOptions(masterDev string, opts VlanOptions) (Vlaner, error) {
	if ok, err := NetInterfaceNameValid(masterDev); !ok {
		return nil, err
	}

	if _, err := net.InterfaceByName(masterDev); err != nil {
		return nil, fmt.Errorf("Master VLAN device %s does not exist on the host", masterDev)
	}

	if err := validateVlanOptions(&opts); err != nil {
		return nil, err
	}

	if err := netlink.NetworkLinkAddVlan(masterDev, opts.Dev, opts.Id); err != nil {
		return nil, err
	}

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

	if opts.MacAddr != "" {
		if err := netlink.NetworkSetMacAddress(vlanIfc, opts.MacAddr); err != nil {
			if errDel := DeleteLink(vlanIfc.Name); errDel != nil {
				return nil, fmt.Errorf("Incorrect options specified. Attempt to delete the link failed: %s",
					errDel)
			}
		}
	}

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

	return &VlanLink{
		Link: Link{
			ifc: vlanIfc,
		},
		masterIfc: masterIfc,
		id:        opts.Id,
	}, nil
}
Пример #3
0
// NewVlanLink creates vlan network link.
//
// It is equivalent of running:
//		ip link add name vlan${RANDOM STRING} link ${master interface name} type vlan id ${tag}
// NewVlanLink returns Vlaner which is initialized to a pointer of type VlanLink if the
// vlan link was successfully created on the Linux host. Newly created link is assigned
// a random name starting with "vlan". It returns error if the link can not be created.
func NewVlanLink(masterDev string, id uint16) (Vlaner, error) {
	vlanDev := makeNetInterfaceName("vlan")

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

	if _, err := net.InterfaceByName(masterDev); err != nil {
		return nil, fmt.Errorf("Master VLAN device %s does not exist on the host", masterDev)
	}

	if id <= 0 {
		return nil, fmt.Errorf("VLAN id must be a postive Integer: %d", id)
	}

	if err := netlink.NetworkLinkAddVlan(masterDev, vlanDev, id); err != nil {
		return nil, err
	}

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

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

	return &VlanLink{
		Link: Link{
			ifc: vlanIfc,
		},
		masterIfc: masterIfc,
		id:        id,
	}, nil
}