Skip to contentSkip to conversation

xc_XXX_build - add parallel routines to support input via buffer rather than files

PATCH In review B Thomas 4 participants 4 messages Jan 16, 2006 – Feb 22, 2006

lore
Share & tools
lore

B Thomas

author
#0 lore raw

PATCH 1/1 xc_XXX_build - add parallel routines to support input via buffer rather than files

I have a need to pass image and ramdisk via buffers, and not files. I added some parallel routines so as not to disturb the existing APIs. This may, or may not, have been the correct choice, but it works. In this patch, you'll find xc_linux_build_mem and xc_vmx_build_mem as the new interfaces, which accept buffer pointer/size arguments rather than filename arguments. The old interfaces remain (both in code and working status). Some internal shuffling was needed to accomodate this, which I've tried to minimize. I've tested most of this, the expection being IA64, which I don't have.

-b

# HG changeset patch
# Node ID 7f9e81f7cbbc31125e9cbf7012d59ecd77f683c3
# Parent d929135007adb8b7cc5ac0aa8b8172f2468d4bc5
# Add support for buffer-input (as opposed to file names) for xc_linux_build

# add xc_vmx_build.  In order to maintain the existing interfaces, add
# new routines called xc_linux_build_mem and xc_vmx_build_mem.  This was one
# choice. Another was to change the existing interfaces.
#
# The buffer-based routines may take compressed and uncompressed buffers,
# and use zlib to gunzip any buffer detected as being gzip compressed.
# This matches the filename-based behavior.
#
# While here, add some more defensive code to guard against NULL input
# arguments to a few routines.
#
# Signed-off-by: Ben Thomas <[email protected]>
tools/libxc/xc_linux_build.c +301 -43
diff -r d929135007ad -r 7f9e81f7cbbc tools/libxc/xc_linux_build.c
@@ -282,8 +282,9 @@
 
 static int setup_guest(int xc_handle,
                        uint32_t dom,
-                       char *image, unsigned long image_size,
+                       const unsigned char *image, unsigned long image_size,
                        gzFile initrd_gfd, unsigned long initrd_len,
+                       unsigned char *ramdisk_buffer, unsigned long ramdisk_size,
                        unsigned long nr_pages,
                        unsigned long *pvsi, unsigned long *pvke,
                        unsigned long *pvss, vcpu_guest_context_t *ctxt,
@@ -350,20 +351,79 @@
     /* Load the initial ramdisk image. */
     if ( initrd_len != 0 )
     {
-        for ( i = (vinitrd_start - dsi.v_start);
-              i < (vinitrd_end - dsi.v_start); i += PAGE_SIZE )
-        {
-            char page[PAGE_SIZE];
-            if ( gzread(initrd_gfd, page, PAGE_SIZE) == -1 )
+        // If there's a file for input, use gzread to get the data
+        if (initrd_gfd != NULL)
+        {
+            for ( i = (vinitrd_start - dsi.v_start); 
+                  i < (vinitrd_end - dsi.v_start); i += PAGE_SIZE )
+                {
+                    char page[PAGE_SIZE];
+                    if ( gzread(initrd_gfd, page, PAGE_SIZE) == -1 )
+                        {
+                            PERROR("Error reading initrd image, could not");
+                            goto error_out;
+                        }
+                    xc_copy_to_domain_page(xc_handle, dom,
+                                           page_array[i>>PAGE_SHIFT], page);
+                }
+        }
+
+        // Else, if there's a buffer, loop over it with inflate
+
+        else if (ramdisk_size > 0)
+        {
+            z_stream    zStream;
+            unsigned char page[PAGE_SIZE];
+            int         sts;
+            int         remain;
+
+            bzero(&zStream, sizeof(zStream));
+            zStream.next_in = ramdisk_buffer;
+            zStream.avail_in = ramdisk_size;
+            zStream.next_out = page;
+            zStream.avail_out = sizeof(page);
+            sts = inflateInit2(&zStream, (MAX_WBITS+32));       // +32 means "handle gzip"
+            if (sts != Z_OK)
             {
-                PERROR("Error reading initrd image, could not");
+                ERROR("inflateInit failed, sts %d\n", sts);
                 goto error_out;
             }
-            xc_copy_to_domain_page(xc_handle, dom,
-                                   page_array[i>>PAGE_SHIFT], page);
-        }
-    }
-
+
+            // Pages are not contiguous, so do the inflation a page at a time
+
+            for ( i = (vinitrd_start - dsi.v_start); 
+                  i < (vinitrd_end - dsi.v_start); i += PAGE_SIZE )
+                {
+                    sts = inflate(&zStream, Z_SYNC_FLUSH);
+                    if (sts < 0)
+                    {
+                        ERROR("Inflate failed, sts %d", sts);
+                        goto error_out;
+                    }
+                    if (sts == Z_STREAM_END)
+                    {
+                        remain = sizeof(page) - zStream.avail_out;
+                        bzero(&page[zStream.avail_out], remain);
+                        xc_copy_to_domain_page(xc_handle, dom,
+                                               page_array[i>>PAGE_SHIFT], page);
+                        break;
+                    }
+                    if (sts == Z_OK)
+                    {
+                        xc_copy_to_domain_page(xc_handle, dom,
+                                               page_array[i>>PAGE_SHIFT], page);
+                        zStream.next_out = page;
+                        zStream.avail_out = sizeof(page);
+                    }
+                    else
+                    {
+                            ERROR("Inflate error, sts %d", sts);
+                            goto error_out;
+                    }
+                }
+        } else
+            ERROR("initrd requested, but no data available");
+    }
 
     *pvke = dsi.v_kernentry;
 
