Skip to contentSkip to conversation

net: Fix packet corruption issue when handling asynch replies

PATCH In review Joe Hershberger 2 participants 24 messages Sep 26, 2018 – Oct 11, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover net: Fix packet corruption issue when handling asynch replies

The issue [1] was reported by (Peter) Tran Tien Dat. Unfortunately his fix for the issue broke notmal operation and I don't feel is a good way to address the issue. Also, the situation was not covered in the unit tests, so we'll add them now.

First we refactor the unit test capability of the sandbox ethernet fake driver so that we can exercise that part of the network stack, then add the tests where we prove that the async replies work, but that in the process, the action expected by the user (ping in this case) is broken.

Lastly, we correct the problem and change the unit tests to also expect success of the user's operation.

[1] https://patchwork.ozlabs.org/patch/939617/

Changes in v2:
- Added parameter comments
- Changed return value to use typical error approach
- In test, stop calling reply functions when one matches
- add comments as to the use of the uts variable
- add missing commit message
- check the return of the injection handler and pass on overflow error
- rename local "uc_priv" variable to "dev_priv" to not be misleading.
- return an error instead of 0 / 1
- return bool instead of int

Joe Hershberger (10):
net: sandbox: Move disabled flag into priv struct
net: sandbox: Refactor sandbox send function
net: sandbox: Make the fake eth driver response configurable
net: sandbox: Share the priv structure with tests
net: sandbox: Allow fake eth to handle more than 1 packet response
net: Add an accessor to know if waiting for ARP
net: sandbox: Add a priv ptr for tests to use
test: eth: Add a test for ARP requests
test: eth: Add a test for the target being pinged
net: Don't overwrite waiting packets with asynchronous replies

 arch/sandbox/include/asm/eth.h |  93 +++++++++
 drivers/net/sandbox.c          | 417 +++++++++++++++++++++++++++++++----------
 include/net.h                  |   9 +
 net/arp.c                      |  20 +-
 net/arp.h                      |   1 +
 net/net.c                      |   8 +
 net/ping.c                     |   7 +-
 test/dm/eth.c                  | 170 +++++++++++++++++
 8 files changed, 616 insertions(+), 109 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH v2 1/10 net: sandbox: Move disabled flag into priv struct

Store the per-device data with the device.

---

Changes in v2: None

 drivers/net/sandbox.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/net/sandbox.c +14 -4
index b71c8f88d9..60fe065ee5 100644
@@ -19,17 +19,18 @@ DECLARE_GLOBAL_DATA_PTR;
  *
  * fake_host_hwaddr: MAC address of mocked machine
  * fake_host_ipaddr: IP address of mocked machine
+ * disabled: Will not respond
  * recv_packet_buffer: buffer of the packet returned as received
  * recv_packet_length: length of the packet returned as received
  */
 struct eth_sandbox_priv {
 	uchar fake_host_hwaddr[ARP_HLEN];
 	struct in_addr fake_host_ipaddr;
+	bool disabled;
 	uchar *recv_packet_buffer;
 	int recv_packet_length;
 };
 
-static bool disabled[8] = {false};
 static bool skip_timeout;
 
 /*
@@ -40,7 +41,16 @@ static bool skip_timeout;
  */
 void sandbox_eth_disable_response(int index, bool disable)
 {
-	disabled[index] = disable;
+	struct udevice *dev;
+	struct eth_sandbox_priv *priv;
+	int ret;
+
+	ret = uclass_get_device(UCLASS_ETH, index, &dev);
+	if (ret)
+		return;
+
+	priv = dev_get_priv(dev);
+	priv->disabled = disable;
 }
 
 /*
@@ -71,8 +81,7 @@ static int sb_eth_send(struct udevice *dev, void *packet, int length)
 
 	debug("eth_sandbox: Send packet %d\n", length);
 
-	if (dev->seq >= 0 && dev->seq < ARRAY_SIZE(disabled) &&
-	    disabled[dev->seq])
+	if (priv->disabled)
 		return 0;
 
 	if (ntohs(eth->et_protlen) == PROT_ARP) {
@@ -212,6 +221,7 @@ static int sb_eth_ofdata_to_platdata(struct udevice *dev)
 		return -EINVAL;
 	}
 	memcpy(priv->fake_host_hwaddr, mac, ARP_HLEN);
+	priv->disabled = false;
 
 	return 0;
 }
Signature · 1 line
2.11.0
#3 lore raw

PATCH v2 2/10 net: sandbox: Refactor sandbox send function

In reply to Joe Hershberger

Make the behavior of the send function reusable.

---

Changes in v2:
- Added parameter comments
- Changed return value to use typical error approach
- In test, stop calling reply functions when one matches

 arch/sandbox/include/asm/eth.h |  26 ++++++
 drivers/net/sandbox.c          | 180 ++++++++++++++++++++++++-----------------
 2 files changed, 132 insertions(+), 74 deletions(-)
arch/sandbox/include/asm/eth.h +26 -0
index bfcd11b593..3dc84f0e4b 100644
@@ -13,4 +13,30 @@ void sandbox_eth_disable_response(int index, bool disable);
 
 void sandbox_eth_skip_timeout(void);
 
+/*
+ * sandbox_eth_arp_req_to_reply()
+ *
+ * Check for an arp request to be sent. If so, inject a reply
+ *
+ * @dev: device that received the packet
+ * @packet: pointer to the received pacaket buffer
+ * @len: length of received packet
+ * @return 0 if injected, -EAGAIN if not
+ */
+int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
+				 unsigned int len);
+
+/*
+ * sandbox_eth_ping_req_to_reply()
+ *
+ * Check for a ping request to be sent. If so, inject a reply
+ *
+ * @dev: device that received the packet
+ * @packet: pointer to the received pacaket buffer
+ * @len: length of received packet
+ * @return 0 if injected, -EAGAIN if not
+ */
+int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
+				  unsigned int len);
+
 #endif /* __ETH_H */
