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