blob: 94113ca29dcf372dba8c239c44fa8e458de322f1 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Pirko007f7902014-11-28 14:34:17 +01002/*
3 * net/switchdev/switchdev.c - Switch device API
Jiri Pirko7ea6eb32015-09-24 10:02:41 +02004 * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
Scott Feldmanf8f21472015-03-09 13:59:09 -07005 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
Jiri Pirko007f7902014-11-28 14:34:17 +01006 */
7
8#include <linux/kernel.h>
9#include <linux/types.h>
10#include <linux/init.h>
Jiri Pirko03bf0c22015-01-15 23:49:36 +010011#include <linux/mutex.h>
12#include <linux/notifier.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010013#include <linux/netdevice.h>
Jiri Pirko850d0cb2015-10-14 19:40:51 +020014#include <linux/etherdevice.h>
Scott Feldman47f83282015-05-10 09:47:56 -070015#include <linux/if_bridge.h>
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020016#include <linux/list.h>
Jiri Pirko793f4012015-10-14 19:40:48 +020017#include <linux/workqueue.h>
Nikolay Aleksandrov87aaf2c2015-10-12 14:31:01 +020018#include <linux/if_vlan.h>
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +010019#include <linux/rtnetlink.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010020#include <net/switchdev.h>
21
Jiri Pirko793f4012015-10-14 19:40:48 +020022static LIST_HEAD(deferred);
23static DEFINE_SPINLOCK(deferred_lock);
24
25typedef void switchdev_deferred_func_t(struct net_device *dev,
26 const void *data);
27
28struct switchdev_deferred_item {
29 struct list_head list;
30 struct net_device *dev;
31 switchdev_deferred_func_t *func;
Gustavo A. R. Silvafbfc8502020-02-17 14:02:36 -060032 unsigned long data[];
Jiri Pirko793f4012015-10-14 19:40:48 +020033};
34
35static struct switchdev_deferred_item *switchdev_deferred_dequeue(void)
36{
37 struct switchdev_deferred_item *dfitem;
38
39 spin_lock_bh(&deferred_lock);
40 if (list_empty(&deferred)) {
41 dfitem = NULL;
42 goto unlock;
43 }
44 dfitem = list_first_entry(&deferred,
45 struct switchdev_deferred_item, list);
46 list_del(&dfitem->list);
47unlock:
48 spin_unlock_bh(&deferred_lock);
49 return dfitem;
50}
51
52/**
53 * switchdev_deferred_process - Process ops in deferred queue
54 *
55 * Called to flush the ops currently queued in deferred ops queue.
56 * rtnl_lock must be held.
57 */
58void switchdev_deferred_process(void)
59{
60 struct switchdev_deferred_item *dfitem;
61
62 ASSERT_RTNL();
63
64 while ((dfitem = switchdev_deferred_dequeue())) {
65 dfitem->func(dfitem->dev, dfitem->data);
66 dev_put(dfitem->dev);
67 kfree(dfitem);
68 }
69}
70EXPORT_SYMBOL_GPL(switchdev_deferred_process);
71
72static void switchdev_deferred_process_work(struct work_struct *work)
73{
74 rtnl_lock();
75 switchdev_deferred_process();
76 rtnl_unlock();
77}
78
79static DECLARE_WORK(deferred_process_work, switchdev_deferred_process_work);
80
81static int switchdev_deferred_enqueue(struct net_device *dev,
82 const void *data, size_t data_len,
83 switchdev_deferred_func_t *func)
84{
85 struct switchdev_deferred_item *dfitem;
86
87 dfitem = kmalloc(sizeof(*dfitem) + data_len, GFP_ATOMIC);
88 if (!dfitem)
89 return -ENOMEM;
90 dfitem->dev = dev;
91 dfitem->func = func;
92 memcpy(dfitem->data, data, data_len);
93 dev_hold(dev);
94 spin_lock_bh(&deferred_lock);
95 list_add_tail(&dfitem->list, &deferred);
96 spin_unlock_bh(&deferred_lock);
97 schedule_work(&deferred_process_work);
98 return 0;
99}
100
Florian Fainellid45224d2019-02-27 11:44:31 -0800101static int switchdev_port_attr_notify(enum switchdev_notifier_type nt,
102 struct net_device *dev,
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200103 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700104{
Florian Fainellid45224d2019-02-27 11:44:31 -0800105 int err;
106 int rc;
Scott Feldman30943332015-05-10 09:47:48 -0700107
Florian Fainellid45224d2019-02-27 11:44:31 -0800108 struct switchdev_notifier_port_attr_info attr_info = {
109 .attr = attr,
Florian Fainellid45224d2019-02-27 11:44:31 -0800110 .handled = false,
111 };
112
113 rc = call_switchdev_blocking_notifiers(nt, dev,
114 &attr_info.info, NULL);
115 err = notifier_to_errno(rc);
116 if (err) {
117 WARN_ON(!attr_info.handled);
118 return err;
Jiri Pirko0c63d802015-11-03 17:40:53 +0100119 }
Scott Feldman30943332015-05-10 09:47:48 -0700120
Florian Fainellid45224d2019-02-27 11:44:31 -0800121 if (!attr_info.handled)
122 return -EOPNOTSUPP;
Scott Feldman30943332015-05-10 09:47:48 -0700123
Florian Fainellid45224d2019-02-27 11:44:31 -0800124 return 0;
Scott Feldman30943332015-05-10 09:47:48 -0700125}
126
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200127static int switchdev_port_attr_set_now(struct net_device *dev,
128 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700129{
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200130 return switchdev_port_attr_notify(SWITCHDEV_PORT_ATTR_SET, dev, attr);
Scott Feldman30943332015-05-10 09:47:48 -0700131}
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200132
133static void switchdev_port_attr_set_deferred(struct net_device *dev,
134 const void *data)
135{
136 const struct switchdev_attr *attr = data;
137 int err;
138
139 err = switchdev_port_attr_set_now(dev, attr);
140 if (err && err != -EOPNOTSUPP)
141 netdev_err(dev, "failed (err=%d) to set attribute (id=%d)\n",
142 err, attr->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200143 if (attr->complete)
144 attr->complete(dev, err, attr->complete_priv);
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200145}
146
147static int switchdev_port_attr_set_defer(struct net_device *dev,
148 const struct switchdev_attr *attr)
149{
150 return switchdev_deferred_enqueue(dev, attr, sizeof(*attr),
151 switchdev_port_attr_set_deferred);
152}
153
154/**
155 * switchdev_port_attr_set - Set port attribute
156 *
157 * @dev: port device
158 * @attr: attribute to set
159 *
Jiri Pirko0bc05d52015-10-14 19:40:50 +0200160 * rtnl_lock must be held and must not be in atomic section,
161 * in case SWITCHDEV_F_DEFER flag is not set.
162 */
163int switchdev_port_attr_set(struct net_device *dev,
164 const struct switchdev_attr *attr)
165{
166 if (attr->flags & SWITCHDEV_F_DEFER)
167 return switchdev_port_attr_set_defer(dev, attr);
168 ASSERT_RTNL();
169 return switchdev_port_attr_set_now(dev, attr);
170}
Scott Feldman30943332015-05-10 09:47:48 -0700171EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
172
Scott Feldmane258d912015-10-28 23:17:31 -0700173static size_t switchdev_obj_size(const struct switchdev_obj *obj)
174{
175 switch (obj->id) {
176 case SWITCHDEV_OBJ_ID_PORT_VLAN:
177 return sizeof(struct switchdev_obj_port_vlan);
Elad Raz4d41e1252016-01-10 21:06:22 +0100178 case SWITCHDEV_OBJ_ID_PORT_MDB:
179 return sizeof(struct switchdev_obj_port_mdb);
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100180 case SWITCHDEV_OBJ_ID_HOST_MDB:
181 return sizeof(struct switchdev_obj_port_mdb);
Scott Feldmane258d912015-10-28 23:17:31 -0700182 default:
183 BUG();
184 }
185 return 0;
186}
187
Petr Machatad17d9f52018-11-22 23:32:57 +0000188static int switchdev_port_obj_notify(enum switchdev_notifier_type nt,
189 struct net_device *dev,
190 const struct switchdev_obj *obj,
Petr Machata69b73202018-12-12 17:02:52 +0000191 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700192{
Petr Machatad17d9f52018-11-22 23:32:57 +0000193 int rc;
194 int err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700195
Petr Machatad17d9f52018-11-22 23:32:57 +0000196 struct switchdev_notifier_port_obj_info obj_info = {
197 .obj = obj,
Petr Machatad17d9f52018-11-22 23:32:57 +0000198 .handled = false,
199 };
Scott Feldman491d0f12015-05-10 09:47:52 -0700200
Petr Machata479c86d2018-12-12 17:02:54 +0000201 rc = call_switchdev_blocking_notifiers(nt, dev, &obj_info.info, extack);
Petr Machatad17d9f52018-11-22 23:32:57 +0000202 err = notifier_to_errno(rc);
203 if (err) {
204 WARN_ON(!obj_info.handled);
205 return err;
Scott Feldman491d0f12015-05-10 09:47:52 -0700206 }
Petr Machatad17d9f52018-11-22 23:32:57 +0000207 if (!obj_info.handled)
208 return -EOPNOTSUPP;
209 return 0;
Scott Feldman491d0f12015-05-10 09:47:52 -0700210}
211
Jiri Pirko4d429c52015-10-14 19:40:52 +0200212static void switchdev_port_obj_add_deferred(struct net_device *dev,
213 const void *data)
214{
215 const struct switchdev_obj *obj = data;
216 int err;
217
Vladimir Olteancf6def52021-01-09 02:01:49 +0200218 ASSERT_RTNL();
219 err = switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
220 dev, obj, NULL);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200221 if (err && err != -EOPNOTSUPP)
222 netdev_err(dev, "failed (err=%d) to add object (id=%d)\n",
223 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200224 if (obj->complete)
225 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200226}
227
228static int switchdev_port_obj_add_defer(struct net_device *dev,
229 const struct switchdev_obj *obj)
230{
Scott Feldmane258d912015-10-28 23:17:31 -0700231 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200232 switchdev_port_obj_add_deferred);
233}
Scott Feldman491d0f12015-05-10 09:47:52 -0700234
235/**
Jiri Pirko4d429c52015-10-14 19:40:52 +0200236 * switchdev_port_obj_add - Add port object
Scott Feldman491d0f12015-05-10 09:47:52 -0700237 *
238 * @dev: port device
Jiri Pirko4d429c52015-10-14 19:40:52 +0200239 * @obj: object to add
Andrew Lunnc8af73f2020-07-13 01:15:13 +0200240 * @extack: netlink extended ack
Jiri Pirko4d429c52015-10-14 19:40:52 +0200241 *
Jiri Pirko4d429c52015-10-14 19:40:52 +0200242 * rtnl_lock must be held and must not be in atomic section,
243 * in case SWITCHDEV_F_DEFER flag is not set.
Scott Feldman491d0f12015-05-10 09:47:52 -0700244 */
Jiri Pirko4d429c52015-10-14 19:40:52 +0200245int switchdev_port_obj_add(struct net_device *dev,
Petr Machata69b73202018-12-12 17:02:52 +0000246 const struct switchdev_obj *obj,
247 struct netlink_ext_ack *extack)
Scott Feldman491d0f12015-05-10 09:47:52 -0700248{
Jiri Pirko4d429c52015-10-14 19:40:52 +0200249 if (obj->flags & SWITCHDEV_F_DEFER)
250 return switchdev_port_obj_add_defer(dev, obj);
251 ASSERT_RTNL();
Vladimir Olteancf6def52021-01-09 02:01:49 +0200252 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_ADD,
253 dev, obj, extack);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200254}
255EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
256
257static int switchdev_port_obj_del_now(struct net_device *dev,
258 const struct switchdev_obj *obj)
259{
Petr Machatad17d9f52018-11-22 23:32:57 +0000260 return switchdev_port_obj_notify(SWITCHDEV_PORT_OBJ_DEL,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200261 dev, obj, NULL);
Scott Feldman491d0f12015-05-10 09:47:52 -0700262}
Jiri Pirko4d429c52015-10-14 19:40:52 +0200263
264static void switchdev_port_obj_del_deferred(struct net_device *dev,
265 const void *data)
266{
267 const struct switchdev_obj *obj = data;
268 int err;
269
270 err = switchdev_port_obj_del_now(dev, obj);
271 if (err && err != -EOPNOTSUPP)
272 netdev_err(dev, "failed (err=%d) to del object (id=%d)\n",
273 err, obj->id);
Elad Raz7ceb2af2016-04-21 12:52:43 +0200274 if (obj->complete)
275 obj->complete(dev, err, obj->complete_priv);
Jiri Pirko4d429c52015-10-14 19:40:52 +0200276}
277
278static int switchdev_port_obj_del_defer(struct net_device *dev,
279 const struct switchdev_obj *obj)
280{
Scott Feldmane258d912015-10-28 23:17:31 -0700281 return switchdev_deferred_enqueue(dev, obj, switchdev_obj_size(obj),
Jiri Pirko4d429c52015-10-14 19:40:52 +0200282 switchdev_port_obj_del_deferred);
283}
284
285/**
286 * switchdev_port_obj_del - Delete port object
287 *
288 * @dev: port device
Jiri Pirko4d429c52015-10-14 19:40:52 +0200289 * @obj: object to delete
290 *
291 * rtnl_lock must be held and must not be in atomic section,
292 * in case SWITCHDEV_F_DEFER flag is not set.
293 */
294int switchdev_port_obj_del(struct net_device *dev,
295 const struct switchdev_obj *obj)
296{
297 if (obj->flags & SWITCHDEV_F_DEFER)
298 return switchdev_port_obj_del_defer(dev, obj);
299 ASSERT_RTNL();
300 return switchdev_port_obj_del_now(dev, obj);
301}
Scott Feldman491d0f12015-05-10 09:47:52 -0700302EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
303
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200304static ATOMIC_NOTIFIER_HEAD(switchdev_notif_chain);
Petr Machataa93e3b12018-11-22 23:28:25 +0000305static BLOCKING_NOTIFIER_HEAD(switchdev_blocking_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100306
307/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700308 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100309 * @nb: notifier_block
310 *
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200311 * Register switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100312 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700313int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100314{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200315 return atomic_notifier_chain_register(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100316}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700317EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100318
319/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700320 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100321 * @nb: notifier_block
322 *
323 * Unregister switch device notifier.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100324 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700325int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100326{
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200327 return atomic_notifier_chain_unregister(&switchdev_notif_chain, nb);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100328}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700329EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100330
331/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700332 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100333 * @val: value passed unmodified to notifier function
334 * @dev: port device
335 * @info: notifier information data
Tian Taoea6754a2020-09-22 21:32:19 +0800336 * @extack: netlink extended ack
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200337 * Call all network notifier blocks.
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100338 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700339int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
Petr Machata66859872019-01-16 23:06:56 +0000340 struct switchdev_notifier_info *info,
341 struct netlink_ext_ack *extack)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100342{
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100343 info->dev = dev;
Petr Machata66859872019-01-16 23:06:56 +0000344 info->extack = extack;
Arkadi Sharshevskyff5cf102017-06-08 08:44:13 +0200345 return atomic_notifier_call_chain(&switchdev_notif_chain, val, info);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100346}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700347EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800348
Petr Machataa93e3b12018-11-22 23:28:25 +0000349int register_switchdev_blocking_notifier(struct notifier_block *nb)
350{
351 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
352
353 return blocking_notifier_chain_register(chain, nb);
354}
355EXPORT_SYMBOL_GPL(register_switchdev_blocking_notifier);
356
357int unregister_switchdev_blocking_notifier(struct notifier_block *nb)
358{
359 struct blocking_notifier_head *chain = &switchdev_blocking_notif_chain;
360
361 return blocking_notifier_chain_unregister(chain, nb);
362}
363EXPORT_SYMBOL_GPL(unregister_switchdev_blocking_notifier);
364
365int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
Petr Machata479c86d2018-12-12 17:02:54 +0000366 struct switchdev_notifier_info *info,
367 struct netlink_ext_ack *extack)
Petr Machataa93e3b12018-11-22 23:28:25 +0000368{
369 info->dev = dev;
Petr Machata479c86d2018-12-12 17:02:54 +0000370 info->extack = extack;
Petr Machataa93e3b12018-11-22 23:28:25 +0000371 return blocking_notifier_call_chain(&switchdev_blocking_notif_chain,
372 val, info);
373}
374EXPORT_SYMBOL_GPL(call_switchdev_blocking_notifiers);
375
Petr Machataf30f0602018-11-22 23:29:44 +0000376static int __switchdev_handle_port_obj_add(struct net_device *dev,
377 struct switchdev_notifier_port_obj_info *port_obj_info,
378 bool (*check_cb)(const struct net_device *dev),
379 int (*add_cb)(struct net_device *dev,
380 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000381 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000382{
Petr Machata69213512018-12-12 17:02:56 +0000383 struct netlink_ext_ack *extack;
Petr Machataf30f0602018-11-22 23:29:44 +0000384 struct net_device *lower_dev;
385 struct list_head *iter;
386 int err = -EOPNOTSUPP;
387
Petr Machata69213512018-12-12 17:02:56 +0000388 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
389
Petr Machataf30f0602018-11-22 23:29:44 +0000390 if (check_cb(dev)) {
Jakub Kicinskic358f952021-01-28 17:09:31 -0800391 err = add_cb(dev, port_obj_info->obj, extack);
Rasmus Villemoes20776b42021-01-25 13:41:16 +0100392 if (err != -EOPNOTSUPP)
393 port_obj_info->handled = true;
394 return err;
Petr Machataf30f0602018-11-22 23:29:44 +0000395 }
396
397 /* Switch ports might be stacked under e.g. a LAG. Ignore the
398 * unsupported devices, another driver might be able to handle them. But
399 * propagate to the callers any hard errors.
400 *
401 * If the driver does its own bookkeeping of stacked ports, it's not
402 * necessary to go through this helper.
403 */
404 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Russell King07c6f982020-02-26 17:14:21 +0000405 if (netif_is_bridge_master(lower_dev))
406 continue;
407
Petr Machataf30f0602018-11-22 23:29:44 +0000408 err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info,
409 check_cb, add_cb);
410 if (err && err != -EOPNOTSUPP)
411 return err;
412 }
413
414 return err;
415}
416
417int switchdev_handle_port_obj_add(struct net_device *dev,
418 struct switchdev_notifier_port_obj_info *port_obj_info,
419 bool (*check_cb)(const struct net_device *dev),
420 int (*add_cb)(struct net_device *dev,
421 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +0000422 struct netlink_ext_ack *extack))
Petr Machataf30f0602018-11-22 23:29:44 +0000423{
424 int err;
425
426 err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb,
427 add_cb);
428 if (err == -EOPNOTSUPP)
429 err = 0;
430 return err;
431}
432EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add);
433
434static int __switchdev_handle_port_obj_del(struct net_device *dev,
435 struct switchdev_notifier_port_obj_info *port_obj_info,
436 bool (*check_cb)(const struct net_device *dev),
437 int (*del_cb)(struct net_device *dev,
438 const struct switchdev_obj *obj))
439{
440 struct net_device *lower_dev;
441 struct list_head *iter;
442 int err = -EOPNOTSUPP;
443
444 if (check_cb(dev)) {
Rasmus Villemoes20776b42021-01-25 13:41:16 +0100445 err = del_cb(dev, port_obj_info->obj);
446 if (err != -EOPNOTSUPP)
447 port_obj_info->handled = true;
448 return err;
Petr Machataf30f0602018-11-22 23:29:44 +0000449 }
450
451 /* Switch ports might be stacked under e.g. a LAG. Ignore the
452 * unsupported devices, another driver might be able to handle them. But
453 * propagate to the callers any hard errors.
454 *
455 * If the driver does its own bookkeeping of stacked ports, it's not
456 * necessary to go through this helper.
457 */
458 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Russell King07c6f982020-02-26 17:14:21 +0000459 if (netif_is_bridge_master(lower_dev))
460 continue;
461
Petr Machataf30f0602018-11-22 23:29:44 +0000462 err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info,
463 check_cb, del_cb);
464 if (err && err != -EOPNOTSUPP)
465 return err;
466 }
467
468 return err;
469}
470
471int switchdev_handle_port_obj_del(struct net_device *dev,
472 struct switchdev_notifier_port_obj_info *port_obj_info,
473 bool (*check_cb)(const struct net_device *dev),
474 int (*del_cb)(struct net_device *dev,
475 const struct switchdev_obj *obj))
476{
477 int err;
478
479 err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb,
480 del_cb);
481 if (err == -EOPNOTSUPP)
482 err = 0;
483 return err;
484}
485EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del);
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800486
487static int __switchdev_handle_port_attr_set(struct net_device *dev,
488 struct switchdev_notifier_port_attr_info *port_attr_info,
489 bool (*check_cb)(const struct net_device *dev),
490 int (*set_cb)(struct net_device *dev,
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200491 const struct switchdev_attr *attr))
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800492{
493 struct net_device *lower_dev;
494 struct list_head *iter;
495 int err = -EOPNOTSUPP;
496
497 if (check_cb(dev)) {
Jakub Kicinskic358f952021-01-28 17:09:31 -0800498 err = set_cb(dev, port_attr_info->attr);
Rasmus Villemoes20776b42021-01-25 13:41:16 +0100499 if (err != -EOPNOTSUPP)
500 port_attr_info->handled = true;
501 return err;
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800502 }
503
504 /* Switch ports might be stacked under e.g. a LAG. Ignore the
505 * unsupported devices, another driver might be able to handle them. But
506 * propagate to the callers any hard errors.
507 *
508 * If the driver does its own bookkeeping of stacked ports, it's not
509 * necessary to go through this helper.
510 */
511 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Russell King07c6f982020-02-26 17:14:21 +0000512 if (netif_is_bridge_master(lower_dev))
513 continue;
514
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800515 err = __switchdev_handle_port_attr_set(lower_dev, port_attr_info,
516 check_cb, set_cb);
517 if (err && err != -EOPNOTSUPP)
518 return err;
519 }
520
521 return err;
522}
523
524int switchdev_handle_port_attr_set(struct net_device *dev,
525 struct switchdev_notifier_port_attr_info *port_attr_info,
526 bool (*check_cb)(const struct net_device *dev),
527 int (*set_cb)(struct net_device *dev,
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200528 const struct switchdev_attr *attr))
Florian Fainelli1cb33af2019-02-27 11:44:25 -0800529{
530 int err;
531
532 err = __switchdev_handle_port_attr_set(dev, port_attr_info, check_cb,
533 set_cb);
534 if (err == -EOPNOTSUPP)
535 err = 0;
536 return err;
537}
538EXPORT_SYMBOL_GPL(switchdev_handle_port_attr_set);