فهرست منبع

Answer ARP/ICMP while receiving a netkexec upload

The board owns the NIC for the whole transfer, so the idle loop's
ARP/ICMP responder could not run.  A multi-megabyte upload outlives the
host's neighbour entry: after three unanswered probes the host marked the
board FAILED and dropped every datagram while sendto() still returned
success, which stalled make net-dev at a random offset.

Answer ARP/ICMP in netkexec_recv() for the receiving NIC, using that
NIC's MAC and link-local default address, so probes and pings keep
working during the transfer.

On the host side, require the offset to advance: a 10 s no-progress
guard now reports a dead neighbour instead of retrying forever on stale
ACKs, and an ACK_BADSUM is reported as a checksum failure instead of
being treated as completion.

Update AGENTS.md, docs/network.html and docs/development.html.
Johannes Zellner 1 هفته پیش
والد
کامیت
abbc1fbd1b
7فایلهای تغییر یافته به همراه162 افزوده شده و 43 حذف شده
  1. 6 1
      AGENTS.md
  2. 6 3
      docs/development.html
  3. 5 1
      docs/network.html
  4. 40 8
      net.c
  5. 7 1
      net.h
  6. 57 28
      netkexec.c
  7. 41 1
      netkexec_send.c

+ 6 - 1
AGENTS.md

@@ -876,7 +876,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   (`make net-dev BOARD_IP=…`): the WM loop auto-detects the header, the board
   acks every datagram with the next expected offset, verifies the checksum and
   jumps. While idle the same loop answers ARP/ICMP for our IP, so the board is
