Skip to content

Commit

Permalink
Implementn vlan:pub support
Browse files Browse the repository at this point in the history
- pub vlan is supported in both single and dual nic setup
- delete some dead code
  • Loading branch information
muhamadazmy committed Oct 18, 2023
1 parent ab00dfc commit 5948c2f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 220 deletions.
4 changes: 3 additions & 1 deletion cmds/modules/networkd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/pkg/errors"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/network/dhcp"
"github.com/threefoldtech/zos/pkg/network/public"
"github.com/threefoldtech/zos/pkg/network/types"
Expand Down Expand Up @@ -98,8 +99,9 @@ func action(cli *cli.Context) error {
if err != nil && err != public.ErrNoPublicConfig {
return errors.Wrap(err, "failed to get node public_config")
}

// EnsurePublicSetup knows how to handle a nil pub (in case of ErrNoPublicConfig)
master, err := public.EnsurePublicSetup(nodeID, pub)
master, err := public.EnsurePublicSetup(nodeID, environment.MustGet().PubVlan, pub)
if err != nil {
return errors.Wrap(err, "failed to setup public bridge")
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import (
//go:generate mkdir -p stubs
//go:generate zbusc -module network -version 0.0.1 -name network -package stubs github.com/threefoldtech/zos/pkg+Networker stubs/network_stub.go

// Member holds information about a the network namespace of a container
type Member struct {
Namespace string
IPv6 net.IP
IPv4 net.IP
YggdrasilIP net.IP
}

// ContainerNetworkConfig defines how to construct the network namespace of a container
type ContainerNetworkConfig struct {
IPs []string
Expand Down
28 changes: 22 additions & 6 deletions pkg/network/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func vethName(from, to string) string {
// can be directly plugged or crossed over with a veth pair
// if name is provided, the name will be used in case of veth pair instead of
// a generated name
func Attach(link netlink.Link, bridge *netlink.Bridge, name ...string) error {
func Attach(link netlink.Link, bridge *netlink.Bridge, vlan *uint16, name ...string) error {
if link.Type() == "device" {
return AttachNic(link, bridge)
return attachNic(link, bridge, vlan)
} else if link.Type() == "bridge" {
linkBr := link.(*netlink.Bridge)
n := vethName(link.Attrs().Name, bridge.Name)
Expand All @@ -116,14 +116,14 @@ func Attach(link netlink.Link, bridge *netlink.Bridge, name ...string) error {
return err
}

return AttachNic(veth, linkBr)
return attachNic(veth, linkBr, vlan)
}

return fmt.Errorf("unsupported link type '%s'", link.Type())
}

// AttachNic attaches an interface to a bridge
func AttachNic(link netlink.Link, bridge *netlink.Bridge) error {
// attachNic attaches an interface to a bridge
func attachNic(link netlink.Link, bridge *netlink.Bridge, vlan *uint16) error {
// Jan said this was fine
if err := netlink.LinkSetUp(link); err != nil {
return errors.Wrap(err, "could not set veth peer up")
Expand All @@ -133,7 +133,23 @@ func AttachNic(link netlink.Link, bridge *netlink.Bridge) error {
if err := options.Set(link.Attrs().Name, options.IPv6Disable(true)); err != nil {
return errors.Wrap(err, "failed to disable ipv6 on link interface")
}
return netlink.LinkSetMaster(link, bridge)
if err := netlink.LinkSetMaster(link, bridge); err != nil {
return errors.Wrapf(err, "failed to attach link %s to bridge %s", link.Attrs().Name, bridge.Name)
}

if vlan == nil {
return nil
}

if err := netlink.BridgeVlanDel(link, 1, true, true, false, false); err != nil {
return errors.Wrapf(err, "failed to delete default vlan tag on device '%s'", link.Attrs().Name)
}

if err := netlink.BridgeVlanAdd(link, *vlan, true, true, false, false); err != nil {
return errors.Wrapf(err, "failed to set vlan on device '%s'", link.Attrs().Name)
}

return nil
}

// List all nics attached to a bridge
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestAttachBridge(t *testing.T) {
_ = netlink.LinkDel(dummy)
}()

err = AttachNic(dummy, br)
err = attachNic(dummy, br, nil)
assert.NoError(t, err)
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/network/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/blang/semver"

"github.com/threefoldtech/zos/pkg/cache"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/zos/pkg/gridtypes/zos"
"github.com/threefoldtech/zos/pkg/network/bootstrap"
Expand Down Expand Up @@ -932,7 +933,7 @@ func (n *networker) Namespace(id zos.NetID) string {

func (n *networker) UnsetPublicConfig() error {
id := n.identity.NodeID(context.Background())
_, err := public.EnsurePublicSetup(id, nil)
_, err := public.EnsurePublicSetup(id, environment.MustGet().PubVlan, nil)
return err
}

Expand All @@ -953,7 +954,7 @@ func (n *networker) SetPublicConfig(cfg pkg.PublicConfig) error {
}

id := n.identity.NodeID(context.Background())
_, err = public.EnsurePublicSetup(id, &cfg)
_, err = public.EnsurePublicSetup(id, environment.MustGet().PubVlan, &cfg)
if err != nil {
return errors.Wrap(err, "failed to apply public config")
}
Expand Down
195 changes: 0 additions & 195 deletions pkg/network/nr/container.go

This file was deleted.

15 changes: 8 additions & 7 deletions pkg/network/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/zos/pkg/network/bootstrap"
"github.com/threefoldtech/zos/pkg/network/bridge"
Expand Down Expand Up @@ -95,7 +96,7 @@ func IPs() ([]net.IPNet, error) {
return ips, err
}

func setupPublicBridge(br *netlink.Bridge) error {
func setupPublicBridge(br *netlink.Bridge, vlan *uint16) error {
exit, err := detectExitNic()
if err != nil {
return errors.Wrap(err, "failed to find possible exit")
Expand All @@ -107,15 +108,15 @@ func setupPublicBridge(br *netlink.Bridge) error {
return errors.Wrapf(err, "failed to get link '%s' by name", exit)
}

return attachPublicToExit(br, exitLink)
return attachPublicToExit(br, exitLink, vlan)
}

func attachPublicToExit(br *netlink.Bridge, exit netlink.Link) error {
func attachPublicToExit(br *netlink.Bridge, exit netlink.Link, vlan *uint16) error {
if err := netlink.LinkSetUp(exit); err != nil {
return errors.Wrapf(err, "failed to set link '%s' up", exit.Attrs().Name)
}

if err := bridge.Attach(exit, br, toZosVeth); err != nil {
if err := bridge.Attach(exit, br, vlan, toZosVeth); err != nil {
return errors.Wrap(err, "failed to attach exit nic to public bridge 'br-pub'")
}

Expand Down Expand Up @@ -212,7 +213,7 @@ func SetPublicExitLink(link netlink.Link) error {
}
}

return attachPublicToExit(br, link)
return attachPublicToExit(br, link, environment.MustGet().PubVlan)
}

func HasPublicSetup() bool {
Expand Down Expand Up @@ -301,7 +302,7 @@ func GetPublicSetup() (pkg.PublicConfig, error) {
//
// if no nic is found zos is selected.
// changes to the br-pub exit nic can then be done later with SetPublicExitLink
func EnsurePublicSetup(nodeID pkg.Identifier, inf *pkg.PublicConfig) (*netlink.Bridge, error) {
func EnsurePublicSetup(nodeID pkg.Identifier, vlan *uint16, inf *pkg.PublicConfig) (*netlink.Bridge, error) {
log.Debug().Msg("ensure public setup")
br, err := ensurePublicBridge()
if err != nil {
Expand All @@ -312,7 +313,7 @@ func EnsurePublicSetup(nodeID pkg.Identifier, inf *pkg.PublicConfig) (*netlink.B
if os.IsNotExist(err) {
// bridge is not initialized, wire it.
log.Debug().Msg("no public bridge uplink found, setting up...")
if err := setupPublicBridge(br); err != nil {
if err := setupPublicBridge(br, vlan); err != nil {
return nil, err
}
} else if err != nil {
Expand Down

0 comments on commit 5948c2f

Please sign in to comment.