blob: 4cdeedf9c61f87ce19e5de77388e05443fb5e0f6 [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>
Ido Schimmel03ea01e2017-05-23 21:56:30 +020045#include <linux/if_bridge.h>
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +020046#include <linux/socket.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020047#include <net/netevent.h>
Jiri Pirko6cf3c972016-07-05 11:27:39 +020048#include <net/neighbour.h>
49#include <net/arp.h>
Jiri Pirkob45f64d2016-09-26 12:52:31 +020050#include <net/ip_fib.h>
Ido Schimmel5d7bfd12017-03-16 09:08:14 +010051#include <net/fib_rules.h>
Ido Schimmel57837882017-03-16 09:08:16 +010052#include <net/l3mdev.h>
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +020053#include <net/addrconf.h>
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +020054#include <net/ndisc.h>
55#include <net/ipv6.h>
Ido Schimmel464dce12016-07-02 11:00:15 +020056
57#include "spectrum.h"
58#include "core.h"
59#include "reg.h"
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +020060#include "spectrum_cnt.h"
61#include "spectrum_dpipe.h"
62#include "spectrum_router.h"
Ido Schimmel464dce12016-07-02 11:00:15 +020063
Ido Schimmel9011b672017-05-16 19:38:25 +020064struct mlxsw_sp_vr;
65struct mlxsw_sp_lpm_tree;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +020066struct mlxsw_sp_rif_ops;
Ido Schimmel9011b672017-05-16 19:38:25 +020067
68struct mlxsw_sp_router {
69 struct mlxsw_sp *mlxsw_sp;
Ido Schimmel5f9efff2017-05-16 19:38:27 +020070 struct mlxsw_sp_rif **rifs;
Ido Schimmel9011b672017-05-16 19:38:25 +020071 struct mlxsw_sp_vr *vrs;
72 struct rhashtable neigh_ht;
73 struct rhashtable nexthop_group_ht;
74 struct rhashtable nexthop_ht;
75 struct {
76 struct mlxsw_sp_lpm_tree *trees;
77 unsigned int tree_count;
78 } lpm;
79 struct {
80 struct delayed_work dw;
81 unsigned long interval; /* ms */
82 } neighs_update;
83 struct delayed_work nexthop_probe_dw;
84#define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */
85 struct list_head nexthop_neighs_list;
86 bool aborted;
Ido Schimmel7e39d112017-05-16 19:38:28 +020087 struct notifier_block fib_nb;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +020088 const struct mlxsw_sp_rif_ops **rif_ops_arr;
Ido Schimmel9011b672017-05-16 19:38:25 +020089};
90
Ido Schimmel4724ba562017-03-10 08:53:39 +010091struct mlxsw_sp_rif {
92 struct list_head nexthop_list;
93 struct list_head neigh_list;
94 struct net_device *dev;
Ido Schimmela1107482017-05-26 08:37:39 +020095 struct mlxsw_sp_fid *fid;
Ido Schimmel4724ba562017-03-10 08:53:39 +010096 unsigned char addr[ETH_ALEN];
97 int mtu;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +010098 u16 rif_index;
Ido Schimmel69132292017-03-10 08:53:42 +010099 u16 vr_id;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200100 const struct mlxsw_sp_rif_ops *ops;
101 struct mlxsw_sp *mlxsw_sp;
102
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +0200103 unsigned int counter_ingress;
104 bool counter_ingress_valid;
105 unsigned int counter_egress;
106 bool counter_egress_valid;
Ido Schimmel4724ba562017-03-10 08:53:39 +0100107};
108
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200109struct mlxsw_sp_rif_params {
110 struct net_device *dev;
111 union {
112 u16 system_port;
113 u16 lag_id;
114 };
115 u16 vid;
116 bool lag;
117};
118
Ido Schimmel4d93cee2017-05-26 08:37:34 +0200119struct mlxsw_sp_rif_subport {
120 struct mlxsw_sp_rif common;
121 union {
122 u16 system_port;
123 u16 lag_id;
124 };
125 u16 vid;
126 bool lag;
127};
128
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200129struct mlxsw_sp_rif_ops {
130 enum mlxsw_sp_rif_type type;
131 size_t rif_size;
132
133 void (*setup)(struct mlxsw_sp_rif *rif,
134 const struct mlxsw_sp_rif_params *params);
135 int (*configure)(struct mlxsw_sp_rif *rif);
136 void (*deconfigure)(struct mlxsw_sp_rif *rif);
137 struct mlxsw_sp_fid * (*fid_get)(struct mlxsw_sp_rif *rif);
138};
139
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +0200140static unsigned int *
141mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
142 enum mlxsw_sp_rif_counter_dir dir)
143{
144 switch (dir) {
145 case MLXSW_SP_RIF_COUNTER_EGRESS:
146 return &rif->counter_egress;
147 case MLXSW_SP_RIF_COUNTER_INGRESS:
148 return &rif->counter_ingress;
149 }
150 return NULL;
151}
152
153static bool
154mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
155 enum mlxsw_sp_rif_counter_dir dir)
156{
157 switch (dir) {
158 case MLXSW_SP_RIF_COUNTER_EGRESS:
159 return rif->counter_egress_valid;
160 case MLXSW_SP_RIF_COUNTER_INGRESS:
161 return rif->counter_ingress_valid;
162 }
163 return false;
164}
165
166static void
167mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
168 enum mlxsw_sp_rif_counter_dir dir,
169 bool valid)
170{
171 switch (dir) {
172 case MLXSW_SP_RIF_COUNTER_EGRESS:
173 rif->counter_egress_valid = valid;
174 break;
175 case MLXSW_SP_RIF_COUNTER_INGRESS:
176 rif->counter_ingress_valid = valid;
177 break;
178 }
179}
180
181static int mlxsw_sp_rif_counter_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
182 unsigned int counter_index, bool enable,
183 enum mlxsw_sp_rif_counter_dir dir)
184{
185 char ritr_pl[MLXSW_REG_RITR_LEN];
186 bool is_egress = false;
187 int err;
188
189 if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
190 is_egress = true;
191 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
192 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
193 if (err)
194 return err;
195
196 mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
197 is_egress);
198 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
199}
200
201int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
202 struct mlxsw_sp_rif *rif,
203 enum mlxsw_sp_rif_counter_dir dir, u64 *cnt)
204{
205 char ricnt_pl[MLXSW_REG_RICNT_LEN];
206 unsigned int *p_counter_index;
207 bool valid;
208 int err;
209
210 valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
211 if (!valid)
212 return -EINVAL;
213
214 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
215 if (!p_counter_index)
216 return -EINVAL;
217 mlxsw_reg_ricnt_pack(ricnt_pl, *p_counter_index,
218 MLXSW_REG_RICNT_OPCODE_NOP);
219 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
220 if (err)
221 return err;
222 *cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
223 return 0;
224}
225
226static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
227 unsigned int counter_index)
228{
229 char ricnt_pl[MLXSW_REG_RICNT_LEN];
230
231 mlxsw_reg_ricnt_pack(ricnt_pl, counter_index,
232 MLXSW_REG_RICNT_OPCODE_CLEAR);
233 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
234}
235
236int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
237 struct mlxsw_sp_rif *rif,
238 enum mlxsw_sp_rif_counter_dir dir)
239{
240 unsigned int *p_counter_index;
241 int err;
242
243 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
244 if (!p_counter_index)
245 return -EINVAL;
246 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
247 p_counter_index);
248 if (err)
249 return err;
250
251 err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
252 if (err)
253 goto err_counter_clear;
254
255 err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
256 *p_counter_index, true, dir);
257 if (err)
258 goto err_counter_edit;
259 mlxsw_sp_rif_counter_valid_set(rif, dir, true);
260 return 0;
261
262err_counter_edit:
263err_counter_clear:
264 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
265 *p_counter_index);
266 return err;
267}
268
269void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
270 struct mlxsw_sp_rif *rif,
271 enum mlxsw_sp_rif_counter_dir dir)
272{
273 unsigned int *p_counter_index;
274
Arkadi Sharshevsky6b1206b2017-05-18 09:18:53 +0200275 if (!mlxsw_sp_rif_counter_valid_get(rif, dir))
276 return;
277
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +0200278 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
279 if (WARN_ON(!p_counter_index))
280 return;
281 mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
282 *p_counter_index, false, dir);
283 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
284 *p_counter_index);
285 mlxsw_sp_rif_counter_valid_set(rif, dir, false);
286}
287
Ido Schimmele4f3c1c2017-05-26 08:37:40 +0200288static void mlxsw_sp_rif_counters_alloc(struct mlxsw_sp_rif *rif)
289{
290 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
291 struct devlink *devlink;
292
293 devlink = priv_to_devlink(mlxsw_sp->core);
294 if (!devlink_dpipe_table_counter_enabled(devlink,
295 MLXSW_SP_DPIPE_TABLE_NAME_ERIF))
296 return;
297 mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
298}
299
300static void mlxsw_sp_rif_counters_free(struct mlxsw_sp_rif *rif)
301{
302 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
303
304 mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
305}
306
Ido Schimmel4724ba562017-03-10 08:53:39 +0100307static struct mlxsw_sp_rif *
308mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
309 const struct net_device *dev);
310
Ido Schimmel7dcc18a2017-07-18 10:10:30 +0200311#define MLXSW_SP_PREFIX_COUNT (sizeof(struct in6_addr) * BITS_PER_BYTE + 1)
Ido Schimmel9011b672017-05-16 19:38:25 +0200312
313struct mlxsw_sp_prefix_usage {
314 DECLARE_BITMAP(b, MLXSW_SP_PREFIX_COUNT);
315};
316
Jiri Pirko53342022016-07-04 08:23:08 +0200317#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
318 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
319
320static bool
Jiri Pirko6b75c482016-07-04 08:23:09 +0200321mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
322 struct mlxsw_sp_prefix_usage *prefix_usage2)
323{
324 unsigned char prefix;
325
326 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
327 if (!test_bit(prefix, prefix_usage2->b))
328 return false;
329 }
330 return true;
331}
332
333static bool
Jiri Pirko53342022016-07-04 08:23:08 +0200334mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
335 struct mlxsw_sp_prefix_usage *prefix_usage2)
336{
337 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
338}
339
Jiri Pirko6b75c482016-07-04 08:23:09 +0200340static bool
341mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
342{
343 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
344
345 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
346}
347
348static void
349mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
350 struct mlxsw_sp_prefix_usage *prefix_usage2)
351{
352 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
353}
354
355static void
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200356mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
357 unsigned char prefix_len)
358{
359 set_bit(prefix_len, prefix_usage->b);
360}
361
362static void
363mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
364 unsigned char prefix_len)
365{
366 clear_bit(prefix_len, prefix_usage->b);
367}
368
369struct mlxsw_sp_fib_key {
370 unsigned char addr[sizeof(struct in6_addr)];
371 unsigned char prefix_len;
372};
373
Jiri Pirko61c503f2016-07-04 08:23:11 +0200374enum mlxsw_sp_fib_entry_type {
375 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
376 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
377 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
378};
379
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200380struct mlxsw_sp_nexthop_group;
Ido Schimmel9011b672017-05-16 19:38:25 +0200381struct mlxsw_sp_fib;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200382
Ido Schimmel9aecce12017-02-09 10:28:42 +0100383struct mlxsw_sp_fib_node {
384 struct list_head entry_list;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200385 struct list_head list;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100386 struct rhash_head ht_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100387 struct mlxsw_sp_fib *fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100388 struct mlxsw_sp_fib_key key;
389};
390
Ido Schimmel9aecce12017-02-09 10:28:42 +0100391struct mlxsw_sp_fib_entry {
392 struct list_head list;
393 struct mlxsw_sp_fib_node *fib_node;
394 enum mlxsw_sp_fib_entry_type type;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200395 struct list_head nexthop_group_node;
396 struct mlxsw_sp_nexthop_group *nh_group;
Ido Schimmel013b20f2017-02-08 11:16:36 +0100397 bool offloaded;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200398};
399
Ido Schimmel4f1c7f12017-07-18 10:10:26 +0200400struct mlxsw_sp_fib4_entry {
401 struct mlxsw_sp_fib_entry common;
402 u32 tb_id;
403 u32 prio;
404 u8 tos;
405 u8 type;
406};
407
Ido Schimmel9011b672017-05-16 19:38:25 +0200408enum mlxsw_sp_l3proto {
409 MLXSW_SP_L3_PROTO_IPV4,
410 MLXSW_SP_L3_PROTO_IPV6,
411};
412
413struct mlxsw_sp_lpm_tree {
414 u8 id; /* tree ID */
415 unsigned int ref_count;
416 enum mlxsw_sp_l3proto proto;
417 struct mlxsw_sp_prefix_usage prefix_usage;
418};
419
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200420struct mlxsw_sp_fib {
421 struct rhashtable ht;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100422 struct list_head node_list;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100423 struct mlxsw_sp_vr *vr;
424 struct mlxsw_sp_lpm_tree *lpm_tree;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200425 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
426 struct mlxsw_sp_prefix_usage prefix_usage;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100427 enum mlxsw_sp_l3proto proto;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200428};
429
Ido Schimmel9011b672017-05-16 19:38:25 +0200430struct mlxsw_sp_vr {
431 u16 id; /* virtual router ID */
432 u32 tb_id; /* kernel fib table id */
433 unsigned int rif_count;
434 struct mlxsw_sp_fib *fib4;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200435 struct mlxsw_sp_fib *fib6;
Ido Schimmel9011b672017-05-16 19:38:25 +0200436};
437
Ido Schimmel9aecce12017-02-09 10:28:42 +0100438static const struct rhashtable_params mlxsw_sp_fib_ht_params;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200439
Ido Schimmel76610eb2017-03-10 08:53:41 +0100440static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp_vr *vr,
441 enum mlxsw_sp_l3proto proto)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200442{
443 struct mlxsw_sp_fib *fib;
444 int err;
445
446 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
447 if (!fib)
448 return ERR_PTR(-ENOMEM);
449 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
450 if (err)
451 goto err_rhashtable_init;
Ido Schimmel9aecce12017-02-09 10:28:42 +0100452 INIT_LIST_HEAD(&fib->node_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100453 fib->proto = proto;
454 fib->vr = vr;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200455 return fib;
456
457err_rhashtable_init:
458 kfree(fib);
459 return ERR_PTR(err);
460}
461
462static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
463{
Ido Schimmel9aecce12017-02-09 10:28:42 +0100464 WARN_ON(!list_empty(&fib->node_list));
Ido Schimmel76610eb2017-03-10 08:53:41 +0100465 WARN_ON(fib->lpm_tree);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200466 rhashtable_destroy(&fib->ht);
467 kfree(fib);
468}
469
Jiri Pirko53342022016-07-04 08:23:08 +0200470static struct mlxsw_sp_lpm_tree *
Ido Schimmel382dbb42017-03-10 08:53:40 +0100471mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko53342022016-07-04 08:23:08 +0200472{
473 static struct mlxsw_sp_lpm_tree *lpm_tree;
474 int i;
475
Ido Schimmel9011b672017-05-16 19:38:25 +0200476 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
477 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
Ido Schimmel382dbb42017-03-10 08:53:40 +0100478 if (lpm_tree->ref_count == 0)
479 return lpm_tree;
Jiri Pirko53342022016-07-04 08:23:08 +0200480 }
481 return NULL;
482}
483
484static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
485 struct mlxsw_sp_lpm_tree *lpm_tree)
486{
487 char ralta_pl[MLXSW_REG_RALTA_LEN];
488
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200489 mlxsw_reg_ralta_pack(ralta_pl, true,
490 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
491 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200492 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
493}
494
495static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
496 struct mlxsw_sp_lpm_tree *lpm_tree)
497{
498 char ralta_pl[MLXSW_REG_RALTA_LEN];
499
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200500 mlxsw_reg_ralta_pack(ralta_pl, false,
501 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
502 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200503 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
504}
505
506static int
507mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
508 struct mlxsw_sp_prefix_usage *prefix_usage,
509 struct mlxsw_sp_lpm_tree *lpm_tree)
510{
511 char ralst_pl[MLXSW_REG_RALST_LEN];
512 u8 root_bin = 0;
513 u8 prefix;
514 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
515
516 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
517 root_bin = prefix;
518
519 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
520 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
521 if (prefix == 0)
522 continue;
523 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
524 MLXSW_REG_RALST_BIN_NO_CHILD);
525 last_prefix = prefix;
526 }
527 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
528}
529
530static struct mlxsw_sp_lpm_tree *
531mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
532 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100533 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200534{
535 struct mlxsw_sp_lpm_tree *lpm_tree;
536 int err;
537
Ido Schimmel382dbb42017-03-10 08:53:40 +0100538 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
Jiri Pirko53342022016-07-04 08:23:08 +0200539 if (!lpm_tree)
540 return ERR_PTR(-EBUSY);
541 lpm_tree->proto = proto;
542 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
543 if (err)
544 return ERR_PTR(err);
545
546 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
547 lpm_tree);
548 if (err)
549 goto err_left_struct_set;
Jiri Pirko2083d362016-10-25 11:25:56 +0200550 memcpy(&lpm_tree->prefix_usage, prefix_usage,
551 sizeof(lpm_tree->prefix_usage));
Jiri Pirko53342022016-07-04 08:23:08 +0200552 return lpm_tree;
553
554err_left_struct_set:
555 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
556 return ERR_PTR(err);
557}
558
559static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
560 struct mlxsw_sp_lpm_tree *lpm_tree)
561{
562 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
563}
564
565static struct mlxsw_sp_lpm_tree *
566mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
567 struct mlxsw_sp_prefix_usage *prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100568 enum mlxsw_sp_l3proto proto)
Jiri Pirko53342022016-07-04 08:23:08 +0200569{
570 struct mlxsw_sp_lpm_tree *lpm_tree;
571 int i;
572
Ido Schimmel9011b672017-05-16 19:38:25 +0200573 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
574 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
Jiri Pirko8b99bec2016-10-25 11:25:57 +0200575 if (lpm_tree->ref_count != 0 &&
576 lpm_tree->proto == proto &&
Jiri Pirko53342022016-07-04 08:23:08 +0200577 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
578 prefix_usage))
579 goto inc_ref_count;
580 }
581 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
Ido Schimmel382dbb42017-03-10 08:53:40 +0100582 proto);
Jiri Pirko53342022016-07-04 08:23:08 +0200583 if (IS_ERR(lpm_tree))
584 return lpm_tree;
585
586inc_ref_count:
587 lpm_tree->ref_count++;
588 return lpm_tree;
589}
590
591static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
592 struct mlxsw_sp_lpm_tree *lpm_tree)
593{
594 if (--lpm_tree->ref_count == 0)
595 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
596 return 0;
597}
598
Ido Schimmeld7a60302017-06-08 08:47:43 +0200599#define MLXSW_SP_LPM_TREE_MIN 1 /* tree 0 is reserved */
Ido Schimmel8494ab02017-03-24 08:02:47 +0100600
601static int mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko53342022016-07-04 08:23:08 +0200602{
603 struct mlxsw_sp_lpm_tree *lpm_tree;
Ido Schimmel8494ab02017-03-24 08:02:47 +0100604 u64 max_trees;
Jiri Pirko53342022016-07-04 08:23:08 +0200605 int i;
606
Ido Schimmel8494ab02017-03-24 08:02:47 +0100607 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LPM_TREES))
608 return -EIO;
609
610 max_trees = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LPM_TREES);
Ido Schimmel9011b672017-05-16 19:38:25 +0200611 mlxsw_sp->router->lpm.tree_count = max_trees - MLXSW_SP_LPM_TREE_MIN;
612 mlxsw_sp->router->lpm.trees = kcalloc(mlxsw_sp->router->lpm.tree_count,
Ido Schimmel8494ab02017-03-24 08:02:47 +0100613 sizeof(struct mlxsw_sp_lpm_tree),
614 GFP_KERNEL);
Ido Schimmel9011b672017-05-16 19:38:25 +0200615 if (!mlxsw_sp->router->lpm.trees)
Ido Schimmel8494ab02017-03-24 08:02:47 +0100616 return -ENOMEM;
617
Ido Schimmel9011b672017-05-16 19:38:25 +0200618 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
619 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
Jiri Pirko53342022016-07-04 08:23:08 +0200620 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
621 }
Ido Schimmel8494ab02017-03-24 08:02:47 +0100622
623 return 0;
624}
625
626static void mlxsw_sp_lpm_fini(struct mlxsw_sp *mlxsw_sp)
627{
Ido Schimmel9011b672017-05-16 19:38:25 +0200628 kfree(mlxsw_sp->router->lpm.trees);
Jiri Pirko53342022016-07-04 08:23:08 +0200629}
630
Ido Schimmel76610eb2017-03-10 08:53:41 +0100631static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
632{
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200633 return !!vr->fib4 || !!vr->fib6;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100634}
635
Jiri Pirko6b75c482016-07-04 08:23:09 +0200636static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
637{
638 struct mlxsw_sp_vr *vr;
639 int i;
640
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200641 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +0200642 vr = &mlxsw_sp->router->vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100643 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200644 return vr;
645 }
646 return NULL;
647}
648
649static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100650 const struct mlxsw_sp_fib *fib)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200651{
652 char raltb_pl[MLXSW_REG_RALTB_LEN];
653
Ido Schimmel76610eb2017-03-10 08:53:41 +0100654 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
655 (enum mlxsw_reg_ralxx_protocol) fib->proto,
656 fib->lpm_tree->id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200657 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
658}
659
660static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100661 const struct mlxsw_sp_fib *fib)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200662{
663 char raltb_pl[MLXSW_REG_RALTB_LEN];
664
665 /* Bind to tree 0 which is default */
Ido Schimmel76610eb2017-03-10 08:53:41 +0100666 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
667 (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200668 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
669}
670
671static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
672{
673 /* For our purpose, squash main and local table into one */
674 if (tb_id == RT_TABLE_LOCAL)
675 tb_id = RT_TABLE_MAIN;
676 return tb_id;
677}
678
679static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100680 u32 tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200681{
682 struct mlxsw_sp_vr *vr;
683 int i;
684
685 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Nogah Frankel9497c042016-09-20 11:16:54 +0200686
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200687 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +0200688 vr = &mlxsw_sp->router->vrs[i];
Ido Schimmel76610eb2017-03-10 08:53:41 +0100689 if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200690 return vr;
691 }
692 return NULL;
693}
694
Ido Schimmel76610eb2017-03-10 08:53:41 +0100695static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
696 enum mlxsw_sp_l3proto proto)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200697{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100698 switch (proto) {
699 case MLXSW_SP_L3_PROTO_IPV4:
700 return vr->fib4;
701 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200702 return vr->fib6;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100703 }
704 return NULL;
705}
706
707static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
708 u32 tb_id)
709{
Jiri Pirko6b75c482016-07-04 08:23:09 +0200710 struct mlxsw_sp_vr *vr;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200711 int err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200712
713 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
714 if (!vr)
715 return ERR_PTR(-EBUSY);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100716 vr->fib4 = mlxsw_sp_fib_create(vr, MLXSW_SP_L3_PROTO_IPV4);
717 if (IS_ERR(vr->fib4))
718 return ERR_CAST(vr->fib4);
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200719 vr->fib6 = mlxsw_sp_fib_create(vr, MLXSW_SP_L3_PROTO_IPV6);
720 if (IS_ERR(vr->fib6)) {
721 err = PTR_ERR(vr->fib6);
722 goto err_fib6_create;
723 }
Jiri Pirko6b75c482016-07-04 08:23:09 +0200724 vr->tb_id = tb_id;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200725 return vr;
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200726
727err_fib6_create:
728 mlxsw_sp_fib_destroy(vr->fib4);
729 vr->fib4 = NULL;
730 return ERR_PTR(err);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200731}
732
Ido Schimmel76610eb2017-03-10 08:53:41 +0100733static void mlxsw_sp_vr_destroy(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200734{
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200735 mlxsw_sp_fib_destroy(vr->fib6);
736 vr->fib6 = NULL;
Ido Schimmel76610eb2017-03-10 08:53:41 +0100737 mlxsw_sp_fib_destroy(vr->fib4);
738 vr->fib4 = NULL;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200739}
740
741static int
Ido Schimmel76610eb2017-03-10 08:53:41 +0100742mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fib *fib,
Jiri Pirko6b75c482016-07-04 08:23:09 +0200743 struct mlxsw_sp_prefix_usage *req_prefix_usage)
744{
Ido Schimmel76610eb2017-03-10 08:53:41 +0100745 struct mlxsw_sp_lpm_tree *lpm_tree = fib->lpm_tree;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100746 struct mlxsw_sp_lpm_tree *new_tree;
747 int err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200748
Ido Schimmelf7df4922017-02-28 08:55:40 +0100749 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage, &lpm_tree->prefix_usage))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200750 return 0;
751
Ido Schimmelf7df4922017-02-28 08:55:40 +0100752 new_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
Ido Schimmel76610eb2017-03-10 08:53:41 +0100753 fib->proto);
Ido Schimmelf7df4922017-02-28 08:55:40 +0100754 if (IS_ERR(new_tree)) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200755 /* We failed to get a tree according to the required
756 * prefix usage. However, the current tree might be still good
757 * for us if our requirement is subset of the prefixes used
758 * in the tree.
759 */
760 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
Ido Schimmelf7df4922017-02-28 08:55:40 +0100761 &lpm_tree->prefix_usage))
Jiri Pirko6b75c482016-07-04 08:23:09 +0200762 return 0;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100763 return PTR_ERR(new_tree);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200764 }
765
Ido Schimmelf7df4922017-02-28 08:55:40 +0100766 /* Prevent packet loss by overwriting existing binding */
Ido Schimmel76610eb2017-03-10 08:53:41 +0100767 fib->lpm_tree = new_tree;
768 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
Ido Schimmelf7df4922017-02-28 08:55:40 +0100769 if (err)
770 goto err_tree_bind;
771 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
772
773 return 0;
774
775err_tree_bind:
Ido Schimmel76610eb2017-03-10 08:53:41 +0100776 fib->lpm_tree = lpm_tree;
Ido Schimmelf7df4922017-02-28 08:55:40 +0100777 mlxsw_sp_lpm_tree_put(mlxsw_sp, new_tree);
778 return err;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200779}
780
Ido Schimmel76610eb2017-03-10 08:53:41 +0100781static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200782{
783 struct mlxsw_sp_vr *vr;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200784
785 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Ido Schimmel76610eb2017-03-10 08:53:41 +0100786 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
787 if (!vr)
788 vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200789 return vr;
790}
791
Ido Schimmel76610eb2017-03-10 08:53:41 +0100792static void mlxsw_sp_vr_put(struct mlxsw_sp_vr *vr)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200793{
Ido Schimmela3d9bc52017-07-18 10:10:22 +0200794 if (!vr->rif_count && list_empty(&vr->fib4->node_list) &&
795 list_empty(&vr->fib6->node_list))
Ido Schimmel76610eb2017-03-10 08:53:41 +0100796 mlxsw_sp_vr_destroy(vr);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200797}
798
Nogah Frankel9497c042016-09-20 11:16:54 +0200799static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200800{
801 struct mlxsw_sp_vr *vr;
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200802 u64 max_vrs;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200803 int i;
804
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200805 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
Nogah Frankel9497c042016-09-20 11:16:54 +0200806 return -EIO;
807
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200808 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
Ido Schimmel9011b672017-05-16 19:38:25 +0200809 mlxsw_sp->router->vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
810 GFP_KERNEL);
811 if (!mlxsw_sp->router->vrs)
Nogah Frankel9497c042016-09-20 11:16:54 +0200812 return -ENOMEM;
813
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200814 for (i = 0; i < max_vrs; i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +0200815 vr = &mlxsw_sp->router->vrs[i];
Jiri Pirko6b75c482016-07-04 08:23:09 +0200816 vr->id = i;
817 }
Nogah Frankel9497c042016-09-20 11:16:54 +0200818
819 return 0;
820}
821
Ido Schimmelac571de2016-11-14 11:26:32 +0100822static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
823
Nogah Frankel9497c042016-09-20 11:16:54 +0200824static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
825{
Ido Schimmel30572242016-12-03 16:45:01 +0100826 /* At this stage we're guaranteed not to have new incoming
827 * FIB notifications and the work queue is free from FIBs
828 * sitting on top of mlxsw netdevs. However, we can still
829 * have other FIBs queued. Flush the queue before flushing
830 * the device's tables. No need for locks, as we're the only
831 * writer.
832 */
833 mlxsw_core_flush_owq();
Ido Schimmelac571de2016-11-14 11:26:32 +0100834 mlxsw_sp_router_fib_flush(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +0200835 kfree(mlxsw_sp->router->vrs);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200836}
837
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200838struct mlxsw_sp_neigh_key {
Jiri Pirko33b13412016-11-10 12:31:04 +0100839 struct neighbour *n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200840};
841
842struct mlxsw_sp_neigh_entry {
Ido Schimmel9665b742017-02-08 11:16:42 +0100843 struct list_head rif_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200844 struct rhash_head ht_node;
845 struct mlxsw_sp_neigh_key key;
846 u16 rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100847 bool connected;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200848 unsigned char ha[ETH_ALEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200849 struct list_head nexthop_list; /* list of nexthops using
850 * this neigh entry
851 */
Yotam Gigib2157142016-07-05 11:27:51 +0200852 struct list_head nexthop_neighs_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200853};
854
855static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
856 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
857 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
858 .key_len = sizeof(struct mlxsw_sp_neigh_key),
859};
860
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100861static struct mlxsw_sp_neigh_entry *
862mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
863 u16 rif)
864{
865 struct mlxsw_sp_neigh_entry *neigh_entry;
866
867 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
868 if (!neigh_entry)
869 return NULL;
870
871 neigh_entry->key.n = n;
872 neigh_entry->rif = rif;
873 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
874
875 return neigh_entry;
876}
877
878static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
879{
880 kfree(neigh_entry);
881}
882
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200883static int
884mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
885 struct mlxsw_sp_neigh_entry *neigh_entry)
886{
Ido Schimmel9011b672017-05-16 19:38:25 +0200887 return rhashtable_insert_fast(&mlxsw_sp->router->neigh_ht,
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200888 &neigh_entry->ht_node,
889 mlxsw_sp_neigh_ht_params);
890}
891
892static void
893mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
894 struct mlxsw_sp_neigh_entry *neigh_entry)
895{
Ido Schimmel9011b672017-05-16 19:38:25 +0200896 rhashtable_remove_fast(&mlxsw_sp->router->neigh_ht,
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200897 &neigh_entry->ht_node,
898 mlxsw_sp_neigh_ht_params);
899}
900
901static struct mlxsw_sp_neigh_entry *
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100902mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200903{
904 struct mlxsw_sp_neigh_entry *neigh_entry;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100905 struct mlxsw_sp_rif *rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100906 int err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200907
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100908 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
909 if (!rif)
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100910 return ERR_PTR(-EINVAL);
911
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100912 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200913 if (!neigh_entry)
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100914 return ERR_PTR(-ENOMEM);
915
916 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
917 if (err)
918 goto err_neigh_entry_insert;
919
Arkadi Sharshevskybf952332017-03-17 09:38:00 +0100920 list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
Ido Schimmel9665b742017-02-08 11:16:42 +0100921
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200922 return neigh_entry;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100923
924err_neigh_entry_insert:
925 mlxsw_sp_neigh_entry_free(neigh_entry);
926 return ERR_PTR(err);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200927}
928
929static void
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100930mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
931 struct mlxsw_sp_neigh_entry *neigh_entry)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200932{
Ido Schimmel9665b742017-02-08 11:16:42 +0100933 list_del(&neigh_entry->rif_list_node);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100934 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
935 mlxsw_sp_neigh_entry_free(neigh_entry);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200936}
937
938static struct mlxsw_sp_neigh_entry *
Jiri Pirko33b13412016-11-10 12:31:04 +0100939mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200940{
Jiri Pirko33b13412016-11-10 12:31:04 +0100941 struct mlxsw_sp_neigh_key key;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200942
Jiri Pirko33b13412016-11-10 12:31:04 +0100943 key.n = n;
Ido Schimmel9011b672017-05-16 19:38:25 +0200944 return rhashtable_lookup_fast(&mlxsw_sp->router->neigh_ht,
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200945 &key, mlxsw_sp_neigh_ht_params);
946}
947
Yotam Gigic723c7352016-07-05 11:27:43 +0200948static void
949mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
950{
Arkadi Sharshevskya6c9b5d2017-07-18 10:10:18 +0200951 unsigned long interval;
Yotam Gigic723c7352016-07-05 11:27:43 +0200952
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +0200953#if IS_ENABLED(CONFIG_IPV6)
Arkadi Sharshevskya6c9b5d2017-07-18 10:10:18 +0200954 interval = min_t(unsigned long,
955 NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME),
956 NEIGH_VAR(&nd_tbl.parms, DELAY_PROBE_TIME));
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +0200957#else
958 interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
959#endif
Ido Schimmel9011b672017-05-16 19:38:25 +0200960 mlxsw_sp->router->neighs_update.interval = jiffies_to_msecs(interval);
Yotam Gigic723c7352016-07-05 11:27:43 +0200961}
962
963static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
964 char *rauhtd_pl,
965 int ent_index)
966{
967 struct net_device *dev;
968 struct neighbour *n;
969 __be32 dipn;
970 u32 dip;
971 u16 rif;
972
973 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
974
Ido Schimmel5f9efff2017-05-16 19:38:27 +0200975 if (!mlxsw_sp->router->rifs[rif]) {
Yotam Gigic723c7352016-07-05 11:27:43 +0200976 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
977 return;
978 }
979
980 dipn = htonl(dip);
Ido Schimmel5f9efff2017-05-16 19:38:27 +0200981 dev = mlxsw_sp->router->rifs[rif]->dev;
Yotam Gigic723c7352016-07-05 11:27:43 +0200982 n = neigh_lookup(&arp_tbl, &dipn, dev);
983 if (!n) {
984 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
985 &dip);
986 return;
987 }
988
989 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
990 neigh_event_send(n, NULL);
991 neigh_release(n);
992}
993
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +0200994#if IS_ENABLED(IPV6)
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +0200995static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
996 char *rauhtd_pl,
997 int rec_index)
998{
999 struct net_device *dev;
1000 struct neighbour *n;
1001 struct in6_addr dip;
1002 u16 rif;
1003
1004 mlxsw_reg_rauhtd_ent_ipv6_unpack(rauhtd_pl, rec_index, &rif,
1005 (char *) &dip);
1006
1007 if (!mlxsw_sp->router->rifs[rif]) {
1008 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
1009 return;
1010 }
1011
1012 dev = mlxsw_sp->router->rifs[rif]->dev;
1013 n = neigh_lookup(&nd_tbl, &dip, dev);
1014 if (!n) {
1015 netdev_err(dev, "Failed to find matching neighbour for IP=%pI6c\n",
1016 &dip);
1017 return;
1018 }
1019
1020 netdev_dbg(dev, "Updating neighbour with IP=%pI6c\n", &dip);
1021 neigh_event_send(n, NULL);
1022 neigh_release(n);
1023}
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001024#else
1025static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
1026 char *rauhtd_pl,
1027 int rec_index)
1028{
1029}
1030#endif
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001031
Yotam Gigic723c7352016-07-05 11:27:43 +02001032static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
1033 char *rauhtd_pl,
1034 int rec_index)
1035{
1036 u8 num_entries;
1037 int i;
1038
1039 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
1040 rec_index);
1041 /* Hardware starts counting at 0, so add 1. */
1042 num_entries++;
1043
1044 /* Each record consists of several neighbour entries. */
1045 for (i = 0; i < num_entries; i++) {
1046 int ent_index;
1047
1048 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
1049 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
1050 ent_index);
1051 }
1052
1053}
1054
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001055static void mlxsw_sp_router_neigh_rec_ipv6_process(struct mlxsw_sp *mlxsw_sp,
1056 char *rauhtd_pl,
1057 int rec_index)
1058{
1059 /* One record contains one entry. */
1060 mlxsw_sp_router_neigh_ent_ipv6_process(mlxsw_sp, rauhtd_pl,
1061 rec_index);
1062}
1063
Yotam Gigic723c7352016-07-05 11:27:43 +02001064static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
1065 char *rauhtd_pl, int rec_index)
1066{
1067 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
1068 case MLXSW_REG_RAUHTD_TYPE_IPV4:
1069 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
1070 rec_index);
1071 break;
1072 case MLXSW_REG_RAUHTD_TYPE_IPV6:
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001073 mlxsw_sp_router_neigh_rec_ipv6_process(mlxsw_sp, rauhtd_pl,
1074 rec_index);
Yotam Gigic723c7352016-07-05 11:27:43 +02001075 break;
1076 }
1077}
1078
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +01001079static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
1080{
1081 u8 num_rec, last_rec_index, num_entries;
1082
1083 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
1084 last_rec_index = num_rec - 1;
1085
1086 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
1087 return false;
1088 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
1089 MLXSW_REG_RAUHTD_TYPE_IPV6)
1090 return true;
1091
1092 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
1093 last_rec_index);
1094 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
1095 return true;
1096 return false;
1097}
1098
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001099static int
1100__mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp,
1101 char *rauhtd_pl,
1102 enum mlxsw_reg_rauhtd_type type)
Yotam Gigic723c7352016-07-05 11:27:43 +02001103{
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001104 int i, num_rec;
1105 int err;
Yotam Gigic723c7352016-07-05 11:27:43 +02001106
1107 /* Make sure the neighbour's netdev isn't removed in the
1108 * process.
1109 */
1110 rtnl_lock();
1111 do {
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001112 mlxsw_reg_rauhtd_pack(rauhtd_pl, type);
Yotam Gigic723c7352016-07-05 11:27:43 +02001113 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
1114 rauhtd_pl);
1115 if (err) {
1116 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
1117 break;
1118 }
1119 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
1120 for (i = 0; i < num_rec; i++)
1121 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
1122 i);
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +01001123 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
Yotam Gigic723c7352016-07-05 11:27:43 +02001124 rtnl_unlock();
1125
Arkadi Sharshevsky60f040c2017-07-18 10:10:17 +02001126 return err;
1127}
1128
1129static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
1130{
1131 enum mlxsw_reg_rauhtd_type type;
1132 char *rauhtd_pl;
1133 int err;
1134
1135 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
1136 if (!rauhtd_pl)
1137 return -ENOMEM;
1138
1139 type = MLXSW_REG_RAUHTD_TYPE_IPV4;
1140 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
1141 if (err)
1142 goto out;
1143
1144 type = MLXSW_REG_RAUHTD_TYPE_IPV6;
1145 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
1146out:
Yotam Gigic723c7352016-07-05 11:27:43 +02001147 kfree(rauhtd_pl);
Yotam Gigib2157142016-07-05 11:27:51 +02001148 return err;
1149}
1150
1151static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
1152{
1153 struct mlxsw_sp_neigh_entry *neigh_entry;
1154
1155 /* Take RTNL mutex here to prevent lists from changes */
1156 rtnl_lock();
Ido Schimmel9011b672017-05-16 19:38:25 +02001157 list_for_each_entry(neigh_entry, &mlxsw_sp->router->nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +01001158 nexthop_neighs_list_node)
Yotam Gigib2157142016-07-05 11:27:51 +02001159 /* If this neigh have nexthops, make the kernel think this neigh
1160 * is active regardless of the traffic.
1161 */
Ido Schimmel8a0b7272017-02-06 16:20:15 +01001162 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigib2157142016-07-05 11:27:51 +02001163 rtnl_unlock();
1164}
1165
1166static void
1167mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
1168{
Ido Schimmel9011b672017-05-16 19:38:25 +02001169 unsigned long interval = mlxsw_sp->router->neighs_update.interval;
Yotam Gigib2157142016-07-05 11:27:51 +02001170
Ido Schimmel9011b672017-05-16 19:38:25 +02001171 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw,
Yotam Gigib2157142016-07-05 11:27:51 +02001172 msecs_to_jiffies(interval));
1173}
1174
1175static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
1176{
Ido Schimmel9011b672017-05-16 19:38:25 +02001177 struct mlxsw_sp_router *router;
Yotam Gigib2157142016-07-05 11:27:51 +02001178 int err;
1179
Ido Schimmel9011b672017-05-16 19:38:25 +02001180 router = container_of(work, struct mlxsw_sp_router,
1181 neighs_update.dw.work);
1182 err = mlxsw_sp_router_neighs_update_rauhtd(router->mlxsw_sp);
Yotam Gigib2157142016-07-05 11:27:51 +02001183 if (err)
Ido Schimmel9011b672017-05-16 19:38:25 +02001184 dev_err(router->mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
Yotam Gigib2157142016-07-05 11:27:51 +02001185
Ido Schimmel9011b672017-05-16 19:38:25 +02001186 mlxsw_sp_router_neighs_update_nh(router->mlxsw_sp);
Yotam Gigib2157142016-07-05 11:27:51 +02001187
Ido Schimmel9011b672017-05-16 19:38:25 +02001188 mlxsw_sp_router_neighs_update_work_schedule(router->mlxsw_sp);
Yotam Gigic723c7352016-07-05 11:27:43 +02001189}
1190
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001191static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
1192{
1193 struct mlxsw_sp_neigh_entry *neigh_entry;
Ido Schimmel9011b672017-05-16 19:38:25 +02001194 struct mlxsw_sp_router *router;
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001195
Ido Schimmel9011b672017-05-16 19:38:25 +02001196 router = container_of(work, struct mlxsw_sp_router,
1197 nexthop_probe_dw.work);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001198 /* Iterate over nexthop neighbours, find those who are unresolved and
1199 * send arp on them. This solves the chicken-egg problem when
1200 * the nexthop wouldn't get offloaded until the neighbor is resolved
1201 * but it wouldn't get resolved ever in case traffic is flowing in HW
1202 * using different nexthop.
1203 *
1204 * Take RTNL mutex here to prevent lists from changes.
1205 */
1206 rtnl_lock();
Ido Schimmel9011b672017-05-16 19:38:25 +02001207 list_for_each_entry(neigh_entry, &router->nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +01001208 nexthop_neighs_list_node)
Ido Schimmel01b1aa32017-02-06 16:20:16 +01001209 if (!neigh_entry->connected)
Jiri Pirko33b13412016-11-10 12:31:04 +01001210 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001211 rtnl_unlock();
1212
Ido Schimmel9011b672017-05-16 19:38:25 +02001213 mlxsw_core_schedule_dw(&router->nexthop_probe_dw,
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001214 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
1215}
1216
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001217static void
1218mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1219 struct mlxsw_sp_neigh_entry *neigh_entry,
1220 bool removing);
1221
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001222static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001223{
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001224 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
1225 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
1226}
1227
1228static void
1229mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
1230 struct mlxsw_sp_neigh_entry *neigh_entry,
1231 enum mlxsw_reg_rauht_op op)
1232{
Jiri Pirko33b13412016-11-10 12:31:04 +01001233 struct neighbour *n = neigh_entry->key.n;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001234 u32 dip = ntohl(*((__be32 *) n->primary_key));
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001235 char rauht_pl[MLXSW_REG_RAUHT_LEN];
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001236
1237 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
1238 dip);
1239 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1240}
1241
1242static void
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02001243mlxsw_sp_router_neigh_entry_op6(struct mlxsw_sp *mlxsw_sp,
1244 struct mlxsw_sp_neigh_entry *neigh_entry,
1245 enum mlxsw_reg_rauht_op op)
1246{
1247 struct neighbour *n = neigh_entry->key.n;
1248 char rauht_pl[MLXSW_REG_RAUHT_LEN];
1249 const char *dip = n->primary_key;
1250
1251 mlxsw_reg_rauht_pack6(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
1252 dip);
1253 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1254}
1255
1256static bool mlxsw_sp_neigh_ipv6_ignore(struct neighbour *n)
1257{
1258 /* Packets with a link-local destination address are trapped
1259 * after LPM lookup and never reach the neighbour table, so
1260 * there is no need to program such neighbours to the device.
1261 */
1262 if (ipv6_addr_type((struct in6_addr *) &n->primary_key) &
1263 IPV6_ADDR_LINKLOCAL)
1264 return true;
1265 return false;
1266}
1267
1268static void
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001269mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
1270 struct mlxsw_sp_neigh_entry *neigh_entry,
1271 bool adding)
1272{
1273 if (!adding && !neigh_entry->connected)
1274 return;
1275 neigh_entry->connected = adding;
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001276 if (neigh_entry->key.n->tbl->family == AF_INET) {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001277 mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
1278 mlxsw_sp_rauht_op(adding));
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001279 } else if (neigh_entry->key.n->tbl->family == AF_INET6) {
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02001280 if (mlxsw_sp_neigh_ipv6_ignore(neigh_entry->key.n))
1281 return;
1282 mlxsw_sp_router_neigh_entry_op6(mlxsw_sp, neigh_entry,
1283 mlxsw_sp_rauht_op(adding));
1284 } else {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001285 WARN_ON_ONCE(1);
Arkadi Sharshevskyd5eb89c2017-07-18 10:10:15 +02001286 }
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001287}
1288
1289struct mlxsw_sp_neigh_event_work {
1290 struct work_struct work;
1291 struct mlxsw_sp *mlxsw_sp;
1292 struct neighbour *n;
1293};
1294
1295static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
1296{
1297 struct mlxsw_sp_neigh_event_work *neigh_work =
1298 container_of(work, struct mlxsw_sp_neigh_event_work, work);
1299 struct mlxsw_sp *mlxsw_sp = neigh_work->mlxsw_sp;
1300 struct mlxsw_sp_neigh_entry *neigh_entry;
1301 struct neighbour *n = neigh_work->n;
1302 unsigned char ha[ETH_ALEN];
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001303 bool entry_connected;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001304 u8 nud_state, dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001305
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001306 /* If these parameters are changed after we release the lock,
1307 * then we are guaranteed to receive another event letting us
1308 * know about it.
1309 */
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001310 read_lock_bh(&n->lock);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001311 memcpy(ha, n->ha, ETH_ALEN);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001312 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001313 dead = n->dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001314 read_unlock_bh(&n->lock);
1315
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001316 rtnl_lock();
Ido Schimmel93a87e52016-12-23 09:32:49 +01001317 entry_connected = nud_state & NUD_VALID && !dead;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001318 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1319 if (!entry_connected && !neigh_entry)
1320 goto out;
1321 if (!neigh_entry) {
1322 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1323 if (IS_ERR(neigh_entry))
1324 goto out;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001325 }
1326
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001327 memcpy(neigh_entry->ha, ha, ETH_ALEN);
1328 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
1329 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected);
1330
1331 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1332 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1333
1334out:
1335 rtnl_unlock();
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001336 neigh_release(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001337 kfree(neigh_work);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001338}
1339
Jiri Pirkoe7322632016-09-01 10:37:43 +02001340int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
1341 unsigned long event, void *ptr)
Yotam Gigic723c7352016-07-05 11:27:43 +02001342{
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001343 struct mlxsw_sp_neigh_event_work *neigh_work;
Yotam Gigic723c7352016-07-05 11:27:43 +02001344 struct mlxsw_sp_port *mlxsw_sp_port;
1345 struct mlxsw_sp *mlxsw_sp;
1346 unsigned long interval;
1347 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001348 struct neighbour *n;
Yotam Gigic723c7352016-07-05 11:27:43 +02001349
1350 switch (event) {
1351 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
1352 p = ptr;
1353
1354 /* We don't care about changes in the default table. */
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001355 if (!p->dev || (p->tbl->family != AF_INET &&
1356 p->tbl->family != AF_INET6))
Yotam Gigic723c7352016-07-05 11:27:43 +02001357 return NOTIFY_DONE;
1358
1359 /* We are in atomic context and can't take RTNL mutex,
1360 * so use RCU variant to walk the device chain.
1361 */
1362 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
1363 if (!mlxsw_sp_port)
1364 return NOTIFY_DONE;
1365
1366 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1367 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
Ido Schimmel9011b672017-05-16 19:38:25 +02001368 mlxsw_sp->router->neighs_update.interval = interval;
Yotam Gigic723c7352016-07-05 11:27:43 +02001369
1370 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1371 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001372 case NETEVENT_NEIGH_UPDATE:
1373 n = ptr;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001374
Ido Schimmelb5f3e0d2017-07-24 09:56:00 +02001375 if (n->tbl->family != AF_INET && n->tbl->family != AF_INET6)
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001376 return NOTIFY_DONE;
1377
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001378 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001379 if (!mlxsw_sp_port)
1380 return NOTIFY_DONE;
1381
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001382 neigh_work = kzalloc(sizeof(*neigh_work), GFP_ATOMIC);
1383 if (!neigh_work) {
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001384 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001385 return NOTIFY_BAD;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001386 }
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001387
1388 INIT_WORK(&neigh_work->work, mlxsw_sp_router_neigh_event_work);
1389 neigh_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1390 neigh_work->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001391
1392 /* Take a reference to ensure the neighbour won't be
1393 * destructed until we drop the reference in delayed
1394 * work.
1395 */
1396 neigh_clone(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001397 mlxsw_core_schedule_work(&neigh_work->work);
1398 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001399 break;
Yotam Gigic723c7352016-07-05 11:27:43 +02001400 }
1401
1402 return NOTIFY_DONE;
1403}
1404
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001405static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
1406{
Yotam Gigic723c7352016-07-05 11:27:43 +02001407 int err;
1408
Ido Schimmel9011b672017-05-16 19:38:25 +02001409 err = rhashtable_init(&mlxsw_sp->router->neigh_ht,
Yotam Gigic723c7352016-07-05 11:27:43 +02001410 &mlxsw_sp_neigh_ht_params);
1411 if (err)
1412 return err;
1413
1414 /* Initialize the polling interval according to the default
1415 * table.
1416 */
1417 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1418
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001419 /* Create the delayed works for the activity_update */
Ido Schimmel9011b672017-05-16 19:38:25 +02001420 INIT_DELAYED_WORK(&mlxsw_sp->router->neighs_update.dw,
Yotam Gigic723c7352016-07-05 11:27:43 +02001421 mlxsw_sp_router_neighs_update_work);
Ido Schimmel9011b672017-05-16 19:38:25 +02001422 INIT_DELAYED_WORK(&mlxsw_sp->router->nexthop_probe_dw,
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001423 mlxsw_sp_router_probe_unresolved_nexthops);
Ido Schimmel9011b672017-05-16 19:38:25 +02001424 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw, 0);
1425 mlxsw_core_schedule_dw(&mlxsw_sp->router->nexthop_probe_dw, 0);
Yotam Gigic723c7352016-07-05 11:27:43 +02001426 return 0;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001427}
1428
1429static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1430{
Ido Schimmel9011b672017-05-16 19:38:25 +02001431 cancel_delayed_work_sync(&mlxsw_sp->router->neighs_update.dw);
1432 cancel_delayed_work_sync(&mlxsw_sp->router->nexthop_probe_dw);
1433 rhashtable_destroy(&mlxsw_sp->router->neigh_ht);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001434}
1435
Ido Schimmel9665b742017-02-08 11:16:42 +01001436static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001437 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001438{
1439 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
1440
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001441 list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
Ido Schimmel4a3c67a2017-07-21 20:31:38 +02001442 rif_list_node) {
1443 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, false);
Ido Schimmel9665b742017-02-08 11:16:42 +01001444 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
Ido Schimmel4a3c67a2017-07-21 20:31:38 +02001445 }
Ido Schimmel9665b742017-02-08 11:16:42 +01001446}
1447
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001448struct mlxsw_sp_nexthop_key {
1449 struct fib_nh *fib_nh;
1450};
1451
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001452struct mlxsw_sp_nexthop {
1453 struct list_head neigh_list_node; /* member of neigh entry list */
Ido Schimmel9665b742017-02-08 11:16:42 +01001454 struct list_head rif_list_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001455 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1456 * this belongs to
1457 */
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001458 struct rhash_head ht_node;
1459 struct mlxsw_sp_nexthop_key key;
Ido Schimmel58adf2c2017-07-18 10:10:19 +02001460 unsigned char gw_addr[sizeof(struct in6_addr)];
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001461 struct mlxsw_sp_rif *rif;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001462 u8 should_offload:1, /* set indicates this neigh is connected and
1463 * should be put to KVD linear area of this group.
1464 */
1465 offloaded:1, /* set in case the neigh is actually put into
1466 * KVD linear area of this group.
1467 */
1468 update:1; /* set indicates that MAC of this neigh should be
1469 * updated in HW
1470 */
1471 struct mlxsw_sp_neigh_entry *neigh_entry;
1472};
1473
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001474struct mlxsw_sp_nexthop_group_key {
1475 struct fib_info *fi;
1476};
1477
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001478struct mlxsw_sp_nexthop_group {
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001479 struct rhash_head ht_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001480 struct list_head fib_list; /* list of fib entries that use this group */
Ido Schimmel58adf2c2017-07-18 10:10:19 +02001481 struct neigh_table *neigh_tbl;
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001482 struct mlxsw_sp_nexthop_group_key key;
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001483 u8 adj_index_valid:1,
1484 gateway:1; /* routes using the group use a gateway */
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001485 u32 adj_index;
1486 u16 ecmp_size;
1487 u16 count;
1488 struct mlxsw_sp_nexthop nexthops[0];
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001489#define nh_rif nexthops[0].rif
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001490};
1491
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001492static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
1493 .key_offset = offsetof(struct mlxsw_sp_nexthop_group, key),
1494 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
1495 .key_len = sizeof(struct mlxsw_sp_nexthop_group_key),
1496};
1497
1498static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
1499 struct mlxsw_sp_nexthop_group *nh_grp)
1500{
Ido Schimmel9011b672017-05-16 19:38:25 +02001501 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_group_ht,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001502 &nh_grp->ht_node,
1503 mlxsw_sp_nexthop_group_ht_params);
1504}
1505
1506static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
1507 struct mlxsw_sp_nexthop_group *nh_grp)
1508{
Ido Schimmel9011b672017-05-16 19:38:25 +02001509 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_group_ht,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001510 &nh_grp->ht_node,
1511 mlxsw_sp_nexthop_group_ht_params);
1512}
1513
1514static struct mlxsw_sp_nexthop_group *
1515mlxsw_sp_nexthop_group_lookup(struct mlxsw_sp *mlxsw_sp,
1516 struct mlxsw_sp_nexthop_group_key key)
1517{
Ido Schimmel9011b672017-05-16 19:38:25 +02001518 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht, &key,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001519 mlxsw_sp_nexthop_group_ht_params);
1520}
1521
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001522static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
1523 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
1524 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
1525 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
1526};
1527
1528static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
1529 struct mlxsw_sp_nexthop *nh)
1530{
Ido Schimmel9011b672017-05-16 19:38:25 +02001531 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_ht,
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001532 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
1533}
1534
1535static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
1536 struct mlxsw_sp_nexthop *nh)
1537{
Ido Schimmel9011b672017-05-16 19:38:25 +02001538 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_ht, &nh->ht_node,
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001539 mlxsw_sp_nexthop_ht_params);
1540}
1541
Ido Schimmelad178c82017-02-08 11:16:40 +01001542static struct mlxsw_sp_nexthop *
1543mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
1544 struct mlxsw_sp_nexthop_key key)
1545{
Ido Schimmel9011b672017-05-16 19:38:25 +02001546 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_ht, &key,
Ido Schimmelad178c82017-02-08 11:16:40 +01001547 mlxsw_sp_nexthop_ht_params);
1548}
1549
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001550static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001551 const struct mlxsw_sp_fib *fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001552 u32 adj_index, u16 ecmp_size,
1553 u32 new_adj_index,
1554 u16 new_ecmp_size)
1555{
1556 char raleu_pl[MLXSW_REG_RALEU_LEN];
1557
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001558 mlxsw_reg_raleu_pack(raleu_pl,
Ido Schimmel76610eb2017-03-10 08:53:41 +01001559 (enum mlxsw_reg_ralxx_protocol) fib->proto,
1560 fib->vr->id, adj_index, ecmp_size, new_adj_index,
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001561 new_ecmp_size);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001562 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1563}
1564
1565static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1566 struct mlxsw_sp_nexthop_group *nh_grp,
1567 u32 old_adj_index, u16 old_ecmp_size)
1568{
1569 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001570 struct mlxsw_sp_fib *fib = NULL;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001571 int err;
1572
1573 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
Ido Schimmel76610eb2017-03-10 08:53:41 +01001574 if (fib == fib_entry->fib_node->fib)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001575 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01001576 fib = fib_entry->fib_node->fib;
1577 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001578 old_adj_index,
1579 old_ecmp_size,
1580 nh_grp->adj_index,
1581 nh_grp->ecmp_size);
1582 if (err)
1583 return err;
1584 }
1585 return 0;
1586}
1587
1588static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1589 struct mlxsw_sp_nexthop *nh)
1590{
1591 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1592 char ratr_pl[MLXSW_REG_RATR_LEN];
1593
1594 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1595 true, adj_index, neigh_entry->rif);
1596 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1597 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1598}
1599
1600static int
1601mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
Ido Schimmela59b7e02017-01-23 11:11:42 +01001602 struct mlxsw_sp_nexthop_group *nh_grp,
1603 bool reallocate)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001604{
1605 u32 adj_index = nh_grp->adj_index; /* base */
1606 struct mlxsw_sp_nexthop *nh;
1607 int i;
1608 int err;
1609
1610 for (i = 0; i < nh_grp->count; i++) {
1611 nh = &nh_grp->nexthops[i];
1612
1613 if (!nh->should_offload) {
1614 nh->offloaded = 0;
1615 continue;
1616 }
1617
Ido Schimmela59b7e02017-01-23 11:11:42 +01001618 if (nh->update || reallocate) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001619 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1620 adj_index, nh);
1621 if (err)
1622 return err;
1623 nh->update = 0;
1624 nh->offloaded = 1;
1625 }
1626 adj_index++;
1627 }
1628 return 0;
1629}
1630
1631static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1632 struct mlxsw_sp_fib_entry *fib_entry);
1633
Ido Schimmel1819ae32017-07-21 18:04:28 +02001634static bool
1635mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
1636 const struct mlxsw_sp_fib_entry *fib_entry);
1637
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001638static int
1639mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1640 struct mlxsw_sp_nexthop_group *nh_grp)
1641{
1642 struct mlxsw_sp_fib_entry *fib_entry;
1643 int err;
1644
1645 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
Ido Schimmel1819ae32017-07-21 18:04:28 +02001646 if (!mlxsw_sp_fib_node_entry_is_first(fib_entry->fib_node,
1647 fib_entry))
1648 continue;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001649 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1650 if (err)
1651 return err;
1652 }
1653 return 0;
1654}
1655
1656static void
1657mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1658 struct mlxsw_sp_nexthop_group *nh_grp)
1659{
1660 struct mlxsw_sp_nexthop *nh;
1661 bool offload_change = false;
1662 u32 adj_index;
1663 u16 ecmp_size = 0;
1664 bool old_adj_index_valid;
1665 u32 old_adj_index;
1666 u16 old_ecmp_size;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001667 int i;
1668 int err;
1669
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001670 if (!nh_grp->gateway) {
1671 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1672 return;
1673 }
1674
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001675 for (i = 0; i < nh_grp->count; i++) {
1676 nh = &nh_grp->nexthops[i];
1677
Petr Machata56b8a9e2017-07-31 09:27:29 +02001678 if (nh->should_offload != nh->offloaded) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001679 offload_change = true;
1680 if (nh->should_offload)
1681 nh->update = 1;
1682 }
1683 if (nh->should_offload)
1684 ecmp_size++;
1685 }
1686 if (!offload_change) {
1687 /* Nothing was added or removed, so no need to reallocate. Just
1688 * update MAC on existing adjacency indexes.
1689 */
Ido Schimmela59b7e02017-01-23 11:11:42 +01001690 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp,
1691 false);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001692 if (err) {
1693 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1694 goto set_trap;
1695 }
1696 return;
1697 }
1698 if (!ecmp_size)
1699 /* No neigh of this group is connected so we just set
1700 * the trap and let everthing flow through kernel.
1701 */
1702 goto set_trap;
1703
Arkadi Sharshevsky13124442017-03-25 08:28:22 +01001704 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size, &adj_index);
1705 if (err) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001706 /* We ran out of KVD linear space, just set the
1707 * trap and let everything flow through kernel.
1708 */
1709 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1710 goto set_trap;
1711 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001712 old_adj_index_valid = nh_grp->adj_index_valid;
1713 old_adj_index = nh_grp->adj_index;
1714 old_ecmp_size = nh_grp->ecmp_size;
1715 nh_grp->adj_index_valid = 1;
1716 nh_grp->adj_index = adj_index;
1717 nh_grp->ecmp_size = ecmp_size;
Ido Schimmela59b7e02017-01-23 11:11:42 +01001718 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001719 if (err) {
1720 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1721 goto set_trap;
1722 }
1723
1724 if (!old_adj_index_valid) {
1725 /* The trap was set for fib entries, so we have to call
1726 * fib entry update to unset it and use adjacency index.
1727 */
1728 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1729 if (err) {
1730 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1731 goto set_trap;
1732 }
1733 return;
1734 }
1735
1736 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1737 old_adj_index, old_ecmp_size);
1738 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1739 if (err) {
1740 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1741 goto set_trap;
1742 }
1743 return;
1744
1745set_trap:
1746 old_adj_index_valid = nh_grp->adj_index_valid;
1747 nh_grp->adj_index_valid = 0;
1748 for (i = 0; i < nh_grp->count; i++) {
1749 nh = &nh_grp->nexthops[i];
1750 nh->offloaded = 0;
1751 }
1752 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1753 if (err)
1754 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1755 if (old_adj_index_valid)
1756 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1757}
1758
1759static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1760 bool removing)
1761{
Petr Machata213666a2017-07-31 09:27:30 +02001762 if (!removing)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001763 nh->should_offload = 1;
Petr Machata213666a2017-07-31 09:27:30 +02001764 else if (nh->offloaded)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001765 nh->should_offload = 0;
1766 nh->update = 1;
1767}
1768
1769static void
1770mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1771 struct mlxsw_sp_neigh_entry *neigh_entry,
1772 bool removing)
1773{
1774 struct mlxsw_sp_nexthop *nh;
1775
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001776 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1777 neigh_list_node) {
1778 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1779 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1780 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001781}
1782
Ido Schimmel9665b742017-02-08 11:16:42 +01001783static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001784 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001785{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001786 if (nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001787 return;
1788
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001789 nh->rif = rif;
1790 list_add(&nh->rif_list_node, &rif->nexthop_list);
Ido Schimmel9665b742017-02-08 11:16:42 +01001791}
1792
1793static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
1794{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001795 if (!nh->rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001796 return;
1797
1798 list_del(&nh->rif_list_node);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001799 nh->rif = NULL;
Ido Schimmel9665b742017-02-08 11:16:42 +01001800}
1801
Ido Schimmela8c97012017-02-08 11:16:35 +01001802static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
1803 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001804{
1805 struct mlxsw_sp_neigh_entry *neigh_entry;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001806 struct neighbour *n;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001807 u8 nud_state, dead;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001808 int err;
1809
Ido Schimmelad178c82017-02-08 11:16:40 +01001810 if (!nh->nh_grp->gateway || nh->neigh_entry)
Ido Schimmelb8399a12017-02-08 11:16:33 +01001811 return 0;
1812
Jiri Pirko33b13412016-11-10 12:31:04 +01001813 /* Take a reference of neigh here ensuring that neigh would
Petr Machata8de3c172017-07-31 09:27:25 +02001814 * not be destructed before the nexthop entry is finished.
Jiri Pirko33b13412016-11-10 12:31:04 +01001815 * The reference is taken either in neigh_lookup() or
Ido Schimmelfd76d912017-02-06 16:20:17 +01001816 * in neigh_create() in case n is not found.
Jiri Pirko33b13412016-11-10 12:31:04 +01001817 */
Ido Schimmel58adf2c2017-07-18 10:10:19 +02001818 n = neigh_lookup(nh->nh_grp->neigh_tbl, &nh->gw_addr, nh->rif->dev);
Jiri Pirko33b13412016-11-10 12:31:04 +01001819 if (!n) {
Ido Schimmel58adf2c2017-07-18 10:10:19 +02001820 n = neigh_create(nh->nh_grp->neigh_tbl, &nh->gw_addr,
1821 nh->rif->dev);
Ido Schimmela8c97012017-02-08 11:16:35 +01001822 if (IS_ERR(n))
1823 return PTR_ERR(n);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001824 neigh_event_send(n, NULL);
Jiri Pirko33b13412016-11-10 12:31:04 +01001825 }
1826 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1827 if (!neigh_entry) {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001828 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1829 if (IS_ERR(neigh_entry)) {
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001830 err = -EINVAL;
1831 goto err_neigh_entry_create;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001832 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001833 }
Yotam Gigib2157142016-07-05 11:27:51 +02001834
1835 /* If that is the first nexthop connected to that neigh, add to
1836 * nexthop_neighs_list
1837 */
1838 if (list_empty(&neigh_entry->nexthop_list))
1839 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
Ido Schimmel9011b672017-05-16 19:38:25 +02001840 &mlxsw_sp->router->nexthop_neighs_list);
Yotam Gigib2157142016-07-05 11:27:51 +02001841
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001842 nh->neigh_entry = neigh_entry;
1843 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1844 read_lock_bh(&n->lock);
1845 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001846 dead = n->dead;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001847 read_unlock_bh(&n->lock);
Ido Schimmel93a87e52016-12-23 09:32:49 +01001848 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001849
1850 return 0;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001851
1852err_neigh_entry_create:
1853 neigh_release(n);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001854 return err;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001855}
1856
Ido Schimmela8c97012017-02-08 11:16:35 +01001857static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
1858 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001859{
1860 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01001861 struct neighbour *n;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001862
Ido Schimmelb8399a12017-02-08 11:16:33 +01001863 if (!neigh_entry)
Ido Schimmela8c97012017-02-08 11:16:35 +01001864 return;
1865 n = neigh_entry->key.n;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001866
Ido Schimmel58312122016-12-23 09:32:50 +01001867 __mlxsw_sp_nexthop_neigh_update(nh, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001868 list_del(&nh->neigh_list_node);
Ido Schimmele58be792017-02-08 11:16:28 +01001869 nh->neigh_entry = NULL;
Yotam Gigib2157142016-07-05 11:27:51 +02001870
1871 /* If that is the last nexthop connected to that neigh, remove from
1872 * nexthop_neighs_list
1873 */
Ido Schimmele58be792017-02-08 11:16:28 +01001874 if (list_empty(&neigh_entry->nexthop_list))
1875 list_del(&neigh_entry->nexthop_neighs_list_node);
Yotam Gigib2157142016-07-05 11:27:51 +02001876
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001877 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1878 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1879
1880 neigh_release(n);
Ido Schimmela8c97012017-02-08 11:16:35 +01001881}
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001882
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02001883static int mlxsw_sp_nexthop4_init(struct mlxsw_sp *mlxsw_sp,
1884 struct mlxsw_sp_nexthop_group *nh_grp,
1885 struct mlxsw_sp_nexthop *nh,
1886 struct fib_nh *fib_nh)
Ido Schimmela8c97012017-02-08 11:16:35 +01001887{
1888 struct net_device *dev = fib_nh->nh_dev;
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001889 struct in_device *in_dev;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001890 struct mlxsw_sp_rif *rif;
Ido Schimmela8c97012017-02-08 11:16:35 +01001891 int err;
1892
1893 nh->nh_grp = nh_grp;
1894 nh->key.fib_nh = fib_nh;
Ido Schimmel58adf2c2017-07-18 10:10:19 +02001895 memcpy(&nh->gw_addr, &fib_nh->nh_gw, sizeof(fib_nh->nh_gw));
Ido Schimmela8c97012017-02-08 11:16:35 +01001896 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
1897 if (err)
1898 return err;
1899
Ido Schimmel97989ee2017-03-10 08:53:38 +01001900 if (!dev)
1901 return 0;
1902
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01001903 in_dev = __in_dev_get_rtnl(dev);
1904 if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1905 fib_nh->nh_flags & RTNH_F_LINKDOWN)
1906 return 0;
1907
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001908 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
1909 if (!rif)
Ido Schimmela8c97012017-02-08 11:16:35 +01001910 return 0;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001911 mlxsw_sp_nexthop_rif_init(nh, rif);
Ido Schimmela8c97012017-02-08 11:16:35 +01001912
1913 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1914 if (err)
1915 goto err_nexthop_neigh_init;
1916
1917 return 0;
1918
1919err_nexthop_neigh_init:
Ido Schimmela4e75b72017-07-12 09:12:52 +02001920 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmela8c97012017-02-08 11:16:35 +01001921 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1922 return err;
1923}
1924
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02001925static void mlxsw_sp_nexthop4_fini(struct mlxsw_sp *mlxsw_sp,
1926 struct mlxsw_sp_nexthop *nh)
Ido Schimmela8c97012017-02-08 11:16:35 +01001927{
1928 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001929 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001930 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001931}
1932
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02001933static void mlxsw_sp_nexthop4_event(struct mlxsw_sp *mlxsw_sp,
1934 unsigned long event, struct fib_nh *fib_nh)
Ido Schimmelad178c82017-02-08 11:16:40 +01001935{
1936 struct mlxsw_sp_nexthop_key key;
1937 struct mlxsw_sp_nexthop *nh;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001938 struct mlxsw_sp_rif *rif;
Ido Schimmelad178c82017-02-08 11:16:40 +01001939
Ido Schimmel9011b672017-05-16 19:38:25 +02001940 if (mlxsw_sp->router->aborted)
Ido Schimmelad178c82017-02-08 11:16:40 +01001941 return;
1942
1943 key.fib_nh = fib_nh;
1944 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
1945 if (WARN_ON_ONCE(!nh))
1946 return;
1947
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001948 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fib_nh->nh_dev);
1949 if (!rif)
Ido Schimmelad178c82017-02-08 11:16:40 +01001950 return;
1951
1952 switch (event) {
1953 case FIB_EVENT_NH_ADD:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001954 mlxsw_sp_nexthop_rif_init(nh, rif);
Ido Schimmelad178c82017-02-08 11:16:40 +01001955 mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1956 break;
1957 case FIB_EVENT_NH_DEL:
1958 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001959 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01001960 break;
1961 }
1962
1963 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1964}
1965
Ido Schimmel9665b742017-02-08 11:16:42 +01001966static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001967 struct mlxsw_sp_rif *rif)
Ido Schimmel9665b742017-02-08 11:16:42 +01001968{
1969 struct mlxsw_sp_nexthop *nh, *tmp;
1970
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01001971 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
Ido Schimmel9665b742017-02-08 11:16:42 +01001972 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
1973 mlxsw_sp_nexthop_rif_fini(nh);
1974 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1975 }
1976}
1977
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001978static struct mlxsw_sp_nexthop_group *
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02001979mlxsw_sp_nexthop4_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001980{
1981 struct mlxsw_sp_nexthop_group *nh_grp;
1982 struct mlxsw_sp_nexthop *nh;
1983 struct fib_nh *fib_nh;
1984 size_t alloc_size;
1985 int i;
1986 int err;
1987
1988 alloc_size = sizeof(*nh_grp) +
1989 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1990 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1991 if (!nh_grp)
1992 return ERR_PTR(-ENOMEM);
1993 INIT_LIST_HEAD(&nh_grp->fib_list);
Ido Schimmel58adf2c2017-07-18 10:10:19 +02001994 nh_grp->neigh_tbl = &arp_tbl;
1995
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001996 nh_grp->gateway = fi->fib_nh->nh_scope == RT_SCOPE_LINK;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001997 nh_grp->count = fi->fib_nhs;
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001998 nh_grp->key.fi = fi;
Ido Schimmel7387dbb2017-07-12 09:12:53 +02001999 fib_info_hold(fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002000 for (i = 0; i < nh_grp->count; i++) {
2001 nh = &nh_grp->nexthops[i];
2002 fib_nh = &fi->fib_nh[i];
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002003 err = mlxsw_sp_nexthop4_init(mlxsw_sp, nh_grp, nh, fib_nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002004 if (err)
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002005 goto err_nexthop4_init;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002006 }
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002007 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
2008 if (err)
2009 goto err_nexthop_group_insert;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002010 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
2011 return nh_grp;
2012
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002013err_nexthop_group_insert:
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002014err_nexthop4_init:
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01002015 for (i--; i >= 0; i--) {
2016 nh = &nh_grp->nexthops[i];
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002017 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
Ido Schimmeldf6dd79b2017-02-08 14:36:49 +01002018 }
Ido Schimmel7387dbb2017-07-12 09:12:53 +02002019 fib_info_put(nh_grp->key.fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002020 kfree(nh_grp);
2021 return ERR_PTR(err);
2022}
2023
2024static void
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002025mlxsw_sp_nexthop4_group_destroy(struct mlxsw_sp *mlxsw_sp,
2026 struct mlxsw_sp_nexthop_group *nh_grp)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002027{
2028 struct mlxsw_sp_nexthop *nh;
2029 int i;
2030
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002031 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002032 for (i = 0; i < nh_grp->count; i++) {
2033 nh = &nh_grp->nexthops[i];
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002034 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002035 }
Ido Schimmel58312122016-12-23 09:32:50 +01002036 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
2037 WARN_ON_ONCE(nh_grp->adj_index_valid);
Ido Schimmel7387dbb2017-07-12 09:12:53 +02002038 fib_info_put(nh_grp->key.fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002039 kfree(nh_grp);
2040}
2041
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002042static int mlxsw_sp_nexthop4_group_get(struct mlxsw_sp *mlxsw_sp,
2043 struct mlxsw_sp_fib_entry *fib_entry,
2044 struct fib_info *fi)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002045{
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002046 struct mlxsw_sp_nexthop_group_key key;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002047 struct mlxsw_sp_nexthop_group *nh_grp;
2048
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002049 key.fi = fi;
2050 nh_grp = mlxsw_sp_nexthop_group_lookup(mlxsw_sp, key);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002051 if (!nh_grp) {
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002052 nh_grp = mlxsw_sp_nexthop4_group_create(mlxsw_sp, fi);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002053 if (IS_ERR(nh_grp))
2054 return PTR_ERR(nh_grp);
2055 }
2056 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
2057 fib_entry->nh_group = nh_grp;
2058 return 0;
2059}
2060
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002061static void mlxsw_sp_nexthop4_group_put(struct mlxsw_sp *mlxsw_sp,
2062 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002063{
2064 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
2065
2066 list_del(&fib_entry->nexthop_group_node);
2067 if (!list_empty(&nh_grp->fib_list))
2068 return;
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002069 mlxsw_sp_nexthop4_group_destroy(mlxsw_sp, nh_grp);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002070}
2071
Ido Schimmel013b20f2017-02-08 11:16:36 +01002072static bool
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002073mlxsw_sp_fib4_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
2074{
2075 struct mlxsw_sp_fib4_entry *fib4_entry;
2076
2077 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
2078 common);
2079 return !fib4_entry->tos;
2080}
2081
2082static bool
Ido Schimmel013b20f2017-02-08 11:16:36 +01002083mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
2084{
2085 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
2086
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002087 switch (fib_entry->fib_node->fib->proto) {
2088 case MLXSW_SP_L3_PROTO_IPV4:
2089 if (!mlxsw_sp_fib4_entry_should_offload(fib_entry))
2090 return false;
2091 break;
2092 case MLXSW_SP_L3_PROTO_IPV6:
2093 break;
2094 }
Ido Schimmel9aecce12017-02-09 10:28:42 +01002095
Ido Schimmel013b20f2017-02-08 11:16:36 +01002096 switch (fib_entry->type) {
2097 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
2098 return !!nh_group->adj_index_valid;
2099 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
Ido Schimmel70ad3502017-02-08 11:16:38 +01002100 return !!nh_group->nh_rif;
Ido Schimmel013b20f2017-02-08 11:16:36 +01002101 default:
2102 return false;
2103 }
2104}
2105
Ido Schimmel3984d1a2017-08-02 09:56:03 +02002106static void
2107mlxsw_sp_fib4_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
2108{
2109 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
2110 int i;
2111
2112 if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_LOCAL) {
2113 nh_grp->nexthops->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
2114 return;
2115 }
2116
2117 for (i = 0; i < nh_grp->count; i++) {
2118 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
2119
2120 if (nh->offloaded)
2121 nh->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
2122 else
2123 nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
2124 }
2125}
2126
2127static void
2128mlxsw_sp_fib4_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
2129{
2130 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
2131 int i;
2132
2133 for (i = 0; i < nh_grp->count; i++) {
2134 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
2135
2136 nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
2137 }
2138}
2139
Ido Schimmel013b20f2017-02-08 11:16:36 +01002140static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
2141{
2142 fib_entry->offloaded = true;
2143
Ido Schimmel76610eb2017-03-10 08:53:41 +01002144 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01002145 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel3984d1a2017-08-02 09:56:03 +02002146 mlxsw_sp_fib4_entry_offload_set(fib_entry);
Ido Schimmel013b20f2017-02-08 11:16:36 +01002147 break;
2148 case MLXSW_SP_L3_PROTO_IPV6:
2149 WARN_ON_ONCE(1);
2150 }
2151}
2152
2153static void
2154mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
2155{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002156 switch (fib_entry->fib_node->fib->proto) {
Ido Schimmel013b20f2017-02-08 11:16:36 +01002157 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel3984d1a2017-08-02 09:56:03 +02002158 mlxsw_sp_fib4_entry_offload_unset(fib_entry);
Ido Schimmel013b20f2017-02-08 11:16:36 +01002159 break;
2160 case MLXSW_SP_L3_PROTO_IPV6:
2161 WARN_ON_ONCE(1);
2162 }
2163
2164 fib_entry->offloaded = false;
2165}
2166
2167static void
2168mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
2169 enum mlxsw_reg_ralue_op op, int err)
2170{
2171 switch (op) {
2172 case MLXSW_REG_RALUE_OP_WRITE_DELETE:
2173 if (!fib_entry->offloaded)
2174 return;
2175 return mlxsw_sp_fib_entry_offload_unset(fib_entry);
2176 case MLXSW_REG_RALUE_OP_WRITE_WRITE:
2177 if (err)
2178 return;
2179 if (mlxsw_sp_fib_entry_should_offload(fib_entry) &&
2180 !fib_entry->offloaded)
2181 mlxsw_sp_fib_entry_offload_set(fib_entry);
2182 else if (!mlxsw_sp_fib_entry_should_offload(fib_entry) &&
2183 fib_entry->offloaded)
2184 mlxsw_sp_fib_entry_offload_unset(fib_entry);
2185 return;
2186 default:
2187 return;
2188 }
2189}
2190
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002191static void
2192mlxsw_sp_fib_entry_ralue_pack(char *ralue_pl,
2193 const struct mlxsw_sp_fib_entry *fib_entry,
2194 enum mlxsw_reg_ralue_op op)
2195{
2196 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
2197 enum mlxsw_reg_ralxx_protocol proto;
2198 u32 *p_dip;
2199
2200 proto = (enum mlxsw_reg_ralxx_protocol) fib->proto;
2201
2202 switch (fib->proto) {
2203 case MLXSW_SP_L3_PROTO_IPV4:
2204 p_dip = (u32 *) fib_entry->fib_node->key.addr;
2205 mlxsw_reg_ralue_pack4(ralue_pl, proto, op, fib->vr->id,
2206 fib_entry->fib_node->key.prefix_len,
2207 *p_dip);
2208 break;
2209 case MLXSW_SP_L3_PROTO_IPV6:
2210 mlxsw_reg_ralue_pack6(ralue_pl, proto, op, fib->vr->id,
2211 fib_entry->fib_node->key.prefix_len,
2212 fib_entry->fib_node->key.addr);
2213 break;
2214 }
2215}
2216
2217static int mlxsw_sp_fib_entry_op_remote(struct mlxsw_sp *mlxsw_sp,
2218 struct mlxsw_sp_fib_entry *fib_entry,
2219 enum mlxsw_reg_ralue_op op)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002220{
2221 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002222 enum mlxsw_reg_ralue_trap_action trap_action;
2223 u16 trap_id = 0;
2224 u32 adjacency_index = 0;
2225 u16 ecmp_size = 0;
2226
2227 /* In case the nexthop group adjacency index is valid, use it
2228 * with provided ECMP size. Otherwise, setup trap and pass
2229 * traffic to kernel.
2230 */
Ido Schimmel4b411472017-02-08 11:16:37 +01002231 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002232 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
2233 adjacency_index = fib_entry->nh_group->adj_index;
2234 ecmp_size = fib_entry->nh_group->ecmp_size;
2235 } else {
2236 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
2237 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
2238 }
2239
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002240 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002241 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
2242 adjacency_index, ecmp_size);
2243 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
2244}
2245
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002246static int mlxsw_sp_fib_entry_op_local(struct mlxsw_sp *mlxsw_sp,
2247 struct mlxsw_sp_fib_entry *fib_entry,
2248 enum mlxsw_reg_ralue_op op)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002249{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002250 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nh_rif;
Ido Schimmel70ad3502017-02-08 11:16:38 +01002251 enum mlxsw_reg_ralue_trap_action trap_action;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002252 char ralue_pl[MLXSW_REG_RALUE_LEN];
Ido Schimmel70ad3502017-02-08 11:16:38 +01002253 u16 trap_id = 0;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002254 u16 rif_index = 0;
Ido Schimmel70ad3502017-02-08 11:16:38 +01002255
2256 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
2257 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002258 rif_index = rif->rif_index;
Ido Schimmel70ad3502017-02-08 11:16:38 +01002259 } else {
2260 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
2261 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
2262 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02002263
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002264 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01002265 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id,
2266 rif_index);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002267 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
2268}
2269
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002270static int mlxsw_sp_fib_entry_op_trap(struct mlxsw_sp *mlxsw_sp,
2271 struct mlxsw_sp_fib_entry *fib_entry,
2272 enum mlxsw_reg_ralue_op op)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002273{
2274 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirko61c503f2016-07-04 08:23:11 +02002275
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002276 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002277 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
2278 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
2279}
2280
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002281static int __mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
2282 struct mlxsw_sp_fib_entry *fib_entry,
2283 enum mlxsw_reg_ralue_op op)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002284{
2285 switch (fib_entry->type) {
2286 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002287 return mlxsw_sp_fib_entry_op_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002288 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002289 return mlxsw_sp_fib_entry_op_local(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002290 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002291 return mlxsw_sp_fib_entry_op_trap(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002292 }
2293 return -EINVAL;
2294}
2295
2296static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
2297 struct mlxsw_sp_fib_entry *fib_entry,
2298 enum mlxsw_reg_ralue_op op)
2299{
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002300 int err = __mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
Ido Schimmel013b20f2017-02-08 11:16:36 +01002301
Ido Schimmel013b20f2017-02-08 11:16:36 +01002302 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
Ido Schimmel9dbf4d72017-07-18 10:10:24 +02002303
Ido Schimmel013b20f2017-02-08 11:16:36 +01002304 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002305}
2306
2307static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
2308 struct mlxsw_sp_fib_entry *fib_entry)
2309{
Jiri Pirko7146da32016-09-01 10:37:41 +02002310 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
2311 MLXSW_REG_RALUE_OP_WRITE_WRITE);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002312}
2313
2314static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
2315 struct mlxsw_sp_fib_entry *fib_entry)
2316{
2317 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
2318 MLXSW_REG_RALUE_OP_WRITE_DELETE);
2319}
2320
Jiri Pirko61c503f2016-07-04 08:23:11 +02002321static int
Ido Schimmel013b20f2017-02-08 11:16:36 +01002322mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
2323 const struct fib_entry_notifier_info *fen_info,
2324 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002325{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002326 struct fib_info *fi = fen_info->fi;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002327
Ido Schimmel97989ee2017-03-10 08:53:38 +01002328 switch (fen_info->type) {
2329 case RTN_BROADCAST: /* fall through */
2330 case RTN_LOCAL:
Jiri Pirko61c503f2016-07-04 08:23:11 +02002331 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
2332 return 0;
Ido Schimmel97989ee2017-03-10 08:53:38 +01002333 case RTN_UNREACHABLE: /* fall through */
2334 case RTN_BLACKHOLE: /* fall through */
2335 case RTN_PROHIBIT:
2336 /* Packets hitting these routes need to be trapped, but
2337 * can do so with a lower priority than packets directed
2338 * at the host, so use action type local instead of trap.
2339 */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002340 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Ido Schimmel97989ee2017-03-10 08:53:38 +01002341 return 0;
2342 case RTN_UNICAST:
2343 if (fi->fib_nh->nh_scope != RT_SCOPE_LINK)
2344 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
2345 else
2346 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
2347 return 0;
2348 default:
2349 return -EINVAL;
2350 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02002351}
2352
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002353static struct mlxsw_sp_fib4_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01002354mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
2355 struct mlxsw_sp_fib_node *fib_node,
2356 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02002357{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002358 struct mlxsw_sp_fib4_entry *fib4_entry;
Jiri Pirko5b004412016-09-01 10:37:40 +02002359 struct mlxsw_sp_fib_entry *fib_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002360 int err;
2361
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002362 fib4_entry = kzalloc(sizeof(*fib4_entry), GFP_KERNEL);
2363 if (!fib4_entry)
2364 return ERR_PTR(-ENOMEM);
2365 fib_entry = &fib4_entry->common;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002366
2367 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
2368 if (err)
2369 goto err_fib4_entry_type_set;
2370
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002371 err = mlxsw_sp_nexthop4_group_get(mlxsw_sp, fib_entry, fen_info->fi);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002372 if (err)
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002373 goto err_nexthop4_group_get;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002374
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002375 fib4_entry->prio = fen_info->fi->fib_priority;
2376 fib4_entry->tb_id = fen_info->tb_id;
2377 fib4_entry->type = fen_info->type;
2378 fib4_entry->tos = fen_info->tos;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002379
2380 fib_entry->fib_node = fib_node;
2381
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002382 return fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002383
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002384err_nexthop4_group_get:
Ido Schimmel9aecce12017-02-09 10:28:42 +01002385err_fib4_entry_type_set:
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002386 kfree(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002387 return ERR_PTR(err);
2388}
2389
2390static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002391 struct mlxsw_sp_fib4_entry *fib4_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002392{
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02002393 mlxsw_sp_nexthop4_group_put(mlxsw_sp, &fib4_entry->common);
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002394 kfree(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002395}
2396
2397static struct mlxsw_sp_fib_node *
Ido Schimmel160e22a2017-07-18 10:10:20 +02002398mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
2399 size_t addr_len, unsigned char prefix_len);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002400
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002401static struct mlxsw_sp_fib4_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01002402mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
2403 const struct fib_entry_notifier_info *fen_info)
2404{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002405 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002406 struct mlxsw_sp_fib_node *fib_node;
Ido Schimmel160e22a2017-07-18 10:10:20 +02002407 struct mlxsw_sp_fib *fib;
2408 struct mlxsw_sp_vr *vr;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002409
Ido Schimmel160e22a2017-07-18 10:10:20 +02002410 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id);
2411 if (!vr)
2412 return NULL;
2413 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
2414
2415 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
2416 sizeof(fen_info->dst),
2417 fen_info->dst_len);
2418 if (!fib_node)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002419 return NULL;
2420
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002421 list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
2422 if (fib4_entry->tb_id == fen_info->tb_id &&
2423 fib4_entry->tos == fen_info->tos &&
2424 fib4_entry->type == fen_info->type &&
2425 fib4_entry->common.nh_group->key.fi == fen_info->fi) {
2426 return fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002427 }
2428 }
2429
2430 return NULL;
2431}
2432
2433static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
2434 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
2435 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
2436 .key_len = sizeof(struct mlxsw_sp_fib_key),
2437 .automatic_shrinking = true,
2438};
2439
2440static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
2441 struct mlxsw_sp_fib_node *fib_node)
2442{
2443 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
2444 mlxsw_sp_fib_ht_params);
2445}
2446
2447static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
2448 struct mlxsw_sp_fib_node *fib_node)
2449{
2450 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
2451 mlxsw_sp_fib_ht_params);
2452}
2453
2454static struct mlxsw_sp_fib_node *
2455mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
2456 size_t addr_len, unsigned char prefix_len)
2457{
2458 struct mlxsw_sp_fib_key key;
2459
2460 memset(&key, 0, sizeof(key));
2461 memcpy(key.addr, addr, addr_len);
2462 key.prefix_len = prefix_len;
2463 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
2464}
2465
2466static struct mlxsw_sp_fib_node *
Ido Schimmel76610eb2017-03-10 08:53:41 +01002467mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
Ido Schimmel9aecce12017-02-09 10:28:42 +01002468 size_t addr_len, unsigned char prefix_len)
2469{
2470 struct mlxsw_sp_fib_node *fib_node;
2471
2472 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
2473 if (!fib_node)
2474 return NULL;
2475
2476 INIT_LIST_HEAD(&fib_node->entry_list);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002477 list_add(&fib_node->list, &fib->node_list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002478 memcpy(fib_node->key.addr, addr, addr_len);
2479 fib_node->key.prefix_len = prefix_len;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002480
2481 return fib_node;
2482}
2483
2484static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
2485{
Ido Schimmel9aecce12017-02-09 10:28:42 +01002486 list_del(&fib_node->list);
2487 WARN_ON(!list_empty(&fib_node->entry_list));
2488 kfree(fib_node);
2489}
2490
2491static bool
2492mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
2493 const struct mlxsw_sp_fib_entry *fib_entry)
2494{
2495 return list_first_entry(&fib_node->entry_list,
2496 struct mlxsw_sp_fib_entry, list) == fib_entry;
2497}
2498
2499static void mlxsw_sp_fib_node_prefix_inc(struct mlxsw_sp_fib_node *fib_node)
2500{
2501 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002502 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002503
2504 if (fib->prefix_ref_count[prefix_len]++ == 0)
2505 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
2506}
2507
2508static void mlxsw_sp_fib_node_prefix_dec(struct mlxsw_sp_fib_node *fib_node)
2509{
2510 unsigned char prefix_len = fib_node->key.prefix_len;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002511 struct mlxsw_sp_fib *fib = fib_node->fib;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002512
2513 if (--fib->prefix_ref_count[prefix_len] == 0)
2514 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
2515}
2516
Ido Schimmel76610eb2017-03-10 08:53:41 +01002517static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
2518 struct mlxsw_sp_fib_node *fib_node,
2519 struct mlxsw_sp_fib *fib)
2520{
2521 struct mlxsw_sp_prefix_usage req_prefix_usage;
2522 struct mlxsw_sp_lpm_tree *lpm_tree;
2523 int err;
2524
2525 err = mlxsw_sp_fib_node_insert(fib, fib_node);
2526 if (err)
2527 return err;
2528 fib_node->fib = fib;
2529
2530 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &fib->prefix_usage);
2531 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
2532
2533 if (!mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2534 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib,
2535 &req_prefix_usage);
2536 if (err)
2537 goto err_tree_check;
2538 } else {
2539 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
2540 fib->proto);
2541 if (IS_ERR(lpm_tree))
2542 return PTR_ERR(lpm_tree);
2543 fib->lpm_tree = lpm_tree;
2544 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib);
2545 if (err)
2546 goto err_tree_bind;
2547 }
2548
2549 mlxsw_sp_fib_node_prefix_inc(fib_node);
2550
2551 return 0;
2552
2553err_tree_bind:
2554 fib->lpm_tree = NULL;
2555 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2556err_tree_check:
2557 fib_node->fib = NULL;
2558 mlxsw_sp_fib_node_remove(fib, fib_node);
2559 return err;
2560}
2561
2562static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
2563 struct mlxsw_sp_fib_node *fib_node)
2564{
2565 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
2566 struct mlxsw_sp_fib *fib = fib_node->fib;
2567
2568 mlxsw_sp_fib_node_prefix_dec(fib_node);
2569
2570 if (mlxsw_sp_prefix_usage_none(&fib->prefix_usage)) {
2571 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
2572 fib->lpm_tree = NULL;
2573 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
2574 } else {
2575 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, fib, &fib->prefix_usage);
2576 }
2577
2578 fib_node->fib = NULL;
2579 mlxsw_sp_fib_node_remove(fib, fib_node);
2580}
2581
Ido Schimmel9aecce12017-02-09 10:28:42 +01002582static struct mlxsw_sp_fib_node *
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002583mlxsw_sp_fib_node_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id, const void *addr,
2584 size_t addr_len, unsigned char prefix_len,
2585 enum mlxsw_sp_l3proto proto)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002586{
2587 struct mlxsw_sp_fib_node *fib_node;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002588 struct mlxsw_sp_fib *fib;
Jiri Pirko5b004412016-09-01 10:37:40 +02002589 struct mlxsw_sp_vr *vr;
2590 int err;
2591
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002592 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id);
Jiri Pirko5b004412016-09-01 10:37:40 +02002593 if (IS_ERR(vr))
2594 return ERR_CAST(vr);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002595 fib = mlxsw_sp_vr_fib(vr, proto);
Jiri Pirko5b004412016-09-01 10:37:40 +02002596
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002597 fib_node = mlxsw_sp_fib_node_lookup(fib, addr, addr_len, prefix_len);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002598 if (fib_node)
2599 return fib_node;
2600
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002601 fib_node = mlxsw_sp_fib_node_create(fib, addr, addr_len, prefix_len);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002602 if (!fib_node) {
Jiri Pirko5b004412016-09-01 10:37:40 +02002603 err = -ENOMEM;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002604 goto err_fib_node_create;
Jiri Pirko5b004412016-09-01 10:37:40 +02002605 }
Jiri Pirko5b004412016-09-01 10:37:40 +02002606
Ido Schimmel76610eb2017-03-10 08:53:41 +01002607 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
2608 if (err)
2609 goto err_fib_node_init;
2610
Ido Schimmel9aecce12017-02-09 10:28:42 +01002611 return fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002612
Ido Schimmel76610eb2017-03-10 08:53:41 +01002613err_fib_node_init:
2614 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002615err_fib_node_create:
Ido Schimmel76610eb2017-03-10 08:53:41 +01002616 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002617 return ERR_PTR(err);
2618}
2619
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002620static void mlxsw_sp_fib_node_put(struct mlxsw_sp *mlxsw_sp,
2621 struct mlxsw_sp_fib_node *fib_node)
Jiri Pirko5b004412016-09-01 10:37:40 +02002622{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002623 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
Jiri Pirko5b004412016-09-01 10:37:40 +02002624
Ido Schimmel9aecce12017-02-09 10:28:42 +01002625 if (!list_empty(&fib_node->entry_list))
2626 return;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002627 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002628 mlxsw_sp_fib_node_destroy(fib_node);
Ido Schimmel76610eb2017-03-10 08:53:41 +01002629 mlxsw_sp_vr_put(vr);
Jiri Pirko5b004412016-09-01 10:37:40 +02002630}
2631
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002632static struct mlxsw_sp_fib4_entry *
Ido Schimmel9aecce12017-02-09 10:28:42 +01002633mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002634 const struct mlxsw_sp_fib4_entry *new4_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002635{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002636 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002637
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002638 list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
2639 if (fib4_entry->tb_id > new4_entry->tb_id)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002640 continue;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002641 if (fib4_entry->tb_id != new4_entry->tb_id)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002642 break;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002643 if (fib4_entry->tos > new4_entry->tos)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002644 continue;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002645 if (fib4_entry->prio >= new4_entry->prio ||
2646 fib4_entry->tos < new4_entry->tos)
2647 return fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002648 }
2649
2650 return NULL;
2651}
2652
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002653static int
2654mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib4_entry *fib4_entry,
2655 struct mlxsw_sp_fib4_entry *new4_entry)
Ido Schimmel4283bce2017-02-09 10:28:43 +01002656{
2657 struct mlxsw_sp_fib_node *fib_node;
2658
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002659 if (WARN_ON(!fib4_entry))
Ido Schimmel4283bce2017-02-09 10:28:43 +01002660 return -EINVAL;
2661
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002662 fib_node = fib4_entry->common.fib_node;
2663 list_for_each_entry_from(fib4_entry, &fib_node->entry_list,
2664 common.list) {
2665 if (fib4_entry->tb_id != new4_entry->tb_id ||
2666 fib4_entry->tos != new4_entry->tos ||
2667 fib4_entry->prio != new4_entry->prio)
Ido Schimmel4283bce2017-02-09 10:28:43 +01002668 break;
2669 }
2670
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002671 list_add_tail(&new4_entry->common.list, &fib4_entry->common.list);
Ido Schimmel4283bce2017-02-09 10:28:43 +01002672 return 0;
2673}
2674
Ido Schimmel9aecce12017-02-09 10:28:42 +01002675static int
Ido Schimmel9efbee62017-07-18 10:10:28 +02002676mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib4_entry *new4_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002677 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002678{
Ido Schimmel9efbee62017-07-18 10:10:28 +02002679 struct mlxsw_sp_fib_node *fib_node = new4_entry->common.fib_node;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002680 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002681
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002682 fib4_entry = mlxsw_sp_fib4_node_entry_find(fib_node, new4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002683
Ido Schimmel4283bce2017-02-09 10:28:43 +01002684 if (append)
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002685 return mlxsw_sp_fib4_node_list_append(fib4_entry, new4_entry);
2686 if (replace && WARN_ON(!fib4_entry))
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002687 return -EINVAL;
Ido Schimmel4283bce2017-02-09 10:28:43 +01002688
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002689 /* Insert new entry before replaced one, so that we can later
2690 * remove the second.
2691 */
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002692 if (fib4_entry) {
2693 list_add_tail(&new4_entry->common.list,
2694 &fib4_entry->common.list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002695 } else {
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002696 struct mlxsw_sp_fib4_entry *last;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002697
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002698 list_for_each_entry(last, &fib_node->entry_list, common.list) {
2699 if (new4_entry->tb_id > last->tb_id)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002700 break;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002701 fib4_entry = last;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002702 }
2703
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002704 if (fib4_entry)
2705 list_add(&new4_entry->common.list,
2706 &fib4_entry->common.list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002707 else
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002708 list_add(&new4_entry->common.list,
2709 &fib_node->entry_list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002710 }
2711
2712 return 0;
2713}
2714
2715static void
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002716mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib4_entry *fib4_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002717{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002718 list_del(&fib4_entry->common.list);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002719}
2720
Ido Schimmel80c238f2017-07-18 10:10:29 +02002721static int mlxsw_sp_fib_node_entry_add(struct mlxsw_sp *mlxsw_sp,
2722 struct mlxsw_sp_fib_entry *fib_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002723{
Ido Schimmel9efbee62017-07-18 10:10:28 +02002724 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2725
Ido Schimmel9aecce12017-02-09 10:28:42 +01002726 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2727 return 0;
2728
2729 /* To prevent packet loss, overwrite the previously offloaded
2730 * entry.
2731 */
2732 if (!list_is_singular(&fib_node->entry_list)) {
2733 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2734 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2735
2736 mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
2737 }
2738
2739 return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
2740}
2741
Ido Schimmel80c238f2017-07-18 10:10:29 +02002742static void mlxsw_sp_fib_node_entry_del(struct mlxsw_sp *mlxsw_sp,
2743 struct mlxsw_sp_fib_entry *fib_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002744{
Ido Schimmel9efbee62017-07-18 10:10:28 +02002745 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
2746
Ido Schimmel9aecce12017-02-09 10:28:42 +01002747 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
2748 return;
2749
2750 /* Promote the next entry by overwriting the deleted entry */
2751 if (!list_is_singular(&fib_node->entry_list)) {
2752 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
2753 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
2754
2755 mlxsw_sp_fib_entry_update(mlxsw_sp, n);
2756 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
2757 return;
2758 }
2759
2760 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
2761}
2762
2763static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002764 struct mlxsw_sp_fib4_entry *fib4_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002765 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002766{
Ido Schimmel9aecce12017-02-09 10:28:42 +01002767 int err;
2768
Ido Schimmel9efbee62017-07-18 10:10:28 +02002769 err = mlxsw_sp_fib4_node_list_insert(fib4_entry, replace, append);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002770 if (err)
2771 return err;
2772
Ido Schimmel80c238f2017-07-18 10:10:29 +02002773 err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib4_entry->common);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002774 if (err)
Ido Schimmel80c238f2017-07-18 10:10:29 +02002775 goto err_fib_node_entry_add;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002776
Ido Schimmel9aecce12017-02-09 10:28:42 +01002777 return 0;
2778
Ido Schimmel80c238f2017-07-18 10:10:29 +02002779err_fib_node_entry_add:
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002780 mlxsw_sp_fib4_node_list_remove(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002781 return err;
2782}
2783
2784static void
2785mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002786 struct mlxsw_sp_fib4_entry *fib4_entry)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002787{
Ido Schimmel80c238f2017-07-18 10:10:29 +02002788 mlxsw_sp_fib_node_entry_del(mlxsw_sp, &fib4_entry->common);
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002789 mlxsw_sp_fib4_node_list_remove(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002790}
2791
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002792static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002793 struct mlxsw_sp_fib4_entry *fib4_entry,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002794 bool replace)
2795{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002796 struct mlxsw_sp_fib_node *fib_node = fib4_entry->common.fib_node;
2797 struct mlxsw_sp_fib4_entry *replaced;
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002798
2799 if (!replace)
2800 return;
2801
2802 /* We inserted the new entry before replaced one */
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002803 replaced = list_next_entry(fib4_entry, common.list);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002804
2805 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, replaced);
2806 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, replaced);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002807 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002808}
2809
Ido Schimmel9aecce12017-02-09 10:28:42 +01002810static int
2811mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
Ido Schimmel4283bce2017-02-09 10:28:43 +01002812 const struct fib_entry_notifier_info *fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002813 bool replace, bool append)
Ido Schimmel9aecce12017-02-09 10:28:42 +01002814{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002815 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002816 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002817 int err;
2818
Ido Schimmel9011b672017-05-16 19:38:25 +02002819 if (mlxsw_sp->router->aborted)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002820 return 0;
2821
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002822 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, fen_info->tb_id,
2823 &fen_info->dst, sizeof(fen_info->dst),
2824 fen_info->dst_len,
2825 MLXSW_SP_L3_PROTO_IPV4);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002826 if (IS_ERR(fib_node)) {
2827 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
2828 return PTR_ERR(fib_node);
2829 }
2830
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002831 fib4_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
2832 if (IS_ERR(fib4_entry)) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002833 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002834 err = PTR_ERR(fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002835 goto err_fib4_entry_create;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002836 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02002837
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002838 err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib4_entry, replace,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002839 append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002840 if (err) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002841 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
2842 goto err_fib4_node_entry_link;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002843 }
Ido Schimmel9aecce12017-02-09 10:28:42 +01002844
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002845 mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib4_entry, replace);
Ido Schimmel599cf8f2017-02-09 10:28:44 +01002846
Jiri Pirko61c503f2016-07-04 08:23:11 +02002847 return 0;
2848
Ido Schimmel9aecce12017-02-09 10:28:42 +01002849err_fib4_node_entry_link:
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002850 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002851err_fib4_entry_create:
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002852 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002853 return err;
2854}
2855
Jiri Pirko37956d72016-10-20 16:05:43 +02002856static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
2857 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002858{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002859 struct mlxsw_sp_fib4_entry *fib4_entry;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002860 struct mlxsw_sp_fib_node *fib_node;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002861
Ido Schimmel9011b672017-05-16 19:38:25 +02002862 if (mlxsw_sp->router->aborted)
Jiri Pirko37956d72016-10-20 16:05:43 +02002863 return;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002864
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002865 fib4_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
2866 if (WARN_ON(!fib4_entry))
Jiri Pirko37956d72016-10-20 16:05:43 +02002867 return;
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002868 fib_node = fib4_entry->common.fib_node;
Jiri Pirko5b004412016-09-01 10:37:40 +02002869
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002870 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
2871 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002872 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002873}
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002874
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02002875static int __mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp,
2876 enum mlxsw_reg_ralxx_protocol proto,
2877 u8 tree_id)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002878{
2879 char ralta_pl[MLXSW_REG_RALTA_LEN];
2880 char ralst_pl[MLXSW_REG_RALST_LEN];
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002881 int i, err;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002882
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02002883 mlxsw_reg_ralta_pack(ralta_pl, true, proto, tree_id);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002884 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
2885 if (err)
2886 return err;
2887
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02002888 mlxsw_reg_ralst_pack(ralst_pl, 0xff, tree_id);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002889 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
2890 if (err)
2891 return err;
2892
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002893 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +02002894 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002895 char raltb_pl[MLXSW_REG_RALTB_LEN];
2896 char ralue_pl[MLXSW_REG_RALUE_LEN];
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002897
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002898 if (!mlxsw_sp_vr_is_used(vr))
2899 continue;
2900
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02002901 mlxsw_reg_raltb_pack(raltb_pl, vr->id, proto, tree_id);
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002902 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
2903 raltb_pl);
2904 if (err)
2905 return err;
2906
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02002907 mlxsw_reg_ralue_pack(ralue_pl, proto,
2908 MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0);
Ido Schimmelb5d90e62017-03-10 08:53:43 +01002909 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
2910 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
2911 ralue_pl);
2912 if (err)
2913 return err;
2914 }
2915
2916 return 0;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002917}
2918
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02002919static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
2920{
2921 enum mlxsw_reg_ralxx_protocol proto = MLXSW_REG_RALXX_PROTOCOL_IPV4;
2922 int err;
2923
2924 err = __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
2925 MLXSW_SP_LPM_TREE_MIN);
2926 if (err)
2927 return err;
2928
2929 proto = MLXSW_REG_RALXX_PROTOCOL_IPV6;
2930 return __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
2931 MLXSW_SP_LPM_TREE_MIN + 1);
2932}
2933
Ido Schimmel9aecce12017-02-09 10:28:42 +01002934static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
2935 struct mlxsw_sp_fib_node *fib_node)
2936{
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002937 struct mlxsw_sp_fib4_entry *fib4_entry, *tmp;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002938
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002939 list_for_each_entry_safe(fib4_entry, tmp, &fib_node->entry_list,
2940 common.list) {
2941 bool do_break = &tmp->common.list == &fib_node->entry_list;
Ido Schimmel9aecce12017-02-09 10:28:42 +01002942
Ido Schimmel4f1c7f12017-07-18 10:10:26 +02002943 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
2944 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
Ido Schimmel731ea1c2017-07-18 10:10:21 +02002945 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
Ido Schimmel9aecce12017-02-09 10:28:42 +01002946 /* Break when entry list is empty and node was freed.
2947 * Otherwise, we'll access freed memory in the next
2948 * iteration.
2949 */
2950 if (do_break)
2951 break;
2952 }
2953}
2954
2955static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
2956 struct mlxsw_sp_fib_node *fib_node)
2957{
Ido Schimmel76610eb2017-03-10 08:53:41 +01002958 switch (fib_node->fib->proto) {
Ido Schimmel9aecce12017-02-09 10:28:42 +01002959 case MLXSW_SP_L3_PROTO_IPV4:
2960 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
2961 break;
2962 case MLXSW_SP_L3_PROTO_IPV6:
2963 WARN_ON_ONCE(1);
2964 break;
2965 }
2966}
2967
Ido Schimmel76610eb2017-03-10 08:53:41 +01002968static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
2969 struct mlxsw_sp_vr *vr,
2970 enum mlxsw_sp_l3proto proto)
2971{
2972 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
2973 struct mlxsw_sp_fib_node *fib_node, *tmp;
2974
2975 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
2976 bool do_break = &tmp->list == &fib->node_list;
2977
2978 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
2979 if (do_break)
2980 break;
2981 }
2982}
2983
Ido Schimmelac571de2016-11-14 11:26:32 +01002984static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002985{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002986 int i;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002987
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002988 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Ido Schimmel9011b672017-05-16 19:38:25 +02002989 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
Ido Schimmelac571de2016-11-14 11:26:32 +01002990
Ido Schimmel76610eb2017-03-10 08:53:41 +01002991 if (!mlxsw_sp_vr_is_used(vr))
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002992 continue;
Ido Schimmel76610eb2017-03-10 08:53:41 +01002993 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
Ido Schimmela3d9bc52017-07-18 10:10:22 +02002994
2995 /* If virtual router was only used for IPv4, then it's no
2996 * longer used.
2997 */
2998 if (!mlxsw_sp_vr_is_used(vr))
2999 continue;
3000 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003001 }
Ido Schimmelac571de2016-11-14 11:26:32 +01003002}
3003
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02003004static void mlxsw_sp_router_fib_abort(struct mlxsw_sp *mlxsw_sp)
Ido Schimmelac571de2016-11-14 11:26:32 +01003005{
3006 int err;
3007
Ido Schimmel9011b672017-05-16 19:38:25 +02003008 if (mlxsw_sp->router->aborted)
Ido Schimmeld331d302016-11-16 09:51:58 +01003009 return;
3010 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 +01003011 mlxsw_sp_router_fib_flush(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02003012 mlxsw_sp->router->aborted = true;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003013 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
3014 if (err)
3015 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
3016}
3017
Ido Schimmel30572242016-12-03 16:45:01 +01003018struct mlxsw_sp_fib_event_work {
Ido Schimmela0e47612017-02-06 16:20:10 +01003019 struct work_struct work;
Ido Schimmelad178c82017-02-08 11:16:40 +01003020 union {
3021 struct fib_entry_notifier_info fen_info;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01003022 struct fib_rule_notifier_info fr_info;
Ido Schimmelad178c82017-02-08 11:16:40 +01003023 struct fib_nh_notifier_info fnh_info;
3024 };
Ido Schimmel30572242016-12-03 16:45:01 +01003025 struct mlxsw_sp *mlxsw_sp;
3026 unsigned long event;
3027};
3028
3029static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003030{
Ido Schimmel30572242016-12-03 16:45:01 +01003031 struct mlxsw_sp_fib_event_work *fib_work =
Ido Schimmela0e47612017-02-06 16:20:10 +01003032 container_of(work, struct mlxsw_sp_fib_event_work, work);
Ido Schimmel30572242016-12-03 16:45:01 +01003033 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01003034 struct fib_rule *rule;
Ido Schimmel599cf8f2017-02-09 10:28:44 +01003035 bool replace, append;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003036 int err;
3037
Ido Schimmel30572242016-12-03 16:45:01 +01003038 /* Protect internal structures from changes */
3039 rtnl_lock();
3040 switch (fib_work->event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01003041 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01003042 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003043 case FIB_EVENT_ENTRY_ADD:
Ido Schimmel599cf8f2017-02-09 10:28:44 +01003044 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
Ido Schimmel4283bce2017-02-09 10:28:43 +01003045 append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
3046 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
Ido Schimmel599cf8f2017-02-09 10:28:44 +01003047 replace, append);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003048 if (err)
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02003049 mlxsw_sp_router_fib_abort(mlxsw_sp);
Ido Schimmel30572242016-12-03 16:45:01 +01003050 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003051 break;
3052 case FIB_EVENT_ENTRY_DEL:
Ido Schimmel30572242016-12-03 16:45:01 +01003053 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
3054 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003055 break;
3056 case FIB_EVENT_RULE_ADD: /* fall through */
3057 case FIB_EVENT_RULE_DEL:
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01003058 rule = fib_work->fr_info.rule;
Ido Schimmelc7f6e662017-03-16 09:08:20 +01003059 if (!fib4_rule_default(rule) && !rule->l3mdev)
Ido Schimmelbc65a8a2017-07-18 10:10:25 +02003060 mlxsw_sp_router_fib_abort(mlxsw_sp);
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01003061 fib_rule_put(rule);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003062 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01003063 case FIB_EVENT_NH_ADD: /* fall through */
3064 case FIB_EVENT_NH_DEL:
Ido Schimmel0e6ea2a2017-07-18 10:10:27 +02003065 mlxsw_sp_nexthop4_event(mlxsw_sp, fib_work->event,
3066 fib_work->fnh_info.fib_nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01003067 fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
3068 break;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003069 }
Ido Schimmel30572242016-12-03 16:45:01 +01003070 rtnl_unlock();
3071 kfree(fib_work);
3072}
3073
3074/* Called with rcu_read_lock() */
3075static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
3076 unsigned long event, void *ptr)
3077{
Ido Schimmel30572242016-12-03 16:45:01 +01003078 struct mlxsw_sp_fib_event_work *fib_work;
3079 struct fib_notifier_info *info = ptr;
Ido Schimmel7e39d112017-05-16 19:38:28 +02003080 struct mlxsw_sp_router *router;
Ido Schimmel30572242016-12-03 16:45:01 +01003081
3082 if (!net_eq(info->net, &init_net))
3083 return NOTIFY_DONE;
3084
3085 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
3086 if (WARN_ON(!fib_work))
3087 return NOTIFY_BAD;
3088
Ido Schimmela0e47612017-02-06 16:20:10 +01003089 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work);
Ido Schimmel7e39d112017-05-16 19:38:28 +02003090 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
3091 fib_work->mlxsw_sp = router->mlxsw_sp;
Ido Schimmel30572242016-12-03 16:45:01 +01003092 fib_work->event = event;
3093
3094 switch (event) {
Ido Schimmel599cf8f2017-02-09 10:28:44 +01003095 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
Ido Schimmel4283bce2017-02-09 10:28:43 +01003096 case FIB_EVENT_ENTRY_APPEND: /* fall through */
Ido Schimmel30572242016-12-03 16:45:01 +01003097 case FIB_EVENT_ENTRY_ADD: /* fall through */
3098 case FIB_EVENT_ENTRY_DEL:
3099 memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
3100 /* Take referece on fib_info to prevent it from being
3101 * freed while work is queued. Release it afterwards.
3102 */
3103 fib_info_hold(fib_work->fen_info.fi);
3104 break;
Ido Schimmel5d7bfd12017-03-16 09:08:14 +01003105 case FIB_EVENT_RULE_ADD: /* fall through */
3106 case FIB_EVENT_RULE_DEL:
3107 memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
3108 fib_rule_get(fib_work->fr_info.rule);
3109 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01003110 case FIB_EVENT_NH_ADD: /* fall through */
3111 case FIB_EVENT_NH_DEL:
3112 memcpy(&fib_work->fnh_info, ptr, sizeof(fib_work->fnh_info));
3113 fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
3114 break;
Ido Schimmel30572242016-12-03 16:45:01 +01003115 }
3116
Ido Schimmela0e47612017-02-06 16:20:10 +01003117 mlxsw_core_schedule_work(&fib_work->work);
Ido Schimmel30572242016-12-03 16:45:01 +01003118
Jiri Pirkob45f64d2016-09-26 12:52:31 +02003119 return NOTIFY_DONE;
3120}
3121
Ido Schimmel4724ba562017-03-10 08:53:39 +01003122static struct mlxsw_sp_rif *
3123mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
3124 const struct net_device *dev)
3125{
3126 int i;
3127
3128 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
Ido Schimmel5f9efff2017-05-16 19:38:27 +02003129 if (mlxsw_sp->router->rifs[i] &&
3130 mlxsw_sp->router->rifs[i]->dev == dev)
3131 return mlxsw_sp->router->rifs[i];
Ido Schimmel4724ba562017-03-10 08:53:39 +01003132
3133 return NULL;
3134}
3135
3136static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
3137{
3138 char ritr_pl[MLXSW_REG_RITR_LEN];
3139 int err;
3140
3141 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
3142 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3143 if (WARN_ON_ONCE(err))
3144 return err;
3145
3146 mlxsw_reg_ritr_enable_set(ritr_pl, false);
3147 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3148}
3149
3150static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003151 struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003152{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003153 mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
3154 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
3155 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003156}
3157
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02003158static bool
3159mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif, struct net_device *dev,
3160 unsigned long event)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003161{
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02003162 struct inet6_dev *inet6_dev;
3163 bool addr_list_empty = true;
3164 struct in_device *idev;
3165
Ido Schimmel4724ba562017-03-10 08:53:39 +01003166 switch (event) {
3167 case NETDEV_UP:
Petr Machataf1b1f272017-07-31 09:27:28 +02003168 return rif == NULL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003169 case NETDEV_DOWN:
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02003170 idev = __in_dev_get_rtnl(dev);
3171 if (idev && idev->ifa_list)
3172 addr_list_empty = false;
3173
3174 inet6_dev = __in6_dev_get(dev);
3175 if (addr_list_empty && inet6_dev &&
3176 !list_empty(&inet6_dev->addr_list))
3177 addr_list_empty = false;
3178
3179 if (rif && addr_list_empty &&
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003180 !netif_is_l3_slave(rif->dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01003181 return true;
3182 /* It is possible we already removed the RIF ourselves
3183 * if it was assigned to a netdev that is now a bridge
3184 * or LAG slave.
3185 */
3186 return false;
3187 }
3188
3189 return false;
3190}
3191
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003192static enum mlxsw_sp_rif_type
3193mlxsw_sp_dev_rif_type(const struct mlxsw_sp *mlxsw_sp,
3194 const struct net_device *dev)
3195{
3196 enum mlxsw_sp_fid_type type;
3197
3198 /* RIF type is derived from the type of the underlying FID */
3199 if (is_vlan_dev(dev) && netif_is_bridge_master(vlan_dev_real_dev(dev)))
3200 type = MLXSW_SP_FID_TYPE_8021Q;
3201 else if (netif_is_bridge_master(dev) && br_vlan_enabled(dev))
3202 type = MLXSW_SP_FID_TYPE_8021Q;
3203 else if (netif_is_bridge_master(dev))
3204 type = MLXSW_SP_FID_TYPE_8021D;
3205 else
3206 type = MLXSW_SP_FID_TYPE_RFID;
3207
3208 return mlxsw_sp_fid_type_rif_type(mlxsw_sp, type);
3209}
3210
Ido Schimmelde5ed992017-06-04 16:53:40 +02003211static int mlxsw_sp_rif_index_alloc(struct mlxsw_sp *mlxsw_sp, u16 *p_rif_index)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003212{
3213 int i;
3214
Ido Schimmelde5ed992017-06-04 16:53:40 +02003215 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
3216 if (!mlxsw_sp->router->rifs[i]) {
3217 *p_rif_index = i;
3218 return 0;
3219 }
3220 }
Ido Schimmel4724ba562017-03-10 08:53:39 +01003221
Ido Schimmelde5ed992017-06-04 16:53:40 +02003222 return -ENOBUFS;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003223}
3224
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003225static struct mlxsw_sp_rif *mlxsw_sp_rif_alloc(size_t rif_size, u16 rif_index,
3226 u16 vr_id,
3227 struct net_device *l3_dev)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003228{
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003229 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003230
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003231 rif = kzalloc(rif_size, GFP_KERNEL);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003232 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003233 return NULL;
3234
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003235 INIT_LIST_HEAD(&rif->nexthop_list);
3236 INIT_LIST_HEAD(&rif->neigh_list);
3237 ether_addr_copy(rif->addr, l3_dev->dev_addr);
3238 rif->mtu = l3_dev->mtu;
3239 rif->vr_id = vr_id;
3240 rif->dev = l3_dev;
3241 rif->rif_index = rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003242
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003243 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003244}
3245
Ido Schimmel5f9efff2017-05-16 19:38:27 +02003246struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
3247 u16 rif_index)
3248{
3249 return mlxsw_sp->router->rifs[rif_index];
3250}
3251
Arkadi Sharshevskyfd1b9d42017-03-28 17:24:16 +02003252u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
3253{
3254 return rif->rif_index;
3255}
3256
3257int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
3258{
3259 return rif->dev->ifindex;
3260}
3261
Ido Schimmel4724ba562017-03-10 08:53:39 +01003262static struct mlxsw_sp_rif *
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003263mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
3264 const struct mlxsw_sp_rif_params *params)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003265{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003266 u32 tb_id = l3mdev_fib_table(params->dev);
3267 const struct mlxsw_sp_rif_ops *ops;
3268 enum mlxsw_sp_rif_type type;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003269 struct mlxsw_sp_rif *rif;
Ido Schimmela1107482017-05-26 08:37:39 +02003270 struct mlxsw_sp_fid *fid;
3271 struct mlxsw_sp_vr *vr;
3272 u16 rif_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003273 int err;
3274
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003275 type = mlxsw_sp_dev_rif_type(mlxsw_sp, params->dev);
3276 ops = mlxsw_sp->router->rif_ops_arr[type];
3277
Ido Schimmelc9ec53f2017-05-26 08:37:38 +02003278 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN);
3279 if (IS_ERR(vr))
3280 return ERR_CAST(vr);
3281
Ido Schimmelde5ed992017-06-04 16:53:40 +02003282 err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
3283 if (err)
3284 goto err_rif_index_alloc;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003285
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003286 rif = mlxsw_sp_rif_alloc(ops->rif_size, rif_index, vr->id, params->dev);
Ido Schimmela13a5942017-05-26 08:37:33 +02003287 if (!rif) {
3288 err = -ENOMEM;
3289 goto err_rif_alloc;
3290 }
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003291 rif->mlxsw_sp = mlxsw_sp;
3292 rif->ops = ops;
Ido Schimmela13a5942017-05-26 08:37:33 +02003293
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003294 fid = ops->fid_get(rif);
3295 if (IS_ERR(fid)) {
3296 err = PTR_ERR(fid);
3297 goto err_fid_get;
Ido Schimmel4d93cee2017-05-26 08:37:34 +02003298 }
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003299 rif->fid = fid;
Ido Schimmel4d93cee2017-05-26 08:37:34 +02003300
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003301 if (ops->setup)
3302 ops->setup(rif, params);
3303
3304 err = ops->configure(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003305 if (err)
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003306 goto err_configure;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003307
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003308 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, params->dev->dev_addr,
Ido Schimmela1107482017-05-26 08:37:39 +02003309 mlxsw_sp_fid_index(fid), true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003310 if (err)
3311 goto err_rif_fdb_op;
3312
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003313 mlxsw_sp_rif_counters_alloc(rif);
Ido Schimmela1107482017-05-26 08:37:39 +02003314 mlxsw_sp_fid_rif_set(fid, rif);
Ido Schimmel5f9efff2017-05-16 19:38:27 +02003315 mlxsw_sp->router->rifs[rif_index] = rif;
Ido Schimmel69132292017-03-10 08:53:42 +01003316 vr->rif_count++;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003317
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003318 return rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003319
Ido Schimmel4724ba562017-03-10 08:53:39 +01003320err_rif_fdb_op:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003321 ops->deconfigure(rif);
3322err_configure:
Ido Schimmela1107482017-05-26 08:37:39 +02003323 mlxsw_sp_fid_put(fid);
3324err_fid_get:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003325 kfree(rif);
3326err_rif_alloc:
Ido Schimmelde5ed992017-06-04 16:53:40 +02003327err_rif_index_alloc:
Ido Schimmelc9ec53f2017-05-26 08:37:38 +02003328 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003329 return ERR_PTR(err);
3330}
3331
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003332void mlxsw_sp_rif_destroy(struct mlxsw_sp_rif *rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003333{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003334 const struct mlxsw_sp_rif_ops *ops = rif->ops;
3335 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
Ido Schimmela1107482017-05-26 08:37:39 +02003336 struct mlxsw_sp_fid *fid = rif->fid;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003337 struct mlxsw_sp_vr *vr;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003338
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003339 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003340 vr = &mlxsw_sp->router->vrs[rif->vr_id];
Arkadi Sharshevskye0c0afd2017-03-28 17:24:15 +02003341
Ido Schimmel69132292017-03-10 08:53:42 +01003342 vr->rif_count--;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003343 mlxsw_sp->router->rifs[rif->rif_index] = NULL;
Ido Schimmela1107482017-05-26 08:37:39 +02003344 mlxsw_sp_fid_rif_set(fid, NULL);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003345 mlxsw_sp_rif_counters_free(rif);
3346 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->dev->dev_addr,
3347 mlxsw_sp_fid_index(fid), false);
3348 ops->deconfigure(rif);
Ido Schimmela1107482017-05-26 08:37:39 +02003349 mlxsw_sp_fid_put(fid);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003350 kfree(rif);
Ido Schimmelc9ec53f2017-05-26 08:37:38 +02003351 mlxsw_sp_vr_put(vr);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003352}
3353
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003354static void
3355mlxsw_sp_rif_subport_params_init(struct mlxsw_sp_rif_params *params,
3356 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
3357{
3358 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
3359
3360 params->vid = mlxsw_sp_port_vlan->vid;
3361 params->lag = mlxsw_sp_port->lagged;
3362 if (params->lag)
3363 params->lag_id = mlxsw_sp_port->lag_id;
3364 else
3365 params->system_port = mlxsw_sp_port->local_port;
3366}
3367
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003368static int
Ido Schimmela1107482017-05-26 08:37:39 +02003369mlxsw_sp_port_vlan_router_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003370 struct net_device *l3_dev)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003371{
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003372 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
Ido Schimmel1b8f09a2017-05-26 08:37:36 +02003373 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003374 u16 vid = mlxsw_sp_port_vlan->vid;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003375 struct mlxsw_sp_rif *rif;
Ido Schimmela1107482017-05-26 08:37:39 +02003376 struct mlxsw_sp_fid *fid;
Ido Schimmel03ea01e2017-05-23 21:56:30 +02003377 int err;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003378
Ido Schimmel1b8f09a2017-05-26 08:37:36 +02003379 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003380 if (!rif) {
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003381 struct mlxsw_sp_rif_params params = {
3382 .dev = l3_dev,
3383 };
3384
3385 mlxsw_sp_rif_subport_params_init(&params, mlxsw_sp_port_vlan);
3386 rif = mlxsw_sp_rif_create(mlxsw_sp, &params);
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003387 if (IS_ERR(rif))
3388 return PTR_ERR(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003389 }
3390
Ido Schimmela1107482017-05-26 08:37:39 +02003391 /* FID was already created, just take a reference */
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003392 fid = rif->ops->fid_get(rif);
Ido Schimmela1107482017-05-26 08:37:39 +02003393 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
3394 if (err)
3395 goto err_fid_port_vid_map;
3396
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003397 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
Ido Schimmel03ea01e2017-05-23 21:56:30 +02003398 if (err)
3399 goto err_port_vid_learning_set;
3400
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003401 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
Ido Schimmel03ea01e2017-05-23 21:56:30 +02003402 BR_STATE_FORWARDING);
3403 if (err)
3404 goto err_port_vid_stp_set;
3405
Ido Schimmela1107482017-05-26 08:37:39 +02003406 mlxsw_sp_port_vlan->fid = fid;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003407
Ido Schimmel4724ba562017-03-10 08:53:39 +01003408 return 0;
Ido Schimmel03ea01e2017-05-23 21:56:30 +02003409
3410err_port_vid_stp_set:
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003411 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
Ido Schimmel03ea01e2017-05-23 21:56:30 +02003412err_port_vid_learning_set:
Ido Schimmela1107482017-05-26 08:37:39 +02003413 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
3414err_fid_port_vid_map:
3415 mlxsw_sp_fid_put(fid);
Ido Schimmel03ea01e2017-05-23 21:56:30 +02003416 return err;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003417}
3418
Ido Schimmela1107482017-05-26 08:37:39 +02003419void
3420mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003421{
Ido Schimmelce95e152017-05-26 08:37:27 +02003422 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003423 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
Ido Schimmelce95e152017-05-26 08:37:27 +02003424 u16 vid = mlxsw_sp_port_vlan->vid;
Ido Schimmelce95e152017-05-26 08:37:27 +02003425
Ido Schimmela1107482017-05-26 08:37:39 +02003426 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_RFID))
3427 return;
Ido Schimmel4aafc362017-05-26 08:37:25 +02003428
Ido Schimmela1107482017-05-26 08:37:39 +02003429 mlxsw_sp_port_vlan->fid = NULL;
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003430 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_BLOCKING);
3431 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
Ido Schimmela1107482017-05-26 08:37:39 +02003432 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
3433 /* If router port holds the last reference on the rFID, then the
3434 * associated Sub-port RIF will be destroyed.
3435 */
3436 mlxsw_sp_fid_put(fid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003437}
3438
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003439static int mlxsw_sp_inetaddr_port_vlan_event(struct net_device *l3_dev,
3440 struct net_device *port_dev,
3441 unsigned long event, u16 vid)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003442{
3443 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
Ido Schimmelce95e152017-05-26 08:37:27 +02003444 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003445
Ido Schimmelce95e152017-05-26 08:37:27 +02003446 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003447 if (WARN_ON(!mlxsw_sp_port_vlan))
3448 return -EINVAL;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003449
3450 switch (event) {
3451 case NETDEV_UP:
Ido Schimmela1107482017-05-26 08:37:39 +02003452 return mlxsw_sp_port_vlan_router_join(mlxsw_sp_port_vlan,
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003453 l3_dev);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003454 case NETDEV_DOWN:
Ido Schimmela1107482017-05-26 08:37:39 +02003455 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003456 break;
3457 }
3458
3459 return 0;
3460}
3461
3462static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
3463 unsigned long event)
3464{
Jiri Pirko2b94e582017-04-18 16:55:37 +02003465 if (netif_is_bridge_port(port_dev) ||
3466 netif_is_lag_port(port_dev) ||
3467 netif_is_ovs_port(port_dev))
Ido Schimmel4724ba562017-03-10 08:53:39 +01003468 return 0;
3469
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003470 return mlxsw_sp_inetaddr_port_vlan_event(port_dev, port_dev, event, 1);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003471}
3472
3473static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
3474 struct net_device *lag_dev,
3475 unsigned long event, u16 vid)
3476{
3477 struct net_device *port_dev;
3478 struct list_head *iter;
3479 int err;
3480
3481 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
3482 if (mlxsw_sp_port_dev_check(port_dev)) {
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003483 err = mlxsw_sp_inetaddr_port_vlan_event(l3_dev,
3484 port_dev,
3485 event, vid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003486 if (err)
3487 return err;
3488 }
3489 }
3490
3491 return 0;
3492}
3493
3494static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
3495 unsigned long event)
3496{
3497 if (netif_is_bridge_port(lag_dev))
3498 return 0;
3499
3500 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1);
3501}
3502
Ido Schimmel4724ba562017-03-10 08:53:39 +01003503static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003504 unsigned long event)
3505{
3506 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003507 struct mlxsw_sp_rif_params params = {
3508 .dev = l3_dev,
3509 };
Ido Schimmela1107482017-05-26 08:37:39 +02003510 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003511
3512 switch (event) {
3513 case NETDEV_UP:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003514 rif = mlxsw_sp_rif_create(mlxsw_sp, &params);
3515 if (IS_ERR(rif))
3516 return PTR_ERR(rif);
3517 break;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003518 case NETDEV_DOWN:
Ido Schimmela1107482017-05-26 08:37:39 +02003519 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003520 mlxsw_sp_rif_destroy(rif);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003521 break;
3522 }
3523
3524 return 0;
3525}
3526
3527static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
3528 unsigned long event)
3529{
3530 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003531 u16 vid = vlan_dev_vlan_id(vlan_dev);
3532
Ido Schimmel6b27c8a2017-06-28 09:03:12 +03003533 if (netif_is_bridge_port(vlan_dev))
3534 return 0;
3535
Ido Schimmel4724ba562017-03-10 08:53:39 +01003536 if (mlxsw_sp_port_dev_check(real_dev))
Ido Schimmel7cbecf22017-05-26 08:37:28 +02003537 return mlxsw_sp_inetaddr_port_vlan_event(vlan_dev, real_dev,
3538 event, vid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003539 else if (netif_is_lag_master(real_dev))
3540 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
3541 vid);
Ido Schimmelc57529e2017-05-26 08:37:31 +02003542 else if (netif_is_bridge_master(real_dev) && br_vlan_enabled(real_dev))
Ido Schimmela1107482017-05-26 08:37:39 +02003543 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, event);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003544
3545 return 0;
3546}
3547
Ido Schimmelb1e45522017-04-30 19:47:14 +03003548static int __mlxsw_sp_inetaddr_event(struct net_device *dev,
3549 unsigned long event)
3550{
3551 if (mlxsw_sp_port_dev_check(dev))
3552 return mlxsw_sp_inetaddr_port_event(dev, event);
3553 else if (netif_is_lag_master(dev))
3554 return mlxsw_sp_inetaddr_lag_event(dev, event);
3555 else if (netif_is_bridge_master(dev))
Ido Schimmela1107482017-05-26 08:37:39 +02003556 return mlxsw_sp_inetaddr_bridge_event(dev, event);
Ido Schimmelb1e45522017-04-30 19:47:14 +03003557 else if (is_vlan_dev(dev))
3558 return mlxsw_sp_inetaddr_vlan_event(dev, event);
3559 else
3560 return 0;
3561}
3562
Ido Schimmel4724ba562017-03-10 08:53:39 +01003563int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
3564 unsigned long event, void *ptr)
3565{
3566 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3567 struct net_device *dev = ifa->ifa_dev->dev;
3568 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003569 struct mlxsw_sp_rif *rif;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003570 int err = 0;
3571
3572 mlxsw_sp = mlxsw_sp_lower_get(dev);
3573 if (!mlxsw_sp)
3574 goto out;
3575
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003576 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02003577 if (!mlxsw_sp_rif_should_config(rif, dev, event))
Ido Schimmel4724ba562017-03-10 08:53:39 +01003578 goto out;
3579
Ido Schimmelb1e45522017-04-30 19:47:14 +03003580 err = __mlxsw_sp_inetaddr_event(dev, event);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003581out:
3582 return notifier_from_errno(err);
3583}
3584
Arkadi Sharshevsky5ea12372017-07-18 10:10:13 +02003585struct mlxsw_sp_inet6addr_event_work {
3586 struct work_struct work;
3587 struct net_device *dev;
3588 unsigned long event;
3589};
3590
3591static void mlxsw_sp_inet6addr_event_work(struct work_struct *work)
3592{
3593 struct mlxsw_sp_inet6addr_event_work *inet6addr_work =
3594 container_of(work, struct mlxsw_sp_inet6addr_event_work, work);
3595 struct net_device *dev = inet6addr_work->dev;
3596 unsigned long event = inet6addr_work->event;
3597 struct mlxsw_sp *mlxsw_sp;
3598 struct mlxsw_sp_rif *rif;
3599
3600 rtnl_lock();
3601 mlxsw_sp = mlxsw_sp_lower_get(dev);
3602 if (!mlxsw_sp)
3603 goto out;
3604
3605 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3606 if (!mlxsw_sp_rif_should_config(rif, dev, event))
3607 goto out;
3608
3609 __mlxsw_sp_inetaddr_event(dev, event);
3610out:
3611 rtnl_unlock();
3612 dev_put(dev);
3613 kfree(inet6addr_work);
3614}
3615
3616/* Called with rcu_read_lock() */
3617int mlxsw_sp_inet6addr_event(struct notifier_block *unused,
3618 unsigned long event, void *ptr)
3619{
3620 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *) ptr;
3621 struct mlxsw_sp_inet6addr_event_work *inet6addr_work;
3622 struct net_device *dev = if6->idev->dev;
3623
3624 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
3625 return NOTIFY_DONE;
3626
3627 inet6addr_work = kzalloc(sizeof(*inet6addr_work), GFP_ATOMIC);
3628 if (!inet6addr_work)
3629 return NOTIFY_BAD;
3630
3631 INIT_WORK(&inet6addr_work->work, mlxsw_sp_inet6addr_event_work);
3632 inet6addr_work->dev = dev;
3633 inet6addr_work->event = event;
3634 dev_hold(dev);
3635 mlxsw_core_schedule_work(&inet6addr_work->work);
3636
3637 return NOTIFY_DONE;
3638}
3639
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003640static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
Ido Schimmel4724ba562017-03-10 08:53:39 +01003641 const char *mac, int mtu)
3642{
3643 char ritr_pl[MLXSW_REG_RITR_LEN];
3644 int err;
3645
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003646 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003647 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3648 if (err)
3649 return err;
3650
3651 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
3652 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
3653 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
3654 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3655}
3656
3657int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
3658{
3659 struct mlxsw_sp *mlxsw_sp;
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003660 struct mlxsw_sp_rif *rif;
Ido Schimmela1107482017-05-26 08:37:39 +02003661 u16 fid_index;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003662 int err;
3663
3664 mlxsw_sp = mlxsw_sp_lower_get(dev);
3665 if (!mlxsw_sp)
3666 return 0;
3667
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003668 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3669 if (!rif)
Ido Schimmel4724ba562017-03-10 08:53:39 +01003670 return 0;
Ido Schimmela1107482017-05-26 08:37:39 +02003671 fid_index = mlxsw_sp_fid_index(rif->fid);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003672
Ido Schimmela1107482017-05-26 08:37:39 +02003673 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003674 if (err)
3675 return err;
3676
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003677 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
3678 dev->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003679 if (err)
3680 goto err_rif_edit;
3681
Ido Schimmela1107482017-05-26 08:37:39 +02003682 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, fid_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003683 if (err)
3684 goto err_rif_fdb_op;
3685
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003686 ether_addr_copy(rif->addr, dev->dev_addr);
3687 rif->mtu = dev->mtu;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003688
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003689 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003690
3691 return 0;
3692
3693err_rif_fdb_op:
Arkadi Sharshevskybf952332017-03-17 09:38:00 +01003694 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003695err_rif_edit:
Ido Schimmela1107482017-05-26 08:37:39 +02003696 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003697 return err;
3698}
3699
Ido Schimmelb1e45522017-04-30 19:47:14 +03003700static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
3701 struct net_device *l3_dev)
Ido Schimmel7179eb52017-03-16 09:08:18 +01003702{
Ido Schimmelb1e45522017-04-30 19:47:14 +03003703 struct mlxsw_sp_rif *rif;
Ido Schimmel7179eb52017-03-16 09:08:18 +01003704
Ido Schimmelb1e45522017-04-30 19:47:14 +03003705 /* If netdev is already associated with a RIF, then we need to
3706 * destroy it and create a new one with the new virtual router ID.
Ido Schimmel7179eb52017-03-16 09:08:18 +01003707 */
Ido Schimmelb1e45522017-04-30 19:47:14 +03003708 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3709 if (rif)
3710 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN);
Ido Schimmel7179eb52017-03-16 09:08:18 +01003711
Ido Schimmelb1e45522017-04-30 19:47:14 +03003712 return __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_UP);
Ido Schimmel7179eb52017-03-16 09:08:18 +01003713}
3714
Ido Schimmelb1e45522017-04-30 19:47:14 +03003715static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
3716 struct net_device *l3_dev)
Ido Schimmel7179eb52017-03-16 09:08:18 +01003717{
Ido Schimmelb1e45522017-04-30 19:47:14 +03003718 struct mlxsw_sp_rif *rif;
Ido Schimmel7179eb52017-03-16 09:08:18 +01003719
Ido Schimmelb1e45522017-04-30 19:47:14 +03003720 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3721 if (!rif)
Ido Schimmel7179eb52017-03-16 09:08:18 +01003722 return;
Ido Schimmelb1e45522017-04-30 19:47:14 +03003723 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN);
Ido Schimmel7179eb52017-03-16 09:08:18 +01003724}
3725
Ido Schimmelb1e45522017-04-30 19:47:14 +03003726int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
3727 struct netdev_notifier_changeupper_info *info)
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003728{
Ido Schimmelb1e45522017-04-30 19:47:14 +03003729 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3730 int err = 0;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003731
Ido Schimmelb1e45522017-04-30 19:47:14 +03003732 if (!mlxsw_sp)
3733 return 0;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003734
Ido Schimmelb1e45522017-04-30 19:47:14 +03003735 switch (event) {
3736 case NETDEV_PRECHANGEUPPER:
3737 return 0;
3738 case NETDEV_CHANGEUPPER:
3739 if (info->linking)
3740 err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev);
3741 else
3742 mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
3743 break;
3744 }
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003745
Ido Schimmelb1e45522017-04-30 19:47:14 +03003746 return err;
Ido Schimmel3d70e4582017-03-16 09:08:19 +01003747}
3748
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003749static struct mlxsw_sp_rif_subport *
3750mlxsw_sp_rif_subport_rif(const struct mlxsw_sp_rif *rif)
Ido Schimmela1107482017-05-26 08:37:39 +02003751{
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003752 return container_of(rif, struct mlxsw_sp_rif_subport, common);
Ido Schimmela1107482017-05-26 08:37:39 +02003753}
3754
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003755static void mlxsw_sp_rif_subport_setup(struct mlxsw_sp_rif *rif,
3756 const struct mlxsw_sp_rif_params *params)
3757{
3758 struct mlxsw_sp_rif_subport *rif_subport;
3759
3760 rif_subport = mlxsw_sp_rif_subport_rif(rif);
3761 rif_subport->vid = params->vid;
3762 rif_subport->lag = params->lag;
3763 if (params->lag)
3764 rif_subport->lag_id = params->lag_id;
3765 else
3766 rif_subport->system_port = params->system_port;
3767}
3768
3769static int mlxsw_sp_rif_subport_op(struct mlxsw_sp_rif *rif, bool enable)
3770{
3771 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
3772 struct mlxsw_sp_rif_subport *rif_subport;
3773 char ritr_pl[MLXSW_REG_RITR_LEN];
3774
3775 rif_subport = mlxsw_sp_rif_subport_rif(rif);
3776 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_SP_IF,
3777 rif->rif_index, rif->vr_id, rif->dev->mtu,
3778 rif->dev->dev_addr);
3779 mlxsw_reg_ritr_sp_if_pack(ritr_pl, rif_subport->lag,
3780 rif_subport->lag ? rif_subport->lag_id :
3781 rif_subport->system_port,
3782 rif_subport->vid);
3783
3784 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3785}
3786
3787static int mlxsw_sp_rif_subport_configure(struct mlxsw_sp_rif *rif)
3788{
3789 return mlxsw_sp_rif_subport_op(rif, true);
3790}
3791
3792static void mlxsw_sp_rif_subport_deconfigure(struct mlxsw_sp_rif *rif)
3793{
3794 mlxsw_sp_rif_subport_op(rif, false);
3795}
3796
3797static struct mlxsw_sp_fid *
3798mlxsw_sp_rif_subport_fid_get(struct mlxsw_sp_rif *rif)
3799{
3800 return mlxsw_sp_fid_rfid_get(rif->mlxsw_sp, rif->rif_index);
3801}
3802
3803static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_subport_ops = {
3804 .type = MLXSW_SP_RIF_TYPE_SUBPORT,
3805 .rif_size = sizeof(struct mlxsw_sp_rif_subport),
3806 .setup = mlxsw_sp_rif_subport_setup,
3807 .configure = mlxsw_sp_rif_subport_configure,
3808 .deconfigure = mlxsw_sp_rif_subport_deconfigure,
3809 .fid_get = mlxsw_sp_rif_subport_fid_get,
3810};
3811
3812static int mlxsw_sp_rif_vlan_fid_op(struct mlxsw_sp_rif *rif,
3813 enum mlxsw_reg_ritr_if_type type,
3814 u16 vid_fid, bool enable)
3815{
3816 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
3817 char ritr_pl[MLXSW_REG_RITR_LEN];
3818
3819 mlxsw_reg_ritr_pack(ritr_pl, enable, type, rif->rif_index, rif->vr_id,
3820 rif->dev->mtu, rif->dev->dev_addr);
3821 mlxsw_reg_ritr_fid_set(ritr_pl, type, vid_fid);
3822
3823 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3824}
3825
3826static u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
3827{
3828 return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
3829}
3830
3831static int mlxsw_sp_rif_vlan_configure(struct mlxsw_sp_rif *rif)
3832{
3833 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
3834 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
3835 int err;
3836
3837 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, true);
3838 if (err)
3839 return err;
3840
Ido Schimmel0d284812017-07-18 10:10:12 +02003841 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
3842 mlxsw_sp_router_port(mlxsw_sp), true);
3843 if (err)
3844 goto err_fid_mc_flood_set;
3845
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003846 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
3847 mlxsw_sp_router_port(mlxsw_sp), true);
3848 if (err)
3849 goto err_fid_bc_flood_set;
3850
3851 return 0;
3852
3853err_fid_bc_flood_set:
Ido Schimmel0d284812017-07-18 10:10:12 +02003854 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
3855 mlxsw_sp_router_port(mlxsw_sp), false);
3856err_fid_mc_flood_set:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003857 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
3858 return err;
3859}
3860
3861static void mlxsw_sp_rif_vlan_deconfigure(struct mlxsw_sp_rif *rif)
3862{
3863 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
3864 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
3865
3866 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
3867 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmel0d284812017-07-18 10:10:12 +02003868 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
3869 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003870 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
3871}
3872
3873static struct mlxsw_sp_fid *
3874mlxsw_sp_rif_vlan_fid_get(struct mlxsw_sp_rif *rif)
3875{
3876 u16 vid = is_vlan_dev(rif->dev) ? vlan_dev_vlan_id(rif->dev) : 1;
3877
3878 return mlxsw_sp_fid_8021q_get(rif->mlxsw_sp, vid);
3879}
3880
3881static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_vlan_ops = {
3882 .type = MLXSW_SP_RIF_TYPE_VLAN,
3883 .rif_size = sizeof(struct mlxsw_sp_rif),
3884 .configure = mlxsw_sp_rif_vlan_configure,
3885 .deconfigure = mlxsw_sp_rif_vlan_deconfigure,
3886 .fid_get = mlxsw_sp_rif_vlan_fid_get,
3887};
3888
3889static int mlxsw_sp_rif_fid_configure(struct mlxsw_sp_rif *rif)
3890{
3891 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
3892 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
3893 int err;
3894
3895 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index,
3896 true);
3897 if (err)
3898 return err;
3899
Ido Schimmel0d284812017-07-18 10:10:12 +02003900 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
3901 mlxsw_sp_router_port(mlxsw_sp), true);
3902 if (err)
3903 goto err_fid_mc_flood_set;
3904
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003905 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
3906 mlxsw_sp_router_port(mlxsw_sp), true);
3907 if (err)
3908 goto err_fid_bc_flood_set;
3909
3910 return 0;
3911
3912err_fid_bc_flood_set:
Ido Schimmel0d284812017-07-18 10:10:12 +02003913 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
3914 mlxsw_sp_router_port(mlxsw_sp), false);
3915err_fid_mc_flood_set:
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003916 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
3917 return err;
3918}
3919
3920static void mlxsw_sp_rif_fid_deconfigure(struct mlxsw_sp_rif *rif)
3921{
3922 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
3923 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
3924
3925 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
3926 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmel0d284812017-07-18 10:10:12 +02003927 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
3928 mlxsw_sp_router_port(mlxsw_sp), false);
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003929 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
3930}
3931
3932static struct mlxsw_sp_fid *
3933mlxsw_sp_rif_fid_fid_get(struct mlxsw_sp_rif *rif)
3934{
3935 return mlxsw_sp_fid_8021d_get(rif->mlxsw_sp, rif->dev->ifindex);
3936}
3937
3938static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_fid_ops = {
3939 .type = MLXSW_SP_RIF_TYPE_FID,
3940 .rif_size = sizeof(struct mlxsw_sp_rif),
3941 .configure = mlxsw_sp_rif_fid_configure,
3942 .deconfigure = mlxsw_sp_rif_fid_deconfigure,
3943 .fid_get = mlxsw_sp_rif_fid_fid_get,
3944};
3945
3946static const struct mlxsw_sp_rif_ops *mlxsw_sp_rif_ops_arr[] = {
3947 [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
3948 [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_ops,
3949 [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
3950};
3951
Ido Schimmel348b8fc2017-05-16 19:38:29 +02003952static int mlxsw_sp_rifs_init(struct mlxsw_sp *mlxsw_sp)
3953{
3954 u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
3955
3956 mlxsw_sp->router->rifs = kcalloc(max_rifs,
3957 sizeof(struct mlxsw_sp_rif *),
3958 GFP_KERNEL);
3959 if (!mlxsw_sp->router->rifs)
3960 return -ENOMEM;
Ido Schimmele4f3c1c2017-05-26 08:37:40 +02003961
3962 mlxsw_sp->router->rif_ops_arr = mlxsw_sp_rif_ops_arr;
3963
Ido Schimmel348b8fc2017-05-16 19:38:29 +02003964 return 0;
3965}
3966
3967static void mlxsw_sp_rifs_fini(struct mlxsw_sp *mlxsw_sp)
3968{
3969 int i;
3970
3971 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
3972 WARN_ON_ONCE(mlxsw_sp->router->rifs[i]);
3973
3974 kfree(mlxsw_sp->router->rifs);
3975}
3976
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003977static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
3978{
Ido Schimmel7e39d112017-05-16 19:38:28 +02003979 struct mlxsw_sp_router *router;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003980
3981 /* Flush pending FIB notifications and then flush the device's
3982 * table before requesting another dump. The FIB notification
3983 * block is unregistered, so no need to take RTNL.
3984 */
3985 mlxsw_core_flush_owq();
Ido Schimmel7e39d112017-05-16 19:38:28 +02003986 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
3987 mlxsw_sp_router_fib_flush(router->mlxsw_sp);
Ido Schimmelc3852ef2016-12-03 16:45:07 +01003988}
3989
Ido Schimmel4724ba562017-03-10 08:53:39 +01003990static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
3991{
3992 char rgcr_pl[MLXSW_REG_RGCR_LEN];
3993 u64 max_rifs;
3994 int err;
3995
3996 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
3997 return -EIO;
Ido Schimmel4724ba562017-03-10 08:53:39 +01003998 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
Ido Schimmel4724ba562017-03-10 08:53:39 +01003999
Arkadi Sharshevskye29237e2017-07-18 10:10:09 +02004000 mlxsw_reg_rgcr_pack(rgcr_pl, true, true);
Ido Schimmel4724ba562017-03-10 08:53:39 +01004001 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
4002 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
4003 if (err)
Ido Schimmel348b8fc2017-05-16 19:38:29 +02004004 return err;
Ido Schimmel4724ba562017-03-10 08:53:39 +01004005 return 0;
Ido Schimmel4724ba562017-03-10 08:53:39 +01004006}
4007
4008static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
4009{
4010 char rgcr_pl[MLXSW_REG_RGCR_LEN];
Ido Schimmel4724ba562017-03-10 08:53:39 +01004011
Arkadi Sharshevskye29237e2017-07-18 10:10:09 +02004012 mlxsw_reg_rgcr_pack(rgcr_pl, false, false);
Ido Schimmel4724ba562017-03-10 08:53:39 +01004013 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
Ido Schimmel4724ba562017-03-10 08:53:39 +01004014}
4015
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004016int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
4017{
Ido Schimmel9011b672017-05-16 19:38:25 +02004018 struct mlxsw_sp_router *router;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004019 int err;
4020
Ido Schimmel9011b672017-05-16 19:38:25 +02004021 router = kzalloc(sizeof(*mlxsw_sp->router), GFP_KERNEL);
4022 if (!router)
4023 return -ENOMEM;
4024 mlxsw_sp->router = router;
4025 router->mlxsw_sp = mlxsw_sp;
4026
4027 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_neighs_list);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004028 err = __mlxsw_sp_router_init(mlxsw_sp);
4029 if (err)
Ido Schimmel9011b672017-05-16 19:38:25 +02004030 goto err_router_init;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004031
Ido Schimmel348b8fc2017-05-16 19:38:29 +02004032 err = mlxsw_sp_rifs_init(mlxsw_sp);
4033 if (err)
4034 goto err_rifs_init;
4035
Ido Schimmel9011b672017-05-16 19:38:25 +02004036 err = rhashtable_init(&mlxsw_sp->router->nexthop_ht,
Ido Schimmelc53b8e12017-02-08 11:16:30 +01004037 &mlxsw_sp_nexthop_ht_params);
4038 if (err)
4039 goto err_nexthop_ht_init;
4040
Ido Schimmel9011b672017-05-16 19:38:25 +02004041 err = rhashtable_init(&mlxsw_sp->router->nexthop_group_ht,
Ido Schimmele9ad5e72017-02-08 11:16:29 +01004042 &mlxsw_sp_nexthop_group_ht_params);
4043 if (err)
4044 goto err_nexthop_group_ht_init;
4045
Ido Schimmel8494ab02017-03-24 08:02:47 +01004046 err = mlxsw_sp_lpm_init(mlxsw_sp);
4047 if (err)
4048 goto err_lpm_init;
4049
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004050 err = mlxsw_sp_vrs_init(mlxsw_sp);
4051 if (err)
4052 goto err_vrs_init;
4053
Ido Schimmel8c9583a2016-10-27 15:12:57 +02004054 err = mlxsw_sp_neigh_init(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004055 if (err)
4056 goto err_neigh_init;
4057
Ido Schimmel7e39d112017-05-16 19:38:28 +02004058 mlxsw_sp->router->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
4059 err = register_fib_notifier(&mlxsw_sp->router->fib_nb,
Ido Schimmelc3852ef2016-12-03 16:45:07 +01004060 mlxsw_sp_router_fib_dump_flush);
4061 if (err)
4062 goto err_register_fib_notifier;
4063
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004064 return 0;
4065
Ido Schimmelc3852ef2016-12-03 16:45:07 +01004066err_register_fib_notifier:
4067 mlxsw_sp_neigh_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004068err_neigh_init:
4069 mlxsw_sp_vrs_fini(mlxsw_sp);
4070err_vrs_init:
Ido Schimmel8494ab02017-03-24 08:02:47 +01004071 mlxsw_sp_lpm_fini(mlxsw_sp);
4072err_lpm_init:
Ido Schimmel9011b672017-05-16 19:38:25 +02004073 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
Ido Schimmele9ad5e72017-02-08 11:16:29 +01004074err_nexthop_group_ht_init:
Ido Schimmel9011b672017-05-16 19:38:25 +02004075 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01004076err_nexthop_ht_init:
Ido Schimmel348b8fc2017-05-16 19:38:29 +02004077 mlxsw_sp_rifs_fini(mlxsw_sp);
4078err_rifs_init:
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004079 __mlxsw_sp_router_fini(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02004080err_router_init:
4081 kfree(mlxsw_sp->router);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004082 return err;
4083}
4084
4085void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
4086{
Ido Schimmel7e39d112017-05-16 19:38:28 +02004087 unregister_fib_notifier(&mlxsw_sp->router->fib_nb);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004088 mlxsw_sp_neigh_fini(mlxsw_sp);
4089 mlxsw_sp_vrs_fini(mlxsw_sp);
Ido Schimmel8494ab02017-03-24 08:02:47 +01004090 mlxsw_sp_lpm_fini(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02004091 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
4092 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
Ido Schimmel348b8fc2017-05-16 19:38:29 +02004093 mlxsw_sp_rifs_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004094 __mlxsw_sp_router_fini(mlxsw_sp);
Ido Schimmel9011b672017-05-16 19:38:25 +02004095 kfree(mlxsw_sp->router);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02004096}