772-v5.12-net-dsa-don-t-use-switchdev_notifier_fdb_info-in-dsa.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. From c4bb76a9a0ef87c4cc1f636defed5f12deb9f5a7 Mon Sep 17 00:00:00 2001
  2. From: Vladimir Oltean <[email protected]>
  3. Date: Wed, 6 Jan 2021 11:51:32 +0200
  4. Subject: [PATCH] net: dsa: don't use switchdev_notifier_fdb_info in
  5. dsa_switchdev_event_work
  6. Currently DSA doesn't add FDB entries on the CPU port, because it only
  7. does so through switchdev, which is associated with a net_device, and
  8. there are none of those for the CPU port.
  9. But actually FDB addresses on the CPU port have some use cases of their
  10. own, if the switchdev operations are initiated from within the DSA
  11. layer. There is just one problem with the existing code: it passes a
  12. structure in dsa_switchdev_event_work which was retrieved directly from
  13. switchdev, so it contains a net_device. We need to generalize the
  14. contents to something that covers the CPU port as well: the "ds, port"
  15. tuple is fine for that.
  16. Note that the new procedure for notifying the successful FDB offload is
  17. inspired from the rocker model.
  18. Also, nothing was being done if added_by_user was false. Let's check for
  19. that a lot earlier, and don't actually bother to schedule the worker
  20. for nothing.
  21. Signed-off-by: Vladimir Oltean <[email protected]>
  22. Reviewed-by: Florian Fainelli <[email protected]>
  23. Signed-off-by: Jakub Kicinski <[email protected]>
  24. ---
  25. net/dsa/dsa_priv.h | 12 +++++
  26. net/dsa/slave.c | 106 ++++++++++++++++++++++-----------------------
  27. 2 files changed, 65 insertions(+), 53 deletions(-)
  28. --- a/net/dsa/dsa_priv.h
  29. +++ b/net/dsa/dsa_priv.h
  30. @@ -73,6 +73,18 @@ struct dsa_notifier_mtu_info {
  31. int mtu;
  32. };
  33. +struct dsa_switchdev_event_work {
  34. + struct dsa_switch *ds;
  35. + int port;
  36. + struct work_struct work;
  37. + unsigned long event;
  38. + /* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
  39. + * SWITCHDEV_FDB_DEL_TO_DEVICE
  40. + */
  41. + unsigned char addr[ETH_ALEN];
  42. + u16 vid;
  43. +};
  44. +
  45. struct dsa_slave_priv {
  46. /* Copy of CPU port xmit for faster access in slave transmit hot path */
  47. struct sk_buff * (*xmit)(struct sk_buff *skb,
  48. --- a/net/dsa/slave.c
  49. +++ b/net/dsa/slave.c
  50. @@ -2005,76 +2005,66 @@ static int dsa_slave_netdevice_event(str
  51. return NOTIFY_DONE;
  52. }
  53. -struct dsa_switchdev_event_work {
  54. - struct work_struct work;
  55. - struct switchdev_notifier_fdb_info fdb_info;
  56. - struct net_device *dev;
  57. - unsigned long event;
  58. -};
  59. +static void
  60. +dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
  61. +{
  62. + struct dsa_switch *ds = switchdev_work->ds;
  63. + struct switchdev_notifier_fdb_info info;
  64. + struct dsa_port *dp;
  65. +
  66. + if (!dsa_is_user_port(ds, switchdev_work->port))
  67. + return;
  68. +
  69. + info.addr = switchdev_work->addr;
  70. + info.vid = switchdev_work->vid;
  71. + info.offloaded = true;
  72. + dp = dsa_to_port(ds, switchdev_work->port);
  73. + call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
  74. + dp->slave, &info.info, NULL);
  75. +}
  76. static void dsa_slave_switchdev_event_work(struct work_struct *work)
  77. {
  78. struct dsa_switchdev_event_work *switchdev_work =
  79. container_of(work, struct dsa_switchdev_event_work, work);
  80. - struct net_device *dev = switchdev_work->dev;
  81. - struct switchdev_notifier_fdb_info *fdb_info;
  82. - struct dsa_port *dp = dsa_slave_to_port(dev);
  83. + struct dsa_switch *ds = switchdev_work->ds;
  84. + struct dsa_port *dp;
  85. int err;
  86. + dp = dsa_to_port(ds, switchdev_work->port);
  87. +
  88. rtnl_lock();
  89. switch (switchdev_work->event) {
  90. case SWITCHDEV_FDB_ADD_TO_DEVICE:
  91. - fdb_info = &switchdev_work->fdb_info;
  92. - if (!fdb_info->added_by_user)
  93. - break;
  94. -
  95. - err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
  96. + err = dsa_port_fdb_add(dp, switchdev_work->addr,
  97. + switchdev_work->vid);
  98. if (err) {
  99. - netdev_err(dev,
  100. - "failed to add %pM vid %d to fdb: %d\n",
  101. - fdb_info->addr, fdb_info->vid, err);
  102. + dev_err(ds->dev,
  103. + "port %d failed to add %pM vid %d to fdb: %d\n",
  104. + dp->index, switchdev_work->addr,
  105. + switchdev_work->vid, err);
  106. break;
  107. }
  108. - fdb_info->offloaded = true;
  109. - call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
  110. - &fdb_info->info, NULL);
  111. + dsa_fdb_offload_notify(switchdev_work);
  112. break;
  113. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  114. - fdb_info = &switchdev_work->fdb_info;
  115. - if (!fdb_info->added_by_user)
  116. - break;
  117. -
  118. - err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
  119. + err = dsa_port_fdb_del(dp, switchdev_work->addr,
  120. + switchdev_work->vid);
  121. if (err) {
  122. - netdev_err(dev,
  123. - "failed to delete %pM vid %d from fdb: %d\n",
  124. - fdb_info->addr, fdb_info->vid, err);
  125. + dev_err(ds->dev,
  126. + "port %d failed to delete %pM vid %d from fdb: %d\n",
  127. + dp->index, switchdev_work->addr,
  128. + switchdev_work->vid, err);
  129. }
  130. break;
  131. }
  132. rtnl_unlock();
  133. - kfree(switchdev_work->fdb_info.addr);
  134. kfree(switchdev_work);
  135. - dev_put(dev);
  136. -}
  137. -
  138. -static int
  139. -dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
  140. - switchdev_work,
  141. - const struct switchdev_notifier_fdb_info *
  142. - fdb_info)
  143. -{
  144. - memcpy(&switchdev_work->fdb_info, fdb_info,
  145. - sizeof(switchdev_work->fdb_info));
  146. - switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
  147. - if (!switchdev_work->fdb_info.addr)
  148. - return -ENOMEM;
  149. - ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
  150. - fdb_info->addr);
  151. - return 0;
  152. + if (dsa_is_user_port(ds, dp->index))
  153. + dev_put(dp->slave);
  154. }
  155. /* Called under rcu_read_lock() */
  156. @@ -2082,7 +2072,9 @@ static int dsa_slave_switchdev_event(str
  157. unsigned long event, void *ptr)
  158. {
  159. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  160. + const struct switchdev_notifier_fdb_info *fdb_info;
  161. struct dsa_switchdev_event_work *switchdev_work;
  162. + struct dsa_port *dp;
  163. int err;
  164. if (event == SWITCHDEV_PORT_ATTR_SET) {
  165. @@ -2095,20 +2087,32 @@ static int dsa_slave_switchdev_event(str
  166. if (!dsa_slave_dev_check(dev))
  167. return NOTIFY_DONE;
  168. + dp = dsa_slave_to_port(dev);
  169. +
  170. switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
  171. if (!switchdev_work)
  172. return NOTIFY_BAD;
  173. INIT_WORK(&switchdev_work->work,
  174. dsa_slave_switchdev_event_work);
  175. - switchdev_work->dev = dev;
  176. + switchdev_work->ds = dp->ds;
  177. + switchdev_work->port = dp->index;
  178. switchdev_work->event = event;
  179. switch (event) {
  180. case SWITCHDEV_FDB_ADD_TO_DEVICE:
  181. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  182. - if (dsa_slave_switchdev_fdb_work_init(switchdev_work, ptr))
  183. - goto err_fdb_work_init;
  184. + fdb_info = ptr;
  185. +
  186. + if (!fdb_info->added_by_user) {
  187. + kfree(switchdev_work);
  188. + return NOTIFY_OK;
  189. + }
  190. +
  191. + ether_addr_copy(switchdev_work->addr,
  192. + fdb_info->addr);
  193. + switchdev_work->vid = fdb_info->vid;
  194. +
  195. dev_hold(dev);
  196. break;
  197. default:
  198. @@ -2118,10 +2122,6 @@ static int dsa_slave_switchdev_event(str
  199. dsa_schedule_work(&switchdev_work->work);
  200. return NOTIFY_OK;
  201. -
  202. -err_fdb_work_init:
  203. - kfree(switchdev_work);
  204. - return NOTIFY_BAD;
  205. }
  206. static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,