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 | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 103 | const struct switchdev_attr *attr, |
| 104 | struct netlink_ext_ack *extack) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 105 | { |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 106 | int err; |
| 107 | int rc; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 108 | |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 109 | struct switchdev_notifier_port_attr_info attr_info = { |
| 110 | .attr = attr, |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 111 | .handled = false, |
| 112 | }; |
| 113 | |
| 114 | rc = call_switchdev_blocking_notifiers(nt, dev, |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 115 | &attr_info.info, extack); |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 116 | err = notifier_to_errno(rc); |
| 117 | if (err) { |
| 118 | WARN_ON(!attr_info.handled); |
| 119 | return err; |
Jiri Pirko | 0c63d80 | 2015-11-03 17:40:53 +0100 | [diff] [blame] | 120 | } |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 121 | |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 122 | if (!attr_info.handled) |
| 123 | return -EOPNOTSUPP; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 124 | |
Florian Fainelli | d45224d | 2019-02-27 11:44:31 -0800 | [diff] [blame] | 125 | return 0; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 128 | static int switchdev_port_attr_set_now(struct net_device *dev, |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 129 | const struct switchdev_attr *attr, |
| 130 | struct netlink_ext_ack *extack) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 131 | { |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 132 | return switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr, |
| 133 | extack); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 134 | } |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 135 | |
| 136 | static void switchdev_port_attr_set_deferred(struct net_device *dev, |
| 137 | const void *data) |
| 138 | { |
| 139 | const struct switchdev_attr *attr = data; |
| 140 | int err; |
| 141 | |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 142 | err = switchdev_port_attr_set_now(dev, attr, NULL); |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 143 | if (err && err != -EOPNOTSUPP) |
| 144 | netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n", |
| 145 | err, attr->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 146 | if (attr->complete) |
| 147 | attr->complete(dev, err, attr->complete_priv); |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int switchdev_port_attr_set_defer(struct net_device *dev, |
| 151 | const struct switchdev_attr *attr) |
| 152 | { |
| 153 | return switchdev_deferred_enqueue(dev, attr, sizeof(*attr), |
| 154 | switchdev_port_attr_set_deferred); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * switchdev_port_attr_set - Set port attribute |
| 159 | * |
| 160 | * @dev: port device |
| 161 | * @attr: attribute to set |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 162 | * @extack: netlink extended ack, for error message propagation |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 163 | * |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 164 | * rtnl_lock must be held and must not be in atomic section, |
| 165 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 166 | */ |
| 167 | int switchdev_port_attr_set(struct net_device *dev, |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 168 | const struct switchdev_attr *attr, |
| 169 | struct netlink_ext_ack *extack) |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 170 | { |
| 171 | if (attr->flags & SWITCHDEV_F_DEFER) |
| 172 | return switchdev_port_attr_set_defer(dev, attr); |
| 173 | ASSERT_RTNL(); |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame] | 174 | return switchdev_port_attr_set_now(dev, attr, extack); |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 175 | } |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 176 | EXPORT_SYMBOL_GPL(switchdev_port_attr_set); |
| 177 | |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 178 | static size_t switchdev_obj_size(const struct switchdev_obj *obj) |
| 179 | { |
| 180 | switch (obj->id) { |
| 181 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 182 | return sizeof(struct switchdev_obj_port_vlan); |
Elad Raz | 4d41e125 | 2016-01-10 21:06:22 +0100 | [diff] [blame] | 183 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 184 | return sizeof(struct switchdev_obj_port_mdb); |
Andrew Lunn | 47d5b6d | 2017-11-09 23:10:59 +0100 | [diff] [blame] | 185 | case SWITCHDEV_OBJ_ID_HOST_MDB: |
| 186 | return sizeof(struct switchdev_obj_port_mdb); |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 187 | default: |
| 188 | BUG(); |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 193 | static int switchdev_port_obj_notify(enum switchdev_notifier_type nt, |
| 194 | struct net_device *dev, |
| 195 | const struct switchdev_obj *obj, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 196 | struct netlink_ext_ack *extack) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 197 | { |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 198 | int rc; |
| 199 | int err; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 200 | |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 201 | struct switchdev_notifier_port_obj_info obj_info = { |
| 202 | .obj = obj, |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 203 | .handled = false, |
| 204 | }; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 205 | |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 206 | rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack); |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 207 | err = notifier_to_errno(rc); |
| 208 | if (err) { |
| 209 | WARN_ON(!obj_info.handled); |
| 210 | return err; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 211 | } |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 212 | if (!obj_info.handled) |
| 213 | return -EOPNOTSUPP; |
| 214 | return 0; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 217 | static void switchdev_port_obj_add_deferred(struct net_device *dev, |
| 218 | const void *data) |
| 219 | { |
| 220 | const struct switchdev_obj *obj = data; |
| 221 | int err; |
| 222 | |
Vladimir Oltean | cf6def5 | 2021-01-09 02:01:49 +0200 | [diff] [blame] | 223 | ASSERT_RTNL(); |
| 224 | err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD, |
| 225 | dev, obj, NULL); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 226 | if (err && err != -EOPNOTSUPP) |
| 227 | netdev_err(dev, "failed (err=%d) to add object (id=%d)\n", |
| 228 | err, obj->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 229 | if (obj->complete) |
| 230 | obj->complete(dev, err, obj->complete_priv); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int switchdev_port_obj_add_defer(struct net_device *dev, |
| 234 | const struct switchdev_obj *obj) |
| 235 | { |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 236 | return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj), |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 237 | switchdev_port_obj_add_deferred); |
| 238 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 239 | |
| 240 | /** |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 241 | * switchdev_port_obj_add - Add port object |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 242 | * |
| 243 | * @dev: port device |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 244 | * @obj: object to add |
Andrew Lunn | c8af73f | 2020-07-13 01:15:13 +0200 | [diff] [blame] | 245 | * @extack: netlink extended ack |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 246 | * |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 247 | * rtnl_lock must be held and must not be in atomic section, |
| 248 | * in case SWITCHDEV_F_DEFER flag is not set. |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 249 | */ |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 250 | int switchdev_port_obj_add(struct net_device *dev, |
Petr Machata | 69b7320 | 2018-12-12 17:02:52 +0000 | [diff] [blame] | 251 | const struct switchdev_obj *obj, |
| 252 | struct netlink_ext_ack *extack) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 253 | { |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 254 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 255 | return switchdev_port_obj_add_defer(dev, obj); |
| 256 | ASSERT_RTNL(); |
Vladimir Oltean | cf6def5 | 2021-01-09 02:01:49 +0200 | [diff] [blame] | 257 | return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD, |
| 258 | dev, obj, extack); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL_GPL(switchdev_port_obj_add); |
| 261 | |
| 262 | static int switchdev_port_obj_del_now(struct net_device *dev, |
| 263 | const struct switchdev_obj *obj) |
| 264 | { |
Petr Machata | d17d9f5 | 2018-11-22 23:32:57 +0000 | [diff] [blame] | 265 | return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL, |
Vladimir Oltean | ffb68fc | 2021-01-09 02:01:48 +0200 | [diff] [blame] | 266 | dev, obj, NULL); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 267 | } |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 268 | |
| 269 | static void switchdev_port_obj_del_deferred(struct net_device *dev, |
| 270 | const void *data) |
| 271 | { |
| 272 | const struct switchdev_obj *obj = data; |
| 273 | int err; |
| 274 | |
| 275 | err = switchdev_port_obj_del_now(dev, obj); |
| 276 | if (err && err != -EOPNOTSUPP) |
| 277 | netdev_err(dev, "failed (err=%d) to del object (id=%d)\n", |
| 278 | err, obj->id); |
Elad Raz | 7ceb2af | 2016-04-21 12:52:43 +0200 | [diff] [blame] | 279 | if (obj->complete) |
| 280 | obj->complete(dev, err, obj->complete_priv); |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int switchdev_port_obj_del_defer(struct net_device *dev, |
| 284 | const struct switchdev_obj *obj) |
| 285 | { |
Scott Feldman | e258d91 | 2015-10-28 23:17:31 -0700 | [diff] [blame] | 286 | return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj), |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 287 | switchdev_port_obj_del_deferred); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * switchdev_port_obj_del - Delete port object |
| 292 | * |
| 293 | * @dev: port device |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame] | 294 | * @obj: object to delete |
| 295 | * |
| 296 | * rtnl_lock must be held and must not be in atomic section, |
| 297 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 298 | */ |
| 299 | int switchdev_port_obj_del(struct net_device *dev, |
| 300 | const struct switchdev_obj *obj) |
| 301 | { |
| 302 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 303 | return switchdev_port_obj_del_defer(dev, obj); |
| 304 | ASSERT_RTNL(); |
| 305 | return switchdev_port_obj_del_now(dev, obj); |
| 306 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 307 | EXPORT_SYMBOL_GPL(switchdev_port_obj_del); |
| 308 | |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 309 | static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain); |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 310 | static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 311 | |
| 312 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 313 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 314 | * @nb: notifier_block |
| 315 | * |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 316 | * Register switch device notifier. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 317 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 318 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 319 | { |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 320 | return atomic_notifier_chain_register(&switchdev_notif_chain, nb); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 321 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 322 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 323 | |
| 324 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 325 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 326 | * @nb: notifier_block |
| 327 | * |
| 328 | * Unregister switch device notifier. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 329 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 330 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 331 | { |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 332 | return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 333 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 334 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 335 | |
| 336 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 337 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 338 | * @val: value passed unmodified to notifier function |
| 339 | * @dev: port device |
| 340 | * @info: notifier information data |
Tian Tao | ea6754a | 2020-09-22 21:32:19 +0800 | [diff] [blame] | 341 | * @extack: netlink extended ack |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 342 | * Call all network notifier blocks. |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 343 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 344 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
Petr Machata | 6685987 | 2019-01-16 23:06:56 +0000 | [diff] [blame] | 345 | struct switchdev_notifier_info *info, |
| 346 | struct netlink_ext_ack *extack) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 347 | { |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 348 | info->dev = dev; |
Petr Machata | 6685987 | 2019-01-16 23:06:56 +0000 | [diff] [blame] | 349 | info->extack = extack; |
Arkadi Sharshevsky | ff5cf10 | 2017-06-08 08:44:13 +0200 | [diff] [blame] | 350 | return atomic_notifier_call_chain(&switchdev_notif_chain, val, info); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 351 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 352 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 353 | |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 354 | int register_switchdev_blocking_notifier(struct notifier_block *nb) |
| 355 | { |
| 356 | struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain; |
| 357 | |
| 358 | return blocking_notifier_chain_register(chain, nb); |
| 359 | } |
| 360 | EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier); |
| 361 | |
| 362 | int unregister_switchdev_blocking_notifier(struct notifier_block *nb) |
| 363 | { |
| 364 | struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain; |
| 365 | |
| 366 | return blocking_notifier_chain_unregister(chain, nb); |
| 367 | } |
| 368 | EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier); |
| 369 | |
| 370 | int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 371 | struct switchdev_notifier_info *info, |
| 372 | struct netlink_ext_ack *extack) |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 373 | { |
| 374 | info->dev = dev; |
Petr Machata | 479c86d | 2018-12-12 17:02:54 +0000 | [diff] [blame] | 375 | info->extack = extack; |
Petr Machata | a93e3b1 | 2018-11-22 23:28:25 +0000 | [diff] [blame] | 376 | return blocking_notifier_call_chain(&switchdev_blocking_notif_chain, |
| 377 | val, info); |
| 378 | } |
| 379 | EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers); |
| 380 | |
Vladimir Oltean | 8ca0717 | 2021-07-19 16:51:39 +0300 | [diff] [blame^] | 381 | static int __switchdev_handle_fdb_add_to_device(struct net_device *dev, |
| 382 | const struct net_device *orig_dev, |
| 383 | const struct switchdev_notifier_fdb_info *fdb_info, |
| 384 | bool (*check_cb)(const struct net_device *dev), |
| 385 | bool (*foreign_dev_check_cb)(const struct net_device *dev, |
| 386 | const struct net_device *foreign_dev), |
| 387 | int (*add_cb)(struct net_device *dev, |
| 388 | const struct net_device *orig_dev, const void *ctx, |
| 389 | const struct switchdev_notifier_fdb_info *fdb_info), |
| 390 | int (*lag_add_cb)(struct net_device *dev, |
| 391 | const struct net_device *orig_dev, const void *ctx, |
| 392 | const struct switchdev_notifier_fdb_info *fdb_info)) |
| 393 | { |
| 394 | const struct switchdev_notifier_info *info = &fdb_info->info; |
| 395 | struct net_device *lower_dev; |
| 396 | struct list_head *iter; |
| 397 | int err = -EOPNOTSUPP; |
| 398 | |
| 399 | if (check_cb(dev)) { |
| 400 | /* Handle FDB entries on foreign interfaces as FDB entries |
| 401 | * towards the software bridge. |
| 402 | */ |
| 403 | if (foreign_dev_check_cb && foreign_dev_check_cb(dev, orig_dev)) { |
| 404 | struct net_device *br = netdev_master_upper_dev_get_rcu(dev); |
| 405 | |
| 406 | if (!br || !netif_is_bridge_master(br)) |
| 407 | return 0; |
| 408 | |
| 409 | /* No point in handling FDB entries on a foreign bridge */ |
| 410 | if (foreign_dev_check_cb(dev, br)) |
| 411 | return 0; |
| 412 | |
| 413 | return __switchdev_handle_fdb_add_to_device(br, orig_dev, |
| 414 | fdb_info, check_cb, |
| 415 | foreign_dev_check_cb, |
| 416 | add_cb, lag_add_cb); |
| 417 | } |
| 418 | |
| 419 | return add_cb(dev, orig_dev, info->ctx, fdb_info); |
| 420 | } |
| 421 | |
| 422 | /* If we passed over the foreign check, it means that the LAG interface |
| 423 | * is offloaded. |
| 424 | */ |
| 425 | if (netif_is_lag_master(dev)) { |
| 426 | if (!lag_add_cb) |
| 427 | return -EOPNOTSUPP; |
| 428 | |
| 429 | return lag_add_cb(dev, orig_dev, info->ctx, fdb_info); |
| 430 | } |
| 431 | |
| 432 | /* Recurse through lower interfaces in case the FDB entry is pointing |
| 433 | * towards a bridge device. |
| 434 | */ |
| 435 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 436 | /* Do not propagate FDB entries across bridges */ |
| 437 | if (netif_is_bridge_master(lower_dev)) |
| 438 | continue; |
| 439 | |
| 440 | err = __switchdev_handle_fdb_add_to_device(lower_dev, orig_dev, |
| 441 | fdb_info, check_cb, |
| 442 | foreign_dev_check_cb, |
| 443 | add_cb, lag_add_cb); |
| 444 | if (err && err != -EOPNOTSUPP) |
| 445 | return err; |
| 446 | } |
| 447 | |
| 448 | return err; |
| 449 | } |
| 450 | |
| 451 | int switchdev_handle_fdb_add_to_device(struct net_device *dev, |
| 452 | const struct switchdev_notifier_fdb_info *fdb_info, |
| 453 | bool (*check_cb)(const struct net_device *dev), |
| 454 | bool (*foreign_dev_check_cb)(const struct net_device *dev, |
| 455 | const struct net_device *foreign_dev), |
| 456 | int (*add_cb)(struct net_device *dev, |
| 457 | const struct net_device *orig_dev, const void *ctx, |
| 458 | const struct switchdev_notifier_fdb_info *fdb_info), |
| 459 | int (*lag_add_cb)(struct net_device *dev, |
| 460 | const struct net_device *orig_dev, const void *ctx, |
| 461 | const struct switchdev_notifier_fdb_info *fdb_info)) |
| 462 | { |
| 463 | int err; |
| 464 | |
| 465 | err = __switchdev_handle_fdb_add_to_device(dev, dev, fdb_info, |
| 466 | check_cb, |
| 467 | foreign_dev_check_cb, |
| 468 | add_cb, lag_add_cb); |
| 469 | if (err == -EOPNOTSUPP) |
| 470 | err = 0; |
| 471 | |
| 472 | return err; |
| 473 | } |
| 474 | EXPORT_SYMBOL_GPL(switchdev_handle_fdb_add_to_device); |
| 475 | |
| 476 | static int __switchdev_handle_fdb_del_to_device(struct net_device *dev, |
| 477 | const struct net_device *orig_dev, |
| 478 | const struct switchdev_notifier_fdb_info *fdb_info, |
| 479 | bool (*check_cb)(const struct net_device *dev), |
| 480 | bool (*foreign_dev_check_cb)(const struct net_device *dev, |
| 481 | const struct net_device *foreign_dev), |
| 482 | int (*del_cb)(struct net_device *dev, |
| 483 | const struct net_device *orig_dev, const void *ctx, |
| 484 | const struct switchdev_notifier_fdb_info *fdb_info), |
| 485 | int (*lag_del_cb)(struct net_device *dev, |
| 486 | const struct net_device *orig_dev, const void *ctx, |
| 487 | const struct switchdev_notifier_fdb_info *fdb_info)) |
| 488 | { |
| 489 | const struct switchdev_notifier_info *info = &fdb_info->info; |
| 490 | struct net_device *lower_dev; |
| 491 | struct list_head *iter; |
| 492 | int err = -EOPNOTSUPP; |
| 493 | |
| 494 | if (check_cb(dev)) { |
| 495 | /* Handle FDB entries on foreign interfaces as FDB entries |
| 496 | * towards the software bridge. |
| 497 | */ |
| 498 | if (foreign_dev_check_cb && foreign_dev_check_cb(dev, orig_dev)) { |
| 499 | struct net_device *br = netdev_master_upper_dev_get_rcu(dev); |
| 500 | |
| 501 | if (!br || !netif_is_bridge_master(br)) |
| 502 | return 0; |
| 503 | |
| 504 | /* No point in handling FDB entries on a foreign bridge */ |
| 505 | if (foreign_dev_check_cb(dev, br)) |
| 506 | return 0; |
| 507 | |
| 508 | return __switchdev_handle_fdb_del_to_device(br, orig_dev, |
| 509 | fdb_info, check_cb, |
| 510 | foreign_dev_check_cb, |
| 511 | del_cb, lag_del_cb); |
| 512 | } |
| 513 | |
| 514 | return del_cb(dev, orig_dev, info->ctx, fdb_info); |
| 515 | } |
| 516 | |
| 517 | /* If we passed over the foreign check, it means that the LAG interface |
| 518 | * is offloaded. |
| 519 | */ |
| 520 | if (netif_is_lag_master(dev)) { |
| 521 | if (!lag_del_cb) |
| 522 | return -EOPNOTSUPP; |
| 523 | |
| 524 | return lag_del_cb(dev, orig_dev, info->ctx, fdb_info); |
| 525 | } |
| 526 | |
| 527 | /* Recurse through lower interfaces in case the FDB entry is pointing |
| 528 | * towards a bridge device. |
| 529 | */ |
| 530 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 531 | /* Do not propagate FDB entries across bridges */ |
| 532 | if (netif_is_bridge_master(lower_dev)) |
| 533 | continue; |
| 534 | |
| 535 | err = switchdev_handle_fdb_del_to_device(lower_dev, fdb_info, |
| 536 | check_cb, |
| 537 | foreign_dev_check_cb, |
| 538 | del_cb, lag_del_cb); |
| 539 | if (err && err != -EOPNOTSUPP) |
| 540 | return err; |
| 541 | } |
| 542 | |
| 543 | return err; |
| 544 | } |
| 545 | |
| 546 | int switchdev_handle_fdb_del_to_device(struct net_device *dev, |
| 547 | const struct switchdev_notifier_fdb_info *fdb_info, |
| 548 | bool (*check_cb)(const struct net_device *dev), |
| 549 | bool (*foreign_dev_check_cb)(const struct net_device *dev, |
| 550 | const struct net_device *foreign_dev), |
| 551 | int (*del_cb)(struct net_device *dev, |
| 552 | const struct net_device *orig_dev, const void *ctx, |
| 553 | const struct switchdev_notifier_fdb_info *fdb_info), |
| 554 | int (*lag_del_cb)(struct net_device *dev, |
| 555 | const struct net_device *orig_dev, const void *ctx, |
| 556 | const struct switchdev_notifier_fdb_info *fdb_info)) |
| 557 | { |
| 558 | int err; |
| 559 | |
| 560 | err = __switchdev_handle_fdb_del_to_device(dev, dev, fdb_info, |
| 561 | check_cb, |
| 562 | foreign_dev_check_cb, |
| 563 | del_cb, lag_del_cb); |
| 564 | if (err == -EOPNOTSUPP) |
| 565 | err = 0; |
| 566 | |
| 567 | return err; |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(switchdev_handle_fdb_del_to_device); |
| 570 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 571 | static int __switchdev_handle_port_obj_add(struct net_device *dev, |
| 572 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 573 | bool (*check_cb)(const struct net_device *dev), |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 574 | int (*add_cb)(struct net_device *dev, const void *ctx, |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 575 | const struct switchdev_obj *obj, |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 576 | struct netlink_ext_ack *extack)) |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 577 | { |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 578 | struct switchdev_notifier_info *info = &port_obj_info->info; |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 579 | struct netlink_ext_ack *extack; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 580 | struct net_device *lower_dev; |
| 581 | struct list_head *iter; |
| 582 | int err = -EOPNOTSUPP; |
| 583 | |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 584 | extack = switchdev_notifier_info_to_extack(info); |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 585 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 586 | if (check_cb(dev)) { |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 587 | err = add_cb(dev, info->ctx, port_obj_info->obj, extack); |
Rasmus Villemoes | 20776b4 | 2021-01-25 13:41:16 +0100 | [diff] [blame] | 588 | if (err != -EOPNOTSUPP) |
| 589 | port_obj_info->handled = true; |
| 590 | return err; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 594 | * unsupported devices, another driver might be able to handle them. But |
| 595 | * propagate to the callers any hard errors. |
| 596 | * |
| 597 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 598 | * necessary to go through this helper. |
| 599 | */ |
| 600 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Russell King | 07c6f98 | 2020-02-26 17:14:21 +0000 | [diff] [blame] | 601 | if (netif_is_bridge_master(lower_dev)) |
| 602 | continue; |
| 603 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 604 | err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info, |
| 605 | check_cb, add_cb); |
| 606 | if (err && err != -EOPNOTSUPP) |
| 607 | return err; |
| 608 | } |
| 609 | |
| 610 | return err; |
| 611 | } |
| 612 | |
| 613 | int switchdev_handle_port_obj_add(struct net_device *dev, |
| 614 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 615 | bool (*check_cb)(const struct net_device *dev), |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 616 | int (*add_cb)(struct net_device *dev, const void *ctx, |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 617 | const struct switchdev_obj *obj, |
Petr Machata | 6921351 | 2018-12-12 17:02:56 +0000 | [diff] [blame] | 618 | struct netlink_ext_ack *extack)) |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 619 | { |
| 620 | int err; |
| 621 | |
| 622 | err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb, |
| 623 | add_cb); |
| 624 | if (err == -EOPNOTSUPP) |
| 625 | err = 0; |
| 626 | return err; |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add); |
| 629 | |
| 630 | static int __switchdev_handle_port_obj_del(struct net_device *dev, |
| 631 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 632 | bool (*check_cb)(const struct net_device *dev), |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 633 | int (*del_cb)(struct net_device *dev, const void *ctx, |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 634 | const struct switchdev_obj *obj)) |
| 635 | { |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 636 | struct switchdev_notifier_info *info = &port_obj_info->info; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 637 | struct net_device *lower_dev; |
| 638 | struct list_head *iter; |
| 639 | int err = -EOPNOTSUPP; |
| 640 | |
| 641 | if (check_cb(dev)) { |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 642 | err = del_cb(dev, info->ctx, port_obj_info->obj); |
Rasmus Villemoes | 20776b4 | 2021-01-25 13:41:16 +0100 | [diff] [blame] | 643 | if (err != -EOPNOTSUPP) |
| 644 | port_obj_info->handled = true; |
| 645 | return err; |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 649 | * unsupported devices, another driver might be able to handle them. But |
| 650 | * propagate to the callers any hard errors. |
| 651 | * |
| 652 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 653 | * necessary to go through this helper. |
| 654 | */ |
| 655 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Russell King | 07c6f98 | 2020-02-26 17:14:21 +0000 | [diff] [blame] | 656 | if (netif_is_bridge_master(lower_dev)) |
| 657 | continue; |
| 658 | |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 659 | err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info, |
| 660 | check_cb, del_cb); |
| 661 | if (err && err != -EOPNOTSUPP) |
| 662 | return err; |
| 663 | } |
| 664 | |
| 665 | return err; |
| 666 | } |
| 667 | |
| 668 | int switchdev_handle_port_obj_del(struct net_device *dev, |
| 669 | struct switchdev_notifier_port_obj_info *port_obj_info, |
| 670 | bool (*check_cb)(const struct net_device *dev), |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 671 | int (*del_cb)(struct net_device *dev, const void *ctx, |
Petr Machata | f30f060 | 2018-11-22 23:29:44 +0000 | [diff] [blame] | 672 | const struct switchdev_obj *obj)) |
| 673 | { |
| 674 | int err; |
| 675 | |
| 676 | err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb, |
| 677 | del_cb); |
| 678 | if (err == -EOPNOTSUPP) |
| 679 | err = 0; |
| 680 | return err; |
| 681 | } |
| 682 | EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del); |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 683 | |
| 684 | static int __switchdev_handle_port_attr_set(struct net_device *dev, |
| 685 | struct switchdev_notifier_port_attr_info *port_attr_info, |
| 686 | bool (*check_cb)(const struct net_device *dev), |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 687 | int (*set_cb)(struct net_device *dev, const void *ctx, |
Vladimir Oltean | 4c08c58 | 2021-02-12 17:15:51 +0200 | [diff] [blame] | 688 | const struct switchdev_attr *attr, |
| 689 | struct netlink_ext_ack *extack)) |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 690 | { |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 691 | struct switchdev_notifier_info *info = &port_attr_info->info; |
Vladimir Oltean | 4c08c58 | 2021-02-12 17:15:51 +0200 | [diff] [blame] | 692 | struct netlink_ext_ack *extack; |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 693 | struct net_device *lower_dev; |
| 694 | struct list_head *iter; |
| 695 | int err = -EOPNOTSUPP; |
| 696 | |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 697 | extack = switchdev_notifier_info_to_extack(info); |
Vladimir Oltean | 4c08c58 | 2021-02-12 17:15:51 +0200 | [diff] [blame] | 698 | |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 699 | if (check_cb(dev)) { |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 700 | err = set_cb(dev, info->ctx, port_attr_info->attr, extack); |
Rasmus Villemoes | 20776b4 | 2021-01-25 13:41:16 +0100 | [diff] [blame] | 701 | if (err != -EOPNOTSUPP) |
| 702 | port_attr_info->handled = true; |
| 703 | return err; |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /* Switch ports might be stacked under e.g. a LAG. Ignore the |
| 707 | * unsupported devices, another driver might be able to handle them. But |
| 708 | * propagate to the callers any hard errors. |
| 709 | * |
| 710 | * If the driver does its own bookkeeping of stacked ports, it's not |
| 711 | * necessary to go through this helper. |
| 712 | */ |
| 713 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Russell King | 07c6f98 | 2020-02-26 17:14:21 +0000 | [diff] [blame] | 714 | if (netif_is_bridge_master(lower_dev)) |
| 715 | continue; |
| 716 | |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 717 | err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info, |
| 718 | check_cb, set_cb); |
| 719 | if (err && err != -EOPNOTSUPP) |
| 720 | return err; |
| 721 | } |
| 722 | |
| 723 | return err; |
| 724 | } |
| 725 | |
| 726 | int switchdev_handle_port_attr_set(struct net_device *dev, |
| 727 | struct switchdev_notifier_port_attr_info *port_attr_info, |
| 728 | bool (*check_cb)(const struct net_device *dev), |
Vladimir Oltean | 69bfac9 | 2021-06-27 14:54:24 +0300 | [diff] [blame] | 729 | int (*set_cb)(struct net_device *dev, const void *ctx, |
Vladimir Oltean | 4c08c58 | 2021-02-12 17:15:51 +0200 | [diff] [blame] | 730 | const struct switchdev_attr *attr, |
| 731 | struct netlink_ext_ack *extack)) |
Florian Fainelli | 1cb33af | 2019-02-27 11:44:25 -0800 | [diff] [blame] | 732 | { |
| 733 | int err; |
| 734 | |
| 735 | err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb, |
| 736 | set_cb); |
| 737 | if (err == -EOPNOTSUPP) |
| 738 | err = 0; |
| 739 | return err; |
| 740 | } |
| 741 | EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set); |