@@ -419,8 +479,9 @@
 #else /* x86 */
 static int setup_guest(int xc_handle,
                        uint32_t dom,
-                       char *image, unsigned long image_size,
+                       const unsigned char *image, unsigned long image_size,
                        gzFile initrd_gfd, unsigned long initrd_len,
+                       unsigned char *ramdisk_buffer, unsigned long ramdisk_size,
                        unsigned long nr_pages,
                        unsigned long *pvsi, unsigned long *pvke,
                        unsigned long *pvss, vcpu_guest_context_t *ctxt,
@@ -459,13 +520,13 @@
     unsigned long vpt_end;
     unsigned long v_end;
 
-    rc = probeimageformat(image, image_size, &load_funcs);
+    rc = probeimageformat((char *)image, (unsigned long)image_size, &load_funcs);
     if ( rc != 0 )
         goto error_out;
 
     memset(&dsi, 0, sizeof(struct domain_setup_info));
 
-    rc = (load_funcs.parseimage)(image, image_size, &dsi);
+    rc = (load_funcs.parseimage)((char *)image, (unsigned long)image_size, &dsi);
     if ( rc != 0 )
         goto error_out;
 
@@ -570,24 +631,84 @@
         goto error_out;
     }
 
-    (load_funcs.loadimage)(image, image_size, xc_handle, dom, page_array,
+    (load_funcs.loadimage)((char *)image, image_size, xc_handle, dom, page_array,
                            &dsi);
 
     /* Load the initial ramdisk image. */
     if ( initrd_len != 0 )
     {
-        for ( i = (vinitrd_start - dsi.v_start); 
-              i < (vinitrd_end - dsi.v_start); i += PAGE_SIZE )
-        {
-            char page[PAGE_SIZE];
-            if ( gzread(initrd_gfd, page, PAGE_SIZE) == -1 )
+        // If there's a file for input, use gzread to get the data
+        if (initrd_gfd != NULL)
+        {
+            for ( i = (vinitrd_start - dsi.v_start); 
+                  i < (vinitrd_end - dsi.v_start); i += PAGE_SIZE )
+                {
+                    char page[PAGE_SIZE];
+                    if ( gzread(initrd_gfd, page, PAGE_SIZE) == -1 )
+                        {
+                            PERROR("Error reading initrd image, could not");
+                            goto error_out;
+                        }
+                    xc_copy_to_domain_page(xc_handle, dom,
+                                           page_array[i>>PAGE_SHIFT], page);
+                }
+        }
+
+        // Else, if there's a buffer, loop over it with inflate
+
+        else if (ramdisk_size > 0)
+        {
+            z_stream    zStream;
+            unsigned char page[PAGE_SIZE];
+            int         sts;
+            int         remain;
+
+            bzero(&zStream, sizeof(zStream));
+            zStream.next_in = ramdisk_buffer;
+            zStream.avail_in = ramdisk_size;
+            zStream.next_out = page;
+            zStream.avail_out = sizeof(page);
+            sts = inflateInit2(&zStream, (MAX_WBITS+32));
+            if (sts != Z_OK)
             {
-                PERROR("Error reading initrd image, could not");
+                ERROR("inflateInit failed, sts %d\n", sts);
                 goto error_out;
             }
-            xc_copy_to_domain_page(xc_handle, dom,
-                                   page_array[i>>PAGE_SHIFT], page);
-        }
+
+            // Pages are not contiguous, so do the inflation a page at a time
+
+            for ( i = (vinitrd_start - dsi.v_start); 
+                  i < (vinitrd_end - dsi.v_start); i += PAGE_SIZE )
+                {
+                    sts = inflate(&zStream, Z_SYNC_FLUSH);
+                    if (sts < 0)
+                    {
+                        ERROR("Inflate failed, sts %d", sts);
+                        goto error_out;
+                    }
+                    if (sts == Z_STREAM_END)
+                    {                                   // Done, but may have partial buffer
+                        remain = sizeof(page) - zStream.avail_out;
+                        bzero(&page[zStream.avail_out], remain);
+                        xc_copy_to_domain_page(xc_handle, dom,
+                                               page_array[i>>PAGE_SHIFT], page);
+                        break;
+                    }
+                    if (sts == Z_OK)
+                    {
+                        xc_copy_to_domain_page(xc_handle, dom,
+                                               page_array[i>>PAGE_SHIFT], page);
+                        zStream.next_out = page;
+                        zStream.avail_out = sizeof(page);
+                    }
+                    else
+                    {
+                            ERROR("Inflate error, sts %d", sts);
+                            goto error_out;
+                    }
+                }
+        } else
+            ERROR("initrd requested, but no data available");
     }
 
     if ( (mmu = xc_init_mmu_updates(xc_handle, dom)) == NULL )
@@ -733,16 +854,28 @@
 }
 #endif
 