drivers/net/sandbox.c +106 -74

198 lines not on this page · raw message

Signature · 1 line
2.11.0
#4 lore raw

PATCH v2 2/10 net: sandbox: Refactor sandbox send function

Hi Joe,

Quoted 7 lines · Joe Hershberger

Make the behavior of the send function reusable.

---

Quoted 9 lines

Changes in v2:
- Added parameter comments
- Changed return value to use typical error approach
- In test, stop calling reply functions when one matches

 arch/sandbox/include/asm/eth.h |  26 ++++++
 drivers/net/sandbox.c          | 180 ++++++++++++++++++++++++-----------------
 2 files changed, 132 insertions(+), 74 deletions(-)

But please see comments below.

Quoted 226 lines
arch/sandbox/include/asm/eth.h +26 -0
index bfcd11b593..3dc84f0e4b 100644
@@ -13,4 +13,30 @@ void sandbox_eth_disable_response(int index, bool disable);
 
 void sandbox_eth_skip_timeout(void);
 
+/*
+ * sandbox_eth_arp_req_to_reply()
+ *
+ * Check for an arp request to be sent. If so, inject a reply
+ *
+ * @dev: device that received the packet
+ * @packet: pointer to the received pacaket buffer
+ * @len: length of received packet
+ * @return 0 if injected, -EAGAIN if not
+ */
+int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
+                                unsigned int len);
+
+/*
+ * sandbox_eth_ping_req_to_reply()
+ *
+ * Check for a ping request to be sent. If so, inject a reply
+ *
+ * @dev: device that received the packet
+ * @packet: pointer to the received pacaket buffer
+ * @len: length of received packet
+ * @return 0 if injected, -EAGAIN if not
+ */
+int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
+                                 unsigned int len);
+
 #endif /* __ETH_H */
drivers/net/sandbox.c +102 -68
index 60fe065ee5..c472261568 100644
@@ -63,6 +63,108 @@ void sandbox_eth_skip_timeout(void)
        skip_timeout = true;
 }
 
