conduit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Handling of a conduit device, switching frames via its switch fabric CPU port
  4. *
  5. * Copyright (c) 2017 Savoir-faire Linux Inc.
  6. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  7. */
  8. #include <linux/ethtool.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/netlink.h>
  11. #include <net/dsa.h>
  12. #include <net/netdev_lock.h>
  13. #include "conduit.h"
  14. #include "dsa.h"
  15. #include "port.h"
  16. #include "tag.h"
  17. static int dsa_conduit_get_regs_len(struct net_device *dev)
  18. {
  19. struct dsa_port *cpu_dp = dev->dsa_ptr;
  20. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  21. struct dsa_switch *ds = cpu_dp->ds;
  22. int port = cpu_dp->index;
  23. int ret = 0;
  24. int len;
  25. if (ops && ops->get_regs_len) {
  26. netdev_lock_ops(dev);
  27. len = ops->get_regs_len(dev);
  28. netdev_unlock_ops(dev);
  29. if (len < 0)
  30. return len;
  31. ret += len;
  32. }
  33. ret += sizeof(struct ethtool_drvinfo);
  34. ret += sizeof(struct ethtool_regs);
  35. if (ds->ops->get_regs_len) {
  36. len = ds->ops->get_regs_len(ds, port);
  37. if (len < 0)
  38. return len;
  39. ret += len;
  40. }
  41. return ret;
  42. }
  43. static void dsa_conduit_get_regs(struct net_device *dev,
  44. struct ethtool_regs *regs, void *data)
  45. {
  46. struct dsa_port *cpu_dp = dev->dsa_ptr;
  47. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  48. struct dsa_switch *ds = cpu_dp->ds;
  49. struct ethtool_drvinfo *cpu_info;
  50. struct ethtool_regs *cpu_regs;
  51. int port = cpu_dp->index;
  52. int len;
  53. if (ops && ops->get_regs_len && ops->get_regs) {
  54. netdev_lock_ops(dev);
  55. len = ops->get_regs_len(dev);
  56. if (len < 0) {
  57. netdev_unlock_ops(dev);
  58. return;
  59. }
  60. regs->len = len;
  61. ops->get_regs(dev, regs, data);
  62. netdev_unlock_ops(dev);
  63. data += regs->len;
  64. }
  65. cpu_info = (struct ethtool_drvinfo *)data;
  66. strscpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver));
  67. data += sizeof(*cpu_info);
  68. cpu_regs = (struct ethtool_regs *)data;
  69. data += sizeof(*cpu_regs);
  70. if (ds->ops->get_regs_len && ds->ops->get_regs) {
  71. len = ds->ops->get_regs_len(ds, port);
  72. if (len < 0)
  73. return;
  74. cpu_regs->len = len;
  75. ds->ops->get_regs(ds, port, cpu_regs, data);
  76. }
  77. }
  78. static ssize_t dsa_conduit_append_port_stats(struct dsa_switch *ds, int port,
  79. u64 *data, size_t start)
  80. {
  81. int count;
  82. if (!ds->ops->get_sset_count)
  83. return 0;
  84. count = ds->ops->get_sset_count(ds, port, ETH_SS_STATS);
  85. if (count < 0)
  86. return count;
  87. if (ds->ops->get_ethtool_stats)
  88. ds->ops->get_ethtool_stats(ds, port, data + start);
  89. return count;
  90. }
  91. static void dsa_conduit_get_ethtool_stats(struct net_device *dev,
  92. struct ethtool_stats *stats,
  93. u64 *data)
  94. {
  95. struct dsa_port *dp, *cpu_dp = dev->dsa_ptr;
  96. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  97. struct dsa_switch_tree *dst = cpu_dp->dst;
  98. int count, mcount = 0;
  99. if (ops && ops->get_sset_count && ops->get_ethtool_stats) {
  100. netdev_lock_ops(dev);
  101. mcount = ops->get_sset_count(dev, ETH_SS_STATS);
  102. ops->get_ethtool_stats(dev, stats, data);
  103. netdev_unlock_ops(dev);
  104. }
  105. list_for_each_entry(dp, &dst->ports, list) {
  106. if (!dsa_port_is_dsa(dp) && !dsa_port_is_cpu(dp))
  107. continue;
  108. count = dsa_conduit_append_port_stats(dp->ds, dp->index,
  109. data, mcount);
  110. if (count < 0)
  111. return;
  112. mcount += count;
  113. }
  114. }
  115. static void dsa_conduit_get_ethtool_phy_stats(struct net_device *dev,
  116. struct ethtool_stats *stats,
  117. u64 *data)
  118. {
  119. struct dsa_port *cpu_dp = dev->dsa_ptr;
  120. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  121. struct dsa_switch *ds = cpu_dp->ds;
  122. int port = cpu_dp->index;
  123. int count = 0;
  124. if (dev->phydev && (!ops || !ops->get_ethtool_phy_stats)) {
  125. count = phy_ethtool_get_sset_count(dev->phydev);
  126. if (count >= 0)
  127. phy_ethtool_get_stats(dev->phydev, stats, data);
  128. } else if (ops && ops->get_sset_count && ops->get_ethtool_phy_stats) {
  129. netdev_lock_ops(dev);
  130. count = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
  131. ops->get_ethtool_phy_stats(dev, stats, data);
  132. netdev_unlock_ops(dev);
  133. }
  134. if (count < 0)
  135. count = 0;
  136. if (ds->ops->get_ethtool_phy_stats)
  137. ds->ops->get_ethtool_phy_stats(ds, port, data + count);
  138. }
  139. static void dsa_conduit_append_port_sset_count(struct dsa_switch *ds, int port,
  140. int sset, int *count)
  141. {
  142. if (ds->ops->get_sset_count)
  143. *count += ds->ops->get_sset_count(ds, port, sset);
  144. }
  145. static int dsa_conduit_get_sset_count(struct net_device *dev, int sset)
  146. {
  147. struct dsa_port *dp, *cpu_dp = dev->dsa_ptr;
  148. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  149. struct dsa_switch_tree *dst = cpu_dp->dst;
  150. int count = 0;
  151. netdev_lock_ops(dev);
  152. if (sset == ETH_SS_PHY_STATS && dev->phydev &&
  153. (!ops || !ops->get_ethtool_phy_stats))
  154. count = phy_ethtool_get_sset_count(dev->phydev);
  155. else if (ops && ops->get_sset_count)
  156. count = ops->get_sset_count(dev, sset);
  157. netdev_unlock_ops(dev);
  158. if (count < 0)
  159. count = 0;
  160. list_for_each_entry(dp, &dst->ports, list) {
  161. if (!dsa_port_is_dsa(dp) && !dsa_port_is_cpu(dp))
  162. continue;
  163. dsa_conduit_append_port_sset_count(dp->ds, dp->index, sset,
  164. &count);
  165. }
  166. return count;
  167. }
  168. static ssize_t dsa_conduit_append_port_strings(struct dsa_switch *ds, int port,
  169. u32 stringset, u8 *data,
  170. size_t start)
  171. {
  172. int len = ETH_GSTRING_LEN;
  173. u8 pfx[8], *ndata;
  174. int count, i;
  175. if (!ds->ops->get_strings)
  176. return 0;
  177. snprintf(pfx, sizeof(pfx), "s%.2d_p%.2d", ds->index, port);
  178. /* We do not want to be NULL-terminated, since this is a prefix */
  179. pfx[sizeof(pfx) - 1] = '_';
  180. ndata = data + start * len;
  181. /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
  182. * the output after to prepend our CPU port prefix we
  183. * constructed earlier
  184. */
  185. ds->ops->get_strings(ds, port, stringset, ndata);
  186. count = ds->ops->get_sset_count(ds, port, stringset);
  187. if (count < 0)
  188. return count;
  189. for (i = 0; i < count; i++) {
  190. memmove(ndata + (i * len + sizeof(pfx)),
  191. ndata + i * len, len - sizeof(pfx));
  192. memcpy(ndata + i * len, pfx, sizeof(pfx));
  193. }
  194. return count;
  195. }
  196. static void dsa_conduit_get_strings(struct net_device *dev, u32 stringset,
  197. u8 *data)
  198. {
  199. struct dsa_port *dp, *cpu_dp = dev->dsa_ptr;
  200. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  201. struct dsa_switch_tree *dst = cpu_dp->dst;
  202. int count, mcount = 0;
  203. netdev_lock_ops(dev);
  204. if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
  205. !ops->get_ethtool_phy_stats) {
  206. mcount = phy_ethtool_get_sset_count(dev->phydev);
  207. if (mcount < 0)
  208. mcount = 0;
  209. else
  210. phy_ethtool_get_strings(dev->phydev, data);
  211. } else if (ops->get_sset_count && ops->get_strings) {
  212. mcount = ops->get_sset_count(dev, stringset);
  213. if (mcount < 0)
  214. mcount = 0;
  215. ops->get_strings(dev, stringset, data);
  216. }
  217. netdev_unlock_ops(dev);
  218. list_for_each_entry(dp, &dst->ports, list) {
  219. if (!dsa_port_is_dsa(dp) && !dsa_port_is_cpu(dp))
  220. continue;
  221. count = dsa_conduit_append_port_strings(dp->ds, dp->index,
  222. stringset, data,
  223. mcount);
  224. if (count < 0)
  225. return;
  226. mcount += count;
  227. }
  228. }
  229. /* Deny PTP operations on conduit if there is at least one switch in the tree
  230. * that is PTP capable.
  231. */
  232. int __dsa_conduit_hwtstamp_validate(struct net_device *dev,
  233. const struct kernel_hwtstamp_config *config,
  234. struct netlink_ext_ack *extack)
  235. {
  236. struct dsa_port *cpu_dp = dev->dsa_ptr;
  237. struct dsa_switch *ds = cpu_dp->ds;
  238. struct dsa_switch_tree *dst;
  239. struct dsa_port *dp;
  240. dst = ds->dst;
  241. list_for_each_entry(dp, &dst->ports, list) {
  242. if (dsa_port_supports_hwtstamp(dp)) {
  243. NL_SET_ERR_MSG(extack,
  244. "HW timestamping not allowed on DSA conduit when switch supports the operation");
  245. return -EBUSY;
  246. }
  247. }
  248. return 0;
  249. }
  250. static int dsa_conduit_ethtool_setup(struct net_device *dev)
  251. {
  252. struct dsa_port *cpu_dp = dev->dsa_ptr;
  253. struct dsa_switch *ds = cpu_dp->ds;
  254. struct ethtool_ops *ops;
  255. if (netif_is_lag_master(dev))
  256. return 0;
  257. ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
  258. if (!ops)
  259. return -ENOMEM;
  260. cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
  261. if (cpu_dp->orig_ethtool_ops)
  262. memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
  263. ops->get_regs_len = dsa_conduit_get_regs_len;
  264. ops->get_regs = dsa_conduit_get_regs;
  265. ops->get_sset_count = dsa_conduit_get_sset_count;
  266. ops->get_ethtool_stats = dsa_conduit_get_ethtool_stats;
  267. ops->get_strings = dsa_conduit_get_strings;
  268. ops->get_ethtool_phy_stats = dsa_conduit_get_ethtool_phy_stats;
  269. dev->ethtool_ops = ops;
  270. return 0;
  271. }
  272. static void dsa_conduit_ethtool_teardown(struct net_device *dev)
  273. {
  274. struct dsa_port *cpu_dp = dev->dsa_ptr;
  275. if (netif_is_lag_master(dev))
  276. return;
  277. dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
  278. cpu_dp->orig_ethtool_ops = NULL;
  279. }
  280. /* Keep the conduit always promiscuous if the tagging protocol requires that
  281. * (garbles MAC DA) or if it doesn't support unicast filtering, case in which
  282. * it would revert to promiscuous mode as soon as we call dev_uc_add() on it
  283. * anyway.
  284. */
  285. static void dsa_conduit_set_promiscuity(struct net_device *dev, int inc)
  286. {
  287. const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops;
  288. if ((dev->priv_flags & IFF_UNICAST_FLT) && !ops->promisc_on_conduit)
  289. return;
  290. ASSERT_RTNL();
  291. dev_set_promiscuity(dev, inc);
  292. }
  293. static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
  294. char *buf)
  295. {
  296. struct net_device *dev = to_net_dev(d);
  297. struct dsa_port *cpu_dp = dev->dsa_ptr;
  298. return sysfs_emit(buf, "%s\n",
  299. dsa_tag_protocol_to_str(cpu_dp->tag_ops));
  300. }
  301. static ssize_t tagging_store(struct device *d, struct device_attribute *attr,
  302. const char *buf, size_t count)
  303. {
  304. const struct dsa_device_ops *new_tag_ops, *old_tag_ops;
  305. const char *end = strchrnul(buf, '\n'), *name;
  306. struct net_device *dev = to_net_dev(d);
  307. struct dsa_port *cpu_dp = dev->dsa_ptr;
  308. size_t len = end - buf;
  309. int err;
  310. /* Empty string passed */
  311. if (!len)
  312. return -ENOPROTOOPT;
  313. name = kstrndup(buf, len, GFP_KERNEL);
  314. if (!name)
  315. return -ENOMEM;
  316. old_tag_ops = cpu_dp->tag_ops;
  317. new_tag_ops = dsa_tag_driver_get_by_name(name);
  318. kfree(name);
  319. /* Bad tagger name? */
  320. if (IS_ERR(new_tag_ops))
  321. return PTR_ERR(new_tag_ops);
  322. if (new_tag_ops == old_tag_ops)
  323. /* Drop the temporarily held duplicate reference, since
  324. * the DSA switch tree uses this tagger.
  325. */
  326. goto out;
  327. err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, new_tag_ops,
  328. old_tag_ops);
  329. if (err) {
  330. /* On failure the old tagger is restored, so we don't need the
  331. * driver for the new one.
  332. */
  333. dsa_tag_driver_put(new_tag_ops);
  334. return err;
  335. }
  336. /* On success we no longer need the module for the old tagging protocol
  337. */
  338. out:
  339. dsa_tag_driver_put(old_tag_ops);
  340. return count;
  341. }
  342. static DEVICE_ATTR_RW(tagging);
  343. static struct attribute *dsa_user_attrs[] = {
  344. &dev_attr_tagging.attr,
  345. NULL
  346. };
  347. static const struct attribute_group dsa_group = {
  348. .name = "dsa",
  349. .attrs = dsa_user_attrs,
  350. };
  351. static void dsa_conduit_reset_mtu(struct net_device *dev)
  352. {
  353. int err;
  354. err = dev_set_mtu(dev, ETH_DATA_LEN);
  355. if (err)
  356. netdev_dbg(dev,
  357. "Unable to reset MTU to exclude DSA overheads\n");
  358. }
  359. int dsa_conduit_setup(struct net_device *dev, struct dsa_port *cpu_dp)
  360. {
  361. const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops;
  362. struct dsa_switch *ds = cpu_dp->ds;
  363. struct device_link *consumer_link;
  364. int mtu, ret;
  365. mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops);
  366. /* The DSA conduit must use SET_NETDEV_DEV for this to work. */
  367. if (!netif_is_lag_master(dev)) {
  368. consumer_link = device_link_add(ds->dev, dev->dev.parent,
  369. DL_FLAG_AUTOREMOVE_CONSUMER);
  370. if (!consumer_link)
  371. netdev_err(dev,
  372. "Failed to create a device link to DSA switch %s\n",
  373. dev_name(ds->dev));
  374. }
  375. /* The switch driver may not implement ->port_change_mtu(), case in
  376. * which dsa_user_change_mtu() will not update the conduit MTU either,
  377. * so we need to do that here.
  378. */
  379. ret = dev_set_mtu(dev, mtu);
  380. if (ret)
  381. netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n",
  382. ret, mtu);
  383. /* If we use a tagging format that doesn't have an ethertype
  384. * field, make sure that all packets from this point on get
  385. * sent to the tag format's receive function.
  386. */
  387. wmb();
  388. dev->dsa_ptr = cpu_dp;
  389. dsa_conduit_set_promiscuity(dev, 1);
  390. ret = dsa_conduit_ethtool_setup(dev);
  391. if (ret)
  392. goto out_err_reset_promisc;
  393. ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
  394. if (ret)
  395. goto out_err_ethtool_teardown;
  396. return ret;
  397. out_err_ethtool_teardown:
  398. dsa_conduit_ethtool_teardown(dev);
  399. out_err_reset_promisc:
  400. dsa_conduit_set_promiscuity(dev, -1);
  401. return ret;
  402. }
  403. void dsa_conduit_teardown(struct net_device *dev)
  404. {
  405. sysfs_remove_group(&dev->dev.kobj, &dsa_group);
  406. dsa_conduit_ethtool_teardown(dev);
  407. dsa_conduit_reset_mtu(dev);
  408. dsa_conduit_set_promiscuity(dev, -1);
  409. dev->dsa_ptr = NULL;
  410. /* If we used a tagging format that doesn't have an ethertype
  411. * field, make sure that all packets from this point get sent
  412. * without the tag and go through the regular receive path.
  413. */
  414. wmb();
  415. }
  416. int dsa_conduit_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp,
  417. struct netdev_lag_upper_info *uinfo,
  418. struct netlink_ext_ack *extack)
  419. {
  420. bool conduit_setup = false;
  421. int err;
  422. if (!netdev_uses_dsa(lag_dev)) {
  423. err = dsa_conduit_setup(lag_dev, cpu_dp);
  424. if (err)
  425. return err;
  426. conduit_setup = true;
  427. }
  428. err = dsa_port_lag_join(cpu_dp, lag_dev, uinfo, extack);
  429. if (err) {
  430. NL_SET_ERR_MSG_WEAK_MOD(extack, "CPU port failed to join LAG");
  431. goto out_conduit_teardown;
  432. }
  433. return 0;
  434. out_conduit_teardown:
  435. if (conduit_setup)
  436. dsa_conduit_teardown(lag_dev);
  437. return err;
  438. }
  439. /* Tear down a conduit if there isn't any other user port on it,
  440. * optionally also destroying LAG information.
  441. */
  442. void dsa_conduit_lag_teardown(struct net_device *lag_dev,
  443. struct dsa_port *cpu_dp)
  444. {
  445. struct net_device *upper;
  446. struct list_head *iter;
  447. dsa_port_lag_leave(cpu_dp, lag_dev);
  448. netdev_for_each_upper_dev_rcu(lag_dev, upper, iter)
  449. if (dsa_user_dev_check(upper))
  450. return;
  451. dsa_conduit_teardown(lag_dev);
  452. }