-int xc_linux_build(int xc_handle,
-                   uint32_t domid,
-                   const char *image_name,
-                   const char *ramdisk_name,
-                   const char *cmdline,
-                   unsigned long flags,
-                   unsigned int store_evtchn,
-                   unsigned long *store_mfn,
-                   unsigned int console_evtchn,
-                   unsigned long *console_mfn)
+/* xc_linux_build_internal
+ *
+ * Common routine to create a domain for a para-virtualized Linux.  Always called with
+ * a buffer containing the uncompressed kernel.  The RAMdisk is either a file, or a
+ * buffer with the potentially compressed RAMdisk.  Figure out which, and make it all
+ * happen.
+ *
+ */
+
+static int xc_linux_build_internal(int xc_handle,
+                                   uint32_t domid,
+                                   const unsigned char *image,
+                                   unsigned long image_size,
+                                   const char *ramdisk_name,
+                                   unsigned char *ramdisk_buffer,
+                                   unsigned long ramdisk_size,
+                                   const char *cmdline,
+                                   unsigned long flags,
+                                   unsigned int store_evtchn,
+                                   unsigned long *store_mfn,
+                                   unsigned int console_evtchn,
+                                   unsigned long *console_mfn)
 {
     dom0_op_t launch_op;
     DECLARE_DOM0_OP;
@@ -751,8 +884,7 @@
     int rc, i;
     vcpu_guest_context_t st_ctxt, *ctxt = &st_ctxt;
     unsigned long nr_pages;
-    char         *image = NULL;
-    unsigned long image_size, initrd_size=0;
+    unsigned long initrd_size=0;
     unsigned long vstartinfo_start, vkern_entry, vstack_start;
 
     if ( (nr_pages = get_tot_pages(xc_handle, domid)) < 0 )
@@ -761,9 +893,10 @@
         goto error_out;
     }
 
