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