Skip to contentSkip to conversation

xen: fix usage of ENODATA

PATCH Superseded Roger Pau Monne 4 participants 7 messages Apr 15, 2014 – Apr 16, 2014 3 messages not in the archive Networking

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 xen: fix usage of ENODATA

ENODATA doesn't exist on FreeBSD, so ENODATA errors returned by the hypervisor are translated to ENOENT.

Also, the error code is returned in errno if the call returns -1, so compare the error code with the value in errno instead of the value returned by the function.

Cc Stefano Stabellini <[email protected]>
Cc Anthony Perard <[email protected]>
---
 xen-all.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
xen-all.c +6 -2
index 0b4d934..4026b7b 100644
@@ -499,11 +499,15 @@ static void xen_sync_dirty_bitmap(XenIOState *state,
                                  start_addr >> TARGET_PAGE_BITS, npages,
                                  bitmap);
     if (rc < 0) {
-        if (rc != -ENODATA) {
+#ifdef ENODATA
+        if (errno == ENODATA) {
+#else
+        if (errno == ENOENT) {
+#endif
             memory_region_set_dirty(framebuffer, 0, size);
             DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
                     ", 0x" TARGET_FMT_plx "): %s\n",
-                    start_addr, start_addr + size, strerror(-rc));
+                    start_addr, start_addr + size, strerror(errno));
         }
         return;
     }
Signature · 7 lines
1.7.7.5 (Apple Git-26)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#2 lore raw

PATCH 2/2 tap-bsd: implement a FreeBSD only version of tap_open

In reply to a message not in the archive

The current behaviour of tap_open for BSD systems differ greatly from it's Linux counterpart. Since FreeBSD supports interface renaming and tap device cloning by opening /dev/tap, implement a FreeBSD specific version of tap_open that behaves like it's Linux counterpart.

This is specially important for toolstacks that use Qemu (like Xen libxl), in order to have a unified behaviour across suported platforms.

Cc Stefano Stabellini <[email protected]>
Cc Anthony Liguori <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
---
 net/tap-bsd.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 1 deletions(-)
net/tap-bsd.c +69 -1
index 90f8a02..bf91bd0 100644
@@ -27,12 +27,13 @@
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 
-#ifdef __NetBSD__
+#if defined(__NetBSD__) || defined(__FreeBSD__)
 #include <sys/ioctl.h>
 #include <net/if.h>
 #include <net/if_tap.h>
 #endif
 
+#ifndef __FreeBSD__
 int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
              int vnet_hdr_required, int mq_required)
 {
@@ -108,6 +109,73 @@ int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
     return fd;
 }
 
