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