blob: 061584e2e1648c788a96685ae064875242ce0d11 [file] [log] [blame]
Joe Hershberger4c197242015-03-22 17:09:15 -05001/*
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 Hershberger4c197242015-03-22 17:09:15 -050012#include <fdtdec.h>
13#include <malloc.h>
14#include <net.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050015#include <dm/test.h>
Joe Hershbergerf933a932015-03-22 17:09:20 -050016#include <asm/eth.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050017#include <test/ut.h>
Joe Hershberger4c197242015-03-22 17:09:15 -050018
19DECLARE_GLOBAL_DATA_PTR;
20
Joe Hershberger3a77be52015-05-20 14:27:27 -050021static int dm_test_eth(struct unit_test_state *uts)
Joe Hershberger4c197242015-03-22 17:09:15 -050022{
Joe Hershberger5874dec2015-04-08 01:41:01 -050023 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershberger4c197242015-03-22 17:09:15 -050024
25 setenv("ethact", "eth@10002000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050026 ut_assertok(net_loop(PING));
Joe Hershberger4c197242015-03-22 17:09:15 -050027 ut_asserteq_str("eth@10002000", getenv("ethact"));
28
29 setenv("ethact", "eth@10003000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050030 ut_assertok(net_loop(PING));
Joe Hershberger4c197242015-03-22 17:09:15 -050031 ut_asserteq_str("eth@10003000", getenv("ethact"));
32
33 setenv("ethact", "eth@10004000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050034 ut_assertok(net_loop(PING));
Joe Hershberger4c197242015-03-22 17:09:15 -050035 ut_asserteq_str("eth@10004000", getenv("ethact"));
36
37 return 0;
38}
39DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
Joe Hershberger279d2f62015-03-22 17:09:16 -050040
Joe Hershberger3a77be52015-05-20 14:27:27 -050041static int dm_test_eth_alias(struct unit_test_state *uts)
Joe Hershberger279d2f62015-03-22 17:09:16 -050042{
Joe Hershberger5874dec2015-04-08 01:41:01 -050043 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershberger279d2f62015-03-22 17:09:16 -050044 setenv("ethact", "eth0");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050045 ut_assertok(net_loop(PING));
Joe Hershberger279d2f62015-03-22 17:09:16 -050046 ut_asserteq_str("eth@10002000", getenv("ethact"));
47
48 setenv("ethact", "eth1");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050049 ut_assertok(net_loop(PING));
Joe Hershberger279d2f62015-03-22 17:09:16 -050050 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 Hershbergerc80b41b02015-04-08 01:41:21 -050054 ut_assertok(net_loop(PING));
Joe Hershberger279d2f62015-03-22 17:09:16 -050055 ut_asserteq_str("eth@10002000", getenv("ethact"));
56
57 setenv("ethact", "eth5");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050058 ut_assertok(net_loop(PING));
Joe Hershberger279d2f62015-03-22 17:09:16 -050059 ut_asserteq_str("eth@10003000", getenv("ethact"));
60
61 return 0;
62}
63DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050064
Joe Hershberger3a77be52015-05-20 14:27:27 -050065static int dm_test_eth_prime(struct unit_test_state *uts)
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050066{
Joe Hershberger5874dec2015-04-08 01:41:01 -050067 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050068
69 /* Expected to be "eth@10003000" because of ethprime variable */
70 setenv("ethact", NULL);
71 setenv("ethprime", "eth5");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050072 ut_assertok(net_loop(PING));
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050073 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 Hershbergerc80b41b02015-04-08 01:41:21 -050078 ut_assertok(net_loop(PING));
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050079 ut_asserteq_str("eth@10002000", getenv("ethact"));
80
81 return 0;
82}
83DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
Joe Hershbergerf340e542015-03-22 17:09:18 -050084
Joe Hershberger3a77be52015-05-20 14:27:27 -050085static int dm_test_eth_rotate(struct unit_test_state *uts)
Joe Hershbergerf340e542015-03-22 17:09:18 -050086{
87 char ethaddr[18];
88
89 /* Invalidate eth1's MAC address */
Joe Hershberger5874dec2015-04-08 01:41:01 -050090 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergerf340e542015-03-22 17:09:18 -050091 strcpy(ethaddr, getenv("eth1addr"));
Joe Hershbergere84ca292015-05-20 14:27:22 -050092 /* Must disable access protection for eth1addr before clearing */
93 setenv(".flags", "eth1addr");
Joe Hershbergerf340e542015-03-22 17:09:18 -050094 setenv("eth1addr", NULL);
95
96 /* Make sure that the default is to rotate to the next interface */
97 setenv("ethact", "eth@10004000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050098 ut_assertok(net_loop(PING));
Joe Hershbergerf340e542015-03-22 17:09:18 -050099 ut_asserteq_str("eth@10002000", getenv("ethact"));
100
101 /* If ethrotate is no, then we should fail on a bad MAC */
102 setenv("ethact", "eth@10004000");
103 setenv("ethrotate", "no");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500104 ut_asserteq(-EINVAL, net_loop(PING));
Joe Hershbergerf340e542015-03-22 17:09:18 -0500105 ut_asserteq_str("eth@10004000", getenv("ethact"));
106
107 /* Restore the env */
108 setenv("eth1addr", ethaddr);
109 setenv("ethrotate", NULL);
110
111 /* Invalidate eth0's MAC address */
112 strcpy(ethaddr, getenv("ethaddr"));
Joe Hershbergere84ca292015-05-20 14:27:22 -0500113 /* Must disable access protection for ethaddr before clearing */
Joe Hershbergerf340e542015-03-22 17:09:18 -0500114 setenv(".flags", "ethaddr");
115 setenv("ethaddr", NULL);
116
117 /* Make sure we can skip invalid devices */
118 setenv("ethact", "eth@10004000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500119 ut_assertok(net_loop(PING));
Joe Hershbergerf340e542015-03-22 17:09:18 -0500120 ut_asserteq_str("eth@10004000", getenv("ethact"));
121
122 /* Restore the env */
123 setenv("ethaddr", ethaddr);
124 setenv(".flags", NULL);
125
126 return 0;
127}
128DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
Joe Hershbergerf933a932015-03-22 17:09:20 -0500129
Joe Hershberger3a77be52015-05-20 14:27:27 -0500130static int dm_test_net_retry(struct unit_test_state *uts)
Joe Hershbergerf933a932015-03-22 17:09:20 -0500131{
Joe Hershberger5874dec2015-04-08 01:41:01 -0500132 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergerf933a932015-03-22 17:09:20 -0500133
134 /*
135 * eth1 is disabled and netretry is yes, so the ping should succeed and
136 * the active device should be eth0
137 */
138 sandbox_eth_disable_response(1, true);
139 setenv("ethact", "eth@10004000");
140 setenv("netretry", "yes");
Joe Hershberger6901f432015-04-21 13:57:20 -0500141 sandbox_eth_skip_timeout();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500142 ut_assertok(net_loop(PING));
Joe Hershbergerf933a932015-03-22 17:09:20 -0500143 ut_asserteq_str("eth@10002000", getenv("ethact"));
144
145 /*
146 * eth1 is disabled and netretry is no, so the ping should fail and the
147 * active device should be eth1
148 */
149 setenv("ethact", "eth@10004000");
150 setenv("netretry", "no");
Joe Hershberger6901f432015-04-21 13:57:20 -0500151 sandbox_eth_skip_timeout();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500152 ut_asserteq(-ETIMEDOUT, net_loop(PING));
Joe Hershbergerf933a932015-03-22 17:09:20 -0500153 ut_asserteq_str("eth@10004000", getenv("ethact"));
154
155 /* Restore the env */
156 setenv("netretry", NULL);
157 sandbox_eth_disable_response(1, false);
158
159 return 0;
160}
161DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);