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