File: netdef.py

package info (click to toggle)
netplan.io 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,268 kB
  • sloc: python: 34,640; ansic: 14,096; xml: 4,989; javascript: 2,165; sh: 513; makefile: 118
file content (397 lines) | stat: -rw-r--r-- 13,704 bytes parent folder | download | duplicates (3)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Copyright (C) 2023 Canonical, Ltd.
# Author: Lukas Märdian <slyon@ubuntu.com>
#
# 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; version 3.
#
# 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 dataclasses import dataclass
from typing import Optional

from ._netplan_cffi import ffi, lib
from ._utils import _string_realloc_call_no_error, NetplanException


class NetDefinition():
    def __init__(self, np_state, ptr):
        self._ptr = ptr
        # We hold on to this to avoid the underlying pointer being invalidated by
        # the GC invoking netplan_state_free
        self._parent = np_state

    def __eq__(self, other: 'NetDefinition') -> bool:
        if not hasattr(other, '_ptr'):
            return False
        return self._ptr == other._ptr

    def _match_interface(self, iface_name: str = None, iface_driver: str = None, iface_mac: str = None) -> bool:
        return bool(lib.netplan_netdef_match_interface(
            self._ptr,
            iface_name.encode('utf-8') if iface_name else ffi.NULL,
            iface_mac.encode('utf-8') if iface_mac else ffi.NULL,
            iface_driver.encode('utf-8') if iface_driver else ffi.NULL))

    @property
    def addresses(self) -> '_NetdefAddressIterator':
        return _NetdefAddressIterator(self._ptr)

    @property
    def dhcp4(self) -> bool:
        return bool(lib.netplan_netdef_get_dhcp4(self._ptr))

    @property
    def dhcp6(self) -> bool:
        return bool(lib.netplan_netdef_get_dhcp6(self._ptr))

    @property
    def link_local(self) -> list:
        linklocal = []
        if bool(lib.netplan_netdef_get_link_local_ipv4(self._ptr)):
            linklocal.append('ipv4')
        if bool(lib.netplan_netdef_get_link_local_ipv6(self._ptr)):
            linklocal.append('ipv6')
        return linklocal

    @property
    def accept_ra(self) -> Optional[bool]:
        # Even though 'accept-ra' is defined as a boolean in Netplan, it's mapped
        # to three different values inside libnetplan: kernel, enabled and disabled.
        # 'kernel' is the default value when it's not set.
        value = lib.netplan_netdef_get_accept_ra(self._ptr)
        if value == 1:
            return True
        elif value == 2:
            return False
        else:
            return None

    @property
    def nameserver_addresses(self) -> '_NetdefNameserverIterator':
        return _NetdefNameserverIterator(self._ptr)

    @property
    def nameserver_search(self) -> '_NetdefSearchDomainIterator':
        return _NetdefSearchDomainIterator(self._ptr)

    @property
    def routes(self) -> '_NetdefRouteIterator':
        return _NetdefRouteIterator(self._ptr)

    @property
    def _gateway4(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib._netplan_netdef_get_gateway4(self._ptr, b, len(b)))

    @property
    def _gateway6(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib._netplan_netdef_get_gateway6(self._ptr, b, len(b)))

    @property
    def macaddress(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_macaddress(self._ptr, b, len(b)))

    @property
    def _has_match(self) -> bool:
        return bool(lib.netplan_netdef_has_match(self._ptr))

    @property
    def set_name(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_set_name(self._ptr, b, len(b)))

    @property
    def critical(self) -> bool:
        return bool(lib._netplan_netdef_get_critical(self._ptr))

    @property
    def links(self) -> dict:
        d = dict()
        if sriov_link := lib.netplan_netdef_get_sriov_link(self._ptr):
            d['sriov'] = NetDefinition(self._parent, sriov_link)

        if vlan_link := lib.netplan_netdef_get_vlan_link(self._ptr):
            d['vlan'] = NetDefinition(self._parent, vlan_link)

        if bridge_link := lib.netplan_netdef_get_bridge_link(self._ptr):
            d['bridge'] = NetDefinition(self._parent, bridge_link)

        if bond_link := lib.netplan_netdef_get_bond_link(self._ptr):
            d['bond'] = NetDefinition(self._parent, bond_link)

        if vrf_link := lib.netplan_netdef_get_vrf_link(self._ptr):
            d['vrf'] = NetDefinition(self._parent, vrf_link)

        # TODO: ovs vs veth? Should we use the same field?
        if peer_link := lib.netplan_netdef_get_peer_link(self._ptr):
            d['peer'] = NetDefinition(self._parent, peer_link)
        return d

    @property
    def _vlan_id(self) -> int:
        vlan_id = lib._netplan_netdef_get_vlan_id(self._ptr)
        if vlan_id == lib.UINT_MAX:
            return None
        return vlan_id

    @property
    def _has_sriov_vlan_filter(self) -> bool:
        return bool(lib._netplan_netdef_get_sriov_vlan_filter(self._ptr))

    @property
    def backend(self) -> str:
        return ffi.string(lib.netplan_backend_name(lib.netplan_netdef_get_backend(self._ptr))).decode('utf-8')

    @property
    def type(self) -> str:
        return ffi.string(lib.netplan_def_type_name(lib.netplan_netdef_get_type(self._ptr))).decode('utf-8')

    @property
    def id(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_id(self._ptr, b, len(b)))

    @property
    def filepath(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_filepath(self._ptr, b, len(b)))

    @property
    def _embedded_switch_mode(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib._netplan_netdef_get_embedded_switch_mode(self._ptr, b, len(b)))

    @property
    def _delay_virtual_functions_rebind(self) -> bool:
        return bool(lib._netplan_netdef_get_delay_virtual_functions_rebind(self._ptr))

    @property
    def _vf_count(self) -> int:
        ref = ffi.new('NetplanError **')
        count = lib._netplan_state_get_vf_count_for_def(self._parent._ptr, self._ptr, ref)
        if count < 0:
            err = ref[0]
            msg = _string_realloc_call_no_error(lambda b: lib.netplan_error_message(err, b, len(b)))
            raise NetplanException(msg)
        return count

    @property
    def _bond_mode(self) -> str:
        return _string_realloc_call_no_error(lambda b: lib._netplan_netdef_get_bond_mode(self._ptr, b, len(b)))

    @property
    def _is_trivial_compound_itf(self) -> bool:
        '''
        Returns True if the interface is a compound interface (bond or bridge),
        and its configuration is trivial, without any variation from the defaults.
        '''
        return bool(lib._netplan_netdef_is_trivial_compound_itf(self._ptr))


