-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for parsing the Path Attribute: BGP Prefix-SID (Type 40) …
…in BGP UPDATE Message (#167) * 🎨 Style(__init__.py): replace "Sid" with "SID" * ✨ Feat(bgpprefixsid.py): add support for parsing the Path Attribute: BGP Prefix-SID (Type 40) Refer: https://github.com/Exa-Networks/exabgp/blob/main/src/exabgp/bgp/message/update/attribute/sr/prefixsid.py * ✅ Test(test_bgpprefixsid.py): add unittest and format result * 💚 Fix-ci(ci.yml): remove Python 2.7.x from the CI matrix Refer: actions/setup-python#672 * 🔥 Prune(.travis.yml): remove Travis CI
- Loading branch information
1 parent
3a28d5c
commit 59dc51b
Showing
17 changed files
with
462 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
|
||
|
||
@LinkState.register() | ||
class SRv6EndXSid(TLV): | ||
class SRv6EndXSID(TLV): | ||
""" | ||
SRv6 End.X SID | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2024 Cisco Systems, Inc. | ||
# All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from .bgpprefixsid import BGPPrefixSID # noqa | ||
from .srv6.l3service import SRv6L3Service # noqa | ||
from .srv6.sidinformation import SRv6SIDInformation # noqa | ||
from .srv6.sidstructure import SRv6SIDStructure # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright 2024 Cisco Systems, Inc. | ||
# All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import struct | ||
|
||
import binascii | ||
|
||
from yabgp.message.attribute import Attribute, AttributeFlag, AttributeID | ||
|
||
|
||
class BGPPrefixSID(Attribute): | ||
""" | ||
BGP Prefix SID | ||
Original: https://datatracker.ietf.org/doc/html/rfc8669#section-3 | ||
Extend: https://datatracker.ietf.org/doc/html/rfc9252#section-2 | ||
""" | ||
|
||
ID = AttributeID.BGP_PREFIX_SID | ||
FLAG = AttributeFlag.OPTIONAL + AttributeFlag.TRANSITIVE | ||
|
||
registered_tlvs = dict() | ||
|
||
def __init__(self, value, hex_value=None): | ||
self.value = value | ||
self.hex_value = hex_value | ||
|
||
@classmethod | ||
def register(cls, _type=None): | ||
""" | ||
:param _type: | ||
:return: | ||
""" | ||
|
||
def decorator(klass): | ||
""" | ||
:param klass: | ||
:return: | ||
""" | ||
_id = klass.TYPE if _type is None else _type | ||
if _id in cls.registered_tlvs: | ||
raise RuntimeError('Duplicated attribute type') | ||
cls.registered_tlvs[_id] = klass | ||
return klass | ||
|
||
return decorator | ||
|
||
@classmethod | ||
def unpack(cls, data): | ||
""" | ||
:param data: | ||
:return: | ||
""" | ||
tlvs = [] | ||
while data: | ||
type_code = data[0] # Note: Type = 1 octet | ||
length = struct.unpack('!H', data[1:3])[0] # Note: Length = 2 octet | ||
value = data[3: 3 + length] | ||
|
||
if type_code in cls.registered_tlvs: | ||
tlvs.append(cls.registered_tlvs[type_code].unpack(value)) | ||
else: | ||
tlvs.append({ | ||
'type': type_code, | ||
'value': str(binascii.b2a_hex(value)) | ||
}) | ||
data = data[3 + length:] | ||
return tlvs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# +---------------+---------------------------------+----------+-----------------+ | ||
# | TLV Code | Description | Length | Reference | | ||
# | Point | | | | | ||
# +---------------+---------------------------------+----------+-----------------+ | ||
# | 5 | SRv6 L3 Service TLV | variable | Section 2 | | ||
# | 1 | SRv6 SID Information Sub-TLV | variable | Section 3.1 | | ||
# | 1 | SRv6 SID Structure Sub-Sub-TLV | 6 | Section 3.2.1 | | ||
# +---------------+---------------------------------+----------+-----------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2024 Cisco Systems, Inc. | ||
# All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import struct | ||
|
||
import binascii | ||
|
||
from yabgp.tlv import TLV | ||
from ..bgpprefixsid import BGPPrefixSID | ||
|
||
|
||
# 2. SRv6 Services TLVs | ||
# | ||
# 0 1 2 3 | ||
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
# | TLV Type | TLV Length | RESERVED | | ||
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
# | SRv6 Service Sub-TLVs // | ||
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
# | ||
# Figure 1: SRv6 Service TLVs | ||
|
||
|
||
@BGPPrefixSID.register() | ||
class SRv6L3Service(TLV): | ||
""" | ||
SRv6 L3 Service | ||
""" | ||
TYPE = 5 # https://datatracker.ietf.org/doc/html/rfc9252.html#section-2 | ||
TYPE_STR = 'srv6_l3_service' | ||
|
||
registered_tlvs = dict() | ||
|
||
@classmethod | ||
def register(cls, _type=None): | ||
""" | ||
:param _type: | ||
:return: | ||
""" | ||
|
||
def decorator(klass): | ||
""" | ||
:param klass: | ||
:return: | ||
""" | ||
_id = klass.TYPE if _type is None else _type | ||
if _id in cls.registered_tlvs: | ||
raise RuntimeError('Duplicated SRv6 Service Sub-TLV type') | ||
cls.registered_tlvs[_id] = klass | ||
return klass | ||
|
||
return decorator | ||
|
||
@classmethod | ||
def unpack(cls, data): | ||
""" | ||
:param data: | ||
:return: | ||
""" | ||
tlvs = [] | ||
|
||
# reserved = data[0:1] # Note: First byte is reserved | ||
data = data[1:] | ||
while data: | ||
srv6_service_sub_tlv_type_code = data[0] # Note: Type = 1 octet | ||
srv6_service_sub_tlv_length = struct.unpack('!H', data[1:3])[0] # Note: Length = 2 octet | ||
value = data[3: 3 + srv6_service_sub_tlv_length] | ||
|
||
if srv6_service_sub_tlv_type_code in cls.registered_tlvs: | ||
tlvs.append(cls.registered_tlvs[srv6_service_sub_tlv_type_code].unpack(value)) | ||
else: | ||
tlvs.append({ | ||
'type': srv6_service_sub_tlv_type_code, | ||
'value': str(binascii.b2a_hex(value)) | ||
}) | ||
data = data[3 + srv6_service_sub_tlv_length:] | ||
value = { | ||
'srv6_service_sub_tlvs': tlvs | ||
} | ||
return {cls.TYPE_STR: value} |
Oops, something went wrong.