api: add to_net parameter to endian messages

The VPP API auto-generated endian conversion functions are intended to
be symmetrical. They are used both by the API client and the API server.
Called on send to convert from host endian to network endian and on
receive to convert back.

For variable length arrays, we have to iterate over the array and call
a more specific handler for the array type. Unfortunately the length of
the array is part of the api definition, and if it's endian swapped
prior to the for loop, unexpected behaviour will ensue.

There was an earlier fix, for some specific messages, but unfortunately
that only fixed the problem from the VPP (server) side.

This adds a new parameters to the endian handler, so the boundary
argument to the loop can be treated differently depending on if this
message is to the network or from the network.

Type: fix
Change-Id: I43011aed384e3b847579a1dd2c390867ae17a9ad
Signed-off-by: Ole Troan <otroan@employees.org>
This commit is contained in:
Ole Troan 2024-04-26 14:11:20 +02:00 committed by Damjan Marion
parent 4013851e42
commit 1a319aadc6
13 changed files with 72 additions and 92 deletions

View File

@ -114,7 +114,7 @@ static void vl_api_acl_interface_list_details_t_handler
int i;
vat_main_t * vam = acl_test_main.vat_main;
u8 *out = 0;
vl_api_acl_interface_list_details_t_endian(mp);
vl_api_acl_interface_list_details_t_endian (mp, 0 /* from network */);
out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
out = format(out, " input ");
for(i=0; i<mp->count; i++) {
@ -141,7 +141,8 @@ static void vl_api_acl_interface_etype_whitelist_details_t_handler
int i;
vat_main_t * vam = acl_test_main.vat_main;
u8 *out = 0;
vl_api_acl_interface_etype_whitelist_details_t_endian(mp);
vl_api_acl_interface_etype_whitelist_details_t_endian (
mp, 0 /* from network */);
out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
out = format(out, " input ");
for(i=0; i<mp->count; i++) {
@ -173,15 +174,15 @@ vl_api_acl_rule_t_pretty_format (u8 *out, vl_api_acl_rule_t * a)
inet_ntop(af, &a->src_prefix.address.un, (void *)src, sizeof(src));
inet_ntop(af, &a->dst_prefix.address.un, (void *)dst, sizeof(dst));
out = format(out, "%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport %d-%d tcpflags %d mask %d",
a->src_prefix.address.af ? "ipv6" : "ipv4", a->is_permit,
src, a->src_prefix.len,
dst, a->dst_prefix.len,
a->proto,
a->srcport_or_icmptype_first, a->srcport_or_icmptype_last,
a->dstport_or_icmpcode_first, a->dstport_or_icmpcode_last,
a->tcp_flags_value, a->tcp_flags_mask);
return(out);
out = format (out,
"%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport "
"%d-%d tcpflags %d mask %d",
a->src_prefix.address.af ? "ipv6" : "ipv4", a->is_permit, src,
a->src_prefix.len, dst, a->dst_prefix.len, a->proto,
a->srcport_or_icmptype_first, a->srcport_or_icmptype_last,
a->dstport_or_icmpcode_first, a->dstport_or_icmpcode_last,
a->tcp_flags_value, a->tcp_flags_mask);
return (out);
}
@ -191,9 +192,10 @@ static void vl_api_acl_details_t_handler
{
int i;
vat_main_t * vam = acl_test_main.vat_main;
vl_api_acl_details_t_endian(mp);
u8 *out = 0;
out = format(0, "acl_index: %d, count: %d\n tag {%s}\n", mp->acl_index, mp->count, mp->tag);
vl_api_acl_details_t_endian (mp, 0 /* from network */);
u8 *out = 0;
out = format (0, "acl_index: %d, count: %d\n tag {%s}\n",
mp->acl_index, mp->count, mp->tag);
for(i=0; i<mp->count; i++) {
out = format(out, " ");
out = vl_api_acl_rule_t_pretty_format(out, &mp->r[i]);
@ -225,8 +227,9 @@ static void vl_api_macip_acl_details_t_handler
{
int i;
vat_main_t * vam = acl_test_main.vat_main;
vl_api_macip_acl_details_t_endian(mp);
u8 *out = format(0,"MACIP acl_index: %d, count: %d\n tag {%s}\n", mp->acl_index, mp->count, mp->tag);
vl_api_macip_acl_details_t_endian (mp, 0 /* from network */);
u8 *out = format (0, "MACIP acl_index: %d, count: %d\n tag {%s}\n",
mp->acl_index, mp->count, mp->tag);
for(i=0; i<mp->count; i++) {
out = format(out, " ");
out = vl_api_macip_acl_rule_t_pretty_format(out, &mp->r[i]);

View File

@ -173,7 +173,7 @@ send_profile (ikev2_profile_t * profile, vl_api_registration_t * reg,
rmp->profile.lifetime_jitter = profile->lifetime_jitter;
rmp->profile.handover = profile->handover;
vl_api_ikev2_profile_t_endian (&rmp->profile);
vl_api_ikev2_profile_t_endian (&rmp->profile, 1 /* to network */);
vl_api_send_msg (reg, (u8 *) rmp);
}
@ -291,7 +291,7 @@ send_sa (ikev2_sa_t * sa, vl_api_ikev2_sa_dump_t * mp, u32 api_sa_index)
ikev2_copy_stats (&rsa->stats, &sa->stats);
vl_api_ikev2_sa_t_endian(rsa);
vl_api_ikev2_sa_t_endian (rsa, 1 /* to network */);
});
}
@ -382,7 +382,7 @@ send_sa_v2 (ikev2_sa_t *sa, vl_api_ikev2_sa_v2_dump_t *mp, u32 api_sa_index)
ikev2_copy_stats (&rsa->stats, &sa->stats);
vl_api_ikev2_sa_v2_t_endian (rsa);
vl_api_ikev2_sa_v2_t_endian (rsa, 1 /* to network */);
});
}
@ -476,7 +476,7 @@ send_sa_v3 (ikev2_sa_t *sa, vl_api_ikev2_sa_v3_dump_t *mp, u32 api_sa_index)
ikev2_copy_stats (&rsa->stats, &sa->stats);
vl_api_ikev2_sa_v3_t_endian (rsa);
vl_api_ikev2_sa_v3_t_endian (rsa, 1 /* to network */);
});
}
@ -549,7 +549,7 @@ send_child_sa (ikev2_child_sa_t * child,
k->sk_ar_len);
}
vl_api_ikev2_child_sa_t_endian (&rmp->child_sa);
vl_api_ikev2_child_sa_t_endian (&rmp->child_sa, 1 /* to network */);
});
}
@ -628,7 +628,7 @@ send_child_sa_v2 (ikev2_child_sa_t *child, vl_api_ikev2_child_sa_v2_dump_t *mp,
clib_memcpy (&k->sk_ar, child->sk_ar, k->sk_ar_len);
}
vl_api_ikev2_child_sa_v2_t_endian (&rmp->child_sa);
vl_api_ikev2_child_sa_v2_t_endian (&rmp->child_sa, 1 /* to network */);
});
}
@ -700,7 +700,7 @@ static void
rmp->ts.sa_index = api_sa_index;
rmp->ts.child_sa_index = child_sa_index;
cp_ts (&rmp->ts, ts, mp->is_initiator);
vl_api_ikev2_ts_t_endian (&rmp->ts);
vl_api_ikev2_ts_t_endian (&rmp->ts, 1 /* to network */);
});
}
}

