Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/switchdev/switchdev.c - Switch device API |
| 3 | * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us> |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 4 | * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/init.h> |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 15 | #include <linux/mutex.h> |
| 16 | #include <linux/notifier.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 17 | #include <linux/netdevice.h> |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 18 | #include <linux/if_bridge.h> |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 19 | #include <net/ip_fib.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 20 | #include <net/switchdev.h> |
| 21 | |
| 22 | /** |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 23 | * switchdev_port_attr_get - Get port attribute |
| 24 | * |
| 25 | * @dev: port device |
| 26 | * @attr: attribute to get |
| 27 | */ |
| 28 | int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr) |
| 29 | { |
| 30 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 31 | struct net_device *lower_dev; |
| 32 | struct list_head *iter; |
| 33 | struct switchdev_attr first = { |
| 34 | .id = SWITCHDEV_ATTR_UNDEFINED |
| 35 | }; |
| 36 | int err = -EOPNOTSUPP; |
| 37 | |
| 38 | if (ops && ops->switchdev_port_attr_get) |
| 39 | return ops->switchdev_port_attr_get(dev, attr); |
| 40 | |
| 41 | if (attr->flags & SWITCHDEV_F_NO_RECURSE) |
| 42 | return err; |
| 43 | |
| 44 | /* Switch device port(s) may be stacked under |
| 45 | * bond/team/vlan dev, so recurse down to get attr on |
| 46 | * each port. Return -ENODATA if attr values don't |
| 47 | * compare across ports. |
| 48 | */ |
| 49 | |
| 50 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 51 | err = switchdev_port_attr_get(lower_dev, attr); |
| 52 | if (err) |
| 53 | break; |
| 54 | if (first.id == SWITCHDEV_ATTR_UNDEFINED) |
| 55 | first = *attr; |
| 56 | else if (memcmp(&first, attr, sizeof(*attr))) |
| 57 | return -ENODATA; |
| 58 | } |
| 59 | |
| 60 | return err; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(switchdev_port_attr_get); |
| 63 | |
| 64 | static int __switchdev_port_attr_set(struct net_device *dev, |
| 65 | struct switchdev_attr *attr) |
| 66 | { |
| 67 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 68 | struct net_device *lower_dev; |
| 69 | struct list_head *iter; |
| 70 | int err = -EOPNOTSUPP; |
| 71 | |
| 72 | if (ops && ops->switchdev_port_attr_set) |
| 73 | return ops->switchdev_port_attr_set(dev, attr); |
| 74 | |
| 75 | if (attr->flags & SWITCHDEV_F_NO_RECURSE) |
| 76 | return err; |
| 77 | |
| 78 | /* Switch device port(s) may be stacked under |
| 79 | * bond/team/vlan dev, so recurse down to set attr on |
| 80 | * each port. |
| 81 | */ |
| 82 | |
| 83 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 84 | err = __switchdev_port_attr_set(lower_dev, attr); |
| 85 | if (err) |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | return err; |
| 90 | } |
| 91 | |
| 92 | struct switchdev_attr_set_work { |
| 93 | struct work_struct work; |
| 94 | struct net_device *dev; |
| 95 | struct switchdev_attr attr; |
| 96 | }; |
| 97 | |
| 98 | static void switchdev_port_attr_set_work(struct work_struct *work) |
| 99 | { |
| 100 | struct switchdev_attr_set_work *asw = |
| 101 | container_of(work, struct switchdev_attr_set_work, work); |
| 102 | int err; |
| 103 | |
| 104 | rtnl_lock(); |
| 105 | err = switchdev_port_attr_set(asw->dev, &asw->attr); |
Scott Feldman | 57225e77 | 2015-06-11 08:19:01 -0700 | [diff] [blame^] | 106 | if (err && err != -EOPNOTSUPP) |
| 107 | netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n", |
| 108 | err, asw->attr.id); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 109 | rtnl_unlock(); |
| 110 | |
| 111 | dev_put(asw->dev); |
| 112 | kfree(work); |
| 113 | } |
| 114 | |
| 115 | static int switchdev_port_attr_set_defer(struct net_device *dev, |
| 116 | struct switchdev_attr *attr) |
| 117 | { |
| 118 | struct switchdev_attr_set_work *asw; |
| 119 | |
| 120 | asw = kmalloc(sizeof(*asw), GFP_ATOMIC); |
| 121 | if (!asw) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | INIT_WORK(&asw->work, switchdev_port_attr_set_work); |
| 125 | |
| 126 | dev_hold(dev); |
| 127 | asw->dev = dev; |
| 128 | memcpy(&asw->attr, attr, sizeof(asw->attr)); |
| 129 | |
| 130 | schedule_work(&asw->work); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * switchdev_port_attr_set - Set port attribute |
| 137 | * |
| 138 | * @dev: port device |
| 139 | * @attr: attribute to set |
| 140 | * |
| 141 | * Use a 2-phase prepare-commit transaction model to ensure |
| 142 | * system is not left in a partially updated state due to |
| 143 | * failure from driver/device. |
| 144 | */ |
| 145 | int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr) |
| 146 | { |
| 147 | int err; |
| 148 | |
| 149 | if (!rtnl_is_locked()) { |
| 150 | /* Running prepare-commit transaction across stacked |
| 151 | * devices requires nothing moves, so if rtnl_lock is |
| 152 | * not held, schedule a worker thread to hold rtnl_lock |
| 153 | * while setting attr. |
| 154 | */ |
| 155 | |
| 156 | return switchdev_port_attr_set_defer(dev, attr); |
| 157 | } |
| 158 | |
| 159 | /* Phase I: prepare for attr set. Driver/device should fail |
| 160 | * here if there are going to be issues in the commit phase, |
| 161 | * such as lack of resources or support. The driver/device |
| 162 | * should reserve resources needed for the commit phase here, |
| 163 | * but should not commit the attr. |
| 164 | */ |
| 165 | |
| 166 | attr->trans = SWITCHDEV_TRANS_PREPARE; |
| 167 | err = __switchdev_port_attr_set(dev, attr); |
| 168 | if (err) { |
| 169 | /* Prepare phase failed: abort the transaction. Any |
| 170 | * resources reserved in the prepare phase are |
| 171 | * released. |
| 172 | */ |
| 173 | |
| 174 | attr->trans = SWITCHDEV_TRANS_ABORT; |
| 175 | __switchdev_port_attr_set(dev, attr); |
| 176 | |
| 177 | return err; |
| 178 | } |
| 179 | |
| 180 | /* Phase II: commit attr set. This cannot fail as a fault |
| 181 | * of driver/device. If it does, it's a bug in the driver/device |
| 182 | * because the driver said everythings was OK in phase I. |
| 183 | */ |
| 184 | |
| 185 | attr->trans = SWITCHDEV_TRANS_COMMIT; |
| 186 | err = __switchdev_port_attr_set(dev, attr); |
| 187 | BUG_ON(err); |
| 188 | |
| 189 | return err; |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(switchdev_port_attr_set); |
| 192 | |
Scott Feldman | 22c1f67 | 2015-05-12 23:03:51 -0700 | [diff] [blame] | 193 | static int __switchdev_port_obj_add(struct net_device *dev, |
| 194 | struct switchdev_obj *obj) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 195 | { |
| 196 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 197 | struct net_device *lower_dev; |
| 198 | struct list_head *iter; |
| 199 | int err = -EOPNOTSUPP; |
| 200 | |
| 201 | if (ops && ops->switchdev_port_obj_add) |
| 202 | return ops->switchdev_port_obj_add(dev, obj); |
| 203 | |
| 204 | /* Switch device port(s) may be stacked under |
| 205 | * bond/team/vlan dev, so recurse down to add object on |
| 206 | * each port. |
| 207 | */ |
| 208 | |
| 209 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 210 | err = __switchdev_port_obj_add(lower_dev, obj); |
| 211 | if (err) |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * switchdev_port_obj_add - Add port object |
| 220 | * |
| 221 | * @dev: port device |
| 222 | * @obj: object to add |
| 223 | * |
| 224 | * Use a 2-phase prepare-commit transaction model to ensure |
| 225 | * system is not left in a partially updated state due to |
| 226 | * failure from driver/device. |
| 227 | * |
| 228 | * rtnl_lock must be held. |
| 229 | */ |
| 230 | int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj) |
| 231 | { |
| 232 | int err; |
| 233 | |
| 234 | ASSERT_RTNL(); |
| 235 | |
| 236 | /* Phase I: prepare for obj add. Driver/device should fail |
| 237 | * here if there are going to be issues in the commit phase, |
| 238 | * such as lack of resources or support. The driver/device |
| 239 | * should reserve resources needed for the commit phase here, |
| 240 | * but should not commit the obj. |
| 241 | */ |
| 242 | |
| 243 | obj->trans = SWITCHDEV_TRANS_PREPARE; |
| 244 | err = __switchdev_port_obj_add(dev, obj); |
| 245 | if (err) { |
| 246 | /* Prepare phase failed: abort the transaction. Any |
| 247 | * resources reserved in the prepare phase are |
| 248 | * released. |
| 249 | */ |
| 250 | |
| 251 | obj->trans = SWITCHDEV_TRANS_ABORT; |
| 252 | __switchdev_port_obj_add(dev, obj); |
| 253 | |
| 254 | return err; |
| 255 | } |
| 256 | |
| 257 | /* Phase II: commit obj add. This cannot fail as a fault |
| 258 | * of driver/device. If it does, it's a bug in the driver/device |
| 259 | * because the driver said everythings was OK in phase I. |
| 260 | */ |
| 261 | |
| 262 | obj->trans = SWITCHDEV_TRANS_COMMIT; |
| 263 | err = __switchdev_port_obj_add(dev, obj); |
| 264 | WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id); |
| 265 | |
| 266 | return err; |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(switchdev_port_obj_add); |
| 269 | |
| 270 | /** |
| 271 | * switchdev_port_obj_del - Delete port object |
| 272 | * |
| 273 | * @dev: port device |
| 274 | * @obj: object to delete |
| 275 | */ |
| 276 | int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj) |
| 277 | { |
| 278 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 279 | struct net_device *lower_dev; |
| 280 | struct list_head *iter; |
| 281 | int err = -EOPNOTSUPP; |
| 282 | |
| 283 | if (ops && ops->switchdev_port_obj_del) |
| 284 | return ops->switchdev_port_obj_del(dev, obj); |
| 285 | |
| 286 | /* Switch device port(s) may be stacked under |
| 287 | * bond/team/vlan dev, so recurse down to delete object on |
| 288 | * each port. |
| 289 | */ |
| 290 | |
| 291 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 292 | err = switchdev_port_obj_del(lower_dev, obj); |
| 293 | if (err) |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | return err; |
| 298 | } |
| 299 | EXPORT_SYMBOL_GPL(switchdev_port_obj_del); |
| 300 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 301 | /** |
| 302 | * switchdev_port_obj_dump - Dump port objects |
| 303 | * |
| 304 | * @dev: port device |
| 305 | * @obj: object to dump |
| 306 | */ |
| 307 | int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj) |
| 308 | { |
| 309 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 310 | struct net_device *lower_dev; |
| 311 | struct list_head *iter; |
| 312 | int err = -EOPNOTSUPP; |
| 313 | |
| 314 | if (ops && ops->switchdev_port_obj_dump) |
| 315 | return ops->switchdev_port_obj_dump(dev, obj); |
| 316 | |
| 317 | /* Switch device port(s) may be stacked under |
| 318 | * bond/team/vlan dev, so recurse down to dump objects on |
| 319 | * first port at bottom of stack. |
| 320 | */ |
| 321 | |
| 322 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 323 | err = switchdev_port_obj_dump(lower_dev, obj); |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | return err; |
| 328 | } |
| 329 | EXPORT_SYMBOL_GPL(switchdev_port_obj_dump); |
| 330 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 331 | static DEFINE_MUTEX(switchdev_mutex); |
| 332 | static RAW_NOTIFIER_HEAD(switchdev_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 333 | |
| 334 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 335 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 336 | * @nb: notifier_block |
| 337 | * |
| 338 | * Register switch device notifier. This should be used by code |
| 339 | * which needs to monitor events happening in particular device. |
| 340 | * Return values are same as for atomic_notifier_chain_register(). |
| 341 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 342 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 343 | { |
| 344 | int err; |
| 345 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 346 | mutex_lock(&switchdev_mutex); |
| 347 | err = raw_notifier_chain_register(&switchdev_notif_chain, nb); |
| 348 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 349 | return err; |
| 350 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 351 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 352 | |
| 353 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 354 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 355 | * @nb: notifier_block |
| 356 | * |
| 357 | * Unregister switch device notifier. |
| 358 | * Return values are same as for atomic_notifier_chain_unregister(). |
| 359 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 360 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 361 | { |
| 362 | int err; |
| 363 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 364 | mutex_lock(&switchdev_mutex); |
| 365 | err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb); |
| 366 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 367 | return err; |
| 368 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 369 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 370 | |
| 371 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 372 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 373 | * @val: value passed unmodified to notifier function |
| 374 | * @dev: port device |
| 375 | * @info: notifier information data |
| 376 | * |
| 377 | * Call all network notifier blocks. This should be called by driver |
| 378 | * when it needs to propagate hardware event. |
| 379 | * Return values are same as for atomic_notifier_call_chain(). |
| 380 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 381 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
| 382 | struct switchdev_notifier_info *info) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 383 | { |
| 384 | int err; |
| 385 | |
| 386 | info->dev = dev; |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 387 | mutex_lock(&switchdev_mutex); |
| 388 | err = raw_notifier_call_chain(&switchdev_notif_chain, val, info); |
| 389 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 390 | return err; |
| 391 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 392 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 393 | |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 394 | /** |
| 395 | * switchdev_port_bridge_getlink - Get bridge port attributes |
| 396 | * |
| 397 | * @dev: port device |
| 398 | * |
| 399 | * Called for SELF on rtnl_bridge_getlink to get bridge port |
| 400 | * attributes. |
| 401 | */ |
| 402 | int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
| 403 | struct net_device *dev, u32 filter_mask, |
| 404 | int nlflags) |
| 405 | { |
| 406 | struct switchdev_attr attr = { |
| 407 | .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS, |
| 408 | }; |
| 409 | u16 mode = BRIDGE_MODE_UNDEF; |
| 410 | u32 mask = BR_LEARNING | BR_LEARNING_SYNC; |
| 411 | int err; |
| 412 | |
| 413 | err = switchdev_port_attr_get(dev, &attr); |
| 414 | if (err) |
| 415 | return err; |
| 416 | |
| 417 | return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 418 | attr.u.brport_flags, mask, nlflags); |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 419 | } |
| 420 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink); |
| 421 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 422 | static int switchdev_port_br_setflag(struct net_device *dev, |
| 423 | struct nlattr *nlattr, |
| 424 | unsigned long brport_flag) |
| 425 | { |
| 426 | struct switchdev_attr attr = { |
| 427 | .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS, |
| 428 | }; |
| 429 | u8 flag = nla_get_u8(nlattr); |
| 430 | int err; |
| 431 | |
| 432 | err = switchdev_port_attr_get(dev, &attr); |
| 433 | if (err) |
| 434 | return err; |
| 435 | |
| 436 | if (flag) |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 437 | attr.u.brport_flags |= brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 438 | else |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 439 | attr.u.brport_flags &= ~brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 440 | |
| 441 | return switchdev_port_attr_set(dev, &attr); |
| 442 | } |
| 443 | |
| 444 | static const struct nla_policy |
| 445 | switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = { |
| 446 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, |
| 447 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, |
| 448 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, |
| 449 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, |
| 450 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
| 451 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
| 452 | [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 }, |
| 453 | [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, |
| 454 | [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 }, |
| 455 | [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, |
| 456 | }; |
| 457 | |
| 458 | static int switchdev_port_br_setlink_protinfo(struct net_device *dev, |
| 459 | struct nlattr *protinfo) |
| 460 | { |
| 461 | struct nlattr *attr; |
| 462 | int rem; |
| 463 | int err; |
| 464 | |
| 465 | err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX, |
| 466 | switchdev_port_bridge_policy); |
| 467 | if (err) |
| 468 | return err; |
| 469 | |
| 470 | nla_for_each_nested(attr, protinfo, rem) { |
| 471 | switch (nla_type(attr)) { |
| 472 | case IFLA_BRPORT_LEARNING: |
| 473 | err = switchdev_port_br_setflag(dev, attr, |
| 474 | BR_LEARNING); |
| 475 | break; |
| 476 | case IFLA_BRPORT_LEARNING_SYNC: |
| 477 | err = switchdev_port_br_setflag(dev, attr, |
| 478 | BR_LEARNING_SYNC); |
| 479 | break; |
| 480 | default: |
| 481 | err = -EOPNOTSUPP; |
| 482 | break; |
| 483 | } |
| 484 | if (err) |
| 485 | return err; |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static int switchdev_port_br_afspec(struct net_device *dev, |
| 492 | struct nlattr *afspec, |
| 493 | int (*f)(struct net_device *dev, |
| 494 | struct switchdev_obj *obj)) |
| 495 | { |
| 496 | struct nlattr *attr; |
| 497 | struct bridge_vlan_info *vinfo; |
| 498 | struct switchdev_obj obj = { |
| 499 | .id = SWITCHDEV_OBJ_PORT_VLAN, |
| 500 | }; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 501 | struct switchdev_obj_vlan *vlan = &obj.u.vlan; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 502 | int rem; |
| 503 | int err; |
| 504 | |
| 505 | nla_for_each_nested(attr, afspec, rem) { |
| 506 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) |
| 507 | continue; |
| 508 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) |
| 509 | return -EINVAL; |
| 510 | vinfo = nla_data(attr); |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 511 | vlan->flags = vinfo->flags; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 512 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 513 | if (vlan->vid_start) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 514 | return -EINVAL; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 515 | vlan->vid_start = vinfo->vid; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 516 | } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) { |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 517 | if (!vlan->vid_start) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 518 | return -EINVAL; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 519 | vlan->vid_end = vinfo->vid; |
| 520 | if (vlan->vid_end <= vlan->vid_start) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 521 | return -EINVAL; |
| 522 | err = f(dev, &obj); |
| 523 | if (err) |
| 524 | return err; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 525 | memset(vlan, 0, sizeof(*vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 526 | } else { |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 527 | if (vlan->vid_start) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 528 | return -EINVAL; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 529 | vlan->vid_start = vinfo->vid; |
| 530 | vlan->vid_end = vinfo->vid; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 531 | err = f(dev, &obj); |
| 532 | if (err) |
| 533 | return err; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 534 | memset(vlan, 0, sizeof(*vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 541 | /** |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 542 | * switchdev_port_bridge_setlink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 543 | * |
| 544 | * @dev: port device |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 545 | * @nlh: netlink header |
| 546 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 547 | * |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 548 | * Called for SELF on rtnl_bridge_setlink to set bridge port |
| 549 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 550 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 551 | int switchdev_port_bridge_setlink(struct net_device *dev, |
| 552 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 553 | { |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 554 | struct nlattr *protinfo; |
| 555 | struct nlattr *afspec; |
| 556 | int err = 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 557 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 558 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 559 | IFLA_PROTINFO); |
| 560 | if (protinfo) { |
| 561 | err = switchdev_port_br_setlink_protinfo(dev, protinfo); |
| 562 | if (err) |
| 563 | return err; |
| 564 | } |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 565 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 566 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 567 | IFLA_AF_SPEC); |
| 568 | if (afspec) |
| 569 | err = switchdev_port_br_afspec(dev, afspec, |
| 570 | switchdev_port_obj_add); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 571 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 572 | return err; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 573 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 574 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 575 | |
| 576 | /** |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 577 | * switchdev_port_bridge_dellink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 578 | * |
| 579 | * @dev: port device |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 580 | * @nlh: netlink header |
| 581 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 582 | * |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 583 | * Called for SELF on rtnl_bridge_dellink to set bridge port |
| 584 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 585 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 586 | int switchdev_port_bridge_dellink(struct net_device *dev, |
| 587 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 588 | { |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 589 | struct nlattr *afspec; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 590 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 591 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 592 | IFLA_AF_SPEC); |
| 593 | if (afspec) |
| 594 | return switchdev_port_br_afspec(dev, afspec, |
| 595 | switchdev_port_obj_del); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 596 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 597 | return 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 598 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 599 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 600 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 601 | /** |
| 602 | * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port |
| 603 | * |
| 604 | * @ndmsg: netlink hdr |
| 605 | * @nlattr: netlink attributes |
| 606 | * @dev: port device |
| 607 | * @addr: MAC address to add |
| 608 | * @vid: VLAN to add |
| 609 | * |
| 610 | * Add FDB entry to switch device. |
| 611 | */ |
| 612 | int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 613 | struct net_device *dev, const unsigned char *addr, |
| 614 | u16 vid, u16 nlm_flags) |
| 615 | { |
| 616 | struct switchdev_obj obj = { |
| 617 | .id = SWITCHDEV_OBJ_PORT_FDB, |
| 618 | .u.fdb = { |
| 619 | .addr = addr, |
| 620 | .vid = vid, |
| 621 | }, |
| 622 | }; |
| 623 | |
| 624 | return switchdev_port_obj_add(dev, &obj); |
| 625 | } |
| 626 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_add); |
| 627 | |
| 628 | /** |
| 629 | * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port |
| 630 | * |
| 631 | * @ndmsg: netlink hdr |
| 632 | * @nlattr: netlink attributes |
| 633 | * @dev: port device |
| 634 | * @addr: MAC address to delete |
| 635 | * @vid: VLAN to delete |
| 636 | * |
| 637 | * Delete FDB entry from switch device. |
| 638 | */ |
| 639 | int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
| 640 | struct net_device *dev, const unsigned char *addr, |
| 641 | u16 vid) |
| 642 | { |
| 643 | struct switchdev_obj obj = { |
| 644 | .id = SWITCHDEV_OBJ_PORT_FDB, |
| 645 | .u.fdb = { |
| 646 | .addr = addr, |
| 647 | .vid = vid, |
| 648 | }, |
| 649 | }; |
| 650 | |
| 651 | return switchdev_port_obj_del(dev, &obj); |
| 652 | } |
| 653 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_del); |
| 654 | |
| 655 | struct switchdev_fdb_dump { |
| 656 | struct switchdev_obj obj; |
| 657 | struct sk_buff *skb; |
| 658 | struct netlink_callback *cb; |
| 659 | struct net_device *filter_dev; |
| 660 | int idx; |
| 661 | }; |
| 662 | |
| 663 | static int switchdev_port_fdb_dump_cb(struct net_device *dev, |
| 664 | struct switchdev_obj *obj) |
| 665 | { |
| 666 | struct switchdev_fdb_dump *dump = |
| 667 | container_of(obj, struct switchdev_fdb_dump, obj); |
| 668 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 669 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 670 | struct nlmsghdr *nlh; |
| 671 | struct ndmsg *ndm; |
| 672 | struct net_device *master = netdev_master_upper_dev_get(dev); |
| 673 | |
| 674 | if (dump->idx < dump->cb->args[0]) |
| 675 | goto skip; |
| 676 | |
| 677 | if (master && dump->filter_dev != master) |
| 678 | goto skip; |
| 679 | |
| 680 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 681 | sizeof(*ndm), NLM_F_MULTI); |
| 682 | if (!nlh) |
| 683 | return -EMSGSIZE; |
| 684 | |
| 685 | ndm = nlmsg_data(nlh); |
| 686 | ndm->ndm_family = AF_BRIDGE; |
| 687 | ndm->ndm_pad1 = 0; |
| 688 | ndm->ndm_pad2 = 0; |
| 689 | ndm->ndm_flags = NTF_SELF; |
| 690 | ndm->ndm_type = 0; |
| 691 | ndm->ndm_ifindex = dev->ifindex; |
| 692 | ndm->ndm_state = NUD_REACHABLE; |
| 693 | |
| 694 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, obj->u.fdb.addr)) |
| 695 | goto nla_put_failure; |
| 696 | |
| 697 | if (obj->u.fdb.vid && nla_put_u16(dump->skb, NDA_VLAN, obj->u.fdb.vid)) |
| 698 | goto nla_put_failure; |
| 699 | |
| 700 | nlmsg_end(dump->skb, nlh); |
| 701 | |
| 702 | skip: |
| 703 | dump->idx++; |
| 704 | return 0; |
| 705 | |
| 706 | nla_put_failure: |
| 707 | nlmsg_cancel(dump->skb, nlh); |
| 708 | return -EMSGSIZE; |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries |
| 713 | * |
| 714 | * @skb: netlink skb |
| 715 | * @cb: netlink callback |
| 716 | * @dev: port device |
| 717 | * @filter_dev: filter device |
| 718 | * @idx: |
| 719 | * |
| 720 | * Delete FDB entry from switch device. |
| 721 | */ |
| 722 | int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 723 | struct net_device *dev, |
| 724 | struct net_device *filter_dev, int idx) |
| 725 | { |
| 726 | struct switchdev_fdb_dump dump = { |
| 727 | .obj = { |
| 728 | .id = SWITCHDEV_OBJ_PORT_FDB, |
| 729 | .cb = switchdev_port_fdb_dump_cb, |
| 730 | }, |
| 731 | .skb = skb, |
| 732 | .cb = cb, |
| 733 | .filter_dev = filter_dev, |
| 734 | .idx = idx, |
| 735 | }; |
| 736 | int err; |
| 737 | |
| 738 | err = switchdev_port_obj_dump(dev, &dump.obj); |
| 739 | if (err) |
| 740 | return err; |
| 741 | |
| 742 | return dump.idx; |
| 743 | } |
| 744 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump); |
| 745 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 746 | static struct net_device *switchdev_get_lowest_dev(struct net_device *dev) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 747 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 748 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 749 | struct net_device *lower_dev; |
| 750 | struct net_device *port_dev; |
| 751 | struct list_head *iter; |
| 752 | |
| 753 | /* Recusively search down until we find a sw port dev. |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 754 | * (A sw port dev supports switchdev_port_attr_get). |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 755 | */ |
| 756 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 757 | if (ops && ops->switchdev_port_attr_get) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 758 | return dev; |
| 759 | |
| 760 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 761 | port_dev = switchdev_get_lowest_dev(lower_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 762 | if (port_dev) |
| 763 | return port_dev; |
| 764 | } |
| 765 | |
| 766 | return NULL; |
| 767 | } |
| 768 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 769 | static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 770 | { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 771 | struct switchdev_attr attr = { |
| 772 | .id = SWITCHDEV_ATTR_PORT_PARENT_ID, |
| 773 | }; |
| 774 | struct switchdev_attr prev_attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 775 | struct net_device *dev = NULL; |
| 776 | int nhsel; |
| 777 | |
| 778 | /* For this route, all nexthop devs must be on the same switch. */ |
| 779 | |
| 780 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 781 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
| 782 | |
| 783 | if (!nh->nh_dev) |
| 784 | return NULL; |
| 785 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 786 | dev = switchdev_get_lowest_dev(nh->nh_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 787 | if (!dev) |
| 788 | return NULL; |
| 789 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 790 | if (switchdev_port_attr_get(dev, &attr)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 791 | return NULL; |
| 792 | |
| 793 | if (nhsel > 0) { |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 794 | if (prev_attr.u.ppid.id_len != attr.u.ppid.id_len) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 795 | return NULL; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 796 | if (memcmp(prev_attr.u.ppid.id, attr.u.ppid.id, |
| 797 | attr.u.ppid.id_len)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 798 | return NULL; |
| 799 | } |
| 800 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 801 | prev_attr = attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | return dev; |
| 805 | } |
| 806 | |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 807 | /** |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 808 | * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 809 | * |
| 810 | * @dst: route's IPv4 destination address |
| 811 | * @dst_len: destination address length (prefix length) |
| 812 | * @fi: route FIB info structure |
| 813 | * @tos: route TOS |
| 814 | * @type: route type |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 815 | * @nlflags: netlink flags passed in (NLM_F_*) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 816 | * @tb_id: route table ID |
| 817 | * |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 818 | * Add/modify switch IPv4 route entry. |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 819 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 820 | int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi, |
| 821 | u8 tos, u8 type, u32 nlflags, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 822 | { |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 823 | struct switchdev_obj fib_obj = { |
| 824 | .id = SWITCHDEV_OBJ_IPV4_FIB, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 825 | .u.ipv4_fib = { |
Scott Feldman | 7a7ee53 | 2015-05-12 23:03:52 -0700 | [diff] [blame] | 826 | .dst = dst, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 827 | .dst_len = dst_len, |
| 828 | .fi = fi, |
| 829 | .tos = tos, |
| 830 | .type = type, |
| 831 | .nlflags = nlflags, |
| 832 | .tb_id = tb_id, |
| 833 | }, |
| 834 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 835 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 836 | int err = 0; |
| 837 | |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 838 | /* Don't offload route if using custom ip rules or if |
| 839 | * IPv4 FIB offloading has been disabled completely. |
| 840 | */ |
| 841 | |
Scott Feldman | e1315db | 2015-03-06 01:14:36 -0800 | [diff] [blame] | 842 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 843 | if (fi->fib_net->ipv4.fib_has_custom_rules) |
| 844 | return 0; |
| 845 | #endif |
| 846 | |
| 847 | if (fi->fib_net->ipv4.fib_offload_disabled) |
Scott Feldman | 104616e | 2015-03-05 21:21:16 -0800 | [diff] [blame] | 848 | return 0; |
| 849 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 850 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 851 | if (!dev) |
| 852 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 853 | |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 854 | err = switchdev_port_obj_add(dev, &fib_obj); |
| 855 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 856 | fi->fib_flags |= RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 857 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 858 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 859 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 860 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add); |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 861 | |
| 862 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 863 | * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 864 | * |
| 865 | * @dst: route's IPv4 destination address |
| 866 | * @dst_len: destination address length (prefix length) |
| 867 | * @fi: route FIB info structure |
| 868 | * @tos: route TOS |
| 869 | * @type: route type |
| 870 | * @tb_id: route table ID |
| 871 | * |
| 872 | * Delete IPv4 route entry from switch device. |
| 873 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 874 | int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi, |
| 875 | u8 tos, u8 type, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 876 | { |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 877 | struct switchdev_obj fib_obj = { |
| 878 | .id = SWITCHDEV_OBJ_IPV4_FIB, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 879 | .u.ipv4_fib = { |
Scott Feldman | 7a7ee53 | 2015-05-12 23:03:52 -0700 | [diff] [blame] | 880 | .dst = dst, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 881 | .dst_len = dst_len, |
| 882 | .fi = fi, |
| 883 | .tos = tos, |
| 884 | .type = type, |
| 885 | .nlflags = 0, |
| 886 | .tb_id = tb_id, |
| 887 | }, |
| 888 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 889 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 890 | int err = 0; |
| 891 | |
Roopa Prabhu | eea3994 | 2015-05-13 21:17:41 -0700 | [diff] [blame] | 892 | if (!(fi->fib_flags & RTNH_F_OFFLOAD)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 893 | return 0; |
| 894 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 895 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 896 | if (!dev) |
| 897 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 898 | |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 899 | err = switchdev_port_obj_del(dev, &fib_obj); |
| 900 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 901 | fi->fib_flags &= ~RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 902 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 903 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 904 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 905 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del); |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 906 | |
| 907 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 908 | * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 909 | * |
| 910 | * @fi: route FIB info structure |
| 911 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 912 | void switchdev_fib_ipv4_abort(struct fib_info *fi) |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 913 | { |
| 914 | /* There was a problem installing this route to the offload |
| 915 | * device. For now, until we come up with more refined |
| 916 | * policy handling, abruptly end IPv4 fib offloading for |
| 917 | * for entire net by flushing offload device(s) of all |
| 918 | * IPv4 routes, and mark IPv4 fib offloading broken from |
| 919 | * this point forward. |
| 920 | */ |
| 921 | |
| 922 | fib_flush_external(fi->fib_net); |
| 923 | fi->fib_net->ipv4.fib_offload_disabled = true; |
| 924 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 925 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort); |