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