Skip to content

Commit

Permalink
Merge d542158 into 88e8414
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopeng163 committed Jan 26, 2016
2 parents 88e8414 + d542158 commit e6f2b7e
Show file tree
Hide file tree
Showing 12 changed files with 724 additions and 8 deletions.
34 changes: 32 additions & 2 deletions yabgp/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@
BGPTYPE_LINK_STATE = 99
BGPTYPE_ATTRIBUTE_SET = 128

# BGP Tunnel Encapsulation Attribute Tunnel Types
BGP_TUNNEL_ENCAPS_RESERVED = 0
BGP_TUNNEL_ENCAPS_L2TPV3_OVER_IP = 1
BGP_TUNNEL_ENCAPS_GRE = 2
BGP_TUNNEL_ENCAPS_TRANSMIT_TUNNEL_ENDPOINT = 3
BGP_TUNNEL_ENCAPS_IPSEC_TUNNEL_MODE = 4
BGP_TUNNEL_ENCAPS_IP_IN_IP_TUNNEL_WITH_IPSEC = 5
BGP_TUNNEL_ENCAPS_MPLS_IN_IP_TUNNEL_WITH_IPSEC = 6
BGP_TUNNEL_ENCAPS_IP_IN_IP = 7
BGP_TUNNEL_ENCAPS_VXLAN = 8
BGP_TUNNEL_ENCAPS_NVGRE = 9
BGP_TUNNEL_ENCAPS_MPLS = 10
BGP_TUNNEL_ENCAPS_MPLS_IN_GRE = 11
BGP_TUNNEL_ENCAPS_VXLAN_GRE = 12
BGP_TUNNEL_ENCAPS_MPLS_IN_UDP = 13

# VPN Route Target #
BGP_EXT_COM_RT_0 = 0x0002 # Route Target,Format AS(2bytes):AN(4bytes)
BGP_EXT_COM_RT_1 = 0x0102 # Route Target,Format IPv4 address(4bytes):AN(2bytes)
Expand All @@ -107,6 +123,18 @@
BGP_EXT_REDIRECT_VRF = 0x8008 # redirect 6-byte Route Target
BGP_EXT_TRA_MARK = 0x8009 # traffic-marking DSCP value

# Transitive Opaque
BGP_EXT_COM_OSPF_ROUTE_TYPE = 0x0306 # OSPF Route Type
BGP_EXT_COM_COLOR = 0x030b # Color
BGP_EXT_COM_ENCAP = 0x030c # BGP_EXT_COM_ENCAP = 0x030c
BGP_EXT_COM_DEFAULT_GATEWAY = 0x030d # Default Gateway

# BGP EVPN
BGP_EXT_COM_EVPN_MAC_MOBIL = 0x0600 # Mac Mobility
BGP_EXT_COM_EVPN_ESI_MPLS_LABEL = 0x0601 # ESI MPLS Label
BGP_EXT_COM_EVPN_ES_IMPORT = 0x0602 # ES Import


# BGP cost cummunity
BGP_EXT_COM_COST = 0x4301

Expand Down Expand Up @@ -169,13 +197,15 @@
(1, 1): 'ipv4',
(2, 1): 'ipv6',
(1, 133): 'flowspec',
(1, 128): 'vpnv4'
(1, 128): 'vpnv4',
(25, 70): 'evpn'
}
AFI_SAFI_STR_DICT = {
'ipv6': (2, 1),
'ipv4': (1, 1),
'flowspec': (1, 133),
'vpnv4': (1, 128)
'vpnv4': (1, 128),
'evpn': (25, 70)
}

