aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <signal.h> |
| 27 | #include <time.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <zlib.h> |
| 31 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 32 | /* Needed early for CONFIG_BSD etc. */ |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 33 | #include "config-host.h" |
| 34 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 35 | #ifndef _WIN32 |
| 36 | #include <sys/times.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <termios.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 41 | #include <sys/resource.h> |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 42 | #include <sys/socket.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 43 | #include <net/if.h> |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 44 | #include <dirent.h> |
| 45 | #include <netdb.h> |
| 46 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_BSD |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 48 | #include <sys/stat.h> |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 49 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 50 | #include <libutil.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 51 | #else |
| 52 | #include <util.h> |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 53 | #endif |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 54 | #ifdef __linux__ |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 55 | #include <pty.h> |
| 56 | #include <malloc.h> |
| 57 | #include <linux/rtc.h> |
| 58 | |
| 59 | /* For the benefit of older linux systems which don't supply it, |
| 60 | we use a local copy of hpet.h. */ |
| 61 | /* #include <linux/hpet.h> */ |
| 62 | #include "hpet.h" |
| 63 | |
| 64 | #include <linux/ppdev.h> |
| 65 | #include <linux/parport.h> |
| 66 | #endif |
| 67 | #ifdef __sun__ |
| 68 | #include <sys/stat.h> |
| 69 | #include <sys/ethernet.h> |
| 70 | #include <sys/sockio.h> |
| 71 | #include <netinet/arp.h> |
| 72 | #include <netinet/in.h> |
| 73 | #include <netinet/in_systm.h> |
| 74 | #include <netinet/ip.h> |
| 75 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 76 | #include <netinet/udp.h> |
| 77 | #include <netinet/tcp.h> |
| 78 | #include <net/if.h> |
| 79 | #include <syslog.h> |
| 80 | #include <stropts.h> |
| 81 | #endif |
| 82 | #endif |
| 83 | #endif |
| 84 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 85 | #if defined(__OpenBSD__) |
| 86 | #include <util.h> |
| 87 | #endif |
| 88 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 89 | #include "qemu-common.h" |
| 90 | #include "net.h" |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 91 | #include "net/tap.h" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 92 | #include "net/slirp.h" |
Mark McLoughlin | 5c361cc | 2009-11-25 18:48:55 +0000 | [diff] [blame^] | 93 | #include "net/vde.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 94 | #include "monitor.h" |
| 95 | #include "sysemu.h" |
| 96 | #include "qemu-timer.h" |
| 97 | #include "qemu-char.h" |
| 98 | #include "audio/audio.h" |
| 99 | #include "qemu_socket.h" |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 100 | #include "qemu-log.h" |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 101 | #include "qemu-config.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 102 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 103 | static QTAILQ_HEAD(, VLANState) vlans; |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 104 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 105 | |
| 106 | /***********************************************************/ |
| 107 | /* network device redirectors */ |
| 108 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 109 | #if defined(DEBUG_NET) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 110 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
| 111 | { |
| 112 | int len, i, j, c; |
| 113 | |
| 114 | for(i=0;i<size;i+=16) { |
| 115 | len = size - i; |
| 116 | if (len > 16) |
| 117 | len = 16; |
| 118 | fprintf(f, "%08x ", i); |
| 119 | for(j=0;j<16;j++) { |
| 120 | if (j < len) |
| 121 | fprintf(f, " %02x", buf[i+j]); |
| 122 | else |
| 123 | fprintf(f, " "); |
| 124 | } |
| 125 | fprintf(f, " "); |
| 126 | for(j=0;j<len;j++) { |
| 127 | c = buf[i+j]; |
| 128 | if (c < ' ' || c > '~') |
| 129 | c = '.'; |
| 130 | fprintf(f, "%c", c); |
| 131 | } |
| 132 | fprintf(f, "\n"); |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
| 138 | { |
| 139 | int i; |
| 140 | char *last_char; |
| 141 | long int offset; |
| 142 | |
| 143 | errno = 0; |
| 144 | offset = strtol(p, &last_char, 0); |
| 145 | if (0 == errno && '\0' == *last_char && |
| 146 | offset >= 0 && offset <= 0xFFFFFF) { |
| 147 | macaddr[3] = (offset & 0xFF0000) >> 16; |
| 148 | macaddr[4] = (offset & 0xFF00) >> 8; |
| 149 | macaddr[5] = offset & 0xFF; |
| 150 | return 0; |
| 151 | } else { |
| 152 | for(i = 0; i < 6; i++) { |
| 153 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 154 | if (i == 5) { |
| 155 | if (*p != '\0') |
| 156 | return -1; |
| 157 | } else { |
| 158 | if (*p != ':' && *p != '-') |
| 159 | return -1; |
| 160 | p++; |
| 161 | } |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 170 | { |
| 171 | const char *p, *p1; |
| 172 | int len; |
| 173 | p = *pp; |
| 174 | p1 = strchr(p, sep); |
| 175 | if (!p1) |
| 176 | return -1; |
| 177 | len = p1 - p; |
| 178 | p1++; |
| 179 | if (buf_size > 0) { |
| 180 | if (len > buf_size - 1) |
| 181 | len = buf_size - 1; |
| 182 | memcpy(buf, p, len); |
| 183 | buf[len] = '\0'; |
| 184 | } |
| 185 | *pp = p1; |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 190 | struct sockaddr_in *saddr, |
| 191 | const char *input_str) |
| 192 | { |
| 193 | char *str = strdup(input_str); |
| 194 | char *host_str = str; |
| 195 | char *src_str; |
| 196 | const char *src_str2; |
| 197 | char *ptr; |
| 198 | |
| 199 | /* |
| 200 | * Chop off any extra arguments at the end of the string which |
| 201 | * would start with a comma, then fill in the src port information |
| 202 | * if it was provided else use the "any address" and "any port". |
| 203 | */ |
| 204 | if ((ptr = strchr(str,','))) |
| 205 | *ptr = '\0'; |
| 206 | |
| 207 | if ((src_str = strchr(input_str,'@'))) { |
| 208 | *src_str = '\0'; |
| 209 | src_str++; |
| 210 | } |
| 211 | |
| 212 | if (parse_host_port(haddr, host_str) < 0) |
| 213 | goto fail; |
| 214 | |
| 215 | src_str2 = src_str; |
| 216 | if (!src_str || *src_str == '\0') |
| 217 | src_str2 = ":0"; |
| 218 | |
| 219 | if (parse_host_port(saddr, src_str2) < 0) |
| 220 | goto fail; |
| 221 | |
| 222 | free(str); |
| 223 | return(0); |
| 224 | |
| 225 | fail: |
| 226 | free(str); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 231 | { |
| 232 | char buf[512]; |
| 233 | struct hostent *he; |
| 234 | const char *p, *r; |
| 235 | int port; |
| 236 | |
| 237 | p = str; |
| 238 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 239 | return -1; |
| 240 | saddr->sin_family = AF_INET; |
| 241 | if (buf[0] == '\0') { |
| 242 | saddr->sin_addr.s_addr = 0; |
| 243 | } else { |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 244 | if (qemu_isdigit(buf[0])) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 245 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 246 | return -1; |
| 247 | } else { |
| 248 | if ((he = gethostbyname(buf)) == NULL) |
| 249 | return - 1; |
| 250 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
| 251 | } |
| 252 | } |
| 253 | port = strtol(p, (char **)&r, 0); |
| 254 | if (r == p) |
| 255 | return -1; |
| 256 | saddr->sin_port = htons(port); |
| 257 | return 0; |
| 258 | } |
| 259 | |
aliguori | 7cb7434 | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 260 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
| 261 | { |
| 262 | snprintf(vc->info_str, sizeof(vc->info_str), |
aliguori | 4dda406 | 2009-01-08 19:01:37 +0000 | [diff] [blame] | 263 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
| 264 | vc->model, |
aliguori | 7cb7434 | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 265 | macaddr[0], macaddr[1], macaddr[2], |
| 266 | macaddr[3], macaddr[4], macaddr[5]); |
| 267 | } |
| 268 | |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 269 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
| 270 | { |
| 271 | static int index = 0; |
| 272 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
| 273 | |
| 274 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
| 275 | return; |
| 276 | macaddr->a[0] = 0x52; |
| 277 | macaddr->a[1] = 0x54; |
| 278 | macaddr->a[2] = 0x00; |
| 279 | macaddr->a[3] = 0x12; |
| 280 | macaddr->a[4] = 0x34; |
| 281 | macaddr->a[5] = 0x56 + index++; |
| 282 | } |
| 283 | |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 284 | static char *assign_name(VLANClientState *vc1, const char *model) |
| 285 | { |
| 286 | VLANState *vlan; |
| 287 | char buf[256]; |
| 288 | int id = 0; |
| 289 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 290 | QTAILQ_FOREACH(vlan, &vlans, next) { |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 291 | VLANClientState *vc; |
| 292 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 293 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 294 | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 295 | id++; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 296 | } |
| 297 | } |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
| 301 | |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 302 | return qemu_strdup(buf); |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 305 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 306 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 307 | const uint8_t *data, |
| 308 | size_t size, |
| 309 | void *opaque); |
| 310 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 311 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 312 | const struct iovec *iov, |
| 313 | int iovcnt, |
| 314 | void *opaque); |
| 315 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 316 | VLANClientState *qemu_new_vlan_client(net_client_type type, |
| 317 | VLANState *vlan, |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 318 | VLANClientState *peer, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 319 | const char *model, |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 320 | const char *name, |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 321 | NetCanReceive *can_receive, |
| 322 | NetReceive *receive, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 323 | NetReceive *receive_raw, |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 324 | NetReceiveIOV *receive_iov, |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 325 | NetCleanup *cleanup, |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 326 | void *opaque) |
| 327 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 328 | VLANClientState *vc; |
| 329 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 330 | vc = qemu_mallocz(sizeof(VLANClientState)); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 331 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 332 | vc->type = type; |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 333 | vc->model = qemu_strdup(model); |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 334 | if (name) |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 335 | vc->name = qemu_strdup(name); |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 336 | else |
| 337 | vc->name = assign_name(vc, model); |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 338 | vc->can_receive = can_receive; |
| 339 | vc->receive = receive; |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 340 | vc->receive_raw = receive_raw; |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 341 | vc->receive_iov = receive_iov; |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 342 | vc->cleanup = cleanup; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 343 | vc->opaque = opaque; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 344 | |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 345 | if (vlan) { |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 346 | assert(!peer); |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 347 | vc->vlan = vlan; |
| 348 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 349 | } else { |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 350 | if (peer) { |
| 351 | vc->peer = peer; |
| 352 | peer->peer = vc; |
| 353 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 354 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 355 | |
| 356 | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
| 357 | qemu_deliver_packet_iov, |
| 358 | vc); |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 359 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 360 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 361 | return vc; |
| 362 | } |
| 363 | |
| 364 | void qemu_del_vlan_client(VLANClientState *vc) |
| 365 | { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 366 | if (vc->vlan) { |
| 367 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 368 | } else { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 369 | if (vc->send_queue) { |
| 370 | qemu_del_net_queue(vc->send_queue); |
| 371 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 372 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 373 | if (vc->peer) { |
| 374 | vc->peer->peer = NULL; |
| 375 | } |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 376 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 377 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 378 | if (vc->cleanup) { |
| 379 | vc->cleanup(vc); |
| 380 | } |
| 381 | |
| 382 | qemu_free(vc->name); |
| 383 | qemu_free(vc->model); |
| 384 | qemu_free(vc); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 385 | } |
| 386 | |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 387 | VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque) |
| 388 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 389 | VLANClientState *vc; |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 390 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 391 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 392 | if (vc->opaque == opaque) { |
| 393 | return vc; |
| 394 | } |
| 395 | } |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 396 | |
| 397 | return NULL; |
| 398 | } |
| 399 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 400 | VLANClientState * |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 401 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, |
| 402 | const char *client_str) |
| 403 | { |
| 404 | VLANState *vlan; |
| 405 | VLANClientState *vc; |
| 406 | |
| 407 | vlan = qemu_find_vlan(vlan_id, 0); |
| 408 | if (!vlan) { |
| 409 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); |
| 410 | return NULL; |
| 411 | } |
| 412 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 413 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 414 | if (!strcmp(vc->name, client_str)) { |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | if (!vc) { |
| 419 | monitor_printf(mon, "can't find device %s on VLAN %d\n", |
| 420 | client_str, vlan_id); |
| 421 | } |
| 422 | |
| 423 | return vc; |
| 424 | } |
| 425 | |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 426 | int qemu_can_send_packet(VLANClientState *sender) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 427 | { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 428 | VLANState *vlan = sender->vlan; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 429 | VLANClientState *vc; |
| 430 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 431 | if (sender->peer) { |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 432 | if (sender->peer->receive_disabled) { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 433 | return 0; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 434 | } else if (sender->peer->can_receive && |
| 435 | !sender->peer->can_receive(sender->peer)) { |
| 436 | return 0; |
| 437 | } else { |
| 438 | return 1; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 442 | if (!sender->vlan) { |
| 443 | return 1; |
| 444 | } |
| 445 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 446 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 447 | if (vc == sender) { |
| 448 | continue; |
| 449 | } |
| 450 | |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 451 | /* no can_receive() handler, they can always receive */ |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 452 | if (!vc->can_receive || vc->can_receive(vc)) { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 453 | return 1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 459 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 460 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 461 | const uint8_t *data, |
| 462 | size_t size, |
| 463 | void *opaque) |
| 464 | { |
| 465 | VLANClientState *vc = opaque; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 466 | ssize_t ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 467 | |
| 468 | if (vc->link_down) { |
| 469 | return size; |
| 470 | } |
| 471 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 472 | if (vc->receive_disabled) { |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { |
| 477 | ret = vc->receive_raw(vc, data, size); |
| 478 | } else { |
| 479 | ret = vc->receive(vc, data, size); |
| 480 | } |
| 481 | |
| 482 | if (ret == 0) { |
| 483 | vc->receive_disabled = 1; |
| 484 | }; |
| 485 | |
| 486 | return ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 489 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 490 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 491 | const uint8_t *buf, |
| 492 | size_t size, |
| 493 | void *opaque) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 494 | { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 495 | VLANState *vlan = opaque; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 496 | VLANClientState *vc; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 497 | ssize_t ret = -1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 498 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 499 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 500 | ssize_t len; |
| 501 | |
| 502 | if (vc == sender) { |
| 503 | continue; |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 504 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 505 | |
| 506 | if (vc->link_down) { |
| 507 | ret = size; |
| 508 | continue; |
| 509 | } |
| 510 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 511 | if (vc->receive_disabled) { |
| 512 | ret = 0; |
| 513 | continue; |
| 514 | } |
| 515 | |
| 516 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 517 | len = vc->receive_raw(vc, buf, size); |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 518 | } else { |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 519 | len = vc->receive(vc, buf, size); |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (len == 0) { |
| 523 | vc->receive_disabled = 1; |
| 524 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 525 | |
| 526 | ret = (ret >= 0) ? ret : len; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 527 | |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 528 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 529 | |
| 530 | return ret; |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 533 | void qemu_purge_queued_packets(VLANClientState *vc) |
| 534 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 535 | NetQueue *queue; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 536 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 537 | if (!vc->peer && !vc->vlan) { |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | if (vc->peer) { |
| 542 | queue = vc->peer->send_queue; |
| 543 | } else { |
| 544 | queue = vc->vlan->send_queue; |
| 545 | } |
| 546 | |
| 547 | qemu_net_queue_purge(queue, vc); |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 550 | void qemu_flush_queued_packets(VLANClientState *vc) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 551 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 552 | NetQueue *queue; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 553 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 554 | vc->receive_disabled = 0; |
| 555 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 556 | if (vc->vlan) { |
| 557 | queue = vc->vlan->send_queue; |
| 558 | } else { |
| 559 | queue = vc->send_queue; |
| 560 | } |
| 561 | |
| 562 | qemu_net_queue_flush(queue); |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 563 | } |
| 564 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 565 | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender, |
| 566 | unsigned flags, |
| 567 | const uint8_t *buf, int size, |
| 568 | NetPacketSent *sent_cb) |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 569 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 570 | NetQueue *queue; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 571 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 572 | #ifdef DEBUG_NET |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 573 | printf("qemu_send_packet_async:\n"); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 574 | hex_dump(stdout, buf, size); |
| 575 | #endif |
| 576 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 577 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
| 578 | return size; |
| 579 | } |
| 580 | |
| 581 | if (sender->peer) { |
| 582 | queue = sender->peer->send_queue; |
| 583 | } else { |
| 584 | queue = sender->vlan->send_queue; |
| 585 | } |
| 586 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 587 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
| 588 | } |
| 589 | |
| 590 | ssize_t qemu_send_packet_async(VLANClientState *sender, |
| 591 | const uint8_t *buf, int size, |
| 592 | NetPacketSent *sent_cb) |
| 593 | { |
| 594 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, |
| 595 | buf, size, sent_cb); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
| 599 | { |
| 600 | qemu_send_packet_async(vc, buf, size, NULL); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 603 | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
| 604 | { |
| 605 | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW, |
| 606 | buf, size, NULL); |
| 607 | } |
| 608 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 609 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
| 610 | int iovcnt) |
| 611 | { |
| 612 | uint8_t buffer[4096]; |
| 613 | size_t offset = 0; |
| 614 | int i; |
| 615 | |
| 616 | for (i = 0; i < iovcnt; i++) { |
| 617 | size_t len; |
| 618 | |
| 619 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); |
| 620 | memcpy(buffer + offset, iov[i].iov_base, len); |
| 621 | offset += len; |
| 622 | } |
| 623 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 624 | return vc->receive(vc, buffer, offset); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 625 | } |
| 626 | |
aliguori | e0e7877 | 2009-01-26 15:37:44 +0000 | [diff] [blame] | 627 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
| 628 | { |
| 629 | size_t offset = 0; |
| 630 | int i; |
| 631 | |
| 632 | for (i = 0; i < iovcnt; i++) |
| 633 | offset += iov[i].iov_len; |
| 634 | return offset; |
| 635 | } |
| 636 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 637 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 638 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 639 | const struct iovec *iov, |
| 640 | int iovcnt, |
| 641 | void *opaque) |
| 642 | { |
| 643 | VLANClientState *vc = opaque; |
| 644 | |
| 645 | if (vc->link_down) { |
| 646 | return calc_iov_length(iov, iovcnt); |
| 647 | } |
| 648 | |
| 649 | if (vc->receive_iov) { |
| 650 | return vc->receive_iov(vc, iov, iovcnt); |
| 651 | } else { |
| 652 | return vc_sendv_compat(vc, iov, iovcnt); |
| 653 | } |
| 654 | } |
| 655 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 656 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 657 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 658 | const struct iovec *iov, |
| 659 | int iovcnt, |
| 660 | void *opaque) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 661 | { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 662 | VLANState *vlan = opaque; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 663 | VLANClientState *vc; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 664 | ssize_t ret = -1; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 665 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 666 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 667 | ssize_t len; |
| 668 | |
| 669 | if (vc == sender) { |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | if (vc->link_down) { |
| 674 | ret = calc_iov_length(iov, iovcnt); |
| 675 | continue; |
| 676 | } |
| 677 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 678 | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
| 679 | |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 680 | if (vc->receive_iov) { |
| 681 | len = vc->receive_iov(vc, iov, iovcnt); |
| 682 | } else { |
| 683 | len = vc_sendv_compat(vc, iov, iovcnt); |
| 684 | } |
| 685 | |
| 686 | ret = (ret >= 0) ? ret : len; |
| 687 | } |
| 688 | |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 689 | return ret; |
| 690 | } |
| 691 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 692 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
| 693 | const struct iovec *iov, int iovcnt, |
| 694 | NetPacketSent *sent_cb) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 695 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 696 | NetQueue *queue; |
| 697 | |
| 698 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
aliguori | e0e7877 | 2009-01-26 15:37:44 +0000 | [diff] [blame] | 699 | return calc_iov_length(iov, iovcnt); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 702 | if (sender->peer) { |
| 703 | queue = sender->peer->send_queue; |
| 704 | } else { |
| 705 | queue = sender->vlan->send_queue; |
| 706 | } |
| 707 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 708 | return qemu_net_queue_send_iov(queue, sender, |
| 709 | QEMU_NET_PACKET_FLAG_NONE, |
| 710 | iov, iovcnt, sent_cb); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 713 | ssize_t |
| 714 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
| 715 | { |
| 716 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
| 717 | } |
| 718 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 719 | /* network connection */ |
| 720 | typedef struct NetSocketState { |
| 721 | VLANClientState *vc; |
| 722 | int fd; |
| 723 | int state; /* 0 = getting length, 1 = getting data */ |
aliguori | abcd2ba | 2009-02-27 19:54:01 +0000 | [diff] [blame] | 724 | unsigned int index; |
| 725 | unsigned int packet_len; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 726 | uint8_t buf[4096]; |
| 727 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
| 728 | } NetSocketState; |
| 729 | |
| 730 | typedef struct NetSocketListenState { |
| 731 | VLANState *vlan; |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 732 | char *model; |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 733 | char *name; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 734 | int fd; |
| 735 | } NetSocketListenState; |
| 736 | |
| 737 | /* XXX: we consider we can send the whole packet without blocking */ |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 738 | static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 739 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 740 | NetSocketState *s = vc->opaque; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 741 | uint32_t len; |
| 742 | len = htonl(size); |
| 743 | |
| 744 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 745 | return send_all(s->fd, buf, size); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 748 | static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 749 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 750 | NetSocketState *s = vc->opaque; |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 751 | |
Stefan Weil | 7050326 | 2009-06-13 13:05:27 +0200 | [diff] [blame] | 752 | return sendto(s->fd, (const void *)buf, size, 0, |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 753 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | static void net_socket_send(void *opaque) |
| 757 | { |
| 758 | NetSocketState *s = opaque; |
aliguori | abcd2ba | 2009-02-27 19:54:01 +0000 | [diff] [blame] | 759 | int size, err; |
| 760 | unsigned l; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 761 | uint8_t buf1[4096]; |
| 762 | const uint8_t *buf; |
| 763 | |
Blue Swirl | c5b76b3 | 2009-06-13 08:44:31 +0000 | [diff] [blame] | 764 | size = recv(s->fd, (void *)buf1, sizeof(buf1), 0); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 765 | if (size < 0) { |
| 766 | err = socket_error(); |
| 767 | if (err != EWOULDBLOCK) |
| 768 | goto eoc; |
| 769 | } else if (size == 0) { |
| 770 | /* end of connection */ |
| 771 | eoc: |
| 772 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 773 | closesocket(s->fd); |
| 774 | return; |
| 775 | } |
| 776 | buf = buf1; |
| 777 | while (size > 0) { |
| 778 | /* reassemble a packet from the network */ |
| 779 | switch(s->state) { |
| 780 | case 0: |
| 781 | l = 4 - s->index; |
| 782 | if (l > size) |
| 783 | l = size; |
| 784 | memcpy(s->buf + s->index, buf, l); |
| 785 | buf += l; |
| 786 | size -= l; |
| 787 | s->index += l; |
| 788 | if (s->index == 4) { |
| 789 | /* got length */ |
| 790 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 791 | s->index = 0; |
| 792 | s->state = 1; |
| 793 | } |
| 794 | break; |
| 795 | case 1: |
| 796 | l = s->packet_len - s->index; |
| 797 | if (l > size) |
| 798 | l = size; |
aliguori | abcd2ba | 2009-02-27 19:54:01 +0000 | [diff] [blame] | 799 | if (s->index + l <= sizeof(s->buf)) { |
| 800 | memcpy(s->buf + s->index, buf, l); |
| 801 | } else { |
| 802 | fprintf(stderr, "serious error: oversized packet received," |
| 803 | "connection terminated.\n"); |
| 804 | s->state = 0; |
| 805 | goto eoc; |
| 806 | } |
| 807 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 808 | s->index += l; |
| 809 | buf += l; |
| 810 | size -= l; |
| 811 | if (s->index >= s->packet_len) { |
| 812 | qemu_send_packet(s->vc, s->buf, s->packet_len); |
| 813 | s->index = 0; |
| 814 | s->state = 0; |
| 815 | } |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | static void net_socket_send_dgram(void *opaque) |
| 822 | { |
| 823 | NetSocketState *s = opaque; |
| 824 | int size; |
| 825 | |
Blue Swirl | c5b76b3 | 2009-06-13 08:44:31 +0000 | [diff] [blame] | 826 | size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 827 | if (size < 0) |
| 828 | return; |
| 829 | if (size == 0) { |
| 830 | /* end of connection */ |
| 831 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 832 | return; |
| 833 | } |
| 834 | qemu_send_packet(s->vc, s->buf, size); |
| 835 | } |
| 836 | |
| 837 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
| 838 | { |
| 839 | struct ip_mreq imr; |
| 840 | int fd; |
| 841 | int val, ret; |
| 842 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
| 843 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", |
| 844 | inet_ntoa(mcastaddr->sin_addr), |
| 845 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
| 846 | return -1; |
| 847 | |
| 848 | } |
| 849 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 850 | if (fd < 0) { |
| 851 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | val = 1; |
| 856 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 857 | (const char *)&val, sizeof(val)); |
| 858 | if (ret < 0) { |
| 859 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 860 | goto fail; |
| 861 | } |
| 862 | |
| 863 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 864 | if (ret < 0) { |
| 865 | perror("bind"); |
| 866 | goto fail; |
| 867 | } |
| 868 | |
| 869 | /* Add host to multicast group */ |
| 870 | imr.imr_multiaddr = mcastaddr->sin_addr; |
| 871 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 872 | |
| 873 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 874 | (const char *)&imr, sizeof(struct ip_mreq)); |
| 875 | if (ret < 0) { |
| 876 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 877 | goto fail; |
| 878 | } |
| 879 | |
| 880 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
| 881 | val = 1; |
| 882 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 883 | (const char *)&val, sizeof(val)); |
| 884 | if (ret < 0) { |
| 885 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 886 | goto fail; |
| 887 | } |
| 888 | |
| 889 | socket_set_nonblock(fd); |
| 890 | return fd; |
| 891 | fail: |
| 892 | if (fd >= 0) |
| 893 | closesocket(fd); |
| 894 | return -1; |
| 895 | } |
| 896 | |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 897 | static void net_socket_cleanup(VLANClientState *vc) |
| 898 | { |
| 899 | NetSocketState *s = vc->opaque; |
| 900 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 901 | close(s->fd); |
| 902 | qemu_free(s); |
| 903 | } |
| 904 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 905 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, |
| 906 | const char *model, |
| 907 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 908 | int fd, int is_connected) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 909 | { |
| 910 | struct sockaddr_in saddr; |
| 911 | int newfd; |
| 912 | socklen_t saddr_len; |
| 913 | NetSocketState *s; |
| 914 | |
| 915 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 916 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 917 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 918 | */ |
| 919 | |
| 920 | if (is_connected) { |
| 921 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 922 | /* must be bound */ |
| 923 | if (saddr.sin_addr.s_addr==0) { |
| 924 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
| 925 | fd); |
| 926 | return NULL; |
| 927 | } |
| 928 | /* clone dgram socket */ |
| 929 | newfd = net_socket_mcast_create(&saddr); |
| 930 | if (newfd < 0) { |
| 931 | /* error already reported by net_socket_mcast_create() */ |
| 932 | close(fd); |
| 933 | return NULL; |
| 934 | } |
| 935 | /* clone newfd to fd, close newfd */ |
| 936 | dup2(newfd, fd); |
| 937 | close(newfd); |
| 938 | |
| 939 | } else { |
| 940 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 941 | fd, strerror(errno)); |
| 942 | return NULL; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | s = qemu_mallocz(sizeof(NetSocketState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 947 | s->fd = fd; |
| 948 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 949 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
| 950 | vlan, NULL, model, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 951 | net_socket_receive_dgram, NULL, NULL, |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 952 | net_socket_cleanup, s); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 953 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 954 | |
| 955 | /* mcast: save bound address as dst */ |
| 956 | if (is_connected) s->dgram_dst=saddr; |
| 957 | |
| 958 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 959 | "socket: fd=%d (%s mcast=%s:%d)", |
| 960 | fd, is_connected? "cloned" : "", |
| 961 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 962 | return s; |
| 963 | } |
| 964 | |
| 965 | static void net_socket_connect(void *opaque) |
| 966 | { |
| 967 | NetSocketState *s = opaque; |
| 968 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 969 | } |
| 970 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 971 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, |
| 972 | const char *model, |
| 973 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 974 | int fd, int is_connected) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 975 | { |
| 976 | NetSocketState *s; |
| 977 | s = qemu_mallocz(sizeof(NetSocketState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 978 | s->fd = fd; |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 979 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
| 980 | vlan, NULL, model, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 981 | net_socket_receive, NULL, NULL, |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 982 | net_socket_cleanup, s); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 983 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 984 | "socket: fd=%d", fd); |
| 985 | if (is_connected) { |
| 986 | net_socket_connect(s); |
| 987 | } else { |
| 988 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 989 | } |
| 990 | return s; |
| 991 | } |
| 992 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 993 | static NetSocketState *net_socket_fd_init(VLANState *vlan, |
| 994 | const char *model, const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 995 | int fd, int is_connected) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 996 | { |
Michael S. Tsirkin | acedcfb | 2009-09-30 18:56:44 +0000 | [diff] [blame] | 997 | int so_type = -1, optlen=sizeof(so_type); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 998 | |
| 999 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
| 1000 | (socklen_t *)&optlen)< 0) { |
| 1001 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | switch(so_type) { |
| 1005 | case SOCK_DGRAM: |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1006 | return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1007 | case SOCK_STREAM: |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1008 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1009 | default: |
| 1010 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 1011 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1012 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1013 | } |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
| 1017 | static void net_socket_accept(void *opaque) |
| 1018 | { |
| 1019 | NetSocketListenState *s = opaque; |
| 1020 | NetSocketState *s1; |
| 1021 | struct sockaddr_in saddr; |
| 1022 | socklen_t len; |
| 1023 | int fd; |
| 1024 | |
| 1025 | for(;;) { |
| 1026 | len = sizeof(saddr); |
| 1027 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
| 1028 | if (fd < 0 && errno != EINTR) { |
| 1029 | return; |
| 1030 | } else if (fd >= 0) { |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1034 | s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1035 | if (!s1) { |
| 1036 | closesocket(fd); |
| 1037 | } else { |
| 1038 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
| 1039 | "socket: connection from %s:%d", |
| 1040 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 1041 | } |
| 1042 | } |
| 1043 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1044 | static int net_socket_listen_init(VLANState *vlan, |
| 1045 | const char *model, |
| 1046 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1047 | const char *host_str) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1048 | { |
| 1049 | NetSocketListenState *s; |
| 1050 | int fd, val, ret; |
| 1051 | struct sockaddr_in saddr; |
| 1052 | |
| 1053 | if (parse_host_port(&saddr, host_str) < 0) |
| 1054 | return -1; |
| 1055 | |
| 1056 | s = qemu_mallocz(sizeof(NetSocketListenState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1057 | |
| 1058 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 1059 | if (fd < 0) { |
| 1060 | perror("socket"); |
| 1061 | return -1; |
| 1062 | } |
| 1063 | socket_set_nonblock(fd); |
| 1064 | |
| 1065 | /* allow fast reuse */ |
| 1066 | val = 1; |
| 1067 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 1068 | |
| 1069 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 1070 | if (ret < 0) { |
| 1071 | perror("bind"); |
| 1072 | return -1; |
| 1073 | } |
| 1074 | ret = listen(fd, 0); |
| 1075 | if (ret < 0) { |
| 1076 | perror("listen"); |
| 1077 | return -1; |
| 1078 | } |
| 1079 | s->vlan = vlan; |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 1080 | s->model = qemu_strdup(model); |
| 1081 | s->name = name ? qemu_strdup(name) : NULL; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1082 | s->fd = fd; |
| 1083 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1087 | static int net_socket_connect_init(VLANState *vlan, |
| 1088 | const char *model, |
| 1089 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1090 | const char *host_str) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1091 | { |
| 1092 | NetSocketState *s; |
| 1093 | int fd, connected, ret, err; |
| 1094 | struct sockaddr_in saddr; |
| 1095 | |
| 1096 | if (parse_host_port(&saddr, host_str) < 0) |
| 1097 | return -1; |
| 1098 | |
| 1099 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 1100 | if (fd < 0) { |
| 1101 | perror("socket"); |
| 1102 | return -1; |
| 1103 | } |
| 1104 | socket_set_nonblock(fd); |
| 1105 | |
| 1106 | connected = 0; |
| 1107 | for(;;) { |
| 1108 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 1109 | if (ret < 0) { |
| 1110 | err = socket_error(); |
| 1111 | if (err == EINTR || err == EWOULDBLOCK) { |
| 1112 | } else if (err == EINPROGRESS) { |
| 1113 | break; |
| 1114 | #ifdef _WIN32 |
| 1115 | } else if (err == WSAEALREADY) { |
| 1116 | break; |
| 1117 | #endif |
| 1118 | } else { |
| 1119 | perror("connect"); |
| 1120 | closesocket(fd); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | } else { |
| 1124 | connected = 1; |
| 1125 | break; |
| 1126 | } |
| 1127 | } |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1128 | s = net_socket_fd_init(vlan, model, name, fd, connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1129 | if (!s) |
| 1130 | return -1; |
| 1131 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 1132 | "socket: connect to %s:%d", |
| 1133 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1137 | static int net_socket_mcast_init(VLANState *vlan, |
| 1138 | const char *model, |
| 1139 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1140 | const char *host_str) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1141 | { |
| 1142 | NetSocketState *s; |
| 1143 | int fd; |
| 1144 | struct sockaddr_in saddr; |
| 1145 | |
| 1146 | if (parse_host_port(&saddr, host_str) < 0) |
| 1147 | return -1; |
| 1148 | |
| 1149 | |
| 1150 | fd = net_socket_mcast_create(&saddr); |
| 1151 | if (fd < 0) |
| 1152 | return -1; |
| 1153 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1154 | s = net_socket_fd_init(vlan, model, name, fd, 0); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1155 | if (!s) |
| 1156 | return -1; |
| 1157 | |
| 1158 | s->dgram_dst = saddr; |
| 1159 | |
| 1160 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 1161 | "socket: mcast=%s:%d", |
| 1162 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 1163 | return 0; |
| 1164 | |
| 1165 | } |
| 1166 | |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1167 | typedef struct DumpState { |
| 1168 | VLANClientState *pcap_vc; |
| 1169 | int fd; |
| 1170 | int pcap_caplen; |
| 1171 | } DumpState; |
| 1172 | |
| 1173 | #define PCAP_MAGIC 0xa1b2c3d4 |
| 1174 | |
| 1175 | struct pcap_file_hdr { |
| 1176 | uint32_t magic; |
| 1177 | uint16_t version_major; |
| 1178 | uint16_t version_minor; |
| 1179 | int32_t thiszone; |
| 1180 | uint32_t sigfigs; |
| 1181 | uint32_t snaplen; |
| 1182 | uint32_t linktype; |
| 1183 | }; |
| 1184 | |
| 1185 | struct pcap_sf_pkthdr { |
| 1186 | struct { |
| 1187 | int32_t tv_sec; |
| 1188 | int32_t tv_usec; |
| 1189 | } ts; |
| 1190 | uint32_t caplen; |
| 1191 | uint32_t len; |
| 1192 | }; |
| 1193 | |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1194 | static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1195 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 1196 | DumpState *s = vc->opaque; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1197 | struct pcap_sf_pkthdr hdr; |
| 1198 | int64_t ts; |
| 1199 | int caplen; |
| 1200 | |
| 1201 | /* Early return in case of previous error. */ |
| 1202 | if (s->fd < 0) { |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1203 | return size; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 1206 | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec()); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1207 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
| 1208 | |
Jan Kiszka | 37cb6fc | 2009-05-10 22:23:23 +0200 | [diff] [blame] | 1209 | hdr.ts.tv_sec = ts / 1000000; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1210 | hdr.ts.tv_usec = ts % 1000000; |
| 1211 | hdr.caplen = caplen; |
| 1212 | hdr.len = size; |
| 1213 | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || |
| 1214 | write(s->fd, buf, caplen) != caplen) { |
| 1215 | qemu_log("-net dump write error - stop dump\n"); |
| 1216 | close(s->fd); |
| 1217 | s->fd = -1; |
| 1218 | } |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1219 | |
| 1220 | return size; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | static void net_dump_cleanup(VLANClientState *vc) |
| 1224 | { |
| 1225 | DumpState *s = vc->opaque; |
| 1226 | |
| 1227 | close(s->fd); |
| 1228 | qemu_free(s); |
| 1229 | } |
| 1230 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1231 | static int net_dump_init(VLANState *vlan, const char *device, |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1232 | const char *name, const char *filename, int len) |
| 1233 | { |
| 1234 | struct pcap_file_hdr hdr; |
| 1235 | DumpState *s; |
| 1236 | |
| 1237 | s = qemu_malloc(sizeof(DumpState)); |
| 1238 | |
Filip Navara | 024431b | 2009-06-17 19:48:08 +0200 | [diff] [blame] | 1239 | s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1240 | if (s->fd < 0) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1241 | qemu_error("-net dump: can't open %s\n", filename); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1242 | return -1; |
| 1243 | } |
| 1244 | |
| 1245 | s->pcap_caplen = len; |
| 1246 | |
| 1247 | hdr.magic = PCAP_MAGIC; |
| 1248 | hdr.version_major = 2; |
| 1249 | hdr.version_minor = 4; |
| 1250 | hdr.thiszone = 0; |
| 1251 | hdr.sigfigs = 0; |
| 1252 | hdr.snaplen = s->pcap_caplen; |
| 1253 | hdr.linktype = 1; |
| 1254 | |
| 1255 | if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1256 | qemu_error("-net dump write error: %s\n", strerror(errno)); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1257 | close(s->fd); |
| 1258 | qemu_free(s); |
| 1259 | return -1; |
| 1260 | } |
| 1261 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 1262 | s->pcap_vc = qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP, |
| 1263 | vlan, NULL, device, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 1264 | dump_receive, NULL, NULL, |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1265 | net_dump_cleanup, s); |
| 1266 | snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str), |
| 1267 | "dump to %s (len=%d)", filename, len); |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1271 | /* find or alloc a new VLAN */ |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1272 | VLANState *qemu_find_vlan(int id, int allocate) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1273 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1274 | VLANState *vlan; |
| 1275 | |
| 1276 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 1277 | if (vlan->id == id) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1278 | return vlan; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1279 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1280 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1281 | |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1282 | if (!allocate) { |
| 1283 | return NULL; |
| 1284 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1285 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1286 | vlan = qemu_mallocz(sizeof(VLANState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1287 | vlan->id = id; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1288 | QTAILQ_INIT(&vlan->clients); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 1289 | |
| 1290 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
| 1291 | qemu_vlan_deliver_packet_iov, |
| 1292 | vlan); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1293 | |
| 1294 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
| 1295 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1296 | return vlan; |
| 1297 | } |
| 1298 | |
Gerd Hoffmann | 2ef924b | 2009-10-21 15:25:24 +0200 | [diff] [blame] | 1299 | VLANClientState *qemu_find_netdev(const char *id) |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1300 | { |
| 1301 | VLANClientState *vc; |
| 1302 | |
| 1303 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
| 1304 | if (!strcmp(vc->name, id)) { |
| 1305 | return vc; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
aliguori | 7697079 | 2009-02-11 15:20:03 +0000 | [diff] [blame] | 1312 | static int nic_get_free_idx(void) |
| 1313 | { |
| 1314 | int index; |
| 1315 | |
| 1316 | for (index = 0; index < MAX_NICS; index++) |
| 1317 | if (!nd_table[index].used) |
| 1318 | return index; |
| 1319 | return -1; |
| 1320 | } |
| 1321 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 1322 | int qemu_show_nic_models(const char *arg, const char *const *models) |
| 1323 | { |
| 1324 | int i; |
| 1325 | |
| 1326 | if (!arg || strcmp(arg, "?")) |
| 1327 | return 0; |
| 1328 | |
| 1329 | fprintf(stderr, "qemu: Supported NIC models: "); |
| 1330 | for (i = 0 ; models[i]; i++) |
| 1331 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
| 1332 | return 1; |
| 1333 | } |
| 1334 | |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 1335 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
| 1336 | { |
| 1337 | const char *models[2]; |
| 1338 | |
| 1339 | models[0] = model; |
| 1340 | models[1] = NULL; |
| 1341 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 1342 | if (qemu_show_nic_models(nd->model, models)) |
| 1343 | exit(0); |
| 1344 | if (qemu_find_nic_model(nd, models, model) < 0) |
| 1345 | exit(1); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 1348 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
| 1349 | const char *default_model) |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 1350 | { |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 1351 | int i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 1352 | |
| 1353 | if (!nd->model) |
Mark McLoughlin | 32a8e14 | 2009-10-06 12:16:51 +0100 | [diff] [blame] | 1354 | nd->model = qemu_strdup(default_model); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 1355 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 1356 | for (i = 0 ; models[i]; i++) { |
| 1357 | if (strcmp(nd->model, models[i]) == 0) |
| 1358 | return i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 1361 | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model); |
| 1362 | return -1; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 1365 | int net_handle_fd_param(Monitor *mon, const char *param) |
Mark McLoughlin | c1d6eed | 2009-07-22 09:11:42 +0100 | [diff] [blame] | 1366 | { |
| 1367 | if (!qemu_isdigit(param[0])) { |
| 1368 | int fd; |
| 1369 | |
| 1370 | fd = monitor_get_fd(mon, param); |
| 1371 | if (fd == -1) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1372 | qemu_error("No file descriptor named %s found", param); |
Mark McLoughlin | c1d6eed | 2009-07-22 09:11:42 +0100 | [diff] [blame] | 1373 | return -1; |
| 1374 | } |
| 1375 | |
| 1376 | return fd; |
| 1377 | } else { |
| 1378 | return strtol(param, NULL, 0); |
| 1379 | } |
| 1380 | } |
| 1381 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1382 | static int net_init_nic(QemuOpts *opts, |
| 1383 | Monitor *mon, |
| 1384 | const char *name, |
| 1385 | VLANState *vlan) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1386 | { |
| 1387 | int idx; |
| 1388 | NICInfo *nd; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1389 | const char *netdev; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1390 | |
| 1391 | idx = nic_get_free_idx(); |
| 1392 | if (idx == -1 || nb_nics >= MAX_NICS) { |
| 1393 | qemu_error("Too Many NICs\n"); |
| 1394 | return -1; |
| 1395 | } |
| 1396 | |
| 1397 | nd = &nd_table[idx]; |
| 1398 | |
| 1399 | memset(nd, 0, sizeof(*nd)); |
| 1400 | |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1401 | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
| 1402 | nd->netdev = qemu_find_netdev(netdev); |
| 1403 | if (!nd->netdev) { |
| 1404 | qemu_error("netdev '%s' not found\n", netdev); |
| 1405 | return -1; |
| 1406 | } |
| 1407 | } else { |
| 1408 | assert(vlan); |
| 1409 | nd->vlan = vlan; |
| 1410 | } |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 1411 | if (name) { |
| 1412 | nd->name = qemu_strdup(name); |
| 1413 | } |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1414 | if (qemu_opt_get(opts, "model")) { |
| 1415 | nd->model = qemu_strdup(qemu_opt_get(opts, "model")); |
| 1416 | } |
| 1417 | if (qemu_opt_get(opts, "addr")) { |
| 1418 | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr")); |
| 1419 | } |
| 1420 | |
| 1421 | nd->macaddr[0] = 0x52; |
| 1422 | nd->macaddr[1] = 0x54; |
| 1423 | nd->macaddr[2] = 0x00; |
| 1424 | nd->macaddr[3] = 0x12; |
| 1425 | nd->macaddr[4] = 0x34; |
| 1426 | nd->macaddr[5] = 0x56 + idx; |
| 1427 | |
| 1428 | if (qemu_opt_get(opts, "macaddr") && |
| 1429 | parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
| 1430 | qemu_error("invalid syntax for ethernet address\n"); |
| 1431 | return -1; |
| 1432 | } |
| 1433 | |
| 1434 | nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED); |
| 1435 | if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED && |
| 1436 | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
| 1437 | qemu_error("invalid # of vectors: %d\n", nd->nvectors); |
| 1438 | return -1; |
| 1439 | } |
| 1440 | |
| 1441 | nd->used = 1; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1442 | if (vlan) { |
| 1443 | nd->vlan->nb_guest_devs++; |
| 1444 | } |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1445 | nb_nics++; |
| 1446 | |
| 1447 | return idx; |
| 1448 | } |
| 1449 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1450 | static int net_init_socket(QemuOpts *opts, |
| 1451 | Monitor *mon, |
| 1452 | const char *name, |
| 1453 | VLANState *vlan) |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 1454 | { |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 1455 | if (qemu_opt_get(opts, "fd")) { |
| 1456 | int fd; |
| 1457 | |
| 1458 | if (qemu_opt_get(opts, "listen") || |
| 1459 | qemu_opt_get(opts, "connect") || |
| 1460 | qemu_opt_get(opts, "mcast")) { |
| 1461 | qemu_error("listen=, connect= and mcast= is invalid with fd=\n"); |
| 1462 | return -1; |
| 1463 | } |
| 1464 | |
| 1465 | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd")); |
| 1466 | if (fd == -1) { |
| 1467 | return -1; |
| 1468 | } |
| 1469 | |
| 1470 | if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { |
| 1471 | close(fd); |
| 1472 | return -1; |
| 1473 | } |
| 1474 | } else if (qemu_opt_get(opts, "listen")) { |
| 1475 | const char *listen; |
| 1476 | |
| 1477 | if (qemu_opt_get(opts, "fd") || |
| 1478 | qemu_opt_get(opts, "connect") || |
| 1479 | qemu_opt_get(opts, "mcast")) { |
| 1480 | qemu_error("fd=, connect= and mcast= is invalid with listen=\n"); |
| 1481 | return -1; |
| 1482 | } |
| 1483 | |
| 1484 | listen = qemu_opt_get(opts, "listen"); |
| 1485 | |
| 1486 | if (net_socket_listen_init(vlan, "socket", name, listen) == -1) { |
| 1487 | return -1; |
| 1488 | } |
| 1489 | } else if (qemu_opt_get(opts, "connect")) { |
| 1490 | const char *connect; |
| 1491 | |
| 1492 | if (qemu_opt_get(opts, "fd") || |
| 1493 | qemu_opt_get(opts, "listen") || |
| 1494 | qemu_opt_get(opts, "mcast")) { |
| 1495 | qemu_error("fd=, listen= and mcast= is invalid with connect=\n"); |
| 1496 | return -1; |
| 1497 | } |
| 1498 | |
| 1499 | connect = qemu_opt_get(opts, "connect"); |
| 1500 | |
| 1501 | if (net_socket_connect_init(vlan, "socket", name, connect) == -1) { |
| 1502 | return -1; |
| 1503 | } |
| 1504 | } else if (qemu_opt_get(opts, "mcast")) { |
| 1505 | const char *mcast; |
| 1506 | |
| 1507 | if (qemu_opt_get(opts, "fd") || |
| 1508 | qemu_opt_get(opts, "connect") || |
| 1509 | qemu_opt_get(opts, "listen")) { |
| 1510 | qemu_error("fd=, connect= and listen= is invalid with mcast=\n"); |
| 1511 | return -1; |
| 1512 | } |
| 1513 | |
| 1514 | mcast = qemu_opt_get(opts, "mcast"); |
| 1515 | |
| 1516 | if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) { |
| 1517 | return -1; |
| 1518 | } |
| 1519 | } else { |
| 1520 | qemu_error("-socket requires fd=, listen=, connect= or mcast=\n"); |
| 1521 | return -1; |
| 1522 | } |
| 1523 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1524 | if (vlan) { |
| 1525 | vlan->nb_host_devs++; |
| 1526 | } |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1531 | static int net_init_dump(QemuOpts *opts, |
| 1532 | Monitor *mon, |
| 1533 | const char *name, |
| 1534 | VLANState *vlan) |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 1535 | { |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 1536 | int len; |
| 1537 | const char *file; |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 1538 | char def_file[128]; |
| 1539 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1540 | assert(vlan); |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 1541 | |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 1542 | file = qemu_opt_get(opts, "file"); |
| 1543 | if (!file) { |
| 1544 | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); |
| 1545 | file = def_file; |
| 1546 | } |
| 1547 | |
| 1548 | len = qemu_opt_get_size(opts, "len", 65536); |
| 1549 | |
| 1550 | return net_dump_init(vlan, "dump", name, file, len); |
| 1551 | } |
| 1552 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1553 | #define NET_COMMON_PARAMS_DESC \ |
| 1554 | { \ |
| 1555 | .name = "type", \ |
| 1556 | .type = QEMU_OPT_STRING, \ |
| 1557 | .help = "net client type (nic, tap etc.)", \ |
| 1558 | }, { \ |
| 1559 | .name = "vlan", \ |
| 1560 | .type = QEMU_OPT_NUMBER, \ |
| 1561 | .help = "vlan number", \ |
| 1562 | }, { \ |
| 1563 | .name = "name", \ |
| 1564 | .type = QEMU_OPT_STRING, \ |
| 1565 | .help = "identifier for monitor commands", \ |
| 1566 | } |
| 1567 | |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 1568 | typedef int (*net_client_init_func)(QemuOpts *opts, |
| 1569 | Monitor *mon, |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1570 | const char *name, |
| 1571 | VLANState *vlan); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1572 | |
| 1573 | /* magic number, but compiler will warn if too small */ |
| 1574 | #define NET_MAX_DESC 20 |
| 1575 | |
| 1576 | static struct { |
| 1577 | const char *type; |
| 1578 | net_client_init_func init; |
| 1579 | QemuOptDesc desc[NET_MAX_DESC]; |
| 1580 | } net_client_types[] = { |
| 1581 | { |
| 1582 | .type = "none", |
| 1583 | .desc = { |
| 1584 | NET_COMMON_PARAMS_DESC, |
| 1585 | { /* end of list */ } |
| 1586 | }, |
| 1587 | }, { |
| 1588 | .type = "nic", |
| 1589 | .init = net_init_nic, |
| 1590 | .desc = { |
| 1591 | NET_COMMON_PARAMS_DESC, |
| 1592 | { |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1593 | .name = "netdev", |
| 1594 | .type = QEMU_OPT_STRING, |
| 1595 | .help = "id of -netdev to connect to", |
| 1596 | }, |
| 1597 | { |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1598 | .name = "macaddr", |
| 1599 | .type = QEMU_OPT_STRING, |
| 1600 | .help = "MAC address", |
| 1601 | }, { |
| 1602 | .name = "model", |
| 1603 | .type = QEMU_OPT_STRING, |
| 1604 | .help = "device model (e1000, rtl8139, virtio etc.)", |
| 1605 | }, { |
| 1606 | .name = "addr", |
| 1607 | .type = QEMU_OPT_STRING, |
| 1608 | .help = "PCI device address", |
| 1609 | }, { |
| 1610 | .name = "vectors", |
| 1611 | .type = QEMU_OPT_NUMBER, |
| 1612 | .help = "number of MSI-x vectors, 0 to disable MSI-X", |
| 1613 | }, |
| 1614 | { /* end of list */ } |
| 1615 | }, |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 1616 | #ifdef CONFIG_SLIRP |
| 1617 | }, { |
| 1618 | .type = "user", |
| 1619 | .init = net_init_slirp, |
| 1620 | .desc = { |
| 1621 | NET_COMMON_PARAMS_DESC, |
| 1622 | { |
| 1623 | .name = "hostname", |
| 1624 | .type = QEMU_OPT_STRING, |
| 1625 | .help = "client hostname reported by the builtin DHCP server", |
| 1626 | }, { |
| 1627 | .name = "restrict", |
| 1628 | .type = QEMU_OPT_STRING, |
| 1629 | .help = "isolate the guest from the host (y|yes|n|no)", |
| 1630 | }, { |
| 1631 | .name = "ip", |
| 1632 | .type = QEMU_OPT_STRING, |
| 1633 | .help = "legacy parameter, use net= instead", |
| 1634 | }, { |
| 1635 | .name = "net", |
| 1636 | .type = QEMU_OPT_STRING, |
| 1637 | .help = "IP address and optional netmask", |
| 1638 | }, { |
| 1639 | .name = "host", |
| 1640 | .type = QEMU_OPT_STRING, |
| 1641 | .help = "guest-visible address of the host", |
| 1642 | }, { |
| 1643 | .name = "tftp", |
| 1644 | .type = QEMU_OPT_STRING, |
| 1645 | .help = "root directory of the built-in TFTP server", |
| 1646 | }, { |
| 1647 | .name = "bootfile", |
| 1648 | .type = QEMU_OPT_STRING, |
| 1649 | .help = "BOOTP filename, for use with tftp=", |
| 1650 | }, { |
| 1651 | .name = "dhcpstart", |
| 1652 | .type = QEMU_OPT_STRING, |
| 1653 | .help = "the first of the 16 IPs the built-in DHCP server can assign", |
| 1654 | }, { |
| 1655 | .name = "dns", |
| 1656 | .type = QEMU_OPT_STRING, |
| 1657 | .help = "guest-visible address of the virtual nameserver", |
| 1658 | }, { |
| 1659 | .name = "smb", |
| 1660 | .type = QEMU_OPT_STRING, |
| 1661 | .help = "root directory of the built-in SMB server", |
| 1662 | }, { |
| 1663 | .name = "smbserver", |
| 1664 | .type = QEMU_OPT_STRING, |
| 1665 | .help = "IP address of the built-in SMB server", |
| 1666 | }, { |
| 1667 | .name = "hostfwd", |
| 1668 | .type = QEMU_OPT_STRING, |
| 1669 | .help = "guest port number to forward incoming TCP or UDP connections", |
| 1670 | }, { |
| 1671 | .name = "guestfwd", |
| 1672 | .type = QEMU_OPT_STRING, |
| 1673 | .help = "IP address and port to forward guest TCP connections", |
| 1674 | }, |
| 1675 | { /* end of list */ } |
| 1676 | }, |
| 1677 | #endif |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1678 | }, { |
| 1679 | .type = "tap", |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 1680 | .init = net_init_tap, |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1681 | .desc = { |
| 1682 | NET_COMMON_PARAMS_DESC, |
| 1683 | { |
| 1684 | .name = "ifname", |
| 1685 | .type = QEMU_OPT_STRING, |
| 1686 | .help = "interface name", |
| 1687 | }, |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 1688 | #ifndef _WIN32 |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1689 | { |
| 1690 | .name = "fd", |
| 1691 | .type = QEMU_OPT_STRING, |
| 1692 | .help = "file descriptor of an already opened tap", |
| 1693 | }, { |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1694 | .name = "script", |
| 1695 | .type = QEMU_OPT_STRING, |
| 1696 | .help = "script to initialize the interface", |
| 1697 | }, { |
| 1698 | .name = "downscript", |
| 1699 | .type = QEMU_OPT_STRING, |
| 1700 | .help = "script to shut down the interface", |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1701 | }, { |
| 1702 | .name = "sndbuf", |
| 1703 | .type = QEMU_OPT_SIZE, |
| 1704 | .help = "send buffer limit" |
Mark McLoughlin | baf74c9 | 2009-10-22 17:43:37 +0100 | [diff] [blame] | 1705 | }, { |
| 1706 | .name = "vnet_hdr", |
| 1707 | .type = QEMU_OPT_BOOL, |
| 1708 | .help = "enable the IFF_VNET_HDR flag on the tap interface" |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1709 | }, |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 1710 | #endif /* _WIN32 */ |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1711 | { /* end of list */ } |
| 1712 | }, |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 1713 | }, { |
| 1714 | .type = "socket", |
| 1715 | .init = net_init_socket, |
| 1716 | .desc = { |
| 1717 | NET_COMMON_PARAMS_DESC, |
| 1718 | { |
| 1719 | .name = "fd", |
| 1720 | .type = QEMU_OPT_STRING, |
| 1721 | .help = "file descriptor of an already opened socket", |
| 1722 | }, { |
| 1723 | .name = "listen", |
| 1724 | .type = QEMU_OPT_STRING, |
| 1725 | .help = "port number, and optional hostname, to listen on", |
| 1726 | }, { |
| 1727 | .name = "connect", |
| 1728 | .type = QEMU_OPT_STRING, |
| 1729 | .help = "port number, and optional hostname, to connect to", |
| 1730 | }, { |
| 1731 | .name = "mcast", |
| 1732 | .type = QEMU_OPT_STRING, |
| 1733 | .help = "UDP multicast address and port number", |
| 1734 | }, |
| 1735 | { /* end of list */ } |
| 1736 | }, |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 1737 | #ifdef CONFIG_VDE |
| 1738 | }, { |
| 1739 | .type = "vde", |
| 1740 | .init = net_init_vde, |
| 1741 | .desc = { |
| 1742 | NET_COMMON_PARAMS_DESC, |
| 1743 | { |
| 1744 | .name = "sock", |
| 1745 | .type = QEMU_OPT_STRING, |
| 1746 | .help = "socket path", |
| 1747 | }, { |
| 1748 | .name = "port", |
| 1749 | .type = QEMU_OPT_NUMBER, |
| 1750 | .help = "port number", |
| 1751 | }, { |
| 1752 | .name = "group", |
| 1753 | .type = QEMU_OPT_STRING, |
| 1754 | .help = "group owner of socket", |
| 1755 | }, { |
| 1756 | .name = "mode", |
| 1757 | .type = QEMU_OPT_NUMBER, |
| 1758 | .help = "permissions for socket", |
| 1759 | }, |
| 1760 | { /* end of list */ } |
| 1761 | }, |
| 1762 | #endif |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 1763 | }, { |
| 1764 | .type = "dump", |
| 1765 | .init = net_init_dump, |
| 1766 | .desc = { |
| 1767 | NET_COMMON_PARAMS_DESC, |
| 1768 | { |
| 1769 | .name = "len", |
| 1770 | .type = QEMU_OPT_SIZE, |
| 1771 | .help = "per-packet size limit (64k default)", |
| 1772 | }, { |
| 1773 | .name = "file", |
| 1774 | .type = QEMU_OPT_STRING, |
| 1775 | .help = "dump file path (default is qemu-vlan0.pcap)", |
| 1776 | }, |
| 1777 | { /* end of list */ } |
| 1778 | }, |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1779 | }, |
| 1780 | { /* end of list */ } |
| 1781 | }; |
| 1782 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1783 | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1784 | { |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 1785 | const char *name; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1786 | const char *type; |
| 1787 | int i; |
| 1788 | |
| 1789 | type = qemu_opt_get(opts, "type"); |
| 1790 | if (!type) { |
| 1791 | qemu_error("No type specified for -net\n"); |
| 1792 | return -1; |
| 1793 | } |
| 1794 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1795 | if (is_netdev) { |
| 1796 | if (strcmp(type, "tap") != 0 && |
| 1797 | #ifdef CONFIG_SLIRP |
| 1798 | strcmp(type, "user") != 0 && |
| 1799 | #endif |
| 1800 | #ifdef CONFIG_VDE |
| 1801 | strcmp(type, "vde") != 0 && |
| 1802 | #endif |
| 1803 | strcmp(type, "socket") != 0) { |
| 1804 | qemu_error("The '%s' network backend type is not valid with -netdev\n", |
| 1805 | type); |
| 1806 | return -1; |
| 1807 | } |
| 1808 | |
| 1809 | if (qemu_opt_get(opts, "vlan")) { |
| 1810 | qemu_error("The 'vlan' parameter is not valid with -netdev\n"); |
| 1811 | return -1; |
| 1812 | } |
| 1813 | if (qemu_opt_get(opts, "name")) { |
| 1814 | qemu_error("The 'name' parameter is not valid with -netdev\n"); |
| 1815 | return -1; |
| 1816 | } |
| 1817 | if (!qemu_opts_id(opts)) { |
| 1818 | qemu_error("The id= parameter is required with -netdev\n"); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | } |
| 1822 | |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 1823 | name = qemu_opts_id(opts); |
| 1824 | if (!name) { |
| 1825 | name = qemu_opt_get(opts, "name"); |
| 1826 | } |
| 1827 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1828 | for (i = 0; net_client_types[i].type != NULL; i++) { |
| 1829 | if (!strcmp(net_client_types[i].type, type)) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1830 | VLANState *vlan = NULL; |
| 1831 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1832 | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
| 1833 | return -1; |
| 1834 | } |
| 1835 | |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1836 | /* Do not add to a vlan if it's a -netdev or a nic with a |
| 1837 | * netdev= parameter. */ |
| 1838 | if (!(is_netdev || |
| 1839 | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1840 | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
| 1841 | } |
| 1842 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1843 | if (net_client_types[i].init) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1844 | return net_client_types[i].init(opts, mon, name, vlan); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1845 | } else { |
| 1846 | return 0; |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | qemu_error("Invalid -net type '%s'\n", type); |
| 1852 | return -1; |
| 1853 | } |
| 1854 | |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 1855 | void net_client_uninit(NICInfo *nd) |
| 1856 | { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 1857 | if (nd->vlan) { |
| 1858 | nd->vlan->nb_guest_devs--; |
| 1859 | } |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 1860 | nb_nics--; |
Glauber Costa | a979670 | 2009-09-17 16:53:39 -0400 | [diff] [blame] | 1861 | |
Mark McLoughlin | 9203f52 | 2009-10-06 12:16:53 +0100 | [diff] [blame] | 1862 | qemu_free(nd->model); |
| 1863 | qemu_free(nd->name); |
| 1864 | qemu_free(nd->devaddr); |
Glauber Costa | a979670 | 2009-09-17 16:53:39 -0400 | [diff] [blame] | 1865 | |
Mark McLoughlin | d2cffe3 | 2009-10-06 12:16:54 +0100 | [diff] [blame] | 1866 | nd->used = 0; |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1869 | static int net_host_check_device(const char *device) |
| 1870 | { |
| 1871 | int i; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1872 | const char *valid_param_list[] = { "tap", "socket", "dump" |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1873 | #ifdef CONFIG_SLIRP |
| 1874 | ,"user" |
| 1875 | #endif |
| 1876 | #ifdef CONFIG_VDE |
| 1877 | ,"vde" |
| 1878 | #endif |
| 1879 | }; |
| 1880 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
| 1881 | if (!strncmp(valid_param_list[i], device, |
| 1882 | strlen(valid_param_list[i]))) |
| 1883 | return 1; |
| 1884 | } |
| 1885 | |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1889 | void net_host_device_add(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1890 | { |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1891 | const char *device = qdict_get_str(qdict, "device"); |
Mark McLoughlin | 7f1c9d2 | 2009-10-06 12:17:13 +0100 | [diff] [blame] | 1892 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
| 1893 | QemuOpts *opts; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1894 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1895 | if (!net_host_check_device(device)) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1896 | monitor_printf(mon, "invalid host network device %s\n", device); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1897 | return; |
| 1898 | } |
Mark McLoughlin | 7f1c9d2 | 2009-10-06 12:17:13 +0100 | [diff] [blame] | 1899 | |
| 1900 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); |
| 1901 | if (!opts) { |
| 1902 | monitor_printf(mon, "parsing network options '%s' failed\n", |
| 1903 | opts_str ? opts_str : ""); |
| 1904 | return; |
| 1905 | } |
| 1906 | |
| 1907 | qemu_opt_set(opts, "type", device); |
| 1908 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1909 | if (net_client_init(mon, opts, 0) < 0) { |
aliguori | 5c8be67 | 2009-04-21 19:56:32 +0000 | [diff] [blame] | 1910 | monitor_printf(mon, "adding host network device %s failed\n", device); |
| 1911 | } |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1914 | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1915 | { |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1916 | VLANClientState *vc; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1917 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
| 1918 | const char *device = qdict_get_str(qdict, "device"); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1919 | |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1920 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1921 | if (!vc) { |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1922 | return; |
| 1923 | } |
aliguori | e8f1f9d | 2009-04-21 19:56:08 +0000 | [diff] [blame] | 1924 | if (!net_host_check_device(vc->model)) { |
| 1925 | monitor_printf(mon, "invalid host network device %s\n", device); |
| 1926 | return; |
| 1927 | } |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1928 | qemu_del_vlan_client(vc); |
| 1929 | } |
| 1930 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 1931 | void net_set_boot_mask(int net_boot_mask) |
| 1932 | { |
| 1933 | int i; |
| 1934 | |
| 1935 | /* Only the first four NICs may be bootable */ |
| 1936 | net_boot_mask = net_boot_mask & 0xF; |
| 1937 | |
| 1938 | for (i = 0; i < nb_nics; i++) { |
| 1939 | if (net_boot_mask & (1 << i)) { |
| 1940 | nd_table[i].bootable = 1; |
| 1941 | net_boot_mask &= ~(1 << i); |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | if (net_boot_mask) { |
| 1946 | fprintf(stderr, "Cannot boot from non-existent NIC\n"); |
| 1947 | exit(1); |
| 1948 | } |
| 1949 | } |
| 1950 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1951 | void do_info_network(Monitor *mon) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1952 | { |
| 1953 | VLANState *vlan; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1954 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1955 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 1956 | VLANClientState *vc; |
| 1957 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1958 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1959 | |
| 1960 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1961 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1962 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1966 | void do_set_link(Monitor *mon, const QDict *qdict) |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1967 | { |
| 1968 | VLANState *vlan; |
| 1969 | VLANClientState *vc = NULL; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1970 | const char *name = qdict_get_str(qdict, "name"); |
| 1971 | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1972 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1973 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 1974 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 1975 | if (strcmp(vc->name, name) == 0) { |
edgar_igl | dd5de37 | 2009-01-09 00:48:28 +0000 | [diff] [blame] | 1976 | goto done; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | } |
edgar_igl | dd5de37 | 2009-01-09 00:48:28 +0000 | [diff] [blame] | 1980 | done: |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1981 | |
| 1982 | if (!vc) { |
Stefan Weil | 7dc3fa0 | 2009-08-08 23:33:26 +0200 | [diff] [blame] | 1983 | monitor_printf(mon, "could not find network device '%s'\n", name); |
Luiz Capitulino | c3cf0d3 | 2009-08-03 13:56:59 -0300 | [diff] [blame] | 1984 | return; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | if (strcmp(up_or_down, "up") == 0) |
| 1988 | vc->link_down = 0; |
| 1989 | else if (strcmp(up_or_down, "down") == 0) |
| 1990 | vc->link_down = 1; |
| 1991 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1992 | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " |
| 1993 | "valid\n", up_or_down); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1994 | |
aliguori | 34b25ca | 2009-01-08 19:45:03 +0000 | [diff] [blame] | 1995 | if (vc->link_status_changed) |
| 1996 | vc->link_status_changed(vc); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1999 | void net_cleanup(void) |
| 2000 | { |
| 2001 | VLANState *vlan; |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 2002 | VLANClientState *vc, *next_vc; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2003 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2004 | QTAILQ_FOREACH(vlan, &vlans, next) { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2005 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 2006 | qemu_del_vlan_client(vc); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 2009 | |
| 2010 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
| 2011 | qemu_del_vlan_client(vc); |
| 2012 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2015 | static void net_check_clients(void) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2016 | { |
| 2017 | VLANState *vlan; |
| 2018 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2019 | QTAILQ_FOREACH(vlan, &vlans, next) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2020 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
| 2021 | continue; |
| 2022 | if (vlan->nb_guest_devs == 0) |
| 2023 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); |
| 2024 | if (vlan->nb_host_devs == 0) |
| 2025 | fprintf(stderr, |
| 2026 | "Warning: vlan %d is not connected to host network\n", |
| 2027 | vlan->id); |
| 2028 | } |
| 2029 | } |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2030 | |
| 2031 | static int net_init_client(QemuOpts *opts, void *dummy) |
| 2032 | { |
Mark McLoughlin | c1671a0 | 2009-10-12 09:52:00 +0100 | [diff] [blame] | 2033 | if (net_client_init(NULL, opts, 0) < 0) |
| 2034 | return -1; |
| 2035 | return 0; |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | static int net_init_netdev(QemuOpts *opts, void *dummy) |
| 2039 | { |
| 2040 | return net_client_init(NULL, opts, 1); |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | int net_init_clients(void) |
| 2044 | { |
| 2045 | if (QTAILQ_EMPTY(&qemu_net_opts.head)) { |
| 2046 | /* if no clients, we use a default config */ |
| 2047 | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); |
| 2048 | #ifdef CONFIG_SLIRP |
| 2049 | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); |
| 2050 | #endif |
| 2051 | } |
| 2052 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2053 | QTAILQ_INIT(&vlans); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 2054 | QTAILQ_INIT(&non_vlan_clients); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2055 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2056 | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
| 2057 | return -1; |
| 2058 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2059 | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
| 2060 | return -1; |
| 2061 | } |
| 2062 | |
| 2063 | net_check_clients(); |
| 2064 | |
| 2065 | return 0; |
| 2066 | } |
| 2067 | |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 2068 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2069 | { |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 2070 | #if defined(CONFIG_SLIRP) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 2071 | int ret; |
| 2072 | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2073 | return ret; |
| 2074 | } |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 2075 | #endif |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 2076 | |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 2077 | if (!qemu_opts_parse(opts_list, optarg, "type")) { |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2078 | return -1; |
| 2079 | } |
| 2080 | |
| 2081 | return 0; |
| 2082 | } |