Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/switchdev/switchdev.c - Switch device API |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us> |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 4 | * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/init.h> |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 15 | #include <linux/mutex.h> |
| 16 | #include <linux/notifier.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 17 | #include <linux/netdevice.h> |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 18 | #include <linux/etherdevice.h> |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 19 | #include <linux/if_bridge.h> |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 20 | #include <linux/list.h> |
Jiri Pirko | 793f401 | 2015-10-14 19:40:48 +0200 | [diff] [blame] | 21 | #include <linux/workqueue.h> |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 22 | #include <net/ip_fib.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 23 | #include <net/switchdev.h> |
| 24 | |
| 25 | /** |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 26 | * switchdev_trans_item_enqueue - Enqueue data item to transaction queue |
| 27 | * |
| 28 | * @trans: transaction |
| 29 | * @data: pointer to data being queued |
| 30 | * @destructor: data destructor |
| 31 | * @tritem: transaction item being queued |
| 32 | * |
| 33 | * Enqeueue data item to transaction queue. tritem is typically placed in |
| 34 | * cointainter pointed at by data pointer. Destructor is called on |
| 35 | * transaction abort and after successful commit phase in case |
| 36 | * the caller did not dequeue the item before. |
| 37 | */ |
| 38 | void switchdev_trans_item_enqueue(struct switchdev_trans *trans, |
| 39 | void *data, void (*destructor)(void const *), |
| 40 | struct switchdev_trans_item *tritem) |
| 41 | { |
| 42 | tritem->data = data; |
| 43 | tritem->destructor = destructor; |
| 44 | list_add_tail(&tritem->list, &trans->item_list); |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue); |
| 47 | |
| 48 | static struct switchdev_trans_item * |
| 49 | __switchdev_trans_item_dequeue(struct switchdev_trans *trans) |
| 50 | { |
| 51 | struct switchdev_trans_item *tritem; |
| 52 | |
| 53 | if (list_empty(&trans->item_list)) |
| 54 | return NULL; |
| 55 | tritem = list_first_entry(&trans->item_list, |
| 56 | struct switchdev_trans_item, list); |
| 57 | list_del(&tritem->list); |
| 58 | return tritem; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * switchdev_trans_item_dequeue - Dequeue data item from transaction queue |
| 63 | * |
| 64 | * @trans: transaction |
| 65 | */ |
| 66 | void *switchdev_trans_item_dequeue(struct switchdev_trans *trans) |
| 67 | { |
| 68 | struct switchdev_trans_item *tritem; |
| 69 | |
| 70 | tritem = __switchdev_trans_item_dequeue(trans); |
| 71 | BUG_ON(!tritem); |
| 72 | return tritem->data; |
| 73 | } |
| 74 | EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue); |
| 75 | |
| 76 | static void switchdev_trans_init(struct switchdev_trans *trans) |
| 77 | { |
| 78 | INIT_LIST_HEAD(&trans->item_list); |
| 79 | } |
| 80 | |
| 81 | static void switchdev_trans_items_destroy(struct switchdev_trans *trans) |
| 82 | { |
| 83 | struct switchdev_trans_item *tritem; |
| 84 | |
| 85 | while ((tritem = __switchdev_trans_item_dequeue(trans))) |
| 86 | tritem->destructor(tritem->data); |
| 87 | } |
| 88 | |
| 89 | static void switchdev_trans_items_warn_destroy(struct net_device *dev, |
| 90 | struct switchdev_trans *trans) |
| 91 | { |
| 92 | WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n", |
| 93 | dev->name); |
| 94 | switchdev_trans_items_destroy(trans); |
| 95 | } |
| 96 | |
Jiri Pirko | 793f401 | 2015-10-14 19:40:48 +0200 | [diff] [blame] | 97 | static LIST_HEAD(deferred); |
| 98 | static DEFINE_SPINLOCK(deferred_lock); |
| 99 | |
| 100 | typedef void switchdev_deferred_func_t(struct net_device *dev, |
| 101 | const void *data); |
| 102 | |
| 103 | struct switchdev_deferred_item { |
| 104 | struct list_head list; |
| 105 | struct net_device *dev; |
| 106 | switchdev_deferred_func_t *func; |
| 107 | unsigned long data[0]; |
| 108 | }; |
| 109 | |
| 110 | static struct switchdev_deferred_item *switchdev_deferred_dequeue(void) |
| 111 | { |
| 112 | struct switchdev_deferred_item *dfitem; |
| 113 | |
| 114 | spin_lock_bh(&deferred_lock); |
| 115 | if (list_empty(&deferred)) { |
| 116 | dfitem = NULL; |
| 117 | goto unlock; |
| 118 | } |
| 119 | dfitem = list_first_entry(&deferred, |
| 120 | struct switchdev_deferred_item, list); |
| 121 | list_del(&dfitem->list); |
| 122 | unlock: |
| 123 | spin_unlock_bh(&deferred_lock); |
| 124 | return dfitem; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * switchdev_deferred_process - Process ops in deferred queue |
| 129 | * |
| 130 | * Called to flush the ops currently queued in deferred ops queue. |
| 131 | * rtnl_lock must be held. |
| 132 | */ |
| 133 | void switchdev_deferred_process(void) |
| 134 | { |
| 135 | struct switchdev_deferred_item *dfitem; |
| 136 | |
| 137 | ASSERT_RTNL(); |
| 138 | |
| 139 | while ((dfitem = switchdev_deferred_dequeue())) { |
| 140 | dfitem->func(dfitem->dev, dfitem->data); |
| 141 | dev_put(dfitem->dev); |
| 142 | kfree(dfitem); |
| 143 | } |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(switchdev_deferred_process); |
| 146 | |
| 147 | static void switchdev_deferred_process_work(struct work_struct *work) |
| 148 | { |
| 149 | rtnl_lock(); |
| 150 | switchdev_deferred_process(); |
| 151 | rtnl_unlock(); |
| 152 | } |
| 153 | |
| 154 | static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work); |
| 155 | |
| 156 | static int switchdev_deferred_enqueue(struct net_device *dev, |
| 157 | const void *data, size_t data_len, |
| 158 | switchdev_deferred_func_t *func) |
| 159 | { |
| 160 | struct switchdev_deferred_item *dfitem; |
| 161 | |
| 162 | dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC); |
| 163 | if (!dfitem) |
| 164 | return -ENOMEM; |
| 165 | dfitem->dev = dev; |
| 166 | dfitem->func = func; |
| 167 | memcpy(dfitem->data, data, data_len); |
| 168 | dev_hold(dev); |
| 169 | spin_lock_bh(&deferred_lock); |
| 170 | list_add_tail(&dfitem->list, &deferred); |
| 171 | spin_unlock_bh(&deferred_lock); |
| 172 | schedule_work(&deferred_process_work); |
| 173 | return 0; |
| 174 | } |
| 175 | |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 176 | /** |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 177 | * switchdev_port_attr_get - Get port attribute |
| 178 | * |
| 179 | * @dev: port device |
| 180 | * @attr: attribute to get |
| 181 | */ |
| 182 | int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr) |
| 183 | { |
| 184 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 185 | struct net_device *lower_dev; |
| 186 | struct list_head *iter; |
| 187 | struct switchdev_attr first = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 188 | .id = SWITCHDEV_ATTR_ID_UNDEFINED |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 189 | }; |
| 190 | int err = -EOPNOTSUPP; |
| 191 | |
| 192 | if (ops && ops->switchdev_port_attr_get) |
| 193 | return ops->switchdev_port_attr_get(dev, attr); |
| 194 | |
| 195 | if (attr->flags & SWITCHDEV_F_NO_RECURSE) |
| 196 | return err; |
| 197 | |
| 198 | /* Switch device port(s) may be stacked under |
| 199 | * bond/team/vlan dev, so recurse down to get attr on |
| 200 | * each port. Return -ENODATA if attr values don't |
| 201 | * compare across ports. |
| 202 | */ |
| 203 | |
| 204 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 205 | err = switchdev_port_attr_get(lower_dev, attr); |
| 206 | if (err) |
| 207 | break; |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 208 | if (first.id == SWITCHDEV_ATTR_ID_UNDEFINED) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 209 | first = *attr; |
| 210 | else if (memcmp(&first, attr, sizeof(*attr))) |
| 211 | return -ENODATA; |
| 212 | } |
| 213 | |
| 214 | return err; |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(switchdev_port_attr_get); |
| 217 | |
| 218 | static int __switchdev_port_attr_set(struct net_device *dev, |
Jiri Pirko | f7fadf3 | 2015-10-14 19:40:49 +0200 | [diff] [blame] | 219 | const struct switchdev_attr *attr, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 220 | struct switchdev_trans *trans) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 221 | { |
| 222 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 223 | struct net_device *lower_dev; |
| 224 | struct list_head *iter; |
| 225 | int err = -EOPNOTSUPP; |
| 226 | |
| 227 | if (ops && ops->switchdev_port_attr_set) |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 228 | return ops->switchdev_port_attr_set(dev, attr, trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 229 | |
| 230 | if (attr->flags & SWITCHDEV_F_NO_RECURSE) |
Scott Feldman | 464314e | 2015-10-08 19:23:18 -0700 | [diff] [blame] | 231 | goto done; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 232 | |
| 233 | /* Switch device port(s) may be stacked under |
| 234 | * bond/team/vlan dev, so recurse down to set attr on |
| 235 | * each port. |
| 236 | */ |
| 237 | |
| 238 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 239 | err = __switchdev_port_attr_set(lower_dev, attr, trans); |
Scott Feldman | 464314e | 2015-10-08 19:23:18 -0700 | [diff] [blame] | 240 | if (err == -EOPNOTSUPP && |
| 241 | attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP) |
| 242 | continue; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 243 | if (err) |
| 244 | break; |
| 245 | } |
| 246 | |
Scott Feldman | 464314e | 2015-10-08 19:23:18 -0700 | [diff] [blame] | 247 | done: |
| 248 | if (err == -EOPNOTSUPP && attr->flags & SWITCHDEV_F_SKIP_EOPNOTSUPP) |
| 249 | err = 0; |
| 250 | |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 251 | return err; |
| 252 | } |
| 253 | |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 254 | static int switchdev_port_attr_set_now(struct net_device *dev, |
| 255 | const struct switchdev_attr *attr) |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 256 | { |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 257 | struct switchdev_trans trans; |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 258 | int err; |
| 259 | |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 260 | switchdev_trans_init(&trans); |
| 261 | |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 262 | /* Phase I: prepare for attr set. Driver/device should fail |
| 263 | * here if there are going to be issues in the commit phase, |
| 264 | * such as lack of resources or support. The driver/device |
| 265 | * should reserve resources needed for the commit phase here, |
| 266 | * but should not commit the attr. |
| 267 | */ |
| 268 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 269 | trans.ph_prepare = true; |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 270 | err = __switchdev_port_attr_set(dev, attr, &trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 271 | if (err) { |
| 272 | /* Prepare phase failed: abort the transaction. Any |
| 273 | * resources reserved in the prepare phase are |
| 274 | * released. |
| 275 | */ |
| 276 | |
Jiri Pirko | 9f6467c | 2015-09-24 10:02:47 +0200 | [diff] [blame] | 277 | if (err != -EOPNOTSUPP) |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 278 | switchdev_trans_items_destroy(&trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 279 | |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | /* Phase II: commit attr set. This cannot fail as a fault |
| 284 | * of driver/device. If it does, it's a bug in the driver/device |
| 285 | * because the driver said everythings was OK in phase I. |
| 286 | */ |
| 287 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 288 | trans.ph_prepare = false; |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 289 | err = __switchdev_port_attr_set(dev, attr, &trans); |
Scott Feldman | e9fdaec | 2015-06-11 11:20:42 -0700 | [diff] [blame] | 290 | WARN(err, "%s: Commit of attribute (id=%d) failed.\n", |
| 291 | dev->name, attr->id); |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 292 | switchdev_trans_items_warn_destroy(dev, &trans); |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 293 | |
| 294 | return err; |
| 295 | } |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 296 | |
| 297 | static void switchdev_port_attr_set_deferred(struct net_device *dev, |
| 298 | const void *data) |
| 299 | { |
| 300 | const struct switchdev_attr *attr = data; |
| 301 | int err; |
| 302 | |
| 303 | err = switchdev_port_attr_set_now(dev, attr); |
| 304 | if (err && err != -EOPNOTSUPP) |
| 305 | netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n", |
| 306 | err, attr->id); |
| 307 | } |
| 308 | |
| 309 | static int switchdev_port_attr_set_defer(struct net_device *dev, |
| 310 | const struct switchdev_attr *attr) |
| 311 | { |
| 312 | return switchdev_deferred_enqueue(dev, attr, sizeof(*attr), |
| 313 | switchdev_port_attr_set_deferred); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * switchdev_port_attr_set - Set port attribute |
| 318 | * |
| 319 | * @dev: port device |
| 320 | * @attr: attribute to set |
| 321 | * |
| 322 | * Use a 2-phase prepare-commit transaction model to ensure |
| 323 | * system is not left in a partially updated state due to |
| 324 | * failure from driver/device. |
| 325 | * |
| 326 | * rtnl_lock must be held and must not be in atomic section, |
| 327 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 328 | */ |
| 329 | int switchdev_port_attr_set(struct net_device *dev, |
| 330 | const struct switchdev_attr *attr) |
| 331 | { |
| 332 | if (attr->flags & SWITCHDEV_F_DEFER) |
| 333 | return switchdev_port_attr_set_defer(dev, attr); |
| 334 | ASSERT_RTNL(); |
| 335 | return switchdev_port_attr_set_now(dev, attr); |
| 336 | } |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame] | 337 | EXPORT_SYMBOL_GPL(switchdev_port_attr_set); |
| 338 | |
Scott Feldman | 22c1f67 | 2015-05-12 23:03:51 -0700 | [diff] [blame] | 339 | static int __switchdev_port_obj_add(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 340 | const struct switchdev_obj *obj, |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 341 | struct switchdev_trans *trans) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 342 | { |
| 343 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 344 | struct net_device *lower_dev; |
| 345 | struct list_head *iter; |
| 346 | int err = -EOPNOTSUPP; |
| 347 | |
| 348 | if (ops && ops->switchdev_port_obj_add) |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 349 | return ops->switchdev_port_obj_add(dev, obj, trans); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 350 | |
| 351 | /* Switch device port(s) may be stacked under |
| 352 | * bond/team/vlan dev, so recurse down to add object on |
| 353 | * each port. |
| 354 | */ |
| 355 | |
| 356 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 357 | err = __switchdev_port_obj_add(lower_dev, obj, trans); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 358 | if (err) |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return err; |
| 363 | } |
| 364 | |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 365 | static int switchdev_port_obj_add_now(struct net_device *dev, |
| 366 | const struct switchdev_obj *obj) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 367 | { |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 368 | struct switchdev_trans trans; |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 369 | int err; |
| 370 | |
| 371 | ASSERT_RTNL(); |
| 372 | |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 373 | switchdev_trans_init(&trans); |
| 374 | |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 375 | /* Phase I: prepare for obj add. Driver/device should fail |
| 376 | * here if there are going to be issues in the commit phase, |
| 377 | * such as lack of resources or support. The driver/device |
| 378 | * should reserve resources needed for the commit phase here, |
| 379 | * but should not commit the obj. |
| 380 | */ |
| 381 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 382 | trans.ph_prepare = true; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 383 | err = __switchdev_port_obj_add(dev, obj, &trans); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 384 | if (err) { |
| 385 | /* Prepare phase failed: abort the transaction. Any |
| 386 | * resources reserved in the prepare phase are |
| 387 | * released. |
| 388 | */ |
| 389 | |
Jiri Pirko | 9f6467c | 2015-09-24 10:02:47 +0200 | [diff] [blame] | 390 | if (err != -EOPNOTSUPP) |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 391 | switchdev_trans_items_destroy(&trans); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 392 | |
| 393 | return err; |
| 394 | } |
| 395 | |
| 396 | /* Phase II: commit obj add. This cannot fail as a fault |
| 397 | * of driver/device. If it does, it's a bug in the driver/device |
| 398 | * because the driver said everythings was OK in phase I. |
| 399 | */ |
| 400 | |
Jiri Pirko | f623ab7 | 2015-09-24 10:02:49 +0200 | [diff] [blame] | 401 | trans.ph_prepare = false; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 402 | err = __switchdev_port_obj_add(dev, obj, &trans); |
| 403 | WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id); |
Jiri Pirko | 7ea6eb3 | 2015-09-24 10:02:41 +0200 | [diff] [blame] | 404 | switchdev_trans_items_warn_destroy(dev, &trans); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 405 | |
| 406 | return err; |
| 407 | } |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 408 | |
| 409 | static void switchdev_port_obj_add_deferred(struct net_device *dev, |
| 410 | const void *data) |
| 411 | { |
| 412 | const struct switchdev_obj *obj = data; |
| 413 | int err; |
| 414 | |
| 415 | err = switchdev_port_obj_add_now(dev, obj); |
| 416 | if (err && err != -EOPNOTSUPP) |
| 417 | netdev_err(dev, "failed (err=%d) to add object (id=%d)\n", |
| 418 | err, obj->id); |
| 419 | } |
| 420 | |
| 421 | static int switchdev_port_obj_add_defer(struct net_device *dev, |
| 422 | const struct switchdev_obj *obj) |
| 423 | { |
| 424 | return switchdev_deferred_enqueue(dev, obj, sizeof(*obj), |
| 425 | switchdev_port_obj_add_deferred); |
| 426 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 427 | |
| 428 | /** |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 429 | * switchdev_port_obj_add - Add port object |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 430 | * |
| 431 | * @dev: port device |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 432 | * @id: object ID |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 433 | * @obj: object to add |
| 434 | * |
| 435 | * Use a 2-phase prepare-commit transaction model to ensure |
| 436 | * system is not left in a partially updated state due to |
| 437 | * failure from driver/device. |
| 438 | * |
| 439 | * rtnl_lock must be held and must not be in atomic section, |
| 440 | * in case SWITCHDEV_F_DEFER flag is not set. |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 441 | */ |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 442 | int switchdev_port_obj_add(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 443 | const struct switchdev_obj *obj) |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 444 | { |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 445 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 446 | return switchdev_port_obj_add_defer(dev, obj); |
| 447 | ASSERT_RTNL(); |
| 448 | return switchdev_port_obj_add_now(dev, obj); |
| 449 | } |
| 450 | EXPORT_SYMBOL_GPL(switchdev_port_obj_add); |
| 451 | |
| 452 | static int switchdev_port_obj_del_now(struct net_device *dev, |
| 453 | const struct switchdev_obj *obj) |
| 454 | { |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 455 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 456 | struct net_device *lower_dev; |
| 457 | struct list_head *iter; |
| 458 | int err = -EOPNOTSUPP; |
| 459 | |
| 460 | if (ops && ops->switchdev_port_obj_del) |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 461 | return ops->switchdev_port_obj_del(dev, obj); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 462 | |
| 463 | /* Switch device port(s) may be stacked under |
| 464 | * bond/team/vlan dev, so recurse down to delete object on |
| 465 | * each port. |
| 466 | */ |
| 467 | |
| 468 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 469 | err = switchdev_port_obj_del_now(lower_dev, obj); |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 470 | if (err) |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | return err; |
| 475 | } |
Jiri Pirko | 4d429c5 | 2015-10-14 19:40:52 +0200 | [diff] [blame^] | 476 | |
| 477 | static void switchdev_port_obj_del_deferred(struct net_device *dev, |
| 478 | const void *data) |
| 479 | { |
| 480 | const struct switchdev_obj *obj = data; |
| 481 | int err; |
| 482 | |
| 483 | err = switchdev_port_obj_del_now(dev, obj); |
| 484 | if (err && err != -EOPNOTSUPP) |
| 485 | netdev_err(dev, "failed (err=%d) to del object (id=%d)\n", |
| 486 | err, obj->id); |
| 487 | } |
| 488 | |
| 489 | static int switchdev_port_obj_del_defer(struct net_device *dev, |
| 490 | const struct switchdev_obj *obj) |
| 491 | { |
| 492 | return switchdev_deferred_enqueue(dev, obj, sizeof(*obj), |
| 493 | switchdev_port_obj_del_deferred); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * switchdev_port_obj_del - Delete port object |
| 498 | * |
| 499 | * @dev: port device |
| 500 | * @id: object ID |
| 501 | * @obj: object to delete |
| 502 | * |
| 503 | * rtnl_lock must be held and must not be in atomic section, |
| 504 | * in case SWITCHDEV_F_DEFER flag is not set. |
| 505 | */ |
| 506 | int switchdev_port_obj_del(struct net_device *dev, |
| 507 | const struct switchdev_obj *obj) |
| 508 | { |
| 509 | if (obj->flags & SWITCHDEV_F_DEFER) |
| 510 | return switchdev_port_obj_del_defer(dev, obj); |
| 511 | ASSERT_RTNL(); |
| 512 | return switchdev_port_obj_del_now(dev, obj); |
| 513 | } |
Scott Feldman | 491d0f1 | 2015-05-10 09:47:52 -0700 | [diff] [blame] | 514 | EXPORT_SYMBOL_GPL(switchdev_port_obj_del); |
| 515 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 516 | /** |
| 517 | * switchdev_port_obj_dump - Dump port objects |
| 518 | * |
| 519 | * @dev: port device |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 520 | * @id: object ID |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 521 | * @obj: object to dump |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 522 | * @cb: function to call with a filled object |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 523 | */ |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 524 | int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 525 | switchdev_obj_dump_cb_t *cb) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 526 | { |
| 527 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 528 | struct net_device *lower_dev; |
| 529 | struct list_head *iter; |
| 530 | int err = -EOPNOTSUPP; |
| 531 | |
| 532 | if (ops && ops->switchdev_port_obj_dump) |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 533 | return ops->switchdev_port_obj_dump(dev, obj, cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 534 | |
| 535 | /* Switch device port(s) may be stacked under |
| 536 | * bond/team/vlan dev, so recurse down to dump objects on |
| 537 | * first port at bottom of stack. |
| 538 | */ |
| 539 | |
| 540 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 541 | err = switchdev_port_obj_dump(lower_dev, obj, cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | |
| 545 | return err; |
| 546 | } |
| 547 | EXPORT_SYMBOL_GPL(switchdev_port_obj_dump); |
| 548 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 549 | static DEFINE_MUTEX(switchdev_mutex); |
| 550 | static RAW_NOTIFIER_HEAD(switchdev_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 551 | |
| 552 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 553 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 554 | * @nb: notifier_block |
| 555 | * |
| 556 | * Register switch device notifier. This should be used by code |
| 557 | * which needs to monitor events happening in particular device. |
| 558 | * Return values are same as for atomic_notifier_chain_register(). |
| 559 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 560 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 561 | { |
| 562 | int err; |
| 563 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 564 | mutex_lock(&switchdev_mutex); |
| 565 | err = raw_notifier_chain_register(&switchdev_notif_chain, nb); |
| 566 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 567 | return err; |
| 568 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 569 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 570 | |
| 571 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 572 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 573 | * @nb: notifier_block |
| 574 | * |
| 575 | * Unregister switch device notifier. |
| 576 | * Return values are same as for atomic_notifier_chain_unregister(). |
| 577 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 578 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 579 | { |
| 580 | int err; |
| 581 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 582 | mutex_lock(&switchdev_mutex); |
| 583 | err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb); |
| 584 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 585 | return err; |
| 586 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 587 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 588 | |
| 589 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 590 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 591 | * @val: value passed unmodified to notifier function |
| 592 | * @dev: port device |
| 593 | * @info: notifier information data |
| 594 | * |
| 595 | * Call all network notifier blocks. This should be called by driver |
| 596 | * when it needs to propagate hardware event. |
| 597 | * Return values are same as for atomic_notifier_call_chain(). |
| 598 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 599 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
| 600 | struct switchdev_notifier_info *info) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 601 | { |
| 602 | int err; |
| 603 | |
| 604 | info->dev = dev; |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 605 | mutex_lock(&switchdev_mutex); |
| 606 | err = raw_notifier_call_chain(&switchdev_notif_chain, val, info); |
| 607 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 608 | return err; |
| 609 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 610 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 611 | |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 612 | struct switchdev_vlan_dump { |
Jiri Pirko | 8f24f30 | 2015-10-01 11:03:43 +0200 | [diff] [blame] | 613 | struct switchdev_obj_port_vlan vlan; |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 614 | struct sk_buff *skb; |
| 615 | u32 filter_mask; |
| 616 | u16 flags; |
| 617 | u16 begin; |
| 618 | u16 end; |
| 619 | }; |
| 620 | |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 621 | static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump) |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 622 | { |
| 623 | struct bridge_vlan_info vinfo; |
| 624 | |
| 625 | vinfo.flags = dump->flags; |
| 626 | |
| 627 | if (dump->begin == 0 && dump->end == 0) { |
| 628 | return 0; |
| 629 | } else if (dump->begin == dump->end) { |
| 630 | vinfo.vid = dump->begin; |
| 631 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 632 | sizeof(vinfo), &vinfo)) |
| 633 | return -EMSGSIZE; |
| 634 | } else { |
| 635 | vinfo.vid = dump->begin; |
| 636 | vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 637 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 638 | sizeof(vinfo), &vinfo)) |
| 639 | return -EMSGSIZE; |
| 640 | vinfo.vid = dump->end; |
| 641 | vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 642 | vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END; |
| 643 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 644 | sizeof(vinfo), &vinfo)) |
| 645 | return -EMSGSIZE; |
| 646 | } |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 651 | static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj) |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 652 | { |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 653 | struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 654 | struct switchdev_vlan_dump *dump = |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 655 | container_of(vlan, struct switchdev_vlan_dump, vlan); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 656 | int err = 0; |
| 657 | |
| 658 | if (vlan->vid_begin > vlan->vid_end) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | if (dump->filter_mask & RTEXT_FILTER_BRVLAN) { |
| 662 | dump->flags = vlan->flags; |
| 663 | for (dump->begin = dump->end = vlan->vid_begin; |
| 664 | dump->begin <= vlan->vid_end; |
| 665 | dump->begin++, dump->end++) { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 666 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 667 | if (err) |
| 668 | return err; |
| 669 | } |
| 670 | } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) { |
| 671 | if (dump->begin > vlan->vid_begin && |
| 672 | dump->begin >= vlan->vid_end) { |
| 673 | if ((dump->begin - 1) == vlan->vid_end && |
| 674 | dump->flags == vlan->flags) { |
| 675 | /* prepend */ |
| 676 | dump->begin = vlan->vid_begin; |
| 677 | } else { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 678 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 679 | dump->flags = vlan->flags; |
| 680 | dump->begin = vlan->vid_begin; |
| 681 | dump->end = vlan->vid_end; |
| 682 | } |
| 683 | } else if (dump->end <= vlan->vid_begin && |
| 684 | dump->end < vlan->vid_end) { |
| 685 | if ((dump->end + 1) == vlan->vid_begin && |
| 686 | dump->flags == vlan->flags) { |
| 687 | /* append */ |
| 688 | dump->end = vlan->vid_end; |
| 689 | } else { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 690 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 691 | dump->flags = vlan->flags; |
| 692 | dump->begin = vlan->vid_begin; |
| 693 | dump->end = vlan->vid_end; |
| 694 | } |
| 695 | } else { |
| 696 | err = -EINVAL; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return err; |
| 701 | } |
| 702 | |
| 703 | static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev, |
| 704 | u32 filter_mask) |
| 705 | { |
| 706 | struct switchdev_vlan_dump dump = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 707 | .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 708 | .skb = skb, |
| 709 | .filter_mask = filter_mask, |
| 710 | }; |
| 711 | int err = 0; |
| 712 | |
| 713 | if ((filter_mask & RTEXT_FILTER_BRVLAN) || |
| 714 | (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 715 | err = switchdev_port_obj_dump(dev, &dump.vlan.obj, |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 716 | switchdev_port_vlan_dump_cb); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 717 | if (err) |
| 718 | goto err_out; |
| 719 | if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) |
| 720 | /* last one */ |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 721 | err = switchdev_port_vlan_dump_put(&dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | err_out: |
| 725 | return err == -EOPNOTSUPP ? 0 : err; |
| 726 | } |
| 727 | |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 728 | /** |
| 729 | * switchdev_port_bridge_getlink - Get bridge port attributes |
| 730 | * |
| 731 | * @dev: port device |
| 732 | * |
| 733 | * Called for SELF on rtnl_bridge_getlink to get bridge port |
| 734 | * attributes. |
| 735 | */ |
| 736 | int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
| 737 | struct net_device *dev, u32 filter_mask, |
| 738 | int nlflags) |
| 739 | { |
| 740 | struct switchdev_attr attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 741 | .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS, |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 742 | }; |
| 743 | u16 mode = BRIDGE_MODE_UNDEF; |
| 744 | u32 mask = BR_LEARNING | BR_LEARNING_SYNC; |
| 745 | int err; |
| 746 | |
| 747 | err = switchdev_port_attr_get(dev, &attr); |
Vivien Didelot | 5c8079d | 2015-06-23 10:26:04 -0400 | [diff] [blame] | 748 | if (err && err != -EOPNOTSUPP) |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 749 | return err; |
| 750 | |
| 751 | return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 752 | attr.u.brport_flags, mask, nlflags, |
| 753 | filter_mask, switchdev_port_vlan_fill); |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 754 | } |
| 755 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink); |
| 756 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 757 | static int switchdev_port_br_setflag(struct net_device *dev, |
| 758 | struct nlattr *nlattr, |
| 759 | unsigned long brport_flag) |
| 760 | { |
| 761 | struct switchdev_attr attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 762 | .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS, |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 763 | }; |
| 764 | u8 flag = nla_get_u8(nlattr); |
| 765 | int err; |
| 766 | |
| 767 | err = switchdev_port_attr_get(dev, &attr); |
| 768 | if (err) |
| 769 | return err; |
| 770 | |
| 771 | if (flag) |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 772 | attr.u.brport_flags |= brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 773 | else |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 774 | attr.u.brport_flags &= ~brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 775 | |
| 776 | return switchdev_port_attr_set(dev, &attr); |
| 777 | } |
| 778 | |
| 779 | static const struct nla_policy |
| 780 | switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = { |
| 781 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, |
| 782 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, |
| 783 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, |
| 784 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, |
| 785 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
| 786 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
| 787 | [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 }, |
| 788 | [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, |
| 789 | [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 }, |
| 790 | [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, |
| 791 | }; |
| 792 | |
| 793 | static int switchdev_port_br_setlink_protinfo(struct net_device *dev, |
| 794 | struct nlattr *protinfo) |
| 795 | { |
| 796 | struct nlattr *attr; |
| 797 | int rem; |
| 798 | int err; |
| 799 | |
| 800 | err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX, |
| 801 | switchdev_port_bridge_policy); |
| 802 | if (err) |
| 803 | return err; |
| 804 | |
| 805 | nla_for_each_nested(attr, protinfo, rem) { |
| 806 | switch (nla_type(attr)) { |
| 807 | case IFLA_BRPORT_LEARNING: |
| 808 | err = switchdev_port_br_setflag(dev, attr, |
| 809 | BR_LEARNING); |
| 810 | break; |
| 811 | case IFLA_BRPORT_LEARNING_SYNC: |
| 812 | err = switchdev_port_br_setflag(dev, attr, |
| 813 | BR_LEARNING_SYNC); |
| 814 | break; |
| 815 | default: |
| 816 | err = -EOPNOTSUPP; |
| 817 | break; |
| 818 | } |
| 819 | if (err) |
| 820 | return err; |
| 821 | } |
| 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | static int switchdev_port_br_afspec(struct net_device *dev, |
| 827 | struct nlattr *afspec, |
| 828 | int (*f)(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 829 | const struct switchdev_obj *obj)) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 830 | { |
| 831 | struct nlattr *attr; |
| 832 | struct bridge_vlan_info *vinfo; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 833 | struct switchdev_obj_port_vlan vlan = { |
| 834 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 835 | }; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 836 | int rem; |
| 837 | int err; |
| 838 | |
| 839 | nla_for_each_nested(attr, afspec, rem) { |
| 840 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) |
| 841 | continue; |
| 842 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) |
| 843 | return -EINVAL; |
| 844 | vinfo = nla_data(attr); |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 845 | vlan.flags = vinfo->flags; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 846 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 847 | if (vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 848 | return -EINVAL; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 849 | vlan.vid_begin = vinfo->vid; |
Nikolay Aleksandrov | cc02aa8 | 2015-10-12 14:01:39 +0200 | [diff] [blame] | 850 | /* don't allow range of pvids */ |
| 851 | if (vlan.flags & BRIDGE_VLAN_INFO_PVID) |
| 852 | return -EINVAL; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 853 | } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 854 | if (!vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 855 | return -EINVAL; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 856 | vlan.vid_end = vinfo->vid; |
| 857 | if (vlan.vid_end <= vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 858 | return -EINVAL; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 859 | err = f(dev, &vlan.obj); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 860 | if (err) |
| 861 | return err; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 862 | memset(&vlan, 0, sizeof(vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 863 | } else { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 864 | if (vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 865 | return -EINVAL; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 866 | vlan.vid_begin = vinfo->vid; |
| 867 | vlan.vid_end = vinfo->vid; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 868 | err = f(dev, &vlan.obj); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 869 | if (err) |
| 870 | return err; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 871 | memset(&vlan, 0, sizeof(vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 878 | /** |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 879 | * switchdev_port_bridge_setlink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 880 | * |
| 881 | * @dev: port device |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 882 | * @nlh: netlink header |
| 883 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 884 | * |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 885 | * Called for SELF on rtnl_bridge_setlink to set bridge port |
| 886 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 887 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 888 | int switchdev_port_bridge_setlink(struct net_device *dev, |
| 889 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 890 | { |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 891 | struct nlattr *protinfo; |
| 892 | struct nlattr *afspec; |
| 893 | int err = 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 894 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 895 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 896 | IFLA_PROTINFO); |
| 897 | if (protinfo) { |
| 898 | err = switchdev_port_br_setlink_protinfo(dev, protinfo); |
| 899 | if (err) |
| 900 | return err; |
| 901 | } |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 902 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 903 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 904 | IFLA_AF_SPEC); |
| 905 | if (afspec) |
| 906 | err = switchdev_port_br_afspec(dev, afspec, |
| 907 | switchdev_port_obj_add); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 908 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 909 | return err; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 910 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 911 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 912 | |
| 913 | /** |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 914 | * switchdev_port_bridge_dellink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 915 | * |
| 916 | * @dev: port device |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 917 | * @nlh: netlink header |
| 918 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 919 | * |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 920 | * Called for SELF on rtnl_bridge_dellink to set bridge port |
| 921 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 922 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 923 | int switchdev_port_bridge_dellink(struct net_device *dev, |
| 924 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 925 | { |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 926 | struct nlattr *afspec; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 927 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 928 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 929 | IFLA_AF_SPEC); |
| 930 | if (afspec) |
| 931 | return switchdev_port_br_afspec(dev, afspec, |
| 932 | switchdev_port_obj_del); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 933 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 934 | return 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 935 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 936 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 937 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 938 | /** |
| 939 | * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port |
| 940 | * |
| 941 | * @ndmsg: netlink hdr |
| 942 | * @nlattr: netlink attributes |
| 943 | * @dev: port device |
| 944 | * @addr: MAC address to add |
| 945 | * @vid: VLAN to add |
| 946 | * |
| 947 | * Add FDB entry to switch device. |
| 948 | */ |
| 949 | int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 950 | struct net_device *dev, const unsigned char *addr, |
| 951 | u16 vid, u16 nlm_flags) |
| 952 | { |
Jiri Pirko | 52ba57c | 2015-10-01 11:03:44 +0200 | [diff] [blame] | 953 | struct switchdev_obj_port_fdb fdb = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 954 | .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 955 | .vid = vid, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 956 | }; |
| 957 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 958 | ether_addr_copy(fdb.addr, addr); |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 959 | return switchdev_port_obj_add(dev, &fdb.obj); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 960 | } |
| 961 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_add); |
| 962 | |
| 963 | /** |
| 964 | * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port |
| 965 | * |
| 966 | * @ndmsg: netlink hdr |
| 967 | * @nlattr: netlink attributes |
| 968 | * @dev: port device |
| 969 | * @addr: MAC address to delete |
| 970 | * @vid: VLAN to delete |
| 971 | * |
| 972 | * Delete FDB entry from switch device. |
| 973 | */ |
| 974 | int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
| 975 | struct net_device *dev, const unsigned char *addr, |
| 976 | u16 vid) |
| 977 | { |
Jiri Pirko | 52ba57c | 2015-10-01 11:03:44 +0200 | [diff] [blame] | 978 | struct switchdev_obj_port_fdb fdb = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 979 | .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 980 | .vid = vid, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 981 | }; |
| 982 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 983 | ether_addr_copy(fdb.addr, addr); |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 984 | return switchdev_port_obj_del(dev, &fdb.obj); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 985 | } |
| 986 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_del); |
| 987 | |
| 988 | struct switchdev_fdb_dump { |
Jiri Pirko | 52ba57c | 2015-10-01 11:03:44 +0200 | [diff] [blame] | 989 | struct switchdev_obj_port_fdb fdb; |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 990 | struct net_device *dev; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 991 | struct sk_buff *skb; |
| 992 | struct netlink_callback *cb; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 993 | int idx; |
| 994 | }; |
| 995 | |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 996 | static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 997 | { |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 998 | struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 999 | struct switchdev_fdb_dump *dump = |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1000 | container_of(fdb, struct switchdev_fdb_dump, fdb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1001 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 1002 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 1003 | struct nlmsghdr *nlh; |
| 1004 | struct ndmsg *ndm; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1005 | |
| 1006 | if (dump->idx < dump->cb->args[0]) |
| 1007 | goto skip; |
| 1008 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1009 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 1010 | sizeof(*ndm), NLM_F_MULTI); |
| 1011 | if (!nlh) |
| 1012 | return -EMSGSIZE; |
| 1013 | |
| 1014 | ndm = nlmsg_data(nlh); |
| 1015 | ndm->ndm_family = AF_BRIDGE; |
| 1016 | ndm->ndm_pad1 = 0; |
| 1017 | ndm->ndm_pad2 = 0; |
| 1018 | ndm->ndm_flags = NTF_SELF; |
| 1019 | ndm->ndm_type = 0; |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 1020 | ndm->ndm_ifindex = dump->dev->ifindex; |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1021 | ndm->ndm_state = fdb->ndm_state; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1022 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1023 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr)) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1024 | goto nla_put_failure; |
| 1025 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1026 | if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid)) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1027 | goto nla_put_failure; |
| 1028 | |
| 1029 | nlmsg_end(dump->skb, nlh); |
| 1030 | |
| 1031 | skip: |
| 1032 | dump->idx++; |
| 1033 | return 0; |
| 1034 | |
| 1035 | nla_put_failure: |
| 1036 | nlmsg_cancel(dump->skb, nlh); |
| 1037 | return -EMSGSIZE; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries |
| 1042 | * |
| 1043 | * @skb: netlink skb |
| 1044 | * @cb: netlink callback |
| 1045 | * @dev: port device |
| 1046 | * @filter_dev: filter device |
| 1047 | * @idx: |
| 1048 | * |
| 1049 | * Delete FDB entry from switch device. |
| 1050 | */ |
| 1051 | int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 1052 | struct net_device *dev, |
| 1053 | struct net_device *filter_dev, int idx) |
| 1054 | { |
| 1055 | struct switchdev_fdb_dump dump = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1056 | .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB, |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 1057 | .dev = dev, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1058 | .skb = skb, |
| 1059 | .cb = cb, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1060 | .idx = idx, |
| 1061 | }; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1062 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1063 | switchdev_port_obj_dump(dev, &dump.fdb.obj, switchdev_port_fdb_dump_cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1064 | return dump.idx; |
| 1065 | } |
| 1066 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump); |
| 1067 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1068 | static struct net_device *switchdev_get_lowest_dev(struct net_device *dev) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1069 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 1070 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1071 | struct net_device *lower_dev; |
| 1072 | struct net_device *port_dev; |
| 1073 | struct list_head *iter; |
| 1074 | |
| 1075 | /* Recusively search down until we find a sw port dev. |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1076 | * (A sw port dev supports switchdev_port_attr_get). |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1077 | */ |
| 1078 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1079 | if (ops && ops->switchdev_port_attr_get) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1080 | return dev; |
| 1081 | |
| 1082 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1083 | port_dev = switchdev_get_lowest_dev(lower_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1084 | if (port_dev) |
| 1085 | return port_dev; |
| 1086 | } |
| 1087 | |
| 1088 | return NULL; |
| 1089 | } |
| 1090 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1091 | static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1092 | { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1093 | struct switchdev_attr attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1094 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1095 | }; |
| 1096 | struct switchdev_attr prev_attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1097 | struct net_device *dev = NULL; |
| 1098 | int nhsel; |
| 1099 | |
| 1100 | /* For this route, all nexthop devs must be on the same switch. */ |
| 1101 | |
| 1102 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 1103 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
| 1104 | |
| 1105 | if (!nh->nh_dev) |
| 1106 | return NULL; |
| 1107 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1108 | dev = switchdev_get_lowest_dev(nh->nh_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1109 | if (!dev) |
| 1110 | return NULL; |
| 1111 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1112 | if (switchdev_port_attr_get(dev, &attr)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1113 | return NULL; |
| 1114 | |
Scott Feldman | d754f98 | 2015-07-18 18:24:49 -0700 | [diff] [blame] | 1115 | if (nhsel > 0 && |
| 1116 | !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1117 | return NULL; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1118 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1119 | prev_attr = attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | return dev; |
| 1123 | } |
| 1124 | |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1125 | /** |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 1126 | * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1127 | * |
| 1128 | * @dst: route's IPv4 destination address |
| 1129 | * @dst_len: destination address length (prefix length) |
| 1130 | * @fi: route FIB info structure |
| 1131 | * @tos: route TOS |
| 1132 | * @type: route type |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 1133 | * @nlflags: netlink flags passed in (NLM_F_*) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1134 | * @tb_id: route table ID |
| 1135 | * |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 1136 | * Add/modify switch IPv4 route entry. |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1137 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1138 | int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi, |
| 1139 | u8 tos, u8 type, u32 nlflags, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1140 | { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1141 | struct switchdev_obj_ipv4_fib ipv4_fib = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1142 | .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1143 | .dst = dst, |
| 1144 | .dst_len = dst_len, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1145 | .tos = tos, |
| 1146 | .type = type, |
| 1147 | .nlflags = nlflags, |
| 1148 | .tb_id = tb_id, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1149 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1150 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1151 | int err = 0; |
| 1152 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 1153 | memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi)); |
| 1154 | |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1155 | /* Don't offload route if using custom ip rules or if |
| 1156 | * IPv4 FIB offloading has been disabled completely. |
| 1157 | */ |
| 1158 | |
Scott Feldman | e1315db | 2015-03-06 01:14:36 -0800 | [diff] [blame] | 1159 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 1160 | if (fi->fib_net->ipv4.fib_has_custom_rules) |
| 1161 | return 0; |
| 1162 | #endif |
| 1163 | |
| 1164 | if (fi->fib_net->ipv4.fib_offload_disabled) |
Scott Feldman | 104616e | 2015-03-05 21:21:16 -0800 | [diff] [blame] | 1165 | return 0; |
| 1166 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1167 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1168 | if (!dev) |
| 1169 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1170 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1171 | err = switchdev_port_obj_add(dev, &ipv4_fib.obj); |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1172 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 1173 | fi->fib_flags |= RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1174 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 1175 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1176 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1177 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add); |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1178 | |
| 1179 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1180 | * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1181 | * |
| 1182 | * @dst: route's IPv4 destination address |
| 1183 | * @dst_len: destination address length (prefix length) |
| 1184 | * @fi: route FIB info structure |
| 1185 | * @tos: route TOS |
| 1186 | * @type: route type |
| 1187 | * @tb_id: route table ID |
| 1188 | * |
| 1189 | * Delete IPv4 route entry from switch device. |
| 1190 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1191 | int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi, |
| 1192 | u8 tos, u8 type, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1193 | { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1194 | struct switchdev_obj_ipv4_fib ipv4_fib = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1195 | .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1196 | .dst = dst, |
| 1197 | .dst_len = dst_len, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1198 | .tos = tos, |
| 1199 | .type = type, |
| 1200 | .nlflags = 0, |
| 1201 | .tb_id = tb_id, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1202 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1203 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1204 | int err = 0; |
| 1205 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 1206 | memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi)); |
| 1207 | |
Roopa Prabhu | eea3994 | 2015-05-13 21:17:41 -0700 | [diff] [blame] | 1208 | if (!(fi->fib_flags & RTNH_F_OFFLOAD)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1209 | return 0; |
| 1210 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1211 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1212 | if (!dev) |
| 1213 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1214 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1215 | err = switchdev_port_obj_del(dev, &ipv4_fib.obj); |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1216 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 1217 | fi->fib_flags &= ~RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1218 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 1219 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1220 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1221 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del); |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1222 | |
| 1223 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1224 | * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1225 | * |
| 1226 | * @fi: route FIB info structure |
| 1227 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1228 | void switchdev_fib_ipv4_abort(struct fib_info *fi) |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1229 | { |
| 1230 | /* There was a problem installing this route to the offload |
| 1231 | * device. For now, until we come up with more refined |
| 1232 | * policy handling, abruptly end IPv4 fib offloading for |
| 1233 | * for entire net by flushing offload device(s) of all |
| 1234 | * IPv4 routes, and mark IPv4 fib offloading broken from |
| 1235 | * this point forward. |
| 1236 | */ |
| 1237 | |
| 1238 | fib_flush_external(fi->fib_net); |
| 1239 | fi->fib_net->ipv4.fib_offload_disabled = true; |
| 1240 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1241 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort); |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1242 | |
| 1243 | static bool switchdev_port_same_parent_id(struct net_device *a, |
| 1244 | struct net_device *b) |
| 1245 | { |
| 1246 | struct switchdev_attr a_attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1247 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1248 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1249 | }; |
| 1250 | struct switchdev_attr b_attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1251 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1252 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1253 | }; |
| 1254 | |
| 1255 | if (switchdev_port_attr_get(a, &a_attr) || |
| 1256 | switchdev_port_attr_get(b, &b_attr)) |
| 1257 | return false; |
| 1258 | |
| 1259 | return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid); |
| 1260 | } |
| 1261 | |
| 1262 | static u32 switchdev_port_fwd_mark_get(struct net_device *dev, |
| 1263 | struct net_device *group_dev) |
| 1264 | { |
| 1265 | struct net_device *lower_dev; |
| 1266 | struct list_head *iter; |
| 1267 | |
| 1268 | netdev_for_each_lower_dev(group_dev, lower_dev, iter) { |
| 1269 | if (lower_dev == dev) |
| 1270 | continue; |
| 1271 | if (switchdev_port_same_parent_id(dev, lower_dev)) |
| 1272 | return lower_dev->offload_fwd_mark; |
| 1273 | return switchdev_port_fwd_mark_get(dev, lower_dev); |
| 1274 | } |
| 1275 | |
| 1276 | return dev->ifindex; |
| 1277 | } |
| 1278 | |
| 1279 | static void switchdev_port_fwd_mark_reset(struct net_device *group_dev, |
| 1280 | u32 old_mark, u32 *reset_mark) |
| 1281 | { |
| 1282 | struct net_device *lower_dev; |
| 1283 | struct list_head *iter; |
| 1284 | |
| 1285 | netdev_for_each_lower_dev(group_dev, lower_dev, iter) { |
| 1286 | if (lower_dev->offload_fwd_mark == old_mark) { |
| 1287 | if (!*reset_mark) |
| 1288 | *reset_mark = lower_dev->ifindex; |
| 1289 | lower_dev->offload_fwd_mark = *reset_mark; |
| 1290 | } |
| 1291 | switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * switchdev_port_fwd_mark_set - Set port offload forwarding mark |
| 1297 | * |
| 1298 | * @dev: port device |
| 1299 | * @group_dev: containing device |
| 1300 | * @joining: true if dev is joining group; false if leaving group |
| 1301 | * |
| 1302 | * An ungrouped port's offload mark is just its ifindex. A grouped |
| 1303 | * port's (member of a bridge, for example) offload mark is the ifindex |
| 1304 | * of one of the ports in the group with the same parent (switch) ID. |
| 1305 | * Ports on the same device in the same group will have the same mark. |
| 1306 | * |
| 1307 | * Example: |
| 1308 | * |
| 1309 | * br0 ifindex=9 |
| 1310 | * sw1p1 ifindex=2 mark=2 |
| 1311 | * sw1p2 ifindex=3 mark=2 |
| 1312 | * sw2p1 ifindex=4 mark=5 |
| 1313 | * sw2p2 ifindex=5 mark=5 |
| 1314 | * |
| 1315 | * If sw2p2 leaves the bridge, we'll have: |
| 1316 | * |
| 1317 | * br0 ifindex=9 |
| 1318 | * sw1p1 ifindex=2 mark=2 |
| 1319 | * sw1p2 ifindex=3 mark=2 |
| 1320 | * sw2p1 ifindex=4 mark=4 |
| 1321 | * sw2p2 ifindex=5 mark=5 |
| 1322 | */ |
| 1323 | void switchdev_port_fwd_mark_set(struct net_device *dev, |
| 1324 | struct net_device *group_dev, |
| 1325 | bool joining) |
| 1326 | { |
| 1327 | u32 mark = dev->ifindex; |
| 1328 | u32 reset_mark = 0; |
| 1329 | |
| 1330 | if (group_dev && joining) { |
| 1331 | mark = switchdev_port_fwd_mark_get(dev, group_dev); |
| 1332 | } else if (group_dev && !joining) { |
| 1333 | if (dev->offload_fwd_mark == mark) |
| 1334 | /* Ohoh, this port was the mark reference port, |
| 1335 | * but it's leaving the group, so reset the |
| 1336 | * mark for the remaining ports in the group. |
| 1337 | */ |
| 1338 | switchdev_port_fwd_mark_reset(group_dev, mark, |
| 1339 | &reset_mark); |
| 1340 | } |
| 1341 | |
| 1342 | dev->offload_fwd_mark = mark; |
| 1343 | } |
| 1344 | EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set); |