+/*
+ * sandbox_eth_arp_req_to_reply()
+ *
+ * Check for an arp request to be sent. If so, inject a reply
+ *
+ * returns 0 if injected, -EAGAIN if not
+ */
+int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
+                                unsigned int len)
+{
+       struct eth_sandbox_priv *priv = dev_get_priv(dev);
+       struct ethernet_hdr *eth = packet;
+       struct arp_hdr *arp;
+       struct ethernet_hdr *eth_recv;
+       struct arp_hdr *arp_recv;
+
+       if (ntohs(eth->et_protlen) != PROT_ARP)
+               return -EAGAIN;
+
+       arp = packet + ETHER_HDR_SIZE;
+
+       if (ntohs(arp->ar_op) != ARPOP_REQUEST)
+               return -EAGAIN;
+
+       /* store this as the assumed IP of the fake host */
+       priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
+
+       /* Formulate a fake response */
+       eth_recv = (void *)priv->recv_packet_buffer;
+       memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
+       memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
+       eth_recv->et_protlen = htons(PROT_ARP);
+
+       arp_recv = (void *)eth_recv + ETHER_HDR_SIZE;
+       arp_recv->ar_hrd = htons(ARP_ETHER);
+       arp_recv->ar_pro = htons(PROT_IP);
+       arp_recv->ar_hln = ARP_HLEN;
+       arp_recv->ar_pln = ARP_PLEN;
+       arp_recv->ar_op = htons(ARPOP_REPLY);
+       memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr, ARP_HLEN);
+       net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
+       memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
+       net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
+
+       priv->recv_packet_length = ETHER_HDR_SIZE + ARP_HDR_SIZE;
+
+       return 0;
+}
+
+/*
+ * sandbox_eth_ping_req_to_reply()
+ *
+ * Check for a ping request to be sent. If so, inject a reply
+ *
+ * returns 0 if injected, -EAGAIN if not
+ */
+int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
+                                 unsigned int len)
+{
+       struct eth_sandbox_priv *priv = dev_get_priv(dev);
+       struct ethernet_hdr *eth = packet;
+       struct ip_udp_hdr *ip;
+       struct icmp_hdr *icmp;
+       struct ethernet_hdr *eth_recv;
+       struct ip_udp_hdr *ipr;
+       struct icmp_hdr *icmpr;
+
+       if (ntohs(eth->et_protlen) != PROT_IP)
+               return -EAGAIN;
+
+       ip = packet + ETHER_HDR_SIZE;
+
+       if (ip->ip_p != IPPROTO_ICMP)
+               return -EAGAIN;
+
+       icmp = (struct icmp_hdr *)&ip->udp_src;
+
+       if (icmp->type != ICMP_ECHO_REQUEST)
+               return -EAGAIN;
+
+       /* reply to the ping */
+       eth_recv = (void *)priv->recv_packet_buffer;
+       memcpy(eth_recv, packet, len);
+       ipr = (void *)eth_recv + ETHER_HDR_SIZE;
+       icmpr = (struct icmp_hdr *)&ipr->udp_src;
+       memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
+       memcpy(eth_recv->et_src, priv->fake_host_hwaddr, ARP_HLEN);
+       ipr->ip_sum = 0;
+       ipr->ip_off = 0;
+       net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
+       net_write_ip((void *)&ipr->ip_src, priv->fake_host_ipaddr);
+       ipr->ip_sum = compute_ip_checksum(ipr, IP_HDR_SIZE);
+
+       icmpr->type = ICMP_ECHO_REPLY;
+       icmpr->checksum = 0;
+       icmpr->checksum = compute_ip_checksum(icmpr, ICMP_HDR_SIZE);
+
+       priv->recv_packet_length = len;
+
+       return 0;
+}
+
 static int sb_eth_start(struct udevice *dev)
 {
        struct eth_sandbox_priv *priv = dev_get_priv(dev);
@@ -77,86 +179,16 @@ static int sb_eth_start(struct udevice *dev)
 static int sb_eth_send(struct udevice *dev, void *packet, int length)
 {
        struct eth_sandbox_priv *priv = dev_get_priv(dev);
-       struct ethernet_hdr *eth = packet;
 
        debug("eth_sandbox: Send packet %d\n", length);
 
        if (priv->disabled)
                return 0;
 
-       if (ntohs(eth->et_protlen) == PROT_ARP) {
-               struct arp_hdr *arp = packet + ETHER_HDR_SIZE;
-
-               if (ntohs(arp->ar_op) == ARPOP_REQUEST) {
-                       struct ethernet_hdr *eth_recv;
-                       struct arp_hdr *arp_recv;
-
-                       /* store this as the assumed IP of the fake host */
-                       priv->fake_host_ipaddr = net_read_ip(&arp->ar_tpa);
-                       /* Formulate a fake response */
-                       eth_recv = (void *)priv->recv_packet_buffer;
-                       memcpy(eth_recv->et_dest, eth->et_src, ARP_HLEN);
-                       memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
-                              ARP_HLEN);
-                       eth_recv->et_protlen = htons(PROT_ARP);
-
-                       arp_recv = (void *)priv->recv_packet_buffer +
-                               ETHER_HDR_SIZE;
-                       arp_recv->ar_hrd = htons(ARP_ETHER);
-                       arp_recv->ar_pro = htons(PROT_IP);
-                       arp_recv->ar_hln = ARP_HLEN;
-                       arp_recv->ar_pln = ARP_PLEN;
-                       arp_recv->ar_op = htons(ARPOP_REPLY);
-                       memcpy(&arp_recv->ar_sha, priv->fake_host_hwaddr,
-                              ARP_HLEN);
-                       net_write_ip(&arp_recv->ar_spa, priv->fake_host_ipaddr);
-                       memcpy(&arp_recv->ar_tha, &arp->ar_sha, ARP_HLEN);
-                       net_copy_ip(&arp_recv->ar_tpa, &arp->ar_spa);
-
-                       priv->recv_packet_length = ETHER_HDR_SIZE +
-                               ARP_HDR_SIZE;
-               }
-       } else if (ntohs(eth->et_protlen) == PROT_IP) {
-               struct ip_udp_hdr *ip = packet + ETHER_HDR_SIZE;
-
-               if (ip->ip_p == IPPROTO_ICMP) {
-                       struct icmp_hdr *icmp = (struct icmp_hdr *)&ip->udp_src;
-
-                       if (icmp->type == ICMP_ECHO_REQUEST) {
-                               struct ethernet_hdr *eth_recv;
-                               struct ip_udp_hdr *ipr;
-                               struct icmp_hdr *icmpr;
-
-                               /* reply to the ping */
-                               memcpy(priv->recv_packet_buffer, packet,
-                                      length);
-                               eth_recv = (void *)priv->recv_packet_buffer;
-                               ipr = (void *)priv->recv_packet_buffer +
-                                       ETHER_HDR_SIZE;
-                               icmpr = (struct icmp_hdr *)&ipr->udp_src;
-                               memcpy(eth_recv->et_dest, eth->et_src,
-                                      ARP_HLEN);
-                               memcpy(eth_recv->et_src, priv->fake_host_hwaddr,
-                                      ARP_HLEN);
-                               ipr->ip_sum = 0;
-                               ipr->ip_off = 0;
-                               net_copy_ip((void *)&ipr->ip_dst, &ip->ip_src);
-                               net_write_ip((void *)&ipr->ip_src,
-                                            priv->fake_host_ipaddr);
-                               ipr->ip_sum = compute_ip_checksum(ipr,
-                                       IP_HDR_SIZE);
-
-                               icmpr->type = ICMP_ECHO_REPLY;
-                               icmpr->checksum = 0;
-                               icmpr->checksum = compute_ip_checksum(icmpr,
-                                       ICMP_HDR_SIZE);
-

drivers/net/sandbox.c static int sb_eth_start(struct udevice *dev) · line 191

Quoted 10 lines
drivers/net/sandbox.c +4 -6
-                               priv->recv_packet_length = length;
-                       }
-               }
-       }
-
-       return 0;
+       if (!sandbox_eth_arp_req_to_reply(dev, packet, length))
+               return 0;
+       if (!sandbox_eth_ping_req_to_reply(dev, packet, length))
+               return 0;

I think you will get a warning here: no return value specified ..

drivers/net/sandbox.c static int sb_eth_start(struct udevice *dev)

Quoted 4 lines
drivers/net/sandbox.c +0 -1
 }
 
 static int sb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
