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