From 7accb36765361ca3ead6b025590404d59e879cac Mon Sep 17 00:00:00 2001 From: ty-dc Date: Mon, 15 Jan 2024 10:34:08 +0800 Subject: [PATCH] fix: the parent interface is down, set it to up before creating the vlan sub-interface Signed-off-by: ty-dc --- cmd/ifacer/cmd/cmd_add.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/ifacer/cmd/cmd_add.go b/cmd/ifacer/cmd/cmd_add.go index 587ecb8753..fb114445ee 100644 --- a/cmd/ifacer/cmd/cmd_add.go +++ b/cmd/ifacer/cmd/cmd_add.go @@ -164,6 +164,19 @@ func createBondDevice(conf *Ifacer) (*netlink.Bond, error) { func createVlanDevice(conf *Ifacer) error { var err error + // If the parent interface is down, we set it to up. + var parentLink netlink.Link + parentLink, err = netlink.LinkByName(conf.Interfaces[0]) + if err != nil { + return fmt.Errorf("failed to LinkByName %s: %w", conf.Interfaces[0], err) + } + + if parentLink.Attrs().Flags != net.FlagUp { + if err = netlink.LinkSetUp(parentLink); err != nil { + return fmt.Errorf("failed to set %s up: %v", parentLink.Attrs().Name, err) + } + } + var vlanLink netlink.Link vlanIfName := getVlanIfaceName(conf.Interfaces[0], conf.VlanID) vlanLink, err = netlink.LinkByName(vlanIfName) @@ -180,19 +193,6 @@ func createVlanDevice(conf *Ifacer) error { return fmt.Errorf("failed to LinkByName %s: %v", vlanIfName, err) } - // we create vlanif if it only not present - var parentLink netlink.Link - parentLink, err = netlink.LinkByName(conf.Interfaces[0]) - if err != nil { - return fmt.Errorf("failed to LinkByName %s: %w", conf.Interfaces[0], err) - } - - if parentLink.Attrs().Flags != net.FlagUp { - if err = netlink.LinkSetUp(parentLink); err != nil { - return fmt.Errorf("failed to set %s up: %v", parentLink.Attrs().Name, err) - } - } - // we only create if vlanIf not present return networking.LinkAdd(&netlink.Vlan{ LinkAttrs: netlink.LinkAttrs{