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