Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Get of MPLS entries to RIB. #192

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions rib/rib.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
// - the timestamp in nanoseconds since the unix epoch that a function was performed.
// - a string indicating the name of the network instance
// - a ygot.ValidatedGoStruct containing the entry that has been changed.
type RIBHookFn func(constants.OpType, int64, string, ygot.ValidatedGoStruct)

Check failure on line 52 in rib/rib.go

View workflow job for this annotation

GitHub Actions / go / Static Analysis

type name will be used as rib.RIBHookFn by other packages, and that stutters; consider calling this HookFn

// ResolvedEntryFn is a function that is called for all entries that can be fully
// resolved within the RIB. Fully resolved in this case is defined as an input
Expand Down Expand Up @@ -1744,6 +1744,35 @@
}, nil
}

// concreteMPLSProto takes the input LabelEntry GoStruct and returns it as a gRIBI
// LabelEntryKey protobuf. It returns an error if the protobuf cannot be marshalled.
func concreteMPLSProto(e *aft.Afts_LabelEntry) (*aftpb.Afts_LabelEntryKey, error) {
mplsProto := &aftpb.Afts_LabelEntry{}
if err := protoFromGoStruct(e, &gpb.Path{
Elem: []*gpb.PathElem{{
Name: "afts",
}, {
Name: "mpls",
}, {
Name: "label-entry",
}},
}, mplsProto); err != nil {
return nil, fmt.Errorf("cannot marshal MPLS label %v, %v", e.GetLabel(), err)
}

l, ok := e.GetLabel().(aft.UnionUint32)
if !ok {
return nil, fmt.Errorf("cannot marshal MPLS label %v, incorrect type %T", e.GetLabel(), e.GetLabel())
}

return &aftpb.Afts_LabelEntryKey{
Label: &aftpb.Afts_LabelEntryKey_LabelUint64{
LabelUint64: uint64(l),
},
LabelEntry: mplsProto,
}, nil
}

// concreteNextHopProto takes the input NextHop GoStruct and returns it as a gRIBI
// NextHopEntryKey protobuf. It returns an error if the protobuf cannot be marshalled.
func concreteNextHopProto(e *aft.Afts_NextHop) (*aftpb.Afts_NextHopKey, error) {
Expand Down Expand Up @@ -1844,6 +1873,7 @@
if filter[spb.AFTType_ALL] {
filter = map[spb.AFTType]bool{
spb.AFTType_IPV4: true,
spb.AFTType_MPLS: true,
spb.AFTType_NEXTHOP: true,
spb.AFTType_NEXTHOP_GROUP: true,
}
Expand Down Expand Up @@ -1871,6 +1901,28 @@
}
}

if filter[spb.AFTType_MPLS] {
for lbl, e := range r.r.Afts.LabelEntry {
select {
case <-stopCh:
return nil
default:
p, err := concreteMPLSProto(e)
if err != nil {
return status.Errorf(codes.Internal, "cannot marshal MPLS entry for label %d into GetResponse, %v", lbl, err)
}
msgCh <- &spb.GetResponse{
Entry: []*spb.AFTEntry{{
NetworkInstance: r.name,
Entry: &spb.AFTEntry_Mpls{
Mpls: p,
},
}},
}
}
}
}

if filter[spb.AFTType_NEXTHOP_GROUP] {
for index, e := range r.r.Afts.NextHopGroup {
select {
Expand Down
117 changes: 116 additions & 1 deletion rib/rib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,61 @@ func TestConcreteIPv4Proto(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
got, err := concreteIPv4Proto(tt.inEntry)
if (err != nil) != tt.wantErr {
t.Fatalf("did not get expec ted error, got: %v, wantErr? %v", err, tt.wantErr)
t.Fatalf("did not get expected error, got: %v, wantErr? %v", err, tt.wantErr)
}
if diff := cmp.Diff(got, tt.want, protocmp.Transform(), cmpopts.EquateEmpty()); diff != "" {
t.Fatalf("did not get expected proto, diff(-got,+want):\n%s", diff)
}
})
}
}