View File

@ -391,7 +391,7 @@ vl_api_ikev2_sa_details_t_handler (vl_api_ikev2_sa_details_t * mp)
ip_address_t iaddr;
ip_address_t raddr;
vl_api_ikev2_keys_t *k = &sa->keys;
vl_api_ikev2_sa_t_endian (sa);
vl_api_ikev2_sa_t_endian (sa, 0 /* from network */);
ip_address_decode2 (&sa->iaddr, &iaddr);
ip_address_decode2 (&sa->raddr, &raddr);
@ -461,7 +461,7 @@ vl_api_ikev2_sa_v2_details_t_handler (vl_api_ikev2_sa_v2_details_t *mp)
ip_address_t iaddr;
ip_address_t raddr;
vl_api_ikev2_keys_t *k = &sa->keys;
vl_api_ikev2_sa_v2_t_endian (sa);
vl_api_ikev2_sa_v2_t_endian (sa, 0 /* from network */);
ip_address_decode2 (&sa->iaddr, &iaddr);
ip_address_decode2 (&sa->raddr, &raddr);
@ -533,7 +533,7 @@ vl_api_ikev2_sa_v3_details_t_handler (vl_api_ikev2_sa_v3_details_t *mp)
ip_address_t iaddr;
ip_address_t raddr;
vl_api_ikev2_keys_t *k = &sa->keys;
vl_api_ikev2_sa_v3_t_endian (sa);
vl_api_ikev2_sa_v3_t_endian (sa, 0 /* from network */);
ip_address_decode2 (&sa->iaddr, &iaddr);
ip_address_decode2 (&sa->raddr, &raddr);
@ -619,7 +619,7 @@ vl_api_ikev2_child_sa_details_t_handler (vl_api_ikev2_child_sa_details_t * mp)
vat_main_t *vam = ikev2_test_main.vat_main;
vl_api_ikev2_child_sa_t *child_sa = &mp->child_sa;
vl_api_ikev2_keys_t *k = &child_sa->keys;
vl_api_ikev2_child_sa_t_endian (child_sa);
vl_api_ikev2_child_sa_t_endian (child_sa, 0 /* from network */);
fformat (vam->ofp, " child sa %u:\n", child_sa->child_sa_index);
@ -696,7 +696,7 @@ vl_api_ikev2_child_sa_v2_details_t_handler (
vat_main_t *vam = ikev2_test_main.vat_main;
vl_api_ikev2_child_sa_t *child_sa = &mp->child_sa;
vl_api_ikev2_keys_t *k = &child_sa->keys;
vl_api_ikev2_child_sa_t_endian (child_sa);
vl_api_ikev2_child_sa_t_endian (child_sa, 0 /* from network */);
fformat (vam->ofp, " child sa %u:\n", child_sa->child_sa_index);
@ -784,7 +784,7 @@ static void
vat_main_t *vam = ikev2_test_main.vat_main;
vl_api_ikev2_ts_t *ts = &mp->ts;
ip_address_t start_addr, end_addr;
vl_api_ikev2_ts_t_endian (ts);
vl_api_ikev2_ts_t_endian (ts, 0 /* from network */);
ip_address_decode2 (&ts->start_addr, &start_addr);
ip_address_decode2 (&ts->end_addr, &end_addr);

View File

@ -442,7 +442,8 @@ send_nat44_ed_output_interface_details (u32 index, vl_api_registration_t *rp,
/* Endian hack until apigen registers _details
* endian functions */
vl_api_nat44_ed_output_interface_details_t_endian (rmp);
vl_api_nat44_ed_output_interface_details_t_endian (rmp,
1 /* to network */);
rmp->_vl_msg_id = htons (rmp->_vl_msg_id);
rmp->context = htonl (rmp->context);
}));

