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