ia64/xen-unstable

view tools/xfrd/xen_domain.c @ 1921:24ecc060e9d7

bitkeeper revision 1.1108.21.1 (41062740xHG36OEbpVAmVX5N9WCaNw)

make vmlinuz really stripped
author cl349@freefall.cl.cam.ac.uk
date Tue Jul 27 09:58:24 2004 +0000 (2004-07-27)
parents 51b2d77c6d1f
children dae98734f12e
line source
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
5 #ifdef _XEN_XFR_STUB_
6 typedef unsigned long u32;
7 #else
8 #include "xc.h"
9 #include "xc_io.h"
10 #endif
12 #include "xen_domain.h"
13 #include "marshal.h"
14 #include "xdr.h"
15 #include "xfrd.h"
17 #define MODULE_NAME "XFRD"
18 #define DEBUG 1
19 #include "debug.h"
22 int domain_suspend(u32 dom, void *data){
23 int err = 0;
24 Conn *xend = data;
26 dprintf("> dom=%lu data=%p\n", dom, data);
27 err = xfr_vm_suspend(xend, dom);
28 dprintf("< err=%d\n", err);
29 return err;
30 }
32 #ifndef _XEN_XFR_STUB_
33 static int xc_handle = 0;
35 int xcinit(void){
36 if(xc_handle <= 0){
37 xc_handle = xc_interface_open();
38 }
39 dprintf("< xc_handle=%d\n", xc_handle);
40 return xc_handle;
41 }
43 void xcfini(void){
44 if(xc_handle > 0){
45 xc_interface_close(xc_handle);
46 xc_handle = 0;
47 }
48 }
49 #endif
51 /** Write domain state.
52 *
53 * At some point during this the domain is suspended, and then there's no way back.
54 * Even if something later goes wrong we can't restart the domain.
55 */
56 int xen_domain_snd(Conn *xend, IOStream *io, uint32_t dom, char *vmconfig, int vmconfig_n){
57 int err = 0;
58 #ifdef _XEN_XFR_STUB_
59 char buf[1024];
60 int n, k, d, buf_n;
61 dprintf("> dom=%d\n", dom);
62 err = marshal_uint32(io, dom);
63 if(err) goto exit;
64 err = marshal_string(io, vmconfig, vmconfig_n);
65 if(err) goto exit;
66 n = 32 * 1024 * 1024;
67 n = 32 * 1024;
68 buf_n = sizeof(buf);
69 err = marshal_uint32(io, n);
70 for(k = 0; k < n; k += d){
71 d = n - k;
72 if(d > buf_n) d = buf_n;
73 err = marshal_bytes(io, buf, d);
74 if(err) goto exit;
75 dprintf("> k=%d n=%d\n", k, n);
76 }
78 dom = 99;
79 err = domain_suspend(dom, xend);
80 IOStream_close(io);
81 exit:
82 #else
83 XcIOContext _ioctxt = {}, *ioctxt = &_ioctxt;
84 dprintf("> dom=%d\n", dom);
85 ioctxt->domain = dom;
86 ioctxt->io = io;
87 ioctxt->info = iostdout;
88 ioctxt->err = iostderr;
89 ioctxt->data = xend;
90 ioctxt->suspend = domain_suspend;
92 err = xc_linux_save(xcinit(), ioctxt);
93 #endif
94 dprintf("< err=%d\n", err);
95 return err;
96 }
98 /** Receive domain state.
99 * Create a new domain and store the received state into it.
100 */
101 int xen_domain_rcv(IOStream *io, uint32_t *dom, char **vmconfig, int *vmconfig_n){
102 int err = 0;
103 #ifdef _XEN_XFR_STUB_
104 char buf[1024];
105 int n, k, d, buf_n;
106 dprintf(">\n");
107 err = unmarshal_uint32(io, dom);
108 if(err) goto exit;
109 err = unmarshal_new_string(io, vmconfig, vmconfig_n);
110 if(err) goto exit;
111 err = unmarshal_uint32(io, &n);
112 buf_n = sizeof(buf);
113 for(k = 0; k < n; k += d){
114 d = n - k;
115 if(d > buf_n) d = buf_n;
116 err = unmarshal_bytes(io, buf, d);
117 if(err) goto exit;
118 dprintf("> k=%d n=%d\n", k, n);
119 }
120 exit:
121 #else
122 XcIOContext _ioctxt = {}, *ioctxt = &_ioctxt;
123 dprintf(">\n");
124 ioctxt->io = io;
125 ioctxt->info = iostdout;
126 ioctxt->err = iostderr;
128 err = xc_linux_restore(xcinit(), ioctxt);
129 #endif
130 dprintf("< err=%d\n", err);
131 return err;
132 }
134 #include <curl/curl.h>
136 static int do_curl_global_init = 1;
138 static CURL *curlinit(void){
139 if(do_curl_global_init){
140 do_curl_global_init = 0;
141 curl_global_init(CURL_GLOBAL_ALL);
142 }
143 return curl_easy_init();
144 }
146 /** Configure a new domain. Talk to xend using libcurl.
147 */
148 int xen_domain_configure(uint32_t dom, char *vmconfig, int vmconfig_n){
149 int err = 0;
150 CURL *curl = NULL;
151 CURLcode curlcode = 0;
152 char domainurl[128] = {};
153 int domainurl_n = sizeof(domainurl) - 1;
154 int n;
155 struct curl_httppost *form = NULL, *last = NULL;
156 CURLFORMcode formcode = 0;
158 dprintf("> dom=%u\n", dom);
159 curl = curlinit();
160 if(!curl){
161 eprintf("> Could not init libcurl\n");
162 err = -ENOMEM;
163 goto exit;
164 }
165 n = snprintf(domainurl, domainurl_n,
166 "http://localhost:%d/xend/domain/%u", XEND_PORT, dom);
167 if(n <= 0 || n >= domainurl_n){
168 err = -ENOMEM;
169 eprintf("Out of memory in url.\n");
170 goto exit;
171 }
172 // Config field - set from vmconfig.
173 formcode = curl_formadd(&form, &last,
174 CURLFORM_COPYNAME, "config",
175 CURLFORM_BUFFER, "config",
176 CURLFORM_BUFFERPTR, vmconfig,
177 CURLFORM_BUFFERLENGTH, vmconfig_n,
178 CURLFORM_CONTENTTYPE, "application/octet-stream",
179 CURLFORM_END);
180 if(formcode){
181 eprintf("> Error adding config field.\n");
182 goto exit;
183 }
184 // Op field.
185 formcode = curl_formadd(&form, &last,
186 CURLFORM_COPYNAME, "op",
187 CURLFORM_COPYCONTENTS, "configure",
188 CURLFORM_END);
190 if(formcode){
191 eprintf("> Error adding op field.\n");
192 goto exit;
193 }
194 // No progress meter.
195 //curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
196 // Completely quiet.
197 //curl_easy_setopt(curl, CURLOPT_MUTE, 1);
198 // Set the URL.
199 curl_easy_setopt(curl, CURLOPT_URL, domainurl);
200 // POST the form.
201 curl_easy_setopt(curl, CURLOPT_HTTPPOST, form);
202 dprintf("> curl perform...\n");
203 #if 0 && defined(_XEN_XFR_STUB_)
204 dprintf("> _XEN_XFR_STUB_ defined - not calling xend\n");
205 curlcode = 0;
206 #else
207 curlcode = curl_easy_perform(curl);
208 #endif
209 exit:
210 if(curl) curl_easy_cleanup(curl);
211 if(form) curl_formfree(form);
212 if(formcode){
213 dprintf("> formcode=%d\n", formcode);
214 err = -EINVAL;
215 }
216 if(curlcode){
217 dprintf("> curlcode=%d\n", curlcode);
218 err = -EINVAL;
219 }
220 dprintf("< err=%d\n", err);
221 return err;
222 }