Skip to content

Commit

Permalink
Add MTU property as parameter of the Service
Browse files Browse the repository at this point in the history
Makes the MTU configurable in addition to the vlan ID and the base
interface.

Signed-off-by: Lionel Jouin <[email protected]>
  • Loading branch information
LionelJouin committed Oct 24, 2022
1 parent 55bd4c4 commit f44416c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
18 changes: 13 additions & 5 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
labelsPrefix = "labels:"
viaPrefix = "via:"
domainPrefix = "domain:"
mtuPrefix = "mtu:"

tcpSchema = "tcp"
)
Expand Down Expand Up @@ -74,15 +75,17 @@ type ServiceConfig struct {
Name string
Domain string
Via string
VLANTag int32
VLANTag uint32
Labels map[string]string
MTU uint32
}

// InitValues set initial values for ServiceConfig
func (s *ServiceConfig) InitValues() {
s.Domain = ""
s.VLANTag = 0
s.Via = ""
s.MTU = 0
}

// UnmarshalBinary expects string(bytes) to be in format:
Expand All @@ -102,13 +105,15 @@ func (s *ServiceConfig) UnmarshalBinary(bytes []byte) (err error) {
part = strings.TrimSpace(part)
switch {
case strings.HasPrefix(part, vlanPrefix):
s.VLANTag, err = parseInt32(trimPrefix(part, vlanPrefix))
s.VLANTag, err = parseUInt32(trimPrefix(part, vlanPrefix))
case strings.HasPrefix(part, labelsPrefix):
s.Labels, err = parseMap(trimPrefix(part, labelsPrefix))
case strings.HasPrefix(part, viaPrefix):
s.Via = trimPrefix(part, viaPrefix)
case strings.HasPrefix(part, domainPrefix):
s.Domain = trimPrefix(part, domainPrefix)
case strings.HasPrefix(part, mtuPrefix):
s.MTU, err = parseUInt32(trimPrefix(part, mtuPrefix))
default:
err = errors.Errorf("invalid format: %s", text)
}
Expand All @@ -124,12 +129,12 @@ func trimPrefix(s, prefix string) string {
return strings.TrimSpace(s)
}

func parseInt32(s string) (int32, error) {
i, err := strconv.ParseInt(s, 0, 32)
func parseUInt32(s string) (uint32, error) {
i, err := strconv.ParseUint(s, 0, 32)
if err != nil {
return 0, err
}
return int32(i), nil
return uint32(i), nil
}

func parseMap(s string) (map[string]string, error) {
Expand All @@ -154,5 +159,8 @@ func (s *ServiceConfig) validate() error {
if s.VLANTag < 0 || s.VLANTag > 4095 {
return errors.New("Invalid VLAN ID")
}
if s.MTU > 0 && (s.MTU < 68 || s.MTU > 65535) {
return errors.New("Invalid MTU")
}
return nil
}
4 changes: 3 additions & 1 deletion internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import (
func TestServiceConfig_UnmarshalBinary(t *testing.T) {
cfg := new(config.ServiceConfig)

err := cfg.UnmarshalBinary([]byte("finance-bridge { domain: service-domain.2; vlan: 100; via: gw-1 }"))
err := cfg.UnmarshalBinary([]byte("finance-bridge { domain: service-domain.2; vlan: 100; via: gw-1 ; mtu: 1450 }"))
require.NoError(t, err)

require.Equal(t, &config.ServiceConfig{
Name: "finance-bridge",
Domain: "service-domain.2",
Via: "gw-1",
VLANTag: 100,
MTU: 1450,
}, cfg)

err = cfg.UnmarshalBinary([]byte("finance-bridge { vlan: 200; via: service-domain.1 }"))
Expand All @@ -44,5 +45,6 @@ func TestServiceConfig_UnmarshalBinary(t *testing.T) {
Name: "finance-bridge",
Via: "service-domain.1",
VLANTag: 200,
MTU: 0,
}, cfg)
}
9 changes: 6 additions & 3 deletions internal/pkg/networkservice/vlanmapserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type vlanMapServer struct {
entries map[string]*entry
}
type entry struct {
vlanTag int32
vlanTag uint32
via string
mtu uint32
}

// NewServer - creates a NetworkServiceServer that requests a vlan interface and populates the netns inode
Expand All @@ -54,6 +55,7 @@ func NewServer(cfg *config.Config) networkservice.NetworkServiceServer {
v.entries[service.Name] = &entry{
vlanTag: service.VLANTag,
via: service.Via,
mtu: service.MTU,
}
}
return v
Expand All @@ -68,15 +70,16 @@ func (v *vlanMapServer) Request(ctx context.Context, request *networkservice.Net
}

if mechanism := vlan.ToMechanism(conn.GetMechanism()); mechanism != nil {
mechanism.SetVlanID(uint32(entry.vlanTag))
mechanism.SetVlanID(entry.vlanTag)

conn.Labels = make(map[string]string, 1)
conn.Labels[viaLabel] = entry.via
}
if request.GetConnection().GetContext() == nil {
request.GetConnection().Context = &networkservice.ConnectionContext{}
}
request.GetConnection().GetContext().MTU = 0
request.GetConnection().GetContext().MTU = entry.mtu

return next.Server(ctx).Request(ctx, request)
}

Expand Down

0 comments on commit f44416c

Please sign in to comment.