eth.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2015 National Instruments
  3. *
  4. * (C) Copyright 2015
  5. * Joe Hershberger <joe.hershberger@ni.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <fdtdec.h>
  12. #include <malloc.h>
  13. #include <net.h>
  14. #include <dm/test.h>
  15. #include <dm/device-internal.h>
  16. #include <dm/uclass-internal.h>
  17. #include <asm/eth.h>
  18. #include <test/ut.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #define DM_TEST_ETH_NUM 4
  21. static int dm_test_eth(struct unit_test_state *uts)
  22. {
  23. net_ping_ip = string_to_ip("1.1.2.2");
  24. setenv("ethact", "eth@10002000");
  25. ut_assertok(net_loop(PING));
  26. ut_asserteq_str("eth@10002000", getenv("ethact"));
  27. setenv("ethact", "eth@10003000");
  28. ut_assertok(net_loop(PING));
  29. ut_asserteq_str("eth@10003000", getenv("ethact"));
  30. setenv("ethact", "eth@10004000");
  31. ut_assertok(net_loop(PING));
  32. ut_asserteq_str("eth@10004000", getenv("ethact"));
  33. return 0;
  34. }
  35. DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
  36. static int dm_test_eth_alias(struct unit_test_state *uts)
  37. {
  38. net_ping_ip = string_to_ip("1.1.2.2");
  39. setenv("ethact", "eth0");
  40. ut_assertok(net_loop(PING));
  41. ut_asserteq_str("eth@10002000", getenv("ethact"));
  42. setenv("ethact", "eth1");
  43. ut_assertok(net_loop(PING));
  44. ut_asserteq_str("eth@10004000", getenv("ethact"));
  45. /* Expected to fail since eth2 is not defined in the device tree */
  46. setenv("ethact", "eth2");
  47. ut_assertok(net_loop(PING));
  48. ut_asserteq_str("eth@10002000", getenv("ethact"));
  49. setenv("ethact", "eth5");
  50. ut_assertok(net_loop(PING));
  51. ut_asserteq_str("eth@10003000", getenv("ethact"));
  52. return 0;
  53. }
  54. DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
  55. static int dm_test_eth_prime(struct unit_test_state *uts)
  56. {
  57. net_ping_ip = string_to_ip("1.1.2.2");
  58. /* Expected to be "eth@10003000" because of ethprime variable */
  59. setenv("ethact", NULL);
  60. setenv("ethprime", "eth5");
  61. ut_assertok(net_loop(PING));
  62. ut_asserteq_str("eth@10003000", getenv("ethact"));
  63. /* Expected to be "eth@10002000" because it is first */
  64. setenv("ethact", NULL);
  65. setenv("ethprime", NULL);
  66. ut_assertok(net_loop(PING));
  67. ut_asserteq_str("eth@10002000", getenv("ethact"));
  68. return 0;
  69. }
  70. DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
  71. /**
  72. * This test case is trying to test the following scenario:
  73. * - All ethernet devices are not probed
  74. * - "ethaddr" for all ethernet devices are not set
  75. * - "ethact" is set to a valid ethernet device name
  76. *
  77. * With Sandbox default test configuration, all ethernet devices are
  78. * probed after power-up, so we have to manually create such scenario:
  79. * - Remove all ethernet devices
  80. * - Remove all "ethaddr" environment variables
  81. * - Set "ethact" to the first ethernet device
  82. *
  83. * Do a ping test to see if anything goes wrong.
  84. */
  85. static int dm_test_eth_act(struct unit_test_state *uts)
  86. {
  87. struct udevice *dev[DM_TEST_ETH_NUM];
  88. const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000",
  89. "sbe5", "eth@10004000"};
  90. const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr",
  91. "eth3addr", "eth1addr"};
  92. char ethaddr[DM_TEST_ETH_NUM][18];
  93. int i;
  94. net_ping_ip = string_to_ip("1.1.2.2");
  95. /* Prepare the test scenario */
  96. for (i = 0; i < DM_TEST_ETH_NUM; i++) {
  97. ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
  98. ethname[i], &dev[i]));
  99. ut_assertok(device_remove(dev[i]));
  100. /* Invalidate MAC address */
  101. strcpy(ethaddr[i], getenv(addrname[i]));
  102. /* Must disable access protection for ethaddr before clearing */
  103. setenv(".flags", addrname[i]);
  104. setenv(addrname[i], NULL);
  105. }
  106. /* Set ethact to "eth@10002000" */
  107. setenv("ethact", ethname[0]);
  108. /* Segment fault might happen if something is wrong */
  109. ut_asserteq(-ENODEV, net_loop(PING));
  110. for (i = 0; i < DM_TEST_ETH_NUM; i++) {
  111. /* Restore the env */
  112. setenv(".flags", addrname[i]);
  113. setenv(addrname[i], ethaddr[i]);
  114. /* Probe the device again */
  115. ut_assertok(device_probe(dev[i]));
  116. }
  117. setenv(".flags", NULL);
  118. setenv("ethact", NULL);
  119. return 0;
  120. }
  121. DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT);
  122. /* The asserts include a return on fail; cleanup in the caller */
  123. static int _dm_test_eth_rotate1(struct unit_test_state *uts)
  124. {
  125. /* Make sure that the default is to rotate to the next interface */
  126. setenv("ethact", "eth@10004000");
  127. ut_assertok(net_loop(PING));
  128. ut_asserteq_str("eth@10002000", getenv("ethact"));
  129. /* If ethrotate is no, then we should fail on a bad MAC */
  130. setenv("ethact", "eth@10004000");
  131. setenv("ethrotate", "no");
  132. ut_asserteq(-EINVAL, net_loop(PING));
  133. ut_asserteq_str("eth@10004000", getenv("ethact"));
  134. return 0;
  135. }
  136. static int _dm_test_eth_rotate2(struct unit_test_state *uts)
  137. {
  138. /* Make sure we can skip invalid devices */
  139. setenv("ethact", "eth@10004000");
  140. ut_assertok(net_loop(PING));
  141. ut_asserteq_str("eth@10004000", getenv("ethact"));
  142. /* Make sure we can handle device name which is not eth# */
  143. setenv("ethact", "sbe5");
  144. ut_assertok(net_loop(PING));
  145. ut_asserteq_str("sbe5", getenv("ethact"));
  146. return 0;
  147. }
  148. static int dm_test_eth_rotate(struct unit_test_state *uts)
  149. {
  150. char ethaddr[18];
  151. int retval;
  152. /* Set target IP to mock ping */
  153. net_ping_ip = string_to_ip("1.1.2.2");
  154. /* Invalidate eth1's MAC address */
  155. strcpy(ethaddr, getenv("eth1addr"));
  156. /* Must disable access protection for eth1addr before clearing */
  157. setenv(".flags", "eth1addr");
  158. setenv("eth1addr", NULL);
  159. retval = _dm_test_eth_rotate1(uts);
  160. /* Restore the env */
  161. setenv("eth1addr", ethaddr);
  162. setenv("ethrotate", NULL);
  163. if (!retval) {
  164. /* Invalidate eth0's MAC address */
  165. strcpy(ethaddr, getenv("ethaddr"));
  166. /* Must disable access protection for ethaddr before clearing */
  167. setenv(".flags", "ethaddr");
  168. setenv("ethaddr", NULL);
  169. retval = _dm_test_eth_rotate2(uts);
  170. /* Restore the env */
  171. setenv("ethaddr", ethaddr);
  172. }
  173. /* Restore the env */
  174. setenv(".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. setenv("ethact", "eth@10004000");
  187. setenv("netretry", "yes");
  188. sandbox_eth_skip_timeout();
  189. ut_assertok(net_loop(PING));
  190. ut_asserteq_str("eth@10002000", getenv("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. setenv("ethact", "eth@10004000");
  196. setenv("netretry", "no");
  197. sandbox_eth_skip_timeout();
  198. ut_asserteq(-ETIMEDOUT, net_loop(PING));
  199. ut_asserteq_str("eth@10004000", getenv("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. setenv("netretry", NULL);
  209. sandbox_eth_disable_response(1, false);
  210. return retval;
  211. }
  212. DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);