blob: 488bc1fd786803223d051d83ccba26fa60926275 [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;
63 u16 rif;
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;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100603 struct mlxsw_sp_rif *r;
604 int err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200605
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100606 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
607 if (!r)
608 return ERR_PTR(-EINVAL);
609
610 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, r->rif);
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
Ido Schimmel9665b742017-02-08 11:16:42 +0100618 list_add(&neigh_entry->rif_list_node, &r->neigh_list);
619
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,
1026 const struct mlxsw_sp_rif *r)
1027{
1028 char rauht_pl[MLXSW_REG_RAUHT_LEN];
1029
1030 mlxsw_reg_rauht_pack(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE_ALL,
1031 r->rif, r->addr);
1032 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,
1036 struct mlxsw_sp_rif *r)
1037{
1038 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
1039
1040 mlxsw_sp_neigh_rif_flush(mlxsw_sp, r);
1041 list_for_each_entry_safe(neigh_entry, tmp, &r->neigh_list,
1042 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;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001058 struct mlxsw_sp_rif *r;
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];
Ido Schimmelb8399a12017-02-08 11:16:33 +01001085#define nh_rif nexthops[0].r
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,
1375 struct mlxsw_sp_rif *r)
1376{
1377 if (nh->r)
1378 return;
1379
1380 nh->r = r;
1381 list_add(&nh->rif_list_node, &r->nexthop_list);
1382}
1383
1384static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
1385{
1386 if (!nh->r)
1387 return;
1388
1389 list_del(&nh->rif_list_node);
1390 nh->r = NULL;
1391}
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;
Ido Schimmela8c97012017-02-08 11:16:35 +01001481 struct mlxsw_sp_rif *r;
1482 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
Ido Schimmela8c97012017-02-08 11:16:35 +01001498 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
1499 if (!r)
1500 return 0;
Ido Schimmel9665b742017-02-08 11:16:42 +01001501 mlxsw_sp_nexthop_rif_init(nh, r);
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;
1527 struct mlxsw_sp_rif *r;
1528
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
1537 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fib_nh->nh_dev);
1538 if (!r)
1539 return;
1540
1541 switch (event) {
1542 case FIB_EVENT_NH_ADD:
Ido Schimmel9665b742017-02-08 11:16:42 +01001543 mlxsw_sp_nexthop_rif_init(nh, r);
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,
1556 struct mlxsw_sp_rif *r)
1557{
1558 struct mlxsw_sp_nexthop *nh, *tmp;
1559
1560 list_for_each_entry_safe(nh, tmp, &r->nexthop_list, rif_list_node) {
1561 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{
Ido Schimmelb8399a12017-02-08 11:16:33 +01001763 struct mlxsw_sp_rif *r = 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;
1769 u16 rif = 0;
1770
1771 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
1772 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1773 rif = r->rif;
1774 } 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);
Ido Schimmel70ad3502017-02-08 11:16:38 +01001783 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id, rif);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001784 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1785}
1786
1787static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1788 struct mlxsw_sp_fib_entry *fib_entry,
1789 enum mlxsw_reg_ralue_op op)
1790{
Ido Schimmel76610eb2017-03-10 08:53:41 +01001791 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001792 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel9aecce12017-02-09 10:28:42 +01001793 u32 *p_dip = (u32 *) fib_entry->fib_node->key.addr;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001794
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001795 mlxsw_reg_ralue_pack4(ralue_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001796 (enum mlxsw_reg_ralxx_protocol) fib->proto, op,
1797 fib->vr->id, fib_entry->fib_node->key.prefix_len,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001798 *p_dip);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001799 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1800 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1801}
1802
1803static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1804 struct mlxsw_sp_fib_entry *fib_entry,
1805 enum mlxsw_reg_ralue_op op)
1806{
1807 switch (fib_entry->type) {
1808 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001809 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001810 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1811 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1812 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1813 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1814 }
1815 return -EINVAL;
1816}
1817
1818static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1819 struct mlxsw_sp_fib_entry *fib_entry,
1820 enum mlxsw_reg_ralue_op op)
1821{
Ido Schimmel013b20f2017-02-08 11:16:36 +01001822 int err = -EINVAL;
1823
Ido Schimmel76610eb2017-03-10 08:53:41 +01001824 switch (fib_entry->fib_node->fib->proto) {
Jiri Pirko61c503f2016-07-04 08:23:11 +02001825 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel013b20f2017-02-08 11:16:36 +01001826 err = mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1827 break;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001828 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmel013b20f2017-02-08 11:16:36 +01001829 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001830 }
Ido Schimmel013b20f2017-02-08 11:16:36 +01001831 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
1832 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001833}
1834
1835static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1836 struct mlxsw_sp_fib_entry *fib_entry)
1837{
Jiri Pirko7146da32016-09-01 10:37:41 +02001838 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1839 MLXSW_REG_RALUE_OP_WRITE_WRITE);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001840}
1841
1842static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1843 struct mlxsw_sp_fib_entry *fib_entry)
1844{
1845 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1846 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1847}
1848
Jiri Pirko61c503f2016-07-04 08:23:11 +02001849static int
Ido Schimmel013b20f2017-02-08 11:16:36 +01001850mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
1851 const struct fib_entry_notifier_info *fen_info,
1852 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001853{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001854 struct fib_info *fi = fen_info->fi;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001855
Ido Schimmel97989ee2017-03-10 08:53:38 +01001856 switch (fen_info->type) {
1857 case RTN_BROADCAST: /* fall through */
1858 case RTN_LOCAL:
Jiri Pirko61c503f2016-07-04 08:23:11 +02001859 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1860 return 0;
Ido Schimmel97989ee2017-03-10 08:53:38 +01001861 case RTN_UNREACHABLE: /* fall through */
1862 case RTN_BLACKHOLE: /* fall through */
1863 case RTN_PROHIBIT:
1864 /* Packets hitting these routes need to be trapped, but
1865 * can do so with a lower priority than packets directed
1866 * at the host, so use action type local instead of trap.
1867 */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001868 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Ido Schimmel97989ee2017-03-10 08:53:38 +01001869 return 0;
1870 case RTN_UNICAST:
1871 if (fi->fib_nh->nh_scope != RT_SCOPE_LINK)
1872 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
1873 else
1874 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
1875 return 0;
1876 default:
1877 return -EINVAL;
1878 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001879}
1880
Jiri Pirko5b004412016-09-01 10:37:40 +02001881static struct mlxsw_sp_fib_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01001882mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
1883 struct mlxsw_sp_fib_node *fib_node,
1884 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02001885{
1886 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01001887 int err;
1888
1889 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
1890 if (!fib_entry) {
1891 err = -ENOMEM;
1892 goto err_fib_entry_alloc;
1893 }
1894
1895 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
1896 if (err)
1897 goto err_fib4_entry_type_set;
1898
1899 err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fen_info->fi);
1900 if (err)
1901 goto err_nexthop_group_get;
1902
1903 fib_entry->params.prio = fen_info->fi->fib_priority;
1904 fib_entry->params.tb_id = fen_info->tb_id;
1905 fib_entry->params.type = fen_info->type;
1906 fib_entry->params.tos = fen_info->tos;
1907
1908 fib_entry->fib_node = fib_node;
1909
1910 return fib_entry;
1911
1912err_nexthop_group_get:
1913err_fib4_entry_type_set:
1914 kfree(fib_entry);
1915err_fib_entry_alloc:
1916 return ERR_PTR(err);
1917}
1918
1919static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1920 struct mlxsw_sp_fib_entry *fib_entry)
1921{
1922 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
1923 kfree(fib_entry);
1924}
1925
1926static struct mlxsw_sp_fib_node *
1927mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
1928 const struct fib_entry_notifier_info *fen_info);
1929
1930static struct mlxsw_sp_fib_entry *
1931mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
1932 const struct fib_entry_notifier_info *fen_info)
1933{
1934 struct mlxsw_sp_fib_entry *fib_entry;
1935 struct mlxsw_sp_fib_node *fib_node;
1936
1937 fib_node = mlxsw_sp_fib4_node_get(mlxsw_sp, fen_info);
1938 if (IS_ERR(fib_node))
1939 return NULL;
1940
1941 list_for_each_entry(fib_entry, &fib_node->entry_list, list) {
1942 if (fib_entry->params.tb_id == fen_info->tb_id &&
1943 fib_entry->params.tos == fen_info->tos &&
1944 fib_entry->params.type == fen_info->type &&
1945 fib_entry->nh_group->key.fi == fen_info->fi) {
1946 return fib_entry;
1947 }
1948 }
1949
1950 return NULL;
1951}
1952
1953static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
1954 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
1955 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
1956 .key_len = sizeof(struct mlxsw_sp_fib_key),
1957 .automatic_shrinking = true,
1958};
1959
1960static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
1961 struct mlxsw_sp_fib_node *fib_node)
1962{
1963 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
1964 mlxsw_sp_fib_ht_params);
1965}
1966
1967static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
1968 struct mlxsw_sp_fib_node *fib_node)
1969{
1970 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
1971 mlxsw_sp_fib_ht_params);
1972}
1973
1974static struct mlxsw_sp_fib_node *
1975mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
1976 size_t addr_len, unsigned char prefix_len)
1977{
1978 struct mlxsw_sp_fib_key key;
1979
1980 memset(&key, 0, sizeof(key));
1981 memcpy(key.addr, addr, addr_len);
1982 key.prefix_len = prefix_len;
1983 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
1984}
1985
1986static struct mlxsw_sp_fib_node *
Ido Schimmel76610eb2017-03-10 08:53:41 +01001987mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
Ido Schimmel9aecce12017-02-09 10:28:42 +01001988 size_t addr_len, unsigned char prefix_len)
1989{
1990 struct mlxsw_sp_fib_node *fib_node;
1991
1992 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
1993 if (!fib_node)
1994 return NULL;
1995
1996 INIT_LIST_HEAD(&fib_node->entry_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +01001997 list_add(&fib_node->list, &fib->node_list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01001998 memcpy(fib_node->key.addr, addr, addr_len);
1999 fib_node->key.prefix_len = prefix_len;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002000
2001 return fib_node;
2002}
2003
2004static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
2005{
Ido Schimmel9aecce12017-02-09 10:28:42 +01002006 list_del(&fib_node->list);
2007 WARN_ON(!list_empty(&fib_node->entry_list));
2008 kfree(fib_node);
2009}
2010
2011static bool
2012mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
2013 const struct mlxsw_sp_fib_entry *fib_entry)
2014{
2015 return list_first_entry(&fib_node->entry_list,
2016 struct mlxsw_sp_fib_entry, list) == fib_entry;
2017}
2018
2019static void mlxsw_sp_fib_node_prefix_inc(struct mlxsw_sp_fib_node *fib_node)
2020{
2021 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002022 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002023
2024 if (fib->prefix_ref_count[prefix_len]++ == 0)
2025 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
2026}
2027
2028static void mlxsw_sp_fib_node_prefix_dec(struct mlxsw_sp_fib_node *fib_node)
2029{
2030 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002031 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002032
2033 if (--fib->prefix_ref_count[prefix_len] == 0)
2034 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
2035}
2036
Ido Schimmel76610eb2017-03-10 08:53:41 +01002037static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
2038 struct mlxsw_sp_fib_node *fib_node,
2039 struct mlxsw_sp_fib *fib)
2040{
2041 struct mlxsw_sp_prefix_usage req_prefix_usage;
2042 struct mlxsw_sp_lpm_tree *lpm_tree;
2043 int err;
2044
2045 err = mlxsw_sp_fib_node_insert(fib, fib_node);
2046 if (err)
2047 return err;
2048 fib_node->fib = fib;
2049
2050 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &fib->prefix_usage);
2051 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
2052
2053 if (!mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2054 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib,
2055 &req_prefix_usage);
2056 if (err)
2057 goto err_tree_check;
2058 } else {
2059 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
2060 fib->proto);
2061 if (IS_ERR(lpm_tree))
2062 return PTR_ERR(lpm_tree);
2063 fib->lpm_tree = lpm_tree;
2064 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
2065 if (err)
2066 goto err_tree_bind;
2067 }
2068
2069 mlxsw_sp_fib_node_prefix_inc(fib_node);
2070
2071 return 0;
2072
2073err_tree_bind:
2074 fib->lpm_tree = NULL;
2075 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2076err_tree_check:
2077 fib_node->fib = NULL;
2078 mlxsw_sp_fib_node_remove(fib, fib_node);
2079 return err;
2080}
2081
2082static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
2083 struct mlxsw_sp_fib_node *fib_node)
2084{
2085 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
2086 struct mlxsw_sp_fib *fib = fib_node->fib;
2087
2088 mlxsw_sp_fib_node_prefix_dec(fib_node);
2089
2090 if (mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2091 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
2092 fib->lpm_tree = NULL;
2093 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2094 } else {
2095 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib, &fib->prefix_usage);
2096 }
2097
2098 fib_node->fib = NULL;
2099 mlxsw_sp_fib_node_remove(fib, fib_node);
2100}
2101
Ido Schimmel9aecce12017-02-09 10:28:42 +01002102static struct mlxsw_sp_fib_node *
2103mlxsw_sp_fib4_node_get(struct mlxsw_sp *mlxsw_sp,
2104 const struct fib_entry_notifier_info *fen_info)
2105{
2106 struct mlxsw_sp_fib_node *fib_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002107 struct mlxsw_sp_fib *fib;
Jiri Pirko5b004412016-09-01 10:37:40 +02002108 struct mlxsw_sp_vr *vr;
2109 int err;
2110
Ido Schimmel76610eb2017-03-10 08:53:41 +01002111 vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->tb_id);
Jiri Pirko5b004412016-09-01 10:37:40 +02002112 if (IS_ERR(vr))
2113 return ERR_CAST(vr);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002114 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirko5b004412016-09-01 10:37:40 +02002115
Ido Schimmel76610eb2017-03-10 08:53:41 +01002116 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002117 sizeof(fen_info->dst),
2118 fen_info->dst_len);
2119 if (fib_node)
2120 return fib_node;
2121
Ido Schimmel76610eb2017-03-10 08:53:41 +01002122 fib_node = mlxsw_sp_fib_node_create(fib, &fen_info->dst,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002123 sizeof(fen_info->dst),
2124 fen_info->dst_len);
2125 if (!fib_node) {
Jiri Pirko5b004412016-09-01 10:37:40 +02002126 err = -ENOMEM;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002127 goto err_fib_node_create;
Jiri Pirko5b004412016-09-01 10:37:40 +02002128 }
Jiri Pirko5b004412016-09-01 10:37:40 +02002129
Ido Schimmel76610eb2017-03-10 08:53:41 +01002130 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
2131 if (err)
2132 goto err_fib_node_init;
2133
Ido Schimmel9aecce12017-02-09 10:28:42 +01002134 return fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002135
Ido Schimmel76610eb2017-03-10 08:53:41 +01002136err_fib_node_init:
2137 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002138err_fib_node_create:
Ido Schimmel76610eb2017-03-10 08:53:41 +01002139 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002140 return ERR_PTR(err);
2141}
2142
Ido Schimmel9aecce12017-02-09 10:28:42 +01002143static void mlxsw_sp_fib4_node_put(struct mlxsw_sp *mlxsw_sp,
2144 struct mlxsw_sp_fib_node *fib_node)
Jiri Pirko5b004412016-09-01 10:37:40 +02002145{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002146 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
Jiri Pirko5b004412016-09-01 10:37:40 +02002147
Ido Schimmel9aecce12017-02-09 10:28:42 +01002148 if (!list_empty(&fib_node->entry_list))
2149 return;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002150 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002151 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002152 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002153}
2154
Ido Schimmel9aecce12017-02-09 10:28:42 +01002155static struct mlxsw_sp_fib_entry *
2156mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
2157 const struct mlxsw_sp_fib_entry_params *params)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002158{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002159 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002160
2161 list_for_each_entry(fib_entry, &fib_node->entry_list, list) {
2162 if (fib_entry->params.tb_id > params->tb_id)
2163 continue;
2164 if (fib_entry->params.tb_id != params->tb_id)
2165 break;
2166 if (fib_entry->params.tos > params->tos)
2167 continue;
2168 if (fib_entry->params.prio >= params->prio ||
2169 fib_entry->params.tos < params->tos)
2170 return fib_entry;
2171 }
2172
2173 return NULL;
2174}
2175
Ido Schimmel4283bce2017-02-09 10:28:43 +01002176static int mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib_entry *fib_entry,
2177 struct mlxsw_sp_fib_entry *new_entry)
2178{
2179 struct mlxsw_sp_fib_node *fib_node;
2180
2181 if (WARN_ON(!fib_entry))
2182 return -EINVAL;
2183
2184 fib_node = fib_entry->fib_node;
2185 list_for_each_entry_from(fib_entry, &fib_node->entry_list, list) {
2186 if (fib_entry->params.tb_id != new_entry->params.tb_id ||
2187 fib_entry->params.tos != new_entry->params.tos ||
2188 fib_entry->params.prio != new_entry->params.prio)
2189 break;
2190 }
2191
2192 list_add_tail(&new_entry->list, &fib_entry->list);
2193 return 0;
2194}
2195
Ido Schimmel9aecce12017-02-09 10:28:42 +01002196static int
2197mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib_node *fib_node,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002198 struct mlxsw_sp_fib_entry *new_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002199 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002200{
2201 struct mlxsw_sp_fib_entry *fib_entry;
2202
2203 fib_entry = mlxsw_sp_fib4_node_entry_find(fib_node, &new_entry->params);
2204
Ido Schimmel4283bce2017-02-09 10:28:43 +01002205 if (append)
2206 return mlxsw_sp_fib4_node_list_append(fib_entry, new_entry);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002207 if (replace && WARN_ON(!fib_entry))
2208 return -EINVAL;
Ido Schimmel4283bce2017-02-09 10:28:43 +01002209
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002210 /* Insert new entry before replaced one, so that we can later
2211 * remove the second.
2212 */
Ido Schimmel9aecce12017-02-09 10:28:42 +01002213 if (fib_entry) {
2214 list_add_tail(&new_entry->list, &fib_entry->list);
2215 } else {
2216 struct mlxsw_sp_fib_entry *last;
2217
2218 list_for_each_entry(last, &fib_node->entry_list, list) {
2219 if (new_entry->params.tb_id > last->params.tb_id)
2220 break;
2221 fib_entry = last;
2222 }
2223
2224 if (fib_entry)
2225 list_add(&new_entry->list, &fib_entry->list);
2226 else
2227 list_add(&new_entry->list, &fib_node->entry_list);
2228 }
2229
2230 return 0;
2231}
2232
2233static void
2234mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib_entry *fib_entry)
2235{
2236 list_del(&fib_entry->list);
2237}
2238
2239static int
2240mlxsw_sp_fib4_node_entry_add(struct mlxsw_sp *mlxsw_sp,
2241 const struct mlxsw_sp_fib_node *fib_node,
2242 struct mlxsw_sp_fib_entry *fib_entry)
2243{
2244 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2245 return 0;
2246
2247 /* To prevent packet loss, overwrite the previously offloaded
2248 * entry.
2249 */
2250 if (!list_is_singular(&fib_node->entry_list)) {
2251 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2252 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2253
2254 mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
2255 }
2256
2257 return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
2258}
2259
2260static void
2261mlxsw_sp_fib4_node_entry_del(struct mlxsw_sp *mlxsw_sp,
2262 const struct mlxsw_sp_fib_node *fib_node,
2263 struct mlxsw_sp_fib_entry *fib_entry)
2264{
2265 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2266 return;
2267
2268 /* Promote the next entry by overwriting the deleted entry */
2269 if (!list_is_singular(&fib_node->entry_list)) {
2270 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2271 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2272
2273 mlxsw_sp_fib_entry_update(mlxsw_sp, n);
2274 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
2275 return;
2276 }
2277
2278 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
2279}
2280
2281static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002282 struct mlxsw_sp_fib_entry *fib_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002283 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002284{
2285 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2286 int err;
2287
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002288 err = mlxsw_sp_fib4_node_list_insert(fib_node, fib_entry, replace,
2289 append);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002290 if (err)
2291 return err;
2292
2293 err = mlxsw_sp_fib4_node_entry_add(mlxsw_sp, fib_node, fib_entry);
2294 if (err)
2295 goto err_fib4_node_entry_add;
2296
Ido Schimmel9aecce12017-02-09 10:28:42 +01002297 return 0;
2298
2299err_fib4_node_entry_add:
2300 mlxsw_sp_fib4_node_list_remove(fib_entry);
2301 return err;
2302}
2303
2304static void
2305mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
2306 struct mlxsw_sp_fib_entry *fib_entry)
2307{
2308 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2309
Ido Schimmel9aecce12017-02-09 10:28:42 +01002310 mlxsw_sp_fib4_node_entry_del(mlxsw_sp, fib_node, fib_entry);
2311 mlxsw_sp_fib4_node_list_remove(fib_entry);
2312}
2313
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002314static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
2315 struct mlxsw_sp_fib_entry *fib_entry,
2316 bool replace)
2317{
2318 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2319 struct mlxsw_sp_fib_entry *replaced;
2320
2321 if (!replace)
2322 return;
2323
2324 /* We inserted the new entry before replaced one */
2325 replaced = list_next_entry(fib_entry, list);
2326
2327 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, replaced);
2328 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, replaced);
2329 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
2330}
2331
Ido Schimmel9aecce12017-02-09 10:28:42 +01002332static int
2333mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002334 const struct fib_entry_notifier_info *fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002335 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002336{
2337 struct mlxsw_sp_fib_entry *fib_entry;
2338 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002339 int err;
2340
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002341 if (mlxsw_sp->router.aborted)
2342 return 0;
2343
Ido Schimmel9aecce12017-02-09 10:28:42 +01002344 fib_node = mlxsw_sp_fib4_node_get(mlxsw_sp, fen_info);
2345 if (IS_ERR(fib_node)) {
2346 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
2347 return PTR_ERR(fib_node);
2348 }
2349
2350 fib_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002351 if (IS_ERR(fib_entry)) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002352 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
2353 err = PTR_ERR(fib_entry);
2354 goto err_fib4_entry_create;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002355 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02002356
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002357 err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib_entry, replace,
2358 append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002359 if (err) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002360 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
2361 goto err_fib4_node_entry_link;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002362 }
Ido Schimmel9aecce12017-02-09 10:28:42 +01002363
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002364 mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib_entry, replace);
2365
Jiri Pirko61c503f2016-07-04 08:23:11 +02002366 return 0;
2367
Ido Schimmel9aecce12017-02-09 10:28:42 +01002368err_fib4_node_entry_link:
2369 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2370err_fib4_entry_create:
2371 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002372 return err;
2373}
2374
Jiri Pirko37956d72016-10-20 16:05:43 +02002375static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
2376 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002377{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002378 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002379 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002380
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002381 if (mlxsw_sp->router.aborted)
Jiri Pirko37956d72016-10-20 16:05:43 +02002382 return;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002383
Ido Schimmel9aecce12017-02-09 10:28:42 +01002384 fib_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
2385 if (WARN_ON(!fib_entry))
Jiri Pirko37956d72016-10-20 16:05:43 +02002386 return;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002387 fib_node = fib_entry->fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002388
Ido Schimmel9aecce12017-02-09 10:28:42 +01002389 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib_entry);
2390 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2391 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002392}
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002393
2394static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
2395{
2396 char ralta_pl[MLXSW_REG_RALTA_LEN];
2397 char ralst_pl[MLXSW_REG_RALST_LEN];
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002398 int i, err;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002399
2400 mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4,
2401 MLXSW_SP_LPM_TREE_MIN);
2402 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
2403 if (err)
2404 return err;
2405
2406 mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN);
2407 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
2408 if (err)
2409 return err;
2410
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002411 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
2412 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[i];
2413 char raltb_pl[MLXSW_REG_RALTB_LEN];
2414 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002415
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002416 if (!mlxsw_sp_vr_is_used(vr))
2417 continue;
2418
2419 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
2420 MLXSW_REG_RALXX_PROTOCOL_IPV4,
2421 MLXSW_SP_LPM_TREE_MIN);
2422 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
2423 raltb_pl);
2424 if (err)
2425 return err;
2426
2427 mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4,
2428 MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0,
2429 0);
2430 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
2431 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
2432 ralue_pl);
2433 if (err)
2434 return err;
2435 }
2436
2437 return 0;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002438}
2439
Ido Schimmel9aecce12017-02-09 10:28:42 +01002440static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
2441 struct mlxsw_sp_fib_node *fib_node)
2442{
2443 struct mlxsw_sp_fib_entry *fib_entry, *tmp;
2444
2445 list_for_each_entry_safe(fib_entry, tmp, &fib_node->entry_list, list) {
2446 bool do_break = &tmp->list == &fib_node->entry_list;
2447
2448 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib_entry);
2449 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib_entry);
2450 mlxsw_sp_fib4_node_put(mlxsw_sp, fib_node);
2451 /* Break when entry list is empty and node was freed.
2452 * Otherwise, we'll access freed memory in the next
2453 * iteration.
2454 */
2455 if (do_break)
2456 break;
2457 }
2458}
2459
2460static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
2461 struct mlxsw_sp_fib_node *fib_node)
2462{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002463 switch (fib_node->fib->proto) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002464 case MLXSW_SP_L3_PROTO_IPV4:
2465 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
2466 break;
2467 case MLXSW_SP_L3_PROTO_IPV6:
2468 WARN_ON_ONCE(1);
2469 break;
2470 }
2471}
2472
Ido Schimmel76610eb2017-03-10 08:53:41 +01002473static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
2474 struct mlxsw_sp_vr *vr,
2475 enum mlxsw_sp_l3proto proto)
2476{
2477 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
2478 struct mlxsw_sp_fib_node *fib_node, *tmp;
2479
2480 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
2481 bool do_break = &tmp->list == &fib->node_list;
2482
2483 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
2484 if (do_break)
2485 break;
2486 }
2487}
2488
Ido Schimmelac571de2016-11-14 11:26:32 +01002489static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002490{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002491 int i;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002492
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002493 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel76610eb2017-03-10 08:53:41 +01002494 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[i];
Ido Schimmelac571de2016-11-14 11:26:32 +01002495
Ido Schimmel76610eb2017-03-10 08:53:41 +01002496 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002497 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002498 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002499 }
Ido Schimmelac571de2016-11-14 11:26:32 +01002500}
2501
2502static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp)
2503{
2504 int err;
2505
Ido Schimmeld331d302016-11-16 09:51:58 +01002506 if (mlxsw_sp->router.aborted)
2507 return;
2508 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 +01002509 mlxsw_sp_router_fib_flush(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002510 mlxsw_sp->router.aborted = true;
2511 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
2512 if (err)
2513 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
2514}
2515
Ido Schimmel30572242016-12-03 16:45:01 +01002516struct mlxsw_sp_fib_event_work {
Ido Schimmela0e47612017-02-06 16:20:10 +01002517 struct work_struct work;
Ido Schimmelad178c82017-02-08 11:16:40 +01002518 union {
2519 struct fib_entry_notifier_info fen_info;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002520 struct fib_rule_notifier_info fr_info;
Ido Schimmelad178c82017-02-08 11:16:40 +01002521 struct fib_nh_notifier_info fnh_info;
2522 };
Ido Schimmel30572242016-12-03 16:45:01 +01002523 struct mlxsw_sp *mlxsw_sp;
2524 unsigned long event;
2525};
2526
2527static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002528{
Ido Schimmel30572242016-12-03 16:45:01 +01002529 struct mlxsw_sp_fib_event_work *fib_work =
Ido Schimmela0e47612017-02-06 16:20:10 +01002530 container_of(work, struct mlxsw_sp_fib_event_work, work);
Ido Schimmel30572242016-12-03 16:45:01 +01002531 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002532 struct fib_rule *rule;
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002533 bool replace, append;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002534 int err;
2535
Ido Schimmel30572242016-12-03 16:45:01 +01002536 /* Protect internal structures from changes */
2537 rtnl_lock();
2538 switch (fib_work->event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002539 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01002540 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002541 case FIB_EVENT_ENTRY_ADD:
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002542 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel4283bce2017-02-09 10:28:43 +01002543 append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
2544 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002545 replace, append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002546 if (err)
2547 mlxsw_sp_router_fib4_abort(mlxsw_sp);
Ido Schimmel30572242016-12-03 16:45:01 +01002548 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002549 break;
2550 case FIB_EVENT_ENTRY_DEL:
Ido Schimmel30572242016-12-03 16:45:01 +01002551 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
2552 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002553 break;
2554 case FIB_EVENT_RULE_ADD: /* fall through */
2555 case FIB_EVENT_RULE_DEL:
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002556 rule = fib_work->fr_info.rule;
Ido Schimmelc7f6e662017-03-16 09:08:20 +01002557 if (!fib4_rule_default(rule) && !rule->l3mdev)
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002558 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2559 fib_rule_put(rule);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002560 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002561 case FIB_EVENT_NH_ADD: /* fall through */
2562 case FIB_EVENT_NH_DEL:
2563 mlxsw_sp_nexthop_event(mlxsw_sp, fib_work->event,
2564 fib_work->fnh_info.fib_nh);
2565 fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
2566 break;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002567 }
Ido Schimmel30572242016-12-03 16:45:01 +01002568 rtnl_unlock();
2569 kfree(fib_work);
2570}
2571
2572/* Called with rcu_read_lock() */
2573static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
2574 unsigned long event, void *ptr)
2575{
2576 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
2577 struct mlxsw_sp_fib_event_work *fib_work;
2578 struct fib_notifier_info *info = ptr;
2579
2580 if (!net_eq(info->net, &init_net))
2581 return NOTIFY_DONE;
2582
2583 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2584 if (WARN_ON(!fib_work))
2585 return NOTIFY_BAD;
2586
Ido Schimmela0e47612017-02-06 16:20:10 +01002587 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work);
Ido Schimmel30572242016-12-03 16:45:01 +01002588 fib_work->mlxsw_sp = mlxsw_sp;
2589 fib_work->event = event;
2590
2591 switch (event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002592 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01002593 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Ido Schimmel30572242016-12-03 16:45:01 +01002594 case FIB_EVENT_ENTRY_ADD: /* fall through */
2595 case FIB_EVENT_ENTRY_DEL:
2596 memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2597 /* Take referece on fib_info to prevent it from being
2598 * freed while work is queued. Release it afterwards.
2599 */
2600 fib_info_hold(fib_work->fen_info.fi);
2601 break;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01002602 case FIB_EVENT_RULE_ADD: /* fall through */
2603 case FIB_EVENT_RULE_DEL:
2604 memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
2605 fib_rule_get(fib_work->fr_info.rule);
2606 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002607 case FIB_EVENT_NH_ADD: /* fall through */
2608 case FIB_EVENT_NH_DEL:
2609 memcpy(&fib_work->fnh_info, ptr, sizeof(fib_work->fnh_info));
2610 fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
2611 break;
Ido Schimmel30572242016-12-03 16:45:01 +01002612 }
2613
Ido Schimmela0e47612017-02-06 16:20:10 +01002614 mlxsw_core_schedule_work(&fib_work->work);
Ido Schimmel30572242016-12-03 16:45:01 +01002615
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002616 return NOTIFY_DONE;
2617}
2618
Ido Schimmel4724ba562017-03-10 08:53:39 +01002619static struct mlxsw_sp_rif *
2620mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
2621 const struct net_device *dev)
2622{
2623 int i;
2624
2625 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2626 if (mlxsw_sp->rifs[i] && mlxsw_sp->rifs[i]->dev == dev)
2627 return mlxsw_sp->rifs[i];
2628
2629 return NULL;
2630}
2631
2632static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
2633{
2634 char ritr_pl[MLXSW_REG_RITR_LEN];
2635 int err;
2636
2637 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
2638 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2639 if (WARN_ON_ONCE(err))
2640 return err;
2641
2642 mlxsw_reg_ritr_enable_set(ritr_pl, false);
2643 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2644}
2645
2646static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
2647 struct mlxsw_sp_rif *r)
2648{
2649 mlxsw_sp_router_rif_disable(mlxsw_sp, r->rif);
2650 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, r);
2651 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, r);
2652}
2653
2654static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *r,
2655 const struct in_device *in_dev,
2656 unsigned long event)
2657{
2658 switch (event) {
2659 case NETDEV_UP:
2660 if (!r)
2661 return true;
2662 return false;
2663 case NETDEV_DOWN:
Ido Schimmel9db032b2017-03-16 09:08:17 +01002664 if (r && !in_dev->ifa_list && !netif_is_l3_slave(r->dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01002665 return true;
2666 /* It is possible we already removed the RIF ourselves
2667 * if it was assigned to a netdev that is now a bridge
2668 * or LAG slave.
2669 */
2670 return false;
2671 }
2672
2673 return false;
2674}
2675
2676#define MLXSW_SP_INVALID_RIF 0xffff
2677static int mlxsw_sp_avail_rif_get(struct mlxsw_sp *mlxsw_sp)
2678{
2679 int i;
2680
2681 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
2682 if (!mlxsw_sp->rifs[i])
2683 return i;
2684
2685 return MLXSW_SP_INVALID_RIF;
2686}
2687
2688static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port *mlxsw_sp_vport,
2689 bool *p_lagged, u16 *p_system_port)
2690{
2691 u8 local_port = mlxsw_sp_vport->local_port;
2692
2693 *p_lagged = mlxsw_sp_vport->lagged;
2694 *p_system_port = *p_lagged ? mlxsw_sp_vport->lag_id : local_port;
2695}
2696
2697static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port *mlxsw_sp_vport,
Ido Schimmel69132292017-03-10 08:53:42 +01002698 u16 vr_id, struct net_device *l3_dev,
2699 u16 rif, bool create)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002700{
2701 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
2702 bool lagged = mlxsw_sp_vport->lagged;
2703 char ritr_pl[MLXSW_REG_RITR_LEN];
2704 u16 system_port;
2705
Ido Schimmel69132292017-03-10 08:53:42 +01002706 mlxsw_reg_ritr_pack(ritr_pl, create, MLXSW_REG_RITR_SP_IF, rif, vr_id,
Ido Schimmel4724ba562017-03-10 08:53:39 +01002707 l3_dev->mtu, l3_dev->dev_addr);
2708
2709 mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport, &lagged, &system_port);
2710 mlxsw_reg_ritr_sp_if_pack(ritr_pl, lagged, system_port,
2711 mlxsw_sp_vport_vid_get(mlxsw_sp_vport));
2712
2713 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2714}
2715
2716static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
2717
2718static u16 mlxsw_sp_rif_sp_to_fid(u16 rif)
2719{
2720 return MLXSW_SP_RFID_BASE + rif;
2721}
2722
2723static struct mlxsw_sp_fid *
2724mlxsw_sp_rfid_alloc(u16 fid, struct net_device *l3_dev)
2725{
2726 struct mlxsw_sp_fid *f;
2727
2728 f = kzalloc(sizeof(*f), GFP_KERNEL);
2729 if (!f)
2730 return NULL;
2731
2732 f->leave = mlxsw_sp_vport_rif_sp_leave;
2733 f->ref_count = 0;
2734 f->dev = l3_dev;
2735 f->fid = fid;
2736
2737 return f;
2738}
2739
2740static struct mlxsw_sp_rif *
Ido Schimmel69132292017-03-10 08:53:42 +01002741mlxsw_sp_rif_alloc(u16 rif, u16 vr_id, struct net_device *l3_dev,
2742 struct mlxsw_sp_fid *f)
Ido Schimmel4724ba562017-03-10 08:53:39 +01002743{
2744 struct mlxsw_sp_rif *r;
2745
2746 r = kzalloc(sizeof(*r), GFP_KERNEL);
2747 if (!r)
2748 return NULL;
2749
2750 INIT_LIST_HEAD(&r->nexthop_list);
2751 INIT_LIST_HEAD(&r->neigh_list);
2752 ether_addr_copy(r->addr, l3_dev->dev_addr);
2753 r->mtu = l3_dev->mtu;
Ido Schimmel69132292017-03-10 08:53:42 +01002754 r->vr_id = vr_id;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002755 r->dev = l3_dev;
2756 r->rif = rif;
2757 r->f = f;
2758
2759 return r;
2760}
2761
2762static struct mlxsw_sp_rif *
2763mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
2764 struct net_device *l3_dev)
2765{
2766 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Ido Schimmel57837882017-03-16 09:08:16 +01002767 u32 tb_id = l3mdev_fib_table(l3_dev);
Ido Schimmel69132292017-03-10 08:53:42 +01002768 struct mlxsw_sp_vr *vr;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002769 struct mlxsw_sp_fid *f;
2770 struct mlxsw_sp_rif *r;
2771 u16 fid, rif;
2772 int err;
2773
2774 rif = mlxsw_sp_avail_rif_get(mlxsw_sp);
2775 if (rif == MLXSW_SP_INVALID_RIF)
2776 return ERR_PTR(-ERANGE);
2777
Ido Schimmel57837882017-03-16 09:08:16 +01002778 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
Ido Schimmel69132292017-03-10 08:53:42 +01002779 if (IS_ERR(vr))
2780 return ERR_CAST(vr);
2781
2782 err = mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif,
2783 true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002784 if (err)
Ido Schimmel69132292017-03-10 08:53:42 +01002785 goto err_vport_rif_sp_op;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002786
2787 fid = mlxsw_sp_rif_sp_to_fid(rif);
2788 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, true);
2789 if (err)
2790 goto err_rif_fdb_op;
2791
2792 f = mlxsw_sp_rfid_alloc(fid, l3_dev);
2793 if (!f) {
2794 err = -ENOMEM;
2795 goto err_rfid_alloc;
2796 }
2797
Ido Schimmel69132292017-03-10 08:53:42 +01002798 r = mlxsw_sp_rif_alloc(rif, vr->id, l3_dev, f);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002799 if (!r) {
2800 err = -ENOMEM;
2801 goto err_rif_alloc;
2802 }
2803
2804 f->r = r;
2805 mlxsw_sp->rifs[rif] = r;
Ido Schimmel69132292017-03-10 08:53:42 +01002806 vr->rif_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002807
2808 return r;
2809
2810err_rif_alloc:
2811 kfree(f);
2812err_rfid_alloc:
2813 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
2814err_rif_fdb_op:
Ido Schimmel69132292017-03-10 08:53:42 +01002815 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif, false);
2816err_vport_rif_sp_op:
2817 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002818 return ERR_PTR(err);
2819}
2820
2821static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
2822 struct mlxsw_sp_rif *r)
2823{
2824 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
Ido Schimmel69132292017-03-10 08:53:42 +01002825 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[r->vr_id];
Ido Schimmel4724ba562017-03-10 08:53:39 +01002826 struct net_device *l3_dev = r->dev;
2827 struct mlxsw_sp_fid *f = r->f;
2828 u16 fid = f->fid;
2829 u16 rif = r->rif;
2830
2831 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, r);
2832
Ido Schimmel69132292017-03-10 08:53:42 +01002833 vr->rif_count--;
Ido Schimmel4724ba562017-03-10 08:53:39 +01002834 mlxsw_sp->rifs[rif] = NULL;
2835 f->r = NULL;
2836
2837 kfree(r);
2838
2839 kfree(f);
2840
2841 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
2842
Ido Schimmel69132292017-03-10 08:53:42 +01002843 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, vr->id, l3_dev, rif, false);
2844
2845 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01002846}
2847
2848static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port *mlxsw_sp_vport,
2849 struct net_device *l3_dev)
2850{
2851 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
2852 struct mlxsw_sp_rif *r;
2853
2854 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
2855 if (!r) {
2856 r = mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport, l3_dev);
2857 if (IS_ERR(r))
2858 return PTR_ERR(r);
2859 }
2860
2861 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, r->f);
2862 r->f->ref_count++;
2863
2864 netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", r->f->fid);
2865
2866 return 0;
2867}
2868
2869static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
2870{
2871 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
2872
2873 netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
2874
2875 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
2876 if (--f->ref_count == 0)
2877 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport, f->r);
2878}
2879
2880static int mlxsw_sp_inetaddr_vport_event(struct net_device *l3_dev,
2881 struct net_device *port_dev,
2882 unsigned long event, u16 vid)
2883{
2884 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
2885 struct mlxsw_sp_port *mlxsw_sp_vport;
2886
2887 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
2888 if (WARN_ON(!mlxsw_sp_vport))
2889 return -EINVAL;
2890
2891 switch (event) {
2892 case NETDEV_UP:
2893 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, l3_dev);
2894 case NETDEV_DOWN:
2895 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
2896 break;
2897 }
2898
2899 return 0;
2900}
2901
2902static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
2903 unsigned long event)
2904{
2905 if (netif_is_bridge_port(port_dev) || netif_is_lag_port(port_dev))
2906 return 0;
2907
2908 return mlxsw_sp_inetaddr_vport_event(port_dev, port_dev, event, 1);
2909}
2910
2911static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
2912 struct net_device *lag_dev,
2913 unsigned long event, u16 vid)
2914{
2915 struct net_device *port_dev;
2916 struct list_head *iter;
2917 int err;
2918
2919 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
2920 if (mlxsw_sp_port_dev_check(port_dev)) {
2921 err = mlxsw_sp_inetaddr_vport_event(l3_dev, port_dev,
2922 event, vid);
2923 if (err)
2924 return err;
2925 }
2926 }
2927
2928 return 0;
2929}
2930
2931static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
2932 unsigned long event)
2933{
2934 if (netif_is_bridge_port(lag_dev))
2935 return 0;
2936
2937 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1);
2938}
2939
2940static struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp,
2941 struct net_device *l3_dev)
2942{
2943 u16 fid;
2944
2945 if (is_vlan_dev(l3_dev))
2946 fid = vlan_dev_vlan_id(l3_dev);
2947 else if (mlxsw_sp->master_bridge.dev == l3_dev)
2948 fid = 1;
2949 else
2950 return mlxsw_sp_vfid_find(mlxsw_sp, l3_dev);
2951
2952 return mlxsw_sp_fid_find(mlxsw_sp, fid);
2953}
2954
2955static enum mlxsw_flood_table_type mlxsw_sp_flood_table_type_get(u16 fid)
2956{
2957 return mlxsw_sp_fid_is_vfid(fid) ? MLXSW_REG_SFGC_TABLE_TYPE_FID :
2958 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
2959}
2960
2961static u16 mlxsw_sp_flood_table_index_get(u16 fid)
2962{
2963 return mlxsw_sp_fid_is_vfid(fid) ? mlxsw_sp_fid_to_vfid(fid) : fid;
2964}
2965
2966static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp *mlxsw_sp, u16 fid,
2967 bool set)
2968{
2969 enum mlxsw_flood_table_type table_type;
2970 char *sftr_pl;
2971 u16 index;
2972 int err;
2973
2974 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
2975 if (!sftr_pl)
2976 return -ENOMEM;
2977
2978 table_type = mlxsw_sp_flood_table_type_get(fid);
2979 index = mlxsw_sp_flood_table_index_get(fid);
2980 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BC, index, table_type,
2981 1, MLXSW_PORT_ROUTER_PORT, set);
2982 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
2983
2984 kfree(sftr_pl);
2985 return err;
2986}
2987
2988static enum mlxsw_reg_ritr_if_type mlxsw_sp_rif_type_get(u16 fid)
2989{
2990 if (mlxsw_sp_fid_is_vfid(fid))
2991 return MLXSW_REG_RITR_FID_IF;
2992 else
2993 return MLXSW_REG_RITR_VLAN_IF;
2994}
2995
Ido Schimmel69132292017-03-10 08:53:42 +01002996static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp *mlxsw_sp, u16 vr_id,
Ido Schimmel4724ba562017-03-10 08:53:39 +01002997 struct net_device *l3_dev,
2998 u16 fid, u16 rif,
2999 bool create)
3000{
3001 enum mlxsw_reg_ritr_if_type rif_type;
3002 char ritr_pl[MLXSW_REG_RITR_LEN];
3003
3004 rif_type = mlxsw_sp_rif_type_get(fid);
Ido Schimmel69132292017-03-10 08:53:42 +01003005 mlxsw_reg_ritr_pack(ritr_pl, create, rif_type, rif, vr_id, l3_dev->mtu,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003006 l3_dev->dev_addr);
3007 mlxsw_reg_ritr_fid_set(ritr_pl, rif_type, fid);
3008
3009 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3010}
3011
3012static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp *mlxsw_sp,
3013 struct net_device *l3_dev,
3014 struct mlxsw_sp_fid *f)
3015{
Ido Schimmel57837882017-03-16 09:08:16 +01003016 u32 tb_id = l3mdev_fib_table(l3_dev);
Ido Schimmel69132292017-03-10 08:53:42 +01003017 struct mlxsw_sp_vr *vr;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003018 struct mlxsw_sp_rif *r;
3019 u16 rif;
3020 int err;
3021
3022 rif = mlxsw_sp_avail_rif_get(mlxsw_sp);
3023 if (rif == MLXSW_SP_INVALID_RIF)
3024 return -ERANGE;
3025
Ido Schimmel57837882017-03-16 09:08:16 +01003026 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
Ido Schimmel69132292017-03-10 08:53:42 +01003027 if (IS_ERR(vr))
3028 return PTR_ERR(vr);
3029
Ido Schimmel4724ba562017-03-10 08:53:39 +01003030 err = mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, true);
3031 if (err)
Ido Schimmel69132292017-03-10 08:53:42 +01003032 goto err_port_flood_set;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003033
Ido Schimmel69132292017-03-10 08:53:42 +01003034 err = mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif,
3035 true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003036 if (err)
3037 goto err_rif_bridge_op;
3038
3039 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, true);
3040 if (err)
3041 goto err_rif_fdb_op;
3042
Ido Schimmel69132292017-03-10 08:53:42 +01003043 r = mlxsw_sp_rif_alloc(rif, vr->id, l3_dev, f);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003044 if (!r) {
3045 err = -ENOMEM;
3046 goto err_rif_alloc;
3047 }
3048
3049 f->r = r;
3050 mlxsw_sp->rifs[rif] = r;
Ido Schimmel69132292017-03-10 08:53:42 +01003051 vr->rif_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003052
3053 netdev_dbg(l3_dev, "RIF=%d created\n", rif);
3054
3055 return 0;
3056
3057err_rif_alloc:
3058 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3059err_rif_fdb_op:
Ido Schimmel69132292017-03-10 08:53:42 +01003060 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003061err_rif_bridge_op:
3062 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
Ido Schimmel69132292017-03-10 08:53:42 +01003063err_port_flood_set:
3064 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003065 return err;
3066}
3067
3068void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp *mlxsw_sp,
3069 struct mlxsw_sp_rif *r)
3070{
Ido Schimmel69132292017-03-10 08:53:42 +01003071 struct mlxsw_sp_vr *vr = &mlxsw_sp->router.vrs[r->vr_id];
Ido Schimmel4724ba562017-03-10 08:53:39 +01003072 struct net_device *l3_dev = r->dev;
3073 struct mlxsw_sp_fid *f = r->f;
3074 u16 rif = r->rif;
3075
3076 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, r);
3077
Ido Schimmel69132292017-03-10 08:53:42 +01003078 vr->rif_count--;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003079 mlxsw_sp->rifs[rif] = NULL;
3080 f->r = NULL;
3081
3082 kfree(r);
3083
3084 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3085
Ido Schimmel69132292017-03-10 08:53:42 +01003086 mlxsw_sp_rif_bridge_op(mlxsw_sp, vr->id, l3_dev, f->fid, rif, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003087
3088 mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
3089
Ido Schimmel69132292017-03-10 08:53:42 +01003090 mlxsw_sp_vr_put(vr);
3091
Ido Schimmel4724ba562017-03-10 08:53:39 +01003092 netdev_dbg(l3_dev, "RIF=%d destroyed\n", rif);
3093}
3094
3095static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
3096 struct net_device *br_dev,
3097 unsigned long event)
3098{
3099 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3100 struct mlxsw_sp_fid *f;
3101
3102 /* FID can either be an actual FID if the L3 device is the
3103 * VLAN-aware bridge or a VLAN device on top. Otherwise, the
3104 * L3 device is a VLAN-unaware bridge and we get a vFID.
3105 */
3106 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3107 if (WARN_ON(!f))
3108 return -EINVAL;
3109
3110 switch (event) {
3111 case NETDEV_UP:
3112 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3113 case NETDEV_DOWN:
3114 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
3115 break;
3116 }
3117
3118 return 0;
3119}
3120
3121static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
3122 unsigned long event)
3123{
3124 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
3125 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
3126 u16 vid = vlan_dev_vlan_id(vlan_dev);
3127
3128 if (mlxsw_sp_port_dev_check(real_dev))
3129 return mlxsw_sp_inetaddr_vport_event(vlan_dev, real_dev, event,
3130 vid);
3131 else if (netif_is_lag_master(real_dev))
3132 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
3133 vid);
3134 else if (netif_is_bridge_master(real_dev) &&
3135 mlxsw_sp->master_bridge.dev == real_dev)
3136 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, real_dev,
3137 event);
3138
3139 return 0;
3140}
3141
3142int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
3143 unsigned long event, void *ptr)
3144{
3145 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3146 struct net_device *dev = ifa->ifa_dev->dev;
3147 struct mlxsw_sp *mlxsw_sp;
3148 struct mlxsw_sp_rif *r;
3149 int err = 0;
3150
3151 mlxsw_sp = mlxsw_sp_lower_get(dev);
3152 if (!mlxsw_sp)
3153 goto out;
3154
3155 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3156 if (!mlxsw_sp_rif_should_config(r, ifa->ifa_dev, event))
3157 goto out;
3158
3159 if (mlxsw_sp_port_dev_check(dev))
3160 err = mlxsw_sp_inetaddr_port_event(dev, event);
3161 else if (netif_is_lag_master(dev))
3162 err = mlxsw_sp_inetaddr_lag_event(dev, event);
3163 else if (netif_is_bridge_master(dev))
3164 err = mlxsw_sp_inetaddr_bridge_event(dev, dev, event);
3165 else if (is_vlan_dev(dev))
3166 err = mlxsw_sp_inetaddr_vlan_event(dev, event);
3167
3168out:
3169 return notifier_from_errno(err);
3170}
3171
3172static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif,
3173 const char *mac, int mtu)
3174{
3175 char ritr_pl[MLXSW_REG_RITR_LEN];
3176 int err;
3177
3178 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
3179 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3180 if (err)
3181 return err;
3182
3183 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
3184 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
3185 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
3186 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3187}
3188
3189int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
3190{
3191 struct mlxsw_sp *mlxsw_sp;
3192 struct mlxsw_sp_rif *r;
3193 int err;
3194
3195 mlxsw_sp = mlxsw_sp_lower_get(dev);
3196 if (!mlxsw_sp)
3197 return 0;
3198
3199 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3200 if (!r)
3201 return 0;
3202
3203 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, false);
3204 if (err)
3205 return err;
3206
3207 err = mlxsw_sp_rif_edit(mlxsw_sp, r->rif, dev->dev_addr, dev->mtu);
3208 if (err)
3209 goto err_rif_edit;
3210
3211 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, r->f->fid, true);
3212 if (err)
3213 goto err_rif_fdb_op;
3214
3215 ether_addr_copy(r->addr, dev->dev_addr);
3216 r->mtu = dev->mtu;
3217
3218 netdev_dbg(dev, "Updated RIF=%d\n", r->rif);
3219
3220 return 0;
3221
3222err_rif_fdb_op:
3223 mlxsw_sp_rif_edit(mlxsw_sp, r->rif, r->addr, r->mtu);
3224err_rif_edit:
3225 mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, true);
3226 return err;
3227}
3228
Ido Schimmel7179eb52017-03-16 09:08:18 +01003229int mlxsw_sp_vport_vrf_join(struct mlxsw_sp_port *mlxsw_sp_vport)
3230{
3231 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3232 struct net_device *dev = mlxsw_sp_vport->dev;
3233
3234 /* In case vPort already has a RIF, then we need to drop it.
3235 * A new one will be created using the VRF's VR.
3236 */
3237 if (f && f->r)
3238 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3239
3240 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, dev);
3241}
3242
3243void mlxsw_sp_vport_vrf_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
3244{
3245 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3246}
3247
3248int mlxsw_sp_port_vrf_join(struct mlxsw_sp_port *mlxsw_sp_port)
3249{
3250 struct mlxsw_sp_port *mlxsw_sp_vport;
3251
3252 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
3253 if (WARN_ON(!mlxsw_sp_vport))
3254 return -EINVAL;
3255
3256 return mlxsw_sp_vport_vrf_join(mlxsw_sp_vport);
3257}
3258
3259void mlxsw_sp_port_vrf_leave(struct mlxsw_sp_port *mlxsw_sp_port)
3260{
3261 struct mlxsw_sp_port *mlxsw_sp_vport;
3262
3263 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
3264 if (WARN_ON(!mlxsw_sp_vport))
3265 return;
3266
3267 mlxsw_sp_vport_vrf_leave(mlxsw_sp_vport);
3268}
3269
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003270int mlxsw_sp_bridge_vrf_join(struct mlxsw_sp *mlxsw_sp,
3271 struct net_device *l3_dev)
3272{
3273 struct mlxsw_sp_fid *f;
3274
3275 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3276 if (WARN_ON(!f))
3277 return -EINVAL;
3278
3279 if (f->r)
3280 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
3281
3282 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3283}
3284
3285void mlxsw_sp_bridge_vrf_leave(struct mlxsw_sp *mlxsw_sp,
3286 struct net_device *l3_dev)
3287{
3288 struct mlxsw_sp_fid *f;
3289
3290 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3291 if (WARN_ON(!f))
3292 return;
3293 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
3294}
3295
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003296static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
3297{
3298 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
3299
3300 /* Flush pending FIB notifications and then flush the device's
3301 * table before requesting another dump. The FIB notification
3302 * block is unregistered, so no need to take RTNL.
3303 */
3304 mlxsw_core_flush_owq();
3305 mlxsw_sp_router_fib_flush(mlxsw_sp);
3306}
3307
Ido Schimmel4724ba562017-03-10 08:53:39 +01003308static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3309{
3310 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3311 u64 max_rifs;
3312 int err;
3313
3314 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
3315 return -EIO;
3316
3317 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
3318 mlxsw_sp->rifs = kcalloc(max_rifs, sizeof(struct mlxsw_sp_rif *),
3319 GFP_KERNEL);
3320 if (!mlxsw_sp->rifs)
3321 return -ENOMEM;
3322
3323 mlxsw_reg_rgcr_pack(rgcr_pl, true);
3324 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
3325 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3326 if (err)
3327 goto err_rgcr_fail;
3328
3329 return 0;
3330
3331err_rgcr_fail:
3332 kfree(mlxsw_sp->rifs);
3333 return err;
3334}
3335
3336static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3337{
3338 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3339 int i;
3340
3341 mlxsw_reg_rgcr_pack(rgcr_pl, false);
3342 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
3343
3344 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
3345 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
3346
3347 kfree(mlxsw_sp->rifs);
3348}
3349
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003350int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3351{
3352 int err;
3353
3354 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003355 err = __mlxsw_sp_router_init(mlxsw_sp);
3356 if (err)
3357 return err;
3358
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003359 err = rhashtable_init(&mlxsw_sp->router.nexthop_ht,
3360 &mlxsw_sp_nexthop_ht_params);
3361 if (err)
3362 goto err_nexthop_ht_init;
3363
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003364 err = rhashtable_init(&mlxsw_sp->router.nexthop_group_ht,
3365 &mlxsw_sp_nexthop_group_ht_params);
3366 if (err)
3367 goto err_nexthop_group_ht_init;
3368
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003369 mlxsw_sp_lpm_init(mlxsw_sp);
3370 err = mlxsw_sp_vrs_init(mlxsw_sp);
3371 if (err)
3372 goto err_vrs_init;
3373
Ido Schimmel8c9583a2016-10-27 15:12:57 +02003374 err = mlxsw_sp_neigh_init(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003375 if (err)
3376 goto err_neigh_init;
3377
3378 mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003379 err = register_fib_notifier(&mlxsw_sp->fib_nb,
3380 mlxsw_sp_router_fib_dump_flush);
3381 if (err)
3382 goto err_register_fib_notifier;
3383
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003384 return 0;
3385
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003386err_register_fib_notifier:
3387 mlxsw_sp_neigh_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003388err_neigh_init:
3389 mlxsw_sp_vrs_fini(mlxsw_sp);
3390err_vrs_init:
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003391 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
3392err_nexthop_group_ht_init:
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003393 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
3394err_nexthop_ht_init:
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003395 __mlxsw_sp_router_fini(mlxsw_sp);
3396 return err;
3397}
3398
3399void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
3400{
3401 unregister_fib_notifier(&mlxsw_sp->fib_nb);
3402 mlxsw_sp_neigh_fini(mlxsw_sp);
3403 mlxsw_sp_vrs_fini(mlxsw_sp);
Ido Schimmele9ad5e72017-02-08 11:16:29 +01003404 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01003405 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003406 __mlxsw_sp_router_fini(mlxsw_sp);
3407}