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 SRv6 SID Information TLV (type 518), SRv6 Endpoint Behavior TLV (type 1250) & SRv6 BGP Peer Node SID TLV (type 1251) #146

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions yabgp/message/attribute/linkstate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@
from .prefix.ext_igp_route_tag_list import ExtIGPRouteTagList # noqa
from .prefix.ospf_forward_addr import OspfForwardingAddr # noqa
from .prefix.srv6_locator import SRv6Locator # noqa
from .srv6_sid.srv6_endpoint_behavior import SRv6EndpointBehavior # noqa
from .srv6_sid.srv6_bgp_peer_node_sid import SRv6BGPPeerNodeSID # noqa
4 changes: 2 additions & 2 deletions yabgp/message/attribute/linkstate/node/srv6_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def unpack(cls, data, bgpls_pro_id):
flag = {}
if bgpls_pro_id in (1, 2):
# https://datatracker.ietf.org/doc/html/rfc9352#name-srv6-capabilities-sub-tlv
flag['O-flag'] = (flags << 1) % 256 >> 15
flag['O'] = (flags << 1) % 256 >> 15
elif bgpls_pro_id in (3, 6):
# https://datatracker.ietf.org/doc/html/draft-ietf-lsr-ospfv3-srv6-extensions-09#section-2
flag['O-flag'] = (flags << 1) % 256 >> 15
flag['O'] = (flags << 1) % 256 >> 15
else:
flag = flags
# reserved = struct.unpack('!H', data[2:4])[0]
Expand Down
2 changes: 1 addition & 1 deletion yabgp/message/attribute/linkstate/prefix/srv6_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def unpack(cls, data, bgpls_pro_id):
flag = {}
if bgpls_pro_id in (1, 2): # IS-IS
# https://datatracker.ietf.org/doc/html/rfc9352#section-7.1
flag['D-flag'] = flags >> 7
flag['D'] = flags >> 7
else:
flag = flags # TODO (OSPFv3 SRv6)
algorithm = ord(data[1:2])
Expand Down
7 changes: 7 additions & 0 deletions yabgp/message/attribute/linkstate/srv6_sid/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# +---------------+------------------------+----------+-----------------+
# | TLV Code | Description | Length | Reference |
# | Point | | | |
# +---------------+------------------------+----------+-----------------+
# | 1250 | SRv6 Endpoint Behavior | 4 | Section 7.1 |
# | 1251 | SRv6 BGP Peer Node SID | 12 | Section 7.2 |
# +---------------+------------------------+----------+-----------------+
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2023 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

from yabgp.tlv import TLV
from ..linkstate import LinkState


@LinkState.register()
class SRv6BGPPeerNodeSID(TLV):
"""
SRv6 BGP Peer Node SID
"""
TYPE = 1251 # https://datatracker.ietf.org/doc/html/draft-ietf-idr-bgpls-srv6-ext-14#section-7.2
TYPE_STR = 'srv6_bgp_peer_node_sid'

@classmethod
def unpack(cls, data):
"""

:param data:
:return:
"""
flags = ord(data[0:1])
flag = {}
flag['B'] = flags >> 7
flag['S'] = (flags << 1) % 256 >> 7
flag['P'] = (flags << 2) % 256 >> 7
weight = ord(data[1:2])
# reserved = struct.unpack('!H', data[2:4])[0]
peer_as_number = '{0}.{1}'.format(struct.unpack('!H', data[4:6])[0], struct.unpack('!H', data[6:8])[0])
peer_bgp_identifier = struct.unpack('!HH', data[8:12])[0]

