Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(pkg/wireguard): remove duplicate link syncing, and cleanup #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 16 additions & 39 deletions pkg/wireguard/wireguard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wireguard

import (
"errors"
"fmt"
"net"
"os/exec"
Expand Down Expand Up @@ -88,7 +89,7 @@ func createLinkUsingUserspaceImpl(iface string, wgUserspaceImplementationFallbac

}

func createLinkUsingKernalModule(iface string) error {
func createLinkUsingKernelModule(iface string) error {
// link not created
wgLink := &netlink.GenericLink{
LinkAttrs: netlink.LinkAttrs{
Expand All @@ -104,49 +105,25 @@ func createLinkUsingKernalModule(iface string) error {
return nil
}

func SyncLink(_ agent.State, iface string, wgUserspaceImplementationFallback string, wgUseUserspaceImpl bool) error {
_, err := netlink.LinkByName(iface)
func SyncLink(state agent.State, iface string, wgUserspaceImplementationFallback string, wgUseUserspaceImpl bool) error {
link, err := netlink.LinkByName(iface)
if err != nil {
if _, ok := err.(netlink.LinkNotFoundError); !ok {
return err
}
}
if errors.As(err, &netlink.LinkNotFoundError{}) {
if wgUseUserspaceImpl {
if err := createLinkUsingUserspaceImpl(iface, wgUserspaceImplementationFallback); err != nil {
return fmt.Errorf("create link using user space impl: %w", err)
}

if _, ok := err.(netlink.LinkNotFoundError); ok {
if wgUseUserspaceImpl {
err = createLinkUsingUserspaceImpl(iface, wgUserspaceImplementationFallback)
} else if err := createLinkUsingKernelModule(iface); err != nil {
fmt.Printf("could not create link using kernel module, will attempt to fallback to user space implementation: %v\n", err)

if err != nil {
return err
// Fallback to user space implementation.
wgUseUserspaceImpl = true
}

} else {
err = createLinkUsingKernalModule(iface)

if err != nil {
err = createLinkUsingUserspaceImpl(iface, wgUserspaceImplementationFallback)

if err != nil {
return err
}
}
}

// TODO: Can this be removed?
link, err := netlink.LinkByName(iface)
if err != nil {
return err
}
if err := netlink.LinkSetUp(link); err != nil {
return err
}
}

link, err := netlink.LinkByName(iface)
if err != nil {
if _, ok := err.(netlink.LinkNotFoundError); !ok {
return err
return SyncLink(state, iface, wgUserspaceImplementationFallback, wgUseUserspaceImpl)
}
return fmt.Errorf("link by name: %w", err)
}

addresses, err := netlink.AddrList(link, syscall.AF_INET)
Expand All @@ -165,7 +142,7 @@ func SyncLink(_ agent.State, iface string, wgUserspaceImplementationFallback str
}

if err := netlink.LinkSetUp(link); err != nil {
return err
return fmt.Errorf("link set up: %w", err)
}
return nil
}
Expand Down
Loading