Skip to contentSkip to conversation

pc-bios/s390-ccw: Fix problems related to network booting

PATCH In review Thomas Huth 4 participants 10 messages Jan 16, 2025

lore
Share & tools
lore
#0 lore raw

cover pc-bios/s390-ccw: Fix problems related to network booting

The boot can currently fail after the s390-ccw bios used a network device since we do not properly shut down the device afterwards, so that incoming network packets can corrupt the memory. We have to make sure to put the virtio-net device into a sane state again afterwards.

The third patch is unrelated, but I spotted this rather cosmetic problem while working on the code in netmain.c, so I included it here, too.

Thomas Huth (3):
pc-bios/s390-ccw/virtio: Add a function to reset a virtio device pc-bios/s390-ccw: Fix boot problem with virtio-net devices pc-bios/s390-ccw/netmain: Fix error messages with regards to the TFTP
server

 pc-bios/s390-ccw/virtio.h     |  2 ++
 pc-bios/s390-ccw/netmain.c    | 52 +++++++++++++++++++++--------------
 pc-bios/s390-ccw/virtio-net.c |  5 ++++
 pc-bios/s390-ccw/virtio.c     |  7 ++++-
 4 files changed, 44 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/3 pc-bios/s390-ccw/virtio: Add a function to reset a virtio device

To be able to properly silence a virtio device after using it, we need a global function to reset the device.

---
 pc-bios/s390-ccw/virtio.h | 1 +
 pc-bios/s390-ccw/virtio.c | 7 ++++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
pc-bios/s390-ccw/virtio.h +1 -0
index 9faf3986b1..f13fa6f5fe 100644
@@ -274,6 +274,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags);
 int vr_poll(VRing *vr);
 int vring_wait_reply(void);
 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
+int virtio_reset(VDev *vdev);
 int virtio_setup_ccw(VDev *vdev);
 
 int virtio_net_init(void *mac_addr);
pc-bios/s390-ccw/virtio.c +6 -1
index 8b5a370bb3..cd6c99c7e3 100644
@@ -217,6 +217,11 @@ int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
     return 0;
 }
 
+int virtio_reset(VDev *vdev)
+{
+    return run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
+}
+
 int virtio_setup_ccw(VDev *vdev)
 {
     int i, cfg_size = 0;
@@ -235,7 +240,7 @@ int virtio_setup_ccw(VDev *vdev)
     vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
     vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
 
-    run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
+    virtio_reset(vdev);
 
     status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
     if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
Signature · 1 line
2.47.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] pc-bios/s390-ccw/virtio: Add a function to reset a virtio device

Quoted 46 lines · Thomas Huth

To be able to properly silence a virtio device after using it, we need a global function to reset the device.

---
  pc-bios/s390-ccw/virtio.h | 1 +
  pc-bios/s390-ccw/virtio.c | 7 ++++++-
  2 files changed, 7 insertions(+), 1 deletion(-)
pc-bios/s390-ccw/virtio.h +1 -0
index 9faf3986b1..f13fa6f5fe 100644
@@ -274,6 +274,7 @@ void vring_send_buf(VRing *vr, void *p, int len, int flags);
  int vr_poll(VRing *vr);
  int vring_wait_reply(void);
  int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
+int virtio_reset(VDev *vdev);
  int virtio_setup_ccw(VDev *vdev);
 
  int virtio_net_init(void *mac_addr);
pc-bios/s390-ccw/virtio.c +6 -1
index 8b5a370bb3..cd6c99c7e3 100644
@@ -217,6 +217,11 @@ int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
      return 0;
  }
 
