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