Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 National Instruments |
| 4 | * |
| 5 | * (C) Copyright 2015 |
| 6 | * Joe Hershberger <joe.hershberger@ni.com> |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 11 | #include <env.h> |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 12 | #include <fdtdec.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <net.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | #include <dm/test.h> |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
| 18 | #include <dm/uclass-internal.h> |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 19 | #include <asm/eth.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | #include <test/ut.h> |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 21 | |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 22 | #define DM_TEST_ETH_NUM 4 |
| 23 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 24 | static int dm_test_eth(struct unit_test_state *uts) |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 25 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 26 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 27 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 28 | env_set("ethact", "eth@10002000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 29 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 30 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 31 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 32 | env_set("ethact", "eth@10003000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 33 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 34 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 35 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 36 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 37 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 38 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 43 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 44 | static int dm_test_eth_alias(struct unit_test_state *uts) |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 45 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 46 | net_ping_ip = string_to_ip("1.1.2.2"); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 47 | env_set("ethact", "eth0"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 48 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 49 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 50 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 51 | env_set("ethact", "eth1"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 52 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 53 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 54 | |
| 55 | /* Expected to fail since eth2 is not defined in the device tree */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 56 | env_set("ethact", "eth2"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 57 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 58 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 59 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 60 | env_set("ethact", "eth5"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 61 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 62 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 67 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 68 | static int dm_test_eth_prime(struct unit_test_state *uts) |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 69 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 70 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 71 | |
| 72 | /* Expected to be "eth@10003000" because of ethprime variable */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 73 | env_set("ethact", NULL); |
| 74 | env_set("ethprime", "eth5"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 75 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 76 | ut_asserteq_str("eth@10003000", env_get("ethact")); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 77 | |
| 78 | /* Expected to be "eth@10002000" because it is first */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 79 | env_set("ethact", NULL); |
| 80 | env_set("ethprime", NULL); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 81 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 82 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 87 | |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 88 | /** |
| 89 | * This test case is trying to test the following scenario: |
| 90 | * - All ethernet devices are not probed |
| 91 | * - "ethaddr" for all ethernet devices are not set |
| 92 | * - "ethact" is set to a valid ethernet device name |
| 93 | * |
| 94 | * With Sandbox default test configuration, all ethernet devices are |
| 95 | * probed after power-up, so we have to manually create such scenario: |
| 96 | * - Remove all ethernet devices |
| 97 | * - Remove all "ethaddr" environment variables |
| 98 | * - Set "ethact" to the first ethernet device |
| 99 | * |
| 100 | * Do a ping test to see if anything goes wrong. |
| 101 | */ |
| 102 | static int dm_test_eth_act(struct unit_test_state *uts) |
| 103 | { |
| 104 | struct udevice *dev[DM_TEST_ETH_NUM]; |
| 105 | const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000", |
| 106 | "sbe5", "eth@10004000"}; |
| 107 | const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr", |
| 108 | "eth3addr", "eth1addr"}; |
| 109 | char ethaddr[DM_TEST_ETH_NUM][18]; |
| 110 | int i; |
| 111 | |
Tom Rini | e2e6dae | 2017-09-26 14:08:30 -0400 | [diff] [blame] | 112 | memset(ethaddr, '\0', sizeof(ethaddr)); |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 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])); |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 119 | ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL)); |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 120 | |
| 121 | /* Invalidate MAC address */ |
Tom Rini | e2e6dae | 2017-09-26 14:08:30 -0400 | [diff] [blame] | 122 | strncpy(ethaddr[i], env_get(addrname[i]), 17); |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 123 | /* Must disable access protection for ethaddr before clearing */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 124 | env_set(".flags", addrname[i]); |
| 125 | env_set(addrname[i], NULL); |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* Set ethact to "eth@10002000" */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 129 | env_set("ethact", ethname[0]); |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 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 */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 136 | env_set(".flags", addrname[i]); |
| 137 | env_set(addrname[i], ethaddr[i]); |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 138 | |
| 139 | /* Probe the device again */ |
| 140 | ut_assertok(device_probe(dev[i])); |
| 141 | } |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 142 | env_set(".flags", NULL); |
| 143 | env_set("ethact", NULL); |
Bin Meng | 6d9764c | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT); |
| 148 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 149 | /* The asserts include a return on fail; cleanup in the caller */ |
| 150 | static int _dm_test_eth_rotate1(struct unit_test_state *uts) |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 151 | { |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 152 | /* Make sure that the default is to rotate to the next interface */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 153 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 154 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 155 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 156 | |
| 157 | /* If ethrotate is no, then we should fail on a bad MAC */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 158 | env_set("ethact", "eth@10004000"); |
| 159 | env_set("ethrotate", "no"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 160 | ut_asserteq(-EINVAL, net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 161 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 162 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 163 | return 0; |
| 164 | } |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 165 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 166 | static int _dm_test_eth_rotate2(struct unit_test_state *uts) |
| 167 | { |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 168 | /* Make sure we can skip invalid devices */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 169 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 170 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 171 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 172 | |
Bin Meng | 71d7971 | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 173 | /* Make sure we can handle device name which is not eth# */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 174 | env_set("ethact", "sbe5"); |
Bin Meng | 71d7971 | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 175 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 176 | ut_asserteq_str("sbe5", env_get("ethact")); |
Bin Meng | 71d7971 | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 177 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 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 */ |
Tom Rini | e2e6dae | 2017-09-26 14:08:30 -0400 | [diff] [blame] | 190 | memset(ethaddr, '\0', sizeof(ethaddr)); |
| 191 | strncpy(ethaddr, env_get("eth1addr"), 17); |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 192 | /* Must disable access protection for eth1addr before clearing */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 193 | env_set(".flags", "eth1addr"); |
| 194 | env_set("eth1addr", NULL); |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 195 | |
| 196 | retval = _dm_test_eth_rotate1(uts); |
| 197 | |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 198 | /* Restore the env */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 199 | env_set("eth1addr", ethaddr); |
| 200 | env_set("ethrotate", NULL); |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 201 | |
| 202 | if (!retval) { |
| 203 | /* Invalidate eth0's MAC address */ |
Tom Rini | e2e6dae | 2017-09-26 14:08:30 -0400 | [diff] [blame] | 204 | strncpy(ethaddr, env_get("ethaddr"), 17); |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 205 | /* Must disable access protection for ethaddr before clearing */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 206 | env_set(".flags", "ethaddr"); |
| 207 | env_set("ethaddr", NULL); |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 208 | |
| 209 | retval = _dm_test_eth_rotate2(uts); |
| 210 | |
| 211 | /* Restore the env */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 212 | env_set("ethaddr", ethaddr); |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 213 | } |
| 214 | /* Restore the env */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 215 | env_set(".flags", NULL); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 216 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 217 | return retval; |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 218 | } |
| 219 | DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 220 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 221 | /* The asserts include a return on fail; cleanup in the caller */ |
| 222 | static int _dm_test_net_retry(struct unit_test_state *uts) |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 223 | { |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 224 | /* |
| 225 | * eth1 is disabled and netretry is yes, so the ping should succeed and |
| 226 | * the active device should be eth0 |
| 227 | */ |
| 228 | sandbox_eth_disable_response(1, true); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 229 | env_set("ethact", "eth@10004000"); |
| 230 | env_set("netretry", "yes"); |
Joe Hershberger | 172a31b | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 231 | sandbox_eth_skip_timeout(); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 232 | ut_assertok(net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 233 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * eth1 is disabled and netretry is no, so the ping should fail and the |
| 237 | * active device should be eth1 |
| 238 | */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 239 | env_set("ethact", "eth@10004000"); |
| 240 | env_set("netretry", "no"); |
Joe Hershberger | 172a31b | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 241 | sandbox_eth_skip_timeout(); |
Thomas RIENOESSL | a735e6e | 2018-11-21 15:56:07 +0100 | [diff] [blame] | 242 | ut_asserteq(-ENONET, net_loop(PING)); |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 243 | ut_asserteq_str("eth@10004000", env_get("ethact")); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 244 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int dm_test_net_retry(struct unit_test_state *uts) |
| 249 | { |
| 250 | int retval; |
| 251 | |
| 252 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 253 | |
| 254 | retval = _dm_test_net_retry(uts); |
| 255 | |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 256 | /* Restore the env */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 257 | env_set("netretry", NULL); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 258 | sandbox_eth_disable_response(1, false); |
| 259 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 260 | return retval; |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 261 | } |
| 262 | DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 45988da | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 263 | |
| 264 | static int sb_check_arp_reply(struct udevice *dev, void *packet, |
| 265 | unsigned int len) |
| 266 | { |
| 267 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 268 | struct ethernet_hdr *eth = packet; |
| 269 | struct arp_hdr *arp; |
| 270 | /* Used by all of the ut_assert macros */ |
| 271 | struct unit_test_state *uts = priv->priv; |
| 272 | |
| 273 | if (ntohs(eth->et_protlen) != PROT_ARP) |
| 274 | return 0; |
| 275 | |
| 276 | arp = packet + ETHER_HDR_SIZE; |
| 277 | |
| 278 | if (ntohs(arp->ar_op) != ARPOP_REPLY) |
| 279 | return 0; |
| 280 | |
| 281 | /* This test would be worthless if we are not waiting */ |
| 282 | ut_assert(arp_is_waiting()); |
| 283 | |
| 284 | /* Validate response */ |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame^] | 285 | ut_asserteq_mem(eth->et_src, net_ethaddr, ARP_HLEN); |
| 286 | ut_asserteq_mem(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN); |
Joe Hershberger | 45988da | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 287 | ut_assert(eth->et_protlen == htons(PROT_ARP)); |
| 288 | |
| 289 | ut_assert(arp->ar_hrd == htons(ARP_ETHER)); |
| 290 | ut_assert(arp->ar_pro == htons(PROT_IP)); |
| 291 | ut_assert(arp->ar_hln == ARP_HLEN); |
| 292 | ut_assert(arp->ar_pln == ARP_PLEN); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame^] | 293 | ut_asserteq_mem(&arp->ar_sha, net_ethaddr, ARP_HLEN); |
Joe Hershberger | 45988da | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 294 | ut_assert(net_read_ip(&arp->ar_spa).s_addr == net_ip.s_addr); |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame^] | 295 | ut_asserteq_mem(&arp->ar_tha, priv->fake_host_hwaddr, ARP_HLEN); |
Joe Hershberger | 45988da | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 296 | ut_assert(net_read_ip(&arp->ar_tpa).s_addr == |
| 297 | string_to_ip("1.1.2.4").s_addr); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int sb_with_async_arp_handler(struct udevice *dev, void *packet, |
| 303 | unsigned int len) |
| 304 | { |
| 305 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 306 | struct ethernet_hdr *eth = packet; |
| 307 | struct arp_hdr *arp = packet + ETHER_HDR_SIZE; |
| 308 | int ret; |
| 309 | |
| 310 | /* |
| 311 | * If we are about to generate a reply to ARP, first inject a request |
| 312 | * from another host |
| 313 | */ |
| 314 | if (ntohs(eth->et_protlen) == PROT_ARP && |
| 315 | ntohs(arp->ar_op) == ARPOP_REQUEST) { |
| 316 | /* Make sure sandbox_eth_recv_arp_req() knows who is asking */ |
| 317 | priv->fake_host_ipaddr = string_to_ip("1.1.2.4"); |
| 318 | |
| 319 | ret = sandbox_eth_recv_arp_req(dev); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | sandbox_eth_arp_req_to_reply(dev, packet, len); |
| 325 | sandbox_eth_ping_req_to_reply(dev, packet, len); |
| 326 | |
| 327 | return sb_check_arp_reply(dev, packet, len); |
| 328 | } |
| 329 | |
| 330 | static int dm_test_eth_async_arp_reply(struct unit_test_state *uts) |
| 331 | { |
| 332 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 333 | |
| 334 | sandbox_eth_set_tx_handler(0, sb_with_async_arp_handler); |
| 335 | /* Used by all of the ut_assert macros in the tx_handler */ |
| 336 | sandbox_eth_set_priv(0, uts); |
Joe Hershberger | 45988da | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 337 | |
| 338 | env_set("ethact", "eth@10002000"); |
Joe Hershberger | ac3f26c | 2018-09-26 16:49:02 -0500 | [diff] [blame] | 339 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 45988da | 2018-09-26 16:49:00 -0500 | [diff] [blame] | 340 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
| 341 | |
| 342 | sandbox_eth_set_tx_handler(0, NULL); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | DM_TEST(dm_test_eth_async_arp_reply, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 72ff004 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 348 | |
| 349 | static int sb_check_ping_reply(struct udevice *dev, void *packet, |
| 350 | unsigned int len) |
| 351 | { |
| 352 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 353 | struct ethernet_hdr *eth = packet; |
| 354 | struct ip_udp_hdr *ip; |
| 355 | struct icmp_hdr *icmp; |
| 356 | /* Used by all of the ut_assert macros */ |
| 357 | struct unit_test_state *uts = priv->priv; |
| 358 | |
| 359 | if (ntohs(eth->et_protlen) != PROT_IP) |
| 360 | return 0; |
| 361 | |
| 362 | ip = packet + ETHER_HDR_SIZE; |
| 363 | |
| 364 | if (ip->ip_p != IPPROTO_ICMP) |
| 365 | return 0; |
| 366 | |
| 367 | icmp = (struct icmp_hdr *)&ip->udp_src; |
| 368 | |
| 369 | if (icmp->type != ICMP_ECHO_REPLY) |
| 370 | return 0; |
| 371 | |
| 372 | /* This test would be worthless if we are not waiting */ |
| 373 | ut_assert(arp_is_waiting()); |
| 374 | |
| 375 | /* Validate response */ |
Simon Glass | f91f366 | 2020-05-10 12:52:45 -0600 | [diff] [blame^] | 376 | ut_asserteq_mem(eth->et_src, net_ethaddr, ARP_HLEN); |
| 377 | ut_asserteq_mem(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN); |
Joe Hershberger | 72ff004 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 378 | ut_assert(eth->et_protlen == htons(PROT_IP)); |
| 379 | |
| 380 | ut_assert(net_read_ip(&ip->ip_src).s_addr == net_ip.s_addr); |
| 381 | ut_assert(net_read_ip(&ip->ip_dst).s_addr == |
| 382 | string_to_ip("1.1.2.4").s_addr); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int sb_with_async_ping_handler(struct udevice *dev, void *packet, |
| 388 | unsigned int len) |
| 389 | { |
| 390 | struct eth_sandbox_priv *priv = dev_get_priv(dev); |
| 391 | struct ethernet_hdr *eth = packet; |
| 392 | struct arp_hdr *arp = packet + ETHER_HDR_SIZE; |
| 393 | int ret; |
| 394 | |
| 395 | /* |
| 396 | * If we are about to generate a reply to ARP, first inject a request |
| 397 | * from another host |
| 398 | */ |
| 399 | if (ntohs(eth->et_protlen) == PROT_ARP && |
| 400 | ntohs(arp->ar_op) == ARPOP_REQUEST) { |
| 401 | /* Make sure sandbox_eth_recv_arp_req() knows who is asking */ |
| 402 | priv->fake_host_ipaddr = string_to_ip("1.1.2.4"); |
| 403 | |
| 404 | ret = sandbox_eth_recv_ping_req(dev); |
| 405 | if (ret) |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | sandbox_eth_arp_req_to_reply(dev, packet, len); |
| 410 | sandbox_eth_ping_req_to_reply(dev, packet, len); |
| 411 | |
| 412 | return sb_check_ping_reply(dev, packet, len); |
| 413 | } |
| 414 | |
| 415 | static int dm_test_eth_async_ping_reply(struct unit_test_state *uts) |
| 416 | { |
| 417 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 418 | |
| 419 | sandbox_eth_set_tx_handler(0, sb_with_async_ping_handler); |
| 420 | /* Used by all of the ut_assert macros in the tx_handler */ |
| 421 | sandbox_eth_set_priv(0, uts); |
Joe Hershberger | 72ff004 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 422 | |
| 423 | env_set("ethact", "eth@10002000"); |
Joe Hershberger | ac3f26c | 2018-09-26 16:49:02 -0500 | [diff] [blame] | 424 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 72ff004 | 2018-09-26 16:49:01 -0500 | [diff] [blame] | 425 | ut_asserteq_str("eth@10002000", env_get("ethact")); |
| 426 | |
| 427 | sandbox_eth_set_tx_handler(0, NULL); |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | DM_TEST(dm_test_eth_async_ping_reply, DM_TESTF_SCAN_FDT); |