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