Ido Schimmel | 464dce1 | 2016-07-02 11:00:15 +0200 | [diff] [blame] | 1 | /* |
| 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 Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 6 | * Copyright (c) 2016 Yotam Gigi <yotamg@mellanox.com> |
Ido Schimmel | 464dce1 | 2016-07-02 11:00:15 +0200 | [diff] [blame] | 7 | * |
| 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 Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 39 | #include <linux/rhashtable.h> |
| 40 | #include <linux/bitops.h> |
| 41 | #include <linux/in6.h> |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 42 | #include <linux/notifier.h> |
| 43 | #include <net/netevent.h> |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 44 | #include <net/neighbour.h> |
| 45 | #include <net/arp.h> |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 46 | #include <net/ip_fib.h> |
Ido Schimmel | 464dce1 | 2016-07-02 11:00:15 +0200 | [diff] [blame] | 47 | |
| 48 | #include "spectrum.h" |
| 49 | #include "core.h" |
| 50 | #include "reg.h" |
| 51 | |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 52 | #define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \ |
| 53 | for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT) |
| 54 | |
| 55 | static bool |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 56 | mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1, |
| 57 | struct mlxsw_sp_prefix_usage *prefix_usage2) |
| 58 | { |
| 59 | unsigned char prefix; |
| 60 | |
| 61 | mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) { |
| 62 | if (!test_bit(prefix, prefix_usage2->b)) |
| 63 | return false; |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | static bool |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 69 | mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1, |
| 70 | struct mlxsw_sp_prefix_usage *prefix_usage2) |
| 71 | { |
| 72 | return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1)); |
| 73 | } |
| 74 | |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 75 | static bool |
| 76 | mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage) |
| 77 | { |
| 78 | struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } }; |
| 79 | |
| 80 | return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none); |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1, |
| 85 | struct mlxsw_sp_prefix_usage *prefix_usage2) |
| 86 | { |
| 87 | memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1)); |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | mlxsw_sp_prefix_usage_zero(struct mlxsw_sp_prefix_usage *prefix_usage) |
| 92 | { |
| 93 | memset(prefix_usage, 0, sizeof(*prefix_usage)); |
| 94 | } |
| 95 | |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 96 | static void |
| 97 | mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage, |
| 98 | unsigned char prefix_len) |
| 99 | { |
| 100 | set_bit(prefix_len, prefix_usage->b); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage, |
| 105 | unsigned char prefix_len) |
| 106 | { |
| 107 | clear_bit(prefix_len, prefix_usage->b); |
| 108 | } |
| 109 | |
| 110 | struct mlxsw_sp_fib_key { |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 111 | struct net_device *dev; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 112 | unsigned char addr[sizeof(struct in6_addr)]; |
| 113 | unsigned char prefix_len; |
| 114 | }; |
| 115 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 116 | enum mlxsw_sp_fib_entry_type { |
| 117 | MLXSW_SP_FIB_ENTRY_TYPE_REMOTE, |
| 118 | MLXSW_SP_FIB_ENTRY_TYPE_LOCAL, |
| 119 | MLXSW_SP_FIB_ENTRY_TYPE_TRAP, |
| 120 | }; |
| 121 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 122 | struct mlxsw_sp_nexthop_group; |
| 123 | |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 124 | struct mlxsw_sp_fib_entry { |
| 125 | struct rhash_head ht_node; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 126 | struct list_head list; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 127 | struct mlxsw_sp_fib_key key; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 128 | enum mlxsw_sp_fib_entry_type type; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 129 | unsigned int ref_count; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 130 | struct mlxsw_sp_vr *vr; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 131 | struct list_head nexthop_group_node; |
| 132 | struct mlxsw_sp_nexthop_group *nh_group; |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 133 | bool offloaded; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | struct mlxsw_sp_fib { |
| 137 | struct rhashtable ht; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 138 | struct list_head entry_list; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 139 | unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT]; |
| 140 | struct mlxsw_sp_prefix_usage prefix_usage; |
| 141 | }; |
| 142 | |
| 143 | static const struct rhashtable_params mlxsw_sp_fib_ht_params = { |
| 144 | .key_offset = offsetof(struct mlxsw_sp_fib_entry, key), |
| 145 | .head_offset = offsetof(struct mlxsw_sp_fib_entry, ht_node), |
| 146 | .key_len = sizeof(struct mlxsw_sp_fib_key), |
| 147 | .automatic_shrinking = true, |
| 148 | }; |
| 149 | |
| 150 | static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib *fib, |
| 151 | struct mlxsw_sp_fib_entry *fib_entry) |
| 152 | { |
| 153 | unsigned char prefix_len = fib_entry->key.prefix_len; |
| 154 | int err; |
| 155 | |
| 156 | err = rhashtable_insert_fast(&fib->ht, &fib_entry->ht_node, |
| 157 | mlxsw_sp_fib_ht_params); |
| 158 | if (err) |
| 159 | return err; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 160 | list_add_tail(&fib_entry->list, &fib->entry_list); |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 161 | if (fib->prefix_ref_count[prefix_len]++ == 0) |
| 162 | mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib *fib, |
| 167 | struct mlxsw_sp_fib_entry *fib_entry) |
| 168 | { |
| 169 | unsigned char prefix_len = fib_entry->key.prefix_len; |
| 170 | |
| 171 | if (--fib->prefix_ref_count[prefix_len] == 0) |
| 172 | mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 173 | list_del(&fib_entry->list); |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 174 | rhashtable_remove_fast(&fib->ht, &fib_entry->ht_node, |
| 175 | mlxsw_sp_fib_ht_params); |
| 176 | } |
| 177 | |
| 178 | static struct mlxsw_sp_fib_entry * |
| 179 | mlxsw_sp_fib_entry_create(struct mlxsw_sp_fib *fib, const void *addr, |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 180 | size_t addr_len, unsigned char prefix_len, |
| 181 | struct net_device *dev) |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 182 | { |
| 183 | struct mlxsw_sp_fib_entry *fib_entry; |
| 184 | |
| 185 | fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL); |
| 186 | if (!fib_entry) |
| 187 | return NULL; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 188 | fib_entry->key.dev = dev; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 189 | memcpy(fib_entry->key.addr, addr, addr_len); |
| 190 | fib_entry->key.prefix_len = prefix_len; |
| 191 | return fib_entry; |
| 192 | } |
| 193 | |
| 194 | static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry *fib_entry) |
| 195 | { |
| 196 | kfree(fib_entry); |
| 197 | } |
| 198 | |
| 199 | static struct mlxsw_sp_fib_entry * |
| 200 | mlxsw_sp_fib_entry_lookup(struct mlxsw_sp_fib *fib, const void *addr, |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 201 | size_t addr_len, unsigned char prefix_len, |
| 202 | struct net_device *dev) |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 203 | { |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 204 | struct mlxsw_sp_fib_key key; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 205 | |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 206 | memset(&key, 0, sizeof(key)); |
| 207 | key.dev = dev; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 208 | memcpy(key.addr, addr, addr_len); |
| 209 | key.prefix_len = prefix_len; |
| 210 | return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params); |
| 211 | } |
| 212 | |
| 213 | static struct mlxsw_sp_fib *mlxsw_sp_fib_create(void) |
| 214 | { |
| 215 | struct mlxsw_sp_fib *fib; |
| 216 | int err; |
| 217 | |
| 218 | fib = kzalloc(sizeof(*fib), GFP_KERNEL); |
| 219 | if (!fib) |
| 220 | return ERR_PTR(-ENOMEM); |
| 221 | err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params); |
| 222 | if (err) |
| 223 | goto err_rhashtable_init; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 224 | INIT_LIST_HEAD(&fib->entry_list); |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 225 | return fib; |
| 226 | |
| 227 | err_rhashtable_init: |
| 228 | kfree(fib); |
| 229 | return ERR_PTR(err); |
| 230 | } |
| 231 | |
| 232 | static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib) |
| 233 | { |
| 234 | rhashtable_destroy(&fib->ht); |
| 235 | kfree(fib); |
| 236 | } |
| 237 | |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 238 | static struct mlxsw_sp_lpm_tree * |
| 239 | mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp, bool one_reserved) |
| 240 | { |
| 241 | static struct mlxsw_sp_lpm_tree *lpm_tree; |
| 242 | int i; |
| 243 | |
| 244 | for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) { |
| 245 | lpm_tree = &mlxsw_sp->router.lpm_trees[i]; |
| 246 | if (lpm_tree->ref_count == 0) { |
| 247 | if (one_reserved) |
| 248 | one_reserved = false; |
| 249 | else |
| 250 | return lpm_tree; |
| 251 | } |
| 252 | } |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp, |
| 257 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 258 | { |
| 259 | char ralta_pl[MLXSW_REG_RALTA_LEN]; |
| 260 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 261 | mlxsw_reg_ralta_pack(ralta_pl, true, |
| 262 | (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto, |
| 263 | lpm_tree->id); |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 264 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl); |
| 265 | } |
| 266 | |
| 267 | static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp, |
| 268 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 269 | { |
| 270 | char ralta_pl[MLXSW_REG_RALTA_LEN]; |
| 271 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 272 | mlxsw_reg_ralta_pack(ralta_pl, false, |
| 273 | (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto, |
| 274 | lpm_tree->id); |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 275 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl); |
| 276 | } |
| 277 | |
| 278 | static int |
| 279 | mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp, |
| 280 | struct mlxsw_sp_prefix_usage *prefix_usage, |
| 281 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 282 | { |
| 283 | char ralst_pl[MLXSW_REG_RALST_LEN]; |
| 284 | u8 root_bin = 0; |
| 285 | u8 prefix; |
| 286 | u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD; |
| 287 | |
| 288 | mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) |
| 289 | root_bin = prefix; |
| 290 | |
| 291 | mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id); |
| 292 | mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) { |
| 293 | if (prefix == 0) |
| 294 | continue; |
| 295 | mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix, |
| 296 | MLXSW_REG_RALST_BIN_NO_CHILD); |
| 297 | last_prefix = prefix; |
| 298 | } |
| 299 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl); |
| 300 | } |
| 301 | |
| 302 | static struct mlxsw_sp_lpm_tree * |
| 303 | mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp, |
| 304 | struct mlxsw_sp_prefix_usage *prefix_usage, |
| 305 | enum mlxsw_sp_l3proto proto, bool one_reserved) |
| 306 | { |
| 307 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 308 | int err; |
| 309 | |
| 310 | lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp, one_reserved); |
| 311 | if (!lpm_tree) |
| 312 | return ERR_PTR(-EBUSY); |
| 313 | lpm_tree->proto = proto; |
| 314 | err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree); |
| 315 | if (err) |
| 316 | return ERR_PTR(err); |
| 317 | |
| 318 | err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage, |
| 319 | lpm_tree); |
| 320 | if (err) |
| 321 | goto err_left_struct_set; |
Jiri Pirko | 2083d36 | 2016-10-25 11:25:56 +0200 | [diff] [blame] | 322 | memcpy(&lpm_tree->prefix_usage, prefix_usage, |
| 323 | sizeof(lpm_tree->prefix_usage)); |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 324 | return lpm_tree; |
| 325 | |
| 326 | err_left_struct_set: |
| 327 | mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree); |
| 328 | return ERR_PTR(err); |
| 329 | } |
| 330 | |
| 331 | static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp, |
| 332 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 333 | { |
| 334 | return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree); |
| 335 | } |
| 336 | |
| 337 | static struct mlxsw_sp_lpm_tree * |
| 338 | mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp, |
| 339 | struct mlxsw_sp_prefix_usage *prefix_usage, |
| 340 | enum mlxsw_sp_l3proto proto, bool one_reserved) |
| 341 | { |
| 342 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 343 | int i; |
| 344 | |
| 345 | for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) { |
| 346 | lpm_tree = &mlxsw_sp->router.lpm_trees[i]; |
Jiri Pirko | 8b99bec | 2016-10-25 11:25:57 +0200 | [diff] [blame] | 347 | if (lpm_tree->ref_count != 0 && |
| 348 | lpm_tree->proto == proto && |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 349 | mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage, |
| 350 | prefix_usage)) |
| 351 | goto inc_ref_count; |
| 352 | } |
| 353 | lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage, |
| 354 | proto, one_reserved); |
| 355 | if (IS_ERR(lpm_tree)) |
| 356 | return lpm_tree; |
| 357 | |
| 358 | inc_ref_count: |
| 359 | lpm_tree->ref_count++; |
| 360 | return lpm_tree; |
| 361 | } |
| 362 | |
| 363 | static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp, |
| 364 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 365 | { |
| 366 | if (--lpm_tree->ref_count == 0) |
| 367 | return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp) |
| 372 | { |
| 373 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 374 | int i; |
| 375 | |
| 376 | for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) { |
| 377 | lpm_tree = &mlxsw_sp->router.lpm_trees[i]; |
| 378 | lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN; |
| 379 | } |
| 380 | } |
| 381 | |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 382 | static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp) |
| 383 | { |
| 384 | struct mlxsw_sp_vr *vr; |
| 385 | int i; |
| 386 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 387 | for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) { |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 388 | vr = &mlxsw_sp->router.vrs[i]; |
| 389 | if (!vr->used) |
| 390 | return vr; |
| 391 | } |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp, |
| 396 | struct mlxsw_sp_vr *vr) |
| 397 | { |
| 398 | char raltb_pl[MLXSW_REG_RALTB_LEN]; |
| 399 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 400 | mlxsw_reg_raltb_pack(raltb_pl, vr->id, |
| 401 | (enum mlxsw_reg_ralxx_protocol) vr->proto, |
| 402 | vr->lpm_tree->id); |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 403 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl); |
| 404 | } |
| 405 | |
| 406 | static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp, |
| 407 | struct mlxsw_sp_vr *vr) |
| 408 | { |
| 409 | char raltb_pl[MLXSW_REG_RALTB_LEN]; |
| 410 | |
| 411 | /* Bind to tree 0 which is default */ |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 412 | mlxsw_reg_raltb_pack(raltb_pl, vr->id, |
| 413 | (enum mlxsw_reg_ralxx_protocol) vr->proto, 0); |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 414 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl); |
| 415 | } |
| 416 | |
| 417 | static u32 mlxsw_sp_fix_tb_id(u32 tb_id) |
| 418 | { |
| 419 | /* For our purpose, squash main and local table into one */ |
| 420 | if (tb_id == RT_TABLE_LOCAL) |
| 421 | tb_id = RT_TABLE_MAIN; |
| 422 | return tb_id; |
| 423 | } |
| 424 | |
| 425 | static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp, |
| 426 | u32 tb_id, |
| 427 | enum mlxsw_sp_l3proto proto) |
| 428 | { |
| 429 | struct mlxsw_sp_vr *vr; |
| 430 | int i; |
| 431 | |
| 432 | tb_id = mlxsw_sp_fix_tb_id(tb_id); |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 433 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 434 | for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) { |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 435 | vr = &mlxsw_sp->router.vrs[i]; |
| 436 | if (vr->used && vr->proto == proto && vr->tb_id == tb_id) |
| 437 | return vr; |
| 438 | } |
| 439 | return NULL; |
| 440 | } |
| 441 | |
| 442 | static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp, |
| 443 | unsigned char prefix_len, |
| 444 | u32 tb_id, |
| 445 | enum mlxsw_sp_l3proto proto) |
| 446 | { |
| 447 | struct mlxsw_sp_prefix_usage req_prefix_usage; |
| 448 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 449 | struct mlxsw_sp_vr *vr; |
| 450 | int err; |
| 451 | |
| 452 | vr = mlxsw_sp_vr_find_unused(mlxsw_sp); |
| 453 | if (!vr) |
| 454 | return ERR_PTR(-EBUSY); |
| 455 | vr->fib = mlxsw_sp_fib_create(); |
| 456 | if (IS_ERR(vr->fib)) |
| 457 | return ERR_CAST(vr->fib); |
| 458 | |
| 459 | vr->proto = proto; |
| 460 | vr->tb_id = tb_id; |
| 461 | mlxsw_sp_prefix_usage_zero(&req_prefix_usage); |
| 462 | mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len); |
| 463 | lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage, |
| 464 | proto, true); |
| 465 | if (IS_ERR(lpm_tree)) { |
| 466 | err = PTR_ERR(lpm_tree); |
| 467 | goto err_tree_get; |
| 468 | } |
| 469 | vr->lpm_tree = lpm_tree; |
| 470 | err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr); |
| 471 | if (err) |
| 472 | goto err_tree_bind; |
| 473 | |
| 474 | vr->used = true; |
| 475 | return vr; |
| 476 | |
| 477 | err_tree_bind: |
| 478 | mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree); |
| 479 | err_tree_get: |
| 480 | mlxsw_sp_fib_destroy(vr->fib); |
| 481 | |
| 482 | return ERR_PTR(err); |
| 483 | } |
| 484 | |
| 485 | static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp, |
| 486 | struct mlxsw_sp_vr *vr) |
| 487 | { |
| 488 | mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr); |
| 489 | mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree); |
| 490 | mlxsw_sp_fib_destroy(vr->fib); |
| 491 | vr->used = false; |
| 492 | } |
| 493 | |
| 494 | static int |
| 495 | mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr, |
| 496 | struct mlxsw_sp_prefix_usage *req_prefix_usage) |
| 497 | { |
| 498 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 499 | |
| 500 | if (mlxsw_sp_prefix_usage_eq(req_prefix_usage, |
| 501 | &vr->lpm_tree->prefix_usage)) |
| 502 | return 0; |
| 503 | |
| 504 | lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage, |
| 505 | vr->proto, false); |
| 506 | if (IS_ERR(lpm_tree)) { |
| 507 | /* We failed to get a tree according to the required |
| 508 | * prefix usage. However, the current tree might be still good |
| 509 | * for us if our requirement is subset of the prefixes used |
| 510 | * in the tree. |
| 511 | */ |
| 512 | if (mlxsw_sp_prefix_usage_subset(req_prefix_usage, |
| 513 | &vr->lpm_tree->prefix_usage)) |
| 514 | return 0; |
| 515 | return PTR_ERR(lpm_tree); |
| 516 | } |
| 517 | |
| 518 | mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr); |
| 519 | mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree); |
| 520 | vr->lpm_tree = lpm_tree; |
| 521 | return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr); |
| 522 | } |
| 523 | |
| 524 | static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, |
| 525 | unsigned char prefix_len, |
| 526 | u32 tb_id, |
| 527 | enum mlxsw_sp_l3proto proto) |
| 528 | { |
| 529 | struct mlxsw_sp_vr *vr; |
| 530 | int err; |
| 531 | |
| 532 | tb_id = mlxsw_sp_fix_tb_id(tb_id); |
| 533 | vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id, proto); |
| 534 | if (!vr) { |
| 535 | vr = mlxsw_sp_vr_create(mlxsw_sp, prefix_len, tb_id, proto); |
| 536 | if (IS_ERR(vr)) |
| 537 | return vr; |
| 538 | } else { |
| 539 | struct mlxsw_sp_prefix_usage req_prefix_usage; |
| 540 | |
| 541 | mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, |
| 542 | &vr->fib->prefix_usage); |
| 543 | mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len); |
| 544 | /* Need to replace LPM tree in case new prefix is required. */ |
| 545 | err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr, |
| 546 | &req_prefix_usage); |
| 547 | if (err) |
| 548 | return ERR_PTR(err); |
| 549 | } |
| 550 | return vr; |
| 551 | } |
| 552 | |
| 553 | static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr) |
| 554 | { |
| 555 | /* Destroy virtual router entity in case the associated FIB is empty |
| 556 | * and allow it to be used for other tables in future. Otherwise, |
| 557 | * check if some prefix usage did not disappear and change tree if |
| 558 | * that is the case. Note that in case new, smaller tree cannot be |
| 559 | * allocated, the original one will be kept being used. |
| 560 | */ |
| 561 | if (mlxsw_sp_prefix_usage_none(&vr->fib->prefix_usage)) |
| 562 | mlxsw_sp_vr_destroy(mlxsw_sp, vr); |
| 563 | else |
| 564 | mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr, |
| 565 | &vr->fib->prefix_usage); |
| 566 | } |
| 567 | |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 568 | static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp) |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 569 | { |
| 570 | struct mlxsw_sp_vr *vr; |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 571 | u64 max_vrs; |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 572 | int i; |
| 573 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 574 | if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS)) |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 575 | return -EIO; |
| 576 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 577 | max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); |
| 578 | mlxsw_sp->router.vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr), |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 579 | GFP_KERNEL); |
| 580 | if (!mlxsw_sp->router.vrs) |
| 581 | return -ENOMEM; |
| 582 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 583 | for (i = 0; i < max_vrs; i++) { |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 584 | vr = &mlxsw_sp->router.vrs[i]; |
| 585 | vr->id = i; |
| 586 | } |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
Ido Schimmel | ac571de | 2016-11-14 11:26:32 +0100 | [diff] [blame] | 591 | static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp); |
| 592 | |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 593 | static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp) |
| 594 | { |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 595 | /* At this stage we're guaranteed not to have new incoming |
| 596 | * FIB notifications and the work queue is free from FIBs |
| 597 | * sitting on top of mlxsw netdevs. However, we can still |
| 598 | * have other FIBs queued. Flush the queue before flushing |
| 599 | * the device's tables. No need for locks, as we're the only |
| 600 | * writer. |
| 601 | */ |
| 602 | mlxsw_core_flush_owq(); |
Ido Schimmel | ac571de | 2016-11-14 11:26:32 +0100 | [diff] [blame] | 603 | mlxsw_sp_router_fib_flush(mlxsw_sp); |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 604 | kfree(mlxsw_sp->router.vrs); |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 607 | struct mlxsw_sp_neigh_key { |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 608 | struct neighbour *n; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 609 | }; |
| 610 | |
| 611 | struct mlxsw_sp_neigh_entry { |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 612 | struct list_head rif_list_node; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 613 | struct rhash_head ht_node; |
| 614 | struct mlxsw_sp_neigh_key key; |
| 615 | u16 rif; |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 616 | bool connected; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 617 | unsigned char ha[ETH_ALEN]; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 618 | struct list_head nexthop_list; /* list of nexthops using |
| 619 | * this neigh entry |
| 620 | */ |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 621 | struct list_head nexthop_neighs_list_node; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | static const struct rhashtable_params mlxsw_sp_neigh_ht_params = { |
| 625 | .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key), |
| 626 | .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node), |
| 627 | .key_len = sizeof(struct mlxsw_sp_neigh_key), |
| 628 | }; |
| 629 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 630 | static struct mlxsw_sp_neigh_entry * |
| 631 | mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n, |
| 632 | u16 rif) |
| 633 | { |
| 634 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 635 | |
| 636 | neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL); |
| 637 | if (!neigh_entry) |
| 638 | return NULL; |
| 639 | |
| 640 | neigh_entry->key.n = n; |
| 641 | neigh_entry->rif = rif; |
| 642 | INIT_LIST_HEAD(&neigh_entry->nexthop_list); |
| 643 | |
| 644 | return neigh_entry; |
| 645 | } |
| 646 | |
| 647 | static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry) |
| 648 | { |
| 649 | kfree(neigh_entry); |
| 650 | } |
| 651 | |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 652 | static int |
| 653 | mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp, |
| 654 | struct mlxsw_sp_neigh_entry *neigh_entry) |
| 655 | { |
| 656 | return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht, |
| 657 | &neigh_entry->ht_node, |
| 658 | mlxsw_sp_neigh_ht_params); |
| 659 | } |
| 660 | |
| 661 | static void |
| 662 | mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp, |
| 663 | struct mlxsw_sp_neigh_entry *neigh_entry) |
| 664 | { |
| 665 | rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht, |
| 666 | &neigh_entry->ht_node, |
| 667 | mlxsw_sp_neigh_ht_params); |
| 668 | } |
| 669 | |
| 670 | static struct mlxsw_sp_neigh_entry * |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 671 | mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n) |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 672 | { |
| 673 | struct mlxsw_sp_neigh_entry *neigh_entry; |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 674 | struct mlxsw_sp_rif *r; |
| 675 | int err; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 676 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 677 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev); |
| 678 | if (!r) |
| 679 | return ERR_PTR(-EINVAL); |
| 680 | |
| 681 | neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, r->rif); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 682 | if (!neigh_entry) |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 683 | return ERR_PTR(-ENOMEM); |
| 684 | |
| 685 | err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry); |
| 686 | if (err) |
| 687 | goto err_neigh_entry_insert; |
| 688 | |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 689 | list_add(&neigh_entry->rif_list_node, &r->neigh_list); |
| 690 | |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 691 | return neigh_entry; |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 692 | |
| 693 | err_neigh_entry_insert: |
| 694 | mlxsw_sp_neigh_entry_free(neigh_entry); |
| 695 | return ERR_PTR(err); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static void |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 699 | mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp, |
| 700 | struct mlxsw_sp_neigh_entry *neigh_entry) |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 701 | { |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 702 | list_del(&neigh_entry->rif_list_node); |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 703 | mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry); |
| 704 | mlxsw_sp_neigh_entry_free(neigh_entry); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static struct mlxsw_sp_neigh_entry * |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 708 | mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n) |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 709 | { |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 710 | struct mlxsw_sp_neigh_key key; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 711 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 712 | key.n = n; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 713 | return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht, |
| 714 | &key, mlxsw_sp_neigh_ht_params); |
| 715 | } |
| 716 | |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 717 | static void |
| 718 | mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp) |
| 719 | { |
| 720 | unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME); |
| 721 | |
| 722 | mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval); |
| 723 | } |
| 724 | |
| 725 | static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp, |
| 726 | char *rauhtd_pl, |
| 727 | int ent_index) |
| 728 | { |
| 729 | struct net_device *dev; |
| 730 | struct neighbour *n; |
| 731 | __be32 dipn; |
| 732 | u32 dip; |
| 733 | u16 rif; |
| 734 | |
| 735 | mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip); |
| 736 | |
| 737 | if (!mlxsw_sp->rifs[rif]) { |
| 738 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n"); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | dipn = htonl(dip); |
| 743 | dev = mlxsw_sp->rifs[rif]->dev; |
| 744 | n = neigh_lookup(&arp_tbl, &dipn, dev); |
| 745 | if (!n) { |
| 746 | netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n", |
| 747 | &dip); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip); |
| 752 | neigh_event_send(n, NULL); |
| 753 | neigh_release(n); |
| 754 | } |
| 755 | |
| 756 | static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp, |
| 757 | char *rauhtd_pl, |
| 758 | int rec_index) |
| 759 | { |
| 760 | u8 num_entries; |
| 761 | int i; |
| 762 | |
| 763 | num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl, |
| 764 | rec_index); |
| 765 | /* Hardware starts counting at 0, so add 1. */ |
| 766 | num_entries++; |
| 767 | |
| 768 | /* Each record consists of several neighbour entries. */ |
| 769 | for (i = 0; i < num_entries; i++) { |
| 770 | int ent_index; |
| 771 | |
| 772 | ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i; |
| 773 | mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl, |
| 774 | ent_index); |
| 775 | } |
| 776 | |
| 777 | } |
| 778 | |
| 779 | static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp, |
| 780 | char *rauhtd_pl, int rec_index) |
| 781 | { |
| 782 | switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) { |
| 783 | case MLXSW_REG_RAUHTD_TYPE_IPV4: |
| 784 | mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl, |
| 785 | rec_index); |
| 786 | break; |
| 787 | case MLXSW_REG_RAUHTD_TYPE_IPV6: |
| 788 | WARN_ON_ONCE(1); |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | |
Arkadi Sharshevsky | 42cdb33 | 2016-11-11 16:34:26 +0100 | [diff] [blame] | 793 | static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl) |
| 794 | { |
| 795 | u8 num_rec, last_rec_index, num_entries; |
| 796 | |
| 797 | num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl); |
| 798 | last_rec_index = num_rec - 1; |
| 799 | |
| 800 | if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM) |
| 801 | return false; |
| 802 | if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) == |
| 803 | MLXSW_REG_RAUHTD_TYPE_IPV6) |
| 804 | return true; |
| 805 | |
| 806 | num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl, |
| 807 | last_rec_index); |
| 808 | if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC) |
| 809 | return true; |
| 810 | return false; |
| 811 | } |
| 812 | |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 813 | static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp) |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 814 | { |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 815 | char *rauhtd_pl; |
| 816 | u8 num_rec; |
| 817 | int i, err; |
| 818 | |
| 819 | rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL); |
| 820 | if (!rauhtd_pl) |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 821 | return -ENOMEM; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 822 | |
| 823 | /* Make sure the neighbour's netdev isn't removed in the |
| 824 | * process. |
| 825 | */ |
| 826 | rtnl_lock(); |
| 827 | do { |
| 828 | mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4); |
| 829 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd), |
| 830 | rauhtd_pl); |
| 831 | if (err) { |
| 832 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n"); |
| 833 | break; |
| 834 | } |
| 835 | num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl); |
| 836 | for (i = 0; i < num_rec; i++) |
| 837 | mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl, |
| 838 | i); |
Arkadi Sharshevsky | 42cdb33 | 2016-11-11 16:34:26 +0100 | [diff] [blame] | 839 | } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl)); |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 840 | rtnl_unlock(); |
| 841 | |
| 842 | kfree(rauhtd_pl); |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 843 | return err; |
| 844 | } |
| 845 | |
| 846 | static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp) |
| 847 | { |
| 848 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 849 | |
| 850 | /* Take RTNL mutex here to prevent lists from changes */ |
| 851 | rtnl_lock(); |
| 852 | list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list, |
Ido Schimmel | 8a0b727 | 2017-02-06 16:20:15 +0100 | [diff] [blame] | 853 | nexthop_neighs_list_node) |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 854 | /* If this neigh have nexthops, make the kernel think this neigh |
| 855 | * is active regardless of the traffic. |
| 856 | */ |
Ido Schimmel | 8a0b727 | 2017-02-06 16:20:15 +0100 | [diff] [blame] | 857 | neigh_event_send(neigh_entry->key.n, NULL); |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 858 | rtnl_unlock(); |
| 859 | } |
| 860 | |
| 861 | static void |
| 862 | mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp) |
| 863 | { |
| 864 | unsigned long interval = mlxsw_sp->router.neighs_update.interval; |
| 865 | |
| 866 | mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, |
| 867 | msecs_to_jiffies(interval)); |
| 868 | } |
| 869 | |
| 870 | static void mlxsw_sp_router_neighs_update_work(struct work_struct *work) |
| 871 | { |
| 872 | struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp, |
| 873 | router.neighs_update.dw.work); |
| 874 | int err; |
| 875 | |
| 876 | err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp); |
| 877 | if (err) |
| 878 | dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity"); |
| 879 | |
| 880 | mlxsw_sp_router_neighs_update_nh(mlxsw_sp); |
| 881 | |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 882 | mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp); |
| 883 | } |
| 884 | |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 885 | static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work) |
| 886 | { |
| 887 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 888 | struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp, |
| 889 | router.nexthop_probe_dw.work); |
| 890 | |
| 891 | /* Iterate over nexthop neighbours, find those who are unresolved and |
| 892 | * send arp on them. This solves the chicken-egg problem when |
| 893 | * the nexthop wouldn't get offloaded until the neighbor is resolved |
| 894 | * but it wouldn't get resolved ever in case traffic is flowing in HW |
| 895 | * using different nexthop. |
| 896 | * |
| 897 | * Take RTNL mutex here to prevent lists from changes. |
| 898 | */ |
| 899 | rtnl_lock(); |
| 900 | list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list, |
Ido Schimmel | 8a0b727 | 2017-02-06 16:20:15 +0100 | [diff] [blame] | 901 | nexthop_neighs_list_node) |
Ido Schimmel | 01b1aa3 | 2017-02-06 16:20:16 +0100 | [diff] [blame] | 902 | if (!neigh_entry->connected) |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 903 | neigh_event_send(neigh_entry->key.n, NULL); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 904 | rtnl_unlock(); |
| 905 | |
| 906 | mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, |
| 907 | MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL); |
| 908 | } |
| 909 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 910 | static void |
| 911 | mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp, |
| 912 | struct mlxsw_sp_neigh_entry *neigh_entry, |
| 913 | bool removing); |
| 914 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 915 | static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding) |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 916 | { |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 917 | return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD : |
| 918 | MLXSW_REG_RAUHT_OP_WRITE_DELETE; |
| 919 | } |
| 920 | |
| 921 | static void |
| 922 | mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp, |
| 923 | struct mlxsw_sp_neigh_entry *neigh_entry, |
| 924 | enum mlxsw_reg_rauht_op op) |
| 925 | { |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 926 | struct neighbour *n = neigh_entry->key.n; |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 927 | u32 dip = ntohl(*((__be32 *) n->primary_key)); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 928 | char rauht_pl[MLXSW_REG_RAUHT_LEN]; |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 929 | |
| 930 | mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha, |
| 931 | dip); |
| 932 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl); |
| 933 | } |
| 934 | |
| 935 | static void |
| 936 | mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp, |
| 937 | struct mlxsw_sp_neigh_entry *neigh_entry, |
| 938 | bool adding) |
| 939 | { |
| 940 | if (!adding && !neigh_entry->connected) |
| 941 | return; |
| 942 | neigh_entry->connected = adding; |
| 943 | if (neigh_entry->key.n->tbl == &arp_tbl) |
| 944 | mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry, |
| 945 | mlxsw_sp_rauht_op(adding)); |
| 946 | else |
| 947 | WARN_ON_ONCE(1); |
| 948 | } |
| 949 | |
| 950 | struct mlxsw_sp_neigh_event_work { |
| 951 | struct work_struct work; |
| 952 | struct mlxsw_sp *mlxsw_sp; |
| 953 | struct neighbour *n; |
| 954 | }; |
| 955 | |
| 956 | static void mlxsw_sp_router_neigh_event_work(struct work_struct *work) |
| 957 | { |
| 958 | struct mlxsw_sp_neigh_event_work *neigh_work = |
| 959 | container_of(work, struct mlxsw_sp_neigh_event_work, work); |
| 960 | struct mlxsw_sp *mlxsw_sp = neigh_work->mlxsw_sp; |
| 961 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 962 | struct neighbour *n = neigh_work->n; |
| 963 | unsigned char ha[ETH_ALEN]; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 964 | bool entry_connected; |
Ido Schimmel | 93a87e5 | 2016-12-23 09:32:49 +0100 | [diff] [blame] | 965 | u8 nud_state, dead; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 966 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 967 | /* If these parameters are changed after we release the lock, |
| 968 | * then we are guaranteed to receive another event letting us |
| 969 | * know about it. |
| 970 | */ |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 971 | read_lock_bh(&n->lock); |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 972 | memcpy(ha, n->ha, ETH_ALEN); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 973 | nud_state = n->nud_state; |
Ido Schimmel | 93a87e5 | 2016-12-23 09:32:49 +0100 | [diff] [blame] | 974 | dead = n->dead; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 975 | read_unlock_bh(&n->lock); |
| 976 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 977 | rtnl_lock(); |
Ido Schimmel | 93a87e5 | 2016-12-23 09:32:49 +0100 | [diff] [blame] | 978 | entry_connected = nud_state & NUD_VALID && !dead; |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 979 | neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n); |
| 980 | if (!entry_connected && !neigh_entry) |
| 981 | goto out; |
| 982 | if (!neigh_entry) { |
| 983 | neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n); |
| 984 | if (IS_ERR(neigh_entry)) |
| 985 | goto out; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 988 | memcpy(neigh_entry->ha, ha, ETH_ALEN); |
| 989 | mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected); |
| 990 | mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected); |
| 991 | |
| 992 | if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list)) |
| 993 | mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry); |
| 994 | |
| 995 | out: |
| 996 | rtnl_unlock(); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 997 | neigh_release(n); |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 998 | kfree(neigh_work); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Jiri Pirko | e732263 | 2016-09-01 10:37:43 +0200 | [diff] [blame] | 1001 | int mlxsw_sp_router_netevent_event(struct notifier_block *unused, |
| 1002 | unsigned long event, void *ptr) |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1003 | { |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1004 | struct mlxsw_sp_neigh_event_work *neigh_work; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1005 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 1006 | struct mlxsw_sp *mlxsw_sp; |
| 1007 | unsigned long interval; |
| 1008 | struct neigh_parms *p; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1009 | struct neighbour *n; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1010 | |
| 1011 | switch (event) { |
| 1012 | case NETEVENT_DELAY_PROBE_TIME_UPDATE: |
| 1013 | p = ptr; |
| 1014 | |
| 1015 | /* We don't care about changes in the default table. */ |
| 1016 | if (!p->dev || p->tbl != &arp_tbl) |
| 1017 | return NOTIFY_DONE; |
| 1018 | |
| 1019 | /* We are in atomic context and can't take RTNL mutex, |
| 1020 | * so use RCU variant to walk the device chain. |
| 1021 | */ |
| 1022 | mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev); |
| 1023 | if (!mlxsw_sp_port) |
| 1024 | return NOTIFY_DONE; |
| 1025 | |
| 1026 | mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1027 | interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME)); |
| 1028 | mlxsw_sp->router.neighs_update.interval = interval; |
| 1029 | |
| 1030 | mlxsw_sp_port_dev_put(mlxsw_sp_port); |
| 1031 | break; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1032 | case NETEVENT_NEIGH_UPDATE: |
| 1033 | n = ptr; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1034 | |
| 1035 | if (n->tbl != &arp_tbl) |
| 1036 | return NOTIFY_DONE; |
| 1037 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1038 | mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1039 | if (!mlxsw_sp_port) |
| 1040 | return NOTIFY_DONE; |
| 1041 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1042 | neigh_work = kzalloc(sizeof(*neigh_work), GFP_ATOMIC); |
| 1043 | if (!neigh_work) { |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1044 | mlxsw_sp_port_dev_put(mlxsw_sp_port); |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1045 | return NOTIFY_BAD; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1046 | } |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1047 | |
| 1048 | INIT_WORK(&neigh_work->work, mlxsw_sp_router_neigh_event_work); |
| 1049 | neigh_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1050 | neigh_work->n = n; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1051 | |
| 1052 | /* Take a reference to ensure the neighbour won't be |
| 1053 | * destructed until we drop the reference in delayed |
| 1054 | * work. |
| 1055 | */ |
| 1056 | neigh_clone(n); |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1057 | mlxsw_core_schedule_work(&neigh_work->work); |
| 1058 | mlxsw_sp_port_dev_put(mlxsw_sp_port); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1059 | break; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | return NOTIFY_DONE; |
| 1063 | } |
| 1064 | |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 1065 | static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp) |
| 1066 | { |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1067 | int err; |
| 1068 | |
| 1069 | err = rhashtable_init(&mlxsw_sp->router.neigh_ht, |
| 1070 | &mlxsw_sp_neigh_ht_params); |
| 1071 | if (err) |
| 1072 | return err; |
| 1073 | |
| 1074 | /* Initialize the polling interval according to the default |
| 1075 | * table. |
| 1076 | */ |
| 1077 | mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp); |
| 1078 | |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1079 | /* Create the delayed works for the activity_update */ |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1080 | INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw, |
| 1081 | mlxsw_sp_router_neighs_update_work); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1082 | INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw, |
| 1083 | mlxsw_sp_router_probe_unresolved_nexthops); |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1084 | mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1085 | mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0); |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1086 | return 0; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp) |
| 1090 | { |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1091 | cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1092 | cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 1093 | rhashtable_destroy(&mlxsw_sp->router.neigh_ht); |
| 1094 | } |
| 1095 | |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1096 | static int mlxsw_sp_neigh_rif_flush(struct mlxsw_sp *mlxsw_sp, |
| 1097 | const struct mlxsw_sp_rif *r) |
| 1098 | { |
| 1099 | char rauht_pl[MLXSW_REG_RAUHT_LEN]; |
| 1100 | |
| 1101 | mlxsw_reg_rauht_pack(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE_ALL, |
| 1102 | r->rif, r->addr); |
| 1103 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl); |
| 1104 | } |
| 1105 | |
| 1106 | static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp, |
| 1107 | struct mlxsw_sp_rif *r) |
| 1108 | { |
| 1109 | struct mlxsw_sp_neigh_entry *neigh_entry, *tmp; |
| 1110 | |
| 1111 | mlxsw_sp_neigh_rif_flush(mlxsw_sp, r); |
| 1112 | list_for_each_entry_safe(neigh_entry, tmp, &r->neigh_list, |
| 1113 | rif_list_node) |
| 1114 | mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry); |
| 1115 | } |
| 1116 | |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1117 | struct mlxsw_sp_nexthop_key { |
| 1118 | struct fib_nh *fib_nh; |
| 1119 | }; |
| 1120 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1121 | struct mlxsw_sp_nexthop { |
| 1122 | struct list_head neigh_list_node; /* member of neigh entry list */ |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1123 | struct list_head rif_list_node; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1124 | struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group |
| 1125 | * this belongs to |
| 1126 | */ |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1127 | struct rhash_head ht_node; |
| 1128 | struct mlxsw_sp_nexthop_key key; |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1129 | struct mlxsw_sp_rif *r; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1130 | u8 should_offload:1, /* set indicates this neigh is connected and |
| 1131 | * should be put to KVD linear area of this group. |
| 1132 | */ |
| 1133 | offloaded:1, /* set in case the neigh is actually put into |
| 1134 | * KVD linear area of this group. |
| 1135 | */ |
| 1136 | update:1; /* set indicates that MAC of this neigh should be |
| 1137 | * updated in HW |
| 1138 | */ |
| 1139 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 1140 | }; |
| 1141 | |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1142 | struct mlxsw_sp_nexthop_group_key { |
| 1143 | struct fib_info *fi; |
| 1144 | }; |
| 1145 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1146 | struct mlxsw_sp_nexthop_group { |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1147 | struct rhash_head ht_node; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1148 | struct list_head fib_list; /* list of fib entries that use this group */ |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1149 | struct mlxsw_sp_nexthop_group_key key; |
Ido Schimmel | b3e8d1e | 2017-02-08 11:16:32 +0100 | [diff] [blame] | 1150 | u8 adj_index_valid:1, |
| 1151 | gateway:1; /* routes using the group use a gateway */ |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1152 | u32 adj_index; |
| 1153 | u16 ecmp_size; |
| 1154 | u16 count; |
| 1155 | struct mlxsw_sp_nexthop nexthops[0]; |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1156 | #define nh_rif nexthops[0].r |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1157 | }; |
| 1158 | |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1159 | static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = { |
| 1160 | .key_offset = offsetof(struct mlxsw_sp_nexthop_group, key), |
| 1161 | .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node), |
| 1162 | .key_len = sizeof(struct mlxsw_sp_nexthop_group_key), |
| 1163 | }; |
| 1164 | |
| 1165 | static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp, |
| 1166 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1167 | { |
| 1168 | return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_group_ht, |
| 1169 | &nh_grp->ht_node, |
| 1170 | mlxsw_sp_nexthop_group_ht_params); |
| 1171 | } |
| 1172 | |
| 1173 | static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp, |
| 1174 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1175 | { |
| 1176 | rhashtable_remove_fast(&mlxsw_sp->router.nexthop_group_ht, |
| 1177 | &nh_grp->ht_node, |
| 1178 | mlxsw_sp_nexthop_group_ht_params); |
| 1179 | } |
| 1180 | |
| 1181 | static struct mlxsw_sp_nexthop_group * |
| 1182 | mlxsw_sp_nexthop_group_lookup(struct mlxsw_sp *mlxsw_sp, |
| 1183 | struct mlxsw_sp_nexthop_group_key key) |
| 1184 | { |
| 1185 | return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_group_ht, &key, |
| 1186 | mlxsw_sp_nexthop_group_ht_params); |
| 1187 | } |
| 1188 | |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1189 | static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = { |
| 1190 | .key_offset = offsetof(struct mlxsw_sp_nexthop, key), |
| 1191 | .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node), |
| 1192 | .key_len = sizeof(struct mlxsw_sp_nexthop_key), |
| 1193 | }; |
| 1194 | |
| 1195 | static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp, |
| 1196 | struct mlxsw_sp_nexthop *nh) |
| 1197 | { |
| 1198 | return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_ht, |
| 1199 | &nh->ht_node, mlxsw_sp_nexthop_ht_params); |
| 1200 | } |
| 1201 | |
| 1202 | static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp, |
| 1203 | struct mlxsw_sp_nexthop *nh) |
| 1204 | { |
| 1205 | rhashtable_remove_fast(&mlxsw_sp->router.nexthop_ht, &nh->ht_node, |
| 1206 | mlxsw_sp_nexthop_ht_params); |
| 1207 | } |
| 1208 | |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 1209 | static struct mlxsw_sp_nexthop * |
| 1210 | mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp, |
| 1211 | struct mlxsw_sp_nexthop_key key) |
| 1212 | { |
| 1213 | return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_ht, &key, |
| 1214 | mlxsw_sp_nexthop_ht_params); |
| 1215 | } |
| 1216 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1217 | static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp, |
| 1218 | struct mlxsw_sp_vr *vr, |
| 1219 | u32 adj_index, u16 ecmp_size, |
| 1220 | u32 new_adj_index, |
| 1221 | u16 new_ecmp_size) |
| 1222 | { |
| 1223 | char raleu_pl[MLXSW_REG_RALEU_LEN]; |
| 1224 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1225 | mlxsw_reg_raleu_pack(raleu_pl, |
| 1226 | (enum mlxsw_reg_ralxx_protocol) vr->proto, vr->id, |
| 1227 | adj_index, ecmp_size, new_adj_index, |
| 1228 | new_ecmp_size); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1229 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl); |
| 1230 | } |
| 1231 | |
| 1232 | static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp, |
| 1233 | struct mlxsw_sp_nexthop_group *nh_grp, |
| 1234 | u32 old_adj_index, u16 old_ecmp_size) |
| 1235 | { |
| 1236 | struct mlxsw_sp_fib_entry *fib_entry; |
| 1237 | struct mlxsw_sp_vr *vr = NULL; |
| 1238 | int err; |
| 1239 | |
| 1240 | list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) { |
| 1241 | if (vr == fib_entry->vr) |
| 1242 | continue; |
| 1243 | vr = fib_entry->vr; |
| 1244 | err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, vr, |
| 1245 | old_adj_index, |
| 1246 | old_ecmp_size, |
| 1247 | nh_grp->adj_index, |
| 1248 | nh_grp->ecmp_size); |
| 1249 | if (err) |
| 1250 | return err; |
| 1251 | } |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index, |
| 1256 | struct mlxsw_sp_nexthop *nh) |
| 1257 | { |
| 1258 | struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry; |
| 1259 | char ratr_pl[MLXSW_REG_RATR_LEN]; |
| 1260 | |
| 1261 | mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY, |
| 1262 | true, adj_index, neigh_entry->rif); |
| 1263 | mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha); |
| 1264 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl); |
| 1265 | } |
| 1266 | |
| 1267 | static int |
| 1268 | mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp, |
Ido Schimmel | a59b7e0 | 2017-01-23 11:11:42 +0100 | [diff] [blame] | 1269 | struct mlxsw_sp_nexthop_group *nh_grp, |
| 1270 | bool reallocate) |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1271 | { |
| 1272 | u32 adj_index = nh_grp->adj_index; /* base */ |
| 1273 | struct mlxsw_sp_nexthop *nh; |
| 1274 | int i; |
| 1275 | int err; |
| 1276 | |
| 1277 | for (i = 0; i < nh_grp->count; i++) { |
| 1278 | nh = &nh_grp->nexthops[i]; |
| 1279 | |
| 1280 | if (!nh->should_offload) { |
| 1281 | nh->offloaded = 0; |
| 1282 | continue; |
| 1283 | } |
| 1284 | |
Ido Schimmel | a59b7e0 | 2017-01-23 11:11:42 +0100 | [diff] [blame] | 1285 | if (nh->update || reallocate) { |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1286 | err = mlxsw_sp_nexthop_mac_update(mlxsw_sp, |
| 1287 | adj_index, nh); |
| 1288 | if (err) |
| 1289 | return err; |
| 1290 | nh->update = 0; |
| 1291 | nh->offloaded = 1; |
| 1292 | } |
| 1293 | adj_index++; |
| 1294 | } |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp, |
| 1299 | struct mlxsw_sp_fib_entry *fib_entry); |
| 1300 | |
| 1301 | static int |
| 1302 | mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp, |
| 1303 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1304 | { |
| 1305 | struct mlxsw_sp_fib_entry *fib_entry; |
| 1306 | int err; |
| 1307 | |
| 1308 | list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) { |
| 1309 | err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry); |
| 1310 | if (err) |
| 1311 | return err; |
| 1312 | } |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | static void |
| 1317 | mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp, |
| 1318 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1319 | { |
| 1320 | struct mlxsw_sp_nexthop *nh; |
| 1321 | bool offload_change = false; |
| 1322 | u32 adj_index; |
| 1323 | u16 ecmp_size = 0; |
| 1324 | bool old_adj_index_valid; |
| 1325 | u32 old_adj_index; |
| 1326 | u16 old_ecmp_size; |
| 1327 | int ret; |
| 1328 | int i; |
| 1329 | int err; |
| 1330 | |
Ido Schimmel | b3e8d1e | 2017-02-08 11:16:32 +0100 | [diff] [blame] | 1331 | if (!nh_grp->gateway) { |
| 1332 | mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp); |
| 1333 | return; |
| 1334 | } |
| 1335 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1336 | for (i = 0; i < nh_grp->count; i++) { |
| 1337 | nh = &nh_grp->nexthops[i]; |
| 1338 | |
| 1339 | if (nh->should_offload ^ nh->offloaded) { |
| 1340 | offload_change = true; |
| 1341 | if (nh->should_offload) |
| 1342 | nh->update = 1; |
| 1343 | } |
| 1344 | if (nh->should_offload) |
| 1345 | ecmp_size++; |
| 1346 | } |
| 1347 | if (!offload_change) { |
| 1348 | /* Nothing was added or removed, so no need to reallocate. Just |
| 1349 | * update MAC on existing adjacency indexes. |
| 1350 | */ |
Ido Schimmel | a59b7e0 | 2017-01-23 11:11:42 +0100 | [diff] [blame] | 1351 | err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, |
| 1352 | false); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1353 | if (err) { |
| 1354 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n"); |
| 1355 | goto set_trap; |
| 1356 | } |
| 1357 | return; |
| 1358 | } |
| 1359 | if (!ecmp_size) |
| 1360 | /* No neigh of this group is connected so we just set |
| 1361 | * the trap and let everthing flow through kernel. |
| 1362 | */ |
| 1363 | goto set_trap; |
| 1364 | |
| 1365 | ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size); |
| 1366 | if (ret < 0) { |
| 1367 | /* We ran out of KVD linear space, just set the |
| 1368 | * trap and let everything flow through kernel. |
| 1369 | */ |
| 1370 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n"); |
| 1371 | goto set_trap; |
| 1372 | } |
| 1373 | adj_index = ret; |
| 1374 | old_adj_index_valid = nh_grp->adj_index_valid; |
| 1375 | old_adj_index = nh_grp->adj_index; |
| 1376 | old_ecmp_size = nh_grp->ecmp_size; |
| 1377 | nh_grp->adj_index_valid = 1; |
| 1378 | nh_grp->adj_index = adj_index; |
| 1379 | nh_grp->ecmp_size = ecmp_size; |
Ido Schimmel | a59b7e0 | 2017-01-23 11:11:42 +0100 | [diff] [blame] | 1380 | err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, true); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1381 | if (err) { |
| 1382 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n"); |
| 1383 | goto set_trap; |
| 1384 | } |
| 1385 | |
| 1386 | if (!old_adj_index_valid) { |
| 1387 | /* The trap was set for fib entries, so we have to call |
| 1388 | * fib entry update to unset it and use adjacency index. |
| 1389 | */ |
| 1390 | err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp); |
| 1391 | if (err) { |
| 1392 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n"); |
| 1393 | goto set_trap; |
| 1394 | } |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp, |
| 1399 | old_adj_index, old_ecmp_size); |
| 1400 | mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index); |
| 1401 | if (err) { |
| 1402 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n"); |
| 1403 | goto set_trap; |
| 1404 | } |
| 1405 | return; |
| 1406 | |
| 1407 | set_trap: |
| 1408 | old_adj_index_valid = nh_grp->adj_index_valid; |
| 1409 | nh_grp->adj_index_valid = 0; |
| 1410 | for (i = 0; i < nh_grp->count; i++) { |
| 1411 | nh = &nh_grp->nexthops[i]; |
| 1412 | nh->offloaded = 0; |
| 1413 | } |
| 1414 | err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp); |
| 1415 | if (err) |
| 1416 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n"); |
| 1417 | if (old_adj_index_valid) |
| 1418 | mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index); |
| 1419 | } |
| 1420 | |
| 1421 | static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh, |
| 1422 | bool removing) |
| 1423 | { |
| 1424 | if (!removing && !nh->should_offload) |
| 1425 | nh->should_offload = 1; |
| 1426 | else if (removing && nh->offloaded) |
| 1427 | nh->should_offload = 0; |
| 1428 | nh->update = 1; |
| 1429 | } |
| 1430 | |
| 1431 | static void |
| 1432 | mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp, |
| 1433 | struct mlxsw_sp_neigh_entry *neigh_entry, |
| 1434 | bool removing) |
| 1435 | { |
| 1436 | struct mlxsw_sp_nexthop *nh; |
| 1437 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1438 | list_for_each_entry(nh, &neigh_entry->nexthop_list, |
| 1439 | neigh_list_node) { |
| 1440 | __mlxsw_sp_nexthop_neigh_update(nh, removing); |
| 1441 | mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp); |
| 1442 | } |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1445 | static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh, |
| 1446 | struct mlxsw_sp_rif *r) |
| 1447 | { |
| 1448 | if (nh->r) |
| 1449 | return; |
| 1450 | |
| 1451 | nh->r = r; |
| 1452 | list_add(&nh->rif_list_node, &r->nexthop_list); |
| 1453 | } |
| 1454 | |
| 1455 | static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh) |
| 1456 | { |
| 1457 | if (!nh->r) |
| 1458 | return; |
| 1459 | |
| 1460 | list_del(&nh->rif_list_node); |
| 1461 | nh->r = NULL; |
| 1462 | } |
| 1463 | |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1464 | static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp, |
| 1465 | struct mlxsw_sp_nexthop *nh) |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1466 | { |
| 1467 | struct mlxsw_sp_neigh_entry *neigh_entry; |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1468 | struct fib_nh *fib_nh = nh->key.fib_nh; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1469 | struct neighbour *n; |
Ido Schimmel | 93a87e5 | 2016-12-23 09:32:49 +0100 | [diff] [blame] | 1470 | u8 nud_state, dead; |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1471 | int err; |
| 1472 | |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 1473 | if (!nh->nh_grp->gateway || nh->neigh_entry) |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1474 | return 0; |
| 1475 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1476 | /* Take a reference of neigh here ensuring that neigh would |
| 1477 | * not be detructed before the nexthop entry is finished. |
| 1478 | * The reference is taken either in neigh_lookup() or |
Ido Schimmel | fd76d91 | 2017-02-06 16:20:17 +0100 | [diff] [blame] | 1479 | * in neigh_create() in case n is not found. |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1480 | */ |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1481 | n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev); |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1482 | if (!n) { |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1483 | n = neigh_create(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev); |
| 1484 | if (IS_ERR(n)) |
| 1485 | return PTR_ERR(n); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1486 | neigh_event_send(n, NULL); |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1487 | } |
| 1488 | neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n); |
| 1489 | if (!neigh_entry) { |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1490 | neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n); |
| 1491 | if (IS_ERR(neigh_entry)) { |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1492 | err = -EINVAL; |
| 1493 | goto err_neigh_entry_create; |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1494 | } |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1495 | } |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 1496 | |
| 1497 | /* If that is the first nexthop connected to that neigh, add to |
| 1498 | * nexthop_neighs_list |
| 1499 | */ |
| 1500 | if (list_empty(&neigh_entry->nexthop_list)) |
| 1501 | list_add_tail(&neigh_entry->nexthop_neighs_list_node, |
| 1502 | &mlxsw_sp->router.nexthop_neighs_list); |
| 1503 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1504 | nh->neigh_entry = neigh_entry; |
| 1505 | list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list); |
| 1506 | read_lock_bh(&n->lock); |
| 1507 | nud_state = n->nud_state; |
Ido Schimmel | 93a87e5 | 2016-12-23 09:32:49 +0100 | [diff] [blame] | 1508 | dead = n->dead; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1509 | read_unlock_bh(&n->lock); |
Ido Schimmel | 93a87e5 | 2016-12-23 09:32:49 +0100 | [diff] [blame] | 1510 | __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead)); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1511 | |
| 1512 | return 0; |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1513 | |
| 1514 | err_neigh_entry_create: |
| 1515 | neigh_release(n); |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1516 | return err; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1517 | } |
| 1518 | |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1519 | static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp, |
| 1520 | struct mlxsw_sp_nexthop *nh) |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1521 | { |
| 1522 | struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry; |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1523 | struct neighbour *n; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1524 | |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1525 | if (!neigh_entry) |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1526 | return; |
| 1527 | n = neigh_entry->key.n; |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1528 | |
Ido Schimmel | 5831212 | 2016-12-23 09:32:50 +0100 | [diff] [blame] | 1529 | __mlxsw_sp_nexthop_neigh_update(nh, true); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1530 | list_del(&nh->neigh_list_node); |
Ido Schimmel | e58be79 | 2017-02-08 11:16:28 +0100 | [diff] [blame] | 1531 | nh->neigh_entry = NULL; |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 1532 | |
| 1533 | /* If that is the last nexthop connected to that neigh, remove from |
| 1534 | * nexthop_neighs_list |
| 1535 | */ |
Ido Schimmel | e58be79 | 2017-02-08 11:16:28 +0100 | [diff] [blame] | 1536 | if (list_empty(&neigh_entry->nexthop_list)) |
| 1537 | list_del(&neigh_entry->nexthop_neighs_list_node); |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 1538 | |
Ido Schimmel | 5c8802f | 2017-02-06 16:20:13 +0100 | [diff] [blame] | 1539 | if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list)) |
| 1540 | mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry); |
| 1541 | |
| 1542 | neigh_release(n); |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1543 | } |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1544 | |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1545 | static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp, |
| 1546 | struct mlxsw_sp_nexthop_group *nh_grp, |
| 1547 | struct mlxsw_sp_nexthop *nh, |
| 1548 | struct fib_nh *fib_nh) |
| 1549 | { |
| 1550 | struct net_device *dev = fib_nh->nh_dev; |
| 1551 | struct mlxsw_sp_rif *r; |
| 1552 | int err; |
| 1553 | |
| 1554 | nh->nh_grp = nh_grp; |
| 1555 | nh->key.fib_nh = fib_nh; |
| 1556 | err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh); |
| 1557 | if (err) |
| 1558 | return err; |
| 1559 | |
| 1560 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev); |
| 1561 | if (!r) |
| 1562 | return 0; |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1563 | mlxsw_sp_nexthop_rif_init(nh, r); |
Ido Schimmel | a8c9701 | 2017-02-08 11:16:35 +0100 | [diff] [blame] | 1564 | |
| 1565 | err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh); |
| 1566 | if (err) |
| 1567 | goto err_nexthop_neigh_init; |
| 1568 | |
| 1569 | return 0; |
| 1570 | |
| 1571 | err_nexthop_neigh_init: |
| 1572 | mlxsw_sp_nexthop_remove(mlxsw_sp, nh); |
| 1573 | return err; |
| 1574 | } |
| 1575 | |
| 1576 | static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp, |
| 1577 | struct mlxsw_sp_nexthop *nh) |
| 1578 | { |
| 1579 | mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh); |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1580 | mlxsw_sp_nexthop_rif_fini(nh); |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 1581 | mlxsw_sp_nexthop_remove(mlxsw_sp, nh); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 1584 | static void mlxsw_sp_nexthop_event(struct mlxsw_sp *mlxsw_sp, |
| 1585 | unsigned long event, struct fib_nh *fib_nh) |
| 1586 | { |
| 1587 | struct mlxsw_sp_nexthop_key key; |
| 1588 | struct mlxsw_sp_nexthop *nh; |
| 1589 | struct mlxsw_sp_rif *r; |
| 1590 | |
| 1591 | if (mlxsw_sp->router.aborted) |
| 1592 | return; |
| 1593 | |
| 1594 | key.fib_nh = fib_nh; |
| 1595 | nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key); |
| 1596 | if (WARN_ON_ONCE(!nh)) |
| 1597 | return; |
| 1598 | |
| 1599 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fib_nh->nh_dev); |
| 1600 | if (!r) |
| 1601 | return; |
| 1602 | |
| 1603 | switch (event) { |
| 1604 | case FIB_EVENT_NH_ADD: |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1605 | mlxsw_sp_nexthop_rif_init(nh, r); |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 1606 | mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh); |
| 1607 | break; |
| 1608 | case FIB_EVENT_NH_DEL: |
| 1609 | mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh); |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1610 | mlxsw_sp_nexthop_rif_fini(nh); |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 1611 | break; |
| 1612 | } |
| 1613 | |
| 1614 | mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp); |
| 1615 | } |
| 1616 | |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 1617 | static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp, |
| 1618 | struct mlxsw_sp_rif *r) |
| 1619 | { |
| 1620 | struct mlxsw_sp_nexthop *nh, *tmp; |
| 1621 | |
| 1622 | list_for_each_entry_safe(nh, tmp, &r->nexthop_list, rif_list_node) { |
| 1623 | mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh); |
| 1624 | mlxsw_sp_nexthop_rif_fini(nh); |
| 1625 | mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp); |
| 1626 | } |
| 1627 | } |
| 1628 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1629 | static struct mlxsw_sp_nexthop_group * |
| 1630 | mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi) |
| 1631 | { |
| 1632 | struct mlxsw_sp_nexthop_group *nh_grp; |
| 1633 | struct mlxsw_sp_nexthop *nh; |
| 1634 | struct fib_nh *fib_nh; |
| 1635 | size_t alloc_size; |
| 1636 | int i; |
| 1637 | int err; |
| 1638 | |
| 1639 | alloc_size = sizeof(*nh_grp) + |
| 1640 | fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop); |
| 1641 | nh_grp = kzalloc(alloc_size, GFP_KERNEL); |
| 1642 | if (!nh_grp) |
| 1643 | return ERR_PTR(-ENOMEM); |
| 1644 | INIT_LIST_HEAD(&nh_grp->fib_list); |
Ido Schimmel | b3e8d1e | 2017-02-08 11:16:32 +0100 | [diff] [blame] | 1645 | nh_grp->gateway = fi->fib_nh->nh_scope == RT_SCOPE_LINK; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1646 | nh_grp->count = fi->fib_nhs; |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1647 | nh_grp->key.fi = fi; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1648 | for (i = 0; i < nh_grp->count; i++) { |
| 1649 | nh = &nh_grp->nexthops[i]; |
| 1650 | fib_nh = &fi->fib_nh[i]; |
| 1651 | err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh); |
| 1652 | if (err) |
| 1653 | goto err_nexthop_init; |
| 1654 | } |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1655 | err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp); |
| 1656 | if (err) |
| 1657 | goto err_nexthop_group_insert; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1658 | mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp); |
| 1659 | return nh_grp; |
| 1660 | |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1661 | err_nexthop_group_insert: |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1662 | err_nexthop_init: |
| 1663 | for (i--; i >= 0; i--) |
| 1664 | mlxsw_sp_nexthop_fini(mlxsw_sp, nh); |
| 1665 | kfree(nh_grp); |
| 1666 | return ERR_PTR(err); |
| 1667 | } |
| 1668 | |
| 1669 | static void |
| 1670 | mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp, |
| 1671 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1672 | { |
| 1673 | struct mlxsw_sp_nexthop *nh; |
| 1674 | int i; |
| 1675 | |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1676 | mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1677 | for (i = 0; i < nh_grp->count; i++) { |
| 1678 | nh = &nh_grp->nexthops[i]; |
| 1679 | mlxsw_sp_nexthop_fini(mlxsw_sp, nh); |
| 1680 | } |
Ido Schimmel | 5831212 | 2016-12-23 09:32:50 +0100 | [diff] [blame] | 1681 | mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp); |
| 1682 | WARN_ON_ONCE(nh_grp->adj_index_valid); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1683 | kfree(nh_grp); |
| 1684 | } |
| 1685 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1686 | static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp, |
| 1687 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1688 | struct fib_info *fi) |
| 1689 | { |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1690 | struct mlxsw_sp_nexthop_group_key key; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1691 | struct mlxsw_sp_nexthop_group *nh_grp; |
| 1692 | |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 1693 | key.fi = fi; |
| 1694 | nh_grp = mlxsw_sp_nexthop_group_lookup(mlxsw_sp, key); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1695 | if (!nh_grp) { |
| 1696 | nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi); |
| 1697 | if (IS_ERR(nh_grp)) |
| 1698 | return PTR_ERR(nh_grp); |
| 1699 | } |
| 1700 | list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list); |
| 1701 | fib_entry->nh_group = nh_grp; |
| 1702 | return 0; |
| 1703 | } |
| 1704 | |
| 1705 | static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp, |
| 1706 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1707 | { |
| 1708 | struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group; |
| 1709 | |
| 1710 | list_del(&fib_entry->nexthop_group_node); |
| 1711 | if (!list_empty(&nh_grp->fib_list)) |
| 1712 | return; |
| 1713 | mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp); |
| 1714 | } |
| 1715 | |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1716 | static bool |
| 1717 | mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry) |
| 1718 | { |
| 1719 | struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group; |
| 1720 | |
| 1721 | switch (fib_entry->type) { |
| 1722 | case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE: |
| 1723 | return !!nh_group->adj_index_valid; |
| 1724 | case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL: |
Ido Schimmel | 70ad350 | 2017-02-08 11:16:38 +0100 | [diff] [blame] | 1725 | return !!nh_group->nh_rif; |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1726 | default: |
| 1727 | return false; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry) |
| 1732 | { |
| 1733 | fib_entry->offloaded = true; |
| 1734 | |
| 1735 | switch (fib_entry->vr->proto) { |
| 1736 | case MLXSW_SP_L3_PROTO_IPV4: |
| 1737 | fib_info_offload_inc(fib_entry->nh_group->key.fi); |
| 1738 | break; |
| 1739 | case MLXSW_SP_L3_PROTO_IPV6: |
| 1740 | WARN_ON_ONCE(1); |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | static void |
| 1745 | mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry) |
| 1746 | { |
| 1747 | switch (fib_entry->vr->proto) { |
| 1748 | case MLXSW_SP_L3_PROTO_IPV4: |
| 1749 | fib_info_offload_dec(fib_entry->nh_group->key.fi); |
| 1750 | break; |
| 1751 | case MLXSW_SP_L3_PROTO_IPV6: |
| 1752 | WARN_ON_ONCE(1); |
| 1753 | } |
| 1754 | |
| 1755 | fib_entry->offloaded = false; |
| 1756 | } |
| 1757 | |
| 1758 | static void |
| 1759 | mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry, |
| 1760 | enum mlxsw_reg_ralue_op op, int err) |
| 1761 | { |
| 1762 | switch (op) { |
| 1763 | case MLXSW_REG_RALUE_OP_WRITE_DELETE: |
| 1764 | if (!fib_entry->offloaded) |
| 1765 | return; |
| 1766 | return mlxsw_sp_fib_entry_offload_unset(fib_entry); |
| 1767 | case MLXSW_REG_RALUE_OP_WRITE_WRITE: |
| 1768 | if (err) |
| 1769 | return; |
| 1770 | if (mlxsw_sp_fib_entry_should_offload(fib_entry) && |
| 1771 | !fib_entry->offloaded) |
| 1772 | mlxsw_sp_fib_entry_offload_set(fib_entry); |
| 1773 | else if (!mlxsw_sp_fib_entry_should_offload(fib_entry) && |
| 1774 | fib_entry->offloaded) |
| 1775 | mlxsw_sp_fib_entry_offload_unset(fib_entry); |
| 1776 | return; |
| 1777 | default: |
| 1778 | return; |
| 1779 | } |
| 1780 | } |
| 1781 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1782 | static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp, |
| 1783 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1784 | enum mlxsw_reg_ralue_op op) |
| 1785 | { |
| 1786 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 1787 | u32 *p_dip = (u32 *) fib_entry->key.addr; |
| 1788 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
| 1789 | enum mlxsw_reg_ralue_trap_action trap_action; |
| 1790 | u16 trap_id = 0; |
| 1791 | u32 adjacency_index = 0; |
| 1792 | u16 ecmp_size = 0; |
| 1793 | |
| 1794 | /* In case the nexthop group adjacency index is valid, use it |
| 1795 | * with provided ECMP size. Otherwise, setup trap and pass |
| 1796 | * traffic to kernel. |
| 1797 | */ |
Ido Schimmel | 4b41147 | 2017-02-08 11:16:37 +0100 | [diff] [blame] | 1798 | if (mlxsw_sp_fib_entry_should_offload(fib_entry)) { |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1799 | trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP; |
| 1800 | adjacency_index = fib_entry->nh_group->adj_index; |
| 1801 | ecmp_size = fib_entry->nh_group->ecmp_size; |
| 1802 | } else { |
| 1803 | trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP; |
| 1804 | trap_id = MLXSW_TRAP_ID_RTR_INGRESS0; |
| 1805 | } |
| 1806 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1807 | mlxsw_reg_ralue_pack4(ralue_pl, |
| 1808 | (enum mlxsw_reg_ralxx_protocol) vr->proto, op, |
| 1809 | vr->id, fib_entry->key.prefix_len, *p_dip); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1810 | mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id, |
| 1811 | adjacency_index, ecmp_size); |
| 1812 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 1813 | } |
| 1814 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1815 | static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp, |
| 1816 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1817 | enum mlxsw_reg_ralue_op op) |
| 1818 | { |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1819 | struct mlxsw_sp_rif *r = fib_entry->nh_group->nh_rif; |
Ido Schimmel | 70ad350 | 2017-02-08 11:16:38 +0100 | [diff] [blame] | 1820 | enum mlxsw_reg_ralue_trap_action trap_action; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1821 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 1822 | u32 *p_dip = (u32 *) fib_entry->key.addr; |
| 1823 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
Ido Schimmel | 70ad350 | 2017-02-08 11:16:38 +0100 | [diff] [blame] | 1824 | u16 trap_id = 0; |
| 1825 | u16 rif = 0; |
| 1826 | |
| 1827 | if (mlxsw_sp_fib_entry_should_offload(fib_entry)) { |
| 1828 | trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP; |
| 1829 | rif = r->rif; |
| 1830 | } else { |
| 1831 | trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP; |
| 1832 | trap_id = MLXSW_TRAP_ID_RTR_INGRESS0; |
| 1833 | } |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1834 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1835 | mlxsw_reg_ralue_pack4(ralue_pl, |
| 1836 | (enum mlxsw_reg_ralxx_protocol) vr->proto, op, |
| 1837 | vr->id, fib_entry->key.prefix_len, *p_dip); |
Ido Schimmel | 70ad350 | 2017-02-08 11:16:38 +0100 | [diff] [blame] | 1838 | mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id, rif); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1839 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 1840 | } |
| 1841 | |
| 1842 | static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp, |
| 1843 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1844 | enum mlxsw_reg_ralue_op op) |
| 1845 | { |
| 1846 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 1847 | u32 *p_dip = (u32 *) fib_entry->key.addr; |
| 1848 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
| 1849 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1850 | mlxsw_reg_ralue_pack4(ralue_pl, |
| 1851 | (enum mlxsw_reg_ralxx_protocol) vr->proto, op, |
| 1852 | vr->id, fib_entry->key.prefix_len, *p_dip); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1853 | mlxsw_reg_ralue_act_ip2me_pack(ralue_pl); |
| 1854 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 1855 | } |
| 1856 | |
| 1857 | static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp, |
| 1858 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1859 | enum mlxsw_reg_ralue_op op) |
| 1860 | { |
| 1861 | switch (fib_entry->type) { |
| 1862 | case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE: |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1863 | return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1864 | case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL: |
| 1865 | return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op); |
| 1866 | case MLXSW_SP_FIB_ENTRY_TYPE_TRAP: |
| 1867 | return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op); |
| 1868 | } |
| 1869 | return -EINVAL; |
| 1870 | } |
| 1871 | |
| 1872 | static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp, |
| 1873 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1874 | enum mlxsw_reg_ralue_op op) |
| 1875 | { |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1876 | int err = -EINVAL; |
| 1877 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1878 | switch (fib_entry->vr->proto) { |
| 1879 | case MLXSW_SP_L3_PROTO_IPV4: |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1880 | err = mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op); |
| 1881 | break; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1882 | case MLXSW_SP_L3_PROTO_IPV6: |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1883 | return err; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1884 | } |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1885 | mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err); |
| 1886 | return err; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp, |
| 1890 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1891 | { |
Jiri Pirko | 7146da3 | 2016-09-01 10:37:41 +0200 | [diff] [blame] | 1892 | return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, |
| 1893 | MLXSW_REG_RALUE_OP_WRITE_WRITE); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp, |
| 1897 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1898 | { |
| 1899 | return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, |
| 1900 | MLXSW_REG_RALUE_OP_WRITE_DELETE); |
| 1901 | } |
| 1902 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1903 | static int |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1904 | mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp, |
| 1905 | const struct fib_entry_notifier_info *fen_info, |
| 1906 | struct mlxsw_sp_fib_entry *fib_entry) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1907 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1908 | struct fib_info *fi = fen_info->fi; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1909 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1910 | if (fen_info->type == RTN_LOCAL || fen_info->type == RTN_BROADCAST) { |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1911 | fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP; |
| 1912 | return 0; |
| 1913 | } |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1914 | if (fen_info->type != RTN_UNICAST) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1915 | return -EINVAL; |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1916 | if (fi->fib_nh->nh_scope != RT_SCOPE_LINK) |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1917 | fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL; |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1918 | else |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1919 | fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1920 | return 0; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1921 | } |
| 1922 | |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1923 | static struct mlxsw_sp_fib_entry * |
| 1924 | mlxsw_sp_fib_entry_get(struct mlxsw_sp *mlxsw_sp, |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1925 | const struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1926 | { |
| 1927 | struct mlxsw_sp_fib_entry *fib_entry; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1928 | struct fib_info *fi = fen_info->fi; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1929 | struct mlxsw_sp_vr *vr; |
| 1930 | int err; |
| 1931 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1932 | vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->dst_len, fen_info->tb_id, |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1933 | MLXSW_SP_L3_PROTO_IPV4); |
| 1934 | if (IS_ERR(vr)) |
| 1935 | return ERR_CAST(vr); |
| 1936 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1937 | fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst, |
| 1938 | sizeof(fen_info->dst), |
| 1939 | fen_info->dst_len, fi->fib_dev); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1940 | if (fib_entry) { |
| 1941 | /* Already exists, just take a reference */ |
| 1942 | fib_entry->ref_count++; |
| 1943 | return fib_entry; |
| 1944 | } |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1945 | fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fen_info->dst, |
| 1946 | sizeof(fen_info->dst), |
| 1947 | fen_info->dst_len, fi->fib_dev); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1948 | if (!fib_entry) { |
| 1949 | err = -ENOMEM; |
| 1950 | goto err_fib_entry_create; |
| 1951 | } |
| 1952 | fib_entry->vr = vr; |
| 1953 | fib_entry->ref_count = 1; |
| 1954 | |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1955 | err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1956 | if (err) |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1957 | goto err_fib4_entry_type_set; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1958 | |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1959 | err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fi); |
| 1960 | if (err) |
| 1961 | goto err_nexthop_group_get; |
| 1962 | |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1963 | return fib_entry; |
| 1964 | |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1965 | err_nexthop_group_get: |
Ido Schimmel | 013b20f | 2017-02-08 11:16:36 +0100 | [diff] [blame] | 1966 | err_fib4_entry_type_set: |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1967 | mlxsw_sp_fib_entry_destroy(fib_entry); |
| 1968 | err_fib_entry_create: |
| 1969 | mlxsw_sp_vr_put(mlxsw_sp, vr); |
| 1970 | |
| 1971 | return ERR_PTR(err); |
| 1972 | } |
| 1973 | |
| 1974 | static struct mlxsw_sp_fib_entry * |
| 1975 | mlxsw_sp_fib_entry_find(struct mlxsw_sp *mlxsw_sp, |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1976 | const struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1977 | { |
| 1978 | struct mlxsw_sp_vr *vr; |
| 1979 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1980 | vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id, |
| 1981 | MLXSW_SP_L3_PROTO_IPV4); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1982 | if (!vr) |
| 1983 | return NULL; |
| 1984 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1985 | return mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst, |
| 1986 | sizeof(fen_info->dst), |
| 1987 | fen_info->dst_len, |
| 1988 | fen_info->fi->fib_dev); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1989 | } |
| 1990 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1991 | static void mlxsw_sp_fib_entry_put(struct mlxsw_sp *mlxsw_sp, |
| 1992 | struct mlxsw_sp_fib_entry *fib_entry) |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1993 | { |
| 1994 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
| 1995 | |
| 1996 | if (--fib_entry->ref_count == 0) { |
Ido Schimmel | b8399a1 | 2017-02-08 11:16:33 +0100 | [diff] [blame] | 1997 | mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1998 | mlxsw_sp_fib_entry_destroy(fib_entry); |
| 1999 | } |
| 2000 | mlxsw_sp_vr_put(mlxsw_sp, vr); |
| 2001 | } |
| 2002 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2003 | static void mlxsw_sp_fib_entry_put_all(struct mlxsw_sp *mlxsw_sp, |
| 2004 | struct mlxsw_sp_fib_entry *fib_entry) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2005 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2006 | unsigned int last_ref_count; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2007 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2008 | do { |
| 2009 | last_ref_count = fib_entry->ref_count; |
| 2010 | mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry); |
| 2011 | } while (last_ref_count != 1); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2012 | } |
| 2013 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2014 | static int mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp, |
| 2015 | struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2016 | { |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2017 | struct mlxsw_sp_fib_entry *fib_entry; |
| 2018 | struct mlxsw_sp_vr *vr; |
| 2019 | int err; |
| 2020 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2021 | if (mlxsw_sp->router.aborted) |
| 2022 | return 0; |
| 2023 | |
| 2024 | fib_entry = mlxsw_sp_fib_entry_get(mlxsw_sp, fen_info); |
| 2025 | if (IS_ERR(fib_entry)) { |
| 2026 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB4 entry being added.\n"); |
| 2027 | return PTR_ERR(fib_entry); |
| 2028 | } |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2029 | |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 2030 | if (fib_entry->ref_count != 1) |
| 2031 | return 0; |
| 2032 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2033 | vr = fib_entry->vr; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 2034 | err = mlxsw_sp_fib_entry_insert(vr->fib, fib_entry); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2035 | if (err) { |
| 2036 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to insert FIB4 entry being added.\n"); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2037 | goto err_fib_entry_insert; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2038 | } |
| 2039 | err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2040 | if (err) |
| 2041 | goto err_fib_entry_add; |
| 2042 | return 0; |
| 2043 | |
| 2044 | err_fib_entry_add: |
| 2045 | mlxsw_sp_fib_entry_remove(vr->fib, fib_entry); |
| 2046 | err_fib_entry_insert: |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 2047 | mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2048 | return err; |
| 2049 | } |
| 2050 | |
Jiri Pirko | 37956d7 | 2016-10-20 16:05:43 +0200 | [diff] [blame] | 2051 | static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp, |
| 2052 | struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2053 | { |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2054 | struct mlxsw_sp_fib_entry *fib_entry; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2055 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2056 | if (mlxsw_sp->router.aborted) |
Jiri Pirko | 37956d7 | 2016-10-20 16:05:43 +0200 | [diff] [blame] | 2057 | return; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2058 | |
| 2059 | fib_entry = mlxsw_sp_fib_entry_find(mlxsw_sp, fen_info); |
Jiri Pirko | 37956d7 | 2016-10-20 16:05:43 +0200 | [diff] [blame] | 2060 | if (!fib_entry) |
| 2061 | return; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 2062 | |
| 2063 | if (fib_entry->ref_count == 1) { |
| 2064 | mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry); |
| 2065 | mlxsw_sp_fib_entry_remove(fib_entry->vr->fib, fib_entry); |
| 2066 | } |
| 2067 | |
| 2068 | mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 2069 | } |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2070 | |
| 2071 | static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp) |
| 2072 | { |
| 2073 | char ralta_pl[MLXSW_REG_RALTA_LEN]; |
| 2074 | char ralst_pl[MLXSW_REG_RALST_LEN]; |
| 2075 | char raltb_pl[MLXSW_REG_RALTB_LEN]; |
| 2076 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 2077 | int err; |
| 2078 | |
| 2079 | mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4, |
| 2080 | MLXSW_SP_LPM_TREE_MIN); |
| 2081 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl); |
| 2082 | if (err) |
| 2083 | return err; |
| 2084 | |
| 2085 | mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN); |
| 2086 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl); |
| 2087 | if (err) |
| 2088 | return err; |
| 2089 | |
Jiri Pirko | 19271c1 | 2016-10-20 16:05:42 +0200 | [diff] [blame] | 2090 | mlxsw_reg_raltb_pack(raltb_pl, 0, MLXSW_REG_RALXX_PROTOCOL_IPV4, |
| 2091 | MLXSW_SP_LPM_TREE_MIN); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2092 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl); |
| 2093 | if (err) |
| 2094 | return err; |
| 2095 | |
| 2096 | mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4, |
| 2097 | MLXSW_REG_RALUE_OP_WRITE_WRITE, 0, 0, 0); |
| 2098 | mlxsw_reg_ralue_act_ip2me_pack(ralue_pl); |
| 2099 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 2100 | } |
| 2101 | |
Ido Schimmel | ac571de | 2016-11-14 11:26:32 +0100 | [diff] [blame] | 2102 | static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp) |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2103 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2104 | struct mlxsw_sp_fib_entry *fib_entry; |
| 2105 | struct mlxsw_sp_fib_entry *tmp; |
| 2106 | struct mlxsw_sp_vr *vr; |
| 2107 | int i; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2108 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2109 | for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2110 | vr = &mlxsw_sp->router.vrs[i]; |
Ido Schimmel | ac571de | 2016-11-14 11:26:32 +0100 | [diff] [blame] | 2111 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2112 | if (!vr->used) |
| 2113 | continue; |
| 2114 | |
| 2115 | list_for_each_entry_safe(fib_entry, tmp, |
| 2116 | &vr->fib->entry_list, list) { |
| 2117 | bool do_break = &tmp->list == &vr->fib->entry_list; |
| 2118 | |
| 2119 | mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry); |
| 2120 | mlxsw_sp_fib_entry_remove(fib_entry->vr->fib, |
| 2121 | fib_entry); |
| 2122 | mlxsw_sp_fib_entry_put_all(mlxsw_sp, fib_entry); |
| 2123 | if (do_break) |
| 2124 | break; |
| 2125 | } |
| 2126 | } |
Ido Schimmel | ac571de | 2016-11-14 11:26:32 +0100 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp) |
| 2130 | { |
| 2131 | int err; |
| 2132 | |
Ido Schimmel | d331d30 | 2016-11-16 09:51:58 +0100 | [diff] [blame] | 2133 | if (mlxsw_sp->router.aborted) |
| 2134 | return; |
| 2135 | dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n"); |
Ido Schimmel | ac571de | 2016-11-14 11:26:32 +0100 | [diff] [blame] | 2136 | mlxsw_sp_router_fib_flush(mlxsw_sp); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2137 | mlxsw_sp->router.aborted = true; |
| 2138 | err = mlxsw_sp_router_set_abort_trap(mlxsw_sp); |
| 2139 | if (err) |
| 2140 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n"); |
| 2141 | } |
| 2142 | |
Ido Schimmel | 9665b74 | 2017-02-08 11:16:42 +0100 | [diff] [blame^] | 2143 | static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif) |
| 2144 | { |
| 2145 | char ritr_pl[MLXSW_REG_RITR_LEN]; |
| 2146 | int err; |
| 2147 | |
| 2148 | mlxsw_reg_ritr_rif_pack(ritr_pl, rif); |
| 2149 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl); |
| 2150 | if (WARN_ON_ONCE(err)) |
| 2151 | return err; |
| 2152 | |
| 2153 | mlxsw_reg_ritr_enable_set(ritr_pl, false); |
| 2154 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl); |
| 2155 | } |
| 2156 | |
| 2157 | void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp, |
| 2158 | struct mlxsw_sp_rif *r) |
| 2159 | { |
| 2160 | mlxsw_sp_router_rif_disable(mlxsw_sp, r->rif); |
| 2161 | mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, r); |
| 2162 | mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, r); |
| 2163 | } |
| 2164 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2165 | static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp) |
| 2166 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2167 | char rgcr_pl[MLXSW_REG_RGCR_LEN]; |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2168 | u64 max_rifs; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2169 | int err; |
| 2170 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2171 | if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS)) |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2172 | return -EIO; |
| 2173 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2174 | max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); |
| 2175 | mlxsw_sp->rifs = kcalloc(max_rifs, sizeof(struct mlxsw_sp_rif *), |
| 2176 | GFP_KERNEL); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2177 | if (!mlxsw_sp->rifs) |
| 2178 | return -ENOMEM; |
| 2179 | |
| 2180 | mlxsw_reg_rgcr_pack(rgcr_pl, true); |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2181 | mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2182 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl); |
| 2183 | if (err) |
| 2184 | goto err_rgcr_fail; |
| 2185 | |
| 2186 | return 0; |
| 2187 | |
| 2188 | err_rgcr_fail: |
| 2189 | kfree(mlxsw_sp->rifs); |
| 2190 | return err; |
| 2191 | } |
| 2192 | |
| 2193 | static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp) |
| 2194 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2195 | char rgcr_pl[MLXSW_REG_RGCR_LEN]; |
| 2196 | int i; |
| 2197 | |
| 2198 | mlxsw_reg_rgcr_pack(rgcr_pl, false); |
| 2199 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl); |
| 2200 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2201 | for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2202 | WARN_ON_ONCE(mlxsw_sp->rifs[i]); |
| 2203 | |
| 2204 | kfree(mlxsw_sp->rifs); |
| 2205 | } |
| 2206 | |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2207 | struct mlxsw_sp_fib_event_work { |
Ido Schimmel | a0e4761 | 2017-02-06 16:20:10 +0100 | [diff] [blame] | 2208 | struct work_struct work; |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 2209 | union { |
| 2210 | struct fib_entry_notifier_info fen_info; |
| 2211 | struct fib_nh_notifier_info fnh_info; |
| 2212 | }; |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2213 | struct mlxsw_sp *mlxsw_sp; |
| 2214 | unsigned long event; |
| 2215 | }; |
| 2216 | |
| 2217 | static void mlxsw_sp_router_fib_event_work(struct work_struct *work) |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2218 | { |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2219 | struct mlxsw_sp_fib_event_work *fib_work = |
Ido Schimmel | a0e4761 | 2017-02-06 16:20:10 +0100 | [diff] [blame] | 2220 | container_of(work, struct mlxsw_sp_fib_event_work, work); |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2221 | struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2222 | int err; |
| 2223 | |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2224 | /* Protect internal structures from changes */ |
| 2225 | rtnl_lock(); |
| 2226 | switch (fib_work->event) { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2227 | case FIB_EVENT_ENTRY_ADD: |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2228 | err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2229 | if (err) |
| 2230 | mlxsw_sp_router_fib4_abort(mlxsw_sp); |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2231 | fib_info_put(fib_work->fen_info.fi); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2232 | break; |
| 2233 | case FIB_EVENT_ENTRY_DEL: |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2234 | mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info); |
| 2235 | fib_info_put(fib_work->fen_info.fi); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2236 | break; |
| 2237 | case FIB_EVENT_RULE_ADD: /* fall through */ |
| 2238 | case FIB_EVENT_RULE_DEL: |
| 2239 | mlxsw_sp_router_fib4_abort(mlxsw_sp); |
| 2240 | break; |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 2241 | case FIB_EVENT_NH_ADD: /* fall through */ |
| 2242 | case FIB_EVENT_NH_DEL: |
| 2243 | mlxsw_sp_nexthop_event(mlxsw_sp, fib_work->event, |
| 2244 | fib_work->fnh_info.fib_nh); |
| 2245 | fib_info_put(fib_work->fnh_info.fib_nh->nh_parent); |
| 2246 | break; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2247 | } |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2248 | rtnl_unlock(); |
| 2249 | kfree(fib_work); |
| 2250 | } |
| 2251 | |
| 2252 | /* Called with rcu_read_lock() */ |
| 2253 | static int mlxsw_sp_router_fib_event(struct notifier_block *nb, |
| 2254 | unsigned long event, void *ptr) |
| 2255 | { |
| 2256 | struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb); |
| 2257 | struct mlxsw_sp_fib_event_work *fib_work; |
| 2258 | struct fib_notifier_info *info = ptr; |
| 2259 | |
| 2260 | if (!net_eq(info->net, &init_net)) |
| 2261 | return NOTIFY_DONE; |
| 2262 | |
| 2263 | fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC); |
| 2264 | if (WARN_ON(!fib_work)) |
| 2265 | return NOTIFY_BAD; |
| 2266 | |
Ido Schimmel | a0e4761 | 2017-02-06 16:20:10 +0100 | [diff] [blame] | 2267 | INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work); |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2268 | fib_work->mlxsw_sp = mlxsw_sp; |
| 2269 | fib_work->event = event; |
| 2270 | |
| 2271 | switch (event) { |
| 2272 | case FIB_EVENT_ENTRY_ADD: /* fall through */ |
| 2273 | case FIB_EVENT_ENTRY_DEL: |
| 2274 | memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info)); |
| 2275 | /* Take referece on fib_info to prevent it from being |
| 2276 | * freed while work is queued. Release it afterwards. |
| 2277 | */ |
| 2278 | fib_info_hold(fib_work->fen_info.fi); |
| 2279 | break; |
Ido Schimmel | ad178c8 | 2017-02-08 11:16:40 +0100 | [diff] [blame] | 2280 | case FIB_EVENT_NH_ADD: /* fall through */ |
| 2281 | case FIB_EVENT_NH_DEL: |
| 2282 | memcpy(&fib_work->fnh_info, ptr, sizeof(fib_work->fnh_info)); |
| 2283 | fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent); |
| 2284 | break; |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2285 | } |
| 2286 | |
Ido Schimmel | a0e4761 | 2017-02-06 16:20:10 +0100 | [diff] [blame] | 2287 | mlxsw_core_schedule_work(&fib_work->work); |
Ido Schimmel | 3057224 | 2016-12-03 16:45:01 +0100 | [diff] [blame] | 2288 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2289 | return NOTIFY_DONE; |
| 2290 | } |
| 2291 | |
Ido Schimmel | c3852ef | 2016-12-03 16:45:07 +0100 | [diff] [blame] | 2292 | static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb) |
| 2293 | { |
| 2294 | struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb); |
| 2295 | |
| 2296 | /* Flush pending FIB notifications and then flush the device's |
| 2297 | * table before requesting another dump. The FIB notification |
| 2298 | * block is unregistered, so no need to take RTNL. |
| 2299 | */ |
| 2300 | mlxsw_core_flush_owq(); |
| 2301 | mlxsw_sp_router_fib_flush(mlxsw_sp); |
| 2302 | } |
| 2303 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2304 | int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp) |
| 2305 | { |
| 2306 | int err; |
| 2307 | |
| 2308 | INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2309 | err = __mlxsw_sp_router_init(mlxsw_sp); |
| 2310 | if (err) |
| 2311 | return err; |
| 2312 | |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 2313 | err = rhashtable_init(&mlxsw_sp->router.nexthop_ht, |
| 2314 | &mlxsw_sp_nexthop_ht_params); |
| 2315 | if (err) |
| 2316 | goto err_nexthop_ht_init; |
| 2317 | |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 2318 | err = rhashtable_init(&mlxsw_sp->router.nexthop_group_ht, |
| 2319 | &mlxsw_sp_nexthop_group_ht_params); |
| 2320 | if (err) |
| 2321 | goto err_nexthop_group_ht_init; |
| 2322 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2323 | mlxsw_sp_lpm_init(mlxsw_sp); |
| 2324 | err = mlxsw_sp_vrs_init(mlxsw_sp); |
| 2325 | if (err) |
| 2326 | goto err_vrs_init; |
| 2327 | |
Ido Schimmel | 8c9583a | 2016-10-27 15:12:57 +0200 | [diff] [blame] | 2328 | err = mlxsw_sp_neigh_init(mlxsw_sp); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2329 | if (err) |
| 2330 | goto err_neigh_init; |
| 2331 | |
| 2332 | mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event; |
Ido Schimmel | c3852ef | 2016-12-03 16:45:07 +0100 | [diff] [blame] | 2333 | err = register_fib_notifier(&mlxsw_sp->fib_nb, |
| 2334 | mlxsw_sp_router_fib_dump_flush); |
| 2335 | if (err) |
| 2336 | goto err_register_fib_notifier; |
| 2337 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2338 | return 0; |
| 2339 | |
Ido Schimmel | c3852ef | 2016-12-03 16:45:07 +0100 | [diff] [blame] | 2340 | err_register_fib_notifier: |
| 2341 | mlxsw_sp_neigh_fini(mlxsw_sp); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2342 | err_neigh_init: |
| 2343 | mlxsw_sp_vrs_fini(mlxsw_sp); |
| 2344 | err_vrs_init: |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 2345 | rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht); |
| 2346 | err_nexthop_group_ht_init: |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 2347 | rhashtable_destroy(&mlxsw_sp->router.nexthop_ht); |
| 2348 | err_nexthop_ht_init: |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2349 | __mlxsw_sp_router_fini(mlxsw_sp); |
| 2350 | return err; |
| 2351 | } |
| 2352 | |
| 2353 | void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp) |
| 2354 | { |
| 2355 | unregister_fib_notifier(&mlxsw_sp->fib_nb); |
| 2356 | mlxsw_sp_neigh_fini(mlxsw_sp); |
| 2357 | mlxsw_sp_vrs_fini(mlxsw_sp); |
Ido Schimmel | e9ad5e7 | 2017-02-08 11:16:29 +0100 | [diff] [blame] | 2358 | rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht); |
Ido Schimmel | c53b8e1 | 2017-02-08 11:16:30 +0100 | [diff] [blame] | 2359 | rhashtable_destroy(&mlxsw_sp->router.nexthop_ht); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 2360 | __mlxsw_sp_router_fini(mlxsw_sp); |
| 2361 | } |