Skip to content

Commit

Permalink
Create LsBgpPeerSegmentSID struct
Browse files Browse the repository at this point in the history
Co-authored-by: watal <watal.i27e@gmail.com>
Co-authored-by: kanaya516 <abcs516tk1@gmail.com>
  • Loading branch information
3 people committed Jan 10, 2023
1 parent 37f04b3 commit 82aebc2
Showing 1 changed file with 123 additions and 25 deletions.
148 changes: 123 additions & 25 deletions pkg/packet/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5894,7 +5894,6 @@ func NewLsAttributeTLVs(lsAttr *LsAttribute) []LsTLVInterface {
}

if lsAttr.BgpPeerSegment.BgpPeerNodeSid != nil {
// TODO
tlvs = append(tlvs, NewLsTLVPeerNodeSID(lsAttr.BgpPeerSegment.BgpPeerNodeSid))
}
if lsAttr.BgpPeerSegment.BgpPeerAdjacencySid != nil {
Expand Down Expand Up @@ -7954,23 +7953,75 @@ func (l *LsTLVAdjacencySID) MarshalJSON() ([]byte, error) {
})
}

// https://tools.ietf.org/html/rfc9086#section-5
type LsAttributeBgpPeerSegmentSIDFlags struct {
Value bool `json:"value"`
Local bool `json:"local"`
Backup bool `json:"backup"`
Persistent bool `json:"persistent"`
}

func NewBgpPeerSegmentSIDFlag(v uint8) LsAttributeBgpPeerSegmentSIDFlags {
var flags LsAttributeBgpPeerSegmentSIDFlags
if (v & (1 >> 7)) == 1 {
flags.Value = true
}
if (v & (1 >> 6)) == 1 {
flags.Local = true
}
if (v & (1 >> 5)) == 1 {
flags.Backup = true
}
if (v & (1 >> 4)) == 1 {
flags.Persistent = true
}
return flags
}

type LsBgpPeerSegmentSID struct {
Flags LsAttributeBgpPeerSegmentSIDFlags `json:"flags"`
Weight uint8 `json:"weight"`
SID uint32 `json:"sid"`
}

type LsTLVPeerNodeSID struct {
LsTLV
Flags uint8
Weight uint8
SID uint32
}

