blob: db1c2c42cd3b6239a1e7ea5c18a9c30cd4a4b69b [file] [log] [blame]
Ido Schimmel464dce12016-07-02 11:00:15 +02001/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2016 Ido Schimmel <idosch@mellanox.com>
Yotam Gigic723c7352016-07-05 11:27:43 +02006 * Copyright (c) 2016 Yotam Gigi <yotamg@mellanox.com>
Ido Schimmel464dce12016-07-02 11:00:15 +02007 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/kernel.h>
38#include <linux/types.h>
Jiri Pirko5e9c16c2016-07-04 08:23:04 +020039#include <linux/rhashtable.h>
40#include <linux/bitops.h>
41#include <linux/in6.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020042#include <linux/notifier.h>
43#include <net/netevent.h>
Jiri Pirko6cf3c972016-07-05 11:27:39 +020044#include <net/neighbour.h>
45#include <net/arp.h>
Ido Schimmel464dce12016-07-02 11:00:15 +020046
47#include "spectrum.h"
48#include "core.h"
49#include "reg.h"
50
Jiri Pirko53342022016-07-04 08:23:08 +020051#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
52 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
53
54static bool
Jiri Pirko6b75c482016-07-04 08:23:09 +020055mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
56 struct mlxsw_sp_prefix_usage *prefix_usage2)
57{
58 unsigned char prefix;
59
60 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
61 if (!test_bit(prefix, prefix_usage2->b))
62 return false;
63 }
64 return true;
65}
66
67static bool
Jiri Pirko53342022016-07-04 08:23:08 +020068mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
69 struct mlxsw_sp_prefix_usage *prefix_usage2)
70{
71 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
72}
73
Jiri Pirko6b75c482016-07-04 08:23:09 +020074static bool
75mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
76{
77 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
78
79 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
80}
81
82static void
83mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
84 struct mlxsw_sp_prefix_usage *prefix_usage2)
85{
86 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
87}
88
89static void
90mlxsw_sp_prefix_usage_zero(struct mlxsw_sp_prefix_usage *prefix_usage)
91{
92 memset(prefix_usage, 0, sizeof(*prefix_usage));
93}
94
Jiri Pirko5e9c16c2016-07-04 08:23:04 +020095static void
96mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
97 unsigned char prefix_len)
98{
99 set_bit(prefix_len, prefix_usage->b);
100}
101
102static void
103mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
104 unsigned char prefix_len)
105{
106 clear_bit(prefix_len, prefix_usage->b);
107}
108
109struct mlxsw_sp_fib_key {
110 unsigned char addr[sizeof(struct in6_addr)];
111 unsigned char prefix_len;
112};
113
Jiri Pirko61c503f2016-07-04 08:23:11 +0200114enum mlxsw_sp_fib_entry_type {
115 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
116 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
117 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
118};
119
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200120struct mlxsw_sp_fib_entry {
121 struct rhash_head ht_node;
122 struct mlxsw_sp_fib_key key;
Jiri Pirko61c503f2016-07-04 08:23:11 +0200123 enum mlxsw_sp_fib_entry_type type;
124 u8 added:1;
125 u16 rif; /* used for action local */
126 struct mlxsw_sp_vr *vr;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200127};
128
129struct mlxsw_sp_fib {
130 struct rhashtable ht;
131 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
132 struct mlxsw_sp_prefix_usage prefix_usage;
133};
134
135static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
136 .key_offset = offsetof(struct mlxsw_sp_fib_entry, key),
137 .head_offset = offsetof(struct mlxsw_sp_fib_entry, ht_node),
138 .key_len = sizeof(struct mlxsw_sp_fib_key),
139 .automatic_shrinking = true,
140};
141
142static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib *fib,
143 struct mlxsw_sp_fib_entry *fib_entry)
144{
145 unsigned char prefix_len = fib_entry->key.prefix_len;
146 int err;
147
148 err = rhashtable_insert_fast(&fib->ht, &fib_entry->ht_node,
149 mlxsw_sp_fib_ht_params);
150 if (err)
151 return err;
152 if (fib->prefix_ref_count[prefix_len]++ == 0)
153 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
154 return 0;
155}
156
157static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib *fib,
158 struct mlxsw_sp_fib_entry *fib_entry)
159{
160 unsigned char prefix_len = fib_entry->key.prefix_len;
161
162 if (--fib->prefix_ref_count[prefix_len] == 0)
163 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
164 rhashtable_remove_fast(&fib->ht, &fib_entry->ht_node,
165 mlxsw_sp_fib_ht_params);
166}
167
168static struct mlxsw_sp_fib_entry *
169mlxsw_sp_fib_entry_create(struct mlxsw_sp_fib *fib, const void *addr,
170 size_t addr_len, unsigned char prefix_len)
171{
172 struct mlxsw_sp_fib_entry *fib_entry;
173
174 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
175 if (!fib_entry)
176 return NULL;
177 memcpy(fib_entry->key.addr, addr, addr_len);
178 fib_entry->key.prefix_len = prefix_len;
179 return fib_entry;
180}
181
182static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry *fib_entry)
183{
184 kfree(fib_entry);
185}
186
187static struct mlxsw_sp_fib_entry *
188mlxsw_sp_fib_entry_lookup(struct mlxsw_sp_fib *fib, const void *addr,
189 size_t addr_len, unsigned char prefix_len)
190{
191 struct mlxsw_sp_fib_key key = {{ 0 } };
192
193 memcpy(key.addr, addr, addr_len);
194 key.prefix_len = prefix_len;
195 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
196}
197
198static struct mlxsw_sp_fib *mlxsw_sp_fib_create(void)
199{
200 struct mlxsw_sp_fib *fib;
201 int err;
202
203 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
204 if (!fib)
205 return ERR_PTR(-ENOMEM);
206 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
207 if (err)
208 goto err_rhashtable_init;
209 return fib;
210
211err_rhashtable_init:
212 kfree(fib);
213 return ERR_PTR(err);
214}
215
216static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
217{
218 rhashtable_destroy(&fib->ht);
219 kfree(fib);
220}
221
Jiri Pirko53342022016-07-04 08:23:08 +0200222static struct mlxsw_sp_lpm_tree *
223mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp, bool one_reserved)
224{
225 static struct mlxsw_sp_lpm_tree *lpm_tree;
226 int i;
227
228 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
229 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
230 if (lpm_tree->ref_count == 0) {
231 if (one_reserved)
232 one_reserved = false;
233 else
234 return lpm_tree;
235 }
236 }
237 return NULL;
238}
239
240static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
241 struct mlxsw_sp_lpm_tree *lpm_tree)
242{
243 char ralta_pl[MLXSW_REG_RALTA_LEN];
244
245 mlxsw_reg_ralta_pack(ralta_pl, true, lpm_tree->proto, lpm_tree->id);
246 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
247}
248
249static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
250 struct mlxsw_sp_lpm_tree *lpm_tree)
251{
252 char ralta_pl[MLXSW_REG_RALTA_LEN];
253
254 mlxsw_reg_ralta_pack(ralta_pl, false, lpm_tree->proto, lpm_tree->id);
255 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
256}
257
258static int
259mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
260 struct mlxsw_sp_prefix_usage *prefix_usage,
261 struct mlxsw_sp_lpm_tree *lpm_tree)
262{
263 char ralst_pl[MLXSW_REG_RALST_LEN];
264 u8 root_bin = 0;
265 u8 prefix;
266 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
267
268 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
269 root_bin = prefix;
270
271 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
272 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
273 if (prefix == 0)
274 continue;
275 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
276 MLXSW_REG_RALST_BIN_NO_CHILD);
277 last_prefix = prefix;
278 }
279 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
280}
281
282static struct mlxsw_sp_lpm_tree *
283mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
284 struct mlxsw_sp_prefix_usage *prefix_usage,
285 enum mlxsw_sp_l3proto proto, bool one_reserved)
286{
287 struct mlxsw_sp_lpm_tree *lpm_tree;
288 int err;
289
290 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp, one_reserved);
291 if (!lpm_tree)
292 return ERR_PTR(-EBUSY);
293 lpm_tree->proto = proto;
294 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
295 if (err)
296 return ERR_PTR(err);
297
298 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
299 lpm_tree);
300 if (err)
301 goto err_left_struct_set;
302 return lpm_tree;
303
304err_left_struct_set:
305 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
306 return ERR_PTR(err);
307}
308
309static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
310 struct mlxsw_sp_lpm_tree *lpm_tree)
311{
312 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
313}
314
315static struct mlxsw_sp_lpm_tree *
316mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
317 struct mlxsw_sp_prefix_usage *prefix_usage,
318 enum mlxsw_sp_l3proto proto, bool one_reserved)
319{
320 struct mlxsw_sp_lpm_tree *lpm_tree;
321 int i;
322
323 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
324 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
325 if (lpm_tree->proto == proto &&
326 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
327 prefix_usage))
328 goto inc_ref_count;
329 }
330 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
331 proto, one_reserved);
332 if (IS_ERR(lpm_tree))
333 return lpm_tree;
334
335inc_ref_count:
336 lpm_tree->ref_count++;
337 return lpm_tree;
338}
339
340static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
341 struct mlxsw_sp_lpm_tree *lpm_tree)
342{
343 if (--lpm_tree->ref_count == 0)
344 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
345 return 0;
346}
347
348static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
349{
350 struct mlxsw_sp_lpm_tree *lpm_tree;
351 int i;
352
353 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
354 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
355 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
356 }
357}
358
Jiri Pirko6b75c482016-07-04 08:23:09 +0200359static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
360{
361 struct mlxsw_sp_vr *vr;
362 int i;
363
364 for (i = 0; i < MLXSW_SP_VIRTUAL_ROUTER_MAX; i++) {
365 vr = &mlxsw_sp->router.vrs[i];
366 if (!vr->used)
367 return vr;
368 }
369 return NULL;
370}
371
372static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
373 struct mlxsw_sp_vr *vr)
374{
375 char raltb_pl[MLXSW_REG_RALTB_LEN];
376
377 mlxsw_reg_raltb_pack(raltb_pl, vr->id, vr->proto, vr->lpm_tree->id);
378 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
379}
380
381static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
382 struct mlxsw_sp_vr *vr)
383{
384 char raltb_pl[MLXSW_REG_RALTB_LEN];
385
386 /* Bind to tree 0 which is default */
387 mlxsw_reg_raltb_pack(raltb_pl, vr->id, vr->proto, 0);
388 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
389}
390
391static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
392{
393 /* For our purpose, squash main and local table into one */
394 if (tb_id == RT_TABLE_LOCAL)
395 tb_id = RT_TABLE_MAIN;
396 return tb_id;
397}
398
399static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
400 u32 tb_id,
401 enum mlxsw_sp_l3proto proto)
402{
403 struct mlxsw_sp_vr *vr;
404 int i;
405
406 tb_id = mlxsw_sp_fix_tb_id(tb_id);
407 for (i = 0; i < MLXSW_SP_VIRTUAL_ROUTER_MAX; i++) {
408 vr = &mlxsw_sp->router.vrs[i];
409 if (vr->used && vr->proto == proto && vr->tb_id == tb_id)
410 return vr;
411 }
412 return NULL;
413}
414
415static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
416 unsigned char prefix_len,
417 u32 tb_id,
418 enum mlxsw_sp_l3proto proto)
419{
420 struct mlxsw_sp_prefix_usage req_prefix_usage;
421 struct mlxsw_sp_lpm_tree *lpm_tree;
422 struct mlxsw_sp_vr *vr;
423 int err;
424
425 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
426 if (!vr)
427 return ERR_PTR(-EBUSY);
428 vr->fib = mlxsw_sp_fib_create();
429 if (IS_ERR(vr->fib))
430 return ERR_CAST(vr->fib);
431
432 vr->proto = proto;
433 vr->tb_id = tb_id;
434 mlxsw_sp_prefix_usage_zero(&req_prefix_usage);
435 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
436 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
437 proto, true);
438 if (IS_ERR(lpm_tree)) {
439 err = PTR_ERR(lpm_tree);
440 goto err_tree_get;
441 }
442 vr->lpm_tree = lpm_tree;
443 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
444 if (err)
445 goto err_tree_bind;
446
447 vr->used = true;
448 return vr;
449
450err_tree_bind:
451 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
452err_tree_get:
453 mlxsw_sp_fib_destroy(vr->fib);
454
455 return ERR_PTR(err);
456}
457
458static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
459 struct mlxsw_sp_vr *vr)
460{
461 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
462 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
463 mlxsw_sp_fib_destroy(vr->fib);
464 vr->used = false;
465}
466
467static int
468mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr,
469 struct mlxsw_sp_prefix_usage *req_prefix_usage)
470{
471 struct mlxsw_sp_lpm_tree *lpm_tree;
472
473 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage,
474 &vr->lpm_tree->prefix_usage))
475 return 0;
476
477 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
478 vr->proto, false);
479 if (IS_ERR(lpm_tree)) {
480 /* We failed to get a tree according to the required
481 * prefix usage. However, the current tree might be still good
482 * for us if our requirement is subset of the prefixes used
483 * in the tree.
484 */
485 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
486 &vr->lpm_tree->prefix_usage))
487 return 0;
488 return PTR_ERR(lpm_tree);
489 }
490
491 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
492 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
493 vr->lpm_tree = lpm_tree;
494 return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
495}
496
497static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp,
498 unsigned char prefix_len,
499 u32 tb_id,
500 enum mlxsw_sp_l3proto proto)
501{
502 struct mlxsw_sp_vr *vr;
503 int err;
504
505 tb_id = mlxsw_sp_fix_tb_id(tb_id);
506 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id, proto);
507 if (!vr) {
508 vr = mlxsw_sp_vr_create(mlxsw_sp, prefix_len, tb_id, proto);
509 if (IS_ERR(vr))
510 return vr;
511 } else {
512 struct mlxsw_sp_prefix_usage req_prefix_usage;
513
514 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage,
515 &vr->fib->prefix_usage);
516 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
517 /* Need to replace LPM tree in case new prefix is required. */
518 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
519 &req_prefix_usage);
520 if (err)
521 return ERR_PTR(err);
522 }
523 return vr;
524}
525
526static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
527{
528 /* Destroy virtual router entity in case the associated FIB is empty
529 * and allow it to be used for other tables in future. Otherwise,
530 * check if some prefix usage did not disappear and change tree if
531 * that is the case. Note that in case new, smaller tree cannot be
532 * allocated, the original one will be kept being used.
533 */
534 if (mlxsw_sp_prefix_usage_none(&vr->fib->prefix_usage))
535 mlxsw_sp_vr_destroy(mlxsw_sp, vr);
536 else
537 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
538 &vr->fib->prefix_usage);
539}
540
541static void mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
542{
543 struct mlxsw_sp_vr *vr;
544 int i;
545
546 for (i = 0; i < MLXSW_SP_VIRTUAL_ROUTER_MAX; i++) {
547 vr = &mlxsw_sp->router.vrs[i];
548 vr->id = i;
549 }
550}
551
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200552struct mlxsw_sp_neigh_key {
553 unsigned char addr[sizeof(struct in6_addr)];
554 struct net_device *dev;
555};
556
557struct mlxsw_sp_neigh_entry {
558 struct rhash_head ht_node;
559 struct mlxsw_sp_neigh_key key;
560 u16 rif;
561 struct neighbour *n;
562};
563
564static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
565 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
566 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
567 .key_len = sizeof(struct mlxsw_sp_neigh_key),
568};
569
570static int
571mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
572 struct mlxsw_sp_neigh_entry *neigh_entry)
573{
574 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
575 &neigh_entry->ht_node,
576 mlxsw_sp_neigh_ht_params);
577}
578
579static void
580mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
581 struct mlxsw_sp_neigh_entry *neigh_entry)
582{
583 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
584 &neigh_entry->ht_node,
585 mlxsw_sp_neigh_ht_params);
586}
587
588static struct mlxsw_sp_neigh_entry *
589mlxsw_sp_neigh_entry_create(const void *addr, size_t addr_len,
590 struct net_device *dev, u16 rif,
591 struct neighbour *n)
592{
593 struct mlxsw_sp_neigh_entry *neigh_entry;
594
595 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_ATOMIC);
596 if (!neigh_entry)
597 return NULL;
598 memcpy(neigh_entry->key.addr, addr, addr_len);
599 neigh_entry->key.dev = dev;
600 neigh_entry->rif = rif;
601 neigh_entry->n = n;
602 return neigh_entry;
603}
604
605static void
606mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp_neigh_entry *neigh_entry)
607{
608 kfree(neigh_entry);
609}
610
611static struct mlxsw_sp_neigh_entry *
612mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, const void *addr,
613 size_t addr_len, struct net_device *dev)
614{
615 struct mlxsw_sp_neigh_key key = {{ 0 } };
616
617 memcpy(key.addr, addr, addr_len);
618 key.dev = dev;
619 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
620 &key, mlxsw_sp_neigh_ht_params);
621}
622
623int mlxsw_sp_router_neigh_construct(struct net_device *dev,
624 struct neighbour *n)
625{
626 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
627 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
628 struct mlxsw_sp_neigh_entry *neigh_entry;
629 struct mlxsw_sp_rif *r;
630 u32 dip;
631 int err;
632
633 if (n->tbl != &arp_tbl)
634 return 0;
635
636 dip = ntohl(*((__be32 *) n->primary_key));
637 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
638 n->dev);
639 if (neigh_entry) {
640 WARN_ON(neigh_entry->n != n);
641 return 0;
642 }
643
644 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
645 if (WARN_ON(!r))
646 return -EINVAL;
647
648 neigh_entry = mlxsw_sp_neigh_entry_create(&dip, sizeof(dip), n->dev,
649 r->rif, n);
650 if (!neigh_entry)
651 return -ENOMEM;
652 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
653 if (err)
654 goto err_neigh_entry_insert;
655 return 0;
656
657err_neigh_entry_insert:
658 mlxsw_sp_neigh_entry_destroy(neigh_entry);
659 return err;
660}
661
662void mlxsw_sp_router_neigh_destroy(struct net_device *dev,
663 struct neighbour *n)
664{
665 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
666 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
667 struct mlxsw_sp_neigh_entry *neigh_entry;
668 u32 dip;
669
670 if (n->tbl != &arp_tbl)
671 return;
672
673 dip = ntohl(*((__be32 *) n->primary_key));
674 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
675 n->dev);
676 if (!neigh_entry)
677 return;
678 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
679 mlxsw_sp_neigh_entry_destroy(neigh_entry);
680}
681
Yotam Gigic723c7352016-07-05 11:27:43 +0200682static void
683mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
684{
685 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
686
687 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
688}
689
690static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
691 char *rauhtd_pl,
692 int ent_index)
693{
694 struct net_device *dev;
695 struct neighbour *n;
696 __be32 dipn;
697 u32 dip;
698 u16 rif;
699
700 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
701
702 if (!mlxsw_sp->rifs[rif]) {
703 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
704 return;
705 }
706
707 dipn = htonl(dip);
708 dev = mlxsw_sp->rifs[rif]->dev;
709 n = neigh_lookup(&arp_tbl, &dipn, dev);
710 if (!n) {
711 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
712 &dip);
713 return;
714 }
715
716 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
717 neigh_event_send(n, NULL);
718 neigh_release(n);
719}
720
721static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
722 char *rauhtd_pl,
723 int rec_index)
724{
725 u8 num_entries;
726 int i;
727
728 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
729 rec_index);
730 /* Hardware starts counting at 0, so add 1. */
731 num_entries++;
732
733 /* Each record consists of several neighbour entries. */
734 for (i = 0; i < num_entries; i++) {
735 int ent_index;
736
737 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
738 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
739 ent_index);
740 }
741
742}
743
744static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
745 char *rauhtd_pl, int rec_index)
746{
747 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
748 case MLXSW_REG_RAUHTD_TYPE_IPV4:
749 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
750 rec_index);
751 break;
752 case MLXSW_REG_RAUHTD_TYPE_IPV6:
753 WARN_ON_ONCE(1);
754 break;
755 }
756}
757
758static void
759mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
760{
761 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
762
763 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
764 msecs_to_jiffies(interval));
765}
766
767static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
768{
769 struct mlxsw_sp *mlxsw_sp;
770 char *rauhtd_pl;
771 u8 num_rec;
772 int i, err;
773
774 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
775 if (!rauhtd_pl)
776 return;
777
778 mlxsw_sp = container_of(work, struct mlxsw_sp,
779 router.neighs_update.dw.work);
780
781 /* Make sure the neighbour's netdev isn't removed in the
782 * process.
783 */
784 rtnl_lock();
785 do {
786 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
787 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
788 rauhtd_pl);
789 if (err) {
790 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
791 break;
792 }
793 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
794 for (i = 0; i < num_rec; i++)
795 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
796 i);
797 } while (num_rec);
798 rtnl_unlock();
799
800 kfree(rauhtd_pl);
801 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
802}
803
804static int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
805 unsigned long event, void *ptr)
806{
807 struct mlxsw_sp_port *mlxsw_sp_port;
808 struct mlxsw_sp *mlxsw_sp;
809 unsigned long interval;
810 struct neigh_parms *p;
811
812 switch (event) {
813 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
814 p = ptr;
815
816 /* We don't care about changes in the default table. */
817 if (!p->dev || p->tbl != &arp_tbl)
818 return NOTIFY_DONE;
819
820 /* We are in atomic context and can't take RTNL mutex,
821 * so use RCU variant to walk the device chain.
822 */
823 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
824 if (!mlxsw_sp_port)
825 return NOTIFY_DONE;
826
827 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
828 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
829 mlxsw_sp->router.neighs_update.interval = interval;
830
831 mlxsw_sp_port_dev_put(mlxsw_sp_port);
832 break;
833 }
834
835 return NOTIFY_DONE;
836}
837
838static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly = {
839 .notifier_call = mlxsw_sp_router_netevent_event,
840};
841
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200842static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
843{
Yotam Gigic723c7352016-07-05 11:27:43 +0200844 int err;
845
846 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
847 &mlxsw_sp_neigh_ht_params);
848 if (err)
849 return err;
850
851 /* Initialize the polling interval according to the default
852 * table.
853 */
854 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
855
856 err = register_netevent_notifier(&mlxsw_sp_router_netevent_nb);
857 if (err)
858 goto err_register_netevent_notifier;
859
860 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
861 mlxsw_sp_router_neighs_update_work);
862 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
863
864 return 0;
865
866err_register_netevent_notifier:
867 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
868 return err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200869}
870
871static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
872{
Yotam Gigic723c7352016-07-05 11:27:43 +0200873 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
874 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200875 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
876}
877
Ido Schimmel464dce12016-07-02 11:00:15 +0200878static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
879{
880 char rgcr_pl[MLXSW_REG_RGCR_LEN];
881
882 mlxsw_reg_rgcr_pack(rgcr_pl, true);
883 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, MLXSW_SP_RIF_MAX);
884 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
885}
886
887static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
888{
889 char rgcr_pl[MLXSW_REG_RGCR_LEN];
890
891 mlxsw_reg_rgcr_pack(rgcr_pl, false);
892 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
893}
894
895int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
896{
Jiri Pirko53342022016-07-04 08:23:08 +0200897 int err;
898
899 err = __mlxsw_sp_router_init(mlxsw_sp);
900 if (err)
901 return err;
902 mlxsw_sp_lpm_init(mlxsw_sp);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200903 mlxsw_sp_vrs_init(mlxsw_sp);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200904 return mlxsw_sp_neigh_init(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +0200905}
906
907void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
908{
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200909 mlxsw_sp_neigh_fini(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +0200910 __mlxsw_sp_router_fini(mlxsw_sp);
911}
Jiri Pirko61c503f2016-07-04 08:23:11 +0200912
913static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
914 struct mlxsw_sp_fib_entry *fib_entry,
915 enum mlxsw_reg_ralue_op op)
916{
917 char ralue_pl[MLXSW_REG_RALUE_LEN];
918 u32 *p_dip = (u32 *) fib_entry->key.addr;
919 struct mlxsw_sp_vr *vr = fib_entry->vr;
920
921 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
922 fib_entry->key.prefix_len, *p_dip);
923 mlxsw_reg_ralue_act_local_pack(ralue_pl,
924 MLXSW_REG_RALUE_TRAP_ACTION_NOP, 0,
925 fib_entry->rif);
926 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
927}
928
929static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
930 struct mlxsw_sp_fib_entry *fib_entry,
931 enum mlxsw_reg_ralue_op op)
932{
933 char ralue_pl[MLXSW_REG_RALUE_LEN];
934 u32 *p_dip = (u32 *) fib_entry->key.addr;
935 struct mlxsw_sp_vr *vr = fib_entry->vr;
936
937 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
938 fib_entry->key.prefix_len, *p_dip);
939 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
940 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
941}
942
943static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
944 struct mlxsw_sp_fib_entry *fib_entry,
945 enum mlxsw_reg_ralue_op op)
946{
947 switch (fib_entry->type) {
948 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
949 return -EINVAL;
950 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
951 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
952 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
953 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
954 }
955 return -EINVAL;
956}
957
958static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
959 struct mlxsw_sp_fib_entry *fib_entry,
960 enum mlxsw_reg_ralue_op op)
961{
962 switch (fib_entry->vr->proto) {
963 case MLXSW_SP_L3_PROTO_IPV4:
964 return mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
965 case MLXSW_SP_L3_PROTO_IPV6:
966 return -EINVAL;
967 }
968 return -EINVAL;
969}
970
971static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
972 struct mlxsw_sp_fib_entry *fib_entry)
973{
974 enum mlxsw_reg_ralue_op op;
975
976 op = !fib_entry->added ? MLXSW_REG_RALUE_OP_WRITE_WRITE :
977 MLXSW_REG_RALUE_OP_WRITE_UPDATE;
978 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
979}
980
981static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
982 struct mlxsw_sp_fib_entry *fib_entry)
983{
984 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
985 MLXSW_REG_RALUE_OP_WRITE_DELETE);
986}
987
988struct mlxsw_sp_router_fib4_add_info {
989 struct switchdev_trans_item tritem;
990 struct mlxsw_sp *mlxsw_sp;
991 struct mlxsw_sp_fib_entry *fib_entry;
992};
993
994static void mlxsw_sp_router_fib4_add_info_destroy(void const *data)
995{
996 const struct mlxsw_sp_router_fib4_add_info *info = data;
997 struct mlxsw_sp_fib_entry *fib_entry = info->fib_entry;
998 struct mlxsw_sp *mlxsw_sp = info->mlxsw_sp;
999
1000 mlxsw_sp_fib_entry_destroy(fib_entry);
1001 mlxsw_sp_vr_put(mlxsw_sp, fib_entry->vr);
1002 kfree(info);
1003}
1004
1005static int
1006mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp *mlxsw_sp,
1007 const struct switchdev_obj_ipv4_fib *fib4,
1008 struct mlxsw_sp_fib_entry *fib_entry)
1009{
1010 struct fib_info *fi = fib4->fi;
1011
1012 if (fib4->type == RTN_LOCAL || fib4->type == RTN_BROADCAST) {
1013 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1014 return 0;
1015 }
1016 if (fib4->type != RTN_UNICAST)
1017 return -EINVAL;
1018
1019 if (fi->fib_scope != RT_SCOPE_UNIVERSE) {
1020 struct mlxsw_sp_rif *r;
1021
1022 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1023 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fi->fib_dev);
1024 if (!r)
1025 return -EINVAL;
1026 fib_entry->rif = r->rif;
1027 return 0;
1028 }
1029 return -EINVAL;
1030}
1031
1032static int
1033mlxsw_sp_router_fib4_add_prepare(struct mlxsw_sp_port *mlxsw_sp_port,
1034 const struct switchdev_obj_ipv4_fib *fib4,
1035 struct switchdev_trans *trans)
1036{
1037 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1038 struct mlxsw_sp_router_fib4_add_info *info;
1039 struct mlxsw_sp_fib_entry *fib_entry;
1040 struct mlxsw_sp_vr *vr;
1041 int err;
1042
1043 vr = mlxsw_sp_vr_get(mlxsw_sp, fib4->dst_len, fib4->tb_id,
1044 MLXSW_SP_L3_PROTO_IPV4);
1045 if (IS_ERR(vr))
1046 return PTR_ERR(vr);
1047
1048 fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fib4->dst,
1049 sizeof(fib4->dst), fib4->dst_len);
1050 if (!fib_entry) {
1051 err = -ENOMEM;
1052 goto err_fib_entry_create;
1053 }
1054 fib_entry->vr = vr;
1055
1056 err = mlxsw_sp_router_fib4_entry_init(mlxsw_sp, fib4, fib_entry);
1057 if (err)
1058 goto err_fib4_entry_init;
1059
1060 info = kmalloc(sizeof(*info), GFP_KERNEL);
1061 if (!info) {
1062 err = -ENOMEM;
1063 goto err_alloc_info;
1064 }
1065 info->mlxsw_sp = mlxsw_sp;
1066 info->fib_entry = fib_entry;
1067 switchdev_trans_item_enqueue(trans, info,
1068 mlxsw_sp_router_fib4_add_info_destroy,
1069 &info->tritem);
1070 return 0;
1071
1072err_alloc_info:
1073err_fib4_entry_init:
1074 mlxsw_sp_fib_entry_destroy(fib_entry);
1075err_fib_entry_create:
1076 mlxsw_sp_vr_put(mlxsw_sp, vr);
1077 return err;
1078}
1079
1080static int
1081mlxsw_sp_router_fib4_add_commit(struct mlxsw_sp_port *mlxsw_sp_port,
1082 const struct switchdev_obj_ipv4_fib *fib4,
1083 struct switchdev_trans *trans)
1084{
1085 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1086 struct mlxsw_sp_router_fib4_add_info *info;
1087 struct mlxsw_sp_fib_entry *fib_entry;
1088 struct mlxsw_sp_vr *vr;
1089 int err;
1090
1091 info = switchdev_trans_item_dequeue(trans);
1092 fib_entry = info->fib_entry;
1093 kfree(info);
1094
1095 vr = fib_entry->vr;
1096 err = mlxsw_sp_fib_entry_insert(fib_entry->vr->fib, fib_entry);
1097 if (err)
1098 goto err_fib_entry_insert;
1099 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1100 if (err)
1101 goto err_fib_entry_add;
1102 return 0;
1103
1104err_fib_entry_add:
1105 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1106err_fib_entry_insert:
1107 mlxsw_sp_fib_entry_destroy(fib_entry);
1108 mlxsw_sp_vr_put(mlxsw_sp, vr);
1109 return err;
1110}
1111
1112int mlxsw_sp_router_fib4_add(struct mlxsw_sp_port *mlxsw_sp_port,
1113 const struct switchdev_obj_ipv4_fib *fib4,
1114 struct switchdev_trans *trans)
1115{
1116 if (switchdev_trans_ph_prepare(trans))
1117 return mlxsw_sp_router_fib4_add_prepare(mlxsw_sp_port,
1118 fib4, trans);
1119 return mlxsw_sp_router_fib4_add_commit(mlxsw_sp_port,
1120 fib4, trans);
1121}
1122
1123int mlxsw_sp_router_fib4_del(struct mlxsw_sp_port *mlxsw_sp_port,
1124 const struct switchdev_obj_ipv4_fib *fib4)
1125{
1126 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1127 struct mlxsw_sp_fib_entry *fib_entry;
1128 struct mlxsw_sp_vr *vr;
1129
1130 vr = mlxsw_sp_vr_find(mlxsw_sp, fib4->tb_id, MLXSW_SP_L3_PROTO_IPV4);
1131 if (!vr) {
1132 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find virtual router for FIB4 entry being removed.\n");
1133 return -ENOENT;
1134 }
1135 fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fib4->dst,
1136 sizeof(fib4->dst), fib4->dst_len);
1137 if (!fib_entry) {
1138 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find FIB4 entry being removed.\n");
1139 return PTR_ERR(vr);
1140 }
1141 mlxsw_sp_fib_entry_del(mlxsw_sp_port->mlxsw_sp, fib_entry);
1142 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1143 mlxsw_sp_fib_entry_destroy(fib_entry);
1144 mlxsw_sp_vr_put(mlxsw_sp, vr);
1145 return 0;
1146}