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 |
Jiri Pirko | 771acac | 2015-10-14 19:40:55 +0200 | [diff] [blame^] | 523 | * |
| 524 | * rtnl_lock must be held. |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 525 | */ |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 526 | 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] | 527 | switchdev_obj_dump_cb_t *cb) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 528 | { |
| 529 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 530 | struct net_device *lower_dev; |
| 531 | struct list_head *iter; |
| 532 | int err = -EOPNOTSUPP; |
| 533 | |
Jiri Pirko | 771acac | 2015-10-14 19:40:55 +0200 | [diff] [blame^] | 534 | ASSERT_RTNL(); |
| 535 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 536 | if (ops && ops->switchdev_port_obj_dump) |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 537 | return ops->switchdev_port_obj_dump(dev, obj, cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 538 | |
| 539 | /* Switch device port(s) may be stacked under |
| 540 | * bond/team/vlan dev, so recurse down to dump objects on |
| 541 | * first port at bottom of stack. |
| 542 | */ |
| 543 | |
| 544 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 545 | err = switchdev_port_obj_dump(lower_dev, obj, cb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | |
| 549 | return err; |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(switchdev_port_obj_dump); |
| 552 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 553 | static DEFINE_MUTEX(switchdev_mutex); |
| 554 | static RAW_NOTIFIER_HEAD(switchdev_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 555 | |
| 556 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 557 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 558 | * @nb: notifier_block |
| 559 | * |
| 560 | * Register switch device notifier. This should be used by code |
| 561 | * which needs to monitor events happening in particular device. |
| 562 | * Return values are same as for atomic_notifier_chain_register(). |
| 563 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 564 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 565 | { |
| 566 | int err; |
| 567 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 568 | mutex_lock(&switchdev_mutex); |
| 569 | err = raw_notifier_chain_register(&switchdev_notif_chain, nb); |
| 570 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 571 | return err; |
| 572 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 573 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 574 | |
| 575 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 576 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 577 | * @nb: notifier_block |
| 578 | * |
| 579 | * Unregister switch device notifier. |
| 580 | * Return values are same as for atomic_notifier_chain_unregister(). |
| 581 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 582 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 583 | { |
| 584 | int err; |
| 585 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 586 | mutex_lock(&switchdev_mutex); |
| 587 | err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb); |
| 588 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 589 | return err; |
| 590 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 591 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 592 | |
| 593 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 594 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 595 | * @val: value passed unmodified to notifier function |
| 596 | * @dev: port device |
| 597 | * @info: notifier information data |
| 598 | * |
| 599 | * Call all network notifier blocks. This should be called by driver |
| 600 | * when it needs to propagate hardware event. |
| 601 | * Return values are same as for atomic_notifier_call_chain(). |
| 602 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 603 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
| 604 | struct switchdev_notifier_info *info) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 605 | { |
| 606 | int err; |
| 607 | |
| 608 | info->dev = dev; |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 609 | mutex_lock(&switchdev_mutex); |
| 610 | err = raw_notifier_call_chain(&switchdev_notif_chain, val, info); |
| 611 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 612 | return err; |
| 613 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 614 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 615 | |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 616 | struct switchdev_vlan_dump { |
Jiri Pirko | 8f24f30 | 2015-10-01 11:03:43 +0200 | [diff] [blame] | 617 | struct switchdev_obj_port_vlan vlan; |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 618 | struct sk_buff *skb; |
| 619 | u32 filter_mask; |
| 620 | u16 flags; |
| 621 | u16 begin; |
| 622 | u16 end; |
| 623 | }; |
| 624 | |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 625 | static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump) |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 626 | { |
| 627 | struct bridge_vlan_info vinfo; |
| 628 | |
| 629 | vinfo.flags = dump->flags; |
| 630 | |
| 631 | if (dump->begin == 0 && dump->end == 0) { |
| 632 | return 0; |
| 633 | } else if (dump->begin == dump->end) { |
| 634 | vinfo.vid = dump->begin; |
| 635 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 636 | sizeof(vinfo), &vinfo)) |
| 637 | return -EMSGSIZE; |
| 638 | } else { |
| 639 | vinfo.vid = dump->begin; |
| 640 | vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 641 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 642 | sizeof(vinfo), &vinfo)) |
| 643 | return -EMSGSIZE; |
| 644 | vinfo.vid = dump->end; |
| 645 | vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN; |
| 646 | vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END; |
| 647 | if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO, |
| 648 | sizeof(vinfo), &vinfo)) |
| 649 | return -EMSGSIZE; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 655 | static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj) |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 656 | { |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 657 | struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 658 | struct switchdev_vlan_dump *dump = |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 659 | container_of(vlan, struct switchdev_vlan_dump, vlan); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 660 | int err = 0; |
| 661 | |
| 662 | if (vlan->vid_begin > vlan->vid_end) |
| 663 | return -EINVAL; |
| 664 | |
| 665 | if (dump->filter_mask & RTEXT_FILTER_BRVLAN) { |
| 666 | dump->flags = vlan->flags; |
| 667 | for (dump->begin = dump->end = vlan->vid_begin; |
| 668 | dump->begin <= vlan->vid_end; |
| 669 | dump->begin++, dump->end++) { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 670 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 671 | if (err) |
| 672 | return err; |
| 673 | } |
| 674 | } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) { |
| 675 | if (dump->begin > vlan->vid_begin && |
| 676 | dump->begin >= vlan->vid_end) { |
| 677 | if ((dump->begin - 1) == vlan->vid_end && |
| 678 | dump->flags == vlan->flags) { |
| 679 | /* prepend */ |
| 680 | dump->begin = vlan->vid_begin; |
| 681 | } else { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 682 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 683 | dump->flags = vlan->flags; |
| 684 | dump->begin = vlan->vid_begin; |
| 685 | dump->end = vlan->vid_end; |
| 686 | } |
| 687 | } else if (dump->end <= vlan->vid_begin && |
| 688 | dump->end < vlan->vid_end) { |
| 689 | if ((dump->end + 1) == vlan->vid_begin && |
| 690 | dump->flags == vlan->flags) { |
| 691 | /* append */ |
| 692 | dump->end = vlan->vid_end; |
| 693 | } else { |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 694 | err = switchdev_port_vlan_dump_put(dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 695 | dump->flags = vlan->flags; |
| 696 | dump->begin = vlan->vid_begin; |
| 697 | dump->end = vlan->vid_end; |
| 698 | } |
| 699 | } else { |
| 700 | err = -EINVAL; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | return err; |
| 705 | } |
| 706 | |
| 707 | static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev, |
| 708 | u32 filter_mask) |
| 709 | { |
| 710 | struct switchdev_vlan_dump dump = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 711 | .vlan.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 712 | .skb = skb, |
| 713 | .filter_mask = filter_mask, |
| 714 | }; |
| 715 | int err = 0; |
| 716 | |
| 717 | if ((filter_mask & RTEXT_FILTER_BRVLAN) || |
| 718 | (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 719 | err = switchdev_port_obj_dump(dev, &dump.vlan.obj, |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 720 | switchdev_port_vlan_dump_cb); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 721 | if (err) |
| 722 | goto err_out; |
| 723 | if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) |
| 724 | /* last one */ |
Vivien Didelot | e23b002 | 2015-09-29 12:07:13 -0400 | [diff] [blame] | 725 | err = switchdev_port_vlan_dump_put(&dump); |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | err_out: |
| 729 | return err == -EOPNOTSUPP ? 0 : err; |
| 730 | } |
| 731 | |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 732 | /** |
| 733 | * switchdev_port_bridge_getlink - Get bridge port attributes |
| 734 | * |
| 735 | * @dev: port device |
| 736 | * |
| 737 | * Called for SELF on rtnl_bridge_getlink to get bridge port |
| 738 | * attributes. |
| 739 | */ |
| 740 | int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
| 741 | struct net_device *dev, u32 filter_mask, |
| 742 | int nlflags) |
| 743 | { |
| 744 | struct switchdev_attr attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 745 | .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS, |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 746 | }; |
| 747 | u16 mode = BRIDGE_MODE_UNDEF; |
| 748 | u32 mask = BR_LEARNING | BR_LEARNING_SYNC; |
| 749 | int err; |
| 750 | |
| 751 | err = switchdev_port_attr_get(dev, &attr); |
Vivien Didelot | 5c8079d | 2015-06-23 10:26:04 -0400 | [diff] [blame] | 752 | if (err && err != -EOPNOTSUPP) |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 753 | return err; |
| 754 | |
| 755 | return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, |
Scott Feldman | 7d4f8d8 | 2015-06-22 00:27:17 -0700 | [diff] [blame] | 756 | attr.u.brport_flags, mask, nlflags, |
| 757 | filter_mask, switchdev_port_vlan_fill); |
Scott Feldman | 8793d0a | 2015-05-10 09:48:04 -0700 | [diff] [blame] | 758 | } |
| 759 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink); |
| 760 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 761 | static int switchdev_port_br_setflag(struct net_device *dev, |
| 762 | struct nlattr *nlattr, |
| 763 | unsigned long brport_flag) |
| 764 | { |
| 765 | struct switchdev_attr attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 766 | .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS, |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 767 | }; |
| 768 | u8 flag = nla_get_u8(nlattr); |
| 769 | int err; |
| 770 | |
| 771 | err = switchdev_port_attr_get(dev, &attr); |
| 772 | if (err) |
| 773 | return err; |
| 774 | |
| 775 | if (flag) |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 776 | attr.u.brport_flags |= brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 777 | else |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 778 | attr.u.brport_flags &= ~brport_flag; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 779 | |
| 780 | return switchdev_port_attr_set(dev, &attr); |
| 781 | } |
| 782 | |
| 783 | static const struct nla_policy |
| 784 | switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = { |
| 785 | [IFLA_BRPORT_STATE] = { .type = NLA_U8 }, |
| 786 | [IFLA_BRPORT_COST] = { .type = NLA_U32 }, |
| 787 | [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 }, |
| 788 | [IFLA_BRPORT_MODE] = { .type = NLA_U8 }, |
| 789 | [IFLA_BRPORT_GUARD] = { .type = NLA_U8 }, |
| 790 | [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 }, |
| 791 | [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 }, |
| 792 | [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 }, |
| 793 | [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 }, |
| 794 | [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 }, |
| 795 | }; |
| 796 | |
| 797 | static int switchdev_port_br_setlink_protinfo(struct net_device *dev, |
| 798 | struct nlattr *protinfo) |
| 799 | { |
| 800 | struct nlattr *attr; |
| 801 | int rem; |
| 802 | int err; |
| 803 | |
| 804 | err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX, |
| 805 | switchdev_port_bridge_policy); |
| 806 | if (err) |
| 807 | return err; |
| 808 | |
| 809 | nla_for_each_nested(attr, protinfo, rem) { |
| 810 | switch (nla_type(attr)) { |
| 811 | case IFLA_BRPORT_LEARNING: |
| 812 | err = switchdev_port_br_setflag(dev, attr, |
| 813 | BR_LEARNING); |
| 814 | break; |
| 815 | case IFLA_BRPORT_LEARNING_SYNC: |
| 816 | err = switchdev_port_br_setflag(dev, attr, |
| 817 | BR_LEARNING_SYNC); |
| 818 | break; |
| 819 | default: |
| 820 | err = -EOPNOTSUPP; |
| 821 | break; |
| 822 | } |
| 823 | if (err) |
| 824 | return err; |
| 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | static int switchdev_port_br_afspec(struct net_device *dev, |
| 831 | struct nlattr *afspec, |
| 832 | int (*f)(struct net_device *dev, |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 833 | const struct switchdev_obj *obj)) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 834 | { |
| 835 | struct nlattr *attr; |
| 836 | struct bridge_vlan_info *vinfo; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 837 | struct switchdev_obj_port_vlan vlan = { |
| 838 | .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, |
| 839 | }; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 840 | int rem; |
| 841 | int err; |
| 842 | |
| 843 | nla_for_each_nested(attr, afspec, rem) { |
| 844 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) |
| 845 | continue; |
| 846 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) |
| 847 | return -EINVAL; |
| 848 | vinfo = nla_data(attr); |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 849 | vlan.flags = vinfo->flags; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 850 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 851 | if (vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 852 | return -EINVAL; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 853 | vlan.vid_begin = vinfo->vid; |
Nikolay Aleksandrov | cc02aa8 | 2015-10-12 14:01:39 +0200 | [diff] [blame] | 854 | /* don't allow range of pvids */ |
| 855 | if (vlan.flags & BRIDGE_VLAN_INFO_PVID) |
| 856 | return -EINVAL; |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 857 | } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 858 | if (!vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 859 | return -EINVAL; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 860 | vlan.vid_end = vinfo->vid; |
| 861 | if (vlan.vid_end <= vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 862 | return -EINVAL; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 863 | err = f(dev, &vlan.obj); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 864 | if (err) |
| 865 | return err; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 866 | memset(&vlan, 0, sizeof(vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 867 | } else { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 868 | if (vlan.vid_begin) |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 869 | return -EINVAL; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 870 | vlan.vid_begin = vinfo->vid; |
| 871 | vlan.vid_end = vinfo->vid; |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 872 | err = f(dev, &vlan.obj); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 873 | if (err) |
| 874 | return err; |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 875 | memset(&vlan, 0, sizeof(vlan)); |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 882 | /** |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 883 | * switchdev_port_bridge_setlink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 884 | * |
| 885 | * @dev: port device |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 886 | * @nlh: netlink header |
| 887 | * @flags: netlink flags |
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 | * Called for SELF on rtnl_bridge_setlink to set bridge port |
| 890 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 891 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 892 | int switchdev_port_bridge_setlink(struct net_device *dev, |
| 893 | struct nlmsghdr *nlh, u16 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 | struct nlattr *protinfo; |
| 896 | struct nlattr *afspec; |
| 897 | int err = 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 898 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 899 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 900 | IFLA_PROTINFO); |
| 901 | if (protinfo) { |
| 902 | err = switchdev_port_br_setlink_protinfo(dev, protinfo); |
| 903 | if (err) |
| 904 | return err; |
| 905 | } |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 906 | |
Scott Feldman | 47f8328 | 2015-05-10 09:47:56 -0700 | [diff] [blame] | 907 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 908 | IFLA_AF_SPEC); |
| 909 | if (afspec) |
| 910 | err = switchdev_port_br_afspec(dev, afspec, |
| 911 | switchdev_port_obj_add); |
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 | return err; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 914 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 915 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 916 | |
| 917 | /** |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 918 | * switchdev_port_bridge_dellink - Set bridge port attributes |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 919 | * |
| 920 | * @dev: port device |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 921 | * @nlh: netlink header |
| 922 | * @flags: netlink flags |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 923 | * |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 924 | * Called for SELF on rtnl_bridge_dellink to set bridge port |
| 925 | * attributes. |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 926 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 927 | int switchdev_port_bridge_dellink(struct net_device *dev, |
| 928 | struct nlmsghdr *nlh, u16 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 | struct nlattr *afspec; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 931 | |
Scott Feldman | 5c34e02 | 2015-05-10 09:48:00 -0700 | [diff] [blame] | 932 | afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 933 | IFLA_AF_SPEC); |
| 934 | if (afspec) |
| 935 | return switchdev_port_br_afspec(dev, afspec, |
| 936 | switchdev_port_obj_del); |
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 | return 0; |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 939 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 940 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 941 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 942 | /** |
| 943 | * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port |
| 944 | * |
| 945 | * @ndmsg: netlink hdr |
| 946 | * @nlattr: netlink attributes |
| 947 | * @dev: port device |
| 948 | * @addr: MAC address to add |
| 949 | * @vid: VLAN to add |
| 950 | * |
| 951 | * Add FDB entry to switch device. |
| 952 | */ |
| 953 | int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 954 | struct net_device *dev, const unsigned char *addr, |
| 955 | u16 vid, u16 nlm_flags) |
| 956 | { |
Jiri Pirko | 52ba57c | 2015-10-01 11:03:44 +0200 | [diff] [blame] | 957 | struct switchdev_obj_port_fdb fdb = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 958 | .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 959 | .vid = vid, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 960 | }; |
| 961 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 962 | ether_addr_copy(fdb.addr, addr); |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 963 | return switchdev_port_obj_add(dev, &fdb.obj); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 964 | } |
| 965 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_add); |
| 966 | |
| 967 | /** |
| 968 | * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port |
| 969 | * |
| 970 | * @ndmsg: netlink hdr |
| 971 | * @nlattr: netlink attributes |
| 972 | * @dev: port device |
| 973 | * @addr: MAC address to delete |
| 974 | * @vid: VLAN to delete |
| 975 | * |
| 976 | * Delete FDB entry from switch device. |
| 977 | */ |
| 978 | int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
| 979 | struct net_device *dev, const unsigned char *addr, |
| 980 | u16 vid) |
| 981 | { |
Jiri Pirko | 52ba57c | 2015-10-01 11:03:44 +0200 | [diff] [blame] | 982 | struct switchdev_obj_port_fdb fdb = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 983 | .obj.id = SWITCHDEV_OBJ_ID_PORT_FDB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 984 | .vid = vid, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 985 | }; |
| 986 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 987 | ether_addr_copy(fdb.addr, addr); |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 988 | return switchdev_port_obj_del(dev, &fdb.obj); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 989 | } |
| 990 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_del); |
| 991 | |
| 992 | struct switchdev_fdb_dump { |
Jiri Pirko | 52ba57c | 2015-10-01 11:03:44 +0200 | [diff] [blame] | 993 | struct switchdev_obj_port_fdb fdb; |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 994 | struct net_device *dev; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 995 | struct sk_buff *skb; |
| 996 | struct netlink_callback *cb; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 997 | int idx; |
| 998 | }; |
| 999 | |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 1000 | static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1001 | { |
Jiri Pirko | 648b4a9 | 2015-10-01 11:03:45 +0200 | [diff] [blame] | 1002 | struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1003 | struct switchdev_fdb_dump *dump = |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1004 | container_of(fdb, struct switchdev_fdb_dump, fdb); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1005 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 1006 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 1007 | struct nlmsghdr *nlh; |
| 1008 | struct ndmsg *ndm; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1009 | |
| 1010 | if (dump->idx < dump->cb->args[0]) |
| 1011 | goto skip; |
| 1012 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1013 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 1014 | sizeof(*ndm), NLM_F_MULTI); |
| 1015 | if (!nlh) |
| 1016 | return -EMSGSIZE; |
| 1017 | |
| 1018 | ndm = nlmsg_data(nlh); |
| 1019 | ndm->ndm_family = AF_BRIDGE; |
| 1020 | ndm->ndm_pad1 = 0; |
| 1021 | ndm->ndm_pad2 = 0; |
| 1022 | ndm->ndm_flags = NTF_SELF; |
| 1023 | ndm->ndm_type = 0; |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 1024 | ndm->ndm_ifindex = dump->dev->ifindex; |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1025 | ndm->ndm_state = fdb->ndm_state; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1026 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1027 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr)) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1028 | goto nla_put_failure; |
| 1029 | |
Vivien Didelot | 25f07ad | 2015-09-29 12:07:16 -0400 | [diff] [blame] | 1030 | if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid)) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1031 | goto nla_put_failure; |
| 1032 | |
| 1033 | nlmsg_end(dump->skb, nlh); |
| 1034 | |
| 1035 | skip: |
| 1036 | dump->idx++; |
| 1037 | return 0; |
| 1038 | |
| 1039 | nla_put_failure: |
| 1040 | nlmsg_cancel(dump->skb, nlh); |
| 1041 | return -EMSGSIZE; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries |
| 1046 | * |
| 1047 | * @skb: netlink skb |
| 1048 | * @cb: netlink callback |
| 1049 | * @dev: port device |
| 1050 | * @filter_dev: filter device |
| 1051 | * @idx: |
| 1052 | * |
| 1053 | * Delete FDB entry from switch device. |
| 1054 | */ |
| 1055 | int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, |
| 1056 | struct net_device *dev, |
| 1057 | struct net_device *filter_dev, int idx) |
| 1058 | { |
| 1059 | struct switchdev_fdb_dump dump = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1060 | .fdb.obj.id = SWITCHDEV_OBJ_ID_PORT_FDB, |
Vivien Didelot | e02a06b2 | 2015-09-29 12:07:14 -0400 | [diff] [blame] | 1061 | .dev = dev, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1062 | .skb = skb, |
| 1063 | .cb = cb, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1064 | .idx = idx, |
| 1065 | }; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 1066 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1067 | 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] | 1068 | return dump.idx; |
| 1069 | } |
| 1070 | EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump); |
| 1071 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1072 | static struct net_device *switchdev_get_lowest_dev(struct net_device *dev) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1073 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 1074 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1075 | struct net_device *lower_dev; |
| 1076 | struct net_device *port_dev; |
| 1077 | struct list_head *iter; |
| 1078 | |
| 1079 | /* Recusively search down until we find a sw port dev. |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1080 | * (A sw port dev supports switchdev_port_attr_get). |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1081 | */ |
| 1082 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1083 | if (ops && ops->switchdev_port_attr_get) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1084 | return dev; |
| 1085 | |
| 1086 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1087 | port_dev = switchdev_get_lowest_dev(lower_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1088 | if (port_dev) |
| 1089 | return port_dev; |
| 1090 | } |
| 1091 | |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1095 | 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] | 1096 | { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1097 | struct switchdev_attr attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1098 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1099 | }; |
| 1100 | struct switchdev_attr prev_attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1101 | struct net_device *dev = NULL; |
| 1102 | int nhsel; |
| 1103 | |
Jiri Pirko | 771acac | 2015-10-14 19:40:55 +0200 | [diff] [blame^] | 1104 | ASSERT_RTNL(); |
| 1105 | |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1106 | /* For this route, all nexthop devs must be on the same switch. */ |
| 1107 | |
| 1108 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 1109 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
| 1110 | |
| 1111 | if (!nh->nh_dev) |
| 1112 | return NULL; |
| 1113 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1114 | dev = switchdev_get_lowest_dev(nh->nh_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1115 | if (!dev) |
| 1116 | return NULL; |
| 1117 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1118 | if (switchdev_port_attr_get(dev, &attr)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1119 | return NULL; |
| 1120 | |
Scott Feldman | d754f98 | 2015-07-18 18:24:49 -0700 | [diff] [blame] | 1121 | if (nhsel > 0 && |
| 1122 | !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1123 | return NULL; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1124 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 1125 | prev_attr = attr; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | return dev; |
| 1129 | } |
| 1130 | |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1131 | /** |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 1132 | * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1133 | * |
| 1134 | * @dst: route's IPv4 destination address |
| 1135 | * @dst_len: destination address length (prefix length) |
| 1136 | * @fi: route FIB info structure |
| 1137 | * @tos: route TOS |
| 1138 | * @type: route type |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 1139 | * @nlflags: netlink flags passed in (NLM_F_*) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1140 | * @tb_id: route table ID |
| 1141 | * |
Scott Feldman | 7616dcb | 2015-06-03 20:43:43 -0700 | [diff] [blame] | 1142 | * Add/modify switch IPv4 route entry. |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1143 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1144 | int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi, |
| 1145 | u8 tos, u8 type, u32 nlflags, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1146 | { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1147 | struct switchdev_obj_ipv4_fib ipv4_fib = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1148 | .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1149 | .dst = dst, |
| 1150 | .dst_len = dst_len, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1151 | .tos = tos, |
| 1152 | .type = type, |
| 1153 | .nlflags = nlflags, |
| 1154 | .tb_id = tb_id, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1155 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1156 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1157 | int err = 0; |
| 1158 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 1159 | memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi)); |
| 1160 | |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1161 | /* Don't offload route if using custom ip rules or if |
| 1162 | * IPv4 FIB offloading has been disabled completely. |
| 1163 | */ |
| 1164 | |
Scott Feldman | e1315db | 2015-03-06 01:14:36 -0800 | [diff] [blame] | 1165 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 1166 | if (fi->fib_net->ipv4.fib_has_custom_rules) |
| 1167 | return 0; |
| 1168 | #endif |
| 1169 | |
| 1170 | if (fi->fib_net->ipv4.fib_offload_disabled) |
Scott Feldman | 104616e | 2015-03-05 21:21:16 -0800 | [diff] [blame] | 1171 | return 0; |
| 1172 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1173 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1174 | if (!dev) |
| 1175 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1176 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1177 | err = switchdev_port_obj_add(dev, &ipv4_fib.obj); |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1178 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 1179 | fi->fib_flags |= RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1180 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 1181 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1182 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1183 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add); |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1184 | |
| 1185 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1186 | * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1187 | * |
| 1188 | * @dst: route's IPv4 destination address |
| 1189 | * @dst_len: destination address length (prefix length) |
| 1190 | * @fi: route FIB info structure |
| 1191 | * @tos: route TOS |
| 1192 | * @type: route type |
| 1193 | * @tb_id: route table ID |
| 1194 | * |
| 1195 | * Delete IPv4 route entry from switch device. |
| 1196 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1197 | int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi, |
| 1198 | u8 tos, u8 type, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1199 | { |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1200 | struct switchdev_obj_ipv4_fib ipv4_fib = { |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1201 | .obj.id = SWITCHDEV_OBJ_ID_IPV4_FIB, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1202 | .dst = dst, |
| 1203 | .dst_len = dst_len, |
Vivien Didelot | ab06900 | 2015-09-29 12:07:17 -0400 | [diff] [blame] | 1204 | .tos = tos, |
| 1205 | .type = type, |
| 1206 | .nlflags = 0, |
| 1207 | .tb_id = tb_id, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1208 | }; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1209 | struct net_device *dev; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1210 | int err = 0; |
| 1211 | |
Jiri Pirko | 850d0cb | 2015-10-14 19:40:51 +0200 | [diff] [blame] | 1212 | memcpy(&ipv4_fib.fi, fi, sizeof(ipv4_fib.fi)); |
| 1213 | |
Roopa Prabhu | eea3994 | 2015-05-13 21:17:41 -0700 | [diff] [blame] | 1214 | if (!(fi->fib_flags & RTNH_F_OFFLOAD)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1215 | return 0; |
| 1216 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1217 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1218 | if (!dev) |
| 1219 | return 0; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1220 | |
Jiri Pirko | 9e8f4a5 | 2015-10-01 11:03:46 +0200 | [diff] [blame] | 1221 | err = switchdev_port_obj_del(dev, &ipv4_fib.obj); |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 1222 | if (!err) |
David S. Miller | 36583eb | 2015-05-23 01:22:35 -0400 | [diff] [blame] | 1223 | fi->fib_flags &= ~RTNH_F_OFFLOAD; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 1224 | |
Scott Feldman | af201f7 | 2015-06-10 17:04:49 -0700 | [diff] [blame] | 1225 | return err == -EOPNOTSUPP ? 0 : err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 1226 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1227 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del); |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1228 | |
| 1229 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1230 | * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1231 | * |
| 1232 | * @fi: route FIB info structure |
| 1233 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1234 | void switchdev_fib_ipv4_abort(struct fib_info *fi) |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 1235 | { |
| 1236 | /* There was a problem installing this route to the offload |
| 1237 | * device. For now, until we come up with more refined |
| 1238 | * policy handling, abruptly end IPv4 fib offloading for |
| 1239 | * for entire net by flushing offload device(s) of all |
| 1240 | * IPv4 routes, and mark IPv4 fib offloading broken from |
| 1241 | * this point forward. |
| 1242 | */ |
| 1243 | |
| 1244 | fib_flush_external(fi->fib_net); |
| 1245 | fi->fib_net->ipv4.fib_offload_disabled = true; |
| 1246 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 1247 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort); |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1248 | |
| 1249 | static bool switchdev_port_same_parent_id(struct net_device *a, |
| 1250 | struct net_device *b) |
| 1251 | { |
| 1252 | struct switchdev_attr a_attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1253 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1254 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1255 | }; |
| 1256 | struct switchdev_attr b_attr = { |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 1257 | .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1258 | .flags = SWITCHDEV_F_NO_RECURSE, |
| 1259 | }; |
| 1260 | |
| 1261 | if (switchdev_port_attr_get(a, &a_attr) || |
| 1262 | switchdev_port_attr_get(b, &b_attr)) |
| 1263 | return false; |
| 1264 | |
| 1265 | return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid); |
| 1266 | } |
| 1267 | |
| 1268 | static u32 switchdev_port_fwd_mark_get(struct net_device *dev, |
| 1269 | struct net_device *group_dev) |
| 1270 | { |
| 1271 | struct net_device *lower_dev; |
| 1272 | struct list_head *iter; |
| 1273 | |
| 1274 | netdev_for_each_lower_dev(group_dev, lower_dev, iter) { |
| 1275 | if (lower_dev == dev) |
| 1276 | continue; |
| 1277 | if (switchdev_port_same_parent_id(dev, lower_dev)) |
| 1278 | return lower_dev->offload_fwd_mark; |
| 1279 | return switchdev_port_fwd_mark_get(dev, lower_dev); |
| 1280 | } |
| 1281 | |
| 1282 | return dev->ifindex; |
| 1283 | } |
| 1284 | |
| 1285 | static void switchdev_port_fwd_mark_reset(struct net_device *group_dev, |
| 1286 | u32 old_mark, u32 *reset_mark) |
| 1287 | { |
| 1288 | struct net_device *lower_dev; |
| 1289 | struct list_head *iter; |
| 1290 | |
| 1291 | netdev_for_each_lower_dev(group_dev, lower_dev, iter) { |
| 1292 | if (lower_dev->offload_fwd_mark == old_mark) { |
| 1293 | if (!*reset_mark) |
| 1294 | *reset_mark = lower_dev->ifindex; |
| 1295 | lower_dev->offload_fwd_mark = *reset_mark; |
| 1296 | } |
| 1297 | switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * switchdev_port_fwd_mark_set - Set port offload forwarding mark |
| 1303 | * |
| 1304 | * @dev: port device |
| 1305 | * @group_dev: containing device |
| 1306 | * @joining: true if dev is joining group; false if leaving group |
| 1307 | * |
| 1308 | * An ungrouped port's offload mark is just its ifindex. A grouped |
| 1309 | * port's (member of a bridge, for example) offload mark is the ifindex |
| 1310 | * of one of the ports in the group with the same parent (switch) ID. |
| 1311 | * Ports on the same device in the same group will have the same mark. |
| 1312 | * |
| 1313 | * Example: |
| 1314 | * |
| 1315 | * br0 ifindex=9 |
| 1316 | * sw1p1 ifindex=2 mark=2 |
| 1317 | * sw1p2 ifindex=3 mark=2 |
| 1318 | * sw2p1 ifindex=4 mark=5 |
| 1319 | * sw2p2 ifindex=5 mark=5 |
| 1320 | * |
| 1321 | * If sw2p2 leaves the bridge, we'll have: |
| 1322 | * |
| 1323 | * br0 ifindex=9 |
| 1324 | * sw1p1 ifindex=2 mark=2 |
| 1325 | * sw1p2 ifindex=3 mark=2 |
| 1326 | * sw2p1 ifindex=4 mark=4 |
| 1327 | * sw2p2 ifindex=5 mark=5 |
| 1328 | */ |
| 1329 | void switchdev_port_fwd_mark_set(struct net_device *dev, |
| 1330 | struct net_device *group_dev, |
| 1331 | bool joining) |
| 1332 | { |
| 1333 | u32 mark = dev->ifindex; |
| 1334 | u32 reset_mark = 0; |
| 1335 | |
Jiri Pirko | 771acac | 2015-10-14 19:40:55 +0200 | [diff] [blame^] | 1336 | if (group_dev) { |
| 1337 | ASSERT_RTNL(); |
| 1338 | if (joining) |
| 1339 | mark = switchdev_port_fwd_mark_get(dev, group_dev); |
| 1340 | else if (dev->offload_fwd_mark == mark) |
Scott Feldman | 1a3b2ec | 2015-07-18 18:24:50 -0700 | [diff] [blame] | 1341 | /* Ohoh, this port was the mark reference port, |
| 1342 | * but it's leaving the group, so reset the |
| 1343 | * mark for the remaining ports in the group. |
| 1344 | */ |
| 1345 | switchdev_port_fwd_mark_reset(group_dev, mark, |
| 1346 | &reset_mark); |
| 1347 | } |
| 1348 | |
| 1349 | dev->offload_fwd_mark = mark; |
| 1350 | } |
| 1351 | EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set); |