Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 2 | /* |
| 3 | * net/switchdev/switchdev.c - Switch device API |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 4 | * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us> |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 5 | * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/init.h> |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 11 | #include <linux/mutex.h> |
| 12 | #include <linux/notifier.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 13 | #include <linux/netdevice.h> |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 14 | #include <linux/etherdevice.h> |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 15 | #include <linux/if_bridge.h> |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 16 | #include <linux/list.h> |
Jiri Pirko | 793f401 | 2015-10-14 19:40:48 +0200 | [diff] [blame] | 17 | #include <linux/workqueue.h> |
Nikolay Aleksandrov | 87aaf2c | 2015-10-12 14:31:01 +0200 | [diff] [blame] | 18 | #include <linux/if_vlan.h> |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 19 | #include <linux/rtnetlink.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 20 | #include <net/switchdev.h> |
| 21 | |
Jiri Pirko | 793f401 | 2015-10-14 19:40:48 +0200 | [diff] [blame] | 22 | static LIST_HEAD(deferred); |
| 23 | static DEFINE_SPINLOCK(deferred_lock); |
| 24 | |
| 25 | typedef void switchdev_deferred_func_t(struct net_device *dev, |
| 26 | const void *data); |
| 27 | |
| 28 | struct switchdev_deferred_item { |
| 29 | struct list_head list; |
| 30 | struct net_device *dev; |
| 31 | switchdev_deferred_func_t *func; |
Gustavo A. R. Silva | fbfc850 | 2020-02-17 14:02:36 -0600 | [diff] [blame] | 32 | unsigned long data[]; |
Jiri Pirko | 793f401 | 2015-10-14 19:40:48 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct switchdev_deferred_item *switchdev_deferred_dequeue(void) |
| 36 | { |
| 37 | struct switchdev_deferred_item *dfitem; |
| 38 | |
| 39 | spin_lock_bh(&deferred_lock); |
| 40 | if (list_empty(&deferred)) { |
| 41 | dfitem = NULL; |
| 42 | goto unlock; |
| 43 | } |
| 44 | dfitem = list_first_entry(&deferred, |
| 45 | struct switchdev_deferred_item, list); |
| 46 | list_del(&dfitem->list); |
| 47 | unlock: |
| 48 | spin_unlock_bh(&deferred_lock); |
| 49 | return dfitem; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * switchdev_deferred_process - Process ops in deferred queue |
| 54 | * |
| 55 | * Called to flush the ops currently queued in deferred ops queue. |
| 56 | * rtnl_lock must be held. |
| 57 | */ |
| 58 | void switchdev_deferred_process(void) |
| 59 | { |
| 60 | struct switchdev_deferred_item *dfitem; |
| 61 | |
| 62 | ASSERT_RTNL(); |
| 63 | |
| 64 | while ((dfitem = switchdev_deferred_dequeue())) { |
| 65 | dfitem->func(dfitem->dev, dfitem->data); |
| 66 | dev_put(dfitem->dev); |
| 67 | kfree(dfitem); |
| 68 | } |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(switchdev_deferred_process); |
| 71 | |
| 72 | static void switchdev_deferred_process_work(struct work_struct *work) |
| 73 | { |
| 74 | rtnl_lock(); |
| 75 | switchdev_deferred_process(); |
| 76 | rtnl_unlock(); |
| 77 | } |
| 78 | |
| 79 | static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work); |
| 80 | |
| 81 | static int switchdev_deferred_enqueue(struct net_device *dev, |
| 82 | const void *data, size_t data_len, |
| 83 | switchdev_deferred_func_t *func) |
| 84 | { |
| 85 | struct switchdev_deferred_item *dfitem; |
| 86 | |
| 87 | dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC); |
| 88 | if (!dfitem) |
| 89 | return -ENOMEM; |
| 90 | dfitem->dev = dev; |
| 91 | dfitem->func = func; |
| 92 | memcpy(dfitem->data, data, data_len); |
| 93 | dev_hold(dev); |
| 94 | spin_lock_bh(&deferred_lock); |
| 95 | list_add_tail(&dfitem->list, &deferred); |
| 96 | spin_unlock_bh(&deferred_lock); |
| 97 | schedule_work(&deferred_process_work); |
| 98 | return 0; |
| 99 | } |
| 100 | |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 101 | static int switchdev_port_attr_notify(enum switchdev_notifier_type nt, |
| 102 | struct net_device *dev, |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 103 | const struct switchdev_attr *attr) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 104 | { |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 105 | int err; |
| 106 | int rc; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 107 | |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 108 | struct switchdev_notifier_port_attr_info attr_info = { |
| 109 | .attr = attr, |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 110 | .handled = false, |
| 111 | }; |
| 112 | |
| 113 | rc = call_switchdev_blocking_notifiers(nt, dev, |
| 114 | &attr_info.info, NULL); |
| 115 | err = notifier_to_errno(rc); |
| 116 | if (err) { |
| 117 | WARN_ON(!attr_info.handled); |
| 118 | return err; |
Jiri Pirko | 0c63d80 | 2015-11-03 17:40:53 +0100 | [diff] [blame] | 119 | } |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 120 | |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 121 | if (!attr_info.handled) |
| 122 | return -EOPNOTSUPP; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 123 | |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 124 | return 0; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 127 | static int switchdev_port_attr_set_now(struct net_device *dev, |
| 128 | const struct switchdev_attr *attr) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 129 | { |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 130 | return switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 131 | } |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 132 | |
| 133 | static void switchdev_port_attr_set_deferred(struct net_device *dev, |
| 134 | const void *data) |
| 135 | { |
| 136 | const struct switchdev_attr *attr = data; |
| 137 | int err; |
| 138 | |
| 139 | err = switchdev_port_attr_set_now(dev, attr); |
| 140 | if (err && err != -EOPNOTSUPP) |
| 141 | netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n", |
| 142 | err, attr->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 143 | if (attr->complete) |
| 144 | attr->complete(dev, err, attr->complete_priv); |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static int switchdev_port_attr_set_defer(struct net_device *dev, |
| 148 | const struct switchdev_attr *attr) |
| 149 | { |
| 150 | return switchdev_deferred_enqueue(dev, attr, sizeof(*attr), |
| 151 | switchdev_port_attr_set_deferred); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * switchdev_port_attr_set - Set port attribute |
| 156 | * |
| 157 | * @dev: port device |
| 158 | * @attr: attribute to set |
| 159 | * |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 160 | * rtnl_lock must be held and must not be in atomic section, |
| 161 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 162 | */ |
| 163 | int switchdev_port_attr_set(struct net_device *dev, |
| 164 | const struct switchdev_attr *attr) |
| 165 | { |
| 166 | if (attr->flags & SWITCHDEV_F_DEFER) |
| 167 | return switchdev_port_attr_set_defer(dev, attr); |
| 168 | ASSERT_RTNL(); |
| 169 | return switchdev_port_attr_set_now(dev, attr); |
| 170 | } |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 171 | EXPORT_SYMBOL_GPL(switchdev_port_attr_set); |
| 172 | |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 173 | static size_t switchdev_obj_size(const struct switchdev_obj *obj) |
| 174 | { |
| 175 | switch (obj->id) { |
| 176 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 177 | return sizeof(struct switchdev_obj_port_vlan); |
Elad Raz | 4d41e125 | 2016-01-10 21:06:22 +0100 | [diff] [blame] | 178 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 179 | return sizeof(struct switchdev_obj_port_mdb); |
Andrew Lunn | 47d5b6d | 2017-11-09 23:10:59 +0100 | [diff] [blame] | 180 | case SWITCHDEV_OBJ_ID_HOST_MDB: |
| 181 | return sizeof(struct switchdev_obj_port_mdb); |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 182 | default: |
| 183 | BUG(); |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 188 | static int switchdev_port_obj_notify(enum switchdev_notifier_type nt, |
| 189 | struct net_device *dev, |
| 190 | const struct switchdev_obj *obj, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 191 | struct netlink_ext_ack *extack) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 192 | { |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 193 | int rc; |
| 194 | int err; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 195 | |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 196 | struct switchdev_notifier_port_obj_info obj_info = { |
| 197 | .obj = obj, |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 198 | .handled = false, |
| 199 | }; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 200 | |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 201 | rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack); |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 202 | err = notifier_to_errno(rc); |
| 203 | if (err) { |
| 204 | WARN_ON(!obj_info.handled); |
| 205 | return err; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 206 | } |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 207 | if (!obj_info.handled) |
| 208 | return -EOPNOTSUPP; |
| 209 | return 0; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 212 | static void switchdev_port_obj_add_deferred(struct net_device *dev, |
| 213 | const void *data) |
| 214 | { |
| 215 | const struct switchdev_obj *obj = data; |
| 216 | int err; |
| 217 | |
Vladimir Oltean | cf6def5 | 2021-01-09 02:01:49 +0200 | [diff] [blame] | 218 | ASSERT_RTNL(); |
| 219 | err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD, |
| 220 | dev, obj, NULL); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 221 | if (err && err != -EOPNOTSUPP) |
| 222 | netdev_err(dev, "failed (err=%d) to add object (id=%d)\n", |
| 223 | err, obj->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 224 | if (obj->complete) |
| 225 | obj->complete(dev, err, obj->complete_priv); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static int switchdev_port_obj_add_defer(struct net_device *dev, |
| 229 | const struct switchdev_obj *obj) |
| 230 | { |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 231 | return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj), |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 232 | switchdev_port_obj_add_deferred); |
| 233 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 234 | |
| 235 | /** |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 236 | * switchdev_port_obj_add - Add port object |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 237 | * |
| 238 | * @dev: port device |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 239 | * @obj: object to add |
Andrew Lunn | c8af73f | 2020-07-13 01:15:13 +0200 | [diff] [blame] | 240 | * @extack: netlink extended ack |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 241 | * |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 242 | * rtnl_lock must be held and must not be in atomic section, |
| 243 | * in case SWITCHDEV_F_DEFER flag is not set. |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 244 | */ |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 245 | int switchdev_port_obj_add(struct net_device *dev, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 246 | const struct switchdev_obj *obj, |
| 247 | struct netlink_ext_ack *extack) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 248 | { |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 249 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 250 | return switchdev_port_obj_add_defer(dev, obj); |
| 251 | ASSERT_RTNL(); |
Vladimir Oltean | cf6def5 | 2021-01-09 02:01:49 +0200 | [diff] [blame] | 252 | return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD, |
| 253 | dev, obj, extack); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 254 | } |
| 255 | EXPORT_SYMBOL_GPL(switchdev_port_obj_add); |
| 256 | |
| 257 | static int switchdev_port_obj_del_now(struct net_device *dev, |
| 258 | const struct switchdev_obj *obj) |
| 259 | { |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 260 | return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL, |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 261 | dev, obj, NULL); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 262 | } |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 263 | |
| 264 | static void switchdev_port_obj_del_deferred(struct net_device *dev, |
| 265 | const void *data) |
| 266 | { |
| 267 | const struct switchdev_obj *obj = data; |
| 268 | int err; |
| 269 | |
| 270 | err = switchdev_port_obj_del_now(dev, obj); |
| 271 | if (err && err != -EOPNOTSUPP) |
| 272 | netdev_err(dev, "failed (err=%d) to del object (id=%d)\n", |
| 273 | err, obj->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 274 | if (obj->complete) |
| 275 | obj->complete(dev, err, obj->complete_priv); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static int switchdev_port_obj_del_defer(struct net_device *dev, |
| 279 | const struct switchdev_obj *obj) |
| 280 | { |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 281 | return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj), |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 282 | switchdev_port_obj_del_deferred); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * switchdev_port_obj_del - Delete port object |
| 287 | * |
| 288 | * @dev: port device |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 289 | * @obj: object to delete |
| 290 | * |
| 291 | * rtnl_lock must be held and must not be in atomic section, |
| 292 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 293 | */ |
| 294 | int switchdev_port_obj_del(struct net_device *dev, |
| 295 | const struct switchdev_obj *obj) |
| 296 | { |
| 297 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 298 | return switchdev_port_obj_del_defer(dev, obj); |
| 299 | ASSERT_RTNL(); |
| 300 | return switchdev_port_obj_del_now(dev, obj); |
| 301 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 302 | EXPORT_SYMBOL_GPL(switchdev_port_obj_del); |
| 303 | |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 304 | static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain); |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 305 | static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 306 | |
| 307 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 308 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 309 | * @nb: notifier_block |
| 310 | * |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 311 | * Register switch device notifier. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 312 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 313 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 314 | { |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 315 | return atomic_notifier_chain_register(&switchdev_notif_chain, nb); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 316 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 317 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 318 | |
| 319 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 320 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 321 | * @nb: notifier_block |
| 322 | * |
| 323 | * Unregister switch device notifier. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 324 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 325 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 326 | { |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 327 | return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 328 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 329 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 330 | |
| 331 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 332 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 333 | * @val: value passed unmodified to notifier function |
| 334 | * @dev: port device |
| 335 | * @info: notifier information data |
Tian Tao | ea6754a | 2020-09-22 21:32:19 +0800 | [diff] [blame] | 336 | * @extack: netlink extended ack |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 337 | * Call all network notifier blocks. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 338 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 339 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
Petr Machata | 6685987 | 2019-01-16 23:06:56 +0000 | [diff] [blame] | 340 | struct switchdev_notifier_info *info, |
| 341 | struct netlink_ext_ack *extack) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 342 | { |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 343 | info->dev = dev; |
Petr Machata | 6685987 | 2019-01-16 23:06:56 +0000 | [diff] [blame] | 344 | info->extack = extack; |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 345 | return atomic_notifier_call_chain(&switchdev_notif_chain, val, info); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 346 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 348 | |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 349 | int register_switchdev_blocking_notifier(struct notifier_block *nb) |
| 350 | { |
| 351 | struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain; |
| 352 | |
| 353 | return blocking_notifier_chain_register(chain, nb); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier); |
| 356 | |
| 357 | int unregister_switchdev_blocking_notifier(struct notifier_block *nb) |
| 358 | { |
| 359 | struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain; |
| 360 | |
| 361 | return blocking_notifier_chain_unregister(chain, nb); |
| 362 | } |
| 363 | EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier); |
| 364 | |
| 365 | int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 366 | struct switchdev_notifier_info *info, |
| 367 | struct netlink_ext_ack *extack) |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 368 | { |
| 369 | info->dev = dev; |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 370 | info->extack = extack; |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 371 | return blocking_notifier_call_chain(&switchdev_blocking_notif_chain, |
| 372 | val, info); |
| 373 | } |
| 374 | EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers); |
| 375 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 376 | static int __switchdev_handle_port_obj_add(struct net_device *dev, |
| 377 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 378 | bool (*check_cb)(const struct net_device *dev), |
| 379 | int (*add_cb)(struct net_device *dev, |
| 380 | const struct switchdev_obj *obj, |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 381 | struct netlink_ext_ack *extack)) |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 382 | { |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 383 | struct netlink_ext_ack *extack; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 384 | struct net_device *lower_dev; |
| 385 | struct list_head *iter; |
| 386 | int err = -EOPNOTSUPP; |
| 387 | |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 388 | extack = switchdev_notifier_info_to_extack(&port_obj_info->info); |
| 389 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 390 | if (check_cb(dev)) { |
Jakub Kicinski | c358f95 | 2021-01-28 17:09:31 -0800 | [diff] [blame] | 391 | err = add_cb(dev, port_obj_info->obj, extack); |
Rasmus Villemoes | 20776b4 | 2021-01-25 13:41:16 +0100 | [diff] [blame] | 392 | if (err != -EOPNOTSUPP) |
| 393 | port_obj_info->handled = true; |
| 394 | return err; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 398 | * unsupported devices, another driver might be able to handle them. But |
| 399 | * propagate to the callers any hard errors. |
| 400 | * |
| 401 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 402 | * necessary to go through this helper. |
| 403 | */ |
| 404 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Russell King | 07c6f98 | 2020-02-26 17:14:21 +0000 | [diff] [blame] | 405 | if (netif_is_bridge_master(lower_dev)) |
| 406 | continue; |
| 407 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 408 | err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info, |
| 409 | check_cb, add_cb); |
| 410 | if (err && err != -EOPNOTSUPP) |
| 411 | return err; |
| 412 | } |
| 413 | |
| 414 | return err; |
| 415 | } |
| 416 | |
| 417 | int switchdev_handle_port_obj_add(struct net_device *dev, |
| 418 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 419 | bool (*check_cb)(const struct net_device *dev), |
| 420 | int (*add_cb)(struct net_device *dev, |
| 421 | const struct switchdev_obj *obj, |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 422 | struct netlink_ext_ack *extack)) |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 423 | { |
| 424 | int err; |
| 425 | |
| 426 | err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb, |
| 427 | add_cb); |
| 428 | if (err == -EOPNOTSUPP) |
| 429 | err = 0; |
| 430 | return err; |
| 431 | } |
| 432 | EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add); |
| 433 | |
| 434 | static int __switchdev_handle_port_obj_del(struct net_device *dev, |
| 435 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 436 | bool (*check_cb)(const struct net_device *dev), |
| 437 | int (*del_cb)(struct net_device *dev, |
| 438 | const struct switchdev_obj *obj)) |
| 439 | { |
| 440 | struct net_device *lower_dev; |
| 441 | struct list_head *iter; |
| 442 | int err = -EOPNOTSUPP; |
| 443 | |
| 444 | if (check_cb(dev)) { |
Rasmus Villemoes | 20776b4 | 2021-01-25 13:41:16 +0100 | [diff] [blame] | 445 | err = del_cb(dev, port_obj_info->obj); |
| 446 | if (err != -EOPNOTSUPP) |
| 447 | port_obj_info->handled = true; |
| 448 | return err; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 452 | * unsupported devices, another driver might be able to handle them. But |
| 453 | * propagate to the callers any hard errors. |
| 454 | * |
| 455 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 456 | * necessary to go through this helper. |
| 457 | */ |
| 458 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Russell King | 07c6f98 | 2020-02-26 17:14:21 +0000 | [diff] [blame] | 459 | if (netif_is_bridge_master(lower_dev)) |
| 460 | continue; |
| 461 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 462 | err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info, |
| 463 | check_cb, del_cb); |
| 464 | if (err && err != -EOPNOTSUPP) |
| 465 | return err; |
| 466 | } |
| 467 | |
| 468 | return err; |
| 469 | } |
| 470 | |
| 471 | int switchdev_handle_port_obj_del(struct net_device *dev, |
| 472 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 473 | bool (*check_cb)(const struct net_device *dev), |
| 474 | int (*del_cb)(struct net_device *dev, |
| 475 | const struct switchdev_obj *obj)) |
| 476 | { |
| 477 | int err; |
| 478 | |
| 479 | err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb, |
| 480 | del_cb); |
| 481 | if (err == -EOPNOTSUPP) |
| 482 | err = 0; |
| 483 | return err; |
| 484 | } |
| 485 | EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del); |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 486 | |
| 487 | static int __switchdev_handle_port_attr_set(struct net_device *dev, |
| 488 | struct switchdev_notifier_port_attr_info *port_attr_info, |
| 489 | bool (*check_cb)(const struct net_device *dev), |
| 490 | int (*set_cb)(struct net_device *dev, |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 491 | const struct switchdev_attr *attr)) |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 492 | { |
| 493 | struct net_device *lower_dev; |
| 494 | struct list_head *iter; |
| 495 | int err = -EOPNOTSUPP; |
| 496 | |
| 497 | if (check_cb(dev)) { |
Jakub Kicinski | c358f95 | 2021-01-28 17:09:31 -0800 | [diff] [blame] | 498 | err = set_cb(dev, port_attr_info->attr); |
Rasmus Villemoes | 20776b4 | 2021-01-25 13:41:16 +0100 | [diff] [blame] | 499 | if (err != -EOPNOTSUPP) |
| 500 | port_attr_info->handled = true; |
| 501 | return err; |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 505 | * unsupported devices, another driver might be able to handle them. But |
| 506 | * propagate to the callers any hard errors. |
| 507 | * |
| 508 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 509 | * necessary to go through this helper. |
| 510 | */ |
| 511 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Russell King | 07c6f98 | 2020-02-26 17:14:21 +0000 | [diff] [blame] | 512 | if (netif_is_bridge_master(lower_dev)) |
| 513 | continue; |
| 514 | |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 515 | err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info, |
| 516 | check_cb, set_cb); |
| 517 | if (err && err != -EOPNOTSUPP) |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | int switchdev_handle_port_attr_set(struct net_device *dev, |
| 525 | struct switchdev_notifier_port_attr_info *port_attr_info, |
| 526 | bool (*check_cb)(const struct net_device *dev), |
| 527 | int (*set_cb)(struct net_device *dev, |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 528 | const struct switchdev_attr *attr)) |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 529 | { |
| 530 | int err; |
| 531 | |
| 532 | err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb, |
| 533 | set_cb); |
| 534 | if (err == -EOPNOTSUPP) |
| 535 | err = 0; |
| 536 | return err; |
| 537 | } |
| 538 | EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set); |