diff options
-rw-r--r-- | .rootkeys | 17 | ||||
-rw-r--r-- | tools/Makefile | 1 | ||||
-rwxr-xr-x | tools/check/check_twisted | 46 | ||||
-rwxr-xr-x | tools/examples/init.d/xend | 2 | ||||
-rw-r--r-- | tools/misc/xend | 31 | ||||
-rw-r--r-- | tools/python/xen/xend/server/SrvDaemon.py | 24 | ||||
-rwxr-xr-x | tools/python/xen/xend/server/console.py | 1 | ||||
-rw-r--r-- | tools/python/xen/xend/server/params.py | 1 | ||||
-rw-r--r-- | tools/python/xen/xm/migrate.py | 3 | ||||
-rw-r--r-- | tools/xfrd/Make.xfrd | 34 | ||||
-rw-r--r-- | tools/xfrd/Makefile | 78 | ||||
-rw-r--r-- | tools/xfrd/connection.c | 195 | ||||
-rw-r--r-- | tools/xfrd/connection.h | 36 | ||||
-rw-r--r-- | tools/xfrd/debug.h | 73 | ||||
-rw-r--r-- | tools/xfrd/lzi_stream.c | 533 | ||||
-rw-r--r-- | tools/xfrd/lzi_stream.h | 35 | ||||
-rw-r--r-- | tools/xfrd/marshal.c | 207 | ||||
-rw-r--r-- | tools/xfrd/marshal.h | 42 | ||||
-rw-r--r-- | tools/xfrd/select.c | 50 | ||||
-rw-r--r-- | tools/xfrd/select.h | 16 | ||||
-rw-r--r-- | tools/xfrd/xen_domain.c | 338 | ||||
-rw-r--r-- | tools/xfrd/xen_domain.h | 22 | ||||
-rw-r--r-- | tools/xfrd/xfrd.c | 1268 | ||||
-rw-r--r-- | tools/xfrd/xfrd.h | 17 | ||||
-rwxr-xr-x | tools/xfrd/xfrdClient.py | 124 |
25 files changed, 4 insertions, 3190 deletions
@@ -513,7 +513,6 @@ 42642813SYRkwr07qVZ9eCI5QTZANg tools/check/check_iproute 4124b307u-FeKvFP9kZnh0rLV0XjGg tools/check/check_logging 4124b307tRTjLqzRy60QrUoqN2Fhuw tools/check/check_python -4124b307XdznSNCv97lrT3RpOdMM1A tools/check/check_twisted 4124b307lnAATmulpXYa0M-dzxLBDA tools/check/check_zlib_devel 4124b308ly20ptMKQoiztPyP_X68Mw tools/check/check_zlib_lib 4124b308O8yPHMKbj4YPR_grPGZmdA tools/check/chk @@ -1040,22 +1039,6 @@ 403a3edbVpV2E_wq1zeEkJ_n4Uu2eg tools/xentrace/xentrace.c 403a3edblCUrzSj0mmKhO5HOPrOrSQ tools/xentrace/xentrace_format 4050c413NtuyIq5lsYJV4P7KIjujXw tools/xentrace/xentrace_format.1 -40e9808eHO3QprCFKg9l2JJzgt2voA tools/xfrd/Make.xfrd -40e9808epTR4zWrYjGUnaaynK20Q5A tools/xfrd/Makefile -40e9808eysqT4VNDlJFqsZB2rdg4Qw tools/xfrd/connection.c -40e9808eyXfJUi4E0C3WSgrEXqQ1sQ tools/xfrd/connection.h -40e9808eULGwffNOE4kBrAfZ9YAVMA tools/xfrd/debug.h -40e9808ePADCSKL1YgGCt2TbYPnYkw tools/xfrd/lzi_stream.c -40e9808eDNAdpF71o5teYb9DTT-PRw tools/xfrd/lzi_stream.h -40e9808eQxi0EzTcPJtosrzxEIjA-Q tools/xfrd/marshal.c -40e9808etg13xfRm0Lqd8vY-jHOoTg tools/xfrd/marshal.h -40e9808eCsmywryb036TdtRMJHDMmQ tools/xfrd/select.c -40e9808e99OcM547cKMTfmCVSoWVAw tools/xfrd/select.h -40e9808edpUtf4bJ8IbqClPJj_OvbA tools/xfrd/xen_domain.c -40e9808eHviFFIwdUKOA234uIeifjA tools/xfrd/xen_domain.h -40e9808eIFeV-MDCNyVTNt5NfMPKeQ tools/xfrd/xfrd.c -40e9808eGIbOoSNJRiwWK2C3mjGWaA tools/xfrd/xfrd.h -40e9808eHXvs_5eggj9McD_J90mhNw tools/xfrd/xfrdClient.py 3f72f1bdJPsV3JCnBqs9ddL9tr6D2g xen/COPYING 3ddb79bcbOVHh38VJzc97-JEGD4dJQ xen/Makefile 3ddb79bcWnTwYsQRWl_PaneJfa6p0w xen/Rules.mk diff --git a/tools/Makefile b/tools/Makefile index 9e6c159050..2ffa481328 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -8,7 +8,6 @@ SUBDIRS += misc SUBDIRS += examples SUBDIRS += xentrace SUBDIRS += python -SUBDIRS += xfrd SUBDIRS += xcs SUBDIRS += xcutils SUBDIRS += pygrub diff --git a/tools/check/check_twisted b/tools/check/check_twisted deleted file mode 100755 index 06d70b4692..0000000000 --- a/tools/check/check_twisted +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python -# CHECK-INSTALL -# -*- mode: python; -*- - -import os -import sys - -def hline(): - print >>sys.stderr, "*" * 70 - -def msg(message): - print >>sys.stderr, "*" * 3, message - -def check_twisted_version(): - """Check twisted is installed with a supported version and print a warning if not. - Raises an error if twisted is not installed. - """ - # Supported twisted release and major version. - RELEASE = 1 - MAJOR = 3 - try: - from twisted.copyright import version - except ImportError: - hline() - msg("The Twisted framework is not installed.") - msg("Use 'make install-twisted' at the xen root to install.") - msg("") - msg("Alternatively download and install version %d.%d or higher" % (RELEASE, MAJOR)) - msg("from http://www.twistedmatrix.com/products") - hline() - sys.exit(1) - - (release, major, minor) = version.split('.') - release = int(release) - major = int(major) - if release > RELEASE: return - if release == RELEASE and major >= MAJOR: return - hline() - msg("Warning: Twisted version not supported: %s" % version) - msg("Use Twisted version %d.%d.0 or higher" % (RELEASE, MAJOR)) - hline() - sys.exit(1) - -if __name__ == '__main__': - check_twisted_version() - diff --git a/tools/examples/init.d/xend b/tools/examples/init.d/xend index 058f6a821f..b6d1ad41f3 100755 --- a/tools/examples/init.d/xend +++ b/tools/examples/init.d/xend @@ -11,7 +11,7 @@ if ! [ -e /proc/xen/privcmd ]; then exit 0 fi -# Wait for Xend / Xfrd to be up +# Wait for Xend and xcs to be up function await_daemons_up { i=1 diff --git a/tools/misc/xend b/tools/misc/xend index 355a320292..768cd20f94 100644 --- a/tools/misc/xend +++ b/tools/misc/xend @@ -60,36 +60,6 @@ def check_logging(): hline() raise CheckError("logging is not installed") -def check_twisted_version(): - """Check twisted is installed with a supported version and print a warning if not. - Raises an error if twisted is not installed. - """ - # Supported twisted release and major version. - RELEASE = 1 - MAJOR = 3 - try: - from twisted.copyright import version - except ImportError: - hline() - msg("The Twisted framework is not installed.") - msg("Use 'make install-twisted' at the xen root to install.") - msg("") - msg("Alternatively download and install version %d.%d or higher" % (RELEASE, MAJOR)) - msg("from http://www.twistedmatrix.com/products") - hline() - raise CheckError("twisted is not installed") - - - (release, major, minor) = version.split('.') - release = int(release) - major = int(major) - if release > RELEASE: return - if release == RELEASE and major >= MAJOR: return - hline() - msg("Warning: Twisted version not supported: %s" % version) - msg("Use Twisted version %d.%d.0 or higher" % (RELEASE, MAJOR)) - hline() - def check_user(): """Check that the effective user id is 0 (root). """ @@ -146,7 +116,6 @@ def stop_xcs(): def main(): try: check_logging() - check_twisted_version() check_user() except CheckError: sys.exit(1) diff --git a/tools/python/xen/xend/server/SrvDaemon.py b/tools/python/xen/xend/server/SrvDaemon.py index 61a0e33949..85c1c23560 100644 --- a/tools/python/xen/xend/server/SrvDaemon.py +++ b/tools/python/xen/xend/server/SrvDaemon.py @@ -128,21 +128,16 @@ class Daemon: def cleanup_xend(self, kill=False): return self.cleanup_process(XEND_PID_FILE, "xend", kill) - def cleanup_xfrd(self, kill=False): - return self.cleanup_process(XFRD_PID_FILE, "xfrd", kill) - def cleanup(self, kill=False): self.cleanup_xend(kill=kill) - self.cleanup_xfrd(kill=kill) def status(self): - """Returns the status of the xend and xfrd daemons. + """Returns the status of the xend daemon. The return value is defined by the LSB: 0 Running 3 Not running """ - if (self.cleanup_process(XEND_PID_FILE, "xend", False) == 0 or - self.cleanup_process(XFRD_PID_FILE, "xfrd", False) == 0): + if self.cleanup_process(XEND_PID_FILE, "xend", False) == 0: return 3 else: return 0 @@ -171,17 +166,6 @@ class Daemon: pidfile.close() return pid - def start_xfrd(self): - """Fork and exec xfrd, writing its pid to XFRD_PID_FILE. - """ - if self.fork_pid(XFRD_PID_FILE): - # Parent - pass - else: - # Child - self.daemonize() - os.execl("/usr/sbin/xfrd", "xfrd") - def daemonize(self): if not DAEMONIZE: return # Detach from TTY. @@ -212,15 +196,11 @@ class Daemon: 4 Insufficient privileges """ xend_pid = self.cleanup_xend() - xfrd_pid = self.cleanup_xfrd() - if self.set_user(): return 4 os.chdir("/") - if xfrd_pid == 0: - self.start_xfrd() if xend_pid > 0: # Trying to run an already-running service is a success. return 0 diff --git a/tools/python/xen/xend/server/console.py b/tools/python/xen/xend/server/console.py index 8706e97966..f3dade883b 100755 --- a/tools/python/xen/xend/server/console.py +++ b/tools/python/xen/xend/server/console.py @@ -246,7 +246,6 @@ class ConsoleDev(Dev, protocol.ServerFactory): def disconnect(self, conn=None): """Disconnect the TCP connection to the console. """ - print 'ConsoleDev>disconnect>', conn try: self.lock.acquire() if conn and conn != self.conn: return diff --git a/tools/python/xen/xend/server/params.py b/tools/python/xen/xend/server/params.py index 0f8632a4f2..5c7fdf7bad 100644 --- a/tools/python/xen/xend/server/params.py +++ b/tools/python/xen/xend/server/params.py @@ -1,6 +1,5 @@ # The following parameters could be placed in a configuration file. XEND_PID_FILE = '/var/run/xend.pid' -XFRD_PID_FILE = '/var/run/xfrd.pid' XEND_TRACE_FILE = '/var/log/xend.trace' XEND_USER = 'root' diff --git a/tools/python/xen/xm/migrate.py b/tools/python/xen/xm/migrate.py index 147c0c4d08..374b80b257 100644 --- a/tools/python/xen/xm/migrate.py +++ b/tools/python/xen/xm/migrate.py @@ -14,8 +14,7 @@ DOM0_ID = '0' gopts = Opts(use="""[options] DOM HOST Migrate domain DOM to host HOST. -The transfer daemon xfrd must be running on the -local host and on HOST. +Xend must be running on the local host and on HOST. """) gopts.opt('help', short='h', diff --git a/tools/xfrd/Make.xfrd b/tools/xfrd/Make.xfrd deleted file mode 100644 index a3f13a4fd4..0000000000 --- a/tools/xfrd/Make.xfrd +++ /dev/null @@ -1,34 +0,0 @@ -# -*- mode: Makefile; -*- -#============================================================================ - -UTIL_LIB = libutil.a - -UTIL_LIB_SRC = -UTIL_LIB_SRC += allocate.c -UTIL_LIB_SRC += enum.c -UTIL_LIB_SRC += fd_stream.c -UTIL_LIB_SRC += file_stream.c -UTIL_LIB_SRC += gzip_stream.c -UTIL_LIB_SRC += hash_table.c -UTIL_LIB_SRC += iostream.c -UTIL_LIB_SRC += lexis.c -UTIL_LIB_SRC += lzi_stream.c -UTIL_LIB_SRC += marshal.c -UTIL_LIB_SRC += string_stream.c -UTIL_LIB_SRC += sxpr.c -UTIL_LIB_SRC += sxpr_parser.c -UTIL_LIB_SRC += sys_net.c -UTIL_LIB_SRC += sys_string.c -#UTIL_LIB_SRC += util.c -#UTIL_LIB_SRC += xdr.c - -#---------------------------------------------------------------------------- -# Xfrd. - -XFRD_PROG_SRC = -XFRD_PROG_SRC += xfrd.c -XFRD_PROG_SRC += xen_domain.c -XFRD_PROG_SRC += select.c -XFRD_PROG_SRC += connection.c - -#============================================================================ diff --git a/tools/xfrd/Makefile b/tools/xfrd/Makefile deleted file mode 100644 index 949c6f915a..0000000000 --- a/tools/xfrd/Makefile +++ /dev/null @@ -1,78 +0,0 @@ -# -*- mode: Makefile; -*- -#============================================================================ -# -# Mike Wray <mike.wray@hp.com> -#============================================================================ - -INSTALL = install -INSTALL_PROG = $(INSTALL) -m0755 -INSTALL_DIR = $(INSTALL) -d -m0755 - -XEN_ROOT = ../.. -include $(XEN_ROOT)/tools/Rules.mk - -XFRD_INSTALL_DIR = /usr/sbin - -vpath %.h $(XEN_LIBXC) -INCLUDES += -I $(XEN_LIBXC) - -vpath %c $(XEN_LIBXUTIL) -INCLUDES += -I $(XEN_LIBXUTIL) - -include Make.xfrd - -UTIL_LIB_OBJ = $(UTIL_LIB_SRC:.c=.o) - -XFRD_PROG_OBJ = $(XFRD_PROG_SRC:.c=.o) -XFRD_PROG_OBJ += $(UTIL_LIB) - -CC := gcc - -CFLAGS += -Wall -Werror -O3 -fno-strict-aliasing -CFLAGS += $(INCLUDES) - -# Make gcc generate dependencies. -CFLAGS += -Wp,-MD,.$(@F).d -PROG_DEP = .*.d - -#$(warning XFRD_PROG_OBJ= $(XFRD_PROG_OBJ)) -#$(warning UTIL_LIB= $(UTIL_LIB)) -#$(warning UTIL_LIB_OBJ= $(UTIL_LIB_OBJ)) - -# Libraries for xfrd. -XFRD_LIBS := - -XFRD_LIBS += -L $(XEN_LIBXC) -lxc -XFRD_LIBS += -L $(XEN_LIBXUTIL) -lxutil - -# zlib library. -XFRD_LIBS += -lz - -#$(warning XFRD_LIBS = $(XFRD_LIBS)) - -all: build -build: xfrd - -xfrd: $(XFRD_PROG_OBJ) - $(CC) -o $@ $^ $(XFRD_LIBS) - -.PHONY: install -install: xfrd - [ -d $(DESTDIR)$(XFRD_INSTALL_DIR) ] || \ - $(INSTALL_DIR) $(DESTDIR)$(XFRD_INSTALL_DIR) - $(INSTALL_PROG) xfrd $(DESTDIR)$(XFRD_INSTALL_DIR) - -.PHONY: libutil -libutil: $(UTIL_LIB) - -$(UTIL_LIB): $(UTIL_LIB_OBJ) - $(AR) rc $@ $^ - -.PHONY: clean -clean: - $(RM) *.o *.a *.so *~ xfrd - $(RM) $(PROG_DEP) - -$(XFRD_PROG_OBJ): Makefile --include $(PROG_DEP) - diff --git a/tools/xfrd/connection.c b/tools/xfrd/connection.c deleted file mode 100644 index 7b493cdcfa..0000000000 --- a/tools/xfrd/connection.c +++ /dev/null @@ -1,195 +0,0 @@ -#include <stdlib.h> -#include <errno.h> -#include <unistd.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> - -#include "connection.h" -#include "file_stream.h" -#include "lzi_stream.h" -#include "sxpr_parser.h" - -#define dprintf(fmt, args...) fprintf(stdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args) -#define wprintf(fmt, args...) fprintf(stderr, "[WARN] %s" fmt, __FUNCTION__, ##args) -#define iprintf(fmt, args...) fprintf(stdout, "[INFO] %s" fmt, __FUNCTION__, ##args) -#define eprintf(fmt, args...) fprintf(stderr, "[ERROR] %s" fmt, __FUNCTION__, ##args) - -/** Compress magic header. */ -char compress_magic[2] = { 0x1f, 0x8b }; - -/** Plain magic header. */ -char plain_magic[2] = { 0x0, 0x0 }; - -int Conn_read_header(int sock, int *flags){ - int err = 0; - char magic[2] = {}; - int k, n = sizeof(magic); - k = read(sock, magic, n); - if(k != n){ - err = -EINVAL; - goto exit; - } - dprintf("> magic={ 0x%x, 0x%x }\n", magic[0], magic[1]); - if(magic[0] == compress_magic[0] && magic[1] == compress_magic[1]){ - *flags |= CONN_READ_COMPRESS; - dprintf("> Using compress read.\n"); - } else { - dprintf("> Using plain read.\n"); - } - exit: - return err; -} - -int Conn_write_header(int sock, int flags){ - int err = 0; - if(flags & CONN_WRITE_COMPRESS){ - dprintf("> Using compress write.\n"); - err = write(sock, compress_magic, 2); - } else { - dprintf("> Using plain write.\n"); - err = write(sock, plain_magic, 2); - } - if(err == 2) err = 0; - return err; -} - -/** Initialize a file stream from a file desciptor. - * - * @param fd file descriptor - * @param mode file mode - * @param flags control compression and buffering - * @param io return parameter for the stream - * @return 0 on success, error code otherwise - */ -int stream_init(int fd, const char *mode, int flags, int compress, IOStream **io){ - int err = 0; - dprintf(">mode=%s flags=%x compress=%d\n", mode, flags, compress); - if(compress){ - *io = lzi_stream_fdopen(fd, mode); - } else { - *io = file_stream_fdopen(fd, mode); - } - if(!*io){ - err = -errno; - perror("fdopen"); - goto exit; - } - if(1 && (flags & CONN_NOBUFFER)){ - // Make unbuffered. - dprintf("> unbuffer...\n"); - err = file_stream_setvbuf((compress ? lzi_stream_io(*io) : *io), NULL, _IONBF, 0); - if(err){ - err = -errno; - perror("setvbuf"); - goto exit; - } - } - exit: - if(err && *io){ - dprintf("> close err=%d\n", err); - IOStream_close(*io); - *io = NULL; - } - dprintf("< err=%d\n", err); - return err; -} - -/** Initialize a connection. - * - * @param conn connection - * @param flags - * @param sock socket - * @param ipaddr ip address - * @return 0 on success, error code otherwise - */ -int Conn_init(Conn *conn, int flags, int sock, struct sockaddr_in addr){ - int err = 0; - dprintf("> flags=%x\n", flags); - conn->addr = addr; - conn->sock = sock; - dprintf("> write stream...\n"); - err = stream_init(sock, "w", flags, (flags & CONN_WRITE_COMPRESS), &conn->out); - if(err) goto exit; - IOStream_flush(conn->out); - dprintf("> read stream...\n"); - err = stream_init(sock, "r", flags, (flags & CONN_READ_COMPRESS) , &conn->in); - if(err) goto exit; - exit: - if(err) eprintf("< err=%d\n", err); - return err; -} - -/** Open a connection. - * - * @param conn connection - * @param flags - * @param ipaddr ip address to connect to - * @param port port - * @return 0 on success, error code otherwise - */ -int Conn_connect(Conn *conn, int flags, struct in_addr ipaddr, uint16_t port){ - int err = 0; - int sock; - struct sockaddr_in addr_in; - struct sockaddr *addr = (struct sockaddr *)&addr_in; - socklen_t addr_n = sizeof(addr_in); - dprintf("> addr=%s:%d\n", inet_ntoa(ipaddr), ntohs(port)); - sock = socket(AF_INET, SOCK_STREAM, 0); - if(sock < 0){ - err = -errno; - goto exit; - } - addr_in.sin_family = AF_INET; - addr_in.sin_addr = ipaddr; - addr_in.sin_port = port; - err = connect(sock, addr, addr_n); - if(err) goto exit; - //err = Conn_write_header(sock, flags); - //if(err < 0) goto exit; - err = Conn_init(conn, flags, sock, addr_in); - exit: - if(err) eprintf("< err=%d\n", err); - return err; -} - -/** Close a connection. - * - * @param conn connection - */ -void Conn_close(Conn *conn){ - if(conn->in) IOStream_close(conn->in); - if(conn->out) IOStream_close(conn->out); - shutdown(conn->sock, 2); -} - -int Conn_sxpr(Conn *conn, Sxpr *sxpr){ - int err = 0; - Sxpr val = ONONE; - int c = 0; - - dprintf(">\n"); - if(!conn->parser){ - conn->parser = Parser_new(); - Parser_set_error_stream(conn->parser, iostdout); - } - while(!err && c >= 0 && !Parser_ready(conn->parser)){ - c = IOStream_getc(conn->in); - printf("%c", (char)c); - if(c < 0){ - err = Parser_input_eof(conn->parser); - } else { - err = Parser_input_char(conn->parser, c); - } - } - if(Parser_ready(conn->parser)){ - val = Parser_get_val(conn->parser); - } - if(err){ - objfree(val); - val = ONONE; - } - *sxpr = val; - dprintf("< err=%d\n", err); - return err; -} diff --git a/tools/xfrd/connection.h b/tools/xfrd/connection.h deleted file mode 100644 index 2b67e767ba..0000000000 --- a/tools/xfrd/connection.h +++ /dev/null @@ -1,36 +0,0 @@ -/* $Id: connection.h,v 1.1 2003/10/17 15:48:43 mjw Exp $ */ -#ifndef _VFC_CONNECTION_H_ -#define _VFC_CONNECTION_H_ - -#include <netinet/in.h> - -#include "iostream.h" -#include "sxpr_parser.h" - -/** A connection. - * The underlying transport is a socket. - * Contains in and out streams using the socket. - */ -typedef struct Conn { - struct sockaddr_in addr; - int sock; - IOStream *in; - IOStream *out; - Parser *parser; -} Conn; - -enum { - CONN_NOBUFFER=1, - CONN_READ_COMPRESS=2, - CONN_WRITE_COMPRESS=4, -}; - -extern int Conn_read_header(int sock, int *flags); -extern int Conn_write_header(int sock, int flags); -extern int Conn_init(Conn *conn, int flags, int sock, struct sockaddr_in addr); -extern int Conn_connect(Conn *conn, int flags, struct in_addr ipaddr, uint16_t port); -extern void Conn_close(Conn *conn); - -extern int Conn_sxpr(Conn *conn, Sxpr *sxpr); - -#endif /* ! _VFC_CONNECTION_H_ */ diff --git a/tools/xfrd/debug.h b/tools/xfrd/debug.h deleted file mode 100644 index 3df5345095..0000000000 --- a/tools/xfrd/debug.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2004 Mike Wray <mike.wray@hp.com> - * - * This library is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -#ifndef _XUTIL_DEBUG_H_ -#define _XUTIL_DEBUG_H_ - -#ifndef MODULE_NAME -#define MODULE_NAME "" -#endif - -#ifdef __KERNEL__ -#include <linux/config.h> -#include <linux/kernel.h> - -#ifdef DEBUG - -#define dprintf(fmt, args...) printk(KERN_DEBUG "[DBG] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) -#define wprintf(fmt, args...) printk(KERN_WARNING "[WRN] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) -#define iprintf(fmt, args...) printk(KERN_INFO "[INF] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) -#define eprintf(fmt, args...) printk(KERN_ERR "[ERR] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) - -#else - -#define dprintf(fmt, args...) do {} while(0) -#define wprintf(fmt, args...) printk(KERN_WARNING "[WRN] " MODULE_NAME fmt, ##args) -#define iprintf(fmt, args...) printk(KERN_INFO "[INF] " MODULE_NAME fmt, ##args) -#define eprintf(fmt, args...) printk(KERN_ERR "[ERR] " MODULE_NAME fmt, ##args) - -#endif - -#else - -#include <stdio.h> -#include <unistd.h> - -#ifdef DEBUG - -#define dprintf(fmt, args...) fprintf(stdout, "%d [DBG] " MODULE_NAME ">%s" fmt, getpid(), __FUNCTION__, ##args) -#define wprintf(fmt, args...) fprintf(stderr, "%d [WRN] " MODULE_NAME ">%s" fmt, getpid(),__FUNCTION__, ##args) -#define iprintf(fmt, args...) fprintf(stderr, "%d [INF] " MODULE_NAME ">%s" fmt, getpid(),__FUNCTION__, ##args) -#define eprintf(fmt, args...) fprintf(stderr, "%d [ERR] " MODULE_NAME ">%s" fmt, getpid(),__FUNCTION__, ##args) - -#else - -#define dprintf(fmt, args...) do {} while(0) -#define wprintf(fmt, args...) fprintf(stderr, "%d [WRN] " MODULE_NAME fmt, getpid(), ##args) -#define iprintf(fmt, args...) fprintf(stderr, "%d [INF] " MODULE_NAME fmt, getpid(), ##args) -#define eprintf(fmt, args...) fprintf(stderr, "%d [ERR] " MODULE_NAME fmt, getpid(), ##args) - -#endif - -#endif - -/** Print format for an IP address. - * See NIPQUAD(), HIPQUAD() - */ -#define IPFMT "%u.%u.%u.%u" - -#endif /* ! _XUTIL_DEBUG_H_ */ diff --git a/tools/xfrd/lzi_stream.c b/tools/xfrd/lzi_stream.c deleted file mode 100644 index b50b5c7953..0000000000 --- a/tools/xfrd/lzi_stream.c +++ /dev/null @@ -1,533 +0,0 @@ -/* - * Copyright (C) 2003 Hewlett-Packard Company. - * - * This library is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/** @file - * An IOStream implementation using LZI to provide compression and decompression. - * This is designed to provide compression without output latency. - * Flushing an LZI stream flushes all pending data to the underlying stream. - * This is essential for stream-based (e.g. networked) applications. - * - * A compressed data stream is a sequence of blocks. - * Each block is the block size followed by the compressed data. - * The last block has size zero. - * Sizes are 4-byte unsigned in network order. - * - * This format allows compressed data to be read from a stream without reading - * past the logical end of compressed data. - * - * @author Mike Wray <mike.wray@hpl.hp.com> - */ -#ifndef __KERNEL__ - -#include <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <string.h> - -#include "zlib.h" - -#include "allocate.h" -#include "lzi_stream.h" -#include "file_stream.h" -#include "marshal.h" - -#define dprintf(fmt, args...) fprintf(stdout, "[DEBUG] LZI>%s" fmt, __FUNCTION__, ##args) -#define wprintf(fmt, args...) fprintf(stderr, "[WARN] LZI>%s" fmt, __FUNCTION__, ##args) -#define iprintf(fmt, args...) fprintf(stdout, "[INFO] LZI>%s" fmt, __FUNCTION__, ##args) -#define eprintf(fmt, args...) fprintf(stderr, "[ERROR] LZI>%s" fmt, __FUNCTION__, ##args) - -static int lzi_read(IOStream *s, void *buf, size_t n); -static int lzi_write(IOStream *s, const void *buf, size_t n); -static int lzi_error(IOStream *s); -static int lzi_close(IOStream *s); -static void lzi_free(IOStream *s); -static int lzi_flush(IOStream *s); - -enum { - LZI_WRITE = 1, - LZI_READ = 2, -}; - -/** Methods used by a gzFile* IOStream. */ -static const IOMethods lzi_methods = { - read: lzi_read, - write: lzi_write, - error: lzi_error, - close: lzi_close, - free: lzi_free, - flush: lzi_flush, -}; - -#define BUFFER_SIZE (512 * 1024) - -typedef struct LZIState { - z_stream zstream; - void *inbuf; - uint32_t inbuf_size; - void *outbuf; - uint32_t outbuf_size; - /** Underlying stream for I/O. */ - IOStream *io; - /** Flags. */ - int flags; - /** Error indicator. */ - int error; - int eof; - int plain_bytes; - int comp_bytes; - int zstream_initialized; - int flushed; -} LZIState; - -static inline int LZIState_writeable(LZIState *s){ - return (s->flags & LZI_WRITE) != 0; -} - -static inline int LZIState_readable(LZIState *s){ - return (s->flags & LZI_READ) != 0; -} - -void LZIState_free(LZIState *z){ - if(!z) return; - if(z->zstream_initialized){ - if(LZIState_writeable(z)){ - deflateEnd(&z->zstream); - } else if(LZIState_readable(z)){ - inflateEnd(&z->zstream); - } - } - deallocate(z->inbuf); - deallocate(z->outbuf); - deallocate(z); -} - -static int mode_flags(const char *mode, int *flags){ - int err = 0; - int r=0, w=0; - if(!mode){ - err = -EINVAL; - goto exit; - } - for(; *mode; mode++){ - if(*mode == 'w') w = 1; - if(*mode == 'r') r = 1; - } - if(r + w != 1){ - err = -EINVAL; - goto exit; - } - if(r) *flags |= LZI_READ; - if(w) *flags |= LZI_WRITE; - exit: - return err; -} - -/** Get the stream state. - * - * @param s lzi stream - * @return stream state. - */ -static inline LZIState * lzi_state(IOStream *io){ - return (LZIState*)io->data; -} - -IOStream *lzi_stream_io(IOStream *io){ - LZIState *s = lzi_state(io); - return s->io; -} - -static inline void set_error(LZIState *s, int err){ - if(err < 0 && !s->error){ - s->error = err; - } -} - -static int zerror(LZIState *s, int err){ - if(err){ - //dprintf("> err=%d\n", err); - if(err < 0) set_error(s, -EIO); - } - return s->error; -} - -int lzi_stream_plain_bytes(IOStream *io){ - LZIState *s = lzi_state(io); - return s->plain_bytes; -} - -int lzi_stream_comp_bytes(IOStream *io){ - LZIState *s = lzi_state(io); - return s->comp_bytes; -} - -float lzi_stream_ratio(IOStream *io){ - LZIState *s = lzi_state(io); - float ratio = 0.0; - if(s->comp_bytes){ - ratio = ((float) s->comp_bytes)/((float) s->plain_bytes); - } - return ratio; -} - -static int alloc(void **p, int n){ - *p = allocate(n); - return (p ? 0 : -ENOMEM); -} - -LZIState * LZIState_new(IOStream *io, int flags){ - int err = -ENOMEM; - int zlevel = Z_BEST_SPEED; // Level 1 compression - fastest. - int zstrategy = Z_DEFAULT_STRATEGY; - int zwindow = MAX_WBITS; - int zmemory = 8; - LZIState *z = ALLOCATE(LZIState); - - //dprintf(">\n"); - if(!z) goto exit; - z->io = io; - z->flags = flags; - - if(LZIState_writeable(z)){ - z->outbuf_size = BUFFER_SIZE; - /* windowBits is passed < 0 to suppress zlib header */ - err = deflateInit2(&z->zstream, zlevel, Z_DEFLATED, -zwindow, zmemory, zstrategy); - if (err != Z_OK) goto exit; - z->zstream_initialized = 1; - err = alloc(&z->outbuf, z->outbuf_size); - if(err) goto exit; - z->zstream.next_out = z->outbuf; - z->zstream.avail_out = z->outbuf_size; - } else { - z->inbuf_size = BUFFER_SIZE; - err = alloc(&z->inbuf, z->inbuf_size); - if(err) goto exit; - ///z->zstream.next_in = z->inbuf; - - /* windowBits is passed < 0 to tell that there is no zlib header. - * Note that in this case inflate *requires* an extra "dummy" byte - * after the compressed stream in order to complete decompression and - * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are - * present after the compressed stream. - */ - err = inflateInit2(&z->zstream, -zwindow); - if(err != Z_OK) goto exit; - z->zstream_initialized = 1; - } - - exit: - if(err){ - LZIState_free(z); - z = NULL; - } - //dprintf("< z=%p\n", z); - return z; -} - -int read_block(LZIState *s){ - int err = 0; - uint32_t k = 0; - //dprintf(">\n"); - if(s->eof) goto exit; - err = unmarshal_uint32(s->io, &k); - if(err) goto exit; - if(k > s->inbuf_size){ - err = -EINVAL; - goto exit; - } - if(k){ - err = unmarshal_bytes(s->io, s->inbuf, k); - if(err) goto exit; - } else { - s->eof = 1; - } - s->zstream.avail_in = k; - s->zstream.next_in = s->inbuf; - s->comp_bytes += 4; - s->comp_bytes += k; - exit: - //dprintf("< err=%d\n", err); - return err; -} - -int write_block(LZIState *s){ - int err = 0; - int k = ((char*)s->zstream.next_out) - ((char*)s->outbuf); - //int k2 = s->outbuf_size - s->zstream.avail_out; - //dprintf("> k=%d k2=%d\n", k, k2); - if(!k) goto exit; - err = marshal_uint32(s->io, k); - if(err) goto exit; - err = marshal_bytes(s->io, s->outbuf, k); - if(err) goto exit; - s->zstream.next_out = s->outbuf; - s->zstream.avail_out = s->outbuf_size; - s->comp_bytes += 4; - s->comp_bytes += k; - exit: - //dprintf("< err=%d\n", err); - return err; -} - -int write_terminator(LZIState *s){ - int err = 0; - char c = 0; - err = marshal_uint32(s->io, 1); - if(err) goto exit; - err = marshal_bytes(s->io, &c, 1); - if(err) goto exit; - err = marshal_uint32(s->io, 0); - if(err) goto exit; - s->comp_bytes += 9; - exit: - return err; -} - -/** Write to the underlying stream using fwrite(); - * - * @param io destination - * @param buf data - * @param n number of bytes to write - * @return number of bytes written - */ -static int lzi_write(IOStream *io, const void *buf, size_t n){ - int err = 0; - LZIState *s = lzi_state(io); - - //dprintf("> buf=%p n=%d\n", buf, n); - if(!LZIState_writeable(s)){ - err = -EINVAL; - goto exit; - } - s->flushed = 0; - s->zstream.next_in = (void*)buf; - s->zstream.avail_in = n; - while(s->zstream.avail_in){ - if(s->zstream.avail_out == 0){ - err = write_block(s); - if(err) goto exit; - } - //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); - //dprintf("> 1 deflate next_in=%p next_out=%p\n", s->zstream.next_in, s->zstream.next_out); - err = zerror(s, deflate(&s->zstream, Z_NO_FLUSH)); - //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); - //dprintf("> 2 deflate next_in=%p next_out=%p\n", s->zstream.next_in, s->zstream.next_out); - if(err) goto exit; - } - err = n; - s->plain_bytes += n; - exit: - //dprintf("< err=%d\n", err); - return err; -} - - -/** Read from the underlying stream. - * - * @param io input - * @param buf where to put input - * @param n number of bytes to read - * @return number of bytes read - */ -static int lzi_read(IOStream *io, void *buf, size_t n){ - int err, zerr; - LZIState *s = lzi_state(io); - - //dprintf("> n=%d\n", n); - if(!LZIState_readable(s)){ - err = -EINVAL; - goto exit; - } - s->zstream.next_out = buf; - s->zstream.avail_out = n; - while(s->zstream.avail_out){ - if(s->zstream.avail_in == 0){ - err = read_block(s); - } - //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); - zerr = inflate(&s->zstream, Z_NO_FLUSH); - //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); - if(zerr == Z_STREAM_END) break; - //dprintf("> zerr=%d\n", zerr); - err = zerror(s, zerr); - if(err) goto exit; - } - err = n - s->zstream.avail_out; - s->plain_bytes += err; - exit: - set_error(s, err); - //dprintf("< err=%d\n", err); - return err; -} - -static int flush_output(LZIState *s, int mode){ - int err = 0, zerr; - int done = 0; - int avail_out_old; - - //dprintf("> avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); - if(s->flushed == 1 + mode) goto exit; - //s->zstream.avail_in = 0; /* should be zero already anyway */ - for(;;){ - // Write any available output. - if(done || s->zstream.avail_out == 0){ - err = write_block(s); - if(err) goto exit; - if(done) break; - } - //dprintf("> 1 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); - avail_out_old = s->zstream.avail_out; - zerr = deflate(&s->zstream, mode); - err = zerror(s, zerr); - //dprintf("> 2 deflate avail_in=%d avail_out=%d\n", s->zstream.avail_in, s->zstream.avail_out); - //dprintf("> deflate=%d\n", err); - //done = (s->zstream.avail_out != 0); - //done = (s->zstream.avail_in == 0) && (s->zstream.avail_out == avail_out_old); - if(0 && mode == Z_FINISH){ - done = (zerr == Z_STREAM_END); - } else { - done = (s->zstream.avail_in == 0) - //&& (s->zstream.avail_out == avail_out_old) - && (s->zstream.avail_out != 0); - } - } - s->flushed = 1 + mode; - exit: - //dprintf("< err=%d\n", err); - return err; -} - -/** Flush any pending input to the underlying stream. - * - * @param s lzi stream - * @return 0 on success, error code otherwise - */ -static int lzi_flush(IOStream *io){ - int err = 0; - LZIState *s = lzi_state(io); - //dprintf(">\n"); - if(!LZIState_writeable(s)){ - err = -EINVAL; - goto exit; - } - err = flush_output(s, Z_SYNC_FLUSH); - if(err) goto exit; - err = IOStream_flush(s->io); - exit: - set_error(s, err); - //dprintf("< err=%d\n", err); - return (err < 0 ? err : 0); -} - -/** Check if a stream has an error. - * - * @param s lzi stream - * @return code if has an error, 0 otherwise - */ -static int lzi_error(IOStream *s){ - int err = 0; - LZIState *state = lzi_state(s); - err = state->error; - if(err) goto exit; - err = IOStream_error(state->io); - exit: - return err; -} - -/** Close an lzi stream. - * - * @param s lzi stream to close - * @return result of the close - */ -static int lzi_close(IOStream *io){ - int err = 0; - LZIState *s = lzi_state(io); - if(LZIState_writeable(s)){ - err = flush_output(s, Z_FINISH); - if(err) goto exit; - err = write_terminator(s); - if(err) goto exit; - err = IOStream_flush(s->io); - } - exit: - err = IOStream_close(s->io); - set_error(s, err); - return err; -} - -/** Free an lzi stream. - * - * @param s lzi stream - */ -static void lzi_free(IOStream *s){ - LZIState *state = lzi_state(s); - LZIState_free(state); - s->data = NULL; -} - -/** Create an lzi stream for an IOStream. - * - * @param io stream to wrap - * @return new IOStream using f for i/o - */ -IOStream *lzi_stream_new(IOStream *io, const char *mode){ - int err = -ENOMEM; - int flags = 0; - IOStream *zio = NULL; - LZIState *state = NULL; - - zio = ALLOCATE(IOStream); - if(!zio) goto exit; - err = mode_flags(mode, &flags); - if(err) goto exit; - state = LZIState_new(io, flags); - if(!state) goto exit; - err = 0; - zio->data = state; - zio->methods = &lzi_methods; - exit: - if(err){ - if(state) LZIState_free(state); - if(zio) deallocate(zio); - zio = NULL; - } - return zio; -} - -/** IOStream version of fdopen(). - * - * @param fd file descriptor - * @param flags giving the mode to open in (as for fdopen()) - * @return new stream for the open file, or NULL if failed - */ -IOStream *lzi_stream_fdopen(int fd, const char *mode){ - int err = -ENOMEM; - IOStream *io = NULL, *zio = NULL; - io = file_stream_fdopen(fd, mode); - if(!io) goto exit; - zio = lzi_stream_new(io, mode); - if(!io) goto exit; - err = 0; - exit: - if(err){ - IOStream_close(io); - IOStream_close(zio); - zio = NULL; - } - return zio; -} -#endif diff --git a/tools/xfrd/lzi_stream.h b/tools/xfrd/lzi_stream.h deleted file mode 100644 index 959059eccf..0000000000 --- a/tools/xfrd/lzi_stream.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2003 Hewlett-Packard Company. - * - * This library is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef _XUTIL_LZI_STREAM_H_ -#define _XUTIL_LZI_STREAM_H_ - -#ifndef __KERNEL__ -#include "iostream.h" - -extern IOStream *lzi_stream_new(IOStream *io, const char *mode); -extern IOStream *lzi_stream_fopen(const char *file, const char *mode); -extern IOStream *lzi_stream_fdopen(int fd, const char *mode); -extern IOStream *lzi_stream_io(IOStream *zio); - -extern int lzi_stream_plain_bytes(IOStream *io); -extern int lzi_stream_comp_bytes(IOStream *io); -extern float lzi_stream_ratio(IOStream *io); - -#endif -#endif /* !_XUTIL_LZI_STREAM_H_ */ diff --git a/tools/xfrd/marshal.c b/tools/xfrd/marshal.c deleted file mode 100644 index 21775b658b..0000000000 --- a/tools/xfrd/marshal.c +++ /dev/null @@ -1,207 +0,0 @@ -#include <errno.h> -#include "sys_net.h" -#include "allocate.h" -#include "marshal.h" - -#define dprintf(fmt, args...) IOStream_print(iostdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args) -#define wprintf(fmt, args...) IOStream_print(iostderr, "[WARN] %s" fmt, __FUNCTION__, ##args) -#define iprintf(fmt, args...) IOStream_print(iostdout, "[INFO] %s" fmt, __FUNCTION__, ##args) -#define eprintf(fmt, args...) IOStream_print(iostderr, "[ERROR] %s" fmt, __FUNCTION__, ##args) - - -#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof((ary)[0])) - -/* Messages are coded as msgid followed by message fields. - * Initial message on any channel is hello - so can check version - * compatibility. - * - * char* -> uint16_t:n <n bytes> - * ints/uints go as suitable number of bytes (e.g. uint16_t is 2 bytes). - * optional fields go as '1' <val> or '0' (the 0/1 is 1 byte). - * lists go as ('1' <elt>)* '0' - */ - -int marshal_flush(IOStream *io){ - int err = 0; - err = IOStream_flush(io); - return err; -} - -int marshal_bytes(IOStream *io, void *s, uint32_t s_n){ - int err = 0; - int n; - n = IOStream_write(io, s, s_n); - if(n < 0){ - err = n; - } else if (n < s_n){ - wprintf("> Wanted %d, got %d\n", s_n, n); - err = -EIO; - } - return err; -} - -int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n){ - int err = 0; - int n; - //dprintf("> s_n=%d\n", s_n); - n = IOStream_read(io, s, s_n); - //dprintf("> n=%d\n", n); - if(n < 0){ - err = n; - } else if(n < s_n){ - wprintf("> Wanted %d, got %d\n", s_n, n); - err = -EIO; - } - //dprintf("< err=%d\n", err); - return err; -} - -int marshal_uint8(IOStream *io, uint8_t x){ - return marshal_bytes(io, &x, sizeof(x)); -} - -int unmarshal_uint8(IOStream *io, uint8_t *x){ - return unmarshal_bytes(io, x, sizeof(*x)); -} - -int marshal_uint16(IOStream *io, uint16_t x){ - x = htons(x); - return marshal_bytes(io, &x, sizeof(x)); -} - -int unmarshal_uint16(IOStream *io, uint16_t *x){ - int err = 0; - err = unmarshal_bytes(io, x, sizeof(*x)); - *x = ntohs(*x); - return err; -} - -int marshal_int32(IOStream *io, int32_t x){ - int err = 0; - //dprintf("> x=%d\n", x); - x = htonl(x); - err = marshal_bytes(io, &x, sizeof(x)); - //dprintf("< err=%d\n", err); - return err; -} - -int unmarshal_int32(IOStream *io, int32_t *x){ - int err = 0; - //dprintf(">\n"); - err = unmarshal_bytes(io, x, sizeof(*x)); - *x = ntohl(*x); - //dprintf("< err=%d x=%d\n", err, *x); - return err; -} - -int marshal_uint32(IOStream *io, uint32_t x){ - int err = 0; - //dprintf("> x=%u\n", x); - x = htonl(x); - err = marshal_bytes(io, &x, sizeof(x)); - //dprintf("< err=%d\n", err); - return err; -} - -int unmarshal_uint32(IOStream *io, uint32_t *x){ - int err = 0; - //dprintf(">\n"); - err = unmarshal_bytes(io, x, sizeof(*x)); - *x = ntohl(*x); - //dprintf("< err=%d x=%u\n", err, *x); - return err; -} - -int marshal_uint64(IOStream *io, uint64_t x){ - int err; - err = marshal_uint32(io, (uint32_t) ((x >> 32) & 0xffffffff)); - if(err) goto exit; - err = marshal_uint32(io, (uint32_t) ( x & 0xffffffff)); - exit: - return err; -} - -int unmarshal_uint64(IOStream *io, uint64_t *x){ - int err = 0; - uint32_t hi, lo; - err = unmarshal_uint32(io, &hi); - if(err) goto exit; - err = unmarshal_uint32(io, &lo); - *x = (((uint64_t) hi) << 32) | lo; - exit: - return err; -} - -int marshal_net16(IOStream *io, net16_t x){ - return marshal_bytes(io, &x, sizeof(x)); -} - -int unmarshal_net16(IOStream *io, net16_t *x){ - int err = 0; - err = unmarshal_bytes(io, x, sizeof(*x)); - return err; -} - -int marshal_net32(IOStream *io, net32_t x){ - return marshal_bytes(io, &x, sizeof(x)); -} - -int unmarshal_net32(IOStream *io, net32_t *x){ - int err = 0; - err = unmarshal_bytes(io, x, sizeof(*x)); - return err; -} - -int marshal_string(IOStream *io, char *s, uint32_t s_n){ - int err; - //dprintf("> s=%s\n", s); - err = marshal_uint32(io, s_n); - if(err) goto exit; - err = marshal_bytes(io, s, s_n); - exit: - //dprintf("< err=%d\n", err); - return err; -} - -int unmarshal_string(IOStream *io, char *s, uint32_t s_n){ - int err = 0, val_n = 0; - //dprintf(">\n"); - err = unmarshal_uint32(io, (unsigned int *)&val_n); - if(err) goto exit; - if(val_n >= s_n){ - err = -EINVAL; - goto exit; - } - err = unmarshal_bytes(io, s, val_n); - if(err) goto exit; - s[val_n] = '\0'; - exit: - //dprintf("< err=%d s=%s\n", err, s); - return err; -} - -int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n){ - int err = 0, val_n = 0; - char *val = NULL; - //dprintf(">\n"); - err = unmarshal_uint32(io, (unsigned int *)&val_n); - if(err) goto exit; - val = allocate(val_n + 1); - if(!val){ - err = -ENOMEM; - goto exit; - } - err = unmarshal_bytes(io, val, val_n); - if(err) goto exit; - val[val_n] = '\0'; - exit: - if(err){ - if(val) deallocate(val); - val = NULL; - val_n = 0; - } - *s = val; - if(s_n) *s_n = val_n; - //dprintf("< err=%d s=%s\n", err, *s); - return err; -} diff --git a/tools/xfrd/marshal.h b/tools/xfrd/marshal.h deleted file mode 100644 index 65e9682cea..0000000000 --- a/tools/xfrd/marshal.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef _XUTIL_MARSHAL_H_ -#define _XUTIL_MARSHAL_H_ - -#include "iostream.h" - -/** A 16-bit uint in network order, e.g. a port number. */ -typedef uint16_t net16_t; - -/** A 32-bit uint in network order, e.g. an IP address. */ -typedef uint32_t net32_t; - -extern int marshal_flush(IOStream *io); - -extern int marshal_bytes(IOStream *io, void *s, uint32_t s_n); -extern int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n); - -extern int marshal_uint8(IOStream *io, uint8_t x); -extern int unmarshal_uint8(IOStream *io, uint8_t *x); - -extern int marshal_uint16(IOStream *io, uint16_t x); -extern int unmarshal_uint16(IOStream *io, uint16_t *x); - -extern int marshal_uint32(IOStream *io, uint32_t x); -extern int unmarshal_uint32(IOStream *io, uint32_t *x); - -extern int marshal_int32(IOStream *io, int32_t x); -extern int unmarshal_int32(IOStream *io, int32_t *x); - -extern int marshal_uint64(IOStream *io, uint64_t x); -extern int unmarshal_uint64(IOStream *io, uint64_t *x); - -extern int marshal_net16(IOStream *io, net16_t x); -extern int unmarshal_net16(IOStream *io, net16_t *x); - -extern int marshal_net32(IOStream *io, net32_t x); -extern int unmarshal_net32(IOStream *io, net32_t *x); - -extern int marshal_string(IOStream *io, char *s, uint32_t s_n); -extern int unmarshal_string(IOStream *io, char *s, uint32_t s_n); -extern int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n); - -#endif /* ! _XUTIL_MARSHAL_H_ */ diff --git a/tools/xfrd/select.c b/tools/xfrd/select.c deleted file mode 100644 index bdaccfe000..0000000000 --- a/tools/xfrd/select.c +++ /dev/null @@ -1,50 +0,0 @@ -#include <stdlib.h> -#include <errno.h> -#include <unistd.h> - -#include "select.h" - -/** Zero all the file descriptor sets. - * - * @param set select set - * @param fd file descriptor - * @return 0 on success, -1 otherwise - */ -void SelectSet_zero(SelectSet *set){ - set->n = 0; - FD_ZERO(&set->rd); - FD_ZERO(&set->wr); - FD_ZERO(&set->er); -} - -/** Add a file descriptor to the write set. - * - * @param set select set - * @param fd file descriptor - * @return 0 on success, -1 otherwise - */ -void SelectSet_add_read(SelectSet *set, int fd){ - FD_SET(fd, &set->rd); - if(fd > set->n) set->n = fd; -} - -/** Add a file descriptor to the write set. - * - * @param set select set - * @param fd file descriptor - * @return 0 on success, -1 otherwise - */ -void SelectSet_add_write(SelectSet *set, int fd){ - FD_SET(fd, &set->wr); - if(fd > set->n) set->n = fd; -} - -/** Select on file descriptors. - * - * @param set select set - * @param timeout timeout (may be NULL for no timeout) - * @return 0 on success, -1 otherwise - */ -int SelectSet_select(SelectSet *set, struct timeval *timeout){ - return select(set->n+1, &set->rd, &set->wr, &set->er, timeout); -} diff --git a/tools/xfrd/select.h b/tools/xfrd/select.h deleted file mode 100644 index 2453f98536..0000000000 --- a/tools/xfrd/select.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef _XFRD_SELECT_H_ -#define _XFRD_SELECT_H_ - -/** Set of file descriptors for select. - */ -typedef struct SelectSet { - int n; - fd_set rd, wr, er; -} SelectSet; - -extern void SelectSet_zero(SelectSet *set); -extern void SelectSet_add_read(SelectSet *set, int fd); -extern void SelectSet_add_write(SelectSet *set, int fd); -extern int SelectSet_select(SelectSet *set, struct timeval *timeout); - -#endif /* ! _XFRD_SELECT_H_ */ diff --git a/tools/xfrd/xen_domain.c b/tools/xfrd/xen_domain.c deleted file mode 100644 index 659bf39cd6..0000000000 --- a/tools/xfrd/xen_domain.c +++ /dev/null @@ -1,338 +0,0 @@ -#include <unistd.h> -#include <stdlib.h> -#include <stdio.h> -#include <sys/socket.h> -#include <sys/un.h> - -#include "xc.h" -#include "xc_io.h" - -#include "sxpr.h" -#include "sxpr_parser.h" -#include "file_stream.h" -#include "fd_stream.h" - -#include "xen_domain.h" -#include "xfrd.h" - -#define MODULE_NAME "XFRD" -#define DEBUG 1 -#undef DEBUG -#include "debug.h" - -int domain_suspend(void *data, u32 dom){ - int err = 0; - Conn *xend = data; - - dprintf("> dom=%lu data=%p\n", dom, data); - err = xfr_vm_suspend(xend, dom); - dprintf("< err=%d\n", err); - return err; -} - -int domain_configure(void *data, u32 dom, char *vmconfig, int vmconfig_n){ - return xen_domain_configure(dom, vmconfig, vmconfig_n); -} - -static int xc_handle = 0; - -int xcinit(void){ - if(xc_handle <= 0){ - xc_handle = xc_interface_open(); - } - dprintf("< xc_handle=%d\n", xc_handle); - return xc_handle; -} - -void xcfini(void){ - if(xc_handle > 0){ - xc_interface_close(xc_handle); - xc_handle = 0; - } -} - -/** Write domain state. - * - * At some point during this the domain is suspended, and then there's no way back. - * Even if something later goes wrong we can't restart the domain. - */ -int xen_domain_snd(Conn *xend, IOStream *io, - uint32_t dom, - char *vmconfig, int vmconfig_n, - int live, int resource){ - int err = 0; -#if 0 - XcIOContext _ioctxt = {}, *ioctxt = &_ioctxt; - ioctxt->domain = dom; - ioctxt->io = io; - ioctxt->info = iostdout; - ioctxt->err = iostderr; - ioctxt->data = xend; - ioctxt->suspend = domain_suspend; - ioctxt->vmconfig = vmconfig; - ioctxt->vmconfig_n = vmconfig_n; - if(live){ - ioctxt->flags |= XCFLAGS_LIVE; - } - ioctxt->resource = resource; - err = xc_linux_save(xcinit(), ioctxt); - dprintf("< err=%d\n", err); -#endif - return err; -} - -/** Receive domain state. - * Create a new domain and store the received state into it. - */ -int xen_domain_rcv(IOStream *io, - uint32_t *dom, - char **vmconfig, int *vmconfig_n, - int *configured){ - int err = 0; -#if 0 - XcIOContext _ioctxt = {}, *ioctxt = &_ioctxt; - dprintf(">\n"); - ioctxt->io = io; - ioctxt->info = iostdout; - ioctxt->err = iostderr; - ioctxt->configure = domain_configure; - if ( !*configured ) - ioctxt->flags |= XCFLAGS_CONFIGURE; - - err = xc_linux_restore(xcinit(), ioctxt); - *dom = ioctxt->domain; - *vmconfig = ioctxt->vmconfig; - *vmconfig_n = ioctxt->vmconfig_n; - *configured = (ioctxt->flags & XCFLAGS_CONFIGURE); - dprintf("< err=%d\n", err); -#endif - return err; -} - -typedef struct xend { - int fd; - IOStream *io; - Parser *parser; - int seeneof; -} Xend; - -char *xend_server_addr(void){ - char * val = getenv("XEND_EVENT_ADDR"); - return (val ? val : "/var/lib/xend/event-socket"); -} - -/** Open a unix-domain socket to the xend server. - */ -int xend_open_fd(void){ - struct sockaddr_un addr_un = { .sun_family = AF_UNIX }; - struct sockaddr *addr = (struct sockaddr*)&addr_un; - int addr_n = sizeof(addr_un); - int err = 0; - - int fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(fd < 0){ - err = -errno; - perror("socket"); - goto exit; - } - strcpy(addr_un.sun_path, xend_server_addr()); - if(connect(fd, addr, addr_n) < 0){ - err = -errno; - perror("connect"); - goto exit; - } - exit: - if(err && (fd >= 0)){ - close(fd); - } - - return (err ? err : fd); -} - -/** Close a connection to the server. - * - * @param xend connection -*/ -void xend_close(Xend *xend){ - if(!xend) return; - close(xend->fd); - Parser_free(xend->parser); -} - -/** Open a connection to the server. - * - * @param xend result parameter for the connection - * @return 0 on success, negative error code otherwise - */ -int xend_open(Xend **xend){ - int err = 0; - Xend *val = ALLOCATE(Xend); - - val->fd = xend_open_fd(); - - if(val->fd < 0){ - err = val->fd; - goto exit; - } - val->io = fd_stream_new(val->fd); - val->parser = Parser_new(); - exit: - if(err) xend_close(val); - *xend = (err ? NULL : val); - return err; -} - -/** Read a response from a server connection. - */ -int xend_read_resp(Xend *xend, Sxpr *resp){ - int err = 0; - Sxpr val = ONONE; - char buf[1024]; - int buf_n = sizeof(buf), n; - - for( ; ; ){ - if(Parser_ready(xend->parser)){ - val = Parser_get_val(xend->parser); - goto exit; - } - if(xend->seeneof){ - err = -EIO; - goto exit; - } - memset(buf, 0, buf_n); - n = IOStream_read(xend->io, buf, 100); - if(n <= 0){ - xend->seeneof = 1; - err = Parser_input_eof(xend->parser); - } else { - err = Parser_input(xend->parser, buf, n); - } - } - exit: - *resp = (err < 0 ? ONONE : val); - return err; -} - -/** Read a response from a server connection and decode the value. - * - * @param xend server connection - * @param resp result parameter for the response value - * @return 0 on success, negative error code otherwise - */ -int xend_read(Xend *xend, Sxpr *resp){ - int err = 0; - Sxpr val = ONONE; - - dprintf(">\n"); - for( ; ; ){ - err = xend_read_resp(xend, &val); - if(err < 0) goto exit; - - if(sxpr_is(sxpr_name(val), "event")){ - // We don't care about events, try again. - err = 0; - continue; - } else if(sxpr_is(sxpr_name(val), "err")){ - eprintf("> "); objprint(iostderr, val, 0); fprintf(stderr, "\n"); - err = -EINVAL; - break; - } else { - err = 0; - val = sxpr_child0(val, ONULL); - break; - } - } -#ifdef DEBUG - dprintf("> OK "); - objprint(iostdout, val, 0); - printf("\n"); -#endif - exit: - if(resp){ - *resp = (err < 0 ? ONONE : val); - } - dprintf("> err=%d\n", err); - return err; -} - -/** Send a request to the server and return the result value in resp. - * - * @param xend server connection - * @param resp result parameter for the response value - * @param format request format followed by args to print - * @return 0 on success, negative error code otherwise - */ -int xend_call(Xend *xend, Sxpr *resp, char *format, ...){ - va_list args; - int err; - - dprintf("> "); - va_start(args, format); -#ifdef DEBUG - vprintf(format, args); printf("\n"); -#endif - err = IOStream_vprint(xend->io, format, args); - va_end(args); - if(err < 0) goto exit; - IOStream_flush(xend->io); - err = xend_read(xend, resp); - exit: - dprintf("> err=%d\n", err); - return (err < 0 ? err : 0); -} - -/** Get xend to list domains. - * We use this to force xend to refresh its domain list. - * - * @return 0 on success, error code otherwise - */ -int xen_domain_ls(void){ - int err = 0; - Xend *xend = NULL; - err = xend_open(&xend); - if(err) goto exit; - err = xend_call(xend, NULL, "(domain.ls)"); - exit: - xend_close(xend); - return err; -} - -/** Get xend to configure a new domain. - * - * @param dom domain id - * @param vmconfig configuration string - * @param vmconfig_n length of vmconfig - * @return 0 on success, error code otherwise - */ -int xen_domain_configure(uint32_t dom, char *vmconfig, int vmconfig_n){ - int err = 0; - Xend *xend = NULL; - dprintf("> dom=%u\n", dom); - // List domains so that xend will update its domain list and notice the new domain. - xen_domain_ls(); - // Now configure it. - err = xend_open(&xend); - if(err) goto exit; - err = xend_call(xend, NULL, "(domain.configure (dom %d) (config %*s))", - dom, vmconfig_n, vmconfig); - exit: - dprintf("< err=%d\n", err); - xend_close(xend); - return err; -} - -/** Get xend to unpause a domain. - * - * @param dom domain id - * @return 0 on success, error code otherwise - */ -int xen_domain_unpause(uint32_t dom){ - int err = 0; - Xend *xend = NULL; - err = xend_open(&xend); - if(err) goto exit; - err = xend_call(xend, NULL, "(domain.unpause (dom %d))", dom); - exit: - xend_close(xend); - return err; -} diff --git a/tools/xfrd/xen_domain.h b/tools/xfrd/xen_domain.h deleted file mode 100644 index c84e8b8d63..0000000000 --- a/tools/xfrd/xen_domain.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _XFRD_XEN_DOMAIN_H_ -#define _XFRD_XEN_DOMAIN_H_ -#include <sys/types.h> -#include <iostream.h> -#include "connection.h" - -/** Define to use stubs. Undefine to use Xen ops. */ -//#define _XEN_XFR_STUB_ - -extern int xen_domain_snd(Conn *xend, IOStream *io, - uint32_t dom, - char *vmconfig, int vmconfig_n, - int live, int resource); -extern int xen_domain_rcv(IOStream *io, - uint32_t *dom, - char **vmconfig, int *vmconfig_n, - int *configured); - - -extern int xen_domain_configure(uint32_t dom, char *vmconfig, int vmconfig_n); -extern int xen_domain_unpause(uint32_t dom); -#endif diff --git a/tools/xfrd/xfrd.c b/tools/xfrd/xfrd.c deleted file mode 100644 index 2379b045c7..0000000000 --- a/tools/xfrd/xfrd.c +++ /dev/null @@ -1,1268 +0,0 @@ -/** @file - * XFRD - Domain Transfer Daemon for Xen. - * - * The xfrd is forked by xend to transfer a vm to a remote system. - * - * The vm is suspended, then its state and memory are transferred to the remote system. - * The remote system attempts to create a vm and copy the transferred state and memory into it, - * finally resuming the vm. If all is OK the vm ends up running on the remote - * system and is removed from the originating system. If the transfer does not complete - * successfully the originating system attempts to resume the vm. - * The children exit when the transfer completes. - * - * @author Mike Wray <mike.wray@hpl.hp.com> - */ - -#include <stdlib.h> -#include <unistd.h> -#include <stdio.h> -#include <getopt.h> -#include <errno.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <time.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <string.h> - -#include <signal.h> -#include <sys/wait.h> -#include <sys/select.h> - -#include "allocate.h" -#include "file_stream.h" -#include "string_stream.h" -#include "lzi_stream.h" -#include "gzip_stream.h" -#include "sys_net.h" -#include "sys_string.h" - -//#include "xdr.h" -#include "enum.h" -#include "xfrd.h" - -#include "xen_domain.h" - -#include "connection.h" -#include "select.h" - -#define MODULE_NAME "XFRD" -#define DEBUG 1 -#include "debug.h" - -/* -sender: - xend connects to xfrd and writes migrate message - xend writes domain config to xfrd - - xfrd forks - - xfrd connects to peer - xfrd sends hello, reads response - xfrd sends domain - xfrd reads response - reports progress/status to xend - - xend reads xfrd for progress/status, disconnects - If ok, destroys domain. - If not ok, unpauses domain. - -receiver: - xfrd accepts connection on inbound port - xfrd forks and accepts connection - xfrd receives hello, writes response - xfrd receives domain - xfrd connects to xend, configures new domain - xfrd writes status back to peer, child exits - - - (xfr.hello <major> <minor>) - (xfr.err <code> <reason>) - - xend->xfrd (xfr.migrate <domain> <vmconfig> <host> <port> <live>) - (xfr.save <domain> <vmconfig> <file>) - xfrd->xend (xfr.suspend <domain>) - xfrd->xend (xfr.progress <percent> <rate: kb/s>) - xfrd->xend (xfr.err <code> <reason>) | (xfr.ok <domain>) - xfrd->xfrd (xfr.xfr <domain>) - xfrd->xfrd (xfr.err <code>) | (xfr.ok <domain>) - - xfrd->xend (xfr.configure <domain> <vmconfig>) - */ - -Sxpr oxfr_configure; // (xfr.configure <vmid> <vmconfig>) -Sxpr oxfr_err; // (xfr.err <code>) -Sxpr oxfr_hello; // (xfr.hello <major> <minor>) -Sxpr oxfr_migrate; // (xfr.migrate <vmid> <vmconfig> <host> <port> <live> <resource>) -Sxpr oxfr_migrate_ok;// (xfr.migrate.ok <value>) -Sxpr oxfr_progress; // (xfr.progress <percent> <rate: kb/s>) -Sxpr oxfr_restore; // (xfr.restore <file>) -Sxpr oxfr_restore_ok;// (xfr.restore.ok <vmid>) -Sxpr oxfr_save; // (xfr.save <vmid> <vmconfig> <file>) -Sxpr oxfr_save_ok; // (xfr.save.ok) -Sxpr oxfr_vm_destroy;// (xfr.vm.destroy <vmid>) -Sxpr oxfr_vm_suspend;// (xfr.vm.suspend <vmid>) -Sxpr oxfr_xfr; // (xfr.xfr <vmid>) -Sxpr oxfr_xfr_ok; // (xfr.xfr.ok <vmid>) - -void xfr_init(void){ - oxfr_configure = intern("xfr.configure"); - oxfr_err = intern("xfr.err"); - oxfr_hello = intern("xfr.hello"); - oxfr_migrate = intern("xfr.migrate"); - oxfr_migrate_ok = intern("xfr.migrate.ok"); - oxfr_progress = intern("xfr.progress"); - oxfr_restore = intern("xfr.restore"); - oxfr_restore_ok = intern("xfr.restore.ok"); - oxfr_save = intern("xfr.save"); - oxfr_save_ok = intern("xfr.save.ok"); - oxfr_vm_destroy = intern("xfr.vm.destroy"); - oxfr_vm_suspend = intern("xfr.vm.suspend"); - oxfr_xfr = intern("xfr.xfr"); - oxfr_xfr_ok = intern("xfr.xfr.ok"); -} - -#ifndef TRUE -#define TRUE 1 -#endif - -#ifndef FALSE -#define FALSE 0 -#endif - -#define PROGRAM "xfrd" - -#define OPT_PORT 'P' -#define KEY_PORT "port" -#define DOC_PORT "<port>\n\txfr port (as a number or service name)" - -#define OPT_COMPRESS 'Z' -#define KEY_COMPRESS "compress" -#define DOC_COMPRESS "\n\tuse compression for migration" - -#define OPT_HELP 'h' -#define KEY_HELP "help" -#define DOC_HELP "\n\tprint help" - -#define OPT_VERSION 'v' -#define KEY_VERSION "version" -#define DOC_VERSION "\n\tprint version" - -#define OPT_VERBOSE 'V' -#define KEY_VERBOSE "verbose" -#define DOC_VERBOSE "\n\tverbose flag" - -/** Print a usage message. - * Prints to stdout if err is zero, and exits with 0. - * Prints to stderr if err is non-zero, and exits with 1. - */ -void usage(int err){ - FILE *out = (err ? stderr : stdout); - - fprintf(out, "Usage: %s [options]\n", PROGRAM); - fprintf(out, "-%c, --%s %s\n", OPT_PORT, KEY_PORT, DOC_PORT); - fprintf(out, "-%c, --%s %s\n", OPT_COMPRESS, KEY_COMPRESS, DOC_COMPRESS); - fprintf(out, "-%c, --%s %s\n", OPT_VERBOSE, KEY_VERBOSE, DOC_VERBOSE); - fprintf(out, "-%c, --%s %s\n", OPT_VERSION, KEY_VERSION, DOC_VERSION); - fprintf(out, "-%c, --%s %s\n", OPT_HELP, KEY_HELP, DOC_HELP); - exit(err ? 1 : 0); -} - -typedef struct Args { - int bufsize; - unsigned long port; - int verbose; - int compress; -} Args; - -/** Transfer states. */ -enum { - XFR_INIT, - XFR_HELLO, - XFR_STATE, - XFR_RUN, - XFR_FAIL, - XFR_DONE, - XFR_MAX -}; - -/** Short options. Options followed by ':' take an argument. */ -static char *short_opts = (char[]){ - OPT_PORT, ':', - OPT_COMPRESS, - OPT_HELP, - OPT_VERSION, - OPT_VERBOSE, - 0 }; - -/** Long options. */ -static struct option const long_opts[] = { - { KEY_PORT, required_argument, NULL, OPT_PORT }, - { KEY_COMPRESS, no_argument, NULL, OPT_COMPRESS }, - { KEY_HELP, no_argument, NULL, OPT_HELP }, - { KEY_VERSION, no_argument, NULL, OPT_VERSION }, - { KEY_VERBOSE, no_argument, NULL, OPT_VERBOSE }, - { NULL, 0, NULL, 0 } -}; - -/** Xfrd arguments. */ -static Args _args = {}; - -/** Xfrd arguments. */ -static Args *args = &_args; - -/** Initialize an array element for a constant to its string name. */ -#define VALDEF(val) { val, #val } - -/** Names for the transfer states. */ -static EnumDef xfr_states[] = { - VALDEF(XFR_INIT), - VALDEF(XFR_HELLO), - VALDEF(XFR_STATE), - VALDEF(XFR_RUN), - VALDEF(XFR_FAIL), - VALDEF(XFR_DONE), - { 0, NULL } -}; - - -/** State machine for transfer. */ -typedef struct XfrState { - /** Current state. */ - int state; - /** Error codes for the states. */ - int state_err[XFR_MAX]; - /** First error. */ - int err; - /** State when first error happened. */ - int err_state; - - uint32_t vmid; - char* vmconfig; - int vmconfig_n; - unsigned long xfr_port; - char *xfr_host; - uint32_t vmid_new; - int live; - int resource; -} XfrState; - -/** Get the name of a transfer state. - * - * @param s state - * @return name - */ -char * xfr_state_name(int s){ - return enum_val_to_name(s, xfr_states); -} - -/** Set the state of a transfer. - * - * @param s transfer - * @param state state - * @return state - */ -int XfrState_set_state(XfrState *s, int state){ - s->state = state; - return s->state; -} - -/** Get the state of a transfer. - * - * @param s transfer - * @return state - */ -int XfrState_get_state(XfrState *s){ - return s->state; -} - -/** Set an error in the current state. - * Does nothing if an error is already set. - * - * @param s transfer - * @param err error - * @return error - */ -int XfrState_set_err(XfrState *s, int err){ - if(!s->state_err[s->state]){ - s->state_err[s->state] = err; - } - if(!s->err){ - s->err = err; - s->err_state = s->state; - } - return err; -} - -/** Get the error in the current state. - * - * @param s transfer - * @return error - */ -int XfrState_get_err(XfrState *s){ - return s->state_err[s->state]; -} - -/** Get the first error of a transfer. - * - * @param s transfer - * @return error - */ -int XfrState_first_err(XfrState *s){ - return s->err; -} - -/** Get the state a transfer was in when it had its first error. - * - * @param s transfer - * @return error state - */ -int XfrState_first_err_state(XfrState *s){ - return s->err_state; -} - -/** Set xfrd default arguments. - * - * @param args arguments to set - */ -void set_defaults(Args *args){ - args->compress = FALSE; - args->bufsize = 128 * 1024; - args->port = htons(XFRD_PORT); -} - -int stringof(Sxpr exp, char **s){ - int err = 0; - //dprintf(">\n"); objprint(iostdout, exp, PRINT_TYPE); IOStream_print(iostdout, "\n"); - if(ATOMP(exp)){ - *s = atom_name(exp); - } else if(STRINGP(exp)){ - *s = string_string(exp); - } else { - err = -EINVAL; - *s = NULL; - } - //dprintf("< err=%d s=%s\n", err, *s); - return err; -} - -int intof(Sxpr exp, int *v){ - int err = 0; - char *s; - unsigned long l; - //dprintf(">\n"); objprint(iostdout, exp, 0); IOStream_print(iostdout, "\n"); - if(INTP(exp)){ - *v = OBJ_INT(exp); - } else { - err = stringof(exp, &s); - if(err) goto exit; - err = convert_atoul(s, &l); - *v = (int)l; - } - exit: - //dprintf("< err=%d v=%d\n", err, *v); - return err; -} - -int addrof(Sxpr exp, uint32_t *v){ - char *h; - unsigned long a; - int err = 0; - //dprintf(">\n"); objprint(iostdout, exp, 0); IOStream_print(iostdout, "\n"); - err = stringof(exp, &h); - if(err) goto exit; - if(get_host_address(h, &a)){ - err = -EINVAL; - goto exit; - } - *v = a; - exit: - //dprintf("< err=%d v=%x\n", err, *v); - return err; -} - -int portof(Sxpr exp, uint16_t *v){ - char *s; - int err = 0; - //dprintf(">\n"); objprint(iostdout, exp, 0); IOStream_print(iostdout, "\n"); - if(INTP(exp)){ - *v = get_ul(exp); - *v = htons(*v); - } else { - unsigned long p; - err = stringof(exp, &s); - if(err) goto exit; - err = convert_service_to_port(s, &p); - if(err){ - err = -EINVAL; - goto exit; - } - *v = p; - } - exit: - //dprintf("< err=%d v=%u\n", err, *v); - return err; -} - -static inline struct in_addr inaddr(uint32_t addr){ - return (struct in_addr){ .s_addr = addr }; -} - -time_t stats(time_t t0, uint64_t offset, uint64_t memory, float *percent, float *rate){ - time_t t1 = time(NULL); - *percent = (offset * 100.0f) / memory; - t1 = time(NULL) - t0; - *rate = (t1 ? offset/(t1 * 1024.0f) : 0.0f); - return t1; -} - -/** Notify success or error. - * - * @param conn connection - * @param errcode error code - * @return 0 on success, error code otherwise - */ -int xfr_error(Conn *conn, int errcode){ - int err = 0; - - if(!conn->out) return -ENOTCONN; - if(errcode <0) errcode = -errcode; - err = IOStream_print(conn->out, "(%s %d)", - atom_name(oxfr_err), errcode); - return (err < 0 ? err : 0); -} - -/** Read a response message - error or ok. - * - * @param conn connection - * @return 0 on success, error code otherwise - */ -int xfr_response(Conn *conn){ - int err; - Sxpr sxpr; - - dprintf(">\n"); - if(!conn->out) return -ENOTCONN; - err = Conn_sxpr(conn, &sxpr); - if(err) goto exit; - if(sxpr_elementp(sxpr, oxfr_err)){ - int errcode; - err = intof(sxpr_childN(sxpr, 0, ONONE), &errcode); - if(err) goto exit; - err = errcode; - } - exit: - dprintf("< err=%d\n", err); - return err; -} - -/** Get the initial hello message and check the protocol version. - * It is an error to receive anything other than a hello message - * with the correct protocol version. - * - * @param conn connection - * @return 0 on success, error code otherwise - */ -int xfr_hello(Conn *conn){ - int err; - uint32_t major = XFR_PROTO_MAJOR, minor = XFR_PROTO_MINOR; - uint32_t hello_major, hello_minor; - Sxpr sxpr; - if(!conn->in) return -ENOTCONN; - dprintf(">\n"); - err = Conn_sxpr(conn, &sxpr); - if(err) goto exit; - if(!sxpr_elementp(sxpr, oxfr_hello)){ - wprintf("> sxpr_elementp test failed\n"); - err = -EINVAL; - goto exit; - } - err = intof(sxpr_childN(sxpr, 0, ONONE), (int *)&hello_major); - if(err) goto exit; - err = intof(sxpr_childN(sxpr, 1, ONONE), (int *)&hello_minor); - if(err) goto exit; - if(hello_major != major || hello_minor != minor){ - eprintf("> Wanted protocol version %d.%d, got %d.%d", - major, minor, hello_major, hello_minor); - err = -EINVAL; - goto exit; - } - exit: - xfr_error(conn, err); - if(err){ - eprintf("> Hello failed: %d\n", err); - } - dprintf("< err=%d\n", err); - return err; -} - -/** Send the initial hello message. - * - * @param conn connection - * @param msg message - * @return 0 on success, error code otherwise - */ -int xfr_send_hello(Conn *conn){ - int err = 0; - dprintf(">\n"); - - err = IOStream_print(conn->out, "(%s %d %d)", - atom_name(oxfr_hello), - XFR_PROTO_MAJOR, - XFR_PROTO_MINOR); - if(err < 0) goto exit; - IOStream_flush(conn->out); - err = xfr_response(conn); - exit: - dprintf("< err=%d\n", err); - return err; -} - -int xfr_send_xfr(Conn *conn, uint32_t vmid){ - int err; - - err = IOStream_print(conn->out, "(%s %d)", - atom_name(oxfr_xfr), vmid); - return (err < 0 ? err : 0); -} - -int xfr_send_xfr_ok(Conn *conn, uint32_t vmid){ - int err = 0; - - err = IOStream_print(conn->out, "(%s %d)", - atom_name(oxfr_xfr_ok), vmid); - return (err < 0 ? err : 0); -} - -int xfr_send_migrate_ok(Conn *conn, uint32_t vmid){ - int err = 0; - - err = IOStream_print(conn->out, "(%s %d)", - atom_name(oxfr_migrate_ok), vmid); - return (err < 0 ? err : 0); -} - -int xfr_send_restore_ok(Conn *conn, uint32_t vmid){ - int err = 0; - - err = IOStream_print(conn->out, "(%s %d)", - atom_name(oxfr_restore_ok), vmid); - return (err < 0 ? err : 0); -} - -int xfr_send_save_ok(Conn *conn){ - int err = 0; - - err = IOStream_print(conn->out, "(%s)", - atom_name(oxfr_save_ok)); - return (err < 0 ? err : 0); -} - -int xfr_send_suspend(Conn *conn, uint32_t vmid){ - int err = 0; - - err = IOStream_print(conn->out, "(%s %d)", - atom_name(oxfr_vm_suspend), vmid); - return (err < 0 ? err : 0); -} - -/** Suspend a vm on behalf of save/migrate. - */ -int xfr_vm_suspend(Conn *xend, uint32_t vmid){ - int err = 0; - dprintf("> vmid=%u\n", vmid); - err = xfr_send_suspend(xend, vmid); - if(err) goto exit; - IOStream_flush(xend->out); - err = xfr_response(xend); - exit: - dprintf("< err=%d\n", err); - return err; -} - -int xfr_send_destroy(Conn *conn, uint32_t vmid){ - int err = 0; - - err = IOStream_print(conn->out, "(%s %d)", - atom_name(oxfr_vm_destroy), vmid); - return (err < 0 ? err : 0); -} - -/** Destroy a vm on behalf of save/migrate. - */ -int xfr_vm_destroy(Conn *xend, uint32_t vmid){ - int err = 0; - dprintf("> vmid=%u\n", vmid); - err = xfr_send_destroy(xend, vmid); - if(err) goto exit; - IOStream_flush(xend->out); - err = xfr_response(xend); - exit: - dprintf("< err=%d\n", err); - return err; -} - -/** Get vm state. Send transfer message. - * - * @param peer connection - * @param msg message - * @return 0 on success, error code otherwise - */ -int xfr_send_state(XfrState *state, Conn *xend, Conn *peer){ - int err = 0; - Sxpr sxpr; - - dprintf(">\n"); - XfrState_set_state(state, XFR_STATE); - // Send xfr message and the domain state. - err = xfr_send_xfr(peer, state->vmid); - if(err) goto exit; - dprintf(">*** Sending domain %u\n", state->vmid); - err = xen_domain_snd(xend, peer->out, - state->vmid, - state->vmconfig, state->vmconfig_n, - state->live, state->resource); - dprintf(">*** Sent domain %u\n", state->vmid); - if(err) goto exit; - // Sending the domain suspends it, and there's no way back. - // So destroy it now. If anything goes wrong now it's too late. - dprintf(">*** Destroying domain %u\n", state->vmid); - err = xfr_vm_destroy(xend, state->vmid); - if(err) goto exit; - err = xfr_error(peer, err); - if(err) goto exit; - IOStream_flush(peer->out); - // Read the response from the peer. - err = Conn_sxpr(peer, &sxpr); - if(err) goto exit; - if(sxpr_elementp(sxpr, oxfr_err)){ - // Error. - int errcode; - err = intof(sxpr_childN(sxpr, 0, ONONE), &errcode); - if(!err) err = errcode; - } else if(sxpr_elementp(sxpr, oxfr_xfr_ok)){ - // Ok - get the new domain id. - err = intof(sxpr_childN(sxpr, 0, ONONE), (int *)&state->vmid_new); - xfr_error(peer, err); - } else { - // Anything else is invalid. But it may be too late. - err = -EINVAL; - xfr_error(peer, err); - } - exit: - XfrState_set_err(state, err); - dprintf("< err=%d\n", err); - return err; -} - -/** Finish the transfer. - */ -int xfr_send_done(XfrState *state, Conn *xend){ - int err = 0; - int first_err = 0; - - first_err = XfrState_first_err(state); - if(first_err){ - XfrState_set_state(state, XFR_FAIL); - } else { - XfrState_set_state(state, XFR_DONE); - } - if(first_err){ - err = xfr_error(xend, first_err); - } else { - // Report new domain id to xend. - err = xfr_send_migrate_ok(xend, state->vmid_new); - } - - XfrState_set_err(state, err); - if(XfrState_first_err(state)){ - int s, serr; - - wprintf("> Transfer errors:\n"); - for(s = 0; s < XFR_MAX; s++){ - serr = state->state_err[s]; - if(!serr) continue; - wprintf("> state=%-12s err=%d\n", xfr_state_name(s), serr); - } - } else { - wprintf("> Transfer OK\n"); - } - dprintf("< err=%d\n", err); - return err; -} - -/** Migrate a vm to another node. - * - * @param xend connection - * @return 0 on success, error code otherwise - */ -int xfr_send(Args *args, XfrState *state, Conn *xend, uint32_t addr, uint32_t port){ - int err = 0; - Conn _peer = {}, *peer = &_peer; - int flags = 0; - struct in_addr xfr_addr; - uint16_t xfr_port; - time_t t0 = time(NULL), t1; - - dprintf(">\n"); - flags |= CONN_NOBUFFER; - if(args->compress){ - flags |= CONN_WRITE_COMPRESS; - } - xfr_addr.s_addr = addr; - xfr_port = port; - if(!xfr_port) xfr_port = htons(XFRD_PORT); - dprintf("> Xfr vmid=%u\n", state->vmid); - dprintf("> Xfr xfr_addr=%s:%d\n", inet_ntoa(xfr_addr), ntohs(xfr_port)); - err = Conn_connect(peer, flags, xfr_addr, xfr_port); - if(err) goto exit; - XfrState_set_state(state, XFR_HELLO); - // Send hello message. - err = xfr_send_hello(peer); - if(err) goto exit; - printf("\n"); - // Send vm state. - err = xfr_send_state(state, xend, peer); - if(err) goto exit; - if(args->compress){ - IOStream *zio = peer->out; - int plain_bytes = lzi_stream_plain_bytes(zio); - int comp_bytes = lzi_stream_comp_bytes(zio); - float ratio = lzi_stream_ratio(zio); - iprintf("> Compression: plain %d bytes, compressed %d bytes, ratio %3.2f\n", - plain_bytes, comp_bytes, ratio); - } - exit: - dprintf("> err=%d\n", err); - if(err && !XfrState_get_err(state)){ - XfrState_set_err(state, err); - } - Conn_close(peer); - if(!err){ - t1 = time(NULL) - t0; - iprintf("> Transfer complete in %lu seconds\n", t1); - } - dprintf("> done err=%d, notifying xend...\n", err); - xfr_send_done(state, xend); - dprintf("< err=%d\n", err); - return err; -} - -/** Save a vm to file. - */ -int xfr_save(Args *args, XfrState *state, Conn *xend, char *file){ - int err = 0; - int compress = 0; - IOStream *io = NULL; - - dprintf("> file=%s\n", file); - if(compress){ - io = gzip_stream_fopen(file, "wb1"); - } else { - io = file_stream_fopen(file, "wb"); - } - if(!io){ - eprintf("> Failed to open %s\n", file); - err = -EINVAL; - goto exit; - } - err = xen_domain_snd(xend, io, - state->vmid, - state->vmconfig, state->vmconfig_n, - 0, 0); - if(err){ - err = xfr_error(xend, err); - } else { - err = xfr_send_save_ok(xend); - } - exit: - if(io){ - IOStream_close(io); - } - if(err){ - unlink(file); - } - dprintf("< err=%d\n", err); - return err; -} - -/** Restore a vm from file. - * - * @return 0 on success, error code otherwise - */ -int xfr_restore(Args *args, XfrState *state, Conn *xend, char *file){ - int err = 0; - IOStream *io = NULL; - int configured = 0; - - dprintf("> file=%s\n", file); - io = gzip_stream_fopen(file, "rb"); - if(!io){ - eprintf("> Failed to open %s\n", file); - err = -EINVAL; - goto exit; - } - err = xen_domain_rcv(io, - &state->vmid_new, - &state->vmconfig, &state->vmconfig_n, - &configured); - if(err) goto exit; - if(!configured){ - err = xen_domain_configure(state->vmid_new, state->vmconfig, state->vmconfig_n); - if(err) goto exit; - } - err = xen_domain_unpause(state->vmid_new); - exit: - if(io){ - IOStream_close(io); - } - if(err){ - xfr_error(xend, err); - } else { - xfr_send_restore_ok(xend, state->vmid_new); - } - dprintf("< err=%d\n", err); - return err; -} - -/** Accept the transfer of a vm from another node. - * - * @param peer connection - * @param msg message - * @return 0 on success, error code otherwise - */ -int xfr_recv(Args *args, XfrState *state, Conn *peer){ - int err = 0; - time_t t0 = time(NULL), t1; - Sxpr sxpr; - int configured=0; - - dprintf("> peer=%s\n", inet_ntoa(peer->addr.sin_addr)); - // If receiving from localhost set configured so that that xen_domain_rcv() - // does not attempt to configure the new domain. This is because the old - // domain still exists and will make it fail. - if(peer->addr.sin_addr.s_addr == htonl(INADDR_LOOPBACK)){ - dprintf("> Peer is localhost\n"); - configured = 1; - } - err = xen_domain_rcv(peer->in, - &state->vmid_new, - &state->vmconfig, &state->vmconfig_n, - &configured); - if(err) goto exit; - // Read from the peer. This is just so we wait before configuring. - // When migrating to the same host the peer must destroy the domain - // before we configure the new one. - err = Conn_sxpr(peer, &sxpr); - if(err) goto exit; - if(!configured){ - dprintf("> Configuring...\n"); - err = xen_domain_configure(state->vmid_new, state->vmconfig, state->vmconfig_n); - if(err) goto exit; - err = xen_domain_unpause(state->vmid_new); - if(err) goto exit; - } - // Report new domain id to peer. - err = xfr_send_xfr_ok(peer, state->vmid_new); - if(err) goto exit; - // Get the final ok. - err = xfr_response(peer); - exit: - if(!err){ - t1 = time(NULL) - t0; - iprintf("> Transfer complete in %lu seconds\n", t1); - } - if(err){ - xfr_error(peer, err); - } - dprintf("< err=%d\n", err); - return err; -} - -/** Listen for a hello followed by a service request. - * The request can be from the local xend or from xfrd on another node. - * - * @param peersock socket - * @param peer_in peer address - * @return 0 on success, error code otherwise - */ -int xfrd_service(Args *args, int peersock, struct sockaddr_in peer_in){ - int err = 0; - Sxpr sxpr; - Conn _conn = {}, *conn = &_conn; - int flags = CONN_NOBUFFER; - - dprintf(">\n"); - err = Conn_init(conn, flags, peersock, peer_in); - if(err) goto exit; - //dprintf(">xfr_hello... \n"); - err = xfr_hello(conn); - if(err) goto exit; - //dprintf("> sxpr...\n"); - err = Conn_sxpr(conn, &sxpr); - if(err) goto exit; - //dprintf("> sxpr=\n"); - //objprint(iostdout, sxpr, PRINT_TYPE); IOStream_print(iostdout, "\n"); - if(sxpr_elementp(sxpr, oxfr_migrate)){ - // Migrate message from xend. - uint32_t addr; - uint16_t port; - XfrState _state = {}, *state = &_state; - int n = 0; - - dprintf("> xfr.migrate\n"); - err = intof(sxpr_childN(sxpr, n++, ONONE), (int *)&state->vmid); - if(err) goto exit; - err = stringof(sxpr_childN(sxpr, n++, ONONE), &state->vmconfig); - if(err) goto exit; - state->vmconfig_n = strlen(state->vmconfig); - err = addrof(sxpr_childN(sxpr, n++, ONONE), &addr); - if(err) goto exit; - err = portof(sxpr_childN(sxpr, n++, ONONE), &port); - if(err) goto exit; - err = intof(sxpr_childN(sxpr, n++, ONONE), &state->live); - if(err) goto exit; - err = intof(sxpr_childN(sxpr, n++, ONONE), &state->resource); - if(err) goto exit; - err = xfr_send(args, state, conn, addr, port); - - } else if(sxpr_elementp(sxpr, oxfr_save)){ - // Save message from xend. - char *file; - XfrState _state = {}, *state = &_state; - int n = 0; - - dprintf("> xfr.save\n"); - err = intof(sxpr_childN(sxpr, n++, ONONE), (int *)&state->vmid); - if(err) goto exit; - err = stringof(sxpr_childN(sxpr, n++, ONONE), &state->vmconfig); - if(err) goto exit; - state->vmconfig_n = strlen(state->vmconfig); - err = stringof(sxpr_childN(sxpr, n++, ONONE), &file); - if(err) goto exit; - err = xfr_save(args, state, conn, file); - - } else if(sxpr_elementp(sxpr, oxfr_restore)){ - // Restore message from xend. - char *file; - XfrState _state = {}, *state = &_state; - int n = 0; - - dprintf("> xfr.restore\n"); - err = stringof(sxpr_childN(sxpr, n++, ONONE), &file); - if(err) goto exit; - err = xfr_restore(args, state, conn, file); - - } else if(sxpr_elementp(sxpr, oxfr_xfr)){ - // Xfr message from peer xfrd. - XfrState _state = {}, *state = &_state; - int n = 0; - - dprintf("> xfr.xfr\n"); - err = intof(sxpr_childN(sxpr, n++, ONONE), (int *)&state->vmid); - if(err) goto exit; - err = xfr_recv(args, state, conn); - - } else{ - // Anything else is invalid. - err = -EINVAL; - eprintf("> Invalid message: "); - objprint(iostderr, sxpr, 0); - IOStream_print(iostderr, "\n"); - xfr_error(conn, err); - } - exit: - Conn_close(conn); - dprintf("< err=%d\n", err); - return err; -} - -/** Accept an incoming connection. - * - * @param sock tcp socket - * @return 0 on success, error code otherwise - */ -int xfrd_accept(Args *args, int sock){ - struct sockaddr_in peer_in; - struct sockaddr *peer = (struct sockaddr *)&peer_in; - socklen_t peer_n = sizeof(peer_in); - int peersock; - pid_t pid; - int err = 0; - - dprintf("> sock=%d\n", sock); - dprintf("> accept...\n"); - peersock = accept(sock, peer, &peer_n); - dprintf("> accept=%d\n", peersock); - if(peersock < 0){ - perror("accept"); - err = -errno; - goto exit; - } - iprintf("> Accepted connection from %s:%d on %d\n", - inet_ntoa(peer_in.sin_addr), htons(peer_in.sin_port), sock); - fflush(stdout); - fflush(stderr); - pid = fork(); - if(pid > 0){ - // Parent, fork succeeded. - iprintf("> Forked child pid=%d\n", pid); - close(peersock); - } else if (pid < 0){ - // Parent, fork failed. - perror("fork"); - close(peersock); - } else { - // Child. - iprintf("> Xfr service for %s:%d\n", - inet_ntoa(peer_in.sin_addr), htons(peer_in.sin_port)); - err = xfrd_service(args, peersock, peer_in); - iprintf("> Xfr service err=%d\n", err); - shutdown(peersock, 2); - exit(err ? 1 : 0); - } - exit: - dprintf("< err=%d\n", err); - return err; -} - -/** Socket select loop. - * Accepts connections on the tcp socket. - * - * @param listen_sock tcp listen socket - * @return 0 on success, error code otherwise - */ -int xfrd_select(Args *args, int listen_sock){ - int err = 0; - SelectSet set = {}; - dprintf("> socks: %d\n", listen_sock); - while(1){ - SelectSet_zero(&set); - SelectSet_add_read(&set, listen_sock); - err = SelectSet_select(&set, NULL); - if(err < 0){ - if(errno == EINTR) continue; - perror("select"); - goto exit; - } - if(FD_ISSET(listen_sock, &set.rd)){ - xfrd_accept(args, listen_sock); - } - } - exit: - dprintf("< err=%d\n", err); - return err; -} - -/** Create a socket. - * - * @param args program arguments - * @param socktype socket type - * @param reuse whether to set SO_REUSEADDR - * @param val return value for the socket - * @return 0 on success, error code otherwise - */ -int create_socket(Args *args, int socktype, int reuse, int *val){ - int err = 0; - int sock = 0; - struct sockaddr_in addr_in; - struct sockaddr *addr = (struct sockaddr *)&addr_in; - socklen_t addr_n = sizeof(addr_in); - - dprintf(">\n"); - // Create socket and bind it. - sock = socket(AF_INET, socktype, 0); - if(sock < 0){ - err = -errno; - goto exit; - } - addr_in.sin_family = AF_INET; - addr_in.sin_addr.s_addr = INADDR_ANY; - addr_in.sin_port = args->port; - dprintf("> port=%d\n", ntohs(addr_in.sin_port)); - if(reuse){ - // Set socket option to reuse address. - int val = 1; - err = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); - if(err < 0){ - err = -errno; - perror("setsockopt"); - goto exit; - } - } - err = bind(sock, addr, addr_n); - if(err < 0){ - err = -errno; - perror("bind"); - goto exit; - } - exit: - *val = (err ? -1 : sock); - dprintf("< err=%d\n", err); - return err; -} - -/** Create the tcp listen socket. - * - * @param args program arguments - * @param val return value for the socket - * @return 0 on success, error code otherwise - */ -int xfrd_listen_socket(Args *args, int *val){ - int err = 0; - int sock; - dprintf(">\n"); - err = create_socket(args, SOCK_STREAM, 1, &sock); - if(err) goto exit; - dprintf("> listen...\n"); - err = listen(sock, 5); - if(err < 0){ - err = -errno; - perror("listen"); - goto exit; - } - exit: - *val = (err ? -1 : sock); - if(err) close(sock); - dprintf("< err=%d\n", err); - return err; -} - -/** Type for signal handling functions. */ -typedef void SignalAction(int code, siginfo_t *info, void *data); - -/** Handle SIGCHLD by getting child exit status. - * This prevents child processes being defunct. - * - * @param code signal code - * @param info signal info - * @param data - */ -void sigaction_SIGCHLD(int code, siginfo_t *info, void *data){ - int status; - pid_t pid; - //dprintf("> child_exit=%d waiting...\n", child_exit); - pid = wait(&status); - dprintf("> child pid=%d status=%d\n", pid, status); -} - -/** Handle SIGPIPE. - * - * @param code signal code - * @param info signal info - * @param data - */ -void sigaction_SIGPIPE(int code, siginfo_t *info, void *data){ - dprintf("> SIGPIPE\n"); - //fflush(stdout); - //fflush(stderr); - //exit(1); -} - -/** Handle SIGALRM. - * - * @param code signal code - * @param info signal info - * @param data - */ -void sigaction_SIGALRM(int code, siginfo_t *info, void *data){ - dprintf("> SIGALRM\n"); -} - -/** Install a handler for a signal. - * - * @param signum signal - * @param action handler - * @return 0 on success, error code otherwise - */ -int catch_signal(int signum, SignalAction *action){ - int err = 0; - struct sigaction sig = {}; - sig.sa_sigaction = action; - sig.sa_flags = SA_SIGINFO; - err = sigaction(signum, &sig, NULL); - if(err){ - perror("sigaction"); - } - return err; -} - -/** Transfer daemon main program. - * - * @param args program arguments - * @return 0 on success, error code otherwise - */ -int xfrd_main(Args *args){ - int err = 0; - int listen_sock; - - dprintf(">\n"); - catch_signal(SIGCHLD,sigaction_SIGCHLD); - catch_signal(SIGPIPE,sigaction_SIGPIPE); - catch_signal(SIGALRM,sigaction_SIGALRM); - err = xfrd_listen_socket(args, &listen_sock); - if(err) goto exit; - err = xfrd_select(args, listen_sock); - exit: - close(listen_sock); - dprintf("< err=%d\n", err); - return err; -} - -/** Parse command-line arguments and call the xfrd main program. - * - * @param arg argument count - * @param argv arguments - * @return 0 on success, 1 otherwise - */ -int main(int argc, char *argv[]){ - int err = 0; - int key = 0; - int long_index = 0; -#ifndef DEBUG - static const char * LOGFILE = "/var/log/xfrd.log"; -#endif - -#ifndef DEBUG - freopen(LOGFILE, "w+", stdout); - fclose(stderr); - stderr = stdout; -#endif - dprintf(">\n"); - set_defaults(args); - while(1){ - key = getopt_long(argc, argv, short_opts, long_opts, &long_index); - if(key == -1) break; - switch(key){ - case OPT_PORT: - err = !convert_service_to_port(optarg, &args->port); - if(err) goto exit; - break; - case OPT_COMPRESS: - args->compress = TRUE; - break; - case OPT_HELP: - usage(0); - break; - case OPT_VERBOSE: - args->verbose = TRUE; - break; - case OPT_VERSION: - printf("> Version %d.%d\n", XFR_PROTO_MAJOR, XFR_PROTO_MINOR); - exit(0); - break; - default: - usage(EINVAL); - break; - } - } - xfr_init(); - err = xfrd_main(args); - exit: - if(err && key > 0){ - fprintf(stderr, "Error in arg %c\n", key); - } - return (err ? 1 : 0); -} diff --git a/tools/xfrd/xfrd.h b/tools/xfrd/xfrd.h deleted file mode 100644 index 0671b383eb..0000000000 --- a/tools/xfrd/xfrd.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _XFRD_XFRD_H_ -#define _XFRD_XFRD_H_ - -/** Xend port in host order. */ -#define XEND_PORT 8000 - -/** Xfrd port in host order. */ -#define XFRD_PORT 8002 - -/** Protocol version. */ -#define XFR_PROTO_MAJOR 1 -#define XFR_PROTO_MINOR 0 - -struct Conn; -extern int xfr_vm_suspend(struct Conn *xend, uint32_t vmid); -extern int xfr_vm_destroy(struct Conn *xend, uint32_t vmid); -#endif diff --git a/tools/xfrd/xfrdClient.py b/tools/xfrd/xfrdClient.py deleted file mode 100755 index 4badf454db..0000000000 --- a/tools/xfrd/xfrdClient.py +++ /dev/null @@ -1,124 +0,0 @@ -#!/bin/env python -""" -Test client for the migration daemon (xfrd). - -Author: Mike Wray <mike.wray@hp.com> - -""" -import getopt -import sys -import os -from socket import * -import StringIO - -sys.path.append("/home/mjw/repos-bk/xeno-unstable.bk/tools/python") - -import xen.xend.sxp as sxp - -XFRD_PORT = 8002 - -verbose = 0 - -class TCPClient: - - def __init__(self, host, port): - print ">TCPClient" - self.sock = socket(AF_INET, SOCK_STREAM, 0) - print ">TCPClient sock=", self.sock - print ">TCPClient> connect ", host, port - v = self.sock.connect((host, port)) - print ">TCPClient> connect=", v - # Send plain header (no gzip). - #self.sock.send("\0\0") - - self.sockin = self.sock.makefile("r") - self.sockout = self.sock.makefile("w") - #pass - - def request(self, req): - print "request>", req - sxp.show(req, out=self.sockout) - self.sockout.flush() - print "request<" - - def request_hello(self): - self.request(['xfr.hello', XFR_PROTO_MAJOR, XFR_PROTO_MINOR]) - - def request_migrate(self, vmid, vhost, vport, vmconfig='(vm)'): - self.request(['xfr.migrate', vmid, vmconfig, vhost, vport]) - - def read(self): - while(1): - v = self.sockin.read() - print 'read>', v - #if v[0] == 'xfr.err' and v[1]: return - #if v[0] == 'xfr.ok': return - -XFR_PROTO_MAJOR = 1 -XFR_PROTO_MINOR = 0 - -host_default = "localhost" -port_default = XFRD_PORT -vhost_default = "localhost" -vport_default = 8003 -vmid_default = 1 - -# Short options. Options followed by ':' need a parameter. -short_opts = 'h' - -# Long options. Options ending in '=' need a parameter. -long_opts = [ 'host=', 'port=', 'vhost=', 'vport=', 'vmid=', 'verbose', 'help'] - -def usage(err=None): - if err: - out = sys.stderr - else: - out = sys.stdout - print >> out, 'Usage: %s [options] [command...]\n' % sys.argv[0] - print >> out, '--host <host>\n\tHost to initiate transfer on. Default %s.' % host_default - print >> out, '--port <port>\n\tPort to initiate transfer on. Default %d.' % port_default - print >> out, '--vhost <vhost>\n\tHost to transfer VM to. Default %s.' % vhost_default - print >> out, '--vport <vport>\n\tPort to transfer VM to. Default %d.' % vport_default - print >> out, '--vmid <vmid>\n\tVM id. Default %d.' % vmid_default - print >> out, '--help\n\tPrint help.' - -def main(argv): - global verbose - host = host_default - port = port_default - vhost = vhost_default - vport = vport_default - vmid = vmid_default - - try: - opts, args = getopt.getopt(argv[1:], short_opts, long_opts) - except getopt.GetoptError, ex: - print >>sys.stderr, 'Error:', ex - usage(1) - sys.exit(1) - - for key, val in opts: - if key == '--help': - usage() - sys.exit(0) - elif key == '--host': - host = val - elif key == '--port': - port = int(val) - elif key == '--vhost': - vhost = val - elif key == '--vport': - vport = int(val) - elif key == '--vmid': - vmid = int(val) - - print "host=%s port=%d" % (host, port) - print "vhost=%s vport=%d vmid=%d" % (vhost, vport, vmid) - client = TCPClient(gethostbyname(host), port) - client.request_hello() - client.request_migrate(vmid, gethostbyname(vhost), vport) - client.read() - -if __name__ == '__main__': - main(sys.argv) - |