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 | u16 rif; /* used for action local */ |
| 131 | struct mlxsw_sp_vr *vr; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 132 | struct fib_info *fi; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 133 | struct list_head nexthop_group_node; |
| 134 | struct mlxsw_sp_nexthop_group *nh_group; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | struct mlxsw_sp_fib { |
| 138 | struct rhashtable ht; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 139 | struct list_head entry_list; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 140 | unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT]; |
| 141 | struct mlxsw_sp_prefix_usage prefix_usage; |
| 142 | }; |
| 143 | |
| 144 | static const struct rhashtable_params mlxsw_sp_fib_ht_params = { |
| 145 | .key_offset = offsetof(struct mlxsw_sp_fib_entry, key), |
| 146 | .head_offset = offsetof(struct mlxsw_sp_fib_entry, ht_node), |
| 147 | .key_len = sizeof(struct mlxsw_sp_fib_key), |
| 148 | .automatic_shrinking = true, |
| 149 | }; |
| 150 | |
| 151 | static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib *fib, |
| 152 | struct mlxsw_sp_fib_entry *fib_entry) |
| 153 | { |
| 154 | unsigned char prefix_len = fib_entry->key.prefix_len; |
| 155 | int err; |
| 156 | |
| 157 | err = rhashtable_insert_fast(&fib->ht, &fib_entry->ht_node, |
| 158 | mlxsw_sp_fib_ht_params); |
| 159 | if (err) |
| 160 | return err; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 161 | list_add_tail(&fib_entry->list, &fib->entry_list); |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 162 | if (fib->prefix_ref_count[prefix_len]++ == 0) |
| 163 | mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib *fib, |
| 168 | struct mlxsw_sp_fib_entry *fib_entry) |
| 169 | { |
| 170 | unsigned char prefix_len = fib_entry->key.prefix_len; |
| 171 | |
| 172 | if (--fib->prefix_ref_count[prefix_len] == 0) |
| 173 | mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 174 | list_del(&fib_entry->list); |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 175 | rhashtable_remove_fast(&fib->ht, &fib_entry->ht_node, |
| 176 | mlxsw_sp_fib_ht_params); |
| 177 | } |
| 178 | |
| 179 | static struct mlxsw_sp_fib_entry * |
| 180 | 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] | 181 | size_t addr_len, unsigned char prefix_len, |
| 182 | struct net_device *dev) |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 183 | { |
| 184 | struct mlxsw_sp_fib_entry *fib_entry; |
| 185 | |
| 186 | fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL); |
| 187 | if (!fib_entry) |
| 188 | return NULL; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 189 | fib_entry->key.dev = dev; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 190 | memcpy(fib_entry->key.addr, addr, addr_len); |
| 191 | fib_entry->key.prefix_len = prefix_len; |
| 192 | return fib_entry; |
| 193 | } |
| 194 | |
| 195 | static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry *fib_entry) |
| 196 | { |
| 197 | kfree(fib_entry); |
| 198 | } |
| 199 | |
| 200 | static struct mlxsw_sp_fib_entry * |
| 201 | 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] | 202 | size_t addr_len, unsigned char prefix_len, |
| 203 | struct net_device *dev) |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 204 | { |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 205 | struct mlxsw_sp_fib_key key; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 206 | |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 207 | memset(&key, 0, sizeof(key)); |
| 208 | key.dev = dev; |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 209 | memcpy(key.addr, addr, addr_len); |
| 210 | key.prefix_len = prefix_len; |
| 211 | return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params); |
| 212 | } |
| 213 | |
| 214 | static struct mlxsw_sp_fib *mlxsw_sp_fib_create(void) |
| 215 | { |
| 216 | struct mlxsw_sp_fib *fib; |
| 217 | int err; |
| 218 | |
| 219 | fib = kzalloc(sizeof(*fib), GFP_KERNEL); |
| 220 | if (!fib) |
| 221 | return ERR_PTR(-ENOMEM); |
| 222 | err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params); |
| 223 | if (err) |
| 224 | goto err_rhashtable_init; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 225 | INIT_LIST_HEAD(&fib->entry_list); |
Jiri Pirko | 5e9c16c | 2016-07-04 08:23:04 +0200 | [diff] [blame] | 226 | return fib; |
| 227 | |
| 228 | err_rhashtable_init: |
| 229 | kfree(fib); |
| 230 | return ERR_PTR(err); |
| 231 | } |
| 232 | |
| 233 | static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib) |
| 234 | { |
| 235 | rhashtable_destroy(&fib->ht); |
| 236 | kfree(fib); |
| 237 | } |
| 238 | |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 239 | static struct mlxsw_sp_lpm_tree * |
| 240 | mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp, bool one_reserved) |
| 241 | { |
| 242 | static struct mlxsw_sp_lpm_tree *lpm_tree; |
| 243 | int i; |
| 244 | |
| 245 | for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) { |
| 246 | lpm_tree = &mlxsw_sp->router.lpm_trees[i]; |
| 247 | if (lpm_tree->ref_count == 0) { |
| 248 | if (one_reserved) |
| 249 | one_reserved = false; |
| 250 | else |
| 251 | return lpm_tree; |
| 252 | } |
| 253 | } |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp, |
| 258 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 259 | { |
| 260 | char ralta_pl[MLXSW_REG_RALTA_LEN]; |
| 261 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 262 | mlxsw_reg_ralta_pack(ralta_pl, true, |
| 263 | (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto, |
| 264 | lpm_tree->id); |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 265 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl); |
| 266 | } |
| 267 | |
| 268 | static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp, |
| 269 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 270 | { |
| 271 | char ralta_pl[MLXSW_REG_RALTA_LEN]; |
| 272 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 273 | mlxsw_reg_ralta_pack(ralta_pl, false, |
| 274 | (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto, |
| 275 | lpm_tree->id); |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 276 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl); |
| 277 | } |
| 278 | |
| 279 | static int |
| 280 | mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp, |
| 281 | struct mlxsw_sp_prefix_usage *prefix_usage, |
| 282 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 283 | { |
| 284 | char ralst_pl[MLXSW_REG_RALST_LEN]; |
| 285 | u8 root_bin = 0; |
| 286 | u8 prefix; |
| 287 | u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD; |
| 288 | |
| 289 | mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) |
| 290 | root_bin = prefix; |
| 291 | |
| 292 | mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id); |
| 293 | mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) { |
| 294 | if (prefix == 0) |
| 295 | continue; |
| 296 | mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix, |
| 297 | MLXSW_REG_RALST_BIN_NO_CHILD); |
| 298 | last_prefix = prefix; |
| 299 | } |
| 300 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl); |
| 301 | } |
| 302 | |
| 303 | static struct mlxsw_sp_lpm_tree * |
| 304 | mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp, |
| 305 | struct mlxsw_sp_prefix_usage *prefix_usage, |
| 306 | enum mlxsw_sp_l3proto proto, bool one_reserved) |
| 307 | { |
| 308 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 309 | int err; |
| 310 | |
| 311 | lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp, one_reserved); |
| 312 | if (!lpm_tree) |
| 313 | return ERR_PTR(-EBUSY); |
| 314 | lpm_tree->proto = proto; |
| 315 | err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree); |
| 316 | if (err) |
| 317 | return ERR_PTR(err); |
| 318 | |
| 319 | err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage, |
| 320 | lpm_tree); |
| 321 | if (err) |
| 322 | goto err_left_struct_set; |
Jiri Pirko | 2083d36 | 2016-10-25 11:25:56 +0200 | [diff] [blame] | 323 | memcpy(&lpm_tree->prefix_usage, prefix_usage, |
| 324 | sizeof(lpm_tree->prefix_usage)); |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 325 | return lpm_tree; |
| 326 | |
| 327 | err_left_struct_set: |
| 328 | mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree); |
| 329 | return ERR_PTR(err); |
| 330 | } |
| 331 | |
| 332 | static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp, |
| 333 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 334 | { |
| 335 | return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree); |
| 336 | } |
| 337 | |
| 338 | static struct mlxsw_sp_lpm_tree * |
| 339 | mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp, |
| 340 | struct mlxsw_sp_prefix_usage *prefix_usage, |
| 341 | enum mlxsw_sp_l3proto proto, bool one_reserved) |
| 342 | { |
| 343 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 344 | int i; |
| 345 | |
| 346 | for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) { |
| 347 | lpm_tree = &mlxsw_sp->router.lpm_trees[i]; |
Jiri Pirko | 8b99bec | 2016-10-25 11:25:57 +0200 | [diff] [blame] | 348 | if (lpm_tree->ref_count != 0 && |
| 349 | lpm_tree->proto == proto && |
Jiri Pirko | 5334202 | 2016-07-04 08:23:08 +0200 | [diff] [blame] | 350 | mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage, |
| 351 | prefix_usage)) |
| 352 | goto inc_ref_count; |
| 353 | } |
| 354 | lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage, |
| 355 | proto, one_reserved); |
| 356 | if (IS_ERR(lpm_tree)) |
| 357 | return lpm_tree; |
| 358 | |
| 359 | inc_ref_count: |
| 360 | lpm_tree->ref_count++; |
| 361 | return lpm_tree; |
| 362 | } |
| 363 | |
| 364 | static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp, |
| 365 | struct mlxsw_sp_lpm_tree *lpm_tree) |
| 366 | { |
| 367 | if (--lpm_tree->ref_count == 0) |
| 368 | return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp) |
| 373 | { |
| 374 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 375 | int i; |
| 376 | |
| 377 | for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) { |
| 378 | lpm_tree = &mlxsw_sp->router.lpm_trees[i]; |
| 379 | lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN; |
| 380 | } |
| 381 | } |
| 382 | |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 383 | static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp) |
| 384 | { |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 385 | struct mlxsw_resources *resources; |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 386 | struct mlxsw_sp_vr *vr; |
| 387 | int i; |
| 388 | |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 389 | resources = mlxsw_core_resources_get(mlxsw_sp->core); |
| 390 | for (i = 0; i < resources->max_virtual_routers; i++) { |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 391 | vr = &mlxsw_sp->router.vrs[i]; |
| 392 | if (!vr->used) |
| 393 | return vr; |
| 394 | } |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp, |
| 399 | struct mlxsw_sp_vr *vr) |
| 400 | { |
| 401 | char raltb_pl[MLXSW_REG_RALTB_LEN]; |
| 402 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 403 | mlxsw_reg_raltb_pack(raltb_pl, vr->id, |
| 404 | (enum mlxsw_reg_ralxx_protocol) vr->proto, |
| 405 | vr->lpm_tree->id); |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 406 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl); |
| 407 | } |
| 408 | |
| 409 | static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp, |
| 410 | struct mlxsw_sp_vr *vr) |
| 411 | { |
| 412 | char raltb_pl[MLXSW_REG_RALTB_LEN]; |
| 413 | |
| 414 | /* Bind to tree 0 which is default */ |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 415 | mlxsw_reg_raltb_pack(raltb_pl, vr->id, |
| 416 | (enum mlxsw_reg_ralxx_protocol) vr->proto, 0); |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 417 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl); |
| 418 | } |
| 419 | |
| 420 | static u32 mlxsw_sp_fix_tb_id(u32 tb_id) |
| 421 | { |
| 422 | /* For our purpose, squash main and local table into one */ |
| 423 | if (tb_id == RT_TABLE_LOCAL) |
| 424 | tb_id = RT_TABLE_MAIN; |
| 425 | return tb_id; |
| 426 | } |
| 427 | |
| 428 | static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp, |
| 429 | u32 tb_id, |
| 430 | enum mlxsw_sp_l3proto proto) |
| 431 | { |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 432 | struct mlxsw_resources *resources; |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 433 | struct mlxsw_sp_vr *vr; |
| 434 | int i; |
| 435 | |
| 436 | tb_id = mlxsw_sp_fix_tb_id(tb_id); |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 437 | |
| 438 | resources = mlxsw_core_resources_get(mlxsw_sp->core); |
| 439 | for (i = 0; i < resources->max_virtual_routers; i++) { |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 440 | vr = &mlxsw_sp->router.vrs[i]; |
| 441 | if (vr->used && vr->proto == proto && vr->tb_id == tb_id) |
| 442 | return vr; |
| 443 | } |
| 444 | return NULL; |
| 445 | } |
| 446 | |
| 447 | static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp, |
| 448 | unsigned char prefix_len, |
| 449 | u32 tb_id, |
| 450 | enum mlxsw_sp_l3proto proto) |
| 451 | { |
| 452 | struct mlxsw_sp_prefix_usage req_prefix_usage; |
| 453 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 454 | struct mlxsw_sp_vr *vr; |
| 455 | int err; |
| 456 | |
| 457 | vr = mlxsw_sp_vr_find_unused(mlxsw_sp); |
| 458 | if (!vr) |
| 459 | return ERR_PTR(-EBUSY); |
| 460 | vr->fib = mlxsw_sp_fib_create(); |
| 461 | if (IS_ERR(vr->fib)) |
| 462 | return ERR_CAST(vr->fib); |
| 463 | |
| 464 | vr->proto = proto; |
| 465 | vr->tb_id = tb_id; |
| 466 | mlxsw_sp_prefix_usage_zero(&req_prefix_usage); |
| 467 | mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len); |
| 468 | lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage, |
| 469 | proto, true); |
| 470 | if (IS_ERR(lpm_tree)) { |
| 471 | err = PTR_ERR(lpm_tree); |
| 472 | goto err_tree_get; |
| 473 | } |
| 474 | vr->lpm_tree = lpm_tree; |
| 475 | err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr); |
| 476 | if (err) |
| 477 | goto err_tree_bind; |
| 478 | |
| 479 | vr->used = true; |
| 480 | return vr; |
| 481 | |
| 482 | err_tree_bind: |
| 483 | mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree); |
| 484 | err_tree_get: |
| 485 | mlxsw_sp_fib_destroy(vr->fib); |
| 486 | |
| 487 | return ERR_PTR(err); |
| 488 | } |
| 489 | |
| 490 | static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp, |
| 491 | struct mlxsw_sp_vr *vr) |
| 492 | { |
| 493 | mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr); |
| 494 | mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree); |
| 495 | mlxsw_sp_fib_destroy(vr->fib); |
| 496 | vr->used = false; |
| 497 | } |
| 498 | |
| 499 | static int |
| 500 | mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr, |
| 501 | struct mlxsw_sp_prefix_usage *req_prefix_usage) |
| 502 | { |
| 503 | struct mlxsw_sp_lpm_tree *lpm_tree; |
| 504 | |
| 505 | if (mlxsw_sp_prefix_usage_eq(req_prefix_usage, |
| 506 | &vr->lpm_tree->prefix_usage)) |
| 507 | return 0; |
| 508 | |
| 509 | lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage, |
| 510 | vr->proto, false); |
| 511 | if (IS_ERR(lpm_tree)) { |
| 512 | /* We failed to get a tree according to the required |
| 513 | * prefix usage. However, the current tree might be still good |
| 514 | * for us if our requirement is subset of the prefixes used |
| 515 | * in the tree. |
| 516 | */ |
| 517 | if (mlxsw_sp_prefix_usage_subset(req_prefix_usage, |
| 518 | &vr->lpm_tree->prefix_usage)) |
| 519 | return 0; |
| 520 | return PTR_ERR(lpm_tree); |
| 521 | } |
| 522 | |
| 523 | mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr); |
| 524 | mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree); |
| 525 | vr->lpm_tree = lpm_tree; |
| 526 | return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr); |
| 527 | } |
| 528 | |
| 529 | static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, |
| 530 | unsigned char prefix_len, |
| 531 | u32 tb_id, |
| 532 | enum mlxsw_sp_l3proto proto) |
| 533 | { |
| 534 | struct mlxsw_sp_vr *vr; |
| 535 | int err; |
| 536 | |
| 537 | tb_id = mlxsw_sp_fix_tb_id(tb_id); |
| 538 | vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id, proto); |
| 539 | if (!vr) { |
| 540 | vr = mlxsw_sp_vr_create(mlxsw_sp, prefix_len, tb_id, proto); |
| 541 | if (IS_ERR(vr)) |
| 542 | return vr; |
| 543 | } else { |
| 544 | struct mlxsw_sp_prefix_usage req_prefix_usage; |
| 545 | |
| 546 | mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, |
| 547 | &vr->fib->prefix_usage); |
| 548 | mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len); |
| 549 | /* Need to replace LPM tree in case new prefix is required. */ |
| 550 | err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr, |
| 551 | &req_prefix_usage); |
| 552 | if (err) |
| 553 | return ERR_PTR(err); |
| 554 | } |
| 555 | return vr; |
| 556 | } |
| 557 | |
| 558 | static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr) |
| 559 | { |
| 560 | /* Destroy virtual router entity in case the associated FIB is empty |
| 561 | * and allow it to be used for other tables in future. Otherwise, |
| 562 | * check if some prefix usage did not disappear and change tree if |
| 563 | * that is the case. Note that in case new, smaller tree cannot be |
| 564 | * allocated, the original one will be kept being used. |
| 565 | */ |
| 566 | if (mlxsw_sp_prefix_usage_none(&vr->fib->prefix_usage)) |
| 567 | mlxsw_sp_vr_destroy(mlxsw_sp, vr); |
| 568 | else |
| 569 | mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr, |
| 570 | &vr->fib->prefix_usage); |
| 571 | } |
| 572 | |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 573 | static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp) |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 574 | { |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 575 | struct mlxsw_resources *resources; |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 576 | struct mlxsw_sp_vr *vr; |
| 577 | int i; |
| 578 | |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 579 | resources = mlxsw_core_resources_get(mlxsw_sp->core); |
| 580 | if (!resources->max_virtual_routers_valid) |
| 581 | return -EIO; |
| 582 | |
| 583 | mlxsw_sp->router.vrs = kcalloc(resources->max_virtual_routers, |
| 584 | sizeof(struct mlxsw_sp_vr), |
| 585 | GFP_KERNEL); |
| 586 | if (!mlxsw_sp->router.vrs) |
| 587 | return -ENOMEM; |
| 588 | |
| 589 | for (i = 0; i < resources->max_virtual_routers; i++) { |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 590 | vr = &mlxsw_sp->router.vrs[i]; |
| 591 | vr->id = i; |
| 592 | } |
Nogah Frankel | 9497c04 | 2016-09-20 11:16:54 +0200 | [diff] [blame] | 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp) |
| 598 | { |
| 599 | kfree(mlxsw_sp->router.vrs); |
Jiri Pirko | 6b75c48 | 2016-07-04 08:23:09 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 602 | struct mlxsw_sp_neigh_key { |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 603 | struct neighbour *n; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | struct mlxsw_sp_neigh_entry { |
| 607 | struct rhash_head ht_node; |
| 608 | struct mlxsw_sp_neigh_key key; |
| 609 | u16 rif; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 610 | bool offloaded; |
| 611 | struct delayed_work dw; |
| 612 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 613 | unsigned char ha[ETH_ALEN]; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 614 | struct list_head nexthop_list; /* list of nexthops using |
| 615 | * this neigh entry |
| 616 | */ |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 617 | struct list_head nexthop_neighs_list_node; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | static const struct rhashtable_params mlxsw_sp_neigh_ht_params = { |
| 621 | .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key), |
| 622 | .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node), |
| 623 | .key_len = sizeof(struct mlxsw_sp_neigh_key), |
| 624 | }; |
| 625 | |
| 626 | static int |
| 627 | mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp, |
| 628 | struct mlxsw_sp_neigh_entry *neigh_entry) |
| 629 | { |
| 630 | return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht, |
| 631 | &neigh_entry->ht_node, |
| 632 | mlxsw_sp_neigh_ht_params); |
| 633 | } |
| 634 | |
| 635 | static void |
| 636 | mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp, |
| 637 | struct mlxsw_sp_neigh_entry *neigh_entry) |
| 638 | { |
| 639 | rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht, |
| 640 | &neigh_entry->ht_node, |
| 641 | mlxsw_sp_neigh_ht_params); |
| 642 | } |
| 643 | |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 644 | static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work); |
| 645 | |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 646 | static struct mlxsw_sp_neigh_entry * |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 647 | mlxsw_sp_neigh_entry_create(struct neighbour *n, u16 rif) |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 648 | { |
| 649 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 650 | |
| 651 | neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_ATOMIC); |
| 652 | if (!neigh_entry) |
| 653 | return NULL; |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 654 | neigh_entry->key.n = n; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 655 | neigh_entry->rif = rif; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 656 | INIT_DELAYED_WORK(&neigh_entry->dw, mlxsw_sp_router_neigh_update_hw); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 657 | INIT_LIST_HEAD(&neigh_entry->nexthop_list); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 658 | return neigh_entry; |
| 659 | } |
| 660 | |
| 661 | static void |
| 662 | mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp_neigh_entry *neigh_entry) |
| 663 | { |
| 664 | kfree(neigh_entry); |
| 665 | } |
| 666 | |
| 667 | static struct mlxsw_sp_neigh_entry * |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 668 | 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] | 669 | { |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 670 | struct mlxsw_sp_neigh_key key; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 671 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 672 | key.n = n; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 673 | return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht, |
| 674 | &key, mlxsw_sp_neigh_ht_params); |
| 675 | } |
| 676 | |
| 677 | int mlxsw_sp_router_neigh_construct(struct net_device *dev, |
| 678 | struct neighbour *n) |
| 679 | { |
| 680 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 681 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 682 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 683 | struct mlxsw_sp_rif *r; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 684 | int err; |
| 685 | |
| 686 | if (n->tbl != &arp_tbl) |
| 687 | return 0; |
| 688 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 689 | neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n); |
| 690 | if (neigh_entry) |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 691 | return 0; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 692 | |
Yotam Gigi | 51af96b | 2016-08-24 11:18:52 +0200 | [diff] [blame] | 693 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 694 | if (WARN_ON(!r)) |
| 695 | return -EINVAL; |
| 696 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 697 | neigh_entry = mlxsw_sp_neigh_entry_create(n, r->rif); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 698 | if (!neigh_entry) |
| 699 | return -ENOMEM; |
| 700 | err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry); |
| 701 | if (err) |
| 702 | goto err_neigh_entry_insert; |
| 703 | return 0; |
| 704 | |
| 705 | err_neigh_entry_insert: |
| 706 | mlxsw_sp_neigh_entry_destroy(neigh_entry); |
| 707 | return err; |
| 708 | } |
| 709 | |
| 710 | void mlxsw_sp_router_neigh_destroy(struct net_device *dev, |
| 711 | struct neighbour *n) |
| 712 | { |
| 713 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 714 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 715 | struct mlxsw_sp_neigh_entry *neigh_entry; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 716 | |
| 717 | if (n->tbl != &arp_tbl) |
| 718 | return; |
| 719 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 720 | neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 721 | if (!neigh_entry) |
| 722 | return; |
| 723 | mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry); |
| 724 | mlxsw_sp_neigh_entry_destroy(neigh_entry); |
| 725 | } |
| 726 | |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 727 | static void |
| 728 | mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp) |
| 729 | { |
| 730 | unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME); |
| 731 | |
| 732 | mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval); |
| 733 | } |
| 734 | |
| 735 | static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp, |
| 736 | char *rauhtd_pl, |
| 737 | int ent_index) |
| 738 | { |
| 739 | struct net_device *dev; |
| 740 | struct neighbour *n; |
| 741 | __be32 dipn; |
| 742 | u32 dip; |
| 743 | u16 rif; |
| 744 | |
| 745 | mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip); |
| 746 | |
| 747 | if (!mlxsw_sp->rifs[rif]) { |
| 748 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n"); |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | dipn = htonl(dip); |
| 753 | dev = mlxsw_sp->rifs[rif]->dev; |
| 754 | n = neigh_lookup(&arp_tbl, &dipn, dev); |
| 755 | if (!n) { |
| 756 | netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n", |
| 757 | &dip); |
| 758 | return; |
| 759 | } |
| 760 | |
| 761 | netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip); |
| 762 | neigh_event_send(n, NULL); |
| 763 | neigh_release(n); |
| 764 | } |
| 765 | |
| 766 | static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp, |
| 767 | char *rauhtd_pl, |
| 768 | int rec_index) |
| 769 | { |
| 770 | u8 num_entries; |
| 771 | int i; |
| 772 | |
| 773 | num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl, |
| 774 | rec_index); |
| 775 | /* Hardware starts counting at 0, so add 1. */ |
| 776 | num_entries++; |
| 777 | |
| 778 | /* Each record consists of several neighbour entries. */ |
| 779 | for (i = 0; i < num_entries; i++) { |
| 780 | int ent_index; |
| 781 | |
| 782 | ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i; |
| 783 | mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl, |
| 784 | ent_index); |
| 785 | } |
| 786 | |
| 787 | } |
| 788 | |
| 789 | static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp, |
| 790 | char *rauhtd_pl, int rec_index) |
| 791 | { |
| 792 | switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) { |
| 793 | case MLXSW_REG_RAUHTD_TYPE_IPV4: |
| 794 | mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl, |
| 795 | rec_index); |
| 796 | break; |
| 797 | case MLXSW_REG_RAUHTD_TYPE_IPV6: |
| 798 | WARN_ON_ONCE(1); |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | |
Arkadi Sharshevsky | 42cdb33 | 2016-11-11 16:34:26 +0100 | [diff] [blame^] | 803 | static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl) |
| 804 | { |
| 805 | u8 num_rec, last_rec_index, num_entries; |
| 806 | |
| 807 | num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl); |
| 808 | last_rec_index = num_rec - 1; |
| 809 | |
| 810 | if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM) |
| 811 | return false; |
| 812 | if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) == |
| 813 | MLXSW_REG_RAUHTD_TYPE_IPV6) |
| 814 | return true; |
| 815 | |
| 816 | num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl, |
| 817 | last_rec_index); |
| 818 | if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC) |
| 819 | return true; |
| 820 | return false; |
| 821 | } |
| 822 | |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 823 | 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] | 824 | { |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 825 | char *rauhtd_pl; |
| 826 | u8 num_rec; |
| 827 | int i, err; |
| 828 | |
| 829 | rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL); |
| 830 | if (!rauhtd_pl) |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 831 | return -ENOMEM; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 832 | |
| 833 | /* Make sure the neighbour's netdev isn't removed in the |
| 834 | * process. |
| 835 | */ |
| 836 | rtnl_lock(); |
| 837 | do { |
| 838 | mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4); |
| 839 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd), |
| 840 | rauhtd_pl); |
| 841 | if (err) { |
| 842 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n"); |
| 843 | break; |
| 844 | } |
| 845 | num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl); |
| 846 | for (i = 0; i < num_rec; i++) |
| 847 | mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl, |
| 848 | i); |
Arkadi Sharshevsky | 42cdb33 | 2016-11-11 16:34:26 +0100 | [diff] [blame^] | 849 | } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl)); |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 850 | rtnl_unlock(); |
| 851 | |
| 852 | kfree(rauhtd_pl); |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 853 | return err; |
| 854 | } |
| 855 | |
| 856 | static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp) |
| 857 | { |
| 858 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 859 | |
| 860 | /* Take RTNL mutex here to prevent lists from changes */ |
| 861 | rtnl_lock(); |
| 862 | list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list, |
| 863 | nexthop_neighs_list_node) { |
| 864 | /* If this neigh have nexthops, make the kernel think this neigh |
| 865 | * is active regardless of the traffic. |
| 866 | */ |
| 867 | if (!list_empty(&neigh_entry->nexthop_list)) |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 868 | neigh_event_send(neigh_entry->key.n, NULL); |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 869 | } |
| 870 | rtnl_unlock(); |
| 871 | } |
| 872 | |
| 873 | static void |
| 874 | mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp) |
| 875 | { |
| 876 | unsigned long interval = mlxsw_sp->router.neighs_update.interval; |
| 877 | |
| 878 | mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, |
| 879 | msecs_to_jiffies(interval)); |
| 880 | } |
| 881 | |
| 882 | static void mlxsw_sp_router_neighs_update_work(struct work_struct *work) |
| 883 | { |
| 884 | struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp, |
| 885 | router.neighs_update.dw.work); |
| 886 | int err; |
| 887 | |
| 888 | err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp); |
| 889 | if (err) |
| 890 | dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity"); |
| 891 | |
| 892 | mlxsw_sp_router_neighs_update_nh(mlxsw_sp); |
| 893 | |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 894 | mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp); |
| 895 | } |
| 896 | |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 897 | static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work) |
| 898 | { |
| 899 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 900 | struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp, |
| 901 | router.nexthop_probe_dw.work); |
| 902 | |
| 903 | /* Iterate over nexthop neighbours, find those who are unresolved and |
| 904 | * send arp on them. This solves the chicken-egg problem when |
| 905 | * the nexthop wouldn't get offloaded until the neighbor is resolved |
| 906 | * but it wouldn't get resolved ever in case traffic is flowing in HW |
| 907 | * using different nexthop. |
| 908 | * |
| 909 | * Take RTNL mutex here to prevent lists from changes. |
| 910 | */ |
| 911 | rtnl_lock(); |
| 912 | list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list, |
| 913 | nexthop_neighs_list_node) { |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 914 | if (!(neigh_entry->key.n->nud_state & NUD_VALID) && |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 915 | !list_empty(&neigh_entry->nexthop_list)) |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 916 | neigh_event_send(neigh_entry->key.n, NULL); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 917 | } |
| 918 | rtnl_unlock(); |
| 919 | |
| 920 | mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, |
| 921 | MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL); |
| 922 | } |
| 923 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 924 | static void |
| 925 | mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp, |
| 926 | struct mlxsw_sp_neigh_entry *neigh_entry, |
| 927 | bool removing); |
| 928 | |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 929 | static void mlxsw_sp_router_neigh_update_hw(struct work_struct *work) |
| 930 | { |
| 931 | struct mlxsw_sp_neigh_entry *neigh_entry = |
| 932 | container_of(work, struct mlxsw_sp_neigh_entry, dw.work); |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 933 | struct neighbour *n = neigh_entry->key.n; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 934 | struct mlxsw_sp_port *mlxsw_sp_port = neigh_entry->mlxsw_sp_port; |
| 935 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 936 | char rauht_pl[MLXSW_REG_RAUHT_LEN]; |
| 937 | struct net_device *dev; |
| 938 | bool entry_connected; |
| 939 | u8 nud_state; |
| 940 | bool updating; |
| 941 | bool removing; |
| 942 | bool adding; |
| 943 | u32 dip; |
| 944 | int err; |
| 945 | |
| 946 | read_lock_bh(&n->lock); |
| 947 | dip = ntohl(*((__be32 *) n->primary_key)); |
| 948 | memcpy(neigh_entry->ha, n->ha, sizeof(neigh_entry->ha)); |
| 949 | nud_state = n->nud_state; |
| 950 | dev = n->dev; |
| 951 | read_unlock_bh(&n->lock); |
| 952 | |
| 953 | entry_connected = nud_state & NUD_VALID; |
| 954 | adding = (!neigh_entry->offloaded) && entry_connected; |
| 955 | updating = neigh_entry->offloaded && entry_connected; |
| 956 | removing = neigh_entry->offloaded && !entry_connected; |
| 957 | |
| 958 | if (adding || updating) { |
| 959 | mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_ADD, |
| 960 | neigh_entry->rif, |
| 961 | neigh_entry->ha, dip); |
| 962 | err = mlxsw_reg_write(mlxsw_sp->core, |
| 963 | MLXSW_REG(rauht), rauht_pl); |
| 964 | if (err) { |
| 965 | netdev_err(dev, "Could not add neigh %pI4h\n", &dip); |
| 966 | neigh_entry->offloaded = false; |
| 967 | } else { |
| 968 | neigh_entry->offloaded = true; |
| 969 | } |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 970 | mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, false); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 971 | } else if (removing) { |
| 972 | mlxsw_reg_rauht_pack4(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE, |
| 973 | neigh_entry->rif, |
| 974 | neigh_entry->ha, dip); |
| 975 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), |
| 976 | rauht_pl); |
| 977 | if (err) { |
| 978 | netdev_err(dev, "Could not delete neigh %pI4h\n", &dip); |
| 979 | neigh_entry->offloaded = true; |
| 980 | } else { |
| 981 | neigh_entry->offloaded = false; |
| 982 | } |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 983 | mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, true); |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | neigh_release(n); |
| 987 | mlxsw_sp_port_dev_put(mlxsw_sp_port); |
| 988 | } |
| 989 | |
Jiri Pirko | e732263 | 2016-09-01 10:37:43 +0200 | [diff] [blame] | 990 | int mlxsw_sp_router_netevent_event(struct notifier_block *unused, |
| 991 | unsigned long event, void *ptr) |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 992 | { |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 993 | struct mlxsw_sp_neigh_entry *neigh_entry; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 994 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 995 | struct mlxsw_sp *mlxsw_sp; |
| 996 | unsigned long interval; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 997 | struct net_device *dev; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 998 | struct neigh_parms *p; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 999 | struct neighbour *n; |
| 1000 | u32 dip; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1001 | |
| 1002 | switch (event) { |
| 1003 | case NETEVENT_DELAY_PROBE_TIME_UPDATE: |
| 1004 | p = ptr; |
| 1005 | |
| 1006 | /* We don't care about changes in the default table. */ |
| 1007 | if (!p->dev || p->tbl != &arp_tbl) |
| 1008 | return NOTIFY_DONE; |
| 1009 | |
| 1010 | /* We are in atomic context and can't take RTNL mutex, |
| 1011 | * so use RCU variant to walk the device chain. |
| 1012 | */ |
| 1013 | mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev); |
| 1014 | if (!mlxsw_sp_port) |
| 1015 | return NOTIFY_DONE; |
| 1016 | |
| 1017 | mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1018 | interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME)); |
| 1019 | mlxsw_sp->router.neighs_update.interval = interval; |
| 1020 | |
| 1021 | mlxsw_sp_port_dev_put(mlxsw_sp_port); |
| 1022 | break; |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1023 | case NETEVENT_NEIGH_UPDATE: |
| 1024 | n = ptr; |
| 1025 | dev = n->dev; |
| 1026 | |
| 1027 | if (n->tbl != &arp_tbl) |
| 1028 | return NOTIFY_DONE; |
| 1029 | |
| 1030 | mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(dev); |
| 1031 | if (!mlxsw_sp_port) |
| 1032 | return NOTIFY_DONE; |
| 1033 | |
| 1034 | mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1035 | dip = ntohl(*((__be32 *) n->primary_key)); |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1036 | neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n); |
| 1037 | if (WARN_ON(!neigh_entry)) { |
Yotam Gigi | a6bf9e9 | 2016-07-05 11:27:44 +0200 | [diff] [blame] | 1038 | mlxsw_sp_port_dev_put(mlxsw_sp_port); |
| 1039 | return NOTIFY_DONE; |
| 1040 | } |
| 1041 | neigh_entry->mlxsw_sp_port = mlxsw_sp_port; |
| 1042 | |
| 1043 | /* Take a reference to ensure the neighbour won't be |
| 1044 | * destructed until we drop the reference in delayed |
| 1045 | * work. |
| 1046 | */ |
| 1047 | neigh_clone(n); |
| 1048 | if (!mlxsw_core_schedule_dw(&neigh_entry->dw, 0)) { |
| 1049 | neigh_release(n); |
| 1050 | mlxsw_sp_port_dev_put(mlxsw_sp_port); |
| 1051 | } |
| 1052 | break; |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | return NOTIFY_DONE; |
| 1056 | } |
| 1057 | |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 1058 | static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp) |
| 1059 | { |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1060 | int err; |
| 1061 | |
| 1062 | err = rhashtable_init(&mlxsw_sp->router.neigh_ht, |
| 1063 | &mlxsw_sp_neigh_ht_params); |
| 1064 | if (err) |
| 1065 | return err; |
| 1066 | |
| 1067 | /* Initialize the polling interval according to the default |
| 1068 | * table. |
| 1069 | */ |
| 1070 | mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp); |
| 1071 | |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1072 | /* Create the delayed works for the activity_update */ |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1073 | INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw, |
| 1074 | mlxsw_sp_router_neighs_update_work); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1075 | INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw, |
| 1076 | mlxsw_sp_router_probe_unresolved_nexthops); |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1077 | mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1078 | mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0); |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1079 | return 0; |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp) |
| 1083 | { |
Yotam Gigi | c723c735 | 2016-07-05 11:27:43 +0200 | [diff] [blame] | 1084 | cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw); |
Yotam Gigi | 0b2361d | 2016-07-05 11:27:52 +0200 | [diff] [blame] | 1085 | cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw); |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 1086 | rhashtable_destroy(&mlxsw_sp->router.neigh_ht); |
| 1087 | } |
| 1088 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1089 | struct mlxsw_sp_nexthop { |
| 1090 | struct list_head neigh_list_node; /* member of neigh entry list */ |
| 1091 | struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group |
| 1092 | * this belongs to |
| 1093 | */ |
| 1094 | u8 should_offload:1, /* set indicates this neigh is connected and |
| 1095 | * should be put to KVD linear area of this group. |
| 1096 | */ |
| 1097 | offloaded:1, /* set in case the neigh is actually put into |
| 1098 | * KVD linear area of this group. |
| 1099 | */ |
| 1100 | update:1; /* set indicates that MAC of this neigh should be |
| 1101 | * updated in HW |
| 1102 | */ |
| 1103 | struct mlxsw_sp_neigh_entry *neigh_entry; |
| 1104 | }; |
| 1105 | |
| 1106 | struct mlxsw_sp_nexthop_group { |
| 1107 | struct list_head list; /* node in mlxsw->router.nexthop_group_list */ |
| 1108 | struct list_head fib_list; /* list of fib entries that use this group */ |
| 1109 | u8 adj_index_valid:1; |
| 1110 | u32 adj_index; |
| 1111 | u16 ecmp_size; |
| 1112 | u16 count; |
| 1113 | struct mlxsw_sp_nexthop nexthops[0]; |
| 1114 | }; |
| 1115 | |
| 1116 | static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp, |
| 1117 | struct mlxsw_sp_vr *vr, |
| 1118 | u32 adj_index, u16 ecmp_size, |
| 1119 | u32 new_adj_index, |
| 1120 | u16 new_ecmp_size) |
| 1121 | { |
| 1122 | char raleu_pl[MLXSW_REG_RALEU_LEN]; |
| 1123 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1124 | mlxsw_reg_raleu_pack(raleu_pl, |
| 1125 | (enum mlxsw_reg_ralxx_protocol) vr->proto, vr->id, |
| 1126 | adj_index, ecmp_size, new_adj_index, |
| 1127 | new_ecmp_size); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1128 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl); |
| 1129 | } |
| 1130 | |
| 1131 | static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp, |
| 1132 | struct mlxsw_sp_nexthop_group *nh_grp, |
| 1133 | u32 old_adj_index, u16 old_ecmp_size) |
| 1134 | { |
| 1135 | struct mlxsw_sp_fib_entry *fib_entry; |
| 1136 | struct mlxsw_sp_vr *vr = NULL; |
| 1137 | int err; |
| 1138 | |
| 1139 | list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) { |
| 1140 | if (vr == fib_entry->vr) |
| 1141 | continue; |
| 1142 | vr = fib_entry->vr; |
| 1143 | err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, vr, |
| 1144 | old_adj_index, |
| 1145 | old_ecmp_size, |
| 1146 | nh_grp->adj_index, |
| 1147 | nh_grp->ecmp_size); |
| 1148 | if (err) |
| 1149 | return err; |
| 1150 | } |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index, |
| 1155 | struct mlxsw_sp_nexthop *nh) |
| 1156 | { |
| 1157 | struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry; |
| 1158 | char ratr_pl[MLXSW_REG_RATR_LEN]; |
| 1159 | |
| 1160 | mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY, |
| 1161 | true, adj_index, neigh_entry->rif); |
| 1162 | mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha); |
| 1163 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl); |
| 1164 | } |
| 1165 | |
| 1166 | static int |
| 1167 | mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp, |
| 1168 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1169 | { |
| 1170 | u32 adj_index = nh_grp->adj_index; /* base */ |
| 1171 | struct mlxsw_sp_nexthop *nh; |
| 1172 | int i; |
| 1173 | int err; |
| 1174 | |
| 1175 | for (i = 0; i < nh_grp->count; i++) { |
| 1176 | nh = &nh_grp->nexthops[i]; |
| 1177 | |
| 1178 | if (!nh->should_offload) { |
| 1179 | nh->offloaded = 0; |
| 1180 | continue; |
| 1181 | } |
| 1182 | |
| 1183 | if (nh->update) { |
| 1184 | err = mlxsw_sp_nexthop_mac_update(mlxsw_sp, |
| 1185 | adj_index, nh); |
| 1186 | if (err) |
| 1187 | return err; |
| 1188 | nh->update = 0; |
| 1189 | nh->offloaded = 1; |
| 1190 | } |
| 1191 | adj_index++; |
| 1192 | } |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp, |
| 1197 | struct mlxsw_sp_fib_entry *fib_entry); |
| 1198 | |
| 1199 | static int |
| 1200 | mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp, |
| 1201 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1202 | { |
| 1203 | struct mlxsw_sp_fib_entry *fib_entry; |
| 1204 | int err; |
| 1205 | |
| 1206 | list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) { |
| 1207 | err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry); |
| 1208 | if (err) |
| 1209 | return err; |
| 1210 | } |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | static void |
| 1215 | mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp, |
| 1216 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1217 | { |
| 1218 | struct mlxsw_sp_nexthop *nh; |
| 1219 | bool offload_change = false; |
| 1220 | u32 adj_index; |
| 1221 | u16 ecmp_size = 0; |
| 1222 | bool old_adj_index_valid; |
| 1223 | u32 old_adj_index; |
| 1224 | u16 old_ecmp_size; |
| 1225 | int ret; |
| 1226 | int i; |
| 1227 | int err; |
| 1228 | |
| 1229 | for (i = 0; i < nh_grp->count; i++) { |
| 1230 | nh = &nh_grp->nexthops[i]; |
| 1231 | |
| 1232 | if (nh->should_offload ^ nh->offloaded) { |
| 1233 | offload_change = true; |
| 1234 | if (nh->should_offload) |
| 1235 | nh->update = 1; |
| 1236 | } |
| 1237 | if (nh->should_offload) |
| 1238 | ecmp_size++; |
| 1239 | } |
| 1240 | if (!offload_change) { |
| 1241 | /* Nothing was added or removed, so no need to reallocate. Just |
| 1242 | * update MAC on existing adjacency indexes. |
| 1243 | */ |
| 1244 | err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp); |
| 1245 | if (err) { |
| 1246 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n"); |
| 1247 | goto set_trap; |
| 1248 | } |
| 1249 | return; |
| 1250 | } |
| 1251 | if (!ecmp_size) |
| 1252 | /* No neigh of this group is connected so we just set |
| 1253 | * the trap and let everthing flow through kernel. |
| 1254 | */ |
| 1255 | goto set_trap; |
| 1256 | |
| 1257 | ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size); |
| 1258 | if (ret < 0) { |
| 1259 | /* We ran out of KVD linear space, just set the |
| 1260 | * trap and let everything flow through kernel. |
| 1261 | */ |
| 1262 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n"); |
| 1263 | goto set_trap; |
| 1264 | } |
| 1265 | adj_index = ret; |
| 1266 | old_adj_index_valid = nh_grp->adj_index_valid; |
| 1267 | old_adj_index = nh_grp->adj_index; |
| 1268 | old_ecmp_size = nh_grp->ecmp_size; |
| 1269 | nh_grp->adj_index_valid = 1; |
| 1270 | nh_grp->adj_index = adj_index; |
| 1271 | nh_grp->ecmp_size = ecmp_size; |
| 1272 | err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp); |
| 1273 | if (err) { |
| 1274 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n"); |
| 1275 | goto set_trap; |
| 1276 | } |
| 1277 | |
| 1278 | if (!old_adj_index_valid) { |
| 1279 | /* The trap was set for fib entries, so we have to call |
| 1280 | * fib entry update to unset it and use adjacency index. |
| 1281 | */ |
| 1282 | err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp); |
| 1283 | if (err) { |
| 1284 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n"); |
| 1285 | goto set_trap; |
| 1286 | } |
| 1287 | return; |
| 1288 | } |
| 1289 | |
| 1290 | err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp, |
| 1291 | old_adj_index, old_ecmp_size); |
| 1292 | mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index); |
| 1293 | if (err) { |
| 1294 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n"); |
| 1295 | goto set_trap; |
| 1296 | } |
| 1297 | return; |
| 1298 | |
| 1299 | set_trap: |
| 1300 | old_adj_index_valid = nh_grp->adj_index_valid; |
| 1301 | nh_grp->adj_index_valid = 0; |
| 1302 | for (i = 0; i < nh_grp->count; i++) { |
| 1303 | nh = &nh_grp->nexthops[i]; |
| 1304 | nh->offloaded = 0; |
| 1305 | } |
| 1306 | err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp); |
| 1307 | if (err) |
| 1308 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n"); |
| 1309 | if (old_adj_index_valid) |
| 1310 | mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index); |
| 1311 | } |
| 1312 | |
| 1313 | static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh, |
| 1314 | bool removing) |
| 1315 | { |
| 1316 | if (!removing && !nh->should_offload) |
| 1317 | nh->should_offload = 1; |
| 1318 | else if (removing && nh->offloaded) |
| 1319 | nh->should_offload = 0; |
| 1320 | nh->update = 1; |
| 1321 | } |
| 1322 | |
| 1323 | static void |
| 1324 | mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp, |
| 1325 | struct mlxsw_sp_neigh_entry *neigh_entry, |
| 1326 | bool removing) |
| 1327 | { |
| 1328 | struct mlxsw_sp_nexthop *nh; |
| 1329 | |
| 1330 | /* Take RTNL mutex here to prevent lists from changes */ |
| 1331 | rtnl_lock(); |
| 1332 | list_for_each_entry(nh, &neigh_entry->nexthop_list, |
| 1333 | neigh_list_node) { |
| 1334 | __mlxsw_sp_nexthop_neigh_update(nh, removing); |
| 1335 | mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp); |
| 1336 | } |
| 1337 | rtnl_unlock(); |
| 1338 | } |
| 1339 | |
| 1340 | static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp, |
| 1341 | struct mlxsw_sp_nexthop_group *nh_grp, |
| 1342 | struct mlxsw_sp_nexthop *nh, |
| 1343 | struct fib_nh *fib_nh) |
| 1344 | { |
| 1345 | struct mlxsw_sp_neigh_entry *neigh_entry; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1346 | struct net_device *dev = fib_nh->nh_dev; |
| 1347 | struct neighbour *n; |
| 1348 | u8 nud_state; |
| 1349 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1350 | /* Take a reference of neigh here ensuring that neigh would |
| 1351 | * not be detructed before the nexthop entry is finished. |
| 1352 | * The reference is taken either in neigh_lookup() or |
| 1353 | * in neith_create() in case n is not found. |
| 1354 | */ |
| 1355 | n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, dev); |
| 1356 | if (!n) { |
| 1357 | n = neigh_create(&arp_tbl, &fib_nh->nh_gw, dev); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1358 | if (IS_ERR(n)) |
| 1359 | return PTR_ERR(n); |
| 1360 | neigh_event_send(n, NULL); |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1361 | } |
| 1362 | neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n); |
| 1363 | if (!neigh_entry) { |
| 1364 | neigh_release(n); |
| 1365 | return -EINVAL; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1366 | } |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 1367 | |
| 1368 | /* If that is the first nexthop connected to that neigh, add to |
| 1369 | * nexthop_neighs_list |
| 1370 | */ |
| 1371 | if (list_empty(&neigh_entry->nexthop_list)) |
| 1372 | list_add_tail(&neigh_entry->nexthop_neighs_list_node, |
| 1373 | &mlxsw_sp->router.nexthop_neighs_list); |
| 1374 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1375 | nh->nh_grp = nh_grp; |
| 1376 | nh->neigh_entry = neigh_entry; |
| 1377 | list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list); |
| 1378 | read_lock_bh(&n->lock); |
| 1379 | nud_state = n->nud_state; |
| 1380 | read_unlock_bh(&n->lock); |
| 1381 | __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID)); |
| 1382 | |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp, |
| 1387 | struct mlxsw_sp_nexthop *nh) |
| 1388 | { |
| 1389 | struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry; |
| 1390 | |
| 1391 | list_del(&nh->neigh_list_node); |
Yotam Gigi | b215714 | 2016-07-05 11:27:51 +0200 | [diff] [blame] | 1392 | |
| 1393 | /* If that is the last nexthop connected to that neigh, remove from |
| 1394 | * nexthop_neighs_list |
| 1395 | */ |
| 1396 | if (list_empty(&nh->neigh_entry->nexthop_list)) |
| 1397 | list_del(&nh->neigh_entry->nexthop_neighs_list_node); |
| 1398 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1399 | neigh_release(neigh_entry->key.n); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | static struct mlxsw_sp_nexthop_group * |
| 1403 | mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi) |
| 1404 | { |
| 1405 | struct mlxsw_sp_nexthop_group *nh_grp; |
| 1406 | struct mlxsw_sp_nexthop *nh; |
| 1407 | struct fib_nh *fib_nh; |
| 1408 | size_t alloc_size; |
| 1409 | int i; |
| 1410 | int err; |
| 1411 | |
| 1412 | alloc_size = sizeof(*nh_grp) + |
| 1413 | fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop); |
| 1414 | nh_grp = kzalloc(alloc_size, GFP_KERNEL); |
| 1415 | if (!nh_grp) |
| 1416 | return ERR_PTR(-ENOMEM); |
| 1417 | INIT_LIST_HEAD(&nh_grp->fib_list); |
| 1418 | nh_grp->count = fi->fib_nhs; |
| 1419 | for (i = 0; i < nh_grp->count; i++) { |
| 1420 | nh = &nh_grp->nexthops[i]; |
| 1421 | fib_nh = &fi->fib_nh[i]; |
| 1422 | err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh); |
| 1423 | if (err) |
| 1424 | goto err_nexthop_init; |
| 1425 | } |
| 1426 | list_add_tail(&nh_grp->list, &mlxsw_sp->router.nexthop_group_list); |
| 1427 | mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp); |
| 1428 | return nh_grp; |
| 1429 | |
| 1430 | err_nexthop_init: |
| 1431 | for (i--; i >= 0; i--) |
| 1432 | mlxsw_sp_nexthop_fini(mlxsw_sp, nh); |
| 1433 | kfree(nh_grp); |
| 1434 | return ERR_PTR(err); |
| 1435 | } |
| 1436 | |
| 1437 | static void |
| 1438 | mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp, |
| 1439 | struct mlxsw_sp_nexthop_group *nh_grp) |
| 1440 | { |
| 1441 | struct mlxsw_sp_nexthop *nh; |
| 1442 | int i; |
| 1443 | |
| 1444 | list_del(&nh_grp->list); |
| 1445 | for (i = 0; i < nh_grp->count; i++) { |
| 1446 | nh = &nh_grp->nexthops[i]; |
| 1447 | mlxsw_sp_nexthop_fini(mlxsw_sp, nh); |
| 1448 | } |
| 1449 | kfree(nh_grp); |
| 1450 | } |
| 1451 | |
| 1452 | static bool mlxsw_sp_nexthop_match(struct mlxsw_sp_nexthop *nh, |
| 1453 | struct fib_info *fi) |
| 1454 | { |
| 1455 | int i; |
| 1456 | |
| 1457 | for (i = 0; i < fi->fib_nhs; i++) { |
| 1458 | struct fib_nh *fib_nh = &fi->fib_nh[i]; |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1459 | struct neighbour *n = nh->neigh_entry->key.n; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1460 | |
Jiri Pirko | 33b1341 | 2016-11-10 12:31:04 +0100 | [diff] [blame] | 1461 | if (memcmp(n->primary_key, &fib_nh->nh_gw, |
| 1462 | sizeof(fib_nh->nh_gw)) == 0 && |
| 1463 | n->dev == fib_nh->nh_dev) |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1464 | return true; |
| 1465 | } |
| 1466 | return false; |
| 1467 | } |
| 1468 | |
| 1469 | static bool mlxsw_sp_nexthop_group_match(struct mlxsw_sp_nexthop_group *nh_grp, |
| 1470 | struct fib_info *fi) |
| 1471 | { |
| 1472 | int i; |
| 1473 | |
| 1474 | if (nh_grp->count != fi->fib_nhs) |
| 1475 | return false; |
| 1476 | for (i = 0; i < nh_grp->count; i++) { |
| 1477 | struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i]; |
| 1478 | |
| 1479 | if (!mlxsw_sp_nexthop_match(nh, fi)) |
| 1480 | return false; |
| 1481 | } |
| 1482 | return true; |
| 1483 | } |
| 1484 | |
| 1485 | static struct mlxsw_sp_nexthop_group * |
| 1486 | mlxsw_sp_nexthop_group_find(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi) |
| 1487 | { |
| 1488 | struct mlxsw_sp_nexthop_group *nh_grp; |
| 1489 | |
| 1490 | list_for_each_entry(nh_grp, &mlxsw_sp->router.nexthop_group_list, |
| 1491 | list) { |
| 1492 | if (mlxsw_sp_nexthop_group_match(nh_grp, fi)) |
| 1493 | return nh_grp; |
| 1494 | } |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
| 1498 | static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp, |
| 1499 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1500 | struct fib_info *fi) |
| 1501 | { |
| 1502 | struct mlxsw_sp_nexthop_group *nh_grp; |
| 1503 | |
| 1504 | nh_grp = mlxsw_sp_nexthop_group_find(mlxsw_sp, fi); |
| 1505 | if (!nh_grp) { |
| 1506 | nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi); |
| 1507 | if (IS_ERR(nh_grp)) |
| 1508 | return PTR_ERR(nh_grp); |
| 1509 | } |
| 1510 | list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list); |
| 1511 | fib_entry->nh_group = nh_grp; |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp, |
| 1516 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1517 | { |
| 1518 | struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group; |
| 1519 | |
| 1520 | list_del(&fib_entry->nexthop_group_node); |
| 1521 | if (!list_empty(&nh_grp->fib_list)) |
| 1522 | return; |
| 1523 | mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp); |
| 1524 | } |
| 1525 | |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1526 | static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp, |
| 1527 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1528 | enum mlxsw_reg_ralue_op op) |
| 1529 | { |
| 1530 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 1531 | u32 *p_dip = (u32 *) fib_entry->key.addr; |
| 1532 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
| 1533 | enum mlxsw_reg_ralue_trap_action trap_action; |
| 1534 | u16 trap_id = 0; |
| 1535 | u32 adjacency_index = 0; |
| 1536 | u16 ecmp_size = 0; |
| 1537 | |
| 1538 | /* In case the nexthop group adjacency index is valid, use it |
| 1539 | * with provided ECMP size. Otherwise, setup trap and pass |
| 1540 | * traffic to kernel. |
| 1541 | */ |
| 1542 | if (fib_entry->nh_group->adj_index_valid) { |
| 1543 | trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP; |
| 1544 | adjacency_index = fib_entry->nh_group->adj_index; |
| 1545 | ecmp_size = fib_entry->nh_group->ecmp_size; |
| 1546 | } else { |
| 1547 | trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP; |
| 1548 | trap_id = MLXSW_TRAP_ID_RTR_INGRESS0; |
| 1549 | } |
| 1550 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1551 | mlxsw_reg_ralue_pack4(ralue_pl, |
| 1552 | (enum mlxsw_reg_ralxx_protocol) vr->proto, op, |
| 1553 | vr->id, fib_entry->key.prefix_len, *p_dip); |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1554 | mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id, |
| 1555 | adjacency_index, ecmp_size); |
| 1556 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 1557 | } |
| 1558 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1559 | static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp, |
| 1560 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1561 | enum mlxsw_reg_ralue_op op) |
| 1562 | { |
| 1563 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 1564 | u32 *p_dip = (u32 *) fib_entry->key.addr; |
| 1565 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
| 1566 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1567 | mlxsw_reg_ralue_pack4(ralue_pl, |
| 1568 | (enum mlxsw_reg_ralxx_protocol) vr->proto, op, |
| 1569 | vr->id, fib_entry->key.prefix_len, *p_dip); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1570 | mlxsw_reg_ralue_act_local_pack(ralue_pl, |
| 1571 | MLXSW_REG_RALUE_TRAP_ACTION_NOP, 0, |
| 1572 | fib_entry->rif); |
| 1573 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 1574 | } |
| 1575 | |
| 1576 | static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp, |
| 1577 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1578 | enum mlxsw_reg_ralue_op op) |
| 1579 | { |
| 1580 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 1581 | u32 *p_dip = (u32 *) fib_entry->key.addr; |
| 1582 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
| 1583 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1584 | mlxsw_reg_ralue_pack4(ralue_pl, |
| 1585 | (enum mlxsw_reg_ralxx_protocol) vr->proto, op, |
| 1586 | vr->id, fib_entry->key.prefix_len, *p_dip); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1587 | mlxsw_reg_ralue_act_ip2me_pack(ralue_pl); |
| 1588 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 1589 | } |
| 1590 | |
| 1591 | static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp, |
| 1592 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1593 | enum mlxsw_reg_ralue_op op) |
| 1594 | { |
| 1595 | switch (fib_entry->type) { |
| 1596 | case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE: |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1597 | return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1598 | case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL: |
| 1599 | return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op); |
| 1600 | case MLXSW_SP_FIB_ENTRY_TYPE_TRAP: |
| 1601 | return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op); |
| 1602 | } |
| 1603 | return -EINVAL; |
| 1604 | } |
| 1605 | |
| 1606 | static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp, |
| 1607 | struct mlxsw_sp_fib_entry *fib_entry, |
| 1608 | enum mlxsw_reg_ralue_op op) |
| 1609 | { |
| 1610 | switch (fib_entry->vr->proto) { |
| 1611 | case MLXSW_SP_L3_PROTO_IPV4: |
| 1612 | return mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op); |
| 1613 | case MLXSW_SP_L3_PROTO_IPV6: |
| 1614 | return -EINVAL; |
| 1615 | } |
| 1616 | return -EINVAL; |
| 1617 | } |
| 1618 | |
| 1619 | static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp, |
| 1620 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1621 | { |
Jiri Pirko | 7146da3 | 2016-09-01 10:37:41 +0200 | [diff] [blame] | 1622 | return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, |
| 1623 | MLXSW_REG_RALUE_OP_WRITE_WRITE); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp, |
| 1627 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1628 | { |
| 1629 | return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, |
| 1630 | MLXSW_REG_RALUE_OP_WRITE_DELETE); |
| 1631 | } |
| 1632 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1633 | static int |
| 1634 | mlxsw_sp_router_fib4_entry_init(struct mlxsw_sp *mlxsw_sp, |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1635 | const struct fib_entry_notifier_info *fen_info, |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1636 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1637 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1638 | struct fib_info *fi = fen_info->fi; |
Arnd Bergmann | ab58070 | 2016-09-30 18:17:10 +0200 | [diff] [blame] | 1639 | struct mlxsw_sp_rif *r = NULL; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1640 | int nhsel; |
| 1641 | int err; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1642 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1643 | if (fen_info->type == RTN_LOCAL || fen_info->type == RTN_BROADCAST) { |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1644 | fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP; |
| 1645 | return 0; |
| 1646 | } |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1647 | if (fen_info->type != RTN_UNICAST) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1648 | return -EINVAL; |
| 1649 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1650 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 1651 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1652 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1653 | if (!nh->nh_dev) |
| 1654 | continue; |
| 1655 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, nh->nh_dev); |
| 1656 | if (!r) { |
| 1657 | /* In case router interface is not found for |
| 1658 | * at least one of the nexthops, that means |
| 1659 | * the nexthop points to some device unrelated |
| 1660 | * to us. Set trap and pass the packets for |
| 1661 | * this prefix to kernel. |
| 1662 | */ |
Arnd Bergmann | ab58070 | 2016-09-30 18:17:10 +0200 | [diff] [blame] | 1663 | break; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1664 | } |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1665 | } |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1666 | |
Arnd Bergmann | ab58070 | 2016-09-30 18:17:10 +0200 | [diff] [blame] | 1667 | if (!r) { |
| 1668 | fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP; |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1672 | if (fi->fib_scope != RT_SCOPE_UNIVERSE) { |
| 1673 | fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL; |
| 1674 | fib_entry->rif = r->rif; |
| 1675 | } else { |
| 1676 | fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE; |
| 1677 | err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fi); |
| 1678 | if (err) |
| 1679 | return err; |
| 1680 | } |
| 1681 | fib_info_offload_inc(fen_info->fi); |
| 1682 | return 0; |
Jiri Pirko | a7ff87a | 2016-07-05 11:27:50 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | static void |
| 1686 | mlxsw_sp_router_fib4_entry_fini(struct mlxsw_sp *mlxsw_sp, |
| 1687 | struct mlxsw_sp_fib_entry *fib_entry) |
| 1688 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1689 | if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_TRAP) |
| 1690 | fib_info_offload_dec(fib_entry->fi); |
| 1691 | if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_REMOTE) |
| 1692 | mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1695 | static struct mlxsw_sp_fib_entry * |
| 1696 | mlxsw_sp_fib_entry_get(struct mlxsw_sp *mlxsw_sp, |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1697 | const struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1698 | { |
| 1699 | struct mlxsw_sp_fib_entry *fib_entry; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1700 | struct fib_info *fi = fen_info->fi; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1701 | struct mlxsw_sp_vr *vr; |
| 1702 | int err; |
| 1703 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1704 | 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] | 1705 | MLXSW_SP_L3_PROTO_IPV4); |
| 1706 | if (IS_ERR(vr)) |
| 1707 | return ERR_CAST(vr); |
| 1708 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1709 | fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst, |
| 1710 | sizeof(fen_info->dst), |
| 1711 | fen_info->dst_len, fi->fib_dev); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1712 | if (fib_entry) { |
| 1713 | /* Already exists, just take a reference */ |
| 1714 | fib_entry->ref_count++; |
| 1715 | return fib_entry; |
| 1716 | } |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1717 | fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fen_info->dst, |
| 1718 | sizeof(fen_info->dst), |
| 1719 | fen_info->dst_len, fi->fib_dev); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1720 | if (!fib_entry) { |
| 1721 | err = -ENOMEM; |
| 1722 | goto err_fib_entry_create; |
| 1723 | } |
| 1724 | fib_entry->vr = vr; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1725 | fib_entry->fi = fi; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1726 | fib_entry->ref_count = 1; |
| 1727 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1728 | err = mlxsw_sp_router_fib4_entry_init(mlxsw_sp, fen_info, fib_entry); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1729 | if (err) |
| 1730 | goto err_fib4_entry_init; |
| 1731 | |
| 1732 | return fib_entry; |
| 1733 | |
| 1734 | err_fib4_entry_init: |
| 1735 | mlxsw_sp_fib_entry_destroy(fib_entry); |
| 1736 | err_fib_entry_create: |
| 1737 | mlxsw_sp_vr_put(mlxsw_sp, vr); |
| 1738 | |
| 1739 | return ERR_PTR(err); |
| 1740 | } |
| 1741 | |
| 1742 | static struct mlxsw_sp_fib_entry * |
| 1743 | mlxsw_sp_fib_entry_find(struct mlxsw_sp *mlxsw_sp, |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1744 | const struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1745 | { |
| 1746 | struct mlxsw_sp_vr *vr; |
| 1747 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1748 | vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id, |
| 1749 | MLXSW_SP_L3_PROTO_IPV4); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1750 | if (!vr) |
| 1751 | return NULL; |
| 1752 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1753 | return mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst, |
| 1754 | sizeof(fen_info->dst), |
| 1755 | fen_info->dst_len, |
| 1756 | fen_info->fi->fib_dev); |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1757 | } |
| 1758 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 1759 | static void mlxsw_sp_fib_entry_put(struct mlxsw_sp *mlxsw_sp, |
| 1760 | struct mlxsw_sp_fib_entry *fib_entry) |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1761 | { |
| 1762 | struct mlxsw_sp_vr *vr = fib_entry->vr; |
| 1763 | |
| 1764 | if (--fib_entry->ref_count == 0) { |
| 1765 | mlxsw_sp_router_fib4_entry_fini(mlxsw_sp, fib_entry); |
| 1766 | mlxsw_sp_fib_entry_destroy(fib_entry); |
| 1767 | } |
| 1768 | mlxsw_sp_vr_put(mlxsw_sp, vr); |
| 1769 | } |
| 1770 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1771 | static void mlxsw_sp_fib_entry_put_all(struct mlxsw_sp *mlxsw_sp, |
| 1772 | struct mlxsw_sp_fib_entry *fib_entry) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1773 | { |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1774 | unsigned int last_ref_count; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1775 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1776 | do { |
| 1777 | last_ref_count = fib_entry->ref_count; |
| 1778 | mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry); |
| 1779 | } while (last_ref_count != 1); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1780 | } |
| 1781 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1782 | static int mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp, |
| 1783 | struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1784 | { |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1785 | struct mlxsw_sp_fib_entry *fib_entry; |
| 1786 | struct mlxsw_sp_vr *vr; |
| 1787 | int err; |
| 1788 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1789 | if (mlxsw_sp->router.aborted) |
| 1790 | return 0; |
| 1791 | |
| 1792 | fib_entry = mlxsw_sp_fib_entry_get(mlxsw_sp, fen_info); |
| 1793 | if (IS_ERR(fib_entry)) { |
| 1794 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB4 entry being added.\n"); |
| 1795 | return PTR_ERR(fib_entry); |
| 1796 | } |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1797 | |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1798 | if (fib_entry->ref_count != 1) |
| 1799 | return 0; |
| 1800 | |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1801 | vr = fib_entry->vr; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1802 | err = mlxsw_sp_fib_entry_insert(vr->fib, fib_entry); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1803 | if (err) { |
| 1804 | 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] | 1805 | goto err_fib_entry_insert; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1806 | } |
| 1807 | err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1808 | if (err) |
| 1809 | goto err_fib_entry_add; |
| 1810 | return 0; |
| 1811 | |
| 1812 | err_fib_entry_add: |
| 1813 | mlxsw_sp_fib_entry_remove(vr->fib, fib_entry); |
| 1814 | err_fib_entry_insert: |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1815 | mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1816 | return err; |
| 1817 | } |
| 1818 | |
Jiri Pirko | 37956d7 | 2016-10-20 16:05:43 +0200 | [diff] [blame] | 1819 | static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp, |
| 1820 | struct fib_entry_notifier_info *fen_info) |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1821 | { |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1822 | struct mlxsw_sp_fib_entry *fib_entry; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1823 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1824 | if (mlxsw_sp->router.aborted) |
Jiri Pirko | 37956d7 | 2016-10-20 16:05:43 +0200 | [diff] [blame] | 1825 | return; |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1826 | |
| 1827 | fib_entry = mlxsw_sp_fib_entry_find(mlxsw_sp, fen_info); |
Jiri Pirko | 37956d7 | 2016-10-20 16:05:43 +0200 | [diff] [blame] | 1828 | if (!fib_entry) |
| 1829 | return; |
Jiri Pirko | 5b00441 | 2016-09-01 10:37:40 +0200 | [diff] [blame] | 1830 | |
| 1831 | if (fib_entry->ref_count == 1) { |
| 1832 | mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry); |
| 1833 | mlxsw_sp_fib_entry_remove(fib_entry->vr->fib, fib_entry); |
| 1834 | } |
| 1835 | |
| 1836 | mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry); |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1837 | } |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1838 | |
| 1839 | static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp) |
| 1840 | { |
| 1841 | char ralta_pl[MLXSW_REG_RALTA_LEN]; |
| 1842 | char ralst_pl[MLXSW_REG_RALST_LEN]; |
| 1843 | char raltb_pl[MLXSW_REG_RALTB_LEN]; |
| 1844 | char ralue_pl[MLXSW_REG_RALUE_LEN]; |
| 1845 | int err; |
| 1846 | |
| 1847 | mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4, |
| 1848 | MLXSW_SP_LPM_TREE_MIN); |
| 1849 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl); |
| 1850 | if (err) |
| 1851 | return err; |
| 1852 | |
| 1853 | mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN); |
| 1854 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl); |
| 1855 | if (err) |
| 1856 | return err; |
| 1857 | |
Jiri Pirko | 19271c1 | 2016-10-20 16:05:42 +0200 | [diff] [blame] | 1858 | mlxsw_reg_raltb_pack(raltb_pl, 0, MLXSW_REG_RALXX_PROTOCOL_IPV4, |
| 1859 | MLXSW_SP_LPM_TREE_MIN); |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1860 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl); |
| 1861 | if (err) |
| 1862 | return err; |
| 1863 | |
| 1864 | mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4, |
| 1865 | MLXSW_REG_RALUE_OP_WRITE_WRITE, 0, 0, 0); |
| 1866 | mlxsw_reg_ralue_act_ip2me_pack(ralue_pl); |
| 1867 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl); |
| 1868 | } |
| 1869 | |
| 1870 | static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp) |
| 1871 | { |
| 1872 | struct mlxsw_resources *resources; |
| 1873 | struct mlxsw_sp_fib_entry *fib_entry; |
| 1874 | struct mlxsw_sp_fib_entry *tmp; |
| 1875 | struct mlxsw_sp_vr *vr; |
| 1876 | int i; |
| 1877 | int err; |
| 1878 | |
| 1879 | resources = mlxsw_core_resources_get(mlxsw_sp->core); |
| 1880 | for (i = 0; i < resources->max_virtual_routers; i++) { |
| 1881 | vr = &mlxsw_sp->router.vrs[i]; |
| 1882 | if (!vr->used) |
| 1883 | continue; |
| 1884 | |
| 1885 | list_for_each_entry_safe(fib_entry, tmp, |
| 1886 | &vr->fib->entry_list, list) { |
| 1887 | bool do_break = &tmp->list == &vr->fib->entry_list; |
| 1888 | |
| 1889 | mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry); |
| 1890 | mlxsw_sp_fib_entry_remove(fib_entry->vr->fib, |
| 1891 | fib_entry); |
| 1892 | mlxsw_sp_fib_entry_put_all(mlxsw_sp, fib_entry); |
| 1893 | if (do_break) |
| 1894 | break; |
| 1895 | } |
| 1896 | } |
| 1897 | mlxsw_sp->router.aborted = true; |
| 1898 | err = mlxsw_sp_router_set_abort_trap(mlxsw_sp); |
| 1899 | if (err) |
| 1900 | dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n"); |
| 1901 | } |
| 1902 | |
| 1903 | static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp) |
| 1904 | { |
| 1905 | struct mlxsw_resources *resources; |
| 1906 | char rgcr_pl[MLXSW_REG_RGCR_LEN]; |
| 1907 | int err; |
| 1908 | |
| 1909 | resources = mlxsw_core_resources_get(mlxsw_sp->core); |
| 1910 | if (!resources->max_rif_valid) |
| 1911 | return -EIO; |
| 1912 | |
| 1913 | mlxsw_sp->rifs = kcalloc(resources->max_rif, |
| 1914 | sizeof(struct mlxsw_sp_rif *), GFP_KERNEL); |
| 1915 | if (!mlxsw_sp->rifs) |
| 1916 | return -ENOMEM; |
| 1917 | |
| 1918 | mlxsw_reg_rgcr_pack(rgcr_pl, true); |
| 1919 | mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, resources->max_rif); |
| 1920 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl); |
| 1921 | if (err) |
| 1922 | goto err_rgcr_fail; |
| 1923 | |
| 1924 | return 0; |
| 1925 | |
| 1926 | err_rgcr_fail: |
| 1927 | kfree(mlxsw_sp->rifs); |
| 1928 | return err; |
| 1929 | } |
| 1930 | |
| 1931 | static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp) |
| 1932 | { |
| 1933 | struct mlxsw_resources *resources; |
| 1934 | char rgcr_pl[MLXSW_REG_RGCR_LEN]; |
| 1935 | int i; |
| 1936 | |
| 1937 | mlxsw_reg_rgcr_pack(rgcr_pl, false); |
| 1938 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl); |
| 1939 | |
| 1940 | resources = mlxsw_core_resources_get(mlxsw_sp->core); |
| 1941 | for (i = 0; i < resources->max_rif; i++) |
| 1942 | WARN_ON_ONCE(mlxsw_sp->rifs[i]); |
| 1943 | |
| 1944 | kfree(mlxsw_sp->rifs); |
| 1945 | } |
| 1946 | |
| 1947 | static int mlxsw_sp_router_fib_event(struct notifier_block *nb, |
| 1948 | unsigned long event, void *ptr) |
| 1949 | { |
| 1950 | struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb); |
| 1951 | struct fib_entry_notifier_info *fen_info = ptr; |
| 1952 | int err; |
| 1953 | |
Jiri Pirko | 0e3715c | 2016-11-10 12:31:05 +0100 | [diff] [blame] | 1954 | if (!net_eq(fen_info->info.net, &init_net)) |
| 1955 | return NOTIFY_DONE; |
| 1956 | |
Jiri Pirko | b45f64d | 2016-09-26 12:52:31 +0200 | [diff] [blame] | 1957 | switch (event) { |
| 1958 | case FIB_EVENT_ENTRY_ADD: |
| 1959 | err = mlxsw_sp_router_fib4_add(mlxsw_sp, fen_info); |
| 1960 | if (err) |
| 1961 | mlxsw_sp_router_fib4_abort(mlxsw_sp); |
| 1962 | break; |
| 1963 | case FIB_EVENT_ENTRY_DEL: |
| 1964 | mlxsw_sp_router_fib4_del(mlxsw_sp, fen_info); |
| 1965 | break; |
| 1966 | case FIB_EVENT_RULE_ADD: /* fall through */ |
| 1967 | case FIB_EVENT_RULE_DEL: |
| 1968 | mlxsw_sp_router_fib4_abort(mlxsw_sp); |
| 1969 | break; |
| 1970 | } |
| 1971 | return NOTIFY_DONE; |
| 1972 | } |
| 1973 | |
| 1974 | int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp) |
| 1975 | { |
| 1976 | int err; |
| 1977 | |
| 1978 | INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list); |
| 1979 | INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_group_list); |
| 1980 | err = __mlxsw_sp_router_init(mlxsw_sp); |
| 1981 | if (err) |
| 1982 | return err; |
| 1983 | |
| 1984 | mlxsw_sp_lpm_init(mlxsw_sp); |
| 1985 | err = mlxsw_sp_vrs_init(mlxsw_sp); |
| 1986 | if (err) |
| 1987 | goto err_vrs_init; |
| 1988 | |
| 1989 | err = mlxsw_sp_neigh_init(mlxsw_sp); |
| 1990 | if (err) |
| 1991 | goto err_neigh_init; |
| 1992 | |
| 1993 | mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event; |
| 1994 | register_fib_notifier(&mlxsw_sp->fib_nb); |
| 1995 | return 0; |
| 1996 | |
| 1997 | err_neigh_init: |
| 1998 | mlxsw_sp_vrs_fini(mlxsw_sp); |
| 1999 | err_vrs_init: |
| 2000 | __mlxsw_sp_router_fini(mlxsw_sp); |
| 2001 | return err; |
| 2002 | } |
| 2003 | |
| 2004 | void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp) |
| 2005 | { |
| 2006 | unregister_fib_notifier(&mlxsw_sp->fib_nb); |
| 2007 | mlxsw_sp_neigh_fini(mlxsw_sp); |
| 2008 | mlxsw_sp_vrs_fini(mlxsw_sp); |
| 2009 | __mlxsw_sp_router_fini(mlxsw_sp); |
| 2010 | } |