blob: cbeeddd70c5a419d8e55ef7a11a744dbb4e4f752 [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>
Jiri Pirkob45f64d2016-09-26 12:52:31 +020046#include <net/ip_fib.h>
Ido Schimmel464dce12016-07-02 11:00:15 +020047
48#include "spectrum.h"
49#include "core.h"
50#include "reg.h"
51
Jiri Pirko53342022016-07-04 08:23:08 +020052#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
53 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
54
55static bool
Jiri Pirko6b75c482016-07-04 08:23:09 +020056mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
57 struct mlxsw_sp_prefix_usage *prefix_usage2)
58{
59 unsigned char prefix;
60
61 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
62 if (!test_bit(prefix, prefix_usage2->b))
63 return false;
64 }
65 return true;
66}
67
68static bool
Jiri Pirko53342022016-07-04 08:23:08 +020069mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
70 struct mlxsw_sp_prefix_usage *prefix_usage2)
71{
72 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
73}
74
Jiri Pirko6b75c482016-07-04 08:23:09 +020075static bool
76mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
77{
78 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
79
80 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
81}
82
83static void
84mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
85 struct mlxsw_sp_prefix_usage *prefix_usage2)
86{
87 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
88}
89
90static void
91mlxsw_sp_prefix_usage_zero(struct mlxsw_sp_prefix_usage *prefix_usage)
92{
93 memset(prefix_usage, 0, sizeof(*prefix_usage));
94}
95
Jiri Pirko5e9c16c2016-07-04 08:23:04 +020096static void
97mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
98 unsigned char prefix_len)
99{
100 set_bit(prefix_len, prefix_usage->b);
101}
102
103static void
104mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
105 unsigned char prefix_len)
106{
107 clear_bit(prefix_len, prefix_usage->b);
108}
109
110struct mlxsw_sp_fib_key {
Jiri Pirko5b004412016-09-01 10:37:40 +0200111 struct net_device *dev;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200112 unsigned char addr[sizeof(struct in6_addr)];
113 unsigned char prefix_len;
114};
115
Jiri Pirko61c503f2016-07-04 08:23:11 +0200116enum mlxsw_sp_fib_entry_type {
117 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
118 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
119 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
120};
121
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200122struct mlxsw_sp_nexthop_group;
123
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200124struct mlxsw_sp_fib_entry {
125 struct rhash_head ht_node;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200126 struct list_head list;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200127 struct mlxsw_sp_fib_key key;
Jiri Pirko61c503f2016-07-04 08:23:11 +0200128 enum mlxsw_sp_fib_entry_type type;
Jiri Pirko5b004412016-09-01 10:37:40 +0200129 unsigned int ref_count;
Jiri Pirko61c503f2016-07-04 08:23:11 +0200130 u16 rif; /* used for action local */
131 struct mlxsw_sp_vr *vr;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200132 struct fib_info *fi;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200133 struct list_head nexthop_group_node;
134 struct mlxsw_sp_nexthop_group *nh_group;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200135};
136
137struct mlxsw_sp_fib {
138 struct rhashtable ht;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200139 struct list_head entry_list;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200140 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
141 struct mlxsw_sp_prefix_usage prefix_usage;
142};
143
144static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
145 .key_offset = offsetof(struct mlxsw_sp_fib_entry, key),
146 .head_offset = offsetof(struct mlxsw_sp_fib_entry, ht_node),
147 .key_len = sizeof(struct mlxsw_sp_fib_key),
148 .automatic_shrinking = true,
149};
150
151static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib *fib,
152 struct mlxsw_sp_fib_entry *fib_entry)
153{
154 unsigned char prefix_len = fib_entry->key.prefix_len;
155 int err;
156
157 err = rhashtable_insert_fast(&fib->ht, &fib_entry->ht_node,
158 mlxsw_sp_fib_ht_params);
159 if (err)
160 return err;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200161 list_add_tail(&fib_entry->list, &fib->entry_list);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200162 if (fib->prefix_ref_count[prefix_len]++ == 0)
163 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
164 return 0;
165}
166
167static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib *fib,
168 struct mlxsw_sp_fib_entry *fib_entry)
169{
170 unsigned char prefix_len = fib_entry->key.prefix_len;
171
172 if (--fib->prefix_ref_count[prefix_len] == 0)
173 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200174 list_del(&fib_entry->list);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200175 rhashtable_remove_fast(&fib->ht, &fib_entry->ht_node,
176 mlxsw_sp_fib_ht_params);
177}
178
179static struct mlxsw_sp_fib_entry *
180mlxsw_sp_fib_entry_create(struct mlxsw_sp_fib *fib, const void *addr,
Jiri Pirko5b004412016-09-01 10:37:40 +0200181 size_t addr_len, unsigned char prefix_len,
182 struct net_device *dev)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200183{
184 struct mlxsw_sp_fib_entry *fib_entry;
185
186 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
187 if (!fib_entry)
188 return NULL;
Jiri Pirko5b004412016-09-01 10:37:40 +0200189 fib_entry->key.dev = dev;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200190 memcpy(fib_entry->key.addr, addr, addr_len);
191 fib_entry->key.prefix_len = prefix_len;
192 return fib_entry;
193}
194
195static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry *fib_entry)
196{
197 kfree(fib_entry);
198}
199
200static struct mlxsw_sp_fib_entry *
201mlxsw_sp_fib_entry_lookup(struct mlxsw_sp_fib *fib, const void *addr,
Jiri Pirko5b004412016-09-01 10:37:40 +0200202 size_t addr_len, unsigned char prefix_len,
203 struct net_device *dev)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200204{
Jiri Pirko5b004412016-09-01 10:37:40 +0200205 struct mlxsw_sp_fib_key key;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200206
Jiri Pirko5b004412016-09-01 10:37:40 +0200207 memset(&key, 0, sizeof(key));
208 key.dev = dev;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200209 memcpy(key.addr, addr, addr_len);
210 key.prefix_len = prefix_len;
211 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
212}
213
214static struct mlxsw_sp_fib *mlxsw_sp_fib_create(void)
215{
216 struct mlxsw_sp_fib *fib;
217 int err;
218
219 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
220 if (!fib)
221 return ERR_PTR(-ENOMEM);
222 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
223 if (err)
224 goto err_rhashtable_init;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200225 INIT_LIST_HEAD(&fib->entry_list);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200226 return fib;
227
228err_rhashtable_init:
229 kfree(fib);
230 return ERR_PTR(err);
231}
232
233static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
234{
235 rhashtable_destroy(&fib->ht);
236 kfree(fib);
237}
238
Jiri Pirko53342022016-07-04 08:23:08 +0200239static struct mlxsw_sp_lpm_tree *
240mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp, bool one_reserved)
241{
242 static struct mlxsw_sp_lpm_tree *lpm_tree;
243 int i;
244
245 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
246 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
247 if (lpm_tree->ref_count == 0) {
248 if (one_reserved)
249 one_reserved = false;
250 else
251 return lpm_tree;
252 }
253 }
254 return NULL;
255}
256
257static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
258 struct mlxsw_sp_lpm_tree *lpm_tree)
259{
260 char ralta_pl[MLXSW_REG_RALTA_LEN];
261
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200262 mlxsw_reg_ralta_pack(ralta_pl, true,
263 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
264 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200265 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
266}
267
268static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
269 struct mlxsw_sp_lpm_tree *lpm_tree)
270{
271 char ralta_pl[MLXSW_REG_RALTA_LEN];
272
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200273 mlxsw_reg_ralta_pack(ralta_pl, false,
274 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
275 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200276 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
277}
278
279static int
280mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
281 struct mlxsw_sp_prefix_usage *prefix_usage,
282 struct mlxsw_sp_lpm_tree *lpm_tree)
283{
284 char ralst_pl[MLXSW_REG_RALST_LEN];
285 u8 root_bin = 0;
286 u8 prefix;
287 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
288
289 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
290 root_bin = prefix;
291
292 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
293 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
294 if (prefix == 0)
295 continue;
296 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
297 MLXSW_REG_RALST_BIN_NO_CHILD);
298 last_prefix = prefix;
299 }
300 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
301}
302
303static struct mlxsw_sp_lpm_tree *
304mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
305 struct mlxsw_sp_prefix_usage *prefix_usage,
306 enum mlxsw_sp_l3proto proto, bool one_reserved)
307{
308 struct mlxsw_sp_lpm_tree *lpm_tree;
309 int err;
310
311 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp, one_reserved);
312 if (!lpm_tree)
313 return ERR_PTR(-EBUSY);
314 lpm_tree->proto = proto;
315 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
316 if (err)
317 return ERR_PTR(err);
318
319 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
320 lpm_tree);
321 if (err)
322 goto err_left_struct_set;
Jiri Pirko2083d362016-10-25 11:25:56 +0200323 memcpy(&lpm_tree->prefix_usage, prefix_usage,
324 sizeof(lpm_tree->prefix_usage));
Jiri Pirko53342022016-07-04 08:23:08 +0200325 return lpm_tree;
326
327err_left_struct_set:
328 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
329 return ERR_PTR(err);
330}
331
332static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
333 struct mlxsw_sp_lpm_tree *lpm_tree)
334{
335 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
336}
337
338static struct mlxsw_sp_lpm_tree *
339mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
340 struct mlxsw_sp_prefix_usage *prefix_usage,
341 enum mlxsw_sp_l3proto proto, bool one_reserved)
342{
343 struct mlxsw_sp_lpm_tree *lpm_tree;
344 int i;
345
346 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
347 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
Jiri Pirko8b99bec2016-10-25 11:25:57 +0200348 if (lpm_tree->ref_count != 0 &&
349 lpm_tree->proto == proto &&
Jiri Pirko53342022016-07-04 08:23:08 +0200350 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
351 prefix_usage))
352 goto inc_ref_count;
353 }
354 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
355 proto, one_reserved);
356 if (IS_ERR(lpm_tree))
357 return lpm_tree;
358
359inc_ref_count:
360 lpm_tree->ref_count++;
361 return lpm_tree;
362}
363
364static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
365 struct mlxsw_sp_lpm_tree *lpm_tree)
366{
367 if (--lpm_tree->ref_count == 0)
368 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
369 return 0;
370}
371
372static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
373{
374 struct mlxsw_sp_lpm_tree *lpm_tree;
375 int i;
376
377 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
378 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
379 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
380 }
381}
382
Jiri Pirko6b75c482016-07-04 08:23:09 +0200383static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
384{
Nogah Frankel9497c042016-09-20 11:16:54 +0200385 struct mlxsw_resources *resources;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200386 struct mlxsw_sp_vr *vr;
387 int i;
388
Nogah Frankel9497c042016-09-20 11:16:54 +0200389 resources = mlxsw_core_resources_get(mlxsw_sp->core);
390 for (i = 0; i < resources->max_virtual_routers; i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200391 vr = &mlxsw_sp->router.vrs[i];
392 if (!vr->used)
393 return vr;
394 }
395 return NULL;
396}
397
398static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
399 struct mlxsw_sp_vr *vr)
400{
401 char raltb_pl[MLXSW_REG_RALTB_LEN];
402
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200403 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
404 (enum mlxsw_reg_ralxx_protocol) vr->proto,
405 vr->lpm_tree->id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200406 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
407}
408
409static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
410 struct mlxsw_sp_vr *vr)
411{
412 char raltb_pl[MLXSW_REG_RALTB_LEN];
413
414 /* Bind to tree 0 which is default */
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200415 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
416 (enum mlxsw_reg_ralxx_protocol) vr->proto, 0);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200417 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
418}
419
420static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
421{
422 /* For our purpose, squash main and local table into one */
423 if (tb_id == RT_TABLE_LOCAL)
424 tb_id = RT_TABLE_MAIN;
425 return tb_id;
426}
427
428static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
429 u32 tb_id,
430 enum mlxsw_sp_l3proto proto)
431{
Nogah Frankel9497c042016-09-20 11:16:54 +0200432 struct mlxsw_resources *resources;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200433 struct mlxsw_sp_vr *vr;
434 int i;
435
436 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Nogah Frankel9497c042016-09-20 11:16:54 +0200437
438 resources = mlxsw_core_resources_get(mlxsw_sp->core);
439 for (i = 0; i < resources->max_virtual_routers; i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200440 vr = &mlxsw_sp->router.vrs[i];
441 if (vr->used && vr->proto == proto && vr->tb_id == tb_id)
442 return vr;
443 }
444 return NULL;
445}
446
447static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
448 unsigned char prefix_len,
449 u32 tb_id,
450 enum mlxsw_sp_l3proto proto)
451{
452 struct mlxsw_sp_prefix_usage req_prefix_usage;
453 struct mlxsw_sp_lpm_tree *lpm_tree;
454 struct mlxsw_sp_vr *vr;
455 int err;
456
457 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
458 if (!vr)
459 return ERR_PTR(-EBUSY);
460 vr->fib = mlxsw_sp_fib_create();
461 if (IS_ERR(vr->fib))
462 return ERR_CAST(vr->fib);
463
464 vr->proto = proto;
465 vr->tb_id = tb_id;
466 mlxsw_sp_prefix_usage_zero(&req_prefix_usage);
467 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
468 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
469 proto, true);
470 if (IS_ERR(lpm_tree)) {
471 err = PTR_ERR(lpm_tree);
472 goto err_tree_get;
473 }
474 vr->lpm_tree = lpm_tree;
475 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
476 if (err)
477 goto err_tree_bind;
478
479 vr->used = true;
480 return vr;
481
482err_tree_bind:
483 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
484err_tree_get:
485 mlxsw_sp_fib_destroy(vr->fib);
486
487 return ERR_PTR(err);
488}
489
490static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
491 struct mlxsw_sp_vr *vr)
492{
493 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
494 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
495 mlxsw_sp_fib_destroy(vr->fib);
496 vr->used = false;
497}
498
499static int
500mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr,
501 struct mlxsw_sp_prefix_usage *req_prefix_usage)
502{
503 struct mlxsw_sp_lpm_tree *lpm_tree;
504
505 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage,
506 &vr->lpm_tree->prefix_usage))
507 return 0;
508
509 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
510 vr->proto, false);
511 if (IS_ERR(lpm_tree)) {
512 /* We failed to get a tree according to the required
513 * prefix usage. However, the current tree might be still good
514 * for us if our requirement is subset of the prefixes used
515 * in the tree.
516 */
517 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
518 &vr->lpm_tree->prefix_usage))
519 return 0;
520 return PTR_ERR(lpm_tree);
521 }
522
523 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
524 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
525 vr->lpm_tree = lpm_tree;
526 return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
527}
528
529static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp,
530 unsigned char prefix_len,
531 u32 tb_id,
532 enum mlxsw_sp_l3proto proto)
533{
534 struct mlxsw_sp_vr *vr;
535 int err;
536
537 tb_id = mlxsw_sp_fix_tb_id(tb_id);
538 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id, proto);
539 if (!vr) {
540 vr = mlxsw_sp_vr_create(mlxsw_sp, prefix_len, tb_id, proto);
541 if (IS_ERR(vr))
542 return vr;
543 } else {
544 struct mlxsw_sp_prefix_usage req_prefix_usage;
545
546 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage,
547 &vr->fib->prefix_usage);
548 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
549 /* Need to replace LPM tree in case new prefix is required. */
550 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
551 &req_prefix_usage);
552 if (err)
553 return ERR_PTR(err);
554 }
555 return vr;
556}
557
558static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
559{
560 /* Destroy virtual router entity in case the associated FIB is empty
561 * and allow it to be used for other tables in future. Otherwise,
562 * check if some prefix usage did not disappear and change tree if
563 * that is the case. Note that in case new, smaller tree cannot be
564 * allocated, the original one will be kept being used.
565 */
566 if (mlxsw_sp_prefix_usage_none(&vr->fib->prefix_usage))
567 mlxsw_sp_vr_destroy(mlxsw_sp, vr);
568 else
569 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
570 &vr->fib->prefix_usage);
571}
572
Nogah Frankel9497c042016-09-20 11:16:54 +0200573static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200574{
Nogah Frankel9497c042016-09-20 11:16:54 +0200575 struct mlxsw_resources *resources;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200576 struct mlxsw_sp_vr *vr;
577 int i;
578
Nogah Frankel9497c042016-09-20 11:16:54 +0200579 resources = mlxsw_core_resources_get(mlxsw_sp->core);
580 if (!resources->max_virtual_routers_valid)
581 return -EIO;
582
583 mlxsw_sp->router.vrs = kcalloc(resources->max_virtual_routers,
584 sizeof(struct mlxsw_sp_vr),
585 GFP_KERNEL);
586 if (!mlxsw_sp->router.vrs)
587 return -ENOMEM;
588
589 for (i = 0; i < resources->max_virtual_routers; i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200590 vr = &mlxsw_sp->router.vrs[i];
591 vr->id = i;
592 }
Nogah Frankel9497c042016-09-20 11:16:54 +0200593
594 return 0;
595}
596
597static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
598{
599 kfree(mlxsw_sp->router.vrs);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200600}
601
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200602struct mlxsw_sp_neigh_key {
Jiri Pirko33b13412016-11-10 12:31:04 +0100603 struct neighbour *n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200604};
605
606struct mlxsw_sp_neigh_entry {
607 struct rhash_head ht_node;
608 struct mlxsw_sp_neigh_key key;
609 u16 rif;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200610 bool offloaded;
611 struct delayed_work dw;
612 struct mlxsw_sp_port *mlxsw_sp_port;
613 unsigned char ha[ETH_ALEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200614 struct list_head nexthop_list; /* list of nexthops using
615 * this neigh entry
616 */
Yotam Gigib2157142016-07-05 11:27:51 +0200617 struct list_head nexthop_neighs_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200618};
619
620static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
621 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
622 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
623 .key_len = sizeof(struct mlxsw_sp_neigh_key),
624};
625
626static int
627mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
628 struct mlxsw_sp_neigh_entry *neigh_entry)
629{
630 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
631 &neigh_entry->ht_node,
632 mlxsw_sp_neigh_ht_params);
633}
634
635static void
636mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
637 struct mlxsw_sp_neigh_entry *neigh_entry)
638{
639 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
640 &neigh_entry->ht_node,
641 mlxsw_sp_neigh_ht_params);
642}
643
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200644static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work);
645
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200646static struct mlxsw_sp_neigh_entry *
Jiri Pirko33b13412016-11-10 12:31:04 +0100647mlxsw_sp_neigh_entry_create(struct neighbour *n, u16 rif)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200648{
649 struct mlxsw_sp_neigh_entry *neigh_entry;
650
651 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_ATOMIC);
652 if (!neigh_entry)
653 return NULL;
Jiri Pirko33b13412016-11-10 12:31:04 +0100654 neigh_entry->key.n = n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200655 neigh_entry->rif = rif;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200656 INIT_DELAYED_WORK(&neigh_entry->dw, mlxsw_sp_router_neigh_update_hw);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200657 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200658 return neigh_entry;
659}
660
661static void
662mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp_neigh_entry *neigh_entry)
663{
664 kfree(neigh_entry);
665}
666
667static struct mlxsw_sp_neigh_entry *
Jiri Pirko33b13412016-11-10 12:31:04 +0100668mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200669{
Jiri Pirko33b13412016-11-10 12:31:04 +0100670 struct mlxsw_sp_neigh_key key;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200671
Jiri Pirko33b13412016-11-10 12:31:04 +0100672 key.n = n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200673 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
674 &key, mlxsw_sp_neigh_ht_params);
675}
676
677int mlxsw_sp_router_neigh_construct(struct net_device *dev,
678 struct neighbour *n)
679{
680 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
681 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
682 struct mlxsw_sp_neigh_entry *neigh_entry;
683 struct mlxsw_sp_rif *r;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200684 int err;
685
686 if (n->tbl != &arp_tbl)
687 return 0;
688
Jiri Pirko33b13412016-11-10 12:31:04 +0100689 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
690 if (neigh_entry)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200691 return 0;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200692
Yotam Gigi51af96b2016-08-24 11:18:52 +0200693 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200694 if (WARN_ON(!r))
695 return -EINVAL;
696
Jiri Pirko33b13412016-11-10 12:31:04 +0100697 neigh_entry = mlxsw_sp_neigh_entry_create(n, r->rif);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200698 if (!neigh_entry)
699 return -ENOMEM;
700 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
701 if (err)
702 goto err_neigh_entry_insert;
703 return 0;
704
705err_neigh_entry_insert:
706 mlxsw_sp_neigh_entry_destroy(neigh_entry);
707 return err;
708}
709
710void mlxsw_sp_router_neigh_destroy(struct net_device *dev,
711 struct neighbour *n)
712{
713 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
714 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
715 struct mlxsw_sp_neigh_entry *neigh_entry;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200716
717 if (n->tbl != &arp_tbl)
718 return;
719
Jiri Pirko33b13412016-11-10 12:31:04 +0100720 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200721 if (!neigh_entry)
722 return;
723 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
724 mlxsw_sp_neigh_entry_destroy(neigh_entry);
725}
726
Yotam Gigic723c7352016-07-05 11:27:43 +0200727static void
728mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
729{
730 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
731
732 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
733}
734
735static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
736 char *rauhtd_pl,
737 int ent_index)
738{
739 struct net_device *dev;
740 struct neighbour *n;
741 __be32 dipn;
742 u32 dip;
743 u16 rif;
744
745 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
746
747 if (!mlxsw_sp->rifs[rif]) {
748 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
749 return;
750 }
751
752 dipn = htonl(dip);
753 dev = mlxsw_sp->rifs[rif]->dev;
754 n = neigh_lookup(&arp_tbl, &dipn, dev);
755 if (!n) {
756 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
757 &dip);
758 return;
759 }
760
761 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
762 neigh_event_send(n, NULL);
763 neigh_release(n);
764}
765
766static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
767 char *rauhtd_pl,
768 int rec_index)
769{
770 u8 num_entries;
771 int i;
772
773 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
774 rec_index);
775 /* Hardware starts counting at 0, so add 1. */
776 num_entries++;
777
778 /* Each record consists of several neighbour entries. */
779 for (i = 0; i < num_entries; i++) {
780 int ent_index;
781
782 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
783 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
784 ent_index);
785 }
786
787}
788
789static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
790 char *rauhtd_pl, int rec_index)
791{
792 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
793 case MLXSW_REG_RAUHTD_TYPE_IPV4:
794 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
795 rec_index);
796 break;
797 case MLXSW_REG_RAUHTD_TYPE_IPV6:
798 WARN_ON_ONCE(1);
799 break;
800 }
801}
802
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100803static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
804{
805 u8 num_rec, last_rec_index, num_entries;
806
807 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
808 last_rec_index = num_rec - 1;
809
810 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
811 return false;
812 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
813 MLXSW_REG_RAUHTD_TYPE_IPV6)
814 return true;
815
816 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
817 last_rec_index);
818 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
819 return true;
820 return false;
821}
822
Yotam Gigib2157142016-07-05 11:27:51 +0200823static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
Yotam Gigic723c7352016-07-05 11:27:43 +0200824{
Yotam Gigic723c7352016-07-05 11:27:43 +0200825 char *rauhtd_pl;
826 u8 num_rec;
827 int i, err;
828
829 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
830 if (!rauhtd_pl)
Yotam Gigib2157142016-07-05 11:27:51 +0200831 return -ENOMEM;
Yotam Gigic723c7352016-07-05 11:27:43 +0200832
833 /* Make sure the neighbour's netdev isn't removed in the
834 * process.
835 */
836 rtnl_lock();
837 do {
838 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
839 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
840 rauhtd_pl);
841 if (err) {
842 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
843 break;
844 }
845 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
846 for (i = 0; i < num_rec; i++)
847 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
848 i);
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100849 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
Yotam Gigic723c7352016-07-05 11:27:43 +0200850 rtnl_unlock();
851
852 kfree(rauhtd_pl);
Yotam Gigib2157142016-07-05 11:27:51 +0200853 return err;
854}
855
856static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
857{
858 struct mlxsw_sp_neigh_entry *neigh_entry;
859
860 /* Take RTNL mutex here to prevent lists from changes */
861 rtnl_lock();
862 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
863 nexthop_neighs_list_node) {
864 /* If this neigh have nexthops, make the kernel think this neigh
865 * is active regardless of the traffic.
866 */
867 if (!list_empty(&neigh_entry->nexthop_list))
Jiri Pirko33b13412016-11-10 12:31:04 +0100868 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigib2157142016-07-05 11:27:51 +0200869 }
870 rtnl_unlock();
871}
872
873static void
874mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
875{
876 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
877
878 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
879 msecs_to_jiffies(interval));
880}
881
882static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
883{
884 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
885 router.neighs_update.dw.work);
886 int err;
887
888 err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp);
889 if (err)
890 dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
891
892 mlxsw_sp_router_neighs_update_nh(mlxsw_sp);
893
Yotam Gigic723c7352016-07-05 11:27:43 +0200894 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
895}
896
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200897static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
898{
899 struct mlxsw_sp_neigh_entry *neigh_entry;
900 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
901 router.nexthop_probe_dw.work);
902
903 /* Iterate over nexthop neighbours, find those who are unresolved and
904 * send arp on them. This solves the chicken-egg problem when
905 * the nexthop wouldn't get offloaded until the neighbor is resolved
906 * but it wouldn't get resolved ever in case traffic is flowing in HW
907 * using different nexthop.
908 *
909 * Take RTNL mutex here to prevent lists from changes.
910 */
911 rtnl_lock();
912 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
913 nexthop_neighs_list_node) {
Jiri Pirko33b13412016-11-10 12:31:04 +0100914 if (!(neigh_entry->key.n->nud_state & NUD_VALID) &&
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200915 !list_empty(&neigh_entry->nexthop_list))
Jiri Pirko33b13412016-11-10 12:31:04 +0100916 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200917 }
918 rtnl_unlock();
919
920 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw,
921 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
922}
923
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200924static void
925mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
926 struct mlxsw_sp_neigh_entry *neigh_entry,
927 bool removing);
928
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200929static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work)
930{
931 struct mlxsw_sp_neigh_entry *neigh_entry =
932 container_of(work, struct mlxsw_sp_neigh_entry, dw.work);
Jiri Pirko33b13412016-11-10 12:31:04 +0100933 struct neighbour *n = neigh_entry->key.n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200934 struct mlxsw_sp_port *mlxsw_sp_port = neigh_entry->mlxsw_sp_port;
935 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
936 char rauht_pl[MLXSW_REG_RAUHT_LEN];
937 struct net_device *dev;
938 bool entry_connected;
939 u8 nud_state;
940 bool updating;
941 bool removing;
942 bool adding;
943 u32 dip;
944 int err;
945
946 read_lock_bh(&n->lock);
947 dip = ntohl(*((__be32 *) n->primary_key));
948 memcpy(neigh_entry->ha, n->ha, sizeof(neigh_entry->ha));
949 nud_state = n->nud_state;
950 dev = n->dev;
951 read_unlock_bh(&n->lock);
952
953 entry_connected = nud_state & NUD_VALID;
954 adding = (!neigh_entry->offloaded) && entry_connected;
955 updating = neigh_entry->offloaded && entry_connected;
956 removing = neigh_entry->offloaded && !entry_connected;
957
958 if (adding || updating) {
959 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_ADD,
960 neigh_entry->rif,
961 neigh_entry->ha, dip);
962 err = mlxsw_reg_write(mlxsw_sp->core,
963 MLXSW_REG(rauht), rauht_pl);
964 if (err) {
965 netdev_err(dev, "Could not add neigh %pI4h\n", &dip);
966 neigh_entry->offloaded = false;
967 } else {
968 neigh_entry->offloaded = true;
969 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200970 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, false);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200971 } else if (removing) {
972 mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE,
973 neigh_entry->rif,
974 neigh_entry->ha, dip);
975 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht),
976 rauht_pl);
977 if (err) {
978 netdev_err(dev, "Could not delete neigh %pI4h\n", &dip);
979 neigh_entry->offloaded = true;
980 } else {
981 neigh_entry->offloaded = false;
982 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200983 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, true);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200984 }
985
986 neigh_release(n);
987 mlxsw_sp_port_dev_put(mlxsw_sp_port);
988}
989
Jiri Pirkoe7322632016-09-01 10:37:43 +0200990int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
991 unsigned long event, void *ptr)
Yotam Gigic723c7352016-07-05 11:27:43 +0200992{
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200993 struct mlxsw_sp_neigh_entry *neigh_entry;
Yotam Gigic723c7352016-07-05 11:27:43 +0200994 struct mlxsw_sp_port *mlxsw_sp_port;
995 struct mlxsw_sp *mlxsw_sp;
996 unsigned long interval;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200997 struct net_device *dev;
Yotam Gigic723c7352016-07-05 11:27:43 +0200998 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200999 struct neighbour *n;
1000 u32 dip;
Yotam Gigic723c7352016-07-05 11:27:43 +02001001
1002 switch (event) {
1003 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
1004 p = ptr;
1005
1006 /* We don't care about changes in the default table. */
1007 if (!p->dev || p->tbl != &arp_tbl)
1008 return NOTIFY_DONE;
1009
1010 /* We are in atomic context and can't take RTNL mutex,
1011 * so use RCU variant to walk the device chain.
1012 */
1013 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
1014 if (!mlxsw_sp_port)
1015 return NOTIFY_DONE;
1016
1017 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1018 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
1019 mlxsw_sp->router.neighs_update.interval = interval;
1020
1021 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1022 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001023 case NETEVENT_NEIGH_UPDATE:
1024 n = ptr;
1025 dev = n->dev;
1026
1027 if (n->tbl != &arp_tbl)
1028 return NOTIFY_DONE;
1029
1030 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(dev);
1031 if (!mlxsw_sp_port)
1032 return NOTIFY_DONE;
1033
1034 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1035 dip = ntohl(*((__be32 *) n->primary_key));
Jiri Pirko33b13412016-11-10 12:31:04 +01001036 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1037 if (WARN_ON(!neigh_entry)) {
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001038 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1039 return NOTIFY_DONE;
1040 }
1041 neigh_entry->mlxsw_sp_port = mlxsw_sp_port;
1042
1043 /* Take a reference to ensure the neighbour won't be
1044 * destructed until we drop the reference in delayed
1045 * work.
1046 */
1047 neigh_clone(n);
1048 if (!mlxsw_core_schedule_dw(&neigh_entry->dw, 0)) {
1049 neigh_release(n);
1050 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1051 }
1052 break;
Yotam Gigic723c7352016-07-05 11:27:43 +02001053 }
1054
1055 return NOTIFY_DONE;
1056}
1057
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001058static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
1059{
Yotam Gigic723c7352016-07-05 11:27:43 +02001060 int err;
1061
1062 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
1063 &mlxsw_sp_neigh_ht_params);
1064 if (err)
1065 return err;
1066
1067 /* Initialize the polling interval according to the default
1068 * table.
1069 */
1070 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1071
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001072 /* Create the delayed works for the activity_update */
Yotam Gigic723c7352016-07-05 11:27:43 +02001073 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
1074 mlxsw_sp_router_neighs_update_work);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001075 INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw,
1076 mlxsw_sp_router_probe_unresolved_nexthops);
Yotam Gigic723c7352016-07-05 11:27:43 +02001077 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001078 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0);
Yotam Gigic723c7352016-07-05 11:27:43 +02001079 return 0;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001080}
1081
1082static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1083{
Yotam Gigic723c7352016-07-05 11:27:43 +02001084 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001085 cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001086 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1087}
1088
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001089struct mlxsw_sp_nexthop {
1090 struct list_head neigh_list_node; /* member of neigh entry list */
1091 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1092 * this belongs to
1093 */
1094 u8 should_offload:1, /* set indicates this neigh is connected and
1095 * should be put to KVD linear area of this group.
1096 */
1097 offloaded:1, /* set in case the neigh is actually put into
1098 * KVD linear area of this group.
1099 */
1100 update:1; /* set indicates that MAC of this neigh should be
1101 * updated in HW
1102 */
1103 struct mlxsw_sp_neigh_entry *neigh_entry;
1104};
1105
1106struct mlxsw_sp_nexthop_group {
1107 struct list_head list; /* node in mlxsw->router.nexthop_group_list */
1108 struct list_head fib_list; /* list of fib entries that use this group */
1109 u8 adj_index_valid:1;
1110 u32 adj_index;
1111 u16 ecmp_size;
1112 u16 count;
1113 struct mlxsw_sp_nexthop nexthops[0];
1114};
1115
1116static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
1117 struct mlxsw_sp_vr *vr,
1118 u32 adj_index, u16 ecmp_size,
1119 u32 new_adj_index,
1120 u16 new_ecmp_size)
1121{
1122 char raleu_pl[MLXSW_REG_RALEU_LEN];
1123
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001124 mlxsw_reg_raleu_pack(raleu_pl,
1125 (enum mlxsw_reg_ralxx_protocol) vr->proto, vr->id,
1126 adj_index, ecmp_size, new_adj_index,
1127 new_ecmp_size);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001128 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1129}
1130
1131static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1132 struct mlxsw_sp_nexthop_group *nh_grp,
1133 u32 old_adj_index, u16 old_ecmp_size)
1134{
1135 struct mlxsw_sp_fib_entry *fib_entry;
1136 struct mlxsw_sp_vr *vr = NULL;
1137 int err;
1138
1139 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1140 if (vr == fib_entry->vr)
1141 continue;
1142 vr = fib_entry->vr;
1143 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, vr,
1144 old_adj_index,
1145 old_ecmp_size,
1146 nh_grp->adj_index,
1147 nh_grp->ecmp_size);
1148 if (err)
1149 return err;
1150 }
1151 return 0;
1152}
1153
1154static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1155 struct mlxsw_sp_nexthop *nh)
1156{
1157 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1158 char ratr_pl[MLXSW_REG_RATR_LEN];
1159
1160 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1161 true, adj_index, neigh_entry->rif);
1162 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1163 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1164}
1165
1166static int
1167mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
1168 struct mlxsw_sp_nexthop_group *nh_grp)
1169{
1170 u32 adj_index = nh_grp->adj_index; /* base */
1171 struct mlxsw_sp_nexthop *nh;
1172 int i;
1173 int err;
1174
1175 for (i = 0; i < nh_grp->count; i++) {
1176 nh = &nh_grp->nexthops[i];
1177
1178 if (!nh->should_offload) {
1179 nh->offloaded = 0;
1180 continue;
1181 }
1182
1183 if (nh->update) {
1184 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1185 adj_index, nh);
1186 if (err)
1187 return err;
1188 nh->update = 0;
1189 nh->offloaded = 1;
1190 }
1191 adj_index++;
1192 }
1193 return 0;
1194}
1195
1196static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1197 struct mlxsw_sp_fib_entry *fib_entry);
1198
1199static int
1200mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1201 struct mlxsw_sp_nexthop_group *nh_grp)
1202{
1203 struct mlxsw_sp_fib_entry *fib_entry;
1204 int err;
1205
1206 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1207 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1208 if (err)
1209 return err;
1210 }
1211 return 0;
1212}
1213
1214static void
1215mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1216 struct mlxsw_sp_nexthop_group *nh_grp)
1217{
1218 struct mlxsw_sp_nexthop *nh;
1219 bool offload_change = false;
1220 u32 adj_index;
1221 u16 ecmp_size = 0;
1222 bool old_adj_index_valid;
1223 u32 old_adj_index;
1224 u16 old_ecmp_size;
1225 int ret;
1226 int i;
1227 int err;
1228
1229 for (i = 0; i < nh_grp->count; i++) {
1230 nh = &nh_grp->nexthops[i];
1231
1232 if (nh->should_offload ^ nh->offloaded) {
1233 offload_change = true;
1234 if (nh->should_offload)
1235 nh->update = 1;
1236 }
1237 if (nh->should_offload)
1238 ecmp_size++;
1239 }
1240 if (!offload_change) {
1241 /* Nothing was added or removed, so no need to reallocate. Just
1242 * update MAC on existing adjacency indexes.
1243 */
1244 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp);
1245 if (err) {
1246 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1247 goto set_trap;
1248 }
1249 return;
1250 }
1251 if (!ecmp_size)
1252 /* No neigh of this group is connected so we just set
1253 * the trap and let everthing flow through kernel.
1254 */
1255 goto set_trap;
1256
1257 ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size);
1258 if (ret < 0) {
1259 /* We ran out of KVD linear space, just set the
1260 * trap and let everything flow through kernel.
1261 */
1262 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1263 goto set_trap;
1264 }
1265 adj_index = ret;
1266 old_adj_index_valid = nh_grp->adj_index_valid;
1267 old_adj_index = nh_grp->adj_index;
1268 old_ecmp_size = nh_grp->ecmp_size;
1269 nh_grp->adj_index_valid = 1;
1270 nh_grp->adj_index = adj_index;
1271 nh_grp->ecmp_size = ecmp_size;
1272 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp);
1273 if (err) {
1274 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1275 goto set_trap;
1276 }
1277
1278 if (!old_adj_index_valid) {
1279 /* The trap was set for fib entries, so we have to call
1280 * fib entry update to unset it and use adjacency index.
1281 */
1282 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1283 if (err) {
1284 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1285 goto set_trap;
1286 }
1287 return;
1288 }
1289
1290 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1291 old_adj_index, old_ecmp_size);
1292 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1293 if (err) {
1294 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1295 goto set_trap;
1296 }
1297 return;
1298
1299set_trap:
1300 old_adj_index_valid = nh_grp->adj_index_valid;
1301 nh_grp->adj_index_valid = 0;
1302 for (i = 0; i < nh_grp->count; i++) {
1303 nh = &nh_grp->nexthops[i];
1304 nh->offloaded = 0;
1305 }
1306 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1307 if (err)
1308 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1309 if (old_adj_index_valid)
1310 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1311}
1312
1313static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1314 bool removing)
1315{
1316 if (!removing && !nh->should_offload)
1317 nh->should_offload = 1;
1318 else if (removing && nh->offloaded)
1319 nh->should_offload = 0;
1320 nh->update = 1;
1321}
1322
1323static void
1324mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1325 struct mlxsw_sp_neigh_entry *neigh_entry,
1326 bool removing)
1327{
1328 struct mlxsw_sp_nexthop *nh;
1329
1330 /* Take RTNL mutex here to prevent lists from changes */
1331 rtnl_lock();
1332 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1333 neigh_list_node) {
1334 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1335 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1336 }
1337 rtnl_unlock();
1338}
1339
1340static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp,
1341 struct mlxsw_sp_nexthop_group *nh_grp,
1342 struct mlxsw_sp_nexthop *nh,
1343 struct fib_nh *fib_nh)
1344{
1345 struct mlxsw_sp_neigh_entry *neigh_entry;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001346 struct net_device *dev = fib_nh->nh_dev;
1347 struct neighbour *n;
1348 u8 nud_state;
1349
Jiri Pirko33b13412016-11-10 12:31:04 +01001350 /* Take a reference of neigh here ensuring that neigh would
1351 * not be detructed before the nexthop entry is finished.
1352 * The reference is taken either in neigh_lookup() or
1353 * in neith_create() in case n is not found.
1354 */
1355 n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, dev);
1356 if (!n) {
1357 n = neigh_create(&arp_tbl, &fib_nh->nh_gw, dev);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001358 if (IS_ERR(n))
1359 return PTR_ERR(n);
1360 neigh_event_send(n, NULL);
Jiri Pirko33b13412016-11-10 12:31:04 +01001361 }
1362 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1363 if (!neigh_entry) {
1364 neigh_release(n);
1365 return -EINVAL;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001366 }
Yotam Gigib2157142016-07-05 11:27:51 +02001367
1368 /* If that is the first nexthop connected to that neigh, add to
1369 * nexthop_neighs_list
1370 */
1371 if (list_empty(&neigh_entry->nexthop_list))
1372 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
1373 &mlxsw_sp->router.nexthop_neighs_list);
1374
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001375 nh->nh_grp = nh_grp;
1376 nh->neigh_entry = neigh_entry;
1377 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1378 read_lock_bh(&n->lock);
1379 nud_state = n->nud_state;
1380 read_unlock_bh(&n->lock);
1381 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID));
1382
1383 return 0;
1384}
1385
1386static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1387 struct mlxsw_sp_nexthop *nh)
1388{
1389 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1390
1391 list_del(&nh->neigh_list_node);
Yotam Gigib2157142016-07-05 11:27:51 +02001392
1393 /* If that is the last nexthop connected to that neigh, remove from
1394 * nexthop_neighs_list
1395 */
1396 if (list_empty(&nh->neigh_entry->nexthop_list))
1397 list_del(&nh->neigh_entry->nexthop_neighs_list_node);
1398
Jiri Pirko33b13412016-11-10 12:31:04 +01001399 neigh_release(neigh_entry->key.n);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001400}
1401
1402static struct mlxsw_sp_nexthop_group *
1403mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1404{
1405 struct mlxsw_sp_nexthop_group *nh_grp;
1406 struct mlxsw_sp_nexthop *nh;
1407 struct fib_nh *fib_nh;
1408 size_t alloc_size;
1409 int i;
1410 int err;
1411
1412 alloc_size = sizeof(*nh_grp) +
1413 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1414 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1415 if (!nh_grp)
1416 return ERR_PTR(-ENOMEM);
1417 INIT_LIST_HEAD(&nh_grp->fib_list);
1418 nh_grp->count = fi->fib_nhs;
1419 for (i = 0; i < nh_grp->count; i++) {
1420 nh = &nh_grp->nexthops[i];
1421 fib_nh = &fi->fib_nh[i];
1422 err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh);
1423 if (err)
1424 goto err_nexthop_init;
1425 }
1426 list_add_tail(&nh_grp->list, &mlxsw_sp->router.nexthop_group_list);
1427 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1428 return nh_grp;
1429
1430err_nexthop_init:
1431 for (i--; i >= 0; i--)
1432 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1433 kfree(nh_grp);
1434 return ERR_PTR(err);
1435}
1436
1437static void
1438mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1439 struct mlxsw_sp_nexthop_group *nh_grp)
1440{
1441 struct mlxsw_sp_nexthop *nh;
1442 int i;
1443
1444 list_del(&nh_grp->list);
1445 for (i = 0; i < nh_grp->count; i++) {
1446 nh = &nh_grp->nexthops[i];
1447 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1448 }
1449 kfree(nh_grp);
1450}
1451
1452static bool mlxsw_sp_nexthop_match(struct mlxsw_sp_nexthop *nh,
1453 struct fib_info *fi)
1454{
1455 int i;
1456
1457 for (i = 0; i < fi->fib_nhs; i++) {
1458 struct fib_nh *fib_nh = &fi->fib_nh[i];
Jiri Pirko33b13412016-11-10 12:31:04 +01001459 struct neighbour *n = nh->neigh_entry->key.n;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001460
Jiri Pirko33b13412016-11-10 12:31:04 +01001461 if (memcmp(n->primary_key, &fib_nh->nh_gw,
1462 sizeof(fib_nh->nh_gw)) == 0 &&
1463 n->dev == fib_nh->nh_dev)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001464 return true;
1465 }
1466 return false;
1467}
1468
1469static bool mlxsw_sp_nexthop_group_match(struct mlxsw_sp_nexthop_group *nh_grp,
1470 struct fib_info *fi)
1471{
1472 int i;
1473
1474 if (nh_grp->count != fi->fib_nhs)
1475 return false;
1476 for (i = 0; i < nh_grp->count; i++) {
1477 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
1478
1479 if (!mlxsw_sp_nexthop_match(nh, fi))
1480 return false;
1481 }
1482 return true;
1483}
1484
1485static struct mlxsw_sp_nexthop_group *
1486mlxsw_sp_nexthop_group_find(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1487{
1488 struct mlxsw_sp_nexthop_group *nh_grp;
1489
1490 list_for_each_entry(nh_grp, &mlxsw_sp->router.nexthop_group_list,
1491 list) {
1492 if (mlxsw_sp_nexthop_group_match(nh_grp, fi))
1493 return nh_grp;
1494 }
1495 return NULL;
1496}
1497
1498static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1499 struct mlxsw_sp_fib_entry *fib_entry,
1500 struct fib_info *fi)
1501{
1502 struct mlxsw_sp_nexthop_group *nh_grp;
1503
1504 nh_grp = mlxsw_sp_nexthop_group_find(mlxsw_sp, fi);
1505 if (!nh_grp) {
1506 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1507 if (IS_ERR(nh_grp))
1508 return PTR_ERR(nh_grp);
1509 }
1510 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1511 fib_entry->nh_group = nh_grp;
1512 return 0;
1513}
1514
1515static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1516 struct mlxsw_sp_fib_entry *fib_entry)
1517{
1518 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1519
1520 list_del(&fib_entry->nexthop_group_node);
1521 if (!list_empty(&nh_grp->fib_list))
1522 return;
1523 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1524}
1525
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001526static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp,
1527 struct mlxsw_sp_fib_entry *fib_entry,
1528 enum mlxsw_reg_ralue_op op)
1529{
1530 char ralue_pl[MLXSW_REG_RALUE_LEN];
1531 u32 *p_dip = (u32 *) fib_entry->key.addr;
1532 struct mlxsw_sp_vr *vr = fib_entry->vr;
1533 enum mlxsw_reg_ralue_trap_action trap_action;
1534 u16 trap_id = 0;
1535 u32 adjacency_index = 0;
1536 u16 ecmp_size = 0;
1537
1538 /* In case the nexthop group adjacency index is valid, use it
1539 * with provided ECMP size. Otherwise, setup trap and pass
1540 * traffic to kernel.
1541 */
1542 if (fib_entry->nh_group->adj_index_valid) {
1543 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1544 adjacency_index = fib_entry->nh_group->adj_index;
1545 ecmp_size = fib_entry->nh_group->ecmp_size;
1546 } else {
1547 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1548 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1549 }
1550
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001551 mlxsw_reg_ralue_pack4(ralue_pl,
1552 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1553 vr->id, fib_entry->key.prefix_len, *p_dip);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001554 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
1555 adjacency_index, ecmp_size);
1556 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1557}
1558
Jiri Pirko61c503f2016-07-04 08:23:11 +02001559static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1560 struct mlxsw_sp_fib_entry *fib_entry,
1561 enum mlxsw_reg_ralue_op op)
1562{
1563 char ralue_pl[MLXSW_REG_RALUE_LEN];
1564 u32 *p_dip = (u32 *) fib_entry->key.addr;
1565 struct mlxsw_sp_vr *vr = fib_entry->vr;
1566
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001567 mlxsw_reg_ralue_pack4(ralue_pl,
1568 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1569 vr->id, fib_entry->key.prefix_len, *p_dip);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001570 mlxsw_reg_ralue_act_local_pack(ralue_pl,
1571 MLXSW_REG_RALUE_TRAP_ACTION_NOP, 0,
1572 fib_entry->rif);
1573 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1574}
1575
1576static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1577 struct mlxsw_sp_fib_entry *fib_entry,
1578 enum mlxsw_reg_ralue_op op)
1579{
1580 char ralue_pl[MLXSW_REG_RALUE_LEN];
1581 u32 *p_dip = (u32 *) fib_entry->key.addr;
1582 struct mlxsw_sp_vr *vr = fib_entry->vr;
1583
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001584 mlxsw_reg_ralue_pack4(ralue_pl,
1585 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1586 vr->id, fib_entry->key.prefix_len, *p_dip);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001587 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1588 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1589}
1590
1591static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1592 struct mlxsw_sp_fib_entry *fib_entry,
1593 enum mlxsw_reg_ralue_op op)
1594{
1595 switch (fib_entry->type) {
1596 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001597 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001598 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1599 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1600 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1601 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1602 }
1603 return -EINVAL;
1604}
1605
1606static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1607 struct mlxsw_sp_fib_entry *fib_entry,
1608 enum mlxsw_reg_ralue_op op)
1609{
1610 switch (fib_entry->vr->proto) {
1611 case MLXSW_SP_L3_PROTO_IPV4:
1612 return mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1613 case MLXSW_SP_L3_PROTO_IPV6:
1614 return -EINVAL;
1615 }
1616 return -EINVAL;
1617}
1618
1619static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1620 struct mlxsw_sp_fib_entry *fib_entry)
1621{
Jiri Pirko7146da32016-09-01 10:37:41 +02001622 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1623 MLXSW_REG_RALUE_OP_WRITE_WRITE);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001624}
1625
1626static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1627 struct mlxsw_sp_fib_entry *fib_entry)
1628{
1629 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1630 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1631}
1632
Jiri Pirko61c503f2016-07-04 08:23:11 +02001633static int
1634mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp *mlxsw_sp,
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001635 const struct fib_entry_notifier_info *fen_info,
Jiri Pirko61c503f2016-07-04 08:23:11 +02001636 struct mlxsw_sp_fib_entry *fib_entry)
1637{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001638 struct fib_info *fi = fen_info->fi;
Arnd Bergmannab580702016-09-30 18:17:10 +02001639 struct mlxsw_sp_rif *r = NULL;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001640 int nhsel;
1641 int err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001642
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001643 if (fen_info->type == RTN_LOCAL || fen_info->type == RTN_BROADCAST) {
Jiri Pirko61c503f2016-07-04 08:23:11 +02001644 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1645 return 0;
1646 }
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001647 if (fen_info->type != RTN_UNICAST)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001648 return -EINVAL;
1649
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001650 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1651 const struct fib_nh *nh = &fi->fib_nh[nhsel];
Jiri Pirko61c503f2016-07-04 08:23:11 +02001652
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001653 if (!nh->nh_dev)
1654 continue;
1655 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, nh->nh_dev);
1656 if (!r) {
1657 /* In case router interface is not found for
1658 * at least one of the nexthops, that means
1659 * the nexthop points to some device unrelated
1660 * to us. Set trap and pass the packets for
1661 * this prefix to kernel.
1662 */
Arnd Bergmannab580702016-09-30 18:17:10 +02001663 break;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001664 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02001665 }
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001666
Arnd Bergmannab580702016-09-30 18:17:10 +02001667 if (!r) {
1668 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1669 return 0;
1670 }
1671
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001672 if (fi->fib_scope != RT_SCOPE_UNIVERSE) {
1673 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1674 fib_entry->rif = r->rif;
1675 } else {
1676 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
1677 err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fi);
1678 if (err)
1679 return err;
1680 }
1681 fib_info_offload_inc(fen_info->fi);
1682 return 0;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001683}
1684
1685static void
1686mlxsw_sp_router_fib4_entry_fini(struct mlxsw_sp *mlxsw_sp,
1687 struct mlxsw_sp_fib_entry *fib_entry)
1688{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001689 if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_TRAP)
1690 fib_info_offload_dec(fib_entry->fi);
1691 if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_REMOTE)
1692 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001693}
1694
Jiri Pirko5b004412016-09-01 10:37:40 +02001695static struct mlxsw_sp_fib_entry *
1696mlxsw_sp_fib_entry_get(struct mlxsw_sp *mlxsw_sp,
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001697 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02001698{
1699 struct mlxsw_sp_fib_entry *fib_entry;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001700 struct fib_info *fi = fen_info->fi;
Jiri Pirko5b004412016-09-01 10:37:40 +02001701 struct mlxsw_sp_vr *vr;
1702 int err;
1703
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001704 vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->dst_len, fen_info->tb_id,
Jiri Pirko5b004412016-09-01 10:37:40 +02001705 MLXSW_SP_L3_PROTO_IPV4);
1706 if (IS_ERR(vr))
1707 return ERR_CAST(vr);
1708
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001709 fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst,
1710 sizeof(fen_info->dst),
1711 fen_info->dst_len, fi->fib_dev);
Jiri Pirko5b004412016-09-01 10:37:40 +02001712 if (fib_entry) {
1713 /* Already exists, just take a reference */
1714 fib_entry->ref_count++;
1715 return fib_entry;
1716 }
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001717 fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fen_info->dst,
1718 sizeof(fen_info->dst),
1719 fen_info->dst_len, fi->fib_dev);
Jiri Pirko5b004412016-09-01 10:37:40 +02001720 if (!fib_entry) {
1721 err = -ENOMEM;
1722 goto err_fib_entry_create;
1723 }
1724 fib_entry->vr = vr;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001725 fib_entry->fi = fi;
Jiri Pirko5b004412016-09-01 10:37:40 +02001726 fib_entry->ref_count = 1;
1727
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001728 err = mlxsw_sp_router_fib4_entry_init(mlxsw_sp, fen_info, fib_entry);
Jiri Pirko5b004412016-09-01 10:37:40 +02001729 if (err)
1730 goto err_fib4_entry_init;
1731
1732 return fib_entry;
1733
1734err_fib4_entry_init:
1735 mlxsw_sp_fib_entry_destroy(fib_entry);
1736err_fib_entry_create:
1737 mlxsw_sp_vr_put(mlxsw_sp, vr);
1738
1739 return ERR_PTR(err);
1740}
1741
1742static struct mlxsw_sp_fib_entry *
1743mlxsw_sp_fib_entry_find(struct mlxsw_sp *mlxsw_sp,
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001744 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02001745{
1746 struct mlxsw_sp_vr *vr;
1747
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001748 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id,
1749 MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirko5b004412016-09-01 10:37:40 +02001750 if (!vr)
1751 return NULL;
1752
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001753 return mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst,
1754 sizeof(fen_info->dst),
1755 fen_info->dst_len,
1756 fen_info->fi->fib_dev);
Jiri Pirko5b004412016-09-01 10:37:40 +02001757}
1758
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001759static void mlxsw_sp_fib_entry_put(struct mlxsw_sp *mlxsw_sp,
1760 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko5b004412016-09-01 10:37:40 +02001761{
1762 struct mlxsw_sp_vr *vr = fib_entry->vr;
1763
1764 if (--fib_entry->ref_count == 0) {
1765 mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry);
1766 mlxsw_sp_fib_entry_destroy(fib_entry);
1767 }
1768 mlxsw_sp_vr_put(mlxsw_sp, vr);
1769}
1770
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001771static void mlxsw_sp_fib_entry_put_all(struct mlxsw_sp *mlxsw_sp,
1772 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001773{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001774 unsigned int last_ref_count;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001775
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001776 do {
1777 last_ref_count = fib_entry->ref_count;
1778 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
1779 } while (last_ref_count != 1);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001780}
1781
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001782static int mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
1783 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001784{
Jiri Pirko61c503f2016-07-04 08:23:11 +02001785 struct mlxsw_sp_fib_entry *fib_entry;
1786 struct mlxsw_sp_vr *vr;
1787 int err;
1788
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001789 if (mlxsw_sp->router.aborted)
1790 return 0;
1791
1792 fib_entry = mlxsw_sp_fib_entry_get(mlxsw_sp, fen_info);
1793 if (IS_ERR(fib_entry)) {
1794 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB4 entry being added.\n");
1795 return PTR_ERR(fib_entry);
1796 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02001797
Jiri Pirko5b004412016-09-01 10:37:40 +02001798 if (fib_entry->ref_count != 1)
1799 return 0;
1800
Jiri Pirko61c503f2016-07-04 08:23:11 +02001801 vr = fib_entry->vr;
Jiri Pirko5b004412016-09-01 10:37:40 +02001802 err = mlxsw_sp_fib_entry_insert(vr->fib, fib_entry);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001803 if (err) {
1804 dev_warn(mlxsw_sp->bus_info->dev, "Failed to insert FIB4 entry being added.\n");
Jiri Pirko61c503f2016-07-04 08:23:11 +02001805 goto err_fib_entry_insert;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001806 }
1807 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001808 if (err)
1809 goto err_fib_entry_add;
1810 return 0;
1811
1812err_fib_entry_add:
1813 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
1814err_fib_entry_insert:
Jiri Pirko5b004412016-09-01 10:37:40 +02001815 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001816 return err;
1817}
1818
Jiri Pirko37956d72016-10-20 16:05:43 +02001819static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
1820 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001821{
Jiri Pirko61c503f2016-07-04 08:23:11 +02001822 struct mlxsw_sp_fib_entry *fib_entry;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001823
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001824 if (mlxsw_sp->router.aborted)
Jiri Pirko37956d72016-10-20 16:05:43 +02001825 return;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001826
1827 fib_entry = mlxsw_sp_fib_entry_find(mlxsw_sp, fen_info);
Jiri Pirko37956d72016-10-20 16:05:43 +02001828 if (!fib_entry)
1829 return;
Jiri Pirko5b004412016-09-01 10:37:40 +02001830
1831 if (fib_entry->ref_count == 1) {
1832 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
1833 mlxsw_sp_fib_entry_remove(fib_entry->vr->fib, fib_entry);
1834 }
1835
1836 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001837}
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001838
1839static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
1840{
1841 char ralta_pl[MLXSW_REG_RALTA_LEN];
1842 char ralst_pl[MLXSW_REG_RALST_LEN];
1843 char raltb_pl[MLXSW_REG_RALTB_LEN];
1844 char ralue_pl[MLXSW_REG_RALUE_LEN];
1845 int err;
1846
1847 mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4,
1848 MLXSW_SP_LPM_TREE_MIN);
1849 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
1850 if (err)
1851 return err;
1852
1853 mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN);
1854 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
1855 if (err)
1856 return err;
1857
Jiri Pirko19271c12016-10-20 16:05:42 +02001858 mlxsw_reg_raltb_pack(raltb_pl, 0, MLXSW_REG_RALXX_PROTOCOL_IPV4,
1859 MLXSW_SP_LPM_TREE_MIN);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001860 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
1861 if (err)
1862 return err;
1863
1864 mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4,
1865 MLXSW_REG_RALUE_OP_WRITE_WRITE, 0, 0, 0);
1866 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1867 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1868}
1869
1870static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp)
1871{
1872 struct mlxsw_resources *resources;
1873 struct mlxsw_sp_fib_entry *fib_entry;
1874 struct mlxsw_sp_fib_entry *tmp;
1875 struct mlxsw_sp_vr *vr;
1876 int i;
1877 int err;
1878
1879 resources = mlxsw_core_resources_get(mlxsw_sp->core);
1880 for (i = 0; i < resources->max_virtual_routers; i++) {
1881 vr = &mlxsw_sp->router.vrs[i];
1882 if (!vr->used)
1883 continue;
1884
1885 list_for_each_entry_safe(fib_entry, tmp,
1886 &vr->fib->entry_list, list) {
1887 bool do_break = &tmp->list == &vr->fib->entry_list;
1888
1889 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
1890 mlxsw_sp_fib_entry_remove(fib_entry->vr->fib,
1891 fib_entry);
1892 mlxsw_sp_fib_entry_put_all(mlxsw_sp, fib_entry);
1893 if (do_break)
1894 break;
1895 }
1896 }
1897 mlxsw_sp->router.aborted = true;
1898 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
1899 if (err)
1900 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
1901}
1902
1903static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
1904{
1905 struct mlxsw_resources *resources;
1906 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1907 int err;
1908
1909 resources = mlxsw_core_resources_get(mlxsw_sp->core);
1910 if (!resources->max_rif_valid)
1911 return -EIO;
1912
1913 mlxsw_sp->rifs = kcalloc(resources->max_rif,
1914 sizeof(struct mlxsw_sp_rif *), GFP_KERNEL);
1915 if (!mlxsw_sp->rifs)
1916 return -ENOMEM;
1917
1918 mlxsw_reg_rgcr_pack(rgcr_pl, true);
1919 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, resources->max_rif);
1920 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1921 if (err)
1922 goto err_rgcr_fail;
1923
1924 return 0;
1925
1926err_rgcr_fail:
1927 kfree(mlxsw_sp->rifs);
1928 return err;
1929}
1930
1931static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
1932{
1933 struct mlxsw_resources *resources;
1934 char rgcr_pl[MLXSW_REG_RGCR_LEN];
1935 int i;
1936
1937 mlxsw_reg_rgcr_pack(rgcr_pl, false);
1938 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
1939
1940 resources = mlxsw_core_resources_get(mlxsw_sp->core);
1941 for (i = 0; i < resources->max_rif; i++)
1942 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
1943
1944 kfree(mlxsw_sp->rifs);
1945}
1946
1947static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
1948 unsigned long event, void *ptr)
1949{
1950 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
1951 struct fib_entry_notifier_info *fen_info = ptr;
1952 int err;
1953
Jiri Pirko0e3715c2016-11-10 12:31:05 +01001954 if (!net_eq(fen_info->info.net, &init_net))
1955 return NOTIFY_DONE;
1956
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001957 switch (event) {
1958 case FIB_EVENT_ENTRY_ADD:
1959 err = mlxsw_sp_router_fib4_add(mlxsw_sp, fen_info);
1960 if (err)
1961 mlxsw_sp_router_fib4_abort(mlxsw_sp);
1962 break;
1963 case FIB_EVENT_ENTRY_DEL:
1964 mlxsw_sp_router_fib4_del(mlxsw_sp, fen_info);
1965 break;
1966 case FIB_EVENT_RULE_ADD: /* fall through */
1967 case FIB_EVENT_RULE_DEL:
1968 mlxsw_sp_router_fib4_abort(mlxsw_sp);
1969 break;
1970 }
1971 return NOTIFY_DONE;
1972}
1973
1974int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
1975{
1976 int err;
1977
1978 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
1979 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_group_list);
1980 err = __mlxsw_sp_router_init(mlxsw_sp);
1981 if (err)
1982 return err;
1983
1984 mlxsw_sp_lpm_init(mlxsw_sp);
1985 err = mlxsw_sp_vrs_init(mlxsw_sp);
1986 if (err)
1987 goto err_vrs_init;
1988
1989 err = mlxsw_sp_neigh_init(mlxsw_sp);
1990 if (err)
1991 goto err_neigh_init;
1992
1993 mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
1994 register_fib_notifier(&mlxsw_sp->fib_nb);
1995 return 0;
1996
1997err_neigh_init:
1998 mlxsw_sp_vrs_fini(mlxsw_sp);
1999err_vrs_init:
2000 __mlxsw_sp_router_fini(mlxsw_sp);
2001 return err;
2002}
2003
2004void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
2005{
2006 unregister_fib_notifier(&mlxsw_sp->fib_nb);
2007 mlxsw_sp_neigh_fini(mlxsw_sp);
2008 mlxsw_sp_vrs_fini(mlxsw_sp);
2009 __mlxsw_sp_router_fini(mlxsw_sp);
2010}