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> |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 18 | #include <linux/etherdevice.h> |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 19 | #include <linux/if_bridge.h> |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 20 | #include <linux/list.h> |
Jiri Pirko | 793f401 | 2015-10-14 19:40:48 +0200 | [diff] [blame] | 21 | #include <linux/workqueue.h> |
Nikolay Aleksandrov | 87aaf2c | 2015-10-12 14:31:01 +0200 | [diff] [blame] | 22 | #include <linux/if_vlan.h> |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 23 | #include <linux/rtnetlink.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 24 | #include <net/switchdev.h> |
| 25 | |
| 26 | /** |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 27 | * switchdev_trans_item_enqueue - Enqueue data item to transaction queue |
| 28 | * |
| 29 | * @trans: transaction |
| 30 | * @data: pointer to data being queued |
| 31 | * @destructor: data destructor |
| 32 | * @tritem: transaction item being queued |
| 33 | * |
| 34 | * Enqeueue data item to transaction queue. tritem is typically placed in |
| 35 | * cointainter pointed at by data pointer. Destructor is called on |
| 36 | * transaction abort and after successful commit phase in case |
| 37 | * the caller did not dequeue the item before. |
| 38 | */ |
| 39 | void switchdev_trans_item_enqueue(struct switchdev_trans *trans, |
| 40 | void *data, void (*destructor)(void const *), |
| 41 | struct switchdev_trans_item *tritem) |
| 42 | { |
| 43 | tritem->data = data; |
| 44 | tritem->destructor = destructor; |
| 45 | list_add_tail(&tritem->list, &trans->item_list); |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue); |
| 48 | |
| 49 | static struct switchdev_trans_item * |
| 50 | __switchdev_trans_item_dequeue(struct switchdev_trans *trans) |
| 51 | { |
| 52 | struct switchdev_trans_item *tritem; |
| 53 | |
| 54 | if (list_empty(&trans->item_list)) |
| 55 | return NULL; |
| 56 | tritem = list_first_entry(&trans->item_list, |
| 57 | struct switchdev_trans_item, list); |
| 58 | list_del(&tritem->list); |
| 59 | return tritem; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * switchdev_trans_item_dequeue - Dequeue data item from transaction queue |
| 64 | * |
| 65 | * @trans: transaction |
| 66 | */ |
| 67 | void *switchdev_trans_item_dequeue(struct switchdev_trans *trans) |
| 68 | { |
| 69 | struct switchdev_trans_item *tritem; |
| 70 | |
| 71 | tritem = __switchdev_trans_item_dequeue(trans); |
| 72 | BUG_ON(!tritem); |
| 73 | return tritem->data; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue); |
| 76 | |
| 77 | static void switchdev_trans_init(struct switchdev_trans *trans) |
| 78 | { |
| 79 | INIT_LIST_HEAD(&trans->item_list); |
| 80 | } |
| 81 | |
| 82 | static void switchdev_trans_items_destroy(struct switchdev_trans *trans) |
| 83 | { |
| 84 | struct switchdev_trans_item *tritem; |
| 85 | |
| 86 | while ((tritem = __switchdev_trans_item_dequeue(trans))) |
| 87 | tritem->destructor(tritem->data); |
| 88 | } |
| 89 | |
| 90 | static void switchdev_trans_items_warn_destroy(struct net_device *dev, |
| 91 | struct switchdev_trans *trans) |
| 92 | { |
| 93 | WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n", |
| 94 | dev->name); |
| 95 | switchdev_trans_items_destroy(trans); |
| 96 | } |
| 97 | |
Jiri Pirko | 793f401 | 2015-10-14 19:40:48 +0200 | [diff] [blame] | 98 | static LIST_HEAD(deferred); |
| 99 | static DEFINE_SPINLOCK(deferred_lock); |
| 100 | |
| 101 | typedef void switchdev_deferred_func_t(struct net_device *dev, |
| 102 | const void *data); |
| 103 | |
| 104 | struct switchdev_deferred_item { |
| 105 | struct list_head list; |
| 106 | struct net_device *dev; |
| 107 | switchdev_deferred_func_t *func; |
| 108 | unsigned long data[0]; |
| 109 | }; |
| 110 | |
| 111 | static struct switchdev_deferred_item *switchdev_deferred_dequeue(void) |
| 112 | { |
| 113 | struct switchdev_deferred_item *dfitem; |
| 114 | |
| 115 | spin_lock_bh(&deferred_lock); |
| 116 | if (list_empty(&deferred)) { |
| 117 | dfitem = NULL; |
| 118 | goto unlock; |
| 119 | } |
| 120 | dfitem = list_first_entry(&deferred, |
| 121 | struct switchdev_deferred_item, list); |
| 122 | list_del(&dfitem->list); |
| 123 | unlock: |
| 124 | spin_unlock_bh(&deferred_lock); |
| 125 | return dfitem; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * switchdev_deferred_process - Process ops in deferred queue |
| 130 | * |
| 131 | * Called to flush the ops currently queued in deferred ops queue. |
| 132 | * rtnl_lock must be held. |
| 133 | */ |
| 134 | void switchdev_deferred_process(void) |
| 135 | { |
| 136 | struct switchdev_deferred_item *dfitem; |
| 137 | |
| 138 | ASSERT_RTNL(); |
| 139 | |
| 140 | while ((dfitem = switchdev_deferred_dequeue())) { |
| 141 | dfitem->func(dfitem->dev, dfitem->data); |
| 142 | dev_put(dfitem->dev); |
| 143 | kfree(dfitem); |
| 144 | } |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(switchdev_deferred_process); |
| 147 | |
| 148 | static void switchdev_deferred_process_work(struct work_struct *work) |
| 149 | { |
| 150 | rtnl_lock(); |
| 151 | switchdev_deferred_process(); |
| 152 | rtnl_unlock(); |
| 153 | } |
| 154 | |
| 155 | static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work); |
| 156 | |
| 157 | static int switchdev_deferred_enqueue(struct net_device *dev, |
| 158 | const void *data, size_t data_len, |
| 159 | switchdev_deferred_func_t *func) |
| 160 | { |
| 161 | struct switchdev_deferred_item *dfitem; |
| 162 | |
| 163 | dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC); |
| 164 | if (!dfitem) |
| 165 | return -ENOMEM; |
| 166 | dfitem->dev = dev; |
| 167 | dfitem->func = func; |
| 168 | memcpy(dfitem->data, data, data_len); |
| 169 | dev_hold(dev); |
| 170 | spin_lock_bh(&deferred_lock); |
| 171 | list_add_tail(&dfitem->list, &deferred); |
| 172 | spin_unlock_bh(&deferred_lock); |
| 173 | schedule_work(&deferred_process_work); |
| 174 | return 0; |
| 175 | } |
| 176 | |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 177 | static int __switchdev_port_attr_set(struct net_device *dev, |
Jiri Pirko | f7fadf3 | 2015-10-14 19:40:49 +0200 | [diff] [blame] | 178 | const struct switchdev_attr *attr, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 179 | struct switchdev_trans *trans) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 180 | { |
| 181 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 182 | struct net_device *lower_dev; |
| 183 | struct list_head *iter; |
| 184 | int err = -EOPNOTSUPP; |
| 185 | |
Jiri Pirko | 0c63d80 | 2015-11-03 17:40:53 +0100 | [diff] [blame] | 186 | if (ops && ops->switchdev_port_attr_set) { |
| 187 | err = ops->switchdev_port_attr_set(dev, attr, trans); |
| 188 | goto done; |
| 189 | } |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 190 | |
| 191 | if (attr->flags & SWITCHDEV_F_NO_RECURSE) |
Scott Feldman | 464314e | 2015-10-08 19:23:18 -0700 | [diff] [blame] | 192 | goto done; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 193 | |
| 194 | /* Switch device port(s) may be stacked under |
| 195 | * bond/team/vlan dev, so recurse down to set attr on |
| 196 | * each port. |
| 197 | */ |
| 198 | |
| 199 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 200 | err = __switchdev_port_attr_set(lower_dev, attr, trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 201 | if (err) |
| 202 | break; |
| 203 | } |
| 204 | |
Scott Feldman | 464314e | 2015-10-08 19:23:18 -0700 | [diff] [blame] | 205 | done: |
| 206 | if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP) |
| 207 | err = 0; |
| 208 | |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 209 | return err; |
| 210 | } |
| 211 | |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 212 | static int switchdev_port_attr_set_now(struct net_device *dev, |
| 213 | const struct switchdev_attr *attr) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 214 | { |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 215 | struct switchdev_trans trans; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 216 | int err; |
| 217 | |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 218 | switchdev_trans_init(&trans); |
| 219 | |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 220 | /* Phase I: prepare for attr set. Driver/device should fail |
| 221 | * here if there are going to be issues in the commit phase, |
| 222 | * such as lack of resources or support. The driver/device |
| 223 | * should reserve resources needed for the commit phase here, |
| 224 | * but should not commit the attr. |
| 225 | */ |
| 226 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 227 | trans.ph_prepare = true; |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 228 | err = __switchdev_port_attr_set(dev, attr, &trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 229 | if (err) { |
| 230 | /* Prepare phase failed: abort the transaction. Any |
| 231 | * resources reserved in the prepare phase are |
| 232 | * released. |
| 233 | */ |
| 234 | |
Jiri Pirko | 9f6467c | 2015-09-24 10:02:47 +0200 | [diff] [blame] | 235 | if (err != -EOPNOTSUPP) |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 236 | switchdev_trans_items_destroy(&trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 237 | |
| 238 | return err; |
| 239 | } |
| 240 | |
| 241 | /* Phase II: commit attr set. This cannot fail as a fault |
| 242 | * of driver/device. If it does, it's a bug in the driver/device |
| 243 | * because the driver said everythings was OK in phase I. |
| 244 | */ |
| 245 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 246 | trans.ph_prepare = false; |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 247 | err = __switchdev_port_attr_set(dev, attr, &trans); |
Scott Feldman | e9fdaec | 2015-06-11 11:20:42 -0700 | [diff] [blame] | 248 | WARN(err, "%s: Commit of attribute (id=%d) failed.\n", |
| 249 | dev->name, attr->id); |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 250 | switchdev_trans_items_warn_destroy(dev, &trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 251 | |
| 252 | return err; |
| 253 | } |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 254 | |
| 255 | static void switchdev_port_attr_set_deferred(struct net_device *dev, |
| 256 | const void *data) |
| 257 | { |
| 258 | const struct switchdev_attr *attr = data; |
| 259 | int err; |
| 260 | |
| 261 | err = switchdev_port_attr_set_now(dev, attr); |
| 262 | if (err && err != -EOPNOTSUPP) |
| 263 | netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n", |
| 264 | err, attr->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 265 | if (attr->complete) |
| 266 | attr->complete(dev, err, attr->complete_priv); |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static int switchdev_port_attr_set_defer(struct net_device *dev, |
| 270 | const struct switchdev_attr *attr) |
| 271 | { |
| 272 | return switchdev_deferred_enqueue(dev, attr, sizeof(*attr), |
| 273 | switchdev_port_attr_set_deferred); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * switchdev_port_attr_set - Set port attribute |
| 278 | * |
| 279 | * @dev: port device |
| 280 | * @attr: attribute to set |
| 281 | * |
| 282 | * Use a 2-phase prepare-commit transaction model to ensure |
| 283 | * system is not left in a partially updated state due to |
| 284 | * failure from driver/device. |
| 285 | * |
| 286 | * rtnl_lock must be held and must not be in atomic section, |
| 287 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 288 | */ |
| 289 | int switchdev_port_attr_set(struct net_device *dev, |
| 290 | const struct switchdev_attr *attr) |
| 291 | { |
| 292 | if (attr->flags & SWITCHDEV_F_DEFER) |
| 293 | return switchdev_port_attr_set_defer(dev, attr); |
| 294 | ASSERT_RTNL(); |
| 295 | return switchdev_port_attr_set_now(dev, attr); |
| 296 | } |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 297 | EXPORT_SYMBOL_GPL(switchdev_port_attr_set); |
| 298 | |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 299 | static size_t switchdev_obj_size(const struct switchdev_obj *obj) |
| 300 | { |
| 301 | switch (obj->id) { |
| 302 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 303 | return sizeof(struct switchdev_obj_port_vlan); |
Elad Raz | 4d41e125 | 2016-01-10 21:06:22 +0100 | [diff] [blame] | 304 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 305 | return sizeof(struct switchdev_obj_port_mdb); |
Andrew Lunn | 47d5b6d | 2017-11-09 23:10:59 +0100 | [diff] [blame] | 306 | case SWITCHDEV_OBJ_ID_HOST_MDB: |
| 307 | return sizeof(struct switchdev_obj_port_mdb); |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 308 | default: |
| 309 | BUG(); |
| 310 | } |
| 311 | return 0; |
| 312 | } |
| 313 | |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 314 | static int switchdev_port_obj_notify(enum switchdev_notifier_type nt, |
| 315 | struct net_device *dev, |
| 316 | const struct switchdev_obj *obj, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 317 | struct switchdev_trans *trans, |
| 318 | struct netlink_ext_ack *extack) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 319 | { |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 320 | int rc; |
| 321 | int err; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 322 | |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 323 | struct switchdev_notifier_port_obj_info obj_info = { |
| 324 | .obj = obj, |
| 325 | .trans = trans, |
| 326 | .handled = false, |
| 327 | }; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 328 | |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 329 | rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack); |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 330 | err = notifier_to_errno(rc); |
| 331 | if (err) { |
| 332 | WARN_ON(!obj_info.handled); |
| 333 | return err; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 334 | } |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 335 | if (!obj_info.handled) |
| 336 | return -EOPNOTSUPP; |
| 337 | return 0; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 340 | static int switchdev_port_obj_add_now(struct net_device *dev, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 341 | const struct switchdev_obj *obj, |
| 342 | struct netlink_ext_ack *extack) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 343 | { |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 344 | struct switchdev_trans trans; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 345 | int err; |
| 346 | |
| 347 | ASSERT_RTNL(); |
| 348 | |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 349 | switchdev_trans_init(&trans); |
| 350 | |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 351 | /* Phase I: prepare for obj add. Driver/device should fail |
| 352 | * here if there are going to be issues in the commit phase, |
| 353 | * such as lack of resources or support. The driver/device |
| 354 | * should reserve resources needed for the commit phase here, |
| 355 | * but should not commit the obj. |
| 356 | */ |
| 357 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 358 | trans.ph_prepare = true; |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 359 | err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 360 | dev, obj, &trans, extack); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 361 | if (err) { |
| 362 | /* Prepare phase failed: abort the transaction. Any |
| 363 | * resources reserved in the prepare phase are |
| 364 | * released. |
| 365 | */ |
| 366 | |
Jiri Pirko | 9f6467c | 2015-09-24 10:02:47 +0200 | [diff] [blame] | 367 | if (err != -EOPNOTSUPP) |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 368 | switchdev_trans_items_destroy(&trans); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 369 | |
| 370 | return err; |
| 371 | } |
| 372 | |
| 373 | /* Phase II: commit obj add. This cannot fail as a fault |
| 374 | * of driver/device. If it does, it's a bug in the driver/device |
| 375 | * because the driver said everythings was OK in phase I. |
| 376 | */ |
| 377 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 378 | trans.ph_prepare = false; |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 379 | err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 380 | dev, obj, &trans, extack); |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 381 | 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] | 382 | switchdev_trans_items_warn_destroy(dev, &trans); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 383 | |
| 384 | return err; |
| 385 | } |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 386 | |
| 387 | static void switchdev_port_obj_add_deferred(struct net_device *dev, |
| 388 | const void *data) |
| 389 | { |
| 390 | const struct switchdev_obj *obj = data; |
| 391 | int err; |
| 392 | |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 393 | err = switchdev_port_obj_add_now(dev, obj, NULL); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 394 | if (err && err != -EOPNOTSUPP) |
| 395 | netdev_err(dev, "failed (err=%d) to add object (id=%d)\n", |
| 396 | err, obj->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 397 | if (obj->complete) |
| 398 | obj->complete(dev, err, obj->complete_priv); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static int switchdev_port_obj_add_defer(struct net_device *dev, |
| 402 | const struct switchdev_obj *obj) |
| 403 | { |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 404 | return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj), |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 405 | switchdev_port_obj_add_deferred); |
| 406 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 407 | |
| 408 | /** |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 409 | * switchdev_port_obj_add - Add port object |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 410 | * |
| 411 | * @dev: port device |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 412 | * @id: object ID |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 413 | * @obj: object to add |
| 414 | * |
| 415 | * Use a 2-phase prepare-commit transaction model to ensure |
| 416 | * system is not left in a partially updated state due to |
| 417 | * failure from driver/device. |
| 418 | * |
| 419 | * rtnl_lock must be held and must not be in atomic section, |
| 420 | * in case SWITCHDEV_F_DEFER flag is not set. |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 421 | */ |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 422 | int switchdev_port_obj_add(struct net_device *dev, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 423 | const struct switchdev_obj *obj, |
| 424 | struct netlink_ext_ack *extack) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 425 | { |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 426 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 427 | return switchdev_port_obj_add_defer(dev, obj); |
| 428 | ASSERT_RTNL(); |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 429 | return switchdev_port_obj_add_now(dev, obj, extack); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 430 | } |
| 431 | EXPORT_SYMBOL_GPL(switchdev_port_obj_add); |
| 432 | |
| 433 | static int switchdev_port_obj_del_now(struct net_device *dev, |
| 434 | const struct switchdev_obj *obj) |
| 435 | { |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 436 | return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 437 | dev, obj, NULL, NULL); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 438 | } |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 439 | |
| 440 | static void switchdev_port_obj_del_deferred(struct net_device *dev, |
| 441 | const void *data) |
| 442 | { |
| 443 | const struct switchdev_obj *obj = data; |
| 444 | int err; |
| 445 | |
| 446 | err = switchdev_port_obj_del_now(dev, obj); |
| 447 | if (err && err != -EOPNOTSUPP) |
| 448 | netdev_err(dev, "failed (err=%d) to del object (id=%d)\n", |
| 449 | err, obj->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 450 | if (obj->complete) |
| 451 | obj->complete(dev, err, obj->complete_priv); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static int switchdev_port_obj_del_defer(struct net_device *dev, |
| 455 | const struct switchdev_obj *obj) |
| 456 | { |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 457 | return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj), |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 458 | switchdev_port_obj_del_deferred); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * switchdev_port_obj_del - Delete port object |
| 463 | * |
| 464 | * @dev: port device |
| 465 | * @id: object ID |
| 466 | * @obj: object to delete |
| 467 | * |
| 468 | * rtnl_lock must be held and must not be in atomic section, |
| 469 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 470 | */ |
| 471 | int switchdev_port_obj_del(struct net_device *dev, |
| 472 | const struct switchdev_obj *obj) |
| 473 | { |
| 474 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 475 | return switchdev_port_obj_del_defer(dev, obj); |
| 476 | ASSERT_RTNL(); |
| 477 | return switchdev_port_obj_del_now(dev, obj); |
| 478 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 479 | EXPORT_SYMBOL_GPL(switchdev_port_obj_del); |
| 480 | |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 481 | static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain); |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 482 | static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 483 | |
| 484 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 485 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 486 | * @nb: notifier_block |
| 487 | * |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 488 | * Register switch device notifier. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 489 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 490 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 491 | { |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 492 | return atomic_notifier_chain_register(&switchdev_notif_chain, nb); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 493 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 495 | |
| 496 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 497 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 498 | * @nb: notifier_block |
| 499 | * |
| 500 | * Unregister switch device notifier. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 501 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 502 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 503 | { |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 504 | return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 505 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 506 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 507 | |
| 508 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 509 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 510 | * @val: value passed unmodified to notifier function |
| 511 | * @dev: port device |
| 512 | * @info: notifier information data |
| 513 | * |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 514 | * Call all network notifier blocks. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 515 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 516 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
Petr Machata | 6685987 | 2019-01-16 23:06:56 +0000 | [diff] [blame] | 517 | struct switchdev_notifier_info *info, |
| 518 | struct netlink_ext_ack *extack) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 519 | { |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 520 | info->dev = dev; |
Petr Machata | 6685987 | 2019-01-16 23:06:56 +0000 | [diff] [blame] | 521 | info->extack = extack; |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 522 | return atomic_notifier_call_chain(&switchdev_notif_chain, val, info); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 523 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 524 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 525 | |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 526 | int register_switchdev_blocking_notifier(struct notifier_block *nb) |
| 527 | { |
| 528 | struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain; |
| 529 | |
| 530 | return blocking_notifier_chain_register(chain, nb); |
| 531 | } |
| 532 | EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier); |
| 533 | |
| 534 | int unregister_switchdev_blocking_notifier(struct notifier_block *nb) |
| 535 | { |
| 536 | struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain; |
| 537 | |
| 538 | return blocking_notifier_chain_unregister(chain, nb); |
| 539 | } |
| 540 | EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier); |
| 541 | |
| 542 | int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 543 | struct switchdev_notifier_info *info, |
| 544 | struct netlink_ext_ack *extack) |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 545 | { |
| 546 | info->dev = dev; |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 547 | info->extack = extack; |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 548 | return blocking_notifier_call_chain(&switchdev_blocking_notif_chain, |
| 549 | val, info); |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers); |
| 552 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 553 | static int __switchdev_handle_port_obj_add(struct net_device *dev, |
| 554 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 555 | bool (*check_cb)(const struct net_device *dev), |
| 556 | int (*add_cb)(struct net_device *dev, |
| 557 | const struct switchdev_obj *obj, |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 558 | struct switchdev_trans *trans, |
| 559 | struct netlink_ext_ack *extack)) |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 560 | { |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 561 | struct netlink_ext_ack *extack; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 562 | struct net_device *lower_dev; |
| 563 | struct list_head *iter; |
| 564 | int err = -EOPNOTSUPP; |
| 565 | |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 566 | extack = switchdev_notifier_info_to_extack(&port_obj_info->info); |
| 567 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 568 | if (check_cb(dev)) { |
| 569 | /* This flag is only checked if the return value is success. */ |
| 570 | port_obj_info->handled = true; |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 571 | return add_cb(dev, port_obj_info->obj, port_obj_info->trans, |
| 572 | extack); |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 576 | * unsupported devices, another driver might be able to handle them. But |
| 577 | * propagate to the callers any hard errors. |
| 578 | * |
| 579 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 580 | * necessary to go through this helper. |
| 581 | */ |
| 582 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 583 | err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info, |
| 584 | check_cb, add_cb); |
| 585 | if (err && err != -EOPNOTSUPP) |
| 586 | return err; |
| 587 | } |
| 588 | |
| 589 | return err; |
| 590 | } |
| 591 | |
| 592 | int switchdev_handle_port_obj_add(struct net_device *dev, |
| 593 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 594 | bool (*check_cb)(const struct net_device *dev), |
| 595 | int (*add_cb)(struct net_device *dev, |
| 596 | const struct switchdev_obj *obj, |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 597 | struct switchdev_trans *trans, |
| 598 | struct netlink_ext_ack *extack)) |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 599 | { |
| 600 | int err; |
| 601 | |
| 602 | err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb, |
| 603 | add_cb); |
| 604 | if (err == -EOPNOTSUPP) |
| 605 | err = 0; |
| 606 | return err; |
| 607 | } |
| 608 | EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add); |
| 609 | |
| 610 | static int __switchdev_handle_port_obj_del(struct net_device *dev, |
| 611 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 612 | bool (*check_cb)(const struct net_device *dev), |
| 613 | int (*del_cb)(struct net_device *dev, |
| 614 | const struct switchdev_obj *obj)) |
| 615 | { |
| 616 | struct net_device *lower_dev; |
| 617 | struct list_head *iter; |
| 618 | int err = -EOPNOTSUPP; |
| 619 | |
| 620 | if (check_cb(dev)) { |
| 621 | /* This flag is only checked if the return value is success. */ |
| 622 | port_obj_info->handled = true; |
| 623 | return del_cb(dev, port_obj_info->obj); |
| 624 | } |
| 625 | |
| 626 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 627 | * unsupported devices, another driver might be able to handle them. But |
| 628 | * propagate to the callers any hard errors. |
| 629 | * |
| 630 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 631 | * necessary to go through this helper. |
| 632 | */ |
| 633 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 634 | err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info, |
| 635 | check_cb, del_cb); |
| 636 | if (err && err != -EOPNOTSUPP) |
| 637 | return err; |
| 638 | } |
| 639 | |
| 640 | return err; |
| 641 | } |
| 642 | |
| 643 | int switchdev_handle_port_obj_del(struct net_device *dev, |
| 644 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 645 | bool (*check_cb)(const struct net_device *dev), |
| 646 | int (*del_cb)(struct net_device *dev, |
| 647 | const struct switchdev_obj *obj)) |
| 648 | { |
| 649 | int err; |
| 650 | |
| 651 | err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb, |
| 652 | del_cb); |
| 653 | if (err == -EOPNOTSUPP) |
| 654 | err = 0; |
| 655 | return err; |
| 656 | } |
| 657 | EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del); |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame^] | 658 | |
| 659 | static int __switchdev_handle_port_attr_set(struct net_device *dev, |
| 660 | struct switchdev_notifier_port_attr_info *port_attr_info, |
| 661 | bool (*check_cb)(const struct net_device *dev), |
| 662 | int (*set_cb)(struct net_device *dev, |
| 663 | const struct switchdev_attr *attr, |
| 664 | struct switchdev_trans *trans)) |
| 665 | { |
| 666 | struct net_device *lower_dev; |
| 667 | struct list_head *iter; |
| 668 | int err = -EOPNOTSUPP; |
| 669 | |
| 670 | if (check_cb(dev)) { |
| 671 | port_attr_info->handled = true; |
| 672 | return set_cb(dev, port_attr_info->attr, |
| 673 | port_attr_info->trans); |
| 674 | } |
| 675 | |
| 676 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 677 | * unsupported devices, another driver might be able to handle them. But |
| 678 | * propagate to the callers any hard errors. |
| 679 | * |
| 680 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 681 | * necessary to go through this helper. |
| 682 | */ |
| 683 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 684 | err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info, |
| 685 | check_cb, set_cb); |
| 686 | if (err && err != -EOPNOTSUPP) |
| 687 | return err; |
| 688 | } |
| 689 | |
| 690 | return err; |
| 691 | } |
| 692 | |
| 693 | int switchdev_handle_port_attr_set(struct net_device *dev, |
| 694 | struct switchdev_notifier_port_attr_info *port_attr_info, |
| 695 | bool (*check_cb)(const struct net_device *dev), |
| 696 | int (*set_cb)(struct net_device *dev, |
| 697 | const struct switchdev_attr *attr, |
| 698 | struct switchdev_trans *trans)) |
| 699 | { |
| 700 | int err; |
| 701 | |
| 702 | err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb, |
| 703 | set_cb); |
| 704 | if (err == -EOPNOTSUPP) |
| 705 | err = 0; |
| 706 | return err; |
| 707 | } |
| 708 | EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set); |