703-10-v5.16-net-dsa-introduce-helpers-for-iterating-through-port.patch 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. From 82b318983c515f29b8b3a0dad9f6a5fe8a68a7f4 Mon Sep 17 00:00:00 2001
  2. From: Vladimir Oltean <[email protected]>
  3. Date: Wed, 20 Oct 2021 20:49:49 +0300
  4. Subject: [PATCH] net: dsa: introduce helpers for iterating through ports using
  5. dp
  6. Since the DSA conversion from the ds->ports array into the dst->ports
  7. list, the DSA API has encouraged driver writers, as well as the core
  8. itself, to write inefficient code.
  9. Currently, code that wants to filter by a specific type of port when
  10. iterating, like {!unused, user, cpu, dsa}, uses the dsa_is_*_port helper.
  11. Under the hood, this uses dsa_to_port which iterates again through
  12. dst->ports. But the driver iterates through the port list already, so
  13. the complexity is quadratic for the typical case of a single-switch
  14. tree.
  15. This patch introduces some iteration helpers where the iterator is
  16. already a struct dsa_port *dp, so that the other variant of the
  17. filtering functions, dsa_port_is_{unused,user,cpu_dsa}, can be used
  18. directly on the iterator. This eliminates the second lookup.
  19. These functions can be used both by the core and by drivers.
  20. Signed-off-by: Vladimir Oltean <[email protected]>
  21. Reviewed-by: Florian Fainelli <[email protected]>
  22. Signed-off-by: David S. Miller <[email protected]>
  23. ---
  24. include/net/dsa.h | 28 ++++++++++++++++++++++++++++
  25. 1 file changed, 28 insertions(+)
  26. --- a/include/net/dsa.h
  27. +++ b/include/net/dsa.h
  28. @@ -476,6 +476,34 @@ static inline bool dsa_is_user_port(stru
  29. return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
  30. }
  31. +#define dsa_tree_for_each_user_port(_dp, _dst) \
  32. + list_for_each_entry((_dp), &(_dst)->ports, list) \
  33. + if (dsa_port_is_user((_dp)))
  34. +
  35. +#define dsa_switch_for_each_port(_dp, _ds) \
  36. + list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
  37. + if ((_dp)->ds == (_ds))
  38. +
  39. +#define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
  40. + list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
  41. + if ((_dp)->ds == (_ds))
  42. +
  43. +#define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
  44. + list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
  45. + if ((_dp)->ds == (_ds))
  46. +
  47. +#define dsa_switch_for_each_available_port(_dp, _ds) \
  48. + dsa_switch_for_each_port((_dp), (_ds)) \
  49. + if (!dsa_port_is_unused((_dp)))
  50. +
  51. +#define dsa_switch_for_each_user_port(_dp, _ds) \
  52. + dsa_switch_for_each_port((_dp), (_ds)) \
  53. + if (dsa_port_is_user((_dp)))
  54. +
  55. +#define dsa_switch_for_each_cpu_port(_dp, _ds) \
  56. + dsa_switch_for_each_port((_dp), (_ds)) \
  57. + if (dsa_port_is_cpu((_dp)))
  58. +
  59. static inline u32 dsa_user_ports(struct dsa_switch *ds)
  60. {
  61. u32 mask = 0;