Skip to content

Commit

Permalink
Add TLV definition related to association object
Browse files Browse the repository at this point in the history
  • Loading branch information
Motok1 committed Jun 8, 2024
1 parent 27753ad commit 946196d
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 53 deletions.
64 changes: 11 additions & 53 deletions pkg/packet/pcep/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,59 +1140,17 @@ func NewAssociationObject(srcAddr netip.Addr, dstAddr netip.Addr, color uint32,
} else {
o.AssocID = 1 // (I.D. pce-segment-routing-policy-cp-07 5.1)
o.AssocType = ASSOC_TYPE_SR_POLICY_ASSOCIATION // (I.D. pce-segment-routing-policy-cp-07 5.1)
var associationObjectTLVs []TLVInterface
if srcAddr.Is4() {
associationObjectTLVs = []TLVInterface{
&UndefinedTLV{
Typ: TLV_EXTENDED_ASSOCIATION_ID,
Length: TLV_EXTENDED_ASSOCIATION_ID_IPV4_LENGTH,
Value: AppendByteSlices(
Uint32ToByteSlice(color), dstAddr.AsSlice(),
),
},
&UndefinedTLV{
Typ: TLV_SRPOLICY_CPATH_ID,
Length: TLV_SRPOLICY_CPATH_ID_LENGTH,
Value: []uint8{
0x00, // protocol origin
0x00, 0x00, 0x00, // mbz
0x00, 0x00, 0x00, 0x00, // Originator ASN
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Originator Address
0x00, 0x00, 0x00, 0x00, //discriminator
},
},
&UndefinedTLV{
Typ: TLV_SRPOLICY_CPATH_PREFERENCE,
Length: TLV_SRPOLICY_CPATH_PREFERENCE_LENGTH,
Value: Uint32ToByteSlice(preference),
},
}
} else if srcAddr.Is6() {
associationObjectTLVs = []TLVInterface{
&UndefinedTLV{
Typ: TLV_EXTENDED_ASSOCIATION_ID,
Length: TLV_EXTENDED_ASSOCIATION_ID_IPV6_LENGTH,
Value: AppendByteSlices(
Uint32ToByteSlice(color), dstAddr.AsSlice(),
),
},
&UndefinedTLV{
Typ: TLV_SRPOLICY_CPATH_ID,
Length: TLV_SRPOLICY_CPATH_ID_LENGTH,
Value: []uint8{
0x00, // protocol origin
0x00, 0x00, 0x00, // mbz
0x00, 0x00, 0x00, 0x00, // Originator ASN
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Originator Address
0x00, 0x00, 0x00, 0x00, //discriminator
},
},
&UndefinedTLV{
Typ: TLV_SRPOLICY_CPATH_PREFERENCE,
Length: TLV_SRPOLICY_CPATH_PREFERENCE_LENGTH,
Value: Uint32ToByteSlice(preference),
},
}
associationObjectTLVs := []TLVInterface{
&ExtendedAssociationID{
Color: color,
Endpoint: dstAddr,
},
&SRPolicyCandidatePathIdentifier{
OriginatorAddr: dstAddr,
},
&SRPolicyCandidatePathPreference{
Preference: preference,
},
}
o.TLVs = append(o.TLVs, associationObjectTLVs...)
}
Expand Down
147 changes: 147 additions & 0 deletions pkg/packet/pcep/tlv.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,67 @@ func (tlv *PathSetupType) Len() uint16 {
return TL_LENGTH + TLV_PATH_SETUP_TYPE_LENGTH
}

type ExtendedAssociationID struct {
Color uint32
Endpoint netip.Addr
}

func (tlv *ExtendedAssociationID) DecodeFromBytes(data []uint8) error {
l := binary.BigEndian.Uint16(data[2:4])

tlv.Color = binary.BigEndian.Uint32(data[4:8])

switch l {
case TLV_EXTENDED_ASSOCIATION_ID_IPV4_LENGTH:
tlv.Endpoint, _ = netip.AddrFromSlice(data[8:12])
case TLV_EXTENDED_ASSOCIATION_ID_IPV6_LENGTH:
tlv.Endpoint, _ = netip.AddrFromSlice(data[8:24])
}

return nil
}

func (tlv *ExtendedAssociationID) Serialize() []uint8 {
buf := []uint8{}

typ := make([]uint8, 2)
binary.BigEndian.PutUint16(typ, tlv.Type())
buf = append(buf, typ...)

length := make([]uint8, 2)
if tlv.Endpoint.Is4() {
binary.BigEndian.PutUint16(length, TLV_EXTENDED_ASSOCIATION_ID_IPV4_LENGTH)
} else if tlv.Endpoint.Is6() {
binary.BigEndian.PutUint16(length, TLV_EXTENDED_ASSOCIATION_ID_IPV6_LENGTH)
}
buf = append(buf, length...)

color := make([]uint8, 4)
binary.BigEndian.PutUint32(color, tlv.Color)
buf = append(buf, color...)

buf = append(buf, tlv.Endpoint.AsSlice()...)
return buf
}

func (tlv *ExtendedAssociationID) MarshalLogObject(enc zapcore.ObjectEncoder) error {
return nil
}

func (tlv *ExtendedAssociationID) Type() uint16 {
return TLV_EXTENDED_ASSOCIATION_ID
}

func (tlv *ExtendedAssociationID) Len() uint16 {
if tlv.Endpoint.Is4() {
return TL_LENGTH + TLV_EXTENDED_ASSOCIATION_ID_IPV4_LENGTH
} else if tlv.Endpoint.Is6() {
return TL_LENGTH + TLV_EXTENDED_ASSOCIATION_ID_IPV6_LENGTH
}
return 0

}

type PathSetupTypeCapability struct {
PathSetupTypes Psts
SubTLVs []TLVInterface
Expand Down Expand Up @@ -530,6 +591,92 @@ func (tlv *AssocTypeList) Len() uint16 {
return TL_LENGTH + l + padding
}

type SRPolicyCandidatePathIdentifier struct {
OriginatorAddr netip.Addr // After DecodeFromBytes, even ipv4 addresses are assigned in ipv6 format
}

func (tlv *SRPolicyCandidatePathIdentifier) DecodeFromBytes(data []uint8) error {
tlv.OriginatorAddr, _ = netip.AddrFromSlice(data[12:28])
return nil
}

func (tlv *SRPolicyCandidatePathIdentifier) Serialize() []uint8 {
buf := []uint8{}

typ := make([]uint8, 2)
binary.BigEndian.PutUint16(typ, tlv.Type())
buf = append(buf, typ...)

length := make([]uint8, 2)
binary.BigEndian.PutUint16(length, TLV_SRPOLICY_CPATH_ID_LENGTH)
buf = append(buf, length...)

buf = append(buf, 0x0a) // protocol origin, PCEP = 10
buf = append(buf, 0x00, 0x00, 0x00) // mbz
buf = append(buf, 0x00, 0x00, 0x00, 0x00) // Originator ASN
// Originator Address
if tlv.OriginatorAddr.Is4() {
buf = append(buf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
buf = append(buf, tlv.OriginatorAddr.AsSlice()...)
} else if tlv.OriginatorAddr.Is6() {
buf = append(buf, tlv.OriginatorAddr.AsSlice()...)
}
buf = append(buf, 0x00, 0x00, 0x00, 0x01) // discriminator

return buf
}

func (tlv *SRPolicyCandidatePathIdentifier) MarshalLogObject(enc zapcore.ObjectEncoder) error {
return nil
}

func (tlv *SRPolicyCandidatePathIdentifier) Type() uint16 {
return TLV_SRPOLICY_CPATH_ID
}

func (tlv *SRPolicyCandidatePathIdentifier) Len() uint16 {
return TL_LENGTH + TLV_SRPOLICY_CPATH_ID_LENGTH
}

type SRPolicyCandidatePathPreference struct {
Preference uint32
}

func (tlv *SRPolicyCandidatePathPreference) DecodeFromBytes(data []uint8) error {
tlv.Preference = binary.BigEndian.Uint32(data[4:8])
return nil
}

func (tlv *SRPolicyCandidatePathPreference) Serialize() []uint8 {
buf := []uint8{}

typ := make([]uint8, 2)
binary.BigEndian.PutUint16(typ, tlv.Type())
buf = append(buf, typ...)

length := make([]uint8, 2)
binary.BigEndian.PutUint16(length, TLV_SRPOLICY_CPATH_PREFERENCE_LENGTH)
buf = append(buf, length...)

preference := make([]uint8, 4)
binary.BigEndian.PutUint32(preference, tlv.Preference)
buf = append(buf, preference...)

return buf
}

func (tlv *SRPolicyCandidatePathPreference) MarshalLogObject(enc zapcore.ObjectEncoder) error {
return nil
}

func (tlv *SRPolicyCandidatePathPreference) Type() uint16 {
return TLV_SRPOLICY_CPATH_PREFERENCE
}

func (tlv *SRPolicyCandidatePathPreference) Len() uint16 {
return TL_LENGTH + TLV_SRPOLICY_CPATH_PREFERENCE_LENGTH
}

type UndefinedTLV struct {
Typ uint16
Length uint16
Expand Down

0 comments on commit 946196d

Please sign in to comment.