blob: fe4a55e3272befaed619814b090c3a0a298ef116 [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>
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +010043#include <linux/inetdevice.h>
Ido Schimmel9db032b2017-03-16 09:08:17 +010044#include <linux/netdevice.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020045#include <net/netevent.h>
Jiri Pirko6cf3c972016-07-05 11:27:39 +020046#include <net/neighbour.h>
47#include <net/arp.h>
Jiri Pirkob45f64d2016-09-26 12:52:31 +020048#include <net/ip_fib.h>
Ido Schimmel5d7bfd12017-03-16 09:08:14 +010049#include <net/fib_rules.h>
Ido Schimmel57837882017-03-16 09:08:16 +010050#include <net/l3mdev.h>
Ido Schimmel464dce12016-07-02 11:00:15 +020051
52#include "spectrum.h"
53#include "core.h"
54#include "reg.h"
55
Ido Schimmel4724ba562017-03-10 08:53:39 +010056struct mlxsw_sp_rif {
57 struct list_head nexthop_list;
58 struct list_head neigh_list;
59 struct net_device *dev;
60 struct mlxsw_sp_fid *f;
61 unsigned char addr[ETH_ALEN];
62 int mtu;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +010063 u16 rif_index;
Ido Schimmel69132292017-03-10 08:53:42 +010064 u16 vr_id;
Ido Schimmel4724ba562017-03-10 08:53:39 +010065};
66
67static struct mlxsw_sp_rif *
68mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
69 const struct net_device *dev);
70
Jiri Pirko53342022016-07-04 08:23:08 +020071#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
72 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
73
74static bool
Jiri Pirko6b75c482016-07-04 08:23:09 +020075mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
76 struct mlxsw_sp_prefix_usage *prefix_usage2)
77{
78 unsigned char prefix;
79
80 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
81 if (!test_bit(prefix, prefix_usage2->b))
82 return false;
83 }
84 return true;
85}
86
87static bool
Jiri Pirko53342022016-07-04 08:23:08 +020088mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
89 struct mlxsw_sp_prefix_usage *prefix_usage2)
90{
91 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
92}
93
Jiri Pirko6b75c482016-07-04 08:23:09 +020094static bool
95mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
96{
97 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
98
99 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
100}
101
102static void
103mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
104 struct mlxsw_sp_prefix_usage *prefix_usage2)
105{
106 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
107}
108
109static void
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200110mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
111 unsigned char prefix_len)
112{
113 set_bit(prefix_len, prefix_usage->b);
114}
115
116static void
117mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
118 unsigned char prefix_len)
119{
120 clear_bit(prefix_len, prefix_usage->b);
121}
122
123struct mlxsw_sp_fib_key {
124 unsigned char addr[sizeof(struct in6_addr)];
125 unsigned char prefix_len;
126};
127
Jiri Pirko61c503f2016-07-04 08:23:11 +0200128enum mlxsw_sp_fib_entry_type {
129 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
130 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
131 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
132};
133
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200134struct mlxsw_sp_nexthop_group;
135
Ido Schimmel9aecce12017-02-09 10:28:42 +0100136struct mlxsw_sp_fib_node {
137 struct list_head entry_list;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200138 struct list_head list;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100139 struct rhash_head ht_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100140 struct mlxsw_sp_fib *fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100141 struct mlxsw_sp_fib_key key;
142};
143
144struct mlxsw_sp_fib_entry_params {
145 u32 tb_id;
146 u32 prio;
147 u8 tos;
148 u8 type;
149};
150
151struct mlxsw_sp_fib_entry {
152 struct list_head list;
153 struct mlxsw_sp_fib_node *fib_node;
154 enum mlxsw_sp_fib_entry_type type;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200155 struct list_head nexthop_group_node;
156 struct mlxsw_sp_nexthop_group *nh_group;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100157 struct mlxsw_sp_fib_entry_params params;
Ido Schimmel013b20f2017-02-08 11:16:36 +0100158 bool offloaded;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200159};
160
161struct mlxsw_sp_fib {
162 struct rhashtable ht;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100163 struct list_head node_list;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100164 struct mlxsw_sp_vr *vr;
165 struct mlxsw_sp_lpm_tree *lpm_tree;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200166 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
167 struct mlxsw_sp_prefix_usage prefix_usage;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100168 enum mlxsw_sp_l3proto proto;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200169};
170
Ido Schimmel9aecce12017-02-09 10:28:42 +0100171static const struct rhashtable_params mlxsw_sp_fib_ht_params;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200172
Ido Schimmel76610eb2017-03-10 08:53:41 +0100173static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp_vr *vr,
174 enum mlxsw_sp_l3proto proto)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200175{
176 struct mlxsw_sp_fib *fib;
177 int err;
178
179 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
180 if (!fib)
181 return ERR_PTR(-ENOMEM);
182 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
183 if (err)
184 goto err_rhashtable_init;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100185 INIT_LIST_HEAD(&fib->node_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100186 fib->proto = proto;
187 fib->vr = vr;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200188 return fib;
189
190err_rhashtable_init:
191 kfree(fib);
192 return ERR_PTR(err);
193}
194
195static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
196{
Ido Schimmel9aecce12017-02-09 10:28:42 +0100197 WARN_ON(!list_empty(&fib->node_list));
Ido Schimmel76610eb2017-03-10 08:53:41 +0100198 WARN_ON(fib->lpm_tree);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200199 rhashtable_destroy(&fib->ht);
200 kfree(fib);
201}
202
Jiri Pirko53342022016-07-04 08:23:08 +0200203static struct mlxsw_sp_lpm_tree *
Ido Schimmel382dbb42017-03-10 08:53:40 +0100204mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko53342022016-07-04 08:23:08 +0200205{
206 static struct mlxsw_sp_lpm_tree *lpm_tree;
207 int i;
208
209 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
210 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
Ido Schimmel382dbb42017-03-10 08:53:40 +0100211 if (lpm_tree->ref_count == 0)
212 return lpm_tree;
Jiri Pirko53342022016-07-04 08:23:08 +0200213 }
214 return NULL;
215}
216
217static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
218 struct mlxsw_sp_lpm_tree *lpm_tree)
219{
220 char ralta_pl[MLXSW_REG_RALTA_LEN];
221
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200222 mlxsw_reg_ralta_pack(ralta_pl, true,
223 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
224 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200225 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
226}
227
228static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
229 struct mlxsw_sp_lpm_tree *lpm_tree)
230{
231 char ralta_pl[MLXSW_REG_RALTA_LEN];
232
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200233 mlxsw_reg_ralta_pack(ralta_pl, false,
234 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
235 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200236 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
237}
238
239static int
240mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
241 struct mlxsw_sp_prefix_usage *prefix_usage,
242 struct mlxsw_sp_lpm_tree *lpm_tree)
243{
244 char ralst_pl[MLXSW_REG_RALST_LEN];
245 u8 root_bin = 0;
246 u8 prefix;
247 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
248
249 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
250 root_bin = prefix;
251
252 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
253 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
254 if (prefix == 0)
255 continue;
256 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
257 MLXSW_REG_RALST_BIN_NO_CHILD);
258 last_prefix = prefix;
259 }
260 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
261}
262
263static struct mlxsw_sp_lpm_tree *
264mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
265 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100266 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200267{
268 struct mlxsw_sp_lpm_tree *lpm_tree;
269 int err;
270
Ido Schimmel382dbb42017-03-10 08:53:40 +0100271 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
Jiri Pirko53342022016-07-04 08:23:08 +0200272 if (!lpm_tree)
273 return ERR_PTR(-EBUSY);
274 lpm_tree->proto = proto;
275 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
276 if (err)
277 return ERR_PTR(err);
278
279 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
280 lpm_tree);
281 if (err)
282 goto err_left_struct_set;
Jiri Pirko2083d362016-10-25 11:25:56 +0200283 memcpy(&lpm_tree->prefix_usage, prefix_usage,
284 sizeof(lpm_tree->prefix_usage));
Jiri Pirko53342022016-07-04 08:23:08 +0200285 return lpm_tree;
286
287err_left_struct_set:
288 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
289 return ERR_PTR(err);
290}
291
292static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
293 struct mlxsw_sp_lpm_tree *lpm_tree)
294{
295 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
296}
297
298static struct mlxsw_sp_lpm_tree *
299mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
300 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100301 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200302{
303 struct mlxsw_sp_lpm_tree *lpm_tree;
304 int i;
305
306 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
307 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
Jiri Pirko8b99bec2016-10-25 11:25:57 +0200308 if (lpm_tree->ref_count != 0 &&
309 lpm_tree->proto == proto &&
Jiri Pirko53342022016-07-04 08:23:08 +0200310 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
311 prefix_usage))
312 goto inc_ref_count;
313 }
314 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100315 proto);
Jiri Pirko53342022016-07-04 08:23:08 +0200316 if (IS_ERR(lpm_tree))
317 return lpm_tree;
318
319inc_ref_count:
320 lpm_tree->ref_count++;
321 return lpm_tree;
322}
323
324static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
325 struct mlxsw_sp_lpm_tree *lpm_tree)
326{
327 if (--lpm_tree->ref_count == 0)
328 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
329 return 0;
330}
331
332static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
333{
334 struct mlxsw_sp_lpm_tree *lpm_tree;
335 int i;
336
337 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
338 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
339 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
340 }
341}
342
Ido Schimmel76610eb2017-03-10 08:53:41 +0100343static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
344{
345 return !!vr->fib4;
346}
347
Jiri Pirko6b75c482016-07-04 08:23:09 +0200348static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
349{
350 struct mlxsw_sp_vr *vr;
351 int i;
352
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200353 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200354 vr = &mlxsw_sp->router.vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100355 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200356 return vr;
357 }
358 return NULL;
359}
360
361static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100362 const struct mlxsw_sp_fib *fib)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200363{
364 char raltb_pl[MLXSW_REG_RALTB_LEN];
365
Ido Schimmel76610eb2017-03-10 08:53:41 +0100366 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
367 (enum mlxsw_reg_ralxx_protocol) fib->proto,
368 fib->lpm_tree->id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200369 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
370}
371
372static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100373 const struct mlxsw_sp_fib *fib)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200374{
375 char raltb_pl[MLXSW_REG_RALTB_LEN];
376
377 /* Bind to tree 0 which is default */
Ido Schimmel76610eb2017-03-10 08:53:41 +0100378 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
379 (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200380 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
381}
382
383static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
384{
385 /* For our purpose, squash main and local table into one */
386 if (tb_id == RT_TABLE_LOCAL)
387 tb_id = RT_TABLE_MAIN;
388 return tb_id;
389}
390
391static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100392 u32 tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200393{
394 struct mlxsw_sp_vr *vr;
395 int i;
396
397 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Nogah Frankel9497c042016-09-20 11:16:54 +0200398
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200399 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200400 vr = &mlxsw_sp->router.vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100401 if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200402 return vr;
403 }
404 return NULL;
405}
406
Ido Schimmel76610eb2017-03-10 08:53:41 +0100407static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
408 enum mlxsw_sp_l3proto proto)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200409{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100410 switch (proto) {
411 case MLXSW_SP_L3_PROTO_IPV4:
412 return vr->fib4;
413 case MLXSW_SP_L3_PROTO_IPV6:
414 BUG_ON(1);
415 }
416 return NULL;
417}
418
419static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
420 u32 tb_id)
421{
Jiri Pirko6b75c482016-07-04 08:23:09 +0200422 struct mlxsw_sp_vr *vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200423
424 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
425 if (!vr)
426 return ERR_PTR(-EBUSY);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100427 vr->fib4 = mlxsw_sp_fib_create(vr, MLXSW_SP_L3_PROTO_IPV4);
428 if (IS_ERR(vr->fib4))
429 return ERR_CAST(vr->fib4);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200430 vr->tb_id = tb_id;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200431 return vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200432}
433
Ido Schimmel76610eb2017-03-10 08:53:41 +0100434static void mlxsw_sp_vr_destroy(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200435{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100436 mlxsw_sp_fib_destroy(vr->fib4);
437 vr->fib4 = NULL;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200438}
439
440static int
Ido Schimmel76610eb2017-03-10 08:53:41 +0100441mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fib *fib,
Jiri Pirko6b75c482016-07-04 08:23:09 +0200442 struct mlxsw_sp_prefix_usage *req_prefix_usage)
443{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100444 struct mlxsw_sp_lpm_tree *lpm_tree = fib->lpm_tree;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100445 struct mlxsw_sp_lpm_tree *new_tree;
446 int err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200447
Ido Schimmelf7df4922017-02-28 08:55:40 +0100448 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage, &lpm_tree->prefix_usage))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200449 return 0;
450
Ido Schimmelf7df4922017-02-28 08:55:40 +0100451 new_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100452 fib->proto);
Ido Schimmelf7df4922017-02-28 08:55:40 +0100453 if (IS_ERR(new_tree)) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200454 /* We failed to get a tree according to the required
455 * prefix usage. However, the current tree might be still good
456 * for us if our requirement is subset of the prefixes used
457 * in the tree.
458 */
459 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
Ido Schimmelf7df4922017-02-28 08:55:40 +0100460 &lpm_tree->prefix_usage))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200461 return 0;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100462 return PTR_ERR(new_tree);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200463 }
464
Ido Schimmelf7df4922017-02-28 08:55:40 +0100465 /* Prevent packet loss by overwriting existing binding */
Ido Schimmel76610eb2017-03-10 08:53:41 +0100466 fib->lpm_tree = new_tree;
467 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
Ido Schimmelf7df4922017-02-28 08:55:40 +0100468 if (err)
469 goto err_tree_bind;
470 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
471
472 return 0;
473
474err_tree_bind:
Ido Schimmel76610eb2017-03-10 08:53:41 +0100475 fib->lpm_tree = lpm_tree;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100476 mlxsw_sp_lpm_tree_put(mlxsw_sp, new_tree);
477 return err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200478}
479
Ido Schimmel76610eb2017-03-10 08:53:41 +0100480static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200481{
482 struct mlxsw_sp_vr *vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200483
484 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100485 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
486 if (!vr)
487 vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200488 return vr;
489}
490
Ido Schimmel76610eb2017-03-10 08:53:41 +0100491static void mlxsw_sp_vr_put(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200492{
Ido Schimmel69132292017-03-10 08:53:42 +0100493 if (!vr->rif_count && list_empty(&vr->fib4->node_list))
Ido Schimmel76610eb2017-03-10 08:53:41 +0100494 mlxsw_sp_vr_destroy(vr);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200495}
496
Nogah Frankel9497c042016-09-20 11:16:54 +0200497static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200498{
499 struct mlxsw_sp_vr *vr;
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200500 u64 max_vrs;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200501 int i;
502
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200503 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
Nogah Frankel9497c042016-09-20 11:16:54 +0200504 return -EIO;
505
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200506 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
507 mlxsw_sp->router.vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
Nogah Frankel9497c042016-09-20 11:16:54 +0200508 GFP_KERNEL);
509 if (!mlxsw_sp->router.vrs)
510 return -ENOMEM;
511
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200512 for (i = 0; i < max_vrs; i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200513 vr = &mlxsw_sp->router.vrs[i];
514 vr->id = i;
515 }
Nogah Frankel9497c042016-09-20 11:16:54 +0200516
517 return 0;
518}
519
Ido Schimmelac571de2016-11-14 11:26:32 +0100520static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
521
Nogah Frankel9497c042016-09-20 11:16:54 +0200522static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
523{
Ido Schimmel30572242016-12-03 16:45:01 +0100524 /* At this stage we're guaranteed not to have new incoming
525 * FIB notifications and the work queue is free from FIBs
526 * sitting on top of mlxsw netdevs. However, we can still
527 * have other FIBs queued. Flush the queue before flushing
528 * the device's tables. No need for locks, as we're the only
529 * writer.
530 */
531 mlxsw_core_flush_owq();
Ido Schimmelac571de2016-11-14 11:26:32 +0100532 mlxsw_sp_router_fib_flush(mlxsw_sp);
Nogah Frankel9497c042016-09-20 11:16:54 +0200533 kfree(mlxsw_sp->router.vrs);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200534}
535
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200536struct mlxsw_sp_neigh_key {
Jiri Pirko33b13412016-11-10 12:31:04 +0100537 struct neighbour *n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200538};
539
540struct mlxsw_sp_neigh_entry {
Ido Schimmel9665b742017-02-08 11:16:42 +0100541 struct list_head rif_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200542 struct rhash_head ht_node;
543 struct mlxsw_sp_neigh_key key;
544 u16 rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100545 bool connected;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200546 unsigned char ha[ETH_ALEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200547 struct list_head nexthop_list; /* list of nexthops using
548 * this neigh entry
549 */
Yotam Gigib2157142016-07-05 11:27:51 +0200550 struct list_head nexthop_neighs_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200551};
552
553static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
554 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
555 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
556 .key_len = sizeof(struct mlxsw_sp_neigh_key),
557};
558
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100559static struct mlxsw_sp_neigh_entry *
560mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
561 u16 rif)
562{
563 struct mlxsw_sp_neigh_entry *neigh_entry;
564
565 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
566 if (!neigh_entry)
567 return NULL;
568
569 neigh_entry->key.n = n;
570 neigh_entry->rif = rif;
571 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
572
573 return neigh_entry;
574}
575
576static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
577{
578 kfree(neigh_entry);
579}
580
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200581static int
582mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
583 struct mlxsw_sp_neigh_entry *neigh_entry)
584{
585 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
586 &neigh_entry->ht_node,
587 mlxsw_sp_neigh_ht_params);
588}
589
590static void
591mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
592 struct mlxsw_sp_neigh_entry *neigh_entry)
593{
594 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
595 &neigh_entry->ht_node,
596 mlxsw_sp_neigh_ht_params);
597}
598
599static struct mlxsw_sp_neigh_entry *
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100600mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200601{
602 struct mlxsw_sp_neigh_entry *neigh_entry;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100603 struct mlxsw_sp_rif *rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100604 int err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200605
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100606 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
607 if (!rif)
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100608 return ERR_PTR(-EINVAL);
609
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100610 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200611 if (!neigh_entry)
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100612 return ERR_PTR(-ENOMEM);
613
614 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
615 if (err)
616 goto err_neigh_entry_insert;
617
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100618 list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
Ido Schimmel9665b742017-02-08 11:16:42 +0100619
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200620 return neigh_entry;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100621
622err_neigh_entry_insert:
623 mlxsw_sp_neigh_entry_free(neigh_entry);
624 return ERR_PTR(err);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200625}
626
627static void
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100628mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
629 struct mlxsw_sp_neigh_entry *neigh_entry)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200630{
Ido Schimmel9665b742017-02-08 11:16:42 +0100631 list_del(&neigh_entry->rif_list_node);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100632 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
633 mlxsw_sp_neigh_entry_free(neigh_entry);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200634}
635
636static struct mlxsw_sp_neigh_entry *
Jiri Pirko33b13412016-11-10 12:31:04 +0100637mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200638{
Jiri Pirko33b13412016-11-10 12:31:04 +0100639 struct mlxsw_sp_neigh_key key;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200640
Jiri Pirko33b13412016-11-10 12:31:04 +0100641 key.n = n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200642 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
643 &key, mlxsw_sp_neigh_ht_params);
644}
645
Yotam Gigic723c7352016-07-05 11:27:43 +0200646static void
647mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
648{
649 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
650
651 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
652}
653
654static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
655 char *rauhtd_pl,
656 int ent_index)
657{
658 struct net_device *dev;
659 struct neighbour *n;
660 __be32 dipn;
661 u32 dip;
662 u16 rif;
663
664 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
665
666 if (!mlxsw_sp->rifs[rif]) {
667 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
668 return;
669 }
670
671 dipn = htonl(dip);
672 dev = mlxsw_sp->rifs[rif]->dev;
673 n = neigh_lookup(&arp_tbl, &dipn, dev);
674 if (!n) {
675 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
676 &dip);
677 return;
678 }
679
680 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
681 neigh_event_send(n, NULL);
682 neigh_release(n);
683}
684
685static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
686 char *rauhtd_pl,
687 int rec_index)
688{
689 u8 num_entries;
690 int i;
691
692 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
693 rec_index);
694 /* Hardware starts counting at 0, so add 1. */
695 num_entries++;
696
697 /* Each record consists of several neighbour entries. */
698 for (i = 0; i < num_entries; i++) {
699 int ent_index;
700
701 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
702 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
703 ent_index);
704 }
705
706}
707
708static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
709 char *rauhtd_pl, int rec_index)
710{
711 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
712 case MLXSW_REG_RAUHTD_TYPE_IPV4:
713 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
714 rec_index);
715 break;
716 case MLXSW_REG_RAUHTD_TYPE_IPV6:
717 WARN_ON_ONCE(1);
718 break;
719 }
720}
721
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100722static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
723{
724 u8 num_rec, last_rec_index, num_entries;
725
726 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
727 last_rec_index = num_rec - 1;
728
729 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
730 return false;
731 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
732 MLXSW_REG_RAUHTD_TYPE_IPV6)
733 return true;
734
735 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
736 last_rec_index);
737 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
738 return true;
739 return false;
740}
741
Yotam Gigib2157142016-07-05 11:27:51 +0200742static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
Yotam Gigic723c7352016-07-05 11:27:43 +0200743{
Yotam Gigic723c7352016-07-05 11:27:43 +0200744 char *rauhtd_pl;
745 u8 num_rec;
746 int i, err;
747
748 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
749 if (!rauhtd_pl)
Yotam Gigib2157142016-07-05 11:27:51 +0200750 return -ENOMEM;
Yotam Gigic723c7352016-07-05 11:27:43 +0200751
752 /* Make sure the neighbour's netdev isn't removed in the
753 * process.
754 */
755 rtnl_lock();
756 do {
757 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
758 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
759 rauhtd_pl);
760 if (err) {
761 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
762 break;
763 }
764 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
765 for (i = 0; i < num_rec; i++)
766 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
767 i);
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100768 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
Yotam Gigic723c7352016-07-05 11:27:43 +0200769 rtnl_unlock();
770
771 kfree(rauhtd_pl);
Yotam Gigib2157142016-07-05 11:27:51 +0200772 return err;
773}
774
775static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
776{
777 struct mlxsw_sp_neigh_entry *neigh_entry;
778
779 /* Take RTNL mutex here to prevent lists from changes */
780 rtnl_lock();
781 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100782 nexthop_neighs_list_node)
Yotam Gigib2157142016-07-05 11:27:51 +0200783 /* If this neigh have nexthops, make the kernel think this neigh
784 * is active regardless of the traffic.
785 */
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100786 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigib2157142016-07-05 11:27:51 +0200787 rtnl_unlock();
788}
789
790static void
791mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
792{
793 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
794
795 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
796 msecs_to_jiffies(interval));
797}
798
799static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
800{
801 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
802 router.neighs_update.dw.work);
803 int err;
804
805 err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp);
806 if (err)
807 dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
808
809 mlxsw_sp_router_neighs_update_nh(mlxsw_sp);
810
Yotam Gigic723c7352016-07-05 11:27:43 +0200811 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
812}
813
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200814static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
815{
816 struct mlxsw_sp_neigh_entry *neigh_entry;
817 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
818 router.nexthop_probe_dw.work);
819
820 /* Iterate over nexthop neighbours, find those who are unresolved and
821 * send arp on them. This solves the chicken-egg problem when
822 * the nexthop wouldn't get offloaded until the neighbor is resolved
823 * but it wouldn't get resolved ever in case traffic is flowing in HW
824 * using different nexthop.
825 *
826 * Take RTNL mutex here to prevent lists from changes.
827 */
828 rtnl_lock();
829 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100830 nexthop_neighs_list_node)
Ido Schimmel01b1aa32017-02-06 16:20:16 +0100831 if (!neigh_entry->connected)
Jiri Pirko33b13412016-11-10 12:31:04 +0100832 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200833 rtnl_unlock();
834
835 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw,
836 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
837}
838
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200839static void
840mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
841 struct mlxsw_sp_neigh_entry *neigh_entry,
842 bool removing);
843
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100844static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200845{
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100846 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
847 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
848}
849
850static void
851mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
852 struct mlxsw_sp_neigh_entry *neigh_entry,
853 enum mlxsw_reg_rauht_op op)
854{
Jiri Pirko33b13412016-11-10 12:31:04 +0100855 struct neighbour *n = neigh_entry->key.n;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100856 u32 dip = ntohl(*((__be32 *) n->primary_key));
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200857 char rauht_pl[MLXSW_REG_RAUHT_LEN];
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100858
859 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
860 dip);
861 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
862}
863
864static void
865mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
866 struct mlxsw_sp_neigh_entry *neigh_entry,
867 bool adding)
868{
869 if (!adding && !neigh_entry->connected)
870 return;
871 neigh_entry->connected = adding;
872 if (neigh_entry->key.n->tbl == &arp_tbl)
873 mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
874 mlxsw_sp_rauht_op(adding));
875 else
876 WARN_ON_ONCE(1);
877}
878
879struct mlxsw_sp_neigh_event_work {
880 struct work_struct work;
881 struct mlxsw_sp *mlxsw_sp;
882 struct neighbour *n;
883};
884
885static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
886{
887 struct mlxsw_sp_neigh_event_work *neigh_work =
888 container_of(work, struct mlxsw_sp_neigh_event_work, work);
889 struct mlxsw_sp *mlxsw_sp = neigh_work->mlxsw_sp;
890 struct mlxsw_sp_neigh_entry *neigh_entry;
891 struct neighbour *n = neigh_work->n;
892 unsigned char ha[ETH_ALEN];
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200893 bool entry_connected;
Ido Schimmel93a87e52016-12-23 09:32:49 +0100894 u8 nud_state, dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200895
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100896 /* If these parameters are changed after we release the lock,
897 * then we are guaranteed to receive another event letting us
898 * know about it.
899 */
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200900 read_lock_bh(&n->lock);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100901 memcpy(ha, n->ha, ETH_ALEN);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200902 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +0100903 dead = n->dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200904 read_unlock_bh(&n->lock);
905
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100906 rtnl_lock();
Ido Schimmel93a87e52016-12-23 09:32:49 +0100907 entry_connected = nud_state & NUD_VALID && !dead;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100908 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
909 if (!entry_connected && !neigh_entry)
910 goto out;
911 if (!neigh_entry) {
912 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
913 if (IS_ERR(neigh_entry))
914 goto out;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200915 }
916
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100917 memcpy(neigh_entry->ha, ha, ETH_ALEN);
918 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
919 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected);
920
921 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
922 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
923
924out:
925 rtnl_unlock();
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200926 neigh_release(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100927 kfree(neigh_work);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200928}
929
Jiri Pirkoe7322632016-09-01 10:37:43 +0200930int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
931 unsigned long event, void *ptr)
Yotam Gigic723c7352016-07-05 11:27:43 +0200932{
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100933 struct mlxsw_sp_neigh_event_work *neigh_work;
Yotam Gigic723c7352016-07-05 11:27:43 +0200934 struct mlxsw_sp_port *mlxsw_sp_port;
935 struct mlxsw_sp *mlxsw_sp;
936 unsigned long interval;
937 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200938 struct neighbour *n;
Yotam Gigic723c7352016-07-05 11:27:43 +0200939
940 switch (event) {
941 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
942 p = ptr;
943
944 /* We don't care about changes in the default table. */
945 if (!p->dev || p->tbl != &arp_tbl)
946 return NOTIFY_DONE;
947
948 /* We are in atomic context and can't take RTNL mutex,
949 * so use RCU variant to walk the device chain.
950 */
951 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
952 if (!mlxsw_sp_port)
953 return NOTIFY_DONE;
954
955 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
956 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
957 mlxsw_sp->router.neighs_update.interval = interval;
958
959 mlxsw_sp_port_dev_put(mlxsw_sp_port);
960 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200961 case NETEVENT_NEIGH_UPDATE:
962 n = ptr;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200963
964 if (n->tbl != &arp_tbl)
965 return NOTIFY_DONE;
966
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100967 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200968 if (!mlxsw_sp_port)
969 return NOTIFY_DONE;
970
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100971 neigh_work = kzalloc(sizeof(*neigh_work), GFP_ATOMIC);
972 if (!neigh_work) {
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200973 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100974 return NOTIFY_BAD;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200975 }
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100976
977 INIT_WORK(&neigh_work->work, mlxsw_sp_router_neigh_event_work);
978 neigh_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
979 neigh_work->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200980
981 /* Take a reference to ensure the neighbour won't be
982 * destructed until we drop the reference in delayed
983 * work.
984 */
985 neigh_clone(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100986 mlxsw_core_schedule_work(&neigh_work->work);
987 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200988 break;
Yotam Gigic723c7352016-07-05 11:27:43 +0200989 }
990
991 return NOTIFY_DONE;
992}
993
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200994static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
995{
Yotam Gigic723c7352016-07-05 11:27:43 +0200996 int err;
997
998 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
999 &mlxsw_sp_neigh_ht_params);
1000 if (err)
1001 return err;
1002
1003 /* Initialize the polling interval according to the default
1004 * table.
1005 */
1006 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1007
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001008 /* Create the delayed works for the activity_update */
Yotam Gigic723c7352016-07-05 11:27:43 +02001009 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
1010 mlxsw_sp_router_neighs_update_work);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001011 INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw,
1012 mlxsw_sp_router_probe_unresolved_nexthops);
Yotam Gigic723c7352016-07-05 11:27:43 +02001013 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001014 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0);
Yotam Gigic723c7352016-07-05 11:27:43 +02001015 return 0;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001016}
1017
1018static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1019{
Yotam Gigic723c7352016-07-05 11:27:43 +02001020 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001021 cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001022 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1023}
1024
Ido Schimmel9665b742017-02-08 11:16:42 +01001025static int mlxsw_sp_neigh_rif_flush(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001026 const struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001027{
1028 char rauht_pl[MLXSW_REG_RAUHT_LEN];
1029
1030 mlxsw_reg_rauht_pack(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE_ALL,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001031 rif->rif_index, rif->addr);
Ido Schimmel9665b742017-02-08 11:16:42 +01001032 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1033}
1034
1035static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001036 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001037{
1038 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
1039
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001040 mlxsw_sp_neigh_rif_flush(mlxsw_sp, rif);
1041 list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
Ido Schimmel9665b742017-02-08 11:16:42 +01001042 rif_list_node)
1043 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1044}
1045
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001046struct mlxsw_sp_nexthop_key {
1047 struct fib_nh *fib_nh;
1048};
1049
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001050struct mlxsw_sp_nexthop {
1051 struct list_head neigh_list_node; /* member of neigh entry list */
Ido Schimmel9665b742017-02-08 11:16:42 +01001052 struct list_head rif_list_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001053 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1054 * this belongs to
1055 */
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001056 struct rhash_head ht_node;
1057 struct mlxsw_sp_nexthop_key key;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001058 struct mlxsw_sp_rif *rif;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001059 u8 should_offload:1, /* set indicates this neigh is connected and
1060 * should be put to KVD linear area of this group.
1061 */
1062 offloaded:1, /* set in case the neigh is actually put into
1063 * KVD linear area of this group.
1064 */
1065 update:1; /* set indicates that MAC of this neigh should be
1066 * updated in HW
1067 */
1068 struct mlxsw_sp_neigh_entry *neigh_entry;
1069};
1070
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001071struct mlxsw_sp_nexthop_group_key {
1072 struct fib_info *fi;
1073};
1074
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001075struct mlxsw_sp_nexthop_group {
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001076 struct rhash_head ht_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001077 struct list_head fib_list; /* list of fib entries that use this group */
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001078 struct mlxsw_sp_nexthop_group_key key;
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001079 u8 adj_index_valid:1,
1080 gateway:1; /* routes using the group use a gateway */
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001081 u32 adj_index;
1082 u16 ecmp_size;
1083 u16 count;
1084 struct mlxsw_sp_nexthop nexthops[0];
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001085#define nh_rif nexthops[0].rif
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001086};
1087
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001088static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
1089 .key_offset = offsetof(struct mlxsw_sp_nexthop_group, key),
1090 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
1091 .key_len = sizeof(struct mlxsw_sp_nexthop_group_key),
1092};
1093
1094static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
1095 struct mlxsw_sp_nexthop_group *nh_grp)
1096{
1097 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_group_ht,
1098 &nh_grp->ht_node,
1099 mlxsw_sp_nexthop_group_ht_params);
1100}
1101
1102static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
1103 struct mlxsw_sp_nexthop_group *nh_grp)
1104{
1105 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_group_ht,
1106 &nh_grp->ht_node,
1107 mlxsw_sp_nexthop_group_ht_params);
1108}
1109
1110static struct mlxsw_sp_nexthop_group *
1111mlxsw_sp_nexthop_group_lookup(struct mlxsw_sp *mlxsw_sp,
1112 struct mlxsw_sp_nexthop_group_key key)
1113{
1114 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_group_ht, &key,
1115 mlxsw_sp_nexthop_group_ht_params);
1116}
1117
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001118static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
1119 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
1120 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
1121 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
1122};
1123
1124static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
1125 struct mlxsw_sp_nexthop *nh)
1126{
1127 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_ht,
1128 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
1129}
1130
1131static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
1132 struct mlxsw_sp_nexthop *nh)
1133{
1134 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_ht, &nh->ht_node,
1135 mlxsw_sp_nexthop_ht_params);
1136}
1137
Ido Schimmelad178c82017-02-08 11:16:40 +01001138static struct mlxsw_sp_nexthop *
1139mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
1140 struct mlxsw_sp_nexthop_key key)
1141{
1142 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_ht, &key,
1143 mlxsw_sp_nexthop_ht_params);
1144}
1145
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001146static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001147 const struct mlxsw_sp_fib *fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001148 u32 adj_index, u16 ecmp_size,
1149 u32 new_adj_index,
1150 u16 new_ecmp_size)
1151{
1152 char raleu_pl[MLXSW_REG_RALEU_LEN];
1153
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001154 mlxsw_reg_raleu_pack(raleu_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001155 (enum mlxsw_reg_ralxx_protocol) fib->proto,
1156 fib->vr->id, adj_index, ecmp_size, new_adj_index,
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001157 new_ecmp_size);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001158 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1159}
1160
1161static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1162 struct mlxsw_sp_nexthop_group *nh_grp,
1163 u32 old_adj_index, u16 old_ecmp_size)
1164{
1165 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001166 struct mlxsw_sp_fib *fib = NULL;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001167 int err;
1168
1169 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
Ido Schimmel76610eb2017-03-10 08:53:41 +01001170 if (fib == fib_entry->fib_node->fib)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001171 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001172 fib = fib_entry->fib_node->fib;
1173 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001174 old_adj_index,
1175 old_ecmp_size,
1176 nh_grp->adj_index,
1177 nh_grp->ecmp_size);
1178 if (err)
1179 return err;
1180 }
1181 return 0;
1182}
1183
1184static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1185 struct mlxsw_sp_nexthop *nh)
1186{
1187 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1188 char ratr_pl[MLXSW_REG_RATR_LEN];
1189
1190 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1191 true, adj_index, neigh_entry->rif);
1192 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1193 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1194}
1195
1196static int
1197mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
Ido Schimmela59b7e02017-01-23 11:11:42 +01001198 struct mlxsw_sp_nexthop_group *nh_grp,
1199 bool reallocate)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001200{
1201 u32 adj_index = nh_grp->adj_index; /* base */
1202 struct mlxsw_sp_nexthop *nh;
1203 int i;
1204 int err;
1205
1206 for (i = 0; i < nh_grp->count; i++) {
1207 nh = &nh_grp->nexthops[i];
1208
1209 if (!nh->should_offload) {
1210 nh->offloaded = 0;
1211 continue;
1212 }
1213
Ido Schimmela59b7e02017-01-23 11:11:42 +01001214 if (nh->update || reallocate) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001215 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1216 adj_index, nh);
1217 if (err)
1218 return err;
1219 nh->update = 0;
1220 nh->offloaded = 1;
1221 }
1222 adj_index++;
1223 }
1224 return 0;
1225}
1226
1227static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1228 struct mlxsw_sp_fib_entry *fib_entry);
1229
1230static int
1231mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1232 struct mlxsw_sp_nexthop_group *nh_grp)
1233{
1234 struct mlxsw_sp_fib_entry *fib_entry;
1235 int err;
1236
1237 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1238 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1239 if (err)
1240 return err;
1241 }
1242 return 0;
1243}
1244
1245static void
1246mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1247 struct mlxsw_sp_nexthop_group *nh_grp)
1248{
1249 struct mlxsw_sp_nexthop *nh;
1250 bool offload_change = false;
1251 u32 adj_index;
1252 u16 ecmp_size = 0;
1253 bool old_adj_index_valid;
1254 u32 old_adj_index;
1255 u16 old_ecmp_size;
1256 int ret;
1257 int i;
1258 int err;
1259
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001260 if (!nh_grp->gateway) {
1261 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1262 return;
1263 }
1264
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001265 for (i = 0; i < nh_grp->count; i++) {
1266 nh = &nh_grp->nexthops[i];
1267
1268 if (nh->should_offload ^ nh->offloaded) {
1269 offload_change = true;
1270 if (nh->should_offload)
1271 nh->update = 1;
1272 }
1273 if (nh->should_offload)
1274 ecmp_size++;
1275 }
1276 if (!offload_change) {
1277 /* Nothing was added or removed, so no need to reallocate. Just
1278 * update MAC on existing adjacency indexes.
1279 */
Ido Schimmela59b7e02017-01-23 11:11:42 +01001280 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp,
1281 false);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001282 if (err) {
1283 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1284 goto set_trap;
1285 }
1286 return;
1287 }
1288 if (!ecmp_size)
1289 /* No neigh of this group is connected so we just set
1290 * the trap and let everthing flow through kernel.
1291 */
1292 goto set_trap;
1293
1294 ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size);
1295 if (ret < 0) {
1296 /* We ran out of KVD linear space, just set the
1297 * trap and let everything flow through kernel.
1298 */
1299 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1300 goto set_trap;
1301 }
1302 adj_index = ret;
1303 old_adj_index_valid = nh_grp->adj_index_valid;
1304 old_adj_index = nh_grp->adj_index;
1305 old_ecmp_size = nh_grp->ecmp_size;
1306 nh_grp->adj_index_valid = 1;
1307 nh_grp->adj_index = adj_index;
1308 nh_grp->ecmp_size = ecmp_size;
Ido Schimmela59b7e02017-01-23 11:11:42 +01001309 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001310 if (err) {
1311 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1312 goto set_trap;
1313 }
1314
1315 if (!old_adj_index_valid) {
1316 /* The trap was set for fib entries, so we have to call
1317 * fib entry update to unset it and use adjacency index.
1318 */
1319 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1320 if (err) {
1321 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1322 goto set_trap;
1323 }
1324 return;
1325 }
1326
1327 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1328 old_adj_index, old_ecmp_size);
1329 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1330 if (err) {
1331 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1332 goto set_trap;
1333 }
1334 return;
1335
1336set_trap:
1337 old_adj_index_valid = nh_grp->adj_index_valid;
1338 nh_grp->adj_index_valid = 0;
1339 for (i = 0; i < nh_grp->count; i++) {
1340 nh = &nh_grp->nexthops[i];
1341 nh->offloaded = 0;
1342 }
1343 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1344 if (err)
1345 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1346 if (old_adj_index_valid)
1347 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1348}
1349
1350static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1351 bool removing)
1352{
1353 if (!removing && !nh->should_offload)
1354 nh->should_offload = 1;
1355 else if (removing && nh->offloaded)
1356 nh->should_offload = 0;
1357 nh->update = 1;
1358}
1359
1360static void
1361mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1362 struct mlxsw_sp_neigh_entry *neigh_entry,
1363 bool removing)
1364{
1365 struct mlxsw_sp_nexthop *nh;
1366
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001367 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1368 neigh_list_node) {
1369 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1370 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1371 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001372}
1373
Ido Schimmel9665b742017-02-08 11:16:42 +01001374static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001375 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001376{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001377 if (nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001378 return;
1379
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001380 nh->rif = rif;
1381 list_add(&nh->rif_list_node, &rif->nexthop_list);
Ido Schimmel9665b742017-02-08 11:16:42 +01001382}
1383
1384static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
1385{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001386 if (!nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001387 return;
1388
1389 list_del(&nh->rif_list_node);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001390 nh->rif = NULL;
Ido Schimmel9665b742017-02-08 11:16:42 +01001391}
1392
Ido Schimmela8c97012017-02-08 11:16:35 +01001393static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
1394 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001395{
1396 struct mlxsw_sp_neigh_entry *neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01001397 struct fib_nh *fib_nh = nh->key.fib_nh;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001398 struct neighbour *n;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001399 u8 nud_state, dead;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001400 int err;
1401
Ido Schimmelad178c82017-02-08 11:16:40 +01001402 if (!nh->nh_grp->gateway || nh->neigh_entry)
Ido Schimmelb8399a12017-02-08 11:16:33 +01001403 return 0;
1404
Jiri Pirko33b13412016-11-10 12:31:04 +01001405 /* Take a reference of neigh here ensuring that neigh would
1406 * not be detructed before the nexthop entry is finished.
1407 * The reference is taken either in neigh_lookup() or
Ido Schimmelfd76d912017-02-06 16:20:17 +01001408 * in neigh_create() in case n is not found.
Jiri Pirko33b13412016-11-10 12:31:04 +01001409 */
Ido Schimmela8c97012017-02-08 11:16:35 +01001410 n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
Jiri Pirko33b13412016-11-10 12:31:04 +01001411 if (!n) {
Ido Schimmela8c97012017-02-08 11:16:35 +01001412 n = neigh_create(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
1413 if (IS_ERR(n))
1414 return PTR_ERR(n);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001415 neigh_event_send(n, NULL);
Jiri Pirko33b13412016-11-10 12:31:04 +01001416 }
1417 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1418 if (!neigh_entry) {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001419 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1420 if (IS_ERR(neigh_entry)) {
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001421 err = -EINVAL;
1422 goto err_neigh_entry_create;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001423 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001424 }
Yotam Gigib2157142016-07-05 11:27:51 +02001425
1426 /* If that is the first nexthop connected to that neigh, add to
1427 * nexthop_neighs_list
1428 */
1429 if (list_empty(&neigh_entry->nexthop_list))
1430 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
1431 &mlxsw_sp->router.nexthop_neighs_list);
1432
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001433 nh->neigh_entry = neigh_entry;
1434 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1435 read_lock_bh(&n->lock);
1436 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001437 dead = n->dead;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001438 read_unlock_bh(&n->lock);
Ido Schimmel93a87e52016-12-23 09:32:49 +01001439 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001440
1441 return 0;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001442
1443err_neigh_entry_create:
1444 neigh_release(n);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001445 return err;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001446}
1447
Ido Schimmela8c97012017-02-08 11:16:35 +01001448static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
1449 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001450{
1451 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01001452 struct neighbour *n;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001453
Ido Schimmelb8399a12017-02-08 11:16:33 +01001454 if (!neigh_entry)
Ido Schimmela8c97012017-02-08 11:16:35 +01001455 return;
1456 n = neigh_entry->key.n;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001457
Ido Schimmel58312122016-12-23 09:32:50 +01001458 __mlxsw_sp_nexthop_neigh_update(nh, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001459 list_del(&nh->neigh_list_node);
Ido Schimmele58be792017-02-08 11:16:28 +01001460 nh->neigh_entry = NULL;
Yotam Gigib2157142016-07-05 11:27:51 +02001461
1462 /* If that is the last nexthop connected to that neigh, remove from
1463 * nexthop_neighs_list
1464 */
Ido Schimmele58be792017-02-08 11:16:28 +01001465 if (list_empty(&neigh_entry->nexthop_list))
1466 list_del(&neigh_entry->nexthop_neighs_list_node);
Yotam Gigib2157142016-07-05 11:27:51 +02001467
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001468 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1469 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1470
1471 neigh_release(n);
Ido Schimmela8c97012017-02-08 11:16:35 +01001472}
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001473
Ido Schimmela8c97012017-02-08 11:16:35 +01001474static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp,
1475 struct mlxsw_sp_nexthop_group *nh_grp,
1476 struct mlxsw_sp_nexthop *nh,
1477 struct fib_nh *fib_nh)
1478{
1479 struct net_device *dev = fib_nh->nh_dev;
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001480 struct in_device *in_dev;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001481 struct mlxsw_sp_rif *rif;
Ido Schimmela8c97012017-02-08 11:16:35 +01001482 int err;
1483
1484 nh->nh_grp = nh_grp;
1485 nh->key.fib_nh = fib_nh;
1486 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
1487 if (err)
1488 return err;
1489
Ido Schimmel97989ee2017-03-10 08:53:38 +01001490 if (!dev)
1491 return 0;
1492
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001493 in_dev = __in_dev_get_rtnl(dev);
1494 if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1495 fib_nh->nh_flags & RTNH_F_LINKDOWN)
1496 return 0;
1497
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001498 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
1499 if (!rif)
Ido Schimmela8c97012017-02-08 11:16:35 +01001500 return 0;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001501 mlxsw_sp_nexthop_rif_init(nh, rif);
Ido Schimmela8c97012017-02-08 11:16:35 +01001502
1503 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1504 if (err)
1505 goto err_nexthop_neigh_init;
1506
1507 return 0;
1508
1509err_nexthop_neigh_init:
1510 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1511 return err;
1512}
1513
1514static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1515 struct mlxsw_sp_nexthop *nh)
1516{
1517 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001518 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001519 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001520}
1521
Ido Schimmelad178c82017-02-08 11:16:40 +01001522static void mlxsw_sp_nexthop_event(struct mlxsw_sp *mlxsw_sp,
1523 unsigned long event, struct fib_nh *fib_nh)
1524{
1525 struct mlxsw_sp_nexthop_key key;
1526 struct mlxsw_sp_nexthop *nh;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001527 struct mlxsw_sp_rif *rif;
Ido Schimmelad178c82017-02-08 11:16:40 +01001528
1529 if (mlxsw_sp->router.aborted)
1530 return;
1531
1532 key.fib_nh = fib_nh;
1533 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
1534 if (WARN_ON_ONCE(!nh))
1535 return;
1536
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001537 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fib_nh->nh_dev);
1538 if (!rif)
Ido Schimmelad178c82017-02-08 11:16:40 +01001539 return;
1540
1541 switch (event) {
1542 case FIB_EVENT_NH_ADD:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001543 mlxsw_sp_nexthop_rif_init(nh, rif);
Ido Schimmelad178c82017-02-08 11:16:40 +01001544 mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1545 break;
1546 case FIB_EVENT_NH_DEL:
1547 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001548 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01001549 break;
1550 }
1551
1552 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1553}
1554
Ido Schimmel9665b742017-02-08 11:16:42 +01001555static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001556 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001557{
1558 struct mlxsw_sp_nexthop *nh, *tmp;
1559
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001560 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
Ido Schimmel9665b742017-02-08 11:16:42 +01001561 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
1562 mlxsw_sp_nexthop_rif_fini(nh);
1563 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1564 }
1565}
1566
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001567static struct mlxsw_sp_nexthop_group *
1568mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1569{
1570 struct mlxsw_sp_nexthop_group *nh_grp;
1571 struct mlxsw_sp_nexthop *nh;
1572 struct fib_nh *fib_nh;
1573 size_t alloc_size;
1574 int i;
1575 int err;
1576
1577 alloc_size = sizeof(*nh_grp) +
1578 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1579 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1580 if (!nh_grp)
1581 return ERR_PTR(-ENOMEM);
1582 INIT_LIST_HEAD(&nh_grp->fib_list);
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001583 nh_grp->gateway = fi->fib_nh->nh_scope == RT_SCOPE_LINK;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001584 nh_grp->count = fi->fib_nhs;
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001585 nh_grp->key.fi = fi;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001586 for (i = 0; i < nh_grp->count; i++) {
1587 nh = &nh_grp->nexthops[i];
1588 fib_nh = &fi->fib_nh[i];
1589 err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh);
1590 if (err)
1591 goto err_nexthop_init;
1592 }
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001593 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
1594 if (err)
1595 goto err_nexthop_group_insert;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001596 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1597 return nh_grp;
1598
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001599err_nexthop_group_insert:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001600err_nexthop_init:
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001601 for (i--; i >= 0; i--) {
1602 nh = &nh_grp->nexthops[i];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001603 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001604 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001605 kfree(nh_grp);
1606 return ERR_PTR(err);
1607}
1608
1609static void
1610mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1611 struct mlxsw_sp_nexthop_group *nh_grp)
1612{
1613 struct mlxsw_sp_nexthop *nh;
1614 int i;
1615
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001616 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001617 for (i = 0; i < nh_grp->count; i++) {
1618 nh = &nh_grp->nexthops[i];
1619 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1620 }
Ido Schimmel58312122016-12-23 09:32:50 +01001621 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1622 WARN_ON_ONCE(nh_grp->adj_index_valid);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001623 kfree(nh_grp);
1624}
1625
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001626static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1627 struct mlxsw_sp_fib_entry *fib_entry,
1628 struct fib_info *fi)
1629{
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001630 struct mlxsw_sp_nexthop_group_key key;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001631 struct mlxsw_sp_nexthop_group *nh_grp;
1632
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001633 key.fi = fi;
1634 nh_grp = mlxsw_sp_nexthop_group_lookup(mlxsw_sp, key);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001635 if (!nh_grp) {
1636 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1637 if (IS_ERR(nh_grp))
1638 return PTR_ERR(nh_grp);
1639 }
1640 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1641 fib_entry->nh_group = nh_grp;
1642 return 0;
1643}
1644
1645static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1646 struct mlxsw_sp_fib_entry *fib_entry)
1647{
1648 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1649
1650 list_del(&fib_entry->nexthop_group_node);
1651 if (!list_empty(&nh_grp->fib_list))
1652 return;
1653 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1654}
1655
Ido Schimmel013b20f2017-02-08 11:16:36 +01001656static bool
1657mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
1658{
1659 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
1660
Ido Schimmel9aecce12017-02-09 10:28:42 +01001661 if (fib_entry->params.tos)
1662 return false;
1663
Ido Schimmel013b20f2017-02-08 11:16:36 +01001664 switch (fib_entry->type) {
1665 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
1666 return !!nh_group->adj_index_valid;
1667 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
Ido Schimmel70ad3502017-02-08 11:16:38 +01001668 return !!nh_group->nh_rif;
Ido Schimmel013b20f2017-02-08 11:16:36 +01001669 default:
1670 return false;
1671 }
1672}
1673
1674static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
1675{
1676 fib_entry->offloaded = true;
1677
Ido Schimmel76610eb2017-03-10 08:53:41 +01001678 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01001679 case MLXSW_SP_L3_PROTO_IPV4:
1680 fib_info_offload_inc(fib_entry->nh_group->key.fi);
1681 break;
1682 case MLXSW_SP_L3_PROTO_IPV6:
1683 WARN_ON_ONCE(1);
1684 }
1685}
1686
1687static void
1688mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
1689{
Ido Schimmel76610eb2017-03-10 08:53:41 +01001690 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01001691 case MLXSW_SP_L3_PROTO_IPV4:
1692 fib_info_offload_dec(fib_entry->nh_group->key.fi);
1693 break;
1694 case MLXSW_SP_L3_PROTO_IPV6:
1695 WARN_ON_ONCE(1);
1696 }
1697
1698 fib_entry->offloaded = false;
1699}
1700
1701static void
1702mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
1703 enum mlxsw_reg_ralue_op op, int err)
1704{
1705 switch (op) {
1706 case MLXSW_REG_RALUE_OP_WRITE_DELETE:
1707 if (!fib_entry->offloaded)
1708 return;
1709 return mlxsw_sp_fib_entry_offload_unset(fib_entry);
1710 case MLXSW_REG_RALUE_OP_WRITE_WRITE:
1711 if (err)
1712 return;
1713 if (mlxsw_sp_fib_entry_should_offload(fib_entry) &&
1714 !fib_entry->offloaded)
1715 mlxsw_sp_fib_entry_offload_set(fib_entry);
1716 else if (!mlxsw_sp_fib_entry_should_offload(fib_entry) &&
1717 fib_entry->offloaded)
1718 mlxsw_sp_fib_entry_offload_unset(fib_entry);
1719 return;
1720 default:
1721 return;
1722 }
1723}
1724
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001725static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp,
1726 struct mlxsw_sp_fib_entry *fib_entry,
1727 enum mlxsw_reg_ralue_op op)
1728{
1729 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel76610eb2017-03-10 08:53:41 +01001730 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01001731 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001732 enum mlxsw_reg_ralue_trap_action trap_action;
1733 u16 trap_id = 0;
1734 u32 adjacency_index = 0;
1735 u16 ecmp_size = 0;
1736
1737 /* In case the nexthop group adjacency index is valid, use it
1738 * with provided ECMP size. Otherwise, setup trap and pass
1739 * traffic to kernel.
1740 */
Ido Schimmel4b411472017-02-08 11:16:37 +01001741 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001742 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1743 adjacency_index = fib_entry->nh_group->adj_index;
1744 ecmp_size = fib_entry->nh_group->ecmp_size;
1745 } else {
1746 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1747 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1748 }
1749
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001750 mlxsw_reg_ralue_pack4(ralue_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001751 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1752 fib->vr->id, fib_entry->fib_node->key.prefix_len,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001753 *p_dip);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001754 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
1755 adjacency_index, ecmp_size);
1756 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1757}
1758
Jiri Pirko61c503f2016-07-04 08:23:11 +02001759static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1760 struct mlxsw_sp_fib_entry *fib_entry,
1761 enum mlxsw_reg_ralue_op op)
1762{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001763 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nh_rif;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001764 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001765 enum mlxsw_reg_ralue_trap_action trap_action;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001766 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel9aecce12017-02-09 10:28:42 +01001767 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001768 u16 trap_id = 0;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001769 u16 rif_index = 0;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001770
1771 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
1772 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001773 rif_index = rif->rif_index;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001774 } else {
1775 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1776 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1777 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02001778
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001779 mlxsw_reg_ralue_pack4(ralue_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001780 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1781 fib->vr->id, fib_entry->fib_node->key.prefix_len,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001782 *p_dip);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001783 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id,
1784 rif_index);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001785 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1786}
1787
1788static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1789 struct mlxsw_sp_fib_entry *fib_entry,
1790 enum mlxsw_reg_ralue_op op)
1791{
Ido Schimmel76610eb2017-03-10 08:53:41 +01001792 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001793 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel9aecce12017-02-09 10:28:42 +01001794 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001795
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001796 mlxsw_reg_ralue_pack4(ralue_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001797 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1798 fib->vr->id, fib_entry->fib_node->key.prefix_len,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001799 *p_dip);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001800 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1801 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1802}
1803
1804static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1805 struct mlxsw_sp_fib_entry *fib_entry,
1806 enum mlxsw_reg_ralue_op op)
1807{
1808 switch (fib_entry->type) {
1809 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001810 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001811 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1812 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1813 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1814 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1815 }
1816 return -EINVAL;
1817}
1818
1819static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1820 struct mlxsw_sp_fib_entry *fib_entry,
1821 enum mlxsw_reg_ralue_op op)
1822{
Ido Schimmel013b20f2017-02-08 11:16:36 +01001823 int err = -EINVAL;
1824
Ido Schimmel76610eb2017-03-10 08:53:41 +01001825 switch (fib_entry->fib_node->fib->proto) {
Jiri Pirko61c503f2016-07-04 08:23:11 +02001826 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel013b20f2017-02-08 11:16:36 +01001827 err = mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1828 break;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001829 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmel013b20f2017-02-08 11:16:36 +01001830 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001831 }
Ido Schimmel013b20f2017-02-08 11:16:36 +01001832 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
1833 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001834}
1835
1836static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1837 struct mlxsw_sp_fib_entry *fib_entry)
1838{
Jiri Pirko7146da32016-09-01 10:37:41 +02001839 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1840 MLXSW_REG_RALUE_OP_WRITE_WRITE);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001841}
1842
1843static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1844 struct mlxsw_sp_fib_entry *fib_entry)
1845{
1846 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1847 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1848}
1849
Jiri Pirko61c503f2016-07-04 08:23:11 +02001850static int
Ido Schimmel013b20f2017-02-08 11:16:36 +01001851mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
1852 const struct fib_entry_notifier_info *fen_info,
1853 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001854{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001855 struct fib_info *fi = fen_info->fi;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001856
Ido Schimmel97989ee2017-03-10 08:53:38 +01001857 switch (fen_info->type) {
1858 case RTN_BROADCAST: /* fall through */
1859 case RTN_LOCAL:
Jiri Pirko61c503f2016-07-04 08:23:11 +02001860 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1861 return 0;
Ido Schimmel97989ee2017-03-10 08:53:38 +01001862 case RTN_UNREACHABLE: /* fall through */
1863 case RTN_BLACKHOLE: /* fall through */
1864 case RTN_PROHIBIT:
1865 /* Packets hitting these routes need to be trapped, but
1866 * can do so with a lower priority than packets directed
1867 * at the host, so use action type local instead of trap.
1868 */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001869 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Ido Schimmel97989ee2017-03-10 08:53:38 +01001870 return 0;
1871 case RTN_UNICAST:
1872 if (fi->fib_nh->nh_scope != RT_SCOPE_LINK)
1873 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1874 else
1875 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
1876 return 0;
1877 default:
1878 return -EINVAL;
1879 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001880}
1881
Jiri Pirko5b004412016-09-01 10:37:40 +02001882static struct mlxsw_sp_fib_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01001883mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
1884 struct mlxsw_sp_fib_node *fib_node,
1885 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02001886{
1887 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01001888 int err;
1889
1890 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
1891 if (!fib_entry) {
1892 err = -ENOMEM;
1893 goto err_fib_entry_alloc;
1894 }
1895
1896 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
1897 if (err)
1898 goto err_fib4_entry_type_set;
1899
1900 err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fen_info->fi);
1901 if (err)
1902 goto err_nexthop_group_get;
1903
1904 fib_entry->params.prio = fen_info->fi->fib_priority;
1905 fib_entry->params.tb_id = fen_info->tb_id;
1906 fib_entry->params.type = fen_info->type;
1907 fib_entry->params.tos = fen_info->tos;
1908
1909 fib_entry->fib_node = fib_node;
1910
1911 return fib_entry;
1912
1913err_nexthop_group_get:
1914err_fib4_entry_type_set:
1915 kfree(fib_entry);
1916err_fib_entry_alloc:
1917 return ERR_PTR(err);
1918}
1919
1920static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1921 struct mlxsw_sp_fib_entry *fib_entry)
1922{
1923 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
1924 kfree(fib_entry);
1925}
1926
1927static struct mlxsw_sp_fib_node *
1928mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
1929 const struct fib_entry_notifier_info *fen_info);
1930
1931static struct mlxsw_sp_fib_entry *
1932mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
1933 const struct fib_entry_notifier_info *fen_info)
1934{
1935 struct mlxsw_sp_fib_entry *fib_entry;
1936 struct mlxsw_sp_fib_node *fib_node;
1937
1938 fib_node = mlxsw_sp_fib4_node_get(mlxsw_sp, fen_info);
1939 if (IS_ERR(fib_node))
1940 return NULL;
1941
1942 list_for_each_entry(fib_entry, &fib_node->entry_list, list) {
1943 if (fib_entry->params.tb_id == fen_info->tb_id &&
1944 fib_entry->params.tos == fen_info->tos &&
1945 fib_entry->params.type == fen_info->type &&
1946 fib_entry->nh_group->key.fi == fen_info->fi) {
1947 return fib_entry;
1948 }
1949 }
1950
1951 return NULL;
1952}
1953
1954static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
1955 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
1956 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
1957 .key_len = sizeof(struct mlxsw_sp_fib_key),
1958 .automatic_shrinking = true,
1959};
1960
1961static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
1962 struct mlxsw_sp_fib_node *fib_node)
1963{
1964 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
1965 mlxsw_sp_fib_ht_params);
1966}
1967
1968static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
1969 struct mlxsw_sp_fib_node *fib_node)
1970{
1971 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
1972 mlxsw_sp_fib_ht_params);
1973}
1974
1975static struct mlxsw_sp_fib_node *
1976mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
1977 size_t addr_len, unsigned char prefix_len)
1978{
1979 struct mlxsw_sp_fib_key key;
1980
1981 memset(&key, 0, sizeof(key));
1982 memcpy(key.addr, addr, addr_len);
1983 key.prefix_len = prefix_len;
1984 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
1985}
1986
1987static struct mlxsw_sp_fib_node *
Ido Schimmel76610eb2017-03-10 08:53:41 +01001988mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001989 size_t addr_len, unsigned char prefix_len)
1990{
1991 struct mlxsw_sp_fib_node *fib_node;
1992
1993 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
1994 if (!fib_node)
1995 return NULL;
1996
1997 INIT_LIST_HEAD(&fib_node->entry_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +01001998 list_add(&fib_node->list, &fib->node_list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01001999 memcpy(fib_node->key.addr, addr, addr_len);
2000 fib_node->key.prefix_len = prefix_len;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002001
2002 return fib_node;
2003}
2004
2005static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
2006{
Ido Schimmel9aecce12017-02-09 10:28:42 +01002007 list_del(&fib_node->list);
2008 WARN_ON(!list_empty(&fib_node->entry_list));
2009 kfree(fib_node);
2010}
2011
2012static bool
2013mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
2014 const struct mlxsw_sp_fib_entry *fib_entry)
2015{
2016 return list_first_entry(&fib_node->entry_list,
2017 struct mlxsw_sp_fib_entry, list) == fib_entry;
2018}
2019
2020static void mlxsw_sp_fib_node_prefix_inc(struct mlxsw_sp_fib_node *fib_node)
2021{
2022 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002023 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002024
2025 if (fib->prefix_ref_count[prefix_len]++ == 0)
2026 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
2027}
2028
2029static void mlxsw_sp_fib_node_prefix_dec(struct mlxsw_sp_fib_node *fib_node)
2030{
2031 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002032 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002033
2034 if (--fib->prefix_ref_count[prefix_len] == 0)
2035 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
2036}
2037
Ido Schimmel76610eb2017-03-10 08:53:41 +01002038static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
2039 struct mlxsw_sp_fib_node *fib_node,
2040 struct mlxsw_sp_fib *fib)
2041{
2042 struct mlxsw_sp_prefix_usage req_prefix_usage;
2043 struct mlxsw_sp_lpm_tree *lpm_tree;
2044 int err;
2045
2046 err = mlxsw_sp_fib_node_insert(fib, fib_node);
2047 if (err)
2048 return err;
2049 fib_node->fib = fib;
2050
2051 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &fib->prefix_usage);
2052 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
2053
2054 if (!mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2055 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib,
2056 &req_prefix_usage);
2057 if (err)
2058 goto err_tree_check;
2059 } else {
2060 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
2061 fib->proto);
2062 if (IS_ERR(lpm_tree))
2063 return PTR_ERR(lpm_tree);
2064 fib->lpm_tree = lpm_tree;
2065 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
2066 if (err)
2067 goto err_tree_bind;
2068 }
2069
2070 mlxsw_sp_fib_node_prefix_inc(fib_node);
2071
2072 return 0;
2073
2074err_tree_bind:
2075 fib->lpm_tree = NULL;
2076 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2077err_tree_check:
2078 fib_node->fib = NULL;
2079 mlxsw_sp_fib_node_remove(fib, fib_node);
2080 return err;
2081}
2082
2083static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
2084 struct mlxsw_sp_fib_node *fib_node)
2085{
2086 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
2087 struct mlxsw_sp_fib *fib = fib_node->fib;
2088
2089 mlxsw_sp_fib_node_prefix_dec(fib_node);
2090
2091 if (mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2092 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
2093 fib->lpm_tree = NULL;
2094 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2095 } else {
2096 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib, &fib->prefix_usage);
2097 }
2098
2099 fib_node->fib = NULL;
2100 mlxsw_sp_fib_node_remove(fib, fib_node);
2101}
2102
Ido Schimmel9aecce12017-02-09 10:28:42 +01002103static struct mlxsw_sp_fib_node *
2104mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
2105 const struct fib_entry_notifier_info *fen_info)
2106{
2107 struct mlxsw_sp_fib_node *fib_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002108 struct mlxsw_sp_fib *fib;
Jiri Pirko5b004412016-09-01 10:37:40 +02002109 struct mlxsw_sp_vr *vr;
2110 int err;
2111
Ido Schimmel76610eb2017-03-10 08:53:41 +01002112 vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->tb_id);
Jiri Pirko5b004412016-09-01 10:37:40 +02002113 if (IS_ERR(vr))
2114 return ERR_CAST(vr);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002115 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirko5b004412016-09-01 10:37:40 +02002116
Ido Schimmel76610eb2017-03-10 08:53:41 +01002117 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002118 sizeof(fen_info->dst),
2119 fen_info->dst_len);
2120 if (fib_node)
2121 return fib_node;
2122
Ido Schimmel76610eb2017-03-10 08:53:41 +01002123 fib_node = mlxsw_sp_fib_node_create(fib, &fen_info->dst,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002124 sizeof(fen_info->dst),
2125 fen_info->dst_len);
2126 if (!fib_node) {
Jiri Pirko5b004412016-09-01 10:37:40 +02002127 err = -ENOMEM;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002128 goto err_fib_node_create;
Jiri Pirko5b004412016-09-01 10:37:40 +02002129 }
Jiri Pirko5b004412016-09-01 10:37:40 +02002130
Ido Schimmel76610eb2017-03-10 08:53:41 +01002131 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
2132 if (err)
2133 goto err_fib_node_init;
2134
Ido Schimmel9aecce12017-02-09 10:28:42 +01002135 return fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002136
Ido Schimmel76610eb2017-03-10 08:53:41 +01002137err_fib_node_init:
2138 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002139err_fib_node_create:
Ido Schimmel76610eb2017-03-10 08:53:41 +01002140 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002141 return ERR_PTR(err);
2142}
2143
Ido Schimmel9aecce12017-02-09 10:28:42 +01002144static void mlxsw_sp_fib4_node_put(struct mlxsw_sp *mlxsw_sp,
2145 struct mlxsw_sp_fib_node *fib_node)
Jiri Pirko5b004412016-09-01 10:37:40 +02002146{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002147 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
Jiri Pirko5b004412016-09-01 10:37:40 +02002148
Ido Schimmel9aecce12017-02-09 10:28:42 +01002149 if (!list_empty(&fib_node->entry_list))
2150 return;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002151 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002152 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002153 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002154}
2155
Ido Schimmel9aecce12017-02-09 10:28:42 +01002156static struct mlxsw_sp_fib_entry *
2157mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
2158 const struct mlxsw_sp_fib_entry_params *params)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002159{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002160 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002161
2162 list_for_each_entry(fib_entry, &fib_node->entry_list, list) {
2163 if (fib_entry->params.tb_id > params->tb_id)
2164 continue;
2165 if (fib_entry->params.tb_id != params->tb_id)
2166 break;
2167 if (fib_entry->params.tos > params->tos)
2168 continue;
2169 if (fib_entry->params.prio >= params->prio ||
2170 fib_entry->params.tos < params->tos)
2171 return fib_entry;
2172 }
2173
2174 return NULL;
2175}
2176
Ido Schimmel4283bce2017-02-09 10:28:43 +01002177static int mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib_entry *fib_entry,
2178 struct mlxsw_sp_fib_entry *new_entry)
2179{
2180 struct mlxsw_sp_fib_node *fib_node;
2181
2182 if (WARN_ON(!fib_entry))
2183 return -EINVAL;
2184
2185 fib_node = fib_entry->fib_node;
2186 list_for_each_entry_from(fib_entry, &fib_node->entry_list, list) {
2187 if (fib_entry->params.tb_id != new_entry->params.tb_id ||
2188 fib_entry->params.tos != new_entry->params.tos ||
2189 fib_entry->params.prio != new_entry->params.prio)
2190 break;
2191 }
2192
2193 list_add_tail(&new_entry->list, &fib_entry->list);
2194 return 0;
2195}
2196
Ido Schimmel9aecce12017-02-09 10:28:42 +01002197static int
2198mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib_node *fib_node,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002199 struct mlxsw_sp_fib_entry *new_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002200 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002201{
2202 struct mlxsw_sp_fib_entry *fib_entry;
2203
2204 fib_entry = mlxsw_sp_fib4_node_entry_find(fib_node, &new_entry->params);
2205
Ido Schimmel4283bce2017-02-09 10:28:43 +01002206 if (append)
2207 return mlxsw_sp_fib4_node_list_append(fib_entry, new_entry);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002208 if (replace && WARN_ON(!fib_entry))
2209 return -EINVAL;
Ido Schimmel4283bce2017-02-09 10:28:43 +01002210
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002211 /* Insert new entry before replaced one, so that we can later
2212 * remove the second.
2213 */
Ido Schimmel9aecce12017-02-09 10:28:42 +01002214 if (fib_entry) {
2215 list_add_tail(&new_entry->list, &fib_entry->list);
2216 } else {
2217 struct mlxsw_sp_fib_entry *last;
2218
2219 list_for_each_entry(last, &fib_node->entry_list, list) {
2220 if (new_entry->params.tb_id > last->params.tb_id)
2221 break;
2222 fib_entry = last;
2223 }
2224
2225 if (fib_entry)
2226 list_add(&new_entry->list, &fib_entry->list);
2227 else
2228 list_add(&new_entry->list, &fib_node->entry_list);
2229 }
2230
2231 return 0;
2232}
2233
2234static void
2235mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib_entry *fib_entry)
2236{
2237 list_del(&fib_entry->list);
2238}
2239
2240static int
2241mlxsw_sp_fib4_node_entry_add(struct mlxsw_sp *mlxsw_sp,
2242 const struct mlxsw_sp_fib_node *fib_node,
2243 struct mlxsw_sp_fib_entry *fib_entry)
2244{
2245 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2246 return 0;
2247
2248 /* To prevent packet loss, overwrite the previously offloaded
2249 * entry.
2250 */
2251 if (!list_is_singular(&fib_node->entry_list)) {
2252 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2253 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2254
2255 mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
2256 }
2257
2258 return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
2259}
2260
2261static void
2262mlxsw_sp_fib4_node_entry_del(struct mlxsw_sp *mlxsw_sp,
2263 const struct mlxsw_sp_fib_node *fib_node,
2264 struct mlxsw_sp_fib_entry *fib_entry)
2265{
2266 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2267 return;
2268
2269 /* Promote the next entry by overwriting the deleted entry */
2270 if (!list_is_singular(&fib_node->entry_list)) {
2271 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2272 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2273
2274 mlxsw_sp_fib_entry_update(mlxsw_sp, n);
2275 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
2276 return;
2277 }
2278
2279 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
2280}
2281
2282static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002283 struct mlxsw_sp_fib_entry *fib_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002284 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002285{
2286 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2287 int err;
2288
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002289 err = mlxsw_sp_fib4_node_list_insert(fib_node, fib_entry, replace,
2290 append);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002291 if (err)
2292 return err;
2293
2294 err = mlxsw_sp_fib4_node_entry_add(mlxsw_sp, fib_node, fib_entry);
2295 if (err)
2296 goto err_fib4_node_entry_add;
2297
Ido Schimmel9aecce12017-02-09 10:28:42 +01002298 return 0;
2299
2300err_fib4_node_entry_add:
2301 mlxsw_sp_fib4_node_list_remove(fib_entry);
2302 return err;
2303}
2304
2305static void
2306mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
2307 struct mlxsw_sp_fib_entry *fib_entry)
2308{
2309 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2310
Ido Schimmel9aecce12017-02-09 10:28:42 +01002311 mlxsw_sp_fib4_node_entry_del(mlxsw_sp, fib_node, fib_entry);
2312 mlxsw_sp_fib4_node_list_remove(fib_entry);
2313}
2314
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002315static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
2316 struct mlxsw_sp_fib_entry *fib_entry,
2317 bool replace)
2318{
2319 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2320 struct mlxsw_sp_fib_entry *replaced;
2321
2322 if (!replace)
2323 return;
2324
2325 /* We inserted the new entry before replaced one */
2326 replaced = list_next_entry(fib_entry, list);
2327
2328 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, replaced);
2329 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, replaced);
2330 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
2331}
2332
Ido Schimmel9aecce12017-02-09 10:28:42 +01002333static int
2334mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002335 const struct fib_entry_notifier_info *fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002336 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002337{
2338 struct mlxsw_sp_fib_entry *fib_entry;
2339 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002340 int err;
2341
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002342 if (mlxsw_sp->router.aborted)
2343 return 0;
2344
Ido Schimmel9aecce12017-02-09 10:28:42 +01002345 fib_node = mlxsw_sp_fib4_node_get(mlxsw_sp, fen_info);
2346 if (IS_ERR(fib_node)) {
2347 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
2348 return PTR_ERR(fib_node);
2349 }
2350
2351 fib_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002352 if (IS_ERR(fib_entry)) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002353 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
2354 err = PTR_ERR(fib_entry);
2355 goto err_fib4_entry_create;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002356 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02002357
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002358 err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib_entry, replace,
2359 append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002360 if (err) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002361 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
2362 goto err_fib4_node_entry_link;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002363 }
Ido Schimmel9aecce12017-02-09 10:28:42 +01002364
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002365 mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib_entry, replace);
2366
Jiri Pirko61c503f2016-07-04 08:23:11 +02002367 return 0;
2368
Ido Schimmel9aecce12017-02-09 10:28:42 +01002369err_fib4_node_entry_link:
2370 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2371err_fib4_entry_create:
2372 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002373 return err;
2374}
2375
Jiri Pirko37956d72016-10-20 16:05:43 +02002376static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
2377 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002378{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002379 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002380 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002381
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002382 if (mlxsw_sp->router.aborted)
Jiri Pirko37956d72016-10-20 16:05:43 +02002383 return;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002384
Ido Schimmel9aecce12017-02-09 10:28:42 +01002385 fib_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
2386 if (WARN_ON(!fib_entry))
Jiri Pirko37956d72016-10-20 16:05:43 +02002387 return;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002388 fib_node = fib_entry->fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002389
Ido Schimmel9aecce12017-02-09 10:28:42 +01002390 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib_entry);
2391 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2392 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002393}
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002394
2395static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
2396{
2397 char ralta_pl[MLXSW_REG_RALTA_LEN];
2398 char ralst_pl[MLXSW_REG_RALST_LEN];
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002399 int i, err;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002400
2401 mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4,
2402 MLXSW_SP_LPM_TREE_MIN);
2403 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
2404 if (err)
2405 return err;
2406
2407 mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN);
2408 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
2409 if (err)
2410 return err;
2411
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002412 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
2413 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[i];
2414 char raltb_pl[MLXSW_REG_RALTB_LEN];
2415 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002416
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002417 if (!mlxsw_sp_vr_is_used(vr))
2418 continue;
2419
2420 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
2421 MLXSW_REG_RALXX_PROTOCOL_IPV4,
2422 MLXSW_SP_LPM_TREE_MIN);
2423 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
2424 raltb_pl);
2425 if (err)
2426 return err;
2427
2428 mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4,
2429 MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0,
2430 0);
2431 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
2432 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
2433 ralue_pl);
2434 if (err)
2435 return err;
2436 }
2437
2438 return 0;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002439}
2440
Ido Schimmel9aecce12017-02-09 10:28:42 +01002441static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
2442 struct mlxsw_sp_fib_node *fib_node)
2443{
2444 struct mlxsw_sp_fib_entry *fib_entry, *tmp;
2445
2446 list_for_each_entry_safe(fib_entry, tmp, &fib_node->entry_list, list) {
2447 bool do_break = &tmp->list == &fib_node->entry_list;
2448
2449 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib_entry);
2450 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2451 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
2452 /* Break when entry list is empty and node was freed.
2453 * Otherwise, we'll access freed memory in the next
2454 * iteration.
2455 */
2456 if (do_break)
2457 break;
2458 }
2459}
2460
2461static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
2462 struct mlxsw_sp_fib_node *fib_node)
2463{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002464 switch (fib_node->fib->proto) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002465 case MLXSW_SP_L3_PROTO_IPV4:
2466 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
2467 break;
2468 case MLXSW_SP_L3_PROTO_IPV6:
2469 WARN_ON_ONCE(1);
2470 break;
2471 }
2472}
2473
Ido Schimmel76610eb2017-03-10 08:53:41 +01002474static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
2475 struct mlxsw_sp_vr *vr,
2476 enum mlxsw_sp_l3proto proto)
2477{
2478 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
2479 struct mlxsw_sp_fib_node *fib_node, *tmp;
2480
2481 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
2482 bool do_break = &tmp->list == &fib->node_list;
2483
2484 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
2485 if (do_break)
2486 break;
2487 }
2488}
2489
Ido Schimmelac571de2016-11-14 11:26:32 +01002490static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002491{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002492 int i;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002493
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002494 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel76610eb2017-03-10 08:53:41 +01002495 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[i];
Ido Schimmelac571de2016-11-14 11:26:32 +01002496
Ido Schimmel76610eb2017-03-10 08:53:41 +01002497 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002498 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002499 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002500 }
Ido Schimmelac571de2016-11-14 11:26:32 +01002501}
2502
2503static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp)
2504{
2505 int err;
2506
Ido Schimmeld331d302016-11-16 09:51:58 +01002507 if (mlxsw_sp->router.aborted)
2508 return;
2509 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
Ido Schimmelac571de2016-11-14 11:26:32 +01002510 mlxsw_sp_router_fib_flush(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002511 mlxsw_sp->router.aborted = true;
2512 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
2513 if (err)
2514 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
2515}
2516
Ido Schimmel30572242016-12-03 16:45:01 +01002517struct mlxsw_sp_fib_event_work {
Ido Schimmela0e47612017-02-06 16:20:10 +01002518 struct work_struct work;
Ido Schimmelad178c82017-02-08 11:16:40 +01002519 union {
2520 struct fib_entry_notifier_info fen_info;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002521 struct fib_rule_notifier_info fr_info;
Ido Schimmelad178c82017-02-08 11:16:40 +01002522 struct fib_nh_notifier_info fnh_info;
2523 };
Ido Schimmel30572242016-12-03 16:45:01 +01002524 struct mlxsw_sp *mlxsw_sp;
2525 unsigned long event;
2526};
2527
2528static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002529{
Ido Schimmel30572242016-12-03 16:45:01 +01002530 struct mlxsw_sp_fib_event_work *fib_work =
Ido Schimmela0e47612017-02-06 16:20:10 +01002531 container_of(work, struct mlxsw_sp_fib_event_work, work);
Ido Schimmel30572242016-12-03 16:45:01 +01002532 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002533 struct fib_rule *rule;
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002534 bool replace, append;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002535 int err;
2536
Ido Schimmel30572242016-12-03 16:45:01 +01002537 /* Protect internal structures from changes */
2538 rtnl_lock();
2539 switch (fib_work->event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002540 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01002541 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002542 case FIB_EVENT_ENTRY_ADD:
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002543 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel4283bce2017-02-09 10:28:43 +01002544 append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
2545 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002546 replace, append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002547 if (err)
2548 mlxsw_sp_router_fib4_abort(mlxsw_sp);
Ido Schimmel30572242016-12-03 16:45:01 +01002549 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002550 break;
2551 case FIB_EVENT_ENTRY_DEL:
Ido Schimmel30572242016-12-03 16:45:01 +01002552 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
2553 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002554 break;
2555 case FIB_EVENT_RULE_ADD: /* fall through */
2556 case FIB_EVENT_RULE_DEL:
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002557 rule = fib_work->fr_info.rule;
Ido Schimmelc7f6e662017-03-16 09:08:20 +01002558 if (!fib4_rule_default(rule) && !rule->l3mdev)
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002559 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2560 fib_rule_put(rule);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002561 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002562 case FIB_EVENT_NH_ADD: /* fall through */
2563 case FIB_EVENT_NH_DEL:
2564 mlxsw_sp_nexthop_event(mlxsw_sp, fib_work->event,
2565 fib_work->fnh_info.fib_nh);
2566 fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
2567 break;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002568 }
Ido Schimmel30572242016-12-03 16:45:01 +01002569 rtnl_unlock();
2570 kfree(fib_work);
2571}
2572
2573/* Called with rcu_read_lock() */
2574static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
2575 unsigned long event, void *ptr)
2576{
2577 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
2578 struct mlxsw_sp_fib_event_work *fib_work;
2579 struct fib_notifier_info *info = ptr;
2580
2581 if (!net_eq(info->net, &init_net))
2582 return NOTIFY_DONE;
2583
2584 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2585 if (WARN_ON(!fib_work))
2586 return NOTIFY_BAD;
2587
Ido Schimmela0e47612017-02-06 16:20:10 +01002588 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work);
Ido Schimmel30572242016-12-03 16:45:01 +01002589 fib_work->mlxsw_sp = mlxsw_sp;
2590 fib_work->event = event;
2591
2592 switch (event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002593 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01002594 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Ido Schimmel30572242016-12-03 16:45:01 +01002595 case FIB_EVENT_ENTRY_ADD: /* fall through */
2596 case FIB_EVENT_ENTRY_DEL:
2597 memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2598 /* Take referece on fib_info to prevent it from being
2599 * freed while work is queued. Release it afterwards.
2600 */
2601 fib_info_hold(fib_work->fen_info.fi);
2602 break;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002603 case FIB_EVENT_RULE_ADD: /* fall through */
2604 case FIB_EVENT_RULE_DEL:
2605 memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
2606 fib_rule_get(fib_work->fr_info.rule);
2607 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002608 case FIB_EVENT_NH_ADD: /* fall through */
2609 case FIB_EVENT_NH_DEL:
2610 memcpy(&fib_work->fnh_info, ptr, sizeof(fib_work->fnh_info));
2611 fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
2612 break;
Ido Schimmel30572242016-12-03 16:45:01 +01002613 }
2614
Ido Schimmela0e47612017-02-06 16:20:10 +01002615 mlxsw_core_schedule_work(&fib_work->work);
Ido Schimmel30572242016-12-03 16:45:01 +01002616
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002617 return NOTIFY_DONE;
2618}
2619
Ido Schimmel4724ba562017-03-10 08:53:39 +01002620static struct mlxsw_sp_rif *
2621mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
2622 const struct net_device *dev)
2623{
2624 int i;
2625
2626 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2627 if (mlxsw_sp->rifs[i] && mlxsw_sp->rifs[i]->dev == dev)
2628 return mlxsw_sp->rifs[i];
2629
2630 return NULL;
2631}
2632
2633static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
2634{
2635 char ritr_pl[MLXSW_REG_RITR_LEN];
2636 int err;
2637
2638 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
2639 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2640 if (WARN_ON_ONCE(err))
2641 return err;
2642
2643 mlxsw_reg_ritr_enable_set(ritr_pl, false);
2644 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2645}
2646
2647static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002648 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002649{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002650 mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
2651 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
2652 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002653}
2654
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002655static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif,
Ido Schimmel4724ba562017-03-10 08:53:39 +01002656 const struct in_device *in_dev,
2657 unsigned long event)
2658{
2659 switch (event) {
2660 case NETDEV_UP:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002661 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002662 return true;
2663 return false;
2664 case NETDEV_DOWN:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002665 if (rif && !in_dev->ifa_list &&
2666 !netif_is_l3_slave(rif->dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01002667 return true;
2668 /* It is possible we already removed the RIF ourselves
2669 * if it was assigned to a netdev that is now a bridge
2670 * or LAG slave.
2671 */
2672 return false;
2673 }
2674
2675 return false;
2676}
2677
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002678#define MLXSW_SP_INVALID_INDEX_RIF 0xffff
Ido Schimmel4724ba562017-03-10 08:53:39 +01002679static int mlxsw_sp_avail_rif_get(struct mlxsw_sp *mlxsw_sp)
2680{
2681 int i;
2682
2683 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2684 if (!mlxsw_sp->rifs[i])
2685 return i;
2686
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002687 return MLXSW_SP_INVALID_INDEX_RIF;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002688}
2689
2690static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port *mlxsw_sp_vport,
2691 bool *p_lagged, u16 *p_system_port)
2692{
2693 u8 local_port = mlxsw_sp_vport->local_port;
2694
2695 *p_lagged = mlxsw_sp_vport->lagged;
2696 *p_system_port = *p_lagged ? mlxsw_sp_vport->lag_id : local_port;
2697}
2698
2699static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port *mlxsw_sp_vport,
Ido Schimmel69132292017-03-10 08:53:42 +01002700 u16 vr_id, struct net_device *l3_dev,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002701 u16 rif_index, bool create)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002702{
2703 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
2704 bool lagged = mlxsw_sp_vport->lagged;
2705 char ritr_pl[MLXSW_REG_RITR_LEN];
2706 u16 system_port;
2707
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002708 mlxsw_reg_ritr_pack(ritr_pl, create, MLXSW_REG_RITR_SP_IF, rif_index,
2709 vr_id, l3_dev->mtu, l3_dev->dev_addr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002710
2711 mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport, &lagged, &system_port);
2712 mlxsw_reg_ritr_sp_if_pack(ritr_pl, lagged, system_port,
2713 mlxsw_sp_vport_vid_get(mlxsw_sp_vport));
2714
2715 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2716}
2717
2718static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
2719
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002720static u16 mlxsw_sp_rif_sp_to_fid(u16 rif_index)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002721{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002722 return MLXSW_SP_RFID_BASE + rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002723}
2724
2725static struct mlxsw_sp_fid *
2726mlxsw_sp_rfid_alloc(u16 fid, struct net_device *l3_dev)
2727{
2728 struct mlxsw_sp_fid *f;
2729
2730 f = kzalloc(sizeof(*f), GFP_KERNEL);
2731 if (!f)
2732 return NULL;
2733
2734 f->leave = mlxsw_sp_vport_rif_sp_leave;
2735 f->ref_count = 0;
2736 f->dev = l3_dev;
2737 f->fid = fid;
2738
2739 return f;
2740}
2741
2742static struct mlxsw_sp_rif *
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002743mlxsw_sp_rif_alloc(u16 rif_index, u16 vr_id, struct net_device *l3_dev,
Ido Schimmel69132292017-03-10 08:53:42 +01002744 struct mlxsw_sp_fid *f)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002745{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002746 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002747
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002748 rif = kzalloc(sizeof(*rif), GFP_KERNEL);
2749 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002750 return NULL;
2751
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002752 INIT_LIST_HEAD(&rif->nexthop_list);
2753 INIT_LIST_HEAD(&rif->neigh_list);
2754 ether_addr_copy(rif->addr, l3_dev->dev_addr);
2755 rif->mtu = l3_dev->mtu;
2756 rif->vr_id = vr_id;
2757 rif->dev = l3_dev;
2758 rif->rif_index = rif_index;
2759 rif->f = f;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002760
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002761 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002762}
2763
2764static struct mlxsw_sp_rif *
2765mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
2766 struct net_device *l3_dev)
2767{
2768 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Ido Schimmel57837882017-03-16 09:08:16 +01002769 u32 tb_id = l3mdev_fib_table(l3_dev);
Ido Schimmel69132292017-03-10 08:53:42 +01002770 struct mlxsw_sp_vr *vr;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002771 struct mlxsw_sp_fid *f;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002772 struct mlxsw_sp_rif *rif;
2773 u16 fid, rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002774 int err;
2775
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002776 rif_index = mlxsw_sp_avail_rif_get(mlxsw_sp);
2777 if (rif_index == MLXSW_SP_INVALID_INDEX_RIF)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002778 return ERR_PTR(-ERANGE);
2779
Ido Schimmel57837882017-03-16 09:08:16 +01002780 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
Ido Schimmel69132292017-03-10 08:53:42 +01002781 if (IS_ERR(vr))
2782 return ERR_CAST(vr);
2783
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002784 err = mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev,
2785 rif_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002786 if (err)
Ido Schimmel69132292017-03-10 08:53:42 +01002787 goto err_vport_rif_sp_op;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002788
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002789 fid = mlxsw_sp_rif_sp_to_fid(rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002790 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, true);
2791 if (err)
2792 goto err_rif_fdb_op;
2793
2794 f = mlxsw_sp_rfid_alloc(fid, l3_dev);
2795 if (!f) {
2796 err = -ENOMEM;
2797 goto err_rfid_alloc;
2798 }
2799
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002800 rif = mlxsw_sp_rif_alloc(rif_index, vr->id, l3_dev, f);
2801 if (!rif) {
Ido Schimmel4724ba562017-03-10 08:53:39 +01002802 err = -ENOMEM;
2803 goto err_rif_alloc;
2804 }
2805
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002806 f->rif = rif;
2807 mlxsw_sp->rifs[rif_index] = rif;
Ido Schimmel69132292017-03-10 08:53:42 +01002808 vr->rif_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002809
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002810 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002811
2812err_rif_alloc:
2813 kfree(f);
2814err_rfid_alloc:
2815 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
2816err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002817 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif_index,
2818 false);
Ido Schimmel69132292017-03-10 08:53:42 +01002819err_vport_rif_sp_op:
2820 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002821 return ERR_PTR(err);
2822}
2823
2824static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002825 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002826{
2827 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002828 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[rif->vr_id];
2829 struct net_device *l3_dev = rif->dev;
2830 struct mlxsw_sp_fid *f = rif->f;
2831 u16 rif_index = rif->rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002832 u16 fid = f->fid;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002833
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002834 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002835
Ido Schimmel69132292017-03-10 08:53:42 +01002836 vr->rif_count--;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002837 mlxsw_sp->rifs[rif_index] = NULL;
2838 f->rif = NULL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002839
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002840 kfree(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002841
2842 kfree(f);
2843
2844 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
2845
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002846 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif_index,
2847 false);
Ido Schimmel69132292017-03-10 08:53:42 +01002848 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002849}
2850
2851static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port *mlxsw_sp_vport,
2852 struct net_device *l3_dev)
2853{
2854 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002855 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002856
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002857 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
2858 if (!rif) {
2859 rif = mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport, l3_dev);
2860 if (IS_ERR(rif))
2861 return PTR_ERR(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002862 }
2863
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002864 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, rif->f);
2865 rif->f->ref_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002866
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002867 netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", rif->f->fid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002868
2869 return 0;
2870}
2871
2872static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
2873{
2874 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
2875
2876 netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
2877
2878 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
2879 if (--f->ref_count == 0)
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002880 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport, f->rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002881}
2882
2883static int mlxsw_sp_inetaddr_vport_event(struct net_device *l3_dev,
2884 struct net_device *port_dev,
2885 unsigned long event, u16 vid)
2886{
2887 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
2888 struct mlxsw_sp_port *mlxsw_sp_vport;
2889
2890 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
2891 if (WARN_ON(!mlxsw_sp_vport))
2892 return -EINVAL;
2893
2894 switch (event) {
2895 case NETDEV_UP:
2896 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, l3_dev);
2897 case NETDEV_DOWN:
2898 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
2899 break;
2900 }
2901
2902 return 0;
2903}
2904
2905static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
2906 unsigned long event)
2907{
2908 if (netif_is_bridge_port(port_dev) || netif_is_lag_port(port_dev))
2909 return 0;
2910
2911 return mlxsw_sp_inetaddr_vport_event(port_dev, port_dev, event, 1);
2912}
2913
2914static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
2915 struct net_device *lag_dev,
2916 unsigned long event, u16 vid)
2917{
2918 struct net_device *port_dev;
2919 struct list_head *iter;
2920 int err;
2921
2922 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
2923 if (mlxsw_sp_port_dev_check(port_dev)) {
2924 err = mlxsw_sp_inetaddr_vport_event(l3_dev, port_dev,
2925 event, vid);
2926 if (err)
2927 return err;
2928 }
2929 }
2930
2931 return 0;
2932}
2933
2934static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
2935 unsigned long event)
2936{
2937 if (netif_is_bridge_port(lag_dev))
2938 return 0;
2939
2940 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1);
2941}
2942
2943static struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp,
2944 struct net_device *l3_dev)
2945{
2946 u16 fid;
2947
2948 if (is_vlan_dev(l3_dev))
2949 fid = vlan_dev_vlan_id(l3_dev);
2950 else if (mlxsw_sp->master_bridge.dev == l3_dev)
2951 fid = 1;
2952 else
2953 return mlxsw_sp_vfid_find(mlxsw_sp, l3_dev);
2954
2955 return mlxsw_sp_fid_find(mlxsw_sp, fid);
2956}
2957
2958static enum mlxsw_flood_table_type mlxsw_sp_flood_table_type_get(u16 fid)
2959{
2960 return mlxsw_sp_fid_is_vfid(fid) ? MLXSW_REG_SFGC_TABLE_TYPE_FID :
2961 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
2962}
2963
2964static u16 mlxsw_sp_flood_table_index_get(u16 fid)
2965{
2966 return mlxsw_sp_fid_is_vfid(fid) ? mlxsw_sp_fid_to_vfid(fid) : fid;
2967}
2968
2969static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp *mlxsw_sp, u16 fid,
2970 bool set)
2971{
2972 enum mlxsw_flood_table_type table_type;
2973 char *sftr_pl;
2974 u16 index;
2975 int err;
2976
2977 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
2978 if (!sftr_pl)
2979 return -ENOMEM;
2980
2981 table_type = mlxsw_sp_flood_table_type_get(fid);
2982 index = mlxsw_sp_flood_table_index_get(fid);
2983 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BC, index, table_type,
2984 1, MLXSW_PORT_ROUTER_PORT, set);
2985 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
2986
2987 kfree(sftr_pl);
2988 return err;
2989}
2990
2991static enum mlxsw_reg_ritr_if_type mlxsw_sp_rif_type_get(u16 fid)
2992{
2993 if (mlxsw_sp_fid_is_vfid(fid))
2994 return MLXSW_REG_RITR_FID_IF;
2995 else
2996 return MLXSW_REG_RITR_VLAN_IF;
2997}
2998
Ido Schimmel69132292017-03-10 08:53:42 +01002999static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp *mlxsw_sp, u16 vr_id,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003000 struct net_device *l3_dev,
3001 u16 fid, u16 rif,
3002 bool create)
3003{
3004 enum mlxsw_reg_ritr_if_type rif_type;
3005 char ritr_pl[MLXSW_REG_RITR_LEN];
3006
3007 rif_type = mlxsw_sp_rif_type_get(fid);
Ido Schimmel69132292017-03-10 08:53:42 +01003008 mlxsw_reg_ritr_pack(ritr_pl, create, rif_type, rif, vr_id, l3_dev->mtu,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003009 l3_dev->dev_addr);
3010 mlxsw_reg_ritr_fid_set(ritr_pl, rif_type, fid);
3011
3012 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3013}
3014
3015static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp *mlxsw_sp,
3016 struct net_device *l3_dev,
3017 struct mlxsw_sp_fid *f)
3018{
Ido Schimmel57837882017-03-16 09:08:16 +01003019 u32 tb_id = l3mdev_fib_table(l3_dev);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003020 struct mlxsw_sp_rif *rif;
Ido Schimmel69132292017-03-10 08:53:42 +01003021 struct mlxsw_sp_vr *vr;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003022 u16 rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003023 int err;
3024
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003025 rif_index = mlxsw_sp_avail_rif_get(mlxsw_sp);
3026 if (rif_index == MLXSW_SP_INVALID_INDEX_RIF)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003027 return -ERANGE;
3028
Ido Schimmel57837882017-03-16 09:08:16 +01003029 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
Ido Schimmel69132292017-03-10 08:53:42 +01003030 if (IS_ERR(vr))
3031 return PTR_ERR(vr);
3032
Ido Schimmel4724ba562017-03-10 08:53:39 +01003033 err = mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, true);
3034 if (err)
Ido Schimmel69132292017-03-10 08:53:42 +01003035 goto err_port_flood_set;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003036
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003037 err = mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid,
3038 rif_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003039 if (err)
3040 goto err_rif_bridge_op;
3041
3042 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, true);
3043 if (err)
3044 goto err_rif_fdb_op;
3045
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003046 rif = mlxsw_sp_rif_alloc(rif_index, vr->id, l3_dev, f);
3047 if (!rif) {
Ido Schimmel4724ba562017-03-10 08:53:39 +01003048 err = -ENOMEM;
3049 goto err_rif_alloc;
3050 }
3051
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003052 f->rif = rif;
3053 mlxsw_sp->rifs[rif_index] = rif;
Ido Schimmel69132292017-03-10 08:53:42 +01003054 vr->rif_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003055
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003056 netdev_dbg(l3_dev, "RIF=%d created\n", rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003057
3058 return 0;
3059
3060err_rif_alloc:
3061 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3062err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003063 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif_index,
3064 false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003065err_rif_bridge_op:
3066 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
Ido Schimmel69132292017-03-10 08:53:42 +01003067err_port_flood_set:
3068 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003069 return err;
3070}
3071
3072void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003073 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003074{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003075 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[rif->vr_id];
3076 struct net_device *l3_dev = rif->dev;
3077 struct mlxsw_sp_fid *f = rif->f;
3078 u16 rif_index = rif->rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003079
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003080 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003081
Ido Schimmel69132292017-03-10 08:53:42 +01003082 vr->rif_count--;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003083 mlxsw_sp->rifs[rif_index] = NULL;
3084 f->rif = NULL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003085
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003086 kfree(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003087
3088 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3089
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003090 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif_index,
3091 false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003092
3093 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
3094
Ido Schimmel69132292017-03-10 08:53:42 +01003095 mlxsw_sp_vr_put(vr);
3096
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003097 netdev_dbg(l3_dev, "RIF=%d destroyed\n", rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003098}
3099
3100static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
3101 struct net_device *br_dev,
3102 unsigned long event)
3103{
3104 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3105 struct mlxsw_sp_fid *f;
3106
3107 /* FID can either be an actual FID if the L3 device is the
3108 * VLAN-aware bridge or a VLAN device on top. Otherwise, the
3109 * L3 device is a VLAN-unaware bridge and we get a vFID.
3110 */
3111 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3112 if (WARN_ON(!f))
3113 return -EINVAL;
3114
3115 switch (event) {
3116 case NETDEV_UP:
3117 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3118 case NETDEV_DOWN:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003119 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003120 break;
3121 }
3122
3123 return 0;
3124}
3125
3126static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
3127 unsigned long event)
3128{
3129 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
3130 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
3131 u16 vid = vlan_dev_vlan_id(vlan_dev);
3132
3133 if (mlxsw_sp_port_dev_check(real_dev))
3134 return mlxsw_sp_inetaddr_vport_event(vlan_dev, real_dev, event,
3135 vid);
3136 else if (netif_is_lag_master(real_dev))
3137 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
3138 vid);
3139 else if (netif_is_bridge_master(real_dev) &&
3140 mlxsw_sp->master_bridge.dev == real_dev)
3141 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, real_dev,
3142 event);
3143
3144 return 0;
3145}
3146
3147int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
3148 unsigned long event, void *ptr)
3149{
3150 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3151 struct net_device *dev = ifa->ifa_dev->dev;
3152 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003153 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003154 int err = 0;
3155
3156 mlxsw_sp = mlxsw_sp_lower_get(dev);
3157 if (!mlxsw_sp)
3158 goto out;
3159
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003160 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3161 if (!mlxsw_sp_rif_should_config(rif, ifa->ifa_dev, event))
Ido Schimmel4724ba562017-03-10 08:53:39 +01003162 goto out;
3163
3164 if (mlxsw_sp_port_dev_check(dev))
3165 err = mlxsw_sp_inetaddr_port_event(dev, event);
3166 else if (netif_is_lag_master(dev))
3167 err = mlxsw_sp_inetaddr_lag_event(dev, event);
3168 else if (netif_is_bridge_master(dev))
3169 err = mlxsw_sp_inetaddr_bridge_event(dev, dev, event);
3170 else if (is_vlan_dev(dev))
3171 err = mlxsw_sp_inetaddr_vlan_event(dev, event);
3172
3173out:
3174 return notifier_from_errno(err);
3175}
3176
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003177static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003178 const char *mac, int mtu)
3179{
3180 char ritr_pl[MLXSW_REG_RITR_LEN];
3181 int err;
3182
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003183 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003184 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3185 if (err)
3186 return err;
3187
3188 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
3189 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
3190 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
3191 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3192}
3193
3194int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
3195{
3196 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003197 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003198 int err;
3199
3200 mlxsw_sp = mlxsw_sp_lower_get(dev);
3201 if (!mlxsw_sp)
3202 return 0;
3203
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003204 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3205 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003206 return 0;
3207
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003208 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, rif->f->fid, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003209 if (err)
3210 return err;
3211
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003212 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
3213 dev->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003214 if (err)
3215 goto err_rif_edit;
3216
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003217 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, rif->f->fid, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003218 if (err)
3219 goto err_rif_fdb_op;
3220
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003221 ether_addr_copy(rif->addr, dev->dev_addr);
3222 rif->mtu = dev->mtu;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003223
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003224 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003225
3226 return 0;
3227
3228err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003229 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003230err_rif_edit:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003231 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, rif->f->fid, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003232 return err;
3233}
3234
Ido Schimmel7179eb52017-03-16 09:08:18 +01003235int mlxsw_sp_vport_vrf_join(struct mlxsw_sp_port *mlxsw_sp_vport)
3236{
3237 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3238 struct net_device *dev = mlxsw_sp_vport->dev;
3239
3240 /* In case vPort already has a RIF, then we need to drop it.
3241 * A new one will be created using the VRF's VR.
3242 */
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003243 if (f && f->rif)
Ido Schimmel7179eb52017-03-16 09:08:18 +01003244 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3245
3246 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, dev);
3247}
3248
3249void mlxsw_sp_vport_vrf_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
3250{
3251 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3252}
3253
3254int mlxsw_sp_port_vrf_join(struct mlxsw_sp_port *mlxsw_sp_port)
3255{
3256 struct mlxsw_sp_port *mlxsw_sp_vport;
3257
3258 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
3259 if (WARN_ON(!mlxsw_sp_vport))
3260 return -EINVAL;
3261
3262 return mlxsw_sp_vport_vrf_join(mlxsw_sp_vport);
3263}
3264
3265void mlxsw_sp_port_vrf_leave(struct mlxsw_sp_port *mlxsw_sp_port)
3266{
3267 struct mlxsw_sp_port *mlxsw_sp_vport;
3268
3269 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
3270 if (WARN_ON(!mlxsw_sp_vport))
3271 return;
3272
3273 mlxsw_sp_vport_vrf_leave(mlxsw_sp_vport);
3274}
3275
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003276int mlxsw_sp_bridge_vrf_join(struct mlxsw_sp *mlxsw_sp,
3277 struct net_device *l3_dev)
3278{
3279 struct mlxsw_sp_fid *f;
3280
3281 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3282 if (WARN_ON(!f))
3283 return -EINVAL;
3284
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003285 if (f->rif)
3286 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->rif);
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003287
3288 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3289}
3290
3291void mlxsw_sp_bridge_vrf_leave(struct mlxsw_sp *mlxsw_sp,
3292 struct net_device *l3_dev)
3293{
3294 struct mlxsw_sp_fid *f;
3295
3296 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3297 if (WARN_ON(!f))
3298 return;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003299 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->rif);
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003300}
3301
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003302static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
3303{
3304 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
3305
3306 /* Flush pending FIB notifications and then flush the device's
3307 * table before requesting another dump. The FIB notification
3308 * block is unregistered, so no need to take RTNL.
3309 */
3310 mlxsw_core_flush_owq();
3311 mlxsw_sp_router_fib_flush(mlxsw_sp);
3312}
3313
Ido Schimmel4724ba562017-03-10 08:53:39 +01003314static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3315{
3316 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3317 u64 max_rifs;
3318 int err;
3319
3320 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
3321 return -EIO;
3322
3323 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
3324 mlxsw_sp->rifs = kcalloc(max_rifs, sizeof(struct mlxsw_sp_rif *),
3325 GFP_KERNEL);
3326 if (!mlxsw_sp->rifs)
3327 return -ENOMEM;
3328
3329 mlxsw_reg_rgcr_pack(rgcr_pl, true);
3330 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
3331 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3332 if (err)
3333 goto err_rgcr_fail;
3334
3335 return 0;
3336
3337err_rgcr_fail:
3338 kfree(mlxsw_sp->rifs);
3339 return err;
3340}
3341
3342static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3343{
3344 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3345 int i;
3346
3347 mlxsw_reg_rgcr_pack(rgcr_pl, false);
3348 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3349
3350 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
3351 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
3352
3353 kfree(mlxsw_sp->rifs);
3354}
3355
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003356int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3357{
3358 int err;
3359
3360 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003361 err = __mlxsw_sp_router_init(mlxsw_sp);
3362 if (err)
3363 return err;
3364
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003365 err = rhashtable_init(&mlxsw_sp->router.nexthop_ht,
3366 &mlxsw_sp_nexthop_ht_params);
3367 if (err)
3368 goto err_nexthop_ht_init;
3369
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003370 err = rhashtable_init(&mlxsw_sp->router.nexthop_group_ht,
3371 &mlxsw_sp_nexthop_group_ht_params);
3372 if (err)
3373 goto err_nexthop_group_ht_init;
3374
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003375 mlxsw_sp_lpm_init(mlxsw_sp);
3376 err = mlxsw_sp_vrs_init(mlxsw_sp);
3377 if (err)
3378 goto err_vrs_init;
3379
Ido Schimmel8c9583a2016-10-27 15:12:57 +02003380 err = mlxsw_sp_neigh_init(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003381 if (err)
3382 goto err_neigh_init;
3383
3384 mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003385 err = register_fib_notifier(&mlxsw_sp->fib_nb,
3386 mlxsw_sp_router_fib_dump_flush);
3387 if (err)
3388 goto err_register_fib_notifier;
3389
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003390 return 0;
3391
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003392err_register_fib_notifier:
3393 mlxsw_sp_neigh_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003394err_neigh_init:
3395 mlxsw_sp_vrs_fini(mlxsw_sp);
3396err_vrs_init:
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003397 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
3398err_nexthop_group_ht_init:
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003399 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
3400err_nexthop_ht_init:
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003401 __mlxsw_sp_router_fini(mlxsw_sp);
3402 return err;
3403}
3404
3405void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3406{
3407 unregister_fib_notifier(&mlxsw_sp->fib_nb);
3408 mlxsw_sp_neigh_fini(mlxsw_sp);
3409 mlxsw_sp_vrs_fini(mlxsw_sp);
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003410 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003411 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003412 __mlxsw_sp_router_fini(mlxsw_sp);
3413}