selftests.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 <[email protected]>
  7. *
  8. * Ported from stmmac by:
  9. * Copyright (C) 2021 Oleksij Rempel <[email protected]>
  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. thdr->source = htons(attr->sport);
  85. thdr->dest = htons(attr->dport);
  86. thdr->doff = sizeof(struct tcphdr) / 4;
  87. thdr->check = 0;
  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. skb_put(skb, attr->size);
  125. if (attr->max_size && attr->max_size > skb->len)
  126. skb_put(skb, attr->max_size - skb->len);
  127. skb->csum = 0;
  128. skb->ip_summed = CHECKSUM_PARTIAL;
  129. if (attr->tcp) {
  130. thdr->check = ~tcp_v4_check(skb->len, ihdr->saddr,
  131. ihdr->daddr, 0);
  132. skb->csum_start = skb_transport_header(skb) - skb->head;
  133. skb->csum_offset = offsetof(struct tcphdr, check);
  134. } else {
  135. udp4_hwcsum(skb, ihdr->saddr, ihdr->daddr);
  136. }
  137. skb->protocol = htons(ETH_P_IP);
  138. skb->pkt_type = PACKET_HOST;
  139. skb->dev = ndev;
  140. return skb;
  141. }
  142. static int net_test_loopback_validate(struct sk_buff *skb,
  143. struct net_device *ndev,
  144. struct packet_type *pt,
  145. struct net_device *orig_ndev)
  146. {
  147. struct net_test_priv *tpriv = pt->af_packet_priv;
  148. const unsigned char *src = tpriv->packet->src;
  149. const unsigned char *dst = tpriv->packet->dst;
  150. struct netsfhdr *shdr;
  151. struct ethhdr *ehdr;
  152. struct udphdr *uhdr;
  153. struct tcphdr *thdr;
  154. struct iphdr *ihdr;
  155. skb = skb_unshare(skb, GFP_ATOMIC);
  156. if (!skb)
  157. goto out;
  158. if (skb_linearize(skb))
  159. goto out;
  160. if (skb_headlen(skb) < (NET_TEST_PKT_SIZE - ETH_HLEN))
  161. goto out;
  162. ehdr = (struct ethhdr *)skb_mac_header(skb);
  163. if (dst) {
  164. if (!ether_addr_equal_unaligned(ehdr->h_dest, dst))
  165. goto out;
  166. }
  167. if (src) {
  168. if (!ether_addr_equal_unaligned(ehdr->h_source, src))
  169. goto out;
  170. }
  171. ihdr = ip_hdr(skb);
  172. if (tpriv->double_vlan)
  173. ihdr = (struct iphdr *)(skb_network_header(skb) + 4);
  174. if (tpriv->packet->tcp) {
  175. if (ihdr->protocol != IPPROTO_TCP)
  176. goto out;
  177. thdr = (struct tcphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
  178. if (thdr->dest != htons(tpriv->packet->dport))
  179. goto out;
  180. shdr = (struct netsfhdr *)((u8 *)thdr + sizeof(*thdr));
  181. } else {
  182. if (ihdr->protocol != IPPROTO_UDP)
  183. goto out;
  184. uhdr = (struct udphdr *)((u8 *)ihdr + 4 * ihdr->ihl);
  185. if (uhdr->dest != htons(tpriv->packet->dport))
  186. goto out;
  187. shdr = (struct netsfhdr *)((u8 *)uhdr + sizeof(*uhdr));
  188. }
  189. if (shdr->magic != cpu_to_be64(NET_TEST_PKT_MAGIC))
  190. goto out;
  191. if (tpriv->packet->id != shdr->id)
  192. goto out;
  193. tpriv->ok = true;
  194. complete(&tpriv->comp);
  195. out:
  196. kfree_skb(skb);
  197. return 0;
  198. }
  199. static int __net_test_loopback(struct net_device *ndev,
  200. struct net_packet_attrs *attr)
  201. {
  202. struct net_test_priv *tpriv;
  203. struct sk_buff *skb = NULL;
  204. int ret = 0;
  205. tpriv = kzalloc(sizeof(*tpriv), GFP_KERNEL);
  206. if (!tpriv)
  207. return -ENOMEM;
  208. tpriv->ok = false;
  209. init_completion(&tpriv->comp);
  210. tpriv->pt.type = htons(ETH_P_IP);
  211. tpriv->pt.func = net_test_loopback_validate;
  212. tpriv->pt.dev = ndev;
  213. tpriv->pt.af_packet_priv = tpriv;
  214. tpriv->packet = attr;
  215. dev_add_pack(&tpriv->pt);
  216. skb = net_test_get_skb(ndev, attr);
  217. if (!skb) {
  218. ret = -ENOMEM;
  219. goto cleanup;
  220. }
  221. ret = dev_direct_xmit(skb, attr->queue_mapping);
  222. if (ret < 0) {
  223. goto cleanup;
  224. } else if (ret > 0) {
  225. ret = -ENETUNREACH;
  226. goto cleanup;
  227. }
  228. if (!attr->timeout)
  229. attr->timeout = NET_LB_TIMEOUT;
  230. wait_for_completion_timeout(&tpriv->comp, attr->timeout);
  231. ret = tpriv->ok ? 0 : -ETIMEDOUT;
  232. cleanup:
  233. dev_remove_pack(&tpriv->pt);
  234. kfree(tpriv);
  235. return ret;
  236. }
  237. static int net_test_netif_carrier(struct net_device *ndev)
  238. {
  239. return netif_carrier_ok(ndev) ? 0 : -ENOLINK;
  240. }
  241. static int net_test_phy_phydev(struct net_device *ndev)
  242. {
  243. return ndev->phydev ? 0 : -EOPNOTSUPP;
  244. }
  245. static int net_test_phy_loopback_enable(struct net_device *ndev)
  246. {
  247. if (!ndev->phydev)
  248. return -EOPNOTSUPP;
  249. return phy_loopback(ndev->phydev, true);
  250. }
  251. static int net_test_phy_loopback_disable(struct net_device *ndev)
  252. {
  253. if (!ndev->phydev)
  254. return -EOPNOTSUPP;
  255. return phy_loopback(ndev->phydev, false);
  256. }
  257. static int net_test_phy_loopback_udp(struct net_device *ndev)
  258. {
  259. struct net_packet_attrs attr = { };
  260. attr.dst = ndev->dev_addr;
  261. return __net_test_loopback(ndev, &attr);
  262. }
  263. static int net_test_phy_loopback_udp_mtu(struct net_device *ndev)
  264. {
  265. struct net_packet_attrs attr = { };
  266. attr.dst = ndev->dev_addr;
  267. attr.max_size = ndev->mtu;
  268. return __net_test_loopback(ndev, &attr);
  269. }
  270. static int net_test_phy_loopback_tcp(struct net_device *ndev)
  271. {
  272. struct net_packet_attrs attr = { };
  273. attr.dst = ndev->dev_addr;
  274. attr.tcp = true;
  275. return __net_test_loopback(ndev, &attr);
  276. }
  277. static const struct net_test {
  278. char name[ETH_GSTRING_LEN];
  279. int (*fn)(struct net_device *ndev);
  280. } net_selftests[] = {
  281. {
  282. .name = "Carrier ",
  283. .fn = net_test_netif_carrier,
  284. }, {
  285. .name = "PHY dev is present ",
  286. .fn = net_test_phy_phydev,
  287. }, {
  288. /* This test should be done before all PHY loopback test */
  289. .name = "PHY internal loopback, enable ",
  290. .fn = net_test_phy_loopback_enable,
  291. }, {
  292. .name = "PHY internal loopback, UDP ",
  293. .fn = net_test_phy_loopback_udp,
  294. }, {
  295. .name = "PHY internal loopback, MTU ",
  296. .fn = net_test_phy_loopback_udp_mtu,
  297. }, {
  298. .name = "PHY internal loopback, TCP ",
  299. .fn = net_test_phy_loopback_tcp,
  300. }, {
  301. /* This test should be done after all PHY loopback test */
  302. .name = "PHY internal loopback, disable",
  303. .fn = net_test_phy_loopback_disable,
  304. },
  305. };
  306. void net_selftest(struct net_device *ndev, struct ethtool_test *etest, u64 *buf)
  307. {
  308. int count = net_selftest_get_count();
  309. int i;
  310. memset(buf, 0, sizeof(*buf) * count);
  311. net_test_next_id = 0;
  312. if (etest->flags != ETH_TEST_FL_OFFLINE) {
  313. netdev_err(ndev, "Only offline tests are supported\n");
  314. etest->flags |= ETH_TEST_FL_FAILED;
  315. return;
  316. }
  317. for (i = 0; i < count; i++) {
  318. buf[i] = net_selftests[i].fn(ndev);
  319. if (buf[i] && (buf[i] != -EOPNOTSUPP))
  320. etest->flags |= ETH_TEST_FL_FAILED;
  321. }
  322. }
  323. EXPORT_SYMBOL_GPL(net_selftest);
  324. int net_selftest_get_count(void)
  325. {
  326. return ARRAY_SIZE(net_selftests);
  327. }
  328. EXPORT_SYMBOL_GPL(net_selftest_get_count);
  329. void net_selftest_get_strings(u8 *data)
  330. {
  331. u8 *p = data;
  332. int i;
  333. for (i = 0; i < net_selftest_get_count(); i++) {
  334. snprintf(p, ETH_GSTRING_LEN, "%2d. %s", i + 1,
  335. net_selftests[i].name);
  336. p += ETH_GSTRING_LEN;
  337. }
  338. }
  339. EXPORT_SYMBOL_GPL(net_selftest_get_strings);
  340. MODULE_LICENSE("GPL v2");
  341. MODULE_AUTHOR("Oleksij Rempel <[email protected]>");