浏览代码

Keep network config across kexec

Johannes Zellner 2 周之前
父节点
当前提交
ab9bbbc009
共有 3 个文件被更改,包括 61 次插入 和 3 次删除
  1. 3 0
      bootinfo.h
  2. 36 3
      kernel.c
  3. 22 0
      kexec.c

+ 3 - 0
bootinfo.h

@@ -19,6 +19,9 @@
 
 #define BOOTINFO_FLAG_EFI  0x00000001u
 #define BOOTINFO_FLAG_RGBX 0x00000002u  /* GOP 32bpp byte order R,G,B,X */
+/* The kexec receiver carried the running IPv4 configuration across the
+ * jump: reserved[0..3] hold ip/mask/gateway/DNS (byte 0 in the low byte). */
+#define BOOTINFO_FLAG_NET  0x00000004u
 
 struct boot_info {
 	unsigned int magic;

+ 36 - 3
kernel.c

@@ -13,6 +13,7 @@
 #include "mm.h"
 #include "mouse.h"
 #include "netdev.h"
+#include "net.h"
 #include "dhcp.h"
 #include "netlog.h"
 #include "paging.h"
@@ -326,10 +327,41 @@ static int pit_ticking(void)
 	return 0;
 }
 
+/* A kexec/net-dev reload starts a fresh kernel with zeroed BSS, so the
+ * leased address and resolver would otherwise fall back to the static
+ * default until someone types dhcp.  The previous kernel stashed them in
+ * the boot-info reserved words; apply them before the boot DHCP attempt. */
+static int net_restore_boot_config(void)
+{
+	const volatile unsigned int *p;
+	unsigned char ip[4], mask[4], gw[4], dns[4];
+	unsigned int i;
+
+	if (!bootinfo_valid() || !(bootinfo_flags() & BOOTINFO_FLAG_NET))
+		return 0;
+	p = bootinfo_words();
+	for (i = 0; i < 4; i++) {
+		ip[i] = (unsigned char)(p[7] >> (8 * i));
+		mask[i] = (unsigned char)(p[8] >> (8 * i));
+		gw[i] = (unsigned char)(p[9] >> (8 * i));
+		dns[i] = (unsigned char)(p[10] >> (8 * i));
+	}
+	if (!(ip[0] || ip[1] || ip[2] || ip[3]))
+		return 0;
+	net_configure(ip, mask, gw);
+	if (dns[0] || dns[1] || dns[2] || dns[3])
+		net_set_dns(dns);
+	boot_trace("net retained");
+	/* A resolver is what net-dev users notice missing; without one, let
+	 * the boot DHCP attempt learn it. */
+	return dns[0] || dns[1] || dns[2] || dns[3];
+}
+
 void kernel_main(void);
 void kernel_main(void)
 {
 	unsigned char *p = __bss_start;
+	int net_kept;
 	while (p < __bss_end)
 		*p++ = 0;
 
@@ -476,6 +508,7 @@ void kernel_main(void)
 	/* Probe the NIC (link negotiation is asynchronous now) before the
 	 * kworker starts, so its netlog flushes have a device to use. */
 	netdev_active();
+	net_kept = net_restore_boot_config();
 	boot_trace("net");
 
 	/* The idle process is the kernel_main context; housekeeping runs in
@@ -508,9 +541,9 @@ void kernel_main(void)
 	/* With a NIC present, try DHCP; on failure the static default stays.
 	 * Deliberately after the WM: a missing cable or DHCP server must not
 	 * delay the first frame. */
-	if (netdev_active())
-		dhcp_run(3000);
-	boot_trace("dhcp");
+	if (netdev_active() && !net_kept)
+		dhcp_run(4000);
+	boot_trace(net_kept ? "dhcp skipped" : "dhcp");
 
 	/* Dump the active NIC's counters after the boot DHCP attempt.  With
 	 * the ASIX as the management link this appears in netmon even when

+ 22 - 0
kexec.c

@@ -27,6 +27,7 @@
 #include "bootinfo.h"
 #include "io.h"
 #include "mm.h"
+#include "net.h"
 #include "netdev.h"
 #include "ramfs.h"
 #include "serial.h"
@@ -119,12 +120,19 @@ static int kexec_fail(const char *msg)
 /* Publish the boot-info block the new kernel reads.  The pack source is the
  * staging tail for a full reload, or the running ramfs for a kernel-only one;
  * ramfs_init() takes its own copy and releases the region. */
+static unsigned int pack_ip(const unsigned char ip[4])
+{
+	return (unsigned int)ip[0] | ((unsigned int)ip[1] << 8) |
+	       ((unsigned int)ip[2] << 16) | ((unsigned int)ip[3] << 24);
+}
+
 static void kexec_write_bootinfo(unsigned int pack_addr, unsigned int pack_size)
 {
 	volatile struct boot_info *bi =
 		(volatile struct boot_info *)BOOT_INFO_ADDR;
 	unsigned int words[BOOT_INFO_WORDS];
 	unsigned int rsdp = 0, flags = 0, sum = 0, i;
+	unsigned char ip[4], mask[4], gw[4], dns[4];
 
 	__asm__ volatile ("" : "+r"(bi));
 
@@ -140,6 +148,20 @@ static void kexec_write_bootinfo(unsigned int pack_addr, unsigned int pack_size)
 	words[4] = flags;
 	words[5] = pack_addr;
 	words[6] = pack_size;
+	/* Keep the running IP configuration across the jump: the new kernel's
+	 * boot DHCP only has a four-second link/DHCP window, and a net-dev
+	 * reload otherwise comes back on the static fallback address. */
+	net_get_ip(ip);
+	if (ip[0] || ip[1] || ip[2] || ip[3]) {
+		net_get_mask(mask);
+		net_get_gateway(gw);
+		net_get_dns(dns);
+		flags |= BOOTINFO_FLAG_NET;
+		words[7] = pack_ip(ip);
+		words[8] = pack_ip(mask);
+		words[9] = pack_ip(gw);
+		words[10] = pack_ip(dns);
+	}
 	for (i = 0; i < BOOT_INFO_WORDS; i++)
 		sum += words[i];
 	words[2] = 0u - sum;