-  reachable (and MAC-resolvable) without running any command.
+  reachable (and MAC-resolvable) without running any command.  The receive
+  loop keeps answering ARP/ICMP for the receiving NIC as well, because a
+  multi-megabyte upload outlives the host's neighbour entry: unanswered
+  probes make the host drop datagrams while `sendto()` still succeeds.  The
+  host tool stops after 10 s without the offset advancing and reports an
+  `ACK_BADSUM` as a failure instead of as completion.
   `kexec_jump()` calls `smp_stop_aps()` first: every AP gets an INIT and is
   parked in the wait-for-SIPI state before the trampoline overwrites the kernel
   they were executing.  It runs with interrupts off, resets `sched_lock` (an AP

+ 6 - 3
docs/development.html

@@ -253,9 +253,12 @@ make net-install NETINSTALL_TARGET=sata0
 make net-cmds                   # replace /ramfs only</code></pre>
 		<p>
 			The board must be idle at a shell prompt. The WM loop answers
-			ARP/ICMP, detects the <code>KXN2</code>/<code>KXNP</code> header and
-			pauses the auto-detect while a packet-level command owns the NIC or a
-			transfer is in progress. Datagrams are at most 1400 bytes.
+			ARP/ICMP while idle, detects the <code>KXN2</code>/<code>KXNP</code>
+			header and pauses the auto-detect while a packet-level command owns
+			the NIC or a transfer is in progress.  During the transfer the
+			receive loop keeps answering ARP/ICMP itself, so the host's
+			neighbour entry cannot expire mid-upload. Datagrams are at most
+			1400 bytes.
 		</p>
 		<p>
 			The board address comes from DHCP when a server is present; use

+ 5 - 1
docs/network.html

@@ -396,7 +396,11 @@
 			UDP port 19288 using <code>KXN2</code>, a runtime command pack using
 			<code>KXNP</code>, and a persistent install using <code>KXI2</code>.
 			Datagrams are acknowledged with the next expected offset and verified
-			with a 32-bit checksum. <code>KXN2</code>/<code>KXNP</code> jump or
+			with a 32-bit checksum. While the receive owns the NIC it keeps
+			answering ARP/ICMP for that NIC, so the host's neighbour entry cannot
+			expire in the middle of a long upload; the host gives up after 10 s
+			without the offset advancing and reports a checksum rejection as a
+			failure. <code>KXN2</code>/<code>KXNP</code> jump or
 			swap only in RAM. <code>KXI2</code> carries a 16-byte target name;
 			the board writes P2, then the P1 kernel, patches the exact
 			<code>kernel_sectors</code>, verifies the disk, sends

+ 40 - 8
net.c

@@ -670,10 +670,33 @@ void net_decode_frame(const unsigned char *f, unsigned int len)
 
 /* ---- reply generation ---- */
 
-int net_build_reply(const unsigned char *frame, unsigned int len,
-		    unsigned char *out, unsigned int maxout)
+/* Source address for a reply received on `nd`: the configured IP, or the
+ * driver's link-local default when the peer targets that.  The ASIX
+ * management link keeps 192.168.50.2 while the active e1000e may hold a
+ * DHCP address on another subnet. */
+static int reply_src_ip(const struct netdev *nd, const unsigned char *dst,
+			unsigned char src[4])
 {
-	unsigned char mac[6];
+	unsigned int i;
+
+	if (ip_eq(dst, my_ip)) {
+		for (i = 0; i < 4; i++)
+			src[i] = my_ip[i];
+		return 1;
+	}
+	if (nd && nd->default_ip && ip_eq(dst, nd->default_ip)) {
+		for (i = 0; i < 4; i++)
+			src[i] = nd->default_ip[i];
+		return 1;
+	}
+	return 0;
+}
+
+int net_build_reply_dev(const struct netdev *nd,
+			const unsigned char *frame, unsigned int len,
+			unsigned char *out, unsigned int maxout)
+{
+	unsigned char mac[6], src_ip[4];
 	unsigned short et;
 	unsigned int i;
 
@@ -681,7 +704,10 @@ int net_build_reply(const unsigned char *frame, unsigned int len,
 		return 0;
 	et = ((unsigned short)frame[12] << 8) | frame[13];
 
-	netdev_get_mac(mac);
+	if (nd && nd->get_mac)
+		nd->get_mac(mac);
+	else
+		netdev_get_mac(mac);
 
 	/* ARP request for our IP -> ARP reply. */	if (et == ET_ARP && len >= 14 + 28) {
 		const unsigned char *a = frame + 14;
@@ -695,7 +721,7 @@ int net_build_reply(const unsigned char *frame, unsigned int len,
 			return 0;
 		if (a[4] != 6 || a[5] != 4)
 			return 0;
-		if (!ip_eq(a + 24, my_ip))                       /* target != us */
+		if (!reply_src_ip(nd, a + 24, src_ip))           /* target != us */
 			return 0;
 
 		for (i = 0; i < 6; i++)
@@ -713,7 +739,7 @@ int net_build_reply(const unsigned char *frame, unsigned int len,
 			for (i = 0; i < 6; i++)
 				r[8 + i] = mac[i];        /* sha */
 			for (i = 0; i < 4; i++)
-				r[14 + i] = my_ip[i];     /* spa */
+				r[14 + i] = src_ip[i];    /* spa */
 			for (i = 0; i < 6; i++)
 				r[18 + i] = a[8 + i];     /* tha = requester MAC */
 			for (i = 0; i < 4; i++)
@@ -737,7 +763,7 @@ int net_build_reply(const unsigned char *frame, unsigned int len,
 			return 0;
 		if (ip[9] != IPPROTO_ICMP)
 			return 0;
-		if (!ip_eq(ip + 16, my_ip))
+		if (!reply_src_ip(nd, ip + 16, src_ip))
 			return 0;
 
 		ip_len = ((unsigned short)ip[2] << 8) | ip[3];
@@ -773,7 +799,7 @@ int net_build_reply(const unsigned char *frame, unsigned int len,
 		rip[9] = IPPROTO_ICMP;
 		rip[10] = 0; rip[11] = 0;             /* checksum (computed below) */
 		for (i = 0; i < 4; i++)
-			rip[12 + i] = my_ip[i];         /* src = us */
+			rip[12 + i] = src_ip[i];        /* src = us */
 		for (i = 0; i < 4; i++)
 			rip[16 + i] = ip[12 + i];       /* dst = requester */
 
@@ -801,3 +827,9 @@ int net_build_reply(const unsigned char *frame, unsigned int len,
 
 	return 0;
 }
+
+int net_build_reply(const unsigned char *frame, unsigned int len,
+		    unsigned char *out, unsigned int maxout)
+{
+	return net_build_reply_dev(netdev_active(), frame, len, out, maxout);
+}

+ 7 - 1
net.h

@@ -107,8 +107,14 @@ int net_udp_parse_any(const unsigned char *frame, unsigned int len,
 void net_decode_frame(const unsigned char *frame, unsigned int len);
 
 /* Build a reply to an ARP request or ICMP echo request addressed to us.
-   Returns reply length (> 0) or 0 if the frame needs no reply. */
+   Returns reply length (> 0) or 0 if the frame needs no reply.  The _dev
+   variant uses the given NIC's MAC and accepts its link-local default
+   address (a reply received on the management link must not carry the active
+   NIC's address); net_build_reply() uses the active NIC. */
 int net_build_reply(const unsigned char *frame, unsigned int len,
 		    unsigned char *out, unsigned int maxout);
+int net_build_reply_dev(const struct netdev *nd,
+			const unsigned char *frame, unsigned int len,
+			unsigned char *out, unsigned int maxout);
 
 #endif

+ 57 - 28
netkexec.c

@@ -23,7 +23,10 @@
  * program_netkexec_main() then runs stop-and-wait: every datagram is acked
  * with the next expected offset, so a lost or duplicated datagram costs one
  * retransmit.  Datagrams addressed to the broadcast IP are accepted too, so
- * the host can push without knowing whether the board is up yet.
+ * the host can push without knowing whether the board is up yet.  While the
+ * receive owns the NIC it keeps answering ARP/ICMP, otherwise the host's
+ * neighbour entry expires in the middle of a long upload and the host drops
+ * every datagram while sendto() still succeeds.
  */
 
 #include "install.h"
@@ -163,11 +166,15 @@ static int netkexec_poll_nd(const struct netdev *nd, int capture)
 		 * ICMP echo requests for our IP (so the board can be pinged,
 		 * and a host can resolve its MAC, without running a
 		 * command).  Until an address is configured there is nothing
-		 * to answer for. */
-		if (net_is_configured()) {
-			int rlen = net_build_reply(rxbuf, (unsigned int)len,
-						   replybuf,
-						   sizeof(replybuf));
+		 * to answer for.  Use the receiving NIC's MAC: this also
+		 * runs for the active NIC when the management link is a
+		 * different device.  A driver's link-local default address
+		 * (ASIX) is answerable even when the active NIC has none. */
+		if (net_is_configured() || nd->default_ip) {
+			int rlen = net_build_reply_dev(nd, rxbuf,
+						       (unsigned int)len,
+						       replybuf,
+						       sizeof(replybuf));
 			if (rlen > 0)
 				nd->tx(replybuf, (unsigned int)rlen);
 		}
@@ -268,9 +275,9 @@ int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
 	return 1;
 }
 
-static void send_ack(unsigned int next_offset, unsigned int status)
+static void send_ack(const struct netdev *nd, unsigned int next_offset,
+		     unsigned int status)
 {
-	const struct netdev *nd = netdev_mgmt();
 	unsigned char src[4];
 
 	if (!nd)
@@ -295,24 +302,26 @@ static int netkexec_recv(unsigned int stage_base)
 		return -1;
 	kexec_total = x_size;
 	kexec_progress = 0;
-	send_ack(0, ACK_CONTINUE);          /* header ok, send from offset 0 */
+	send_ack(nd, 0, ACK_CONTINUE);      /* header ok, send from offset 0 */
 
 	while (offset < x_size) {
 		const unsigned char *payload;
-		unsigned int plen, port, doff, dlen;
+		unsigned int plen, doff, dlen;
+		unsigned char p_mac[6], p_ip[4];
+		unsigned int p_port;
 		int len, i;
 
 		/* A host that trickles one datagram at a time can keep the
-		 * per-packet deadline alive forever, holding the NIC (and the
-		 * background ARP/ICMP responder) hostage.  Cap the whole
-		 * transfer as well. */
+		 * per-packet deadline alive forever, holding the NIC (and
+		 * the auto-detect loop) hostage.  Cap the whole transfer as
+		 * well. */
 		if ((int)(get_ticks_ms() - hard_deadline) > 0)
 			return -1;
 
 		if ((int)(get_ticks_ms() - deadline) > 0) {
 			if (++retries > 10)
 				return -1;      /* host gave up */
-			send_ack(offset, ACK_CONTINUE);
+			send_ack(nd, offset, ACK_CONTINUE);
 			deadline = get_ticks_ms() + 1000;
 			continue;
 		}
@@ -325,28 +334,48 @@ static int netkexec_recv(unsigned int stage_base)
 			process_sleep_ms(1);
 			continue;
 		}
+
+		/* We own the NIC for the whole transfer, so the idle loop's
+		 * ARP/ICMP responder cannot run.  Keep answering here: a
+		 * multi-megabyte upload lasts long enough for the host's
+		 * neighbour entry to go STALE, and once its probes go
+		 * unanswered the host drops every datagram while sendto()
+		 * still returns success (the transfer would stall at a
+		 * random offset until the host gives up). */
 		if (!parse_udp(rxbuf, (unsigned int)len, &payload, &plen,
-			       peer_mac, peer_ip, &port)) {
-			process_sleep_ms(1);
-			continue;
-		}
-		if (plen < 12 || rd32(payload) != X_MAGIC_DATA) {
+			       p_mac, p_ip, &p_port) ||
+		    plen < 12 || rd32(payload) != X_MAGIC_DATA) {
+			net_arp_input(rxbuf, (unsigned int)len);
+			if (net_is_configured() || nd->default_ip) {
+				int rlen = net_build_reply_dev(nd, rxbuf,
+							       (unsigned int)len,
+							       replybuf,
+							       sizeof(replybuf));
+				if (rlen > 0)
+					nd->tx(replybuf, (unsigned int)rlen);
+			}
+			/* Yield after a frame that was not ours (ARP/ICMP or
+			 * a stray datagram), so a broadcast storm cannot keep
+			 * the BKL for the whole transfer. */
 			process_sleep_ms(1);
 			continue;
 		}
 
 		doff = rd32(payload + 4);
 		dlen = rd32(payload + 8);
-		if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen) {
-			process_sleep_ms(1);
+		if (dlen > NETKEXEC_DATA_MAX || plen < 12 + dlen)
 			continue;
-		}
 
 		if (doff == offset) {
 			for (i = 0; i < (int)dlen; i++) {
 				stage[offset + i] = payload[12 + i];
 				sum += payload[12 + i];
 			}
+			for (i = 0; i < 6; i++)
+				peer_mac[i] = p_mac[i];
+			for (i = 0; i < 4; i++)
+				peer_ip[i] = p_ip[i];
+			peer_port = p_port;
 			offset += dlen;
 			kexec_progress = offset;
 			retries = 0;
@@ -355,14 +384,14 @@ static int netkexec_recv(unsigned int stage_base)
 				io_putchar('.');
 		}
 		if (offset < x_size)
-			send_ack(offset, ACK_CONTINUE);  /* also acks duplicates */
+			send_ack(nd, offset, ACK_CONTINUE); /* acks duplicates */
 	}
 
 	if (sum != x_csum) {
-		send_ack(offset, ACK_BADSUM);
+		send_ack(nd, offset, ACK_BADSUM);
 		return -1;
 	}
-	send_ack(x_size, ACK_DONE);
+	send_ack(nd, x_size, ACK_DONE);
 	return 0;
 }
 
@@ -454,7 +483,7 @@ void program_netkexec_main(void)
 		if (x_pack_size < ROOTFS_HDR_SIZE ||
 		    rh->pack_bytes > x_pack_size - ROOTFS_HDR_SIZE) {
 			io_putstr("netinstall: bad rootfs image\n");
-			send_ack(x_size, ACK_INSTALL_FAIL);
+			send_ack(netdev_mgmt(), x_size, ACK_INSTALL_FAIL);
 			kexec_stage_free();
 			netdev_release();
 			kexec_busy = 0;
@@ -465,13 +494,13 @@ void program_netkexec_main(void)
 				      rh, x_pack_size);
 		if (r != 0) {
 			io_putstr("netinstall: failed; disk not updated\n");
-			send_ack(x_size, ACK_INSTALL_FAIL);
+			send_ack(netdev_mgmt(), x_size, ACK_INSTALL_FAIL);
 			kexec_stage_free();
 			netdev_release();
 			kexec_busy = 0;
 			return;
 		}
-		send_ack(x_size, ACK_INSTALLED);
+		send_ack(netdev_mgmt(), x_size, ACK_INSTALLED);
 		io_putstr("netinstall: persisted to ");
 		io_putstr(x_install_target);
 		io_putstr(" -> jumping\n");

+ 41 - 1
netkexec_send.c

@@ -26,7 +26,9 @@
 #define MAGIC_INST 0x3249584Bu   /* "KXI2" (persist kernel + rootfs) */
 #define MAGIC_DATA 0x444E584Bu   /* "KXND" */
 #define MAGIC_ACK  0x414E584Bu   /* "KXNA" */
+#define ACK_CONTINUE      0
 #define ACK_DONE          1
+#define ACK_BADSUM        2
 #define ACK_INSTALLED     3
 #define ACK_INSTALL_FAIL  4
 
@@ -51,6 +53,18 @@ static int send_packet(int fd, const struct sockaddr_in *dst,
 		      sizeof(*dst)) == (ssize_t)len ? 0 : -1;
 }
 
+/* Milliseconds for the no-progress guard: a stalled transfer must time out
+ * even while the board keeps sending stale ACKs that do not advance the
+ * offset. */
+static unsigned long long now_ms(void)
+{
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+	return (unsigned long long)tv.tv_sec * 1000ULL +
+	       (unsigned long long)tv.tv_usec / 1000ULL;
+}
+
 /* Wait for an ACK ("KXNA" next_offset status).  Returns next_offset, or -1
  * on timeout. */
 static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status)
@@ -110,6 +124,7 @@ int main(int argc, char **argv)
 	unsigned char *kdata, *pdata = 0, *data, *pkt;
 	long ksize, psize = 0, size;
 	unsigned int offset = 0, sum = 0;
+	unsigned long long progress_ms = 0;
 	int fd, i, fails = 0;
 	int payload = 0, install = 0, installed = 0, install_failed = 0;
 	const char *install_target = 0;
@@ -252,11 +267,24 @@ int main(int argc, char **argv)
 	}
 
 	/* Stop-and-wait data transfer (kernel bytes, then rootfs bytes). */
+	progress_ms = now_ms();
 	while (offset < (unsigned int)size) {
 		unsigned int chunk = (unsigned int)size - offset;
 		uint32_t status = 0;
 		long ack;
 
+		/* An ACK that does not advance the offset (the board keeps
+		 * repeating its last offset while it receives nothing) is
+		 * not progress.  Bound the time without it so a lost ARP
+		 * entry or a dead link reports a readable error instead of
+		 * retransmitting for as long as the ACKs keep coming. */
+		if (now_ms() - progress_ms > 10000ULL) {
+			fprintf(stderr,
+				"\nnetkexec: no forward progress at offset %u/%ld (board unreachable?)\n",
+				offset, size);
+			return 1;
+		}
+
 		if (chunk > DATA_MAX)
 			chunk = DATA_MAX;
 		wr32(pkt, MAGIC_DATA);
@@ -277,6 +305,12 @@ int main(int argc, char **argv)
 			continue;                  /* retransmit */
 		}
 		fails = 0;
+		if (status == ACK_BADSUM) {
+			fprintf(stderr,
+				"\nnetkexec: board rejected the checksum at offset %u\n",
+				offset);
+			return 1;
+		}
 		if ((unsigned int)ack == (unsigned int)size &&
 		    status == ACK_INSTALLED) {
 			offset = (unsigned int)ack;
@@ -292,8 +326,14 @@ int main(int argc, char **argv)
 			offset = (unsigned int)ack;
 			break;
 		}
-		if (ack > (long)offset && (unsigned int)ack <= (unsigned int)size)
+		/* Only a CONTINUE ACK that advances the offset is progress;
+		 * a duplicate or a repeated offset means retransmit the same
+		 * datagram, not that the transfer is healthy. */
+		if (status == ACK_CONTINUE && (unsigned int)ack > offset &&
+		    (unsigned int)ack <= (unsigned int)size) {
 			offset = (unsigned int)ack;
+			progress_ms = now_ms();
+		}
 		printf("\rnetkexec: %3u%% (%u/%ld bytes)", offset * 100 / (unsigned)size,
 		       offset, size);
 		fflush(stdout);