Skip to content

Commit f30f060

Browse files
pmachatadavem330
authored andcommitted
switchdev: Add helpers to aid traversal through lower devices
After the transition from switchdev operations to notifier chain (which will take place in following patches), the onus is on the driver to find its own devices below possible layer of LAG or other uppers. The logic to do so is fairly repetitive: each driver is looking for its own devices among the lowers of the notified device. For those that it finds, it calls a handler. To indicate that the event was handled, struct switchdev_notifier_port_obj_info.handled is set. The differences lie only in what constitutes an "own" device and what handler to call. Therefore abstract this logic into two helpers, switchdev_handle_port_obj_add() and switchdev_handle_port_obj_del(). If a driver only supports physical ports under a bridge device, it will simply avoid this layer of indirection. One area where this helper diverges from the current switchdev behavior is the case of mixed lowers, some of which are switchdev ports and some of which are not. Previously, such scenario would fail with -EOPNOTSUPP. The helper could do that for lowers for which the passed-in predicate doesn't hold. That would however break the case that switchdev ports from several different drivers are stashed under one master, a scenario that switchdev currently happily supports. Therefore tolerate any and all unknown netdevices, whether they are backed by a switchdev driver or not. Signed-off-by: Petr Machata <petrm@mellanox.com> Reviewed-by: Ido Schimmel <idosch@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a39b888 commit f30f060

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎include/net/switchdev.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ void switchdev_port_fwd_mark_set(struct net_device *dev,
210210
bool switchdev_port_same_parent_id(struct net_device *a,
211211
struct net_device *b);
212212

213+
int switchdev_handle_port_obj_add(struct net_device *dev,
214+
struct switchdev_notifier_port_obj_info *port_obj_info,
215+
bool (*check_cb)(const struct net_device *dev),
216+
int (*add_cb)(struct net_device *dev,
217+
const struct switchdev_obj *obj,
218+
struct switchdev_trans *trans));
219+
int switchdev_handle_port_obj_del(struct net_device *dev,
220+
struct switchdev_notifier_port_obj_info *port_obj_info,
221+
bool (*check_cb)(const struct net_device *dev),
222+
int (*del_cb)(struct net_device *dev,
223+
const struct switchdev_obj *obj));
224+
213225
#define SWITCHDEV_SET_OPS(netdev, ops) ((netdev)->switchdev_ops = (ops))
214226
#else
215227

@@ -284,6 +296,27 @@ static inline bool switchdev_port_same_parent_id(struct net_device *a,
284296
return false;
285297
}
286298

299+
static inline int
300+
switchdev_handle_port_obj_add(struct net_device *dev,
301+
struct switchdev_notifier_port_obj_info *port_obj_info,
302+
bool (*check_cb)(const struct net_device *dev),
303+
int (*add_cb)(struct net_device *dev,
304+
const struct switchdev_obj *obj,
305+
struct switchdev_trans *trans))
306+
{
307+
return 0;
308+
}
309+
310+
static inline int
311+
switchdev_handle_port_obj_del(struct net_device *dev,
312+
struct switchdev_notifier_port_obj_info *port_obj_info,
313+
bool (*check_cb)(const struct net_device *dev),
314+
int (*del_cb)(struct net_device *dev,
315+
const struct switchdev_obj *obj))
316+
{
317+
return 0;
318+
}
319+
287320
#define SWITCHDEV_SET_OPS(netdev, ops) do {} while (0)
288321

289322
#endif

‎net/switchdev/switchdev.c‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,103 @@ bool switchdev_port_same_parent_id(struct net_device *a,
621621
return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
622622
}
623623
EXPORT_SYMBOL_GPL(switchdev_port_same_parent_id);
624+
625+
static int __switchdev_handle_port_obj_add(struct net_device *dev,
626+
struct switchdev_notifier_port_obj_info *port_obj_info,
627+
bool (*check_cb)(const struct net_device *dev),
628+
int (*add_cb)(struct net_device *dev,
629+
const struct switchdev_obj *obj,
630+
struct switchdev_trans *trans))
631+
{
632+
struct net_device *lower_dev;
633+
struct list_head *iter;
634+
int err = -EOPNOTSUPP;
635+
636+
if (check_cb(dev)) {
637+
/* This flag is only checked if the return value is success. */
638+
port_obj_info->handled = true;
639+
return add_cb(dev, port_obj_info->obj, port_obj_info->trans);
640+
}
641+
642+
/* Switch ports might be stacked under e.g. a LAG. Ignore the
643+
* unsupported devices, another driver might be able to handle them. But
644+
* propagate to the callers any hard errors.
645+
*
646+
* If the driver does its own bookkeeping of stacked ports, it's not
647+
* necessary to go through this helper.
648+
*/
649+
netdev_for_each_lower_dev(dev, lower_dev, iter) {
650+
err = __switchdev_handle_port_obj_add(lower_dev, port_obj_info,
651+
check_cb, add_cb);
652+
if (err && err != -EOPNOTSUPP)
653+
return err;
654+
}
655+
656+
return err;
657+
}
658+
659+
int switchdev_handle_port_obj_add(struct net_device *dev,
660+
struct switchdev_notifier_port_obj_info *port_obj_info,
661+
bool (*check_cb)(const struct net_device *dev),
662+
int (*add_cb)(struct net_device *dev,
663+
const struct switchdev_obj *obj,
664+
struct switchdev_trans *trans))
665+
{
666+
int err;
667+
668+
err = __switchdev_handle_port_obj_add(dev, port_obj_info, check_cb,
669+
add_cb);
670+
if (err == -EOPNOTSUPP)
671+
err = 0;
672+
return err;
673+
}
674+
EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_add);
675+
676+
static int __switchdev_handle_port_obj_del(struct net_device *dev,
677+
struct switchdev_notifier_port_obj_info *port_obj_info,
678+
bool (*check_cb)(const struct net_device *dev),
679+
int (*del_cb)(struct net_device *dev,
680+
const struct switchdev_obj *obj))
681+
{
682+
struct net_device *lower_dev;
683+
struct list_head *iter;
684+
int err = -EOPNOTSUPP;
685+
686+
if (check_cb(dev)) {
687+
/* This flag is only checked if the return value is success. */
688+
port_obj_info->handled = true;
689+
return del_cb(dev, port_obj_info->obj);
690+
}
691+
692+
/* Switch ports might be stacked under e.g. a LAG. Ignore the
693+
* unsupported devices, another driver might be able to handle them. But
694+
* propagate to the callers any hard errors.
695+
*
696+
* If the driver does its own bookkeeping of stacked ports, it's not
697+
* necessary to go through this helper.
698+
*/
699+
netdev_for_each_lower_dev(dev, lower_dev, iter) {
700+
err = __switchdev_handle_port_obj_del(lower_dev, port_obj_info,
701+
check_cb, del_cb);
702+
if (err && err != -EOPNOTSUPP)
703+
return err;
704+
}
705+
706+
return err;
707+
}
708+
709+
int switchdev_handle_port_obj_del(struct net_device *dev,
710+
struct switchdev_notifier_port_obj_info *port_obj_info,
711+
bool (*check_cb)(const struct net_device *dev),
712+
int (*del_cb)(struct net_device *dev,
713+
const struct switchdev_obj *obj))
714+
{
715+
int err;
716+
717+
err = __switchdev_handle_port_obj_del(dev, port_obj_info, check_cb,
718+
del_cb);
719+
if (err == -EOPNOTSUPP)
720+
err = 0;
721+
return err;
722+
}
723+
EXPORT_SYMBOL_GPL(switchdev_handle_port_obj_del);

0 commit comments

Comments
 (0)