+int virtio_reset(VDev *vdev)
+{
+    return run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
+}
+
  int virtio_setup_ccw(VDev *vdev)
  {
      int i, cfg_size = 0;
@@ -235,7 +240,7 @@ int virtio_setup_ccw(VDev *vdev)
      vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
      vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
 
-    run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
+    virtio_reset(vdev);
 
      status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
      if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
#3 lore raw

PATCH 2/3 pc-bios/s390-ccw: Fix boot problem with virtio-net devices

In reply to Thomas Huth

When we are trying to boot from virtio-net devices, the s390-ccw bios currently leaves the virtio-net device enabled after using it. That means that the receiving virt queues will continue to happily write incoming network packets into memory. This can corrupt data of the following boot process. For example, if you set up a second guest on a virtual network and create a lot of broadcast traffic there, e.g. with:

ping -i 0.02 -s 1400 -b 192.168.1.255

and then you try to boot a guest with two boot devices, a network device first (which should not be bootable) and e.g. a bootable SCSI CD second, then this guest will fail to load the kernel from the CD image:

 $ qemu-system-s390x -m 2G -nographic -device virtio-scsi-ccw \
    -netdev tap,id=net0 -device virtio-net-ccw,netdev=net0,bootindex=1 \
    -drive if=none,file=test.iso,format=raw,id=cd1 \
    -device scsi-cd,drive=cd1,bootindex=2
 LOADPARM=[        ]

Network boot device detected
Network boot starting...
Using MAC address: 52:54:00:12:34:56
Requesting information via DHCP: done
Using IPv4 address: 192.168.1.76
Using TFTP server: 192.168.1.1
Trying pxelinux.cfg files...
TFTP error: ICMP ERROR "port unreachable"
Receiving data: 0 KBytes
Repeating TFTP read request...
TFTP error: ICMP ERROR "port unreachable"
Failed to load OS from network.
Failed to IPL from this network!
LOADPARM=[ ]

Using virtio-scsi.

! virtio-scsi:setup:inquiry: response VS RESP=ff !
ERROR: No suitable device for IPL. Halting...

We really have to shut up the virtio-net devices after we're not using it anymore. The easiest way to do this is to simply reset the device, so let's do that now.

---
 pc-bios/s390-ccw/virtio.h     |  1 +
 pc-bios/s390-ccw/netmain.c    | 33 +++++++++++++++++++++++----------
 pc-bios/s390-ccw/virtio-net.c |  5 +++++
 3 files changed, 29 insertions(+), 10 deletions(-)
pc-bios/s390-ccw/virtio.h +1 -0
index f13fa6f5fe..5c5e808a50 100644
@@ -278,5 +278,6 @@ int virtio_reset(VDev *vdev);
 int virtio_setup_ccw(VDev *vdev);
 
 int virtio_net_init(void *mac_addr);
+void virtio_net_deinit(void);
 
 #endif /* VIRTIO_H */
pc-bios/s390-ccw/netmain.c +23 -10
index e46e470db4..335ea9b63e 100644
@@ -153,19 +153,10 @@ static int tftp_load(filename_ip_t *fnip, void *buffer, int len)
     return rc;
 }
 
