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