func NewLsTLVPeerNodeSID(l *uint32) *LsTLVPeerNodeSID {
func NewLsTLVPeerNodeSID(l *LsBgpPeerSegmentSID) *LsTLVPeerNodeSID {
var flags uint8
if l.Flags.Value {
flags = flags & (1 >> 7)
}
if l.Flags.Local {
flags = flags & (1 >> 6)
}
if l.Flags.Backup {
flags = flags & (1 >> 5)
}
if l.Flags.Persistent {
flags = flags & (1 >> 4)
}

// https://tools.ietf.org/html/rfc9086#section-5
var sidlen uint16
if l.Flags.Value {
sidlen = 7
} else {
sidlen = 8
}

return &LsTLVPeerNodeSID{
LsTLV: LsTLV{
Type: BGP_ASPATH_ATTR_TYPE_SET,
Length: 7, // TODO
Length: sidlen,
},
Flags: flags, // TODO
Weight: 0, // TODO
SID: *l,
Flags: flags,
Weight: l.Weight,
SID: l.SID,
}
}

Expand Down Expand Up @@ -8045,16 +8096,37 @@ type LsTLVPeerAdjacencySID struct {
SID uint32
}

func NewLsTLVPeerAdjacencySID(l *uint32) *LsTLVPeerAdjacencySID {
func NewLsTLVPeerAdjacencySID(l *LsBgpPeerSegmentSID) *LsTLVPeerNodeSID {
var flags uint8
return &LsTLVPeerAdjacencySID{
if l.Flags.Value {
flags = flags & (1 >> 7)
}
if l.Flags.Local {
flags = flags & (1 >> 6)
}
if l.Flags.Backup {
flags = flags & (1 >> 5)
}
if l.Flags.Persistent {
flags = flags & (1 >> 4)
}

// https://tools.ietf.org/html/rfc9086#section-5
var sidlen uint16
if l.Flags.Value {
sidlen = 7
} else {
sidlen = 8
}

return &LsTLVPeerNodeSID{
LsTLV: LsTLV{
Type: BGP_ASPATH_ATTR_TYPE_SET,
Length: 7, // TODO
Length: sidlen,
},
Flags: flags, // TODO
Weight: 0, // TODO
SID: *l,
Flags: flags,
Weight: l.Weight,
SID: l.SID,
}
}

Expand Down Expand Up @@ -8129,16 +8201,37 @@ type LsTLVPeerSetSID struct {
SID uint32
}

func NewLsTLVPeerSetSID(l *uint32) *LsTLVPeerSetSID {
func NewLsTLVPeerSetSID(l *LsBgpPeerSegmentSID) *LsTLVPeerNodeSID {
var flags uint8
return &LsTLVPeerSetSID{
if l.Flags.Value {
flags = flags & (1 >> 7)
}
if l.Flags.Local {
flags = flags & (1 >> 6)
}
if l.Flags.Backup {
flags = flags & (1 >> 5)
}
if l.Flags.Persistent {
flags = flags & (1 >> 4)
}

// https://tools.ietf.org/html/rfc9086#section-5
var sidlen uint16
if l.Flags.Value {
sidlen = 7
} else {
sidlen = 8
}

return &LsTLVPeerNodeSID{
LsTLV: LsTLV{
Type: BGP_ASPATH_ATTR_TYPE_SET,
Length: 7, // TODO
Length: sidlen,
},
Flags: flags, // TODO
Weight: 0, // TODO
SID: *l,
Flags: flags,
Weight: l.Weight,
SID: l.SID,
}
}

Expand Down Expand Up @@ -9056,10 +9149,9 @@ type LsAttributePrefix struct {
}

type LsAttributeBgpPeerSegment struct {
// TODO flag
BgpPeerNodeSid *uint32 `json:"bgp_peer_node_sid,omitempty"`
BgpPeerAdjacencySid *uint32 `json:"bgp_peer_adjacency_sid,omitempty"`
BgpPeerSetSid *uint32 `json:"bgp_peer_set_sid,omitempty"`
BgpPeerNodeSid *LsBgpPeerSegmentSID `json:"bgp_peer_node_sid,omitempty"`
BgpPeerAdjacencySid *LsBgpPeerSegmentSID `json:"bgp_peer_adjacency_sid,omitempty"`
BgpPeerSetSid *LsBgpPeerSegmentSID `json:"bgp_peer_set_sid,omitempty"`
}

type LsAttribute struct {
Expand Down Expand Up @@ -9154,13 +9246,19 @@ func (p *PathAttributeLs) Extract() *LsAttribute {
l.Prefix.SrPrefixSID = &v.SID

case *LsTLVPeerNodeSID:
l.BgpPeerSegment.BgpPeerNodeSid = &v.SID
l.BgpPeerSegment.BgpPeerNodeSid.Flags = NewBgpPeerSegmentSIDFlag(v.Flags)
l.BgpPeerSegment.BgpPeerNodeSid.Weight = v.Weight
l.BgpPeerSegment.BgpPeerNodeSid.SID = v.SID

case *LsTLVPeerAdjacencySID:
l.BgpPeerSegment.BgpPeerAdjacencySid = &v.SID
l.BgpPeerSegment.BgpPeerAdjacencySid.Flags = NewBgpPeerSegmentSIDFlag(v.Flags)
l.BgpPeerSegment.BgpPeerAdjacencySid.Weight = v.Weight
l.BgpPeerSegment.BgpPeerAdjacencySid.SID = v.SID

case *LsTLVPeerSetSID:
l.BgpPeerSegment.BgpPeerSetSid = &v.SID
l.BgpPeerSegment.BgpPeerSetSid.Flags = NewBgpPeerSegmentSIDFlag(v.Flags)
l.BgpPeerSegment.BgpPeerSetSid.Weight = v.Weight
l.BgpPeerSegment.BgpPeerSetSid.SID = v.SID
}
}

Expand Down

0 comments on commit 82aebc2

Please sign in to comment.