selftests.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2019 Synopsys, Inc. and/or its affiliates.
  4. * stmmac Selftests Support
  5. *
  6. * Author: Jose Abreu <joabreu@synopsys.com>
  7. *
  8. * Ported from stmmac by:
  9. * Copyright (C) 2021 Oleksij Rempel <o.rempel@pengutronix.de>
  10. */
  11. #include <linux/phy.h>
  12. #include <net/selftests.h>
  13. #include <net/tcp.h>
  14. #include <net/udp.h>
  15. static u8 net_test_next_id;
  16. struct sk_buff *net_test_get_skb(struct net_device *ndev, u8 id,
  17. struct net_packet_attrs *attr)
  18. {
  19. struct sk_buff *skb = NULL;
  20. struct udphdr *uhdr = NULL;
  21. struct tcphdr *thdr = NULL;
  22. struct netsfhdr *shdr;
  23. struct ethhdr *ehdr;
  24. struct iphdr *ihdr;
  25. int iplen, size;
  26. size = attr->size + NET_TEST_PKT_SIZE;
  27. if (attr->tcp)
  28. size += sizeof(struct tcphdr);
  29. else
  30. size += sizeof(struct udphdr);
  31. if (attr->max_size && attr->max_size > size)
  32. size = attr->max_size;
  33. skb = netdev_alloc_skb(ndev, size);
  34. if (!skb)
  35. return NULL;
  36. prefetchw(skb->data);
  37. ehdr = skb_push(skb, ETH_HLEN);
  38. skb_reset_mac_header(skb);
  39. skb_set_network_header(skb, skb->len);
  40. ihdr = skb_put(skb, sizeof(*ihdr));
  41. skb_set_transport_header(skb, skb->len);
  42. if (attr->tcp)
  43. thdr = skb_put(skb, sizeof(*thdr));
  44. else
  45. uhdr = skb_put(skb, sizeof(*uhdr));
  46. eth_zero_addr(ehdr->h_dest);
  47. if (attr->src)
  48. ether_addr_copy(ehdr->h_source, attr->src);
  49. if (attr->dst)
  50. ether_addr_copy(ehdr->h_dest, attr->dst);
  51. ehdr->h_proto = htons(ETH_P_IP);
  52. if (attr->tcp) {
  53. memset(thdr, 0, sizeof(*thdr));
  54. thdr->source = htons(attr->sport);
  55. thdr->dest = htons(attr->dport);
  56. thdr->doff = sizeof(struct tcphdr) / 4;
  57. } else {
  58. uhdr->source = htons(attr->sport);
  59. uhdr->dest = htons(attr->dport);
  60. uhdr->len = htons(sizeof(*shdr) + sizeof(*uhdr) + attr->size);
  61. if (attr->max_size)
  62. uhdr->len = htons(attr->max_size -
  63. (sizeof(*ihdr) + sizeof(*ehdr)));
  64. uhdr->check = 0;
  65. }
  66. ihdr->ihl = 5;
  67. ihdr->ttl = 32;
  68. ihdr->version = 4;
  69. if (attr->tcp)
  70. ihdr->protocol = IPPROTO_TCP;
  71. else
  72. ihdr->protocol = IPPROTO_UDP;
  73. iplen = sizeof(*ihdr) + sizeof(*shdr) + attr->size;
  74. if (attr->tcp)
  75. iplen += sizeof(*thdr);
  76. else
  77. iplen += sizeof(*uhdr);
  78. if (attr->max_size)
  79. iplen = attr->max_size - sizeof(*ehdr);
  80. ihdr->tot_len = htons(iplen);
  81. ihdr->frag_off = 0;
  82. ihdr->saddr = htonl(attr->ip_src);
  83. ihdr->daddr = htonl(attr->ip_dst);
  84. ihdr->tos = 0;
  85. ihdr->id = 0;
  86. ip_send_check(ihdr);
  87. shdr = skb_put(skb, sizeof(*shdr));
  88. shdr->version = 0;
  89. shdr->magic = cpu_to_be64(NET_TEST_PKT_MAGIC);
  90. attr->id = id;
  91. shdr->id = id;
  92. if (attr->size) {
  93. void *payload = skb_put(skb, attr->size);
  94. memset(payload, 0, attr->size);
  95. }
  96. if (attr->max_size && attr->max_size > skb->len) {
  97. size_t pad_len = attr->max_size - skb->len;
  98. void *pad = skb_put(skb, pad_len);
  99. memset(pad, 0, pad_len);
  100. }
  101. skb->csum = 0;
  102. skb->ip_summed = CHECKSUM_PARTIAL;
  103. if (attr->tcp) {
  104. int l4len = skb->len - skb_transport_offset(skb);
  105. thdr->check = ~tcp_v4_check(l4len, ihdr->saddr, ihdr->daddr, 0);
  106. skb->csum_start = skb_transport_header(skb) - skb->head;
  107. skb->csum_offset = offsetof(struct tcphdr, check);
  108. if (attr->bad_csum) {
  109. /* Force mangled checksum */
  110. if (skb_checksum_help(skb)) {
  111. kfree_skb(skb);
  112. return NULL;
  113. }
  114. if (thdr->check != CSUM_MANGLED_0)
  115. thdr->check = CSUM_MANGLED_0;
  116. else
  117. thdr->check = csum16_sub(thdr->check,
  118. cpu_to_be16(1));
  119. }
  120. } else {
  121. udp4_hwcsum(skb, ihdr->saddr, ihdr->daddr);
  122. }
  123. skb->protocol = htons(ETH_P_IP);
  124. skb->pkt_type = PACKET_HOST;
  125. skb->dev = ndev;
  126. return skb;
  127. }
  128. EXPORT_SYMBOL_GPL(net_test_get_skb);
  129. static int net_test_loopback_validate(struct sk_buff *skb,
  130. struct net_device *ndev,
  131. struct packet_type *pt,
  132. struct net_device *orig_ndev)
  133. {
  134. struct net_test_priv *tpriv = pt->af_packet_priv;
  135. const unsigned char *src = tpriv->packet->src;
  136. const unsigned char *dst = tpriv->packet->dst;
  137. struct netsfhdr *shdr;
  138. struct ethhdr *ehdr;
  139. struct udphdr *uhdr;
  140. struct tcphdr *thdr;
  141. struct iphdr *ihdr;
  142. skb = skb_unshare(skb, GFP_ATOMIC);
  143. if (!skb)
  144. goto out;
  145. if (skb_linearize(skb))
  146. goto out;
  147. if (skb_headlen(skb) < (NET_TEST_PKT_SIZE - ETH_HLEN))
  148. goto out;
  149. ehdr = (struct ethhdr *)skb_mac_header(skb);
  150. if (dst) {
  151. if (!ether_addr_equal_unaligned(ehdr->h_dest, dst))
  152. goto out;
  153. }
  154. if (src) {
  155. if (!ether_addr_equal_unaligned(ehdr->h_source, src))
  156. goto out;
  157. }
  158. ihdr = ip_hdr(skb);
  159. if (tpriv->double_vlan)
  160. ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
  161. if (tpriv->packet->tcp) {
  162. if (ihdr->protocol != IPPROTO_TCP)
  163. goto out;
  164. thdr = (struct tcphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
  165. if (thdr->dest != htons(tpriv->packet->dport))
  166. goto out;
  167. shdr = (struct netsfhdr *)((u8 *)thdr + sizeof(*thdr));
  168. } else {
  169. if (ihdr->protocol != IPPROTO_UDP)
  170. goto out;
  171. uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
  172. if (uhdr->dest != htons(tpriv->packet->dport))
  173. goto out;
  174. shdr = (struct netsfhdr *)((u8 *)uhdr + sizeof(*uhdr));
  175. }
  176. if (shdr->magic != cpu_to_be64(NET_TEST_PKT_MAGIC))
  177. goto out;
  178. if (tpriv->packet->id != shdr->id)
  179. goto out;
  180. if (tpriv->packet->bad_csum && skb->ip_summed == CHECKSUM_UNNECESSARY)
  181. tpriv->ok = -EIO;
  182. else
  183. tpriv->ok = true;
  184. complete(&tpriv->comp);
  185. out:
  186. kfree_skb(skb);
  187. return 0;
  188. }
  189. static int __net_test_loopback(struct net_device *ndev,
  190. struct net_packet_attrs *attr)
  191. {
  192. struct net_test_priv *tpriv;
  193. struct sk_buff *skb = NULL;
  194. int ret = 0;
  195. tpriv = kzalloc_obj(*tpriv);
  196. if (!tpriv)
  197. return -ENOMEM;
  198. tpriv->ok = false;
  199. init_completion(&tpriv->comp);
  200. tpriv->pt.type = htons(ETH_P_IP);
  201. tpriv->pt.func = net_test_loopback_validate;
  202. tpriv->pt.dev = ndev;
  203. tpriv->pt.af_packet_priv = tpriv;
  204. tpriv->packet = attr;
  205. dev_add_pack(&tpriv->pt);
  206. skb = net_test_get_skb(ndev, net_test_next_id, attr);
  207. if (!skb) {
  208. ret = -ENOMEM;
  209. goto cleanup;
  210. }
  211. net_test_next_id++;
  212. ret = dev_direct_xmit(skb, attr->queue_mapping);
  213. if (ret < 0) {
  214. goto cleanup;
  215. } else if (ret > 0) {
  216. ret = -ENETUNREACH;
  217. goto cleanup;
  218. }
  219. if (!attr->timeout)
  220. attr->timeout = NET_LB_TIMEOUT;
  221. wait_for_completion_timeout(&tpriv->comp, attr->timeout);
  222. if (tpriv->ok < 0)
  223. ret = tpriv->ok;
  224. else if (!tpriv->ok)
  225. ret = -ETIMEDOUT;
  226. else
  227. ret = 0;
  228. cleanup:
  229. dev_remove_pack(&tpriv->pt);
  230. kfree(tpriv);
  231. return ret;
  232. }
  233. static int net_test_netif_carrier(struct net_device *ndev)
  234. {
  235. return netif_carrier_ok(ndev) ? 0 : -ENOLINK;
  236. }
  237. static int net_test_phy_phydev(struct net_device *ndev)
  238. {
  239. return ndev->phydev ? 0 : -EOPNOTSUPP;
  240. }
  241. static int net_test_phy_loopback_enable(struct net_device *ndev)
  242. {
  243. if (!ndev->phydev)
  244. return -EOPNOTSUPP;
  245. return phy_loopback(ndev->phydev, true, 0);
  246. }
  247. static int net_test_phy_loopback_disable(struct net_device *ndev)
  248. {
  249. if (!ndev->phydev)
  250. return -EOPNOTSUPP;
  251. return phy_loopback(ndev->phydev, false, 0);
  252. }
  253. static int net_test_phy_loopback_udp(struct net_device *ndev)
  254. {
  255. struct net_packet_attrs attr = { };
  256. attr.dst = ndev->dev_addr;
  257. return __net_test_loopback(ndev, &attr);
  258. }
  259. static int net_test_phy_loopback_udp_mtu(struct net_device *ndev)
  260. {
  261. struct net_packet_attrs attr = { };
  262. attr.dst = ndev->dev_addr;
  263. attr.max_size = ndev->mtu;
  264. return __net_test_loopback(ndev, &attr);
  265. }
  266. static int net_test_phy_loopback_tcp(struct net_device *ndev)
  267. {
  268. struct net_packet_attrs attr = { };
  269. attr.dst = ndev->dev_addr;
  270. attr.tcp = true;
  271. return __net_test_loopback(ndev, &attr);
  272. }
  273. /**
  274. * net_test_phy_loopback_tcp_bad_csum - PHY loopback test with a deliberately
  275. * corrupted TCP checksum
  276. * @ndev: the network device to test
  277. *
  278. * Builds the same minimal Ethernet/IPv4/TCP frame as
  279. * net_test_phy_loopback_tcp(), then flips the least-significant bit of the TCP
  280. * checksum so the resulting value is provably invalid (neither 0 nor 0xFFFF).
  281. * The frame is transmitted through the device’s internal PHY loopback path:
  282. *
  283. * test code -> MAC driver -> MAC HW -> xMII -> PHY ->
  284. * internal PHY loopback -> xMII -> MAC HW -> MAC driver -> test code
  285. *
  286. * Result interpretation
  287. * ---------------------
  288. * 0 The frame is delivered to the stack and the driver reports
  289. * ip_summed as CHECKSUM_NONE or CHECKSUM_COMPLETE - both are
  290. * valid ways to indicate “bad checksum, let the stack verify.”
  291. * -ETIMEDOUT The MAC/PHY silently dropped the frame; hardware checksum
  292. * verification filtered it out before the driver saw it.
  293. * -EIO The driver returned the frame with ip_summed ==
  294. * CHECKSUM_UNNECESSARY, falsely claiming a valid checksum and
  295. * indicating a serious RX-path defect.
  296. *
  297. * Return: 0 on success or a negative error code on failure.
  298. */
  299. static int net_test_phy_loopback_tcp_bad_csum(struct net_device *ndev)
  300. {
  301. struct net_packet_attrs attr = { };
  302. attr.dst = ndev->dev_addr;
  303. attr.tcp = true;
  304. attr.bad_csum = true;
  305. return __net_test_loopback(ndev, &attr);
  306. }
  307. static const struct net_test {
  308. char name[ETH_GSTRING_LEN];
  309. int (*fn)(struct net_device *ndev);
  310. } net_selftests[] = {
  311. {
  312. .name = "Carrier ",
  313. .fn = net_test_netif_carrier,
  314. }, {
  315. .name = "PHY dev is present ",
  316. .fn = net_test_phy_phydev,
  317. }, {
  318. /* This test should be done before all PHY loopback test */
  319. .name = "PHY internal loopback, enable ",
  320. .fn = net_test_phy_loopback_enable,
  321. }, {
  322. .name = "PHY internal loopback, UDP ",
  323. .fn = net_test_phy_loopback_udp,
  324. }, {
  325. .name = "PHY internal loopback, MTU ",
  326. .fn = net_test_phy_loopback_udp_mtu,
  327. }, {
  328. .name = "PHY internal loopback, TCP ",
  329. .fn = net_test_phy_loopback_tcp,
  330. }, {
  331. .name = "PHY loopback, bad TCP csum ",
  332. .fn = net_test_phy_loopback_tcp_bad_csum,
  333. }, {
  334. /* This test should be done after all PHY loopback test */
  335. .name = "PHY internal loopback, disable",
  336. .fn = net_test_phy_loopback_disable,
  337. },
  338. };
  339. void net_selftest(struct net_device *ndev, struct ethtool_test *etest, u64 *buf)
  340. {
  341. int count = net_selftest_get_count();
  342. int i;
  343. memset(buf, 0, sizeof(*buf) * count);
  344. net_test_next_id = 0;
  345. if (etest->flags != ETH_TEST_FL_OFFLINE) {
  346. netdev_err(ndev, "Only offline tests are supported\n");
  347. etest->flags |= ETH_TEST_FL_FAILED;
  348. return;
  349. }
  350. for (i = 0; i < count; i++) {
  351. buf[i] = net_selftests[i].fn(ndev);
  352. if (buf[i] && (buf[i] != -EOPNOTSUPP))
  353. etest->flags |= ETH_TEST_FL_FAILED;
  354. }
  355. }
  356. EXPORT_SYMBOL_GPL(net_selftest);
  357. int net_selftest_get_count(void)
  358. {
  359. return ARRAY_SIZE(net_selftests);
  360. }
  361. EXPORT_SYMBOL_GPL(net_selftest_get_count);
  362. void net_selftest_get_strings(u8 *data)
  363. {
  364. int i;
  365. for (i = 0; i < net_selftest_get_count(); i++)
  366. ethtool_sprintf(&data, "%2d. %s", i + 1,
  367. net_selftests[i].name);
  368. }
  369. EXPORT_SYMBOL_GPL(net_selftest_get_strings);
  370. MODULE_DESCRIPTION("Common library for generic PHY ethtool selftests");
  371. MODULE_LICENSE("GPL v2");
  372. MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");