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