summaryrefslogtreecommitdiff
path: root/src/conf_mode/vrf.py
blob: 75b19da23b7e2d3ba68033c545d1f0233df19a01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
#
# Copyright (C) 2020 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 sys import exit
from copy import deepcopy
from subprocess import check_call, CalledProcessError
from vyos.config import Config
from vyos.configdict import list_diff
from vyos import ConfigError
from vyos import vrf

default_config_data = {
    'vrf_add': [],
    'vrf_existing': [],
    'vrf_remove': []
}

def _cmd(command):
    """
    Run any arbitrary command on the system
    """
    try:
        check_call(command.split())
    except CalledProcessError as e:
        pass
        raise ConfigError(f'Error operationg on VRF: {e}')


def interfaces_with_vrf(match):
    matched = []
    config = Config()
    section = config.get_config_dict('interfaces')
    for type in section:
        interfaces = section[type]
        for name in interfaces:
            interface = interfaces[name]
            if 'vrf' in interface:
                v = interface.get('vrf', '')
                if v == match:
                    matched.append(name)
    return matched


def get_config():
    conf = Config()
    vrf_config = deepcopy(default_config_data)

    cfg_base = ['vrf']
    if not conf.exists(cfg_base):
        # get all currently effetive VRFs and mark them for deletion
        vrf_config['vrf_remove'] = conf.list_effective_nodes(cfg_base + ['name'])
        return vrf_config

    # Determine vrf interfaces (currently effective) - to determine which
    # vrf interface is no longer present and needs to be removed
    eff_vrf = conf.list_effective_nodes(cfg_base + ['name'])
    act_vrf = conf.list_nodes(cfg_base + ['name'])
    vrf_config['vrf_remove'] = list_diff(eff_vrf, act_vrf)

    # read in individual VRF definition and build up
    # configuration
    for name in conf.list_nodes(cfg_base + ['name']):
        vrf_inst = {
            'description' : '\0',
            'members': [],
            'name' : name,
            'table' : '',
            'table_mod': False
        }
        conf.set_level(cfg_base + ['name', name])

        if conf.exists(['table']):
            # VRF table can't be changed on demand, thus we need to read in the
            # current and the effective routing table number
            act_table = conf.return_value(['table'])
            eff_table = conf.return_effective_value(['table'])
            vrf_inst['table'] = act_table
            if eff_table and eff_table != act_table:
                vrf_inst['table_mod'] = True

        if conf.exists(['description']):
            vrf_inst['description'] = conf.return_value(['description'])

        # find member interfaces of this particulat VRF
        vrf_inst['members'] = interfaces_with_vrf(name)

        # append individual VRF configuration to global configuration list
        vrf_config['vrf_add'].append(vrf_inst)

    return vrf_config


def verify(vrf_config):
    # ensure VRF is not assigned to any interface
    for vrf in vrf_config['vrf_add']:
        if vrf['table_mod']:
            raise ConfigError('VRF routing table id modification is not possible')

    # add test to see if routing table already exists or not?

    return None


def generate(vrf_config):
    return None

def apply(vrf_config):
    # https://github.com/torvalds/linux/blob/master/Documentation/networking/vrf.txt

    # set the default VRF global behaviour
    #sysctl('net.ipv4.tcp_l3mdev_accept', command['bind']['ipv4'])
    #sysctl('net.ipv4.udp_l3mdev_accept', command['bind']['ipv4'])

    for vrf_name in vrf_config['vrf_remove']:
        if os.path.isdir(f'/sys/class/net/{vrf_name}'):
            _cmd(f'ip link delete dev {vrf_name}')

    for vrf in vrf_config['vrf_add']:
        name = vrf['name']
        table = vrf['table']

        if not os.path.isdir(f'/sys/class/net/{name}'):
            _cmd(f'ip link add {name} type vrf table {table}')
            _cmd(f'ip link set dev {name} up')
            _cmd(f'ip -4 rule add oif {name} lookup {table}')
            _cmd(f'ip -4 rule add iif {name} lookup {table}')
            _cmd(f'ip -6 rule add oif {name} lookup {table}')
            _cmd(f'ip -6 rule add iif {name} lookup {table}')

        # set VRF description for e.g. SNMP monitoring
        with open(f'/sys/class/net/{name}/ifalias', 'w') as f:
            f.write(vrf['description'])

    return None

if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        exit(1)