File:  [Qemu by Fabrice Bellard] / qemu / migration-tcp.c
Revision 1.1.1.5 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 18:55:31 2018 UTC (3 years, 1 month ago) by root
Branches: qemu, MAIN
CVS tags: qemu1000, qemu0151, HEAD
qemu 0.15.1

    1: /*
    2:  * QEMU live migration
    3:  *
    4:  * Copyright IBM, Corp. 2008
    5:  *
    6:  * Authors:
    7:  *  Anthony Liguori   <aliguori@us.ibm.com>
    8:  *
    9:  * This work is licensed under the terms of the GNU GPL, version 2.  See
   10:  * the COPYING file in the top-level directory.
   11:  *
   12:  */
   13: 
   14: #include "qemu-common.h"
   15: #include "qemu_socket.h"
   16: #include "migration.h"
   17: #include "qemu-char.h"
   18: #include "buffered_file.h"
   19: #include "block.h"
   20: 
   21: //#define DEBUG_MIGRATION_TCP
   22: 
   23: #ifdef DEBUG_MIGRATION_TCP
   24: #define DPRINTF(fmt, ...) \
   25:     do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
   26: #else
   27: #define DPRINTF(fmt, ...) \
   28:     do { } while (0)
   29: #endif
   30: 
   31: static int socket_errno(FdMigrationState *s)
   32: {
   33:     return socket_error();
   34: }
   35: 
   36: static int socket_write(FdMigrationState *s, const void * buf, size_t size)
   37: {
   38:     return send(s->fd, buf, size, 0);
   39: }
   40: 
   41: static int tcp_close(FdMigrationState *s)
   42: {
   43:     DPRINTF("tcp_close\n");
   44:     if (s->fd != -1) {
   45:         close(s->fd);
   46:         s->fd = -1;
   47:     }
   48:     return 0;
   49: }
   50: 
   51: 
   52: static void tcp_wait_for_connect(void *opaque)
   53: {
   54:     FdMigrationState *s = opaque;
   55:     int val, ret;
   56:     socklen_t valsize = sizeof(val);
   57: 
   58:     DPRINTF("connect completed\n");
   59:     do {
   60:         ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
   61:     } while (ret == -1 && (s->get_error(s)) == EINTR);
   62: 
   63:     if (ret < 0) {
   64:         migrate_fd_error(s);
   65:         return;
   66:     }
   67: 
   68:     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
   69: 
   70:     if (val == 0)
   71:         migrate_fd_connect(s);
   72:     else {
   73:         DPRINTF("error connecting %d\n", val);
   74:         migrate_fd_error(s);
   75:     }
   76: }
   77: 
   78: MigrationState *tcp_start_outgoing_migration(Monitor *mon,
   79:                                              const char *host_port,
   80:                                              int64_t bandwidth_limit,
   81:                                              int detach,
   82: 					     int blk,
   83: 					     int inc)
   84: {
   85:     struct sockaddr_in addr;
   86:     FdMigrationState *s;
   87:     int ret;
   88: 
   89:     if (parse_host_port(&addr, host_port) < 0)
   90:         return NULL;
   91: 
   92:     s = qemu_mallocz(sizeof(*s));
   93: 
   94:     s->get_error = socket_errno;
   95:     s->write = socket_write;
   96:     s->close = tcp_close;
   97:     s->mig_state.cancel = migrate_fd_cancel;
   98:     s->mig_state.get_status = migrate_fd_get_status;
   99:     s->mig_state.release = migrate_fd_release;
  100: 
  101:     s->mig_state.blk = blk;
  102:     s->mig_state.shared = inc;
  103: 
  104:     s->state = MIG_STATE_ACTIVE;
  105:     s->mon = NULL;
  106:     s->bandwidth_limit = bandwidth_limit;
  107:     s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
  108:     if (s->fd == -1) {
  109:         qemu_free(s);
  110:         return NULL;
  111:     }
  112: 
  113:     socket_set_nonblock(s->fd);
  114: 
  115:     if (!detach) {
  116:         migrate_fd_monitor_suspend(s, mon);
  117:     }
  118: 
  119:     do {
  120:         ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
  121:         if (ret == -1)
  122:             ret = -(s->get_error(s));
  123: 
  124:         if (ret == -EINPROGRESS || ret == -EWOULDBLOCK)
  125:             qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
  126:     } while (ret == -EINTR);
  127: 
  128:     if (ret < 0 && ret != -EINPROGRESS && ret != -EWOULDBLOCK) {
  129:         DPRINTF("connect failed\n");
  130:         migrate_fd_error(s);
  131:     } else if (ret >= 0)
  132:         migrate_fd_connect(s);
  133: 
  134:     return &s->mig_state;
  135: }
  136: 
  137: static void tcp_accept_incoming_migration(void *opaque)
  138: {
  139:     struct sockaddr_in addr;
  140:     socklen_t addrlen = sizeof(addr);
  141:     int s = (intptr_t)opaque;
  142:     QEMUFile *f;
  143:     int c;
  144: 
  145:     do {
  146:         c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
  147:     } while (c == -1 && socket_error() == EINTR);
  148: 
  149:     DPRINTF("accepted migration\n");
  150: 
  151:     if (c == -1) {
  152:         fprintf(stderr, "could not accept migration connection\n");
  153:         goto out2;
  154:     }
  155: 
  156:     f = qemu_fopen_socket(c);
  157:     if (f == NULL) {
  158:         fprintf(stderr, "could not qemu_fopen socket\n");
  159:         goto out;
  160:     }
  161: 
  162:     process_incoming_migration(f);
  163:     qemu_fclose(f);
  164: out:
  165:     close(c);
  166: out2:
  167:     qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
  168:     close(s);
  169: }
  170: 
  171: int tcp_start_incoming_migration(const char *host_port)
  172: {
  173:     struct sockaddr_in addr;
  174:     int val;
  175:     int s;
  176: 
  177:     if (parse_host_port(&addr, host_port) < 0) {
  178:         fprintf(stderr, "invalid host/port combination: %s\n", host_port);
  179:         return -EINVAL;
  180:     }
  181: 
  182:     s = qemu_socket(PF_INET, SOCK_STREAM, 0);
  183:     if (s == -1)
  184:         return -socket_error();
  185: 
  186:     val = 1;
  187:     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
  188: 
  189:     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
  190:         goto err;
  191: 
  192:     if (listen(s, 1) == -1)
  193:         goto err;
  194: 
  195:     qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
  196:                          (void *)(intptr_t)s);
  197: 
  198:     return 0;
  199: 
  200: err:
  201:     close(s);
  202:     return -socket_error();
  203: }

unix.superglobalmegacorp.com