]>
Commit | Line | Data |
---|---|---|
bfacad7d JH |
1 | /* |
2 | * Copyright (c) 2015 National Instruments | |
3 | * | |
4 | * (C) Copyright 2015 | |
5 | * Joe Hershberger <[email protected]> | |
6 | * | |
7 | * SPDX-License-Identifier: GPL-2.0 | |
8 | */ | |
9 | ||
10 | #include <common.h> | |
11 | #include <dm.h> | |
bfacad7d JH |
12 | #include <fdtdec.h> |
13 | #include <malloc.h> | |
14 | #include <net.h> | |
e721b882 | 15 | #include <dm/test.h> |
6d9764c2 BM |
16 | #include <dm/device-internal.h> |
17 | #include <dm/uclass-internal.h> | |
7ece1c61 | 18 | #include <asm/eth.h> |
e721b882 | 19 | #include <test/ut.h> |
bfacad7d JH |
20 | |
21 | DECLARE_GLOBAL_DATA_PTR; | |
22 | ||
6d9764c2 BM |
23 | #define DM_TEST_ETH_NUM 4 |
24 | ||
e721b882 | 25 | static int dm_test_eth(struct unit_test_state *uts) |
bfacad7d | 26 | { |
049a95a7 | 27 | net_ping_ip = string_to_ip("1.1.2.2"); |
bfacad7d | 28 | |
382bee57 | 29 | env_set("ethact", "eth@10002000"); |
bc0571fc | 30 | ut_assertok(net_loop(PING)); |
00caae6d | 31 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
bfacad7d | 32 | |
382bee57 | 33 | env_set("ethact", "eth@10003000"); |
bc0571fc | 34 | ut_assertok(net_loop(PING)); |
00caae6d | 35 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
bfacad7d | 36 | |
382bee57 | 37 | env_set("ethact", "eth@10004000"); |
bc0571fc | 38 | ut_assertok(net_loop(PING)); |
00caae6d | 39 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
bfacad7d JH |
40 | |
41 | return 0; | |
42 | } | |
43 | DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT); | |
e58780dc | 44 | |
e721b882 | 45 | static int dm_test_eth_alias(struct unit_test_state *uts) |
e58780dc | 46 | { |
049a95a7 | 47 | net_ping_ip = string_to_ip("1.1.2.2"); |
382bee57 | 48 | env_set("ethact", "eth0"); |
bc0571fc | 49 | ut_assertok(net_loop(PING)); |
00caae6d | 50 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
e58780dc | 51 | |
382bee57 | 52 | env_set("ethact", "eth1"); |
bc0571fc | 53 | ut_assertok(net_loop(PING)); |
00caae6d | 54 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
e58780dc JH |
55 | |
56 | /* Expected to fail since eth2 is not defined in the device tree */ | |
382bee57 | 57 | env_set("ethact", "eth2"); |
bc0571fc | 58 | ut_assertok(net_loop(PING)); |
00caae6d | 59 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
e58780dc | 60 | |
382bee57 | 61 | env_set("ethact", "eth5"); |
bc0571fc | 62 | ut_assertok(net_loop(PING)); |
00caae6d | 63 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
e58780dc JH |
64 | |
65 | return 0; | |
66 | } | |
67 | DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT); | |
6536b9bb | 68 | |
e721b882 | 69 | static int dm_test_eth_prime(struct unit_test_state *uts) |
6536b9bb | 70 | { |
049a95a7 | 71 | net_ping_ip = string_to_ip("1.1.2.2"); |
6536b9bb JH |
72 | |
73 | /* Expected to be "eth@10003000" because of ethprime variable */ | |
382bee57 SG |
74 | env_set("ethact", NULL); |
75 | env_set("ethprime", "eth5"); | |
bc0571fc | 76 | ut_assertok(net_loop(PING)); |
00caae6d | 77 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
6536b9bb JH |
78 | |
79 | /* Expected to be "eth@10002000" because it is first */ | |
382bee57 SG |
80 | env_set("ethact", NULL); |
81 | env_set("ethprime", NULL); | |
bc0571fc | 82 | ut_assertok(net_loop(PING)); |
00caae6d | 83 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
6536b9bb JH |
84 | |
85 | return 0; | |
86 | } | |
87 | DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT); | |
7d104eab | 88 | |
6d9764c2 BM |
89 | /** |
90 | * This test case is trying to test the following scenario: | |
91 | * - All ethernet devices are not probed | |
92 | * - "ethaddr" for all ethernet devices are not set | |
93 | * - "ethact" is set to a valid ethernet device name | |
94 | * | |
95 | * With Sandbox default test configuration, all ethernet devices are | |
96 | * probed after power-up, so we have to manually create such scenario: | |
97 | * - Remove all ethernet devices | |
98 | * - Remove all "ethaddr" environment variables | |
99 | * - Set "ethact" to the first ethernet device | |
100 | * | |
101 | * Do a ping test to see if anything goes wrong. | |
102 | */ | |
103 | static int dm_test_eth_act(struct unit_test_state *uts) | |
104 | { | |
105 | struct udevice *dev[DM_TEST_ETH_NUM]; | |
106 | const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000", | |
107 | "sbe5", "eth@10004000"}; | |
108 | const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr", | |
109 | "eth3addr", "eth1addr"}; | |
110 | char ethaddr[DM_TEST_ETH_NUM][18]; | |
111 | int i; | |
112 | ||
113 | net_ping_ip = string_to_ip("1.1.2.2"); | |
114 | ||
115 | /* Prepare the test scenario */ | |
116 | for (i = 0; i < DM_TEST_ETH_NUM; i++) { | |
117 | ut_assertok(uclass_find_device_by_name(UCLASS_ETH, | |
118 | ethname[i], &dev[i])); | |
706865af | 119 | ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL)); |
6d9764c2 BM |
120 | |
121 | /* Invalidate MAC address */ | |
00caae6d | 122 | strcpy(ethaddr[i], env_get(addrname[i])); |
6d9764c2 | 123 | /* Must disable access protection for ethaddr before clearing */ |
382bee57 SG |
124 | env_set(".flags", addrname[i]); |
125 | env_set(addrname[i], NULL); | |
6d9764c2 BM |
126 | } |
127 | ||
128 | /* Set ethact to "eth@10002000" */ | |
382bee57 | 129 | env_set("ethact", ethname[0]); |
6d9764c2 BM |
130 | |
131 | /* Segment fault might happen if something is wrong */ | |
132 | ut_asserteq(-ENODEV, net_loop(PING)); | |
133 | ||
134 | for (i = 0; i < DM_TEST_ETH_NUM; i++) { | |
135 | /* Restore the env */ | |
382bee57 SG |
136 | env_set(".flags", addrname[i]); |
137 | env_set(addrname[i], ethaddr[i]); | |
6d9764c2 BM |
138 | |
139 | /* Probe the device again */ | |
140 | ut_assertok(device_probe(dev[i])); | |
141 | } | |
382bee57 SG |
142 | env_set(".flags", NULL); |
143 | env_set("ethact", NULL); | |
6d9764c2 BM |
144 | |
145 | return 0; | |
146 | } | |
147 | DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT); | |
148 | ||
09129bec JH |
149 | /* The asserts include a return on fail; cleanup in the caller */ |
150 | static int _dm_test_eth_rotate1(struct unit_test_state *uts) | |
7d104eab | 151 | { |
7d104eab | 152 | /* Make sure that the default is to rotate to the next interface */ |
382bee57 | 153 | env_set("ethact", "eth@10004000"); |
bc0571fc | 154 | ut_assertok(net_loop(PING)); |
00caae6d | 155 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
7d104eab JH |
156 | |
157 | /* If ethrotate is no, then we should fail on a bad MAC */ | |
382bee57 SG |
158 | env_set("ethact", "eth@10004000"); |
159 | env_set("ethrotate", "no"); | |
bc0571fc | 160 | ut_asserteq(-EINVAL, net_loop(PING)); |
00caae6d | 161 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
7d104eab | 162 | |
09129bec JH |
163 | return 0; |
164 | } | |
7d104eab | 165 | |
09129bec JH |
166 | static int _dm_test_eth_rotate2(struct unit_test_state *uts) |
167 | { | |
7d104eab | 168 | /* Make sure we can skip invalid devices */ |
382bee57 | 169 | env_set("ethact", "eth@10004000"); |
bc0571fc | 170 | ut_assertok(net_loop(PING)); |
00caae6d | 171 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
7d104eab | 172 | |
71d7971f | 173 | /* Make sure we can handle device name which is not eth# */ |
382bee57 | 174 | env_set("ethact", "sbe5"); |
71d7971f | 175 | ut_assertok(net_loop(PING)); |
00caae6d | 176 | ut_asserteq_str("sbe5", env_get("ethact")); |
71d7971f | 177 | |
09129bec JH |
178 | return 0; |
179 | } | |
180 | ||
181 | static int dm_test_eth_rotate(struct unit_test_state *uts) | |
182 | { | |
183 | char ethaddr[18]; | |
184 | int retval; | |
185 | ||
186 | /* Set target IP to mock ping */ | |
187 | net_ping_ip = string_to_ip("1.1.2.2"); | |
188 | ||
189 | /* Invalidate eth1's MAC address */ | |
00caae6d | 190 | strcpy(ethaddr, env_get("eth1addr")); |
09129bec | 191 | /* Must disable access protection for eth1addr before clearing */ |
382bee57 SG |
192 | env_set(".flags", "eth1addr"); |
193 | env_set("eth1addr", NULL); | |
09129bec JH |
194 | |
195 | retval = _dm_test_eth_rotate1(uts); | |
196 | ||
197 | /* Restore the env */ | |
382bee57 SG |
198 | env_set("eth1addr", ethaddr); |
199 | env_set("ethrotate", NULL); | |
09129bec JH |
200 | |
201 | if (!retval) { | |
202 | /* Invalidate eth0's MAC address */ | |
00caae6d | 203 | strcpy(ethaddr, env_get("ethaddr")); |
09129bec | 204 | /* Must disable access protection for ethaddr before clearing */ |
382bee57 SG |
205 | env_set(".flags", "ethaddr"); |
206 | env_set("ethaddr", NULL); | |
09129bec JH |
207 | |
208 | retval = _dm_test_eth_rotate2(uts); | |
209 | ||
210 | /* Restore the env */ | |
382bee57 | 211 | env_set("ethaddr", ethaddr); |
09129bec | 212 | } |
7d104eab | 213 | /* Restore the env */ |
382bee57 | 214 | env_set(".flags", NULL); |
7d104eab | 215 | |
09129bec | 216 | return retval; |
7d104eab JH |
217 | } |
218 | DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT); | |
7ece1c61 | 219 | |
09129bec JH |
220 | /* The asserts include a return on fail; cleanup in the caller */ |
221 | static int _dm_test_net_retry(struct unit_test_state *uts) | |
7ece1c61 | 222 | { |
7ece1c61 JH |
223 | /* |
224 | * eth1 is disabled and netretry is yes, so the ping should succeed and | |
225 | * the active device should be eth0 | |
226 | */ | |
227 | sandbox_eth_disable_response(1, true); | |
382bee57 SG |
228 | env_set("ethact", "eth@10004000"); |
229 | env_set("netretry", "yes"); | |
172a31bf | 230 | sandbox_eth_skip_timeout(); |
bc0571fc | 231 | ut_assertok(net_loop(PING)); |
00caae6d | 232 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
7ece1c61 JH |
233 | |
234 | /* | |
235 | * eth1 is disabled and netretry is no, so the ping should fail and the | |
236 | * active device should be eth1 | |
237 | */ | |
382bee57 SG |
238 | env_set("ethact", "eth@10004000"); |
239 | env_set("netretry", "no"); | |
172a31bf | 240 | sandbox_eth_skip_timeout(); |
bc0571fc | 241 | ut_asserteq(-ETIMEDOUT, net_loop(PING)); |
00caae6d | 242 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
7ece1c61 | 243 | |
09129bec JH |
244 | return 0; |
245 | } | |
246 | ||
247 | static int dm_test_net_retry(struct unit_test_state *uts) | |
248 | { | |
249 | int retval; | |
250 | ||
251 | net_ping_ip = string_to_ip("1.1.2.2"); | |
252 | ||
253 | retval = _dm_test_net_retry(uts); | |
254 | ||
7ece1c61 | 255 | /* Restore the env */ |
382bee57 | 256 | env_set("netretry", NULL); |
7ece1c61 JH |
257 | sandbox_eth_disable_response(1, false); |
258 | ||
09129bec | 259 | return retval; |
7ece1c61 JH |
260 | } |
261 | DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT); |