blob: 2b20279d2d237c157cae246c7924228abb182e0d [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 */
Yotam Gigib2157142016-07-05 11:27:51 +0200573 struct list_head nexthop_neighs_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200574};
575
576static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
577 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
578 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
579 .key_len = sizeof(struct mlxsw_sp_neigh_key),
580};
581
582static int
583mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
584 struct mlxsw_sp_neigh_entry *neigh_entry)
585{
586 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
587 &neigh_entry->ht_node,
588 mlxsw_sp_neigh_ht_params);
589}
590
591static void
592mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
593 struct mlxsw_sp_neigh_entry *neigh_entry)
594{
595 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
596 &neigh_entry->ht_node,
597 mlxsw_sp_neigh_ht_params);
598}
599
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200600static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work);
601
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200602static struct mlxsw_sp_neigh_entry *
603mlxsw_sp_neigh_entry_create(const void *addr, size_t addr_len,
604 struct net_device *dev, u16 rif,
605 struct neighbour *n)
606{
607 struct mlxsw_sp_neigh_entry *neigh_entry;
608
609 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_ATOMIC);
610 if (!neigh_entry)
611 return NULL;
612 memcpy(neigh_entry->key.addr, addr, addr_len);
613 neigh_entry->key.dev = dev;
614 neigh_entry->rif = rif;
615 neigh_entry->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200616 INIT_DELAYED_WORK(&neigh_entry->dw, mlxsw_sp_router_neigh_update_hw);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200617 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200618 return neigh_entry;
619}
620
621static void
622mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp_neigh_entry *neigh_entry)
623{
624 kfree(neigh_entry);
625}
626
627static struct mlxsw_sp_neigh_entry *
628mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, const void *addr,
629 size_t addr_len, struct net_device *dev)
630{
631 struct mlxsw_sp_neigh_key key = {{ 0 } };
632
633 memcpy(key.addr, addr, addr_len);
634 key.dev = dev;
635 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
636 &key, mlxsw_sp_neigh_ht_params);
637}
638
639int mlxsw_sp_router_neigh_construct(struct net_device *dev,
640 struct neighbour *n)
641{
642 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
643 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
644 struct mlxsw_sp_neigh_entry *neigh_entry;
645 struct mlxsw_sp_rif *r;
646 u32 dip;
647 int err;
648
649 if (n->tbl != &arp_tbl)
650 return 0;
651
652 dip = ntohl(*((__be32 *) n->primary_key));
653 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
654 n->dev);
655 if (neigh_entry) {
656 WARN_ON(neigh_entry->n != n);
657 return 0;
658 }
659
660 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
661 if (WARN_ON(!r))
662 return -EINVAL;
663
664 neigh_entry = mlxsw_sp_neigh_entry_create(&dip, sizeof(dip), n->dev,
665 r->rif, n);
666 if (!neigh_entry)
667 return -ENOMEM;
668 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
669 if (err)
670 goto err_neigh_entry_insert;
671 return 0;
672
673err_neigh_entry_insert:
674 mlxsw_sp_neigh_entry_destroy(neigh_entry);
675 return err;
676}
677
678void mlxsw_sp_router_neigh_destroy(struct net_device *dev,
679 struct neighbour *n)
680{
681 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
682 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
683 struct mlxsw_sp_neigh_entry *neigh_entry;
684 u32 dip;
685
686 if (n->tbl != &arp_tbl)
687 return;
688
689 dip = ntohl(*((__be32 *) n->primary_key));
690 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &dip, sizeof(dip),
691 n->dev);
692 if (!neigh_entry)
693 return;
694 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
695 mlxsw_sp_neigh_entry_destroy(neigh_entry);
696}
697
Yotam Gigic723c7352016-07-05 11:27:43 +0200698static void
699mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
700{
701 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
702
703 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
704}
705
706static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
707 char *rauhtd_pl,
708 int ent_index)
709{
710 struct net_device *dev;
711 struct neighbour *n;
712 __be32 dipn;
713 u32 dip;
714 u16 rif;
715
716 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
717
718 if (!mlxsw_sp->rifs[rif]) {
719 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
720 return;
721 }
722
723 dipn = htonl(dip);
724 dev = mlxsw_sp->rifs[rif]->dev;
725 n = neigh_lookup(&arp_tbl, &dipn, dev);
726 if (!n) {
727 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
728 &dip);
729 return;
730 }
731
732 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
733 neigh_event_send(n, NULL);
734 neigh_release(n);
735}
736
737static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
738 char *rauhtd_pl,
739 int rec_index)
740{
741 u8 num_entries;
742 int i;
743
744 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
745 rec_index);
746 /* Hardware starts counting at 0, so add 1. */
747 num_entries++;
748
749 /* Each record consists of several neighbour entries. */
750 for (i = 0; i < num_entries; i++) {
751 int ent_index;
752
753 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
754 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
755 ent_index);
756 }
757
758}
759
760static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
761 char *rauhtd_pl, int rec_index)
762{
763 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
764 case MLXSW_REG_RAUHTD_TYPE_IPV4:
765 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
766 rec_index);
767 break;
768 case MLXSW_REG_RAUHTD_TYPE_IPV6:
769 WARN_ON_ONCE(1);
770 break;
771 }
772}
773
Yotam Gigib2157142016-07-05 11:27:51 +0200774static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
Yotam Gigic723c7352016-07-05 11:27:43 +0200775{
Yotam Gigic723c7352016-07-05 11:27:43 +0200776 char *rauhtd_pl;
777 u8 num_rec;
778 int i, err;
779
780 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
781 if (!rauhtd_pl)
Yotam Gigib2157142016-07-05 11:27:51 +0200782 return -ENOMEM;
Yotam Gigic723c7352016-07-05 11:27:43 +0200783
784 /* Make sure the neighbour's netdev isn't removed in the
785 * process.
786 */
787 rtnl_lock();
788 do {
789 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
790 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
791 rauhtd_pl);
792 if (err) {
793 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
794 break;
795 }
796 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
797 for (i = 0; i < num_rec; i++)
798 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
799 i);
800 } while (num_rec);
801 rtnl_unlock();
802
803 kfree(rauhtd_pl);
Yotam Gigib2157142016-07-05 11:27:51 +0200804 return err;
805}
806
807static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
808{
809 struct mlxsw_sp_neigh_entry *neigh_entry;
810
811 /* Take RTNL mutex here to prevent lists from changes */
812 rtnl_lock();
813 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
814 nexthop_neighs_list_node) {
815 /* If this neigh have nexthops, make the kernel think this neigh
816 * is active regardless of the traffic.
817 */
818 if (!list_empty(&neigh_entry->nexthop_list))
819 neigh_event_send(neigh_entry->n, NULL);
820 }
821 rtnl_unlock();
822}
823
824static void
825mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
826{
827 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
828
829 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
830 msecs_to_jiffies(interval));
831}
832
833static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
834{
835 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
836 router.neighs_update.dw.work);
837 int err;
838
839 err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp);
840 if (err)
841 dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
842
843 mlxsw_sp_router_neighs_update_nh(mlxsw_sp);
844
Yotam Gigic723c7352016-07-05 11:27:43 +0200845 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
846}
847
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200848static void
849mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
850 struct mlxsw_sp_neigh_entry *neigh_entry,
851 bool removing);
852
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200853static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work)
854{
855 struct mlxsw_sp_neigh_entry *neigh_entry =
856 container_of(work, struct mlxsw_sp_neigh_entry, dw.work);
857 struct neighbour *n = neigh_entry->n;
858 struct mlxsw_sp_port *mlxsw_sp_port = neigh_entry->mlxsw_sp_port;
859 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
860 char rauht_pl[MLXSW_REG_RAUHT_LEN];
861 struct net_device *dev;
862 bool entry_connected;
863 u8 nud_state;
864 bool updating;
865 bool removing;
866 bool adding;
867 u32 dip;
868 int err;
869
870 read_lock_bh(&n->lock);
871 dip = ntohl(*((__be32 *) n->primary_key));
872 memcpy(neigh_entry->ha, n->ha, sizeof(neigh_entry->ha));
873 nud_state = n->nud_state;
874 dev = n->dev;
875 read_unlock_bh(&n->lock);
876
877 entry_connected = nud_state & NUD_VALID;
878 adding = (!neigh_entry->offloaded) && entry_connected;
879 updating = neigh_entry->offloaded && entry_connected;
880 removing = neigh_entry->offloaded && !entry_connected;
881
882 if (adding || updating) {
883 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_ADD,
884 neigh_entry->rif,
885 neigh_entry->ha, dip);
886 err = mlxsw_reg_write(mlxsw_sp->core,
887 MLXSW_REG(rauht), rauht_pl);
888 if (err) {
889 netdev_err(dev, "Could not add neigh %pI4h\n", &dip);
890 neigh_entry->offloaded = false;
891 } else {
892 neigh_entry->offloaded = true;
893 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200894 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, false);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200895 } else if (removing) {
896 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE,
897 neigh_entry->rif,
898 neigh_entry->ha, dip);
899 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht),
900 rauht_pl);
901 if (err) {
902 netdev_err(dev, "Could not delete neigh %pI4h\n", &dip);
903 neigh_entry->offloaded = true;
904 } else {
905 neigh_entry->offloaded = false;
906 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200907 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, true);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200908 }
909
910 neigh_release(n);
911 mlxsw_sp_port_dev_put(mlxsw_sp_port);
912}
913
Yotam Gigic723c7352016-07-05 11:27:43 +0200914static int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
915 unsigned long event, void *ptr)
916{
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200917 struct mlxsw_sp_neigh_entry *neigh_entry;
Yotam Gigic723c7352016-07-05 11:27:43 +0200918 struct mlxsw_sp_port *mlxsw_sp_port;
919 struct mlxsw_sp *mlxsw_sp;
920 unsigned long interval;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200921 struct net_device *dev;
Yotam Gigic723c7352016-07-05 11:27:43 +0200922 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200923 struct neighbour *n;
924 u32 dip;
Yotam Gigic723c7352016-07-05 11:27:43 +0200925
926 switch (event) {
927 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
928 p = ptr;
929
930 /* We don't care about changes in the default table. */
931 if (!p->dev || p->tbl != &arp_tbl)
932 return NOTIFY_DONE;
933
934 /* We are in atomic context and can't take RTNL mutex,
935 * so use RCU variant to walk the device chain.
936 */
937 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
938 if (!mlxsw_sp_port)
939 return NOTIFY_DONE;
940
941 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
942 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
943 mlxsw_sp->router.neighs_update.interval = interval;
944
945 mlxsw_sp_port_dev_put(mlxsw_sp_port);
946 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200947 case NETEVENT_NEIGH_UPDATE:
948 n = ptr;
949 dev = n->dev;
950
951 if (n->tbl != &arp_tbl)
952 return NOTIFY_DONE;
953
954 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(dev);
955 if (!mlxsw_sp_port)
956 return NOTIFY_DONE;
957
958 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
959 dip = ntohl(*((__be32 *) n->primary_key));
960 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp,
961 &dip,
962 sizeof(__be32),
963 dev);
964 if (WARN_ON(!neigh_entry) || WARN_ON(neigh_entry->n != n)) {
965 mlxsw_sp_port_dev_put(mlxsw_sp_port);
966 return NOTIFY_DONE;
967 }
968 neigh_entry->mlxsw_sp_port = mlxsw_sp_port;
969
970 /* Take a reference to ensure the neighbour won't be
971 * destructed until we drop the reference in delayed
972 * work.
973 */
974 neigh_clone(n);
975 if (!mlxsw_core_schedule_dw(&neigh_entry->dw, 0)) {
976 neigh_release(n);
977 mlxsw_sp_port_dev_put(mlxsw_sp_port);
978 }
979 break;
Yotam Gigic723c7352016-07-05 11:27:43 +0200980 }
981
982 return NOTIFY_DONE;
983}
984
985static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly = {
986 .notifier_call = mlxsw_sp_router_netevent_event,
987};
988
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200989static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
990{
Yotam Gigic723c7352016-07-05 11:27:43 +0200991 int err;
992
993 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
994 &mlxsw_sp_neigh_ht_params);
995 if (err)
996 return err;
997
998 /* Initialize the polling interval according to the default
999 * table.
1000 */
1001 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1002
1003 err = register_netevent_notifier(&mlxsw_sp_router_netevent_nb);
1004 if (err)
1005 goto err_register_netevent_notifier;
1006
1007 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
1008 mlxsw_sp_router_neighs_update_work);
1009 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
1010
1011 return 0;
1012
1013err_register_netevent_notifier:
1014 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1015 return err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001016}
1017
1018static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1019{
Yotam Gigic723c7352016-07-05 11:27:43 +02001020 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
1021 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001022 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1023}
1024
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001025struct mlxsw_sp_nexthop {
1026 struct list_head neigh_list_node; /* member of neigh entry list */
1027 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1028 * this belongs to
1029 */
1030 u8 should_offload:1, /* set indicates this neigh is connected and
1031 * should be put to KVD linear area of this group.
1032 */
1033 offloaded:1, /* set in case the neigh is actually put into
1034 * KVD linear area of this group.
1035 */
1036 update:1; /* set indicates that MAC of this neigh should be
1037 * updated in HW
1038 */
1039 struct mlxsw_sp_neigh_entry *neigh_entry;
1040};
1041
1042struct mlxsw_sp_nexthop_group {
1043 struct list_head list; /* node in mlxsw->router.nexthop_group_list */
1044 struct list_head fib_list; /* list of fib entries that use this group */
1045 u8 adj_index_valid:1;
1046 u32 adj_index;
1047 u16 ecmp_size;
1048 u16 count;
1049 struct mlxsw_sp_nexthop nexthops[0];
1050};
1051
1052static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
1053 struct mlxsw_sp_vr *vr,
1054 u32 adj_index, u16 ecmp_size,
1055 u32 new_adj_index,
1056 u16 new_ecmp_size)
1057{
1058 char raleu_pl[MLXSW_REG_RALEU_LEN];
1059
1060 mlxsw_reg_raleu_pack(raleu_pl, vr->proto, vr->id,
1061 adj_index, ecmp_size,
1062 new_adj_index, new_ecmp_size);
1063 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1064}
1065
1066static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1067 struct mlxsw_sp_nexthop_group *nh_grp,
1068 u32 old_adj_index, u16 old_ecmp_size)
1069{
1070 struct mlxsw_sp_fib_entry *fib_entry;
1071 struct mlxsw_sp_vr *vr = NULL;
1072 int err;
1073
1074 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1075 if (vr == fib_entry->vr)
1076 continue;
1077 vr = fib_entry->vr;
1078 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, vr,
1079 old_adj_index,
1080 old_ecmp_size,
1081 nh_grp->adj_index,
1082 nh_grp->ecmp_size);
1083 if (err)
1084 return err;
1085 }
1086 return 0;
1087}
1088
1089static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1090 struct mlxsw_sp_nexthop *nh)
1091{
1092 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1093 char ratr_pl[MLXSW_REG_RATR_LEN];
1094
1095 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1096 true, adj_index, neigh_entry->rif);
1097 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1098 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1099}
1100
1101static int
1102mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
1103 struct mlxsw_sp_nexthop_group *nh_grp)
1104{
1105 u32 adj_index = nh_grp->adj_index; /* base */
1106 struct mlxsw_sp_nexthop *nh;
1107 int i;
1108 int err;
1109
1110 for (i = 0; i < nh_grp->count; i++) {
1111 nh = &nh_grp->nexthops[i];
1112
1113 if (!nh->should_offload) {
1114 nh->offloaded = 0;
1115 continue;
1116 }
1117
1118 if (nh->update) {
1119 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1120 adj_index, nh);
1121 if (err)
1122 return err;
1123 nh->update = 0;
1124 nh->offloaded = 1;
1125 }
1126 adj_index++;
1127 }
1128 return 0;
1129}
1130
1131static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1132 struct mlxsw_sp_fib_entry *fib_entry);
1133
1134static int
1135mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1136 struct mlxsw_sp_nexthop_group *nh_grp)
1137{
1138 struct mlxsw_sp_fib_entry *fib_entry;
1139 int err;
1140
1141 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1142 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1143 if (err)
1144 return err;
1145 }
1146 return 0;
1147}
1148
1149static void
1150mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1151 struct mlxsw_sp_nexthop_group *nh_grp)
1152{
1153 struct mlxsw_sp_nexthop *nh;
1154 bool offload_change = false;
1155 u32 adj_index;
1156 u16 ecmp_size = 0;
1157 bool old_adj_index_valid;
1158 u32 old_adj_index;
1159 u16 old_ecmp_size;
1160 int ret;
1161 int i;
1162 int err;
1163
1164 for (i = 0; i < nh_grp->count; i++) {
1165 nh = &nh_grp->nexthops[i];
1166
1167 if (nh->should_offload ^ nh->offloaded) {
1168 offload_change = true;
1169 if (nh->should_offload)
1170 nh->update = 1;
1171 }
1172 if (nh->should_offload)
1173 ecmp_size++;
1174 }
1175 if (!offload_change) {
1176 /* Nothing was added or removed, so no need to reallocate. Just
1177 * update MAC on existing adjacency indexes.
1178 */
1179 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp);
1180 if (err) {
1181 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1182 goto set_trap;
1183 }
1184 return;
1185 }
1186 if (!ecmp_size)
1187 /* No neigh of this group is connected so we just set
1188 * the trap and let everthing flow through kernel.
1189 */
1190 goto set_trap;
1191
1192 ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size);
1193 if (ret < 0) {
1194 /* We ran out of KVD linear space, just set the
1195 * trap and let everything flow through kernel.
1196 */
1197 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1198 goto set_trap;
1199 }
1200 adj_index = ret;
1201 old_adj_index_valid = nh_grp->adj_index_valid;
1202 old_adj_index = nh_grp->adj_index;
1203 old_ecmp_size = nh_grp->ecmp_size;
1204 nh_grp->adj_index_valid = 1;
1205 nh_grp->adj_index = adj_index;
1206 nh_grp->ecmp_size = ecmp_size;
1207 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp);
1208 if (err) {
1209 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1210 goto set_trap;
1211 }
1212
1213 if (!old_adj_index_valid) {
1214 /* The trap was set for fib entries, so we have to call
1215 * fib entry update to unset it and use adjacency index.
1216 */
1217 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1218 if (err) {
1219 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1220 goto set_trap;
1221 }
1222 return;
1223 }
1224
1225 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1226 old_adj_index, old_ecmp_size);
1227 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1228 if (err) {
1229 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1230 goto set_trap;
1231 }
1232 return;
1233
1234set_trap:
1235 old_adj_index_valid = nh_grp->adj_index_valid;
1236 nh_grp->adj_index_valid = 0;
1237 for (i = 0; i < nh_grp->count; i++) {
1238 nh = &nh_grp->nexthops[i];
1239 nh->offloaded = 0;
1240 }
1241 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1242 if (err)
1243 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1244 if (old_adj_index_valid)
1245 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1246}
1247
1248static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1249 bool removing)
1250{
1251 if (!removing && !nh->should_offload)
1252 nh->should_offload = 1;
1253 else if (removing && nh->offloaded)
1254 nh->should_offload = 0;
1255 nh->update = 1;
1256}
1257
1258static void
1259mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1260 struct mlxsw_sp_neigh_entry *neigh_entry,
1261 bool removing)
1262{
1263 struct mlxsw_sp_nexthop *nh;
1264
1265 /* Take RTNL mutex here to prevent lists from changes */
1266 rtnl_lock();
1267 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1268 neigh_list_node) {
1269 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1270 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1271 }
1272 rtnl_unlock();
1273}
1274
1275static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp,
1276 struct mlxsw_sp_nexthop_group *nh_grp,
1277 struct mlxsw_sp_nexthop *nh,
1278 struct fib_nh *fib_nh)
1279{
1280 struct mlxsw_sp_neigh_entry *neigh_entry;
1281 u32 gwip = ntohl(fib_nh->nh_gw);
1282 struct net_device *dev = fib_nh->nh_dev;
1283 struct neighbour *n;
1284 u8 nud_state;
1285
1286 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &gwip,
1287 sizeof(gwip), dev);
1288 if (!neigh_entry) {
1289 __be32 gwipn = htonl(gwip);
1290
1291 n = neigh_create(&arp_tbl, &gwipn, dev);
1292 if (IS_ERR(n))
1293 return PTR_ERR(n);
1294 neigh_event_send(n, NULL);
1295 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, &gwip,
1296 sizeof(gwip), dev);
1297 if (!neigh_entry) {
1298 neigh_release(n);
1299 return -EINVAL;
1300 }
1301 } else {
1302 /* Take a reference of neigh here ensuring that neigh would
1303 * not be detructed before the nexthop entry is finished.
1304 * The second branch takes the reference in neith_create()
1305 */
1306 n = neigh_entry->n;
1307 neigh_clone(n);
1308 }
Yotam Gigib2157142016-07-05 11:27:51 +02001309
1310 /* If that is the first nexthop connected to that neigh, add to
1311 * nexthop_neighs_list
1312 */
1313 if (list_empty(&neigh_entry->nexthop_list))
1314 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
1315 &mlxsw_sp->router.nexthop_neighs_list);
1316
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001317 nh->nh_grp = nh_grp;
1318 nh->neigh_entry = neigh_entry;
1319 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1320 read_lock_bh(&n->lock);
1321 nud_state = n->nud_state;
1322 read_unlock_bh(&n->lock);
1323 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID));
1324
1325 return 0;
1326}
1327
1328static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1329 struct mlxsw_sp_nexthop *nh)
1330{
1331 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1332
1333 list_del(&nh->neigh_list_node);
Yotam Gigib2157142016-07-05 11:27:51 +02001334
1335 /* If that is the last nexthop connected to that neigh, remove from
1336 * nexthop_neighs_list
1337 */
1338 if (list_empty(&nh->neigh_entry->nexthop_list))
1339 list_del(&nh->neigh_entry->nexthop_neighs_list_node);
1340
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001341 neigh_release(neigh_entry->n);
1342}
1343
1344static struct mlxsw_sp_nexthop_group *
1345mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1346{
1347 struct mlxsw_sp_nexthop_group *nh_grp;
1348 struct mlxsw_sp_nexthop *nh;
1349 struct fib_nh *fib_nh;
1350 size_t alloc_size;
1351 int i;
1352 int err;
1353
1354 alloc_size = sizeof(*nh_grp) +
1355 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1356 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1357 if (!nh_grp)
1358 return ERR_PTR(-ENOMEM);
1359 INIT_LIST_HEAD(&nh_grp->fib_list);
1360 nh_grp->count = fi->fib_nhs;
1361 for (i = 0; i < nh_grp->count; i++) {
1362 nh = &nh_grp->nexthops[i];
1363 fib_nh = &fi->fib_nh[i];
1364 err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh);
1365 if (err)
1366 goto err_nexthop_init;
1367 }
1368 list_add_tail(&nh_grp->list, &mlxsw_sp->router.nexthop_group_list);
1369 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1370 return nh_grp;
1371
1372err_nexthop_init:
1373 for (i--; i >= 0; i--)
1374 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1375 kfree(nh_grp);
1376 return ERR_PTR(err);
1377}
1378
1379static void
1380mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1381 struct mlxsw_sp_nexthop_group *nh_grp)
1382{
1383 struct mlxsw_sp_nexthop *nh;
1384 int i;
1385
1386 list_del(&nh_grp->list);
1387 for (i = 0; i < nh_grp->count; i++) {
1388 nh = &nh_grp->nexthops[i];
1389 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1390 }
1391 kfree(nh_grp);
1392}
1393
1394static bool mlxsw_sp_nexthop_match(struct mlxsw_sp_nexthop *nh,
1395 struct fib_info *fi)
1396{
1397 int i;
1398
1399 for (i = 0; i < fi->fib_nhs; i++) {
1400 struct fib_nh *fib_nh = &fi->fib_nh[i];
1401 u32 gwip = ntohl(fib_nh->nh_gw);
1402
1403 if (memcmp(nh->neigh_entry->key.addr,
1404 &gwip, sizeof(u32)) == 0 &&
1405 nh->neigh_entry->key.dev == fib_nh->nh_dev)
1406 return true;
1407 }
1408 return false;
1409}
1410
1411static bool mlxsw_sp_nexthop_group_match(struct mlxsw_sp_nexthop_group *nh_grp,
1412 struct fib_info *fi)
1413{
1414 int i;
1415
1416 if (nh_grp->count != fi->fib_nhs)
1417 return false;
1418 for (i = 0; i < nh_grp->count; i++) {
1419 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
1420
1421 if (!mlxsw_sp_nexthop_match(nh, fi))
1422 return false;
1423 }
1424 return true;
1425}
1426
1427static struct mlxsw_sp_nexthop_group *
1428mlxsw_sp_nexthop_group_find(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1429{
1430 struct mlxsw_sp_nexthop_group *nh_grp;
1431
1432 list_for_each_entry(nh_grp, &mlxsw_sp->router.nexthop_group_list,
1433 list) {
1434 if (mlxsw_sp_nexthop_group_match(nh_grp, fi))
1435 return nh_grp;
1436 }
1437 return NULL;
1438}
1439
1440static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1441 struct mlxsw_sp_fib_entry *fib_entry,
1442 struct fib_info *fi)
1443{
1444 struct mlxsw_sp_nexthop_group *nh_grp;
1445
1446 nh_grp = mlxsw_sp_nexthop_group_find(mlxsw_sp, fi);
1447 if (!nh_grp) {
1448 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1449 if (IS_ERR(nh_grp))
1450 return PTR_ERR(nh_grp);
1451 }
1452 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1453 fib_entry->nh_group = nh_grp;
1454 return 0;
1455}
1456
1457static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1458 struct mlxsw_sp_fib_entry *fib_entry)
1459{
1460 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1461
1462 list_del(&fib_entry->nexthop_group_node);
1463 if (!list_empty(&nh_grp->fib_list))
1464 return;
1465 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1466}
1467
Ido Schimmel464dce12016-07-02 11:00:15 +02001468static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
1469{
1470 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1471
1472 mlxsw_reg_rgcr_pack(rgcr_pl, true);
1473 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, MLXSW_SP_RIF_MAX);
1474 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1475}
1476
1477static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
1478{
1479 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1480
1481 mlxsw_reg_rgcr_pack(rgcr_pl, false);
1482 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1483}
1484
1485int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
1486{
Jiri Pirko53342022016-07-04 08:23:08 +02001487 int err;
1488
Yotam Gigib2157142016-07-05 11:27:51 +02001489 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001490 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_group_list);
Jiri Pirko53342022016-07-04 08:23:08 +02001491 err = __mlxsw_sp_router_init(mlxsw_sp);
1492 if (err)
1493 return err;
1494 mlxsw_sp_lpm_init(mlxsw_sp);
Jiri Pirko6b75c482016-07-04 08:23:09 +02001495 mlxsw_sp_vrs_init(mlxsw_sp);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001496 return mlxsw_sp_neigh_init(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +02001497}
1498
1499void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
1500{
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001501 mlxsw_sp_neigh_fini(mlxsw_sp);
Ido Schimmel464dce12016-07-02 11:00:15 +02001502 __mlxsw_sp_router_fini(mlxsw_sp);
1503}
Jiri Pirko61c503f2016-07-04 08:23:11 +02001504
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001505static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp,
1506 struct mlxsw_sp_fib_entry *fib_entry,
1507 enum mlxsw_reg_ralue_op op)
1508{
1509 char ralue_pl[MLXSW_REG_RALUE_LEN];
1510 u32 *p_dip = (u32 *) fib_entry->key.addr;
1511 struct mlxsw_sp_vr *vr = fib_entry->vr;
1512 enum mlxsw_reg_ralue_trap_action trap_action;
1513 u16 trap_id = 0;
1514 u32 adjacency_index = 0;
1515 u16 ecmp_size = 0;
1516
1517 /* In case the nexthop group adjacency index is valid, use it
1518 * with provided ECMP size. Otherwise, setup trap and pass
1519 * traffic to kernel.
1520 */
1521 if (fib_entry->nh_group->adj_index_valid) {
1522 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1523 adjacency_index = fib_entry->nh_group->adj_index;
1524 ecmp_size = fib_entry->nh_group->ecmp_size;
1525 } else {
1526 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1527 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1528 }
1529
1530 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1531 fib_entry->key.prefix_len, *p_dip);
1532 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
1533 adjacency_index, ecmp_size);
1534 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1535}
1536
Jiri Pirko61c503f2016-07-04 08:23:11 +02001537static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1538 struct mlxsw_sp_fib_entry *fib_entry,
1539 enum mlxsw_reg_ralue_op op)
1540{
1541 char ralue_pl[MLXSW_REG_RALUE_LEN];
1542 u32 *p_dip = (u32 *) fib_entry->key.addr;
1543 struct mlxsw_sp_vr *vr = fib_entry->vr;
1544
1545 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1546 fib_entry->key.prefix_len, *p_dip);
1547 mlxsw_reg_ralue_act_local_pack(ralue_pl,
1548 MLXSW_REG_RALUE_TRAP_ACTION_NOP, 0,
1549 fib_entry->rif);
1550 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1551}
1552
1553static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1554 struct mlxsw_sp_fib_entry *fib_entry,
1555 enum mlxsw_reg_ralue_op op)
1556{
1557 char ralue_pl[MLXSW_REG_RALUE_LEN];
1558 u32 *p_dip = (u32 *) fib_entry->key.addr;
1559 struct mlxsw_sp_vr *vr = fib_entry->vr;
1560
1561 mlxsw_reg_ralue_pack4(ralue_pl, vr->proto, op, vr->id,
1562 fib_entry->key.prefix_len, *p_dip);
1563 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1564 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1565}
1566
1567static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1568 struct mlxsw_sp_fib_entry *fib_entry,
1569 enum mlxsw_reg_ralue_op op)
1570{
1571 switch (fib_entry->type) {
1572 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001573 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001574 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1575 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1576 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1577 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1578 }
1579 return -EINVAL;
1580}
1581
1582static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1583 struct mlxsw_sp_fib_entry *fib_entry,
1584 enum mlxsw_reg_ralue_op op)
1585{
1586 switch (fib_entry->vr->proto) {
1587 case MLXSW_SP_L3_PROTO_IPV4:
1588 return mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1589 case MLXSW_SP_L3_PROTO_IPV6:
1590 return -EINVAL;
1591 }
1592 return -EINVAL;
1593}
1594
1595static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1596 struct mlxsw_sp_fib_entry *fib_entry)
1597{
1598 enum mlxsw_reg_ralue_op op;
1599
1600 op = !fib_entry->added ? MLXSW_REG_RALUE_OP_WRITE_WRITE :
1601 MLXSW_REG_RALUE_OP_WRITE_UPDATE;
1602 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
1603}
1604
1605static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1606 struct mlxsw_sp_fib_entry *fib_entry)
1607{
1608 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1609 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1610}
1611
1612struct mlxsw_sp_router_fib4_add_info {
1613 struct switchdev_trans_item tritem;
1614 struct mlxsw_sp *mlxsw_sp;
1615 struct mlxsw_sp_fib_entry *fib_entry;
1616};
1617
1618static void mlxsw_sp_router_fib4_add_info_destroy(void const *data)
1619{
1620 const struct mlxsw_sp_router_fib4_add_info *info = data;
1621 struct mlxsw_sp_fib_entry *fib_entry = info->fib_entry;
1622 struct mlxsw_sp *mlxsw_sp = info->mlxsw_sp;
1623
1624 mlxsw_sp_fib_entry_destroy(fib_entry);
1625 mlxsw_sp_vr_put(mlxsw_sp, fib_entry->vr);
1626 kfree(info);
1627}
1628
1629static int
1630mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp *mlxsw_sp,
1631 const struct switchdev_obj_ipv4_fib *fib4,
1632 struct mlxsw_sp_fib_entry *fib_entry)
1633{
1634 struct fib_info *fi = fib4->fi;
1635
1636 if (fib4->type == RTN_LOCAL || fib4->type == RTN_BROADCAST) {
1637 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1638 return 0;
1639 }
1640 if (fib4->type != RTN_UNICAST)
1641 return -EINVAL;
1642
1643 if (fi->fib_scope != RT_SCOPE_UNIVERSE) {
1644 struct mlxsw_sp_rif *r;
1645
1646 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1647 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fi->fib_dev);
1648 if (!r)
1649 return -EINVAL;
1650 fib_entry->rif = r->rif;
1651 return 0;
1652 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001653 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
1654 return mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fi);
1655}
1656
1657static void
1658mlxsw_sp_router_fib4_entry_fini(struct mlxsw_sp *mlxsw_sp,
1659 struct mlxsw_sp_fib_entry *fib_entry)
1660{
1661 if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_REMOTE)
1662 return;
1663 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001664}
1665
1666static int
1667mlxsw_sp_router_fib4_add_prepare(struct mlxsw_sp_port *mlxsw_sp_port,
1668 const struct switchdev_obj_ipv4_fib *fib4,
1669 struct switchdev_trans *trans)
1670{
1671 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1672 struct mlxsw_sp_router_fib4_add_info *info;
1673 struct mlxsw_sp_fib_entry *fib_entry;
1674 struct mlxsw_sp_vr *vr;
1675 int err;
1676
1677 vr = mlxsw_sp_vr_get(mlxsw_sp, fib4->dst_len, fib4->tb_id,
1678 MLXSW_SP_L3_PROTO_IPV4);
1679 if (IS_ERR(vr))
1680 return PTR_ERR(vr);
1681
1682 fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fib4->dst,
1683 sizeof(fib4->dst), fib4->dst_len);
1684 if (!fib_entry) {
1685 err = -ENOMEM;
1686 goto err_fib_entry_create;
1687 }
1688 fib_entry->vr = vr;
1689
1690 err = mlxsw_sp_router_fib4_entry_init(mlxsw_sp, fib4, fib_entry);
1691 if (err)
1692 goto err_fib4_entry_init;
1693
1694 info = kmalloc(sizeof(*info), GFP_KERNEL);
1695 if (!info) {
1696 err = -ENOMEM;
1697 goto err_alloc_info;
1698 }
1699 info->mlxsw_sp = mlxsw_sp;
1700 info->fib_entry = fib_entry;
1701 switchdev_trans_item_enqueue(trans, info,
1702 mlxsw_sp_router_fib4_add_info_destroy,
1703 &info->tritem);
1704 return 0;
1705
1706err_alloc_info:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001707 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001708err_fib4_entry_init:
1709 mlxsw_sp_fib_entry_destroy(fib_entry);
1710err_fib_entry_create:
1711 mlxsw_sp_vr_put(mlxsw_sp, vr);
1712 return err;
1713}
1714
1715static int
1716mlxsw_sp_router_fib4_add_commit(struct mlxsw_sp_port *mlxsw_sp_port,
1717 const struct switchdev_obj_ipv4_fib *fib4,
1718 struct switchdev_trans *trans)
1719{
1720 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1721 struct mlxsw_sp_router_fib4_add_info *info;
1722 struct mlxsw_sp_fib_entry *fib_entry;
1723 struct mlxsw_sp_vr *vr;
1724 int err;
1725
1726 info = switchdev_trans_item_dequeue(trans);
1727 fib_entry = info->fib_entry;
1728 kfree(info);
1729
1730 vr = fib_entry->vr;
1731 err = mlxsw_sp_fib_entry_insert(fib_entry->vr->fib, fib_entry);
1732 if (err)
1733 goto err_fib_entry_insert;
1734 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1735 if (err)
1736 goto err_fib_entry_add;
1737 return 0;
1738
1739err_fib_entry_add:
1740 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1741err_fib_entry_insert:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001742 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001743 mlxsw_sp_fib_entry_destroy(fib_entry);
1744 mlxsw_sp_vr_put(mlxsw_sp, vr);
1745 return err;
1746}
1747
1748int mlxsw_sp_router_fib4_add(struct mlxsw_sp_port *mlxsw_sp_port,
1749 const struct switchdev_obj_ipv4_fib *fib4,
1750 struct switchdev_trans *trans)
1751{
1752 if (switchdev_trans_ph_prepare(trans))
1753 return mlxsw_sp_router_fib4_add_prepare(mlxsw_sp_port,
1754 fib4, trans);
1755 return mlxsw_sp_router_fib4_add_commit(mlxsw_sp_port,
1756 fib4, trans);
1757}
1758
1759int mlxsw_sp_router_fib4_del(struct mlxsw_sp_port *mlxsw_sp_port,
1760 const struct switchdev_obj_ipv4_fib *fib4)
1761{
1762 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1763 struct mlxsw_sp_fib_entry *fib_entry;
1764 struct mlxsw_sp_vr *vr;
1765
1766 vr = mlxsw_sp_vr_find(mlxsw_sp, fib4->tb_id, MLXSW_SP_L3_PROTO_IPV4);
1767 if (!vr) {
1768 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find virtual router for FIB4 entry being removed.\n");
1769 return -ENOENT;
1770 }
1771 fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fib4->dst,
1772 sizeof(fib4->dst), fib4->dst_len);
1773 if (!fib_entry) {
1774 dev_warn(mlxsw_sp->bus_info->dev, "Failed to find FIB4 entry being removed.\n");
1775 return PTR_ERR(vr);
1776 }
1777 mlxsw_sp_fib_entry_del(mlxsw_sp_port->mlxsw_sp, fib_entry);
1778 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001779 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001780 mlxsw_sp_fib_entry_destroy(fib_entry);
1781 mlxsw_sp_vr_put(mlxsw_sp, vr);
1782 return 0;
1783}