From d21e432c98ec9cbe1de2e471ecf7326170d7f176 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Wed, 10 Jul 2024 16:04:29 +0300 Subject: [PATCH 1/3] Implement software version capability for BGP https://datatracker.ietf.org/doc/html/draft-abraitis-bgp-version-capability This is already implemented by FRR, GoBGP, freertr, Wireshark, some more. E.g.: ``` $ vtysh -c 'show bgp neighbor 127.0.0.1 json' | jq '."127.0.0.1".neighborCapabilities.softwareVersion' { "advertisedSoftwareVersion": "FRRouting/10.2-dev-MyOwnFRRVersion-g8ca262943f", "receivedSoftwareVersion": "ExaBGP/feature/bgp_software_version_capability-e510e8c6b4d6c8651" } ``` Signed-off-by: Donatas Abraitis --- .../message/open/capability/capabilities.py | 8 ++++ .../bgp/message/open/capability/capability.py | 3 ++ .../open/capability/software_version.py | 40 +++++++++++++++++++ src/exabgp/bgp/neighbor.py | 5 ++- src/exabgp/configuration/capability.py | 4 ++ src/exabgp/configuration/neighbor/__init__.py | 1 + tests/decode_test.py | 3 +- 7 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/exabgp/bgp/message/open/capability/software_version.py diff --git a/src/exabgp/bgp/message/open/capability/capabilities.py b/src/exabgp/bgp/message/open/capability/capabilities.py index a62d59d5d..1388332d4 100644 --- a/src/exabgp/bgp/message/open/capability/capabilities.py +++ b/src/exabgp/bgp/message/open/capability/capabilities.py @@ -22,6 +22,7 @@ from exabgp.bgp.message.open.capability.refresh import EnhancedRouteRefresh from exabgp.bgp.message.open.capability.extended import ExtendedMessage from exabgp.bgp.message.open.capability.hostname import HostName +from exabgp.bgp.message.open.capability.software_version import SoftwareVersion from exabgp.bgp.message.notification import Notify @@ -142,6 +143,12 @@ def _extended_message(self, neighbor): def _hostname(self, neighbor): self[Capability.CODE.HOSTNAME] = HostName(neighbor['host-name'], neighbor['domain-name']) + def _software_version(self, neighbor): + if not neighbor['capability']['software-version']: + return + + self[Capability.CODE.SOFTRWARE_VERSION] = SoftwareVersion() + def _operational(self, neighbor): if not neighbor['capability']['operational']: return @@ -163,6 +170,7 @@ def new(self, neighbor, restarted): self._operational(neighbor) self._extended_message(neighbor) self._hostname(neighbor) # https://datatracker.ietf.org/doc/html/draft-walton-bgp-hostname-capability-02 + self._software_version(neighbor) # https://datatracker.ietf.org/doc/html/draft-abraitis-bgp-version-capability self._session(neighbor) # MUST be the last key added, really !?! dict is not ordered ! return self diff --git a/src/exabgp/bgp/message/open/capability/capability.py b/src/exabgp/bgp/message/open/capability/capability.py index 05f13767b..cb6d8226f 100644 --- a/src/exabgp/bgp/message/open/capability/capability.py +++ b/src/exabgp/bgp/message/open/capability/capability.py @@ -37,6 +37,7 @@ class _CapabilityCode(int): MULTISESSION_CISCO = 0x83 # What Cisco really use for Multisession (yes this is a reserved range in prod !) HOSTNAME = 0x49 # https://datatracker.ietf.org/doc/html/draft-walton-bgp-hostname-capability-02 + SOFTWARE_VERSION = 0x4B # https://datatracker.ietf.org/doc/html/draft-abraitis-bgp-version-capability OPERATIONAL = 0xB9 # ExaBGP only ... # Internal @@ -61,6 +62,7 @@ class _CapabilityCode(int): MULTISESSION_CISCO: 'cisco-multi-sesion', AIGP: 'aigp', HOSTNAME: 'hostname', + SOFTWARE_VERSION: 'software-version', } def __new__(cls, value): @@ -105,6 +107,7 @@ class CODE(int): ROUTE_REFRESH_CISCO = _CapabilityCode(_CapabilityCode.ROUTE_REFRESH_CISCO) MULTISESSION_CISCO = _CapabilityCode(_CapabilityCode.MULTISESSION_CISCO) HOSTNAME = _CapabilityCode(_CapabilityCode.HOSTNAME) + SOFTRWARE_VERSION = _CapabilityCode(_CapabilityCode.SOFTWARE_VERSION) OPERATIONAL = _CapabilityCode(_CapabilityCode.OPERATIONAL) AIGP = _CapabilityCode(_CapabilityCode.AIGP) # fmt: on diff --git a/src/exabgp/bgp/message/open/capability/software_version.py b/src/exabgp/bgp/message/open/capability/software_version.py new file mode 100644 index 000000000..b7ce8f575 --- /dev/null +++ b/src/exabgp/bgp/message/open/capability/software_version.py @@ -0,0 +1,40 @@ +# encoding: utf-8 +""" +software_version.py + +Copyright (c) 2024 Donatas Abraitis. All rights reserved. +License: 3-clause BSD. (See the COPYRIGHT file) +""" + +# https://datatracker.ietf.org/doc/html/draft-abraitis-bgp-version-capability + + +from exabgp.bgp.message.open.capability.capability import Capability +from exabgp.version import version + + +@Capability.register() +class SoftwareVersion(Capability): + ID = Capability.CODE.SOFTRWARE_VERSION + SOFTWARE_VERSION_MAX_LEN = 64 + + def __init__(self): + self.software_version = f"ExaBGP/{version}" + + def __str__(self): + return f'SoftwareVersion({self.software_version})' + + def json(self): + return f'{ "software-version": "{self.software_version}" }' + + def extract(self): + software_version = self.software_version.encode('utf-8') + if len(software_version) > self.SOFTWARE_VERSION_MAX_LEN: + software_version = software_version[: self.SOFTWARE_VERSION_MAX_LEN] + return [bytes([len(software_version)]) + software_version] + + @staticmethod + def unpack_capability(instance, data, capability=None): # pylint: disable=W0613 + l1 = data[0] + instance.software_version = data[1 : l1 + 1].decode('utf-8') + return instance diff --git a/src/exabgp/bgp/neighbor.py b/src/exabgp/bgp/neighbor.py index d3f0593c6..388f4bf66 100644 --- a/src/exabgp/bgp/neighbor.py +++ b/src/exabgp/bgp/neighbor.py @@ -59,6 +59,7 @@ class Capability(dict): 'route-refresh': 0, 'nexthop': None, 'aigp': None, + 'software-version': None, } defaults = { @@ -406,7 +407,7 @@ def string(self, with_changes=True): '\tmanual-eor %s;\n' '%s%s%s%s%s%s%s%s%s%s%s\n' '\tcapability {\n' - '%s%s%s%s%s%s%s%s%s\t}\n' + '%s%s%s%s%s%s%s%s%s%s\t}\n' '\tfamily {%s\n' '\t}\n' '\tnexthop {%s\n' @@ -446,6 +447,8 @@ def string(self, with_changes=True): '\t\troute-refresh %s;\n' % ('enable' if self['capability']['route-refresh'] else 'disable'), '\t\tgraceful-restart %s;\n' % (self['capability']['graceful-restart'] if self['capability']['graceful-restart'] else 'disable'), + '\t\tsoftware-version %s;\n' + % ('enable' if self['capability']['software-version'] else 'disable'), '\t\tnexthop %s;\n' % ('enable' if self['capability']['nexthop'] else 'disable'), '\t\tadd-path %s;\n' % (AddPath.string[self['capability']['add-path']] if self['capability']['add-path'] else 'disable'), diff --git a/src/exabgp/configuration/capability.py b/src/exabgp/configuration/capability.py index d1c51200f..2e487a470 100644 --- a/src/exabgp/configuration/capability.py +++ b/src/exabgp/configuration/capability.py @@ -71,6 +71,7 @@ class ParseCapability(Section): ' operational enable|disable;\n' ' refresh enable|disable;\n' ' extended-message enable|disable;\n' + ' software-version enable|disable;\n' '}\n' ) @@ -84,6 +85,7 @@ class ParseCapability(Section): 'route-refresh': boolean, 'aigp': boolean, 'extended-message': boolean, + 'software-version': boolean, } action = { @@ -96,6 +98,7 @@ class ParseCapability(Section): 'route-refresh': 'set-command', 'aigp': 'set-command', 'extended-message': 'set-command', + 'software-version': 'set-command', } default = { @@ -107,6 +110,7 @@ class ParseCapability(Section): 'route-refresh': True, 'aigp': True, 'extended-message': True, + 'software-version': False, } name = 'capability' diff --git a/src/exabgp/configuration/neighbor/__init__.py b/src/exabgp/configuration/neighbor/__init__.py index 1329111b9..08f3f684e 100644 --- a/src/exabgp/configuration/neighbor/__init__.py +++ b/src/exabgp/configuration/neighbor/__init__.py @@ -44,6 +44,7 @@ from exabgp.configuration.neighbor.parser import description from exabgp.configuration.neighbor.parser import inherit from exabgp.configuration.neighbor.parser import rate_limit +from exabgp.version import version from exabgp.environment import getenv diff --git a/tests/decode_test.py b/tests/decode_test.py index 9e495dd6b..d4be0370b 100755 --- a/tests/decode_test.py +++ b/tests/decode_test.py @@ -33,7 +33,7 @@ from exabgp.bgp.message.update.nlri import NLRI from exabgp.bgp.message.direction import Direction - + from exabgp.logger import log from exabgp.environment import getenv @@ -328,6 +328,7 @@ def __init__(self): 'aigp': None, 'operational': None, 'extended-message': True, + 'software-version': False, }, }) From b26b5e7f2fccf837b158ee10595c3df50d2ecf8c Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Thu, 11 Jul 2024 15:21:29 +0300 Subject: [PATCH 2/3] Use `main-SHA-YYMMDD` format for the EXABGP_VERSION If it's not a Git repository, unknown will be used, e.g.: ``` main-unknown-20240711 ``` Otherwise, something like: ``` main-c112832abd-20240711 ``` Signed-off-by: Donatas Abraitis --- sbin/exabgp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sbin/exabgp b/sbin/exabgp index 07bc2d50b..ec7f62c0a 100755 --- a/sbin/exabgp +++ b/sbin/exabgp @@ -9,9 +9,11 @@ if [ $? -eq 0 ]; then GIT_TAG=`git status | egrep 'detached at 3|4' | wc -l` if [ $GIT_TAG -eq 0 ]; then GIT_BRANCH=`git branch | grep "*" | awk '{ print $2}'` - GIT_COMMIT=`git log -1 | head -1 | awk '{ print $2 }'` - export EXABGP_VERSION="${GIT_BRANCH}-${GIT_COMMIT}" + GIT_COMMIT=`git rev-parse --short=10 HEAD` + export EXABGP_VERSION="main-${GIT_COMMIT}-$(date +%Y%m%d)" fi +else + export EXABGP_VERSION="main-unknown-$(date +%Y%m%d)" fi From c2a2ea8ebda34a9f3bc520d3a4759576208242cd Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Mon, 15 Jul 2024 16:34:33 +0300 Subject: [PATCH 3/3] Test if software version capability can be exchanged correctly Signed-off-by: Donatas Abraitis --- etc/exabgp/conf-cap-software-version.conf | 15 +++++++++++++++ qa/encoding/conf-cap-software-version.ci | 1 + qa/encoding/conf-cap-software-version.msg | 2 ++ 3 files changed, 18 insertions(+) create mode 100644 etc/exabgp/conf-cap-software-version.conf create mode 100644 qa/encoding/conf-cap-software-version.ci create mode 100644 qa/encoding/conf-cap-software-version.msg diff --git a/etc/exabgp/conf-cap-software-version.conf b/etc/exabgp/conf-cap-software-version.conf new file mode 100644 index 000000000..f51d37d80 --- /dev/null +++ b/etc/exabgp/conf-cap-software-version.conf @@ -0,0 +1,15 @@ +neighbor 127.0.0.1 { + router-id 10.0.0.2; + local-address 127.0.0.1; + local-as 65533; + peer-as 65533; + hold-time 180; + + capability { + software-version enable; + } + + family { + ipv4 unicast; + } +} diff --git a/qa/encoding/conf-cap-software-version.ci b/qa/encoding/conf-cap-software-version.ci new file mode 100644 index 000000000..2fec6bcaa --- /dev/null +++ b/qa/encoding/conf-cap-software-version.ci @@ -0,0 +1 @@ +conf-cap-software-version.conf diff --git a/qa/encoding/conf-cap-software-version.msg b/qa/encoding/conf-cap-software-version.msg new file mode 100644 index 000000000..4b18bd491 --- /dev/null +++ b/qa/encoding/conf-cap-software-version.msg @@ -0,0 +1,2 @@ +option:open:inspect-open-message +1:raw:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:0051:01:04FFFD00B40A000002340206010400010001020641040000FFFD02224B201F4578614247502F6D61696E2D346634323232663265642D3230323430373135