Skip to content

Commit

Permalink
Make it possible to manually configure VLAN
Browse files Browse the repository at this point in the history
  • Loading branch information
simcod committed Jul 4, 2024
1 parent 782fd9d commit b343231
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ type Config struct {
GrpcCACertFile string `required:"false" desc:"the gRPC CA certificate file" envconfig:"grpc_ca_cert_file"`
GrpcClientCertFile string `required:"false" desc:"the gRPC client certificate file" envconfig:"grpc_client_cert_file"`
GrpcClientKeyFile string `required:"false" desc:"the gRPC client key file" envconfig:"grpc_client_key_file"`
Vlan uint16 `required:"false" desc:"the configuration of the vlan" envconfig:"vlan"`
}
5 changes: 5 additions & 0 deletions cmd/internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Core struct {
eventServiceClient v1.EventServiceClient

metrics *metrics.Metrics

vlan uint16
}

type Config struct {
Expand All @@ -56,6 +58,8 @@ type Config struct {
EventServiceClient v1.EventServiceClient

Metrics *metrics.Metrics

Vlan uint16
}

func New(c Config) *Core {
Expand All @@ -77,5 +81,6 @@ func New(c Config) *Core {
driver: c.Driver,
eventServiceClient: c.EventServiceClient,
metrics: c.Metrics,
vlan: c.Vlan,
}
}
16 changes: 12 additions & 4 deletions cmd/internal/core/reconfigure-switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,24 @@ func (c *Core) buildSwitcherConfig(s *models.V1SwitchResponse) (*types.Conf, err
if err != nil {
return nil, err
}

m, err := vlan.ReadMapping()
if err != nil {
return nil, err
}

if c.vlan >= vlan.VlanIDMin && c.vlan <= vlan.VlanIDMax {
return nil, fmt.Errorf("configured VLAN is in the reserved area of %v, %v", vlan.VlanIDMin, vlan.VlanIDMax)
}

switcherConfig := &types.Conf{
Name: s.Name,
LogLevel: mapLogLevel(c.logLevel),
ASN: asn,
Loopback: c.loopbackIP,
MetalCoreCIDR: c.cidr,
AdditionalBridgeVIDs: c.additionalBridgeVIDs,
Vlan: c.vlan,
}

p := types.Ports{
Expand Down Expand Up @@ -187,10 +198,7 @@ func (c *Core) buildSwitcherConfig(s *models.V1SwitchResponse) (*types.Conf, err

c.nos.SanitizeConfig(switcherConfig)
switcherConfig.FillRouteMapsAndIPPrefixLists()
m, err := vlan.ReadMapping()
if err != nil {
return nil, err
}

err = switcherConfig.FillVLANIDs(m)
if err != nil {
return nil, err
Expand Down
9 changes: 6 additions & 3 deletions cmd/internal/switcher/sonic/redis/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log/slog"
"strconv"
"time"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (a *Applier) Apply(cfg *types.Conf) error {
}

for _, interfaceName := range cfg.Ports.Unprovisioned {
if err := a.configureUnprovisionedPort(interfaceName, !cfg.Ports.DownPorts[interfaceName]); err != nil {
if err := a.configureUnprovisionedPort(interfaceName, !cfg.Ports.DownPorts[interfaceName], cfg.Vlan); err != nil {
errs = append(errs, err)
}
}
Expand Down Expand Up @@ -115,7 +116,7 @@ func (a *Applier) refreshOidMaps() error {
return nil
}

func (a *Applier) configureUnprovisionedPort(interfaceName string, isUp bool) error {
func (a *Applier) configureUnprovisionedPort(interfaceName string, isUp bool, vlan uint16) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand All @@ -129,7 +130,9 @@ func (a *Applier) configureUnprovisionedPort(interfaceName string, isUp bool) er
return fmt.Errorf("failed to update Port info for interface %s: %w", interfaceName, err)
}

return a.ensureInterfaceIsVlanMember(ctx, interfaceName, "Vlan4000")
vl := "Vlan" + strconv.FormatUint(uint64(vlan), 10)

return a.ensureInterfaceIsVlanMember(ctx, interfaceName, vl)
}

func (a *Applier) configureFirewallPort(interfaceName string, isUp bool) error {
Expand Down
1 change: 1 addition & 0 deletions cmd/internal/switcher/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Conf struct {
Ports Ports
MetalCoreCIDR string
AdditionalBridgeVIDs []string
Vlan uint16
}

type Ports struct {
Expand Down
6 changes: 3 additions & 3 deletions cmd/internal/vlan/reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import "fmt"

const (
// vlanIDMin specifies the min VLAN-ID we want to use on our switches
vlanIDMin uint16 = 1001
VlanIDMin uint16 = 1001

// vlanIDMax specifies the max VLAN-ID we want to use on our switches
vlanIDMax uint16 = 2000
VlanIDMax uint16 = 2000
)

// ReserveVlanIDs tries to reserve n VLAN-IDs given the current switch configuration
func (m Mapping) ReserveVlanIDs(n uint16) ([]uint16, error) {
return m.reserveVlanIDs(vlanIDMin, vlanIDMax, n)
return m.reserveVlanIDs(VlanIDMin, VlanIDMax, n)
}

func (m Mapping) reserveVlanIDs(min, max, n uint16) ([]uint16, error) {
Expand Down
1 change: 1 addition & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func Run() {
Driver: driver,
EventServiceClient: grpcClient.NewEventClient(),
Metrics: metrics,
Vlan: cfg.Vlan,
})

err = c.RegisterSwitch()
Expand Down

0 comments on commit b343231

Please sign in to comment.