Subject: Re: [PATCH iwl-next v2 04/13] ice: add parser internal helper functions

From: Simon Horman <[email-masked]>
Date: Fri, 31 May 2024 14:15:42 +0100

On Mon, May 27, 2024 at 12:58:01PM -0600, Ahmed Zaki wrote:
> From: Junfeng Guo <[email-masked]>
> 
> Add the following internal helper functions:
> 
> - ice_bst_tcam_match():
>   to perform ternary match on boost TCAM.
> 
> - ice_pg_cam_match():
>   to perform parse graph key match in cam table.
> 
> - ice_pg_nm_cam_match():
>   to perform parse graph key no match in cam table.
> 
> - ice_ptype_mk_tcam_match():
>   to perform ptype markers match in tcam table.
> 
> - ice_flg_redirect():
>   to redirect parser flags to packet flags.
> 
> - ice_xlt_kb_flag_get():
>   to aggregate 64 bit packet flag into 16 bit key builder flags.
> 
> Reviewed-by: Aleksandr Loktionov <[email-masked]>
> Reviewed-by: Wojciech Drewek <[email-masked]>
> Reviewed-by: Marcin Szycik <[email-masked]>
> Signed-off-by: Qi Zhang <[email-masked]>
> Signed-off-by: Junfeng Guo <[email-masked]>
> Signed-off-by: Ahmed Zaki <[email-masked]>
> ---
>  drivers/net/ethernet/intel/ice/ice_parser.c | 196 ++++++++++++++++++++
>  drivers/net/ethernet/intel/ice/ice_parser.h |  52 ++++--
>  2 files changed, 233 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/net/ethernet/intel/ice/ice_parser.c b/drivers/net/ethernet/intel/ice/ice_parser.c
> index 19dd7472b5ba..91dbe70d7fe5 100644
> --- a/drivers/net/ethernet/intel/ice/ice_parser.c
> +++ b/drivers/net/ethernet/intel/ice/ice_parser.c
> @@ -957,6 +957,105 @@ static struct ice_pg_nm_cam_item *ice_pg_nm_sp_cam_table_get(struct ice_hw *hw)
>  					ice_pg_nm_sp_cam_parse_item, false);
>  }
>  
> +static bool __ice_pg_cam_match(struct ice_pg_cam_item *item,
> +			       struct ice_pg_cam_key *key)
> +{
> +	return (item->key.valid &&
> +		!memcmp(&item->key.val, &key->val, sizeof(key->val)));
> +}
> +
> +static bool __ice_pg_nm_cam_match(struct ice_pg_nm_cam_item *item,
> +				  struct ice_pg_cam_key *key)
> +{
> +	return (item->key.valid &&
> +		!memcmp(&item->key.val, &key->val, sizeof(key->val)));
> +}

Hi,

The size of &item->key.val is 9 bytes, while the size of key->val is 13 bytes.
So this will compare data beyond the end of &item->key.val.

I think this is caused by the presence of the next_proto field
in the val struct_group of struct ice_pg_cam_key.

I do also wonder if there could be some consolidation in
the definitions of struct ice_pg_cam_key and struct ice_pg_nm_cam_key.
The main difference seems to be the presence of next_proto at
the end of the latter.

Flagged by Smatch.

...

> +/**
> + * ice_xlt_kb_flag_get - aggregate 64 bits packet flag into 16 bits xlt flag
> + * @kb: xlt key build
> + * @pkt_flag: 64 bits packet flag
> + */
> +u16 ice_xlt_kb_flag_get(struct ice_xlt_kb *kb, u64 pkt_flag)
> +{
> +	struct ice_xlt_kb_entry *entry = &kb->entries[0];
> +	u16 flag = 0;
> +	int i;
> +
> +	/* check flag 15 */
> +	if (kb->flag15 & pkt_flag)
> +		flag = (u16)BIT(ICE_XLT_KB_FLAG0_14_CNT);

nit: It's not clear to me that this cast is necessary.
     Likewise twice more in this function, and elsewhere in this patchset.

> +
> +	/* check flag 0 - 14 */
> +	for (i = 0; i < ICE_XLT_KB_FLAG0_14_CNT; i++) {
> +		/* only check first entry */
> +		u16 idx = (u16)(entry->flg0_14_sel[i] & ICE_XLT_KB_MASK);
> +
> +		if (pkt_flag & BIT(idx))
> +			flag |= (u16)BIT(i);
> +	}
> +
> +	return flag;
> +}

...