• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
3 
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/errno.h>
7 #include <linux/list.h>
8 #include <linux/string.h>
9 #include <linux/rhashtable.h>
10 #include <linux/netdevice.h>
11 #include <net/net_namespace.h>
12 #include <net/tc_act/tc_vlan.h>
13 
14 #include "reg.h"
15 #include "core.h"
16 #include "resources.h"
17 #include "spectrum.h"
18 #include "core_acl_flex_keys.h"
19 #include "core_acl_flex_actions.h"
20 #include "spectrum_acl_tcam.h"
21 
22 struct mlxsw_sp_acl {
23 	struct mlxsw_sp *mlxsw_sp;
24 	struct mlxsw_afk *afk;
25 	struct mlxsw_sp_fid *dummy_fid;
26 	struct rhashtable ruleset_ht;
27 	struct list_head rules;
28 	struct {
29 		struct delayed_work dw;
30 		unsigned long interval;	/* ms */
31 #define MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS 1000
32 	} rule_activity_update;
33 	struct mlxsw_sp_acl_tcam tcam;
34 };
35 
mlxsw_sp_acl_afk(struct mlxsw_sp_acl * acl)36 struct mlxsw_afk *mlxsw_sp_acl_afk(struct mlxsw_sp_acl *acl)
37 {
38 	return acl->afk;
39 }
40 
41 struct mlxsw_sp_acl_block_binding {
42 	struct list_head list;
43 	struct net_device *dev;
44 	struct mlxsw_sp_port *mlxsw_sp_port;
45 	bool ingress;
46 };
47 
48 struct mlxsw_sp_acl_block {
49 	struct list_head binding_list;
50 	struct mlxsw_sp_acl_ruleset *ruleset_zero;
51 	struct mlxsw_sp *mlxsw_sp;
52 	unsigned int rule_count;
53 	unsigned int disable_count;
54 };
55 
56 struct mlxsw_sp_acl_ruleset_ht_key {
57 	struct mlxsw_sp_acl_block *block;
58 	u32 chain_index;
59 	const struct mlxsw_sp_acl_profile_ops *ops;
60 };
61 
62 struct mlxsw_sp_acl_ruleset {
63 	struct rhash_head ht_node; /* Member of acl HT */
64 	struct mlxsw_sp_acl_ruleset_ht_key ht_key;
65 	struct rhashtable rule_ht;
66 	unsigned int ref_count;
67 	unsigned long priv[0];
68 	/* priv has to be always the last item */
69 };
70 
71 struct mlxsw_sp_acl_rule {
72 	struct rhash_head ht_node; /* Member of rule HT */
73 	struct list_head list;
74 	unsigned long cookie; /* HT key */
75 	struct mlxsw_sp_acl_ruleset *ruleset;
76 	struct mlxsw_sp_acl_rule_info *rulei;
77 	u64 last_used;
78 	u64 last_packets;
79 	u64 last_bytes;
80 	unsigned long priv[0];
81 	/* priv has to be always the last item */
82 };
83 
84 static const struct rhashtable_params mlxsw_sp_acl_ruleset_ht_params = {
85 	.key_len = sizeof(struct mlxsw_sp_acl_ruleset_ht_key),
86 	.key_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_key),
87 	.head_offset = offsetof(struct mlxsw_sp_acl_ruleset, ht_node),
88 	.automatic_shrinking = true,
89 };
90 
91 static const struct rhashtable_params mlxsw_sp_acl_rule_ht_params = {
92 	.key_len = sizeof(unsigned long),
93 	.key_offset = offsetof(struct mlxsw_sp_acl_rule, cookie),
94 	.head_offset = offsetof(struct mlxsw_sp_acl_rule, ht_node),
95 	.automatic_shrinking = true,
96 };
97 
mlxsw_sp_acl_dummy_fid(struct mlxsw_sp * mlxsw_sp)98 struct mlxsw_sp_fid *mlxsw_sp_acl_dummy_fid(struct mlxsw_sp *mlxsw_sp)
99 {
100 	return mlxsw_sp->acl->dummy_fid;
101 }
102 
mlxsw_sp_acl_block_mlxsw_sp(struct mlxsw_sp_acl_block * block)103 struct mlxsw_sp *mlxsw_sp_acl_block_mlxsw_sp(struct mlxsw_sp_acl_block *block)
104 {
105 	return block->mlxsw_sp;
106 }
107 
mlxsw_sp_acl_block_rule_count(struct mlxsw_sp_acl_block * block)108 unsigned int mlxsw_sp_acl_block_rule_count(struct mlxsw_sp_acl_block *block)
109 {
110 	return block ? block->rule_count : 0;
111 }
112 
mlxsw_sp_acl_block_disable_inc(struct mlxsw_sp_acl_block * block)113 void mlxsw_sp_acl_block_disable_inc(struct mlxsw_sp_acl_block *block)
114 {
115 	if (block)
116 		block->disable_count++;
117 }
118 
mlxsw_sp_acl_block_disable_dec(struct mlxsw_sp_acl_block * block)119 void mlxsw_sp_acl_block_disable_dec(struct mlxsw_sp_acl_block *block)
120 {
121 	if (block)
122 		block->disable_count--;
123 }
124 
mlxsw_sp_acl_block_disabled(struct mlxsw_sp_acl_block * block)125 bool mlxsw_sp_acl_block_disabled(struct mlxsw_sp_acl_block *block)
126 {
127 	return block->disable_count;
128 }
129 
mlxsw_sp_acl_block_is_egress_bound(struct mlxsw_sp_acl_block * block)130 bool mlxsw_sp_acl_block_is_egress_bound(struct mlxsw_sp_acl_block *block)
131 {
132 	struct mlxsw_sp_acl_block_binding *binding;
133 
134 	list_for_each_entry(binding, &block->binding_list, list) {
135 		if (!binding->ingress)
136 			return true;
137 	}
138 	return false;
139 }
140 
141 static bool
mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset * ruleset)142 mlxsw_sp_acl_ruleset_is_singular(const struct mlxsw_sp_acl_ruleset *ruleset)
143 {
144 	/* We hold a reference on ruleset ourselves */
145 	return ruleset->ref_count == 2;
146 }
147 
148 static int
mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_block * block,struct mlxsw_sp_acl_block_binding * binding)149 mlxsw_sp_acl_ruleset_bind(struct mlxsw_sp *mlxsw_sp,
150 			  struct mlxsw_sp_acl_block *block,
151 			  struct mlxsw_sp_acl_block_binding *binding)
152 {
153 	struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
154 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
155 
156 	return ops->ruleset_bind(mlxsw_sp, ruleset->priv,
157 				 binding->mlxsw_sp_port, binding->ingress);
158 }
159 
160 static void
mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_block * block,struct mlxsw_sp_acl_block_binding * binding)161 mlxsw_sp_acl_ruleset_unbind(struct mlxsw_sp *mlxsw_sp,
162 			    struct mlxsw_sp_acl_block *block,
163 			    struct mlxsw_sp_acl_block_binding *binding)
164 {
165 	struct mlxsw_sp_acl_ruleset *ruleset = block->ruleset_zero;
166 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
167 
168 	ops->ruleset_unbind(mlxsw_sp, ruleset->priv,
169 			    binding->mlxsw_sp_port, binding->ingress);
170 }
171 
mlxsw_sp_acl_ruleset_block_bound(struct mlxsw_sp_acl_block * block)172 static bool mlxsw_sp_acl_ruleset_block_bound(struct mlxsw_sp_acl_block *block)
173 {
174 	return block->ruleset_zero;
175 }
176 
177 static int
mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ruleset * ruleset,struct mlxsw_sp_acl_block * block)178 mlxsw_sp_acl_ruleset_block_bind(struct mlxsw_sp *mlxsw_sp,
179 				struct mlxsw_sp_acl_ruleset *ruleset,
180 				struct mlxsw_sp_acl_block *block)
181 {
182 	struct mlxsw_sp_acl_block_binding *binding;
183 	int err;
184 
185 	block->ruleset_zero = ruleset;
186 	list_for_each_entry(binding, &block->binding_list, list) {
187 		err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
188 		if (err)
189 			goto rollback;
190 	}
191 	return 0;
192 
193 rollback:
194 	list_for_each_entry_continue_reverse(binding, &block->binding_list,
195 					     list)
196 		mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
197 	block->ruleset_zero = NULL;
198 
199 	return err;
200 }
201 
202 static void
mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ruleset * ruleset,struct mlxsw_sp_acl_block * block)203 mlxsw_sp_acl_ruleset_block_unbind(struct mlxsw_sp *mlxsw_sp,
204 				  struct mlxsw_sp_acl_ruleset *ruleset,
205 				  struct mlxsw_sp_acl_block *block)
206 {
207 	struct mlxsw_sp_acl_block_binding *binding;
208 
209 	list_for_each_entry(binding, &block->binding_list, list)
210 		mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
211 	block->ruleset_zero = NULL;
212 }
213 
mlxsw_sp_acl_block_create(struct mlxsw_sp * mlxsw_sp,struct net * net)214 struct mlxsw_sp_acl_block *mlxsw_sp_acl_block_create(struct mlxsw_sp *mlxsw_sp,
215 						     struct net *net)
216 {
217 	struct mlxsw_sp_acl_block *block;
218 
219 	block = kzalloc(sizeof(*block), GFP_KERNEL);
220 	if (!block)
221 		return NULL;
222 	INIT_LIST_HEAD(&block->binding_list);
223 	block->mlxsw_sp = mlxsw_sp;
224 	return block;
225 }
226 
mlxsw_sp_acl_block_destroy(struct mlxsw_sp_acl_block * block)227 void mlxsw_sp_acl_block_destroy(struct mlxsw_sp_acl_block *block)
228 {
229 	WARN_ON(!list_empty(&block->binding_list));
230 	kfree(block);
231 }
232 
233 static struct mlxsw_sp_acl_block_binding *
mlxsw_sp_acl_block_lookup(struct mlxsw_sp_acl_block * block,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)234 mlxsw_sp_acl_block_lookup(struct mlxsw_sp_acl_block *block,
235 			  struct mlxsw_sp_port *mlxsw_sp_port, bool ingress)
236 {
237 	struct mlxsw_sp_acl_block_binding *binding;
238 
239 	list_for_each_entry(binding, &block->binding_list, list)
240 		if (binding->mlxsw_sp_port == mlxsw_sp_port &&
241 		    binding->ingress == ingress)
242 			return binding;
243 	return NULL;
244 }
245 
mlxsw_sp_acl_block_bind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_block * block,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)246 int mlxsw_sp_acl_block_bind(struct mlxsw_sp *mlxsw_sp,
247 			    struct mlxsw_sp_acl_block *block,
248 			    struct mlxsw_sp_port *mlxsw_sp_port,
249 			    bool ingress)
250 {
251 	struct mlxsw_sp_acl_block_binding *binding;
252 	int err;
253 
254 	if (WARN_ON(mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress)))
255 		return -EEXIST;
256 
257 	binding = kzalloc(sizeof(*binding), GFP_KERNEL);
258 	if (!binding)
259 		return -ENOMEM;
260 	binding->mlxsw_sp_port = mlxsw_sp_port;
261 	binding->ingress = ingress;
262 
263 	if (mlxsw_sp_acl_ruleset_block_bound(block)) {
264 		err = mlxsw_sp_acl_ruleset_bind(mlxsw_sp, block, binding);
265 		if (err)
266 			goto err_ruleset_bind;
267 	}
268 
269 	list_add(&binding->list, &block->binding_list);
270 	return 0;
271 
272 err_ruleset_bind:
273 	kfree(binding);
274 	return err;
275 }
276 
mlxsw_sp_acl_block_unbind(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_block * block,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)277 int mlxsw_sp_acl_block_unbind(struct mlxsw_sp *mlxsw_sp,
278 			      struct mlxsw_sp_acl_block *block,
279 			      struct mlxsw_sp_port *mlxsw_sp_port,
280 			      bool ingress)
281 {
282 	struct mlxsw_sp_acl_block_binding *binding;
283 
284 	binding = mlxsw_sp_acl_block_lookup(block, mlxsw_sp_port, ingress);
285 	if (!binding)
286 		return -ENOENT;
287 
288 	list_del(&binding->list);
289 
290 	if (mlxsw_sp_acl_ruleset_block_bound(block))
291 		mlxsw_sp_acl_ruleset_unbind(mlxsw_sp, block, binding);
292 
293 	kfree(binding);
294 	return 0;
295 }
296 
297 static struct mlxsw_sp_acl_ruleset *
mlxsw_sp_acl_ruleset_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_block * block,u32 chain_index,const struct mlxsw_sp_acl_profile_ops * ops,struct mlxsw_afk_element_usage * tmplt_elusage)298 mlxsw_sp_acl_ruleset_create(struct mlxsw_sp *mlxsw_sp,
299 			    struct mlxsw_sp_acl_block *block, u32 chain_index,
300 			    const struct mlxsw_sp_acl_profile_ops *ops,
301 			    struct mlxsw_afk_element_usage *tmplt_elusage)
302 {
303 	struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
304 	struct mlxsw_sp_acl_ruleset *ruleset;
305 	size_t alloc_size;
306 	int err;
307 
308 	alloc_size = sizeof(*ruleset) + ops->ruleset_priv_size;
309 	ruleset = kzalloc(alloc_size, GFP_KERNEL);
310 	if (!ruleset)
311 		return ERR_PTR(-ENOMEM);
312 	ruleset->ref_count = 1;
313 	ruleset->ht_key.block = block;
314 	ruleset->ht_key.chain_index = chain_index;
315 	ruleset->ht_key.ops = ops;
316 
317 	err = rhashtable_init(&ruleset->rule_ht, &mlxsw_sp_acl_rule_ht_params);
318 	if (err)
319 		goto err_rhashtable_init;
320 
321 	err = ops->ruleset_add(mlxsw_sp, &acl->tcam, ruleset->priv,
322 			       tmplt_elusage);
323 	if (err)
324 		goto err_ops_ruleset_add;
325 
326 	err = rhashtable_insert_fast(&acl->ruleset_ht, &ruleset->ht_node,
327 				     mlxsw_sp_acl_ruleset_ht_params);
328 	if (err)
329 		goto err_ht_insert;
330 
331 	return ruleset;
332 
333 err_ht_insert:
334 	ops->ruleset_del(mlxsw_sp, ruleset->priv);
335 err_ops_ruleset_add:
336 	rhashtable_destroy(&ruleset->rule_ht);
337 err_rhashtable_init:
338 	kfree(ruleset);
339 	return ERR_PTR(err);
340 }
341 
mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ruleset * ruleset)342 static void mlxsw_sp_acl_ruleset_destroy(struct mlxsw_sp *mlxsw_sp,
343 					 struct mlxsw_sp_acl_ruleset *ruleset)
344 {
345 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
346 	struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
347 
348 	rhashtable_remove_fast(&acl->ruleset_ht, &ruleset->ht_node,
349 			       mlxsw_sp_acl_ruleset_ht_params);
350 	ops->ruleset_del(mlxsw_sp, ruleset->priv);
351 	rhashtable_destroy(&ruleset->rule_ht);
352 	kfree(ruleset);
353 }
354 
mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset * ruleset)355 static void mlxsw_sp_acl_ruleset_ref_inc(struct mlxsw_sp_acl_ruleset *ruleset)
356 {
357 	ruleset->ref_count++;
358 }
359 
mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ruleset * ruleset)360 static void mlxsw_sp_acl_ruleset_ref_dec(struct mlxsw_sp *mlxsw_sp,
361 					 struct mlxsw_sp_acl_ruleset *ruleset)
362 {
363 	if (--ruleset->ref_count)
364 		return;
365 	mlxsw_sp_acl_ruleset_destroy(mlxsw_sp, ruleset);
366 }
367 
368 static struct mlxsw_sp_acl_ruleset *
__mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl * acl,struct mlxsw_sp_acl_block * block,u32 chain_index,const struct mlxsw_sp_acl_profile_ops * ops)369 __mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp_acl *acl,
370 			      struct mlxsw_sp_acl_block *block, u32 chain_index,
371 			      const struct mlxsw_sp_acl_profile_ops *ops)
372 {
373 	struct mlxsw_sp_acl_ruleset_ht_key ht_key;
374 
375 	memset(&ht_key, 0, sizeof(ht_key));
376 	ht_key.block = block;
377 	ht_key.chain_index = chain_index;
378 	ht_key.ops = ops;
379 	return rhashtable_lookup_fast(&acl->ruleset_ht, &ht_key,
380 				      mlxsw_sp_acl_ruleset_ht_params);
381 }
382 
383 struct mlxsw_sp_acl_ruleset *
mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_block * block,u32 chain_index,enum mlxsw_sp_acl_profile profile)384 mlxsw_sp_acl_ruleset_lookup(struct mlxsw_sp *mlxsw_sp,
385 			    struct mlxsw_sp_acl_block *block, u32 chain_index,
386 			    enum mlxsw_sp_acl_profile profile)
387 {
388 	const struct mlxsw_sp_acl_profile_ops *ops;
389 	struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
390 	struct mlxsw_sp_acl_ruleset *ruleset;
391 
392 	ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
393 	if (!ops)
394 		return ERR_PTR(-EINVAL);
395 	ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
396 	if (!ruleset)
397 		return ERR_PTR(-ENOENT);
398 	return ruleset;
399 }
400 
401 struct mlxsw_sp_acl_ruleset *
mlxsw_sp_acl_ruleset_get(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_block * block,u32 chain_index,enum mlxsw_sp_acl_profile profile,struct mlxsw_afk_element_usage * tmplt_elusage)402 mlxsw_sp_acl_ruleset_get(struct mlxsw_sp *mlxsw_sp,
403 			 struct mlxsw_sp_acl_block *block, u32 chain_index,
404 			 enum mlxsw_sp_acl_profile profile,
405 			 struct mlxsw_afk_element_usage *tmplt_elusage)
406 {
407 	const struct mlxsw_sp_acl_profile_ops *ops;
408 	struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
409 	struct mlxsw_sp_acl_ruleset *ruleset;
410 
411 	ops = mlxsw_sp_acl_tcam_profile_ops(mlxsw_sp, profile);
412 	if (!ops)
413 		return ERR_PTR(-EINVAL);
414 
415 	ruleset = __mlxsw_sp_acl_ruleset_lookup(acl, block, chain_index, ops);
416 	if (ruleset) {
417 		mlxsw_sp_acl_ruleset_ref_inc(ruleset);
418 		return ruleset;
419 	}
420 	return mlxsw_sp_acl_ruleset_create(mlxsw_sp, block, chain_index, ops,
421 					   tmplt_elusage);
422 }
423 
mlxsw_sp_acl_ruleset_put(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ruleset * ruleset)424 void mlxsw_sp_acl_ruleset_put(struct mlxsw_sp *mlxsw_sp,
425 			      struct mlxsw_sp_acl_ruleset *ruleset)
426 {
427 	mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
428 }
429 
mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset * ruleset)430 u16 mlxsw_sp_acl_ruleset_group_id(struct mlxsw_sp_acl_ruleset *ruleset)
431 {
432 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
433 
434 	return ops->ruleset_group_id(ruleset->priv);
435 }
436 
437 struct mlxsw_sp_acl_rule_info *
mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl * acl)438 mlxsw_sp_acl_rulei_create(struct mlxsw_sp_acl *acl)
439 {
440 	struct mlxsw_sp_acl_rule_info *rulei;
441 	int err;
442 
443 	rulei = kzalloc(sizeof(*rulei), GFP_KERNEL);
444 	if (!rulei)
445 		return ERR_PTR(-ENOMEM);
446 
447 	rulei->act_block = mlxsw_afa_block_create(acl->mlxsw_sp->afa);
448 	if (IS_ERR(rulei->act_block)) {
449 		err = PTR_ERR(rulei->act_block);
450 		goto err_afa_block_create;
451 	}
452 	return rulei;
453 
454 err_afa_block_create:
455 	kfree(rulei);
456 	return ERR_PTR(err);
457 }
458 
mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp_acl_rule_info * rulei)459 void mlxsw_sp_acl_rulei_destroy(struct mlxsw_sp_acl_rule_info *rulei)
460 {
461 	mlxsw_afa_block_destroy(rulei->act_block);
462 	kfree(rulei);
463 }
464 
mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info * rulei)465 int mlxsw_sp_acl_rulei_commit(struct mlxsw_sp_acl_rule_info *rulei)
466 {
467 	return mlxsw_afa_block_commit(rulei->act_block);
468 }
469 
mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info * rulei,unsigned int priority)470 void mlxsw_sp_acl_rulei_priority(struct mlxsw_sp_acl_rule_info *rulei,
471 				 unsigned int priority)
472 {
473 	rulei->priority = priority >> 16;
474 }
475 
mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info * rulei,enum mlxsw_afk_element element,u32 key_value,u32 mask_value)476 void mlxsw_sp_acl_rulei_keymask_u32(struct mlxsw_sp_acl_rule_info *rulei,
477 				    enum mlxsw_afk_element element,
478 				    u32 key_value, u32 mask_value)
479 {
480 	mlxsw_afk_values_add_u32(&rulei->values, element,
481 				 key_value, mask_value);
482 }
483 
mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info * rulei,enum mlxsw_afk_element element,const char * key_value,const char * mask_value,unsigned int len)484 void mlxsw_sp_acl_rulei_keymask_buf(struct mlxsw_sp_acl_rule_info *rulei,
485 				    enum mlxsw_afk_element element,
486 				    const char *key_value,
487 				    const char *mask_value, unsigned int len)
488 {
489 	mlxsw_afk_values_add_buf(&rulei->values, element,
490 				 key_value, mask_value, len);
491 }
492 
mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info * rulei)493 int mlxsw_sp_acl_rulei_act_continue(struct mlxsw_sp_acl_rule_info *rulei)
494 {
495 	return mlxsw_afa_block_continue(rulei->act_block);
496 }
497 
mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info * rulei,u16 group_id)498 int mlxsw_sp_acl_rulei_act_jump(struct mlxsw_sp_acl_rule_info *rulei,
499 				u16 group_id)
500 {
501 	return mlxsw_afa_block_jump(rulei->act_block, group_id);
502 }
503 
mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info * rulei)504 int mlxsw_sp_acl_rulei_act_terminate(struct mlxsw_sp_acl_rule_info *rulei)
505 {
506 	return mlxsw_afa_block_terminate(rulei->act_block);
507 }
508 
mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info * rulei)509 int mlxsw_sp_acl_rulei_act_drop(struct mlxsw_sp_acl_rule_info *rulei)
510 {
511 	return mlxsw_afa_block_append_drop(rulei->act_block);
512 }
513 
mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info * rulei)514 int mlxsw_sp_acl_rulei_act_trap(struct mlxsw_sp_acl_rule_info *rulei)
515 {
516 	return mlxsw_afa_block_append_trap(rulei->act_block,
517 					   MLXSW_TRAP_ID_ACL0);
518 }
519 
mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule_info * rulei,struct net_device * out_dev,struct netlink_ext_ack * extack)520 int mlxsw_sp_acl_rulei_act_fwd(struct mlxsw_sp *mlxsw_sp,
521 			       struct mlxsw_sp_acl_rule_info *rulei,
522 			       struct net_device *out_dev,
523 			       struct netlink_ext_ack *extack)
524 {
525 	struct mlxsw_sp_port *mlxsw_sp_port;
526 	u8 local_port;
527 	bool in_port;
528 
529 	if (out_dev) {
530 		if (!mlxsw_sp_port_dev_check(out_dev)) {
531 			NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
532 			return -EINVAL;
533 		}
534 		mlxsw_sp_port = netdev_priv(out_dev);
535 		if (mlxsw_sp_port->mlxsw_sp != mlxsw_sp) {
536 			NL_SET_ERR_MSG_MOD(extack, "Invalid output device");
537 			return -EINVAL;
538 		}
539 		local_port = mlxsw_sp_port->local_port;
540 		in_port = false;
541 	} else {
542 		/* If out_dev is NULL, the caller wants to
543 		 * set forward to ingress port.
544 		 */
545 		local_port = 0;
546 		in_port = true;
547 	}
548 	return mlxsw_afa_block_append_fwd(rulei->act_block,
549 					  local_port, in_port, extack);
550 }
551 
mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule_info * rulei,struct mlxsw_sp_acl_block * block,struct net_device * out_dev,struct netlink_ext_ack * extack)552 int mlxsw_sp_acl_rulei_act_mirror(struct mlxsw_sp *mlxsw_sp,
553 				  struct mlxsw_sp_acl_rule_info *rulei,
554 				  struct mlxsw_sp_acl_block *block,
555 				  struct net_device *out_dev,
556 				  struct netlink_ext_ack *extack)
557 {
558 	struct mlxsw_sp_acl_block_binding *binding;
559 	struct mlxsw_sp_port *in_port;
560 
561 	if (!list_is_singular(&block->binding_list)) {
562 		NL_SET_ERR_MSG_MOD(extack, "Only a single mirror source is allowed");
563 		return -EOPNOTSUPP;
564 	}
565 	binding = list_first_entry(&block->binding_list,
566 				   struct mlxsw_sp_acl_block_binding, list);
567 	in_port = binding->mlxsw_sp_port;
568 
569 	return mlxsw_afa_block_append_mirror(rulei->act_block,
570 					     in_port->local_port,
571 					     out_dev,
572 					     binding->ingress,
573 					     extack);
574 }
575 
mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule_info * rulei,u32 action,u16 vid,u16 proto,u8 prio,struct netlink_ext_ack * extack)576 int mlxsw_sp_acl_rulei_act_vlan(struct mlxsw_sp *mlxsw_sp,
577 				struct mlxsw_sp_acl_rule_info *rulei,
578 				u32 action, u16 vid, u16 proto, u8 prio,
579 				struct netlink_ext_ack *extack)
580 {
581 	u8 ethertype;
582 
583 	if (action == TCA_VLAN_ACT_MODIFY) {
584 		switch (proto) {
585 		case ETH_P_8021Q:
586 			ethertype = 0;
587 			break;
588 		case ETH_P_8021AD:
589 			ethertype = 1;
590 			break;
591 		default:
592 			NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN protocol");
593 			dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN protocol %#04x\n",
594 				proto);
595 			return -EINVAL;
596 		}
597 
598 		return mlxsw_afa_block_append_vlan_modify(rulei->act_block,
599 							  vid, prio, ethertype,
600 							  extack);
601 	} else {
602 		NL_SET_ERR_MSG_MOD(extack, "Unsupported VLAN action");
603 		dev_err(mlxsw_sp->bus_info->dev, "Unsupported VLAN action\n");
604 		return -EINVAL;
605 	}
606 }
607 
mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule_info * rulei,struct netlink_ext_ack * extack)608 int mlxsw_sp_acl_rulei_act_count(struct mlxsw_sp *mlxsw_sp,
609 				 struct mlxsw_sp_acl_rule_info *rulei,
610 				 struct netlink_ext_ack *extack)
611 {
612 	return mlxsw_afa_block_append_counter(rulei->act_block,
613 					      &rulei->counter_index, extack);
614 }
615 
mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule_info * rulei,u16 fid,struct netlink_ext_ack * extack)616 int mlxsw_sp_acl_rulei_act_fid_set(struct mlxsw_sp *mlxsw_sp,
617 				   struct mlxsw_sp_acl_rule_info *rulei,
618 				   u16 fid, struct netlink_ext_ack *extack)
619 {
620 	return mlxsw_afa_block_append_fid_set(rulei->act_block, fid, extack);
621 }
622 
623 struct mlxsw_sp_acl_rule *
mlxsw_sp_acl_rule_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ruleset * ruleset,unsigned long cookie,struct netlink_ext_ack * extack)624 mlxsw_sp_acl_rule_create(struct mlxsw_sp *mlxsw_sp,
625 			 struct mlxsw_sp_acl_ruleset *ruleset,
626 			 unsigned long cookie,
627 			 struct netlink_ext_ack *extack)
628 {
629 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
630 	struct mlxsw_sp_acl_rule *rule;
631 	int err;
632 
633 	mlxsw_sp_acl_ruleset_ref_inc(ruleset);
634 	rule = kzalloc(sizeof(*rule) + ops->rule_priv_size(mlxsw_sp),
635 		       GFP_KERNEL);
636 	if (!rule) {
637 		err = -ENOMEM;
638 		goto err_alloc;
639 	}
640 	rule->cookie = cookie;
641 	rule->ruleset = ruleset;
642 
643 	rule->rulei = mlxsw_sp_acl_rulei_create(mlxsw_sp->acl);
644 	if (IS_ERR(rule->rulei)) {
645 		err = PTR_ERR(rule->rulei);
646 		goto err_rulei_create;
647 	}
648 
649 	return rule;
650 
651 err_rulei_create:
652 	kfree(rule);
653 err_alloc:
654 	mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
655 	return ERR_PTR(err);
656 }
657 
mlxsw_sp_acl_rule_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule * rule)658 void mlxsw_sp_acl_rule_destroy(struct mlxsw_sp *mlxsw_sp,
659 			       struct mlxsw_sp_acl_rule *rule)
660 {
661 	struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
662 
663 	mlxsw_sp_acl_rulei_destroy(rule->rulei);
664 	kfree(rule);
665 	mlxsw_sp_acl_ruleset_ref_dec(mlxsw_sp, ruleset);
666 }
667 
mlxsw_sp_acl_rule_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule * rule)668 int mlxsw_sp_acl_rule_add(struct mlxsw_sp *mlxsw_sp,
669 			  struct mlxsw_sp_acl_rule *rule)
670 {
671 	struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
672 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
673 	int err;
674 
675 	err = ops->rule_add(mlxsw_sp, ruleset->priv, rule->priv, rule->rulei);
676 	if (err)
677 		return err;
678 
679 	err = rhashtable_insert_fast(&ruleset->rule_ht, &rule->ht_node,
680 				     mlxsw_sp_acl_rule_ht_params);
681 	if (err)
682 		goto err_rhashtable_insert;
683 
684 	if (!ruleset->ht_key.chain_index &&
685 	    mlxsw_sp_acl_ruleset_is_singular(ruleset)) {
686 		/* We only need ruleset with chain index 0, the implicit
687 		 * one, to be directly bound to device. The rest of the
688 		 * rulesets are bound by "Goto action set".
689 		 */
690 		err = mlxsw_sp_acl_ruleset_block_bind(mlxsw_sp, ruleset,
691 						      ruleset->ht_key.block);
692 		if (err)
693 			goto err_ruleset_block_bind;
694 	}
695 
696 	list_add_tail(&rule->list, &mlxsw_sp->acl->rules);
697 	ruleset->ht_key.block->rule_count++;
698 	return 0;
699 
700 err_ruleset_block_bind:
701 	rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
702 			       mlxsw_sp_acl_rule_ht_params);
703 err_rhashtable_insert:
704 	ops->rule_del(mlxsw_sp, rule->priv);
705 	return err;
706 }
707 
mlxsw_sp_acl_rule_del(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule * rule)708 void mlxsw_sp_acl_rule_del(struct mlxsw_sp *mlxsw_sp,
709 			   struct mlxsw_sp_acl_rule *rule)
710 {
711 	struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
712 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
713 
714 	ruleset->ht_key.block->rule_count--;
715 	list_del(&rule->list);
716 	if (!ruleset->ht_key.chain_index &&
717 	    mlxsw_sp_acl_ruleset_is_singular(ruleset))
718 		mlxsw_sp_acl_ruleset_block_unbind(mlxsw_sp, ruleset,
719 						  ruleset->ht_key.block);
720 	rhashtable_remove_fast(&ruleset->rule_ht, &rule->ht_node,
721 			       mlxsw_sp_acl_rule_ht_params);
722 	ops->rule_del(mlxsw_sp, rule->priv);
723 }
724 
725 struct mlxsw_sp_acl_rule *
mlxsw_sp_acl_rule_lookup(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_ruleset * ruleset,unsigned long cookie)726 mlxsw_sp_acl_rule_lookup(struct mlxsw_sp *mlxsw_sp,
727 			 struct mlxsw_sp_acl_ruleset *ruleset,
728 			 unsigned long cookie)
729 {
730 	return rhashtable_lookup_fast(&ruleset->rule_ht, &cookie,
731 				       mlxsw_sp_acl_rule_ht_params);
732 }
733 
734 struct mlxsw_sp_acl_rule_info *
mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule * rule)735 mlxsw_sp_acl_rule_rulei(struct mlxsw_sp_acl_rule *rule)
736 {
737 	return rule->rulei;
738 }
739 
mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule * rule)740 static int mlxsw_sp_acl_rule_activity_update(struct mlxsw_sp *mlxsw_sp,
741 					     struct mlxsw_sp_acl_rule *rule)
742 {
743 	struct mlxsw_sp_acl_ruleset *ruleset = rule->ruleset;
744 	const struct mlxsw_sp_acl_profile_ops *ops = ruleset->ht_key.ops;
745 	bool active;
746 	int err;
747 
748 	err = ops->rule_activity_get(mlxsw_sp, rule->priv, &active);
749 	if (err)
750 		return err;
751 	if (active)
752 		rule->last_used = jiffies;
753 	return 0;
754 }
755 
mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl * acl)756 static int mlxsw_sp_acl_rules_activity_update(struct mlxsw_sp_acl *acl)
757 {
758 	struct mlxsw_sp_acl_rule *rule;
759 	int err;
760 
761 	/* Protect internal structures from changes */
762 	rtnl_lock();
763 	list_for_each_entry(rule, &acl->rules, list) {
764 		err = mlxsw_sp_acl_rule_activity_update(acl->mlxsw_sp,
765 							rule);
766 		if (err)
767 			goto err_rule_update;
768 	}
769 	rtnl_unlock();
770 	return 0;
771 
772 err_rule_update:
773 	rtnl_unlock();
774 	return err;
775 }
776 
mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl * acl)777 static void mlxsw_sp_acl_rule_activity_work_schedule(struct mlxsw_sp_acl *acl)
778 {
779 	unsigned long interval = acl->rule_activity_update.interval;
780 
781 	mlxsw_core_schedule_dw(&acl->rule_activity_update.dw,
782 			       msecs_to_jiffies(interval));
783 }
784 
mlxsw_sp_acl_rul_activity_update_work(struct work_struct * work)785 static void mlxsw_sp_acl_rul_activity_update_work(struct work_struct *work)
786 {
787 	struct mlxsw_sp_acl *acl = container_of(work, struct mlxsw_sp_acl,
788 						rule_activity_update.dw.work);
789 	int err;
790 
791 	err = mlxsw_sp_acl_rules_activity_update(acl);
792 	if (err)
793 		dev_err(acl->mlxsw_sp->bus_info->dev, "Could not update acl activity");
794 
795 	mlxsw_sp_acl_rule_activity_work_schedule(acl);
796 }
797 
mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_acl_rule * rule,u64 * packets,u64 * bytes,u64 * last_use)798 int mlxsw_sp_acl_rule_get_stats(struct mlxsw_sp *mlxsw_sp,
799 				struct mlxsw_sp_acl_rule *rule,
800 				u64 *packets, u64 *bytes, u64 *last_use)
801 
802 {
803 	struct mlxsw_sp_acl_rule_info *rulei;
804 	u64 current_packets;
805 	u64 current_bytes;
806 	int err;
807 
808 	rulei = mlxsw_sp_acl_rule_rulei(rule);
809 	err = mlxsw_sp_flow_counter_get(mlxsw_sp, rulei->counter_index,
810 					&current_packets, &current_bytes);
811 	if (err)
812 		return err;
813 
814 	*packets = current_packets - rule->last_packets;
815 	*bytes = current_bytes - rule->last_bytes;
816 	*last_use = rule->last_used;
817 
818 	rule->last_bytes = current_bytes;
819 	rule->last_packets = current_packets;
820 
821 	return 0;
822 }
823 
mlxsw_sp_acl_init(struct mlxsw_sp * mlxsw_sp)824 int mlxsw_sp_acl_init(struct mlxsw_sp *mlxsw_sp)
825 {
826 	struct mlxsw_sp_fid *fid;
827 	struct mlxsw_sp_acl *acl;
828 	size_t alloc_size;
829 	int err;
830 
831 	alloc_size = sizeof(*acl) + mlxsw_sp_acl_tcam_priv_size(mlxsw_sp);
832 	acl = kzalloc(alloc_size, GFP_KERNEL);
833 	if (!acl)
834 		return -ENOMEM;
835 	mlxsw_sp->acl = acl;
836 	acl->mlxsw_sp = mlxsw_sp;
837 	acl->afk = mlxsw_afk_create(MLXSW_CORE_RES_GET(mlxsw_sp->core,
838 						       ACL_FLEX_KEYS),
839 				    mlxsw_sp->afk_ops);
840 	if (!acl->afk) {
841 		err = -ENOMEM;
842 		goto err_afk_create;
843 	}
844 
845 	err = rhashtable_init(&acl->ruleset_ht,
846 			      &mlxsw_sp_acl_ruleset_ht_params);
847 	if (err)
848 		goto err_rhashtable_init;
849 
850 	fid = mlxsw_sp_fid_dummy_get(mlxsw_sp);
851 	if (IS_ERR(fid)) {
852 		err = PTR_ERR(fid);
853 		goto err_fid_get;
854 	}
855 	acl->dummy_fid = fid;
856 
857 	INIT_LIST_HEAD(&acl->rules);
858 	err = mlxsw_sp_acl_tcam_init(mlxsw_sp, &acl->tcam);
859 	if (err)
860 		goto err_acl_ops_init;
861 
862 	/* Create the delayed work for the rule activity_update */
863 	INIT_DELAYED_WORK(&acl->rule_activity_update.dw,
864 			  mlxsw_sp_acl_rul_activity_update_work);
865 	acl->rule_activity_update.interval = MLXSW_SP_ACL_RULE_ACTIVITY_UPDATE_PERIOD_MS;
866 	mlxsw_core_schedule_dw(&acl->rule_activity_update.dw, 0);
867 	return 0;
868 
869 err_acl_ops_init:
870 	mlxsw_sp_fid_put(fid);
871 err_fid_get:
872 	rhashtable_destroy(&acl->ruleset_ht);
873 err_rhashtable_init:
874 	mlxsw_afk_destroy(acl->afk);
875 err_afk_create:
876 	kfree(acl);
877 	return err;
878 }
879 
mlxsw_sp_acl_fini(struct mlxsw_sp * mlxsw_sp)880 void mlxsw_sp_acl_fini(struct mlxsw_sp *mlxsw_sp)
881 {
882 	struct mlxsw_sp_acl *acl = mlxsw_sp->acl;
883 
884 	cancel_delayed_work_sync(&mlxsw_sp->acl->rule_activity_update.dw);
885 	mlxsw_sp_acl_tcam_fini(mlxsw_sp, &acl->tcam);
886 	WARN_ON(!list_empty(&acl->rules));
887 	mlxsw_sp_fid_put(acl->dummy_fid);
888 	rhashtable_destroy(&acl->ruleset_ht);
889 	mlxsw_afk_destroy(acl->afk);
890 	kfree(acl);
891 }
892