class NetDefinitionIterator():
    def __init__(self, np_state, dev_type: str = None):
        # To keep things valid, keep a reference to the parent state
        self.np_state = np_state
        np_type = dev_type.encode('utf-8') if dev_type else ffi.NULL
        self.iterator = lib._netplan_state_new_netdef_pertype_iter(np_state._ptr, np_type)

    def __del__(self):
        lib._netplan_netdef_pertype_iter_free(self.iterator)

    def __iter__(self):
        return self

    def __next__(self):
        next_value = lib._netplan_netdef_pertype_iter_next(self.iterator)
        if not next_value:
            raise StopIteration
        return NetDefinition(self.np_state, next_value)


class NetplanAddress:
    def __init__(self, address: str, lifetime: str, label: str):
        self.address = address
        self.lifetime = lifetime
        self.label = label

    def __str__(self) -> str:
        return self.address


class _NetdefAddressIterator:
    def __init__(self, netdef: NetDefinition):
        self.netdef = netdef
        self.iterator = lib._netplan_netdef_new_address_iter(netdef)

    def __del__(self):
        lib._netplan_address_iter_free(self.iterator)

    def __iter__(self):
        return self

    def __next__(self):
        next_value = lib._netplan_address_iter_next(self.iterator)
        if not next_value:
            raise StopIteration
        content = next_value
        # XXX: Introduce getters for .address/.lifetime/.label, to avoid
        #      exposing the 'address_iter' struct in _netplan_cffi.so
        address = ffi.string(content.address).decode('utf-8') if content.address else None
        lifetime = ffi.string(content.lifetime).decode('utf-8') if content.lifetime else None
        label = ffi.string(content.label).decode('utf-8') if content.label else None
        return NetplanAddress(address, lifetime, label)


