Skip to content

Commit

Permalink
[WIP] new tlv for bgpls (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
meidli authored and xiaopeng163 committed Feb 28, 2018
1 parent 3c0eeb9 commit ee21377
Show file tree
Hide file tree
Showing 25 changed files with 1,007 additions and 30 deletions.
17 changes: 17 additions & 0 deletions yabgp/message/attribute/linkstate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
from .node.local_router_id import LocalRouterID # noqa
from .node.name import NodeName # noqa
from .node.isisarea import ISISArea # noqa
from .node.sr_capabilities import SRCapabilities # noqa
from .node.sr_algorithm import SRAlgorithm # noqa
from .node.node_msd import NodeMSD # noqa
from .node.nodeflags import NodeFlags # noqa
from .node.opa_node_attr import OpaNodeAttr # noqa
from .link.admingroup import AdminGroup # noqa
from .link.remote_router_id import RemoteRouterID # noqa
from .link.max_bw import MaxBandwidth # noqa
Expand All @@ -26,5 +31,17 @@
from .link.link_name import LinkName # noqa
from .link.igp_metric import IGPMetric # noqa
from .link.adj_seg_id import AdjSegID # noqa
from .link.link_identifiers import LinkIdentifiers # noqa
from .link.link_msd import LinkMSD # noqa
from .link.lan_adj_seg_id import LanAdjSegID # noqa
from .link.srlg import SRLGList # noqa
from .link.mplsmask import MplsMask # noqa
from .link.protection_type import ProtectionType # noqa
from .link.opa_link_attr import OpaLinkAttr # noqa
from .prefix.prefix_metric import PrefixMetric # noqa
from .prefix.prefix_sid import PrefixSID # noqa
from .prefix.prefix_igp_attr import PrefixIGPAttr # noqa
from .prefix.src_router_id import SrcRouterID # noqa
from .prefix.igpflags import IGPFlags # noqa
from .prefix.igp_route_tag_list import IGPRouteTagList # noqa
from .prefix.ext_igp_route_tag_list import ExtIGPRouteTagList # noqa
77 changes: 77 additions & 0 deletions yabgp/message/attribute/linkstate/link/lan_adj_seg_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2015-2018 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 binascii
import struct
import netaddr
from yabgp.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

# https://tools.ietf.org/html/draft-gredler-idr-bgp-ls-segment-routing-ext-03#section-2.2.2
# 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
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Type | Length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Flags | Weight | Reserved |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | OSPF Neighbor ID / IS-IS System-ID |
# + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | SID/Label/Index (variable) |
# +---------------------------------------------------------------+

# Flags: 1 octet field of following flags:
# 0 1 2 3 4 5 6 7
# +-+-+-+-+-+-+-+-+
# |F|B|V|L|S|P| |
# +-+-+-+-+-+-+-+-+


@LinkState.register()
class LanAdjSegID(TLV):
"""
Adjacency Segment id
"""
TYPE = 1100
TYPE_STR = 'lan-adj-segment-id'

@classmethod
def unpack(cls, data):
flags = struct.unpack('!B', data[0])[0]
F = flags >> 7
B = (flags << 1) % 256 >> 7
V = (flags << 2) % 256 >> 7
L = (flags << 3) % 256 >> 7
S = (flags << 4) % 256 >> 7
P = (flags << 5) % 256 >> 7
weight = struct.unpack('!B', data[1])[0]
# nei_or_sys_id = struct.unpack('!6B', data[4:10])
nei_or_sys_id = str(netaddr.IPAddress(int(binascii.b2a_hex(data[4:10]), 16)))
sid_index_label = int(binascii.b2a_hex(data[10:]), 16)
# if len(data) == 3:
# sid_index_label = (struct.unpack('!I', "\x00" + data)[0] << 12) >> 12
# elif len(data) == 4:
# sid_index_label = struct.unpack('!I', data)[0]
return cls(value={
"flags": {"F": F, "B": B, "V": V, "L": L, "S": S, "P": P},
"weight": weight,
"nei_or_sys_id": nei_or_sys_id,
"sid_index_label": sid_index_label
})
42 changes: 42 additions & 0 deletions yabgp/message/attribute/linkstate/link/link_identifiers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2015-2018 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

# 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
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Link Local Identifier |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Link Remote Identifier |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


@LinkState.register()
class LinkIdentifiers(TLV):
"""
link local/remote identifiers
"""
TYPE = 258 # RFC 7752
TYPE_STR = 'link-identifiers'

@classmethod
def unpack(cls, data):
local_identifier = struct.unpack('!I', data[:4])[0]
remote_identifier = struct.unpack('!I', data[4:])[0]

return cls(value={'local-identifier': local_identifier, 'remote-identifier': remote_identifier})
33 changes: 33 additions & 0 deletions yabgp/message/attribute/linkstate/link/link_msd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2015-2018 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 LinkMSD(TLV):
"""
link msd
"""
TYPE = 1110 # https://tools.ietf.org/html/draft-tantsura-idr-bgp-ls-segment-routing-msd-05#section-4
TYPE_STR = 'link-msd'

@classmethod
def unpack(cls, data):
_type, value = struct.unpack('!BB', data)
return cls(value={"type": _type, "value": value})
51 changes: 51 additions & 0 deletions yabgp/message/attribute/linkstate/link/mplsmask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2015-2018 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.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

# https://tools.ietf.org/html/rfc7752#section-3.3.2.2
# 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
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Type | Length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# |L|R| Reserved |
# +-+-+-+-+-+-+-+-+

# +------------+------------------------------------------+-----------+
# | Bit | Description | Reference |
# +------------+------------------------------------------+-----------+
# | 'L' | Label Distribution Protocol (LDP) | [RFC5036] |
# | 'R' | Extension to RSVP for LSP Tunnels | [RFC3209] |
# | | (RSVP-TE) | |
# | 'Reserved' | Reserved for future use | |
# +------------+------------------------------------------+-----------+


@LinkState.register()
class MplsMask(TLV):

TYPE = 1094 # https://tools.ietf.org/html/rfc7752#section-3.3.2.2
TYPE_STR = "mpls-mask"

@classmethod
def unpack(cls, value):
"""
"""
value = struct.unpack('!B', value)[0]
L = value >> 7
R = (value << 1) % 256 >> 7
return cls(value={'L': L, 'R': R})
39 changes: 39 additions & 0 deletions yabgp/message/attribute/linkstate/link/opa_link_attr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2015-2018 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.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

# https://tools.ietf.org/html/rfc7752#section-3.3.2.6
# 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
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Type | Length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# // Opaque link attributes (variable) //
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


@LinkState.register()
class OpaLinkAttr(TLV):

TYPE = 1097 # https://tools.ietf.org/html/rfc7752#section-3.3.2.6
TYPE_STR = "opa-link-attr"

@classmethod
def unpack(cls, value):
"""
"""
return cls(value=struct.unpack("!%ds" % len(value), value)[0])
58 changes: 58 additions & 0 deletions yabgp/message/attribute/linkstate/link/protection_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2015-2018 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.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

# https://tools.ietf.org/html/rfc5307#section-1.2
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# |Protection Cap | Reserved |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# 0x01 Extra Traffic
# 0x02 Unprotected
# 0x04 Shared
# 0x08 Dedicated 1:1
# 0x10 Dedicated 1+1
# 0x20 Enhanced
# 0x40 Reserved
# 0x80 Reserved


@LinkState.register()
class ProtectionType(TLV):

TYPE = 1093 # https://tools.ietf.org/html/rfc5307#section-1.2
TYPE_STR = "protection-type"

@classmethod
def unpack(cls, value):
"""
"""
value = struct.unpack('!B', value[0])[0]
if value == 0x01:
return cls(value='Extra Traffic')
if value == 0x02:
return cls(value='Unprotected')
if value == 0x04:
return cls(value='Shared')
if value == 0x08:
return cls(value='Dedicated 1:1')
if value == 0x10:
return cls(value='Dedicated 1+1')
if value == 0x20:
return cls(value='Enhanced')
47 changes: 47 additions & 0 deletions yabgp/message/attribute/linkstate/link/srlg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2015-2018 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.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

# https://tools.ietf.org/html/rfc7752#section-3.3.2.5
# 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
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Type | Length |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Shared Risk Link Group Value |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# // ............ //
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Shared Risk Link Group Value |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


@LinkState.register()
class SRLGList(TLV):

TYPE = 1096 # https://tools.ietf.org/html/rfc7752#section-3.3.2.5
TYPE_STR = "srlg-list"

@classmethod
def unpack(cls, value):
"""
"""
results = []
while value:
results.append(struct.unpack('!L', value[:4])[0])
value = value[4:]
return cls(value=results)
Loading

0 comments on commit ee21377

Please sign in to comment.