-
Notifications
You must be signed in to change notification settings - Fork 70
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 parsing the Path Attribute: BGP Prefix-SID (Type 40) in BGP UPDATE Message #167
Merged
zlpqingmei
merged 5 commits into
smartbgp:master
from
yuangezhizao:166-parsing-bgp_prefix_sid
Mar 6, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a043982
🎨 Style(__init__.py): replace "Sid" with "SID"
yuangezhizao 19a8d88
✨ Feat(bgpprefixsid.py): add support for parsing the Path Attribute: …
yuangezhizao ca0c3ff
✅ Test(test_bgpprefixsid.py): add unittest and format result
yuangezhizao 9ed0976
💚 Fix-ci(ci.yml): remove Python 2.7.x from the CI matrix
yuangezhizao ddf8787
🔥 Prune(.travis.yml): remove Travis CI
yuangezhizao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def dict(cls)
ordef dict(self)