summaryrefslogtreecommitdiff
path: root/src/op_mode/interfaces.py
blob: c97f3b1292d1308a5e872608f03a7da1636899da (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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/usr/bin/env python3
#
# Copyright (C) 2022-2023 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
import re
import sys
import glob
import json
import typing
from datetime import datetime
from tabulate import tabulate

import vyos.opmode
from vyos.ifconfig import Section
from vyos.ifconfig import Interface
from vyos.ifconfig import VRRP
from vyos.utils.process import cmd
from vyos.utils.network import interface_exists
from vyos.utils.process import rc_cmd
from vyos.utils.process import call

def catch_broken_pipe(func):
    def wrapped(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except (BrokenPipeError, KeyboardInterrupt):
            # Flush output to /dev/null and bail out.
            os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())
    return wrapped

# The original implementation of filtered_interfaces has signature:
# (ifnames: list, iftypes: typing.Union[str, list], vif: bool, vrrp: bool) -> intf: Interface:
# Arg types allowed in CLI (ifnames: str, iftypes: str) were manually
# re-typed from argparse args.
# We include the function in a general form, however op-mode standard
# functions will restrict to the CLI-allowed arg types, wrapped in Optional.
def filtered_interfaces(ifnames: typing.Union[str, list],
                        iftypes: typing.Union[str, list],
                        vif: bool, vrrp: bool) -> Interface:
    """
    get all interfaces from the OS and return them; ifnames can be used to
    filter which interfaces should be considered

    ifnames: a list of interface names to consider, empty do not filter

    return an instance of the Interface class
    """
    if isinstance(ifnames, str):
        ifnames = [ifnames] if ifnames else []
    if isinstance(iftypes, list):
        for iftype in iftypes:
            yield from filtered_interfaces(ifnames, iftype, vif, vrrp)

    for ifname in Section.interfaces(iftypes):
        # Bail out early if interface name not part of our search list
        if ifnames and ifname not in ifnames:
            continue

        # As we are only "reading" from the interface - we must use the
        # generic base class which exposes all the data via a common API
        interface = Interface(ifname, create=False, debug=False)

        # VLAN interfaces have a '.' in their name by convention
        if vif and not '.' in ifname:
            continue

        if vrrp:
            vrrp_interfaces = VRRP.active_interfaces()
            if ifname not in vrrp_interfaces:
                continue

        yield interface

def detailed_output(dataset, headers):
    for data in dataset:
        adjusted_rule = data + [""] * (len(headers) - len(data)) # account for different header length, like default-action
        transformed_rule = [[header, adjusted_rule[i]] for i, header in enumerate(headers) if i < len(adjusted_rule)] # create key-pair list from headers and rules lists; wrap at 100 char

        print(tabulate(transformed_rule, tablefmt="presto"))
        print()

def _split_text(text, used=0):
    """
    take a string and attempt to split it to fit with the width of the screen

    text: the string to split
    used: number of characted already used in the screen
    """
    no_tty = call('tty -s')

    returned = cmd('stty size') if not no_tty else ''
    returned = returned.split()
    if len(returned) == 2:
        _, columns = tuple(int(_) for _ in returned)
    else:
        _, columns = (40, 80)

    desc_len = columns - used

    line = ''
    for word in text.split():
        if len(line) + len(word) < desc_len:
            line = f'{line} {word}'
            continue
        if line:
            yield line[1:]
        else:
            line = f'{line} {word}'

    yield line[1:]

def _get_counter_val(prev, now):
    """
    attempt to correct a counter if it wrapped, copied from perl

    prev: previous counter
    now:  the current counter
    """
    # This function has to deal with both 32 and 64 bit counters
    if prev == 0:
        return now

    # device is using 64 bit values assume they never wrap
    value = now - prev
    if (now >> 32) != 0:
        return value

    # The counter has rolled.  If the counter has rolled
    # multiple times since the prev value, then this math
    # is meaningless.
    if value < 0:
        value = (4294967296 - prev) + now

    return value

def _pppoe(ifname):
    out = cmd('ps -C pppd -f')
    if ifname in out:
        return 'C'
    if ifname in [_.split('/')[-1] for _ in glob.glob('/etc/ppp/peers/pppoe*')]:
        return 'D'
    return ''

def _find_intf_by_ifname(intf_l: list, name: str):
    for d in intf_l:
        if d['ifname'] == name:
            return d
    return {}

# lifted out of operational.py to separate formatting from data
def _format_stats(stats, indent=4):
    stat_names = {
        'rx': ['bytes', 'packets', 'errors', 'dropped', 'overrun', 'mcast'],
        'tx': ['bytes', 'packets', 'errors', 'dropped', 'carrier', 'collisions'],
    }

    stats_dir = {
        'rx': ['rx_bytes', 'rx_packets', 'rx_errors', 'rx_dropped', 'rx_over_errors', 'multicast'],
        'tx': ['tx_bytes', 'tx_packets', 'tx_errors', 'tx_dropped', 'tx_carrier_errors', 'collisions'],
    }
    tabs = []
    for rtx in list(stats_dir):
        tabs.append([f'{rtx.upper()}:', ] + stat_names[rtx])
        tabs.append(['', ] + [stats[_] for _ in stats_dir[rtx]])

    s = tabulate(
        tabs,
        stralign="right",
        numalign="right",
        tablefmt="plain"
    )

    p = ' '*indent
    return f'{p}' + s.replace('\n', f'\n{p}')

def _get_raw_data(ifname: typing.Optional[str],
                  iftype: typing.Optional[str],
                  vif: bool, vrrp: bool) -> list:
    if ifname is None:
        ifname = ''
    if iftype is None:
        iftype = ''
    ret =[]
    for interface in filtered_interfaces(ifname, iftype, vif, vrrp):
        res_intf = {}
        cache = interface.operational.load_counters()

        out = cmd(f'ip -json addr show {interface.ifname}')
        res_intf_l = json.loads(out)
        res_intf = res_intf_l[0]

        if res_intf['link_type'] == 'tunnel6':
            # Note that 'ip -6 tun show {interface.ifname}' is not json
            # aware, so find in list
            out = cmd('ip -json -6 tun show')
            tunnel = json.loads(out)
            res_intf['tunnel6'] = _find_intf_by_ifname(tunnel,
                                                       interface.ifname)
            if 'ip6_tnl_f_use_orig_tclass' in res_intf['tunnel6']:
                res_intf['tunnel6']['tclass'] = 'inherit'
                del res_intf['tunnel6']['ip6_tnl_f_use_orig_tclass']

        res_intf['counters_last_clear'] = int(cache.get('timestamp', 0))

        res_intf['description'] = interface.get_alias()

        stats = interface.operational.get_stats()
        for k in list(stats):
            stats[k] = _get_counter_val(cache[k], stats[k])

        res_intf['stats'] = stats

        ret.append(res_intf)

    # find pppoe interfaces that are in a transitional/dead state
    if ifname.startswith('pppoe') and not _find_intf_by_ifname(ret, ifname):
        pppoe_intf = {}
        pppoe_intf['unhandled'] = None
        pppoe_intf['ifname'] = ifname
        pppoe_intf['state'] = _pppoe(ifname)
        ret.append(pppoe_intf)

    return ret

def _get_summary_data(ifname: typing.Optional[str],
                      iftype: typing.Optional[str],
                      vif: bool, vrrp: bool) -> list:
    if ifname is None:
        ifname = ''
    if iftype is None:
        iftype = ''
    ret = []

    def is_interface_has_mac(interface_name):
        interface_no_mac = ('tun', 'wg')
        return not any(interface_name.startswith(prefix) for prefix in interface_no_mac)

    for interface in filtered_interfaces(ifname, iftype, vif, vrrp):
        res_intf = {}

        res_intf['ifname'] = interface.ifname
        res_intf['oper_state'] = interface.operational.get_state()
        res_intf['admin_state'] = interface.get_admin_state()
        res_intf['addr'] = [_ for _ in interface.get_addr() if not _.startswith('fe80::')]
        res_intf['description'] = interface.get_alias()
        res_intf['mtu'] = interface.get_mtu()
        res_intf['mac'] = interface.get_mac() if is_interface_has_mac(interface.ifname) else 'n/a'
        res_intf['vrf'] = interface.get_vrf()

        ret.append(res_intf)

    # find pppoe interfaces that are in a transitional/dead state
    if ifname.startswith('pppoe') and not _find_intf_by_ifname(ret, ifname):
        pppoe_intf = {}
        pppoe_intf['unhandled'] = None
        pppoe_intf['ifname'] = ifname
        pppoe_intf['state'] = _pppoe(ifname)
        ret.append(pppoe_intf)

    return ret

def _get_counter_data(ifname: typing.Optional[str],
                      iftype: typing.Optional[str],
                      vif: bool, vrrp: bool) -> list:
    if ifname is None:
        ifname = ''
    if iftype is None:
        iftype = ''
    ret = []
    for interface in filtered_interfaces(ifname, iftype, vif, vrrp):
        res_intf = {}

        oper = interface.operational.get_state()

        if oper not in ('up','unknown'):
            continue

        stats = interface.operational.get_stats()
        cache = interface.operational.load_counters()
        res_intf['ifname'] = interface.ifname
        res_intf['rx_packets'] = _get_counter_val(cache['rx_packets'], stats['rx_packets'])
        res_intf['rx_bytes'] = _get_counter_val(cache['rx_bytes'], stats['rx_bytes'])
        res_intf['tx_packets'] = _get_counter_val(cache['tx_packets'], stats['tx_packets'])
        res_intf['tx_bytes'] = _get_counter_val(cache['tx_bytes'], stats['tx_bytes'])
        res_intf['rx_dropped'] = _get_counter_val(cache['rx_dropped'], stats['rx_dropped'])
        res_intf['tx_dropped'] = _get_counter_val(cache['tx_dropped'], stats['tx_dropped'])
        res_intf['rx_over_errors'] = _get_counter_val(cache['rx_over_errors'], stats['rx_over_errors'])
        res_intf['tx_carrier_errors'] = _get_counter_val(cache['tx_carrier_errors'], stats['tx_carrier_errors'])

        ret.append(res_intf)

    return ret

def _get_kernel_data(raw, ifname = None, detail = False):
    if ifname:
        # Check if the interface exists
        if not interface_exists(ifname):
            raise vyos.opmode.IncorrectValue(f"{ifname} does not exist!")
        int_name = f'dev {ifname}'
    else:
        int_name = ''

    kernel_interface = json.loads(cmd(f'ip -j -d -s address show {int_name}'))

    # Return early if raw
    if raw:
        return kernel_interface, None

    # Format the kernel data
    kernel_interface_out = _format_kernel_data(kernel_interface, detail)

    return kernel_interface, kernel_interface_out

def _format_kernel_data(data, detail):
    output_list = []
    tmpInfo = {}

    # Sort interfaces by name
    for interface in sorted(data, key=lambda x: x.get('ifname', '')):
        if interface.get('linkinfo', {}).get('info_kind') == 'vrf':
            continue

        # Get the device model; ex. Intel Corporation Ethernet Controller I225-V
        dev_model = interface.get('parentdev', '')
        if 'parentdev' in interface:
            parentdev = interface['parentdev']
            if re.match(r'^[0-9a-fA-F]{4}:', parentdev):
                dev_model = cmd(f'lspci -nn -s {parentdev}').split(']:')[1].strip()

        # Get the IP addresses on interface
        ip_list = []
        has_global = False

        for ip in interface['addr_info']:
            if ip.get('scope') in ('global', 'host'):
                has_global = True
                local = ip.get('local', '-')
                prefixlen = ip.get('prefixlen', '')
                ip_list.append(f"{local}/{prefixlen}")


        # If no global IP address, add '-'; indicates no IP address on interface
        if not has_global:
            ip_list.append('-')

        sl_status = ('A' if not 'UP' in interface['flags'] else 'u') + '/' + ('D' if interface['operstate'] == 'DOWN' else 'u')

        # Generate temporary dict to hold data
        tmpInfo['ifname'] = interface.get('ifname', '')
        tmpInfo['ip'] = ip_list
        tmpInfo['mac'] = interface.get('address', '')
        tmpInfo['mtu'] = interface.get('mtu', '')
        tmpInfo['vrf'] = interface.get('master', 'default')
        tmpInfo['status'] = sl_status
        tmpInfo['description'] = interface.get('ifalias', '')
        tmpInfo['device'] = dev_model
        tmpInfo['alternate_names'] = interface.get('altnames', '')
        tmpInfo['minimum_mtu'] = interface.get('min_mtu', '')
        tmpInfo['maximum_mtu'] = interface.get('max_mtu', '')
        rx_stats = interface.get('stats64', {}).get('rx')
        tx_stats = interface.get('stats64', {}).get('tx')
        tmpInfo['rx_packets'] = rx_stats.get('packets', "")
        tmpInfo['rx_bytes'] = rx_stats.get('bytes', "")
        tmpInfo['rx_errors'] = rx_stats.get('errors', "")
        tmpInfo['rx_dropped'] = rx_stats.get('dropped', "")
        tmpInfo['rx_over_errors'] = rx_stats.get('over_errors', '')
        tmpInfo['multicast'] = rx_stats.get('multicast', "")
        tmpInfo['tx_packets'] = tx_stats.get('packets', "")
        tmpInfo['tx_bytes'] = tx_stats.get('bytes', "")
        tmpInfo['tx_errors'] = tx_stats.get('errors', "")
        tmpInfo['tx_dropped'] = tx_stats.get('dropped', "")
        tmpInfo['tx_carrier_errors'] = tx_stats.get('carrier_errors', "")
        tmpInfo['tx_collisions'] = tx_stats.get('collisions', "")

        # Generate output list; detail adds more fields
        output_list.append([tmpInfo['ifname'],
                            '\n'.join(tmpInfo['ip']),
                            tmpInfo['mac'],
                            tmpInfo['vrf'],
                            tmpInfo['mtu'],
                            tmpInfo['status'],
                            tmpInfo['description'],
                            *([tmpInfo['device']] if detail else []),
                            *(['\n'.join(tmpInfo['alternate_names'])] if detail else []),
                            *([tmpInfo['minimum_mtu']] if detail else []),
                            *([tmpInfo['maximum_mtu']] if detail else []),
                            *([tmpInfo['rx_packets']] if detail else []),
                            *([tmpInfo['rx_bytes']] if detail else []),
                            *([tmpInfo['rx_errors']] if detail else []),
                            *([tmpInfo['rx_dropped']] if detail else []),
                            *([tmpInfo['rx_over_errors']] if detail else []),
                            *([tmpInfo['multicast']] if detail else []),
                            *([tmpInfo['tx_packets']] if detail else []),
                            *([tmpInfo['tx_bytes']] if detail else []),
                            *([tmpInfo['tx_errors']] if detail else []),
                            *([tmpInfo['tx_dropped']] if detail else []),
                            *([tmpInfo['tx_carrier_errors']] if detail else []),
                            *([tmpInfo['tx_collisions']] if detail else [])])

    return output_list

@catch_broken_pipe
def _format_show_data(data: list):
    unhandled = []
    for intf in data:
        if 'unhandled' in intf:
            unhandled.append(intf)
            continue
        # instead of reformatting data, call non-json output:
        rc, out = rc_cmd(f"ip addr show {intf['ifname']}")
        if rc != 0:
            continue
        out = re.sub('^\d+:\s+','',out)
        # add additional data already collected
        if 'tunnel6' in intf:
            t6_d = intf['tunnel6']
            t6_str = 'encaplimit %s hoplimit %s tclass %s flowlabel %s (flowinfo %s)' % (
                    t6_d.get('encap_limit', ''), t6_d.get('hoplimit', ''),
                    t6_d.get('tclass', ''), t6_d.get('flowlabel', ''),
                    t6_d.get('flowinfo', ''))
            out = re.sub('(\n\s+)(link/tunnel6)', f'\g<1>{t6_str}\g<1>\g<2>', out)
        print(out)
        ts = intf.get('counters_last_clear', 0)
        if ts:
            when = datetime.fromtimestamp(ts).strftime("%a %b %d %R:%S %Z %Y")
            print(f'    Last clear: {when}')
        description = intf.get('description', '')
        if description:
            print(f'    Description: {description}')

        stats = intf.get('stats', {})
        if stats:
            print()
            print(_format_stats(stats))

    for intf in unhandled:
        string = {
            'C': 'Coming up',
            'D': 'Link down'
        }[intf['state']]
        print(f"{intf['ifname']}: {string}")

    return 0

@catch_broken_pipe
def _format_show_summary(data):
    format1 = '%-16s %-33s %-4s %s'
    format2 = '%-16s %s'

    print('Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down')
    print(format1 % ("Interface", "IP Address", "S/L", "Description"))
    print(format1 % ("---------", "----------", "---", "-----------"))

    unhandled = []
    for intf in data:
        if 'unhandled' in intf:
            unhandled.append(intf)
            continue
        ifname = [intf['ifname'],]
        oper = ['u',] if intf['oper_state'] in ('up', 'unknown') else ['D',]
        admin = ['u',] if intf['admin_state'] in ('up', 'unknown') else ['A',]
        addrs = intf['addr'] or ['-',]
        descs = list(_split_text(intf['description'], 0))

        while ifname or oper or admin or addrs or descs:
            i = ifname.pop(0) if ifname else ''
            a = addrs.pop(0) if addrs else ''
            d = descs.pop(0) if descs else ''
            s = [admin.pop(0)] if admin else []
            l = [oper.pop(0)] if oper else []
            if len(a) < 33:
                print(format1 % (i, a, '/'.join(s+l), d))
            else:
                print(format2 % (i, a))
                print(format1 % ('', '', '/'.join(s+l), d))

    for intf in unhandled:
        string = {
            'C': 'u/D',
            'D': 'A/D'
        }[intf['state']]
        print(format1 % (ifname, '', string, ''))

    return 0

@catch_broken_pipe
def _format_show_summary_extended(data):
    headers = ["Interface", "IP Address", "MAC", "VRF", "MTU", "S/L", "Description"]
    table_data = []

    print('Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down')

    for intf in data:
        if 'unhandled' in intf:
            continue

        ifname = intf['ifname']
        oper_state = 'u' if intf['oper_state'] in ('up', 'unknown') else 'D'
        admin_state = 'u' if intf['admin_state'] in ('up', 'unknown') else 'A'
        addrs = intf['addr'] or ['-']
        description = '\n'.join(_split_text(intf['description'], 0))
        mac = intf['mac'] if intf['mac'] else 'n/a'
        mtu = intf['mtu'] if intf['mtu'] else 'n/a'
        vrf = intf['vrf'] if intf['vrf'] else 'default'

        ip_addresses = '\n'.join(ip for ip in addrs)

        # Create a row for the table
        row = [
            ifname,
            ip_addresses,
            mac,
            vrf,
            mtu,
            f"{admin_state}/{oper_state}",
            description,
        ]

        # Append the row to the table data
        table_data.append(row)

    for intf in data:
        if 'unhandled' in intf:
            string = {'C': 'u/D', 'D': 'A/D'}[intf['state']]
            table_data.append([intf['ifname'], '', '', '', '', string, ''])

    print(tabulate(table_data, headers))

    return 0

@catch_broken_pipe
def _format_show_counters(data: list):
    data_entries = []
    for entry in data:
            interface = entry.get('ifname')
            rx_packets = entry.get('rx_packets')
            rx_bytes = entry.get('rx_bytes')
            tx_packets = entry.get('tx_packets')
            tx_bytes = entry.get('tx_bytes')
            rx_dropped = entry.get('rx_dropped')
            tx_dropped = entry.get('tx_dropped')
            rx_errors = entry.get('rx_over_errors')
            tx_errors = entry.get('tx_carrier_errors')
            data_entries.append([interface, rx_packets, rx_bytes, tx_packets, tx_bytes, rx_dropped, tx_dropped, rx_errors, tx_errors])

    headers = ['Interface', 'Rx Packets', 'Rx Bytes', 'Tx Packets', 'Tx Bytes', 'Rx Dropped', 'Tx Dropped', 'Rx Errors', 'Tx Errors']
    output = tabulate(data_entries, headers, numalign="left")
    print (output)
    return output

def show_kernel(raw: bool, intf_name: typing.Optional[str], detail: bool):
    raw_data, data = _get_kernel_data(raw, intf_name, detail)

    # Return early if raw
    if raw:
        return raw_data

    # Normal headers; show interfaces kernel
    headers = ['Interface', 'IP Address', 'MAC', 'VRF', 'MTU', 'S/L', 'Description']

    # Detail headers; show interfaces kernel detail
    detail_header = ['Interface', 'IP Address', 'MAC', 'VRF', 'MTU', 'S/L', 'Description',
                     'Device', 'Alternate Names','Minimum MTU', 'Maximum MTU', 'RX_Packets',
                     'RX_Bytes', 'RX_Errors', 'RX_Dropped', 'Receive Overrun Errors', 'Received Multicast',
                     'TX_Packets', 'TX_Bytes', 'TX_Errors', 'TX_Dropped', 'Transmit Carrier Errors',
                     'Transmit Collisions']

    if detail:
        detailed_output(data, detail_header)
    else:
        print(tabulate(data, headers))

def _show_raw(data: list, intf_name: str):
    if intf_name is not None and len(data) <= 1:
        try:
            return data[0]
        except IndexError:
            raise vyos.opmode.UnconfiguredObject(
                f"Interface {intf_name} does not exist")
    else:
        return data


def show(raw: bool, intf_name: typing.Optional[str],
                    intf_type: typing.Optional[str],
                    vif: bool, vrrp: bool):
    data = _get_raw_data(intf_name, intf_type, vif, vrrp)
    if raw:
        return _show_raw(data, intf_name)
    return _format_show_data(data)

def show_summary(raw: bool, intf_name: typing.Optional[str],
                            intf_type: typing.Optional[str],
                            vif: bool, vrrp: bool):
    data = _get_summary_data(intf_name, intf_type, vif, vrrp)
    if raw:
        return _show_raw(data, intf_name)
    return _format_show_summary(data)

def show_summary_extended(raw: bool, intf_name: typing.Optional[str],
                            intf_type: typing.Optional[str],
                            vif: bool, vrrp: bool):
    data = _get_summary_data(intf_name, intf_type, vif, vrrp)
    if raw:
        return _show_raw(data, intf_name)
    return _format_show_summary_extended(data)

def show_counters(raw: bool, intf_name: typing.Optional[str],
                             intf_type: typing.Optional[str],
                             vif: bool, vrrp: bool):
    data = _get_counter_data(intf_name, intf_type, vif, vrrp)
    if raw:
        return _show_raw(data, intf_name)
    return _format_show_counters(data)

def clear_counters(intf_name: typing.Optional[str],
                   intf_type: typing.Optional[str],
                   vif: bool, vrrp: bool):
    for interface in filtered_interfaces(intf_name, intf_type, vif, vrrp):
        interface.operational.clear_counters()

def reset_counters(intf_name: typing.Optional[str],
                   intf_type: typing.Optional[str],
                   vif: bool, vrrp: bool):
    for interface in filtered_interfaces(intf_name, intf_type, vif, vrrp):
        interface.operational.reset_counters()

if __name__ == '__main__':
    try:
        res = vyos.opmode.run(sys.modules[__name__])
        if res:
            print(res)
    except (ValueError, vyos.opmode.Error) as e:
        print(e)
        sys.exit(1)