ADD_PATH_ACT_DICT = {
Expand Down
1 change: 1 addition & 0 deletions yabgp/common/safn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
SAFNUM_TUNNEL = 64 # draft-nalawade-kapoor-tunnel-safi-02.txt
SAFNUM_VPLS = 65
SAFNUM_MDT = 66 # rfc6037
SAFNUM_EVPN = 70 # RFC 7432
SAFNUM_BGPLS = 71
SAFNUM_LAB_VPNUNICAST = 128 # Draft-rosen-rfc2547bis-03
SAFNUM_LAB_VPNMULCAST = 129
Expand Down
31 changes: 31 additions & 0 deletions yabgp/message/attribute/extcommunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ def parse(cls, value):
mark = struct.unpack('!B', value_tmp[-1])[0]
ext_community.append([bgp_cons.BGP_EXT_TRA_MARK, mark])

# Transitive Opaque
elif comm_code == bgp_cons.BGP_EXT_COM_ENCAP:
ext_community.append([bgp_cons.BGP_EXT_COM_ENCAP, struct.unpack('!I', value_tmp[2:])[0]])
# EVPN
elif comm_code == bgp_cons.BGP_EXT_COM_EVPN_ES_IMPORT:
mac = str(netaddr.EUI(int(binascii.b2a_hex(value_tmp), 16)))
ext_community.append([comm_code, mac])
elif comm_code == bgp_cons.BGP_EXT_COM_EVPN_MAC_MOBIL:
flag = ord(value_tmp[0:1])
seq = struct.unpack('!I', value_tmp[2:])[0]
ext_community.append([comm_code, flag, seq])
elif comm_code == bgp_cons.BGP_EXT_COM_EVPN_ESI_MPLS_LABEL:
flag = ord(value_tmp[0:1])
label = struct.unpack('!L', b'\00'+value_tmp[3:])[0]
label >>= 4
ext_community.append([comm_code, flag, label])
else:
ext_community.append([bgp_cons.BGP_EXT_COM_UNKNOW, repr(value_tmp)])
LOG.warn('unknow bgp extended community, type=%s, value=%s', comm_code, repr(value_tmp))
Expand Down Expand Up @@ -190,6 +206,21 @@ def construct(cls, value):
asn, rate = item[1].split(':')
ext_community_hex += struct.pack('!HHf', bgp_cons.BGP_EXT_TRA_RATE, int(asn), int(rate))

# Transitive Opaque
elif item[0] == bgp_cons.BGP_EXT_COM_ENCAP:
ext_community_hex += struct.pack('!HHI', bgp_cons.BGP_EXT_COM_ENCAP, 0, item[1])
# EVPN
elif item[0] == bgp_cons.BGP_EXT_COM_EVPN_ES_IMPORT:
mac = b''.join([struct.pack('!B', (int(i, 16))) for i in item[1].split("-")])
ext_community_hex += struct.pack('!H', item[0]) + mac
elif item[0] == bgp_cons.BGP_EXT_COM_EVPN_ESI_MPLS_LABEL:
flag = struct.pack('!B', item[1])
label = struct.pack('!L', (item[2] << 4 | 1))[1:]
ext_community_hex += struct.pack('!H', item[0]) + flag + b'\x00\x00' + label
elif item[0] == bgp_cons.BGP_EXT_COM_EVPN_MAC_MOBIL:
flag = struct.pack('!B', item[1])
seq = struct.pack('!I', item[2])
ext_community_hex += struct.pack('!H', item[0]) + flag + b'\x00' + seq
else:
LOG.warn('unknow bgp extended community for construct, type=%s, value=%s', item[0], item[1])

Expand Down
20 changes: 19 additions & 1 deletion yabgp/message/attribute/mpreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from yabgp.message.attribute.nlri.ipv4_mpls_vpn import IPv4MPLSVPN
from yabgp.message.attribute.nlri.ipv4_flowspec import IPv4FlowSpec
from yabgp.message.attribute.nlri.ipv6_unicast import IPv6Unicast
from yabgp.message.attribute.nlri.evpn import EVPN


class MpReachNLRI(Attribute):
Expand Down Expand Up @@ -128,6 +129,15 @@ def parse(cls, value):
else:
return dict(afi_safi=(afi, safi), nexthop=nexthop, nlri=nlri)

# for l2vpn
elif afi == afn.AFNUM_L2VPN:
if safi == safn.SAFNUM_EVPN:
nexthop = str(netaddr.IPAddress(int(binascii.b2a_hex(nexthop_bin), 16)))
nlri = EVPN.parse(nlri_bin)
return dict(afi_safi=(afi, safi), nexthop=nexthop, nlri=nlri)
else:
nlri = repr(nlri_bin)

else:
nlri = repr(nlri_bin)

Expand Down Expand Up @@ -204,7 +214,15 @@ def construct(cls, value):
nexthop_bin + b'\x00' + nlri_bin
return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID)\
+ struct.pack('!B', len(attr_value)) + attr_value

# for l2vpn
elif afi == afn.AFNUM_L2VPN:
if safi == safn.SAFNUM_EVPN:
nexthop_bin = netaddr.IPAddress(value['nexthop']).packed
nlri_bin = EVPN.construct(nlri_list=value['nlri'])
attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + struct.pack('!B', len(nexthop_bin)) + \
nexthop_bin + b'\x00' + nlri_bin
return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID)\
+ struct.pack('!B', len(attr_value)) + attr_value
else:
raise excep.ConstructAttributeFailed(
reason='unsupport this sub address family',
Expand Down
19 changes: 19 additions & 0 deletions yabgp/message/attribute/mpunreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from yabgp.message.attribute.nlri.ipv4_mpls_vpn import IPv4MPLSVPN
from yabgp.message.attribute.nlri.ipv4_flowspec import IPv4FlowSpec
from yabgp.message.attribute.nlri.ipv6_unicast import IPv6Unicast
from yabgp.message.attribute.nlri.evpn import EVPN
from yabgp.common import afn
from yabgp.common import safn
from yabgp.common import exception as excep
Expand Down Expand Up @@ -85,6 +86,13 @@ def parse(cls, value):
return dict(afi_safi=(afi, safi), withdraw=IPv6Unicast.parse(nlri_data=nlri_bin))
else:
return dict(afi_safi=(afi, safi), withdraw=repr(nlri_bin))
# for l2vpn
elif afi == afn.AFNUM_L2VPN:
# for evpn
if safi == safn.SAFNUM_EVPN:
return dict(afi_safi=(afi, safi), withdraw=EVPN.parse(nlri_data=nlri_bin))
else:
return dict(afi_safi=(afi, safi), withdraw=repr(nlri_bin))

else:
return dict(afi_safi=(afi, safi), withdraw=repr(nlri_bin))
Expand Down Expand Up @@ -137,6 +145,17 @@ def construct(cls, value):
+ struct.pack('!B', len(attr_value)) + attr_value
else:
return None
# for l2vpn
elif afi == afn.AFNUM_L2VPN:
# for evpn
if safi == safn.SAFNUM_EVPN:
nlri = EVPN.construct(nlri_list=value['withdraw'])
if nlri:
attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
+ struct.pack('!B', len(attr_value)) + attr_value
else:
return None
else:
raise excep.ConstructAttributeFailed(
reason='unsupport this sub address family',
Expand Down
Loading

0 comments on commit e6f2b7e

Please sign in to comment.