Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 National Instruments |
| 3 | * |
| 4 | * (C) Copyright 2015 |
| 5 | * Joe Hershberger <joe.hershberger@ni.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0 |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.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> |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 16 | #include <asm/eth.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 21 | static int dm_test_eth(struct unit_test_state *uts) |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 22 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 23 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 24 | |
| 25 | setenv("ethact", "eth@10002000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 26 | ut_assertok(net_loop(PING)); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 27 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 28 | |
| 29 | setenv("ethact", "eth@10003000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 30 | ut_assertok(net_loop(PING)); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 31 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 32 | |
| 33 | setenv("ethact", "eth@10004000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 34 | ut_assertok(net_loop(PING)); |
Joe Hershberger | bfacad7 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 35 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 40 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 41 | static int dm_test_eth_alias(struct unit_test_state *uts) |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 42 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 43 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 44 | setenv("ethact", "eth0"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 45 | ut_assertok(net_loop(PING)); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 46 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 47 | |
| 48 | setenv("ethact", "eth1"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 49 | ut_assertok(net_loop(PING)); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 50 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 51 | |
| 52 | /* Expected to fail since eth2 is not defined in the device tree */ |
| 53 | setenv("ethact", "eth2"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 54 | ut_assertok(net_loop(PING)); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 55 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 56 | |
| 57 | setenv("ethact", "eth5"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 58 | ut_assertok(net_loop(PING)); |
Joe Hershberger | e58780d | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 59 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 64 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 65 | static int dm_test_eth_prime(struct unit_test_state *uts) |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 66 | { |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 67 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 68 | |
| 69 | /* Expected to be "eth@10003000" because of ethprime variable */ |
| 70 | setenv("ethact", NULL); |
| 71 | setenv("ethprime", "eth5"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 72 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 73 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 74 | |
| 75 | /* Expected to be "eth@10002000" because it is first */ |
| 76 | setenv("ethact", NULL); |
| 77 | setenv("ethprime", NULL); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 78 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 6536b9b | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 79 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 84 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 85 | /* The asserts include a return on fail; cleanup in the caller */ |
| 86 | static int _dm_test_eth_rotate1(struct unit_test_state *uts) |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 87 | { |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 88 | /* Make sure that the default is to rotate to the next interface */ |
| 89 | setenv("ethact", "eth@10004000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 90 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 91 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 92 | |
| 93 | /* If ethrotate is no, then we should fail on a bad MAC */ |
| 94 | setenv("ethact", "eth@10004000"); |
| 95 | setenv("ethrotate", "no"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 96 | ut_asserteq(-EINVAL, net_loop(PING)); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 97 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 98 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 99 | return 0; |
| 100 | } |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 101 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 102 | static int _dm_test_eth_rotate2(struct unit_test_state *uts) |
| 103 | { |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 104 | /* Make sure we can skip invalid devices */ |
| 105 | setenv("ethact", "eth@10004000"); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 106 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 107 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 108 | |
Bin Meng | 71d7971 | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 109 | /* Make sure we can handle device name which is not eth# */ |
| 110 | setenv("ethact", "sbe5"); |
| 111 | ut_assertok(net_loop(PING)); |
| 112 | ut_asserteq_str("sbe5", getenv("ethact")); |
| 113 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int dm_test_eth_rotate(struct unit_test_state *uts) |
| 118 | { |
| 119 | char ethaddr[18]; |
| 120 | int retval; |
| 121 | |
| 122 | /* Set target IP to mock ping */ |
| 123 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 124 | |
| 125 | /* Invalidate eth1's MAC address */ |
| 126 | strcpy(ethaddr, getenv("eth1addr")); |
| 127 | /* Must disable access protection for eth1addr before clearing */ |
| 128 | setenv(".flags", "eth1addr"); |
| 129 | setenv("eth1addr", NULL); |
| 130 | |
| 131 | retval = _dm_test_eth_rotate1(uts); |
| 132 | |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 133 | /* Restore the env */ |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 134 | setenv("eth1addr", ethaddr); |
| 135 | setenv("ethrotate", NULL); |
| 136 | |
| 137 | if (!retval) { |
| 138 | /* Invalidate eth0's MAC address */ |
| 139 | strcpy(ethaddr, getenv("ethaddr")); |
| 140 | /* Must disable access protection for ethaddr before clearing */ |
| 141 | setenv(".flags", "ethaddr"); |
| 142 | setenv("ethaddr", NULL); |
| 143 | |
| 144 | retval = _dm_test_eth_rotate2(uts); |
| 145 | |
| 146 | /* Restore the env */ |
| 147 | setenv("ethaddr", ethaddr); |
| 148 | } |
| 149 | /* Restore the env */ |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 150 | setenv(".flags", NULL); |
| 151 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 152 | return retval; |
Joe Hershberger | 7d104ea | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 153 | } |
| 154 | DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 155 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 156 | /* The asserts include a return on fail; cleanup in the caller */ |
| 157 | static int _dm_test_net_retry(struct unit_test_state *uts) |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 158 | { |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 159 | /* |
| 160 | * eth1 is disabled and netretry is yes, so the ping should succeed and |
| 161 | * the active device should be eth0 |
| 162 | */ |
| 163 | sandbox_eth_disable_response(1, true); |
| 164 | setenv("ethact", "eth@10004000"); |
| 165 | setenv("netretry", "yes"); |
Joe Hershberger | 172a31b | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 166 | sandbox_eth_skip_timeout(); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 167 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 168 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 169 | |
| 170 | /* |
| 171 | * eth1 is disabled and netretry is no, so the ping should fail and the |
| 172 | * active device should be eth1 |
| 173 | */ |
| 174 | setenv("ethact", "eth@10004000"); |
| 175 | setenv("netretry", "no"); |
Joe Hershberger | 172a31b | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 176 | sandbox_eth_skip_timeout(); |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 177 | ut_asserteq(-ETIMEDOUT, net_loop(PING)); |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 178 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 179 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int dm_test_net_retry(struct unit_test_state *uts) |
| 184 | { |
| 185 | int retval; |
| 186 | |
| 187 | net_ping_ip = string_to_ip("1.1.2.2"); |
| 188 | |
| 189 | retval = _dm_test_net_retry(uts); |
| 190 | |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 191 | /* Restore the env */ |
| 192 | setenv("netretry", NULL); |
| 193 | sandbox_eth_disable_response(1, false); |
| 194 | |
Joe Hershberger | 09129be | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 195 | return retval; |
Joe Hershberger | 7ece1c6 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 196 | } |
| 197 | DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT); |