--

Regards,
Bin

#6 lore raw

PATCH v2 3/10 net: sandbox: Make the fake eth driver response configurable

In reply to Joe Hershberger

Make the send handler registerable so tests can check for different things.

---

Changes in v2: None

 arch/sandbox/include/asm/eth.h | 17 +++++++++++++
 drivers/net/sandbox.c          | 55 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 68 insertions(+), 4 deletions(-)
arch/sandbox/include/asm/eth.h +17 -0
index 3dc84f0e4b..20175862f1 100644
@@ -39,4 +39,21 @@ int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
 int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
 				  unsigned int len);
 
+/**
+ * A packet handler
+ *
+ * dev - device pointer
+ * pkt - pointer to the "sent" packet
+ * len - packet length
+ */
+typedef int sandbox_eth_tx_hand_f(struct udevice *dev, void *pkt,
+				   unsigned int len);
+
+/*
+ * Set packet handler
+ *
+ * handler - The func ptr to call on send. If NULL, set to default handler
+ */
+void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler);
+
 #endif /* __ETH_H */
drivers/net/sandbox.c +51 -4

91 lines not on this page · raw message

Signature · 1 line
2.11.0
#8 lore raw

PATCH v2 4/10 net: sandbox: Share the priv structure with tests

In reply to Joe Hershberger

If tests want to implement tx handlers, they will likely need access to the details in the priv structure.

---

Changes in v2: None

 arch/sandbox/include/asm/eth.h | 19 +++++++++++++++++++
 drivers/net/sandbox.c          | 19 -------------------
 2 files changed, 19 insertions(+), 19 deletions(-)
arch/sandbox/include/asm/eth.h +19 -0
index 20175862f1..ced6d51999 100644
@@ -49,6 +49,25 @@ int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
 typedef int sandbox_eth_tx_hand_f(struct udevice *dev, void *pkt,
 				   unsigned int len);
 
