|
| 1 | +# Copyright (C) 2025 VyOS maintainers and contributors |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License version 2 or later as |
| 5 | +# published by the Free Software Foundation. |
| 6 | +# |
| 7 | +# This program is distributed in the hope that it will be useful, |
| 8 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | +# GNU General Public License for more details. |
| 11 | +# |
| 12 | +# You should have received a copy of the GNU General Public License |
| 13 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | +# |
| 15 | + |
| 16 | +from vyos import ConfigError |
| 17 | +from vyos.config import Config |
| 18 | +from vyos.vpp.utils import cli_ifaces_list |
| 19 | +from vyos.vpp import VPPControl |
| 20 | + |
| 21 | + |
| 22 | +def get_config(config=None) -> dict: |
| 23 | + if config: |
| 24 | + conf = config |
| 25 | + else: |
| 26 | + conf = Config() |
| 27 | + |
| 28 | + base = ['vpp', 'sflow'] |
| 29 | + |
| 30 | + # Get config_dict with default values |
| 31 | + config = conf.get_config_dict( |
| 32 | + base, |
| 33 | + key_mangling=('-', '_'), |
| 34 | + get_first_key=True, |
| 35 | + no_tag_node_value_mangle=True, |
| 36 | + with_defaults=True, |
| 37 | + with_recursive_defaults=True, |
| 38 | + ) |
| 39 | + |
| 40 | + # Get effective config as we need full dictionary for deletion |
| 41 | + effective_config = conf.get_config_dict( |
| 42 | + base, |
| 43 | + key_mangling=('-', '_'), |
| 44 | + effective=True, |
| 45 | + get_first_key=True, |
| 46 | + no_tag_node_value_mangle=True, |
| 47 | + ) |
| 48 | + |
| 49 | + # Get system sflow configuration to check for server |
| 50 | + system_sflow = conf.get_config_dict( |
| 51 | + ['system', 'sflow'], |
| 52 | + key_mangling=('-', '_'), |
| 53 | + get_first_key=True, |
| 54 | + no_tag_node_value_mangle=True, |
| 55 | + ) |
| 56 | + |
| 57 | + if system_sflow: |
| 58 | + config['system_sflow'] = system_sflow |
| 59 | + |
| 60 | + if not config: |
| 61 | + config['remove'] = True |
| 62 | + return config |
| 63 | + |
| 64 | + # Add list of VPP interfaces to the config |
| 65 | + config.update({'vpp_ifaces': cli_ifaces_list(conf)}) |
| 66 | + |
| 67 | + if effective_config: |
| 68 | + config.update({'effective': effective_config}) |
| 69 | + |
| 70 | + return config |
| 71 | + |
| 72 | + |
| 73 | +def verify(config): |
| 74 | + if 'remove' in config: |
| 75 | + return None |
| 76 | + |
| 77 | + # Check if interface section exists |
| 78 | + if 'interface' not in config: |
| 79 | + return None |
| 80 | + |
| 81 | + # Verify that all interfaces specified exist in VPP |
| 82 | + for interface in config['interface']: |
| 83 | + if interface not in config['vpp_ifaces']: |
| 84 | + raise ConfigError( |
| 85 | + f'{interface} must be a VPP interface for sFlow monitoring' |
| 86 | + ) |
| 87 | + |
| 88 | + # Verify sample rate is a positive integer |
| 89 | + if 'sample_rate' in config: |
| 90 | + try: |
| 91 | + sample_rate = int(config['sample_rate']) |
| 92 | + if sample_rate <= 0: |
| 93 | + raise ConfigError('sFlow sample rate must be a positive integer') |
| 94 | + except ValueError: |
| 95 | + raise ConfigError('sFlow sample rate must be a valid integer') |
| 96 | + |
| 97 | + # Verify that system sflow has enable-vpp defined |
| 98 | + if 'system_sflow' not in config or 'vpp' not in config.get('system_sflow', {}): |
| 99 | + raise ConfigError( |
| 100 | + 'sFlow enable-vpp must be defined under system sflow configuration' |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def generate(config): |
| 105 | + # No templates to render for sFlow |
| 106 | + pass |
| 107 | + |
| 108 | + |
| 109 | +def apply(config): |
| 110 | + # Initialize VPP control API |
| 111 | + vpp = VPPControl(attempts=20, interval=500) |
| 112 | + |
| 113 | + if 'remove' in config: |
| 114 | + # Disable sFlow on all interfaces |
| 115 | + for interface in config.get('effective', {}).get('interface', []): |
| 116 | + vpp.cli_cmd(f'sflow enable-disable {interface} disable') |
| 117 | + return None |
| 118 | + |
| 119 | + # Configure sample rate if specified |
| 120 | + if 'sample_rate' in config: |
| 121 | + vpp.cli_cmd(f'sflow sampling-rate {config["sample_rate"]}') |
| 122 | + |
| 123 | + # Configure interfaces |
| 124 | + if 'interface' in config: |
| 125 | + # Enable sFlow on specified interfaces |
| 126 | + for interface in config['interface']: |
| 127 | + vpp.cli_cmd(f'sflow enable-disable {interface}') |
| 128 | + |
| 129 | + # Disable sFlow on interfaces that were removed from config |
| 130 | + effective_interfaces = config.get('effective', {}).get('interface', []) |
| 131 | + if effective_interfaces: |
| 132 | + for interface in effective_interfaces: |
| 133 | + if interface not in config['interface']: |
| 134 | + vpp.cli_cmd(f'sflow enable-disable {interface} disable') |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + try: |
| 139 | + c = get_config() |
| 140 | + verify(c) |
| 141 | + generate(c) |
| 142 | + apply(c) |
| 143 | + except ConfigError as e: |
| 144 | + print(e) |
| 145 | + exit(1) |
0 commit comments