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

GRE: add encapsulation gre and gretap #2

Merged
merged 1 commit into from
Jan 24, 2025
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
25 changes: 25 additions & 0 deletions interface-definitions/vpp.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,31 @@
</valueHelp>
</properties>
<children>
<leafNode name="tunnel-type">
<properties>
<help>GRE tunnel type</help>
<completionHelp>
<list>erspan l3 teb</list>
</completionHelp>
<valueHelp>
<format>erspan</format>
<description>Encapsulated Remote Switched Port Analyzer</description>
</valueHelp>
<valueHelp>
<format>l3</format>
<description>Generic Routing Encapsulation (network layer)</description>
</valueHelp>
<valueHelp>
<format>teb</format>
<description>L2 Transparent Ethernet Bridge</description>
</valueHelp>
<constraint>
<regex>(erspan|l3|teb)</regex>
</constraint>
<constraintErrorMessage>Invalid encapsulation, must be one of: l3, teb or erspan</constraintErrorMessage>
</properties>
<defaultValue>l3</defaultValue>
</leafNode>
#include <include/source-address-ipv4-ipv6.xml.i>
#include <include/interface/tunnel-remote.xml.i>
#include <include/kernel-interface-tun.xml.i>
Expand Down
45 changes: 42 additions & 3 deletions python/vyos/vpp/interface/gre.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,58 @@ def show():


class GREInterface:
def __init__(self, ifname, source_address, remote, kernel_interface: str = ''):
"""
Class representing a GRE (Generic Routing Encapsulation) interface.

Attributes:
ifname (str): The interface name.
source_address (str): The source IP address for the GRE tunnel.
remote (str): The remote IP address for the GRE tunnel.
tunnel_type (str): The type of GRE tunnel. Defaults to 'l3'.
kernel_interface (str): The associated kernel interface. Defaults to an empty string.
instance (int): The instance number derived from the interface name.
vpp (VPPControl): An instance of the VPPControl class for interacting with the VPP API.
"""

# Mapping of tunnel types https://github.com/FDio/vpp/blob/stable/2406/src/plugins/gre/gre.api#L25-L35
TUNNEL_TYPE_MAP = {
"l3": 0,
"teb": 1,
"erspan": 2,
}

def __init__(
self,
ifname,
source_address,
remote,
tunnel_type: str = 'l3',
kernel_interface: str = '',
):
"""
Initialize a GREInterface instance.

Args:
ifname (str): The interface name.
source_address (str): The source IP address for the GRE tunnel.
remote (str): The remote IP address for the GRE tunnel.
tunnel_type (str): The type of GRE tunnel. Defaults to 'l3'.
kernel_interface (str): The associated kernel interface. Defaults to an empty string.
"""
self.instance = int(ifname.removeprefix('gre'))
self.ifname = ifname
self.src_address = source_address
self.dst_address = remote
self.tunnel_type = self.TUNNEL_TYPE_MAP[tunnel_type]
self.kernel_interface = kernel_interface
self.vpp = VPPControl()

def add(self):
"""Create GRE interface
https://github.com/FDio/vpp/blob/stable/2306/src/plugins/gre/gre.api
https://github.com/FDio/vpp/blob/stable/2406/src/plugins/gre/gre.api
Example:
from vyos.vpp.interface import GREInterface
a = GREInterface(ifname='gre0', source_address='192.0.2.1', remote='203.0.113.25')
a = GREInterface(ifname='gre0', source_address='192.0.2.1', remote='203.0.113.25', tunnel_type='l3')
a.add()
"""
self.vpp.api.gre_tunnel_add_del(
Expand All @@ -52,6 +90,7 @@ def add(self):
'src': self.src_address,
'dst': self.dst_address,
'instance': self.instance,
'type': self.tunnel_type,
},
)

Expand Down
4 changes: 2 additions & 2 deletions smoketest/scripts/cli/test_vpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def test_06_vpp_bonding(self):
self.assertRegex(
normalized_out,
r'BondEthernet23\s+\d+\s+up',
"Interface BondEthernet23 is not in the expected state 'up'."
"Interface BondEthernet23 is not in the expected state 'up'.",
)

# set kernel interface
Expand Down Expand Up @@ -617,7 +617,7 @@ def test_06_vpp_bonding(self):
self.assertRegex(
normalized_out,
r'BondEthernet23\s+\d+\s+up',
"Interface BondEthernet23 is not in the expected state 'up'."
"Interface BondEthernet23 is not in the expected state 'up'.",
)

# delete vpp kernel-interface vlan
Expand Down
5 changes: 3 additions & 2 deletions src/conf_mode/vpp_interfaces_gre.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def verify(config):
return None

# source-address and remote are mandatory options
required_keys = {'source_address', 'remote'}
required_keys = {'source_address', 'remote', 'tunnel_type'}
if not all(key in config for key in required_keys):
missing_keys = required_keys - set(config.keys())
raise ConfigError(
Expand Down Expand Up @@ -159,7 +159,8 @@ def apply(config):
src_addr = config.get('source_address')
dst_addr = config.get('remote')
kernel_interface = config.get('kernel_interface', '')
i = GREInterface(ifname, src_addr, dst_addr, kernel_interface)
tunnel_type = config.get('tunnel_type')
i = GREInterface(ifname, src_addr, dst_addr, tunnel_type, kernel_interface)
i.add()

# Add kernel-interface (LCP) if interface is not exist
Expand Down
Loading