return cls(value={
'flags': flag,
'weight': weight,
# 'reserved': reserved,
'peer_as_number': peer_as_number,
'peer_bgp_identifier': peer_bgp_identifier
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2023 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

from yabgp.tlv import TLV
from ..linkstate import LinkState


@LinkState.register()
class SRv6EndpointBehavior(TLV):
"""
SRv6 Endpoint Behavior
"""
TYPE = 1250 # https://datatracker.ietf.org/doc/html/draft-ietf-idr-bgpls-srv6-ext-14#section-7.1
TYPE_STR = 'srv6_endpoint_behavior'

@classmethod
def unpack(cls, data):
"""

:param data:
:return:
"""
endpoint_behavior = struct.unpack('!H', data[0:2])[0]
flags = ord(data[2:3])
algorithm = ord(data[3:4])

return cls(value={
'endpoint_behavior': endpoint_behavior,
'flags': flags,
'algorithm': algorithm
})
34 changes: 26 additions & 8 deletions yabgp/message/attribute/nlri/linkstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@

class BGPLS(NLRI):
"""BGPLS

Refer: https://datatracker.ietf.org/doc/html/rfc7752#section-3.2
"""
NODE_NLRI = 1
LINK_NLRI = 2
IPv4_TOPO_PREFIX_NLRI = 3
IPv6_TOPO_PREFIX_NLRI = 4
SRv6_SID_NLRI = 6 # https://datatracker.ietf.org/doc/html/draft-ietf-idr-bgpls-srv6-ext-14#section-6

@classmethod
def parse(cls, nlri_data):
nlri_list = []
while nlri_data:
_type, length = struct.unpack('!HH', nlri_data[0:4])
value = nlri_data[4: 4 + length]
nlri_data = nlri_data[4+length:]
nlri_data = nlri_data[4 + length:]
nlri = dict()
if _type == cls.LINK_NLRI:
nlri['type'] = 'link'
Expand All @@ -48,6 +51,8 @@ def parse(cls, nlri_data):
nlri['type'] = 'ipv4_topo_prefix'
elif _type == cls.IPv6_TOPO_PREFIX_NLRI:
nlri['type'] = 'ipv6_topo_prefix'
elif _type == cls.SRv6_SID_NLRI:
nlri['type'] = 'srv6_sid'
else:
nlri['type'] = 'unknown'
continue
Expand All @@ -60,7 +65,7 @@ def parse(cls, nlri_data):

@classmethod
def parse_nlri(cls, data, nlri_type):
"""parse nlri: node, link, prefix
"""parse nlri: node, link, prefix, srv6_sid
"""
# 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
Expand All @@ -76,6 +81,8 @@ def parse_nlri(cls, data, nlri_type):
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# // Link Descriptors (variable) //
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# // SRv6 SID Descriptors (variable) //
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# Figure 8: The Link NLRI format

# The Protocol-ID field can contain one of the following values:
Expand Down Expand Up @@ -107,8 +114,8 @@ def parse_nlri(cls, data, nlri_type):
descriptors = data[9:]
while descriptors:
_type, length = struct.unpack('!HH', descriptors[0:4])
value = descriptors[4: 4+length]
descriptors = descriptors[4+length:]
value = descriptors[4: 4 + length]
descriptors = descriptors[4 + length:]
descriptor = dict()
if _type == 256: # local node
descriptor['type'] = 'local_node'
Expand Down Expand Up @@ -146,7 +153,7 @@ def parse_nlri(cls, data, nlri_type):
elif _type == 264: # OSPF Route Type
descriptor['type'] = 'prefix_ospf_route_type'
descriptor['value'] = ord(value[0:1])
elif _type == 265: # IP Reachability Information
elif _type == 265: # IP Reachability Information
descriptor['type'] = 'prefix'
mask = ord(value[0:1])
if nlri_type == cls.IPv4_TOPO_PREFIX_NLRI:
Expand All @@ -158,6 +165,17 @@ def parse_nlri(cls, data, nlri_type):
prefix_bit += b'\x00'
ip_str = str(netaddr.IPAddress(int(binascii.b2a_hex(prefix_bit), 16)))
descriptor['value'] = "%s/%s" % (ip_str, mask)
elif _type == 518: # SRv6 SID Information
# Refer: https://datatracker.ietf.org/doc/html/draft-ietf-idr-bgpls-srv6-ext-14#section-6.1
descriptor['type'] = 'srv6_sid_information'
descriptor['value'] = []
while value:
descriptor['value'].append(str(netaddr.IPAddress(int(binascii.b2a_hex(value[:16]), 16))))
value = value[16:]

# This field MUST contain a single SRv6 SID Information TLV (Section 6.1) and
# MAY contain the Multi-Topology Identifier TLV [RFC7752].
# descriptor['value'] = str(netaddr.IPAddress(int(binascii.b2a_hex(value), 16)))
else:
descriptor['type'] = _type
descriptor['value'] = binascii.b2a_hex(value)
Expand All @@ -180,8 +198,8 @@ def parse_node_descriptor(cls, data, proto):
return_data = dict()
while data:
_type, length = struct.unpack('!HH', data[0: 4])
value = data[4: 4+length]
data = data[4+length:]
value = data[4: 4 + length]
data = data[4 + length:]
if _type == 512:
return_data['as_num'] = int(binascii.b2a_hex(value), 16)
elif _type == 513:
Expand Down Expand Up @@ -226,4 +244,4 @@ def parse_node_descriptor(cls, data, proto):
def parse_iso_node_id(cls, data):
tmp = binascii.b2a_hex(data).decode('utf-8')
chunks, chunk_size = len(tmp), len(tmp) // 3
return '.'.join([str(tmp[i:i+chunk_size]) for i in range(0, chunks, chunk_size)])
return '.'.join([str(tmp[i:i + chunk_size]) for i in range(0, chunks, chunk_size)])
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_unpack(self):
'type': 'srv6_capabilities',
'value': {
'flags': {
'O-flag': 0
'O': 0
}
}}
for bgpls_pro_id in (1, 2, 3, 6):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_unpack(self):
'type': 'srv6_locator',
'value': {
'flags': {
'D-flag': 0
'D': 0
},
'algorithm': 0,
'metric': 0,
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 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.

""" Test SRv6 Endpoint Behavior TLV """

import unittest

from yabgp.message.attribute.linkstate.srv6_sid.srv6_endpoint_behavior import SRv6EndpointBehavior


class TestSRv6EndpointBehavior(unittest.TestCase):

def test_unpack(self):
data_bin = b'\x00\x30' \
b'\x00' \
b'\x00'

data_dict = {
'type': 'srv6_endpoint_behavior',
'value': {
'endpoint_behavior': 48,
'flags': 0,
'algorithm': 0
}
}
self.assertEqual(data_dict, SRv6EndpointBehavior.unpack(data=data_bin).dict())