eth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 National Instruments
  4. *
  5. * (C) Copyright 2015
  6. * Joe Hershberger <joe.hershberger@ni.com>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <fdtdec.h>
  11. #include <malloc.h>
  12. #include <net.h>
  13. #include <dm/test.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/uclass-internal.h>
  16. #include <asm/eth.h>
  17. #include <test/ut.h>
  18. #define DM_TEST_ETH_NUM 4
  19. static int dm_test_eth(struct unit_test_state *uts)
  20. {
  21. net_ping_ip = string_to_ip("1.1.2.2");
  22. env_set("ethact", "eth@10002000");
  23. ut_assertok(net_loop(PING));
  24. ut_asserteq_str("eth@10002000", env_get("ethact"));
  25. env_set("ethact", "eth@10003000");
  26. ut_assertok(net_loop(PING));
  27. ut_asserteq_str("eth@10003000", env_get("ethact"));
  28. env_set("ethact", "eth@10004000");
  29. ut_assertok(net_loop(PING));
  30. ut_asserteq_str("eth@10004000", env_get("ethact"));
  31. return 0;
  32. }
  33. DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
  34. static int dm_test_eth_alias(struct unit_test_state *uts)
  35. {
  36. net_ping_ip = string_to_ip("1.1.2.2");
  37. env_set("ethact", "eth0");
  38. ut_assertok(net_loop(PING));
  39. ut_asserteq_str("eth@10002000", env_get("ethact"));
  40. env_set("ethact", "eth1");
  41. ut_assertok(net_loop(PING));
  42. ut_asserteq_str("eth@10004000", env_get("ethact"));
  43. /* Expected to fail since eth2 is not defined in the device tree */
  44. env_set("ethact", "eth2");
  45. ut_assertok(net_loop(PING));
  46. ut_asserteq_str("eth@10002000", env_get("ethact"));
  47. env_set("ethact", "eth5");
  48. ut_assertok(net_loop(PING));
  49. ut_asserteq_str("eth@10003000", env_get("ethact"));
  50. return 0;
  51. }
  52. DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
  53. static int dm_test_eth_prime(struct unit_test_state *uts)
  54. {
  55. net_ping_ip = string_to_ip("1.1.2.2");
  56. /* Expected to be "eth@10003000" because of ethprime variable */
  57. env_set("ethact", NULL);
  58. env_set("ethprime", "eth5");
  59. ut_assertok(net_loop(PING));
  60. ut_asserteq_str("eth@10003000", env_get("ethact"));
  61. /* Expected to be "eth@10002000" because it is first */
  62. env_set("ethact", NULL);
  63. env_set("ethprime", NULL);
  64. ut_assertok(net_loop(PING));
  65. ut_asserteq_str("eth@10002000", env_get("ethact"));
  66. return 0;
  67. }
  68. DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
  69. /**
  70. * This test case is trying to test the following scenario:
  71. * - All ethernet devices are not probed
  72. * - "ethaddr" for all ethernet devices are not set
  73. * - "ethact" is set to a valid ethernet device name
  74. *
  75. * With Sandbox default test configuration, all ethernet devices are
  76. * probed after power-up, so we have to manually create such scenario:
  77. * - Remove all ethernet devices
  78. * - Remove all "ethaddr" environment variables
  79. * - Set "ethact" to the first ethernet device
  80. *
  81. * Do a ping test to see if anything goes wrong.
  82. */
  83. static int dm_test_eth_act(struct unit_test_state *uts)
  84. {
  85. struct udevice *dev[DM_TEST_ETH_NUM];
  86. const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000",
  87. "sbe5", "eth@10004000"};
  88. const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr",
  89. "eth3addr", "eth1addr"};
  90. char ethaddr[DM_TEST_ETH_NUM][18];
  91. int i;
  92. memset(ethaddr, '\0', sizeof(ethaddr));
  93. net_ping_ip = string_to_ip("1.1.2.2");
  94. /* Prepare the test scenario */
  95. for (i = 0; i < DM_TEST_ETH_NUM; i++) {
  96. ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
  97. ethname[i], &dev[i]));
  98. ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL));
  99. /* Invalidate MAC address */
  100. strncpy(ethaddr[i], env_get(addrname[i]), 17);
  101. /* Must disable access protection for ethaddr before clearing */
  102. env_set(".flags", addrname[i]);
  103. env_set(addrname[i], NULL);
  104. }
  105. /* Set ethact to "eth@10002000" */
  106. env_set("ethact", ethname[0]);
  107. /* Segment fault might happen if something is wrong */
  108. ut_asserteq(-ENODEV, net_loop(PING));
  109. for (i = 0; i < DM_TEST_ETH_NUM; i++) {
  110. /* Restore the env */
  111. env_set(".flags", addrname[i]);
  112. env_set(addrname[i], ethaddr[i]);
  113. /* Probe the device again */
  114. ut_assertok(device_probe(dev[i]));
  115. }
  116. env_set(".flags", NULL);
  117. env_set("ethact", NULL);
  118. return 0;
  119. }
  120. DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT);
  121. /* The asserts include a return on fail; cleanup in the caller */
  122. static int _dm_test_eth_rotate1(struct unit_test_state *uts)
  123. {
  124. /* Make sure that the default is to rotate to the next interface */
  125. env_set("ethact", "eth@10004000");
  126. ut_assertok(net_loop(PING));
  127. ut_asserteq_str("eth@10002000", env_get("ethact"));
  128. /* If ethrotate is no, then we should fail on a bad MAC */
  129. env_set("ethact", "eth@10004000");
  130. env_set("ethrotate", "no");
  131. ut_asserteq(-EINVAL, net_loop(PING));
  132. ut_asserteq_str("eth@10004000", env_get("ethact"));
  133. return 0;
  134. }
  135. static int _dm_test_eth_rotate2(struct unit_test_state *uts)
  136. {
  137. /* Make sure we can skip invalid devices */
  138. env_set("ethact", "eth@10004000");
  139. ut_assertok(net_loop(PING));
  140. ut_asserteq_str("eth@10004000", env_get("ethact"));
  141. /* Make sure we can handle device name which is not eth# */
  142. env_set("ethact", "sbe5");
  143. ut_assertok(net_loop(PING));
  144. ut_asserteq_str("sbe5", env_get("ethact"));
  145. return 0;
  146. }
  147. static int dm_test_eth_rotate(struct unit_test_state *uts)
  148. {
  149. char ethaddr[18];
  150. int retval;
  151. /* Set target IP to mock ping */
  152. net_ping_ip = string_to_ip("1.1.2.2");
  153. /* Invalidate eth1's MAC address */
  154. memset(ethaddr, '\0', sizeof(ethaddr));
  155. strncpy(ethaddr, env_get("eth1addr"), 17);
  156. /* Must disable access protection for eth1addr before clearing */
  157. env_set(".flags", "eth1addr");
  158. env_set("eth1addr", NULL);
  159. retval = _dm_test_eth_rotate1(uts);
  160. /* Restore the env */
  161. env_set("eth1addr", ethaddr);
  162. env_set("ethrotate", NULL);
  163. if (!retval) {
  164. /* Invalidate eth0's MAC address */
  165. strncpy(ethaddr, env_get("ethaddr"), 17);
  166. /* Must disable access protection for ethaddr before clearing */
  167. env_set(".flags", "ethaddr");
  168. env_set("ethaddr", NULL);
  169. retval = _dm_test_eth_rotate2(uts);
  170. /* Restore the env */
  171. env_set("ethaddr", ethaddr);
  172. }
  173. /* Restore the env */
  174. env_set(".flags", NULL);
  175. return retval;
  176. }
  177. DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
  178. /* The asserts include a return on fail; cleanup in the caller */
  179. static int _dm_test_net_retry(struct unit_test_state *uts)
  180. {
  181. /*
  182. * eth1 is disabled and netretry is yes, so the ping should succeed and
  183. * the active device should be eth0
  184. */
  185. sandbox_eth_disable_response(1, true);
  186. env_set("ethact", "eth@10004000");
  187. env_set("netretry", "yes");
  188. sandbox_eth_skip_timeout();
  189. ut_assertok(net_loop(PING));
  190. ut_asserteq_str("eth@10002000", env_get("ethact"));
  191. /*
  192. * eth1 is disabled and netretry is no, so the ping should fail and the
  193. * active device should be eth1
  194. */
  195. env_set("ethact", "eth@10004000");
  196. env_set("netretry", "no");
  197. sandbox_eth_skip_timeout();
  198. ut_asserteq(-ETIMEDOUT, net_loop(PING));
  199. ut_asserteq_str("eth@10004000", env_get("ethact"));
  200. return 0;
  201. }
  202. static int dm_test_net_retry(struct unit_test_state *uts)
  203. {
  204. int retval;
  205. net_ping_ip = string_to_ip("1.1.2.2");
  206. retval = _dm_test_net_retry(uts);
  207. /* Restore the env */
  208. env_set("netretry", NULL);
  209. sandbox_eth_disable_response(1, false);
  210. return retval;
  211. }
  212. DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);
  213. static int sb_check_arp_reply(struct udevice *dev, void *packet,
  214. unsigned int len)
  215. {
  216. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  217. struct ethernet_hdr *eth = packet;
  218. struct arp_hdr *arp;
  219. /* Used by all of the ut_assert macros */
  220. struct unit_test_state *uts = priv->priv;
  221. if (ntohs(eth->et_protlen) != PROT_ARP)
  222. return 0;
  223. arp = packet + ETHER_HDR_SIZE;
  224. if (ntohs(arp->ar_op) != ARPOP_REPLY)
  225. return 0;
  226. /* This test would be worthless if we are not waiting */
  227. ut_assert(arp_is_waiting());
  228. /* Validate response */
  229. ut_assert(memcmp(eth->et_src, net_ethaddr, ARP_HLEN) == 0);
  230. ut_assert(memcmp(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN) == 0);
  231. ut_assert(eth->et_protlen == htons(PROT_ARP));
  232. ut_assert(arp->ar_hrd == htons(ARP_ETHER));
  233. ut_assert(arp->ar_pro == htons(PROT_IP));
  234. ut_assert(arp->ar_hln == ARP_HLEN);
  235. ut_assert(arp->ar_pln == ARP_PLEN);
  236. ut_assert(memcmp(&arp->ar_sha, net_ethaddr, ARP_HLEN) == 0);
  237. ut_assert(net_read_ip(&arp->ar_spa).s_addr == net_ip.s_addr);
  238. ut_assert(memcmp(&arp->ar_tha, priv->fake_host_hwaddr, ARP_HLEN) == 0);
  239. ut_assert(net_read_ip(&arp->ar_tpa).s_addr ==
  240. string_to_ip("1.1.2.4").s_addr);
  241. return 0;
  242. }
  243. static int sb_with_async_arp_handler(struct udevice *dev, void *packet,
  244. unsigned int len)
  245. {
  246. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  247. struct ethernet_hdr *eth = packet;
  248. struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
  249. int ret;
  250. /*
  251. * If we are about to generate a reply to ARP, first inject a request
  252. * from another host
  253. */
  254. if (ntohs(eth->et_protlen) == PROT_ARP &&
  255. ntohs(arp->ar_op) == ARPOP_REQUEST) {
  256. /* Make sure sandbox_eth_recv_arp_req() knows who is asking */
  257. priv->fake_host_ipaddr = string_to_ip("1.1.2.4");
  258. ret = sandbox_eth_recv_arp_req(dev);
  259. if (ret)
  260. return ret;
  261. }
  262. sandbox_eth_arp_req_to_reply(dev, packet, len);
  263. sandbox_eth_ping_req_to_reply(dev, packet, len);
  264. return sb_check_arp_reply(dev, packet, len);
  265. }
  266. static int dm_test_eth_async_arp_reply(struct unit_test_state *uts)
  267. {
  268. net_ping_ip = string_to_ip("1.1.2.2");
  269. sandbox_eth_set_tx_handler(0, sb_with_async_arp_handler);
  270. /* Used by all of the ut_assert macros in the tx_handler */
  271. sandbox_eth_set_priv(0, uts);
  272. env_set("ethact", "eth@10002000");
  273. ut_assertok(net_loop(PING));
  274. ut_asserteq_str("eth@10002000", env_get("ethact"));
  275. sandbox_eth_set_tx_handler(0, NULL);
  276. return 0;
  277. }
  278. DM_TEST(dm_test_eth_async_arp_reply, DM_TESTF_SCAN_FDT);
  279. static int sb_check_ping_reply(struct udevice *dev, void *packet,
  280. unsigned int len)
  281. {
  282. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  283. struct ethernet_hdr *eth = packet;
  284. struct ip_udp_hdr *ip;
  285. struct icmp_hdr *icmp;
  286. /* Used by all of the ut_assert macros */
  287. struct unit_test_state *uts = priv->priv;
  288. if (ntohs(eth->et_protlen) != PROT_IP)
  289. return 0;
  290. ip = packet + ETHER_HDR_SIZE;
  291. if (ip->ip_p != IPPROTO_ICMP)
  292. return 0;
  293. icmp = (struct icmp_hdr *)&ip->udp_src;
  294. if (icmp->type != ICMP_ECHO_REPLY)
  295. return 0;
  296. /* This test would be worthless if we are not waiting */
  297. ut_assert(arp_is_waiting());
  298. /* Validate response */
  299. ut_assert(memcmp(eth->et_src, net_ethaddr, ARP_HLEN) == 0);
  300. ut_assert(memcmp(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN) == 0);
  301. ut_assert(eth->et_protlen == htons(PROT_IP));
  302. ut_assert(net_read_ip(&ip->ip_src).s_addr == net_ip.s_addr);
  303. ut_assert(net_read_ip(&ip->ip_dst).s_addr ==
  304. string_to_ip("1.1.2.4").s_addr);
  305. return 0;
  306. }
  307. static int sb_with_async_ping_handler(struct udevice *dev, void *packet,
  308. unsigned int len)
  309. {
  310. struct eth_sandbox_priv *priv = dev_get_priv(dev);
  311. struct ethernet_hdr *eth = packet;
  312. struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
  313. int ret;
  314. /*
  315. * If we are about to generate a reply to ARP, first inject a request
  316. * from another host
  317. */
  318. if (ntohs(eth->et_protlen) == PROT_ARP &&
  319. ntohs(arp->ar_op) == ARPOP_REQUEST) {
  320. /* Make sure sandbox_eth_recv_arp_req() knows who is asking */
  321. priv->fake_host_ipaddr = string_to_ip("1.1.2.4");
  322. ret = sandbox_eth_recv_ping_req(dev);
  323. if (ret)
  324. return ret;
  325. }
  326. sandbox_eth_arp_req_to_reply(dev, packet, len);
  327. sandbox_eth_ping_req_to_reply(dev, packet, len);
  328. return sb_check_ping_reply(dev, packet, len);
  329. }
  330. static int dm_test_eth_async_ping_reply(struct unit_test_state *uts)
  331. {
  332. net_ping_ip = string_to_ip("1.1.2.2");
  333. sandbox_eth_set_tx_handler(0, sb_with_async_ping_handler);
  334. /* Used by all of the ut_assert macros in the tx_handler */
  335. sandbox_eth_set_priv(0, uts);
  336. env_set("ethact", "eth@10002000");
  337. ut_assertok(net_loop(PING));
  338. ut_asserteq_str("eth@10002000", env_get("ethact"));
  339. sandbox_eth_set_tx_handler(0, NULL);
  340. return 0;
  341. }
  342. DM_TEST(dm_test_eth_async_ping_reply, DM_TESTF_SCAN_FDT);