1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 National Instruments
6 * Joe Hershberger <joe.hershberger@ni.com>
16 DECLARE_GLOBAL_DATA_PTR;
18 static bool skip_timeout;
21 * sandbox_eth_disable_response()
23 * index - The alias index (also DM seq number)
24 * disable - If non-zero, ignore sent packets and don't send mock response
26 void sandbox_eth_disable_response(int index, bool disable)
29 struct eth_sandbox_priv *priv;
32 ret = uclass_get_device(UCLASS_ETH, index, &dev);
36 priv = dev_get_priv(dev);
37 priv->disabled = disable;
41 * sandbox_eth_skip_timeout()
43 * When the first packet read is attempted, fast-forward time
45 void sandbox_eth_skip_timeout(void)
51 * sandbox_eth_arp_req_to_reply()
53 * Check for an arp request to be sent. If so, inject a reply
55 * returns 0 if injected, -EAGAIN if not
57 int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
60 struct eth_sandbox_priv *priv = dev_get_priv(dev);
61 struct ethernet_hdr *eth = packet;
63 struct ethernet_hdr *eth_recv;
64 struct arp_hdr *arp_recv;
66 if (ntohs(eth->et_protlen) != PROT_ARP)
69 arp = packet + ETHER_HDR_SIZE;
71 if (ntohs(arp->ar_op) != ARPOP_REQUEST)
74 /* Don't allow the buffer to overrun */
75 if (priv->recv_packets >= PKTBUFSRX)
78 /* store this as the assumed IP of the fake host */
79 priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
81 /* Formulate a fake response */
82 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
83 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
84 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
85 eth_recv->et_protlen = htons(PROT_ARP);
87 arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
88 arp_recv->ar_hrd = htons(ARP_ETHER);
89 arp_recv->ar_pro = htons(PROT_IP);
90 arp_recv->ar_hln = ARP_HLEN;
91 arp_recv->ar_pln = ARP_PLEN;
92 arp_recv->ar_op = htons(ARPOP_REPLY);
93 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
94 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
95 memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
96 net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
98 priv->recv_packet_length[priv->recv_packets] =
99 ETHER_HDR_SIZE + ARP_HDR_SIZE;
100 ++priv->recv_packets;
106 * sandbox_eth_ping_req_to_reply()
108 * Check for a ping request to be sent. If so, inject a reply
110 * returns 0 if injected, -EAGAIN if not
112 int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
115 struct eth_sandbox_priv *priv = dev_get_priv(dev);
116 struct ethernet_hdr *eth = packet;
117 struct ip_udp_hdr *ip;
118 struct icmp_hdr *icmp;
119 struct ethernet_hdr *eth_recv;
120 struct ip_udp_hdr *ipr;
121 struct icmp_hdr *icmpr;
123 if (ntohs(eth->et_protlen) != PROT_IP)
126 ip = packet + ETHER_HDR_SIZE;
128 if (ip->ip_p != IPPROTO_ICMP)
131 icmp = (struct icmp_hdr *)&ip->udp_src;
133 if (icmp->type != ICMP_ECHO_REQUEST)
136 /* Don't allow the buffer to overrun */
137 if (priv->recv_packets >= PKTBUFSRX)
140 /* reply to the ping */
141 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
142 memcpy(eth_recv, packet, len);
143 ipr = (void *)eth_recv + ETHER_HDR_SIZE;
144 icmpr = (struct icmp_hdr *)&ipr->udp_src;
145 memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
146 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
149 net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
150 net_write_ip((void *)&ipr->ip_src, priv->fake_host_ipaddr);
151 ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
153 icmpr->type = ICMP_ECHO_REPLY;
155 icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
157 priv->recv_packet_length[priv->recv_packets] = len;
158 ++priv->recv_packets;
164 * sandbox_eth_recv_arp_req()
166 * Inject an ARP request for this target
168 * returns 0 if injected, -EOVERFLOW if not
170 int sandbox_eth_recv_arp_req(struct udevice *dev)
172 struct eth_sandbox_priv *priv = dev_get_priv(dev);
173 struct ethernet_hdr *eth_recv;
174 struct arp_hdr *arp_recv;
176 /* Don't allow the buffer to overrun */
177 if (priv->recv_packets >= PKTBUFSRX)
180 /* Formulate a fake request */
181 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
182 memcpy(eth_recv->et_dest, net_bcast_ethaddr, ARP_HLEN);
183 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
184 eth_recv->et_protlen = htons(PROT_ARP);
186 arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
187 arp_recv->ar_hrd = htons(ARP_ETHER);
188 arp_recv->ar_pro = htons(PROT_IP);
189 arp_recv->ar_hln = ARP_HLEN;
190 arp_recv->ar_pln = ARP_PLEN;
191 arp_recv->ar_op = htons(ARPOP_REQUEST);
192 memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
193 net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
194 memcpy(&arp_recv->ar_tha, net_null_ethaddr, ARP_HLEN);
195 net_write_ip(&arp_recv->ar_tpa, net_ip);
197 priv->recv_packet_length[priv->recv_packets] =
198 ETHER_HDR_SIZE + ARP_HDR_SIZE;
199 ++priv->recv_packets;
205 * sandbox_eth_recv_ping_req()
207 * Inject a ping request for this target
209 * returns 0 if injected, -EOVERFLOW if not
211 int sandbox_eth_recv_ping_req(struct udevice *dev)
213 struct eth_sandbox_priv *priv = dev_get_priv(dev);
214 struct ethernet_hdr *eth_recv;
215 struct ip_udp_hdr *ipr;
216 struct icmp_hdr *icmpr;
218 /* Don't allow the buffer to overrun */
219 if (priv->recv_packets >= PKTBUFSRX)
222 /* Formulate a fake ping */
223 eth_recv = (void *)priv->recv_packet_buffer[priv->recv_packets];
225 memcpy(eth_recv->et_dest, net_ethaddr, ARP_HLEN);
226 memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
227 eth_recv->et_protlen = htons(PROT_IP);
229 ipr = (void *)eth_recv + ETHER_HDR_SIZE;
231 ipr->ip_len = htons(IP_ICMP_HDR_SIZE);
232 ipr->ip_off = htons(IP_FLAGS_DFRAG);
233 ipr->ip_p = IPPROTO_ICMP;
235 net_write_ip(&ipr->ip_src, priv->fake_host_ipaddr);
236 net_write_ip(&ipr->ip_dst, net_ip);
237 ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
239 icmpr = (struct icmp_hdr *)&ipr->udp_src;
241 icmpr->type = ICMP_ECHO_REQUEST;
244 icmpr->un.echo.id = 0;
245 icmpr->un.echo.sequence = htons(1);
246 icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
248 priv->recv_packet_length[priv->recv_packets] =
249 ETHER_HDR_SIZE + IP_ICMP_HDR_SIZE;
250 ++priv->recv_packets;
256 * sb_default_handler()
258 * perform typical responses to simple ping
260 * dev - device pointer
261 * pkt - "sent" packet buffer
262 * len - length of packet
264 static int sb_default_handler(struct udevice *dev, void *packet,
267 if (!sandbox_eth_arp_req_to_reply(dev, packet, len))
269 if (!sandbox_eth_ping_req_to_reply(dev, packet, len))
276 * sandbox_eth_set_tx_handler()
278 * Set a custom response to a packet being sent through the sandbox eth test
281 * index - interface to set the handler for
282 * handler - The func ptr to call on send. If NULL, set to default handler
284 void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler)
287 struct eth_sandbox_priv *priv;
290 ret = uclass_get_device(UCLASS_ETH, index, &dev);
294 priv = dev_get_priv(dev);
296 priv->tx_handler = handler;
298 priv->tx_handler = sb_default_handler;
304 * priv - priv void ptr to store in the device
306 void sandbox_eth_set_priv(int index, void *priv)
309 struct eth_sandbox_priv *dev_priv;
312 ret = uclass_get_device(UCLASS_ETH, index, &dev);
316 dev_priv = dev_get_priv(dev);
318 dev_priv->priv = priv;
321 static int sb_eth_start(struct udevice *dev)
323 struct eth_sandbox_priv *priv = dev_get_priv(dev);
325 debug("eth_sandbox: Start\n");
327 priv->recv_packets = 0;
328 for (int i = 0; i < PKTBUFSRX; i++) {
329 priv->recv_packet_buffer[i] = net_rx_packets[i];
330 priv->recv_packet_length[i] = 0;
336 static int sb_eth_send(struct udevice *dev, void *packet, int length)
338 struct eth_sandbox_priv *priv = dev_get_priv(dev);
340 debug("eth_sandbox: Send packet %d\n", length);
345 return priv->tx_handler(dev, packet, length);
348 static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
350 struct eth_sandbox_priv *priv = dev_get_priv(dev);
353 sandbox_timer_add_offset(11000UL);
354 skip_timeout = false;
357 if (priv->recv_packets) {
358 int lcl_recv_packet_length = priv->recv_packet_length[0];
360 debug("eth_sandbox: received packet[%d], %d waiting\n",
361 lcl_recv_packet_length, priv->recv_packets - 1);
362 *packetp = priv->recv_packet_buffer[0];
363 return lcl_recv_packet_length;
368 static int sb_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
370 struct eth_sandbox_priv *priv = dev_get_priv(dev);
373 if (!priv->recv_packets)
376 --priv->recv_packets;
377 for (i = 0; i < priv->recv_packets; i++) {
378 priv->recv_packet_length[i] = priv->recv_packet_length[i + 1];
379 memcpy(priv->recv_packet_buffer[i],
380 priv->recv_packet_buffer[i + 1],
381 priv->recv_packet_length[i + 1]);
383 priv->recv_packet_length[priv->recv_packets] = 0;
388 static void sb_eth_stop(struct udevice *dev)
390 debug("eth_sandbox: Stop\n");
393 static int sb_eth_write_hwaddr(struct udevice *dev)
395 struct eth_pdata *pdata = dev_get_platdata(dev);
397 debug("eth_sandbox %s: Write HW ADDR - %pM\n", dev->name,
402 static const struct eth_ops sb_eth_ops = {
403 .start = sb_eth_start,
406 .free_pkt = sb_eth_free_pkt,
408 .write_hwaddr = sb_eth_write_hwaddr,
411 static int sb_eth_remove(struct udevice *dev)
416 static int sb_eth_ofdata_to_platdata(struct udevice *dev)
418 struct eth_pdata *pdata = dev_get_platdata(dev);
419 struct eth_sandbox_priv *priv = dev_get_priv(dev);
422 pdata->iobase = dev_read_addr(dev);
424 mac = dev_read_u8_array_ptr(dev, "fake-host-hwaddr", ARP_HLEN);
426 printf("'fake-host-hwaddr' is missing from the DT\n");
429 memcpy(priv->fake_host_hwaddr, mac, ARP_HLEN);
430 priv->disabled = false;
431 priv->tx_handler = sb_default_handler;
436 static const struct udevice_id sb_eth_ids[] = {
437 { .compatible = "sandbox,eth" },
441 U_BOOT_DRIVER(eth_sandbox) = {
442 .name = "eth_sandbox",
444 .of_match = sb_eth_ids,
445 .ofdata_to_platdata = sb_eth_ofdata_to_platdata,
446 .remove = sb_eth_remove,
448 .priv_auto_alloc_size = sizeof(struct eth_sandbox_priv),
449 .platdata_auto_alloc_size = sizeof(struct eth_pdata),