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