-    if ( (image = xc_read_kernel_image(image_name, &image_size)) == NULL )
-        goto error_out;
-
+    // The kernel image arrives as an uncompressed buffer at this point
+    // The ramdisk may be either a file name, or a buffer/size pair
+    // Determine which, and handle the processing
+  
     if ( (ramdisk_name != NULL) && (strlen(ramdisk_name) != 0) )
     {
         if ( (initrd_fd = open(ramdisk_name, O_RDONLY)) < 0 )
@@ -779,6 +912,14 @@
             PERROR("Could not allocate decompression state for initrd");
             goto error_out;
         }
+    } else if ( (ramdisk_buffer != NULL) && (ramdisk_size > 16) ) {
+        if ( (ramdisk_buffer[0] == 0x1F) && (ramdisk_buffer[1] == 0x8B) ) {
+            initrd_size = ramdisk_buffer[ramdisk_size-4] +
+                          (256 * (ramdisk_buffer[ramdisk_size-3] +
+                                  (256 * (ramdisk_buffer[ramdisk_size-2] +
+                                          (256 * ramdisk_buffer[ramdisk_size-1])))));
+        } else
+            initrd_size = ramdisk_size;
     }
 
 #ifdef VALGRIND
@@ -813,7 +954,8 @@
     }
 
     if ( setup_guest(xc_handle, domid, image, image_size, 
-                     initrd_gfd, initrd_size, nr_pages, 
+                     initrd_gfd, initrd_size, ramdisk_buffer, ramdisk_size,
+                     nr_pages, 
                      &vstartinfo_start, &vkern_entry,
                      &vstack_start, ctxt, cmdline,
                      op.u.getdomaininfo.shared_info_frame,
@@ -828,7 +970,6 @@
         close(initrd_fd);
     if ( initrd_gfd )
         gzclose(initrd_gfd);
-    free(image);
 
 #ifdef __ia64__
     /* based on new_thread in xen/arch/ia64/domain.c */
@@ -917,8 +1058,125 @@
         gzclose(initrd_gfd);
     else if ( initrd_fd >= 0 )
         close(initrd_fd);
+    return -1;
+}
+
+/* xc_linux_build_mem
+ *
+ * Create a domain for a para-virtualized Linux, using buffers containing the kernel
+ * and, possibly, the ramdisk.  This allows for use in situations where files may
+ * not be readily available.
+ *
+ */
+
+int xc_linux_build_mem(int xc_handle,
+                       uint32_t domid,
+                       unsigned char *image_buffer,
+                       unsigned long image_size,
+                       unsigned char *ramdisk_buffer,
+                       unsigned long ramdisk_size,
+                       const char *cmdline,
+                       unsigned long flags,
+                       unsigned int store_evtchn,
+                       unsigned long *store_mfn,
+                       unsigned int console_evtchn,
+                       unsigned long *console_mfn)
+{
+    int                 sts;
+    z_stream            zStream;
+    unsigned long       outbuf_length;
+    unsigned char       *outBuf;
+
+    // Validate that there is a kernel buffer
+
+    if (image_buffer == NULL) {
+        ERROR("kernel image buffer not present");
+        return EINVAL;
+    }
+
+    // If it's gzipped, inflate it;  otherwise, use as is
+
+    if ( (image_buffer[0] == 0x1F) && (image_buffer[1] == 0x8B) ) {
+        outbuf_length = image_buffer[image_size-4] +
+                      (256 * (image_buffer[image_size-3] +
+                              (256 * (image_buffer[image_size-2] +
+                                      (256 * image_buffer[image_size-1])))));
+        bzero(&zStream, sizeof(zStream));
+        outBuf = malloc(outbuf_length + 16);        // Leave a little extra space
+        if (outBuf == NULL) {
+            ERROR("Error mallocing buffer\n");
+            return ENOMEM;
+        }
+
+        zStream.next_in = image_buffer;
+        zStream.avail_in = image_size;
+        zStream.next_out = outBuf;
+        zStream.avail_out = outbuf_length+16;
+        sts = inflateInit2(&zStream, (MAX_WBITS+32));   // +32 means "handle gzip"
+        if (sts != Z_OK) {
+            ERROR("inflateInit failed, sts %d\n", sts);
+            free(outBuf);
+            return sts;
+        }
+
+        // Inflate in one pass/call
+
+        sts = inflate(&zStream, Z_FINISH);
+        if (sts != Z_STREAM_END) {
+            ERROR("inflate failed, sts %d\n", sts);
+            free(outBuf);
+            return sts;
+        }
+        sts = xc_linux_build_internal(xc_handle, domid, outBuf, outbuf_length,
+                                      NULL, ramdisk_buffer, ramdisk_size,
+                                      cmdline, flags, store_evtchn,
+                                      store_mfn, console_evtchn, console_mfn);
+        free(outBuf);
+    } else {
+        sts = xc_linux_build_internal(xc_handle, domid, image_buffer, image_size,
+                                      NULL, ramdisk_buffer, ramdisk_size,
+                                      cmdline, flags, store_evtchn,
+                                      store_mfn, console_evtchn, console_mfn);
+    }
+
+    return sts;
+}
+
+/* xc_linux_build
+ *
+ * Create a domain for a para-virtualized Linux, using files/filenames
+ *
+ */
+
+int xc_linux_build(int xc_handle,
+                   uint32_t domid,
+                   const char *image_name,
+                   const char *ramdisk_name,
+                   const char *cmdline,
+                   unsigned long flags,
+                   unsigned int store_evtchn,
+                   unsigned long *store_mfn,
+                   unsigned int console_evtchn,
+                   unsigned long *console_mfn)
+{
+    unsigned char *image;
+    int  sts;
+    long image_size;
+
+    // Bring uncompressed image into memory
+
+    if ( (image_name == NULL) ||
+         ((image = (unsigned char *)xc_read_kernel_image(image_name, (unsigned long *)&image_size)) == NULL ))
+        return -1;
+
+    // Then, call internal routine for processing (using ramdisk name, not buffer)
+
+    sts = xc_linux_build_internal(xc_handle, domid, image, image_size,
+                                  ramdisk_name, NULL, 0,
+                                  cmdline, flags, store_evtchn,
+                                  store_mfn, console_evtchn, console_mfn);
     free(image);
-    return -1;
+    return sts;
 }
 
 /*
tools/libxc/xc_vmx_build.c +133 -19

194 lines not on this page · raw message

tools/libxc/xenguest.h +59 -0

75 lines not on this page · raw message

tools/libxc/xg_private.c +11 -2
diff -r d929135007ad -r 7f9e81f7cbbc tools/libxc/xg_private.c
@@ -17,8 +17,12 @@
     char *image = NULL;
     unsigned int bytes;
 
-    if ( filename == NULL )
-        goto out;
+    // Check for non-null input.  We could handle a NULL size, but it's
+    // not clear what value it provides to not know the length on return
+    // Also, just return - we haven't done anything requiring cleanup
+
+    if ( ( filename == NULL ) || (size == NULL) )
+        return NULL;
 
     if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
     {
@@ -83,6 +87,11 @@
     unsigned long *p = page;
     unsigned long long sum=0;
 
+    // Return 0 checksum for NULL input
+
+    if (page == NULL)
+      return 0;
+
     for ( i = 0; i < (PAGE_SIZE/sizeof(unsigned long)); i++ )
         sum += p[i];
 

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

#1 lore raw

I'd just like to add a +1 from me to the concept of an optional memory interface to the building APIs. I implemented something similar for domU kexec support, and if something like this is available in the up-to-date tree, I won't have to update that part of my original patch when we update the sparse tree to 2.6.15 ;-)

One suggestion based on my experience:
the interface to setup_guest would seem to be cleaner if it *always* assumed its taking inflated buffers for the initrd, as well as the kernel. You can just load and inflate the initrd in in the builder function (if necessary!) as we do already for the kernel.

Would anyone object to this? The interface to the rest of xc that you propose sounds like a step in the right direction.

Cheers,
Mark

Quoted 11 lines · B Thomas

I have a need to pass image and ramdisk via buffers, and not files. I added some parallel routines so as not to disturb the existing APIs. This may, or may not, have been the correct choice, but it works. In this patch, you'll find xc_linux_build_mem and xc_vmx_build_mem as the new interfaces, which accept buffer pointer/size arguments rather than filename arguments. The old interfaces remain (both in code and working status). Some internal shuffling was needed to accomodate this, which I've tried to minimize. I've tested most of this, the expection being IA64, which I don't have.

-b

Signature · 4 lines
Dave: Just a question. What use is a unicyle with no seat?  And no pedals!
Mark: To answer a question with a question: What use is a skateboard?
Dave: Skateboards have wheels.
Mark: My wheel has a wheel!
#2 lore raw

Hi Mark,

You had asked about this patch, and I wanted to give you an update. I've submitted it twice, the second time with your suggestions incorporated. I haven't received any direct or indirect feedback or comment at this point. I haven't submitted enough patches at this point to be sure whether this means anything or not. At this point, though, it appears that it's not incorporated into the source tree. On the other hand, other patches I've submitted have just shown up without fanfare at some later point.

I'm not sufficiently familiar with the xen-devel process and practices to know the line between helpful persistence and spamming the list, so at this point I'm leaning towards not resubmitting it yet again.

I welcome any feedback and suggestions,
Thanks,
-b

Quoted 38 lines · Mark Williamson

I'd just like to add a +1 from me to the concept of an optional memory interface to the building APIs. I implemented something similar for domU kexec support, and if something like this is available in the up-to-date tree, I won't have to update that part of my original patch when we update the sparse tree to 2.6.15 ;-)

One suggestion based on my experience:
the interface to setup_guest would seem to be cleaner if it *always* assumed
its taking inflated buffers for the initrd, as well as the kernel. You can
just load and inflate the initrd in in the builder function (if necessary!)
as we do already for the kernel.

Would anyone object to this? The interface to the rest of xc that you propose
sounds like a step in the right direction.

Cheers,
Mark

Quoted 2 lines · B Thomas

I have a need to pass image and ramdisk via buffers, and not files. I added some parallel routines so as not to disturb the existing APIs.

This

Quoted 3 lines

may, or may not, have been the correct choice, but it works. In this patch, you'll find xc_linux_build_mem and xc_vmx_build_mem as the new interfaces, which accept buffer pointer/size arguments rather than

filename

Quoted 2 lines

arguments. The old interfaces remain (both in code and working status). Some internal shuffling was needed to accomodate this, which I've tried

to

Quoted 1 line

minimize. I've tested most of this, the expection being IA64, which I

don't

Quoted 2 lines

have.

Quoted 8 lines
Quoted 1 line

-b

--
Dave: Just a question. What use is a unicyle with no seat? And no pedals!
Mark: To answer a question with a question: What use is a skateboard?
Dave: Skateboards have wheels.
Mark: My wheel has a wheel!

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

#3 lore raw
Quoted 5 lines

You had asked about this patch, and I wanted to give you an update.
I've submitted it twice, the second time with your suggestions incorporated.  I haven't received any direct or indirect feedback or comment at this point.  I haven't submitted enough patches at this point to be sure whether this means anything or not. At this point, though, it appears that it's not incorporated into the source tree. On the other hand, other patches I've submitted have just shown up without fanfare at some later point.

I'm not sufficiently familiar with the xen-devel process and
practices to know the line between helpful persistence and spamming the list, so at this point I'm leaning towards not resubmitting it yet again.

The patch is fairly big and noone's had time to pick it up and review and it and test it. If there are others who want this patch in it would be useful to get confirmation that it works for them, and that existing usages of the domain builder are not regressed.

-- Keir