4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "qemu_socket.h"
16 #include "migration.h"
17 #include "qemu-char.h"
20 #include "buffered_file.h"
23 //#define DEBUG_MIGRATION_TCP
25 #ifdef DEBUG_MIGRATION_TCP
26 #define dprintf(fmt, ...) \
27 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
29 #define dprintf(fmt, ...) \
33 static int socket_errno(FdMigrationState
*s
)
35 return socket_error();
38 static int socket_write(FdMigrationState
*s
, const void * buf
, size_t size
)
40 return send(s
->fd
, buf
, size
, 0);
43 static int tcp_close(FdMigrationState
*s
)
45 dprintf("tcp_close\n");
54 static void tcp_wait_for_connect(void *opaque
)
56 FdMigrationState
*s
= opaque
;
58 socklen_t valsize
= sizeof(val
);
60 dprintf("connect completed\n");
62 ret
= getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
63 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
70 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
73 migrate_fd_connect(s
);
75 dprintf("error connecting %d\n", val
);
80 MigrationState
*tcp_start_outgoing_migration(const char *host_port
,
81 int64_t bandwidth_limit
,
84 struct sockaddr_in addr
;
88 if (parse_host_port(&addr
, host_port
) < 0)
91 s
= qemu_mallocz(sizeof(*s
));
95 s
->get_error
= socket_errno
;
96 s
->write
= socket_write
;
98 s
->mig_state
.cancel
= migrate_fd_cancel
;
99 s
->mig_state
.get_status
= migrate_fd_get_status
;
100 s
->mig_state
.release
= migrate_fd_release
;
102 s
->state
= MIG_STATE_ACTIVE
;
104 s
->bandwidth_limit
= bandwidth_limit
;
105 s
->fd
= socket(PF_INET
, SOCK_STREAM
, 0);
111 socket_set_nonblock(s
->fd
);
113 if (s
->detach
== 1) {
114 dprintf("detaching from monitor\n");
120 ret
= connect(s
->fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
122 ret
= -(s
->get_error(s
));
124 if (ret
== -EINPROGRESS
|| ret
== -EWOULDBLOCK
)
125 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, tcp_wait_for_connect
, s
);
126 } while (ret
== -EINTR
);
128 if (ret
< 0 && ret
!= -EINPROGRESS
&& ret
!= -EWOULDBLOCK
) {
129 dprintf("connect failed\n");
134 migrate_fd_connect(s
);
136 return &s
->mig_state
;
139 static void tcp_accept_incoming_migration(void *opaque
)
141 struct sockaddr_in addr
;
142 socklen_t addrlen
= sizeof(addr
);
143 int s
= (unsigned long)opaque
;
148 c
= accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
149 } while (c
== -1 && socket_error() == EINTR
);
151 dprintf("accepted migration\n");
154 fprintf(stderr
, "could not accept migration connection\n");
158 f
= qemu_fopen_socket(c
);
160 fprintf(stderr
, "could not qemu_fopen socket\n");
164 vm_stop(0); /* just in case */
165 ret
= qemu_loadvm_state(f
);
167 fprintf(stderr
, "load of migration failed\n");
170 qemu_announce_self();
171 dprintf("successfully loaded vm state\n");
173 /* we've successfully migrated, close the server socket */
174 qemu_set_fd_handler2(s
, NULL
, NULL
, NULL
, NULL
);
185 int tcp_start_incoming_migration(const char *host_port
)
187 struct sockaddr_in addr
;
191 if (parse_host_port(&addr
, host_port
) < 0) {
192 fprintf(stderr
, "invalid host/port combination: %s\n", host_port
);
196 s
= socket(PF_INET
, SOCK_STREAM
, 0);
198 return -socket_error();
201 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
203 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1)
206 if (listen(s
, 1) == -1)
209 qemu_set_fd_handler2(s
, NULL
, tcp_accept_incoming_migration
, NULL
,
210 (void *)(unsigned long)s
);
216 return -socket_error();