Skip to content

Commit

Permalink
RouteUpdate gains Flags field
Browse files Browse the repository at this point in the history
It allows to distinguish between a new created route or a replaced one.

Signed-off-by: Maxime Soulé <[email protected]>
  • Loading branch information
maxatome committed Nov 30, 2023
1 parent 0ced838 commit e8f9049
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
9 changes: 8 additions & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,15 @@ type flagString struct {
}

// RouteUpdate is sent when a route changes - type is RTM_NEWROUTE or RTM_DELROUTE

// Flags is only non-zero for RTM_NEWROUTE, the following flags can be set:
// - unix.NLM_F_REPLACE - Replace existing matching config object with this request
// - unix.NLM_F_EXCL - Don't replace the config object if it already exists
// - unix.NLM_F_CREATE - Create config object if it doesn't already exist
// - unix.NLM_F_APPEND - Add to the end of the object list
type RouteUpdate struct {
Type uint16
Type uint16
Flags uint16
Route
}

Expand Down
6 changes: 5 additions & 1 deletion route_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,11 @@ func routeSubscribeAt(newNs, curNs netns.NsHandle, ch chan<- RouteUpdate, done <
}
continue
}
ch <- RouteUpdate{Type: m.Header.Type, Route: route}
ch <- RouteUpdate{
Type: m.Header.Type,
Flags: m.Header.Flags & (unix.NLM_F_REPLACE | unix.NLM_F_EXCL | unix.NLM_F_CREATE | unix.NLM_F_APPEND),
Route: route,
}
}
}
}()
Expand Down
23 changes: 12 additions & 11 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,14 @@ func TestRouteAddIncomplete(t *testing.T) {
}
}

// expectNeighUpdate returns whether the expected updated is received within one minute.
func expectRouteUpdate(ch <-chan RouteUpdate, t uint16, dst net.IP) bool {
// expectRouteUpdate returns whether the expected updated is received within one minute.
func expectRouteUpdate(ch <-chan RouteUpdate, t, f uint16, dst net.IP) bool {
for {
timeout := time.After(time.Minute)
select {
case update := <-ch:
if update.Type == t &&
update.Flags == f &&
update.Route.Dst != nil &&
update.Route.Dst.IP.Equal(dst) {
return true
Expand Down Expand Up @@ -528,13 +529,13 @@ func TestRouteSubscribe(t *testing.T) {
t.Fatal(err)
}

if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, dst.IP) {
if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, unix.NLM_F_EXCL|unix.NLM_F_CREATE, dst.IP) {
t.Fatal("Add update not received as expected")
}
if err := RouteDel(&route); err != nil {
t.Fatal(err)
}
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, dst.IP) {
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, 0, dst.IP) {
t.Fatal("Del update not received as expected")
}
}
Expand Down Expand Up @@ -583,7 +584,7 @@ func TestRouteSubscribeWithOptions(t *testing.T) {
t.Fatal(err)
}

if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, dst.IP) {
if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, unix.NLM_F_EXCL|unix.NLM_F_CREATE, dst.IP) {
t.Fatal("Add update not received as expected")
}
}
Expand Down Expand Up @@ -635,13 +636,13 @@ func TestRouteSubscribeAt(t *testing.T) {
t.Fatal(err)
}

if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, dst.IP) {
if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, unix.NLM_F_EXCL|unix.NLM_F_CREATE, dst.IP) {
t.Fatal("Add update not received as expected")
}
if err := nh.RouteDel(&route); err != nil {
t.Fatal(err)
}
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, dst.IP) {
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, 0, dst.IP) {
t.Fatal("Del update not received as expected")
}
}
Expand Down Expand Up @@ -696,7 +697,7 @@ func TestRouteSubscribeListExisting(t *testing.T) {
t.Fatal(err)
}

if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, dst10.IP) {
if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, 0, dst10.IP) {
t.Fatal("Existing add update not received as expected")
}

Expand All @@ -711,19 +712,19 @@ func TestRouteSubscribeListExisting(t *testing.T) {
t.Fatal(err)
}

if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, dst.IP) {
if !expectRouteUpdate(ch, unix.RTM_NEWROUTE, unix.NLM_F_EXCL|unix.NLM_F_CREATE, dst.IP) {
t.Fatal("Add update not received as expected")
}
if err := nh.RouteDel(&route); err != nil {
t.Fatal(err)
}
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, dst.IP) {
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, 0, dst.IP) {
t.Fatal("Del update not received as expected")
}
if err := nh.RouteDel(&route10); err != nil {
t.Fatal(err)
}
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, dst10.IP) {
if !expectRouteUpdate(ch, unix.RTM_DELROUTE, 0, dst10.IP) {
t.Fatal("Del update not received as expected")
}
}
Expand Down

0 comments on commit e8f9049

Please sign in to comment.