From 67010837c60d5905fc65b34da7f4db2ad889bf36 Mon Sep 17 00:00:00 2001 From: Oilbeater Date: Mon, 25 Sep 2023 14:03:40 +0800 Subject: [PATCH] fix: for existing nic, no need to set the port type to internal (#3243) Signed-off-by: oilbeater --- pkg/daemon/ovs.go | 67 +++++++++++++++++++++++++++++---------- pkg/daemon/ovs_linux.go | 11 +++++++ pkg/daemon/ovs_windows.go | 8 +++++ 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/pkg/daemon/ovs.go b/pkg/daemon/ovs.go index 98c98575752..43e521df662 100644 --- a/pkg/daemon/ovs.go +++ b/pkg/daemon/ovs.go @@ -44,29 +44,64 @@ func pingGateway(gw, src string, verbose bool, maxRetry int) error { } func configureGlobalMirror(portName string, mtu int) error { - raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--", - "set", "interface", portName, "type=internal", "--", - "clear", "bridge", "br-int", "mirrors", "--", - "--id=@mirror0", "get", "port", portName, "--", - "--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "select_all=true", "output_port=@mirror0", "--", - "add", "bridge", "br-int", "mirrors", "@m") + nicExist, err := linkExists(portName) if err != nil { - klog.Errorf("failed to configure mirror nic %s %q", portName, raw) - return fmt.Errorf(raw) + return err } + + if !nicExist { + raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--", + "set", "interface", portName, "type=internal", "--", + "clear", "bridge", "br-int", "mirrors", "--", + "--id=@mirror0", "get", "port", portName, "--", + "--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "select_all=true", "output_port=@mirror0", "--", + "add", "bridge", "br-int", "mirrors", "@m") + if err != nil { + klog.Errorf("failed to configure mirror nic %s %q", portName, raw) + return fmt.Errorf(raw) + } + } else { + raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--", + "clear", "bridge", "br-int", "mirrors", "--", + "--id=@mirror0", "get", "port", portName, "--", + "--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "select_all=true", "output_port=@mirror0", "--", + "add", "bridge", "br-int", "mirrors", "@m") + if err != nil { + klog.Errorf("failed to configure mirror nic %s %q", portName, raw) + return fmt.Errorf(raw) + } + } + return configureMirrorLink(portName, mtu) } func configureEmptyMirror(portName string, mtu int) error { - raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--", - "set", "interface", portName, "type=internal", "--", - "clear", "bridge", "br-int", "mirrors", "--", - "--id=@mirror0", "get", "port", portName, "--", - "--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "output_port=@mirror0", "--", - "add", "bridge", "br-int", "mirrors", "@m") + nicExist, err := linkExists(portName) if err != nil { - klog.Errorf("failed to configure mirror nic %s %q", portName, raw) - return fmt.Errorf(raw) + return err + } + + if !nicExist { + raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--", + "set", "interface", portName, "type=internal", "--", + "clear", "bridge", "br-int", "mirrors", "--", + "--id=@mirror0", "get", "port", portName, "--", + "--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "output_port=@mirror0", "--", + "add", "bridge", "br-int", "mirrors", "@m") + if err != nil { + klog.Errorf("failed to configure mirror nic %s %q", portName, raw) + return fmt.Errorf(raw) + } + } else { + raw, err := ovs.Exec(ovs.MayExist, "add-port", "br-int", portName, "--", + "clear", "bridge", "br-int", "mirrors", "--", + "--id=@mirror0", "get", "port", portName, "--", + "--id=@m", "create", "mirror", fmt.Sprintf("name=%s", util.MirrorDefaultName), "output_port=@mirror0", "--", + "add", "bridge", "br-int", "mirrors", "@m") + if err != nil { + klog.Errorf("failed to configure mirror nic %s %q", portName, raw) + return fmt.Errorf(raw) + } } return configureMirrorLink(portName, mtu) } diff --git a/pkg/daemon/ovs_linux.go b/pkg/daemon/ovs_linux.go index 5e284379237..c300dfed022 100644 --- a/pkg/daemon/ovs_linux.go +++ b/pkg/daemon/ovs_linux.go @@ -1167,3 +1167,14 @@ func turnOffNicTxChecksum(nicName string) (err error) { func getShortSharedDir(uid types.UID, volumeName string) string { return filepath.Join(util.DefaultHostVhostuserBaseDir, string(uid), volumeName) } + +func linkExists(name string) (bool, error) { + _, err := netlink.LinkByName(name) + if err == nil { + return true, nil + } else if _, ok := err.(netlink.LinkNotFoundError); ok { + return false, nil + } else { + return false, err + } +} diff --git a/pkg/daemon/ovs_windows.go b/pkg/daemon/ovs_windows.go index 87a75d105ef..8a5d03087e2 100644 --- a/pkg/daemon/ovs_windows.go +++ b/pkg/daemon/ovs_windows.go @@ -333,3 +333,11 @@ func getShortSharedDir(uid types.UID, volumeName string) string { // DPDK is not supported on Windows return "" } + +func linkExists(name string) (bool, error) { + _, err := util.GetNetAdapter(name, true) + if err != nil { + return false, nil + } + return true, nil +}