+/**
+ * struct eth_sandbox_priv - memory for sandbox mock driver
+ *
+ * fake_host_hwaddr - MAC address of mocked machine
+ * fake_host_ipaddr - IP address of mocked machine
+ * disabled - Will not respond
+ * recv_packet_buffer - buffer of the packet returned as received
+ * recv_packet_length - length of the packet returned as received
+ * tx_handler - function to generate responses to sent packets
+ */
+struct eth_sandbox_priv {
+	uchar fake_host_hwaddr[ARP_HLEN];
+	struct in_addr fake_host_ipaddr;
+	bool disabled;
+	uchar *recv_packet_buffer;
+	int recv_packet_length;
+	sandbox_eth_tx_hand_f *tx_handler;
+};
+
 /*
  * Set packet handler
  *
drivers/net/sandbox.c +0 -19
index db461b892e..6f0fe0ced5 100644
@@ -15,25 +15,6 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-/**
- * struct eth_sandbox_priv - memory for sandbox mock driver
- *
- * fake_host_hwaddr: MAC address of mocked machine
- * fake_host_ipaddr: IP address of mocked machine
- * disabled: Will not respond
- * recv_packet_buffer: buffer of the packet returned as received
- * recv_packet_length: length of the packet returned as received
- * tx_handler - function to generate responses to sent packets
- */
-struct eth_sandbox_priv {
-	uchar fake_host_hwaddr[ARP_HLEN];
-	struct in_addr fake_host_ipaddr;
-	bool disabled;
-	uchar *recv_packet_buffer;
-	int recv_packet_length;
-	sandbox_eth_tx_hand_f *tx_handler;
-};
-
 static bool skip_timeout;
 
 /*
Signature · 1 line
2.11.0
#10 lore raw

PATCH v2 5/10 net: sandbox: Allow fake eth to handle more than 1 packet response

In reply to Joe Hershberger

Use up to the max allocated receive buffers so as to be able to test more complex situations.

---

Changes in v2: None

 arch/sandbox/include/asm/eth.h | 10 +++++---
 drivers/net/sandbox.c          | 57 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 52 insertions(+), 15 deletions(-)
arch/sandbox/include/asm/eth.h +6 -4
index ced6d51999..7a49e98fa8 100644
@@ -55,16 +55,18 @@ typedef int sandbox_eth_tx_hand_f(struct udevice *dev, void *pkt,
  * fake_host_hwaddr - MAC address of mocked machine
  * fake_host_ipaddr - IP address of mocked machine
  * disabled - Will not respond
- * recv_packet_buffer - buffer of the packet returned as received
- * recv_packet_length - length of the packet returned as received
+ * recv_packet_buffer - buffers of the packet returned as received
+ * recv_packet_length - lengths of the packet returned as received
+ * recv_packets - number of packets returned
  * tx_handler - function to generate responses to sent packets
  */
 struct eth_sandbox_priv {
 	uchar fake_host_hwaddr[ARP_HLEN];
 	struct in_addr fake_host_ipaddr;
 	bool disabled;
-	uchar *recv_packet_buffer;
-	int recv_packet_length;
+	uchar * recv_packet_buffer[PKTBUFSRX];
+	int recv_packet_length[PKTBUFSRX];
+	int recv_packets;
 	sandbox_eth_tx_hand_f *tx_handler;
 };
 
drivers/net/sandbox.c +46 -11

110 lines not on this page · raw message

Signature · 1 line
2.11.0
#12 lore raw

PATCH v2 6/10 net: Add an accessor to know if waiting for ARP

In reply to Joe Hershberger

This single-sources the state of the ARP.

---

Changes in v2:
- return bool instead of int

 include/net.h |  1 +
 net/arp.c     | 11 ++++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)
include/net.h +1 -0
index 2b2deb5aae..259c4a7ed4 100644
@@ -635,6 +635,7 @@ rxhand_f *net_get_udp_handler(void);	/* Get UDP RX packet handler */
 void net_set_udp_handler(rxhand_f *);	/* Set UDP RX packet handler */
 rxhand_f *net_get_arp_handler(void);	/* Get ARP RX packet handler */
 void net_set_arp_handler(rxhand_f *);	/* Set ARP RX packet handler */
+bool arp_is_waiting(void);		/* Waiting for ARP reply? */
 void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
 void net_set_timeout_handler(ulong, thand_f *);/* Set timeout handler */
 
