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