2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_dpipe.c
3 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2017 Arkadi Sharshevsky <arakdis@mellanox.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/kernel.h>
36 #include <net/devlink.h>
39 #include "spectrum_dpipe.h"
40 #include "spectrum_router.h"
42 enum mlxsw_sp_field_metadata_id {
43 MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT,
44 MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD,
45 MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP,
48 static struct devlink_dpipe_field mlxsw_sp_dpipe_fields_metadata[] = {
49 { .name = "erif_port",
50 .id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT,
52 .mapping_type = DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX,
54 { .name = "l3_forward",
55 .id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD,
59 .id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP,
64 enum mlxsw_sp_dpipe_header_id {
65 MLXSW_SP_DPIPE_HEADER_METADATA,
68 static struct devlink_dpipe_header mlxsw_sp_dpipe_header_metadata = {
70 .id = MLXSW_SP_DPIPE_HEADER_METADATA,
71 .fields = mlxsw_sp_dpipe_fields_metadata,
72 .fields_count = ARRAY_SIZE(mlxsw_sp_dpipe_fields_metadata),
75 static struct devlink_dpipe_header *mlxsw_dpipe_headers[] = {
76 &mlxsw_sp_dpipe_header_metadata,
79 static struct devlink_dpipe_headers mlxsw_sp_dpipe_headers = {
80 .headers = mlxsw_dpipe_headers,
81 .headers_count = ARRAY_SIZE(mlxsw_dpipe_headers),
84 static int mlxsw_sp_dpipe_table_erif_actions_dump(void *priv,
87 struct devlink_dpipe_action action = {0};
90 action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
91 action.header = &mlxsw_sp_dpipe_header_metadata;
92 action.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD;
94 err = devlink_dpipe_action_put(skb, &action);
98 action.type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
99 action.header = &mlxsw_sp_dpipe_header_metadata;
100 action.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_DROP;
102 return devlink_dpipe_action_put(skb, &action);
105 static int mlxsw_sp_dpipe_table_erif_matches_dump(void *priv,
108 struct devlink_dpipe_match match = {0};
110 match.type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
111 match.header = &mlxsw_sp_dpipe_header_metadata;
112 match.field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
114 return devlink_dpipe_match_put(skb, &match);
117 static void mlxsw_sp_erif_entry_clear(struct devlink_dpipe_entry *entry)
119 unsigned int value_count, value_index;
120 struct devlink_dpipe_value *value;
122 value = entry->action_values;
123 value_count = entry->action_values_count;
124 for (value_index = 0; value_index < value_count; value_index++) {
125 kfree(value[value_index].value);
126 kfree(value[value_index].mask);
129 value = entry->match_values;
130 value_count = entry->match_values_count;
131 for (value_index = 0; value_index < value_count; value_index++) {
132 kfree(value[value_index].value);
133 kfree(value[value_index].mask);
138 mlxsw_sp_erif_match_action_prepare(struct devlink_dpipe_match *match,
139 struct devlink_dpipe_action *action)
141 action->type = DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY;
142 action->header = &mlxsw_sp_dpipe_header_metadata;
143 action->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_L3_FORWARD;
145 match->type = DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT;
146 match->header = &mlxsw_sp_dpipe_header_metadata;
147 match->field_id = MLXSW_SP_DPIPE_FIELD_METADATA_ERIF_PORT;
150 static int mlxsw_sp_erif_entry_prepare(struct devlink_dpipe_entry *entry,
151 struct devlink_dpipe_value *match_value,
152 struct devlink_dpipe_match *match,
153 struct devlink_dpipe_value *action_value,
154 struct devlink_dpipe_action *action)
156 entry->match_values = match_value;
157 entry->match_values_count = 1;
159 entry->action_values = action_value;
160 entry->action_values_count = 1;
162 match_value->match = match;
163 match_value->value_size = sizeof(u32);
164 match_value->value = kmalloc(match_value->value_size, GFP_KERNEL);
165 if (!match_value->value)
168 action_value->action = action;
169 action_value->value_size = sizeof(u32);
170 action_value->value = kmalloc(action_value->value_size, GFP_KERNEL);
171 if (!action_value->value)
172 goto err_action_alloc;
176 kfree(match_value->value);
180 static int mlxsw_sp_erif_entry_get(struct mlxsw_sp *mlxsw_sp,
181 struct devlink_dpipe_entry *entry,
182 struct mlxsw_sp_rif *rif,
183 bool counters_enabled)
190 /* Set Match RIF index */
191 rif_value = entry->match_values->value;
192 *rif_value = mlxsw_sp_rif_index(rif);
193 entry->match_values->mapping_value = mlxsw_sp_rif_dev_ifindex(rif);
194 entry->match_values->mapping_valid = true;
196 /* Set Action Forwarding */
197 action_value = entry->action_values->value;
200 entry->counter_valid = false;
202 if (!counters_enabled)
205 entry->index = mlxsw_sp_rif_index(rif);
206 err = mlxsw_sp_rif_counter_value_get(mlxsw_sp, rif,
207 MLXSW_SP_RIF_COUNTER_EGRESS,
210 entry->counter = cnt;
211 entry->counter_valid = true;
217 mlxsw_sp_table_erif_entries_dump(void *priv, bool counters_enabled,
218 struct devlink_dpipe_dump_ctx *dump_ctx)
220 struct devlink_dpipe_value match_value = {{0}}, action_value = {{0}};
221 struct devlink_dpipe_action action = {0};
222 struct devlink_dpipe_match match = {0};
223 struct devlink_dpipe_entry entry = {0};
224 struct mlxsw_sp *mlxsw_sp = priv;
225 unsigned int rif_count;
229 mlxsw_sp_erif_match_action_prepare(&match, &action);
230 err = mlxsw_sp_erif_entry_prepare(&entry, &match_value, &match,
231 &action_value, &action);
235 rif_count = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
239 err = devlink_dpipe_entry_ctx_prepare(dump_ctx);
243 for (; i < rif_count; i++) {
244 if (!mlxsw_sp->rifs[i])
246 err = mlxsw_sp_erif_entry_get(mlxsw_sp, &entry,
251 err = devlink_dpipe_entry_ctx_append(dump_ctx, &entry);
253 if (err == -EMSGSIZE) {
255 goto err_entry_append;
258 goto err_entry_append;
263 devlink_dpipe_entry_ctx_close(dump_ctx);
268 mlxsw_sp_erif_entry_clear(&entry);
273 mlxsw_sp_erif_entry_clear(&entry);
277 static int mlxsw_sp_table_erif_counters_update(void *priv, bool enable)
279 struct mlxsw_sp *mlxsw_sp = priv;
283 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
284 if (!mlxsw_sp->rifs[i])
287 mlxsw_sp_rif_counter_alloc(mlxsw_sp,
289 MLXSW_SP_RIF_COUNTER_EGRESS);
291 mlxsw_sp_rif_counter_free(mlxsw_sp,
293 MLXSW_SP_RIF_COUNTER_EGRESS);
299 static struct devlink_dpipe_table_ops mlxsw_sp_erif_ops = {
300 .matches_dump = mlxsw_sp_dpipe_table_erif_matches_dump,
301 .actions_dump = mlxsw_sp_dpipe_table_erif_actions_dump,
302 .entries_dump = mlxsw_sp_table_erif_entries_dump,
303 .counters_set_update = mlxsw_sp_table_erif_counters_update,
306 static int mlxsw_sp_dpipe_erif_table_init(struct mlxsw_sp *mlxsw_sp)
308 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
311 table_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
312 return devlink_dpipe_table_register(devlink,
313 MLXSW_SP_DPIPE_TABLE_NAME_ERIF,
315 mlxsw_sp, table_size,
319 static void mlxsw_sp_dpipe_erif_table_fini(struct mlxsw_sp *mlxsw_sp)
321 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
323 devlink_dpipe_table_unregister(devlink, MLXSW_SP_DPIPE_TABLE_NAME_ERIF);
326 int mlxsw_sp_dpipe_init(struct mlxsw_sp *mlxsw_sp)
328 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
331 err = devlink_dpipe_headers_register(devlink,
332 &mlxsw_sp_dpipe_headers);
335 err = mlxsw_sp_dpipe_erif_table_init(mlxsw_sp);
337 goto err_erif_register;
341 devlink_dpipe_headers_unregister(priv_to_devlink(mlxsw_sp->core));
345 void mlxsw_sp_dpipe_fini(struct mlxsw_sp *mlxsw_sp)
347 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
349 mlxsw_sp_dpipe_erif_table_fini(mlxsw_sp);
350 devlink_dpipe_headers_unregister(devlink);