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"
18 #include "buffered_file.h"
21 //#define DEBUG_MIGRATION_TCP
23 #ifdef DEBUG_MIGRATION_TCP
24 #define DPRINTF(fmt, ...) \
25 do { printf("migration-tcp: " fmt, ## __VA_ARGS__); } while (0)
27 #define DPRINTF(fmt, ...) \
31 static int socket_errno(MigrationState
*s
)
33 return socket_error();
36 static int socket_write(MigrationState
*s
, const void * buf
, size_t size
)
38 return send(s
->fd
, buf
, size
, 0);
41 static int tcp_close(MigrationState
*s
)
43 DPRINTF("tcp_close\n");
52 static void tcp_wait_for_connect(void *opaque
)
54 MigrationState
*s
= opaque
;
56 socklen_t valsize
= sizeof(val
);
58 DPRINTF("connect completed\n");
60 ret
= getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &val
, &valsize
);
61 } while (ret
== -1 && (s
->get_error(s
)) == EINTR
);
68 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
71 migrate_fd_connect(s
);
73 DPRINTF("error connecting %d\n", val
);
78 MigrationState
*tcp_start_outgoing_migration(Monitor
*mon
,
79 const char *host_port
,
80 int64_t bandwidth_limit
,
85 struct sockaddr_in addr
;
89 if (parse_host_port(&addr
, host_port
) < 0)
92 s
= g_malloc0(sizeof(*s
));
94 s
->get_error
= socket_errno
;
95 s
->write
= socket_write
;
97 s
->cancel
= migrate_fd_cancel
;
98 s
->get_status
= migrate_fd_get_status
;
99 s
->release
= migrate_fd_release
;
104 s
->state
= MIG_STATE_ACTIVE
;
106 s
->bandwidth_limit
= bandwidth_limit
;
107 s
->fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
113 socket_set_nonblock(s
->fd
);
116 migrate_fd_monitor_suspend(s
, mon
);
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");
132 migrate_fd_connect(s
);
137 static void tcp_accept_incoming_migration(void *opaque
)
139 struct sockaddr_in addr
;
140 socklen_t addrlen
= sizeof(addr
);
141 int s
= (intptr_t)opaque
;
146 c
= qemu_accept(s
, (struct sockaddr
*)&addr
, &addrlen
);
147 } while (c
== -1 && socket_error() == EINTR
);
149 DPRINTF("accepted migration\n");
152 fprintf(stderr
, "could not accept migration connection\n");
156 f
= qemu_fopen_socket(c
);
158 fprintf(stderr
, "could not qemu_fopen socket\n");
162 process_incoming_migration(f
);
167 qemu_set_fd_handler2(s
, NULL
, NULL
, NULL
, NULL
);
171 int tcp_start_incoming_migration(const char *host_port
)
173 struct sockaddr_in addr
;
177 if (parse_host_port(&addr
, host_port
) < 0) {
178 fprintf(stderr
, "invalid host/port combination: %s\n", host_port
);
182 s
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
184 return -socket_error();
187 setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
189 if (bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) == -1)
192 if (listen(s
, 1) == -1)
195 qemu_set_fd_handler2(s
, NULL
, tcp_accept_incoming_migration
, NULL
,
196 (void *)(intptr_t)s
);
202 return -socket_error();