vppapigen: endian generation for vla for non u32

The endian function for VLAs assumed length field to be of type u32.
That failed for APIs using different integer width.

Type: fix
Change-Id: I6ecaabb4563c8bafeb100a4c6c1eee9a08e6cabf
Signed-off-by: Ole Troan <otroan@employees.org>
This commit is contained in:
Ole Troan
2024-09-10 23:37:56 +02:00
committed by Andrew Yourtchenko
parent 3b5a013103
commit 9a3f387c30

View File

@ -1143,12 +1143,20 @@ ENDIAN_STRINGS = {
} }
def get_endian_string(o, fieldtype): def get_endian_string(fieldtype):
"""Return proper endian string conversion function""" """Return proper endian string conversion function"""
return ENDIAN_STRINGS[fieldtype] return ENDIAN_STRINGS[fieldtype]
def endianfun_array(o): def get_lengthfield_type(fieldname, block):
"""Return the type of the length field"""
for o in block:
if o.fieldname == fieldname:
return o.fieldtype
return None
def endianfun_array(o, block):
"""Generate endian functions for arrays""" """Generate endian functions for arrays"""
forloop = """\ forloop = """\
ASSERT((u32){length} <= (u32)VL_API_MAX_ARRAY_SIZE); ASSERT((u32){length} <= (u32)VL_API_MAX_ARRAY_SIZE);
@ -1167,10 +1175,14 @@ def endianfun_array(o):
if o.fieldtype == "u8" or o.fieldtype == "string" or o.fieldtype == "bool": if o.fieldtype == "u8" or o.fieldtype == "string" or o.fieldtype == "bool":
output += " /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname) output += " /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname)
else: else:
lfield = "a->" + o.lengthfield if o.lengthfield else o.length # lfield = "a->" + o.lengthfield if o.lengthfield else o.length
if o.lengthfield: if o.lengthfield:
fieldtype = get_lengthfield_type(o.lengthfield, block)
if fieldtype == "u8":
output += f" u32 count = a->{o.lengthfield};\n"
else:
output += ( output += (
f" u32 count = to_net ? clib_host_to_net_u32(a->{o.lengthfield}) : " f" u32 count = to_net ? {get_endian_string(fieldtype)}(a->{o.lengthfield}) : "
f"a->{o.lengthfield};\n" f"a->{o.lengthfield};\n"
) )
lfield = "count" lfield = "count"
@ -1180,7 +1192,7 @@ def endianfun_array(o):
if o.fieldtype in ENDIAN_STRINGS: if o.fieldtype in ENDIAN_STRINGS:
output += forloop.format( output += forloop.format(
length=lfield, length=lfield,
format=get_endian_string(o, o.fieldtype), format=get_endian_string(o.fieldtype),
name=o.fieldname, name=o.fieldname,
) )
else: else:
@ -1193,11 +1205,11 @@ def endianfun_array(o):
NO_ENDIAN_CONVERSION = {"client_index": None} NO_ENDIAN_CONVERSION = {"client_index": None}
def endianfun_obj(o): def endianfun_obj(o, block):
"""Generate endian conversion function for type""" """Generate endian conversion function for type"""
output = "" output = ""
if o.type == "Array": if o.type == "Array":
return endianfun_array(o) return endianfun_array(o, block)
if o.type != "Field": if o.type != "Field":
output += ' s = format(s, "\\n{} {} {} (print not implemented");\n'.format( output += ' s = format(s, "\\n{} {} {} (print not implemented");\n'.format(
o.type, o.fieldtype, o.fieldname o.type, o.fieldtype, o.fieldname
@ -1208,7 +1220,7 @@ def endianfun_obj(o):
return output return output
if o.fieldtype in ENDIAN_STRINGS: if o.fieldtype in ENDIAN_STRINGS:
output += " a->{name} = {format}(a->{name});\n".format( output += " a->{name} = {format}(a->{name});\n".format(
name=o.fieldname, format=get_endian_string(o, o.fieldtype) name=o.fieldname, format=get_endian_string(o.fieldtype)
) )
elif o.fieldtype.startswith("vl_api_"): elif o.fieldtype.startswith("vl_api_"):
output += " {type}_endian(&a->{name}, to_net);\n".format( output += " {type}_endian(&a->{name}, to_net);\n".format(
@ -1252,7 +1264,7 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a, bool to_net)
if t.__class__.__name__ == "Enum" or t.__class__.__name__ == "EnumFlag": if t.__class__.__name__ == "Enum" or t.__class__.__name__ == "EnumFlag":
output += signature.format(name=t.name) output += signature.format(name=t.name)
if t.enumtype in ENDIAN_STRINGS: if t.enumtype in ENDIAN_STRINGS:
output += " *a = {}(*a);\n".format(get_endian_string(t, t.enumtype)) output += " *a = {}(*a);\n".format(get_endian_string(t.enumtype))
else: else:
output += " /* a->{name} = a->{name} (no-op) */\n".format( output += " /* a->{name} = a->{name} (no-op) */\n".format(
name=t.name name=t.name
@ -1273,7 +1285,7 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a, bool to_net)
) )
elif t.alias["type"] in FORMAT_STRINGS: elif t.alias["type"] in FORMAT_STRINGS:
output += " *a = {}(*a);\n".format( output += " *a = {}(*a);\n".format(
get_endian_string(t, t.alias["type"]) get_endian_string(t.alias["type"])
) )
else: else:
output += " /* Not Implemented yet {} */".format(t.name) output += " /* Not Implemented yet {} */".format(t.name)
@ -1283,7 +1295,7 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a, bool to_net)
output += signature.format(name=t.name) output += signature.format(name=t.name)
for o in t.block: for o in t.block:
output += endianfun_obj(o) output += endianfun_obj(o, t.block)
output += "}\n\n" output += "}\n\n"
output += "\n#endif" output += "\n#endif"
@ -1349,7 +1361,7 @@ static inline uword vl_api_{name}_t_calc_size (vl_api_{name}_t *a)
) )
lf = m[0] lf = m[0]
if lf.fieldtype in ENDIAN_STRINGS: if lf.fieldtype in ENDIAN_STRINGS:
output += f" + {get_endian_string(b, lf.fieldtype)}(a->{b.lengthfield}) * sizeof(a->{b.fieldname}[0])" output += f" + {get_endian_string(lf.fieldtype)}(a->{b.lengthfield}) * sizeof(a->{b.fieldname}[0])"
elif lf.fieldtype == "u8": elif lf.fieldtype == "u8":
output += ( output += (
f" + a->{b.lengthfield} * sizeof(a->{b.fieldname}[0])" f" + a->{b.lengthfield} * sizeof(a->{b.fieldname}[0])"