-static int net_init(filename_ip_t *fn_ip)
+static int net_init_ip(filename_ip_t *fn_ip)
 {
     int rc;
 
-    memset(fn_ip, 0, sizeof(filename_ip_t));
-
-    rc = virtio_net_init(mac);
-    if (rc < 0) {
-        puts("Could not initialize network device");
-        return -101;
-    }
-    fn_ip->fd = rc;
-
     printf("  Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
            mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
 
@@ -221,11 +212,33 @@ static int net_init(filename_ip_t *fn_ip)
     return rc;
 }
 
+static int net_init(filename_ip_t *fn_ip)
+{
+    int rc;
+
+    memset(fn_ip, 0, sizeof(filename_ip_t));
+
+    rc = virtio_net_init(mac);
+    if (rc < 0) {
+        puts("Could not initialize network device");
+        return -101;
+    }
+    fn_ip->fd = rc;
+
+    rc = net_init_ip(fn_ip);
+    if (rc < 0) {
+        virtio_net_deinit();
+    }
+
+    return rc;
+}
+
 static void net_release(filename_ip_t *fn_ip)
 {
     if (fn_ip->ip_version == 4) {
         dhcp_send_release(fn_ip->fd);
     }
+    virtio_net_deinit();
 }
 
 /**
pc-bios/s390-ccw/virtio-net.c +5 -0
index 578c89d0c5..301445bf97 100644
@@ -140,3 +140,8 @@ int recv(int fd, void *buf, int maxlen, int flags)
 
     return len;
 }
+
+void virtio_net_deinit(void)
+{
+    virtio_reset(virtio_get_device());
+}
Signature · 1 line
2.47.1
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] pc-bios/s390-ccw: Fix boot problem with virtio-net devices

Quoted 135 lines · Thomas Huth

When we are trying to boot from virtio-net devices, the s390-ccw bios currently leaves the virtio-net device enabled after using it. That means that the receiving virt queues will continue to happily write incoming network packets into memory. This can corrupt data of the following boot process. For example, if you set up a second guest on a virtual network and create a lot of broadcast traffic there, e.g. with:

ping -i 0.02 -s 1400 -b 192.168.1.255

and then you try to boot a guest with two boot devices, a network device first (which should not be bootable) and e.g. a bootable SCSI CD second, then this guest will fail to load the kernel from the CD image:

  $ qemu-system-s390x -m 2G -nographic -device virtio-scsi-ccw \
     -netdev tap,id=net0 -device virtio-net-ccw,netdev=net0,bootindex=1 \
     -drive if=none,file=test.iso,format=raw,id=cd1 \
     -device scsi-cd,drive=cd1,bootindex=2
  LOADPARM=[        ]

Network boot device detected
Network boot starting...

    Using MAC address: 52:54:00:12:34:56
    Requesting information via DHCP: done
    Using IPv4 address: 192.168.1.76
    Using TFTP server: 192.168.1.1

Trying pxelinux.cfg files...
TFTP error: ICMP ERROR "port unreachable"
Receiving data: 0 KBytes
Repeating TFTP read request...
TFTP error: ICMP ERROR "port unreachable"
Failed to load OS from network.
Failed to IPL from this network!
LOADPARM=[ ]

Using virtio-scsi.

! virtio-scsi:setup:inquiry: response VS RESP=ff !
ERROR: No suitable device for IPL. Halting...

We really have to shut up the virtio-net devices after we're not using it anymore. The easiest way to do this is to simply reset the device, so let's do that now.

---
  pc-bios/s390-ccw/virtio.h     |  1 +
  pc-bios/s390-ccw/netmain.c    | 33 +++++++++++++++++++++++----------
  pc-bios/s390-ccw/virtio-net.c |  5 +++++
  3 files changed, 29 insertions(+), 10 deletions(-)
pc-bios/s390-ccw/virtio.h +1 -0
index f13fa6f5fe..5c5e808a50 100644
@@ -278,5 +278,6 @@ int virtio_reset(VDev *vdev);
  int virtio_setup_ccw(VDev *vdev);
 
  int virtio_net_init(void *mac_addr);
+void virtio_net_deinit(void);
 
  #endif /* VIRTIO_H */
pc-bios/s390-ccw/netmain.c +23 -10
index e46e470db4..335ea9b63e 100644
@@ -153,19 +153,10 @@ static int tftp_load(filename_ip_t *fnip, void *buffer, int len)
      return rc;
  }
 
