eth.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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);