class _NetdefNameserverIterator:
    def __init__(self, netdef: NetDefinition):
        self.netdef = netdef
        self.iterator = lib._netplan_netdef_new_nameserver_iter(netdef)

    def __del__(self):
        lib._netplan_nameserver_iter_free(self.iterator)

    def __iter__(self):
        return self

    def __next__(self):
        next_value = lib._netplan_nameserver_iter_next(self.iterator)
        if not next_value:
            raise StopIteration
        return ffi.string(next_value).decode('utf-8')


class _NetdefSearchDomainIterator:
    def __init__(self, netdef):
        self.netdef = netdef
        self.iterator = lib._netplan_netdef_new_search_domain_iter(netdef)

    def __del__(self):
        lib._netplan_search_domain_iter_free(self.iterator)

    def __iter__(self):
        return self

    def __next__(self):
        next_value = lib._netplan_search_domain_iter_next(self.iterator)
        if not next_value:
            raise StopIteration
        return ffi.string(next_value).decode('utf-8')


@dataclass
class NetplanRoute:
    _METRIC_UNSPEC_ = lib.UINT_MAX
    _ADVMSS_UNSPEC_ = 0
    _TABLE_UNSPEC_ = 0

    to: str = None
    via: str = None
    from_addr: str = None
    type: str = 'unicast'
    scope: str = 'global'
    protocol: str = None
    table: int = _TABLE_UNSPEC_
    family: int = -1
    metric: int = _METRIC_UNSPEC_
    mtubytes: int = 0
    congestion_window: int = 0
    advertised_receive_window: int = 0
    onlink: bool = False
    advertised_mss: int = _ADVMSS_UNSPEC_

    def __str__(self):
        route = ""
        if self.to:
            route = route + self.to
        if self.via:
            route = route + f' via {self.via}'
        if self.type:
            route = route + f' type {self.type}'
        if self.scope:
            route = route + f' scope {self.scope}'
        if self.from_addr:
            route = route + f' src {self.from_addr}'
        if self.metric < self._METRIC_UNSPEC_:
            route = route + f' metric {self.metric}'
        if self.table > self._TABLE_UNSPEC_:
            route = route + f' table {self.table}'
        return route.strip()

    def to_dict(self):
        route = {}
        if self.family >= 0:
            route['family'] = self.family
        if self.to:
            route['to'] = self.to
        if self.via:
            route['via'] = self.via
        if self.from_addr:
            route['from'] = self.from_addr
        if self.metric < self._METRIC_UNSPEC_:
            route['metric'] = self.metric
        if self.table > self._TABLE_UNSPEC_:
            route['table'] = self.table

        route['type'] = self.type

        return route

    def __hash__(self):
        return hash(
            (self.to, self.via,
             self.from_addr, self.table,
             self.family, self.metric,
             self.type, self.scope))

    def __eq__(self, route):
        return (
            self.to == route.to and
            self.via == route.via and
            self.from_addr == route.from_addr and
            self.table == route.table and
            self.family == route.family and
            self.metric == route.metric and
            self.type == route.type and
            self.scope == route.scope
        )


class _NetdefRouteIterator:
    def __init__(self, netdef):
        self.netdef = netdef
        self.iterator = lib._netplan_netdef_new_route_iter(netdef)

    def __del__(self):
        lib._netplan_route_iter_free(self.iterator)

    def __iter__(self):
        return self

    def __next__(self):
        next_value = lib._netplan_route_iter_next(self.iterator)
        if not next_value:
            raise StopIteration

        # The field 'from' happens to be a reserved keyword in Python
        from_addr = getattr(next_value, 'from')

        route = {
            'to': ffi.string(next_value.to).decode('utf-8') if next_value.to else None,
            'via': ffi.string(next_value.via).decode('utf-8') if next_value.via else None,
            'from_addr': ffi.string(from_addr).decode('utf-8') if from_addr else None,
            'type': ffi.string(next_value.type).decode('utf-8') if next_value.type else None,
            'scope': ffi.string(next_value.scope).decode('utf-8') if next_value.scope else None,
            'protocol': None,
            'table': next_value.table,
            'family': next_value.family,
            'metric': next_value.metric,
            'mtubytes': next_value.mtubytes,
            'congestion_window': next_value.congestion_window,
            'advertised_receive_window': next_value.advertised_receive_window,
            'onlink': next_value.onlink,
            'advertised_mss': next_value.advmss
        }

        return NetplanRoute(**route)