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