View File

@ -751,7 +751,8 @@ send_nat44_ei_output_interface_details (u32 index, vl_api_registration_t *rp,
/* Endian hack until apigen registers _details
* endian functions */
vl_api_nat44_ei_output_interface_details_t_endian (rmp);
vl_api_nat44_ei_output_interface_details_t_endian (rmp,
1 /* to network */);
rmp->_vl_msg_id = htons (rmp->_vl_msg_id);
rmp->context = htonl (rmp->context);
}));

View File

@ -116,7 +116,8 @@ static void send_bindings_details(u32 index, vl_api_registration_t *rp,
/* Endian hack until apigen registers _details
* endian functions */
vl_api_pnat_bindings_details_t_endian(rmp);
vl_api_pnat_bindings_details_t_endian(
rmp, 1 /* to network */);
rmp->_vl_msg_id = htons(rmp->_vl_msg_id);
rmp->context = htonl(rmp->context);
}));
@ -158,7 +159,7 @@ static void send_interfaces_details(u32 index, vl_api_registration_t *rp,
/* Endian hack until apigen registers _details
* endian functions */
vl_api_pnat_interfaces_details_t_endian(rmp);
vl_api_pnat_interfaces_details_t_endian(rmp, 1 /* to network */);
rmp->_vl_msg_id = htons(rmp->_vl_msg_id);
rmp->context = htonl(rmp->context);
}));

View File

