Annotation of qemu/migration-tcp.c, revision 1.1.1.6

1.1       root        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
1.1.1.4   root       24: #define DPRINTF(fmt, ...) \
1.1       root       25:     do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
                     26: #else
1.1.1.4   root       27: #define DPRINTF(fmt, ...) \
1.1       root       28:     do { } while (0)
                     29: #endif
                     30: 
1.1.1.6 ! root       31: static int socket_errno(MigrationState *s)
1.1       root       32: {
                     33:     return socket_error();
                     34: }
                     35: 
1.1.1.6 ! root       36: static int socket_write(MigrationState *s, const void * buf, size_t size)
1.1       root       37: {
                     38:     return send(s->fd, buf, size, 0);
                     39: }
                     40: 
1.1.1.6 ! root       41: static int tcp_close(MigrationState *s)
1.1       root       42: {
1.1.1.4   root       43:     DPRINTF("tcp_close\n");
1.1       root       44:     if (s->fd != -1) {
                     45:         close(s->fd);
                     46:         s->fd = -1;
                     47:     }
                     48:     return 0;
                     49: }
                     50: 
                     51: static void tcp_wait_for_connect(void *opaque)
                     52: {
1.1.1.6 ! root       53:     MigrationState *s = opaque;
1.1       root       54:     int val, ret;
                     55:     socklen_t valsize = sizeof(val);
                     56: 
1.1.1.4   root       57:     DPRINTF("connect completed\n");
1.1       root       58:     do {
1.1.1.2   root       59:         ret = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
1.1.1.6 ! root       60:     } while (ret == -1 && (socket_error()) == EINTR);
1.1       root       61: 
                     62:     if (ret < 0) {
                     63:         migrate_fd_error(s);
                     64:         return;
                     65:     }
                     66: 
                     67:     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
                     68: 
                     69:     if (val == 0)
                     70:         migrate_fd_connect(s);
                     71:     else {
1.1.1.4   root       72:         DPRINTF("error connecting %d\n", val);
1.1       root       73:         migrate_fd_error(s);
                     74:     }
                     75: }
                     76: 
1.1.1.6 ! root       77: int tcp_start_outgoing_migration(MigrationState *s, const char *host_port)
1.1       root       78: {
                     79:     struct sockaddr_in addr;
                     80:     int ret;
                     81: 
1.1.1.6 ! root       82:     ret = parse_host_port(&addr, host_port);
        !            83:     if (ret < 0) {
        !            84:         return ret;
        !            85:     }
1.1       root       86: 
                     87:     s->get_error = socket_errno;
                     88:     s->write = socket_write;
                     89:     s->close = tcp_close;
1.1.1.6 ! root       90: 
1.1.1.3   root       91:     s->fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
1.1       root       92:     if (s->fd == -1) {
1.1.1.6 ! root       93:         DPRINTF("Unable to open socket");
        !            94:         return -socket_error();
1.1       root       95:     }
                     96: 
                     97:     socket_set_nonblock(s->fd);
                     98: 
                     99:     do {
                    100:         ret = connect(s->fd, (struct sockaddr *)&addr, sizeof(addr));
1.1.1.6 ! root      101:         if (ret == -1) {
        !           102:             ret = -socket_error();
        !           103:         }
        !           104:         if (ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
1.1       root      105:             qemu_set_fd_handler2(s->fd, NULL, NULL, tcp_wait_for_connect, s);
1.1.1.6 ! root      106:             return 0;
        !           107:         }
1.1       root      108:     } while (ret == -EINTR);
                    109: 
1.1.1.6 ! root      110:     if (ret < 0) {
1.1.1.4   root      111:         DPRINTF("connect failed\n");
                    112:         migrate_fd_error(s);
1.1.1.6 ! root      113:         return ret;
        !           114:     }
        !           115:     migrate_fd_connect(s);
        !           116:     return 0;
1.1       root      117: }
                    118: 
                    119: static void tcp_accept_incoming_migration(void *opaque)
                    120: {
                    121:     struct sockaddr_in addr;
                    122:     socklen_t addrlen = sizeof(addr);
1.1.1.5   root      123:     int s = (intptr_t)opaque;
1.1       root      124:     QEMUFile *f;
1.1.1.4   root      125:     int c;
1.1       root      126: 
                    127:     do {
1.1.1.3   root      128:         c = qemu_accept(s, (struct sockaddr *)&addr, &addrlen);
1.1       root      129:     } while (c == -1 && socket_error() == EINTR);
                    130: 
1.1.1.4   root      131:     DPRINTF("accepted migration\n");
1.1       root      132: 
                    133:     if (c == -1) {
                    134:         fprintf(stderr, "could not accept migration connection\n");
1.1.1.4   root      135:         goto out2;
1.1       root      136:     }
                    137: 
                    138:     f = qemu_fopen_socket(c);
                    139:     if (f == NULL) {
                    140:         fprintf(stderr, "could not qemu_fopen socket\n");
                    141:         goto out;
                    142:     }
                    143: 
1.1.1.4   root      144:     process_incoming_migration(f);
1.1       root      145:     qemu_fclose(f);
                    146: out:
                    147:     close(c);
1.1.1.4   root      148: out2:
                    149:     qemu_set_fd_handler2(s, NULL, NULL, NULL, NULL);
                    150:     close(s);
1.1       root      151: }
                    152: 
                    153: int tcp_start_incoming_migration(const char *host_port)
                    154: {
                    155:     struct sockaddr_in addr;
                    156:     int val;
                    157:     int s;
                    158: 
1.1.1.6 ! root      159:     DPRINTF("Attempting to start an incoming migration\n");
        !           160: 
1.1       root      161:     if (parse_host_port(&addr, host_port) < 0) {
                    162:         fprintf(stderr, "invalid host/port combination: %s\n", host_port);
                    163:         return -EINVAL;
                    164:     }
                    165: 
1.1.1.3   root      166:     s = qemu_socket(PF_INET, SOCK_STREAM, 0);
1.1.1.6 ! root      167:     if (s == -1) {
1.1       root      168:         return -socket_error();
1.1.1.6 ! root      169:     }
1.1       root      170: 
                    171:     val = 1;
                    172:     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
                    173: 
1.1.1.6 ! root      174:     if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
1.1       root      175:         goto err;
1.1.1.6 ! root      176:     }
        !           177:     if (listen(s, 1) == -1) {
1.1       root      178:         goto err;
1.1.1.6 ! root      179:     }
1.1       root      180: 
                    181:     qemu_set_fd_handler2(s, NULL, tcp_accept_incoming_migration, NULL,
1.1.1.5   root      182:                          (void *)(intptr_t)s);
1.1       root      183: 
                    184:     return 0;
                    185: 
                    186: err:
                    187:     close(s);
                    188:     return -socket_error();
                    189: }

unix.superglobalmegacorp.com