Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import print_function
- import os
- import fnmatch
- import time
- import sys
- from vpp_papi import VPP
- simple_counter_type = [
- 'VNET_INTERFACE_COUNTER_DROP',
- 'VNET_INTERFACE_COUNTER_PUNT',
- 'VNET_INTERFACE_COUNTER_IP4',
- 'VNET_INTERFACE_COUNTER_IP6',
- 'VNET_INTERFACE_COUNTER_RX_NO_BUF',
- 'VNET_INTERFACE_COUNTER_RX_MISS',
- 'VNET_INTERFACE_COUNTER_RX_ERROR',
- 'VNET_INTERFACE_COUNTER_TX_ERROR',
- 'VNET_INTERFACE_COUNTER_MPLS',
- 'VNET_N_SIMPLE_INTERFACE_COUNTER']
- combined_counter_type = [
- 'VNET_INTERFACE_COUNTER_RX',
- 'VNET_INTERFACE_COUNTER_RX_UNICAST',
- 'VNET_INTERFACE_COUNTER_RX_MULTICAST',
- 'VNET_INTERFACE_COUNTER_RX_BROADCAST',
- 'VNET_INTERFACE_COUNTER_TX',
- 'VNET_INTERFACE_COUNTER_TX_UNICAST',
- 'VNET_INTERFACE_COUNTER_TX_MULTICAST',
- 'VNET_INTERFACE_COUNTER_TX_BROADCAST',
- 'VNET_N_COMBINED_INTERFACE_COUNTER']
- # event handler function to handle reply messages
- # it handle two type of message currently
- # received value is a struct that could be parsed
- def papi_event_handler(msgname, result):
- idx = 0
- if msgname == 'vnet_interface_combined_counters':
- values = []
- # format string will let you to print output by specific format,
- # {0:^11s} means 0 args of format function, it's type is string and maximum length is 11. ^ means align center.
- print('{0:^11s}{1:^40s}{2:^40s}{3:^20s}{4:^20s}'.format('sw_if_index', 'Counter', 'Type', 'Packets', 'Bytes'))
- print('=' * 131)
- for row in result.data:
- print('{0:^11d}{1:^40s}{2:^40s}{3:^20d}{4:^20d}'.format(idx, msgname, combined_counter_type[result.vnet_counter_type], row.packets,
- row.bytes))
- values.append((row.packets, row.bytes))
- idx += 1
- elif msgname == 'vnet_interface_simple_counters':
- values = []
- # format string will let you to print output by specific format,
- # {0:^11s} means 0 args of format function, it's type is string and maximum length is 11. ^ means align center.
- print('{0:^11s}{1:^40s}{2:^40s}{3:^20s}'.format('sw_if_index', 'Counter', 'Type', 'Packets'))
- print('=' * 111)
- for row in result.data:
- print('{0:^11d}{1:^40s}{2:^40s}{3:^20d}'.format(idx, msgname, simple_counter_type[result.vnet_counter_type], row))
- values.append(row)
- idx += 1
- print()
- # set the LD_LIBRARY_PATH such that it points to the directory containing libvppapiclient.so
- os.environ['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu/'
- # directory containing all the json api files.
- # if vpp is installed on the system, these will be in /usr/share/vpp/api/
- vpp_json_dir = '/usr/share/vpp/api/'
- # construct a list of all the json api files
- jsonfiles = []
- for root, dirnames, filenames in os.walk(vpp_json_dir):
- for filename in fnmatch.filter(filenames, '*.api.json'):
- jsonfiles.append(os.path.join(vpp_json_dir, filename))
- if not jsonfiles:
- print('Error: no json api files found')
- sys.exit(-1)
- # use all those files to create vpp.
- # Note that there will be no vpp method available before vpp.connect()
- vpp = VPP(jsonfiles)
- rv = vpp.connect("test_papi")
- # print(rv)
- # print all specification (variables and functions) that exist in vpp.api
- # for func in dir(vpp.api):
- # print(func)
- # print()
- interface_details = vpp.api.sw_interface_dump()
- # set program asynchronous
- async = True
- # register an event handler to receive reply messages of an api
- rv = vpp.register_event_callback(papi_event_handler)
- # enable want_stats api in vpp to receive stats
- rv = vpp.api.want_stats(enable_disable=True)
- # print(rv)
- # lifetime of program
- time.sleep(10)
- # enable want_stats api in vpp to receive stats
- rv = vpp.api.want_stats(enable_disable=False)
- # print(rv)
- # disconnect from vpp
- rv = vpp.disconnect()
- # print(rv)
- sys.exit(rv)
Advertisement
Add Comment
Please, Sign In to add comment