Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.
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
30 changes: 7 additions & 23 deletions python/vyos/vpp/interface/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from vyos.vpp import VPPControl
from vyos.vpp.control_host import set_promisc
from vyos.vpp.interface.interface import Interface


class BondInterface:
class BondInterface(Interface):
def __init__(
self,
ifname,
mode: str = '',
load_balance: int = 0,
mac: str = '',
kernel_interface: str = '',
state: str = 'up',
):
super().__init__(ifname)
self.instance = int(ifname.removeprefix('bond'))
self.ifname = f'BondEthernet{self.instance}'
self.mode = mode
self.load_balance = load_balance
self.mac = mac
self.kernel_interface = kernel_interface
self.vpp = VPPControl()
self.state = state

def add(self):
"""Create Bond interface
Expand All @@ -55,6 +57,8 @@ def add(self):
self.vpp.api.bond_create2(**create_args)
if self.kernel_interface:
self.vpp.lcp_pair_add(self.ifname, self.kernel_interface)
# Set interface state
self.set_state(self.state)

def delete(self):
"""Delete Bond interface
Expand Down Expand Up @@ -112,23 +116,3 @@ def kernel_delete(self):
a.kernel_delete()
"""
self.vpp.lcp_pair_del(self.ifname, self.kernel_interface)

def set_state_up(self):
"""Set Bond interface state to UP
Example:
from vyos.vpp.interface import BondInterface
a = BondInterface(ifname='bond0')
a.set_state_up()
"""
bond_if_index = self.vpp.get_sw_if_index(self.ifname)
self.vpp.api.sw_interface_set_flags(sw_if_index=bond_if_index, flags=1)

def set_state_down(self):
"""Set Bond interface state to DOWN
Example:
from vyos.vpp.interface import BondInterface
a = BondInterface(ifname='bond0')
a.set_state_down()
"""
bond_if_index = self.vpp.get_sw_if_index(self.ifname)
self.vpp.api.sw_interface_set_flags(sw_if_index=bond_if_index, flags=0)
49 changes: 49 additions & 0 deletions python/vyos/vpp/interface/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Copyright (C) 2025 VyOS Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from vyos.vpp import VPPControl


class Interface:
def __init__(self, ifname):
self.ifname = ifname
self.vpp = VPPControl()

def set_state(self, state: str):
"""Set interface state to UP or DOWN
Args:
state (str): The state of the interface. Options are 'up' and 'down'.
Example:
from vyos.vpp.interface import Interface
a = Interface(ifname='eth0')
a.set_state(state='up')
"""
if state not in ['up', 'down']:
raise ValueError(f"Invalid state: {state}")
state_flag = 1 if state == 'up' else 0
if_index = self.vpp.get_sw_if_index(self.ifname)
self.vpp.api.sw_interface_set_flags(sw_if_index=if_index, flags=state_flag)

def get_state(self):
"""Get interface state
Example:
from vyos.vpp.interface import Interface
a = Interface(ifname='eth0')
a.get_state()
"""
if_index = self.vpp.get_sw_if_index(self.ifname)
return self.vpp.api.sw_interface_dump(sw_if_index=if_index)[0]['flags']
5 changes: 2 additions & 3 deletions src/conf_mode/vpp_interfaces_bonding.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ def apply(config):
members = config.get('member', {}).get('interface', [])
mac = config.get('mac', '')
kernel_interface = config.get('kernel_interface', '')
state = 'up' if 'disable' not in config else 'down'

i = BondInterface(ifname, mode, lb, mac, kernel_interface)
i = BondInterface(ifname, mode, lb, mac, kernel_interface, state)
# Introduce a delay to address instability in the VPP API, which may fail to create the LCP
# or establish a connection. This should be reviewed and resolved in future releases.
time.sleep(2)
Expand All @@ -202,8 +203,6 @@ def apply(config):
for member in members:
i.add_member(interface=member)

i.set_state_up()

call_dependents()

return None
Expand Down
Loading