-static int net_init(filename_ip_t *fn_ip)
+static int net_init_ip(filename_ip_t *fn_ip)
  {
      int rc;
 
-    memset(fn_ip, 0, sizeof(filename_ip_t));
-
-    rc = virtio_net_init(mac);
-    if (rc < 0) {
-        puts("Could not initialize network device");
-        return -101;
-    }
-    fn_ip->fd = rc;
-
      printf("  Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
 
@@ -221,11 +212,33 @@ static int net_init(filename_ip_t *fn_ip)
      return rc;
  }
 
+static int net_init(filename_ip_t *fn_ip)
+{
+    int rc;
+
+    memset(fn_ip, 0, sizeof(filename_ip_t));
+
+    rc = virtio_net_init(mac);
+    if (rc < 0) {
+        puts("Could not initialize network device");
+        return -101;
+    }
+    fn_ip->fd = rc;
+
+    rc = net_init_ip(fn_ip);
+    if (rc < 0) {
+        virtio_net_deinit();
+    }
+
+    return rc;
+}
+
  static void net_release(filename_ip_t *fn_ip)
  {
      if (fn_ip->ip_version == 4) {
          dhcp_send_release(fn_ip->fd);
      }
+    virtio_net_deinit();
  }
 
  /**
pc-bios/s390-ccw/virtio-net.c +5 -0
index 578c89d0c5..301445bf97 100644
@@ -140,3 +140,8 @@ int recv(int fd, void *buf, int maxlen, int flags)
 
      return len;
  }
+
+void virtio_net_deinit(void)
+{
+    virtio_reset(virtio_get_device());
+}
#5 lore raw

Re: [PATCH 2/3] pc-bios/s390-ccw: Fix boot problem with virtio-net devices

In reply to Thomas Huth
Quoted 92 lines · Thomas Huth

When we are trying to boot from virtio-net devices, the s390-ccw bios currently leaves the virtio-net device enabled after using it. That means that the receiving virt queues will continue to happily write incoming network packets into memory. This can corrupt data of the following boot process. For example, if you set up a second guest on a virtual network and create a lot of broadcast traffic there, e.g. with:

ping -i 0.02 -s 1400 -b 192.168.1.255

and then you try to boot a guest with two boot devices, a network device first (which should not be bootable) and e.g. a bootable SCSI CD second, then this guest will fail to load the kernel from the CD image:

  $ qemu-system-s390x -m 2G -nographic -device virtio-scsi-ccw \
     -netdev tap,id=net0 -device virtio-net-ccw,netdev=net0,bootindex=1 \
     -drive if=none,file=test.iso,format=raw,id=cd1 \
     -device scsi-cd,drive=cd1,bootindex=2
  LOADPARM=[        ]

Network boot device detected
Network boot starting...

    Using MAC address: 52:54:00:12:34:56
    Requesting information via DHCP: done
    Using IPv4 address: 192.168.1.76
    Using TFTP server: 192.168.1.1

Trying pxelinux.cfg files...
TFTP error: ICMP ERROR "port unreachable"
Receiving data: 0 KBytes
Repeating TFTP read request...
TFTP error: ICMP ERROR "port unreachable"
Failed to load OS from network.
Failed to IPL from this network!
LOADPARM=[ ]

Using virtio-scsi.

! virtio-scsi:setup:inquiry: response VS RESP=ff !
ERROR: No suitable device for IPL. Halting...

We really have to shut up the virtio-net devices after we're not using it anymore. The easiest way to do this is to simply reset the device, so let's do that now.

---
  pc-bios/s390-ccw/virtio.h     |  1 +
  pc-bios/s390-ccw/netmain.c    | 33 +++++++++++++++++++++++----------
  pc-bios/s390-ccw/virtio-net.c |  5 +++++
  3 files changed, 29 insertions(+), 10 deletions(-)
pc-bios/s390-ccw/netmain.c +11 -10
index e46e470db4..335ea9b63e 100644
@@ -153,19 +153,10 @@ static int tftp_load(filename_ip_t *fnip, void *buffer, int len)
      return rc;
  }
 
-static int net_init(filename_ip_t *fn_ip)
+static int net_init_ip(filename_ip_t *fn_ip)
  {
      int rc;
 
-    memset(fn_ip, 0, sizeof(filename_ip_t));
-
-    rc = virtio_net_init(mac);
-    if (rc < 0) {
-        puts("Could not initialize network device");
-        return -101;
-    }
-    fn_ip->fd = rc;
-
      printf("  Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
 
@@ -221,11 +212,33 @@ static int net_init(filename_ip_t *fn_ip)
      return rc;
  }
 
+static int net_init(filename_ip_t *fn_ip)
+{
+    int rc;
+
+    memset(fn_ip, 0, sizeof(filename_ip_t));
+
+    rc = virtio_net_init(mac);
+    if (rc < 0) {
+        puts("Could not initialize network device");
+        return -101;

pc-bios/s390-ccw/netmain.c static int net_init(filename_ip_t *fn_ip) · line 234

Quoted 10 lines
pc-bios/s390-ccw/netmain.c +10 -0
+    }
+    fn_ip->fd = rc;
+
+    rc = net_init_ip(fn_ip);
+    if (rc < 0) {
+        virtio_net_deinit();
+    }
+
+    return rc;
+}

Optionally extract net_init_ip() in a preliminary patch for clarity to see where virtio_net_deinit() is added.

#6 lore raw

PATCH 3/3 pc-bios/s390-ccw/netmain: Fix error messages with regards to the TFTP server

In reply to Thomas Huth

The code in net_init_ip() currently bails out early if "rc" is less than 0, so the if-statements that check for negative "rc" codes to print out some specific error messages with regards to the TFTP server are never reached. Move them earlier to bring that dead code back to life.

---
 pc-bios/s390-ccw/netmain.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
pc-bios/s390-ccw/netmain.c +8 -11
index 335ea9b63e..719a547ada 100644
@@ -168,6 +168,14 @@ static int net_init_ip(filename_ip_t *fn_ip)
         if (fn_ip->ip_version == 4) {
             set_ipv4_address(fn_ip->own_ip);
         }
+    } else if (rc == -2) {
+        printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
+               (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
+               (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
+        return -102;
+    } else if (rc == -4 || rc == -3) {
+        puts("Can't obtain TFTP server IP address");
+        return -107;
     } else {
         puts("Could not get IP address");
         return -101;
@@ -183,17 +191,6 @@ static int net_init_ip(filename_ip_t *fn_ip)
         printf("  Using IPv6 address: %s\n", ip6_str);
     }
 
-    if (rc == -2) {
-        printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
-               (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
-               (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
-        return -102;
-    }
-    if (rc == -4 || rc == -3) {
-        puts("Can't obtain TFTP server IP address");
-        return -107;
-    }
-
     printf("  Using TFTP server: ");
     if (fn_ip->ip_version == 4) {
         printf("%d.%d.%d.%d\n",
Signature · 1 line
2.47.1
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] pc-bios/s390-ccw/netmain: Fix error messages with regards to the TFTP server

Quoted 48 lines · Thomas Huth

The code in net_init_ip() currently bails out early if "rc" is less than 0, so the if-statements that check for negative "rc" codes to print out some specific error messages with regards to the TFTP server are never reached. Move them earlier to bring that dead code back to life.

---
  pc-bios/s390-ccw/netmain.c | 19 ++++++++-----------
  1 file changed, 8 insertions(+), 11 deletions(-)
pc-bios/s390-ccw/netmain.c +8 -11
index 335ea9b63e..719a547ada 100644
@@ -168,6 +168,14 @@ static int net_init_ip(filename_ip_t *fn_ip)
          if (fn_ip->ip_version == 4) {
              set_ipv4_address(fn_ip->own_ip);
          }
+    } else if (rc == -2) {
+        printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
+               (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
+               (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
+        return -102;
+    } else if (rc == -4 || rc == -3) {
+        puts("Can't obtain TFTP server IP address");
+        return -107;
      } else {
          puts("Could not get IP address");
          return -101;
@@ -183,17 +191,6 @@ static int net_init_ip(filename_ip_t *fn_ip)
          printf("  Using IPv6 address: %s\n", ip6_str);
      }
 
-    if (rc == -2) {
-        printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
-               (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
-               (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
-        return -102;
-    }
-    if (rc == -4 || rc == -3) {
-        puts("Can't obtain TFTP server IP address");
-        return -107;
-    }
-
      printf("  Using TFTP server: ");
      if (fn_ip->ip_version == 4) {
          printf("%d.%d.%d.%d\n",
Tested-by on the v1 series
#8 lore raw
In reply to Thomas Huth

Thanks Thomas,

Looks good to me. I've tested and verified that the problem is fixed.

Quoted 21 lines · Thomas Huth

The boot can currently fail after the s390-ccw bios used a network device since we do not properly shut down the device afterwards, so that incoming network packets can corrupt the memory. We have to make sure to put the virtio-net device into a sane state again afterwards.

The third patch is unrelated, but I spotted this rather cosmetic problem while working on the code in netmain.c, so I included it here, too.

Thomas Huth (3):
pc-bios/s390-ccw/virtio: Add a function to reset a virtio device pc-bios/s390-ccw: Fix boot problem with virtio-net devices pc-bios/s390-ccw/netmain: Fix error messages with regards to the TFTP
server

  pc-bios/s390-ccw/virtio.h     |  2 ++
  pc-bios/s390-ccw/netmain.c    | 52 +++++++++++++++++++++--------------
  pc-bios/s390-ccw/virtio-net.c |  5 ++++
  pc-bios/s390-ccw/virtio.c     |  7 ++++-
  4 files changed, 44 insertions(+), 22 deletions(-)
Reviewed-by on the v1 series
#9 lore raw
In reply to Thomas Huth
Quoted 9 lines · Thomas Huth

The boot can currently fail after the s390-ccw bios used a network device since we do not properly shut down the device afterwards, so that incoming network packets can corrupt the memory. We have to make sure to put the virtio-net device into a sane state again afterwards.

The third patch is unrelated, but I spotted this rather cosmetic problem while working on the code in netmain.c, so I included it here, too.

Quoted 5 lines

Thomas Huth (3):
pc-bios/s390-ccw/virtio: Add a function to reset a virtio device pc-bios/s390-ccw: Fix boot problem with virtio-net devices pc-bios/s390-ccw/netmain: Fix error messages with regards to the TFTP
server

Looks good to me; Thank you, Thomas! For the series:

Quoted 7 lines
 pc-bios/s390-ccw/virtio.h     |  2 ++
 pc-bios/s390-ccw/netmain.c    | 52 +++++++++++++++++++++--------------
 pc-bios/s390-ccw/virtio-net.c |  5 ++++
 pc-bios/s390-ccw/virtio.c     |  7 ++++-
 4 files changed, 44 insertions(+), 22 deletions(-)