summaryrefslogtreecommitdiffstats
path: root/python/netlink/route/link.py
blob: 5ec14b269e307c1990a2a25a9ea7f9b1d113d4a7 (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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#
# Copyright (c) 2011 Thomas Graf <tgraf@suug.ch>
#

"""Module providing access to network links

This module provides an interface to view configured network links,
modify them and to add and delete virtual network links.

The following is a basic example:
    import netlink.core as netlink
    import netlink.route.link as link

    sock = netlink.Socket()
    sock.connect(netlink.NETLINK_ROUTE)

    cache = link.LinkCache()	# create new empty link cache
    cache.refill(sock)		# fill cache with all configured links
    eth0 = cache['eth0']		# lookup link "eth0"
    print eth0			# print basic configuration

The module contains the following public classes:

  - Link -- Represents a network link. Instances can be created directly
        via the constructor (empty link objects) or via the refill()
        method of a LinkCache.
  - LinkCache -- Derived from netlink.Cache, holds any number of
         network links (Link instances). Main purpose is to keep
         a local list of all network links configured in the
         kernel.

The following public functions exist:
  - get_from_kernel(socket, name)

"""

from __future__ import absolute_import

__version__ = '0.1'
__all__ = [
    'LinkCache',
    'Link',
    'get_from_kernel',
]

import socket
from .. import core as netlink
from .. import capi as core_capi
from .  import capi as capi
from .links  import inet as inet
from .. import util as util

# Link statistics definitions
RX_PACKETS = 0
TX_PACKETS = 1
RX_BYTES = 2
TX_BYTES = 3
RX_ERRORS = 4
TX_ERRORS = 5
RX_DROPPED = 6
TX_DROPPED = 7
RX_COMPRESSED = 8
TX_COMPRESSED = 9
RX_FIFO_ERR = 10
TX_FIFO_ERR = 11
RX_LEN_ERR = 12
RX_OVER_ERR = 13
RX_CRC_ERR = 14
RX_FRAME_ERR = 15
RX_MISSED_ERR = 16
TX_ABORT_ERR = 17
TX_CARRIER_ERR = 18
TX_HBEAT_ERR = 19
TX_WIN_ERR = 20
COLLISIONS = 21
MULTICAST = 22
IP6_INPKTS = 23
IP6_INHDRERRORS = 24
IP6_INTOOBIGERRORS = 25
IP6_INNOROUTES = 26
IP6_INADDRERRORS = 27
IP6_INUNKNOWNPROTOS = 28
IP6_INTRUNCATEDPKTS = 29
IP6_INDISCARDS = 30
IP6_INDELIVERS = 31
IP6_OUTFORWDATAGRAMS = 32
IP6_OUTPKTS = 33
IP6_OUTDISCARDS = 34
IP6_OUTNOROUTES = 35
IP6_REASMTIMEOUT = 36
IP6_REASMREQDS = 37
IP6_REASMOKS = 38
IP6_REASMFAILS = 39
IP6_FRAGOKS = 40
IP6_FRAGFAILS = 41
IP6_FRAGCREATES = 42
IP6_INMCASTPKTS = 43
IP6_OUTMCASTPKTS = 44
IP6_INBCASTPKTS = 45
IP6_OUTBCASTPKTS = 46
IP6_INOCTETS = 47
IP6_OUTOCTETS = 48
IP6_INMCASTOCTETS = 49
IP6_OUTMCASTOCTETS = 50
IP6_INBCASTOCTETS = 51
IP6_OUTBCASTOCTETS = 52
ICMP6_INMSGS = 53
ICMP6_INERRORS = 54
ICMP6_OUTMSGS = 55
ICMP6_OUTERRORS = 56

class LinkCache(netlink.Cache):
    """Cache of network links"""

    def __init__(self, family=socket.AF_UNSPEC, cache=None):
        if not cache:
            cache = self._alloc_cache_name('route/link')

        self._info_module = None
        self._protocol = netlink.NETLINK_ROUTE
        self._nl_cache = cache
        self._set_arg1(family)

    def __getitem__(self, key):
        if type(key) is int:
            link = capi.rtnl_link_get(self._nl_cache, key)
        else:
            link = capi.rtnl_link_get_by_name(self._nl_cache, key)

        if link is None:
            raise KeyError()
        else:
            return Link.from_capi(link)

    @staticmethod
    def _new_object(obj):
        return Link(obj)

    def _new_cache(self, cache):
        return LinkCache(family=self.arg1, cache=cache)

class Link(netlink.Object):
    """Network link"""

    def __init__(self, obj=None):
        netlink.Object.__init__(self, 'route/link', 'link', obj)
        self._rtnl_link = self._obj2type(self._nl_object)

        if self.type:
            self._module_lookup('netlink.route.links.' + self.type)

        self.inet = inet.InetLink(self)
        self.af = {'inet' : self.inet }

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, tb):
        if exc_type is None:
            self.change()
        else:
            return false

    @classmethod
    def from_capi(cls, obj):
        return cls(capi.link2obj(obj))

    @staticmethod
    def _obj2type(obj):
        return capi.obj2link(obj)

    def __cmp__(self, other):
        return self.ifindex - other.ifindex

    @staticmethod
    def _new_instance(obj):
        if not obj:
            raise ValueError()

        return Link(obj)

    @property
    @netlink.nlattr(type=int, immutable=True, fmt=util.num)
    def ifindex(self):
        """interface index"""
        return capi.rtnl_link_get_ifindex(self._rtnl_link)

    @ifindex.setter
    def ifindex(self, value):
        capi.rtnl_link_set_ifindex(self._rtnl_link, int(value))

        # ifindex is immutable but we assume that if _orig does not
        # have an ifindex specified, it was meant to be given here
        if capi.rtnl_link_get_ifindex(self._orig) == 0:
            capi.rtnl_link_set_ifindex(self._orig, int(value))

    @property
    @netlink.nlattr(type=str, fmt=util.bold)
    def name(self):
        """Name of link"""
        return capi.rtnl_link_get_name(self._rtnl_link)

    @name.setter
    def name(self, value):
        capi.rtnl_link_set_name(self._rtnl_link, value)

        # name is the secondary identifier, if _orig does not have
        # the name specified yet, assume it was meant to be specified
        # here. ifindex will always take priority, therefore if ifindex
        # is specified as well, this will be ignored automatically.
        if capi.rtnl_link_get_name(self._orig) is None:
            capi.rtnl_link_set_name(self._orig, value)

    @property
    @netlink.nlattr(type=str, fmt=util.string)
    def flags(self):
        """Flags
        Setting this property will *Not* reset flags to value you supply in
        Examples:
        link.flags = '+xxx' # add xxx flag
        link.flags = 'xxx'  # exactly the same
        link.flags = '-xxx' # remove xxx flag
        link.flags = [ '+xxx', '-yyy' ] # list operation
        """
        flags = capi.rtnl_link_get_flags(self._rtnl_link)
        return capi.rtnl_link_flags2str(flags, 256)[0].split(',')

    def _set_flag(self, flag):
        if flag.startswith('-'):
            i = capi.rtnl_link_str2flags(flag[1:])
            capi.rtnl_link_unset_flags(self._rtnl_link, i)
        elif flag.startswith('+'):
            i = capi.rtnl_link_str2flags(flag[1:])
            capi.rtnl_link_set_flags(self._rtnl_link, i)
        else:
            i = capi.rtnl_link_str2flags(flag)
            capi.rtnl_link_set_flags(self._rtnl_link, i)

    @flags.setter
    def flags(self, value):
        if not (type(value) is str):
            for flag in value:
                self._set_flag(flag)
        else:
            self._set_flag(value)

    @property
    @netlink.nlattr(type=int, fmt=util.num)
    def mtu(self):
        """Maximum Transmission Unit"""
        return capi.rtnl_link_get_mtu(self._rtnl_link)

    @mtu.setter
    def mtu(self, value):
        capi.rtnl_link_set_mtu(self._rtnl_link, int(value))

    @property
    @netlink.nlattr(type=int, immutable=True, fmt=util.num)
    def family(self):
        """Address family"""
        return capi.rtnl_link_get_family(self._rtnl_link)

    @family.setter
    def family(self, value):
        capi.rtnl_link_set_family(self._rtnl_link, value)

    @property
    @netlink.nlattr(type=str, fmt=util.addr)
    def address(self):
        """Hardware address (MAC address)"""
        a = capi.rtnl_link_get_addr(self._rtnl_link)
        return netlink.AbstractAddress(a)

    @address.setter
    def address(self, value):
        capi.rtnl_link_set_addr(self._rtnl_link, value._addr)

    @property
    @netlink.nlattr(type=str, fmt=util.addr)
    def broadcast(self):
        """Hardware broadcast address"""
        a = capi.rtnl_link_get_broadcast(self._rtnl_link)
        return netlink.AbstractAddress(a)

    @broadcast.setter
    def broadcast(self, value):
        capi.rtnl_link_set_broadcast(self._rtnl_link, value._addr)

    @property
    @netlink.nlattr(type=str, immutable=True, fmt=util.string)
    def qdisc(self):
        """Name of qdisc (cannot be changed)"""
        return capi.rtnl_link_get_qdisc(self._rtnl_link)

    @qdisc.setter
    def qdisc(self, value):
        capi.rtnl_link_set_qdisc(self._rtnl_link, value)

    @property
    @netlink.nlattr(type=int, fmt=util.num)
    def txqlen(self):
        """Length of transmit queue"""
        return capi.rtnl_link_get_txqlen(self._rtnl_link)

    @txqlen.setter
    def txqlen(self, value):
        capi.rtnl_link_set_txqlen(self._rtnl_link, int(value))

    @property
    @netlink.nlattr(type=str, immutable=True, fmt=util.string)
    def arptype(self):
        """Type of link (cannot be changed)"""
        type_ = capi.rtnl_link_get_arptype(self._rtnl_link)
        return core_capi.nl_llproto2str(type_, 64)[0]

    @arptype.setter
    def arptype(self, value):
        i = core_capi.nl_str2llproto(value)
        capi.rtnl_link_set_arptype(self._rtnl_link, i)

    @property
    @netlink.nlattr(type=str, immutable=True, fmt=util.string, title='state')
    def operstate(self):
        """Operational status"""
        operstate = capi.rtnl_link_get_operstate(self._rtnl_link)
        return capi.rtnl_link_operstate2str(operstate, 32)[0]

    @operstate.setter
    def operstate(self, value):
        i = capi.rtnl_link_str2operstate(value)
        capi.rtnl_link_set_operstate(self._rtnl_link, i)

    @property
    @netlink.nlattr(type=str, immutable=True, fmt=util.string)
    def mode(self):
        """Link mode"""
        mode = capi.rtnl_link_get_linkmode(self._rtnl_link)
        return capi.rtnl_link_mode2str(mode, 32)[0]

    @mode.setter
    def mode(self, value):
        i = capi.rtnl_link_str2mode(value)
        capi.rtnl_link_set_linkmode(self._rtnl_link, i)

    @property
    @netlink.nlattr(type=str, fmt=util.string)
    def alias(self):
        """Interface alias (SNMP)"""
        return capi.rtnl_link_get_ifalias(self._rtnl_link)

    @alias.setter
    def alias(self, value):
        capi.rtnl_link_set_ifalias(self._rtnl_link, value)

    @property
    @netlink.nlattr(type=str, fmt=util.string)
    def type(self):
        """Link type"""
        return capi.rtnl_link_get_type(self._rtnl_link)

    @type.setter
    def type(self, value):
        if capi.rtnl_link_set_type(self._rtnl_link, value) < 0:
            raise NameError('unknown info type')

        self._module_lookup('netlink.route.links.' + value)

    def get_stat(self, stat):
        """Retrieve statistical information"""
        if type(stat) is str:
            stat = capi.rtnl_link_str2stat(stat)
            if stat < 0:
                raise NameError('unknown name of statistic')

        return capi.rtnl_link_get_stat(self._rtnl_link, stat)

    def enslave(self, slave, sock=None):
        if not sock:
            sock = netlink.lookup_socket(netlink.NETLINK_ROUTE)

        return capi.rtnl_link_enslave(sock._sock, self._rtnl_link, slave._rtnl_link)

    def release(self, slave, sock=None):
        if not sock:
            sock = netlink.lookup_socket(netlink.NETLINK_ROUTE)

        return capi.rtnl_link_release(sock._sock, self._rtnl_link, slave._rtnl_link)

    def add(self, sock=None, flags=None):
        if not sock:
            sock = netlink.lookup_socket(netlink.NETLINK_ROUTE)

        if not flags:
            flags = netlink.NLM_F_CREATE

        ret = capi.rtnl_link_add(sock._sock, self._rtnl_link, flags)
        if ret < 0:
            raise netlink.KernelError(ret)

    def change(self, sock=None, flags=0):
        """Commit changes made to the link object"""
        if sock is None:
            sock = netlink.lookup_socket(netlink.NETLINK_ROUTE)

        if not self._orig:
            raise netlink.NetlinkError('Original link not available')
        ret = capi.rtnl_link_change(sock._sock, self._orig, self._rtnl_link, flags)
        if ret < 0:
            raise netlink.KernelError(ret)

    def delete(self, sock=None):
        """Attempt to delete this link in the kernel"""
        if sock is None:
            sock = netlink.lookup_socket(netlink.NETLINK_ROUTE)

        ret = capi.rtnl_link_delete(sock._sock, self._rtnl_link)
        if ret < 0:
            raise netlink.KernelError(ret)

    ###################################################################
    # private properties
    #
    # Used for formatting output. USE AT OWN RISK
    @property
    def _state(self):
        if 'up' in self.flags:
            buf = util.good('up')
            if 'lowerup' not in self.flags:
                buf += ' ' + util.bad('no-carrier')
        else:
            buf = util.bad('down')
        return buf

    @property
    def _brief(self):
        return self._module_brief() + self._foreach_af('brief')

    @property
    def _flags(self):
        ignore = [
            'up',
            'running',
            'lowerup',
        ]
        return ','.join([flag for flag in self.flags if flag not in ignore])

    def _foreach_af(self, name, args=None):
        buf = ''
        for af in self.af:
            try:
                func = getattr(self.af[af], name)
                s = str(func(args))
                if len(s) > 0:
                    buf += ' ' + s
            except AttributeError:
                pass
        return buf

    def format(self, details=False, stats=False, indent=''):
        """Return link as formatted text"""
        fmt = util.MyFormatter(self, indent)

        buf = fmt.format('{a|ifindex} {a|name} {a|arptype} {a|address} '\
                 '{a|_state} <{a|_flags}> {a|_brief}')

        if details:
            buf += fmt.nl('\t{t|mtu} {t|txqlen} {t|weight} '\
                      '{t|qdisc} {t|operstate}')
            buf += fmt.nl('\t{t|broadcast} {t|alias}')

            buf += self._foreach_af('details', fmt)

        if stats:
            l = [['Packets', RX_PACKETS, TX_PACKETS],
                 ['Bytes', RX_BYTES, TX_BYTES],
                 ['Errors', RX_ERRORS, TX_ERRORS],
                 ['Dropped', RX_DROPPED, TX_DROPPED],
                 ['Compressed', RX_COMPRESSED, TX_COMPRESSED],
                 ['FIFO Errors', RX_FIFO_ERR, TX_FIFO_ERR],
                 ['Length Errors', RX_LEN_ERR, None],
                 ['Over Errors', RX_OVER_ERR, None],
                 ['CRC Errors', RX_CRC_ERR, None],
                 ['Frame Errors', RX_FRAME_ERR, None],
                 ['Missed Errors', RX_MISSED_ERR, None],
                 ['Abort Errors', None, TX_ABORT_ERR],
                 ['Carrier Errors', None, TX_CARRIER_ERR],
                 ['Heartbeat Errors', None, TX_HBEAT_ERR],
                 ['Window Errors', None, TX_WIN_ERR],
                 ['Collisions', None, COLLISIONS],
                 ['Multicast', None, MULTICAST],
                 ['', None, None],
                 ['Ipv6:', None, None],
                 ['Packets', IP6_INPKTS, IP6_OUTPKTS],
                 ['Bytes', IP6_INOCTETS, IP6_OUTOCTETS],
                 ['Discards', IP6_INDISCARDS, IP6_OUTDISCARDS],
                 ['Multicast Packets', IP6_INMCASTPKTS, IP6_OUTMCASTPKTS],
                 ['Multicast Bytes', IP6_INMCASTOCTETS, IP6_OUTMCASTOCTETS],
                 ['Broadcast Packets', IP6_INBCASTPKTS, IP6_OUTBCASTPKTS],
                 ['Broadcast Bytes', IP6_INBCASTOCTETS, IP6_OUTBCASTOCTETS],
                 ['Delivers', IP6_INDELIVERS, None],
                 ['Forwarded', None, IP6_OUTFORWDATAGRAMS],
                 ['No Routes', IP6_INNOROUTES, IP6_OUTNOROUTES],
                 ['Header Errors', IP6_INHDRERRORS, None],
                 ['Too Big Errors', IP6_INTOOBIGERRORS, None],
                 ['Address Errors', IP6_INADDRERRORS, None],
                 ['Unknown Protocol', IP6_INUNKNOWNPROTOS, None],
                 ['Truncated Packets', IP6_INTRUNCATEDPKTS, None],
                 ['Reasm Timeouts', IP6_REASMTIMEOUT, None],
                 ['Reasm Requests', IP6_REASMREQDS, None],
                 ['Reasm Failures', IP6_REASMFAILS, None],
                 ['Reasm OK', IP6_REASMOKS, None],
                 ['Frag Created', None, IP6_FRAGCREATES],
                 ['Frag Failures', None, IP6_FRAGFAILS],
                 ['Frag OK', None, IP6_FRAGOKS],
                 ['', None, None],
                 ['ICMPv6:', None, None],
                 ['Messages', ICMP6_INMSGS, ICMP6_OUTMSGS],
                 ['Errors', ICMP6_INERRORS, ICMP6_OUTERRORS]]

            buf += '\n\t%s%s%s%s\n' % (33 * ' ', util.title('RX'),
                           15 * ' ', util.title('TX'))

            for row in l:
                row[0] = util.kw(row[0])
                row[1] = self.get_stat(row[1]) if row[1] else ''
                row[2] = self.get_stat(row[2]) if row[2] else ''
                buf += '\t{0[0]:27} {0[1]:>16} {0[2]:>16}\n'.format(row)

            buf += self._foreach_af('stats')

        return buf

def get(name, sock=None):
    """Lookup Link object directly from kernel"""
    if not name:
        raise ValueError()

    if not sock:
        sock = netlink.lookup_socket(netlink.NETLINK_ROUTE)

    link = capi.get_from_kernel(sock._sock, 0, name)
    if not link:
        return None

    return Link.from_capi(link)

_link_cache = LinkCache()

def resolve(name):
    _link_cache.refill()
    return _link_cache[name]