diff options
| author | Viacheslav Hletenko <v.gletenko@vyos.io> | 2025-01-27 12:29:53 +0000 |
|---|---|---|
| committer | Viacheslav Hletenko <v.gletenko@vyos.io> | 2025-01-27 12:49:46 +0000 |
| commit | 9e1c4f73a01ce27bb840072ee771da68593cbe08 (patch) | |
| tree | 74af5a3e0e0f60d00e47d46a607637231480d564 | |
| parent | f76e349c11ae30d104ae09fef4e73c85f35d1885 (diff) | |
| download | vyos-1x-9e1c4f73a01ce27bb840072ee771da68593cbe08.tar.gz vyos-1x-9e1c4f73a01ce27bb840072ee771da68593cbe08.zip | |
Add common interface state
| -rw-r--r-- | python/vyos/vpp/interface/bond.py | 30 | ||||
| -rw-r--r-- | python/vyos/vpp/interface/interface.py | 49 |
2 files changed, 56 insertions, 23 deletions
diff --git a/python/vyos/vpp/interface/bond.py b/python/vyos/vpp/interface/bond.py index a90f2950b..6a96bce58 100644 --- a/python/vyos/vpp/interface/bond.py +++ b/python/vyos/vpp/interface/bond.py @@ -15,11 +15,11 @@ # 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, @@ -27,14 +27,16 @@ class BondInterface: 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 @@ -55,6 +57,8 @@ class BondInterface: 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 @@ -112,23 +116,3 @@ class BondInterface: 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) diff --git a/python/vyos/vpp/interface/interface.py b/python/vyos/vpp/interface/interface.py index e69de29bb..33dc779c2 100644 --- a/python/vyos/vpp/interface/interface.py +++ b/python/vyos/vpp/interface/interface.py @@ -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'] |
