blob: ed0e6c09dcc8816d081799610dd56139b6f6f2a0 [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;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200562 bool offloaded;
563 struct delayed_work dw;
564 struct mlxsw_sp_port *mlxsw_sp_port;
565 unsigned char ha[ETH_ALEN];
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200566};
567
568static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
569 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
570 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
571 .key_len = sizeof(struct mlxsw_sp_neigh_key),
572};
573
574static int
575mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
576 struct mlxsw_sp_neigh_entry *neigh_entry)
577{
578 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
579 &neigh_entry->ht_node,
580 mlxsw_sp_neigh_ht_params);
581}
582
583static void
584mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
585 struct mlxsw_sp_neigh_entry *neigh_entry)
586{
587 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
588 &neigh_entry->ht_node,
589 mlxsw_sp_neigh_ht_params);
590}
591
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200592static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work);
593
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200594static struct mlxsw_sp_neigh_entry *
595mlxsw_sp_neigh_entry_create(const void *addr, size_t addr_len,
596 struct net_device *dev, u16 rif,
597 struct neighbour *n)
598{
599 struct mlxsw_sp_neigh_entry *neigh_entry;
600
601 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_ATOMIC);
602 if (!neigh_entry)
603 return NULL;
604 memcpy(neigh_entry->key.addr, addr, addr_len);
605 neigh_entry->key.dev = dev;
606 neigh_entry->rif = rif;
607 neigh_entry->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200608 INIT_DELAYED_WORK(&neigh_entry->dw, mlxsw_sp_router_neigh_update_hw);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200609 return neigh_entry;
610}
611
612static void
613mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp_neigh_entry *neigh_entry)
614{
615 kfree(neigh_entry);
616}
617
618static struct mlxsw_sp_neigh_entry *
619mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, const void *addr,
620 size_t addr_len, struct net_device *dev)
621{
622 struct mlxsw_sp_neigh_key key = {{ 0 } };
623
624 memcpy(key.addr, addr, addr_len);
625 key.dev = dev;
626 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
627 &key, mlxsw_sp_neigh_ht_params);
628}
629
630int mlxsw_sp_router_neigh_construct(struct net_device *dev,
631 struct neighbour *n)
632{
633 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
634 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
635 struct mlxsw_sp_neigh_entry *neigh_entry;
636 struct mlxsw_sp_rif *r;
637 u32 dip;
638 int err;
639
640 if (n->tbl != &arp_tbl)
641 return 0;
642
643 dip = ntohl(*((__be32 *) n->primary_key));
644 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
645 n->dev);
646 if (neigh_entry) {
647 WARN_ON(neigh_entry->n != n);
648 return 0;
649 }
650
651 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
652 if (WARN_ON(!r))
653 return -EINVAL;
654
655 neigh_entry = mlxsw_sp_neigh_entry_create(&dip, sizeof(dip), n->dev,
656 r->rif, n);
657 if (!neigh_entry)
658 return -ENOMEM;
659 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
660 if (err)
661 goto err_neigh_entry_insert;
662 return 0;
663
664err_neigh_entry_insert:
665 mlxsw_sp_neigh_entry_destroy(neigh_entry);
666 return err;
667}
668
669void mlxsw_sp_router_neigh_destroy(struct net_device *dev,
670 struct neighbour *n)
671{
672 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
673 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
674 struct mlxsw_sp_neigh_entry *neigh_entry;
675 u32 dip;
676
677 if (n->tbl != &arp_tbl)
678 return;
679
680 dip = ntohl(*((__be32 *) n->primary_key));
681 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
682 n->dev);
683 if (!neigh_entry)
684 return;
685 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
686 mlxsw_sp_neigh_entry_destroy(neigh_entry);
687}
688
Yotam Gigic723c7352016-07-05 11:27:43 +0200689static void
690mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
691{
692 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
693
694 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
695}
696
697static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
698 char *rauhtd_pl,
699 int ent_index)
700{
701 struct net_device *dev;
702 struct neighbour *n;
703 __be32 dipn;
704 u32 dip;
705 u16 rif;
706
707 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
708
709 if (!mlxsw_sp->rifs[rif]) {
710 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
711 return;
712 }
713
714 dipn = htonl(dip);
715 dev = mlxsw_sp->rifs[rif]->dev;
716 n = neigh_lookup(&arp_tbl, &dipn, dev);
717 if (!n) {
718 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
719 &dip);
720 return;
721 }
722
723 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
724 neigh_event_send(n, NULL);
725 neigh_release(n);
726}
727
728static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
729 char *rauhtd_pl,
730 int rec_index)
731{
732 u8 num_entries;
733 int i;
734
735 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
736 rec_index);
737 /* Hardware starts counting at 0, so add 1. */
738 num_entries++;
739
740 /* Each record consists of several neighbour entries. */
741 for (i = 0; i < num_entries; i++) {
742 int ent_index;
743
744 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
745 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
746 ent_index);
747 }
748
749}
750
751static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
752 char *rauhtd_pl, int rec_index)
753{
754 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
755 case MLXSW_REG_RAUHTD_TYPE_IPV4:
756 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
757 rec_index);
758 break;
759 case MLXSW_REG_RAUHTD_TYPE_IPV6:
760 WARN_ON_ONCE(1);
761 break;
762 }
763}
764
765static void
766mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
767{
768 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
769
770 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
771 msecs_to_jiffies(interval));
772}
773
774static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
775{
776 struct mlxsw_sp *mlxsw_sp;
777 char *rauhtd_pl;
778 u8 num_rec;
779 int i, err;
780
781 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
782 if (!rauhtd_pl)
783 return;
784
785 mlxsw_sp = container_of(work, struct mlxsw_sp,
786 router.neighs_update.dw.work);
787
788 /* Make sure the neighbour's netdev isn't removed in the
789 * process.
790 */
791 rtnl_lock();
792 do {
793 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
794 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
795 rauhtd_pl);
796 if (err) {
797 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
798 break;
799 }
800 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
801 for (i = 0; i < num_rec; i++)
802 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
803 i);
804 } while (num_rec);
805 rtnl_unlock();
806
807 kfree(rauhtd_pl);
808 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
809}
810
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200811static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work)
812{
813 struct mlxsw_sp_neigh_entry *neigh_entry =
814 container_of(work, struct mlxsw_sp_neigh_entry, dw.work);
815 struct neighbour *n = neigh_entry->n;
816 struct mlxsw_sp_port *mlxsw_sp_port = neigh_entry->mlxsw_sp_port;
817 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
818 char rauht_pl[MLXSW_REG_RAUHT_LEN];
819 struct net_device *dev;
820 bool entry_connected;
821 u8 nud_state;
822 bool updating;
823 bool removing;
824 bool adding;
825 u32 dip;
826 int err;
827
828 read_lock_bh(&n->lock);
829 dip = ntohl(*((__be32 *) n->primary_key));
830 memcpy(neigh_entry->ha, n->ha, sizeof(neigh_entry->ha));
831 nud_state = n->nud_state;
832 dev = n->dev;
833 read_unlock_bh(&n->lock);
834
835 entry_connected = nud_state & NUD_VALID;
836 adding = (!neigh_entry->offloaded) && entry_connected;
837 updating = neigh_entry->offloaded && entry_connected;
838 removing = neigh_entry->offloaded && !entry_connected;
839
840 if (adding || updating) {
841 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_ADD,
842 neigh_entry->rif,
843 neigh_entry->ha, dip);
844 err = mlxsw_reg_write(mlxsw_sp->core,
845 MLXSW_REG(rauht), rauht_pl);
846 if (err) {
847 netdev_err(dev, "Could not add neigh %pI4h\n", &dip);
848 neigh_entry->offloaded = false;
849 } else {
850 neigh_entry->offloaded = true;
851 }
852 } else if (removing) {
853 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE,
854 neigh_entry->rif,
855 neigh_entry->ha, dip);
856 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht),
857 rauht_pl);
858 if (err) {
859 netdev_err(dev, "Could not delete neigh %pI4h\n", &dip);
860 neigh_entry->offloaded = true;
861 } else {
862 neigh_entry->offloaded = false;
863 }
864 }
865
866 neigh_release(n);
867 mlxsw_sp_port_dev_put(mlxsw_sp_port);
868}
869
Yotam Gigic723c7352016-07-05 11:27:43 +0200870static int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
871 unsigned long event, void *ptr)
872{
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200873 struct mlxsw_sp_neigh_entry *neigh_entry;
Yotam Gigic723c7352016-07-05 11:27:43 +0200874 struct mlxsw_sp_port *mlxsw_sp_port;
875 struct mlxsw_sp *mlxsw_sp;
876 unsigned long interval;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200877 struct net_device *dev;
Yotam Gigic723c7352016-07-05 11:27:43 +0200878 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200879 struct neighbour *n;
880 u32 dip;
Yotam Gigic723c7352016-07-05 11:27:43 +0200881
882 switch (event) {
883 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
884 p = ptr;
885
886 /* We don't care about changes in the default table. */
887 if (!p->dev || p->tbl != &arp_tbl)
888 return NOTIFY_DONE;
889
890 /* We are in atomic context and can't take RTNL mutex,
891 * so use RCU variant to walk the device chain.
892 */
893 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
894 if (!mlxsw_sp_port)
895 return NOTIFY_DONE;
896
897 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
898 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
899 mlxsw_sp->router.neighs_update.interval = interval;
900
901 mlxsw_sp_port_dev_put(mlxsw_sp_port);
902 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200903 case NETEVENT_NEIGH_UPDATE:
904 n = ptr;
905 dev = n->dev;
906
907 if (n->tbl != &arp_tbl)
908 return NOTIFY_DONE;
909
910 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(dev);
911 if (!mlxsw_sp_port)
912 return NOTIFY_DONE;
913
914 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
915 dip = ntohl(*((__be32 *) n->primary_key));
916 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp,
917 &dip,
918 sizeof(__be32),
919 dev);
920 if (WARN_ON(!neigh_entry) || WARN_ON(neigh_entry->n != n)) {
921 mlxsw_sp_port_dev_put(mlxsw_sp_port);
922 return NOTIFY_DONE;
923 }
924 neigh_entry->mlxsw_sp_port = mlxsw_sp_port;
925
926 /* Take a reference to ensure the neighbour won't be
927 * destructed until we drop the reference in delayed
928 * work.
929 */
930 neigh_clone(n);
931 if (!mlxsw_core_schedule_dw(&neigh_entry->dw, 0)) {
932 neigh_release(n);
933 mlxsw_sp_port_dev_put(mlxsw_sp_port);
934 }
935 break;
Yotam Gigic723c7352016-07-05 11:27:43 +0200936 }
937
938 return NOTIFY_DONE;
939}
940
941static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly = {
942 .notifier_call = mlxsw_sp_router_netevent_event,
943};
944
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200945static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
946{
Yotam Gigic723c7352016-07-05 11:27:43 +0200947 int err;
948
949 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
950 &mlxsw_sp_neigh_ht_params);
951 if (err)
952 return err;
953
954 /* Initialize the polling interval according to the default
955 * table.
956 */
957 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
958
959 err = register_netevent_notifier(&mlxsw_sp_router_netevent_nb);
960 if (err)
961 goto err_register_netevent_notifier;
962
963 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
964 mlxsw_sp_router_neighs_update_work);
965 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
966
967 return 0;
968
969err_register_netevent_notifier:
970 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
971 return err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200972}
973
974static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
975{
Yotam Gigic723c7352016-07-05 11:27:43 +0200976 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
977 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200978 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
979}
980
Ido Schimmel464dce12016-07-02 11:00:15 +0200981static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
982{
983 char rgcr_pl[MLXSW_REG_RGCR_LEN];
984
985 mlxsw_reg_rgcr_pack(rgcr_pl, true);
986 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, MLXSW_SP_RIF_MAX);
987 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
988}
989
990static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
991{
992 char rgcr_pl[MLXSW_REG_RGCR_LEN];
993
994 mlxsw_reg_rgcr_pack(rgcr_pl, false);
995 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
996}
997
998int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
999{
Jiri Pirko53342022016-07-04 08:23:08 +02001000 int err;
1001
1002 err = __mlxsw_sp_router_init(mlxsw_sp);
1003 if (err)
1004 return err;
1005 mlxsw_sp_lpm_init(mlxsw_sp);
Jiri Pirko6b75c482016-07-04 08:23:09 +02001006 mlxsw_sp_vrs_init(mlxsw_sp);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001007 return mlxsw_sp_neigh_init(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +02001008}
1009
1010void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
1011{
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001012 mlxsw_sp_neigh_fini(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +02001013 __mlxsw_sp_router_fini(mlxsw_sp);
1014}
Jiri Pirko61c503f2016-07-04 08:23:11 +02001015
1016static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1017 struct mlxsw_sp_fib_entry *fib_entry,
1018 enum mlxsw_reg_ralue_op op)
1019{
1020 char ralue_pl[MLXSW_REG_RALUE_LEN];
1021 u32 *p_dip = (u32 *) fib_entry->key.addr;
1022 struct mlxsw_sp_vr *vr = fib_entry->vr;
1023
1024 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1025 fib_entry->key.prefix_len, *p_dip);
1026 mlxsw_reg_ralue_act_local_pack(ralue_pl,
1027 MLXSW_REG_RALUE_TRAP_ACTION_NOP, 0,
1028 fib_entry->rif);
1029 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1030}
1031
1032static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1033 struct mlxsw_sp_fib_entry *fib_entry,
1034 enum mlxsw_reg_ralue_op op)
1035{
1036 char ralue_pl[MLXSW_REG_RALUE_LEN];
1037 u32 *p_dip = (u32 *) fib_entry->key.addr;
1038 struct mlxsw_sp_vr *vr = fib_entry->vr;
1039
1040 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1041 fib_entry->key.prefix_len, *p_dip);
1042 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1043 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1044}
1045
1046static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1047 struct mlxsw_sp_fib_entry *fib_entry,
1048 enum mlxsw_reg_ralue_op op)
1049{
1050 switch (fib_entry->type) {
1051 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
1052 return -EINVAL;
1053 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1054 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1055 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1056 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1057 }
1058 return -EINVAL;
1059}
1060
1061static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1062 struct mlxsw_sp_fib_entry *fib_entry,
1063 enum mlxsw_reg_ralue_op op)
1064{
1065 switch (fib_entry->vr->proto) {
1066 case MLXSW_SP_L3_PROTO_IPV4:
1067 return mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1068 case MLXSW_SP_L3_PROTO_IPV6:
1069 return -EINVAL;
1070 }
1071 return -EINVAL;
1072}
1073
1074static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1075 struct mlxsw_sp_fib_entry *fib_entry)
1076{
1077 enum mlxsw_reg_ralue_op op;
1078
1079 op = !fib_entry->added ? MLXSW_REG_RALUE_OP_WRITE_WRITE :
1080 MLXSW_REG_RALUE_OP_WRITE_UPDATE;
1081 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
1082}
1083
1084static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1085 struct mlxsw_sp_fib_entry *fib_entry)
1086{
1087 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1088 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1089}
1090
1091struct mlxsw_sp_router_fib4_add_info {
1092 struct switchdev_trans_item tritem;
1093 struct mlxsw_sp *mlxsw_sp;
1094 struct mlxsw_sp_fib_entry *fib_entry;
1095};
1096
1097static void mlxsw_sp_router_fib4_add_info_destroy(void const *data)
1098{
1099 const struct mlxsw_sp_router_fib4_add_info *info = data;
1100 struct mlxsw_sp_fib_entry *fib_entry = info->fib_entry;
1101 struct mlxsw_sp *mlxsw_sp = info->mlxsw_sp;
1102
1103 mlxsw_sp_fib_entry_destroy(fib_entry);
1104 mlxsw_sp_vr_put(mlxsw_sp, fib_entry->vr);
1105 kfree(info);
1106}
1107
1108static int
1109mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp *mlxsw_sp,
1110 const struct switchdev_obj_ipv4_fib *fib4,
1111 struct mlxsw_sp_fib_entry *fib_entry)
1112{
1113 struct fib_info *fi = fib4->fi;
1114
1115 if (fib4->type == RTN_LOCAL || fib4->type == RTN_BROADCAST) {
1116 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1117 return 0;
1118 }
1119 if (fib4->type != RTN_UNICAST)
1120 return -EINVAL;
1121
1122 if (fi->fib_scope != RT_SCOPE_UNIVERSE) {
1123 struct mlxsw_sp_rif *r;
1124
1125 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1126 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fi->fib_dev);
1127 if (!r)
1128 return -EINVAL;
1129 fib_entry->rif = r->rif;
1130 return 0;
1131 }
1132 return -EINVAL;
1133}
1134
1135static int
1136mlxsw_sp_router_fib4_add_prepare(struct mlxsw_sp_port *mlxsw_sp_port,
1137 const struct switchdev_obj_ipv4_fib *fib4,
1138 struct switchdev_trans *trans)
1139{
1140 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1141 struct mlxsw_sp_router_fib4_add_info *info;
1142 struct mlxsw_sp_fib_entry *fib_entry;
1143 struct mlxsw_sp_vr *vr;
1144 int err;
1145
1146 vr = mlxsw_sp_vr_get(mlxsw_sp, fib4->dst_len, fib4->tb_id,
1147 MLXSW_SP_L3_PROTO_IPV4);
1148 if (IS_ERR(vr))
1149 return PTR_ERR(vr);
1150
1151 fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fib4->dst,
1152 sizeof(fib4->dst), fib4->dst_len);
1153 if (!fib_entry) {
1154 err = -ENOMEM;
1155 goto err_fib_entry_create;
1156 }
1157 fib_entry->vr = vr;
1158
1159 err = mlxsw_sp_router_fib4_entry_init(mlxsw_sp, fib4, fib_entry);
1160 if (err)
1161 goto err_fib4_entry_init;
1162
1163 info = kmalloc(sizeof(*info), GFP_KERNEL);
1164 if (!info) {
1165 err = -ENOMEM;
1166 goto err_alloc_info;
1167 }
1168 info->mlxsw_sp = mlxsw_sp;
1169 info->fib_entry = fib_entry;
1170 switchdev_trans_item_enqueue(trans, info,
1171 mlxsw_sp_router_fib4_add_info_destroy,
1172 &info->tritem);
1173 return 0;
1174
1175err_alloc_info:
1176err_fib4_entry_init:
1177 mlxsw_sp_fib_entry_destroy(fib_entry);
1178err_fib_entry_create:
1179 mlxsw_sp_vr_put(mlxsw_sp, vr);
1180 return err;
1181}
1182
1183static int
1184mlxsw_sp_router_fib4_add_commit(struct mlxsw_sp_port *mlxsw_sp_port,
1185 const struct switchdev_obj_ipv4_fib *fib4,
1186 struct switchdev_trans *trans)
1187{
1188 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1189 struct mlxsw_sp_router_fib4_add_info *info;
1190 struct mlxsw_sp_fib_entry *fib_entry;
1191 struct mlxsw_sp_vr *vr;
1192 int err;
1193
1194 info = switchdev_trans_item_dequeue(trans);
1195 fib_entry = info->fib_entry;
1196 kfree(info);
1197
1198 vr = fib_entry->vr;
1199 err = mlxsw_sp_fib_entry_insert(fib_entry->vr->fib, fib_entry);
1200 if (err)
1201 goto err_fib_entry_insert;
1202 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1203 if (err)
1204 goto err_fib_entry_add;
1205 return 0;
1206
1207err_fib_entry_add:
1208 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1209err_fib_entry_insert:
1210 mlxsw_sp_fib_entry_destroy(fib_entry);
1211 mlxsw_sp_vr_put(mlxsw_sp, vr);
1212 return err;
1213}
1214
1215int mlxsw_sp_router_fib4_add(struct mlxsw_sp_port *mlxsw_sp_port,
1216 const struct switchdev_obj_ipv4_fib *fib4,
1217 struct switchdev_trans *trans)
1218{
1219 if (switchdev_trans_ph_prepare(trans))
1220 return mlxsw_sp_router_fib4_add_prepare(mlxsw_sp_port,
1221 fib4, trans);
1222 return mlxsw_sp_router_fib4_add_commit(mlxsw_sp_port,
1223 fib4, trans);
1224}
1225
1226int mlxsw_sp_router_fib4_del(struct mlxsw_sp_port *mlxsw_sp_port,
1227 const struct switchdev_obj_ipv4_fib *fib4)
1228{
1229 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1230 struct mlxsw_sp_fib_entry *fib_entry;
1231 struct mlxsw_sp_vr *vr;
1232
1233 vr = mlxsw_sp_vr_find(mlxsw_sp, fib4->tb_id, MLXSW_SP_L3_PROTO_IPV4);
1234 if (!vr) {
1235 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find virtual router for FIB4 entry being removed.\n");
1236 return -ENOENT;
1237 }
1238 fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fib4->dst,
1239 sizeof(fib4->dst), fib4->dst_len);
1240 if (!fib_entry) {
1241 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find FIB4 entry being removed.\n");
1242 return PTR_ERR(vr);
1243 }
1244 mlxsw_sp_fib_entry_del(mlxsw_sp_port->mlxsw_sp, fib_entry);
1245 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1246 mlxsw_sp_fib_entry_destroy(fib_entry);
1247 mlxsw_sp_vr_put(mlxsw_sp, vr);
1248 return 0;
1249}