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