summaryrefslogtreecommitdiff
path: root/src/conf_mode/vpp_interfaces_bridge.py
blob: 4d65690cef6d9efe2a64f9381c126b41150fb23d (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
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
#
# Copyright (C) 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.config import Config
from vyos.configdict import get_interface_dict
from vyos import ConfigError
from vyos.utils.process import is_systemd_service_active

from vyos.ifconfig.vpp import VPPBridgeInterface
from vyos.vpp.config_deps import deps_bond_dict
from vyos.vpp.config_deps import deps_bridge_dict
from vyos.vpp.config_deps import deps_xconnect_dict
from vyos.vpp.config_verify import verify_member_conflicts
from vyos.vpp.config_verify import verify_vpp_interface_not_in_feature


def get_config(config=None) -> dict:
    """Get Bridge interface configuration

    Args:
        config (vyos.config.Config, optional): The VyOS configuration dictionary
    Returns:
        dict: Bridge interface configuration
    """
    if config:
        conf = config
    else:
        conf = Config()

    base = ['interfaces', 'vpp', 'bridge']

    ifname, config = get_interface_dict(conf, base)

    if not conf.exists(['vpp']) and not conf.exists(base):
        config['remove_vpp'] = True
        return config

    # Get global vpp interfaces for verify
    config['vpp_interfaces'] = conf.get_config_dict(
        ['vpp', 'settings', 'interface'],
        key_mangling=('-', '_'),
        get_first_key=True,
        no_tag_node_value_mangle=True,
    )

    # Get all gre interfaces config
    config['gre_interfaces'] = conf.get_config_dict(
        ['interfaces', 'vpp', 'gre'],
        key_mangling=('-', '_'),
        get_first_key=True,
        no_tag_node_value_mangle=True,
        with_defaults=True,
        with_recursive_defaults=True,
    )

    config['bond_members'] = deps_bond_dict(conf)
    config['bridge_members'] = deps_bridge_dict(conf)
    config['xconn_members'] = deps_xconnect_dict(conf)

    # VPP config for member-in-feature checks
    config['vpp'] = conf.get_config_dict(
        ['vpp'],
        key_mangling=('-', '_'),
        get_first_key=True,
        no_tag_node_value_mangle=True,
    )

    return config


def verify(config):
    if 'deleted' in config or 'remove_vpp' in config:
        return None

    if not is_systemd_service_active('vpp.service'):
        raise ConfigError(
            'Cannot configure VPP bridge interface: vpp.service is not running'
        )

    # Check if interface exists in vpp before adding to bridge-domain
    allowed_prefixes = ('vppbond', 'vppgre', 'vpplo', 'vppvxlan')

    if 'member' in config:
        bvi_exists = False
        for member, member_config in (
            config.get('member', {}).get('interface', {}).items()
        ):
            # Check if the interface exists in VPP settings or starts with allowed prefixes
            if not (
                member in config.get('vpp_interfaces', {})
                or member.startswith(allowed_prefixes)
            ):
                raise ConfigError(
                    f"Interface '{member}' not found in 'vpp settings interface' or does not start with allowed prefixes {allowed_prefixes}"
                )

            # Each interface can belong only to one bridge
            bridge_members = config['bridge_members'][member]
            if len(bridge_members) > 1:
                raise ConfigError(
                    f'Interface {member} is added to more than one bridge: {", ".join(bridge_members)}'
                )

            verify_member_conflicts(member, config, 'bridge')
            verify_vpp_interface_not_in_feature(member, config.get('vpp'))

            # Check if BVI is already defined, only one BVI per bridge domain is allowed
            if 'bvi' in member_config:
                if bvi_exists:
                    raise ConfigError("Only one BVI per bridge domain is allowed")
                if not member.startswith('vpplo'):
                    raise ConfigError("BVI can only be defined on loopback interface")
                bvi_exists = True

        # check GRE tunnels as part of the bridge, only tunnel-type "teb" is allowed
        #   set interfaces vpp bridge vppbr1 member interface vppgre1
        #   set interfaces vpp gre vppgre1 tunnel-type teb
        if member.startswith('vppgre'):
            if member in config.get('gre_interfaces'):
                gre_config = config.get('gre_interfaces').get(member)
                if gre_config.get('tunnel_type') != 'teb':
                    raise ConfigError(
                        f'GRE interface "{member}" in bridge must have tunnel-type "teb". '
                        f'Current tunnel-type is "{gre_config.get("tunnel_type")}".'
                    )


def generate(config):
    pass


def apply(config):
    if 'remove_vpp' in config:
        return None

    ifname = config.get('ifname')
    bridge = VPPBridgeInterface(ifname)
    bridge.remove()

    if 'deleted' in config:
        return

    bridge.update(config)

    return None


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