diff options
author | Christian Poessinger <christian@poessinger.com> | 2022-04-27 21:42:22 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2022-04-28 15:03:36 +0200 |
commit | b0aeb2a9c196ea7048545e38e6f3c5759ff4a5ac (patch) | |
tree | 3bfc4a149cabf926adb9aea94ec6c0e890ba020d /src | |
parent | 92b834fdc0129b24de6683afe18a81a24f7bc495 (diff) | |
download | vyos-1x-b0aeb2a9c196ea7048545e38e6f3c5759ff4a5ac.tar.gz vyos-1x-b0aeb2a9c196ea7048545e38e6f3c5759ff4a5ac.zip |
arp: T4397: change CLI syntax to support interface and VRF bound ARP entries
* set protocols static arp interface eth0 address 192.0.2.1 mac 01:23:45:67:89:01
Diffstat (limited to 'src')
-rwxr-xr-x | src/conf_mode/arp.py | 28 | ||||
-rwxr-xr-x | src/migration-scripts/system/23-to-24 | 85 |
2 files changed, 103 insertions, 10 deletions
diff --git a/src/conf_mode/arp.py b/src/conf_mode/arp.py index 51a08bee5..1cd8f5451 100755 --- a/src/conf_mode/arp.py +++ b/src/conf_mode/arp.py @@ -30,9 +30,12 @@ def get_config(config=None): conf = Config() base = ['protocols', 'static', 'arp'] - arp = conf.get_config_dict(base) - tmp = node_changed(conf, base) - if tmp: arp.update({'removed' : node_changed(conf, base)}) + arp = conf.get_config_dict(base, get_first_key=True) + + if 'interface' in arp: + for interface in arp['interface']: + tmp = node_changed(conf, base + ['interface', interface, 'address'], recursive=True) + if tmp: arp['interface'][interface].update({'address_old' : tmp}) return arp @@ -46,14 +49,19 @@ def apply(arp): if not arp: return None - if 'removed' in arp: - for host in arp['removed']: - call(f'arp --delete {host}') + if 'interface' in arp: + for interface, interface_config in arp['interface'].items(): + # Delete old static ARP assignments first + if 'address_old' in interface_config: + for address in interface_config['address_old']: + call(f'ip neigh del {address} dev {interface}') - if 'arp' in arp: - for host, host_config in arp['arp'].items(): - mac = host_config['hwaddr'] - call(f'arp --set {host} {mac}') + # Add new static ARP entries to interface + if 'address' not in interface_config: + continue + for address, address_config in interface_config['address'].items(): + mac = address_config['mac'] + call(f'ip neigh add {address} lladdr {mac} dev {interface}') if __name__ == '__main__': try: diff --git a/src/migration-scripts/system/23-to-24 b/src/migration-scripts/system/23-to-24 new file mode 100755 index 000000000..5ea71d51a --- /dev/null +++ b/src/migration-scripts/system/23-to-24 @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2022 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# 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, see <http://www.gnu.org/licenses/>. + +import os + +from ipaddress import ip_interface +from ipaddress import ip_address +from sys import exit, argv +from vyos.configtree import ConfigTree + +if (len(argv) < 1): + print("Must specify file name!") + exit(1) + +file_name = argv[1] +with open(file_name, 'r') as f: + config_file = f.read() + +base = ['protocols', 'static', 'arp'] +tmp_base = ['protocols', 'static', 'arp-tmp'] +config = ConfigTree(config_file) + +def fixup_cli(config, path, interface): + if config.exists(path + ['address']): + for address in config.return_values(path + ['address']): + tmp = ip_interface(address) + if ip_address(host) in tmp.network.hosts(): + mac = config.return_value(tmp_base + [host, 'hwaddr']) + iface_path = ['protocols', 'static', 'arp', 'interface'] + config.set(iface_path + [interface, 'address', host, 'mac'], value=mac) + config.set_tag(iface_path) + config.set_tag(iface_path + [interface, 'address']) + continue + +if not config.exists(base): + # Nothing to do + exit(0) + +# We need a temporary copy of the config tree as the original one needs to be +# deleted first due to a change iun thge tagNode structure. +config.copy(base, tmp_base) +config.delete(base) + +for host in config.list_nodes(tmp_base): + for type in config.list_nodes(['interfaces']): + for interface in config.list_nodes(['interfaces', type]): + if_base = ['interfaces', type, interface] + fixup_cli(config, if_base, interface) + + if config.exists(if_base + ['vif']): + for vif in config.list_nodes(if_base + ['vif']): + vif_base = ['interfaces', type, interface, 'vif', vif] + fixup_cli(config, vif_base, f'{interface}.{vif}') + + if config.exists(if_base + ['vif-s']): + for vif_s in config.list_nodes(if_base + ['vif-s']): + vif_s_base = ['interfaces', type, interface, 'vif-s', vif_s] + fixup_cli(config, vif_s_base, f'{interface}.{vif_s}') + + if config.exists(if_base + ['vif-s', vif_s, 'vif-c']): + for vif_c in config.list_nodes(if_base + ['vif-s', vif_s, 'vif-c']): + vif_c_base = ['interfaces', type, interface, 'vif-s', vif_s, 'vif-c', vif_c] + fixup_cli(config, vif_c_base, f'{interface}.{vif_s}.{vif_c}') + +config.delete(tmp_base) + +try: + with open(file_name, 'w') as f: + f.write(config.to_string()) +except OSError as e: + print(f'Failed to save the modified config: {e}') + exit(1) |