net/arp.c +8 -3
index b8a71684cd..ea685d9ac6 100644
@@ -100,7 +100,7 @@ int arp_timeout_check(void)
 {
 	ulong t;
 
-	if (!net_arp_wait_packet_ip.s_addr)
+	if (!arp_is_waiting())
 		return 0;
 
 	t = get_timer(0);
@@ -187,8 +187,8 @@ void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 		return;
 
 	case ARPOP_REPLY:		/* arp reply */
-		/* are we waiting for a reply */
-		if (!net_arp_wait_packet_ip.s_addr)
+		/* are we waiting for a reply? */
+		if (!arp_is_waiting())
 			break;
 
 #ifdef CONFIG_KEEP_SERVERADDR
@@ -233,3 +233,8 @@ void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 		return;
 	}
 }
+
+bool arp_is_waiting(void)
+{
+	return !!net_arp_wait_packet_ip.s_addr;
+}
Signature · 1 line
2.11.0
#15 lore raw

PATCH v2 7/10 net: sandbox: Add a priv ptr for tests to use

In reply to Joe Hershberger

Tests need to be able to pass their "unit test state" to the handlers where asserts are evaluated. Add a function that allows the tests to set this private data on the sandbox eth device.

---

Changes in v2:
- add missing commit message
- rename local "uc_priv" variable to "dev_priv" to not be misleading.

 arch/sandbox/include/asm/eth.h |  9 +++++++++
 drivers/net/sandbox.c          | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+)
arch/sandbox/include/asm/eth.h +9 -0
index 7a49e98fa8..a6adec4e83 100644
@@ -59,6 +59,7 @@ typedef int sandbox_eth_tx_hand_f(struct udevice *dev, void *pkt,
  * recv_packet_length - lengths of the packet returned as received
  * recv_packets - number of packets returned
  * tx_handler - function to generate responses to sent packets
+ * priv - a pointer to some structure a test may want to keep track of
  */
 struct eth_sandbox_priv {
 	uchar fake_host_hwaddr[ARP_HLEN];
@@ -68,6 +69,7 @@ struct eth_sandbox_priv {
 	int recv_packet_length[PKTBUFSRX];
 	int recv_packets;
 	sandbox_eth_tx_hand_f *tx_handler;
+	void *priv;
 };
 
 /*
@@ -77,4 +79,11 @@ struct eth_sandbox_priv {
  */
 void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler);
 
+/*
+ * Set priv ptr
+ *
+ * priv - priv void ptr to store in the device
+ */
+void sandbox_eth_set_priv(int index, void *priv);
+
 #endif /* __ETH_H */
drivers/net/sandbox.c +20 -0
index 9c0e0d009e..e26e72ecc1 100644
@@ -206,6 +206,26 @@ void sandbox_eth_set_tx_handler(int index, sandbox_eth_tx_hand_f *handler)
 		priv->tx_handler = sb_default_handler;
 }
 
+/*
+ * Set priv ptr
+ *
+ * priv - priv void ptr to store in the device
+ */
+void sandbox_eth_set_priv(int index, void *priv)
+{
+	struct udevice *dev;
+	struct eth_sandbox_priv *dev_priv;
+	int ret;
+
+	ret = uclass_get_device(UCLASS_ETH, index, &dev);
+	if (ret)
+		return;
+
+	dev_priv = dev_get_priv(dev);
+
+	dev_priv->priv = priv;
+}
+
 static int sb_eth_start(struct udevice *dev)
 {
 	struct eth_sandbox_priv *priv = dev_get_priv(dev);
Signature · 1 line
2.11.0
#16 lore raw

PATCH v2 7/10 net: sandbox: Add a priv ptr for tests to use

Quoted 9 lines · Joe Hershberger

Tests need to be able to pass their "unit test state" to the handlers where asserts are evaluated. Add a function that allows the tests to set this private data on the sandbox eth device.

---

Quoted 8 lines

Changes in v2:
- add missing commit message
- rename local "uc_priv" variable to "dev_priv" to not be misleading.

 arch/sandbox/include/asm/eth.h |  9 +++++++++
 drivers/net/sandbox.c          | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+)
#18 lore raw

PATCH v2 8/10 test: eth: Add a test for ARP requests

In reply to Joe Hershberger

This tests that ARP requests made to this target's IP address are responded-to by the target when it is doing other networking operations.

This currently corrupts the ongoing operation of the device if it happens to be awaiting an ARP reply of its own to whatever serverip it is attempting to communicate with. In the test, add an expectation that the user operation (ping, in this case) will fail. A later patch will address this problem.

---

Changes in v2:
- add comments as to the use of the uts variable
- check the return of the injection handler and pass on overflow error
- return an error instead of 0 / 1

 arch/sandbox/include/asm/eth.h | 10 +++++
 drivers/net/sandbox.c          | 41 ++++++++++++++++++++
 test/dm/eth.c                  | 86 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+)
