Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/switchdev/switchdev.c - Switch device API |
| 3 | * Copyright (c) 2014 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> |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 18 | #include <net/ip_fib.h> |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 19 | #include <net/switchdev.h> |
| 20 | |
| 21 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 22 | * switchdev_parent_id_get - Get ID of a switch |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 23 | * @dev: port device |
| 24 | * @psid: switch ID |
| 25 | * |
| 26 | * Get ID of a switch this port is part of. |
| 27 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 28 | int switchdev_parent_id_get(struct net_device *dev, |
| 29 | struct netdev_phys_item_id *psid) |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 30 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 31 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 32 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 33 | if (!ops || !ops->switchdev_parent_id_get) |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 34 | return -EOPNOTSUPP; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 35 | return ops->switchdev_parent_id_get(dev, psid); |
Jiri Pirko | 007f790 | 2014-11-28 14:34:17 +0100 | [diff] [blame] | 36 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 37 | EXPORT_SYMBOL_GPL(switchdev_parent_id_get); |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
Scott Feldman | 3094333 | 2015-05-10 09:47:48 -0700 | [diff] [blame^] | 40 | * switchdev_port_attr_get - Get port attribute |
| 41 | * |
| 42 | * @dev: port device |
| 43 | * @attr: attribute to get |
| 44 | */ |
| 45 | int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr) |
| 46 | { |
| 47 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 48 | struct net_device *lower_dev; |
| 49 | struct list_head *iter; |
| 50 | struct switchdev_attr first = { |
| 51 | .id = SWITCHDEV_ATTR_UNDEFINED |
| 52 | }; |
| 53 | int err = -EOPNOTSUPP; |
| 54 | |
| 55 | if (ops && ops->switchdev_port_attr_get) |
| 56 | return ops->switchdev_port_attr_get(dev, attr); |
| 57 | |
| 58 | if (attr->flags & SWITCHDEV_F_NO_RECURSE) |
| 59 | return err; |
| 60 | |
| 61 | /* Switch device port(s) may be stacked under |
| 62 | * bond/team/vlan dev, so recurse down to get attr on |
| 63 | * each port. Return -ENODATA if attr values don't |
| 64 | * compare across ports. |
| 65 | */ |
| 66 | |
| 67 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 68 | err = switchdev_port_attr_get(lower_dev, attr); |
| 69 | if (err) |
| 70 | break; |
| 71 | if (first.id == SWITCHDEV_ATTR_UNDEFINED) |
| 72 | first = *attr; |
| 73 | else if (memcmp(&first, attr, sizeof(*attr))) |
| 74 | return -ENODATA; |
| 75 | } |
| 76 | |
| 77 | return err; |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(switchdev_port_attr_get); |
| 80 | |
| 81 | static int __switchdev_port_attr_set(struct net_device *dev, |
| 82 | struct switchdev_attr *attr) |
| 83 | { |
| 84 | const struct switchdev_ops *ops = dev->switchdev_ops; |
| 85 | struct net_device *lower_dev; |
| 86 | struct list_head *iter; |
| 87 | int err = -EOPNOTSUPP; |
| 88 | |
| 89 | if (ops && ops->switchdev_port_attr_set) |
| 90 | return ops->switchdev_port_attr_set(dev, attr); |
| 91 | |
| 92 | if (attr->flags & SWITCHDEV_F_NO_RECURSE) |
| 93 | return err; |
| 94 | |
| 95 | /* Switch device port(s) may be stacked under |
| 96 | * bond/team/vlan dev, so recurse down to set attr on |
| 97 | * each port. |
| 98 | */ |
| 99 | |
| 100 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 101 | err = __switchdev_port_attr_set(lower_dev, attr); |
| 102 | if (err) |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | return err; |
| 107 | } |
| 108 | |
| 109 | struct switchdev_attr_set_work { |
| 110 | struct work_struct work; |
| 111 | struct net_device *dev; |
| 112 | struct switchdev_attr attr; |
| 113 | }; |
| 114 | |
| 115 | static void switchdev_port_attr_set_work(struct work_struct *work) |
| 116 | { |
| 117 | struct switchdev_attr_set_work *asw = |
| 118 | container_of(work, struct switchdev_attr_set_work, work); |
| 119 | int err; |
| 120 | |
| 121 | rtnl_lock(); |
| 122 | err = switchdev_port_attr_set(asw->dev, &asw->attr); |
| 123 | BUG_ON(err); |
| 124 | rtnl_unlock(); |
| 125 | |
| 126 | dev_put(asw->dev); |
| 127 | kfree(work); |
| 128 | } |
| 129 | |
| 130 | static int switchdev_port_attr_set_defer(struct net_device *dev, |
| 131 | struct switchdev_attr *attr) |
| 132 | { |
| 133 | struct switchdev_attr_set_work *asw; |
| 134 | |
| 135 | asw = kmalloc(sizeof(*asw), GFP_ATOMIC); |
| 136 | if (!asw) |
| 137 | return -ENOMEM; |
| 138 | |
| 139 | INIT_WORK(&asw->work, switchdev_port_attr_set_work); |
| 140 | |
| 141 | dev_hold(dev); |
| 142 | asw->dev = dev; |
| 143 | memcpy(&asw->attr, attr, sizeof(asw->attr)); |
| 144 | |
| 145 | schedule_work(&asw->work); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * switchdev_port_attr_set - Set port attribute |
| 152 | * |
| 153 | * @dev: port device |
| 154 | * @attr: attribute to set |
| 155 | * |
| 156 | * Use a 2-phase prepare-commit transaction model to ensure |
| 157 | * system is not left in a partially updated state due to |
| 158 | * failure from driver/device. |
| 159 | */ |
| 160 | int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr) |
| 161 | { |
| 162 | int err; |
| 163 | |
| 164 | if (!rtnl_is_locked()) { |
| 165 | /* Running prepare-commit transaction across stacked |
| 166 | * devices requires nothing moves, so if rtnl_lock is |
| 167 | * not held, schedule a worker thread to hold rtnl_lock |
| 168 | * while setting attr. |
| 169 | */ |
| 170 | |
| 171 | return switchdev_port_attr_set_defer(dev, attr); |
| 172 | } |
| 173 | |
| 174 | /* Phase I: prepare for attr set. Driver/device should fail |
| 175 | * here if there are going to be issues in the commit phase, |
| 176 | * such as lack of resources or support. The driver/device |
| 177 | * should reserve resources needed for the commit phase here, |
| 178 | * but should not commit the attr. |
| 179 | */ |
| 180 | |
| 181 | attr->trans = SWITCHDEV_TRANS_PREPARE; |
| 182 | err = __switchdev_port_attr_set(dev, attr); |
| 183 | if (err) { |
| 184 | /* Prepare phase failed: abort the transaction. Any |
| 185 | * resources reserved in the prepare phase are |
| 186 | * released. |
| 187 | */ |
| 188 | |
| 189 | attr->trans = SWITCHDEV_TRANS_ABORT; |
| 190 | __switchdev_port_attr_set(dev, attr); |
| 191 | |
| 192 | return err; |
| 193 | } |
| 194 | |
| 195 | /* Phase II: commit attr set. This cannot fail as a fault |
| 196 | * of driver/device. If it does, it's a bug in the driver/device |
| 197 | * because the driver said everythings was OK in phase I. |
| 198 | */ |
| 199 | |
| 200 | attr->trans = SWITCHDEV_TRANS_COMMIT; |
| 201 | err = __switchdev_port_attr_set(dev, attr); |
| 202 | BUG_ON(err); |
| 203 | |
| 204 | return err; |
| 205 | } |
| 206 | EXPORT_SYMBOL_GPL(switchdev_port_attr_set); |
| 207 | |
| 208 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 209 | * switchdev_port_stp_update - Notify switch device port of STP |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 210 | * state change |
| 211 | * @dev: port device |
| 212 | * @state: port STP state |
| 213 | * |
| 214 | * Notify switch device port of bridge port STP state change. |
| 215 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 216 | int switchdev_port_stp_update(struct net_device *dev, u8 state) |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 217 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 218 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Roopa Prabhu | 558d51f | 2015-03-21 10:27:28 -0700 | [diff] [blame] | 219 | struct net_device *lower_dev; |
| 220 | struct list_head *iter; |
| 221 | int err = -EOPNOTSUPP; |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 222 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 223 | if (ops && ops->switchdev_port_stp_update) |
| 224 | return ops->switchdev_port_stp_update(dev, state); |
Roopa Prabhu | 558d51f | 2015-03-21 10:27:28 -0700 | [diff] [blame] | 225 | |
| 226 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 227 | err = switchdev_port_stp_update(lower_dev, state); |
Roopa Prabhu | 558d51f | 2015-03-21 10:27:28 -0700 | [diff] [blame] | 228 | if (err && err != -EOPNOTSUPP) |
| 229 | return err; |
| 230 | } |
| 231 | |
| 232 | return err; |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 233 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 234 | EXPORT_SYMBOL_GPL(switchdev_port_stp_update); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 235 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 236 | static DEFINE_MUTEX(switchdev_mutex); |
| 237 | static RAW_NOTIFIER_HEAD(switchdev_notif_chain); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 238 | |
| 239 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 240 | * register_switchdev_notifier - Register notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 241 | * @nb: notifier_block |
| 242 | * |
| 243 | * Register switch device notifier. This should be used by code |
| 244 | * which needs to monitor events happening in particular device. |
| 245 | * Return values are same as for atomic_notifier_chain_register(). |
| 246 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 247 | int register_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 248 | { |
| 249 | int err; |
| 250 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 251 | mutex_lock(&switchdev_mutex); |
| 252 | err = raw_notifier_chain_register(&switchdev_notif_chain, nb); |
| 253 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 254 | return err; |
| 255 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 256 | EXPORT_SYMBOL_GPL(register_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 257 | |
| 258 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 259 | * unregister_switchdev_notifier - Unregister notifier |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 260 | * @nb: notifier_block |
| 261 | * |
| 262 | * Unregister switch device notifier. |
| 263 | * Return values are same as for atomic_notifier_chain_unregister(). |
| 264 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 265 | int unregister_switchdev_notifier(struct notifier_block *nb) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 266 | { |
| 267 | int err; |
| 268 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 269 | mutex_lock(&switchdev_mutex); |
| 270 | err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb); |
| 271 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 272 | return err; |
| 273 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 274 | EXPORT_SYMBOL_GPL(unregister_switchdev_notifier); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 275 | |
| 276 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 277 | * call_switchdev_notifiers - Call notifiers |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 278 | * @val: value passed unmodified to notifier function |
| 279 | * @dev: port device |
| 280 | * @info: notifier information data |
| 281 | * |
| 282 | * Call all network notifier blocks. This should be called by driver |
| 283 | * when it needs to propagate hardware event. |
| 284 | * Return values are same as for atomic_notifier_call_chain(). |
| 285 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 286 | int call_switchdev_notifiers(unsigned long val, struct net_device *dev, |
| 287 | struct switchdev_notifier_info *info) |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 288 | { |
| 289 | int err; |
| 290 | |
| 291 | info->dev = dev; |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 292 | mutex_lock(&switchdev_mutex); |
| 293 | err = raw_notifier_call_chain(&switchdev_notif_chain, val, info); |
| 294 | mutex_unlock(&switchdev_mutex); |
Jiri Pirko | 03bf0c2 | 2015-01-15 23:49:36 +0100 | [diff] [blame] | 295 | return err; |
| 296 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 297 | EXPORT_SYMBOL_GPL(call_switchdev_notifiers); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 298 | |
| 299 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 300 | * switchdev_port_bridge_setlink - Notify switch device port of bridge |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 301 | * port attributes |
| 302 | * |
| 303 | * @dev: port device |
| 304 | * @nlh: netlink msg with bridge port attributes |
| 305 | * @flags: bridge setlink flags |
| 306 | * |
| 307 | * Notify switch device port of bridge port attributes |
| 308 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 309 | int switchdev_port_bridge_setlink(struct net_device *dev, |
| 310 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 311 | { |
| 312 | const struct net_device_ops *ops = dev->netdev_ops; |
| 313 | |
| 314 | if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD)) |
| 315 | return 0; |
| 316 | |
| 317 | if (!ops->ndo_bridge_setlink) |
| 318 | return -EOPNOTSUPP; |
| 319 | |
| 320 | return ops->ndo_bridge_setlink(dev, nlh, flags); |
| 321 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 322 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 323 | |
| 324 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 325 | * switchdev_port_bridge_dellink - Notify switch device port of bridge |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 326 | * port attribute delete |
| 327 | * |
| 328 | * @dev: port device |
| 329 | * @nlh: netlink msg with bridge port attributes |
| 330 | * @flags: bridge setlink flags |
| 331 | * |
| 332 | * Notify switch device port of bridge port attribute delete |
| 333 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 334 | int switchdev_port_bridge_dellink(struct net_device *dev, |
| 335 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 336 | { |
| 337 | const struct net_device_ops *ops = dev->netdev_ops; |
| 338 | |
| 339 | if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD)) |
| 340 | return 0; |
| 341 | |
| 342 | if (!ops->ndo_bridge_dellink) |
| 343 | return -EOPNOTSUPP; |
| 344 | |
| 345 | return ops->ndo_bridge_dellink(dev, nlh, flags); |
| 346 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 348 | |
| 349 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 350 | * ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink |
| 351 | * op for master devices |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 352 | * |
| 353 | * @dev: port device |
| 354 | * @nlh: netlink msg with bridge port attributes |
| 355 | * @flags: bridge setlink flags |
| 356 | * |
| 357 | * Notify master device slaves of bridge port attributes |
| 358 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 359 | int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev, |
| 360 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 361 | { |
| 362 | struct net_device *lower_dev; |
| 363 | struct list_head *iter; |
| 364 | int ret = 0, err = 0; |
| 365 | |
| 366 | if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD)) |
| 367 | return ret; |
| 368 | |
| 369 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 370 | err = switchdev_port_bridge_setlink(lower_dev, nlh, flags); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 371 | if (err && err != -EOPNOTSUPP) |
| 372 | ret = err; |
| 373 | } |
| 374 | |
| 375 | return ret; |
| 376 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 377 | EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 378 | |
| 379 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 380 | * ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink |
| 381 | * op for master devices |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 382 | * |
| 383 | * @dev: port device |
| 384 | * @nlh: netlink msg with bridge port attributes |
| 385 | * @flags: bridge dellink flags |
| 386 | * |
| 387 | * Notify master device slaves of bridge port attribute deletes |
| 388 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 389 | int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev, |
| 390 | struct nlmsghdr *nlh, u16 flags) |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 391 | { |
| 392 | struct net_device *lower_dev; |
| 393 | struct list_head *iter; |
| 394 | int ret = 0, err = 0; |
| 395 | |
| 396 | if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD)) |
| 397 | return ret; |
| 398 | |
| 399 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 400 | err = switchdev_port_bridge_dellink(lower_dev, nlh, flags); |
Roopa Prabhu | 8a44dbb | 2015-01-29 22:40:13 -0800 | [diff] [blame] | 401 | if (err && err != -EOPNOTSUPP) |
| 402 | ret = err; |
| 403 | } |
| 404 | |
| 405 | return ret; |
| 406 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 407 | EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink); |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 408 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 409 | static struct net_device *switchdev_get_lowest_dev(struct net_device *dev) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 410 | { |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 411 | const struct switchdev_ops *ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 412 | struct net_device *lower_dev; |
| 413 | struct net_device *port_dev; |
| 414 | struct list_head *iter; |
| 415 | |
| 416 | /* Recusively search down until we find a sw port dev. |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 417 | * (A sw port dev supports switchdev_parent_id_get). |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 418 | */ |
| 419 | |
| 420 | if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD && |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 421 | ops && ops->switchdev_parent_id_get) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 422 | return dev; |
| 423 | |
| 424 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 425 | port_dev = switchdev_get_lowest_dev(lower_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 426 | if (port_dev) |
| 427 | return port_dev; |
| 428 | } |
| 429 | |
| 430 | return NULL; |
| 431 | } |
| 432 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 433 | 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] | 434 | { |
| 435 | struct netdev_phys_item_id psid; |
| 436 | struct netdev_phys_item_id prev_psid; |
| 437 | struct net_device *dev = NULL; |
| 438 | int nhsel; |
| 439 | |
| 440 | /* For this route, all nexthop devs must be on the same switch. */ |
| 441 | |
| 442 | for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { |
| 443 | const struct fib_nh *nh = &fi->fib_nh[nhsel]; |
| 444 | |
| 445 | if (!nh->nh_dev) |
| 446 | return NULL; |
| 447 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 448 | dev = switchdev_get_lowest_dev(nh->nh_dev); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 449 | if (!dev) |
| 450 | return NULL; |
| 451 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 452 | if (switchdev_parent_id_get(dev, &psid)) |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 453 | return NULL; |
| 454 | |
| 455 | if (nhsel > 0) { |
| 456 | if (prev_psid.id_len != psid.id_len) |
| 457 | return NULL; |
| 458 | if (memcmp(prev_psid.id, psid.id, psid.id_len)) |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | prev_psid = psid; |
| 463 | } |
| 464 | |
| 465 | return dev; |
| 466 | } |
| 467 | |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 468 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 469 | * switchdev_fib_ipv4_add - Add IPv4 route entry to switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 470 | * |
| 471 | * @dst: route's IPv4 destination address |
| 472 | * @dst_len: destination address length (prefix length) |
| 473 | * @fi: route FIB info structure |
| 474 | * @tos: route TOS |
| 475 | * @type: route type |
Scott Feldman | f8f2147 | 2015-03-09 13:59:09 -0700 | [diff] [blame] | 476 | * @nlflags: netlink flags passed in (NLM_F_*) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 477 | * @tb_id: route table ID |
| 478 | * |
| 479 | * Add IPv4 route entry to switch device. |
| 480 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 481 | int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi, |
| 482 | u8 tos, u8 type, u32 nlflags, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 483 | { |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 484 | struct net_device *dev; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 485 | const struct switchdev_ops *ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 486 | int err = 0; |
| 487 | |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 488 | /* Don't offload route if using custom ip rules or if |
| 489 | * IPv4 FIB offloading has been disabled completely. |
| 490 | */ |
| 491 | |
Scott Feldman | e1315db | 2015-03-06 01:14:36 -0800 | [diff] [blame] | 492 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 493 | if (fi->fib_net->ipv4.fib_has_custom_rules) |
| 494 | return 0; |
| 495 | #endif |
| 496 | |
| 497 | if (fi->fib_net->ipv4.fib_offload_disabled) |
Scott Feldman | 104616e | 2015-03-05 21:21:16 -0800 | [diff] [blame] | 498 | return 0; |
| 499 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 500 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 501 | if (!dev) |
| 502 | return 0; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 503 | ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 504 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 505 | if (ops->switchdev_fib_ipv4_add) { |
| 506 | err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len, |
| 507 | fi, tos, type, nlflags, |
| 508 | tb_id); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 509 | if (!err) |
| 510 | fi->fib_flags |= RTNH_F_EXTERNAL; |
| 511 | } |
| 512 | |
| 513 | return err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 514 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 515 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add); |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 516 | |
| 517 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 518 | * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 519 | * |
| 520 | * @dst: route's IPv4 destination address |
| 521 | * @dst_len: destination address length (prefix length) |
| 522 | * @fi: route FIB info structure |
| 523 | * @tos: route TOS |
| 524 | * @type: route type |
| 525 | * @tb_id: route table ID |
| 526 | * |
| 527 | * Delete IPv4 route entry from switch device. |
| 528 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 529 | int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi, |
| 530 | u8 tos, u8 type, u32 tb_id) |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 531 | { |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 532 | struct net_device *dev; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 533 | const struct switchdev_ops *ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 534 | int err = 0; |
| 535 | |
| 536 | if (!(fi->fib_flags & RTNH_F_EXTERNAL)) |
| 537 | return 0; |
| 538 | |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 539 | dev = switchdev_get_dev_by_nhs(fi); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 540 | if (!dev) |
| 541 | return 0; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 542 | ops = dev->switchdev_ops; |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 543 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 544 | if (ops->switchdev_fib_ipv4_del) { |
| 545 | err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len, |
| 546 | fi, tos, type, tb_id); |
Scott Feldman | b5d6fbd | 2015-03-05 21:21:17 -0800 | [diff] [blame] | 547 | if (!err) |
| 548 | fi->fib_flags &= ~RTNH_F_EXTERNAL; |
| 549 | } |
| 550 | |
| 551 | return err; |
Scott Feldman | 5e8d904 | 2015-03-05 21:21:15 -0800 | [diff] [blame] | 552 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 553 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del); |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 554 | |
| 555 | /** |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 556 | * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 557 | * |
| 558 | * @fi: route FIB info structure |
| 559 | */ |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 560 | void switchdev_fib_ipv4_abort(struct fib_info *fi) |
Scott Feldman | 8e05fd7 | 2015-03-05 21:21:19 -0800 | [diff] [blame] | 561 | { |
| 562 | /* There was a problem installing this route to the offload |
| 563 | * device. For now, until we come up with more refined |
| 564 | * policy handling, abruptly end IPv4 fib offloading for |
| 565 | * for entire net by flushing offload device(s) of all |
| 566 | * IPv4 routes, and mark IPv4 fib offloading broken from |
| 567 | * this point forward. |
| 568 | */ |
| 569 | |
| 570 | fib_flush_external(fi->fib_net); |
| 571 | fi->fib_net->ipv4.fib_offload_disabled = true; |
| 572 | } |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 573 | EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort); |