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