selftests.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. struct net_packet_attrs {
  16. const unsigned char *src;
  17. const unsigned char *dst;
  18. u32 ip_src;
  19. u32 ip_dst;
  20. bool tcp;
  21. u16 sport;
  22. u16 dport;
  23. int timeout;
  24. int size;
  25. int max_size;
  26. u8 id;
  27. u16 queue_mapping;
  28. };
  29. struct net_test_priv {
  30. struct net_packet_attrs *packet;
  31. struct packet_type pt;
  32. struct completion comp;
  33. int double_vlan;
  34. int vlan_id;
  35. int ok;
  36. };
  37. struct netsfhdr {
  38. __be32 version;
  39. __be64 magic;
  40. u8 id;
  41. } __packed;
  42. static u8 net_test_next_id;
  43. #define NET_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
  44. sizeof(struct netsfhdr))
  45. #define NET_TEST_PKT_MAGIC 0xdeadcafecafedeadULL
  46. #define NET_LB_TIMEOUT msecs_to_jiffies(200)
  47. static struct sk_buff *net_test_get_skb(struct net_device *ndev,
  48. struct net_packet_attrs *attr)
  49. {
  50. struct sk_buff *skb = NULL;
  51. struct udphdr *uhdr = NULL;
  52. struct tcphdr *thdr = NULL;
  53. struct netsfhdr *shdr;
  54. struct ethhdr *ehdr;
  55. struct iphdr *ihdr;
  56. int iplen, size;
  57. size = attr->size + NET_TEST_PKT_SIZE;
  58. if (attr->tcp)
  59. size += sizeof(struct tcphdr);
  60. else
  61. size += sizeof(struct udphdr);
  62. if (attr->max_size && attr->max_size > size)
  63. size = attr->max_size;
  64. skb = netdev_alloc_skb(ndev, size);
  65. if (!skb)
  66. return NULL;
  67. prefetchw(skb->data);
  68. ehdr = skb_push(skb, ETH_HLEN);
  69. skb_reset_mac_header(skb);
  70. skb_set_network_header(skb, skb->len);
  71. ihdr = skb_put(skb, sizeof(*ihdr));
  72. skb_set_transport_header(skb, skb->len);
  73. if (attr->tcp)
  74. thdr = skb_put(skb, sizeof(*thdr));
  75. else
  76. uhdr = skb_put(skb, sizeof(*uhdr));
  77. eth_zero_addr(ehdr->h_dest);
  78. if (attr->src)
  79. ether_addr_copy(ehdr->h_source, attr->src);
  80. if (attr->dst)
  81. ether_addr_copy(ehdr->h_dest, attr->dst);
  82. ehdr->h_proto = htons(ETH_P_IP);
  83. if (attr->tcp) {
  84. memset(thdr, 0, sizeof(*thdr));
  85. thdr->source = htons(attr->sport);
  86. thdr->dest = htons(attr->dport);
  87. thdr->doff = sizeof(struct tcphdr) / 4;
  88. } else {
  89. uhdr->source = htons(attr->sport);
  90. uhdr->dest = htons(attr->dport);
  91. uhdr->len = htons(sizeof(*shdr) + sizeof(*uhdr) + attr->size);
  92. if (attr->max_size)
  93. uhdr->len = htons(attr->max_size -
  94. (sizeof(*ihdr) + sizeof(*ehdr)));
  95. uhdr->check = 0;
  96. }
  97. ihdr->ihl = 5;
  98. ihdr->ttl = 32;
  99. ihdr->version = 4;
  100. if (attr->tcp)
  101. ihdr->protocol = IPPROTO_TCP;
  102. else
  103. ihdr->protocol = IPPROTO_UDP;
  104. iplen = sizeof(*ihdr) + sizeof(*shdr) + attr->size;
  105. if (attr->tcp)
  106. iplen += sizeof(*thdr);
  107. else
  108. iplen += sizeof(*uhdr);
  109. if (attr->max_size)
  110. iplen = attr->max_size - sizeof(*ehdr);
  111. ihdr->tot_len = htons(iplen);
  112. ihdr->frag_off = 0;
  113. ihdr->saddr = htonl(attr->ip_src);
  114. ihdr->daddr = htonl(attr->ip_dst);
  115. ihdr->tos = 0;
  116. ihdr->id = 0;
  117. ip_send_check(ihdr);
  118. shdr = skb_put(skb, sizeof(*shdr));
  119. shdr->version = 0;
  120. shdr->magic = cpu_to_be64(NET_TEST_PKT_MAGIC);
  121. attr->id = net_test_next_id;
  122. shdr->id = net_test_next_id++;
  123. if (attr->size) {
  124. void *payload = skb_put(skb, attr->size);
  125. memset(payload, 0, attr->size);
  126. }
  127. if (attr->max_size && attr->max_size > skb->len) {
  128. size_t pad_len = attr->max_size - skb->len;
  129. void *pad = skb_put(skb, pad_len);
  130. memset(pad, 0, pad_len);
  131. }
  132. skb->csum = 0;
  133. skb->ip_summed = CHECKSUM_PARTIAL;
  134. if (attr->tcp) {
  135. int l4len = skb->len - skb_transport_offset(skb);
  136. thdr->check = ~tcp_v4_check(l4len, ihdr->saddr, ihdr->daddr, 0);
  137. skb->csum_start = skb_transport_header(skb) - skb->head;
  138. skb->csum_offset = offsetof(struct tcphdr, check);
  139. } else {
  140. udp4_hwcsum(skb, ihdr->saddr, ihdr->daddr);
  141. }
  142. skb->protocol = htons(ETH_P_IP);
  143. skb->pkt_type = PACKET_HOST;
  144. skb->dev = ndev;
  145. return skb;
  146. }
  147. static int net_test_loopback_validate(struct sk_buff *skb,
  148. struct net_device *ndev,
  149. struct packet_type *pt,
  150. struct net_device *orig_ndev)
  151. {
  152. struct net_test_priv *tpriv = pt->af_packet_priv;
  153. const unsigned char *src = tpriv->packet->src;
  154. const unsigned char *dst = tpriv->packet->dst;
  155. struct netsfhdr *shdr;
  156. struct ethhdr *ehdr;
  157. struct udphdr *uhdr;
  158. struct tcphdr *thdr;
  159. struct iphdr *ihdr;
  160. skb = skb_unshare(skb, GFP_ATOMIC);
  161. if (!skb)
  162. goto out;
  163. if (skb_linearize(skb))
  164. goto out;
  165. if (skb_headlen(skb) < (NET_TEST_PKT_SIZE - ETH_HLEN))
  166. goto out;
  167. ehdr = (struct ethhdr *)skb_mac_header(skb);
  168. if (dst) {
  169. if (!ether_addr_equal_unaligned(ehdr->h_dest, dst))
  170. goto out;
  171. }
  172. if (src) {
  173. if (!ether_addr_equal_unaligned(ehdr->h_source, src))
  174. goto out;
  175. }
  176. ihdr = ip_hdr(skb);
  177. if (tpriv->double_vlan)
  178. ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
  179. if (tpriv->packet->tcp) {
  180. if (ihdr->protocol != IPPROTO_TCP)
  181. goto out;
  182. thdr = (struct tcphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
  183. if (thdr->dest != htons(tpriv->packet->dport))
  184. goto out;
  185. shdr = (struct netsfhdr *)((u8 *)thdr + sizeof(*thdr));
  186. } else {
  187. if (ihdr->protocol != IPPROTO_UDP)
  188. goto out;
  189. uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
  190. if (uhdr->dest != htons(tpriv->packet->dport))
  191. goto out;
  192. shdr = (struct netsfhdr *)((u8 *)uhdr + sizeof(*uhdr));
  193. }
  194. if (shdr->magic != cpu_to_be64(NET_TEST_PKT_MAGIC))
  195. goto out;
  196. if (tpriv->packet->id != shdr->id)
  197. goto out;
  198. tpriv->ok = true;
  199. complete(&tpriv->comp);
  200. out:
  201. kfree_skb(skb);
  202. return 0;
  203. }
  204. static int __net_test_loopback(struct net_device *ndev,
  205. struct net_packet_attrs *attr)
  206. {
  207. struct net_test_priv *tpriv;
  208. struct sk_buff *skb = NULL;
  209. int ret = 0;
  210. tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
  211. if (!tpriv)
  212. return -ENOMEM;
  213. tpriv->ok = false;
  214. init_completion(&tpriv->comp);
  215. tpriv->pt.type = htons(ETH_P_IP);
  216. tpriv->pt.func = net_test_loopback_validate;
  217. tpriv->pt.dev = ndev;
  218. tpriv->pt.af_packet_priv = tpriv;
  219. tpriv->packet = attr;
  220. dev_add_pack(&tpriv->pt);
  221. skb = net_test_get_skb(ndev, attr);
  222. if (!skb) {
  223. ret = -ENOMEM;
  224. goto cleanup;
  225. }
  226. ret = dev_direct_xmit(skb, attr->queue_mapping);
  227. if (ret < 0) {
  228. goto cleanup;
  229. } else if (ret > 0) {
  230. ret = -ENETUNREACH;
  231. goto cleanup;
  232. }
  233. if (!attr->timeout)
  234. attr->timeout = NET_LB_TIMEOUT;
  235. wait_for_completion_timeout(&tpriv->comp, attr->timeout);
  236. ret = tpriv->ok ? 0 : -ETIMEDOUT;
  237. cleanup:
  238. dev_remove_pack(&tpriv->pt);
  239. kfree(tpriv);
  240. return ret;
  241. }
  242. static int net_test_netif_carrier(struct net_device *ndev)
  243. {
  244. return netif_carrier_ok(ndev) ? 0 : -ENOLINK;
  245. }
  246. static int net_test_phy_phydev(struct net_device *ndev)
  247. {
  248. return ndev->phydev ? 0 : -EOPNOTSUPP;
  249. }
  250. static int net_test_phy_loopback_enable(struct net_device *ndev)
  251. {
  252. if (!ndev->phydev)
  253. return -EOPNOTSUPP;
  254. return phy_loopback(ndev->phydev, true);
  255. }
  256. static int net_test_phy_loopback_disable(struct net_device *ndev)
  257. {
  258. if (!ndev->phydev)
  259. return -EOPNOTSUPP;
  260. return phy_loopback(ndev->phydev, false);
  261. }
  262. static int net_test_phy_loopback_udp(struct net_device *ndev)
  263. {
  264. struct net_packet_attrs attr = { };
  265. attr.dst = ndev->dev_addr;
  266. return __net_test_loopback(ndev, &attr);
  267. }
  268. static int net_test_phy_loopback_udp_mtu(struct net_device *ndev)
  269. {
  270. struct net_packet_attrs attr = { };
  271. attr.dst = ndev->dev_addr;
  272. attr.max_size = ndev->mtu;
  273. return __net_test_loopback(ndev, &attr);
  274. }
  275. static int net_test_phy_loopback_tcp(struct net_device *ndev)
  276. {
  277. struct net_packet_attrs attr = { };
  278. attr.dst = ndev->dev_addr;
  279. attr.tcp = true;
  280. return __net_test_loopback(ndev, &attr);
  281. }
  282. static const struct net_test {
  283. char name[ETH_GSTRING_LEN];
  284. int (*fn)(struct net_device *ndev);
  285. } net_selftests[] = {
  286. {
  287. .name = "Carrier ",
  288. .fn = net_test_netif_carrier,
  289. }, {
  290. .name = "PHY dev is present ",
  291. .fn = net_test_phy_phydev,
  292. }, {
  293. /* This test should be done before all PHY loopback test */
  294. .name = "PHY internal loopback, enable ",
  295. .fn = net_test_phy_loopback_enable,
  296. }, {
  297. .name = "PHY internal loopback, UDP ",
  298. .fn = net_test_phy_loopback_udp,
  299. }, {
  300. .name = "PHY internal loopback, MTU ",
  301. .fn = net_test_phy_loopback_udp_mtu,
  302. }, {
  303. .name = "PHY internal loopback, TCP ",
  304. .fn = net_test_phy_loopback_tcp,
  305. }, {
  306. /* This test should be done after all PHY loopback test */
  307. .name = "PHY internal loopback, disable",
  308. .fn = net_test_phy_loopback_disable,
  309. },
  310. };
  311. void net_selftest(struct net_device *ndev, struct ethtool_test *etest, u64 *buf)
  312. {
  313. int count = net_selftest_get_count();
  314. int i;
  315. memset(buf, 0, sizeof(*buf) * count);
  316. net_test_next_id = 0;
  317. if (etest->flags != ETH_TEST_FL_OFFLINE) {
  318. netdev_err(ndev, "Only offline tests are supported\n");
  319. etest->flags |= ETH_TEST_FL_FAILED;
  320. return;
  321. }
  322. for (i = 0; i < count; i++) {
  323. buf[i] = net_selftests[i].fn(ndev);
  324. if (buf[i] && (buf[i] != -EOPNOTSUPP))
  325. etest->flags |= ETH_TEST_FL_FAILED;
  326. }
  327. }
  328. EXPORT_SYMBOL_GPL(net_selftest);
  329. int net_selftest_get_count(void)
  330. {
  331. return ARRAY_SIZE(net_selftests);
  332. }
  333. EXPORT_SYMBOL_GPL(net_selftest_get_count);
  334. void net_selftest_get_strings(u8 *data)
  335. {
  336. int i;
  337. for (i = 0; i < net_selftest_get_count(); i++)
  338. ethtool_sprintf(&data, "%2d. %s", i + 1,
  339. net_selftests[i].name);
  340. }
  341. EXPORT_SYMBOL_GPL(net_selftest_get_strings);
  342. MODULE_DESCRIPTION("Common library for generic PHY ethtool selftests");
  343. MODULE_LICENSE("GPL v2");
  344. MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");