bgp: implement multiprotocol capability

Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
YAMAMOTO Takashi 2013-10-22 14:27:27 +09:00 committed by FUJITA Tomonori
parent 6d11c92595
commit 2a4c36ef19
2 changed files with 33 additions and 2 deletions

View File

@ -26,7 +26,6 @@ RFC 4271 BGP-4
# - RFC 4360 BGP Extended Communities Attribute
# - RFC 4364 BGP/MPLS IP Virtual Private Networks (VPNs)
# - RFC 4486 Subcodes for BGP Cease Notification Message
# - RFC 4760 Multiprotocol Extensions for BGP-4
import abc
import struct
@ -57,7 +56,7 @@ _MARKER = 16 * '\xff'
BGP_OPT_CAPABILITY = 2 # RFC 5492
BGP_CAP_MULTI_PROTOCOL = 1 # RFC 4760
BGP_CAP_MULTIPROTOCOL = 1 # RFC 4760
BGP_CAP_ROUTE_REFRESH = 2 # RFC 2918
BGP_CAP_FOUR_OCTET_AS_NUMBER = 65 # RFC 4893
@ -327,6 +326,36 @@ class BGPOptParamCapabilityFourOctetAsNumber(_OptParamCapability):
return buf
@_OptParamCapability.register_type(BGP_CAP_MULTIPROTOCOL)
class BGPOptParamCapabilityMultiprotocol(_OptParamCapability):
_CAP_PACK_STR = '!HBB' # afi, reserved, safi
def __init__(self, afi, safi, reserved=0, **kwargs):
super(BGPOptParamCapabilityMultiprotocol, self).__init__(**kwargs)
self.afi = afi
self.reserved = reserved
self.safi = safi
@classmethod
def parse_cap_value(cls, buf):
(afi, reserved, safi,) = struct.unpack_from(cls._CAP_PACK_STR,
buffer(buf))
return {
'afi': afi,
'reserved': reserved,
'safi': safi,
}
def serialize_cap_value(self):
# fixup
self.reserved = 0
buf = bytearray()
msg_pack_into(self._CAP_PACK_STR, buf, 0,
self.afi, self.reserved, self.safi)
return buf
class BGPWithdrawnRoute(_IPAddrPrefix):
pass

View File

@ -45,6 +45,8 @@ class Test_bgp(unittest.TestCase):
opt_param = [bgp.BGPOptParamCapabilityUnknown(cap_code=200,
cap_value='hoge'),
bgp.BGPOptParamCapabilityRouteRefresh(),
bgp.BGPOptParamCapabilityMultiprotocol(
afi=afi.IP, safi=safi.MPLS_VPN),
bgp.BGPOptParamCapabilityFourOctetAsNumber(
as_number=1234567),
bgp.BGPOptParamUnknown(type_=99, value='fuga')]