arch/sandbox/include/asm/eth.h +10 -0
index a6adec4e83..d068486f0b 100644
@@ -39,6 +39,16 @@ int sandbox_eth_arp_req_to_reply(struct udevice *dev, void *packet,
 int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
 				  unsigned int len);
 
+/*
+ * sandbox_eth_recv_arp_req()
+ *
+ * Inject an ARP request for this target
+ *
+ * @dev: device that received the packet
+ * @return 0 if injected, -EOVERFLOW if not
+ */
+int sandbox_eth_recv_arp_req(struct udevice *dev);
+
 /**
  * A packet handler
  *
drivers/net/sandbox.c +41 -0

47 lines not on this page · raw message

test/dm/eth.c +86 -0

89 lines not on this page · raw message

Signature · 1 line
2.11.0
#20 lore raw

PATCH v2 9/10 test: eth: Add a test for the target being pinged

In reply to Joe Hershberger

The target will respond to pings while doing other network handling. Make sure that the response happens and is correct.

This currently corrupts the ongoing operation of the device if it happens to be awaiting an ARP reply of its own to whatever serverip it is attempting to communicate with. In the test, add an expectation that the user operation (ping, in this case) will fail. A later patch will address this problem.

---

Changes in v2:
- add comments as to the use of the uts variable
- check the return of the injection handler and pass on overflow error
- return an error instead of 0 / 1

 arch/sandbox/include/asm/eth.h | 10 +++++
 drivers/net/sandbox.c          | 51 +++++++++++++++++++++++++
 test/dm/eth.c                  | 86 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 147 insertions(+)
arch/sandbox/include/asm/eth.h +10 -0
index d068486f0b..477fa00fad 100644
@@ -49,6 +49,16 @@ int sandbox_eth_ping_req_to_reply(struct udevice *dev, void *packet,
  */
 int sandbox_eth_recv_arp_req(struct udevice *dev);
 
+/*
+ * sandbox_eth_recv_ping_req()
+ *
+ * Inject a ping request for this target
+ *
+ * @dev: device that received the packet
+ * @return 0 if injected, -EOVERFLOW if not
+ */
+int sandbox_eth_recv_ping_req(struct udevice *dev);
+
 /**
  * A packet handler
  *
drivers/net/sandbox.c +51 -0

57 lines not on this page · raw message

test/dm/eth.c +86 -0

89 lines not on this page · raw message

Signature · 1 line
2.11.0
#22 lore raw

PATCH v2 10/10 net: Don't overwrite waiting packets with asynchronous replies

In reply to Joe Hershberger

Peter originally sent a fix, but it breaks a number of other things. This addresses the original reported issue in a different way.

That report was:

Quoted 7 lines

U-Boot has 1 common buffer to send Ethernet frames, pointed to by net_tx_packet. When sending to an IP address without knowing the MAC address, U-Boot makes an ARP request (using the arp_tx_packet buffer) to find out the MAC address of the IP addressr. When a matching ARP reply is received, U-Boot continues sending the frame stored in the net_tx_packet buffer.

Quoted 6 lines

However, in the mean time, if U-Boot needs to send out any network packets (e.g. replying ping packets or ARP requests for its own IP address etc.), it will use the net_tx_packet buffer to prepare the new packet. Thus this buffer is no longer the original packet meant to be transmitted after the ARP reply. The original packet will be lost.

This instead uses the ARP tx buffer to send async replies in the case where we are actively waiting for an ARP reply.

---

Changes in v2: None

 include/net.h | 8 ++++++++
 net/arp.c     | 9 +++++----
 net/arp.h     | 1 +
 net/net.c     | 8 ++++++++
 net/ping.c    | 7 +++++--
 test/dm/eth.c | 6 ++----
 6 files changed, 29 insertions(+), 10 deletions(-)
include/net.h +8 -0
index 259c4a7ed4..a789a381ab 100644
@@ -654,6 +654,14 @@ static inline void net_set_state(enum net_loop_state state)
 	net_state = state;
 }
 
+/*
+ * net_get_async_tx_pkt_buf - Get a packet buffer that is not in use for
+ *			      sending an asynchronous reply
+ *
+ * returns - ptr to packet buffer
+ */
+uchar * net_get_async_tx_pkt_buf(void);
+
 /* Transmit a packet */
 static inline void net_send_packet(uchar *pkt, int len)
 {
net/arp.c +5 -4
index ea685d9ac6..b49c3d3ced 100644
@@ -34,8 +34,7 @@ uchar	       *arp_wait_packet_ethaddr;
 int		arp_wait_tx_packet_size;
 ulong		arp_wait_timer_start;
 int		arp_wait_try;
-
-static uchar   *arp_tx_packet;	/* THE ARP transmit packet */
+uchar	       *arp_tx_packet; /* THE ARP transmit packet */
 static uchar	arp_tx_packet_buf[PKTSIZE_ALIGN + PKTALIGN];
 
 void arp_init(void)
@@ -126,6 +125,7 @@ void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 	struct arp_hdr *arp;
 	struct in_addr reply_ip_addr;
 	int eth_hdr_size;
+	uchar *tx_packet;
 
 	/*
 	 * We have to deal with two types of ARP packets:
@@ -182,8 +182,9 @@ void arp_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 		    (net_read_ip(&arp->ar_spa).s_addr & net_netmask.s_addr))
 			udelay(5000);
 #endif
-		memcpy(net_tx_packet, et, eth_hdr_size + ARP_HDR_SIZE);
-		net_send_packet(net_tx_packet, eth_hdr_size + ARP_HDR_SIZE);
+		tx_packet = net_get_async_tx_pkt_buf();
+		memcpy(tx_packet, et, eth_hdr_size + ARP_HDR_SIZE);
+		net_send_packet(tx_packet, eth_hdr_size + ARP_HDR_SIZE);
 		return;
 
 	case ARPOP_REPLY:		/* arp reply */
net/arp.h +1 -0
index afb86958f3..25b3c00d5c 100644
@@ -20,6 +20,7 @@ extern uchar *arp_wait_packet_ethaddr;
 extern int arp_wait_tx_packet_size;
 extern ulong arp_wait_timer_start;
 extern int arp_wait_try;
+extern uchar *arp_tx_packet;
 
 void arp_init(void);
 void arp_request(void);
net/net.c +8 -0
index 31cf306ae7..77a71415f0 100644
@@ -799,6 +799,14 @@ void net_set_timeout_handler(ulong iv, thand_f *f)
 	}
 }
 
