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