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> |
| 43 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 44 | #include <net/if.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 45 | #include <arpa/inet.h> |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 46 | #include <dirent.h> |
| 47 | #include <netdb.h> |
| 48 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 49 | #ifdef CONFIG_BSD |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 50 | #include <sys/stat.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 51 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 52 | #include <libutil.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 53 | #else |
| 54 | #include <util.h> |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 55 | #endif |
| 56 | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) |
| 57 | #include <freebsd/stdlib.h> |
| 58 | #else |
| 59 | #ifdef __linux__ |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 60 | #include <pty.h> |
| 61 | #include <malloc.h> |
| 62 | #include <linux/rtc.h> |
| 63 | |
| 64 | /* For the benefit of older linux systems which don't supply it, |
| 65 | we use a local copy of hpet.h. */ |
| 66 | /* #include <linux/hpet.h> */ |
| 67 | #include "hpet.h" |
| 68 | |
| 69 | #include <linux/ppdev.h> |
| 70 | #include <linux/parport.h> |
| 71 | #endif |
| 72 | #ifdef __sun__ |
| 73 | #include <sys/stat.h> |
| 74 | #include <sys/ethernet.h> |
| 75 | #include <sys/sockio.h> |
| 76 | #include <netinet/arp.h> |
| 77 | #include <netinet/in.h> |
| 78 | #include <netinet/in_systm.h> |
| 79 | #include <netinet/ip.h> |
| 80 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 81 | #include <netinet/udp.h> |
| 82 | #include <netinet/tcp.h> |
| 83 | #include <net/if.h> |
| 84 | #include <syslog.h> |
| 85 | #include <stropts.h> |
| 86 | #endif |
| 87 | #endif |
| 88 | #endif |
| 89 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 90 | #if defined(__OpenBSD__) |
| 91 | #include <util.h> |
| 92 | #endif |
| 93 | |
| 94 | #if defined(CONFIG_VDE) |
| 95 | #include <libvdeplug.h> |
| 96 | #endif |
| 97 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 98 | #include "qemu-common.h" |
| 99 | #include "net.h" |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 100 | #include "net/tap.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 101 | #include "monitor.h" |
| 102 | #include "sysemu.h" |
| 103 | #include "qemu-timer.h" |
| 104 | #include "qemu-char.h" |
| 105 | #include "audio/audio.h" |
| 106 | #include "qemu_socket.h" |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 107 | #include "qemu-log.h" |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 108 | #include "qemu-config.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 109 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 110 | #include "slirp/libslirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 111 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 112 | static QTAILQ_HEAD(, VLANState) vlans; |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 113 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 114 | |
| 115 | /***********************************************************/ |
| 116 | /* network device redirectors */ |
| 117 | |
| 118 | #if defined(DEBUG_NET) || defined(DEBUG_SLIRP) |
| 119 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
| 120 | { |
| 121 | int len, i, j, c; |
| 122 | |
| 123 | for(i=0;i<size;i+=16) { |
| 124 | len = size - i; |
| 125 | if (len > 16) |
| 126 | len = 16; |
| 127 | fprintf(f, "%08x ", i); |
| 128 | for(j=0;j<16;j++) { |
| 129 | if (j < len) |
| 130 | fprintf(f, " %02x", buf[i+j]); |
| 131 | else |
| 132 | fprintf(f, " "); |
| 133 | } |
| 134 | fprintf(f, " "); |
| 135 | for(j=0;j<len;j++) { |
| 136 | c = buf[i+j]; |
| 137 | if (c < ' ' || c > '~') |
| 138 | c = '.'; |
| 139 | fprintf(f, "%c", c); |
| 140 | } |
| 141 | fprintf(f, "\n"); |
| 142 | } |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
| 147 | { |
| 148 | int i; |
| 149 | char *last_char; |
| 150 | long int offset; |
| 151 | |
| 152 | errno = 0; |
| 153 | offset = strtol(p, &last_char, 0); |
| 154 | if (0 == errno && '\0' == *last_char && |
| 155 | offset >= 0 && offset <= 0xFFFFFF) { |
| 156 | macaddr[3] = (offset & 0xFF0000) >> 16; |
| 157 | macaddr[4] = (offset & 0xFF00) >> 8; |
| 158 | macaddr[5] = offset & 0xFF; |
| 159 | return 0; |
| 160 | } else { |
| 161 | for(i = 0; i < 6; i++) { |
| 162 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 163 | if (i == 5) { |
| 164 | if (*p != '\0') |
| 165 | return -1; |
| 166 | } else { |
| 167 | if (*p != ':' && *p != '-') |
| 168 | return -1; |
| 169 | p++; |
| 170 | } |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 179 | { |
| 180 | const char *p, *p1; |
| 181 | int len; |
| 182 | p = *pp; |
| 183 | p1 = strchr(p, sep); |
| 184 | if (!p1) |
| 185 | return -1; |
| 186 | len = p1 - p; |
| 187 | p1++; |
| 188 | if (buf_size > 0) { |
| 189 | if (len > buf_size - 1) |
| 190 | len = buf_size - 1; |
| 191 | memcpy(buf, p, len); |
| 192 | buf[len] = '\0'; |
| 193 | } |
| 194 | *pp = p1; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 199 | struct sockaddr_in *saddr, |
| 200 | const char *input_str) |
| 201 | { |
| 202 | char *str = strdup(input_str); |
| 203 | char *host_str = str; |
| 204 | char *src_str; |
| 205 | const char *src_str2; |
| 206 | char *ptr; |
| 207 | |
| 208 | /* |
| 209 | * Chop off any extra arguments at the end of the string which |
| 210 | * would start with a comma, then fill in the src port information |
| 211 | * if it was provided else use the "any address" and "any port". |
| 212 | */ |
| 213 | if ((ptr = strchr(str,','))) |
| 214 | *ptr = '\0'; |
| 215 | |
| 216 | if ((src_str = strchr(input_str,'@'))) { |
| 217 | *src_str = '\0'; |
| 218 | src_str++; |
| 219 | } |
| 220 | |
| 221 | if (parse_host_port(haddr, host_str) < 0) |
| 222 | goto fail; |
| 223 | |
| 224 | src_str2 = src_str; |
| 225 | if (!src_str || *src_str == '\0') |
| 226 | src_str2 = ":0"; |
| 227 | |
| 228 | if (parse_host_port(saddr, src_str2) < 0) |
| 229 | goto fail; |
| 230 | |
| 231 | free(str); |
| 232 | return(0); |
| 233 | |
| 234 | fail: |
| 235 | free(str); |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 240 | { |
| 241 | char buf[512]; |
| 242 | struct hostent *he; |
| 243 | const char *p, *r; |
| 244 | int port; |
| 245 | |
| 246 | p = str; |
| 247 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 248 | return -1; |
| 249 | saddr->sin_family = AF_INET; |
| 250 | if (buf[0] == '\0') { |
| 251 | saddr->sin_addr.s_addr = 0; |
| 252 | } else { |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 253 | if (qemu_isdigit(buf[0])) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 254 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 255 | return -1; |
| 256 | } else { |
| 257 | if ((he = gethostbyname(buf)) == NULL) |
| 258 | return - 1; |
| 259 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
| 260 | } |
| 261 | } |
| 262 | port = strtol(p, (char **)&r, 0); |
| 263 | if (r == p) |
| 264 | return -1; |
| 265 | saddr->sin_port = htons(port); |
| 266 | return 0; |
| 267 | } |
| 268 | |
aliguori | 7cb7434b | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 269 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
| 270 | { |
| 271 | snprintf(vc->info_str, sizeof(vc->info_str), |
aliguori | 4dda406 | 2009-01-08 19:01:37 +0000 | [diff] [blame] | 272 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
| 273 | vc->model, |
aliguori | 7cb7434b | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 274 | macaddr[0], macaddr[1], macaddr[2], |
| 275 | macaddr[3], macaddr[4], macaddr[5]); |
| 276 | } |
| 277 | |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 278 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
| 279 | { |
| 280 | static int index = 0; |
| 281 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
| 282 | |
| 283 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
| 284 | return; |
| 285 | macaddr->a[0] = 0x52; |
| 286 | macaddr->a[1] = 0x54; |
| 287 | macaddr->a[2] = 0x00; |
| 288 | macaddr->a[3] = 0x12; |
| 289 | macaddr->a[4] = 0x34; |
| 290 | macaddr->a[5] = 0x56 + index++; |
| 291 | } |
| 292 | |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 293 | static char *assign_name(VLANClientState *vc1, const char *model) |
| 294 | { |
| 295 | VLANState *vlan; |
| 296 | char buf[256]; |
| 297 | int id = 0; |
| 298 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 299 | QTAILQ_FOREACH(vlan, &vlans, next) { |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 300 | VLANClientState *vc; |
| 301 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 302 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 303 | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 304 | id++; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 305 | } |
| 306 | } |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
| 310 | |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 311 | return qemu_strdup(buf); |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 314 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 315 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 316 | const uint8_t *data, |
| 317 | size_t size, |
| 318 | void *opaque); |
| 319 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 320 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 321 | const struct iovec *iov, |
| 322 | int iovcnt, |
| 323 | void *opaque); |
| 324 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 325 | VLANClientState *qemu_new_vlan_client(net_client_type type, |
| 326 | VLANState *vlan, |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 327 | VLANClientState *peer, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 328 | const char *model, |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 329 | const char *name, |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 330 | NetCanReceive *can_receive, |
| 331 | NetReceive *receive, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 332 | NetReceive *receive_raw, |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 333 | NetReceiveIOV *receive_iov, |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 334 | NetCleanup *cleanup, |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 335 | void *opaque) |
| 336 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 337 | VLANClientState *vc; |
| 338 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 339 | vc = qemu_mallocz(sizeof(VLANClientState)); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 340 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 341 | vc->type = type; |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 342 | vc->model = qemu_strdup(model); |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 343 | if (name) |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 344 | vc->name = qemu_strdup(name); |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 345 | else |
| 346 | vc->name = assign_name(vc, model); |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 347 | vc->can_receive = can_receive; |
| 348 | vc->receive = receive; |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 349 | vc->receive_raw = receive_raw; |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 350 | vc->receive_iov = receive_iov; |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 351 | vc->cleanup = cleanup; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 352 | vc->opaque = opaque; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 353 | |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 354 | if (vlan) { |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 355 | assert(!peer); |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 356 | vc->vlan = vlan; |
| 357 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 358 | } else { |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 359 | if (peer) { |
| 360 | vc->peer = peer; |
| 361 | peer->peer = vc; |
| 362 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 363 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 364 | |
| 365 | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
| 366 | qemu_deliver_packet_iov, |
| 367 | vc); |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 368 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 369 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 370 | return vc; |
| 371 | } |
| 372 | |
| 373 | void qemu_del_vlan_client(VLANClientState *vc) |
| 374 | { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 375 | if (vc->vlan) { |
| 376 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 377 | } else { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 378 | if (vc->send_queue) { |
| 379 | qemu_del_net_queue(vc->send_queue); |
| 380 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 381 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 382 | if (vc->peer) { |
| 383 | vc->peer->peer = NULL; |
| 384 | } |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 385 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 386 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 387 | if (vc->cleanup) { |
| 388 | vc->cleanup(vc); |
| 389 | } |
| 390 | |
| 391 | qemu_free(vc->name); |
| 392 | qemu_free(vc->model); |
| 393 | qemu_free(vc); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 394 | } |
| 395 | |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 396 | VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque) |
| 397 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 398 | VLANClientState *vc; |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 399 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 400 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 401 | if (vc->opaque == opaque) { |
| 402 | return vc; |
| 403 | } |
| 404 | } |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 405 | |
| 406 | return NULL; |
| 407 | } |
| 408 | |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 409 | static VLANClientState * |
| 410 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, |
| 411 | const char *client_str) |
| 412 | { |
| 413 | VLANState *vlan; |
| 414 | VLANClientState *vc; |
| 415 | |
| 416 | vlan = qemu_find_vlan(vlan_id, 0); |
| 417 | if (!vlan) { |
| 418 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); |
| 419 | return NULL; |
| 420 | } |
| 421 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 422 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 423 | if (!strcmp(vc->name, client_str)) { |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | if (!vc) { |
| 428 | monitor_printf(mon, "can't find device %s on VLAN %d\n", |
| 429 | client_str, vlan_id); |
| 430 | } |
| 431 | |
| 432 | return vc; |
| 433 | } |
| 434 | |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 435 | int qemu_can_send_packet(VLANClientState *sender) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 436 | { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 437 | VLANState *vlan = sender->vlan; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 438 | VLANClientState *vc; |
| 439 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 440 | if (sender->peer) { |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 441 | if (sender->peer->receive_disabled) { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 442 | return 0; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 443 | } else if (sender->peer->can_receive && |
| 444 | !sender->peer->can_receive(sender->peer)) { |
| 445 | return 0; |
| 446 | } else { |
| 447 | return 1; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 451 | if (!sender->vlan) { |
| 452 | return 1; |
| 453 | } |
| 454 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 455 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 456 | if (vc == sender) { |
| 457 | continue; |
| 458 | } |
| 459 | |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 460 | /* no can_receive() handler, they can always receive */ |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 461 | if (!vc->can_receive || vc->can_receive(vc)) { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 462 | return 1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 468 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 469 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 470 | const uint8_t *data, |
| 471 | size_t size, |
| 472 | void *opaque) |
| 473 | { |
| 474 | VLANClientState *vc = opaque; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 475 | ssize_t ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 476 | |
| 477 | if (vc->link_down) { |
| 478 | return size; |
| 479 | } |
| 480 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 481 | if (vc->receive_disabled) { |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { |
| 486 | ret = vc->receive_raw(vc, data, size); |
| 487 | } else { |
| 488 | ret = vc->receive(vc, data, size); |
| 489 | } |
| 490 | |
| 491 | if (ret == 0) { |
| 492 | vc->receive_disabled = 1; |
| 493 | }; |
| 494 | |
| 495 | return ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 498 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 499 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 500 | const uint8_t *buf, |
| 501 | size_t size, |
| 502 | void *opaque) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 503 | { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 504 | VLANState *vlan = opaque; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 505 | VLANClientState *vc; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 506 | ssize_t ret = -1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 507 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 508 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 509 | ssize_t len; |
| 510 | |
| 511 | if (vc == sender) { |
| 512 | continue; |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 513 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 514 | |
| 515 | if (vc->link_down) { |
| 516 | ret = size; |
| 517 | continue; |
| 518 | } |
| 519 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 520 | if (vc->receive_disabled) { |
| 521 | ret = 0; |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 526 | len = vc->receive_raw(vc, buf, size); |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 527 | } else { |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 528 | len = vc->receive(vc, buf, size); |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 529 | } |
| 530 | |
| 531 | if (len == 0) { |
| 532 | vc->receive_disabled = 1; |
| 533 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 534 | |
| 535 | ret = (ret >= 0) ? ret : len; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 536 | |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 537 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 538 | |
| 539 | return ret; |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 542 | void qemu_purge_queued_packets(VLANClientState *vc) |
| 543 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 544 | NetQueue *queue; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 545 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 546 | if (!vc->peer && !vc->vlan) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | if (vc->peer) { |
| 551 | queue = vc->peer->send_queue; |
| 552 | } else { |
| 553 | queue = vc->vlan->send_queue; |
| 554 | } |
| 555 | |
| 556 | qemu_net_queue_purge(queue, vc); |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 559 | void qemu_flush_queued_packets(VLANClientState *vc) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 560 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 561 | NetQueue *queue; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 562 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame^] | 563 | vc->receive_disabled = 0; |
| 564 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 565 | if (vc->vlan) { |
| 566 | queue = vc->vlan->send_queue; |
| 567 | } else { |
| 568 | queue = vc->send_queue; |
| 569 | } |
| 570 | |
| 571 | qemu_net_queue_flush(queue); |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 574 | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender, |
| 575 | unsigned flags, |
| 576 | const uint8_t *buf, int size, |
| 577 | NetPacketSent *sent_cb) |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 578 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 579 | NetQueue *queue; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 580 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 581 | #ifdef DEBUG_NET |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 582 | printf("qemu_send_packet_async:\n"); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 583 | hex_dump(stdout, buf, size); |
| 584 | #endif |
| 585 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 586 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
| 587 | return size; |
| 588 | } |
| 589 | |
| 590 | if (sender->peer) { |
| 591 | queue = sender->peer->send_queue; |
| 592 | } else { |
| 593 | queue = sender->vlan->send_queue; |
| 594 | } |
| 595 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 596 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
| 597 | } |
| 598 | |
| 599 | ssize_t qemu_send_packet_async(VLANClientState *sender, |
| 600 | const uint8_t *buf, int size, |
| 601 | NetPacketSent *sent_cb) |
| 602 | { |
| 603 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, |
| 604 | buf, size, sent_cb); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
| 608 | { |
| 609 | qemu_send_packet_async(vc, buf, size, NULL); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 612 | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
| 613 | { |
| 614 | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW, |
| 615 | buf, size, NULL); |
| 616 | } |
| 617 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 618 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
| 619 | int iovcnt) |
| 620 | { |
| 621 | uint8_t buffer[4096]; |
| 622 | size_t offset = 0; |
| 623 | int i; |
| 624 | |
| 625 | for (i = 0; i < iovcnt; i++) { |
| 626 | size_t len; |
| 627 | |
| 628 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); |
| 629 | memcpy(buffer + offset, iov[i].iov_base, len); |
| 630 | offset += len; |
| 631 | } |
| 632 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 633 | return vc->receive(vc, buffer, offset); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 634 | } |
| 635 | |
aliguori | e0e7877 | 2009-01-26 15:37:44 +0000 | [diff] [blame] | 636 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
| 637 | { |
| 638 | size_t offset = 0; |
| 639 | int i; |
| 640 | |
| 641 | for (i = 0; i < iovcnt; i++) |
| 642 | offset += iov[i].iov_len; |
| 643 | return offset; |
| 644 | } |
| 645 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 646 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 647 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 648 | const struct iovec *iov, |
| 649 | int iovcnt, |
| 650 | void *opaque) |
| 651 | { |
| 652 | VLANClientState *vc = opaque; |
| 653 | |
| 654 | if (vc->link_down) { |
| 655 | return calc_iov_length(iov, iovcnt); |
| 656 | } |
| 657 | |
| 658 | if (vc->receive_iov) { |
| 659 | return vc->receive_iov(vc, iov, iovcnt); |
| 660 | } else { |
| 661 | return vc_sendv_compat(vc, iov, iovcnt); |
| 662 | } |
| 663 | } |
| 664 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 665 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 666 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 667 | const struct iovec *iov, |
| 668 | int iovcnt, |
| 669 | void *opaque) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 670 | { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 671 | VLANState *vlan = opaque; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 672 | VLANClientState *vc; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 673 | ssize_t ret = -1; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 674 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 675 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 676 | ssize_t len; |
| 677 | |
| 678 | if (vc == sender) { |
| 679 | continue; |
| 680 | } |
| 681 | |
| 682 | if (vc->link_down) { |
| 683 | ret = calc_iov_length(iov, iovcnt); |
| 684 | continue; |
| 685 | } |
| 686 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 687 | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
| 688 | |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 689 | if (vc->receive_iov) { |
| 690 | len = vc->receive_iov(vc, iov, iovcnt); |
| 691 | } else { |
| 692 | len = vc_sendv_compat(vc, iov, iovcnt); |
| 693 | } |
| 694 | |
| 695 | ret = (ret >= 0) ? ret : len; |
| 696 | } |
| 697 | |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 698 | return ret; |
| 699 | } |
| 700 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 701 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
| 702 | const struct iovec *iov, int iovcnt, |
| 703 | NetPacketSent *sent_cb) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 704 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 705 | NetQueue *queue; |
| 706 | |
| 707 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
aliguori | e0e7877 | 2009-01-26 15:37:44 +0000 | [diff] [blame] | 708 | return calc_iov_length(iov, iovcnt); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 711 | if (sender->peer) { |
| 712 | queue = sender->peer->send_queue; |
| 713 | } else { |
| 714 | queue = sender->vlan->send_queue; |
| 715 | } |
| 716 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 717 | return qemu_net_queue_send_iov(queue, sender, |
| 718 | QEMU_NET_PACKET_FLAG_NONE, |
| 719 | iov, iovcnt, sent_cb); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 722 | ssize_t |
| 723 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
| 724 | { |
| 725 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
| 726 | } |
| 727 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 728 | #if defined(CONFIG_SLIRP) |
| 729 | |
| 730 | /* slirp network adapter */ |
| 731 | |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 732 | #define SLIRP_CFG_HOSTFWD 1 |
| 733 | #define SLIRP_CFG_LEGACY 2 |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 734 | |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 735 | struct slirp_config_str { |
| 736 | struct slirp_config_str *next; |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 737 | int flags; |
| 738 | char str[1024]; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 739 | int legacy_format; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 740 | }; |
| 741 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 742 | typedef struct SlirpState { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 743 | QTAILQ_ENTRY(SlirpState) entry; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 744 | VLANClientState *vc; |
| 745 | Slirp *slirp; |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 746 | #ifndef _WIN32 |
| 747 | char smb_dir[128]; |
| 748 | #endif |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 749 | } SlirpState; |
| 750 | |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 751 | static struct slirp_config_str *slirp_configs; |
| 752 | const char *legacy_tftp_prefix; |
| 753 | const char *legacy_bootp_filename; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 754 | static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks = |
| 755 | QTAILQ_HEAD_INITIALIZER(slirp_stacks); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 756 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 757 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 758 | int legacy_format); |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 759 | static int slirp_guestfwd(SlirpState *s, const char *config_str, |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 760 | int legacy_format); |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 761 | |
Blue Swirl | c5b76b3 | 2009-06-13 08:44:31 +0000 | [diff] [blame] | 762 | #ifndef _WIN32 |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 763 | static const char *legacy_smb_export; |
| 764 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 765 | static int slirp_smb(SlirpState *s, const char *exported_dir, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 766 | struct in_addr vserver_addr); |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 767 | static void slirp_smb_cleanup(SlirpState *s); |
| 768 | #else |
| 769 | static inline void slirp_smb_cleanup(SlirpState *s) { } |
Blue Swirl | c5b76b3 | 2009-06-13 08:44:31 +0000 | [diff] [blame] | 770 | #endif |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 771 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 772 | int slirp_can_output(void *opaque) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 773 | { |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 774 | SlirpState *s = opaque; |
| 775 | |
| 776 | return qemu_can_send_packet(s->vc); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 779 | void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 780 | { |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 781 | SlirpState *s = opaque; |
| 782 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 783 | #ifdef DEBUG_SLIRP |
| 784 | printf("slirp output:\n"); |
| 785 | hex_dump(stdout, pkt, pkt_len); |
| 786 | #endif |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 787 | qemu_send_packet(s->vc, pkt, pkt_len); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 790 | static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 791 | { |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 792 | SlirpState *s = vc->opaque; |
| 793 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 794 | #ifdef DEBUG_SLIRP |
| 795 | printf("slirp input:\n"); |
| 796 | hex_dump(stdout, buf, size); |
| 797 | #endif |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 798 | slirp_input(s->slirp, buf, size); |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 799 | return size; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 800 | } |
| 801 | |
aliguori | 8d6249a | 2009-04-21 20:49:11 +0000 | [diff] [blame] | 802 | static void net_slirp_cleanup(VLANClientState *vc) |
| 803 | { |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 804 | SlirpState *s = vc->opaque; |
| 805 | |
| 806 | slirp_cleanup(s->slirp); |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 807 | slirp_smb_cleanup(s); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 808 | QTAILQ_REMOVE(&slirp_stacks, s, entry); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 809 | qemu_free(s); |
aliguori | 8d6249a | 2009-04-21 20:49:11 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 812 | static int net_slirp_init(VLANState *vlan, const char *model, |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 813 | const char *name, int restricted, |
| 814 | const char *vnetwork, const char *vhost, |
| 815 | const char *vhostname, const char *tftp_export, |
| 816 | const char *bootfile, const char *vdhcp_start, |
| 817 | const char *vnameserver, const char *smb_export, |
| 818 | const char *vsmbserver) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 819 | { |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 820 | /* default settings according to historic slirp */ |
Anthony Liguori | 8389e7f | 2009-07-10 12:43:14 -0500 | [diff] [blame] | 821 | struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */ |
| 822 | struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */ |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 823 | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ |
| 824 | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ |
| 825 | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ |
| 826 | #ifndef _WIN32 |
| 827 | struct in_addr smbsrv = { .s_addr = 0 }; |
| 828 | #endif |
| 829 | SlirpState *s; |
| 830 | char buf[20]; |
| 831 | uint32_t addr; |
| 832 | int shift; |
| 833 | char *end; |
Markus Armbruster | 3a179c6 | 2009-10-06 12:16:56 +0100 | [diff] [blame] | 834 | struct slirp_config_str *config; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 835 | |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 836 | if (!tftp_export) { |
| 837 | tftp_export = legacy_tftp_prefix; |
| 838 | } |
| 839 | if (!bootfile) { |
| 840 | bootfile = legacy_bootp_filename; |
| 841 | } |
| 842 | |
| 843 | if (vnetwork) { |
| 844 | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { |
| 845 | if (!inet_aton(vnetwork, &net)) { |
| 846 | return -1; |
| 847 | } |
| 848 | addr = ntohl(net.s_addr); |
| 849 | if (!(addr & 0x80000000)) { |
| 850 | mask.s_addr = htonl(0xff000000); /* class A */ |
| 851 | } else if ((addr & 0xfff00000) == 0xac100000) { |
| 852 | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ |
| 853 | } else if ((addr & 0xc0000000) == 0x80000000) { |
| 854 | mask.s_addr = htonl(0xffff0000); /* class B */ |
| 855 | } else if ((addr & 0xffff0000) == 0xc0a80000) { |
| 856 | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ |
| 857 | } else if ((addr & 0xffff0000) == 0xc6120000) { |
| 858 | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ |
| 859 | } else if ((addr & 0xe0000000) == 0xe0000000) { |
| 860 | mask.s_addr = htonl(0xffffff00); /* class C */ |
| 861 | } else { |
| 862 | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ |
| 863 | } |
| 864 | } else { |
| 865 | if (!inet_aton(buf, &net)) { |
| 866 | return -1; |
| 867 | } |
| 868 | shift = strtol(vnetwork, &end, 10); |
| 869 | if (*end != '\0') { |
| 870 | if (!inet_aton(vnetwork, &mask)) { |
| 871 | return -1; |
| 872 | } |
| 873 | } else if (shift < 4 || shift > 32) { |
| 874 | return -1; |
| 875 | } else { |
| 876 | mask.s_addr = htonl(0xffffffff << (32 - shift)); |
| 877 | } |
| 878 | } |
| 879 | net.s_addr &= mask.s_addr; |
| 880 | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr); |
| 881 | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr); |
| 882 | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr); |
| 883 | } |
| 884 | |
| 885 | if (vhost && !inet_aton(vhost, &host)) { |
aliguori | 8d6249a | 2009-04-21 20:49:11 +0000 | [diff] [blame] | 886 | return -1; |
| 887 | } |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 888 | if ((host.s_addr & mask.s_addr) != net.s_addr) { |
| 889 | return -1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 890 | } |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 891 | |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 892 | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) { |
| 893 | return -1; |
| 894 | } |
| 895 | if ((dhcp.s_addr & mask.s_addr) != net.s_addr || |
| 896 | dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { |
| 897 | return -1; |
| 898 | } |
| 899 | |
| 900 | if (vnameserver && !inet_aton(vnameserver, &dns)) { |
| 901 | return -1; |
| 902 | } |
| 903 | if ((dns.s_addr & mask.s_addr) != net.s_addr || |
| 904 | dns.s_addr == host.s_addr) { |
| 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | #ifndef _WIN32 |
| 909 | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) { |
| 910 | return -1; |
| 911 | } |
| 912 | #endif |
| 913 | |
| 914 | s = qemu_mallocz(sizeof(SlirpState)); |
| 915 | s->slirp = slirp_init(restricted, net, mask, host, vhostname, |
| 916 | tftp_export, bootfile, dhcp, dns, s); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 917 | QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 918 | |
Markus Armbruster | 3a179c6 | 2009-10-06 12:16:56 +0100 | [diff] [blame] | 919 | for (config = slirp_configs; config; config = config->next) { |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 920 | if (config->flags & SLIRP_CFG_HOSTFWD) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 921 | if (slirp_hostfwd(s, config->str, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 922 | config->flags & SLIRP_CFG_LEGACY) < 0) |
| 923 | return -1; |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 924 | } else { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 925 | if (slirp_guestfwd(s, config->str, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 926 | config->flags & SLIRP_CFG_LEGACY) < 0) |
| 927 | return -1; |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 928 | } |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 929 | } |
| 930 | #ifndef _WIN32 |
| 931 | if (!smb_export) { |
| 932 | smb_export = legacy_smb_export; |
| 933 | } |
| 934 | if (smb_export) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 935 | if (slirp_smb(s, smb_export, smbsrv) < 0) |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 936 | return -1; |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 937 | } |
| 938 | #endif |
| 939 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 940 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SLIRP, |
| 941 | vlan, NULL, model, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 942 | slirp_receive, NULL, NULL, |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 943 | net_slirp_cleanup, s); |
Jan Kiszka | fc57bc5 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 944 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 945 | "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n'); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 949 | static SlirpState *slirp_lookup(Monitor *mon, const char *vlan, |
| 950 | const char *stack) |
| 951 | { |
| 952 | VLANClientState *vc; |
| 953 | |
| 954 | if (vlan) { |
| 955 | vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack); |
| 956 | if (!vc) { |
| 957 | return NULL; |
| 958 | } |
| 959 | if (strcmp(vc->model, "user")) { |
| 960 | monitor_printf(mon, "invalid device specified\n"); |
| 961 | return NULL; |
| 962 | } |
| 963 | return vc->opaque; |
| 964 | } else { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 965 | if (QTAILQ_EMPTY(&slirp_stacks)) { |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 966 | monitor_printf(mon, "user mode network stack not in use\n"); |
| 967 | return NULL; |
| 968 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 969 | return QTAILQ_FIRST(&slirp_stacks); |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 973 | void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict) |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 974 | { |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 975 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 976 | int host_port; |
| 977 | char buf[256] = ""; |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 978 | const char *src_str, *p; |
| 979 | SlirpState *s; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 980 | int is_udp = 0; |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 981 | int err; |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 982 | const char *arg1 = qdict_get_str(qdict, "arg1"); |
| 983 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
| 984 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 985 | |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 986 | if (arg2) { |
| 987 | s = slirp_lookup(mon, arg1, arg2); |
| 988 | src_str = arg3; |
| 989 | } else { |
| 990 | s = slirp_lookup(mon, NULL, NULL); |
| 991 | src_str = arg1; |
| 992 | } |
| 993 | if (!s) { |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 994 | return; |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 995 | } |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 996 | |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 997 | if (!src_str || !src_str[0]) |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 998 | goto fail_syntax; |
| 999 | |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 1000 | p = src_str; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1001 | get_str_sep(buf, sizeof(buf), &p, ':'); |
| 1002 | |
| 1003 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
| 1004 | is_udp = 0; |
| 1005 | } else if (!strcmp(buf, "udp")) { |
| 1006 | is_udp = 1; |
| 1007 | } else { |
| 1008 | goto fail_syntax; |
| 1009 | } |
| 1010 | |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1011 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1012 | goto fail_syntax; |
| 1013 | } |
| 1014 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { |
| 1015 | goto fail_syntax; |
| 1016 | } |
| 1017 | |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1018 | host_port = atoi(p); |
| 1019 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1020 | err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp, |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1021 | host_addr, host_port); |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1022 | |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 1023 | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, |
| 1024 | err ? "removed" : "not found"); |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1025 | return; |
| 1026 | |
| 1027 | fail_syntax: |
| 1028 | monitor_printf(mon, "invalid format\n"); |
| 1029 | } |
| 1030 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1031 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1032 | int legacy_format) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1033 | { |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1034 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1035 | struct in_addr guest_addr = { .s_addr = 0 }; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1036 | int host_port, guest_port; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1037 | const char *p; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1038 | char buf[256]; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1039 | int is_udp; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1040 | char *end; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1041 | |
| 1042 | p = redir_str; |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 1043 | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1044 | goto fail_syntax; |
| 1045 | } |
| 1046 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
| 1047 | is_udp = 0; |
| 1048 | } else if (!strcmp(buf, "udp")) { |
| 1049 | is_udp = 1; |
| 1050 | } else { |
| 1051 | goto fail_syntax; |
| 1052 | } |
| 1053 | |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1054 | if (!legacy_format) { |
| 1055 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1056 | goto fail_syntax; |
| 1057 | } |
| 1058 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { |
| 1059 | goto fail_syntax; |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) { |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1064 | goto fail_syntax; |
| 1065 | } |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1066 | host_port = strtol(buf, &end, 0); |
| 1067 | if (*end != '\0' || host_port < 1 || host_port > 65535) { |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1068 | goto fail_syntax; |
| 1069 | } |
| 1070 | |
| 1071 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1072 | goto fail_syntax; |
| 1073 | } |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1074 | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1075 | goto fail_syntax; |
| 1076 | } |
| 1077 | |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1078 | guest_port = strtol(p, &end, 0); |
| 1079 | if (*end != '\0' || guest_port < 1 || guest_port > 65535) { |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1080 | goto fail_syntax; |
| 1081 | } |
| 1082 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1083 | if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr, |
| 1084 | guest_port) < 0) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1085 | qemu_error("could not set up host forwarding rule '%s'\n", |
| 1086 | redir_str); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1087 | return -1; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1088 | } |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1089 | return 0; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1090 | |
| 1091 | fail_syntax: |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1092 | qemu_error("invalid host forwarding rule '%s'\n", redir_str); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1093 | return -1; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 1096 | void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict) |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1097 | { |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 1098 | const char *redir_str; |
| 1099 | SlirpState *s; |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 1100 | const char *arg1 = qdict_get_str(qdict, "arg1"); |
| 1101 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
| 1102 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); |
Jan Kiszka | f13b572 | 2009-06-24 14:42:32 +0200 | [diff] [blame] | 1103 | |
| 1104 | if (arg2) { |
| 1105 | s = slirp_lookup(mon, arg1, arg2); |
| 1106 | redir_str = arg3; |
| 1107 | } else { |
| 1108 | s = slirp_lookup(mon, NULL, NULL); |
| 1109 | redir_str = arg1; |
| 1110 | } |
| 1111 | if (s) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1112 | slirp_hostfwd(s, redir_str, 0); |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1117 | int net_slirp_redir(const char *redir_str) |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1118 | { |
| 1119 | struct slirp_config_str *config; |
| 1120 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1121 | if (QTAILQ_EMPTY(&slirp_stacks)) { |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1122 | config = qemu_malloc(sizeof(*config)); |
| 1123 | pstrcpy(config->str, sizeof(config->str), redir_str); |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1124 | config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY; |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1125 | config->next = slirp_configs; |
| 1126 | slirp_configs = config; |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1127 | return 0; |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1130 | return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1); |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1133 | #ifndef _WIN32 |
| 1134 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1135 | /* automatic user mode samba server configuration */ |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1136 | static void slirp_smb_cleanup(SlirpState *s) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1137 | { |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1138 | char cmd[128]; |
Jan Kiszka | 09c1892 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1139 | |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1140 | if (s->smb_dir[0] != '\0') { |
| 1141 | snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir); |
| 1142 | system(cmd); |
| 1143 | s->smb_dir[0] = '\0'; |
| 1144 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1147 | static int slirp_smb(SlirpState* s, const char *exported_dir, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1148 | struct in_addr vserver_addr) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1149 | { |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1150 | static int instance; |
| 1151 | char smb_conf[128]; |
| 1152 | char smb_cmdline[128]; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1153 | FILE *f; |
| 1154 | |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1155 | snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d", |
| 1156 | (long)getpid(), instance++); |
| 1157 | if (mkdir(s->smb_dir, 0700) < 0) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1158 | qemu_error("could not create samba server dir '%s'\n", s->smb_dir); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1159 | return -1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1160 | } |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1161 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf"); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1162 | |
| 1163 | f = fopen(smb_conf, "w"); |
| 1164 | if (!f) { |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1165 | slirp_smb_cleanup(s); |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1166 | qemu_error("could not create samba server configuration file '%s'\n", |
| 1167 | smb_conf); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1168 | return -1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1169 | } |
| 1170 | fprintf(f, |
| 1171 | "[global]\n" |
| 1172 | "private dir=%s\n" |
| 1173 | "smb ports=0\n" |
| 1174 | "socket address=127.0.0.1\n" |
| 1175 | "pid directory=%s\n" |
| 1176 | "lock directory=%s\n" |
| 1177 | "log file=%s/log.smbd\n" |
| 1178 | "smb passwd file=%s/smbpasswd\n" |
| 1179 | "security = share\n" |
| 1180 | "[qemu]\n" |
| 1181 | "path=%s\n" |
| 1182 | "read only=no\n" |
| 1183 | "guest ok=yes\n", |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1184 | s->smb_dir, |
| 1185 | s->smb_dir, |
| 1186 | s->smb_dir, |
| 1187 | s->smb_dir, |
| 1188 | s->smb_dir, |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1189 | exported_dir |
| 1190 | ); |
| 1191 | fclose(f); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1192 | |
| 1193 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
| 1194 | SMBD_COMMAND, smb_conf); |
| 1195 | |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1196 | if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) { |
Jan Kiszka | 2843246 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1197 | slirp_smb_cleanup(s); |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1198 | qemu_error("conflicting/invalid smbserver address\n"); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1199 | return -1; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1200 | } |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1201 | return 0; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1204 | /* automatic user mode samba server configuration (legacy interface) */ |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1205 | int net_slirp_smb(const char *exported_dir) |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1206 | { |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1207 | struct in_addr vserver_addr = { .s_addr = 0 }; |
| 1208 | |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1209 | if (legacy_smb_export) { |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1210 | fprintf(stderr, "-smb given twice\n"); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1211 | return -1; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1212 | } |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1213 | legacy_smb_export = exported_dir; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1214 | if (!QTAILQ_EMPTY(&slirp_stacks)) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1215 | return slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1216 | vserver_addr); |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1217 | } |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1218 | return 0; |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1221 | #endif /* !defined(_WIN32) */ |
Jan Kiszka | b8e8af3 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 1222 | |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1223 | struct GuestFwd { |
aliguori | 8ca9217 | 2009-02-16 15:34:18 +0000 | [diff] [blame] | 1224 | CharDriverState *hd; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1225 | struct in_addr server; |
aliguori | 8ca9217 | 2009-02-16 15:34:18 +0000 | [diff] [blame] | 1226 | int port; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1227 | Slirp *slirp; |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 1228 | }; |
aliguori | 8ca9217 | 2009-02-16 15:34:18 +0000 | [diff] [blame] | 1229 | |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1230 | static int guestfwd_can_read(void *opaque) |
aliguori | 8ca9217 | 2009-02-16 15:34:18 +0000 | [diff] [blame] | 1231 | { |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1232 | struct GuestFwd *fwd = opaque; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1233 | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port); |
aliguori | 8ca9217 | 2009-02-16 15:34:18 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1236 | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) |
aliguori | 8ca9217 | 2009-02-16 15:34:18 +0000 | [diff] [blame] | 1237 | { |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1238 | struct GuestFwd *fwd = opaque; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1239 | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); |
aliguori | 8ca9217 | 2009-02-16 15:34:18 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1242 | static int slirp_guestfwd(SlirpState *s, const char *config_str, |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1243 | int legacy_format) |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1244 | { |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1245 | struct in_addr server = { .s_addr = 0 }; |
| 1246 | struct GuestFwd *fwd; |
| 1247 | const char *p; |
| 1248 | char buf[128]; |
| 1249 | char *end; |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1250 | int port; |
| 1251 | |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1252 | p = config_str; |
| 1253 | if (legacy_format) { |
| 1254 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1255 | goto fail_syntax; |
| 1256 | } |
| 1257 | } else { |
| 1258 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1259 | goto fail_syntax; |
| 1260 | } |
| 1261 | if (strcmp(buf, "tcp") && buf[0] != '\0') { |
| 1262 | goto fail_syntax; |
| 1263 | } |
| 1264 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 1265 | goto fail_syntax; |
| 1266 | } |
| 1267 | if (buf[0] != '\0' && !inet_aton(buf, &server)) { |
| 1268 | goto fail_syntax; |
| 1269 | } |
| 1270 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { |
| 1271 | goto fail_syntax; |
| 1272 | } |
| 1273 | } |
| 1274 | port = strtol(buf, &end, 10); |
| 1275 | if (*end != '\0' || port < 1 || port > 65535) { |
| 1276 | goto fail_syntax; |
| 1277 | } |
| 1278 | |
| 1279 | fwd = qemu_malloc(sizeof(struct GuestFwd)); |
| 1280 | snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port); |
| 1281 | fwd->hd = qemu_chr_open(buf, p, NULL); |
| 1282 | if (!fwd->hd) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1283 | qemu_error("could not open guest forwarding device '%s'\n", buf); |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1284 | qemu_free(fwd); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1285 | return -1; |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1286 | } |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1287 | |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1288 | if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1289 | qemu_error("conflicting/invalid host:port in guest forwarding " |
| 1290 | "rule '%s'\n", config_str); |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1291 | qemu_free(fwd); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1292 | return -1; |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1293 | } |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1294 | fwd->server = server; |
| 1295 | fwd->port = port; |
| 1296 | fwd->slirp = s->slirp; |
| 1297 | |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1298 | qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read, |
| 1299 | NULL, fwd); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1300 | return 0; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1301 | |
| 1302 | fail_syntax: |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1303 | qemu_error("invalid guest forwarding rule '%s'\n", config_str); |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 1304 | return -1; |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1305 | } |
| 1306 | |
Jan Kiszka | 6dbe553 | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 1307 | void do_info_usernet(Monitor *mon) |
| 1308 | { |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1309 | SlirpState *s; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1310 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1311 | QTAILQ_FOREACH(s, &slirp_stacks, entry) { |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1312 | monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name); |
| 1313 | slirp_connection_info(s->slirp, mon); |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1314 | } |
Jan Kiszka | 6dbe553 | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1317 | #endif /* CONFIG_SLIRP */ |
| 1318 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1319 | #if defined(CONFIG_VDE) |
| 1320 | typedef struct VDEState { |
| 1321 | VLANClientState *vc; |
| 1322 | VDECONN *vde; |
| 1323 | } VDEState; |
| 1324 | |
| 1325 | static void vde_to_qemu(void *opaque) |
| 1326 | { |
| 1327 | VDEState *s = opaque; |
| 1328 | uint8_t buf[4096]; |
| 1329 | int size; |
| 1330 | |
Paul Brook | cc63bb0 | 2009-05-03 22:40:54 +0100 | [diff] [blame] | 1331 | size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1332 | if (size > 0) { |
| 1333 | qemu_send_packet(s->vc, buf, size); |
| 1334 | } |
| 1335 | } |
| 1336 | |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1337 | static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1338 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 1339 | VDEState *s = vc->opaque; |
Anthony Liguori | 068daed | 2009-06-10 20:24:44 -0500 | [diff] [blame] | 1340 | ssize_t ret; |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1341 | |
| 1342 | do { |
| 1343 | ret = vde_send(s->vde, (const char *)buf, size, 0); |
| 1344 | } while (ret < 0 && errno == EINTR); |
| 1345 | |
| 1346 | return ret; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 1349 | static void vde_cleanup(VLANClientState *vc) |
| 1350 | { |
| 1351 | VDEState *s = vc->opaque; |
| 1352 | qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL); |
| 1353 | vde_close(s->vde); |
| 1354 | qemu_free(s); |
| 1355 | } |
| 1356 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1357 | static int net_vde_init(VLANState *vlan, const char *model, |
| 1358 | const char *name, const char *sock, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1359 | int port, const char *group, int mode) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1360 | { |
| 1361 | VDEState *s; |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 1362 | char *init_group = (char *)group; |
| 1363 | char *init_sock = (char *)sock; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1364 | |
| 1365 | struct vde_open_args args = { |
| 1366 | .port = port, |
| 1367 | .group = init_group, |
| 1368 | .mode = mode, |
| 1369 | }; |
| 1370 | |
| 1371 | s = qemu_mallocz(sizeof(VDEState)); |
Paul Brook | cc63bb0 | 2009-05-03 22:40:54 +0100 | [diff] [blame] | 1372 | s->vde = vde_open(init_sock, (char *)"QEMU", &args); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1373 | if (!s->vde){ |
| 1374 | free(s); |
| 1375 | return -1; |
| 1376 | } |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 1377 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_VDE, |
| 1378 | vlan, NULL, model, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 1379 | vde_receive, NULL, NULL, |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 1380 | vde_cleanup, s); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1381 | qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s); |
aliguori | 7cb7434b | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 1382 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d", |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1383 | sock, vde_datafd(s->vde)); |
| 1384 | return 0; |
| 1385 | } |
| 1386 | #endif |
| 1387 | |
| 1388 | /* network connection */ |
| 1389 | typedef struct NetSocketState { |
| 1390 | VLANClientState *vc; |
| 1391 | int fd; |
| 1392 | int state; /* 0 = getting length, 1 = getting data */ |
aliguori | abcd2ba | 2009-02-27 19:54:01 +0000 | [diff] [blame] | 1393 | unsigned int index; |
| 1394 | unsigned int packet_len; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1395 | uint8_t buf[4096]; |
| 1396 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
| 1397 | } NetSocketState; |
| 1398 | |
| 1399 | typedef struct NetSocketListenState { |
| 1400 | VLANState *vlan; |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1401 | char *model; |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1402 | char *name; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1403 | int fd; |
| 1404 | } NetSocketListenState; |
| 1405 | |
| 1406 | /* XXX: we consider we can send the whole packet without blocking */ |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1407 | 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] | 1408 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 1409 | NetSocketState *s = vc->opaque; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1410 | uint32_t len; |
| 1411 | len = htonl(size); |
| 1412 | |
| 1413 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1414 | return send_all(s->fd, buf, size); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1417 | 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] | 1418 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 1419 | NetSocketState *s = vc->opaque; |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1420 | |
Stefan Weil | 7050326 | 2009-06-13 13:05:27 +0200 | [diff] [blame] | 1421 | return sendto(s->fd, (const void *)buf, size, 0, |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1422 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | static void net_socket_send(void *opaque) |
| 1426 | { |
| 1427 | NetSocketState *s = opaque; |
aliguori | abcd2ba | 2009-02-27 19:54:01 +0000 | [diff] [blame] | 1428 | int size, err; |
| 1429 | unsigned l; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1430 | uint8_t buf1[4096]; |
| 1431 | const uint8_t *buf; |
| 1432 | |
Blue Swirl | c5b76b3 | 2009-06-13 08:44:31 +0000 | [diff] [blame] | 1433 | size = recv(s->fd, (void *)buf1, sizeof(buf1), 0); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1434 | if (size < 0) { |
| 1435 | err = socket_error(); |
| 1436 | if (err != EWOULDBLOCK) |
| 1437 | goto eoc; |
| 1438 | } else if (size == 0) { |
| 1439 | /* end of connection */ |
| 1440 | eoc: |
| 1441 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 1442 | closesocket(s->fd); |
| 1443 | return; |
| 1444 | } |
| 1445 | buf = buf1; |
| 1446 | while (size > 0) { |
| 1447 | /* reassemble a packet from the network */ |
| 1448 | switch(s->state) { |
| 1449 | case 0: |
| 1450 | l = 4 - s->index; |
| 1451 | if (l > size) |
| 1452 | l = size; |
| 1453 | memcpy(s->buf + s->index, buf, l); |
| 1454 | buf += l; |
| 1455 | size -= l; |
| 1456 | s->index += l; |
| 1457 | if (s->index == 4) { |
| 1458 | /* got length */ |
| 1459 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 1460 | s->index = 0; |
| 1461 | s->state = 1; |
| 1462 | } |
| 1463 | break; |
| 1464 | case 1: |
| 1465 | l = s->packet_len - s->index; |
| 1466 | if (l > size) |
| 1467 | l = size; |
aliguori | abcd2ba | 2009-02-27 19:54:01 +0000 | [diff] [blame] | 1468 | if (s->index + l <= sizeof(s->buf)) { |
| 1469 | memcpy(s->buf + s->index, buf, l); |
| 1470 | } else { |
| 1471 | fprintf(stderr, "serious error: oversized packet received," |
| 1472 | "connection terminated.\n"); |
| 1473 | s->state = 0; |
| 1474 | goto eoc; |
| 1475 | } |
| 1476 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1477 | s->index += l; |
| 1478 | buf += l; |
| 1479 | size -= l; |
| 1480 | if (s->index >= s->packet_len) { |
| 1481 | qemu_send_packet(s->vc, s->buf, s->packet_len); |
| 1482 | s->index = 0; |
| 1483 | s->state = 0; |
| 1484 | } |
| 1485 | break; |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | static void net_socket_send_dgram(void *opaque) |
| 1491 | { |
| 1492 | NetSocketState *s = opaque; |
| 1493 | int size; |
| 1494 | |
Blue Swirl | c5b76b3 | 2009-06-13 08:44:31 +0000 | [diff] [blame] | 1495 | size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1496 | if (size < 0) |
| 1497 | return; |
| 1498 | if (size == 0) { |
| 1499 | /* end of connection */ |
| 1500 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 1501 | return; |
| 1502 | } |
| 1503 | qemu_send_packet(s->vc, s->buf, size); |
| 1504 | } |
| 1505 | |
| 1506 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
| 1507 | { |
| 1508 | struct ip_mreq imr; |
| 1509 | int fd; |
| 1510 | int val, ret; |
| 1511 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
| 1512 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", |
| 1513 | inet_ntoa(mcastaddr->sin_addr), |
| 1514 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
| 1515 | return -1; |
| 1516 | |
| 1517 | } |
| 1518 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 1519 | if (fd < 0) { |
| 1520 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 1521 | return -1; |
| 1522 | } |
| 1523 | |
| 1524 | val = 1; |
| 1525 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 1526 | (const char *)&val, sizeof(val)); |
| 1527 | if (ret < 0) { |
| 1528 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 1529 | goto fail; |
| 1530 | } |
| 1531 | |
| 1532 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 1533 | if (ret < 0) { |
| 1534 | perror("bind"); |
| 1535 | goto fail; |
| 1536 | } |
| 1537 | |
| 1538 | /* Add host to multicast group */ |
| 1539 | imr.imr_multiaddr = mcastaddr->sin_addr; |
| 1540 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 1541 | |
| 1542 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 1543 | (const char *)&imr, sizeof(struct ip_mreq)); |
| 1544 | if (ret < 0) { |
| 1545 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 1546 | goto fail; |
| 1547 | } |
| 1548 | |
| 1549 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
| 1550 | val = 1; |
| 1551 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 1552 | (const char *)&val, sizeof(val)); |
| 1553 | if (ret < 0) { |
| 1554 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 1555 | goto fail; |
| 1556 | } |
| 1557 | |
| 1558 | socket_set_nonblock(fd); |
| 1559 | return fd; |
| 1560 | fail: |
| 1561 | if (fd >= 0) |
| 1562 | closesocket(fd); |
| 1563 | return -1; |
| 1564 | } |
| 1565 | |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 1566 | static void net_socket_cleanup(VLANClientState *vc) |
| 1567 | { |
| 1568 | NetSocketState *s = vc->opaque; |
| 1569 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 1570 | close(s->fd); |
| 1571 | qemu_free(s); |
| 1572 | } |
| 1573 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1574 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, |
| 1575 | const char *model, |
| 1576 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1577 | int fd, int is_connected) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1578 | { |
| 1579 | struct sockaddr_in saddr; |
| 1580 | int newfd; |
| 1581 | socklen_t saddr_len; |
| 1582 | NetSocketState *s; |
| 1583 | |
| 1584 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 1585 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 1586 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 1587 | */ |
| 1588 | |
| 1589 | if (is_connected) { |
| 1590 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 1591 | /* must be bound */ |
| 1592 | if (saddr.sin_addr.s_addr==0) { |
| 1593 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
| 1594 | fd); |
| 1595 | return NULL; |
| 1596 | } |
| 1597 | /* clone dgram socket */ |
| 1598 | newfd = net_socket_mcast_create(&saddr); |
| 1599 | if (newfd < 0) { |
| 1600 | /* error already reported by net_socket_mcast_create() */ |
| 1601 | close(fd); |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | /* clone newfd to fd, close newfd */ |
| 1605 | dup2(newfd, fd); |
| 1606 | close(newfd); |
| 1607 | |
| 1608 | } else { |
| 1609 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 1610 | fd, strerror(errno)); |
| 1611 | return NULL; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | s = qemu_mallocz(sizeof(NetSocketState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1616 | s->fd = fd; |
| 1617 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 1618 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
| 1619 | vlan, NULL, model, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 1620 | net_socket_receive_dgram, NULL, NULL, |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 1621 | net_socket_cleanup, s); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1622 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 1623 | |
| 1624 | /* mcast: save bound address as dst */ |
| 1625 | if (is_connected) s->dgram_dst=saddr; |
| 1626 | |
| 1627 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 1628 | "socket: fd=%d (%s mcast=%s:%d)", |
| 1629 | fd, is_connected? "cloned" : "", |
| 1630 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 1631 | return s; |
| 1632 | } |
| 1633 | |
| 1634 | static void net_socket_connect(void *opaque) |
| 1635 | { |
| 1636 | NetSocketState *s = opaque; |
| 1637 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 1638 | } |
| 1639 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1640 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, |
| 1641 | const char *model, |
| 1642 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1643 | int fd, int is_connected) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1644 | { |
| 1645 | NetSocketState *s; |
| 1646 | s = qemu_mallocz(sizeof(NetSocketState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1647 | s->fd = fd; |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 1648 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
| 1649 | vlan, NULL, model, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 1650 | net_socket_receive, NULL, NULL, |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 1651 | net_socket_cleanup, s); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1652 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 1653 | "socket: fd=%d", fd); |
| 1654 | if (is_connected) { |
| 1655 | net_socket_connect(s); |
| 1656 | } else { |
| 1657 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 1658 | } |
| 1659 | return s; |
| 1660 | } |
| 1661 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1662 | static NetSocketState *net_socket_fd_init(VLANState *vlan, |
| 1663 | const char *model, const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1664 | int fd, int is_connected) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1665 | { |
Michael S. Tsirkin | acedcfb | 2009-09-30 18:56:44 +0000 | [diff] [blame] | 1666 | int so_type = -1, optlen=sizeof(so_type); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1667 | |
| 1668 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
| 1669 | (socklen_t *)&optlen)< 0) { |
| 1670 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); |
| 1671 | return NULL; |
| 1672 | } |
| 1673 | switch(so_type) { |
| 1674 | case SOCK_DGRAM: |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1675 | return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1676 | case SOCK_STREAM: |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1677 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1678 | default: |
| 1679 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 1680 | 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] | 1681 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1682 | } |
| 1683 | return NULL; |
| 1684 | } |
| 1685 | |
| 1686 | static void net_socket_accept(void *opaque) |
| 1687 | { |
| 1688 | NetSocketListenState *s = opaque; |
| 1689 | NetSocketState *s1; |
| 1690 | struct sockaddr_in saddr; |
| 1691 | socklen_t len; |
| 1692 | int fd; |
| 1693 | |
| 1694 | for(;;) { |
| 1695 | len = sizeof(saddr); |
| 1696 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
| 1697 | if (fd < 0 && errno != EINTR) { |
| 1698 | return; |
| 1699 | } else if (fd >= 0) { |
| 1700 | break; |
| 1701 | } |
| 1702 | } |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1703 | s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1704 | if (!s1) { |
| 1705 | closesocket(fd); |
| 1706 | } else { |
| 1707 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
| 1708 | "socket: connection from %s:%d", |
| 1709 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 1710 | } |
| 1711 | } |
| 1712 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1713 | static int net_socket_listen_init(VLANState *vlan, |
| 1714 | const char *model, |
| 1715 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1716 | const char *host_str) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1717 | { |
| 1718 | NetSocketListenState *s; |
| 1719 | int fd, val, ret; |
| 1720 | struct sockaddr_in saddr; |
| 1721 | |
| 1722 | if (parse_host_port(&saddr, host_str) < 0) |
| 1723 | return -1; |
| 1724 | |
| 1725 | s = qemu_mallocz(sizeof(NetSocketListenState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1726 | |
| 1727 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 1728 | if (fd < 0) { |
| 1729 | perror("socket"); |
| 1730 | return -1; |
| 1731 | } |
| 1732 | socket_set_nonblock(fd); |
| 1733 | |
| 1734 | /* allow fast reuse */ |
| 1735 | val = 1; |
| 1736 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 1737 | |
| 1738 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 1739 | if (ret < 0) { |
| 1740 | perror("bind"); |
| 1741 | return -1; |
| 1742 | } |
| 1743 | ret = listen(fd, 0); |
| 1744 | if (ret < 0) { |
| 1745 | perror("listen"); |
| 1746 | return -1; |
| 1747 | } |
| 1748 | s->vlan = vlan; |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 1749 | s->model = qemu_strdup(model); |
| 1750 | s->name = name ? qemu_strdup(name) : NULL; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1751 | s->fd = fd; |
| 1752 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1756 | static int net_socket_connect_init(VLANState *vlan, |
| 1757 | const char *model, |
| 1758 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1759 | const char *host_str) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1760 | { |
| 1761 | NetSocketState *s; |
| 1762 | int fd, connected, ret, err; |
| 1763 | struct sockaddr_in saddr; |
| 1764 | |
| 1765 | if (parse_host_port(&saddr, host_str) < 0) |
| 1766 | return -1; |
| 1767 | |
| 1768 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 1769 | if (fd < 0) { |
| 1770 | perror("socket"); |
| 1771 | return -1; |
| 1772 | } |
| 1773 | socket_set_nonblock(fd); |
| 1774 | |
| 1775 | connected = 0; |
| 1776 | for(;;) { |
| 1777 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 1778 | if (ret < 0) { |
| 1779 | err = socket_error(); |
| 1780 | if (err == EINTR || err == EWOULDBLOCK) { |
| 1781 | } else if (err == EINPROGRESS) { |
| 1782 | break; |
| 1783 | #ifdef _WIN32 |
| 1784 | } else if (err == WSAEALREADY) { |
| 1785 | break; |
| 1786 | #endif |
| 1787 | } else { |
| 1788 | perror("connect"); |
| 1789 | closesocket(fd); |
| 1790 | return -1; |
| 1791 | } |
| 1792 | } else { |
| 1793 | connected = 1; |
| 1794 | break; |
| 1795 | } |
| 1796 | } |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1797 | s = net_socket_fd_init(vlan, model, name, fd, connected); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1798 | if (!s) |
| 1799 | return -1; |
| 1800 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 1801 | "socket: connect to %s:%d", |
| 1802 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1806 | static int net_socket_mcast_init(VLANState *vlan, |
| 1807 | const char *model, |
| 1808 | const char *name, |
aliguori | bf38c1a | 2009-01-07 17:42:25 +0000 | [diff] [blame] | 1809 | const char *host_str) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1810 | { |
| 1811 | NetSocketState *s; |
| 1812 | int fd; |
| 1813 | struct sockaddr_in saddr; |
| 1814 | |
| 1815 | if (parse_host_port(&saddr, host_str) < 0) |
| 1816 | return -1; |
| 1817 | |
| 1818 | |
| 1819 | fd = net_socket_mcast_create(&saddr); |
| 1820 | if (fd < 0) |
| 1821 | return -1; |
| 1822 | |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 1823 | s = net_socket_fd_init(vlan, model, name, fd, 0); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1824 | if (!s) |
| 1825 | return -1; |
| 1826 | |
| 1827 | s->dgram_dst = saddr; |
| 1828 | |
| 1829 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 1830 | "socket: mcast=%s:%d", |
| 1831 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 1832 | return 0; |
| 1833 | |
| 1834 | } |
| 1835 | |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1836 | typedef struct DumpState { |
| 1837 | VLANClientState *pcap_vc; |
| 1838 | int fd; |
| 1839 | int pcap_caplen; |
| 1840 | } DumpState; |
| 1841 | |
| 1842 | #define PCAP_MAGIC 0xa1b2c3d4 |
| 1843 | |
| 1844 | struct pcap_file_hdr { |
| 1845 | uint32_t magic; |
| 1846 | uint16_t version_major; |
| 1847 | uint16_t version_minor; |
| 1848 | int32_t thiszone; |
| 1849 | uint32_t sigfigs; |
| 1850 | uint32_t snaplen; |
| 1851 | uint32_t linktype; |
| 1852 | }; |
| 1853 | |
| 1854 | struct pcap_sf_pkthdr { |
| 1855 | struct { |
| 1856 | int32_t tv_sec; |
| 1857 | int32_t tv_usec; |
| 1858 | } ts; |
| 1859 | uint32_t caplen; |
| 1860 | uint32_t len; |
| 1861 | }; |
| 1862 | |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1863 | 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] | 1864 | { |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 1865 | DumpState *s = vc->opaque; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1866 | struct pcap_sf_pkthdr hdr; |
| 1867 | int64_t ts; |
| 1868 | int caplen; |
| 1869 | |
| 1870 | /* Early return in case of previous error. */ |
| 1871 | if (s->fd < 0) { |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1872 | return size; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 1875 | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec()); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1876 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
| 1877 | |
Jan Kiszka | 37cb6fc | 2009-05-10 22:23:23 +0200 | [diff] [blame] | 1878 | hdr.ts.tv_sec = ts / 1000000; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1879 | hdr.ts.tv_usec = ts % 1000000; |
| 1880 | hdr.caplen = caplen; |
| 1881 | hdr.len = size; |
| 1882 | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || |
| 1883 | write(s->fd, buf, caplen) != caplen) { |
| 1884 | qemu_log("-net dump write error - stop dump\n"); |
| 1885 | close(s->fd); |
| 1886 | s->fd = -1; |
| 1887 | } |
Mark McLoughlin | 4f1c942 | 2009-05-18 13:40:55 +0100 | [diff] [blame] | 1888 | |
| 1889 | return size; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | static void net_dump_cleanup(VLANClientState *vc) |
| 1893 | { |
| 1894 | DumpState *s = vc->opaque; |
| 1895 | |
| 1896 | close(s->fd); |
| 1897 | qemu_free(s); |
| 1898 | } |
| 1899 | |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1900 | static int net_dump_init(VLANState *vlan, const char *device, |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1901 | const char *name, const char *filename, int len) |
| 1902 | { |
| 1903 | struct pcap_file_hdr hdr; |
| 1904 | DumpState *s; |
| 1905 | |
| 1906 | s = qemu_malloc(sizeof(DumpState)); |
| 1907 | |
Filip Navara | 024431b | 2009-06-17 19:48:08 +0200 | [diff] [blame] | 1908 | s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1909 | if (s->fd < 0) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1910 | qemu_error("-net dump: can't open %s\n", filename); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1911 | return -1; |
| 1912 | } |
| 1913 | |
| 1914 | s->pcap_caplen = len; |
| 1915 | |
| 1916 | hdr.magic = PCAP_MAGIC; |
| 1917 | hdr.version_major = 2; |
| 1918 | hdr.version_minor = 4; |
| 1919 | hdr.thiszone = 0; |
| 1920 | hdr.sigfigs = 0; |
| 1921 | hdr.snaplen = s->pcap_caplen; |
| 1922 | hdr.linktype = 1; |
| 1923 | |
| 1924 | if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 1925 | qemu_error("-net dump write error: %s\n", strerror(errno)); |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1926 | close(s->fd); |
| 1927 | qemu_free(s); |
| 1928 | return -1; |
| 1929 | } |
| 1930 | |
Mark McLoughlin | bb6e636 | 2009-10-22 17:43:38 +0100 | [diff] [blame] | 1931 | s->pcap_vc = qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP, |
| 1932 | vlan, NULL, device, name, NULL, |
Mark McLoughlin | 70783b9 | 2009-10-22 17:43:42 +0100 | [diff] [blame] | 1933 | dump_receive, NULL, NULL, |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1934 | net_dump_cleanup, s); |
| 1935 | snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str), |
| 1936 | "dump to %s (len=%d)", filename, len); |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1940 | /* find or alloc a new VLAN */ |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1941 | VLANState *qemu_find_vlan(int id, int allocate) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1942 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1943 | VLANState *vlan; |
| 1944 | |
| 1945 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 1946 | if (vlan->id == id) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1947 | return vlan; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1948 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1949 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1950 | |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1951 | if (!allocate) { |
| 1952 | return NULL; |
| 1953 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1954 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1955 | vlan = qemu_mallocz(sizeof(VLANState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1956 | vlan->id = id; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1957 | QTAILQ_INIT(&vlan->clients); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 1958 | |
| 1959 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
| 1960 | qemu_vlan_deliver_packet_iov, |
| 1961 | vlan); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1962 | |
| 1963 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
| 1964 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1965 | return vlan; |
| 1966 | } |
| 1967 | |
Gerd Hoffmann | 2ef924b | 2009-10-21 15:25:24 +0200 | [diff] [blame] | 1968 | VLANClientState *qemu_find_netdev(const char *id) |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1969 | { |
| 1970 | VLANClientState *vc; |
| 1971 | |
| 1972 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
| 1973 | if (!strcmp(vc->name, id)) { |
| 1974 | return vc; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | return NULL; |
| 1979 | } |
| 1980 | |
aliguori | 7697079 | 2009-02-11 15:20:03 +0000 | [diff] [blame] | 1981 | static int nic_get_free_idx(void) |
| 1982 | { |
| 1983 | int index; |
| 1984 | |
| 1985 | for (index = 0; index < MAX_NICS; index++) |
| 1986 | if (!nd_table[index].used) |
| 1987 | return index; |
| 1988 | return -1; |
| 1989 | } |
| 1990 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 1991 | int qemu_show_nic_models(const char *arg, const char *const *models) |
| 1992 | { |
| 1993 | int i; |
| 1994 | |
| 1995 | if (!arg || strcmp(arg, "?")) |
| 1996 | return 0; |
| 1997 | |
| 1998 | fprintf(stderr, "qemu: Supported NIC models: "); |
| 1999 | for (i = 0 ; models[i]; i++) |
| 2000 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
| 2001 | return 1; |
| 2002 | } |
| 2003 | |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 2004 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
| 2005 | { |
| 2006 | const char *models[2]; |
| 2007 | |
| 2008 | models[0] = model; |
| 2009 | models[1] = NULL; |
| 2010 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 2011 | if (qemu_show_nic_models(nd->model, models)) |
| 2012 | exit(0); |
| 2013 | if (qemu_find_nic_model(nd, models, model) < 0) |
| 2014 | exit(1); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 2017 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
| 2018 | const char *default_model) |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 2019 | { |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 2020 | int i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 2021 | |
| 2022 | if (!nd->model) |
Mark McLoughlin | 32a8e14 | 2009-10-06 12:16:51 +0100 | [diff] [blame] | 2023 | nd->model = qemu_strdup(default_model); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 2024 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 2025 | for (i = 0 ; models[i]; i++) { |
| 2026 | if (strcmp(nd->model, models[i]) == 0) |
| 2027 | return i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 2030 | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model); |
| 2031 | return -1; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 2034 | int net_handle_fd_param(Monitor *mon, const char *param) |
Mark McLoughlin | c1d6eed | 2009-07-22 09:11:42 +0100 | [diff] [blame] | 2035 | { |
| 2036 | if (!qemu_isdigit(param[0])) { |
| 2037 | int fd; |
| 2038 | |
| 2039 | fd = monitor_get_fd(mon, param); |
| 2040 | if (fd == -1) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 2041 | qemu_error("No file descriptor named %s found", param); |
Mark McLoughlin | c1d6eed | 2009-07-22 09:11:42 +0100 | [diff] [blame] | 2042 | return -1; |
| 2043 | } |
| 2044 | |
| 2045 | return fd; |
| 2046 | } else { |
| 2047 | return strtol(param, NULL, 0); |
| 2048 | } |
| 2049 | } |
| 2050 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2051 | static int net_init_nic(QemuOpts *opts, |
| 2052 | Monitor *mon, |
| 2053 | const char *name, |
| 2054 | VLANState *vlan) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2055 | { |
| 2056 | int idx; |
| 2057 | NICInfo *nd; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 2058 | const char *netdev; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2059 | |
| 2060 | idx = nic_get_free_idx(); |
| 2061 | if (idx == -1 || nb_nics >= MAX_NICS) { |
| 2062 | qemu_error("Too Many NICs\n"); |
| 2063 | return -1; |
| 2064 | } |
| 2065 | |
| 2066 | nd = &nd_table[idx]; |
| 2067 | |
| 2068 | memset(nd, 0, sizeof(*nd)); |
| 2069 | |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 2070 | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
| 2071 | nd->netdev = qemu_find_netdev(netdev); |
| 2072 | if (!nd->netdev) { |
| 2073 | qemu_error("netdev '%s' not found\n", netdev); |
| 2074 | return -1; |
| 2075 | } |
| 2076 | } else { |
| 2077 | assert(vlan); |
| 2078 | nd->vlan = vlan; |
| 2079 | } |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 2080 | if (name) { |
| 2081 | nd->name = qemu_strdup(name); |
| 2082 | } |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2083 | if (qemu_opt_get(opts, "model")) { |
| 2084 | nd->model = qemu_strdup(qemu_opt_get(opts, "model")); |
| 2085 | } |
| 2086 | if (qemu_opt_get(opts, "addr")) { |
| 2087 | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr")); |
| 2088 | } |
| 2089 | |
| 2090 | nd->macaddr[0] = 0x52; |
| 2091 | nd->macaddr[1] = 0x54; |
| 2092 | nd->macaddr[2] = 0x00; |
| 2093 | nd->macaddr[3] = 0x12; |
| 2094 | nd->macaddr[4] = 0x34; |
| 2095 | nd->macaddr[5] = 0x56 + idx; |
| 2096 | |
| 2097 | if (qemu_opt_get(opts, "macaddr") && |
| 2098 | parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
| 2099 | qemu_error("invalid syntax for ethernet address\n"); |
| 2100 | return -1; |
| 2101 | } |
| 2102 | |
| 2103 | nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED); |
| 2104 | if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED && |
| 2105 | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
| 2106 | qemu_error("invalid # of vectors: %d\n", nd->nvectors); |
| 2107 | return -1; |
| 2108 | } |
| 2109 | |
| 2110 | nd->used = 1; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 2111 | if (vlan) { |
| 2112 | nd->vlan->nb_guest_devs++; |
| 2113 | } |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2114 | nb_nics++; |
| 2115 | |
| 2116 | return idx; |
| 2117 | } |
| 2118 | |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 2119 | #if defined(CONFIG_SLIRP) |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2120 | static int net_init_slirp_configs(const char *name, const char *value, void *opaque) |
| 2121 | { |
| 2122 | struct slirp_config_str *config; |
| 2123 | |
| 2124 | if (strcmp(name, "hostfwd") != 0 && strcmp(name, "guestfwd") != 0) { |
| 2125 | return 0; |
| 2126 | } |
| 2127 | |
| 2128 | config = qemu_mallocz(sizeof(*config)); |
| 2129 | |
| 2130 | pstrcpy(config->str, sizeof(config->str), value); |
| 2131 | |
| 2132 | if (!strcmp(name, "hostfwd")) { |
| 2133 | config->flags = SLIRP_CFG_HOSTFWD; |
| 2134 | } |
| 2135 | |
| 2136 | config->next = slirp_configs; |
| 2137 | slirp_configs = config; |
| 2138 | |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2142 | static int net_init_slirp(QemuOpts *opts, |
| 2143 | Monitor *mon, |
| 2144 | const char *name, |
| 2145 | VLANState *vlan) |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2146 | { |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2147 | struct slirp_config_str *config; |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2148 | const char *vhost; |
| 2149 | const char *vhostname; |
| 2150 | const char *vdhcp_start; |
| 2151 | const char *vnamesrv; |
| 2152 | const char *tftp_export; |
| 2153 | const char *bootfile; |
| 2154 | const char *smb_export; |
| 2155 | const char *vsmbsrv; |
| 2156 | char *vnet = NULL; |
| 2157 | int restricted = 0; |
| 2158 | int ret; |
| 2159 | |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2160 | vhost = qemu_opt_get(opts, "host"); |
| 2161 | vhostname = qemu_opt_get(opts, "hostname"); |
| 2162 | vdhcp_start = qemu_opt_get(opts, "dhcpstart"); |
| 2163 | vnamesrv = qemu_opt_get(opts, "dns"); |
| 2164 | tftp_export = qemu_opt_get(opts, "tftp"); |
| 2165 | bootfile = qemu_opt_get(opts, "bootfile"); |
| 2166 | smb_export = qemu_opt_get(opts, "smb"); |
| 2167 | vsmbsrv = qemu_opt_get(opts, "smbserver"); |
| 2168 | |
| 2169 | if (qemu_opt_get(opts, "ip")) { |
| 2170 | const char *ip = qemu_opt_get(opts, "ip"); |
| 2171 | int l = strlen(ip) + strlen("/24") + 1; |
| 2172 | |
| 2173 | vnet = qemu_malloc(l); |
| 2174 | |
| 2175 | /* emulate legacy ip= parameter */ |
| 2176 | pstrcpy(vnet, l, ip); |
| 2177 | pstrcat(vnet, l, "/24"); |
| 2178 | } |
| 2179 | |
| 2180 | if (qemu_opt_get(opts, "net")) { |
| 2181 | if (vnet) { |
| 2182 | qemu_free(vnet); |
| 2183 | } |
| 2184 | vnet = qemu_strdup(qemu_opt_get(opts, "net")); |
| 2185 | } |
| 2186 | |
| 2187 | if (qemu_opt_get(opts, "restrict") && |
| 2188 | qemu_opt_get(opts, "restrict")[0] == 'y') { |
| 2189 | restricted = 1; |
| 2190 | } |
| 2191 | |
| 2192 | qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0); |
| 2193 | |
| 2194 | ret = net_slirp_init(vlan, "user", name, restricted, vnet, vhost, |
| 2195 | vhostname, tftp_export, bootfile, vdhcp_start, |
| 2196 | vnamesrv, smb_export, vsmbsrv); |
| 2197 | |
| 2198 | while (slirp_configs) { |
| 2199 | config = slirp_configs; |
| 2200 | slirp_configs = config->next; |
| 2201 | qemu_free(config); |
| 2202 | } |
| 2203 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2204 | if (ret != -1 && vlan) { |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2205 | vlan->nb_host_devs++; |
| 2206 | } |
| 2207 | |
| 2208 | qemu_free(vnet); |
| 2209 | |
| 2210 | return ret; |
| 2211 | } |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 2212 | #endif /* CONFIG_SLIRP */ |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2213 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2214 | static int net_init_socket(QemuOpts *opts, |
| 2215 | Monitor *mon, |
| 2216 | const char *name, |
| 2217 | VLANState *vlan) |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 2218 | { |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 2219 | if (qemu_opt_get(opts, "fd")) { |
| 2220 | int fd; |
| 2221 | |
| 2222 | if (qemu_opt_get(opts, "listen") || |
| 2223 | qemu_opt_get(opts, "connect") || |
| 2224 | qemu_opt_get(opts, "mcast")) { |
| 2225 | qemu_error("listen=, connect= and mcast= is invalid with fd=\n"); |
| 2226 | return -1; |
| 2227 | } |
| 2228 | |
| 2229 | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd")); |
| 2230 | if (fd == -1) { |
| 2231 | return -1; |
| 2232 | } |
| 2233 | |
| 2234 | if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { |
| 2235 | close(fd); |
| 2236 | return -1; |
| 2237 | } |
| 2238 | } else if (qemu_opt_get(opts, "listen")) { |
| 2239 | const char *listen; |
| 2240 | |
| 2241 | if (qemu_opt_get(opts, "fd") || |
| 2242 | qemu_opt_get(opts, "connect") || |
| 2243 | qemu_opt_get(opts, "mcast")) { |
| 2244 | qemu_error("fd=, connect= and mcast= is invalid with listen=\n"); |
| 2245 | return -1; |
| 2246 | } |
| 2247 | |
| 2248 | listen = qemu_opt_get(opts, "listen"); |
| 2249 | |
| 2250 | if (net_socket_listen_init(vlan, "socket", name, listen) == -1) { |
| 2251 | return -1; |
| 2252 | } |
| 2253 | } else if (qemu_opt_get(opts, "connect")) { |
| 2254 | const char *connect; |
| 2255 | |
| 2256 | if (qemu_opt_get(opts, "fd") || |
| 2257 | qemu_opt_get(opts, "listen") || |
| 2258 | qemu_opt_get(opts, "mcast")) { |
| 2259 | qemu_error("fd=, listen= and mcast= is invalid with connect=\n"); |
| 2260 | return -1; |
| 2261 | } |
| 2262 | |
| 2263 | connect = qemu_opt_get(opts, "connect"); |
| 2264 | |
| 2265 | if (net_socket_connect_init(vlan, "socket", name, connect) == -1) { |
| 2266 | return -1; |
| 2267 | } |
| 2268 | } else if (qemu_opt_get(opts, "mcast")) { |
| 2269 | const char *mcast; |
| 2270 | |
| 2271 | if (qemu_opt_get(opts, "fd") || |
| 2272 | qemu_opt_get(opts, "connect") || |
| 2273 | qemu_opt_get(opts, "listen")) { |
| 2274 | qemu_error("fd=, connect= and listen= is invalid with mcast=\n"); |
| 2275 | return -1; |
| 2276 | } |
| 2277 | |
| 2278 | mcast = qemu_opt_get(opts, "mcast"); |
| 2279 | |
| 2280 | if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) { |
| 2281 | return -1; |
| 2282 | } |
| 2283 | } else { |
| 2284 | qemu_error("-socket requires fd=, listen=, connect= or mcast=\n"); |
| 2285 | return -1; |
| 2286 | } |
| 2287 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2288 | if (vlan) { |
| 2289 | vlan->nb_host_devs++; |
| 2290 | } |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 2291 | |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 2295 | #ifdef CONFIG_VDE |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2296 | static int net_init_vde(QemuOpts *opts, |
| 2297 | Monitor *mon, |
| 2298 | const char *name, |
| 2299 | VLANState *vlan) |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 2300 | { |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 2301 | const char *sock; |
| 2302 | const char *group; |
| 2303 | int port, mode; |
| 2304 | |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 2305 | sock = qemu_opt_get(opts, "sock"); |
| 2306 | group = qemu_opt_get(opts, "group"); |
| 2307 | |
| 2308 | port = qemu_opt_get_number(opts, "port", 0); |
| 2309 | mode = qemu_opt_get_number(opts, "mode", 0700); |
| 2310 | |
| 2311 | if (net_vde_init(vlan, "vde", name, sock, port, group, mode) == -1) { |
| 2312 | return -1; |
| 2313 | } |
| 2314 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2315 | if (vlan) { |
| 2316 | vlan->nb_host_devs++; |
| 2317 | } |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 2318 | |
| 2319 | return 0; |
| 2320 | } |
| 2321 | #endif |
| 2322 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2323 | static int net_init_dump(QemuOpts *opts, |
| 2324 | Monitor *mon, |
| 2325 | const char *name, |
| 2326 | VLANState *vlan) |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 2327 | { |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 2328 | int len; |
| 2329 | const char *file; |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 2330 | char def_file[128]; |
| 2331 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2332 | assert(vlan); |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 2333 | |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 2334 | file = qemu_opt_get(opts, "file"); |
| 2335 | if (!file) { |
| 2336 | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); |
| 2337 | file = def_file; |
| 2338 | } |
| 2339 | |
| 2340 | len = qemu_opt_get_size(opts, "len", 65536); |
| 2341 | |
| 2342 | return net_dump_init(vlan, "dump", name, file, len); |
| 2343 | } |
| 2344 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2345 | #define NET_COMMON_PARAMS_DESC \ |
| 2346 | { \ |
| 2347 | .name = "type", \ |
| 2348 | .type = QEMU_OPT_STRING, \ |
| 2349 | .help = "net client type (nic, tap etc.)", \ |
| 2350 | }, { \ |
| 2351 | .name = "vlan", \ |
| 2352 | .type = QEMU_OPT_NUMBER, \ |
| 2353 | .help = "vlan number", \ |
| 2354 | }, { \ |
| 2355 | .name = "name", \ |
| 2356 | .type = QEMU_OPT_STRING, \ |
| 2357 | .help = "identifier for monitor commands", \ |
| 2358 | } |
| 2359 | |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 2360 | typedef int (*net_client_init_func)(QemuOpts *opts, |
| 2361 | Monitor *mon, |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2362 | const char *name, |
| 2363 | VLANState *vlan); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2364 | |
| 2365 | /* magic number, but compiler will warn if too small */ |
| 2366 | #define NET_MAX_DESC 20 |
| 2367 | |
| 2368 | static struct { |
| 2369 | const char *type; |
| 2370 | net_client_init_func init; |
| 2371 | QemuOptDesc desc[NET_MAX_DESC]; |
| 2372 | } net_client_types[] = { |
| 2373 | { |
| 2374 | .type = "none", |
| 2375 | .desc = { |
| 2376 | NET_COMMON_PARAMS_DESC, |
| 2377 | { /* end of list */ } |
| 2378 | }, |
| 2379 | }, { |
| 2380 | .type = "nic", |
| 2381 | .init = net_init_nic, |
| 2382 | .desc = { |
| 2383 | NET_COMMON_PARAMS_DESC, |
| 2384 | { |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 2385 | .name = "netdev", |
| 2386 | .type = QEMU_OPT_STRING, |
| 2387 | .help = "id of -netdev to connect to", |
| 2388 | }, |
| 2389 | { |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2390 | .name = "macaddr", |
| 2391 | .type = QEMU_OPT_STRING, |
| 2392 | .help = "MAC address", |
| 2393 | }, { |
| 2394 | .name = "model", |
| 2395 | .type = QEMU_OPT_STRING, |
| 2396 | .help = "device model (e1000, rtl8139, virtio etc.)", |
| 2397 | }, { |
| 2398 | .name = "addr", |
| 2399 | .type = QEMU_OPT_STRING, |
| 2400 | .help = "PCI device address", |
| 2401 | }, { |
| 2402 | .name = "vectors", |
| 2403 | .type = QEMU_OPT_NUMBER, |
| 2404 | .help = "number of MSI-x vectors, 0 to disable MSI-X", |
| 2405 | }, |
| 2406 | { /* end of list */ } |
| 2407 | }, |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 2408 | #ifdef CONFIG_SLIRP |
| 2409 | }, { |
| 2410 | .type = "user", |
| 2411 | .init = net_init_slirp, |
| 2412 | .desc = { |
| 2413 | NET_COMMON_PARAMS_DESC, |
| 2414 | { |
| 2415 | .name = "hostname", |
| 2416 | .type = QEMU_OPT_STRING, |
| 2417 | .help = "client hostname reported by the builtin DHCP server", |
| 2418 | }, { |
| 2419 | .name = "restrict", |
| 2420 | .type = QEMU_OPT_STRING, |
| 2421 | .help = "isolate the guest from the host (y|yes|n|no)", |
| 2422 | }, { |
| 2423 | .name = "ip", |
| 2424 | .type = QEMU_OPT_STRING, |
| 2425 | .help = "legacy parameter, use net= instead", |
| 2426 | }, { |
| 2427 | .name = "net", |
| 2428 | .type = QEMU_OPT_STRING, |
| 2429 | .help = "IP address and optional netmask", |
| 2430 | }, { |
| 2431 | .name = "host", |
| 2432 | .type = QEMU_OPT_STRING, |
| 2433 | .help = "guest-visible address of the host", |
| 2434 | }, { |
| 2435 | .name = "tftp", |
| 2436 | .type = QEMU_OPT_STRING, |
| 2437 | .help = "root directory of the built-in TFTP server", |
| 2438 | }, { |
| 2439 | .name = "bootfile", |
| 2440 | .type = QEMU_OPT_STRING, |
| 2441 | .help = "BOOTP filename, for use with tftp=", |
| 2442 | }, { |
| 2443 | .name = "dhcpstart", |
| 2444 | .type = QEMU_OPT_STRING, |
| 2445 | .help = "the first of the 16 IPs the built-in DHCP server can assign", |
| 2446 | }, { |
| 2447 | .name = "dns", |
| 2448 | .type = QEMU_OPT_STRING, |
| 2449 | .help = "guest-visible address of the virtual nameserver", |
| 2450 | }, { |
| 2451 | .name = "smb", |
| 2452 | .type = QEMU_OPT_STRING, |
| 2453 | .help = "root directory of the built-in SMB server", |
| 2454 | }, { |
| 2455 | .name = "smbserver", |
| 2456 | .type = QEMU_OPT_STRING, |
| 2457 | .help = "IP address of the built-in SMB server", |
| 2458 | }, { |
| 2459 | .name = "hostfwd", |
| 2460 | .type = QEMU_OPT_STRING, |
| 2461 | .help = "guest port number to forward incoming TCP or UDP connections", |
| 2462 | }, { |
| 2463 | .name = "guestfwd", |
| 2464 | .type = QEMU_OPT_STRING, |
| 2465 | .help = "IP address and port to forward guest TCP connections", |
| 2466 | }, |
| 2467 | { /* end of list */ } |
| 2468 | }, |
| 2469 | #endif |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 2470 | }, { |
| 2471 | .type = "tap", |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 2472 | .init = net_init_tap, |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 2473 | .desc = { |
| 2474 | NET_COMMON_PARAMS_DESC, |
| 2475 | { |
| 2476 | .name = "ifname", |
| 2477 | .type = QEMU_OPT_STRING, |
| 2478 | .help = "interface name", |
| 2479 | }, |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 2480 | #ifndef _WIN32 |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 2481 | { |
| 2482 | .name = "fd", |
| 2483 | .type = QEMU_OPT_STRING, |
| 2484 | .help = "file descriptor of an already opened tap", |
| 2485 | }, { |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 2486 | .name = "script", |
| 2487 | .type = QEMU_OPT_STRING, |
| 2488 | .help = "script to initialize the interface", |
| 2489 | }, { |
| 2490 | .name = "downscript", |
| 2491 | .type = QEMU_OPT_STRING, |
| 2492 | .help = "script to shut down the interface", |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 2493 | }, { |
| 2494 | .name = "sndbuf", |
| 2495 | .type = QEMU_OPT_SIZE, |
| 2496 | .help = "send buffer limit" |
Mark McLoughlin | baf74c9 | 2009-10-22 17:43:37 +0100 | [diff] [blame] | 2497 | }, { |
| 2498 | .name = "vnet_hdr", |
| 2499 | .type = QEMU_OPT_BOOL, |
| 2500 | .help = "enable the IFF_VNET_HDR flag on the tap interface" |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 2501 | }, |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 2502 | #endif /* _WIN32 */ |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 2503 | { /* end of list */ } |
| 2504 | }, |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 2505 | }, { |
| 2506 | .type = "socket", |
| 2507 | .init = net_init_socket, |
| 2508 | .desc = { |
| 2509 | NET_COMMON_PARAMS_DESC, |
| 2510 | { |
| 2511 | .name = "fd", |
| 2512 | .type = QEMU_OPT_STRING, |
| 2513 | .help = "file descriptor of an already opened socket", |
| 2514 | }, { |
| 2515 | .name = "listen", |
| 2516 | .type = QEMU_OPT_STRING, |
| 2517 | .help = "port number, and optional hostname, to listen on", |
| 2518 | }, { |
| 2519 | .name = "connect", |
| 2520 | .type = QEMU_OPT_STRING, |
| 2521 | .help = "port number, and optional hostname, to connect to", |
| 2522 | }, { |
| 2523 | .name = "mcast", |
| 2524 | .type = QEMU_OPT_STRING, |
| 2525 | .help = "UDP multicast address and port number", |
| 2526 | }, |
| 2527 | { /* end of list */ } |
| 2528 | }, |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 2529 | #ifdef CONFIG_VDE |
| 2530 | }, { |
| 2531 | .type = "vde", |
| 2532 | .init = net_init_vde, |
| 2533 | .desc = { |
| 2534 | NET_COMMON_PARAMS_DESC, |
| 2535 | { |
| 2536 | .name = "sock", |
| 2537 | .type = QEMU_OPT_STRING, |
| 2538 | .help = "socket path", |
| 2539 | }, { |
| 2540 | .name = "port", |
| 2541 | .type = QEMU_OPT_NUMBER, |
| 2542 | .help = "port number", |
| 2543 | }, { |
| 2544 | .name = "group", |
| 2545 | .type = QEMU_OPT_STRING, |
| 2546 | .help = "group owner of socket", |
| 2547 | }, { |
| 2548 | .name = "mode", |
| 2549 | .type = QEMU_OPT_NUMBER, |
| 2550 | .help = "permissions for socket", |
| 2551 | }, |
| 2552 | { /* end of list */ } |
| 2553 | }, |
| 2554 | #endif |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 2555 | }, { |
| 2556 | .type = "dump", |
| 2557 | .init = net_init_dump, |
| 2558 | .desc = { |
| 2559 | NET_COMMON_PARAMS_DESC, |
| 2560 | { |
| 2561 | .name = "len", |
| 2562 | .type = QEMU_OPT_SIZE, |
| 2563 | .help = "per-packet size limit (64k default)", |
| 2564 | }, { |
| 2565 | .name = "file", |
| 2566 | .type = QEMU_OPT_STRING, |
| 2567 | .help = "dump file path (default is qemu-vlan0.pcap)", |
| 2568 | }, |
| 2569 | { /* end of list */ } |
| 2570 | }, |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2571 | }, |
| 2572 | { /* end of list */ } |
| 2573 | }; |
| 2574 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2575 | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2576 | { |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 2577 | const char *name; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2578 | const char *type; |
| 2579 | int i; |
| 2580 | |
| 2581 | type = qemu_opt_get(opts, "type"); |
| 2582 | if (!type) { |
| 2583 | qemu_error("No type specified for -net\n"); |
| 2584 | return -1; |
| 2585 | } |
| 2586 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2587 | if (is_netdev) { |
| 2588 | if (strcmp(type, "tap") != 0 && |
| 2589 | #ifdef CONFIG_SLIRP |
| 2590 | strcmp(type, "user") != 0 && |
| 2591 | #endif |
| 2592 | #ifdef CONFIG_VDE |
| 2593 | strcmp(type, "vde") != 0 && |
| 2594 | #endif |
| 2595 | strcmp(type, "socket") != 0) { |
| 2596 | qemu_error("The '%s' network backend type is not valid with -netdev\n", |
| 2597 | type); |
| 2598 | return -1; |
| 2599 | } |
| 2600 | |
| 2601 | if (qemu_opt_get(opts, "vlan")) { |
| 2602 | qemu_error("The 'vlan' parameter is not valid with -netdev\n"); |
| 2603 | return -1; |
| 2604 | } |
| 2605 | if (qemu_opt_get(opts, "name")) { |
| 2606 | qemu_error("The 'name' parameter is not valid with -netdev\n"); |
| 2607 | return -1; |
| 2608 | } |
| 2609 | if (!qemu_opts_id(opts)) { |
| 2610 | qemu_error("The id= parameter is required with -netdev\n"); |
| 2611 | return -1; |
| 2612 | } |
| 2613 | } |
| 2614 | |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 2615 | name = qemu_opts_id(opts); |
| 2616 | if (!name) { |
| 2617 | name = qemu_opt_get(opts, "name"); |
| 2618 | } |
| 2619 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2620 | for (i = 0; net_client_types[i].type != NULL; i++) { |
| 2621 | if (!strcmp(net_client_types[i].type, type)) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2622 | VLANState *vlan = NULL; |
| 2623 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2624 | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
| 2625 | return -1; |
| 2626 | } |
| 2627 | |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 2628 | /* Do not add to a vlan if it's a -netdev or a nic with a |
| 2629 | * netdev= parameter. */ |
| 2630 | if (!(is_netdev || |
| 2631 | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2632 | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
| 2633 | } |
| 2634 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2635 | if (net_client_types[i].init) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2636 | return net_client_types[i].init(opts, mon, name, vlan); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 2637 | } else { |
| 2638 | return 0; |
| 2639 | } |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | qemu_error("Invalid -net type '%s'\n", type); |
| 2644 | return -1; |
| 2645 | } |
| 2646 | |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 2647 | void net_client_uninit(NICInfo *nd) |
| 2648 | { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 2649 | if (nd->vlan) { |
| 2650 | nd->vlan->nb_guest_devs--; |
| 2651 | } |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 2652 | nb_nics--; |
Glauber Costa | a979670 | 2009-09-17 16:53:39 -0400 | [diff] [blame] | 2653 | |
Mark McLoughlin | 9203f52 | 2009-10-06 12:16:53 +0100 | [diff] [blame] | 2654 | qemu_free(nd->model); |
| 2655 | qemu_free(nd->name); |
| 2656 | qemu_free(nd->devaddr); |
Glauber Costa | a979670 | 2009-09-17 16:53:39 -0400 | [diff] [blame] | 2657 | |
Mark McLoughlin | d2cffe3 | 2009-10-06 12:16:54 +0100 | [diff] [blame] | 2658 | nd->used = 0; |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2661 | static int net_host_check_device(const char *device) |
| 2662 | { |
| 2663 | int i; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 2664 | const char *valid_param_list[] = { "tap", "socket", "dump" |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2665 | #ifdef CONFIG_SLIRP |
| 2666 | ,"user" |
| 2667 | #endif |
| 2668 | #ifdef CONFIG_VDE |
| 2669 | ,"vde" |
| 2670 | #endif |
| 2671 | }; |
| 2672 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
| 2673 | if (!strncmp(valid_param_list[i], device, |
| 2674 | strlen(valid_param_list[i]))) |
| 2675 | return 1; |
| 2676 | } |
| 2677 | |
| 2678 | return 0; |
| 2679 | } |
| 2680 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2681 | void net_host_device_add(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2682 | { |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2683 | const char *device = qdict_get_str(qdict, "device"); |
Mark McLoughlin | 7f1c9d2 | 2009-10-06 12:17:13 +0100 | [diff] [blame] | 2684 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
| 2685 | QemuOpts *opts; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2686 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2687 | if (!net_host_check_device(device)) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2688 | monitor_printf(mon, "invalid host network device %s\n", device); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2689 | return; |
| 2690 | } |
Mark McLoughlin | 7f1c9d2 | 2009-10-06 12:17:13 +0100 | [diff] [blame] | 2691 | |
| 2692 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); |
| 2693 | if (!opts) { |
| 2694 | monitor_printf(mon, "parsing network options '%s' failed\n", |
| 2695 | opts_str ? opts_str : ""); |
| 2696 | return; |
| 2697 | } |
| 2698 | |
| 2699 | qemu_opt_set(opts, "type", device); |
| 2700 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2701 | if (net_client_init(mon, opts, 0) < 0) { |
aliguori | 5c8be67 | 2009-04-21 19:56:32 +0000 | [diff] [blame] | 2702 | monitor_printf(mon, "adding host network device %s failed\n", device); |
| 2703 | } |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2706 | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2707 | { |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2708 | VLANClientState *vc; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2709 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
| 2710 | const char *device = qdict_get_str(qdict, "device"); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2711 | |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 2712 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2713 | if (!vc) { |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2714 | return; |
| 2715 | } |
aliguori | e8f1f9d | 2009-04-21 19:56:08 +0000 | [diff] [blame] | 2716 | if (!net_host_check_device(vc->model)) { |
| 2717 | monitor_printf(mon, "invalid host network device %s\n", device); |
| 2718 | return; |
| 2719 | } |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 2720 | qemu_del_vlan_client(vc); |
| 2721 | } |
| 2722 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 2723 | void net_set_boot_mask(int net_boot_mask) |
| 2724 | { |
| 2725 | int i; |
| 2726 | |
| 2727 | /* Only the first four NICs may be bootable */ |
| 2728 | net_boot_mask = net_boot_mask & 0xF; |
| 2729 | |
| 2730 | for (i = 0; i < nb_nics; i++) { |
| 2731 | if (net_boot_mask & (1 << i)) { |
| 2732 | nd_table[i].bootable = 1; |
| 2733 | net_boot_mask &= ~(1 << i); |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | if (net_boot_mask) { |
| 2738 | fprintf(stderr, "Cannot boot from non-existent NIC\n"); |
| 2739 | exit(1); |
| 2740 | } |
| 2741 | } |
| 2742 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2743 | void do_info_network(Monitor *mon) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2744 | { |
| 2745 | VLANState *vlan; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2746 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2747 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 2748 | VLANClientState *vc; |
| 2749 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2750 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2751 | |
| 2752 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2753 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2754 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2758 | void do_set_link(Monitor *mon, const QDict *qdict) |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 2759 | { |
| 2760 | VLANState *vlan; |
| 2761 | VLANClientState *vc = NULL; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2762 | const char *name = qdict_get_str(qdict, "name"); |
| 2763 | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 2764 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2765 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 2766 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 2767 | if (strcmp(vc->name, name) == 0) { |
edgar_igl | dd5de37 | 2009-01-09 00:48:28 +0000 | [diff] [blame] | 2768 | goto done; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | } |
edgar_igl | dd5de37 | 2009-01-09 00:48:28 +0000 | [diff] [blame] | 2772 | done: |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 2773 | |
| 2774 | if (!vc) { |
Stefan Weil | 7dc3fa0 | 2009-08-08 23:33:26 +0200 | [diff] [blame] | 2775 | monitor_printf(mon, "could not find network device '%s'\n", name); |
Luiz Capitulino | c3cf0d3 | 2009-08-03 13:56:59 -0300 | [diff] [blame] | 2776 | return; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | if (strcmp(up_or_down, "up") == 0) |
| 2780 | vc->link_down = 0; |
| 2781 | else if (strcmp(up_or_down, "down") == 0) |
| 2782 | vc->link_down = 1; |
| 2783 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2784 | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " |
| 2785 | "valid\n", up_or_down); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 2786 | |
aliguori | 34b25ca | 2009-01-08 19:45:03 +0000 | [diff] [blame] | 2787 | if (vc->link_status_changed) |
| 2788 | vc->link_status_changed(vc); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2791 | void net_cleanup(void) |
| 2792 | { |
| 2793 | VLANState *vlan; |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 2794 | VLANClientState *vc, *next_vc; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2795 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2796 | QTAILQ_FOREACH(vlan, &vlans, next) { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2797 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 2798 | qemu_del_vlan_client(vc); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2799 | } |
| 2800 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 2801 | |
| 2802 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
| 2803 | qemu_del_vlan_client(vc); |
| 2804 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2807 | static void net_check_clients(void) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2808 | { |
| 2809 | VLANState *vlan; |
| 2810 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2811 | QTAILQ_FOREACH(vlan, &vlans, next) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 2812 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
| 2813 | continue; |
| 2814 | if (vlan->nb_guest_devs == 0) |
| 2815 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); |
| 2816 | if (vlan->nb_host_devs == 0) |
| 2817 | fprintf(stderr, |
| 2818 | "Warning: vlan %d is not connected to host network\n", |
| 2819 | vlan->id); |
| 2820 | } |
| 2821 | } |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2822 | |
| 2823 | static int net_init_client(QemuOpts *opts, void *dummy) |
| 2824 | { |
Mark McLoughlin | c1671a0 | 2009-10-12 09:52:00 +0100 | [diff] [blame] | 2825 | if (net_client_init(NULL, opts, 0) < 0) |
| 2826 | return -1; |
| 2827 | return 0; |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | static int net_init_netdev(QemuOpts *opts, void *dummy) |
| 2831 | { |
| 2832 | return net_client_init(NULL, opts, 1); |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | int net_init_clients(void) |
| 2836 | { |
| 2837 | if (QTAILQ_EMPTY(&qemu_net_opts.head)) { |
| 2838 | /* if no clients, we use a default config */ |
| 2839 | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); |
| 2840 | #ifdef CONFIG_SLIRP |
| 2841 | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); |
| 2842 | #endif |
| 2843 | } |
| 2844 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2845 | QTAILQ_INIT(&vlans); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 2846 | QTAILQ_INIT(&non_vlan_clients); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 2847 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2848 | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
| 2849 | return -1; |
| 2850 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2851 | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
| 2852 | return -1; |
| 2853 | } |
| 2854 | |
| 2855 | net_check_clients(); |
| 2856 | |
| 2857 | return 0; |
| 2858 | } |
| 2859 | |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 2860 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2861 | { |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 2862 | #if defined(CONFIG_SLIRP) |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2863 | /* handle legacy -net channel,port:chr */ |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 2864 | if (!strcmp(opts_list->name, "net") && |
| 2865 | !strncmp(optarg, "channel,", strlen("channel,"))) { |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2866 | int ret; |
| 2867 | |
| 2868 | optarg += strlen("channel,"); |
| 2869 | |
| 2870 | if (QTAILQ_EMPTY(&slirp_stacks)) { |
| 2871 | struct slirp_config_str *config; |
| 2872 | |
| 2873 | config = qemu_malloc(sizeof(*config)); |
| 2874 | pstrcpy(config->str, sizeof(config->str), optarg); |
| 2875 | config->flags = SLIRP_CFG_LEGACY; |
| 2876 | config->next = slirp_configs; |
| 2877 | slirp_configs = config; |
| 2878 | ret = 0; |
| 2879 | } else { |
| 2880 | ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1); |
| 2881 | } |
| 2882 | |
| 2883 | return ret; |
| 2884 | } |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 2885 | #endif |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 2886 | if (!qemu_opts_parse(opts_list, optarg, "type")) { |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 2887 | return -1; |
| 2888 | } |
| 2889 | |
| 2890 | return 0; |
| 2891 | } |