+uchar *net_get_async_tx_pkt_buf(void)
+{
+	if (arp_is_waiting())
+		return arp_tx_packet; /* If we are waiting, we already sent */
+	else
+		return net_tx_packet;
+}
+
 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
 		int payload_len)
 {
net/ping.c +5 -2
index 3e5461a36a..821d35d01d 100644
@@ -84,6 +84,7 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 	struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
 	struct in_addr src_ip;
 	int eth_hdr_size;
+	uchar *tx_packet;
 
 	switch (icmph->type) {
 	case ICMP_ECHO_REPLY:
@@ -107,8 +108,10 @@ void ping_receive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 		icmph->type = ICMP_ECHO_REPLY;
 		icmph->checksum = 0;
 		icmph->checksum = compute_ip_checksum(icmph, len - IP_HDR_SIZE);
-		memcpy(net_tx_packet, et, eth_hdr_size + len);
-		net_send_packet(net_tx_packet, eth_hdr_size + len);
+
+		tx_packet = net_get_async_tx_pkt_buf();
+		memcpy(tx_packet, et, eth_hdr_size + len);
+		net_send_packet(tx_packet, eth_hdr_size + len);
 		return;
 /*	default:
 		return;*/
test/dm/eth.c +2 -4
index 86482e9755..850eabb9dc 100644
@@ -332,10 +332,9 @@ static int dm_test_eth_async_arp_reply(struct unit_test_state *uts)
 	sandbox_eth_set_tx_handler(0, sb_with_async_arp_handler);
 	/* Used by all of the ut_assert macros in the tx_handler */
 	sandbox_eth_set_priv(0, uts);
-	sandbox_eth_skip_timeout();
 
 	env_set("ethact", "eth at 10002000");
-	ut_assert(net_loop(PING) == -ETIMEDOUT);
+	ut_assertok(net_loop(PING));
 	ut_asserteq_str("eth at 10002000", env_get("ethact"));
 
 	sandbox_eth_set_tx_handler(0, NULL);
@@ -418,10 +417,9 @@ static int dm_test_eth_async_ping_reply(struct unit_test_state *uts)
 	sandbox_eth_set_tx_handler(0, sb_with_async_ping_handler);
 	/* Used by all of the ut_assert macros in the tx_handler */
 	sandbox_eth_set_priv(0, uts);
-	sandbox_eth_skip_timeout();
 
 	env_set("ethact", "eth at 10002000");
-	ut_assert(net_loop(PING) == -ETIMEDOUT);
+	ut_assertok(net_loop(PING));
 	ut_asserteq_str("eth at 10002000", env_get("ethact"));
 
 	sandbox_eth_set_tx_handler(0, NULL);
Signature · 1 line
2.11.0