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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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/>.
#
from vyos import ConfigError
from vyos.config import Config
from vyos.vpp.ipfix import IPFIX
from vyos.vpp.utils import cli_ifaces_list
from vyos.vpp.utils import vpp_iface_name_transform
from vyos.vpp.control_vpp import VPPControl
from vyos.vpp.config_verify import verify_vpp_interface_not_a_member
def get_config(config=None) -> dict:
if config:
conf = config
else:
conf = Config()
base = ['vpp', 'ipfix']
# Get config_dict with default values
config = conf.get_config_dict(
base,
key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True,
with_defaults=True,
with_recursive_defaults=True,
)
# Get effective config as we need full dictionary for deletion
effective_config = conf.get_config_dict(
base,
key_mangling=('-', '_'),
effective=True,
get_first_key=True,
no_tag_node_value_mangle=True,
)
if effective_config:
config.update({'effective': effective_config})
if not conf.exists(base):
config['remove'] = True
return config
# Add list of VPP interfaces to the config
config.update({'vpp_ifaces': cli_ifaces_list(conf)})
# VPP interface membership data for member-conflict checks
config['interfaces_vpp'] = conf.get_config_dict(
['interfaces', 'vpp'],
key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True,
)
return config
def verify(config):
if 'remove' in config:
return None
# Verify that at least one interface is configured
if 'interface' not in config or not config['interface']:
raise ConfigError(
'At least one interface must be configured for IPFIX monitoring'
)
# Verify that all interfaces specified exist in VPP
vpp = VPPControl()
for interface in config['interface']:
vpp_iface_name = vpp_iface_name_transform(interface)
if vpp.get_sw_if_index(vpp_iface_name) is None:
raise ConfigError(
f'{interface} must be a VPP interface for IPFIX monitoring'
)
verify_vpp_interface_not_a_member(interface, config)
# Verify that at least one collector is configured
if 'collector' not in config:
raise ConfigError('At least one IPFIX collector must be configured')
# Enforce that only one collector is configured (VPP limitation)
if len(config['collector']) > 1:
raise ConfigError('Only one IPFIX collector can be configured')
# Verify that source_address is specified
for c, c_conf in config.get('collector', {}).items():
if 'source_address' not in c_conf:
raise ConfigError(f'Source address must be specified for collector {c}')
# Verify active timeout is not greater than inactive timeout
if 'active_timeout' in config and 'inactive_timeout' in config:
active_timeout = int(config['active_timeout'])
inactive_timeout = int(config['inactive_timeout'])
if active_timeout > inactive_timeout:
raise ConfigError(
f'Active timeout ({active_timeout}) cannot be greater than inactive timeout ({inactive_timeout})'
)
def generate(config):
# No templates to render for IPFIX
pass
def apply(config):
i = IPFIX()
# Remove collectors
for c, c_conf in config.get('effective', {}).get('collector', {}).items():
i.ipfix_exporter_delete()
# Remove interfaces
for iface, iface_conf in config.get('effective', {}).get('interface', {}).items():
iface = vpp_iface_name_transform(iface)
direction = iface_conf.get('direction')
which = iface_conf.get('flow_variant')
i.flowprobe_interface_delete(iface, direction=direction, which=which)
if 'remove' in config:
return None
active_timeout = config.get('active_timeout')
inactive_timeout = config.get('inactive_timeout')
flowprobe_record = config.get('flowprobe_record')
# Flowprobe params
i.flowprobe_set_params(
active_timer=int(active_timeout),
passive_timer=int(inactive_timeout),
record_flags=list(flowprobe_record),
)
# Collectors
for c, c_conf in config.get('collector', {}).items():
collector_address = c
collector_port = c_conf.get('port')
src_address = c_conf.get('source_address')
template_interval = c_conf.get('template_interval')
path_mtu = c_conf.get('path_mtu')
udp_checksum = 'udp_checksum' in c_conf
i.collector_address = collector_address
i.src_address = src_address
i.collector_port = int(collector_port)
i.template_interval = int(template_interval)
i.path_mtu = int(path_mtu)
i.udp_checksum = udp_checksum
# VRF support is not currently implemented; exporter is always configured in the default VRF (0).
# Consider adding VRF support in the future if needed.
i.vrf_id = 0
i.set_ipfix_exporter()
# Interfaces
if 'interface' in config:
for iface, iface_config in config.get('interface', {}).items():
iface = vpp_iface_name_transform(iface)
direction = iface_config.get('direction')
which = iface_config.get('flow_variant')
i.flowprobe_interface_add(iface, direction=direction, which=which)
if __name__ == '__main__':
try:
c = get_config()
verify(c)
generate(c)
apply(c)
except ConfigError as e:
print(e)
exit(1)
|