func TestConcreteMPLSProto(t *testing.T) {
tests := []struct {
desc string
inEntry *aft.Afts_LabelEntry
want *aftpb.Afts_LabelEntryKey
wantErr bool
}{{
desc: "label only",
inEntry: &aft.Afts_LabelEntry{
Label: aft.UnionUint32(42),
},
want: &aftpb.Afts_LabelEntryKey{
Label: &aftpb.Afts_LabelEntryKey_LabelUint64{
LabelUint64: 42,
},
LabelEntry: &aftpb.Afts_LabelEntry{},
},
}, {
desc: "enumerated label",
inEntry: &aft.Afts_LabelEntry{
Label: aft.MplsTypes_MplsLabel_Enum_IPV4_EXPLICIT_NULL, // not allowed
},
wantErr: true,
}, {
desc: "label with metadata set",
inEntry: &aft.Afts_LabelEntry{
Label: aft.UnionUint32(42),
EntryMetadata: []byte{4, 3, 2, 1},
},
want: &aftpb.Afts_LabelEntryKey{
Label: &aftpb.Afts_LabelEntryKey_LabelUint64{
LabelUint64: 42,
},
LabelEntry: &aftpb.Afts_LabelEntry{
EntryMetadata: &wpb.BytesValue{
Value: []byte{4, 3, 2, 1},
},
},
},
}}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
got, err := concreteMPLSProto(tt.inEntry)
if (err != nil) != tt.wantErr {
t.Fatalf("did not get expected error, got: %v, wantErr? %v", err, tt.wantErr)
}
if diff := cmp.Diff(got, tt.want, protocmp.Transform(), cmpopts.EquateEmpty()); diff != "" {
t.Fatalf("did not get expected proto, diff(-got,+want):\n%s", diff)
Expand Down Expand Up @@ -2925,6 +2979,14 @@ func TestGetRIB(t *testing.T) {
t.Fatalf("cannot build RIB, %v", err)
}

cr = &aft.RIB{}
mpls := cr.GetOrCreateAfts().GetOrCreateLabelEntry(aft.UnionUint32(42))
mpls.NextHopGroup = ygot.Uint64(42)

if _, err := r.doAddMPLS(42, cr); err != nil {
t.Fatalf("cannot build RIB, %v", err)
}

return r
}()

Expand Down Expand Up @@ -2971,6 +3033,38 @@ func TestGetRIB(t *testing.T) {
},
}},
}},
}, {
desc: "mpls entry",
inRIB: func() *RIBHolder {
r := NewRIBHolder("VRF-1")

cr := &aft.RIB{}
mpls := cr.GetOrCreateAfts().GetOrCreateLabelEntry(aft.UnionUint32(42))
mpls.NextHopGroup = ygot.Uint64(42)

if _, err := r.doAddMPLS(42, cr); err != nil {
t.Fatalf("cannot build RIB, %v", err)
}
return r
}(),
inFilter: map[spb.AFTType]bool{
spb.AFTType_ALL: true,
},
wantResponses: []*spb.GetResponse{{
Entry: []*spb.AFTEntry{{
NetworkInstance: "VRF-1",
Entry: &spb.AFTEntry_Mpls{
Mpls: &aftpb.Afts_LabelEntryKey{
Label: &aftpb.Afts_LabelEntryKey_LabelUint64{
LabelUint64: 42,
},
LabelEntry: &aftpb.Afts_LabelEntry{
NextHopGroup: &wpb.UintValue{Value: 42},
},
},
},
}},
}},
}, {
desc: "next-hop-group entry",
inRIB: func() *RIBHolder {
Expand Down Expand Up @@ -3050,6 +3144,27 @@ func TestGetRIB(t *testing.T) {
},
}},
}},
}, {
desc: "all tables populated but filtered to mpls",
inRIB: allPopRIB,
inFilter: map[spb.AFTType]bool{
spb.AFTType_MPLS: true,
},
wantResponses: []*spb.GetResponse{{
Entry: []*spb.AFTEntry{{
NetworkInstance: "VRF-42",
Entry: &spb.AFTEntry_Mpls{
Mpls: &aftpb.Afts_LabelEntryKey{
Label: &aftpb.Afts_LabelEntryKey_LabelUint64{
LabelUint64: 42,
},
LabelEntry: &aftpb.Afts_LabelEntry{
NextHopGroup: &wpb.UintValue{Value: 42},
},
},
},
}},
}},
}, {
desc: "all tables populated but filtered to nhg",
inRIB: allPopRIB,
Expand Down
Loading