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 | */ |
Mark McLoughlin | 1df49e0 | 2009-11-25 18:48:58 +0000 | [diff] [blame] | 24 | #include "net.h" |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 25 | |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 26 | #include "config-host.h" |
| 27 | |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 28 | #include "net/tap.h" |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 29 | #include "net/socket.h" |
Mark McLoughlin | 1abecf7 | 2009-11-25 18:48:57 +0000 | [diff] [blame] | 30 | #include "net/dump.h" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 31 | #include "net/slirp.h" |
Mark McLoughlin | 5c361cc | 2009-11-25 18:48:55 +0000 | [diff] [blame] | 32 | #include "net/vde.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 33 | #include "monitor.h" |
| 34 | #include "sysemu.h" |
Mark McLoughlin | 1df49e0 | 2009-11-25 18:48:58 +0000 | [diff] [blame] | 35 | #include "qemu-common.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 36 | #include "qemu_socket.h" |
| 37 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 38 | static QTAILQ_HEAD(, VLANState) vlans; |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 39 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 40 | |
| 41 | /***********************************************************/ |
| 42 | /* network device redirectors */ |
| 43 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 44 | #if defined(DEBUG_NET) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 45 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
| 46 | { |
| 47 | int len, i, j, c; |
| 48 | |
| 49 | for(i=0;i<size;i+=16) { |
| 50 | len = size - i; |
| 51 | if (len > 16) |
| 52 | len = 16; |
| 53 | fprintf(f, "%08x ", i); |
| 54 | for(j=0;j<16;j++) { |
| 55 | if (j < len) |
| 56 | fprintf(f, " %02x", buf[i+j]); |
| 57 | else |
| 58 | fprintf(f, " "); |
| 59 | } |
| 60 | fprintf(f, " "); |
| 61 | for(j=0;j<len;j++) { |
| 62 | c = buf[i+j]; |
| 63 | if (c < ' ' || c > '~') |
| 64 | c = '.'; |
| 65 | fprintf(f, "%c", c); |
| 66 | } |
| 67 | fprintf(f, "\n"); |
| 68 | } |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
| 73 | { |
| 74 | int i; |
| 75 | char *last_char; |
| 76 | long int offset; |
| 77 | |
| 78 | errno = 0; |
| 79 | offset = strtol(p, &last_char, 0); |
| 80 | if (0 == errno && '\0' == *last_char && |
| 81 | offset >= 0 && offset <= 0xFFFFFF) { |
| 82 | macaddr[3] = (offset & 0xFF0000) >> 16; |
| 83 | macaddr[4] = (offset & 0xFF00) >> 8; |
| 84 | macaddr[5] = offset & 0xFF; |
| 85 | return 0; |
| 86 | } else { |
| 87 | for(i = 0; i < 6; i++) { |
| 88 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 89 | if (i == 5) { |
| 90 | if (*p != '\0') |
| 91 | return -1; |
| 92 | } else { |
| 93 | if (*p != ':' && *p != '-') |
| 94 | return -1; |
| 95 | p++; |
| 96 | } |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 105 | { |
| 106 | const char *p, *p1; |
| 107 | int len; |
| 108 | p = *pp; |
| 109 | p1 = strchr(p, sep); |
| 110 | if (!p1) |
| 111 | return -1; |
| 112 | len = p1 - p; |
| 113 | p1++; |
| 114 | if (buf_size > 0) { |
| 115 | if (len > buf_size - 1) |
| 116 | len = buf_size - 1; |
| 117 | memcpy(buf, p, len); |
| 118 | buf[len] = '\0'; |
| 119 | } |
| 120 | *pp = p1; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 125 | struct sockaddr_in *saddr, |
| 126 | const char *input_str) |
| 127 | { |
| 128 | char *str = strdup(input_str); |
| 129 | char *host_str = str; |
| 130 | char *src_str; |
| 131 | const char *src_str2; |
| 132 | char *ptr; |
| 133 | |
| 134 | /* |
| 135 | * Chop off any extra arguments at the end of the string which |
| 136 | * would start with a comma, then fill in the src port information |
| 137 | * if it was provided else use the "any address" and "any port". |
| 138 | */ |
| 139 | if ((ptr = strchr(str,','))) |
| 140 | *ptr = '\0'; |
| 141 | |
| 142 | if ((src_str = strchr(input_str,'@'))) { |
| 143 | *src_str = '\0'; |
| 144 | src_str++; |
| 145 | } |
| 146 | |
| 147 | if (parse_host_port(haddr, host_str) < 0) |
| 148 | goto fail; |
| 149 | |
| 150 | src_str2 = src_str; |
| 151 | if (!src_str || *src_str == '\0') |
| 152 | src_str2 = ":0"; |
| 153 | |
| 154 | if (parse_host_port(saddr, src_str2) < 0) |
| 155 | goto fail; |
| 156 | |
| 157 | free(str); |
| 158 | return(0); |
| 159 | |
| 160 | fail: |
| 161 | free(str); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 166 | { |
| 167 | char buf[512]; |
| 168 | struct hostent *he; |
| 169 | const char *p, *r; |
| 170 | int port; |
| 171 | |
| 172 | p = str; |
| 173 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 174 | return -1; |
| 175 | saddr->sin_family = AF_INET; |
| 176 | if (buf[0] == '\0') { |
| 177 | saddr->sin_addr.s_addr = 0; |
| 178 | } else { |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 179 | if (qemu_isdigit(buf[0])) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 180 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 181 | return -1; |
| 182 | } else { |
| 183 | if ((he = gethostbyname(buf)) == NULL) |
| 184 | return - 1; |
| 185 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
| 186 | } |
| 187 | } |
| 188 | port = strtol(p, (char **)&r, 0); |
| 189 | if (r == p) |
| 190 | return -1; |
| 191 | saddr->sin_port = htons(port); |
| 192 | return 0; |
| 193 | } |
| 194 | |
aliguori | 7cb7434 | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 195 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
| 196 | { |
| 197 | snprintf(vc->info_str, sizeof(vc->info_str), |
aliguori | 4dda406 | 2009-01-08 19:01:37 +0000 | [diff] [blame] | 198 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
| 199 | vc->model, |
aliguori | 7cb7434 | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 200 | macaddr[0], macaddr[1], macaddr[2], |
| 201 | macaddr[3], macaddr[4], macaddr[5]); |
| 202 | } |
| 203 | |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 204 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
| 205 | { |
| 206 | static int index = 0; |
| 207 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
| 208 | |
| 209 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
| 210 | return; |
| 211 | macaddr->a[0] = 0x52; |
| 212 | macaddr->a[1] = 0x54; |
| 213 | macaddr->a[2] = 0x00; |
| 214 | macaddr->a[3] = 0x12; |
| 215 | macaddr->a[4] = 0x34; |
| 216 | macaddr->a[5] = 0x56 + index++; |
| 217 | } |
| 218 | |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 219 | static char *assign_name(VLANClientState *vc1, const char *model) |
| 220 | { |
| 221 | VLANState *vlan; |
| 222 | char buf[256]; |
| 223 | int id = 0; |
| 224 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 225 | QTAILQ_FOREACH(vlan, &vlans, next) { |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 226 | VLANClientState *vc; |
| 227 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 228 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 229 | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 230 | id++; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 231 | } |
| 232 | } |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
| 236 | |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 237 | return qemu_strdup(buf); |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 240 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 241 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 242 | const uint8_t *data, |
| 243 | size_t size, |
| 244 | void *opaque); |
| 245 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 246 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 247 | const struct iovec *iov, |
| 248 | int iovcnt, |
| 249 | void *opaque); |
| 250 | |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 251 | VLANClientState *qemu_new_net_client(NetClientInfo *info, |
| 252 | VLANState *vlan, |
| 253 | VLANClientState *peer, |
| 254 | const char *model, |
| 255 | const char *name) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 256 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 257 | VLANClientState *vc; |
| 258 | |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 259 | assert(info->size >= sizeof(VLANClientState)); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 260 | |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 261 | vc = qemu_mallocz(info->size); |
| 262 | |
| 263 | vc->type = info->type; |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 264 | vc->model = qemu_strdup(model); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 265 | if (name) { |
Mark McLoughlin | 02374aa | 2009-10-06 12:16:55 +0100 | [diff] [blame] | 266 | vc->name = qemu_strdup(name); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 267 | } else { |
aliguori | 7a9f6e4 | 2009-01-07 17:48:51 +0000 | [diff] [blame] | 268 | vc->name = assign_name(vc, model); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 269 | } |
| 270 | vc->can_receive = info->can_receive; |
| 271 | vc->receive = info->receive; |
| 272 | vc->receive_raw = info->receive_raw; |
| 273 | vc->receive_iov = info->receive_iov; |
| 274 | vc->cleanup = info->cleanup; |
| 275 | vc->link_status_changed = info->link_status_changed; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 276 | |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 277 | if (vlan) { |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 278 | assert(!peer); |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 279 | vc->vlan = vlan; |
| 280 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 281 | } else { |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 282 | if (peer) { |
| 283 | vc->peer = peer; |
| 284 | peer->peer = vc; |
| 285 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 286 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 287 | |
| 288 | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
| 289 | qemu_deliver_packet_iov, |
| 290 | vc); |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 291 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 292 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 293 | return vc; |
| 294 | } |
| 295 | |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame^] | 296 | NICState *qemu_new_nic(NetClientInfo *info, |
| 297 | NICConf *conf, |
| 298 | const char *model, |
| 299 | const char *name, |
| 300 | void *opaque) |
| 301 | { |
| 302 | VLANClientState *nc; |
| 303 | NICState *nic; |
| 304 | |
| 305 | assert(info->type == NET_CLIENT_TYPE_NIC); |
| 306 | assert(info->size >= sizeof(NICState)); |
| 307 | |
| 308 | nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); |
| 309 | |
| 310 | nic = DO_UPCAST(NICState, nc, nc); |
| 311 | nic->conf = conf; |
| 312 | nic->opaque = opaque; |
| 313 | |
| 314 | return nic; |
| 315 | } |
| 316 | |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 317 | VLANClientState *qemu_new_vlan_client(net_client_type type, |
| 318 | VLANState *vlan, |
| 319 | VLANClientState *peer, |
| 320 | const char *model, |
| 321 | const char *name, |
| 322 | NetCanReceive *can_receive, |
| 323 | NetReceive *receive, |
| 324 | NetReceive *receive_raw, |
| 325 | NetReceiveIOV *receive_iov, |
| 326 | NetCleanup *cleanup, |
| 327 | void *opaque) |
| 328 | { |
| 329 | VLANClientState *ret; |
| 330 | NetClientInfo info; |
| 331 | |
| 332 | info.type = type; |
| 333 | info.size = sizeof(VLANClientState); |
| 334 | info.can_receive = can_receive; |
| 335 | info.receive = receive; |
| 336 | info.receive_raw = receive_raw; |
| 337 | info.receive_iov = receive_iov; |
| 338 | info.cleanup = cleanup; |
| 339 | info.link_status_changed = NULL; |
| 340 | |
| 341 | ret = qemu_new_net_client(&info, vlan, peer, model, name); |
| 342 | |
| 343 | ret->opaque = opaque; |
| 344 | |
| 345 | return ret; |
| 346 | } |
| 347 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 348 | void qemu_del_vlan_client(VLANClientState *vc) |
| 349 | { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 350 | if (vc->vlan) { |
| 351 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 352 | } else { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 353 | if (vc->send_queue) { |
| 354 | qemu_del_net_queue(vc->send_queue); |
| 355 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 356 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
Mark McLoughlin | 283c7c6 | 2009-10-08 19:58:30 +0100 | [diff] [blame] | 357 | if (vc->peer) { |
| 358 | vc->peer->peer = NULL; |
| 359 | } |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 360 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 361 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 362 | if (vc->cleanup) { |
| 363 | vc->cleanup(vc); |
| 364 | } |
| 365 | |
| 366 | qemu_free(vc->name); |
| 367 | qemu_free(vc->model); |
| 368 | qemu_free(vc); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 369 | } |
| 370 | |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 371 | VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque) |
| 372 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 373 | VLANClientState *vc; |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 374 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 375 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 376 | if (vc->opaque == opaque) { |
| 377 | return vc; |
| 378 | } |
| 379 | } |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 380 | |
| 381 | return NULL; |
| 382 | } |
| 383 | |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 384 | VLANClientState * |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 385 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, |
| 386 | const char *client_str) |
| 387 | { |
| 388 | VLANState *vlan; |
| 389 | VLANClientState *vc; |
| 390 | |
| 391 | vlan = qemu_find_vlan(vlan_id, 0); |
| 392 | if (!vlan) { |
| 393 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 397 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 398 | if (!strcmp(vc->name, client_str)) { |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | if (!vc) { |
| 403 | monitor_printf(mon, "can't find device %s on VLAN %d\n", |
| 404 | client_str, vlan_id); |
| 405 | } |
| 406 | |
| 407 | return vc; |
| 408 | } |
| 409 | |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 410 | int qemu_can_send_packet(VLANClientState *sender) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 411 | { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 412 | VLANState *vlan = sender->vlan; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 413 | VLANClientState *vc; |
| 414 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 415 | if (sender->peer) { |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 416 | if (sender->peer->receive_disabled) { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 417 | return 0; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 418 | } else if (sender->peer->can_receive && |
| 419 | !sender->peer->can_receive(sender->peer)) { |
| 420 | return 0; |
| 421 | } else { |
| 422 | return 1; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 426 | if (!sender->vlan) { |
| 427 | return 1; |
| 428 | } |
| 429 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 430 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 431 | if (vc == sender) { |
| 432 | continue; |
| 433 | } |
| 434 | |
Mark McLoughlin | cda9046 | 2009-05-18 13:13:16 +0100 | [diff] [blame] | 435 | /* no can_receive() handler, they can always receive */ |
Mark McLoughlin | e3f5ec2 | 2009-05-18 13:33:03 +0100 | [diff] [blame] | 436 | if (!vc->can_receive || vc->can_receive(vc)) { |
Mark McLoughlin | 2e1e064 | 2009-04-29 09:36:43 +0100 | [diff] [blame] | 437 | return 1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | return 0; |
| 441 | } |
| 442 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 443 | static ssize_t qemu_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 444 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 445 | const uint8_t *data, |
| 446 | size_t size, |
| 447 | void *opaque) |
| 448 | { |
| 449 | VLANClientState *vc = opaque; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 450 | ssize_t ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 451 | |
| 452 | if (vc->link_down) { |
| 453 | return size; |
| 454 | } |
| 455 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 456 | if (vc->receive_disabled) { |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { |
| 461 | ret = vc->receive_raw(vc, data, size); |
| 462 | } else { |
| 463 | ret = vc->receive(vc, data, size); |
| 464 | } |
| 465 | |
| 466 | if (ret == 0) { |
| 467 | vc->receive_disabled = 1; |
| 468 | }; |
| 469 | |
| 470 | return ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 473 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 474 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 475 | const uint8_t *buf, |
| 476 | size_t size, |
| 477 | void *opaque) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 478 | { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 479 | VLANState *vlan = opaque; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 480 | VLANClientState *vc; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 481 | ssize_t ret = -1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 482 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 483 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 484 | ssize_t len; |
| 485 | |
| 486 | if (vc == sender) { |
| 487 | continue; |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 488 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 489 | |
| 490 | if (vc->link_down) { |
| 491 | ret = size; |
| 492 | continue; |
| 493 | } |
| 494 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 495 | if (vc->receive_disabled) { |
| 496 | ret = 0; |
| 497 | continue; |
| 498 | } |
| 499 | |
| 500 | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) { |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 501 | len = vc->receive_raw(vc, buf, size); |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 502 | } else { |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 503 | len = vc->receive(vc, buf, size); |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (len == 0) { |
| 507 | vc->receive_disabled = 1; |
| 508 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 509 | |
| 510 | ret = (ret >= 0) ? ret : len; |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 511 | |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 512 | } |
Mark McLoughlin | 3e021d4 | 2009-04-29 11:34:52 +0100 | [diff] [blame] | 513 | |
| 514 | return ret; |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 517 | void qemu_purge_queued_packets(VLANClientState *vc) |
| 518 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 519 | NetQueue *queue; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 520 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 521 | if (!vc->peer && !vc->vlan) { |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | if (vc->peer) { |
| 526 | queue = vc->peer->send_queue; |
| 527 | } else { |
| 528 | queue = vc->vlan->send_queue; |
| 529 | } |
| 530 | |
| 531 | qemu_net_queue_purge(queue, vc); |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 534 | void qemu_flush_queued_packets(VLANClientState *vc) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 535 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 536 | NetQueue *queue; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 537 | |
Mark McLoughlin | 893379e | 2009-10-27 18:16:36 +0000 | [diff] [blame] | 538 | vc->receive_disabled = 0; |
| 539 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 540 | if (vc->vlan) { |
| 541 | queue = vc->vlan->send_queue; |
| 542 | } else { |
| 543 | queue = vc->send_queue; |
| 544 | } |
| 545 | |
| 546 | qemu_net_queue_flush(queue); |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 549 | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender, |
| 550 | unsigned flags, |
| 551 | const uint8_t *buf, int size, |
| 552 | NetPacketSent *sent_cb) |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 553 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 554 | NetQueue *queue; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 555 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 556 | #ifdef DEBUG_NET |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 557 | printf("qemu_send_packet_async:\n"); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 558 | hex_dump(stdout, buf, size); |
| 559 | #endif |
| 560 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 561 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
| 562 | return size; |
| 563 | } |
| 564 | |
| 565 | if (sender->peer) { |
| 566 | queue = sender->peer->send_queue; |
| 567 | } else { |
| 568 | queue = sender->vlan->send_queue; |
| 569 | } |
| 570 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 571 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
| 572 | } |
| 573 | |
| 574 | ssize_t qemu_send_packet_async(VLANClientState *sender, |
| 575 | const uint8_t *buf, int size, |
| 576 | NetPacketSent *sent_cb) |
| 577 | { |
| 578 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, |
| 579 | buf, size, sent_cb); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
| 583 | { |
| 584 | qemu_send_packet_async(vc, buf, size, NULL); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 587 | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
| 588 | { |
| 589 | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW, |
| 590 | buf, size, NULL); |
| 591 | } |
| 592 | |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 593 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
| 594 | int iovcnt) |
| 595 | { |
| 596 | uint8_t buffer[4096]; |
| 597 | size_t offset = 0; |
| 598 | int i; |
| 599 | |
| 600 | for (i = 0; i < iovcnt; i++) { |
| 601 | size_t len; |
| 602 | |
| 603 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); |
| 604 | memcpy(buffer + offset, iov[i].iov_base, len); |
| 605 | offset += len; |
| 606 | } |
| 607 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 608 | return vc->receive(vc, buffer, offset); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 609 | } |
| 610 | |
aliguori | e0e7877 | 2009-01-26 15:37:44 +0000 | [diff] [blame] | 611 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
| 612 | { |
| 613 | size_t offset = 0; |
| 614 | int i; |
| 615 | |
| 616 | for (i = 0; i < iovcnt; i++) |
| 617 | offset += iov[i].iov_len; |
| 618 | return offset; |
| 619 | } |
| 620 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 621 | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 622 | unsigned flags, |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 623 | const struct iovec *iov, |
| 624 | int iovcnt, |
| 625 | void *opaque) |
| 626 | { |
| 627 | VLANClientState *vc = opaque; |
| 628 | |
| 629 | if (vc->link_down) { |
| 630 | return calc_iov_length(iov, iovcnt); |
| 631 | } |
| 632 | |
| 633 | if (vc->receive_iov) { |
| 634 | return vc->receive_iov(vc, iov, iovcnt); |
| 635 | } else { |
| 636 | return vc_sendv_compat(vc, iov, iovcnt); |
| 637 | } |
| 638 | } |
| 639 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 640 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 641 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 642 | const struct iovec *iov, |
| 643 | int iovcnt, |
| 644 | void *opaque) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 645 | { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 646 | VLANState *vlan = opaque; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 647 | VLANClientState *vc; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 648 | ssize_t ret = -1; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 649 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 650 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 651 | ssize_t len; |
| 652 | |
| 653 | if (vc == sender) { |
| 654 | continue; |
| 655 | } |
| 656 | |
| 657 | if (vc->link_down) { |
| 658 | ret = calc_iov_length(iov, iovcnt); |
| 659 | continue; |
| 660 | } |
| 661 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 662 | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
| 663 | |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 664 | if (vc->receive_iov) { |
| 665 | len = vc->receive_iov(vc, iov, iovcnt); |
| 666 | } else { |
| 667 | len = vc_sendv_compat(vc, iov, iovcnt); |
| 668 | } |
| 669 | |
| 670 | ret = (ret >= 0) ? ret : len; |
| 671 | } |
| 672 | |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 673 | return ret; |
| 674 | } |
| 675 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 676 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
| 677 | const struct iovec *iov, int iovcnt, |
| 678 | NetPacketSent *sent_cb) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 679 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 680 | NetQueue *queue; |
| 681 | |
| 682 | if (sender->link_down || (!sender->peer && !sender->vlan)) { |
aliguori | e0e7877 | 2009-01-26 15:37:44 +0000 | [diff] [blame] | 683 | return calc_iov_length(iov, iovcnt); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 686 | if (sender->peer) { |
| 687 | queue = sender->peer->send_queue; |
| 688 | } else { |
| 689 | queue = sender->vlan->send_queue; |
| 690 | } |
| 691 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 692 | return qemu_net_queue_send_iov(queue, sender, |
| 693 | QEMU_NET_PACKET_FLAG_NONE, |
| 694 | iov, iovcnt, sent_cb); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 697 | ssize_t |
| 698 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
| 699 | { |
| 700 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
| 701 | } |
| 702 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 703 | /* find or alloc a new VLAN */ |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 704 | VLANState *qemu_find_vlan(int id, int allocate) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 705 | { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 706 | VLANState *vlan; |
| 707 | |
| 708 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 709 | if (vlan->id == id) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 710 | return vlan; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 711 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 712 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 713 | |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 714 | if (!allocate) { |
| 715 | return NULL; |
| 716 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 717 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 718 | vlan = qemu_mallocz(sizeof(VLANState)); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 719 | vlan->id = id; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 720 | QTAILQ_INIT(&vlan->clients); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 721 | |
| 722 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
| 723 | qemu_vlan_deliver_packet_iov, |
| 724 | vlan); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 725 | |
| 726 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
| 727 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 728 | return vlan; |
| 729 | } |
| 730 | |
Gerd Hoffmann | 2ef924b | 2009-10-21 15:25:24 +0200 | [diff] [blame] | 731 | VLANClientState *qemu_find_netdev(const char *id) |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 732 | { |
| 733 | VLANClientState *vc; |
| 734 | |
| 735 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
| 736 | if (!strcmp(vc->name, id)) { |
| 737 | return vc; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | return NULL; |
| 742 | } |
| 743 | |
aliguori | 7697079 | 2009-02-11 15:20:03 +0000 | [diff] [blame] | 744 | static int nic_get_free_idx(void) |
| 745 | { |
| 746 | int index; |
| 747 | |
| 748 | for (index = 0; index < MAX_NICS; index++) |
| 749 | if (!nd_table[index].used) |
| 750 | return index; |
| 751 | return -1; |
| 752 | } |
| 753 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 754 | int qemu_show_nic_models(const char *arg, const char *const *models) |
| 755 | { |
| 756 | int i; |
| 757 | |
| 758 | if (!arg || strcmp(arg, "?")) |
| 759 | return 0; |
| 760 | |
| 761 | fprintf(stderr, "qemu: Supported NIC models: "); |
| 762 | for (i = 0 ; models[i]; i++) |
| 763 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
| 764 | return 1; |
| 765 | } |
| 766 | |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 767 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
| 768 | { |
| 769 | const char *models[2]; |
| 770 | |
| 771 | models[0] = model; |
| 772 | models[1] = NULL; |
| 773 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 774 | if (qemu_show_nic_models(nd->model, models)) |
| 775 | exit(0); |
| 776 | if (qemu_find_nic_model(nd, models, model) < 0) |
| 777 | exit(1); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 780 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
| 781 | const char *default_model) |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 782 | { |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 783 | int i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 784 | |
| 785 | if (!nd->model) |
Mark McLoughlin | 32a8e14 | 2009-10-06 12:16:51 +0100 | [diff] [blame] | 786 | nd->model = qemu_strdup(default_model); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 787 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 788 | for (i = 0 ; models[i]; i++) { |
| 789 | if (strcmp(nd->model, models[i]) == 0) |
| 790 | return i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 793 | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model); |
| 794 | return -1; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 797 | int net_handle_fd_param(Monitor *mon, const char *param) |
Mark McLoughlin | c1d6eed | 2009-07-22 09:11:42 +0100 | [diff] [blame] | 798 | { |
| 799 | if (!qemu_isdigit(param[0])) { |
| 800 | int fd; |
| 801 | |
| 802 | fd = monitor_get_fd(mon, param); |
| 803 | if (fd == -1) { |
Markus Armbruster | fb12577 | 2009-10-06 12:16:58 +0100 | [diff] [blame] | 804 | qemu_error("No file descriptor named %s found", param); |
Mark McLoughlin | c1d6eed | 2009-07-22 09:11:42 +0100 | [diff] [blame] | 805 | return -1; |
| 806 | } |
| 807 | |
| 808 | return fd; |
| 809 | } else { |
| 810 | return strtol(param, NULL, 0); |
| 811 | } |
| 812 | } |
| 813 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 814 | static int net_init_nic(QemuOpts *opts, |
| 815 | Monitor *mon, |
| 816 | const char *name, |
| 817 | VLANState *vlan) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 818 | { |
| 819 | int idx; |
| 820 | NICInfo *nd; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 821 | const char *netdev; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 822 | |
| 823 | idx = nic_get_free_idx(); |
| 824 | if (idx == -1 || nb_nics >= MAX_NICS) { |
| 825 | qemu_error("Too Many NICs\n"); |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | nd = &nd_table[idx]; |
| 830 | |
| 831 | memset(nd, 0, sizeof(*nd)); |
| 832 | |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 833 | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
| 834 | nd->netdev = qemu_find_netdev(netdev); |
| 835 | if (!nd->netdev) { |
| 836 | qemu_error("netdev '%s' not found\n", netdev); |
| 837 | return -1; |
| 838 | } |
| 839 | } else { |
| 840 | assert(vlan); |
| 841 | nd->vlan = vlan; |
| 842 | } |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 843 | if (name) { |
| 844 | nd->name = qemu_strdup(name); |
| 845 | } |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 846 | if (qemu_opt_get(opts, "model")) { |
| 847 | nd->model = qemu_strdup(qemu_opt_get(opts, "model")); |
| 848 | } |
| 849 | if (qemu_opt_get(opts, "addr")) { |
| 850 | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr")); |
| 851 | } |
| 852 | |
| 853 | nd->macaddr[0] = 0x52; |
| 854 | nd->macaddr[1] = 0x54; |
| 855 | nd->macaddr[2] = 0x00; |
| 856 | nd->macaddr[3] = 0x12; |
| 857 | nd->macaddr[4] = 0x34; |
| 858 | nd->macaddr[5] = 0x56 + idx; |
| 859 | |
| 860 | if (qemu_opt_get(opts, "macaddr") && |
| 861 | parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
| 862 | qemu_error("invalid syntax for ethernet address\n"); |
| 863 | return -1; |
| 864 | } |
| 865 | |
| 866 | nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED); |
| 867 | if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED && |
| 868 | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
| 869 | qemu_error("invalid # of vectors: %d\n", nd->nvectors); |
| 870 | return -1; |
| 871 | } |
| 872 | |
| 873 | nd->used = 1; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 874 | if (vlan) { |
| 875 | nd->vlan->nb_guest_devs++; |
| 876 | } |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 877 | nb_nics++; |
| 878 | |
| 879 | return idx; |
| 880 | } |
| 881 | |
| 882 | #define NET_COMMON_PARAMS_DESC \ |
| 883 | { \ |
| 884 | .name = "type", \ |
| 885 | .type = QEMU_OPT_STRING, \ |
| 886 | .help = "net client type (nic, tap etc.)", \ |
| 887 | }, { \ |
| 888 | .name = "vlan", \ |
| 889 | .type = QEMU_OPT_NUMBER, \ |
| 890 | .help = "vlan number", \ |
| 891 | }, { \ |
| 892 | .name = "name", \ |
| 893 | .type = QEMU_OPT_STRING, \ |
| 894 | .help = "identifier for monitor commands", \ |
| 895 | } |
| 896 | |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 897 | typedef int (*net_client_init_func)(QemuOpts *opts, |
| 898 | Monitor *mon, |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 899 | const char *name, |
| 900 | VLANState *vlan); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 901 | |
| 902 | /* magic number, but compiler will warn if too small */ |
| 903 | #define NET_MAX_DESC 20 |
| 904 | |
| 905 | static struct { |
| 906 | const char *type; |
| 907 | net_client_init_func init; |
| 908 | QemuOptDesc desc[NET_MAX_DESC]; |
| 909 | } net_client_types[] = { |
| 910 | { |
| 911 | .type = "none", |
| 912 | .desc = { |
| 913 | NET_COMMON_PARAMS_DESC, |
| 914 | { /* end of list */ } |
| 915 | }, |
| 916 | }, { |
| 917 | .type = "nic", |
| 918 | .init = net_init_nic, |
| 919 | .desc = { |
| 920 | NET_COMMON_PARAMS_DESC, |
| 921 | { |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 922 | .name = "netdev", |
| 923 | .type = QEMU_OPT_STRING, |
| 924 | .help = "id of -netdev to connect to", |
| 925 | }, |
| 926 | { |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 927 | .name = "macaddr", |
| 928 | .type = QEMU_OPT_STRING, |
| 929 | .help = "MAC address", |
| 930 | }, { |
| 931 | .name = "model", |
| 932 | .type = QEMU_OPT_STRING, |
| 933 | .help = "device model (e1000, rtl8139, virtio etc.)", |
| 934 | }, { |
| 935 | .name = "addr", |
| 936 | .type = QEMU_OPT_STRING, |
| 937 | .help = "PCI device address", |
| 938 | }, { |
| 939 | .name = "vectors", |
| 940 | .type = QEMU_OPT_NUMBER, |
| 941 | .help = "number of MSI-x vectors, 0 to disable MSI-X", |
| 942 | }, |
| 943 | { /* end of list */ } |
| 944 | }, |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 945 | #ifdef CONFIG_SLIRP |
| 946 | }, { |
| 947 | .type = "user", |
| 948 | .init = net_init_slirp, |
| 949 | .desc = { |
| 950 | NET_COMMON_PARAMS_DESC, |
| 951 | { |
| 952 | .name = "hostname", |
| 953 | .type = QEMU_OPT_STRING, |
| 954 | .help = "client hostname reported by the builtin DHCP server", |
| 955 | }, { |
| 956 | .name = "restrict", |
| 957 | .type = QEMU_OPT_STRING, |
| 958 | .help = "isolate the guest from the host (y|yes|n|no)", |
| 959 | }, { |
| 960 | .name = "ip", |
| 961 | .type = QEMU_OPT_STRING, |
| 962 | .help = "legacy parameter, use net= instead", |
| 963 | }, { |
| 964 | .name = "net", |
| 965 | .type = QEMU_OPT_STRING, |
| 966 | .help = "IP address and optional netmask", |
| 967 | }, { |
| 968 | .name = "host", |
| 969 | .type = QEMU_OPT_STRING, |
| 970 | .help = "guest-visible address of the host", |
| 971 | }, { |
| 972 | .name = "tftp", |
| 973 | .type = QEMU_OPT_STRING, |
| 974 | .help = "root directory of the built-in TFTP server", |
| 975 | }, { |
| 976 | .name = "bootfile", |
| 977 | .type = QEMU_OPT_STRING, |
| 978 | .help = "BOOTP filename, for use with tftp=", |
| 979 | }, { |
| 980 | .name = "dhcpstart", |
| 981 | .type = QEMU_OPT_STRING, |
| 982 | .help = "the first of the 16 IPs the built-in DHCP server can assign", |
| 983 | }, { |
| 984 | .name = "dns", |
| 985 | .type = QEMU_OPT_STRING, |
| 986 | .help = "guest-visible address of the virtual nameserver", |
| 987 | }, { |
| 988 | .name = "smb", |
| 989 | .type = QEMU_OPT_STRING, |
| 990 | .help = "root directory of the built-in SMB server", |
| 991 | }, { |
| 992 | .name = "smbserver", |
| 993 | .type = QEMU_OPT_STRING, |
| 994 | .help = "IP address of the built-in SMB server", |
| 995 | }, { |
| 996 | .name = "hostfwd", |
| 997 | .type = QEMU_OPT_STRING, |
| 998 | .help = "guest port number to forward incoming TCP or UDP connections", |
| 999 | }, { |
| 1000 | .name = "guestfwd", |
| 1001 | .type = QEMU_OPT_STRING, |
| 1002 | .help = "IP address and port to forward guest TCP connections", |
| 1003 | }, |
| 1004 | { /* end of list */ } |
| 1005 | }, |
| 1006 | #endif |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1007 | }, { |
| 1008 | .type = "tap", |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 1009 | .init = net_init_tap, |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1010 | .desc = { |
| 1011 | NET_COMMON_PARAMS_DESC, |
| 1012 | { |
| 1013 | .name = "ifname", |
| 1014 | .type = QEMU_OPT_STRING, |
| 1015 | .help = "interface name", |
| 1016 | }, |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 1017 | #ifndef _WIN32 |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1018 | { |
| 1019 | .name = "fd", |
| 1020 | .type = QEMU_OPT_STRING, |
| 1021 | .help = "file descriptor of an already opened tap", |
| 1022 | }, { |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1023 | .name = "script", |
| 1024 | .type = QEMU_OPT_STRING, |
| 1025 | .help = "script to initialize the interface", |
| 1026 | }, { |
| 1027 | .name = "downscript", |
| 1028 | .type = QEMU_OPT_STRING, |
| 1029 | .help = "script to shut down the interface", |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1030 | }, { |
| 1031 | .name = "sndbuf", |
| 1032 | .type = QEMU_OPT_SIZE, |
| 1033 | .help = "send buffer limit" |
Mark McLoughlin | baf74c9 | 2009-10-22 17:43:37 +0100 | [diff] [blame] | 1034 | }, { |
| 1035 | .name = "vnet_hdr", |
| 1036 | .type = QEMU_OPT_BOOL, |
| 1037 | .help = "enable the IFF_VNET_HDR flag on the tap interface" |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1038 | }, |
Mark McLoughlin | a8ed73f | 2009-10-22 17:49:05 +0100 | [diff] [blame] | 1039 | #endif /* _WIN32 */ |
Mark McLoughlin | 8a1c523 | 2009-10-06 12:17:08 +0100 | [diff] [blame] | 1040 | { /* end of list */ } |
| 1041 | }, |
Mark McLoughlin | 88ce16c | 2009-10-06 12:17:09 +0100 | [diff] [blame] | 1042 | }, { |
| 1043 | .type = "socket", |
| 1044 | .init = net_init_socket, |
| 1045 | .desc = { |
| 1046 | NET_COMMON_PARAMS_DESC, |
| 1047 | { |
| 1048 | .name = "fd", |
| 1049 | .type = QEMU_OPT_STRING, |
| 1050 | .help = "file descriptor of an already opened socket", |
| 1051 | }, { |
| 1052 | .name = "listen", |
| 1053 | .type = QEMU_OPT_STRING, |
| 1054 | .help = "port number, and optional hostname, to listen on", |
| 1055 | }, { |
| 1056 | .name = "connect", |
| 1057 | .type = QEMU_OPT_STRING, |
| 1058 | .help = "port number, and optional hostname, to connect to", |
| 1059 | }, { |
| 1060 | .name = "mcast", |
| 1061 | .type = QEMU_OPT_STRING, |
| 1062 | .help = "UDP multicast address and port number", |
| 1063 | }, |
| 1064 | { /* end of list */ } |
| 1065 | }, |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 1066 | #ifdef CONFIG_VDE |
| 1067 | }, { |
| 1068 | .type = "vde", |
| 1069 | .init = net_init_vde, |
| 1070 | .desc = { |
| 1071 | NET_COMMON_PARAMS_DESC, |
| 1072 | { |
| 1073 | .name = "sock", |
| 1074 | .type = QEMU_OPT_STRING, |
| 1075 | .help = "socket path", |
| 1076 | }, { |
| 1077 | .name = "port", |
| 1078 | .type = QEMU_OPT_NUMBER, |
| 1079 | .help = "port number", |
| 1080 | }, { |
| 1081 | .name = "group", |
| 1082 | .type = QEMU_OPT_STRING, |
| 1083 | .help = "group owner of socket", |
| 1084 | }, { |
| 1085 | .name = "mode", |
| 1086 | .type = QEMU_OPT_NUMBER, |
| 1087 | .help = "permissions for socket", |
| 1088 | }, |
| 1089 | { /* end of list */ } |
| 1090 | }, |
| 1091 | #endif |
Mark McLoughlin | ed2955c | 2009-10-06 12:17:11 +0100 | [diff] [blame] | 1092 | }, { |
| 1093 | .type = "dump", |
| 1094 | .init = net_init_dump, |
| 1095 | .desc = { |
| 1096 | NET_COMMON_PARAMS_DESC, |
| 1097 | { |
| 1098 | .name = "len", |
| 1099 | .type = QEMU_OPT_SIZE, |
| 1100 | .help = "per-packet size limit (64k default)", |
| 1101 | }, { |
| 1102 | .name = "file", |
| 1103 | .type = QEMU_OPT_STRING, |
| 1104 | .help = "dump file path (default is qemu-vlan0.pcap)", |
| 1105 | }, |
| 1106 | { /* end of list */ } |
| 1107 | }, |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1108 | }, |
| 1109 | { /* end of list */ } |
| 1110 | }; |
| 1111 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1112 | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1113 | { |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 1114 | const char *name; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1115 | const char *type; |
| 1116 | int i; |
| 1117 | |
| 1118 | type = qemu_opt_get(opts, "type"); |
| 1119 | if (!type) { |
| 1120 | qemu_error("No type specified for -net\n"); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1124 | if (is_netdev) { |
| 1125 | if (strcmp(type, "tap") != 0 && |
| 1126 | #ifdef CONFIG_SLIRP |
| 1127 | strcmp(type, "user") != 0 && |
| 1128 | #endif |
| 1129 | #ifdef CONFIG_VDE |
| 1130 | strcmp(type, "vde") != 0 && |
| 1131 | #endif |
| 1132 | strcmp(type, "socket") != 0) { |
| 1133 | qemu_error("The '%s' network backend type is not valid with -netdev\n", |
| 1134 | type); |
| 1135 | return -1; |
| 1136 | } |
| 1137 | |
| 1138 | if (qemu_opt_get(opts, "vlan")) { |
| 1139 | qemu_error("The 'vlan' parameter is not valid with -netdev\n"); |
| 1140 | return -1; |
| 1141 | } |
| 1142 | if (qemu_opt_get(opts, "name")) { |
| 1143 | qemu_error("The 'name' parameter is not valid with -netdev\n"); |
| 1144 | return -1; |
| 1145 | } |
| 1146 | if (!qemu_opts_id(opts)) { |
| 1147 | qemu_error("The id= parameter is required with -netdev\n"); |
| 1148 | return -1; |
| 1149 | } |
| 1150 | } |
| 1151 | |
Mark McLoughlin | 6d952eb | 2009-10-08 19:58:21 +0100 | [diff] [blame] | 1152 | name = qemu_opts_id(opts); |
| 1153 | if (!name) { |
| 1154 | name = qemu_opt_get(opts, "name"); |
| 1155 | } |
| 1156 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1157 | for (i = 0; net_client_types[i].type != NULL; i++) { |
| 1158 | if (!strcmp(net_client_types[i].type, type)) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1159 | VLANState *vlan = NULL; |
| 1160 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1161 | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
| 1162 | return -1; |
| 1163 | } |
| 1164 | |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 1165 | /* Do not add to a vlan if it's a -netdev or a nic with a |
| 1166 | * netdev= parameter. */ |
| 1167 | if (!(is_netdev || |
| 1168 | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1169 | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
| 1170 | } |
| 1171 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1172 | if (net_client_types[i].init) { |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1173 | return net_client_types[i].init(opts, mon, name, vlan); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1174 | } else { |
| 1175 | return 0; |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | qemu_error("Invalid -net type '%s'\n", type); |
| 1181 | return -1; |
| 1182 | } |
| 1183 | |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 1184 | void net_client_uninit(NICInfo *nd) |
| 1185 | { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 1186 | if (nd->vlan) { |
| 1187 | nd->vlan->nb_guest_devs--; |
| 1188 | } |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 1189 | nb_nics--; |
Glauber Costa | a979670 | 2009-09-17 16:53:39 -0400 | [diff] [blame] | 1190 | |
Mark McLoughlin | 9203f52 | 2009-10-06 12:16:53 +0100 | [diff] [blame] | 1191 | qemu_free(nd->model); |
| 1192 | qemu_free(nd->name); |
| 1193 | qemu_free(nd->devaddr); |
Glauber Costa | a979670 | 2009-09-17 16:53:39 -0400 | [diff] [blame] | 1194 | |
Mark McLoughlin | d2cffe3 | 2009-10-06 12:16:54 +0100 | [diff] [blame] | 1195 | nd->used = 0; |
aliguori | 8b13c4a | 2009-02-11 15:20:51 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1198 | static int net_host_check_device(const char *device) |
| 1199 | { |
| 1200 | int i; |
aliguori | bb9ea79 | 2009-04-21 19:56:28 +0000 | [diff] [blame] | 1201 | const char *valid_param_list[] = { "tap", "socket", "dump" |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1202 | #ifdef CONFIG_SLIRP |
| 1203 | ,"user" |
| 1204 | #endif |
| 1205 | #ifdef CONFIG_VDE |
| 1206 | ,"vde" |
| 1207 | #endif |
| 1208 | }; |
| 1209 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
| 1210 | if (!strncmp(valid_param_list[i], device, |
| 1211 | strlen(valid_param_list[i]))) |
| 1212 | return 1; |
| 1213 | } |
| 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1218 | void net_host_device_add(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1219 | { |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1220 | const char *device = qdict_get_str(qdict, "device"); |
Mark McLoughlin | 7f1c9d2 | 2009-10-06 12:17:13 +0100 | [diff] [blame] | 1221 | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
| 1222 | QemuOpts *opts; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1223 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1224 | if (!net_host_check_device(device)) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1225 | monitor_printf(mon, "invalid host network device %s\n", device); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1226 | return; |
| 1227 | } |
Mark McLoughlin | 7f1c9d2 | 2009-10-06 12:17:13 +0100 | [diff] [blame] | 1228 | |
| 1229 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); |
| 1230 | if (!opts) { |
| 1231 | monitor_printf(mon, "parsing network options '%s' failed\n", |
| 1232 | opts_str ? opts_str : ""); |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | qemu_opt_set(opts, "type", device); |
| 1237 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1238 | if (net_client_init(mon, opts, 0) < 0) { |
aliguori | 5c8be67 | 2009-04-21 19:56:32 +0000 | [diff] [blame] | 1239 | monitor_printf(mon, "adding host network device %s failed\n", device); |
| 1240 | } |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1243 | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1244 | { |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1245 | VLANClientState *vc; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1246 | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
| 1247 | const char *device = qdict_get_str(qdict, "device"); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1248 | |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1249 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1250 | if (!vc) { |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1251 | return; |
| 1252 | } |
aliguori | e8f1f9d | 2009-04-21 19:56:08 +0000 | [diff] [blame] | 1253 | if (!net_host_check_device(vc->model)) { |
| 1254 | monitor_printf(mon, "invalid host network device %s\n", device); |
| 1255 | return; |
| 1256 | } |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 1257 | qemu_del_vlan_client(vc); |
| 1258 | } |
| 1259 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 1260 | void net_set_boot_mask(int net_boot_mask) |
| 1261 | { |
| 1262 | int i; |
| 1263 | |
| 1264 | /* Only the first four NICs may be bootable */ |
| 1265 | net_boot_mask = net_boot_mask & 0xF; |
| 1266 | |
| 1267 | for (i = 0; i < nb_nics; i++) { |
| 1268 | if (net_boot_mask & (1 << i)) { |
| 1269 | nd_table[i].bootable = 1; |
| 1270 | net_boot_mask &= ~(1 << i); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | if (net_boot_mask) { |
| 1275 | fprintf(stderr, "Cannot boot from non-existent NIC\n"); |
| 1276 | exit(1); |
| 1277 | } |
| 1278 | } |
| 1279 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1280 | void do_info_network(Monitor *mon) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1281 | { |
| 1282 | VLANState *vlan; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1283 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1284 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 1285 | VLANClientState *vc; |
| 1286 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1287 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1288 | |
| 1289 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1290 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1291 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1295 | void do_set_link(Monitor *mon, const QDict *qdict) |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1296 | { |
| 1297 | VLANState *vlan; |
| 1298 | VLANClientState *vc = NULL; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1299 | const char *name = qdict_get_str(qdict, "name"); |
| 1300 | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1301 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1302 | QTAILQ_FOREACH(vlan, &vlans, next) { |
| 1303 | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
| 1304 | if (strcmp(vc->name, name) == 0) { |
edgar_igl | dd5de37 | 2009-01-09 00:48:28 +0000 | [diff] [blame] | 1305 | goto done; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | } |
edgar_igl | dd5de37 | 2009-01-09 00:48:28 +0000 | [diff] [blame] | 1309 | done: |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1310 | |
| 1311 | if (!vc) { |
Stefan Weil | 7dc3fa0 | 2009-08-08 23:33:26 +0200 | [diff] [blame] | 1312 | monitor_printf(mon, "could not find network device '%s'\n", name); |
Luiz Capitulino | c3cf0d3 | 2009-08-03 13:56:59 -0300 | [diff] [blame] | 1313 | return; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | if (strcmp(up_or_down, "up") == 0) |
| 1317 | vc->link_down = 0; |
| 1318 | else if (strcmp(up_or_down, "down") == 0) |
| 1319 | vc->link_down = 1; |
| 1320 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1321 | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " |
| 1322 | "valid\n", up_or_down); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1323 | |
aliguori | 34b25ca | 2009-01-08 19:45:03 +0000 | [diff] [blame] | 1324 | if (vc->link_status_changed) |
| 1325 | vc->link_status_changed(vc); |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1328 | void net_cleanup(void) |
| 1329 | { |
| 1330 | VLANState *vlan; |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 1331 | VLANClientState *vc, *next_vc; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1332 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1333 | QTAILQ_FOREACH(vlan, &vlans, next) { |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1334 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 1335 | qemu_del_vlan_client(vc); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 1338 | |
| 1339 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
| 1340 | qemu_del_vlan_client(vc); |
| 1341 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1344 | static void net_check_clients(void) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1345 | { |
| 1346 | VLANState *vlan; |
| 1347 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1348 | QTAILQ_FOREACH(vlan, &vlans, next) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1349 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
| 1350 | continue; |
| 1351 | if (vlan->nb_guest_devs == 0) |
| 1352 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); |
| 1353 | if (vlan->nb_host_devs == 0) |
| 1354 | fprintf(stderr, |
| 1355 | "Warning: vlan %d is not connected to host network\n", |
| 1356 | vlan->id); |
| 1357 | } |
| 1358 | } |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1359 | |
| 1360 | static int net_init_client(QemuOpts *opts, void *dummy) |
| 1361 | { |
Mark McLoughlin | c1671a0 | 2009-10-12 09:52:00 +0100 | [diff] [blame] | 1362 | if (net_client_init(NULL, opts, 0) < 0) |
| 1363 | return -1; |
| 1364 | return 0; |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | static int net_init_netdev(QemuOpts *opts, void *dummy) |
| 1368 | { |
| 1369 | return net_client_init(NULL, opts, 1); |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | int net_init_clients(void) |
| 1373 | { |
| 1374 | if (QTAILQ_EMPTY(&qemu_net_opts.head)) { |
| 1375 | /* if no clients, we use a default config */ |
| 1376 | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); |
| 1377 | #ifdef CONFIG_SLIRP |
| 1378 | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); |
| 1379 | #endif |
| 1380 | } |
| 1381 | |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1382 | QTAILQ_INIT(&vlans); |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 1383 | QTAILQ_INIT(&non_vlan_clients); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1384 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1385 | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
| 1386 | return -1; |
| 1387 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1388 | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
| 1389 | return -1; |
| 1390 | } |
| 1391 | |
| 1392 | net_check_clients(); |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 1397 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1398 | { |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 1399 | #if defined(CONFIG_SLIRP) |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1400 | int ret; |
| 1401 | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1402 | return ret; |
| 1403 | } |
Juan Quintela | a3a766e | 2009-10-07 23:44:15 +0200 | [diff] [blame] | 1404 | #endif |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 1405 | |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 1406 | if (!qemu_opts_parse(opts_list, optarg, "type")) { |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1407 | return -1; |
| 1408 | } |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |