blob: 5437f9ea4a07492cbb79cbc1f38d7e6fca99b630 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Joe Hershbergerbfacad72015-03-22 17:09:15 -05002/*
3 * Copyright (c) 2015 National Instruments
4 *
5 * (C) Copyright 2015
6 * Joe Hershberger <joe.hershberger@ni.com>
Joe Hershbergerbfacad72015-03-22 17:09:15 -05007 */
8
9#include <common.h>
10#include <dm.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060011#include <env.h>
Joe Hershbergerbfacad72015-03-22 17:09:15 -050012#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Joe Hershbergerbfacad72015-03-22 17:09:15 -050014#include <malloc.h>
15#include <net.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060016#include <asm/eth.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050017#include <dm/test.h>
Bin Meng6d9764c2015-10-07 21:45:43 -070018#include <dm/device-internal.h>
19#include <dm/uclass-internal.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060020#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050021#include <test/ut.h>
Joe Hershbergerbfacad72015-03-22 17:09:15 -050022
Bin Meng6d9764c2015-10-07 21:45:43 -070023#define DM_TEST_ETH_NUM 4
24
Joe Hershbergere721b882015-05-20 14:27:27 -050025static int dm_test_eth(struct unit_test_state *uts)
Joe Hershbergerbfacad72015-03-22 17:09:15 -050026{
Joe Hershberger049a95a2015-04-08 01:41:01 -050027 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergerbfacad72015-03-22 17:09:15 -050028
Simon Glass382bee52017-08-03 12:22:09 -060029 env_set("ethact", "eth@10002000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050030 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060031 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050032
Simon Glass382bee52017-08-03 12:22:09 -060033 env_set("ethact", "eth@10003000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050034 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060035 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050036
Simon Glass382bee52017-08-03 12:22:09 -060037 env_set("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050038 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060039 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050040
41 return 0;
42}
Simon Glasse180c2b2020-07-28 19:41:12 -060043DM_TEST(dm_test_eth, UT_TESTF_SCAN_FDT);
Joe Hershbergere58780d2015-03-22 17:09:16 -050044
Joe Hershbergere721b882015-05-20 14:27:27 -050045static int dm_test_eth_alias(struct unit_test_state *uts)
Joe Hershbergere58780d2015-03-22 17:09:16 -050046{
Joe Hershberger049a95a2015-04-08 01:41:01 -050047 net_ping_ip = string_to_ip("1.1.2.2");
Simon Glass382bee52017-08-03 12:22:09 -060048 env_set("ethact", "eth0");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050049 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060050 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050051
Michael Wallebe1a6e92020-06-02 01:47:09 +020052 env_set("ethact", "eth6");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050053 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060054 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050055
Claudiu Manoilff98da02021-03-14 20:14:57 +080056 /* Expected to fail since eth1 is not defined in the device tree */
57 env_set("ethact", "eth1");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050058 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060059 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050060
Simon Glass382bee52017-08-03 12:22:09 -060061 env_set("ethact", "eth5");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050062 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060063 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050064
65 return 0;
66}
Simon Glasse180c2b2020-07-28 19:41:12 -060067DM_TEST(dm_test_eth_alias, UT_TESTF_SCAN_FDT);
Joe Hershberger6536b9b2015-03-22 17:09:17 -050068
Joe Hershbergere721b882015-05-20 14:27:27 -050069static int dm_test_eth_prime(struct unit_test_state *uts)
Joe Hershberger6536b9b2015-03-22 17:09:17 -050070{
Joe Hershberger049a95a2015-04-08 01:41:01 -050071 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershberger6536b9b2015-03-22 17:09:17 -050072
73 /* Expected to be "eth@10003000" because of ethprime variable */
Simon Glass382bee52017-08-03 12:22:09 -060074 env_set("ethact", NULL);
75 env_set("ethprime", "eth5");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050076 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060077 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershberger6536b9b2015-03-22 17:09:17 -050078
79 /* Expected to be "eth@10002000" because it is first */
Simon Glass382bee52017-08-03 12:22:09 -060080 env_set("ethact", NULL);
81 env_set("ethprime", NULL);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050082 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060083 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger6536b9b2015-03-22 17:09:17 -050084
85 return 0;
86}
Simon Glasse180c2b2020-07-28 19:41:12 -060087DM_TEST(dm_test_eth_prime, UT_TESTF_SCAN_FDT);
Joe Hershberger7d104ea2015-03-22 17:09:18 -050088
Bin Meng6d9764c2015-10-07 21:45:43 -070089/**
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 */
103static 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 Wallebe1a6e92020-06-02 01:47:09 +0200109 "eth3addr", "eth6addr"};
Bin Meng6d9764c2015-10-07 21:45:43 -0700110 char ethaddr[DM_TEST_ETH_NUM][18];
111 int i;
112
Tom Rinie2e6dae2017-09-26 14:08:30 -0400113 memset(ethaddr, '\0', sizeof(ethaddr));
Bin Meng6d9764c2015-10-07 21:45:43 -0700114 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 Roese706865a2017-03-20 12:51:48 +0100120 ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL));
Bin Meng6d9764c2015-10-07 21:45:43 -0700121
122 /* Invalidate MAC address */
Tom Rinie2e6dae2017-09-26 14:08:30 -0400123 strncpy(ethaddr[i], env_get(addrname[i]), 17);
Bin Meng6d9764c2015-10-07 21:45:43 -0700124 /* Must disable access protection for ethaddr before clearing */
Simon Glass382bee52017-08-03 12:22:09 -0600125 env_set(".flags", addrname[i]);
126 env_set(addrname[i], NULL);
Bin Meng6d9764c2015-10-07 21:45:43 -0700127 }
128
129 /* Set ethact to "eth@10002000" */
Simon Glass382bee52017-08-03 12:22:09 -0600130 env_set("ethact", ethname[0]);
Bin Meng6d9764c2015-10-07 21:45:43 -0700131
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 Glass382bee52017-08-03 12:22:09 -0600137 env_set(".flags", addrname[i]);
138 env_set(addrname[i], ethaddr[i]);
Bin Meng6d9764c2015-10-07 21:45:43 -0700139
140 /* Probe the device again */
141 ut_assertok(device_probe(dev[i]));
142 }
Simon Glass382bee52017-08-03 12:22:09 -0600143 env_set(".flags", NULL);
144 env_set("ethact", NULL);
Bin Meng6d9764c2015-10-07 21:45:43 -0700145
146 return 0;
147}
Simon Glasse180c2b2020-07-28 19:41:12 -0600148DM_TEST(dm_test_eth_act, UT_TESTF_SCAN_FDT);
Bin Meng6d9764c2015-10-07 21:45:43 -0700149
Sean Andersondf33fd22022-05-05 13:11:32 -0400150/* Ensure that all addresses are loaded properly */
151static 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}
177DM_TEST(dm_test_ethaddr, UT_TESTF_SCAN_FDT);
178
Joe Hershberger09129be2015-05-20 14:27:33 -0500179/* The asserts include a return on fail; cleanup in the caller */
180static int _dm_test_eth_rotate1(struct unit_test_state *uts)
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500181{
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500182 /* Make sure that the default is to rotate to the next interface */
Simon Glass382bee52017-08-03 12:22:09 -0600183 env_set("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500184 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600185 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500186
187 /* If ethrotate is no, then we should fail on a bad MAC */
Simon Glass382bee52017-08-03 12:22:09 -0600188 env_set("ethact", "eth@10004000");
189 env_set("ethrotate", "no");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500190 ut_asserteq(-EINVAL, net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600191 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500192
Joe Hershberger09129be2015-05-20 14:27:33 -0500193 return 0;
194}
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500195
Joe Hershberger09129be2015-05-20 14:27:33 -0500196static int _dm_test_eth_rotate2(struct unit_test_state *uts)
197{
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500198 /* Make sure we can skip invalid devices */
Simon Glass382bee52017-08-03 12:22:09 -0600199 env_set("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500200 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600201 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500202
Bin Meng71d79712015-08-27 22:25:53 -0700203 /* Make sure we can handle device name which is not eth# */
Simon Glass382bee52017-08-03 12:22:09 -0600204 env_set("ethact", "sbe5");
Bin Meng71d79712015-08-27 22:25:53 -0700205 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600206 ut_asserteq_str("sbe5", env_get("ethact"));
Bin Meng71d79712015-08-27 22:25:53 -0700207
Joe Hershberger09129be2015-05-20 14:27:33 -0500208 return 0;
209}
210
211static 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 Rinie2e6dae2017-09-26 14:08:30 -0400220 memset(ethaddr, '\0', sizeof(ethaddr));
Michael Wallebe1a6e92020-06-02 01:47:09 +0200221 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 Hershberger09129be2015-05-20 14:27:33 -0500225
226 retval = _dm_test_eth_rotate1(uts);
227
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500228 /* Restore the env */
Michael Wallebe1a6e92020-06-02 01:47:09 +0200229 env_set("eth6addr", ethaddr);
Simon Glass382bee52017-08-03 12:22:09 -0600230 env_set("ethrotate", NULL);
Joe Hershberger09129be2015-05-20 14:27:33 -0500231
232 if (!retval) {
233 /* Invalidate eth0's MAC address */
Tom Rinie2e6dae2017-09-26 14:08:30 -0400234 strncpy(ethaddr, env_get("ethaddr"), 17);
Joe Hershberger09129be2015-05-20 14:27:33 -0500235 /* Must disable access protection for ethaddr before clearing */
Simon Glass382bee52017-08-03 12:22:09 -0600236 env_set(".flags", "ethaddr");
237 env_set("ethaddr", NULL);
Joe Hershberger09129be2015-05-20 14:27:33 -0500238
239 retval = _dm_test_eth_rotate2(uts);
240
241 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600242 env_set("ethaddr", ethaddr);
Joe Hershberger09129be2015-05-20 14:27:33 -0500243 }
244 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600245 env_set(".flags", NULL);
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500246
Joe Hershberger09129be2015-05-20 14:27:33 -0500247 return retval;
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500248}
Simon Glasse180c2b2020-07-28 19:41:12 -0600249DM_TEST(dm_test_eth_rotate, UT_TESTF_SCAN_FDT);
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500250
Joe Hershberger09129be2015-05-20 14:27:33 -0500251/* The asserts include a return on fail; cleanup in the caller */
252static int _dm_test_net_retry(struct unit_test_state *uts)
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500253{
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500254 /*
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 Manoilff98da02021-03-14 20:14:57 +0800259 env_set("ethact", "lan1");
Simon Glass382bee52017-08-03 12:22:09 -0600260 env_set("netretry", "yes");
Joe Hershberger172a31b2015-04-21 13:57:20 -0500261 sandbox_eth_skip_timeout();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500262 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600263 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500264
265 /*
266 * eth1 is disabled and netretry is no, so the ping should fail and the
267 * active device should be eth1
268 */
Claudiu Manoilff98da02021-03-14 20:14:57 +0800269 env_set("ethact", "lan1");
Simon Glass382bee52017-08-03 12:22:09 -0600270 env_set("netretry", "no");
Joe Hershberger172a31b2015-04-21 13:57:20 -0500271 sandbox_eth_skip_timeout();
Thomas RIENOESSLa735e6e2018-11-21 15:56:07 +0100272 ut_asserteq(-ENONET, net_loop(PING));
Claudiu Manoilff98da02021-03-14 20:14:57 +0800273 ut_asserteq_str("lan1", env_get("ethact"));
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500274
Joe Hershberger09129be2015-05-20 14:27:33 -0500275 return 0;
276}
277
278static 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 Hershberger7ece1c62015-03-22 17:09:20 -0500286 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600287 env_set("netretry", NULL);
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500288 sandbox_eth_disable_response(1, false);
289
Joe Hershberger09129be2015-05-20 14:27:33 -0500290 return retval;
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500291}
Simon Glasse180c2b2020-07-28 19:41:12 -0600292DM_TEST(dm_test_net_retry, UT_TESTF_SCAN_FDT);
Joe Hershberger45988da2018-09-26 16:49:00 -0500293
294static 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 Glassf91f3662020-05-10 12:52:45 -0600315 ut_asserteq_mem(eth->et_src, net_ethaddr, ARP_HLEN);
316 ut_asserteq_mem(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN);
Joe Hershberger45988da2018-09-26 16:49:00 -0500317 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 Glassf91f3662020-05-10 12:52:45 -0600323 ut_asserteq_mem(&arp->ar_sha, net_ethaddr, ARP_HLEN);
Joe Hershberger45988da2018-09-26 16:49:00 -0500324 ut_assert(net_read_ip(&arp->ar_spa).s_addr == net_ip.s_addr);
Simon Glassf91f3662020-05-10 12:52:45 -0600325 ut_asserteq_mem(&arp->ar_tha, priv->fake_host_hwaddr, ARP_HLEN);
Joe Hershberger45988da2018-09-26 16:49:00 -0500326 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
332static 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
360static 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 Hershberger45988da2018-09-26 16:49:00 -0500367
368 env_set("ethact", "eth@10002000");
Joe Hershbergerac3f26c2018-09-26 16:49:02 -0500369 ut_assertok(net_loop(PING));
Joe Hershberger45988da2018-09-26 16:49:00 -0500370 ut_asserteq_str("eth@10002000", env_get("ethact"));
371
372 sandbox_eth_set_tx_handler(0, NULL);
373
374 return 0;
375}
376
Simon Glasse180c2b2020-07-28 19:41:12 -0600377DM_TEST(dm_test_eth_async_arp_reply, UT_TESTF_SCAN_FDT);
Joe Hershberger72ff0042018-09-26 16:49:01 -0500378
379static 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 Glassf91f3662020-05-10 12:52:45 -0600406 ut_asserteq_mem(eth->et_src, net_ethaddr, ARP_HLEN);
407 ut_asserteq_mem(eth->et_dest, priv->fake_host_hwaddr, ARP_HLEN);
Joe Hershberger72ff0042018-09-26 16:49:01 -0500408 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
417static 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
445static 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 Hershberger72ff0042018-09-26 16:49:01 -0500452
453 env_set("ethact", "eth@10002000");
Joe Hershbergerac3f26c2018-09-26 16:49:02 -0500454 ut_assertok(net_loop(PING));
Joe Hershberger72ff0042018-09-26 16:49:01 -0500455 ut_asserteq_str("eth@10002000", env_get("ethact"));
456
457 sandbox_eth_set_tx_handler(0, NULL);
458
459 return 0;
460}
461
Simon Glasse180c2b2020-07-28 19:41:12 -0600462DM_TEST(dm_test_eth_async_ping_reply, UT_TESTF_SCAN_FDT);