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 | 57225e7 | 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 |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 389 | * @id: object ID |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 390 | * @obj: object to dump |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 391 | * @cb: function to call with a filled object |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 392 | */ |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 393 | int switchdev_port_obj_dump(struct net_device *dev, enum switchdev_obj_id id, |
| 394 | void *obj, int (*cb)(void *obj)) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 395 | { |
| 396 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 397 | struct net_device *lower_dev; |
| 398 | struct list_head *iter; |
| 399 | int err = -EOPNOTSUPP; |
| 400 | |
| 401 | if (ops && ops->switchdev_port_obj_dump) |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 402 | return ops->switchdev_port_obj_dump(dev, id, obj, cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 403 | |
| 404 | /* Switch device port(s) may be stacked under |
| 405 | * bond/team/vlan dev, so recurse down to dump objects on |
| 406 | * first port at bottom of stack. |
| 407 | */ |
| 408 | |
| 409 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 410 | err = switchdev_port_obj_dump(lower_dev, id, obj, cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
| 414 | return err; |
| 415 | } |
| 416 | EXPORT_SYMBOL_GPL(switchdev_port_obj_dump); |
| 417 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 418 | static DEFINE_MUTEX(switchdev_mutex); |
| 419 | static RAW_NOTIFIER_HEAD(switchdev_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 420 | |
| 421 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 422 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 423 | * @nb: notifier_block |
| 424 | * |
| 425 | * Register switch device notifier. This should be used by code |
| 426 | * which needs to monitor events happening in particular device. |
| 427 | * Return values are same as for atomic_notifier_chain_register(). |
| 428 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 429 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 430 | { |
| 431 | int err; |
| 432 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 433 | mutex_lock(&switchdev_mutex); |
| 434 | err = raw_notifier_chain_register(&switchdev_notif_chain, nb); |
| 435 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 436 | return err; |
| 437 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 438 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 439 | |
| 440 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 441 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 442 | * @nb: notifier_block |
| 443 | * |
| 444 | * Unregister switch device notifier. |
| 445 | * Return values are same as for atomic_notifier_chain_unregister(). |
| 446 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 447 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 448 | { |
| 449 | int err; |
| 450 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 451 | mutex_lock(&switchdev_mutex); |
| 452 | err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb); |
| 453 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 454 | return err; |
| 455 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 457 | |
| 458 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 459 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 460 | * @val: value passed unmodified to notifier function |
| 461 | * @dev: port device |
| 462 | * @info: notifier information data |
| 463 | * |
| 464 | * Call all network notifier blocks. This should be called by driver |
| 465 | * when it needs to propagate hardware event. |
| 466 | * Return values are same as for atomic_notifier_call_chain(). |
| 467 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 468 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
| 469 | struct switchdev_notifier_info *info) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 470 | { |
| 471 | int err; |
| 472 | |
| 473 | info->dev = dev; |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 474 | mutex_lock(&switchdev_mutex); |
| 475 | err = raw_notifier_call_chain(&switchdev_notif_chain, val, info); |
| 476 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 477 | return err; |
| 478 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 479 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 480 | |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 481 | struct switchdev_vlan_dump { |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 482 | struct switchdev_obj_vlan vlan; |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 483 | struct sk_buff *skb; |
| 484 | u32 filter_mask; |
| 485 | u16 flags; |
| 486 | u16 begin; |
| 487 | u16 end; |
| 488 | }; |
| 489 | |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 490 | static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump) |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 491 | { |
| 492 | struct bridge_vlan_info vinfo; |
| 493 | |
| 494 | vinfo.flags = dump->flags; |
| 495 | |
| 496 | if (dump->begin == 0 && dump->end == 0) { |
| 497 | return 0; |
| 498 | } else if (dump->begin == dump->end) { |
| 499 | vinfo.vid = dump->begin; |
| 500 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 501 | sizeof(vinfo), &vinfo)) |
| 502 | return -EMSGSIZE; |
| 503 | } else { |
| 504 | vinfo.vid = dump->begin; |
| 505 | vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 506 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 507 | sizeof(vinfo), &vinfo)) |
| 508 | return -EMSGSIZE; |
| 509 | vinfo.vid = dump->end; |
| 510 | vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 511 | vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END; |
| 512 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 513 | sizeof(vinfo), &vinfo)) |
| 514 | return -EMSGSIZE; |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 520 | static int switchdev_port_vlan_dump_cb(void *obj) |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 521 | { |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 522 | struct switchdev_obj_vlan *vlan = obj; |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 523 | struct switchdev_vlan_dump *dump = |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 524 | container_of(vlan, struct switchdev_vlan_dump, vlan); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 525 | int err = 0; |
| 526 | |
| 527 | if (vlan->vid_begin > vlan->vid_end) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | if (dump->filter_mask & RTEXT_FILTER_BRVLAN) { |
| 531 | dump->flags = vlan->flags; |
| 532 | for (dump->begin = dump->end = vlan->vid_begin; |
| 533 | dump->begin <= vlan->vid_end; |
| 534 | dump->begin++, dump->end++) { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 535 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 536 | if (err) |
| 537 | return err; |
| 538 | } |
| 539 | } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) { |
| 540 | if (dump->begin > vlan->vid_begin && |
| 541 | dump->begin >= vlan->vid_end) { |
| 542 | if ((dump->begin - 1) == vlan->vid_end && |
| 543 | dump->flags == vlan->flags) { |
| 544 | /* prepend */ |
| 545 | dump->begin = vlan->vid_begin; |
| 546 | } else { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 547 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 548 | dump->flags = vlan->flags; |
| 549 | dump->begin = vlan->vid_begin; |
| 550 | dump->end = vlan->vid_end; |
| 551 | } |
| 552 | } else if (dump->end <= vlan->vid_begin && |
| 553 | dump->end < vlan->vid_end) { |
| 554 | if ((dump->end + 1) == vlan->vid_begin && |
| 555 | dump->flags == vlan->flags) { |
| 556 | /* append */ |
| 557 | dump->end = vlan->vid_end; |
| 558 | } else { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 559 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 560 | dump->flags = vlan->flags; |
| 561 | dump->begin = vlan->vid_begin; |
| 562 | dump->end = vlan->vid_end; |
| 563 | } |
| 564 | } else { |
| 565 | err = -EINVAL; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return err; |
| 570 | } |
| 571 | |
| 572 | static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev, |
| 573 | u32 filter_mask) |
| 574 | { |
| 575 | struct switchdev_vlan_dump dump = { |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 576 | .skb = skb, |
| 577 | .filter_mask = filter_mask, |
| 578 | }; |
| 579 | int err = 0; |
| 580 | |
| 581 | if ((filter_mask & RTEXT_FILTER_BRVLAN) || |
| 582 | (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) { |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 583 | err = switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_PORT_VLAN, |
| 584 | &dump.vlan, |
| 585 | switchdev_port_vlan_dump_cb); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 586 | if (err) |
| 587 | goto err_out; |
| 588 | if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) |
| 589 | /* last one */ |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 590 | err = switchdev_port_vlan_dump_put(&dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | err_out: |
| 594 | return err == -EOPNOTSUPP ? 0 : err; |
| 595 | } |
| 596 | |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 597 | /** |
| 598 | * switchdev_port_bridge_getlink - Get bridge port attributes |
| 599 | * |
| 600 | * @dev: port device |
| 601 | * |
| 602 | * Called for SELF on rtnl_bridge_getlink to get bridge port |
| 603 | * attributes. |
| 604 | */ |
| 605 | int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
| 606 | struct net_device *dev, u32 filter_mask, |
| 607 | int nlflags) |
| 608 | { |
| 609 | struct switchdev_attr attr = { |
| 610 | .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS, |
| 611 | }; |
| 612 | u16 mode = BRIDGE_MODE_UNDEF; |
| 613 | u32 mask = BR_LEARNING | BR_LEARNING_SYNC; |
| 614 | int err; |
| 615 | |
| 616 | err = switchdev_port_attr_get(dev, &attr); |
Vivien Didelot | 5c8079d | 2015-06-23 10:26:04 -0400 | [diff] [blame] | 617 | if (err && err != -EOPNOTSUPP) |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 618 | return err; |
| 619 | |
| 620 | return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 621 | attr.u.brport_flags, mask, nlflags, |
| 622 | filter_mask, switchdev_port_vlan_fill); |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 623 | } |
| 624 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink); |
| 625 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 626 | static int switchdev_port_br_setflag(struct net_device *dev, |
| 627 | struct nlattr *nlattr, |
| 628 | unsigned long brport_flag) |
| 629 | { |
| 630 | struct switchdev_attr attr = { |
| 631 | .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS, |
| 632 | }; |
| 633 | u8 flag = nla_get_u8(nlattr); |
| 634 | int err; |
| 635 | |
| 636 | err = switchdev_port_attr_get(dev, &attr); |
| 637 | if (err) |
| 638 | return err; |
| 639 | |
| 640 | if (flag) |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 641 | attr.u.brport_flags |= brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 642 | else |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 643 | attr.u.brport_flags &= ~brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 644 | |
| 645 | return switchdev_port_attr_set(dev, &attr); |
| 646 | } |
| 647 | |
| 648 | static const struct nla_policy |
| 649 | switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = { |
| 650 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, |
| 651 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, |
| 652 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, |
| 653 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, |
| 654 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
| 655 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
| 656 | [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 }, |
| 657 | [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, |
| 658 | [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 }, |
| 659 | [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, |
| 660 | }; |
| 661 | |
| 662 | static int switchdev_port_br_setlink_protinfo(struct net_device *dev, |
| 663 | struct nlattr *protinfo) |
| 664 | { |
| 665 | struct nlattr *attr; |
| 666 | int rem; |
| 667 | int err; |
| 668 | |
| 669 | err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX, |
| 670 | switchdev_port_bridge_policy); |
| 671 | if (err) |
| 672 | return err; |
| 673 | |
| 674 | nla_for_each_nested(attr, protinfo, rem) { |
| 675 | switch (nla_type(attr)) { |
| 676 | case IFLA_BRPORT_LEARNING: |
| 677 | err = switchdev_port_br_setflag(dev, attr, |
| 678 | BR_LEARNING); |
| 679 | break; |
| 680 | case IFLA_BRPORT_LEARNING_SYNC: |
| 681 | err = switchdev_port_br_setflag(dev, attr, |
| 682 | BR_LEARNING_SYNC); |
| 683 | break; |
| 684 | default: |
| 685 | err = -EOPNOTSUPP; |
| 686 | break; |
| 687 | } |
| 688 | if (err) |
| 689 | return err; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static int switchdev_port_br_afspec(struct net_device *dev, |
| 696 | struct nlattr *afspec, |
| 697 | int (*f)(struct net_device *dev, |
| 698 | struct switchdev_obj *obj)) |
| 699 | { |
| 700 | struct nlattr *attr; |
| 701 | struct bridge_vlan_info *vinfo; |
| 702 | struct switchdev_obj obj = { |
| 703 | .id = SWITCHDEV_OBJ_PORT_VLAN, |
| 704 | }; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 705 | struct switchdev_obj_vlan *vlan = &obj.u.vlan; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 706 | int rem; |
| 707 | int err; |
| 708 | |
| 709 | nla_for_each_nested(attr, afspec, rem) { |
| 710 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) |
| 711 | continue; |
| 712 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) |
| 713 | return -EINVAL; |
| 714 | vinfo = nla_data(attr); |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 715 | vlan->flags = vinfo->flags; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 716 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 717 | if (vlan->vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 718 | return -EINVAL; |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 719 | vlan->vid_begin = vinfo->vid; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 720 | } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) { |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 721 | if (!vlan->vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 722 | return -EINVAL; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 723 | vlan->vid_end = vinfo->vid; |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 724 | if (vlan->vid_end <= vlan->vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 725 | return -EINVAL; |
| 726 | err = f(dev, &obj); |
| 727 | if (err) |
| 728 | return err; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 729 | memset(vlan, 0, sizeof(*vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 730 | } else { |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 731 | if (vlan->vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 732 | return -EINVAL; |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame] | 733 | vlan->vid_begin = vinfo->vid; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 734 | vlan->vid_end = vinfo->vid; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 735 | err = f(dev, &obj); |
| 736 | if (err) |
| 737 | return err; |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 738 | memset(vlan, 0, sizeof(*vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 745 | /** |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 746 | * switchdev_port_bridge_setlink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 747 | * |
| 748 | * @dev: port device |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 749 | * @nlh: netlink header |
| 750 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 751 | * |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 752 | * Called for SELF on rtnl_bridge_setlink to set bridge port |
| 753 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 754 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 755 | int switchdev_port_bridge_setlink(struct net_device *dev, |
| 756 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 757 | { |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 758 | struct nlattr *protinfo; |
| 759 | struct nlattr *afspec; |
| 760 | int err = 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 761 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 762 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 763 | IFLA_PROTINFO); |
| 764 | if (protinfo) { |
| 765 | err = switchdev_port_br_setlink_protinfo(dev, protinfo); |
| 766 | if (err) |
| 767 | return err; |
| 768 | } |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 769 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 770 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 771 | IFLA_AF_SPEC); |
| 772 | if (afspec) |
| 773 | err = switchdev_port_br_afspec(dev, afspec, |
| 774 | switchdev_port_obj_add); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 775 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 776 | return err; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 777 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 778 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 779 | |
| 780 | /** |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 781 | * switchdev_port_bridge_dellink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 782 | * |
| 783 | * @dev: port device |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 784 | * @nlh: netlink header |
| 785 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 786 | * |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 787 | * Called for SELF on rtnl_bridge_dellink to set bridge port |
| 788 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 789 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 790 | int switchdev_port_bridge_dellink(struct net_device *dev, |
| 791 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 792 | { |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 793 | struct nlattr *afspec; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 794 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 795 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 796 | IFLA_AF_SPEC); |
| 797 | if (afspec) |
| 798 | return switchdev_port_br_afspec(dev, afspec, |
| 799 | switchdev_port_obj_del); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 800 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 801 | return 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 802 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 803 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 804 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 805 | /** |
| 806 | * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port |
| 807 | * |
| 808 | * @ndmsg: netlink hdr |
| 809 | * @nlattr: netlink attributes |
| 810 | * @dev: port device |
| 811 | * @addr: MAC address to add |
| 812 | * @vid: VLAN to add |
| 813 | * |
| 814 | * Add FDB entry to switch device. |
| 815 | */ |
| 816 | int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 817 | struct net_device *dev, const unsigned char *addr, |
| 818 | u16 vid, u16 nlm_flags) |
| 819 | { |
| 820 | struct switchdev_obj obj = { |
| 821 | .id = SWITCHDEV_OBJ_PORT_FDB, |
| 822 | .u.fdb = { |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 823 | .addr = addr, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 824 | .vid = vid, |
| 825 | }, |
| 826 | }; |
| 827 | |
| 828 | return switchdev_port_obj_add(dev, &obj); |
| 829 | } |
| 830 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_add); |
| 831 | |
| 832 | /** |
| 833 | * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port |
| 834 | * |
| 835 | * @ndmsg: netlink hdr |
| 836 | * @nlattr: netlink attributes |
| 837 | * @dev: port device |
| 838 | * @addr: MAC address to delete |
| 839 | * @vid: VLAN to delete |
| 840 | * |
| 841 | * Delete FDB entry from switch device. |
| 842 | */ |
| 843 | int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
| 844 | struct net_device *dev, const unsigned char *addr, |
| 845 | u16 vid) |
| 846 | { |
| 847 | struct switchdev_obj obj = { |
| 848 | .id = SWITCHDEV_OBJ_PORT_FDB, |
| 849 | .u.fdb = { |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 850 | .addr = addr, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 851 | .vid = vid, |
| 852 | }, |
| 853 | }; |
| 854 | |
| 855 | return switchdev_port_obj_del(dev, &obj); |
| 856 | } |
| 857 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_del); |
| 858 | |
| 859 | struct switchdev_fdb_dump { |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 860 | struct switchdev_obj_fdb fdb; |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 861 | struct net_device *dev; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 862 | struct sk_buff *skb; |
| 863 | struct netlink_callback *cb; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 864 | int idx; |
| 865 | }; |
| 866 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 867 | static int switchdev_port_fdb_dump_cb(void *obj) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 868 | { |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 869 | struct switchdev_obj_fdb *fdb = obj; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 870 | struct switchdev_fdb_dump *dump = |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 871 | container_of(fdb, struct switchdev_fdb_dump, fdb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 872 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 873 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 874 | struct nlmsghdr *nlh; |
| 875 | struct ndmsg *ndm; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 876 | |
| 877 | if (dump->idx < dump->cb->args[0]) |
| 878 | goto skip; |
| 879 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 880 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 881 | sizeof(*ndm), NLM_F_MULTI); |
| 882 | if (!nlh) |
| 883 | return -EMSGSIZE; |
| 884 | |
| 885 | ndm = nlmsg_data(nlh); |
| 886 | ndm->ndm_family = AF_BRIDGE; |
| 887 | ndm->ndm_pad1 = 0; |
| 888 | ndm->ndm_pad2 = 0; |
| 889 | ndm->ndm_flags = NTF_SELF; |
| 890 | ndm->ndm_type = 0; |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 891 | ndm->ndm_ifindex = dump->dev->ifindex; |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 892 | ndm->ndm_state = fdb->ndm_state; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 893 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 894 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr)) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 895 | goto nla_put_failure; |
| 896 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 897 | if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid)) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 898 | goto nla_put_failure; |
| 899 | |
| 900 | nlmsg_end(dump->skb, nlh); |
| 901 | |
| 902 | skip: |
| 903 | dump->idx++; |
| 904 | return 0; |
| 905 | |
| 906 | nla_put_failure: |
| 907 | nlmsg_cancel(dump->skb, nlh); |
| 908 | return -EMSGSIZE; |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries |
| 913 | * |
| 914 | * @skb: netlink skb |
| 915 | * @cb: netlink callback |
| 916 | * @dev: port device |
| 917 | * @filter_dev: filter device |
| 918 | * @idx: |
| 919 | * |
| 920 | * Delete FDB entry from switch device. |
| 921 | */ |
| 922 | int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 923 | struct net_device *dev, |
| 924 | struct net_device *filter_dev, int idx) |
| 925 | { |
| 926 | struct switchdev_fdb_dump dump = { |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 927 | .dev = dev, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 928 | .skb = skb, |
| 929 | .cb = cb, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 930 | .idx = idx, |
| 931 | }; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 932 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame^] | 933 | switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_PORT_FDB, &dump.fdb, |
| 934 | switchdev_port_fdb_dump_cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 935 | return dump.idx; |
| 936 | } |
| 937 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump); |
| 938 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 939 | static struct net_device *switchdev_get_lowest_dev(struct net_device *dev) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 940 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 941 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 942 | struct net_device *lower_dev; |
| 943 | struct net_device *port_dev; |
| 944 | struct list_head *iter; |
| 945 | |
| 946 | /* Recusively search down until we find a sw port dev. |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 947 | * (A sw port dev supports switchdev_port_attr_get). |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 948 | */ |
| 949 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 950 | if (ops && ops->switchdev_port_attr_get) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 951 | return dev; |
| 952 | |
| 953 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 954 | port_dev = switchdev_get_lowest_dev(lower_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 955 | if (port_dev) |
| 956 | return port_dev; |
| 957 | } |
| 958 | |
| 959 | return NULL; |
| 960 | } |
| 961 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 962 | 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] | 963 | { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 964 | struct switchdev_attr attr = { |
| 965 | .id = SWITCHDEV_ATTR_PORT_PARENT_ID, |
| 966 | }; |
| 967 | struct switchdev_attr prev_attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 968 | struct net_device *dev = NULL; |
| 969 | int nhsel; |
| 970 | |
| 971 | /* For this route, all nexthop devs must be on the same switch. */ |
| 972 | |
| 973 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 974 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
| 975 | |
| 976 | if (!nh->nh_dev) |
| 977 | return NULL; |
| 978 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 979 | dev = switchdev_get_lowest_dev(nh->nh_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 980 | if (!dev) |
| 981 | return NULL; |
| 982 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 983 | if (switchdev_port_attr_get(dev, &attr)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 984 | return NULL; |
| 985 | |
Scott Feldman | d754f98 | 2015-07-18 18:24:49 -0700 | [diff] [blame] | 986 | if (nhsel > 0 && |
| 987 | !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 988 | return NULL; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 989 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 990 | prev_attr = attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return dev; |
| 994 | } |
| 995 | |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 996 | /** |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 997 | * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 998 | * |
| 999 | * @dst: route's IPv4 destination address |
| 1000 | * @dst_len: destination address length (prefix length) |
| 1001 | * @fi: route FIB info structure |
| 1002 | * @tos: route TOS |
| 1003 | * @type: route type |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 1004 | * @nlflags: netlink flags passed in (NLM_F_*) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1005 | * @tb_id: route table ID |
| 1006 | * |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 1007 | * Add/modify switch IPv4 route entry. |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1008 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1009 | int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi, |
| 1010 | u8 tos, u8 type, u32 nlflags, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1011 | { |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1012 | struct switchdev_obj fib_obj = { |
| 1013 | .id = SWITCHDEV_OBJ_IPV4_FIB, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 1014 | .u.ipv4_fib = { |
Scott Feldman | 7a7ee53 | 2015-05-12 23:03:52 -0700 | [diff] [blame] | 1015 | .dst = dst, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1016 | .dst_len = dst_len, |
| 1017 | .fi = fi, |
| 1018 | .tos = tos, |
| 1019 | .type = type, |
| 1020 | .nlflags = nlflags, |
| 1021 | .tb_id = tb_id, |
| 1022 | }, |
| 1023 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1024 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1025 | int err = 0; |
| 1026 | |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1027 | /* Don't offload route if using custom ip rules or if |
| 1028 | * IPv4 FIB offloading has been disabled completely. |
| 1029 | */ |
| 1030 | |
Scott Feldman | e1315db | 2015-03-06 01:14:36 -0800 | [diff] [blame] | 1031 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 1032 | if (fi->fib_net->ipv4.fib_has_custom_rules) |
| 1033 | return 0; |
| 1034 | #endif |
| 1035 | |
| 1036 | if (fi->fib_net->ipv4.fib_offload_disabled) |
Scott Feldman | 104616e | 2015-03-05 21:21:16 -0800 | [diff] [blame] | 1037 | return 0; |
| 1038 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1039 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1040 | if (!dev) |
| 1041 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1042 | |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1043 | err = switchdev_port_obj_add(dev, &fib_obj); |
| 1044 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 1045 | fi->fib_flags |= RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1046 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 1047 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1048 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1049 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add); |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1050 | |
| 1051 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1052 | * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1053 | * |
| 1054 | * @dst: route's IPv4 destination address |
| 1055 | * @dst_len: destination address length (prefix length) |
| 1056 | * @fi: route FIB info structure |
| 1057 | * @tos: route TOS |
| 1058 | * @type: route type |
| 1059 | * @tb_id: route table ID |
| 1060 | * |
| 1061 | * Delete IPv4 route entry from switch device. |
| 1062 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1063 | int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi, |
| 1064 | u8 tos, u8 type, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1065 | { |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1066 | struct switchdev_obj fib_obj = { |
| 1067 | .id = SWITCHDEV_OBJ_IPV4_FIB, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 1068 | .u.ipv4_fib = { |
Scott Feldman | 7a7ee53 | 2015-05-12 23:03:52 -0700 | [diff] [blame] | 1069 | .dst = dst, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1070 | .dst_len = dst_len, |
| 1071 | .fi = fi, |
| 1072 | .tos = tos, |
| 1073 | .type = type, |
| 1074 | .nlflags = 0, |
| 1075 | .tb_id = tb_id, |
| 1076 | }, |
| 1077 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1078 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1079 | int err = 0; |
| 1080 | |
Roopa Prabhu | eea3994 | 2015-05-13 21:17:41 -0700 | [diff] [blame] | 1081 | if (!(fi->fib_flags & RTNH_F_OFFLOAD)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1082 | return 0; |
| 1083 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1084 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1085 | if (!dev) |
| 1086 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1087 | |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1088 | err = switchdev_port_obj_del(dev, &fib_obj); |
| 1089 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 1090 | fi->fib_flags &= ~RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1091 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 1092 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1093 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1094 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del); |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1095 | |
| 1096 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1097 | * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1098 | * |
| 1099 | * @fi: route FIB info structure |
| 1100 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1101 | void switchdev_fib_ipv4_abort(struct fib_info *fi) |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1102 | { |
| 1103 | /* There was a problem installing this route to the offload |
| 1104 | * device. For now, until we come up with more refined |
| 1105 | * policy handling, abruptly end IPv4 fib offloading for |
| 1106 | * for entire net by flushing offload device(s) of all |
| 1107 | * IPv4 routes, and mark IPv4 fib offloading broken from |
| 1108 | * this point forward. |
| 1109 | */ |
| 1110 | |
| 1111 | fib_flush_external(fi->fib_net); |
| 1112 | fi->fib_net->ipv4.fib_offload_disabled = true; |
| 1113 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1114 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort); |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1115 | |
| 1116 | static bool switchdev_port_same_parent_id(struct net_device *a, |
| 1117 | struct net_device *b) |
| 1118 | { |
| 1119 | struct switchdev_attr a_attr = { |
| 1120 | .id = SWITCHDEV_ATTR_PORT_PARENT_ID, |
| 1121 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1122 | }; |
| 1123 | struct switchdev_attr b_attr = { |
| 1124 | .id = SWITCHDEV_ATTR_PORT_PARENT_ID, |
| 1125 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1126 | }; |
| 1127 | |
| 1128 | if (switchdev_port_attr_get(a, &a_attr) || |
| 1129 | switchdev_port_attr_get(b, &b_attr)) |
| 1130 | return false; |
| 1131 | |
| 1132 | return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid); |
| 1133 | } |
| 1134 | |
| 1135 | static u32 switchdev_port_fwd_mark_get(struct net_device *dev, |
| 1136 | struct net_device *group_dev) |
| 1137 | { |
| 1138 | struct net_device *lower_dev; |
| 1139 | struct list_head *iter; |
| 1140 | |
| 1141 | netdev_for_each_lower_dev(group_dev, lower_dev, iter) { |
| 1142 | if (lower_dev == dev) |
| 1143 | continue; |
| 1144 | if (switchdev_port_same_parent_id(dev, lower_dev)) |
| 1145 | return lower_dev->offload_fwd_mark; |
| 1146 | return switchdev_port_fwd_mark_get(dev, lower_dev); |
| 1147 | } |
| 1148 | |
| 1149 | return dev->ifindex; |
| 1150 | } |
| 1151 | |
| 1152 | static void switchdev_port_fwd_mark_reset(struct net_device *group_dev, |
| 1153 | u32 old_mark, u32 *reset_mark) |
| 1154 | { |
| 1155 | struct net_device *lower_dev; |
| 1156 | struct list_head *iter; |
| 1157 | |
| 1158 | netdev_for_each_lower_dev(group_dev, lower_dev, iter) { |
| 1159 | if (lower_dev->offload_fwd_mark == old_mark) { |
| 1160 | if (!*reset_mark) |
| 1161 | *reset_mark = lower_dev->ifindex; |
| 1162 | lower_dev->offload_fwd_mark = *reset_mark; |
| 1163 | } |
| 1164 | switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * switchdev_port_fwd_mark_set - Set port offload forwarding mark |
| 1170 | * |
| 1171 | * @dev: port device |
| 1172 | * @group_dev: containing device |
| 1173 | * @joining: true if dev is joining group; false if leaving group |
| 1174 | * |
| 1175 | * An ungrouped port's offload mark is just its ifindex. A grouped |
| 1176 | * port's (member of a bridge, for example) offload mark is the ifindex |
| 1177 | * of one of the ports in the group with the same parent (switch) ID. |
| 1178 | * Ports on the same device in the same group will have the same mark. |
| 1179 | * |
| 1180 | * Example: |
| 1181 | * |
| 1182 | * br0 ifindex=9 |
| 1183 | * sw1p1 ifindex=2 mark=2 |
| 1184 | * sw1p2 ifindex=3 mark=2 |
| 1185 | * sw2p1 ifindex=4 mark=5 |
| 1186 | * sw2p2 ifindex=5 mark=5 |
| 1187 | * |
| 1188 | * If sw2p2 leaves the bridge, we'll have: |
| 1189 | * |
| 1190 | * br0 ifindex=9 |
| 1191 | * sw1p1 ifindex=2 mark=2 |
| 1192 | * sw1p2 ifindex=3 mark=2 |
| 1193 | * sw2p1 ifindex=4 mark=4 |
| 1194 | * sw2p2 ifindex=5 mark=5 |
| 1195 | */ |
| 1196 | void switchdev_port_fwd_mark_set(struct net_device *dev, |
| 1197 | struct net_device *group_dev, |
| 1198 | bool joining) |
| 1199 | { |
| 1200 | u32 mark = dev->ifindex; |
| 1201 | u32 reset_mark = 0; |
| 1202 | |
| 1203 | if (group_dev && joining) { |
| 1204 | mark = switchdev_port_fwd_mark_get(dev, group_dev); |
| 1205 | } else if (group_dev && !joining) { |
| 1206 | if (dev->offload_fwd_mark == mark) |
| 1207 | /* Ohoh, this port was the mark reference port, |
| 1208 | * but it's leaving the group, so reset the |
| 1209 | * mark for the remaining ports in the group. |
| 1210 | */ |
| 1211 | switchdev_port_fwd_mark_reset(group_dev, mark, |
| 1212 | &reset_mark); |
| 1213 | } |
| 1214 | |
| 1215 | dev->offload_fwd_mark = mark; |
| 1216 | } |
| 1217 | EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set); |