Skip to content

Instantly share code, notes, and snippets.

@cwyang
Created February 24, 2021 08:44
Show Gist options
  • Save cwyang/7ce2309f0b10619cd94d708049829728 to your computer and use it in GitHub Desktop.
Save cwyang/7ce2309f0b10619cd94d708049829728 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import time
import fnmatch
from vpp_papi import VPPApiClient
def init_vpp(vpp_json_dir='/usr/share/vpp/api/core/'):
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')
exit(-1)
return VPPApiClient(jsonfiles)
def papi_event_handler(msgname, result):
print(msgname)
print(result)
def run_loop():
async = True
r = vpp.register_event_callback(papi_event_handler)
pid=os.getpid()
r = vpp.api.want_interface_events(enable_disable=True, pid=pid)
print(r)
# Wait for some stats
time.sleep(60)
r = vpp.api.want_interface_events(enable_disable=False)
r = vpp.disconnect()
vpp = init_vpp()
r = vpp.connect("test_papi")
rv = vpp.api.show_version()
print('VPP version =', rv.version.rstrip('\0x00'))
print(rv)
for intf in vpp.api.sw_interface_dump():
print(intf)
run_loop()
exit(vpp.disconnect())
@cwyang
Copy link
Author

cwyang commented Feb 25, 2021

python vpp api를 사용해보았습니다.

vpp src tree의 src/vpp-api/python를 타겟 머신에 풀고 sudo python3 setup.py install로 vpp-papi를 설치하면
import vpp-papi 모듈 사용이 가능하구요

api를 이용해서 버젼, vpp nic 정보를 가지고 오고,
callback을 붙여서 vpp의 nic 상태가 변하게 되면 callback을 호출하는 테스트 프로그램을 해봤어요.

돌리면 이런식으로 나옵니다

$ sudo python3 vpp_api_test.py
VPP version = 20.05-50~g0fe4c569b
show_version_reply(_0=746, context=1, retval=0, program='vpe', version='20.05-50~g0fe4c569b', build_date='2021-01-29T08:58:52', build_directory='/root/vpp')
sw_interface_details(_0=74, context=2, sw_if_index=0, sup_sw_if_index=0, l2_address=MACAddress(00:00:00:00:00:00), flags=<vl_api_if_status_flags_t.0: 0>, type=<vl_api_if_type_t.IF_API_TYPE_HARDWARE: 0>, link_duplex=<vl_api_link_duplex_t.LINK_DUPLEX_API_UNKNOWN: 0>, link_speed=0, link_mtu=0, mtu=[0, 0, 0, 0], sub_id=0, sub_number_of_tags=0, sub_outer_vlan_id=0, sub_inner_vlan_id=0, sub_if_flags=<vl_api_sub_if_flags_t.0: 0>, vtr_op=0, vtr_push_dot1q=0, vtr_tag1=0, vtr_tag2=0, outer_tag=0, b_dmac=MACAddress(00:00:00:00:00:00), b_smac=MACAddress(00:00:00:00:00:00), b_vlanid=0, i_sid=0, interface_name='local0', interface_dev_type='local', tag='')
sw_interface_details(_0=74, context=2, sw_if_index=1, sup_sw_if_index=1, l2_address=MACAddress(c4:00:ad:a9:87:c2), flags=<vl_api_if_status_flags_t.IF_STATUS_API_FLAG_LINK_UP|IF_STATUS_API_FLAG_ADMIN_UP: 3>, type=<vl_api_if_type_t.IF_API_TYPE_HARDWARE: 0>, link_duplex=<vl_api_link_duplex_t.33554432: 33554432>, link_speed=1000000, link_mtu=9206, mtu=[9000, 0, 0, 0], sub_id=0, sub_number_of_tags=0, sub_outer_vlan_id=0, sub_inner_vlan_id=0, sub_if_flags=<vl_api_sub_if_flags_t.0: 0>, vtr_op=0, vtr_push_dot1q=0, vtr_tag1=0, vtr_tag2=0, outer_tag=0, b_dmac=MACAddress(00:00:00:00:00:00), b_smac=MACAddress(00:00:00:00:00:00), b_vlanid=0, i_sid=0, interface_name='enp150s0f0', interface_dev_type='dpdk', tag='')

로컬 머신에서 테스트 프로그램을 돌리고, 다른 창에서 nic 하나를 down하고 up해보면

vagrant@vagrant:~/work$ sudo /usr/bin/python3 /home/vagrant/work/ex1.py
VPP version = 21.01-release
show_version_reply(_0=582, context=1, retval=0, program='vpe', version='21.01-release', build_date='2021-01-27T16:06:22', build_directory='/w/workspace/vpp-merge-2101-ubuntu1804-x86_64')
sw_interface_details(_0=80, context=2, sw_if_index=0, sup_sw_if_index=0, l2_address=MACAddress(00:00:00:00:00:00), flags=<vl_api_if_status_flags_t.0: 0>, type=<vl_api_if_type_t.IF_API_TYPE_HARDWARE: 0>, link_duplex=<vl_api_link_duplex_t.LINK_DUPLEX_API_UNKNOWN: 0>, link_speed=0, link_mtu=0, mtu=[0, 0, 0, 0], sub_id=0, sub_number_of_tags=0, sub_outer_vlan_id=0, sub_inner_vlan_id=0, sub_if_flags=<vl_api_sub_if_flags_t.0: 0>, vtr_op=0, vtr_push_dot1q=0, vtr_tag1=0, vtr_tag2=0, outer_tag=0, b_dmac=MACAddress(00:00:00:00:00:00), b_smac=MACAddress(00:00:00:00:00:00), b_vlanid=0, i_sid=0, interface_name='local0', interface_dev_type='local', tag='')
sw_interface_details(_0=80, context=2, sw_if_index=1, sup_sw_if_index=1, l2_address=MACAddress(02:fe:ed:d2:28:d9), flags=<vl_api_if_status_flags_t.IF_STATUS_API_FLAG_LINK_UP|IF_STATUS_API_FLAG_ADMIN_UP: 3>, type=<vl_api_if_type_t.IF_API_TYPE_HARDWARE: 0>, link_duplex=<vl_api_link_duplex_t.LINK_DUPLEX_API_UNKNOWN: 0>, link_speed=0, link_mtu=9216, mtu=[9000, 0, 0, 0], sub_id=0, sub_number_of_tags=0, sub_outer_vlan_id=0, sub_inner_vlan_id=0, sub_if_flags=<vl_api_sub_if_flags_t.0: 0>, vtr_op=0, vtr_push_dot1q=0, vtr_tag1=0, vtr_tag2=0, outer_tag=0, b_dmac=MACAddress(00:00:00:00:00:00), b_smac=MACAddress(00:00:00:00:00:00), b_vlanid=0, i_sid=0, interface_name='host-vpp1out', interface_dev_type='af-packet', tag='')
want_interface_events_reply(_0=79, context=3, retval=0)
sw_interface_event
sw_interface_event(_0=77, client_index=0, pid=11064, sw_if_index=1, flags=<vl_api_if_status_flags_t.0: 0>, deleted=False)
sw_interface_event
sw_interface_event(_0=77, client_index=0, pid=11064, sw_if_index=1, flags=<vl_api_if_status_flags_t.IF_STATUS_API_FLAG_LINK_UP|IF_STATUS_API_FLAG_ADMIN_UP: 3>, deleted=False)

이런식으로 sw_interface_event가 날아오고 보면 flag가 UP이 없어졌다가 다시 붙고있습니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment