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