Skip to content

Commit

Permalink
Merge pull request #1 from Enigamict/ra_mtu_support
Browse files Browse the repository at this point in the history
Adding the RA MTU option
  • Loading branch information
YutaroHayakawa authored May 18, 2024
2 parents af91f05 + a3367ed commit 0def055
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ declarative configuration interface on top of it.

- Basic RA mechanism defined in RFC4861
- Router MAC address discovery with Source Link Layer Address option
- TBD: MTU discovery with MTU option
- MTU discovery with MTU option
- Stateless Address Auto Configuration (SLAAC) with Prefix Information option
- TBD: DNS auto configuration with RDNSS option

Expand Down
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ type InterfaceConfig struct {
// this router.
RetransmitTimeMilliseconds int `yaml:"retransmitTimeMilliseconds" json:"retransmitTimeMilliseconds" validate:"gte=0,lte=4294967295"`

// The maximum transmission unit (MTU) that should be used for outgoing
// This value specifies the largest packet size, in bytes,
// If set to zero or not specified, MTU opton will not be advertised
MTU int `yaml:"mtu" json:"mtu" validate:"gte=0,lte=4294967295"`

// Prefix-specific configuration parameters. The prefix fields must be
// non-overlapping with each other. The slice itself and elements must
// not be nil.
Expand Down
30 changes: 30 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,36 @@ func TestConfigValidation(t *testing.T) {
errorField: "RetransmitTimeMilliseconds",
errorTag: "lte",
},
{
name: "MTU > 0",
config: &Config{
Interfaces: []*InterfaceConfig{
{
Name: "net0",
RAIntervalMilliseconds: 1000,
MTU: -1,
},
},
},
expectError: true,
errorField: "MTU",
errorTag: "gte",
},
{
name: "MTU > 4294967295",
config: &Config{
Interfaces: []*InterfaceConfig{
{
Name: "net0",
RAIntervalMilliseconds: 1000,
MTU: 4294967296,
},
},
},
expectError: true,
errorField: "MTU",
errorTag: "lte",
},
{
name: "Nil PrefixConfig",
config: &Config{
Expand Down
16 changes: 14 additions & 2 deletions daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ outer:
}
}

// Ensure the interval is correct. We let 5ms of error margin.
mergin := float64(5 * time.Millisecond)
// Ensure the interval is correct. We let 60ms of error margin.
mergin := float64(60 * time.Millisecond)
diff0 := ras[1].tstamp.Sub(ras[0].tstamp)
diff1 := ras[2].tstamp.Sub(ras[1].tstamp)

Expand All @@ -59,6 +59,7 @@ func TestDaemonHappyPath(t *testing.T) {
RouterLifetimeSeconds: 10,
ReachableTimeMilliseconds: 10000,
RetransmitTimeMilliseconds: 10000,
MTU: 1500,
},
{
Name: "net1",
Expand Down Expand Up @@ -112,6 +113,17 @@ func TestDaemonHappyPath(t *testing.T) {
require.Equal(t, time.Second*10, ra.msg.RouterLifetime)
require.Equal(t, time.Millisecond*10000, ra.msg.ReachableTime)
require.Equal(t, time.Millisecond*10000, ra.msg.RetransmitTimer)

// Find MTU option
var mtuOption *ndp.MTU
for _, option := range ra.msg.Options {
if opt, ok := option.(*ndp.MTU); ok {
mtuOption = opt
break
}
}
require.NotNil(t, mtuOption, "MTU option is not advertised")
require.Equal(t, uint32(1500), mtuOption.MTU, "Invalid MTU")
})

t.Run("Ensure the status is running and the result is ordered by name", func(t *testing.T) {
Expand Down
23 changes: 17 additions & 6 deletions ra_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ func newRASender(initialConfig *InterfaceConfig, ctor rAdvSocketCtor, logger *sl
}
}

func (s *raSender) setOptions(config *InterfaceConfig) []ndp.Option {
options := []ndp.Option{
&ndp.LinkLayerAddress{
Direction: ndp.Source,
Addr: s.sock.hardwareAddr(),
},
}

if config.MTU > 0 {
options = append(options, &ndp.MTU{
MTU: uint32(config.MTU),
})
}
return options
}

func (s *raSender) reportRunning() {
s.statusLock.Lock()
defer s.statusLock.Unlock()
Expand Down Expand Up @@ -157,12 +173,7 @@ reload:
RouterLifetime: time.Duration(config.RouterLifetimeSeconds) * time.Second,
ReachableTime: time.Duration(config.ReachableTimeMilliseconds) * time.Millisecond,
RetransmitTimer: time.Duration(config.RetransmitTimeMilliseconds) * time.Millisecond,
Options: []ndp.Option{
&ndp.LinkLayerAddress{
Direction: ndp.Source,
Addr: s.sock.hardwareAddr(),
},
},
Options: s.setOptions(config),
}

for _, prefix := range config.Prefixes {
Expand Down

0 comments on commit 0def055

Please sign in to comment.