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); |
| 106 | BUG_ON(err); |
| 107 | rtnl_unlock(); |
| 108 | |
| 109 | dev_put(asw->dev); |
| 110 | kfree(work); |
| 111 | } |
| 112 | |
| 113 | static int switchdev_port_attr_set_defer(struct net_device *dev, |
| 114 | struct switchdev_attr *attr) |
| 115 | { |
| 116 | struct switchdev_attr_set_work *asw; |
| 117 | |
| 118 | asw = kmalloc(sizeof(*asw), GFP_ATOMIC); |
| 119 | if (!asw) |
| 120 | return -ENOMEM; |
| 121 | |
| 122 | INIT_WORK(&asw->work, switchdev_port_attr_set_work); |
| 123 | |
| 124 | dev_hold(dev); |
| 125 | asw->dev = dev; |
| 126 | memcpy(&asw->attr, attr, sizeof(asw->attr)); |
| 127 | |
| 128 | schedule_work(&asw->work); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * switchdev_port_attr_set - Set port attribute |
| 135 | * |
| 136 | * @dev: port device |
| 137 | * @attr: attribute to set |
| 138 | * |
| 139 | * Use a 2-phase prepare-commit transaction model to ensure |
| 140 | * system is not left in a partially updated state due to |
| 141 | * failure from driver/device. |
| 142 | */ |
| 143 | int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr) |
| 144 | { |
| 145 | int err; |
| 146 | |
| 147 | if (!rtnl_is_locked()) { |
| 148 | /* Running prepare-commit transaction across stacked |
| 149 | * devices requires nothing moves, so if rtnl_lock is |
| 150 | * not held, schedule a worker thread to hold rtnl_lock |
| 151 | * while setting attr. |
| 152 | */ |
| 153 | |
| 154 | return switchdev_port_attr_set_defer(dev, attr); |
| 155 | } |
| 156 | |
| 157 | /* Phase I: prepare for attr set. Driver/device should fail |
| 158 | * here if there are going to be issues in the commit phase, |
| 159 | * such as lack of resources or support. The driver/device |
| 160 | * should reserve resources needed for the commit phase here, |
| 161 | * but should not commit the attr. |
| 162 | */ |
| 163 | |
| 164 | attr->trans = SWITCHDEV_TRANS_PREPARE; |
| 165 | err = __switchdev_port_attr_set(dev, attr); |
| 166 | if (err) { |
| 167 | /* Prepare phase failed: abort the transaction. Any |
| 168 | * resources reserved in the prepare phase are |
| 169 | * released. |
| 170 | */ |
| 171 | |
| 172 | attr->trans = SWITCHDEV_TRANS_ABORT; |
| 173 | __switchdev_port_attr_set(dev, attr); |
| 174 | |
| 175 | return err; |
| 176 | } |
| 177 | |
| 178 | /* Phase II: commit attr set. This cannot fail as a fault |
| 179 | * of driver/device. If it does, it's a bug in the driver/device |
| 180 | * because the driver said everythings was OK in phase I. |
| 181 | */ |
| 182 | |
| 183 | attr->trans = SWITCHDEV_TRANS_COMMIT; |
| 184 | err = __switchdev_port_attr_set(dev, attr); |
| 185 | BUG_ON(err); |
| 186 | |
| 187 | return err; |
| 188 | } |
| 189 | EXPORT_SYMBOL_GPL(switchdev_port_attr_set); |
| 190 | |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 191 | int __switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj) |
| 192 | { |
| 193 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 194 | struct net_device *lower_dev; |
| 195 | struct list_head *iter; |
| 196 | int err = -EOPNOTSUPP; |
| 197 | |
| 198 | if (ops && ops->switchdev_port_obj_add) |
| 199 | return ops->switchdev_port_obj_add(dev, obj); |
| 200 | |
| 201 | /* Switch device port(s) may be stacked under |
| 202 | * bond/team/vlan dev, so recurse down to add object on |
| 203 | * each port. |
| 204 | */ |
| 205 | |
| 206 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 207 | err = __switchdev_port_obj_add(lower_dev, obj); |
| 208 | if (err) |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | return err; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * switchdev_port_obj_add - Add port object |
| 217 | * |
| 218 | * @dev: port device |
| 219 | * @obj: object to add |
| 220 | * |
| 221 | * Use a 2-phase prepare-commit transaction model to ensure |
| 222 | * system is not left in a partially updated state due to |
| 223 | * failure from driver/device. |
| 224 | * |
| 225 | * rtnl_lock must be held. |
| 226 | */ |
| 227 | int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj) |
| 228 | { |
| 229 | int err; |
| 230 | |
| 231 | ASSERT_RTNL(); |
| 232 | |
| 233 | /* Phase I: prepare for obj add. Driver/device should fail |
| 234 | * here if there are going to be issues in the commit phase, |
| 235 | * such as lack of resources or support. The driver/device |
| 236 | * should reserve resources needed for the commit phase here, |
| 237 | * but should not commit the obj. |
| 238 | */ |
| 239 | |
| 240 | obj->trans = SWITCHDEV_TRANS_PREPARE; |
| 241 | err = __switchdev_port_obj_add(dev, obj); |
| 242 | if (err) { |
| 243 | /* Prepare phase failed: abort the transaction. Any |
| 244 | * resources reserved in the prepare phase are |
| 245 | * released. |
| 246 | */ |
| 247 | |
| 248 | obj->trans = SWITCHDEV_TRANS_ABORT; |
| 249 | __switchdev_port_obj_add(dev, obj); |
| 250 | |
| 251 | return err; |
| 252 | } |
| 253 | |
| 254 | /* Phase II: commit obj add. This cannot fail as a fault |
| 255 | * of driver/device. If it does, it's a bug in the driver/device |
| 256 | * because the driver said everythings was OK in phase I. |
| 257 | */ |
| 258 | |
| 259 | obj->trans = SWITCHDEV_TRANS_COMMIT; |
| 260 | err = __switchdev_port_obj_add(dev, obj); |
| 261 | WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id); |
| 262 | |
| 263 | return err; |
| 264 | } |
| 265 | EXPORT_SYMBOL_GPL(switchdev_port_obj_add); |
| 266 | |
| 267 | /** |
| 268 | * switchdev_port_obj_del - Delete port object |
| 269 | * |
| 270 | * @dev: port device |
| 271 | * @obj: object to delete |
| 272 | */ |
| 273 | int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj) |
| 274 | { |
| 275 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 276 | struct net_device *lower_dev; |
| 277 | struct list_head *iter; |
| 278 | int err = -EOPNOTSUPP; |
| 279 | |
| 280 | if (ops && ops->switchdev_port_obj_del) |
| 281 | return ops->switchdev_port_obj_del(dev, obj); |
| 282 | |
| 283 | /* Switch device port(s) may be stacked under |
| 284 | * bond/team/vlan dev, so recurse down to delete object on |
| 285 | * each port. |
| 286 | */ |
| 287 | |
| 288 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 289 | err = switchdev_port_obj_del(lower_dev, obj); |
| 290 | if (err) |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | return err; |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(switchdev_port_obj_del); |
| 297 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 298 | static DEFINE_MUTEX(switchdev_mutex); |
| 299 | static RAW_NOTIFIER_HEAD(switchdev_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 300 | |
| 301 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 302 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 303 | * @nb: notifier_block |
| 304 | * |
| 305 | * Register switch device notifier. This should be used by code |
| 306 | * which needs to monitor events happening in particular device. |
| 307 | * Return values are same as for atomic_notifier_chain_register(). |
| 308 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 309 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 310 | { |
| 311 | int err; |
| 312 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 313 | mutex_lock(&switchdev_mutex); |
| 314 | err = raw_notifier_chain_register(&switchdev_notif_chain, nb); |
| 315 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 316 | return err; |
| 317 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 318 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 319 | |
| 320 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 321 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 322 | * @nb: notifier_block |
| 323 | * |
| 324 | * Unregister switch device notifier. |
| 325 | * Return values are same as for atomic_notifier_chain_unregister(). |
| 326 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 327 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 328 | { |
| 329 | int err; |
| 330 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 331 | mutex_lock(&switchdev_mutex); |
| 332 | err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb); |
| 333 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 334 | return err; |
| 335 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 336 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 337 | |
| 338 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 339 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 340 | * @val: value passed unmodified to notifier function |
| 341 | * @dev: port device |
| 342 | * @info: notifier information data |
| 343 | * |
| 344 | * Call all network notifier blocks. This should be called by driver |
| 345 | * when it needs to propagate hardware event. |
| 346 | * Return values are same as for atomic_notifier_call_chain(). |
| 347 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 348 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
| 349 | struct switchdev_notifier_info *info) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 350 | { |
| 351 | int err; |
| 352 | |
| 353 | info->dev = dev; |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 354 | mutex_lock(&switchdev_mutex); |
| 355 | err = raw_notifier_call_chain(&switchdev_notif_chain, val, info); |
| 356 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 357 | return err; |
| 358 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 359 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 360 | |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame^] | 361 | /** |
| 362 | * switchdev_port_bridge_getlink - Get bridge port attributes |
| 363 | * |
| 364 | * @dev: port device |
| 365 | * |
| 366 | * Called for SELF on rtnl_bridge_getlink to get bridge port |
| 367 | * attributes. |
| 368 | */ |
| 369 | int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
| 370 | struct net_device *dev, u32 filter_mask, |
| 371 | int nlflags) |
| 372 | { |
| 373 | struct switchdev_attr attr = { |
| 374 | .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS, |
| 375 | }; |
| 376 | u16 mode = BRIDGE_MODE_UNDEF; |
| 377 | u32 mask = BR_LEARNING | BR_LEARNING_SYNC; |
| 378 | int err; |
| 379 | |
| 380 | err = switchdev_port_attr_get(dev, &attr); |
| 381 | if (err) |
| 382 | return err; |
| 383 | |
| 384 | return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, |
| 385 | attr.brport_flags, mask, nlflags); |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink); |
| 388 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 389 | static int switchdev_port_br_setflag(struct net_device *dev, |
| 390 | struct nlattr *nlattr, |
| 391 | unsigned long brport_flag) |
| 392 | { |
| 393 | struct switchdev_attr attr = { |
| 394 | .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS, |
| 395 | }; |
| 396 | u8 flag = nla_get_u8(nlattr); |
| 397 | int err; |
| 398 | |
| 399 | err = switchdev_port_attr_get(dev, &attr); |
| 400 | if (err) |
| 401 | return err; |
| 402 | |
| 403 | if (flag) |
| 404 | attr.brport_flags |= brport_flag; |
| 405 | else |
| 406 | attr.brport_flags &= ~brport_flag; |
| 407 | |
| 408 | return switchdev_port_attr_set(dev, &attr); |
| 409 | } |
| 410 | |
| 411 | static const struct nla_policy |
| 412 | switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = { |
| 413 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, |
| 414 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, |
| 415 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, |
| 416 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, |
| 417 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
| 418 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
| 419 | [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 }, |
| 420 | [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, |
| 421 | [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 }, |
| 422 | [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, |
| 423 | }; |
| 424 | |
| 425 | static int switchdev_port_br_setlink_protinfo(struct net_device *dev, |
| 426 | struct nlattr *protinfo) |
| 427 | { |
| 428 | struct nlattr *attr; |
| 429 | int rem; |
| 430 | int err; |
| 431 | |
| 432 | err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX, |
| 433 | switchdev_port_bridge_policy); |
| 434 | if (err) |
| 435 | return err; |
| 436 | |
| 437 | nla_for_each_nested(attr, protinfo, rem) { |
| 438 | switch (nla_type(attr)) { |
| 439 | case IFLA_BRPORT_LEARNING: |
| 440 | err = switchdev_port_br_setflag(dev, attr, |
| 441 | BR_LEARNING); |
| 442 | break; |
| 443 | case IFLA_BRPORT_LEARNING_SYNC: |
| 444 | err = switchdev_port_br_setflag(dev, attr, |
| 445 | BR_LEARNING_SYNC); |
| 446 | break; |
| 447 | default: |
| 448 | err = -EOPNOTSUPP; |
| 449 | break; |
| 450 | } |
| 451 | if (err) |
| 452 | return err; |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int switchdev_port_br_afspec(struct net_device *dev, |
| 459 | struct nlattr *afspec, |
| 460 | int (*f)(struct net_device *dev, |
| 461 | struct switchdev_obj *obj)) |
| 462 | { |
| 463 | struct nlattr *attr; |
| 464 | struct bridge_vlan_info *vinfo; |
| 465 | struct switchdev_obj obj = { |
| 466 | .id = SWITCHDEV_OBJ_PORT_VLAN, |
| 467 | }; |
| 468 | int rem; |
| 469 | int err; |
| 470 | |
| 471 | nla_for_each_nested(attr, afspec, rem) { |
| 472 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) |
| 473 | continue; |
| 474 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) |
| 475 | return -EINVAL; |
| 476 | vinfo = nla_data(attr); |
| 477 | obj.vlan.flags = vinfo->flags; |
| 478 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { |
| 479 | if (obj.vlan.vid_start) |
| 480 | return -EINVAL; |
| 481 | obj.vlan.vid_start = vinfo->vid; |
| 482 | } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) { |
| 483 | if (!obj.vlan.vid_start) |
| 484 | return -EINVAL; |
| 485 | obj.vlan.vid_end = vinfo->vid; |
| 486 | if (obj.vlan.vid_end <= obj.vlan.vid_start) |
| 487 | return -EINVAL; |
| 488 | err = f(dev, &obj); |
| 489 | if (err) |
| 490 | return err; |
| 491 | memset(&obj.vlan, 0, sizeof(obj.vlan)); |
| 492 | } else { |
| 493 | if (obj.vlan.vid_start) |
| 494 | return -EINVAL; |
| 495 | obj.vlan.vid_start = vinfo->vid; |
| 496 | obj.vlan.vid_end = vinfo->vid; |
| 497 | err = f(dev, &obj); |
| 498 | if (err) |
| 499 | return err; |
| 500 | memset(&obj.vlan, 0, sizeof(obj.vlan)); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 507 | /** |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 508 | * switchdev_port_bridge_setlink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 509 | * |
| 510 | * @dev: port device |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 511 | * @nlh: netlink header |
| 512 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 513 | * |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 514 | * Called for SELF on rtnl_bridge_setlink to set bridge port |
| 515 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 516 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 517 | int switchdev_port_bridge_setlink(struct net_device *dev, |
| 518 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 519 | { |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 520 | struct nlattr *protinfo; |
| 521 | struct nlattr *afspec; |
| 522 | int err = 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 523 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 524 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 525 | IFLA_PROTINFO); |
| 526 | if (protinfo) { |
| 527 | err = switchdev_port_br_setlink_protinfo(dev, protinfo); |
| 528 | if (err) |
| 529 | return err; |
| 530 | } |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 531 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 532 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 533 | IFLA_AF_SPEC); |
| 534 | if (afspec) |
| 535 | err = switchdev_port_br_afspec(dev, afspec, |
| 536 | switchdev_port_obj_add); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 537 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 538 | return err; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 539 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 540 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 541 | |
| 542 | /** |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 543 | * switchdev_port_bridge_dellink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 544 | * |
| 545 | * @dev: port device |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 546 | * @nlh: netlink header |
| 547 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 548 | * |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 549 | * Called for SELF on rtnl_bridge_dellink to set bridge port |
| 550 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 551 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 552 | int switchdev_port_bridge_dellink(struct net_device *dev, |
| 553 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 554 | { |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 555 | struct nlattr *afspec; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 556 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 557 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 558 | IFLA_AF_SPEC); |
| 559 | if (afspec) |
| 560 | return switchdev_port_br_afspec(dev, afspec, |
| 561 | switchdev_port_obj_del); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 562 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 563 | return 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 564 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 565 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 566 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 567 | static struct net_device *switchdev_get_lowest_dev(struct net_device *dev) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 568 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 569 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 570 | struct net_device *lower_dev; |
| 571 | struct net_device *port_dev; |
| 572 | struct list_head *iter; |
| 573 | |
| 574 | /* Recusively search down until we find a sw port dev. |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 575 | * (A sw port dev supports switchdev_port_attr_get). |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 576 | */ |
| 577 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 578 | if (ops && ops->switchdev_port_attr_get) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 579 | return dev; |
| 580 | |
| 581 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 582 | port_dev = switchdev_get_lowest_dev(lower_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 583 | if (port_dev) |
| 584 | return port_dev; |
| 585 | } |
| 586 | |
| 587 | return NULL; |
| 588 | } |
| 589 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 590 | 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] | 591 | { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 592 | struct switchdev_attr attr = { |
| 593 | .id = SWITCHDEV_ATTR_PORT_PARENT_ID, |
| 594 | }; |
| 595 | struct switchdev_attr prev_attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 596 | struct net_device *dev = NULL; |
| 597 | int nhsel; |
| 598 | |
| 599 | /* For this route, all nexthop devs must be on the same switch. */ |
| 600 | |
| 601 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 602 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
| 603 | |
| 604 | if (!nh->nh_dev) |
| 605 | return NULL; |
| 606 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 607 | dev = switchdev_get_lowest_dev(nh->nh_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 608 | if (!dev) |
| 609 | return NULL; |
| 610 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 611 | if (switchdev_port_attr_get(dev, &attr)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 612 | return NULL; |
| 613 | |
| 614 | if (nhsel > 0) { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 615 | if (prev_attr.ppid.id_len != attr.ppid.id_len) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 616 | return NULL; |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 617 | if (memcmp(prev_attr.ppid.id, attr.ppid.id, |
| 618 | attr.ppid.id_len)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 619 | return NULL; |
| 620 | } |
| 621 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 622 | prev_attr = attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | return dev; |
| 626 | } |
| 627 | |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 628 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 629 | * switchdev_fib_ipv4_add - Add IPv4 route entry to switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 630 | * |
| 631 | * @dst: route's IPv4 destination address |
| 632 | * @dst_len: destination address length (prefix length) |
| 633 | * @fi: route FIB info structure |
| 634 | * @tos: route TOS |
| 635 | * @type: route type |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 636 | * @nlflags: netlink flags passed in (NLM_F_*) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 637 | * @tb_id: route table ID |
| 638 | * |
| 639 | * Add IPv4 route entry to switch device. |
| 640 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 641 | int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi, |
| 642 | u8 tos, u8 type, u32 nlflags, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 643 | { |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 644 | struct net_device *dev; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 645 | const struct switchdev_ops *ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 646 | int err = 0; |
| 647 | |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 648 | /* Don't offload route if using custom ip rules or if |
| 649 | * IPv4 FIB offloading has been disabled completely. |
| 650 | */ |
| 651 | |
Scott Feldman | e1315db | 2015-03-06 01:14:36 -0800 | [diff] [blame] | 652 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 653 | if (fi->fib_net->ipv4.fib_has_custom_rules) |
| 654 | return 0; |
| 655 | #endif |
| 656 | |
| 657 | if (fi->fib_net->ipv4.fib_offload_disabled) |
Scott Feldman | 104616e | 2015-03-05 21:21:16 -0800 | [diff] [blame] | 658 | return 0; |
| 659 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 660 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 661 | if (!dev) |
| 662 | return 0; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 663 | ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 664 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 665 | if (ops->switchdev_fib_ipv4_add) { |
| 666 | err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len, |
| 667 | fi, tos, type, nlflags, |
| 668 | tb_id); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 669 | if (!err) |
| 670 | fi->fib_flags |= RTNH_F_EXTERNAL; |
| 671 | } |
| 672 | |
| 673 | return err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 674 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 675 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add); |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 676 | |
| 677 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 678 | * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 679 | * |
| 680 | * @dst: route's IPv4 destination address |
| 681 | * @dst_len: destination address length (prefix length) |
| 682 | * @fi: route FIB info structure |
| 683 | * @tos: route TOS |
| 684 | * @type: route type |
| 685 | * @tb_id: route table ID |
| 686 | * |
| 687 | * Delete IPv4 route entry from switch device. |
| 688 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 689 | int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi, |
| 690 | u8 tos, u8 type, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 691 | { |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 692 | struct net_device *dev; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 693 | const struct switchdev_ops *ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 694 | int err = 0; |
| 695 | |
| 696 | if (!(fi->fib_flags & RTNH_F_EXTERNAL)) |
| 697 | return 0; |
| 698 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 699 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 700 | if (!dev) |
| 701 | return 0; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 702 | ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 703 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 704 | if (ops->switchdev_fib_ipv4_del) { |
| 705 | err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len, |
| 706 | fi, tos, type, tb_id); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 707 | if (!err) |
| 708 | fi->fib_flags &= ~RTNH_F_EXTERNAL; |
| 709 | } |
| 710 | |
| 711 | return err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 712 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 713 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del); |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 714 | |
| 715 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 716 | * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 717 | * |
| 718 | * @fi: route FIB info structure |
| 719 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 720 | void switchdev_fib_ipv4_abort(struct fib_info *fi) |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 721 | { |
| 722 | /* There was a problem installing this route to the offload |
| 723 | * device. For now, until we come up with more refined |
| 724 | * policy handling, abruptly end IPv4 fib offloading for |
| 725 | * for entire net by flushing offload device(s) of all |
| 726 | * IPv4 routes, and mark IPv4 fib offloading broken from |
| 727 | * this point forward. |
| 728 | */ |
| 729 | |
| 730 | fib_flush_external(fi->fib_net); |
| 731 | fi->fib_net->ipv4.fib_offload_disabled = true; |
| 732 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 733 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort); |