1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2014-2021 Broadcom 3 * All rights reserved. 4 */ 5 6 #include "ulp_matcher.h" 7 #include "ulp_utils.h" 8 9 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG 10 #include "ulp_template_debug_proto.h" 11 #endif 12 13 /* Utility function to calculate the class matcher hash */ 14 static uint32_t 15 ulp_matcher_class_hash_calculate(uint64_t hi_sig, uint64_t lo_sig) 16 { 17 uint64_t hash; 18 19 hi_sig |= ((hi_sig % BNXT_ULP_CLASS_HID_HIGH_PRIME) << 20 BNXT_ULP_CLASS_HID_SHFTL); 21 lo_sig |= ((lo_sig % BNXT_ULP_CLASS_HID_LOW_PRIME) << 22 (BNXT_ULP_CLASS_HID_SHFTL + 2)); 23 hash = hi_sig ^ lo_sig; 24 hash = (hash >> BNXT_ULP_CLASS_HID_SHFTR) & BNXT_ULP_CLASS_HID_MASK; 25 return (uint32_t)hash; 26 } 27 28 /* Utility function to calculate the action matcher hash */ 29 static uint32_t 30 ulp_matcher_action_hash_calculate(uint64_t hi_sig, uint64_t app_id) 31 { 32 uint64_t hash; 33 34 hi_sig |= ((hi_sig % BNXT_ULP_ACT_HID_HIGH_PRIME) << 35 BNXT_ULP_ACT_HID_SHFTL); 36 app_id |= ((app_id % BNXT_ULP_CLASS_HID_LOW_PRIME) << 37 (BNXT_ULP_CLASS_HID_SHFTL + 2)); 38 hash = hi_sig ^ app_id; 39 hash = (hash >> BNXT_ULP_ACT_HID_SHFTR) & BNXT_ULP_ACT_HID_MASK; 40 return (uint32_t)hash; 41 } 42 43 /* 44 * Function to handle the matching of RTE Flows and validating 45 * the pattern masks against the flow templates. 46 */ 47 int32_t 48 ulp_matcher_pattern_match(struct ulp_rte_parser_params *params, 49 uint32_t *class_id) 50 { 51 struct bnxt_ulp_class_match_info *class_match; 52 uint32_t class_hid; 53 uint8_t vf_to_vf; 54 uint16_t tmpl_id; 55 56 /* Get vf to vf flow */ 57 vf_to_vf = ULP_COMP_FLD_IDX_RD(params, BNXT_ULP_CF_IDX_VF_TO_VF); 58 59 /* calculate the hash of the given flow */ 60 class_hid = ulp_matcher_class_hash_calculate((params->hdr_bitmap.bits ^ 61 params->app_id), 62 params->fld_s_bitmap.bits); 63 64 /* validate the calculate hash values */ 65 if (class_hid >= BNXT_ULP_CLASS_SIG_TBL_MAX_SZ) 66 goto error; 67 tmpl_id = ulp_class_sig_tbl[class_hid]; 68 if (!tmpl_id) 69 goto error; 70 71 class_match = &ulp_class_match_list[tmpl_id]; 72 if (ULP_BITMAP_CMP(¶ms->hdr_bitmap, &class_match->hdr_sig)) { 73 BNXT_TF_DBG(DEBUG, "Proto Header does not match\n"); 74 goto error; 75 } 76 if (ULP_BITMAP_CMP(¶ms->fld_s_bitmap, &class_match->field_sig)) { 77 BNXT_TF_DBG(DEBUG, "Field signature does not match\n"); 78 goto error; 79 } 80 81 /* Match the application id before proceeding */ 82 if (params->app_id != class_match->app_sig) { 83 BNXT_TF_DBG(DEBUG, "Field to match the app id %u:%u\n", 84 params->app_id, class_match->app_sig); 85 goto error; 86 } 87 88 if (vf_to_vf != class_match->act_vnic) { 89 BNXT_TF_DBG(DEBUG, "Vnic Match failed\n"); 90 goto error; 91 } 92 BNXT_TF_DBG(DEBUG, "Found matching pattern template %d\n", 93 class_match->class_tid); 94 *class_id = class_match->class_tid; 95 params->hdr_sig_id = class_match->hdr_sig_id; 96 params->flow_sig_id = class_match->flow_sig_id; 97 params->flow_pattern_id = class_match->flow_pattern_id; 98 return BNXT_TF_RC_SUCCESS; 99 100 error: 101 BNXT_TF_DBG(DEBUG, "Did not find any matching template\n"); 102 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG 103 BNXT_TF_DBG(DEBUG, "class_hid:0x%x, Hdr:%" PRIX64 " Fld:%" PRIX64 "\n", 104 class_hid, params->hdr_bitmap.bits, 105 params->fld_bitmap.bits); 106 #endif 107 *class_id = 0; 108 return BNXT_TF_RC_ERROR; 109 } 110 111 /* 112 * Function to handle the matching of RTE Flows and validating 113 * the action against the flow templates. 114 */ 115 int32_t 116 ulp_matcher_action_match(struct ulp_rte_parser_params *params, 117 uint32_t *act_id) 118 { 119 uint32_t act_hid; 120 uint16_t tmpl_id; 121 struct bnxt_ulp_act_match_info *act_match; 122 123 /* calculate the hash of the given flow action */ 124 act_hid = ulp_matcher_action_hash_calculate(params->act_bitmap.bits, 125 params->app_id); 126 127 /* validate the calculate hash values */ 128 if (act_hid >= BNXT_ULP_ACT_SIG_TBL_MAX_SZ) 129 goto error; 130 tmpl_id = ulp_act_sig_tbl[act_hid]; 131 if (!tmpl_id) 132 goto error; 133 134 act_match = &ulp_act_match_list[tmpl_id]; 135 if (ULP_BITMAP_CMP(¶ms->act_bitmap, &act_match->act_sig)) { 136 BNXT_TF_DBG(DEBUG, "Action Header does not match\n"); 137 goto error; 138 } 139 140 /* Match the application id before proceeding */ 141 if (params->app_id != act_match->app_sig) { 142 BNXT_TF_DBG(DEBUG, "Field to match the app id %u:%u\n", 143 params->app_id, act_match->app_sig); 144 goto error; 145 } 146 147 *act_id = act_match->act_tid; 148 params->act_pattern_id = act_match->act_pattern_id; 149 BNXT_TF_DBG(DEBUG, "Found matching action template %u\n", *act_id); 150 return BNXT_TF_RC_SUCCESS; 151 152 error: 153 BNXT_TF_DBG(DEBUG, "Did not find any matching action template\n"); 154 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG 155 BNXT_TF_DBG(DEBUG, "act_hid:0x%x, Hdr:%" PRIX64 "\n", 156 act_hid, params->act_bitmap.bits); 157 #endif 158 *act_id = 0; 159 return BNXT_TF_RC_ERROR; 160 } 161