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