+#else /* __FreeBSD__ */
+
+#define PATH_NET_TAP "/dev/tap"
+
+int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
+             int vnet_hdr_required, int mq_required)
+{
+    int fd, s, ret;
+    struct ifreq ifr;
+
+    TFR(fd = open(PATH_NET_TAP, O_RDWR));
+    if (fd < 0) {
+        error_report("could not open %s: %s", PATH_NET_TAP, strerror(errno));
+        return -1;
+    }
+
+    memset(&ifr, 0, sizeof(ifr));
+
+    ret = ioctl(fd, TAPGIFNAME, (void *)&ifr);
+    if (ret < 0) {
+        error_report("could not get tap interface name");
+        goto error;
+    }
+
+    if (ifname[0] != '\0') {
+        /* User requested the interface to have a specific name */
+        s = socket(AF_LOCAL, SOCK_DGRAM, 0);
+        if (s < 0) {
+            error_report("could not open socket to set interface name");
+            goto error;
+        }
+        ifr.ifr_data = ifname;
+        ret = ioctl(s, SIOCSIFNAME, (void *)&ifr);
+        close(s);
+        if (ret < 0) {
+            error_report("could not set tap interface name");
+            goto error;
+        }
+    } else {
+        pstrcpy(ifname, ifname_size, ifr.ifr_name);
+    }
+
+    if (*vnet_hdr) {
+        /* BSD doesn't have IFF_VNET_HDR */
+        *vnet_hdr = 0;
+
+        if (vnet_hdr_required && !*vnet_hdr) {
+            error_report("vnet_hdr=1 requested, but no kernel "
+                         "support for IFF_VNET_HDR available");
+            goto error;
+        }
+    }
+    if (mq_required) {
+        error_report("mq_required requested, but not kernel support"
+                     "for IFF_MULTI_QUEUE available");
+        goto error;
+    }
+
+    fcntl(fd, F_SETFL, O_NONBLOCK);
+    return fd;
+
+error:
+    close(fd);
+    return -1;
+}
+#endif /* __FreeBSD__ */
+
 int tap_set_sndbuf(int fd, const NetdevTapOptions *tap)
 {
     return 0;
Signature · 7 lines
1.7.7.5 (Apple Git-26)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 20 lines · Roger Pau Monne

ENODATA doesn't exist on FreeBSD, so ENODATA errors returned by the hypervisor are translated to ENOENT.

Also, the error code is returned in errno if the call returns -1, so compare the error code with the value in errno instead of the value returned by the function.

Cc Stefano Stabellini <[email protected]>
Cc Anthony Perard <[email protected]>
---
 xen-all.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
xen-all.c +0 -0
index 0b4d934..4026b7b 100644

xen-all.c static void xen_sync_dirty_bitmap(XenIOState *state, · line 506

Quoted 9 lines
xen-all.c +5 -1
@@ -499,11 +499,15 @@ static void xen_sync_dirty_bitmap(XenIOState *state,
                                  start_addr >> TARGET_PAGE_BITS, npages,
                                  bitmap);
     if (rc < 0) {
-        if (rc != -ENODATA) {
+#ifdef ENODATA
+        if (errno == ENODATA) {
+#else
+        if (errno == ENOENT) {
+#endif

This does not look good to me.
I suggest this:

#ifndef ENODATA
#define ENODATA ENOENT
#endif

Christoph

xen-all.c static void xen_sync_dirty_bitmap(XenIOState *state, · line 513

Quoted 9 lines
xen-all.c +1 -1
             memory_region_set_dirty(framebuffer, 0, size);
             DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
                     ", 0x" TARGET_FMT_plx "): %s\n",
-                    start_addr, start_addr + size, strerror(-rc));
+                    start_addr, start_addr + size, strerror(errno));
         }
         return;
     }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw
Quoted 34 lines · Christoph
Quoted 20 lines · Roger Pau Monne

ENODATA doesn't exist on FreeBSD, so ENODATA errors returned by the hypervisor are translated to ENOENT.

Also, the error code is returned in errno if the call returns -1, so compare the error code with the value in errno instead of the value returned by the function.

Cc Stefano Stabellini <[email protected]>
Cc Anthony Perard <[email protected]>
---
 xen-all.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
xen-all.c +0 -0
index 0b4d934..4026b7b 100644

xen-all.c static void xen_sync_dirty_bitmap(XenIOState *state, · line 506

Quoted 9 lines
xen-all.c +5 -1
@@ -499,11 +499,15 @@ static void xen_sync_dirty_bitmap(XenIOState *state,
                                  start_addr >> TARGET_PAGE_BITS, npages,
                                  bitmap);
     if (rc < 0) {
-        if (rc != -ENODATA) {
+#ifdef ENODATA
+        if (errno == ENODATA) {
+#else
+        if (errno == ENOENT) {
+#endif

This does not look good to me.
I suggest this:

Quoted 3 lines

#ifndef ENODATA
#define ENODATA ENOENT
#endif

Hello Christoph,

Thanks for the review, I slightly prefer my original proposal over what you suggest, I think it's more clear that there are different error codes, while your suggestion simply masks the fact than FreeBSD doesn't have ENODATA. Anyway, I wouldn't mind changing it if the maintainer prefers your approach.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw
In reply to a message not in the archive
Quoted 6 lines · Roger Pau Monne

ENODATA doesn't exist on FreeBSD, so ENODATA errors returned by the hypervisor are translated to ENOENT.

Also, the error code is returned in errno if the call returns -1, so compare the error code with the value in errno instead of the value returned by the function.

But in xenctrl.h, the comment on the prototype of the function says that it return the error code, there is nothing about errno been updated. So I think rc should still be check for the error code instead of errno.

Quoted 33 lines
Cc Stefano Stabellini <[email protected]>
Cc Anthony Perard <[email protected]>
---
 xen-all.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
xen-all.c +6 -2
index 0b4d934..4026b7b 100644
@@ -499,11 +499,15 @@ static void xen_sync_dirty_bitmap(XenIOState *state,
                                  start_addr >> TARGET_PAGE_BITS, npages,
                                  bitmap);
     if (rc < 0) {
-        if (rc != -ENODATA) {
+#ifdef ENODATA
+        if (errno == ENODATA) {
+#else
+        if (errno == ENOENT) {
+#endif
             memory_region_set_dirty(framebuffer, 0, size);
             DPRINTF("xen: track_dirty_vram failed (0x" TARGET_FMT_plx
                     ", 0x" TARGET_FMT_plx "): %s\n",
-                    start_addr, start_addr + size, strerror(-rc));
+                    start_addr, start_addr + size, strerror(errno));
         }
         return;
     }
Signature · 1 line
1.7.7.5 (Apple Git-26)
Signature · 1 line
Anthony PERARD
#7 lore raw
Quoted 11 lines · Anthony PERARD
Quoted 6 lines · Roger Pau Monne

ENODATA doesn't exist on FreeBSD, so ENODATA errors returned by the hypervisor are translated to ENOENT.

Also, the error code is returned in errno if the call returns -1, so compare the error code with the value in errno instead of the value returned by the function.

But in xenctrl.h, the comment on the prototype of the function says that it return the error code, there is nothing about errno been updated. So I think rc should still be check for the error code instead of errno.

Then I think the comment is wrong (or I'm missing something), xc_hvm_track_dirty_vram return code comes from do_xen_hypercall, which is basically a wrapper around ioctl, and according to the ioctl man page (both Linux and FreeBSD versions):

"On error, -1 is returned, and errno is set appropriately."

Roger.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 11 lines · Roger Pau Monné
Quoted 10 lines · Anthony PERARD
Quoted 6 lines · Roger Pau Monne

ENODATA doesn't exist on FreeBSD, so ENODATA errors returned by the hypervisor are translated to ENOENT.

Also, the error code is returned in errno if the call returns -1, so compare the error code with the value in errno instead of the value returned by the function.

But in xenctrl.h, the comment on the prototype of the function says that it return the error code, there is nothing about errno been updated. So I think rc should still be check for the error code instead of errno.

Quoted 8 lines

Then I think the comment is wrong (or I'm missing something), xc_hvm_track_dirty_vram return code comes from do_xen_hypercall, which is basically a wrapper around ioctl, and according to the ioctl man page (both Linux and FreeBSD versions):

"On error, -1 is returned, and errno is set appropriately."

Roger.

The only thing you can safely assume about libxc and error values is that there is no consistency whatsoever (and in some cases a completely inability to determine success or failure from the return value). This is acknowledged as needing to be fixed.

The only way you have of actually working out the error semantics are to follow the function you care about all the way to the ioctl.

~Andrew