@ -1143,20 +1143,14 @@ ENDIAN_STRINGS = {
}
def get_endian_string(o, type):
def get_endian_string(o, fieldtype):
"""Return proper endian string conversion function"""
try:
if o.to_network:
return ENDIAN_STRINGS[type].replace("net_to_host", "host_to_net")
except:
pass
return ENDIAN_STRINGS[type]
return ENDIAN_STRINGS[fieldtype]
def endianfun_array(o):
"""Generate endian functions for arrays"""
forloop = """\
{comment}
ASSERT((u32){length} <= (u32)VL_API_MAX_ARRAY_SIZE);
for (i = 0; i < {length}; i++) {{
a->{name}[i] = {format}(a->{name}[i]);
@ -1165,31 +1159,26 @@ def endianfun_array(o):
forloop_format = """\
for (i = 0; i < {length}; i++) {{
{type}_endian(&a->{name}[i]);
{type}_endian(&a->{name}[i], to_net);
}}
"""
to_network_comment = ""
try:
if o.to_network:
to_network_comment = """/*
* Array fields processed first to handle variable length arrays and size
* field endian conversion in the proper order for to-network messages.
* Message fields have been sorted by type in the code generator, thus fields
* in this generated code may be converted in a different order than specified
* in the *.api file.
*/"""
except:
pass
output = ""
if o.fieldtype == "u8" or o.fieldtype == "string" or o.fieldtype == "bool":
output += " /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname)
else:
lfield = "a->" + o.lengthfield if o.lengthfield else o.length
if o.lengthfield:
output += (
f" u32 count = to_net ? clib_host_to_net_u32(a->{o.lengthfield}) : "
f"a->{o.lengthfield};\n"
)
lfield = "count"
else:
lfield = o.length
if o.fieldtype in ENDIAN_STRINGS:
output += forloop.format(
comment=to_network_comment,
length=lfield,
format=get_endian_string(o, o.fieldtype),
name=o.fieldname,
@ -1222,7 +1211,7 @@ def endianfun_obj(o):
name=o.fieldname, format=get_endian_string(o, o.fieldtype)
)
elif o.fieldtype.startswith("vl_api_"):
output += " {type}_endian(&a->{name});\n".format(
output += " {type}_endian(&a->{name}, to_net);\n".format(
type=o.fieldtype, name=o.fieldname
)
else:
@ -1254,19 +1243,12 @@ def endianfun(objs, modulename):
output = output.format(module=modulename)
signature = """\
static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a, bool to_net)
{{
int i __attribute__((unused));
"""
for t in objs:
# Outbound (to network) messages are identified by message nomenclature
# i.e. message names ending with these suffixes are 'to network'
if t.name.endswith("_reply") or t.name.endswith("_details"):
t.to_network = True
else:
t.to_network = False
if t.__class__.__name__ == "Enum" or t.__class__.__name__ == "EnumFlag":
output += signature.format(name=t.name)
if t.enumtype in ENDIAN_STRINGS:
@ -1300,15 +1282,7 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
output += signature.format(name=t.name)
# For outbound (to network) messages:
# some arrays have dynamic length -- iterate over
# them before changing endianness for the length field
# by making the Array types show up first
if t.to_network:
t.block.sort(key=lambda x: x.type)
for o in t.block:
o.to_network = t.to_network
output += endianfun_obj(o)
output += "}\n\n"
@ -1852,7 +1826,7 @@ api_{n} (cJSON *o)
}}
mp->_vl_msg_id = vac_get_msg_index(VL_API_{N}_CRC);
vl_api_{n}_t_endian(mp);
vl_api_{n}_t_endian(mp, 1);
vac_write((char *)mp, len);
cJSON_free(mp);
@ -1867,7 +1841,7 @@ api_{n} (cJSON *o)
return 0;
}}
vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
vl_api_{r}_t_endian(rmp);
vl_api_{r}_t_endian(rmp, 0);
return vl_api_{r}_t_tojson(rmp);
}}
@ -1885,7 +1859,7 @@ api_{n} (cJSON *o)
return 0;
}}
mp->_vl_msg_id = msg_id;
vl_api_{n}_t_endian(mp);
vl_api_{n}_t_endian(mp, 1);
vac_write((char *)mp, len);
cJSON_free(mp);
@ -1919,7 +1893,7 @@ api_{n} (cJSON *o)
return 0;
}}
vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
vl_api_{r}_t_endian(rmp);
vl_api_{r}_t_endian(rmp, 0);
cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
}}
}}
@ -1941,7 +1915,7 @@ api_{n} (cJSON *o)
}}
mp->_vl_msg_id = msg_id;
vl_api_{n}_t_endian(mp);
vl_api_{n}_t_endian(mp, 1);
vac_write((char *)mp, len);
cJSON_free(mp);
@ -1962,14 +1936,14 @@ api_{n} (cJSON *o)
u16 msg_id = ntohs(*((u16 *)p));
if (msg_id == reply_msg_id) {{
vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
vl_api_{r}_t_endian(rmp);
vl_api_{r}_t_endian(rmp, 0);
cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
break;
}}
if (msg_id == details_msg_id) {{
vl_api_{d}_t *rmp = (vl_api_{d}_t *)p;
vl_api_{d}_t_endian(rmp);
vl_api_{d}_t_endian(rmp, 0);
cJSON_AddItemToArray(reply, vl_api_{d}_t_tojson(rmp));
}}
}}

View File

@ -29,7 +29,7 @@ vat2_control_ping (u32 context)
vl_api_control_ping_t mp = {0};
mp._vl_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_CRC);
mp.context = context;
vl_api_control_ping_t_endian(&mp);
vl_api_control_ping_t_endian (&mp, 1 /* to network */);
vac_write((char *)&mp, sizeof(mp));
}

View File

@ -235,8 +235,8 @@ typedef struct
/** Message convert function vector */
void *(*fromjson_handler) (cJSON *, int *);
/** Message endian handler vector */
void (*endian_handler) (void *);
/** Message endian handler vector. */
void (*endian_handler) (void *, bool to_net);
/** Message calc size function vector */
uword (*calc_size_func) (void *);

View File

@ -29,9 +29,9 @@
#define _NATIVE_TO_NETWORK(t, rmp) \
api_main_t *am = vlibapi_get_main (); \
void (*endian_fp) (void *); \
void (*endian_fp) (void *, bool); \
endian_fp = am->msg_data[t + (REPLY_MSG_ID_BASE)].endian_handler; \
(*endian_fp) (rmp);
(*endian_fp) (rmp, 1 /* to network */);
#define REPLY_MACRO(msg) \
do \

View File

@ -230,7 +230,7 @@ vl_msg_api_trace_write_one (api_main_t *am, u8 *msg, FILE *fp)
if (m && m->endian_handler)
{
m->endian_handler (tmpmem);
m->endian_handler (tmpmem, 1);
}
if (m && m->tojson_handler)
@ -561,7 +561,7 @@ msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len,
}
if (m->is_autoendian)
m->endian_handler (the_msg);
m->endian_handler (the_msg, 0);
if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
clib_call_callbacks (am->perf_counter_cbs, am, id,

View File

@ -823,9 +823,9 @@ vl_mem_api_handler_with_vm_node (api_main_t *am, svm_region_t *vlib_rp,
if (m->is_autoendian)
{
void (*endian_fp) (void *);
void (*endian_fp) (void *, bool);
endian_fp = am->msg_data[id].endian_handler;
(*endian_fp) (the_msg);
(*endian_fp) (the_msg, 0);
}
if (PREDICT_FALSE (vec_len (am->perf_counter_cbs) != 0))
clib_call_callbacks (am->perf_counter_cbs, am, id, 0 /* before */);

View File

@ -554,7 +554,7 @@ vl_msg_api_process_file (vlib_main_t * vm, u8 * filename,
}
if (m)
{
m->endian_handler (tmpbuf + sizeof (uword));
m->endian_handler (tmpbuf + sizeof (uword), 1 /* to network */);
}
}
@ -674,7 +674,7 @@ vl_msg_print_trace (u8 *msg, void *ctx)
clib_memcpy_fast (tmpbuf, msg, msg_length);
msg = tmpbuf;
m->endian_handler (tmpbuf);
m->endian_handler (tmpbuf, 0 /* from network */);
}
vlib_cli_output (a->vm, "%U\n",
@ -824,7 +824,7 @@ vl_msg_exec_json_command (vlib_main_t *vm, cJSON *o)
}
if (clib_arch_is_little_endian)
m->endian_handler (msg);
m->endian_handler (msg, 1 /* to network */);
if (!m->handler)
{