Python API: Re-adding rudimentary variable length array pack support.
Fixed bug in message-id mapping with non-consequtive APIs. Change-Id: Icd6073e4655f7ce5432816861ae58915e5b336af Signed-off-by: Ole Troan <ot@cisco.com>
This commit is contained in:
@ -14,4 +14,7 @@ print('R:', r)
|
|||||||
r = snat.snat_interface_add_del_feature(1, 1, 1)
|
r = snat.snat_interface_add_del_feature(1, 1, 1)
|
||||||
print('R:', r)
|
print('R:', r)
|
||||||
|
|
||||||
|
list_name = 'foobar'
|
||||||
|
r = pot.pot_profile_add(0, 1, 123, 123, 0, 12, 0, 23, len(list_name), list_name)
|
||||||
|
print('R:', r)
|
||||||
vpp_papi.disconnect()
|
vpp_papi.disconnect()
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
#
|
#
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import signal, logging, os, sys
|
import signal, os, sys
|
||||||
from struct import *
|
from struct import *
|
||||||
|
|
||||||
scriptdir = os.path.dirname(os.path.realpath(__file__))
|
scriptdir = os.path.dirname(os.path.realpath(__file__))
|
||||||
@ -30,25 +30,26 @@ import memclnt
|
|||||||
from vpe import *
|
from vpe import *
|
||||||
vpe = sys.modules['vpe']
|
vpe = sys.modules['vpe']
|
||||||
|
|
||||||
|
def eprint(*args, **kwargs):
|
||||||
|
print(*args, file=sys.stderr, **kwargs)
|
||||||
|
|
||||||
def msg_handler(msg):
|
def msg_handler(msg):
|
||||||
if not msg:
|
if not msg:
|
||||||
logging.warning('vpp_api.read failed')
|
eprint('vpp_api.read failed')
|
||||||
return
|
return
|
||||||
|
|
||||||
id = unpack('>H', msg[0:2])
|
id = unpack('>H', msg[0:2])
|
||||||
logging.debug('Received message', id[0])
|
|
||||||
if id[0] == memclnt.VL_API_RX_THREAD_EXIT:
|
if id[0] == memclnt.VL_API_RX_THREAD_EXIT:
|
||||||
logging.info("We got told to leave")
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#
|
#
|
||||||
# Decode message and returns a tuple.
|
# Decode message and returns a tuple.
|
||||||
#
|
#
|
||||||
logging.debug('api_func', api_func_table[id[0]])
|
try:
|
||||||
r = api_func_table[id[0]](msg)
|
r = api_func_table[id[0]](msg)
|
||||||
if not r:
|
except:
|
||||||
logging.warning('Message decode failed', id[0])
|
eprint('Message decode failed', id[0], api_func_table[id[0]])
|
||||||
return
|
raise
|
||||||
|
|
||||||
if 'context' in r._asdict():
|
if 'context' in r._asdict():
|
||||||
if r.context > 0:
|
if r.context > 0:
|
||||||
@ -71,7 +72,7 @@ def msg_handler(msg):
|
|||||||
waiting_for_reply_clear()
|
waiting_for_reply_clear()
|
||||||
return
|
return
|
||||||
if not is_results_context(context):
|
if not is_results_context(context):
|
||||||
logging.warning('Not expecting results for this context', context)
|
eprint('Not expecting results for this context', context)
|
||||||
return
|
return
|
||||||
if is_results_more(context):
|
if is_results_more(context):
|
||||||
results_append(context, r)
|
results_append(context, r)
|
||||||
@ -85,7 +86,6 @@ def connect(name):
|
|||||||
signal.alarm(3) # 3 second
|
signal.alarm(3) # 3 second
|
||||||
rv = vpp_api.connect(name, msg_handler)
|
rv = vpp_api.connect(name, msg_handler)
|
||||||
signal.alarm(0)
|
signal.alarm(0)
|
||||||
logging.info("Connect:", rv)
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Assign message id space for plugins
|
# Assign message id space for plugins
|
||||||
@ -96,7 +96,6 @@ def connect(name):
|
|||||||
|
|
||||||
def disconnect():
|
def disconnect():
|
||||||
rv = vpp_api.disconnect()
|
rv = vpp_api.disconnect()
|
||||||
logging.info("Disconnected")
|
|
||||||
return rv
|
return rv
|
||||||
|
|
||||||
# CLI convenience wrapper
|
# CLI convenience wrapper
|
||||||
@ -128,10 +127,9 @@ def plugin_map_plugins():
|
|||||||
version = plugins[p]['version']
|
version = plugins[p]['version']
|
||||||
name = p + '_' + format(version, '08x')
|
name = p + '_' + format(version, '08x')
|
||||||
r = memclnt.get_first_msg_id(name.encode('ascii'))
|
r = memclnt.get_first_msg_id(name.encode('ascii'))
|
||||||
|
## TODO: Add error handling / raise exception
|
||||||
## TODO: Add error handling
|
|
||||||
if r.retval != 0:
|
if r.retval != 0:
|
||||||
print('Failed getting first msg id for:', p)
|
eprint('Failed getting first msg id for:', p)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Set base
|
# Set base
|
||||||
@ -141,8 +139,12 @@ def plugin_map_plugins():
|
|||||||
plugins[p]['base'] = base
|
plugins[p]['base'] = base
|
||||||
func_table = plugins[p]['func_table']
|
func_table = plugins[p]['func_table']
|
||||||
i = r.first_msg_id
|
i = r.first_msg_id
|
||||||
|
# Insert doesn't extend the table
|
||||||
|
if i + len(func_table) > len(api_func_table):
|
||||||
|
fill = [None] * (i + len(func_table) - len(api_func_table))
|
||||||
|
api_func_table.extend(fill)
|
||||||
for entry in func_table:
|
for entry in func_table:
|
||||||
api_func_table.insert(i, entry)
|
api_func_table[i] = entry
|
||||||
i += 1
|
i += 1
|
||||||
plugin_name_to_id(p, plugins[p]['name_to_id_table'], base)
|
plugin_name_to_id(p, plugins[p]['name_to_id_table'], base)
|
||||||
|
|
||||||
@ -158,4 +160,3 @@ api_func_table.append(None)
|
|||||||
api_func_table[1:] = plugins['memclnt']['func_table'] + plugins['vpe']['func_table']
|
api_func_table[1:] = plugins['memclnt']['func_table'] + plugins['vpe']['func_table']
|
||||||
plugin_name_to_id('memclnt', plugins['memclnt']['name_to_id_table'], 1)
|
plugin_name_to_id('memclnt', plugins['memclnt']['name_to_id_table'], 1)
|
||||||
plugin_name_to_id('vpe', plugins['vpe']['name_to_id_table'], plugins['vpe']['base'])
|
plugin_name_to_id('vpe', plugins['vpe']['name_to_id_table'], plugins['vpe']['base'])
|
||||||
#logging.basicConfig(level=logging.DEBUG)
|
|
||||||
|
@ -68,7 +68,7 @@ def get_pack(f):
|
|||||||
bytecount = 0
|
bytecount = 0
|
||||||
pack = ''
|
pack = ''
|
||||||
elements = 1
|
elements = 1
|
||||||
if len(f) is 3 or len(f) is 4: # TODO: add support for variable length arrays (VPP-162)
|
if len(f) is 3 or len(f) is 4:
|
||||||
size = type_size[f[0]]
|
size = type_size[f[0]]
|
||||||
bytecount += size * int(f[2])
|
bytecount += size * int(f[2])
|
||||||
# Check if we have a zero length array
|
# Check if we have a zero length array
|
||||||
@ -146,14 +146,26 @@ def encode_print(name, id, t):
|
|||||||
if multipart == True:
|
if multipart == True:
|
||||||
print(u" results_more_set(context)")
|
print(u" results_more_set(context)")
|
||||||
|
|
||||||
### TODO deal with zeroarray!!!
|
pack = '>'
|
||||||
#if zeroarray == True:
|
start = 0
|
||||||
# print(u" vpp_api.write(pack('" + pack + "', " + id + ", 0, context, " + ', '.join(args[3:-1]) + ") + " + args[-1] + ")")
|
end = 0
|
||||||
#else:
|
offset = 0
|
||||||
print(u" vpp_api.write(pack('" + pack + "', base + " + id + ", 0, context, " + ', '.join(args[3:]) + "))")
|
t = list(t)
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while t:
|
||||||
|
t, i, pack, offset, array = get_normal_pack(t, i, pack, offset)
|
||||||
|
if array:
|
||||||
|
print(u" vpp_api.write(pack('" + pack + "', base + " +
|
||||||
|
id + ", 0, context, " + ', '.join(args[3:-1]) + ") + "
|
||||||
|
+ args[-1] + ")")
|
||||||
|
else:
|
||||||
|
print(u" vpp_api.write(pack('" + pack + "', base + " + id +
|
||||||
|
", 0, context, " + ', '.join(args[3:]) + "))")
|
||||||
|
|
||||||
if multipart == True:
|
if multipart == True:
|
||||||
print(u" vpp_api.write(pack('>HII', VL_API_CONTROL_PING, 0, context))")
|
print(
|
||||||
|
u" vpp_api.write(pack('>HII', VL_API_CONTROL_PING, 0, context))")
|
||||||
|
|
||||||
print('''
|
print('''
|
||||||
if not async:
|
if not async:
|
||||||
|
Reference in New Issue
Block a user