blob: dc13178b6f338df254f194a69f9d0caf5476880d [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 Pirkoa7ff87a2016-07-05 11:27:50 +0200120struct mlxsw_sp_nexthop_group;
121
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200122struct mlxsw_sp_fib_entry {
123 struct rhash_head ht_node;
124 struct mlxsw_sp_fib_key key;
Jiri Pirko61c503f2016-07-04 08:23:11 +0200125 enum mlxsw_sp_fib_entry_type type;
126 u8 added:1;
127 u16 rif; /* used for action local */
128 struct mlxsw_sp_vr *vr;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200129 struct list_head nexthop_group_node;
130 struct mlxsw_sp_nexthop_group *nh_group;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200131};
132
133struct mlxsw_sp_fib {
134 struct rhashtable ht;
135 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
136 struct mlxsw_sp_prefix_usage prefix_usage;
137};
138
139static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
140 .key_offset = offsetof(struct mlxsw_sp_fib_entry, key),
141 .head_offset = offsetof(struct mlxsw_sp_fib_entry, ht_node),
142 .key_len = sizeof(struct mlxsw_sp_fib_key),
143 .automatic_shrinking = true,
144};
145
146static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib *fib,
147 struct mlxsw_sp_fib_entry *fib_entry)
148{
149 unsigned char prefix_len = fib_entry->key.prefix_len;
150 int err;
151
152 err = rhashtable_insert_fast(&fib->ht, &fib_entry->ht_node,
153 mlxsw_sp_fib_ht_params);
154 if (err)
155 return err;
156 if (fib->prefix_ref_count[prefix_len]++ == 0)
157 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
158 return 0;
159}
160
161static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib *fib,
162 struct mlxsw_sp_fib_entry *fib_entry)
163{
164 unsigned char prefix_len = fib_entry->key.prefix_len;
165
166 if (--fib->prefix_ref_count[prefix_len] == 0)
167 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
168 rhashtable_remove_fast(&fib->ht, &fib_entry->ht_node,
169 mlxsw_sp_fib_ht_params);
170}
171
172static struct mlxsw_sp_fib_entry *
173mlxsw_sp_fib_entry_create(struct mlxsw_sp_fib *fib, const void *addr,
174 size_t addr_len, unsigned char prefix_len)
175{
176 struct mlxsw_sp_fib_entry *fib_entry;
177
178 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
179 if (!fib_entry)
180 return NULL;
181 memcpy(fib_entry->key.addr, addr, addr_len);
182 fib_entry->key.prefix_len = prefix_len;
183 return fib_entry;
184}
185
186static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry *fib_entry)
187{
188 kfree(fib_entry);
189}
190
191static struct mlxsw_sp_fib_entry *
192mlxsw_sp_fib_entry_lookup(struct mlxsw_sp_fib *fib, const void *addr,
193 size_t addr_len, unsigned char prefix_len)
194{
195 struct mlxsw_sp_fib_key key = {{ 0 } };
196
197 memcpy(key.addr, addr, addr_len);
198 key.prefix_len = prefix_len;
199 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
200}
201
202static struct mlxsw_sp_fib *mlxsw_sp_fib_create(void)
203{
204 struct mlxsw_sp_fib *fib;
205 int err;
206
207 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
208 if (!fib)
209 return ERR_PTR(-ENOMEM);
210 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
211 if (err)
212 goto err_rhashtable_init;
213 return fib;
214
215err_rhashtable_init:
216 kfree(fib);
217 return ERR_PTR(err);
218}
219
220static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
221{
222 rhashtable_destroy(&fib->ht);
223 kfree(fib);
224}
225
Jiri Pirko53342022016-07-04 08:23:08 +0200226static struct mlxsw_sp_lpm_tree *
227mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp, bool one_reserved)
228{
229 static struct mlxsw_sp_lpm_tree *lpm_tree;
230 int i;
231
232 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
233 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
234 if (lpm_tree->ref_count == 0) {
235 if (one_reserved)
236 one_reserved = false;
237 else
238 return lpm_tree;
239 }
240 }
241 return NULL;
242}
243
244static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
245 struct mlxsw_sp_lpm_tree *lpm_tree)
246{
247 char ralta_pl[MLXSW_REG_RALTA_LEN];
248
249 mlxsw_reg_ralta_pack(ralta_pl, true, lpm_tree->proto, lpm_tree->id);
250 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
251}
252
253static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
254 struct mlxsw_sp_lpm_tree *lpm_tree)
255{
256 char ralta_pl[MLXSW_REG_RALTA_LEN];
257
258 mlxsw_reg_ralta_pack(ralta_pl, false, lpm_tree->proto, lpm_tree->id);
259 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
260}
261
262static int
263mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
264 struct mlxsw_sp_prefix_usage *prefix_usage,
265 struct mlxsw_sp_lpm_tree *lpm_tree)
266{
267 char ralst_pl[MLXSW_REG_RALST_LEN];
268 u8 root_bin = 0;
269 u8 prefix;
270 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
271
272 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
273 root_bin = prefix;
274
275 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
276 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
277 if (prefix == 0)
278 continue;
279 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
280 MLXSW_REG_RALST_BIN_NO_CHILD);
281 last_prefix = prefix;
282 }
283 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
284}
285
286static struct mlxsw_sp_lpm_tree *
287mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
288 struct mlxsw_sp_prefix_usage *prefix_usage,
289 enum mlxsw_sp_l3proto proto, bool one_reserved)
290{
291 struct mlxsw_sp_lpm_tree *lpm_tree;
292 int err;
293
294 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp, one_reserved);
295 if (!lpm_tree)
296 return ERR_PTR(-EBUSY);
297 lpm_tree->proto = proto;
298 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
299 if (err)
300 return ERR_PTR(err);
301
302 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
303 lpm_tree);
304 if (err)
305 goto err_left_struct_set;
306 return lpm_tree;
307
308err_left_struct_set:
309 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
310 return ERR_PTR(err);
311}
312
313static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
314 struct mlxsw_sp_lpm_tree *lpm_tree)
315{
316 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
317}
318
319static struct mlxsw_sp_lpm_tree *
320mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
321 struct mlxsw_sp_prefix_usage *prefix_usage,
322 enum mlxsw_sp_l3proto proto, bool one_reserved)
323{
324 struct mlxsw_sp_lpm_tree *lpm_tree;
325 int i;
326
327 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
328 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
329 if (lpm_tree->proto == proto &&
330 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
331 prefix_usage))
332 goto inc_ref_count;
333 }
334 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
335 proto, one_reserved);
336 if (IS_ERR(lpm_tree))
337 return lpm_tree;
338
339inc_ref_count:
340 lpm_tree->ref_count++;
341 return lpm_tree;
342}
343
344static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
345 struct mlxsw_sp_lpm_tree *lpm_tree)
346{
347 if (--lpm_tree->ref_count == 0)
348 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
349 return 0;
350}
351
352static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
353{
354 struct mlxsw_sp_lpm_tree *lpm_tree;
355 int i;
356
357 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
358 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
359 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
360 }
361}
362
Jiri Pirko6b75c482016-07-04 08:23:09 +0200363static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
364{
365 struct mlxsw_sp_vr *vr;
366 int i;
367
368 for (i = 0; i < MLXSW_SP_VIRTUAL_ROUTER_MAX; i++) {
369 vr = &mlxsw_sp->router.vrs[i];
370 if (!vr->used)
371 return vr;
372 }
373 return NULL;
374}
375
376static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
377 struct mlxsw_sp_vr *vr)
378{
379 char raltb_pl[MLXSW_REG_RALTB_LEN];
380
381 mlxsw_reg_raltb_pack(raltb_pl, vr->id, vr->proto, vr->lpm_tree->id);
382 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
383}
384
385static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
386 struct mlxsw_sp_vr *vr)
387{
388 char raltb_pl[MLXSW_REG_RALTB_LEN];
389
390 /* Bind to tree 0 which is default */
391 mlxsw_reg_raltb_pack(raltb_pl, vr->id, vr->proto, 0);
392 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
393}
394
395static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
396{
397 /* For our purpose, squash main and local table into one */
398 if (tb_id == RT_TABLE_LOCAL)
399 tb_id = RT_TABLE_MAIN;
400 return tb_id;
401}
402
403static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
404 u32 tb_id,
405 enum mlxsw_sp_l3proto proto)
406{
407 struct mlxsw_sp_vr *vr;
408 int i;
409
410 tb_id = mlxsw_sp_fix_tb_id(tb_id);
411 for (i = 0; i < MLXSW_SP_VIRTUAL_ROUTER_MAX; i++) {
412 vr = &mlxsw_sp->router.vrs[i];
413 if (vr->used && vr->proto == proto && vr->tb_id == tb_id)
414 return vr;
415 }
416 return NULL;
417}
418
419static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
420 unsigned char prefix_len,
421 u32 tb_id,
422 enum mlxsw_sp_l3proto proto)
423{
424 struct mlxsw_sp_prefix_usage req_prefix_usage;
425 struct mlxsw_sp_lpm_tree *lpm_tree;
426 struct mlxsw_sp_vr *vr;
427 int err;
428
429 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
430 if (!vr)
431 return ERR_PTR(-EBUSY);
432 vr->fib = mlxsw_sp_fib_create();
433 if (IS_ERR(vr->fib))
434 return ERR_CAST(vr->fib);
435
436 vr->proto = proto;
437 vr->tb_id = tb_id;
438 mlxsw_sp_prefix_usage_zero(&req_prefix_usage);
439 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
440 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
441 proto, true);
442 if (IS_ERR(lpm_tree)) {
443 err = PTR_ERR(lpm_tree);
444 goto err_tree_get;
445 }
446 vr->lpm_tree = lpm_tree;
447 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
448 if (err)
449 goto err_tree_bind;
450
451 vr->used = true;
452 return vr;
453
454err_tree_bind:
455 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
456err_tree_get:
457 mlxsw_sp_fib_destroy(vr->fib);
458
459 return ERR_PTR(err);
460}
461
462static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
463 struct mlxsw_sp_vr *vr)
464{
465 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
466 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
467 mlxsw_sp_fib_destroy(vr->fib);
468 vr->used = false;
469}
470
471static int
472mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr,
473 struct mlxsw_sp_prefix_usage *req_prefix_usage)
474{
475 struct mlxsw_sp_lpm_tree *lpm_tree;
476
477 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage,
478 &vr->lpm_tree->prefix_usage))
479 return 0;
480
481 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
482 vr->proto, false);
483 if (IS_ERR(lpm_tree)) {
484 /* We failed to get a tree according to the required
485 * prefix usage. However, the current tree might be still good
486 * for us if our requirement is subset of the prefixes used
487 * in the tree.
488 */
489 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
490 &vr->lpm_tree->prefix_usage))
491 return 0;
492 return PTR_ERR(lpm_tree);
493 }
494
495 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
496 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
497 vr->lpm_tree = lpm_tree;
498 return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
499}
500
501static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp,
502 unsigned char prefix_len,
503 u32 tb_id,
504 enum mlxsw_sp_l3proto proto)
505{
506 struct mlxsw_sp_vr *vr;
507 int err;
508
509 tb_id = mlxsw_sp_fix_tb_id(tb_id);
510 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id, proto);
511 if (!vr) {
512 vr = mlxsw_sp_vr_create(mlxsw_sp, prefix_len, tb_id, proto);
513 if (IS_ERR(vr))
514 return vr;
515 } else {
516 struct mlxsw_sp_prefix_usage req_prefix_usage;
517
518 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage,
519 &vr->fib->prefix_usage);
520 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
521 /* Need to replace LPM tree in case new prefix is required. */
522 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
523 &req_prefix_usage);
524 if (err)
525 return ERR_PTR(err);
526 }
527 return vr;
528}
529
530static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
531{
532 /* Destroy virtual router entity in case the associated FIB is empty
533 * and allow it to be used for other tables in future. Otherwise,
534 * check if some prefix usage did not disappear and change tree if
535 * that is the case. Note that in case new, smaller tree cannot be
536 * allocated, the original one will be kept being used.
537 */
538 if (mlxsw_sp_prefix_usage_none(&vr->fib->prefix_usage))
539 mlxsw_sp_vr_destroy(mlxsw_sp, vr);
540 else
541 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
542 &vr->fib->prefix_usage);
543}
544
545static void mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
546{
547 struct mlxsw_sp_vr *vr;
548 int i;
549
550 for (i = 0; i < MLXSW_SP_VIRTUAL_ROUTER_MAX; i++) {
551 vr = &mlxsw_sp->router.vrs[i];
552 vr->id = i;
553 }
554}
555
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200556struct mlxsw_sp_neigh_key {
557 unsigned char addr[sizeof(struct in6_addr)];
558 struct net_device *dev;
559};
560
561struct mlxsw_sp_neigh_entry {
562 struct rhash_head ht_node;
563 struct mlxsw_sp_neigh_key key;
564 u16 rif;
565 struct neighbour *n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200566 bool offloaded;
567 struct delayed_work dw;
568 struct mlxsw_sp_port *mlxsw_sp_port;
569 unsigned char ha[ETH_ALEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200570 struct list_head nexthop_list; /* list of nexthops using
571 * this neigh entry
572 */
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200573};
574
575static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
576 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
577 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
578 .key_len = sizeof(struct mlxsw_sp_neigh_key),
579};
580
581static int
582mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
583 struct mlxsw_sp_neigh_entry *neigh_entry)
584{
585 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
586 &neigh_entry->ht_node,
587 mlxsw_sp_neigh_ht_params);
588}
589
590static void
591mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
592 struct mlxsw_sp_neigh_entry *neigh_entry)
593{
594 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
595 &neigh_entry->ht_node,
596 mlxsw_sp_neigh_ht_params);
597}
598
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200599static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work);
600
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200601static struct mlxsw_sp_neigh_entry *
602mlxsw_sp_neigh_entry_create(const void *addr, size_t addr_len,
603 struct net_device *dev, u16 rif,
604 struct neighbour *n)
605{
606 struct mlxsw_sp_neigh_entry *neigh_entry;
607
608 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_ATOMIC);
609 if (!neigh_entry)
610 return NULL;
611 memcpy(neigh_entry->key.addr, addr, addr_len);
612 neigh_entry->key.dev = dev;
613 neigh_entry->rif = rif;
614 neigh_entry->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200615 INIT_DELAYED_WORK(&neigh_entry->dw, mlxsw_sp_router_neigh_update_hw);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200616 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200617 return neigh_entry;
618}
619
620static void
621mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp_neigh_entry *neigh_entry)
622{
623 kfree(neigh_entry);
624}
625
626static struct mlxsw_sp_neigh_entry *
627mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, const void *addr,
628 size_t addr_len, struct net_device *dev)
629{
630 struct mlxsw_sp_neigh_key key = {{ 0 } };
631
632 memcpy(key.addr, addr, addr_len);
633 key.dev = dev;
634 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
635 &key, mlxsw_sp_neigh_ht_params);
636}
637
638int mlxsw_sp_router_neigh_construct(struct net_device *dev,
639 struct neighbour *n)
640{
641 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
642 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
643 struct mlxsw_sp_neigh_entry *neigh_entry;
644 struct mlxsw_sp_rif *r;
645 u32 dip;
646 int err;
647
648 if (n->tbl != &arp_tbl)
649 return 0;
650
651 dip = ntohl(*((__be32 *) n->primary_key));
652 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
653 n->dev);
654 if (neigh_entry) {
655 WARN_ON(neigh_entry->n != n);
656 return 0;
657 }
658
659 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
660 if (WARN_ON(!r))
661 return -EINVAL;
662
663 neigh_entry = mlxsw_sp_neigh_entry_create(&dip, sizeof(dip), n->dev,
664 r->rif, n);
665 if (!neigh_entry)
666 return -ENOMEM;
667 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
668 if (err)
669 goto err_neigh_entry_insert;
670 return 0;
671
672err_neigh_entry_insert:
673 mlxsw_sp_neigh_entry_destroy(neigh_entry);
674 return err;
675}
676
677void mlxsw_sp_router_neigh_destroy(struct net_device *dev,
678 struct neighbour *n)
679{
680 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
681 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
682 struct mlxsw_sp_neigh_entry *neigh_entry;
683 u32 dip;
684
685 if (n->tbl != &arp_tbl)
686 return;
687
688 dip = ntohl(*((__be32 *) n->primary_key));
689 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
690 n->dev);
691 if (!neigh_entry)
692 return;
693 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
694 mlxsw_sp_neigh_entry_destroy(neigh_entry);
695}
696
Yotam Gigic723c7352016-07-05 11:27:43 +0200697static void
698mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
699{
700 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
701
702 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
703}
704
705static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
706 char *rauhtd_pl,
707 int ent_index)
708{
709 struct net_device *dev;
710 struct neighbour *n;
711 __be32 dipn;
712 u32 dip;
713 u16 rif;
714
715 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
716
717 if (!mlxsw_sp->rifs[rif]) {
718 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
719 return;
720 }
721
722 dipn = htonl(dip);
723 dev = mlxsw_sp->rifs[rif]->dev;
724 n = neigh_lookup(&arp_tbl, &dipn, dev);
725 if (!n) {
726 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
727 &dip);
728 return;
729 }
730
731 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
732 neigh_event_send(n, NULL);
733 neigh_release(n);
734}
735
736static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
737 char *rauhtd_pl,
738 int rec_index)
739{
740 u8 num_entries;
741 int i;
742
743 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
744 rec_index);
745 /* Hardware starts counting at 0, so add 1. */
746 num_entries++;
747
748 /* Each record consists of several neighbour entries. */
749 for (i = 0; i < num_entries; i++) {
750 int ent_index;
751
752 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
753 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
754 ent_index);
755 }
756
757}
758
759static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
760 char *rauhtd_pl, int rec_index)
761{
762 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
763 case MLXSW_REG_RAUHTD_TYPE_IPV4:
764 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
765 rec_index);
766 break;
767 case MLXSW_REG_RAUHTD_TYPE_IPV6:
768 WARN_ON_ONCE(1);
769 break;
770 }
771}
772
773static void
774mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
775{
776 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
777
778 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
779 msecs_to_jiffies(interval));
780}
781
782static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
783{
784 struct mlxsw_sp *mlxsw_sp;
785 char *rauhtd_pl;
786 u8 num_rec;
787 int i, err;
788
789 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
790 if (!rauhtd_pl)
791 return;
792
793 mlxsw_sp = container_of(work, struct mlxsw_sp,
794 router.neighs_update.dw.work);
795
796 /* Make sure the neighbour's netdev isn't removed in the
797 * process.
798 */
799 rtnl_lock();
800 do {
801 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
802 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
803 rauhtd_pl);
804 if (err) {
805 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
806 break;
807 }
808 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
809 for (i = 0; i < num_rec; i++)
810 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
811 i);
812 } while (num_rec);
813 rtnl_unlock();
814
815 kfree(rauhtd_pl);
816 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
817}
818
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200819static void
820mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
821 struct mlxsw_sp_neigh_entry *neigh_entry,
822 bool removing);
823
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200824static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work)
825{
826 struct mlxsw_sp_neigh_entry *neigh_entry =
827 container_of(work, struct mlxsw_sp_neigh_entry, dw.work);
828 struct neighbour *n = neigh_entry->n;
829 struct mlxsw_sp_port *mlxsw_sp_port = neigh_entry->mlxsw_sp_port;
830 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
831 char rauht_pl[MLXSW_REG_RAUHT_LEN];
832 struct net_device *dev;
833 bool entry_connected;
834 u8 nud_state;
835 bool updating;
836 bool removing;
837 bool adding;
838 u32 dip;
839 int err;
840
841 read_lock_bh(&n->lock);
842 dip = ntohl(*((__be32 *) n->primary_key));
843 memcpy(neigh_entry->ha, n->ha, sizeof(neigh_entry->ha));
844 nud_state = n->nud_state;
845 dev = n->dev;
846 read_unlock_bh(&n->lock);
847
848 entry_connected = nud_state & NUD_VALID;
849 adding = (!neigh_entry->offloaded) && entry_connected;
850 updating = neigh_entry->offloaded && entry_connected;
851 removing = neigh_entry->offloaded && !entry_connected;
852
853 if (adding || updating) {
854 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_ADD,
855 neigh_entry->rif,
856 neigh_entry->ha, dip);
857 err = mlxsw_reg_write(mlxsw_sp->core,
858 MLXSW_REG(rauht), rauht_pl);
859 if (err) {
860 netdev_err(dev, "Could not add neigh %pI4h\n", &dip);
861 neigh_entry->offloaded = false;
862 } else {
863 neigh_entry->offloaded = true;
864 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200865 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, false);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200866 } else if (removing) {
867 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE,
868 neigh_entry->rif,
869 neigh_entry->ha, dip);
870 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht),
871 rauht_pl);
872 if (err) {
873 netdev_err(dev, "Could not delete neigh %pI4h\n", &dip);
874 neigh_entry->offloaded = true;
875 } else {
876 neigh_entry->offloaded = false;
877 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200878 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, true);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200879 }
880
881 neigh_release(n);
882 mlxsw_sp_port_dev_put(mlxsw_sp_port);
883}
884
Yotam Gigic723c7352016-07-05 11:27:43 +0200885static int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
886 unsigned long event, void *ptr)
887{
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200888 struct mlxsw_sp_neigh_entry *neigh_entry;
Yotam Gigic723c7352016-07-05 11:27:43 +0200889 struct mlxsw_sp_port *mlxsw_sp_port;
890 struct mlxsw_sp *mlxsw_sp;
891 unsigned long interval;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200892 struct net_device *dev;
Yotam Gigic723c7352016-07-05 11:27:43 +0200893 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200894 struct neighbour *n;
895 u32 dip;
Yotam Gigic723c7352016-07-05 11:27:43 +0200896
897 switch (event) {
898 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
899 p = ptr;
900
901 /* We don't care about changes in the default table. */
902 if (!p->dev || p->tbl != &arp_tbl)
903 return NOTIFY_DONE;
904
905 /* We are in atomic context and can't take RTNL mutex,
906 * so use RCU variant to walk the device chain.
907 */
908 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
909 if (!mlxsw_sp_port)
910 return NOTIFY_DONE;
911
912 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
913 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
914 mlxsw_sp->router.neighs_update.interval = interval;
915
916 mlxsw_sp_port_dev_put(mlxsw_sp_port);
917 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200918 case NETEVENT_NEIGH_UPDATE:
919 n = ptr;
920 dev = n->dev;
921
922 if (n->tbl != &arp_tbl)
923 return NOTIFY_DONE;
924
925 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(dev);
926 if (!mlxsw_sp_port)
927 return NOTIFY_DONE;
928
929 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
930 dip = ntohl(*((__be32 *) n->primary_key));
931 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp,
932 &dip,
933 sizeof(__be32),
934 dev);
935 if (WARN_ON(!neigh_entry) || WARN_ON(neigh_entry->n != n)) {
936 mlxsw_sp_port_dev_put(mlxsw_sp_port);
937 return NOTIFY_DONE;
938 }
939 neigh_entry->mlxsw_sp_port = mlxsw_sp_port;
940
941 /* Take a reference to ensure the neighbour won't be
942 * destructed until we drop the reference in delayed
943 * work.
944 */
945 neigh_clone(n);
946 if (!mlxsw_core_schedule_dw(&neigh_entry->dw, 0)) {
947 neigh_release(n);
948 mlxsw_sp_port_dev_put(mlxsw_sp_port);
949 }
950 break;
Yotam Gigic723c7352016-07-05 11:27:43 +0200951 }
952
953 return NOTIFY_DONE;
954}
955
956static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly = {
957 .notifier_call = mlxsw_sp_router_netevent_event,
958};
959
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200960static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
961{
Yotam Gigic723c7352016-07-05 11:27:43 +0200962 int err;
963
964 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
965 &mlxsw_sp_neigh_ht_params);
966 if (err)
967 return err;
968
969 /* Initialize the polling interval according to the default
970 * table.
971 */
972 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
973
974 err = register_netevent_notifier(&mlxsw_sp_router_netevent_nb);
975 if (err)
976 goto err_register_netevent_notifier;
977
978 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
979 mlxsw_sp_router_neighs_update_work);
980 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
981
982 return 0;
983
984err_register_netevent_notifier:
985 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
986 return err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200987}
988
989static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
990{
Yotam Gigic723c7352016-07-05 11:27:43 +0200991 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
992 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200993 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
994}
995
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200996struct mlxsw_sp_nexthop {
997 struct list_head neigh_list_node; /* member of neigh entry list */
998 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
999 * this belongs to
1000 */
1001 u8 should_offload:1, /* set indicates this neigh is connected and
1002 * should be put to KVD linear area of this group.
1003 */
1004 offloaded:1, /* set in case the neigh is actually put into
1005 * KVD linear area of this group.
1006 */
1007 update:1; /* set indicates that MAC of this neigh should be
1008 * updated in HW
1009 */
1010 struct mlxsw_sp_neigh_entry *neigh_entry;
1011};
1012
1013struct mlxsw_sp_nexthop_group {
1014 struct list_head list; /* node in mlxsw->router.nexthop_group_list */
1015 struct list_head fib_list; /* list of fib entries that use this group */
1016 u8 adj_index_valid:1;
1017 u32 adj_index;
1018 u16 ecmp_size;
1019 u16 count;
1020 struct mlxsw_sp_nexthop nexthops[0];
1021};
1022
1023static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
1024 struct mlxsw_sp_vr *vr,
1025 u32 adj_index, u16 ecmp_size,
1026 u32 new_adj_index,
1027 u16 new_ecmp_size)
1028{
1029 char raleu_pl[MLXSW_REG_RALEU_LEN];
1030
1031 mlxsw_reg_raleu_pack(raleu_pl, vr->proto, vr->id,
1032 adj_index, ecmp_size,
1033 new_adj_index, new_ecmp_size);
1034 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1035}
1036
1037static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1038 struct mlxsw_sp_nexthop_group *nh_grp,
1039 u32 old_adj_index, u16 old_ecmp_size)
1040{
1041 struct mlxsw_sp_fib_entry *fib_entry;
1042 struct mlxsw_sp_vr *vr = NULL;
1043 int err;
1044
1045 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1046 if (vr == fib_entry->vr)
1047 continue;
1048 vr = fib_entry->vr;
1049 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, vr,
1050 old_adj_index,
1051 old_ecmp_size,
1052 nh_grp->adj_index,
1053 nh_grp->ecmp_size);
1054 if (err)
1055 return err;
1056 }
1057 return 0;
1058}
1059
1060static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1061 struct mlxsw_sp_nexthop *nh)
1062{
1063 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1064 char ratr_pl[MLXSW_REG_RATR_LEN];
1065
1066 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1067 true, adj_index, neigh_entry->rif);
1068 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1069 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1070}
1071
1072static int
1073mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
1074 struct mlxsw_sp_nexthop_group *nh_grp)
1075{
1076 u32 adj_index = nh_grp->adj_index; /* base */
1077 struct mlxsw_sp_nexthop *nh;
1078 int i;
1079 int err;
1080
1081 for (i = 0; i < nh_grp->count; i++) {
1082 nh = &nh_grp->nexthops[i];
1083
1084 if (!nh->should_offload) {
1085 nh->offloaded = 0;
1086 continue;
1087 }
1088
1089 if (nh->update) {
1090 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1091 adj_index, nh);
1092 if (err)
1093 return err;
1094 nh->update = 0;
1095 nh->offloaded = 1;
1096 }
1097 adj_index++;
1098 }
1099 return 0;
1100}
1101
1102static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1103 struct mlxsw_sp_fib_entry *fib_entry);
1104
1105static int
1106mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1107 struct mlxsw_sp_nexthop_group *nh_grp)
1108{
1109 struct mlxsw_sp_fib_entry *fib_entry;
1110 int err;
1111
1112 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1113 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1114 if (err)
1115 return err;
1116 }
1117 return 0;
1118}
1119
1120static void
1121mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1122 struct mlxsw_sp_nexthop_group *nh_grp)
1123{
1124 struct mlxsw_sp_nexthop *nh;
1125 bool offload_change = false;
1126 u32 adj_index;
1127 u16 ecmp_size = 0;
1128 bool old_adj_index_valid;
1129 u32 old_adj_index;
1130 u16 old_ecmp_size;
1131 int ret;
1132 int i;
1133 int err;
1134
1135 for (i = 0; i < nh_grp->count; i++) {
1136 nh = &nh_grp->nexthops[i];
1137
1138 if (nh->should_offload ^ nh->offloaded) {
1139 offload_change = true;
1140 if (nh->should_offload)
1141 nh->update = 1;
1142 }
1143 if (nh->should_offload)
1144 ecmp_size++;
1145 }
1146 if (!offload_change) {
1147 /* Nothing was added or removed, so no need to reallocate. Just
1148 * update MAC on existing adjacency indexes.
1149 */
1150 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp);
1151 if (err) {
1152 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1153 goto set_trap;
1154 }
1155 return;
1156 }
1157 if (!ecmp_size)
1158 /* No neigh of this group is connected so we just set
1159 * the trap and let everthing flow through kernel.
1160 */
1161 goto set_trap;
1162
1163 ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size);
1164 if (ret < 0) {
1165 /* We ran out of KVD linear space, just set the
1166 * trap and let everything flow through kernel.
1167 */
1168 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1169 goto set_trap;
1170 }
1171 adj_index = ret;
1172 old_adj_index_valid = nh_grp->adj_index_valid;
1173 old_adj_index = nh_grp->adj_index;
1174 old_ecmp_size = nh_grp->ecmp_size;
1175 nh_grp->adj_index_valid = 1;
1176 nh_grp->adj_index = adj_index;
1177 nh_grp->ecmp_size = ecmp_size;
1178 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp);
1179 if (err) {
1180 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1181 goto set_trap;
1182 }
1183
1184 if (!old_adj_index_valid) {
1185 /* The trap was set for fib entries, so we have to call
1186 * fib entry update to unset it and use adjacency index.
1187 */
1188 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1189 if (err) {
1190 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1191 goto set_trap;
1192 }
1193 return;
1194 }
1195
1196 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1197 old_adj_index, old_ecmp_size);
1198 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1199 if (err) {
1200 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1201 goto set_trap;
1202 }
1203 return;
1204
1205set_trap:
1206 old_adj_index_valid = nh_grp->adj_index_valid;
1207 nh_grp->adj_index_valid = 0;
1208 for (i = 0; i < nh_grp->count; i++) {
1209 nh = &nh_grp->nexthops[i];
1210 nh->offloaded = 0;
1211 }
1212 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1213 if (err)
1214 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1215 if (old_adj_index_valid)
1216 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1217}
1218
1219static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1220 bool removing)
1221{
1222 if (!removing && !nh->should_offload)
1223 nh->should_offload = 1;
1224 else if (removing && nh->offloaded)
1225 nh->should_offload = 0;
1226 nh->update = 1;
1227}
1228
1229static void
1230mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1231 struct mlxsw_sp_neigh_entry *neigh_entry,
1232 bool removing)
1233{
1234 struct mlxsw_sp_nexthop *nh;
1235
1236 /* Take RTNL mutex here to prevent lists from changes */
1237 rtnl_lock();
1238 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1239 neigh_list_node) {
1240 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1241 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1242 }
1243 rtnl_unlock();
1244}
1245
1246static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp,
1247 struct mlxsw_sp_nexthop_group *nh_grp,
1248 struct mlxsw_sp_nexthop *nh,
1249 struct fib_nh *fib_nh)
1250{
1251 struct mlxsw_sp_neigh_entry *neigh_entry;
1252 u32 gwip = ntohl(fib_nh->nh_gw);
1253 struct net_device *dev = fib_nh->nh_dev;
1254 struct neighbour *n;
1255 u8 nud_state;
1256
1257 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &gwip,
1258 sizeof(gwip), dev);
1259 if (!neigh_entry) {
1260 __be32 gwipn = htonl(gwip);
1261
1262 n = neigh_create(&arp_tbl, &gwipn, dev);
1263 if (IS_ERR(n))
1264 return PTR_ERR(n);
1265 neigh_event_send(n, NULL);
1266 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &gwip,
1267 sizeof(gwip), dev);
1268 if (!neigh_entry) {
1269 neigh_release(n);
1270 return -EINVAL;
1271 }
1272 } else {
1273 /* Take a reference of neigh here ensuring that neigh would
1274 * not be detructed before the nexthop entry is finished.
1275 * The second branch takes the reference in neith_create()
1276 */
1277 n = neigh_entry->n;
1278 neigh_clone(n);
1279 }
1280 nh->nh_grp = nh_grp;
1281 nh->neigh_entry = neigh_entry;
1282 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1283 read_lock_bh(&n->lock);
1284 nud_state = n->nud_state;
1285 read_unlock_bh(&n->lock);
1286 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID));
1287
1288 return 0;
1289}
1290
1291static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1292 struct mlxsw_sp_nexthop *nh)
1293{
1294 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1295
1296 list_del(&nh->neigh_list_node);
1297 neigh_release(neigh_entry->n);
1298}
1299
1300static struct mlxsw_sp_nexthop_group *
1301mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1302{
1303 struct mlxsw_sp_nexthop_group *nh_grp;
1304 struct mlxsw_sp_nexthop *nh;
1305 struct fib_nh *fib_nh;
1306 size_t alloc_size;
1307 int i;
1308 int err;
1309
1310 alloc_size = sizeof(*nh_grp) +
1311 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1312 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1313 if (!nh_grp)
1314 return ERR_PTR(-ENOMEM);
1315 INIT_LIST_HEAD(&nh_grp->fib_list);
1316 nh_grp->count = fi->fib_nhs;
1317 for (i = 0; i < nh_grp->count; i++) {
1318 nh = &nh_grp->nexthops[i];
1319 fib_nh = &fi->fib_nh[i];
1320 err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh);
1321 if (err)
1322 goto err_nexthop_init;
1323 }
1324 list_add_tail(&nh_grp->list, &mlxsw_sp->router.nexthop_group_list);
1325 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1326 return nh_grp;
1327
1328err_nexthop_init:
1329 for (i--; i >= 0; i--)
1330 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1331 kfree(nh_grp);
1332 return ERR_PTR(err);
1333}
1334
1335static void
1336mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1337 struct mlxsw_sp_nexthop_group *nh_grp)
1338{
1339 struct mlxsw_sp_nexthop *nh;
1340 int i;
1341
1342 list_del(&nh_grp->list);
1343 for (i = 0; i < nh_grp->count; i++) {
1344 nh = &nh_grp->nexthops[i];
1345 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1346 }
1347 kfree(nh_grp);
1348}
1349
1350static bool mlxsw_sp_nexthop_match(struct mlxsw_sp_nexthop *nh,
1351 struct fib_info *fi)
1352{
1353 int i;
1354
1355 for (i = 0; i < fi->fib_nhs; i++) {
1356 struct fib_nh *fib_nh = &fi->fib_nh[i];
1357 u32 gwip = ntohl(fib_nh->nh_gw);
1358
1359 if (memcmp(nh->neigh_entry->key.addr,
1360 &gwip, sizeof(u32)) == 0 &&
1361 nh->neigh_entry->key.dev == fib_nh->nh_dev)
1362 return true;
1363 }
1364 return false;
1365}
1366
1367static bool mlxsw_sp_nexthop_group_match(struct mlxsw_sp_nexthop_group *nh_grp,
1368 struct fib_info *fi)
1369{
1370 int i;
1371
1372 if (nh_grp->count != fi->fib_nhs)
1373 return false;
1374 for (i = 0; i < nh_grp->count; i++) {
1375 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
1376
1377 if (!mlxsw_sp_nexthop_match(nh, fi))
1378 return false;
1379 }
1380 return true;
1381}
1382
1383static struct mlxsw_sp_nexthop_group *
1384mlxsw_sp_nexthop_group_find(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1385{
1386 struct mlxsw_sp_nexthop_group *nh_grp;
1387
1388 list_for_each_entry(nh_grp, &mlxsw_sp->router.nexthop_group_list,
1389 list) {
1390 if (mlxsw_sp_nexthop_group_match(nh_grp, fi))
1391 return nh_grp;
1392 }
1393 return NULL;
1394}
1395
1396static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1397 struct mlxsw_sp_fib_entry *fib_entry,
1398 struct fib_info *fi)
1399{
1400 struct mlxsw_sp_nexthop_group *nh_grp;
1401
1402 nh_grp = mlxsw_sp_nexthop_group_find(mlxsw_sp, fi);
1403 if (!nh_grp) {
1404 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1405 if (IS_ERR(nh_grp))
1406 return PTR_ERR(nh_grp);
1407 }
1408 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1409 fib_entry->nh_group = nh_grp;
1410 return 0;
1411}
1412
1413static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1414 struct mlxsw_sp_fib_entry *fib_entry)
1415{
1416 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1417
1418 list_del(&fib_entry->nexthop_group_node);
1419 if (!list_empty(&nh_grp->fib_list))
1420 return;
1421 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1422}
1423
Ido Schimmel464dce12016-07-02 11:00:15 +02001424static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
1425{
1426 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1427
1428 mlxsw_reg_rgcr_pack(rgcr_pl, true);
1429 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, MLXSW_SP_RIF_MAX);
1430 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1431}
1432
1433static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
1434{
1435 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1436
1437 mlxsw_reg_rgcr_pack(rgcr_pl, false);
1438 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1439}
1440
1441int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
1442{
Jiri Pirko53342022016-07-04 08:23:08 +02001443 int err;
1444
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001445 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_group_list);
Jiri Pirko53342022016-07-04 08:23:08 +02001446 err = __mlxsw_sp_router_init(mlxsw_sp);
1447 if (err)
1448 return err;
1449 mlxsw_sp_lpm_init(mlxsw_sp);
Jiri Pirko6b75c482016-07-04 08:23:09 +02001450 mlxsw_sp_vrs_init(mlxsw_sp);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001451 return mlxsw_sp_neigh_init(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +02001452}
1453
1454void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
1455{
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001456 mlxsw_sp_neigh_fini(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +02001457 __mlxsw_sp_router_fini(mlxsw_sp);
1458}
Jiri Pirko61c503f2016-07-04 08:23:11 +02001459
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001460static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp,
1461 struct mlxsw_sp_fib_entry *fib_entry,
1462 enum mlxsw_reg_ralue_op op)
1463{
1464 char ralue_pl[MLXSW_REG_RALUE_LEN];
1465 u32 *p_dip = (u32 *) fib_entry->key.addr;
1466 struct mlxsw_sp_vr *vr = fib_entry->vr;
1467 enum mlxsw_reg_ralue_trap_action trap_action;
1468 u16 trap_id = 0;
1469 u32 adjacency_index = 0;
1470 u16 ecmp_size = 0;
1471
1472 /* In case the nexthop group adjacency index is valid, use it
1473 * with provided ECMP size. Otherwise, setup trap and pass
1474 * traffic to kernel.
1475 */
1476 if (fib_entry->nh_group->adj_index_valid) {
1477 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1478 adjacency_index = fib_entry->nh_group->adj_index;
1479 ecmp_size = fib_entry->nh_group->ecmp_size;
1480 } else {
1481 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1482 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1483 }
1484
1485 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1486 fib_entry->key.prefix_len, *p_dip);
1487 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
1488 adjacency_index, ecmp_size);
1489 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1490}
1491
Jiri Pirko61c503f2016-07-04 08:23:11 +02001492static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1493 struct mlxsw_sp_fib_entry *fib_entry,
1494 enum mlxsw_reg_ralue_op op)
1495{
1496 char ralue_pl[MLXSW_REG_RALUE_LEN];
1497 u32 *p_dip = (u32 *) fib_entry->key.addr;
1498 struct mlxsw_sp_vr *vr = fib_entry->vr;
1499
1500 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1501 fib_entry->key.prefix_len, *p_dip);
1502 mlxsw_reg_ralue_act_local_pack(ralue_pl,
1503 MLXSW_REG_RALUE_TRAP_ACTION_NOP, 0,
1504 fib_entry->rif);
1505 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1506}
1507
1508static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1509 struct mlxsw_sp_fib_entry *fib_entry,
1510 enum mlxsw_reg_ralue_op op)
1511{
1512 char ralue_pl[MLXSW_REG_RALUE_LEN];
1513 u32 *p_dip = (u32 *) fib_entry->key.addr;
1514 struct mlxsw_sp_vr *vr = fib_entry->vr;
1515
1516 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1517 fib_entry->key.prefix_len, *p_dip);
1518 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1519 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1520}
1521
1522static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1523 struct mlxsw_sp_fib_entry *fib_entry,
1524 enum mlxsw_reg_ralue_op op)
1525{
1526 switch (fib_entry->type) {
1527 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001528 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001529 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1530 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1531 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1532 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1533 }
1534 return -EINVAL;
1535}
1536
1537static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1538 struct mlxsw_sp_fib_entry *fib_entry,
1539 enum mlxsw_reg_ralue_op op)
1540{
1541 switch (fib_entry->vr->proto) {
1542 case MLXSW_SP_L3_PROTO_IPV4:
1543 return mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1544 case MLXSW_SP_L3_PROTO_IPV6:
1545 return -EINVAL;
1546 }
1547 return -EINVAL;
1548}
1549
1550static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1551 struct mlxsw_sp_fib_entry *fib_entry)
1552{
1553 enum mlxsw_reg_ralue_op op;
1554
1555 op = !fib_entry->added ? MLXSW_REG_RALUE_OP_WRITE_WRITE :
1556 MLXSW_REG_RALUE_OP_WRITE_UPDATE;
1557 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
1558}
1559
1560static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1561 struct mlxsw_sp_fib_entry *fib_entry)
1562{
1563 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1564 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1565}
1566
1567struct mlxsw_sp_router_fib4_add_info {
1568 struct switchdev_trans_item tritem;
1569 struct mlxsw_sp *mlxsw_sp;
1570 struct mlxsw_sp_fib_entry *fib_entry;
1571};
1572
1573static void mlxsw_sp_router_fib4_add_info_destroy(void const *data)
1574{
1575 const struct mlxsw_sp_router_fib4_add_info *info = data;
1576 struct mlxsw_sp_fib_entry *fib_entry = info->fib_entry;
1577 struct mlxsw_sp *mlxsw_sp = info->mlxsw_sp;
1578
1579 mlxsw_sp_fib_entry_destroy(fib_entry);
1580 mlxsw_sp_vr_put(mlxsw_sp, fib_entry->vr);
1581 kfree(info);
1582}
1583
1584static int
1585mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp *mlxsw_sp,
1586 const struct switchdev_obj_ipv4_fib *fib4,
1587 struct mlxsw_sp_fib_entry *fib_entry)
1588{
1589 struct fib_info *fi = fib4->fi;
1590
1591 if (fib4->type == RTN_LOCAL || fib4->type == RTN_BROADCAST) {
1592 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1593 return 0;
1594 }
1595 if (fib4->type != RTN_UNICAST)
1596 return -EINVAL;
1597
1598 if (fi->fib_scope != RT_SCOPE_UNIVERSE) {
1599 struct mlxsw_sp_rif *r;
1600
1601 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1602 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fi->fib_dev);
1603 if (!r)
1604 return -EINVAL;
1605 fib_entry->rif = r->rif;
1606 return 0;
1607 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001608 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
1609 return mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fi);
1610}
1611
1612static void
1613mlxsw_sp_router_fib4_entry_fini(struct mlxsw_sp *mlxsw_sp,
1614 struct mlxsw_sp_fib_entry *fib_entry)
1615{
1616 if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_REMOTE)
1617 return;
1618 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001619}
1620
1621static int
1622mlxsw_sp_router_fib4_add_prepare(struct mlxsw_sp_port *mlxsw_sp_port,
1623 const struct switchdev_obj_ipv4_fib *fib4,
1624 struct switchdev_trans *trans)
1625{
1626 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1627 struct mlxsw_sp_router_fib4_add_info *info;
1628 struct mlxsw_sp_fib_entry *fib_entry;
1629 struct mlxsw_sp_vr *vr;
1630 int err;
1631
1632 vr = mlxsw_sp_vr_get(mlxsw_sp, fib4->dst_len, fib4->tb_id,
1633 MLXSW_SP_L3_PROTO_IPV4);
1634 if (IS_ERR(vr))
1635 return PTR_ERR(vr);
1636
1637 fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fib4->dst,
1638 sizeof(fib4->dst), fib4->dst_len);
1639 if (!fib_entry) {
1640 err = -ENOMEM;
1641 goto err_fib_entry_create;
1642 }
1643 fib_entry->vr = vr;
1644
1645 err = mlxsw_sp_router_fib4_entry_init(mlxsw_sp, fib4, fib_entry);
1646 if (err)
1647 goto err_fib4_entry_init;
1648
1649 info = kmalloc(sizeof(*info), GFP_KERNEL);
1650 if (!info) {
1651 err = -ENOMEM;
1652 goto err_alloc_info;
1653 }
1654 info->mlxsw_sp = mlxsw_sp;
1655 info->fib_entry = fib_entry;
1656 switchdev_trans_item_enqueue(trans, info,
1657 mlxsw_sp_router_fib4_add_info_destroy,
1658 &info->tritem);
1659 return 0;
1660
1661err_alloc_info:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001662 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001663err_fib4_entry_init:
1664 mlxsw_sp_fib_entry_destroy(fib_entry);
1665err_fib_entry_create:
1666 mlxsw_sp_vr_put(mlxsw_sp, vr);
1667 return err;
1668}
1669
1670static int
1671mlxsw_sp_router_fib4_add_commit(struct mlxsw_sp_port *mlxsw_sp_port,
1672 const struct switchdev_obj_ipv4_fib *fib4,
1673 struct switchdev_trans *trans)
1674{
1675 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1676 struct mlxsw_sp_router_fib4_add_info *info;
1677 struct mlxsw_sp_fib_entry *fib_entry;
1678 struct mlxsw_sp_vr *vr;
1679 int err;
1680
1681 info = switchdev_trans_item_dequeue(trans);
1682 fib_entry = info->fib_entry;
1683 kfree(info);
1684
1685 vr = fib_entry->vr;
1686 err = mlxsw_sp_fib_entry_insert(fib_entry->vr->fib, fib_entry);
1687 if (err)
1688 goto err_fib_entry_insert;
1689 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1690 if (err)
1691 goto err_fib_entry_add;
1692 return 0;
1693
1694err_fib_entry_add:
1695 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1696err_fib_entry_insert:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001697 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001698 mlxsw_sp_fib_entry_destroy(fib_entry);
1699 mlxsw_sp_vr_put(mlxsw_sp, vr);
1700 return err;
1701}
1702
1703int mlxsw_sp_router_fib4_add(struct mlxsw_sp_port *mlxsw_sp_port,
1704 const struct switchdev_obj_ipv4_fib *fib4,
1705 struct switchdev_trans *trans)
1706{
1707 if (switchdev_trans_ph_prepare(trans))
1708 return mlxsw_sp_router_fib4_add_prepare(mlxsw_sp_port,
1709 fib4, trans);
1710 return mlxsw_sp_router_fib4_add_commit(mlxsw_sp_port,
1711 fib4, trans);
1712}
1713
1714int mlxsw_sp_router_fib4_del(struct mlxsw_sp_port *mlxsw_sp_port,
1715 const struct switchdev_obj_ipv4_fib *fib4)
1716{
1717 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1718 struct mlxsw_sp_fib_entry *fib_entry;
1719 struct mlxsw_sp_vr *vr;
1720
1721 vr = mlxsw_sp_vr_find(mlxsw_sp, fib4->tb_id, MLXSW_SP_L3_PROTO_IPV4);
1722 if (!vr) {
1723 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find virtual router for FIB4 entry being removed.\n");
1724 return -ENOENT;
1725 }
1726 fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fib4->dst,
1727 sizeof(fib4->dst), fib4->dst_len);
1728 if (!fib_entry) {
1729 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find FIB4 entry being removed.\n");
1730 return PTR_ERR(vr);
1731 }
1732 mlxsw_sp_fib_entry_del(mlxsw_sp_port->mlxsw_sp, fib_entry);
1733 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001734 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001735 mlxsw_sp_fib_entry_destroy(fib_entry);
1736 mlxsw_sp_vr_put(mlxsw_sp, vr);
1737 return 0;
1738}