xref: /linux/drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.c (revision f2161d5f1aae21a42b0a64d87e10cb31db423f42)
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/mutex.h>
5 #include <linux/rhashtable.h>
6 #include <net/ipv6.h>
7 
8 #include "spectrum_mr.h"
9 #include "spectrum_router.h"
10 
11 struct mlxsw_sp_mr {
12 	const struct mlxsw_sp_mr_ops *mr_ops;
13 	void *catchall_route_priv;
14 	struct delayed_work stats_update_dw;
15 	struct list_head table_list;
16 	struct mutex table_list_lock; /* Protects table_list */
17 #define MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL 5000 /* ms */
18 	unsigned long priv[];
19 	/* priv has to be always the last item */
20 };
21 
22 struct mlxsw_sp_mr_vif;
23 struct mlxsw_sp_mr_vif_ops {
24 	bool (*is_regular)(const struct mlxsw_sp_mr_vif *vif);
25 };
26 
27 struct mlxsw_sp_mr_vif {
28 	struct net_device *dev;
29 	const struct mlxsw_sp_rif *rif;
30 	unsigned long vif_flags;
31 
32 	/* A list of route_vif_entry structs that point to routes that the VIF
33 	 * instance is used as one of the egress VIFs
34 	 */
35 	struct list_head route_evif_list;
36 
37 	/* A list of route_vif_entry structs that point to routes that the VIF
38 	 * instance is used as an ingress VIF
39 	 */
40 	struct list_head route_ivif_list;
41 
42 	/* Protocol specific operations for a VIF */
43 	const struct mlxsw_sp_mr_vif_ops *ops;
44 };
45 
46 struct mlxsw_sp_mr_route_vif_entry {
47 	struct list_head vif_node;
48 	struct list_head route_node;
49 	struct mlxsw_sp_mr_vif *mr_vif;
50 	struct mlxsw_sp_mr_route *mr_route;
51 };
52 
53 struct mlxsw_sp_mr_table;
54 struct mlxsw_sp_mr_table_ops {
55 	bool (*is_route_valid)(const struct mlxsw_sp_mr_table *mr_table,
56 			       const struct mr_mfc *mfc);
57 	void (*key_create)(struct mlxsw_sp_mr_table *mr_table,
58 			   struct mlxsw_sp_mr_route_key *key,
59 			   struct mr_mfc *mfc);
60 	bool (*is_route_starg)(const struct mlxsw_sp_mr_table *mr_table,
61 			       const struct mlxsw_sp_mr_route *mr_route);
62 };
63 
64 struct mlxsw_sp_mr_table {
65 	struct list_head node;
66 	enum mlxsw_sp_l3proto proto;
67 	struct mlxsw_sp *mlxsw_sp;
68 	u32 vr_id;
69 	struct mlxsw_sp_mr_vif vifs[MAXVIFS];
70 	struct list_head route_list;
71 	struct mutex route_list_lock; /* Protects route_list */
72 	struct rhashtable route_ht;
73 	const struct mlxsw_sp_mr_table_ops *ops;
74 	char catchall_route_priv[];
75 	/* catchall_route_priv has to be always the last item */
76 };
77 
78 struct mlxsw_sp_mr_route {
79 	struct list_head node;
80 	struct rhash_head ht_node;
81 	struct mlxsw_sp_mr_route_key key;
82 	enum mlxsw_sp_mr_route_action route_action;
83 	u16 min_mtu;
84 	struct mr_mfc *mfc;
85 	void *route_priv;
86 	const struct mlxsw_sp_mr_table *mr_table;
87 	/* A list of route_vif_entry structs that point to the egress VIFs */
88 	struct list_head evif_list;
89 	/* A route_vif_entry struct that point to the ingress VIF */
90 	struct mlxsw_sp_mr_route_vif_entry ivif;
91 };
92 
93 static const struct rhashtable_params mlxsw_sp_mr_route_ht_params = {
94 	.key_len = sizeof(struct mlxsw_sp_mr_route_key),
95 	.key_offset = offsetof(struct mlxsw_sp_mr_route, key),
96 	.head_offset = offsetof(struct mlxsw_sp_mr_route, ht_node),
97 	.automatic_shrinking = true,
98 };
99 
mlxsw_sp_mr_vif_valid(const struct mlxsw_sp_mr_vif * vif)100 static bool mlxsw_sp_mr_vif_valid(const struct mlxsw_sp_mr_vif *vif)
101 {
102 	return vif->ops->is_regular(vif) && vif->dev && vif->rif;
103 }
104 
mlxsw_sp_mr_vif_exists(const struct mlxsw_sp_mr_vif * vif)105 static bool mlxsw_sp_mr_vif_exists(const struct mlxsw_sp_mr_vif *vif)
106 {
107 	return vif->dev;
108 }
109 
110 static bool
mlxsw_sp_mr_route_ivif_in_evifs(const struct mlxsw_sp_mr_route * mr_route)111 mlxsw_sp_mr_route_ivif_in_evifs(const struct mlxsw_sp_mr_route *mr_route)
112 {
113 	vifi_t ivif = mr_route->mfc->mfc_parent;
114 
115 	return mr_route->mfc->mfc_un.res.ttls[ivif] != 255;
116 }
117 
118 static int
mlxsw_sp_mr_route_valid_evifs_num(const struct mlxsw_sp_mr_route * mr_route)119 mlxsw_sp_mr_route_valid_evifs_num(const struct mlxsw_sp_mr_route *mr_route)
120 {
121 	struct mlxsw_sp_mr_route_vif_entry *rve;
122 	int valid_evifs;
123 
124 	valid_evifs = 0;
125 	list_for_each_entry(rve, &mr_route->evif_list, route_node)
126 		if (mlxsw_sp_mr_vif_valid(rve->mr_vif))
127 			valid_evifs++;
128 	return valid_evifs;
129 }
130 
131 static enum mlxsw_sp_mr_route_action
mlxsw_sp_mr_route_action(const struct mlxsw_sp_mr_route * mr_route)132 mlxsw_sp_mr_route_action(const struct mlxsw_sp_mr_route *mr_route)
133 {
134 	struct mlxsw_sp_mr_route_vif_entry *rve;
135 
136 	/* If the ingress port is not regular and resolved, trap the route */
137 	if (!mlxsw_sp_mr_vif_valid(mr_route->ivif.mr_vif))
138 		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
139 
140 	/* The kernel does not match a (*,G) route that the ingress interface is
141 	 * not one of the egress interfaces, so trap these kind of routes.
142 	 */
143 	if (mr_route->mr_table->ops->is_route_starg(mr_route->mr_table,
144 						    mr_route) &&
145 	    !mlxsw_sp_mr_route_ivif_in_evifs(mr_route))
146 		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
147 
148 	/* If the route has no valid eVIFs, trap it. */
149 	if (!mlxsw_sp_mr_route_valid_evifs_num(mr_route))
150 		return MLXSW_SP_MR_ROUTE_ACTION_TRAP;
151 
152 	/* If one of the eVIFs has no RIF, trap-and-forward the route as there
153 	 * is some more routing to do in software too.
154 	 */
155 	list_for_each_entry(rve, &mr_route->evif_list, route_node)
156 		if (mlxsw_sp_mr_vif_exists(rve->mr_vif) && !rve->mr_vif->rif)
157 			return MLXSW_SP_MR_ROUTE_ACTION_TRAP_AND_FORWARD;
158 
159 	return MLXSW_SP_MR_ROUTE_ACTION_FORWARD;
160 }
161 
162 static enum mlxsw_sp_mr_route_prio
mlxsw_sp_mr_route_prio(const struct mlxsw_sp_mr_route * mr_route)163 mlxsw_sp_mr_route_prio(const struct mlxsw_sp_mr_route *mr_route)
164 {
165 	return mr_route->mr_table->ops->is_route_starg(mr_route->mr_table,
166 						       mr_route) ?
167 		MLXSW_SP_MR_ROUTE_PRIO_STARG : MLXSW_SP_MR_ROUTE_PRIO_SG;
168 }
169 
mlxsw_sp_mr_route_evif_link(struct mlxsw_sp_mr_route * mr_route,struct mlxsw_sp_mr_vif * mr_vif)170 static int mlxsw_sp_mr_route_evif_link(struct mlxsw_sp_mr_route *mr_route,
171 				       struct mlxsw_sp_mr_vif *mr_vif)
172 {
173 	struct mlxsw_sp_mr_route_vif_entry *rve;
174 
175 	rve = kzalloc(sizeof(*rve), GFP_KERNEL);
176 	if (!rve)
177 		return -ENOMEM;
178 	rve->mr_route = mr_route;
179 	rve->mr_vif = mr_vif;
180 	list_add_tail(&rve->route_node, &mr_route->evif_list);
181 	list_add_tail(&rve->vif_node, &mr_vif->route_evif_list);
182 	return 0;
183 }
184 
185 static void
mlxsw_sp_mr_route_evif_unlink(struct mlxsw_sp_mr_route_vif_entry * rve)186 mlxsw_sp_mr_route_evif_unlink(struct mlxsw_sp_mr_route_vif_entry *rve)
187 {
188 	list_del(&rve->route_node);
189 	list_del(&rve->vif_node);
190 	kfree(rve);
191 }
192 
mlxsw_sp_mr_route_ivif_link(struct mlxsw_sp_mr_route * mr_route,struct mlxsw_sp_mr_vif * mr_vif)193 static void mlxsw_sp_mr_route_ivif_link(struct mlxsw_sp_mr_route *mr_route,
194 					struct mlxsw_sp_mr_vif *mr_vif)
195 {
196 	mr_route->ivif.mr_route = mr_route;
197 	mr_route->ivif.mr_vif = mr_vif;
198 	list_add_tail(&mr_route->ivif.vif_node, &mr_vif->route_ivif_list);
199 }
200 
mlxsw_sp_mr_route_ivif_unlink(struct mlxsw_sp_mr_route * mr_route)201 static void mlxsw_sp_mr_route_ivif_unlink(struct mlxsw_sp_mr_route *mr_route)
202 {
203 	list_del(&mr_route->ivif.vif_node);
204 }
205 
206 static int
mlxsw_sp_mr_route_info_create(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route * mr_route,struct mlxsw_sp_mr_route_info * route_info)207 mlxsw_sp_mr_route_info_create(struct mlxsw_sp_mr_table *mr_table,
208 			      struct mlxsw_sp_mr_route *mr_route,
209 			      struct mlxsw_sp_mr_route_info *route_info)
210 {
211 	struct mlxsw_sp_mr_route_vif_entry *rve;
212 	u16 *erif_indices;
213 	u16 irif_index;
214 	u16 erif = 0;
215 
216 	erif_indices = kmalloc_array(MAXVIFS, sizeof(*erif_indices),
217 				     GFP_KERNEL);
218 	if (!erif_indices)
219 		return -ENOMEM;
220 
221 	list_for_each_entry(rve, &mr_route->evif_list, route_node) {
222 		if (mlxsw_sp_mr_vif_valid(rve->mr_vif)) {
223 			u16 rifi = mlxsw_sp_rif_index(rve->mr_vif->rif);
224 
225 			erif_indices[erif++] = rifi;
226 		}
227 	}
228 
229 	if (mlxsw_sp_mr_vif_valid(mr_route->ivif.mr_vif))
230 		irif_index = mlxsw_sp_rif_index(mr_route->ivif.mr_vif->rif);
231 	else
232 		irif_index = 0;
233 
234 	route_info->irif_index = irif_index;
235 	route_info->erif_indices = erif_indices;
236 	route_info->min_mtu = mr_route->min_mtu;
237 	route_info->route_action = mr_route->route_action;
238 	route_info->erif_num = erif;
239 	return 0;
240 }
241 
242 static void
mlxsw_sp_mr_route_info_destroy(struct mlxsw_sp_mr_route_info * route_info)243 mlxsw_sp_mr_route_info_destroy(struct mlxsw_sp_mr_route_info *route_info)
244 {
245 	kfree(route_info->erif_indices);
246 }
247 
mlxsw_sp_mr_route_write(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route * mr_route,bool replace)248 static int mlxsw_sp_mr_route_write(struct mlxsw_sp_mr_table *mr_table,
249 				   struct mlxsw_sp_mr_route *mr_route,
250 				   bool replace)
251 {
252 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
253 	struct mlxsw_sp_mr_route_info route_info;
254 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
255 	int err;
256 
257 	err = mlxsw_sp_mr_route_info_create(mr_table, mr_route, &route_info);
258 	if (err)
259 		return err;
260 
261 	if (!replace) {
262 		struct mlxsw_sp_mr_route_params route_params;
263 
264 		mr_route->route_priv = kzalloc(mr->mr_ops->route_priv_size,
265 					       GFP_KERNEL);
266 		if (!mr_route->route_priv) {
267 			err = -ENOMEM;
268 			goto out;
269 		}
270 
271 		route_params.key = mr_route->key;
272 		route_params.value = route_info;
273 		route_params.prio = mlxsw_sp_mr_route_prio(mr_route);
274 		err = mr->mr_ops->route_create(mlxsw_sp, mr->priv,
275 					       mr_route->route_priv,
276 					       &route_params);
277 		if (err)
278 			kfree(mr_route->route_priv);
279 	} else {
280 		err = mr->mr_ops->route_update(mlxsw_sp, mr_route->route_priv,
281 					       &route_info);
282 	}
283 out:
284 	mlxsw_sp_mr_route_info_destroy(&route_info);
285 	return err;
286 }
287 
mlxsw_sp_mr_route_erase(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route * mr_route)288 static void mlxsw_sp_mr_route_erase(struct mlxsw_sp_mr_table *mr_table,
289 				    struct mlxsw_sp_mr_route *mr_route)
290 {
291 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
292 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
293 
294 	mr->mr_ops->route_destroy(mlxsw_sp, mr->priv, mr_route->route_priv);
295 	kfree(mr_route->route_priv);
296 }
297 
298 static struct mlxsw_sp_mr_route *
mlxsw_sp_mr_route_create(struct mlxsw_sp_mr_table * mr_table,struct mr_mfc * mfc)299 mlxsw_sp_mr_route_create(struct mlxsw_sp_mr_table *mr_table,
300 			 struct mr_mfc *mfc)
301 {
302 	struct mlxsw_sp_mr_route_vif_entry *rve, *tmp;
303 	struct mlxsw_sp_mr_route *mr_route;
304 	int err = 0;
305 	int i;
306 
307 	/* Allocate and init a new route and fill it with parameters */
308 	mr_route = kzalloc(sizeof(*mr_route), GFP_KERNEL);
309 	if (!mr_route)
310 		return ERR_PTR(-ENOMEM);
311 	INIT_LIST_HEAD(&mr_route->evif_list);
312 
313 	/* Find min_mtu and link iVIF and eVIFs */
314 	mr_route->min_mtu = ETH_MAX_MTU;
315 	mr_cache_hold(mfc);
316 	mr_route->mfc = mfc;
317 	mr_table->ops->key_create(mr_table, &mr_route->key, mr_route->mfc);
318 
319 	mr_route->mr_table = mr_table;
320 	for (i = 0; i < MAXVIFS; i++) {
321 		if (mfc->mfc_un.res.ttls[i] != 255) {
322 			err = mlxsw_sp_mr_route_evif_link(mr_route,
323 							  &mr_table->vifs[i]);
324 			if (err)
325 				goto err;
326 			if (mr_table->vifs[i].dev &&
327 			    mr_table->vifs[i].dev->mtu < mr_route->min_mtu)
328 				mr_route->min_mtu = mr_table->vifs[i].dev->mtu;
329 		}
330 	}
331 	mlxsw_sp_mr_route_ivif_link(mr_route,
332 				    &mr_table->vifs[mfc->mfc_parent]);
333 
334 	mr_route->route_action = mlxsw_sp_mr_route_action(mr_route);
335 	return mr_route;
336 err:
337 	mr_cache_put(mfc);
338 	list_for_each_entry_safe(rve, tmp, &mr_route->evif_list, route_node)
339 		mlxsw_sp_mr_route_evif_unlink(rve);
340 	kfree(mr_route);
341 	return ERR_PTR(err);
342 }
343 
mlxsw_sp_mr_route_destroy(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route * mr_route)344 static void mlxsw_sp_mr_route_destroy(struct mlxsw_sp_mr_table *mr_table,
345 				      struct mlxsw_sp_mr_route *mr_route)
346 {
347 	struct mlxsw_sp_mr_route_vif_entry *rve, *tmp;
348 
349 	mlxsw_sp_mr_route_ivif_unlink(mr_route);
350 	mr_cache_put(mr_route->mfc);
351 	list_for_each_entry_safe(rve, tmp, &mr_route->evif_list, route_node)
352 		mlxsw_sp_mr_route_evif_unlink(rve);
353 	kfree(mr_route);
354 }
355 
mlxsw_sp_mr_mfc_offload_set(struct mlxsw_sp_mr_route * mr_route,bool offload)356 static void mlxsw_sp_mr_mfc_offload_set(struct mlxsw_sp_mr_route *mr_route,
357 					bool offload)
358 {
359 	if (offload)
360 		mr_route->mfc->mfc_flags |= MFC_OFFLOAD;
361 	else
362 		mr_route->mfc->mfc_flags &= ~MFC_OFFLOAD;
363 }
364 
mlxsw_sp_mr_mfc_offload_update(struct mlxsw_sp_mr_route * mr_route)365 static void mlxsw_sp_mr_mfc_offload_update(struct mlxsw_sp_mr_route *mr_route)
366 {
367 	bool offload;
368 
369 	offload = mr_route->route_action != MLXSW_SP_MR_ROUTE_ACTION_TRAP;
370 	mlxsw_sp_mr_mfc_offload_set(mr_route, offload);
371 }
372 
__mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route * mr_route)373 static void __mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table *mr_table,
374 				    struct mlxsw_sp_mr_route *mr_route)
375 {
376 	WARN_ON_ONCE(!mutex_is_locked(&mr_table->route_list_lock));
377 
378 	mlxsw_sp_mr_mfc_offload_set(mr_route, false);
379 	rhashtable_remove_fast(&mr_table->route_ht, &mr_route->ht_node,
380 			       mlxsw_sp_mr_route_ht_params);
381 	list_del(&mr_route->node);
382 	mlxsw_sp_mr_route_erase(mr_table, mr_route);
383 	mlxsw_sp_mr_route_destroy(mr_table, mr_route);
384 }
385 
mlxsw_sp_mr_route_add(struct mlxsw_sp_mr_table * mr_table,struct mr_mfc * mfc,bool replace)386 int mlxsw_sp_mr_route_add(struct mlxsw_sp_mr_table *mr_table,
387 			  struct mr_mfc *mfc, bool replace)
388 {
389 	struct mlxsw_sp_mr_route *mr_orig_route = NULL;
390 	struct mlxsw_sp_mr_route *mr_route;
391 	int err;
392 
393 	if (!mr_table->ops->is_route_valid(mr_table, mfc))
394 		return -EINVAL;
395 
396 	/* Create a new route */
397 	mr_route = mlxsw_sp_mr_route_create(mr_table, mfc);
398 	if (IS_ERR(mr_route))
399 		return PTR_ERR(mr_route);
400 
401 	/* Find any route with a matching key */
402 	mr_orig_route = rhashtable_lookup_fast(&mr_table->route_ht,
403 					       &mr_route->key,
404 					       mlxsw_sp_mr_route_ht_params);
405 	if (replace) {
406 		/* On replace case, make the route point to the new route_priv.
407 		 */
408 		if (WARN_ON(!mr_orig_route)) {
409 			err = -ENOENT;
410 			goto err_no_orig_route;
411 		}
412 		mr_route->route_priv = mr_orig_route->route_priv;
413 	} else if (mr_orig_route) {
414 		/* On non replace case, if another route with the same key was
415 		 * found, abort, as duplicate routes are used for proxy routes.
416 		 */
417 		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
418 			 "Offloading proxy routes is not supported.\n");
419 		err = -EINVAL;
420 		goto err_duplicate_route;
421 	}
422 
423 	/* Write the route to the hardware */
424 	err = mlxsw_sp_mr_route_write(mr_table, mr_route, replace);
425 	if (err)
426 		goto err_mr_route_write;
427 
428 	/* Put it in the table data-structures */
429 	mutex_lock(&mr_table->route_list_lock);
430 	list_add_tail(&mr_route->node, &mr_table->route_list);
431 	mutex_unlock(&mr_table->route_list_lock);
432 	err = rhashtable_insert_fast(&mr_table->route_ht,
433 				     &mr_route->ht_node,
434 				     mlxsw_sp_mr_route_ht_params);
435 	if (err)
436 		goto err_rhashtable_insert;
437 
438 	/* Destroy the original route */
439 	if (replace) {
440 		rhashtable_remove_fast(&mr_table->route_ht,
441 				       &mr_orig_route->ht_node,
442 				       mlxsw_sp_mr_route_ht_params);
443 		mutex_lock(&mr_table->route_list_lock);
444 		list_del(&mr_orig_route->node);
445 		mutex_unlock(&mr_table->route_list_lock);
446 		mlxsw_sp_mr_route_destroy(mr_table, mr_orig_route);
447 	}
448 
449 	mlxsw_sp_mr_mfc_offload_update(mr_route);
450 	return 0;
451 
452 err_rhashtable_insert:
453 	mutex_lock(&mr_table->route_list_lock);
454 	list_del(&mr_route->node);
455 	mutex_unlock(&mr_table->route_list_lock);
456 	mlxsw_sp_mr_route_erase(mr_table, mr_route);
457 err_mr_route_write:
458 err_no_orig_route:
459 err_duplicate_route:
460 	mlxsw_sp_mr_route_destroy(mr_table, mr_route);
461 	return err;
462 }
463 
mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table * mr_table,struct mr_mfc * mfc)464 void mlxsw_sp_mr_route_del(struct mlxsw_sp_mr_table *mr_table,
465 			   struct mr_mfc *mfc)
466 {
467 	struct mlxsw_sp_mr_route *mr_route;
468 	struct mlxsw_sp_mr_route_key key;
469 
470 	mr_table->ops->key_create(mr_table, &key, mfc);
471 	mr_route = rhashtable_lookup_fast(&mr_table->route_ht, &key,
472 					  mlxsw_sp_mr_route_ht_params);
473 	if (mr_route) {
474 		mutex_lock(&mr_table->route_list_lock);
475 		__mlxsw_sp_mr_route_del(mr_table, mr_route);
476 		mutex_unlock(&mr_table->route_list_lock);
477 	}
478 }
479 
480 /* Should be called after the VIF struct is updated */
481 static int
mlxsw_sp_mr_route_ivif_resolve(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route_vif_entry * rve)482 mlxsw_sp_mr_route_ivif_resolve(struct mlxsw_sp_mr_table *mr_table,
483 			       struct mlxsw_sp_mr_route_vif_entry *rve)
484 {
485 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
486 	enum mlxsw_sp_mr_route_action route_action;
487 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
488 	u16 irif_index;
489 	int err;
490 
491 	route_action = mlxsw_sp_mr_route_action(rve->mr_route);
492 	if (route_action == MLXSW_SP_MR_ROUTE_ACTION_TRAP)
493 		return 0;
494 
495 	/* rve->mr_vif->rif is guaranteed to be valid at this stage */
496 	irif_index = mlxsw_sp_rif_index(rve->mr_vif->rif);
497 	err = mr->mr_ops->route_irif_update(mlxsw_sp, rve->mr_route->route_priv,
498 					    irif_index);
499 	if (err)
500 		return err;
501 
502 	err = mr->mr_ops->route_action_update(mlxsw_sp,
503 					      rve->mr_route->route_priv,
504 					      route_action);
505 	if (err)
506 		/* No need to rollback here because the iRIF change only takes
507 		 * place after the action has been updated.
508 		 */
509 		return err;
510 
511 	rve->mr_route->route_action = route_action;
512 	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
513 	return 0;
514 }
515 
516 static void
mlxsw_sp_mr_route_ivif_unresolve(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route_vif_entry * rve)517 mlxsw_sp_mr_route_ivif_unresolve(struct mlxsw_sp_mr_table *mr_table,
518 				 struct mlxsw_sp_mr_route_vif_entry *rve)
519 {
520 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
521 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
522 
523 	mr->mr_ops->route_action_update(mlxsw_sp, rve->mr_route->route_priv,
524 					MLXSW_SP_MR_ROUTE_ACTION_TRAP);
525 	rve->mr_route->route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP;
526 	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
527 }
528 
529 /* Should be called after the RIF struct is updated */
530 static int
mlxsw_sp_mr_route_evif_resolve(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route_vif_entry * rve)531 mlxsw_sp_mr_route_evif_resolve(struct mlxsw_sp_mr_table *mr_table,
532 			       struct mlxsw_sp_mr_route_vif_entry *rve)
533 {
534 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
535 	enum mlxsw_sp_mr_route_action route_action;
536 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
537 	u16 erif_index = 0;
538 	int err;
539 
540 	/* Add the eRIF */
541 	if (mlxsw_sp_mr_vif_valid(rve->mr_vif)) {
542 		erif_index = mlxsw_sp_rif_index(rve->mr_vif->rif);
543 		err = mr->mr_ops->route_erif_add(mlxsw_sp,
544 						 rve->mr_route->route_priv,
545 						 erif_index);
546 		if (err)
547 			return err;
548 	}
549 
550 	/* Update the route action, as the new eVIF can be a tunnel or a pimreg
551 	 * device which will require updating the action.
552 	 */
553 	route_action = mlxsw_sp_mr_route_action(rve->mr_route);
554 	if (route_action != rve->mr_route->route_action) {
555 		err = mr->mr_ops->route_action_update(mlxsw_sp,
556 						      rve->mr_route->route_priv,
557 						      route_action);
558 		if (err)
559 			goto err_route_action_update;
560 	}
561 
562 	/* Update the minimum MTU */
563 	if (rve->mr_vif->dev->mtu < rve->mr_route->min_mtu) {
564 		rve->mr_route->min_mtu = rve->mr_vif->dev->mtu;
565 		err = mr->mr_ops->route_min_mtu_update(mlxsw_sp,
566 						       rve->mr_route->route_priv,
567 						       rve->mr_route->min_mtu);
568 		if (err)
569 			goto err_route_min_mtu_update;
570 	}
571 
572 	rve->mr_route->route_action = route_action;
573 	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
574 	return 0;
575 
576 err_route_min_mtu_update:
577 	if (route_action != rve->mr_route->route_action)
578 		mr->mr_ops->route_action_update(mlxsw_sp,
579 						rve->mr_route->route_priv,
580 						rve->mr_route->route_action);
581 err_route_action_update:
582 	if (mlxsw_sp_mr_vif_valid(rve->mr_vif))
583 		mr->mr_ops->route_erif_del(mlxsw_sp, rve->mr_route->route_priv,
584 					   erif_index);
585 	return err;
586 }
587 
588 /* Should be called before the RIF struct is updated */
589 static void
mlxsw_sp_mr_route_evif_unresolve(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route_vif_entry * rve)590 mlxsw_sp_mr_route_evif_unresolve(struct mlxsw_sp_mr_table *mr_table,
591 				 struct mlxsw_sp_mr_route_vif_entry *rve)
592 {
593 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
594 	enum mlxsw_sp_mr_route_action route_action;
595 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
596 	u16 rifi;
597 
598 	/* If the unresolved RIF was not valid, no need to delete it */
599 	if (!mlxsw_sp_mr_vif_valid(rve->mr_vif))
600 		return;
601 
602 	/* Update the route action: if there is only one valid eVIF in the
603 	 * route, set the action to trap as the VIF deletion will lead to zero
604 	 * valid eVIFs. On any other case, use the mlxsw_sp_mr_route_action to
605 	 * determine the route action.
606 	 */
607 	if (mlxsw_sp_mr_route_valid_evifs_num(rve->mr_route) == 1)
608 		route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP;
609 	else
610 		route_action = mlxsw_sp_mr_route_action(rve->mr_route);
611 	if (route_action != rve->mr_route->route_action)
612 		mr->mr_ops->route_action_update(mlxsw_sp,
613 						rve->mr_route->route_priv,
614 						route_action);
615 
616 	/* Delete the erif from the route */
617 	rifi = mlxsw_sp_rif_index(rve->mr_vif->rif);
618 	mr->mr_ops->route_erif_del(mlxsw_sp, rve->mr_route->route_priv, rifi);
619 	rve->mr_route->route_action = route_action;
620 	mlxsw_sp_mr_mfc_offload_update(rve->mr_route);
621 }
622 
mlxsw_sp_mr_vif_resolve(struct mlxsw_sp_mr_table * mr_table,struct net_device * dev,struct mlxsw_sp_mr_vif * mr_vif,unsigned long vif_flags,const struct mlxsw_sp_rif * rif)623 static int mlxsw_sp_mr_vif_resolve(struct mlxsw_sp_mr_table *mr_table,
624 				   struct net_device *dev,
625 				   struct mlxsw_sp_mr_vif *mr_vif,
626 				   unsigned long vif_flags,
627 				   const struct mlxsw_sp_rif *rif)
628 {
629 	struct mlxsw_sp_mr_route_vif_entry *irve, *erve;
630 	int err;
631 
632 	/* Update the VIF */
633 	mr_vif->dev = dev;
634 	mr_vif->rif = rif;
635 	mr_vif->vif_flags = vif_flags;
636 
637 	/* Update all routes where this VIF is used as an unresolved iRIF */
638 	list_for_each_entry(irve, &mr_vif->route_ivif_list, vif_node) {
639 		err = mlxsw_sp_mr_route_ivif_resolve(mr_table, irve);
640 		if (err)
641 			goto err_irif_unresolve;
642 	}
643 
644 	/* Update all routes where this VIF is used as an unresolved eRIF */
645 	list_for_each_entry(erve, &mr_vif->route_evif_list, vif_node) {
646 		err = mlxsw_sp_mr_route_evif_resolve(mr_table, erve);
647 		if (err)
648 			goto err_erif_unresolve;
649 	}
650 	return 0;
651 
652 err_erif_unresolve:
653 	list_for_each_entry_continue_reverse(erve, &mr_vif->route_evif_list,
654 					     vif_node)
655 		mlxsw_sp_mr_route_evif_unresolve(mr_table, erve);
656 err_irif_unresolve:
657 	list_for_each_entry_continue_reverse(irve, &mr_vif->route_ivif_list,
658 					     vif_node)
659 		mlxsw_sp_mr_route_ivif_unresolve(mr_table, irve);
660 	mr_vif->rif = NULL;
661 	return err;
662 }
663 
mlxsw_sp_mr_vif_unresolve(struct mlxsw_sp_mr_table * mr_table,struct net_device * dev,struct mlxsw_sp_mr_vif * mr_vif)664 static void mlxsw_sp_mr_vif_unresolve(struct mlxsw_sp_mr_table *mr_table,
665 				      struct net_device *dev,
666 				      struct mlxsw_sp_mr_vif *mr_vif)
667 {
668 	struct mlxsw_sp_mr_route_vif_entry *rve;
669 
670 	/* Update all routes where this VIF is used as an unresolved eRIF */
671 	list_for_each_entry(rve, &mr_vif->route_evif_list, vif_node)
672 		mlxsw_sp_mr_route_evif_unresolve(mr_table, rve);
673 
674 	/* Update all routes where this VIF is used as an unresolved iRIF */
675 	list_for_each_entry(rve, &mr_vif->route_ivif_list, vif_node)
676 		mlxsw_sp_mr_route_ivif_unresolve(mr_table, rve);
677 
678 	/* Update the VIF */
679 	mr_vif->dev = dev;
680 	mr_vif->rif = NULL;
681 }
682 
mlxsw_sp_mr_vif_add(struct mlxsw_sp_mr_table * mr_table,struct net_device * dev,vifi_t vif_index,unsigned long vif_flags,const struct mlxsw_sp_rif * rif)683 int mlxsw_sp_mr_vif_add(struct mlxsw_sp_mr_table *mr_table,
684 			struct net_device *dev, vifi_t vif_index,
685 			unsigned long vif_flags, const struct mlxsw_sp_rif *rif)
686 {
687 	struct mlxsw_sp_mr_vif *mr_vif = &mr_table->vifs[vif_index];
688 
689 	if (WARN_ON(vif_index >= MAXVIFS))
690 		return -EINVAL;
691 	if (mr_vif->dev)
692 		return -EEXIST;
693 	return mlxsw_sp_mr_vif_resolve(mr_table, dev, mr_vif, vif_flags, rif);
694 }
695 
mlxsw_sp_mr_vif_del(struct mlxsw_sp_mr_table * mr_table,vifi_t vif_index)696 void mlxsw_sp_mr_vif_del(struct mlxsw_sp_mr_table *mr_table, vifi_t vif_index)
697 {
698 	struct mlxsw_sp_mr_vif *mr_vif = &mr_table->vifs[vif_index];
699 
700 	if (WARN_ON(vif_index >= MAXVIFS))
701 		return;
702 	if (WARN_ON(!mr_vif->dev))
703 		return;
704 	mlxsw_sp_mr_vif_unresolve(mr_table, NULL, mr_vif);
705 }
706 
707 static struct mlxsw_sp_mr_vif *
mlxsw_sp_mr_dev_vif_lookup(struct mlxsw_sp_mr_table * mr_table,const struct mlxsw_sp_rif * rif)708 mlxsw_sp_mr_dev_vif_lookup(struct mlxsw_sp_mr_table *mr_table,
709 			   const struct mlxsw_sp_rif *rif)
710 {
711 	vifi_t vif_index;
712 
713 	for (vif_index = 0; vif_index < MAXVIFS; vif_index++)
714 		if (mlxsw_sp_rif_dev_is(rif, mr_table->vifs[vif_index].dev))
715 			return &mr_table->vifs[vif_index];
716 	return NULL;
717 }
718 
mlxsw_sp_mr_rif_add(struct mlxsw_sp_mr_table * mr_table,const struct mlxsw_sp_rif * rif)719 int mlxsw_sp_mr_rif_add(struct mlxsw_sp_mr_table *mr_table,
720 			const struct mlxsw_sp_rif *rif)
721 {
722 	struct mlxsw_sp_mr_vif *mr_vif;
723 
724 	if (!mlxsw_sp_rif_has_dev(rif))
725 		return 0;
726 
727 	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif);
728 	if (!mr_vif)
729 		return 0;
730 	return mlxsw_sp_mr_vif_resolve(mr_table, mr_vif->dev, mr_vif,
731 				       mr_vif->vif_flags, rif);
732 }
733 
mlxsw_sp_mr_rif_del(struct mlxsw_sp_mr_table * mr_table,const struct mlxsw_sp_rif * rif)734 void mlxsw_sp_mr_rif_del(struct mlxsw_sp_mr_table *mr_table,
735 			 const struct mlxsw_sp_rif *rif)
736 {
737 	struct mlxsw_sp_mr_vif *mr_vif;
738 
739 	if (!mlxsw_sp_rif_has_dev(rif))
740 		return;
741 
742 	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif);
743 	if (!mr_vif)
744 		return;
745 	mlxsw_sp_mr_vif_unresolve(mr_table, mr_vif->dev, mr_vif);
746 }
747 
mlxsw_sp_mr_rif_mtu_update(struct mlxsw_sp_mr_table * mr_table,const struct mlxsw_sp_rif * rif,int mtu)748 void mlxsw_sp_mr_rif_mtu_update(struct mlxsw_sp_mr_table *mr_table,
749 				const struct mlxsw_sp_rif *rif, int mtu)
750 {
751 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
752 	struct mlxsw_sp_mr_route_vif_entry *rve;
753 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
754 	struct mlxsw_sp_mr_vif *mr_vif;
755 
756 	if (!mlxsw_sp_rif_has_dev(rif))
757 		return;
758 
759 	/* Search for a VIF that use that RIF */
760 	mr_vif = mlxsw_sp_mr_dev_vif_lookup(mr_table, rif);
761 	if (!mr_vif)
762 		return;
763 
764 	/* Update all the routes that uses that VIF as eVIF */
765 	list_for_each_entry(rve, &mr_vif->route_evif_list, vif_node) {
766 		if (mtu < rve->mr_route->min_mtu) {
767 			rve->mr_route->min_mtu = mtu;
768 			mr->mr_ops->route_min_mtu_update(mlxsw_sp,
769 							 rve->mr_route->route_priv,
770 							 mtu);
771 		}
772 	}
773 }
774 
775 /* Protocol specific functions */
776 static bool
mlxsw_sp_mr_route4_validate(const struct mlxsw_sp_mr_table * mr_table,const struct mr_mfc * c)777 mlxsw_sp_mr_route4_validate(const struct mlxsw_sp_mr_table *mr_table,
778 			    const struct mr_mfc *c)
779 {
780 	struct mfc_cache *mfc = (struct mfc_cache *) c;
781 
782 	/* If the route is a (*,*) route, abort, as these kind of routes are
783 	 * used for proxy routes.
784 	 */
785 	if (mfc->mfc_origin == htonl(INADDR_ANY) &&
786 	    mfc->mfc_mcastgrp == htonl(INADDR_ANY)) {
787 		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
788 			 "Offloading proxy routes is not supported.\n");
789 		return false;
790 	}
791 	return true;
792 }
793 
mlxsw_sp_mr_route4_key(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route_key * key,struct mr_mfc * c)794 static void mlxsw_sp_mr_route4_key(struct mlxsw_sp_mr_table *mr_table,
795 				   struct mlxsw_sp_mr_route_key *key,
796 				   struct mr_mfc *c)
797 {
798 	const struct mfc_cache *mfc = (struct mfc_cache *) c;
799 	bool starg;
800 
801 	starg = (mfc->mfc_origin == htonl(INADDR_ANY));
802 
803 	memset(key, 0, sizeof(*key));
804 	key->vrid = mr_table->vr_id;
805 	key->proto = MLXSW_SP_L3_PROTO_IPV4;
806 	key->group.addr4 = mfc->mfc_mcastgrp;
807 	key->group_mask.addr4 = htonl(0xffffffff);
808 	key->source.addr4 = mfc->mfc_origin;
809 	key->source_mask.addr4 = htonl(starg ? 0 : 0xffffffff);
810 }
811 
mlxsw_sp_mr_route4_starg(const struct mlxsw_sp_mr_table * mr_table,const struct mlxsw_sp_mr_route * mr_route)812 static bool mlxsw_sp_mr_route4_starg(const struct mlxsw_sp_mr_table *mr_table,
813 				     const struct mlxsw_sp_mr_route *mr_route)
814 {
815 	return mr_route->key.source_mask.addr4 == htonl(INADDR_ANY);
816 }
817 
mlxsw_sp_mr_vif4_is_regular(const struct mlxsw_sp_mr_vif * vif)818 static bool mlxsw_sp_mr_vif4_is_regular(const struct mlxsw_sp_mr_vif *vif)
819 {
820 	return !(vif->vif_flags & (VIFF_TUNNEL | VIFF_REGISTER));
821 }
822 
823 static bool
mlxsw_sp_mr_route6_validate(const struct mlxsw_sp_mr_table * mr_table,const struct mr_mfc * c)824 mlxsw_sp_mr_route6_validate(const struct mlxsw_sp_mr_table *mr_table,
825 			    const struct mr_mfc *c)
826 {
827 	struct mfc6_cache *mfc = (struct mfc6_cache *) c;
828 
829 	/* If the route is a (*,*) route, abort, as these kind of routes are
830 	 * used for proxy routes.
831 	 */
832 	if (ipv6_addr_any(&mfc->mf6c_origin) &&
833 	    ipv6_addr_any(&mfc->mf6c_mcastgrp)) {
834 		dev_warn(mr_table->mlxsw_sp->bus_info->dev,
835 			 "Offloading proxy routes is not supported.\n");
836 		return false;
837 	}
838 	return true;
839 }
840 
mlxsw_sp_mr_route6_key(struct mlxsw_sp_mr_table * mr_table,struct mlxsw_sp_mr_route_key * key,struct mr_mfc * c)841 static void mlxsw_sp_mr_route6_key(struct mlxsw_sp_mr_table *mr_table,
842 				   struct mlxsw_sp_mr_route_key *key,
843 				   struct mr_mfc *c)
844 {
845 	const struct mfc6_cache *mfc = (struct mfc6_cache *) c;
846 
847 	memset(key, 0, sizeof(*key));
848 	key->vrid = mr_table->vr_id;
849 	key->proto = MLXSW_SP_L3_PROTO_IPV6;
850 	key->group.addr6 = mfc->mf6c_mcastgrp;
851 	memset(&key->group_mask.addr6, 0xff, sizeof(key->group_mask.addr6));
852 	key->source.addr6 = mfc->mf6c_origin;
853 	if (!ipv6_addr_any(&mfc->mf6c_origin))
854 		memset(&key->source_mask.addr6, 0xff,
855 		       sizeof(key->source_mask.addr6));
856 }
857 
mlxsw_sp_mr_route6_starg(const struct mlxsw_sp_mr_table * mr_table,const struct mlxsw_sp_mr_route * mr_route)858 static bool mlxsw_sp_mr_route6_starg(const struct mlxsw_sp_mr_table *mr_table,
859 				     const struct mlxsw_sp_mr_route *mr_route)
860 {
861 	return ipv6_addr_any(&mr_route->key.source_mask.addr6);
862 }
863 
mlxsw_sp_mr_vif6_is_regular(const struct mlxsw_sp_mr_vif * vif)864 static bool mlxsw_sp_mr_vif6_is_regular(const struct mlxsw_sp_mr_vif *vif)
865 {
866 	return !(vif->vif_flags & MIFF_REGISTER);
867 }
868 
869 static struct
870 mlxsw_sp_mr_vif_ops mlxsw_sp_mr_vif_ops_arr[] = {
871 	{
872 		.is_regular = mlxsw_sp_mr_vif4_is_regular,
873 	},
874 	{
875 		.is_regular = mlxsw_sp_mr_vif6_is_regular,
876 	},
877 };
878 
879 static struct
880 mlxsw_sp_mr_table_ops mlxsw_sp_mr_table_ops_arr[] = {
881 	{
882 		.is_route_valid = mlxsw_sp_mr_route4_validate,
883 		.key_create = mlxsw_sp_mr_route4_key,
884 		.is_route_starg = mlxsw_sp_mr_route4_starg,
885 	},
886 	{
887 		.is_route_valid = mlxsw_sp_mr_route6_validate,
888 		.key_create = mlxsw_sp_mr_route6_key,
889 		.is_route_starg = mlxsw_sp_mr_route6_starg,
890 	},
891 
892 };
893 
mlxsw_sp_mr_table_create(struct mlxsw_sp * mlxsw_sp,u32 vr_id,enum mlxsw_sp_l3proto proto)894 struct mlxsw_sp_mr_table *mlxsw_sp_mr_table_create(struct mlxsw_sp *mlxsw_sp,
895 						   u32 vr_id,
896 						   enum mlxsw_sp_l3proto proto)
897 {
898 	struct mlxsw_sp_mr_route_params catchall_route_params = {
899 		.prio = MLXSW_SP_MR_ROUTE_PRIO_CATCHALL,
900 		.key = {
901 			.vrid = vr_id,
902 			.proto = proto,
903 		},
904 		.value = {
905 			.route_action = MLXSW_SP_MR_ROUTE_ACTION_TRAP,
906 		}
907 	};
908 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
909 	struct mlxsw_sp_mr_table *mr_table;
910 	int err;
911 	int i;
912 
913 	mr_table = kzalloc(sizeof(*mr_table) + mr->mr_ops->route_priv_size,
914 			   GFP_KERNEL);
915 	if (!mr_table)
916 		return ERR_PTR(-ENOMEM);
917 
918 	mr_table->vr_id = vr_id;
919 	mr_table->mlxsw_sp = mlxsw_sp;
920 	mr_table->proto = proto;
921 	mr_table->ops = &mlxsw_sp_mr_table_ops_arr[proto];
922 	INIT_LIST_HEAD(&mr_table->route_list);
923 	mutex_init(&mr_table->route_list_lock);
924 
925 	err = rhashtable_init(&mr_table->route_ht,
926 			      &mlxsw_sp_mr_route_ht_params);
927 	if (err)
928 		goto err_route_rhashtable_init;
929 
930 	for (i = 0; i < MAXVIFS; i++) {
931 		INIT_LIST_HEAD(&mr_table->vifs[i].route_evif_list);
932 		INIT_LIST_HEAD(&mr_table->vifs[i].route_ivif_list);
933 		mr_table->vifs[i].ops = &mlxsw_sp_mr_vif_ops_arr[proto];
934 	}
935 
936 	err = mr->mr_ops->route_create(mlxsw_sp, mr->priv,
937 				       mr_table->catchall_route_priv,
938 				       &catchall_route_params);
939 	if (err)
940 		goto err_ops_route_create;
941 	mutex_lock(&mr->table_list_lock);
942 	list_add_tail(&mr_table->node, &mr->table_list);
943 	mutex_unlock(&mr->table_list_lock);
944 	return mr_table;
945 
946 err_ops_route_create:
947 	rhashtable_destroy(&mr_table->route_ht);
948 err_route_rhashtable_init:
949 	mutex_destroy(&mr_table->route_list_lock);
950 	kfree(mr_table);
951 	return ERR_PTR(err);
952 }
953 
mlxsw_sp_mr_table_destroy(struct mlxsw_sp_mr_table * mr_table)954 void mlxsw_sp_mr_table_destroy(struct mlxsw_sp_mr_table *mr_table)
955 {
956 	struct mlxsw_sp *mlxsw_sp = mr_table->mlxsw_sp;
957 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
958 
959 	WARN_ON(!mlxsw_sp_mr_table_empty(mr_table));
960 	mutex_lock(&mr->table_list_lock);
961 	list_del(&mr_table->node);
962 	mutex_unlock(&mr->table_list_lock);
963 	mr->mr_ops->route_destroy(mlxsw_sp, mr->priv,
964 				  &mr_table->catchall_route_priv);
965 	rhashtable_destroy(&mr_table->route_ht);
966 	mutex_destroy(&mr_table->route_list_lock);
967 	kfree(mr_table);
968 }
969 
mlxsw_sp_mr_table_flush(struct mlxsw_sp_mr_table * mr_table)970 void mlxsw_sp_mr_table_flush(struct mlxsw_sp_mr_table *mr_table)
971 {
972 	struct mlxsw_sp_mr_route *mr_route, *tmp;
973 	int i;
974 
975 	mutex_lock(&mr_table->route_list_lock);
976 	list_for_each_entry_safe(mr_route, tmp, &mr_table->route_list, node)
977 		__mlxsw_sp_mr_route_del(mr_table, mr_route);
978 	mutex_unlock(&mr_table->route_list_lock);
979 
980 	for (i = 0; i < MAXVIFS; i++) {
981 		mr_table->vifs[i].dev = NULL;
982 		mr_table->vifs[i].rif = NULL;
983 	}
984 }
985 
mlxsw_sp_mr_table_empty(const struct mlxsw_sp_mr_table * mr_table)986 bool mlxsw_sp_mr_table_empty(const struct mlxsw_sp_mr_table *mr_table)
987 {
988 	int i;
989 
990 	for (i = 0; i < MAXVIFS; i++)
991 		if (mr_table->vifs[i].dev)
992 			return false;
993 	return list_empty(&mr_table->route_list);
994 }
995 
mlxsw_sp_mr_route_stats_update(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_mr_route * mr_route)996 static void mlxsw_sp_mr_route_stats_update(struct mlxsw_sp *mlxsw_sp,
997 					   struct mlxsw_sp_mr_route *mr_route)
998 {
999 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
1000 	u64 packets, bytes;
1001 
1002 	if (mr_route->route_action == MLXSW_SP_MR_ROUTE_ACTION_TRAP)
1003 		return;
1004 
1005 	mr->mr_ops->route_stats(mlxsw_sp, mr_route->route_priv, &packets,
1006 				&bytes);
1007 
1008 	if (atomic_long_read(&mr_route->mfc->mfc_un.res.pkt) != packets)
1009 		WRITE_ONCE(mr_route->mfc->mfc_un.res.lastuse, jiffies);
1010 	atomic_long_set(&mr_route->mfc->mfc_un.res.pkt, packets);
1011 	atomic_long_set(&mr_route->mfc->mfc_un.res.bytes, bytes);
1012 }
1013 
mlxsw_sp_mr_stats_update(struct work_struct * work)1014 static void mlxsw_sp_mr_stats_update(struct work_struct *work)
1015 {
1016 	struct mlxsw_sp_mr *mr = container_of(work, struct mlxsw_sp_mr,
1017 					      stats_update_dw.work);
1018 	struct mlxsw_sp_mr_table *mr_table;
1019 	struct mlxsw_sp_mr_route *mr_route;
1020 	unsigned long interval;
1021 
1022 	mutex_lock(&mr->table_list_lock);
1023 	list_for_each_entry(mr_table, &mr->table_list, node) {
1024 		mutex_lock(&mr_table->route_list_lock);
1025 		list_for_each_entry(mr_route, &mr_table->route_list, node)
1026 			mlxsw_sp_mr_route_stats_update(mr_table->mlxsw_sp,
1027 						       mr_route);
1028 		mutex_unlock(&mr_table->route_list_lock);
1029 	}
1030 	mutex_unlock(&mr->table_list_lock);
1031 
1032 	interval = msecs_to_jiffies(MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL);
1033 	mlxsw_core_schedule_dw(&mr->stats_update_dw, interval);
1034 }
1035 
mlxsw_sp_mr_init(struct mlxsw_sp * mlxsw_sp,const struct mlxsw_sp_mr_ops * mr_ops)1036 int mlxsw_sp_mr_init(struct mlxsw_sp *mlxsw_sp,
1037 		     const struct mlxsw_sp_mr_ops *mr_ops)
1038 {
1039 	struct mlxsw_sp_mr *mr;
1040 	unsigned long interval;
1041 	int err;
1042 
1043 	mr = kzalloc(sizeof(*mr) + mr_ops->priv_size, GFP_KERNEL);
1044 	if (!mr)
1045 		return -ENOMEM;
1046 	mr->mr_ops = mr_ops;
1047 	mlxsw_sp->mr = mr;
1048 	INIT_LIST_HEAD(&mr->table_list);
1049 	mutex_init(&mr->table_list_lock);
1050 
1051 	err = mr_ops->init(mlxsw_sp, mr->priv);
1052 	if (err)
1053 		goto err;
1054 
1055 	/* Create the delayed work for counter updates */
1056 	INIT_DELAYED_WORK(&mr->stats_update_dw, mlxsw_sp_mr_stats_update);
1057 	interval = msecs_to_jiffies(MLXSW_SP_MR_ROUTES_COUNTER_UPDATE_INTERVAL);
1058 	mlxsw_core_schedule_dw(&mr->stats_update_dw, interval);
1059 	return 0;
1060 err:
1061 	mutex_destroy(&mr->table_list_lock);
1062 	kfree(mr);
1063 	return err;
1064 }
1065 
mlxsw_sp_mr_fini(struct mlxsw_sp * mlxsw_sp)1066 void mlxsw_sp_mr_fini(struct mlxsw_sp *mlxsw_sp)
1067 {
1068 	struct mlxsw_sp_mr *mr = mlxsw_sp->mr;
1069 
1070 	cancel_delayed_work_sync(&mr->stats_update_dw);
1071 	mr->mr_ops->fini(mlxsw_sp, mr->priv);
1072 	mutex_destroy(&mr->table_list_lock);
1073 	kfree(mr);
1074 }
1075