eth.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. env_set("ethact", "eth@10002000");
  25. ut_assertok(net_loop(PING));
  26. ut_asserteq_str("eth@10002000", env_get("ethact"));
  27. env_set("ethact", "eth@10003000");
  28. ut_assertok(net_loop(PING));
  29. ut_asserteq_str("eth@10003000", env_get("ethact"));
  30. env_set("ethact", "eth@10004000");
  31. ut_assertok(net_loop(PING));
  32. ut_asserteq_str("eth@10004000", env_get("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. env_set("ethact", "eth0");
  40. ut_assertok(net_loop(PING));
  41. ut_asserteq_str("eth@10002000", env_get("ethact"));
  42. env_set("ethact", "eth1");
  43. ut_assertok(net_loop(PING));
  44. ut_asserteq_str("eth@10004000", env_get("ethact"));
  45. /* Expected to fail since eth2 is not defined in the device tree */
  46. env_set("ethact", "eth2");
  47. ut_assertok(net_loop(PING));
  48. ut_asserteq_str("eth@10002000", env_get("ethact"));
  49. env_set("ethact", "eth5");
  50. ut_assertok(net_loop(PING));
  51. ut_asserteq_str("eth@10003000", env_get("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. env_set("ethact", NULL);
  60. env_set("ethprime", "eth5");
  61. ut_assertok(net_loop(PING));
  62. ut_asserteq_str("eth@10003000", env_get("ethact"));
  63. /* Expected to be "eth@10002000" because it is first */
  64. env_set("ethact", NULL);
  65. env_set("ethprime", NULL);
  66. ut_assertok(net_loop(PING));
  67. ut_asserteq_str("eth@10002000", env_get("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. memset(ethaddr, '\0', sizeof(ethaddr));
  95. net_ping_ip = string_to_ip("1.1.2.2");
  96. /* Prepare the test scenario */
  97. for (i = 0; i < DM_TEST_ETH_NUM; i++) {
  98. ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
  99. ethname[i], &dev[i]));
  100. ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL));
  101. /* Invalidate MAC address */
  102. strncpy(ethaddr[i], env_get(addrname[i]), 17);
  103. /* Must disable access protection for ethaddr before clearing */
  104. env_set(".flags", addrname[i]);
  105. env_set(addrname[i], NULL);
  106. }
  107. /* Set ethact to "eth@10002000" */
  108. env_set("ethact", ethname[0]);
  109. /* Segment fault might happen if something is wrong */
  110. ut_asserteq(-ENODEV, net_loop(PING));
  111. for (i = 0; i < DM_TEST_ETH_NUM; i++) {
  112. /* Restore the env */
  113. env_set(".flags", addrname[i]);
  114. env_set(addrname[i], ethaddr[i]);
  115. /* Probe the device again */
  116. ut_assertok(device_probe(dev[i]));
  117. }
  118. env_set(".flags", NULL);
  119. env_set("ethact", NULL);
  120. return 0;
  121. }
  122. DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT);
  123. /* The asserts include a return on fail; cleanup in the caller */
  124. static int _dm_test_eth_rotate1(struct unit_test_state *uts)
  125. {
  126. /* Make sure that the default is to rotate to the next interface */
  127. env_set("ethact", "eth@10004000");
  128. ut_assertok(net_loop(PING));
  129. ut_asserteq_str("eth@10002000", env_get("ethact"));
  130. /* If ethrotate is no, then we should fail on a bad MAC */
  131. env_set("ethact", "eth@10004000");
  132. env_set("ethrotate", "no");
  133. ut_asserteq(-EINVAL, net_loop(PING));
  134. ut_asserteq_str("eth@10004000", env_get("ethact"));
  135. return 0;
  136. }
  137. static int _dm_test_eth_rotate2(struct unit_test_state *uts)
  138. {
  139. /* Make sure we can skip invalid devices */
  140. env_set("ethact", "eth@10004000");
  141. ut_assertok(net_loop(PING));
  142. ut_asserteq_str("eth@10004000", env_get("ethact"));
  143. /* Make sure we can handle device name which is not eth# */
  144. env_set("ethact", "sbe5");
  145. ut_assertok(net_loop(PING));
  146. ut_asserteq_str("sbe5", env_get("ethact"));
  147. return 0;
  148. }
  149. static int dm_test_eth_rotate(struct unit_test_state *uts)
  150. {
  151. char ethaddr[18];
  152. int retval;
  153. /* Set target IP to mock ping */
  154. net_ping_ip = string_to_ip("1.1.2.2");
  155. /* Invalidate eth1's MAC address */
  156. memset(ethaddr, '\0', sizeof(ethaddr));
  157. strncpy(ethaddr, env_get("eth1addr"), 17);
  158. /* Must disable access protection for eth1addr before clearing */
  159. env_set(".flags", "eth1addr");
  160. env_set("eth1addr", NULL);
  161. retval = _dm_test_eth_rotate1(uts);
  162. /* Restore the env */
  163. env_set("eth1addr", ethaddr);
  164. env_set("ethrotate", NULL);
  165. if (!retval) {
  166. /* Invalidate eth0's MAC address */
  167. strncpy(ethaddr, env_get("ethaddr"), 17);
  168. /* Must disable access protection for ethaddr before clearing */
  169. env_set(".flags", "ethaddr");
  170. env_set("ethaddr", NULL);
  171. retval = _dm_test_eth_rotate2(uts);
  172. /* Restore the env */
  173. env_set("ethaddr", ethaddr);
  174. }
  175. /* Restore the env */
  176. env_set(".flags", NULL);
  177. return retval;
  178. }
  179. DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
  180. /* The asserts include a return on fail; cleanup in the caller */
  181. static int _dm_test_net_retry(struct unit_test_state *uts)
  182. {
  183. /*
  184. * eth1 is disabled and netretry is yes, so the ping should succeed and
  185. * the active device should be eth0
  186. */
  187. sandbox_eth_disable_response(1, true);
  188. env_set("ethact", "eth@10004000");
  189. env_set("netretry", "yes");
  190. sandbox_eth_skip_timeout();
  191. ut_assertok(net_loop(PING));
  192. ut_asserteq_str("eth@10002000", env_get("ethact"));
  193. /*
  194. * eth1 is disabled and netretry is no, so the ping should fail and the
  195. * active device should be eth1
  196. */
  197. env_set("ethact", "eth@10004000");
  198. env_set("netretry", "no");
  199. sandbox_eth_skip_timeout();
  200. ut_asserteq(-ETIMEDOUT, net_loop(PING));
  201. ut_asserteq_str("eth@10004000", env_get("ethact"));
  202. return 0;
  203. }
  204. static int dm_test_net_retry(struct unit_test_state *uts)
  205. {
  206. int retval;
  207. net_ping_ip = string_to_ip("1.1.2.2");
  208. retval = _dm_test_net_retry(uts);
  209. /* Restore the env */
  210. env_set("netretry", NULL);
  211. sandbox_eth_disable_response(1, false);
  212. return retval;
  213. }
  214. DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);