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 | */ |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 2744d92 | 2016-01-29 17:50:00 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 26 | |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 27 | #include "net/net.h" |
Paolo Bonzini | fd9400b | 2012-10-24 11:27:28 +0200 | [diff] [blame] | 28 | #include "clients.h" |
| 29 | #include "hub.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 30 | #include "net/slirp.h" |
Dmitry Krivenok | d60b20c | 2013-10-21 12:08:44 +0400 | [diff] [blame] | 31 | #include "net/eth.h" |
Paolo Bonzini | fd9400b | 2012-10-24 11:27:28 +0200 | [diff] [blame] | 32 | #include "util.h" |
Paolo Bonzini | a245fc1 | 2012-09-17 18:43:51 +0200 | [diff] [blame] | 33 | |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 34 | #include "monitor/monitor.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 35 | #include "qemu/help_option.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 36 | #include "qapi/qapi-commands-net.h" |
| 37 | #include "qapi/qapi-visit-net.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 38 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 39 | #include "qapi/qmp/qerror.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 40 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 41 | #include "qemu/sockets.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 42 | #include "qemu/cutils.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 43 | #include "qemu/config-file.h" |
Amit Shah | 75422b0 | 2010-02-25 17:24:43 +0530 | [diff] [blame] | 44 | #include "hw/qdev.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 45 | #include "qemu/iov.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 46 | #include "qemu/main-loop.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 47 | #include "qemu/option.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 48 | #include "qapi/error.h" |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 49 | #include "qapi/opts-visitor.h" |
zhanghailiang | e1d64c0 | 2014-08-26 16:06:17 +0800 | [diff] [blame] | 50 | #include "sysemu/sysemu.h" |
Thomas Huth | 559964a | 2017-05-15 15:32:56 +0200 | [diff] [blame] | 51 | #include "sysemu/qtest.h" |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 52 | #include "net/filter.h" |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 53 | #include "qapi/string-output-visitor.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 54 | |
Stefan Weil | 2944e4e | 2012-02-04 09:24:46 +0100 | [diff] [blame] | 55 | /* Net bridge is currently not supported for W32. */ |
| 56 | #if !defined(_WIN32) |
| 57 | # define CONFIG_NET_BRIDGE |
| 58 | #endif |
| 59 | |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 60 | static VMChangeStateEntry *net_change_state_entry; |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 61 | static QTAILQ_HEAD(, NetClientState) net_clients; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 62 | |
| 63 | /***********************************************************/ |
| 64 | /* network device redirectors */ |
| 65 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 66 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 67 | { |
| 68 | const char *p, *p1; |
| 69 | int len; |
| 70 | p = *pp; |
| 71 | p1 = strchr(p, sep); |
| 72 | if (!p1) |
| 73 | return -1; |
| 74 | len = p1 - p; |
| 75 | p1++; |
| 76 | if (buf_size > 0) { |
| 77 | if (len > buf_size - 1) |
| 78 | len = buf_size - 1; |
| 79 | memcpy(buf, p, len); |
| 80 | buf[len] = '\0'; |
| 81 | } |
| 82 | *pp = p1; |
| 83 | return 0; |
| 84 | } |
| 85 | |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 86 | int parse_host_port(struct sockaddr_in *saddr, const char *str, |
| 87 | Error **errp) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 88 | { |
| 89 | char buf[512]; |
| 90 | struct hostent *he; |
| 91 | const char *p, *r; |
| 92 | int port; |
| 93 | |
| 94 | p = str; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 95 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
| 96 | error_setg(errp, "host address '%s' doesn't contain ':' " |
| 97 | "separating host from port", str); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 98 | return -1; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 99 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 100 | saddr->sin_family = AF_INET; |
| 101 | if (buf[0] == '\0') { |
| 102 | saddr->sin_addr.s_addr = 0; |
| 103 | } else { |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 104 | if (qemu_isdigit(buf[0])) { |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 105 | if (!inet_aton(buf, &saddr->sin_addr)) { |
| 106 | error_setg(errp, "host address '%s' is not a valid " |
| 107 | "IPv4 address", buf); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 108 | return -1; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 109 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 110 | } else { |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 111 | he = gethostbyname(buf); |
| 112 | if (he == NULL) { |
| 113 | error_setg(errp, "can't resolve host address '%s'", buf); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 114 | return - 1; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 115 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 116 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
| 117 | } |
| 118 | } |
| 119 | port = strtol(p, (char **)&r, 0); |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 120 | if (r == p) { |
| 121 | error_setg(errp, "port number '%s' is invalid", p); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 122 | return -1; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 123 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 124 | saddr->sin_port = htons(port); |
| 125 | return 0; |
| 126 | } |
| 127 | |
Scott Feldman | 890ee6a | 2015-03-13 21:09:25 -0700 | [diff] [blame] | 128 | char *qemu_mac_strdup_printf(const uint8_t *macaddr) |
| 129 | { |
| 130 | return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", |
| 131 | macaddr[0], macaddr[1], macaddr[2], |
| 132 | macaddr[3], macaddr[4], macaddr[5]); |
| 133 | } |
| 134 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 135 | void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) |
aliguori | 7cb7434b | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 136 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 137 | snprintf(nc->info_str, sizeof(nc->info_str), |
aliguori | 4dda406 | 2009-01-08 19:01:37 +0000 | [diff] [blame] | 138 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 139 | nc->model, |
aliguori | 7cb7434b | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 140 | macaddr[0], macaddr[1], macaddr[2], |
| 141 | macaddr[3], macaddr[4], macaddr[5]); |
| 142 | } |
| 143 | |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 144 | static int mac_table[256] = {0}; |
| 145 | |
| 146 | static void qemu_macaddr_set_used(MACAddr *macaddr) |
| 147 | { |
| 148 | int index; |
| 149 | |
| 150 | for (index = 0x56; index < 0xFF; index++) { |
| 151 | if (macaddr->a[5] == index) { |
| 152 | mac_table[index]++; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void qemu_macaddr_set_free(MACAddr *macaddr) |
| 158 | { |
| 159 | int index; |
| 160 | static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; |
| 161 | |
| 162 | if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { |
| 163 | return; |
| 164 | } |
| 165 | for (index = 0x56; index < 0xFF; index++) { |
| 166 | if (macaddr->a[5] == index) { |
| 167 | mac_table[index]--; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static int qemu_macaddr_get_free(void) |
| 173 | { |
| 174 | int index; |
| 175 | |
| 176 | for (index = 0x56; index < 0xFF; index++) { |
| 177 | if (mac_table[index] == 0) { |
| 178 | return index; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return -1; |
| 183 | } |
| 184 | |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 185 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
| 186 | { |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 187 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 188 | static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 189 | |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 190 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) { |
| 191 | if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { |
| 192 | return; |
| 193 | } else { |
| 194 | qemu_macaddr_set_used(macaddr); |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 199 | macaddr->a[0] = 0x52; |
| 200 | macaddr->a[1] = 0x54; |
| 201 | macaddr->a[2] = 0x00; |
| 202 | macaddr->a[3] = 0x12; |
| 203 | macaddr->a[4] = 0x34; |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 204 | macaddr->a[5] = qemu_macaddr_get_free(); |
| 205 | qemu_macaddr_set_used(macaddr); |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 208 | /** |
| 209 | * Generate a name for net client |
| 210 | * |
Amos Kong | c963530 | 2013-04-15 18:55:19 +0800 | [diff] [blame] | 211 | * 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] | 212 | */ |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 213 | static char *assign_name(NetClientState *nc1, const char *model) |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 214 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 215 | NetClientState *nc; |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 216 | int id = 0; |
| 217 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 218 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 219 | if (nc == nc1) { |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 220 | continue; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 221 | } |
Amos Kong | c963530 | 2013-04-15 18:55:19 +0800 | [diff] [blame] | 222 | if (strcmp(nc->model, model) == 0) { |
Markus Armbruster | 53e51d8 | 2011-06-16 18:45:36 +0200 | [diff] [blame] | 223 | id++; |
| 224 | } |
| 225 | } |
| 226 | |
Hani Benhabiles | 4bf2c13 | 2014-01-09 19:34:27 +0100 | [diff] [blame] | 227 | return g_strdup_printf("%s.%d", model, id); |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 230 | static void qemu_net_client_destructor(NetClientState *nc) |
| 231 | { |
| 232 | g_free(nc); |
| 233 | } |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 234 | static ssize_t qemu_deliver_packet_iov(NetClientState *sender, |
| 235 | unsigned flags, |
| 236 | const struct iovec *iov, |
| 237 | int iovcnt, |
| 238 | void *opaque); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 239 | |
Jason Wang | 18a1541 | 2013-01-30 19:12:26 +0800 | [diff] [blame] | 240 | static void qemu_net_client_setup(NetClientState *nc, |
| 241 | NetClientInfo *info, |
| 242 | NetClientState *peer, |
| 243 | const char *model, |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 244 | const char *name, |
| 245 | NetClientDestructor *destructor) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 246 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 247 | nc->info = info; |
| 248 | nc->model = g_strdup(model); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 249 | if (name) { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 250 | nc->name = g_strdup(name); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 251 | } else { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 252 | nc->name = assign_name(nc, model); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 253 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 254 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame] | 255 | if (peer) { |
| 256 | assert(!peer->peer); |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 257 | nc->peer = peer; |
| 258 | peer->peer = nc; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 259 | } |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 260 | QTAILQ_INSERT_TAIL(&net_clients, nc, next); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 261 | |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame] | 262 | nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 263 | nc->destructor = destructor; |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 264 | QTAILQ_INIT(&nc->filters); |
Jason Wang | 18a1541 | 2013-01-30 19:12:26 +0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | NetClientState *qemu_new_net_client(NetClientInfo *info, |
| 268 | NetClientState *peer, |
| 269 | const char *model, |
| 270 | const char *name) |
| 271 | { |
| 272 | NetClientState *nc; |
| 273 | |
| 274 | assert(info->size >= sizeof(NetClientState)); |
| 275 | |
| 276 | nc = g_malloc0(info->size); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 277 | qemu_net_client_setup(nc, info, peer, model, name, |
| 278 | qemu_net_client_destructor); |
Jason Wang | 18a1541 | 2013-01-30 19:12:26 +0800 | [diff] [blame] | 279 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 280 | return nc; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 283 | NICState *qemu_new_nic(NetClientInfo *info, |
| 284 | NICConf *conf, |
| 285 | const char *model, |
| 286 | const char *name, |
| 287 | void *opaque) |
| 288 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 289 | NetClientState **peers = conf->peers.ncs; |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 290 | NICState *nic; |
Jiri Pirko | 575a1c0 | 2014-05-26 12:04:08 +0200 | [diff] [blame] | 291 | int i, queues = MAX(1, conf->peers.queues); |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 292 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 293 | assert(info->type == NET_CLIENT_DRIVER_NIC); |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 294 | assert(info->size >= sizeof(NICState)); |
| 295 | |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 296 | nic = g_malloc0(info->size + sizeof(NetClientState) * queues); |
| 297 | nic->ncs = (void *)nic + info->size; |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 298 | nic->conf = conf; |
| 299 | nic->opaque = opaque; |
| 300 | |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 301 | for (i = 0; i < queues; i++) { |
| 302 | qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name, |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 303 | NULL); |
| 304 | nic->ncs[i].queue_index = i; |
| 305 | } |
| 306 | |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 307 | return nic; |
| 308 | } |
| 309 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 310 | NetClientState *qemu_get_subqueue(NICState *nic, int queue_index) |
| 311 | { |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 312 | return nic->ncs + queue_index; |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 313 | } |
| 314 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 315 | NetClientState *qemu_get_queue(NICState *nic) |
| 316 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 317 | return qemu_get_subqueue(nic, 0); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 318 | } |
| 319 | |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 320 | NICState *qemu_get_nic(NetClientState *nc) |
| 321 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 322 | NetClientState *nc0 = nc - nc->queue_index; |
| 323 | |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 324 | return (NICState *)((void *)nc0 - nc->info->size); |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void *qemu_get_nic_opaque(NetClientState *nc) |
| 328 | { |
| 329 | NICState *nic = qemu_get_nic(nc); |
| 330 | |
| 331 | return nic->opaque; |
| 332 | } |
| 333 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 334 | static void qemu_cleanup_net_client(NetClientState *nc) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 335 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 336 | QTAILQ_REMOVE(&net_clients, nc, next); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 337 | |
Andreas Färber | cc2a904 | 2013-02-12 23:16:06 +0100 | [diff] [blame] | 338 | if (nc->info->cleanup) { |
| 339 | nc->info->cleanup(nc); |
| 340 | } |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 341 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 342 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 343 | static void qemu_free_net_client(NetClientState *nc) |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 344 | { |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 345 | if (nc->incoming_queue) { |
| 346 | qemu_del_net_queue(nc->incoming_queue); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 347 | } |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 348 | if (nc->peer) { |
| 349 | nc->peer->peer = NULL; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 350 | } |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 351 | g_free(nc->name); |
| 352 | g_free(nc->model); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 353 | if (nc->destructor) { |
| 354 | nc->destructor(nc); |
| 355 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 358 | void qemu_del_net_client(NetClientState *nc) |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 359 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 360 | NetClientState *ncs[MAX_QUEUE_NUM]; |
| 361 | int queues, i; |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 362 | NetFilterState *nf, *next; |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 363 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 364 | assert(nc->info->type != NET_CLIENT_DRIVER_NIC); |
Paolo Bonzini | 7fb4391 | 2014-12-23 17:53:20 +0100 | [diff] [blame] | 365 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 366 | /* If the NetClientState belongs to a multiqueue backend, we will change all |
| 367 | * other NetClientStates also. |
| 368 | */ |
| 369 | queues = qemu_find_net_clients_except(nc->name, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 370 | NET_CLIENT_DRIVER_NIC, |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 371 | MAX_QUEUE_NUM); |
| 372 | assert(queues != 0); |
| 373 | |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 374 | QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) { |
| 375 | object_unparent(OBJECT(nf)); |
| 376 | } |
| 377 | |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 378 | /* If there is a peer NIC, delete and cleanup client, but do not free. */ |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 379 | if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 380 | NICState *nic = qemu_get_nic(nc->peer); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 381 | if (nic->peer_deleted) { |
| 382 | return; |
| 383 | } |
| 384 | nic->peer_deleted = true; |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 385 | |
| 386 | for (i = 0; i < queues; i++) { |
| 387 | ncs[i]->peer->link_down = true; |
| 388 | } |
| 389 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 390 | if (nc->peer->info->link_status_changed) { |
| 391 | nc->peer->info->link_status_changed(nc->peer); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 392 | } |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 393 | |
| 394 | for (i = 0; i < queues; i++) { |
| 395 | qemu_cleanup_net_client(ncs[i]); |
| 396 | } |
| 397 | |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 401 | for (i = 0; i < queues; i++) { |
| 402 | qemu_cleanup_net_client(ncs[i]); |
| 403 | qemu_free_net_client(ncs[i]); |
| 404 | } |
Jason Wang | 948ecf2 | 2013-01-30 19:12:24 +0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void qemu_del_nic(NICState *nic) |
| 408 | { |
Jiri Pirko | 575a1c0 | 2014-05-26 12:04:08 +0200 | [diff] [blame] | 409 | int i, queues = MAX(nic->conf->peers.queues, 1); |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 410 | |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 411 | qemu_macaddr_set_free(&nic->conf->macaddr); |
| 412 | |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 413 | /* 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] | 414 | if (nic->peer_deleted) { |
| 415 | for (i = 0; i < queues; i++) { |
| 416 | qemu_free_net_client(qemu_get_subqueue(nic, i)->peer); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 420 | for (i = queues - 1; i >= 0; i--) { |
| 421 | NetClientState *nc = qemu_get_subqueue(nic, i); |
| 422 | |
| 423 | qemu_cleanup_net_client(nc); |
| 424 | qemu_free_net_client(nc); |
| 425 | } |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 426 | |
| 427 | g_free(nic); |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 430 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
| 431 | { |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 432 | NetClientState *nc; |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 433 | |
Stefan Hajnoczi | 9487899 | 2012-07-24 16:35:12 +0100 | [diff] [blame] | 434 | QTAILQ_FOREACH(nc, &net_clients, next) { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 435 | if (nc->info->type == NET_CLIENT_DRIVER_NIC) { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 436 | if (nc->queue_index == 0) { |
| 437 | func(qemu_get_nic(nc), opaque); |
| 438 | } |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 443 | bool qemu_has_ufo(NetClientState *nc) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 444 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 445 | if (!nc || !nc->info->has_ufo) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 446 | return false; |
| 447 | } |
| 448 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 449 | return nc->info->has_ufo(nc); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 452 | bool qemu_has_vnet_hdr(NetClientState *nc) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 453 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 454 | if (!nc || !nc->info->has_vnet_hdr) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 455 | return false; |
| 456 | } |
| 457 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 458 | return nc->info->has_vnet_hdr(nc); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 461 | bool qemu_has_vnet_hdr_len(NetClientState *nc, int len) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 462 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 463 | if (!nc || !nc->info->has_vnet_hdr_len) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 467 | return nc->info->has_vnet_hdr_len(nc, len); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 470 | void qemu_using_vnet_hdr(NetClientState *nc, bool enable) |
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->using_vnet_hdr) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 476 | nc->info->using_vnet_hdr(nc, enable); |
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 | void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 480 | int ecn, int ufo) |
| 481 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 482 | if (!nc || !nc->info->set_offload) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 483 | return; |
| 484 | } |
| 485 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 486 | nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 489 | void qemu_set_vnet_hdr_len(NetClientState *nc, int len) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 490 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 491 | if (!nc || !nc->info->set_vnet_hdr_len) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
Zhang Chen | d6b732e | 2017-07-04 14:53:45 +0800 | [diff] [blame] | 495 | nc->vnet_hdr_len = len; |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 496 | nc->info->set_vnet_hdr_len(nc, len); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 499 | int qemu_set_vnet_le(NetClientState *nc, bool is_le) |
| 500 | { |
Michael S. Tsirkin | 052bd52 | 2015-10-14 12:11:27 +0300 | [diff] [blame] | 501 | #ifdef HOST_WORDS_BIGENDIAN |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 502 | if (!nc || !nc->info->set_vnet_le) { |
| 503 | return -ENOSYS; |
| 504 | } |
| 505 | |
| 506 | return nc->info->set_vnet_le(nc, is_le); |
Michael S. Tsirkin | 052bd52 | 2015-10-14 12:11:27 +0300 | [diff] [blame] | 507 | #else |
| 508 | return 0; |
| 509 | #endif |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | int qemu_set_vnet_be(NetClientState *nc, bool is_be) |
| 513 | { |
Michael S. Tsirkin | 052bd52 | 2015-10-14 12:11:27 +0300 | [diff] [blame] | 514 | #ifdef HOST_WORDS_BIGENDIAN |
| 515 | return 0; |
| 516 | #else |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 517 | if (!nc || !nc->info->set_vnet_be) { |
| 518 | return -ENOSYS; |
| 519 | } |
| 520 | |
| 521 | return nc->info->set_vnet_be(nc, is_be); |
Michael S. Tsirkin | 052bd52 | 2015-10-14 12:11:27 +0300 | [diff] [blame] | 522 | #endif |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 525 | int qemu_can_send_packet(NetClientState *sender) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 526 | { |
zhanghailiang | e1d64c0 | 2014-08-26 16:06:17 +0800 | [diff] [blame] | 527 | int vm_running = runstate_is_running(); |
| 528 | |
| 529 | if (!vm_running) { |
| 530 | return 0; |
| 531 | } |
| 532 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 533 | if (!sender->peer) { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 534 | return 1; |
| 535 | } |
| 536 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 537 | if (sender->peer->receive_disabled) { |
| 538 | return 0; |
| 539 | } else if (sender->peer->info->can_receive && |
| 540 | !sender->peer->info->can_receive(sender->peer)) { |
| 541 | return 0; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 542 | } |
Vincent Palatin | 60c07d9 | 2011-03-02 17:25:02 -0500 | [diff] [blame] | 543 | return 1; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 546 | static ssize_t filter_receive_iov(NetClientState *nc, |
| 547 | NetFilterDirection direction, |
| 548 | NetClientState *sender, |
| 549 | unsigned flags, |
| 550 | const struct iovec *iov, |
| 551 | int iovcnt, |
| 552 | NetPacketSent *sent_cb) |
| 553 | { |
| 554 | ssize_t ret = 0; |
| 555 | NetFilterState *nf = NULL; |
| 556 | |
Li Zhijian | 25aaadf | 2016-01-26 13:00:22 +0800 | [diff] [blame] | 557 | if (direction == NET_FILTER_DIRECTION_TX) { |
| 558 | QTAILQ_FOREACH(nf, &nc->filters, next) { |
| 559 | ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, |
| 560 | iovcnt, sent_cb); |
| 561 | if (ret) { |
| 562 | return ret; |
| 563 | } |
| 564 | } |
| 565 | } else { |
Paolo Bonzini | eae3eb3 | 2018-12-06 13:10:34 +0100 | [diff] [blame] | 566 | QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) { |
Li Zhijian | 25aaadf | 2016-01-26 13:00:22 +0800 | [diff] [blame] | 567 | ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, |
| 568 | iovcnt, sent_cb); |
| 569 | if (ret) { |
| 570 | return ret; |
| 571 | } |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | static ssize_t filter_receive(NetClientState *nc, |
| 579 | NetFilterDirection direction, |
| 580 | NetClientState *sender, |
| 581 | unsigned flags, |
| 582 | const uint8_t *data, |
| 583 | size_t size, |
| 584 | NetPacketSent *sent_cb) |
| 585 | { |
| 586 | struct iovec iov = { |
| 587 | .iov_base = (void *)data, |
| 588 | .iov_len = size |
| 589 | }; |
| 590 | |
| 591 | return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb); |
| 592 | } |
| 593 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 594 | void qemu_purge_queued_packets(NetClientState *nc) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 595 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 596 | if (!nc->peer) { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 600 | qemu_net_queue_purge(nc->peer->incoming_queue, nc); |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 601 | } |
| 602 | |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 603 | void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 604 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 605 | nc->receive_disabled = 0; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 606 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 607 | if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) { |
Luigi Rizzo | 199ee60 | 2013-02-05 17:53:31 +0100 | [diff] [blame] | 608 | if (net_hub_flush(nc->peer)) { |
| 609 | qemu_notify_event(); |
| 610 | } |
Luigi Rizzo | 199ee60 | 2013-02-05 17:53:31 +0100 | [diff] [blame] | 611 | } |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 612 | if (qemu_net_queue_flush(nc->incoming_queue)) { |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 613 | /* We emptied the queue successfully, signal to the IO thread to repoll |
| 614 | * the file descriptor (for tap, for example). |
| 615 | */ |
| 616 | qemu_notify_event(); |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 617 | } else if (purge) { |
| 618 | /* Unable to empty the queue, purge remaining packets */ |
| 619 | qemu_net_queue_purge(nc->incoming_queue, nc); |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 620 | } |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 621 | } |
| 622 | |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 623 | void qemu_flush_queued_packets(NetClientState *nc) |
| 624 | { |
| 625 | qemu_flush_or_purge_queued_packets(nc, false); |
| 626 | } |
| 627 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 628 | static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender, |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 629 | unsigned flags, |
| 630 | const uint8_t *buf, int size, |
| 631 | NetPacketSent *sent_cb) |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 632 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 633 | NetQueue *queue; |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 634 | int ret; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 635 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 636 | #ifdef DEBUG_NET |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 637 | printf("qemu_send_packet_async:\n"); |
Isaac Lozano | a155555 | 2016-03-25 03:42:15 -0700 | [diff] [blame] | 638 | qemu_hexdump((const char *)buf, stdout, "net", size); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 639 | #endif |
| 640 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 641 | if (sender->link_down || !sender->peer) { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 642 | return size; |
| 643 | } |
| 644 | |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 645 | /* Let filters handle the packet first */ |
| 646 | ret = filter_receive(sender, NET_FILTER_DIRECTION_TX, |
| 647 | sender, flags, buf, size, sent_cb); |
| 648 | if (ret) { |
| 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX, |
| 653 | sender, flags, buf, size, sent_cb); |
| 654 | if (ret) { |
| 655 | return ret; |
| 656 | } |
| 657 | |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 658 | queue = sender->peer->incoming_queue; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 659 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 660 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
| 661 | } |
| 662 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 663 | ssize_t qemu_send_packet_async(NetClientState *sender, |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 664 | const uint8_t *buf, int size, |
| 665 | NetPacketSent *sent_cb) |
| 666 | { |
| 667 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, |
| 668 | buf, size, sent_cb); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 671 | ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size) |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 672 | { |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 673 | return qemu_send_packet_async(nc, buf, size, NULL); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 676 | 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] | 677 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 678 | 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] | 679 | buf, size, NULL); |
| 680 | } |
| 681 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 682 | static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov, |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 683 | int iovcnt, unsigned flags) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 684 | { |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 685 | uint8_t *buf = NULL; |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 686 | uint8_t *buffer; |
Benjamin Poirier | ce05366 | 2011-02-23 19:57:21 -0500 | [diff] [blame] | 687 | size_t offset; |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 688 | ssize_t ret; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 689 | |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 690 | if (iovcnt == 1) { |
| 691 | buffer = iov[0].iov_base; |
| 692 | offset = iov[0].iov_len; |
| 693 | } else { |
Peter Lieven | 47f9f15 | 2016-06-30 11:49:40 +0200 | [diff] [blame] | 694 | offset = iov_size(iov, iovcnt); |
| 695 | if (offset > NET_BUFSIZE) { |
| 696 | return -1; |
| 697 | } |
| 698 | buf = g_malloc(offset); |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 699 | buffer = buf; |
Peter Lieven | 47f9f15 | 2016-06-30 11:49:40 +0200 | [diff] [blame] | 700 | offset = iov_to_buf(iov, iovcnt, 0, buf, offset); |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 701 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 702 | |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 703 | if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) { |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 704 | ret = nc->info->receive_raw(nc, buffer, offset); |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 705 | } else { |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 706 | ret = nc->info->receive(nc, buffer, offset); |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 707 | } |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 708 | |
| 709 | g_free(buf); |
| 710 | return ret; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 713 | static 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 | |
Jason Wang | 1592a99 | 2018-05-30 13:16:36 +0800 | [diff] [blame] | 722 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 723 | if (nc->link_down) { |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 724 | return iov_size(iov, iovcnt); |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 727 | if (nc->receive_disabled) { |
| 728 | return 0; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 729 | } |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 730 | |
Peter Lieven | ca1ee3d | 2016-06-09 11:39:27 +0200 | [diff] [blame] | 731 | if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) { |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 732 | ret = nc->info->receive_iov(nc, iov, iovcnt); |
| 733 | } else { |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 734 | ret = nc_sendv_compat(nc, iov, iovcnt, flags); |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (ret == 0) { |
| 738 | nc->receive_disabled = 1; |
| 739 | } |
| 740 | |
| 741 | return ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 744 | ssize_t qemu_sendv_packet_async(NetClientState *sender, |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 745 | const struct iovec *iov, int iovcnt, |
| 746 | NetPacketSent *sent_cb) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 747 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 748 | NetQueue *queue; |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 749 | size_t size = iov_size(iov, iovcnt); |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 750 | int ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 751 | |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 752 | if (size > NET_BUFSIZE) { |
| 753 | return size; |
| 754 | } |
| 755 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 756 | if (sender->link_down || !sender->peer) { |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 757 | return size; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 760 | /* Let filters handle the packet first */ |
| 761 | ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender, |
| 762 | QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); |
| 763 | if (ret) { |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender, |
| 768 | QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); |
| 769 | if (ret) { |
| 770 | return ret; |
| 771 | } |
| 772 | |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 773 | queue = sender->peer->incoming_queue; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 774 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 775 | return qemu_net_queue_send_iov(queue, sender, |
| 776 | QEMU_NET_PACKET_FLAG_NONE, |
| 777 | iov, iovcnt, sent_cb); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 780 | ssize_t |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 781 | qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt) |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 782 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 783 | return qemu_sendv_packet_async(nc, iov, iovcnt, NULL); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 786 | NetClientState *qemu_find_netdev(const char *id) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 787 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 788 | NetClientState *nc; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 789 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 790 | QTAILQ_FOREACH(nc, &net_clients, next) { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 791 | if (nc->info->type == NET_CLIENT_DRIVER_NIC) |
Markus Armbruster | 85dde9a | 2011-06-16 18:45:37 +0200 | [diff] [blame] | 792 | continue; |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 793 | if (!strcmp(nc->name, id)) { |
| 794 | return nc; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | return NULL; |
| 799 | } |
| 800 | |
Jason Wang | 6c51ae7 | 2013-01-30 19:12:25 +0800 | [diff] [blame] | 801 | int qemu_find_net_clients_except(const char *id, NetClientState **ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 802 | NetClientDriver type, int max) |
Jason Wang | 6c51ae7 | 2013-01-30 19:12:25 +0800 | [diff] [blame] | 803 | { |
| 804 | NetClientState *nc; |
| 805 | int ret = 0; |
| 806 | |
| 807 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 808 | if (nc->info->type == type) { |
| 809 | continue; |
| 810 | } |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 811 | if (!id || !strcmp(nc->name, id)) { |
Jason Wang | 6c51ae7 | 2013-01-30 19:12:25 +0800 | [diff] [blame] | 812 | if (ret < max) { |
| 813 | ncs[ret] = nc; |
| 814 | } |
| 815 | ret++; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | return ret; |
| 820 | } |
| 821 | |
aliguori | 7697079 | 2009-02-11 15:20:03 +0000 | [diff] [blame] | 822 | static int nic_get_free_idx(void) |
| 823 | { |
| 824 | int index; |
| 825 | |
| 826 | for (index = 0; index < MAX_NICS; index++) |
| 827 | if (!nd_table[index].used) |
| 828 | return index; |
| 829 | return -1; |
| 830 | } |
| 831 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 832 | int qemu_show_nic_models(const char *arg, const char *const *models) |
| 833 | { |
| 834 | int i; |
| 835 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 836 | if (!arg || !is_help_option(arg)) { |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 837 | return 0; |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 838 | } |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 839 | |
| 840 | fprintf(stderr, "qemu: Supported NIC models: "); |
| 841 | for (i = 0 ; models[i]; i++) |
| 842 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
| 843 | return 1; |
| 844 | } |
| 845 | |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 846 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
| 847 | { |
| 848 | const char *models[2]; |
| 849 | |
| 850 | models[0] = model; |
| 851 | models[1] = NULL; |
| 852 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 853 | if (qemu_show_nic_models(nd->model, models)) |
| 854 | exit(0); |
| 855 | if (qemu_find_nic_model(nd, models, model) < 0) |
| 856 | exit(1); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 859 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
| 860 | const char *default_model) |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 861 | { |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 862 | int i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 863 | |
| 864 | if (!nd->model) |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 865 | nd->model = g_strdup(default_model); |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 866 | |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 867 | for (i = 0 ; models[i]; i++) { |
| 868 | if (strcmp(nd->model, models[i]) == 0) |
| 869 | return i; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 872 | error_report("Unsupported NIC model: %s", nd->model); |
Markus Armbruster | 07caea3 | 2009-09-25 03:53:51 +0200 | [diff] [blame] | 873 | return -1; |
aliguori | d07f22c | 2009-01-13 19:03:57 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 876 | static int net_init_nic(const Netdev *netdev, const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 877 | NetClientState *peer, Error **errp) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 878 | { |
| 879 | int idx; |
| 880 | NICInfo *nd; |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 881 | const NetLegacyNicOptions *nic; |
| 882 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 883 | assert(netdev->type == NET_CLIENT_DRIVER_NIC); |
| 884 | nic = &netdev->u.nic; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 885 | |
| 886 | idx = nic_get_free_idx(); |
| 887 | if (idx == -1 || nb_nics >= MAX_NICS) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 888 | error_setg(errp, "too many NICs"); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 889 | return -1; |
| 890 | } |
| 891 | |
| 892 | nd = &nd_table[idx]; |
| 893 | |
| 894 | memset(nd, 0, sizeof(*nd)); |
| 895 | |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 896 | if (nic->has_netdev) { |
| 897 | nd->netdev = qemu_find_netdev(nic->netdev); |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 898 | if (!nd->netdev) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 899 | error_setg(errp, "netdev '%s' not found", nic->netdev); |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 900 | return -1; |
| 901 | } |
| 902 | } else { |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 903 | assert(peer); |
| 904 | nd->netdev = peer; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 905 | } |
Markus Armbruster | c64f50d | 2013-01-22 11:07:57 +0100 | [diff] [blame] | 906 | nd->name = g_strdup(name); |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 907 | if (nic->has_model) { |
| 908 | nd->model = g_strdup(nic->model); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 909 | } |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 910 | if (nic->has_addr) { |
| 911 | nd->devaddr = g_strdup(nic->addr); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 914 | if (nic->has_macaddr && |
| 915 | net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 916 | error_setg(errp, "invalid syntax for ethernet address"); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 917 | return -1; |
| 918 | } |
Dmitry Krivenok | d60b20c | 2013-10-21 12:08:44 +0400 | [diff] [blame] | 919 | if (nic->has_macaddr && |
| 920 | is_multicast_ether_addr(nd->macaddr.a)) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 921 | error_setg(errp, |
| 922 | "NIC cannot have multicast MAC address (odd 1st byte)"); |
Dmitry Krivenok | d60b20c | 2013-10-21 12:08:44 +0400 | [diff] [blame] | 923 | return -1; |
| 924 | } |
Jan Kiszka | 6eed185 | 2011-07-20 12:20:22 +0200 | [diff] [blame] | 925 | qemu_macaddr_default_if_unset(&nd->macaddr); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 926 | |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 927 | if (nic->has_vectors) { |
| 928 | if (nic->vectors > 0x7ffffff) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 929 | error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors); |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 930 | return -1; |
| 931 | } |
| 932 | nd->nvectors = nic->vectors; |
| 933 | } else { |
| 934 | nd->nvectors = DEV_NVECTORS_UNSPECIFIED; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | nd->used = 1; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 938 | nb_nics++; |
| 939 | |
| 940 | return idx; |
| 941 | } |
| 942 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 943 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 944 | static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])( |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 945 | const Netdev *netdev, |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 946 | const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 947 | NetClientState *peer, Error **errp) = { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 948 | [NET_CLIENT_DRIVER_NIC] = net_init_nic, |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 949 | #ifdef CONFIG_SLIRP |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 950 | [NET_CLIENT_DRIVER_USER] = net_init_slirp, |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 951 | #endif |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 952 | [NET_CLIENT_DRIVER_TAP] = net_init_tap, |
| 953 | [NET_CLIENT_DRIVER_SOCKET] = net_init_socket, |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 954 | #ifdef CONFIG_VDE |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 955 | [NET_CLIENT_DRIVER_VDE] = net_init_vde, |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 956 | #endif |
Vincenzo Maffione | 5895213 | 2013-11-06 11:44:06 +0100 | [diff] [blame] | 957 | #ifdef CONFIG_NETMAP |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 958 | [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap, |
Vincenzo Maffione | 5895213 | 2013-11-06 11:44:06 +0100 | [diff] [blame] | 959 | #endif |
Stefan Weil | 2944e4e | 2012-02-04 09:24:46 +0100 | [diff] [blame] | 960 | #ifdef CONFIG_NET_BRIDGE |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 961 | [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge, |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 962 | #endif |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 963 | [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport, |
Nikolay Nikolaev | 03ce574 | 2014-06-10 13:02:16 +0300 | [diff] [blame] | 964 | #ifdef CONFIG_VHOST_NET_USED |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 965 | [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user, |
Nikolay Nikolaev | 03ce574 | 2014-06-10 13:02:16 +0300 | [diff] [blame] | 966 | #endif |
Gonglei | 015a33b | 2014-07-01 20:58:08 +0800 | [diff] [blame] | 967 | #ifdef CONFIG_L2TPV3 |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 968 | [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3, |
Anton Ivanov | 3fb69aa | 2014-06-20 10:34:41 +0100 | [diff] [blame] | 969 | #endif |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 970 | }; |
| 971 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 972 | |
Eric Blake | 0e55c38 | 2016-07-13 21:50:24 -0600 | [diff] [blame] | 973 | static int net_client_init1(const void *object, bool is_netdev, Error **errp) |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 974 | { |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 975 | Netdev legacy = {0}; |
| 976 | const Netdev *netdev; |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 977 | const char *name; |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 978 | NetClientState *peer = NULL; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 979 | |
Markus Armbruster | 5294e2c | 2010-03-25 17:22:38 +0100 | [diff] [blame] | 980 | if (is_netdev) { |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 981 | netdev = object; |
Stefan Hajnoczi | 1e81aba | 2015-05-27 17:16:52 +0100 | [diff] [blame] | 982 | name = netdev->id; |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 983 | |
Thomas Huth | 857d208 | 2018-02-21 11:18:34 +0100 | [diff] [blame] | 984 | if (netdev->type == NET_CLIENT_DRIVER_NIC || |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 985 | !net_client_init_fun[netdev->type]) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 986 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", |
| 987 | "a netdev backend type"); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 988 | return -1; |
| 989 | } |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 990 | } else { |
Stefan Hajnoczi | 1e81aba | 2015-05-27 17:16:52 +0100 | [diff] [blame] | 991 | const NetLegacy *net = object; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 992 | const NetLegacyOptions *opts = net->opts; |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 993 | legacy.id = net->id; |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 994 | netdev = &legacy; |
Stefan Hajnoczi | 1e81aba | 2015-05-27 17:16:52 +0100 | [diff] [blame] | 995 | /* missing optional values have been initialized to "all bits zero" */ |
| 996 | name = net->has_id ? net->id : net->name; |
| 997 | |
Thomas Huth | 101625a | 2018-09-20 10:14:08 +0200 | [diff] [blame] | 998 | if (net->has_name) { |
| 999 | warn_report("The 'name' parameter is deprecated, use 'id' instead"); |
| 1000 | } |
| 1001 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1002 | /* Map the old options to the new flat type */ |
| 1003 | switch (opts->type) { |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1004 | case NET_LEGACY_OPTIONS_TYPE_NONE: |
Stefan Hajnoczi | 1e81aba | 2015-05-27 17:16:52 +0100 | [diff] [blame] | 1005 | return 0; /* nothing to do */ |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1006 | case NET_LEGACY_OPTIONS_TYPE_NIC: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1007 | legacy.type = NET_CLIENT_DRIVER_NIC; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1008 | legacy.u.nic = opts->u.nic; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1009 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1010 | case NET_LEGACY_OPTIONS_TYPE_USER: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1011 | legacy.type = NET_CLIENT_DRIVER_USER; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1012 | legacy.u.user = opts->u.user; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1013 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1014 | case NET_LEGACY_OPTIONS_TYPE_TAP: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1015 | legacy.type = NET_CLIENT_DRIVER_TAP; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1016 | legacy.u.tap = opts->u.tap; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1017 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1018 | case NET_LEGACY_OPTIONS_TYPE_L2TPV3: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1019 | legacy.type = NET_CLIENT_DRIVER_L2TPV3; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1020 | legacy.u.l2tpv3 = opts->u.l2tpv3; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1021 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1022 | case NET_LEGACY_OPTIONS_TYPE_SOCKET: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1023 | legacy.type = NET_CLIENT_DRIVER_SOCKET; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1024 | legacy.u.socket = opts->u.socket; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1025 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1026 | case NET_LEGACY_OPTIONS_TYPE_VDE: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1027 | legacy.type = NET_CLIENT_DRIVER_VDE; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1028 | legacy.u.vde = opts->u.vde; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1029 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1030 | case NET_LEGACY_OPTIONS_TYPE_BRIDGE: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1031 | legacy.type = NET_CLIENT_DRIVER_BRIDGE; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1032 | legacy.u.bridge = opts->u.bridge; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1033 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1034 | case NET_LEGACY_OPTIONS_TYPE_NETMAP: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1035 | legacy.type = NET_CLIENT_DRIVER_NETMAP; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1036 | legacy.u.netmap = opts->u.netmap; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1037 | break; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1038 | case NET_LEGACY_OPTIONS_TYPE_VHOST_USER: |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1039 | legacy.type = NET_CLIENT_DRIVER_VHOST_USER; |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1040 | legacy.u.vhost_user = opts->u.vhost_user; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1041 | break; |
| 1042 | default: |
| 1043 | abort(); |
Markus Armbruster | ca7eb18 | 2015-05-15 13:58:49 +0200 | [diff] [blame] | 1044 | } |
Stefan Hajnoczi | d139e9a | 2015-05-27 17:16:50 +0100 | [diff] [blame] | 1045 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1046 | if (!net_client_init_fun[netdev->type]) { |
Stefan Hajnoczi | d139e9a | 2015-05-27 17:16:50 +0100 | [diff] [blame] | 1047 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", |
| 1048 | "a net backend type (maybe it is not compiled " |
| 1049 | "into this binary)"); |
| 1050 | return -1; |
| 1051 | } |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1052 | |
Thomas Huth | af1a5c3 | 2018-04-30 20:02:23 +0200 | [diff] [blame] | 1053 | /* Do not add to a hub if it's a nic with a netdev= parameter. */ |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1054 | if (netdev->type != NET_CLIENT_DRIVER_NIC || |
Markus Armbruster | d3be4b5 | 2017-02-21 21:46:27 +0100 | [diff] [blame] | 1055 | !opts->u.nic.has_netdev) { |
Thomas Huth | af1a5c3 | 2018-04-30 20:02:23 +0200 | [diff] [blame] | 1056 | peer = net_hub_add_port(0, NULL, NULL); |
Thomas Huth | a2dbe13 | 2017-01-24 10:42:49 +0100 | [diff] [blame] | 1057 | } |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 1058 | } |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1059 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1060 | if (net_client_init_fun[netdev->type](netdev, name, peer, errp) < 0) { |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 1061 | /* FIXME drop when all init functions store an Error */ |
| 1062 | if (errp && !*errp) { |
| 1063 | error_setg(errp, QERR_DEVICE_INIT_FAILED, |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 1064 | NetClientDriver_str(netdev->type)); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1065 | } |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 1066 | return -1; |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1067 | } |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1068 | return 0; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1071 | static void show_netdevs(void) |
| 1072 | { |
| 1073 | int idx; |
| 1074 | const char *available_netdevs[] = { |
| 1075 | "socket", |
| 1076 | "hubport", |
| 1077 | "tap", |
| 1078 | #ifdef CONFIG_SLIRP |
| 1079 | "user", |
| 1080 | #endif |
| 1081 | #ifdef CONFIG_L2TPV3 |
| 1082 | "l2tpv3", |
| 1083 | #endif |
| 1084 | #ifdef CONFIG_VDE |
| 1085 | "vde", |
| 1086 | #endif |
| 1087 | #ifdef CONFIG_NET_BRIDGE |
| 1088 | "bridge", |
| 1089 | #endif |
| 1090 | #ifdef CONFIG_NETMAP |
| 1091 | "netmap", |
| 1092 | #endif |
| 1093 | #ifdef CONFIG_POSIX |
| 1094 | "vhost-user", |
| 1095 | #endif |
| 1096 | }; |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1097 | |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1098 | printf("Available netdev backend types:\n"); |
| 1099 | for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) { |
| 1100 | puts(available_netdevs[idx]); |
| 1101 | } |
| 1102 | } |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1103 | |
Thomas Huth | aa09a48 | 2018-02-21 11:18:33 +0100 | [diff] [blame] | 1104 | static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp) |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1105 | { |
| 1106 | void *object = NULL; |
| 1107 | Error *err = NULL; |
| 1108 | int ret = -1; |
Eric Blake | 09204ea | 2016-06-09 10:48:36 -0600 | [diff] [blame] | 1109 | Visitor *v = opts_visitor_new(opts); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1110 | |
Lin Ma | 8b43f96 | 2018-06-11 17:23:05 +0800 | [diff] [blame] | 1111 | const char *type = qemu_opt_get(opts, "type"); |
| 1112 | |
| 1113 | if (is_netdev && type && is_help_option(type)) { |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1114 | show_netdevs(); |
| 1115 | exit(0); |
| 1116 | } else { |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1117 | /* Parse convenience option format ip6-net=fec0::0[/64] */ |
Samuel Thibault | 891a2bb | 2016-03-31 00:52:50 +0200 | [diff] [blame] | 1118 | const char *ip6_net = qemu_opt_get(opts, "ipv6-net"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1119 | |
| 1120 | if (ip6_net) { |
| 1121 | char buf[strlen(ip6_net) + 1]; |
| 1122 | |
| 1123 | if (get_str_sep(buf, sizeof(buf), &ip6_net, '/') < 0) { |
| 1124 | /* Default 64bit prefix length. */ |
Samuel Thibault | 891a2bb | 2016-03-31 00:52:50 +0200 | [diff] [blame] | 1125 | qemu_opt_set(opts, "ipv6-prefix", ip6_net, &error_abort); |
| 1126 | qemu_opt_set_number(opts, "ipv6-prefixlen", 64, &error_abort); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1127 | } else { |
| 1128 | /* User-specified prefix length. */ |
| 1129 | unsigned long len; |
| 1130 | int err; |
| 1131 | |
Samuel Thibault | 891a2bb | 2016-03-31 00:52:50 +0200 | [diff] [blame] | 1132 | qemu_opt_set(opts, "ipv6-prefix", buf, &error_abort); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1133 | err = qemu_strtoul(ip6_net, NULL, 10, &len); |
| 1134 | |
| 1135 | if (err) { |
| 1136 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
Samuel Thibault | 891a2bb | 2016-03-31 00:52:50 +0200 | [diff] [blame] | 1137 | "ipv6-prefix", "a number"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1138 | } else { |
Samuel Thibault | 891a2bb | 2016-03-31 00:52:50 +0200 | [diff] [blame] | 1139 | qemu_opt_set_number(opts, "ipv6-prefixlen", len, |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1140 | &error_abort); |
| 1141 | } |
| 1142 | } |
Samuel Thibault | 891a2bb | 2016-03-31 00:52:50 +0200 | [diff] [blame] | 1143 | qemu_opt_unset(opts, "ipv6-net"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
Eric Blake | 96a1616 | 2016-02-23 14:14:33 -0700 | [diff] [blame] | 1147 | if (is_netdev) { |
| 1148 | visit_type_Netdev(v, NULL, (Netdev **)&object, &err); |
| 1149 | } else { |
| 1150 | visit_type_NetLegacy(v, NULL, (NetLegacy **)&object, &err); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | if (!err) { |
Laszlo Ersek | 1a0c095 | 2012-07-17 16:17:21 +0200 | [diff] [blame] | 1154 | ret = net_client_init1(object, is_netdev, &err); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Eric Blake | 96a1616 | 2016-02-23 14:14:33 -0700 | [diff] [blame] | 1157 | if (is_netdev) { |
| 1158 | qapi_free_Netdev(object); |
| 1159 | } else { |
| 1160 | qapi_free_NetLegacy(object); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | error_propagate(errp, err); |
Eric Blake | 09204ea | 2016-06-09 10:48:36 -0600 | [diff] [blame] | 1164 | visit_free(v); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1165 | return ret; |
| 1166 | } |
| 1167 | |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1168 | void netdev_add(QemuOpts *opts, Error **errp) |
| 1169 | { |
Eric Blake | 0e55c38 | 2016-07-13 21:50:24 -0600 | [diff] [blame] | 1170 | net_client_init(opts, true, errp); |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1171 | } |
| 1172 | |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 1173 | void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp) |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1174 | { |
Luiz Capitulino | 4e89978 | 2012-04-18 17:24:01 -0300 | [diff] [blame] | 1175 | Error *local_err = NULL; |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1176 | QemuOptsList *opts_list; |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1177 | QemuOpts *opts; |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1178 | |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1179 | opts_list = qemu_find_opts_err("netdev", &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1180 | if (local_err) { |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 1181 | goto out; |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1184 | opts = qemu_opts_from_qdict(opts_list, qdict, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1185 | if (local_err) { |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 1186 | goto out; |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | netdev_add(opts, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1190 | if (local_err) { |
Yoshiaki Tamura | 410cbaf | 2010-06-21 10:41:36 +0900 | [diff] [blame] | 1191 | qemu_opts_del(opts); |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 1192 | goto out; |
Yoshiaki Tamura | 410cbaf | 2010-06-21 10:41:36 +0900 | [diff] [blame] | 1193 | } |
| 1194 | |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 1195 | out: |
| 1196 | error_propagate(errp, local_err); |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
Luiz Capitulino | 5f96415 | 2012-04-16 14:36:32 -0300 | [diff] [blame] | 1199 | void qmp_netdev_del(const char *id, Error **errp) |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1200 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1201 | NetClientState *nc; |
Stefan Hajnoczi | 645c949 | 2012-10-24 14:34:12 +0200 | [diff] [blame] | 1202 | QemuOpts *opts; |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1203 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1204 | nc = qemu_find_netdev(id); |
| 1205 | if (!nc) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 1206 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1207 | "Device '%s' not found", id); |
Luiz Capitulino | 5f96415 | 2012-04-16 14:36:32 -0300 | [diff] [blame] | 1208 | return; |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1209 | } |
Luiz Capitulino | 5f96415 | 2012-04-16 14:36:32 -0300 | [diff] [blame] | 1210 | |
Stefan Hajnoczi | 645c949 | 2012-10-24 14:34:12 +0200 | [diff] [blame] | 1211 | opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); |
| 1212 | if (!opts) { |
| 1213 | error_setg(errp, "Device '%s' is not a netdev", id); |
| 1214 | return; |
| 1215 | } |
| 1216 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 1217 | qemu_del_net_client(nc); |
Stefan Hajnoczi | 645c949 | 2012-10-24 14:34:12 +0200 | [diff] [blame] | 1218 | qemu_opts_del(opts); |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1221 | static void netfilter_print_info(Monitor *mon, NetFilterState *nf) |
| 1222 | { |
| 1223 | char *str; |
| 1224 | ObjectProperty *prop; |
| 1225 | ObjectPropertyIterator iter; |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 1226 | Visitor *v; |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1227 | |
| 1228 | /* generate info str */ |
| 1229 | object_property_iter_init(&iter, OBJECT(nf)); |
| 1230 | while ((prop = object_property_iter_next(&iter))) { |
| 1231 | if (!strcmp(prop->name, "type")) { |
| 1232 | continue; |
| 1233 | } |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 1234 | v = string_output_visitor_new(false, &str); |
| 1235 | object_property_get(OBJECT(nf), v, prop->name, NULL); |
| 1236 | visit_complete(v, &str); |
| 1237 | visit_free(v); |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1238 | monitor_printf(mon, ",%s=%s", prop->name, str); |
| 1239 | g_free(str); |
| 1240 | } |
| 1241 | monitor_printf(mon, "\n"); |
| 1242 | } |
| 1243 | |
Zhi Yong Wu | 1a85959 | 2012-07-24 16:35:16 +0100 | [diff] [blame] | 1244 | void print_net_client(Monitor *mon, NetClientState *nc) |
Jan Kiszka | 44e798d | 2011-07-20 12:20:21 +0200 | [diff] [blame] | 1245 | { |
Yang Hongyang | a4960f5 | 2015-10-07 11:52:19 +0800 | [diff] [blame] | 1246 | NetFilterState *nf; |
| 1247 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1248 | monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name, |
| 1249 | nc->queue_index, |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 1250 | NetClientDriver_str(nc->info->type), |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1251 | nc->info_str); |
Yang Hongyang | a4960f5 | 2015-10-07 11:52:19 +0800 | [diff] [blame] | 1252 | if (!QTAILQ_EMPTY(&nc->filters)) { |
| 1253 | monitor_printf(mon, "filters:\n"); |
| 1254 | } |
| 1255 | QTAILQ_FOREACH(nf, &nc->filters, next) { |
Yang Hongyang | a3e8a3f | 2015-10-20 09:51:26 +0800 | [diff] [blame] | 1256 | char *path = object_get_canonical_path_component(OBJECT(nf)); |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1257 | |
| 1258 | monitor_printf(mon, " - %s: type=%s", path, |
| 1259 | object_get_typename(OBJECT(nf))); |
| 1260 | netfilter_print_info(mon, nf); |
Yang Hongyang | a3e8a3f | 2015-10-20 09:51:26 +0800 | [diff] [blame] | 1261 | g_free(path); |
Yang Hongyang | a4960f5 | 2015-10-07 11:52:19 +0800 | [diff] [blame] | 1262 | } |
Jan Kiszka | 44e798d | 2011-07-20 12:20:21 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1265 | RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name, |
| 1266 | Error **errp) |
| 1267 | { |
| 1268 | NetClientState *nc; |
| 1269 | RxFilterInfoList *filter_list = NULL, *last_entry = NULL; |
| 1270 | |
| 1271 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 1272 | RxFilterInfoList *entry; |
| 1273 | RxFilterInfo *info; |
| 1274 | |
| 1275 | if (has_name && strcmp(nc->name, name) != 0) { |
| 1276 | continue; |
| 1277 | } |
| 1278 | |
| 1279 | /* only query rx-filter information of NIC */ |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1280 | if (nc->info->type != NET_CLIENT_DRIVER_NIC) { |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1281 | if (has_name) { |
| 1282 | error_setg(errp, "net client(%s) isn't a NIC", name); |
Markus Armbruster | 9083da1 | 2014-04-24 15:44:18 +0200 | [diff] [blame] | 1283 | return NULL; |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1284 | } |
| 1285 | continue; |
| 1286 | } |
| 1287 | |
Vladislav Yasevich | 5320c2c | 2015-10-19 09:04:38 -0400 | [diff] [blame] | 1288 | /* only query information on queue 0 since the info is per nic, |
| 1289 | * not per queue |
| 1290 | */ |
| 1291 | if (nc->queue_index != 0) |
| 1292 | continue; |
| 1293 | |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1294 | if (nc->info->query_rx_filter) { |
| 1295 | info = nc->info->query_rx_filter(nc); |
| 1296 | entry = g_malloc0(sizeof(*entry)); |
| 1297 | entry->value = info; |
| 1298 | |
| 1299 | if (!filter_list) { |
| 1300 | filter_list = entry; |
| 1301 | } else { |
| 1302 | last_entry->next = entry; |
| 1303 | } |
| 1304 | last_entry = entry; |
| 1305 | } else if (has_name) { |
| 1306 | error_setg(errp, "net client(%s) doesn't support" |
| 1307 | " rx-filter querying", name); |
Markus Armbruster | 9083da1 | 2014-04-24 15:44:18 +0200 | [diff] [blame] | 1308 | return NULL; |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1309 | } |
Markus Armbruster | 638fb14 | 2014-04-24 15:44:17 +0200 | [diff] [blame] | 1310 | |
| 1311 | if (has_name) { |
| 1312 | break; |
| 1313 | } |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1314 | } |
| 1315 | |
Markus Armbruster | 9083da1 | 2014-04-24 15:44:18 +0200 | [diff] [blame] | 1316 | if (filter_list == NULL && has_name) { |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1317 | error_setg(errp, "invalid net client name: %s", name); |
| 1318 | } |
| 1319 | |
| 1320 | return filter_list; |
| 1321 | } |
| 1322 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1323 | void hmp_info_network(Monitor *mon, const QDict *qdict) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1324 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1325 | NetClientState *nc, *peer; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1326 | NetClientDriver type; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1327 | |
Zhi Yong Wu | 1a85959 | 2012-07-24 16:35:16 +0100 | [diff] [blame] | 1328 | net_hub_info(mon); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1329 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1330 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 1331 | peer = nc->peer; |
| 1332 | type = nc->info->type; |
Zhi Yong Wu | 1a85959 | 2012-07-24 16:35:16 +0100 | [diff] [blame] | 1333 | |
| 1334 | /* Skip if already printed in hub info */ |
| 1335 | if (net_hub_id_for_client(nc, NULL) == 0) { |
| 1336 | continue; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 1337 | } |
Zhi Yong Wu | 1a85959 | 2012-07-24 16:35:16 +0100 | [diff] [blame] | 1338 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1339 | if (!peer || type == NET_CLIENT_DRIVER_NIC) { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1340 | print_net_client(mon, nc); |
Jan Kiszka | 19061e6 | 2011-07-20 12:20:19 +0200 | [diff] [blame] | 1341 | } /* else it's a netdev connected to a NIC, printed with the NIC */ |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1342 | if (peer && type == NET_CLIENT_DRIVER_NIC) { |
Zhi Yong Wu | 1a85959 | 2012-07-24 16:35:16 +0100 | [diff] [blame] | 1343 | monitor_printf(mon, " \\ "); |
Jan Kiszka | 44e798d | 2011-07-20 12:20:21 +0200 | [diff] [blame] | 1344 | print_net_client(mon, peer); |
Markus Armbruster | a0104e0 | 2010-02-11 14:45:01 +0100 | [diff] [blame] | 1345 | } |
Markus Armbruster | a0104e0 | 2010-02-11 14:45:01 +0100 | [diff] [blame] | 1346 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Zhang Chen | 5fbba3d | 2018-09-03 12:38:56 +0800 | [diff] [blame] | 1349 | void colo_notify_filters_event(int event, Error **errp) |
| 1350 | { |
| 1351 | NetClientState *nc; |
| 1352 | NetFilterState *nf; |
| 1353 | NetFilterClass *nfc = NULL; |
| 1354 | Error *local_err = NULL; |
| 1355 | |
| 1356 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 1357 | QTAILQ_FOREACH(nf, &nc->filters, next) { |
| 1358 | nfc = NETFILTER_GET_CLASS(OBJECT(nf)); |
| 1359 | nfc->handle_event(nf, event, &local_err); |
| 1360 | if (local_err) { |
| 1361 | error_propagate(errp, local_err); |
| 1362 | return; |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
Luiz Capitulino | 4b37156 | 2011-11-23 13:11:55 -0200 | [diff] [blame] | 1368 | void qmp_set_link(const char *name, bool up, Error **errp) |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1369 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1370 | NetClientState *ncs[MAX_QUEUE_NUM]; |
| 1371 | NetClientState *nc; |
| 1372 | int queues, i; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1373 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1374 | queues = qemu_find_net_clients_except(name, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1375 | NET_CLIENT_DRIVER__MAX, |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1376 | MAX_QUEUE_NUM); |
| 1377 | |
| 1378 | if (queues == 0) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 1379 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1380 | "Device '%s' not found", name); |
Luiz Capitulino | 4b37156 | 2011-11-23 13:11:55 -0200 | [diff] [blame] | 1381 | return; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1382 | } |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1383 | nc = ncs[0]; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1384 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1385 | for (i = 0; i < queues; i++) { |
| 1386 | ncs[i]->link_down = !up; |
| 1387 | } |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1388 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1389 | if (nc->info->link_status_changed) { |
| 1390 | nc->info->link_status_changed(nc); |
Mark McLoughlin | 665a3b0 | 2009-11-25 18:49:30 +0000 | [diff] [blame] | 1391 | } |
Michael S. Tsirkin | ab1cbe1 | 2011-02-09 18:45:04 +0200 | [diff] [blame] | 1392 | |
Vlad Yasevich | 02d38fc | 2013-11-21 21:05:51 -0500 | [diff] [blame] | 1393 | if (nc->peer) { |
| 1394 | /* Change peer link only if the peer is NIC and then notify peer. |
| 1395 | * If the peer is a HUBPORT or a backend, we do not change the |
| 1396 | * link status. |
| 1397 | * |
Thomas Huth | af1a5c3 | 2018-04-30 20:02:23 +0200 | [diff] [blame] | 1398 | * This behavior is compatible with qemu hubs where there could be |
Vlad Yasevich | 02d38fc | 2013-11-21 21:05:51 -0500 | [diff] [blame] | 1399 | * multiple clients that can still communicate with each other in |
| 1400 | * disconnected mode. For now maintain this compatibility. |
| 1401 | */ |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1402 | if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { |
Vlad Yasevich | 02d38fc | 2013-11-21 21:05:51 -0500 | [diff] [blame] | 1403 | for (i = 0; i < queues; i++) { |
| 1404 | ncs[i]->peer->link_down = !up; |
| 1405 | } |
| 1406 | } |
| 1407 | if (nc->peer->info->link_status_changed) { |
| 1408 | nc->peer->info->link_status_changed(nc->peer); |
| 1409 | } |
Michael S. Tsirkin | ab1cbe1 | 2011-02-09 18:45:04 +0200 | [diff] [blame] | 1410 | } |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1413 | static void net_vm_change_state_handler(void *opaque, int running, |
| 1414 | RunState state) |
| 1415 | { |
Fam Zheng | 625de44 | 2015-07-07 09:21:07 +0800 | [diff] [blame] | 1416 | NetClientState *nc; |
| 1417 | NetClientState *tmp; |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1418 | |
Fam Zheng | 625de44 | 2015-07-07 09:21:07 +0800 | [diff] [blame] | 1419 | QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { |
| 1420 | if (running) { |
| 1421 | /* Flush queued packets and wake up backends. */ |
| 1422 | if (nc->peer && qemu_can_send_packet(nc)) { |
| 1423 | qemu_flush_queued_packets(nc->peer); |
| 1424 | } |
| 1425 | } else { |
| 1426 | /* Complete all queued packets, to guarantee we don't modify |
| 1427 | * state later when VM is not running. |
| 1428 | */ |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1429 | qemu_flush_or_purge_queued_packets(nc, true); |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1434 | void net_cleanup(void) |
| 1435 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1436 | NetClientState *nc; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1437 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1438 | /* We may del multiple entries during qemu_del_net_client(), |
| 1439 | * so QTAILQ_FOREACH_SAFE() is also not safe here. |
| 1440 | */ |
| 1441 | while (!QTAILQ_EMPTY(&net_clients)) { |
| 1442 | nc = QTAILQ_FIRST(&net_clients); |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1443 | if (nc->info->type == NET_CLIENT_DRIVER_NIC) { |
Jason Wang | 948ecf2 | 2013-01-30 19:12:24 +0800 | [diff] [blame] | 1444 | qemu_del_nic(qemu_get_nic(nc)); |
| 1445 | } else { |
| 1446 | qemu_del_net_client(nc); |
| 1447 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 1448 | } |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1449 | |
| 1450 | qemu_del_vm_change_state_handler(net_change_state_entry); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Markus Armbruster | 668680f | 2010-02-11 14:44:58 +0100 | [diff] [blame] | 1453 | void net_check_clients(void) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1454 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1455 | NetClientState *nc; |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 1456 | int i; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1457 | |
Stefan Hajnoczi | 8101764 | 2012-07-24 16:35:07 +0100 | [diff] [blame] | 1458 | net_hub_check_clients(); |
Tristan Gingold | ac60cc1 | 2011-03-15 14:20:54 +0100 | [diff] [blame] | 1459 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1460 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 1461 | if (!nc->peer) { |
Alistair Francis | 8297be8 | 2017-09-11 12:52:53 -0700 | [diff] [blame] | 1462 | warn_report("%s %s has no peer", |
Alistair Francis | b62e39b | 2017-09-11 12:52:56 -0700 | [diff] [blame] | 1463 | nc->info->type == NET_CLIENT_DRIVER_NIC |
| 1464 | ? "nic" : "netdev", |
| 1465 | nc->name); |
Markus Armbruster | efe32fd | 2010-02-11 14:45:00 +0100 | [diff] [blame] | 1466 | } |
| 1467 | } |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 1468 | |
| 1469 | /* Check that all NICs requested via -net nic actually got created. |
| 1470 | * NICs created via -device don't need to be checked here because |
| 1471 | * they are always instantiated. |
| 1472 | */ |
| 1473 | for (i = 0; i < MAX_NICS; i++) { |
| 1474 | NICInfo *nd = &nd_table[i]; |
| 1475 | if (nd->used && !nd->instantiated) { |
Alistair Francis | 8297be8 | 2017-09-11 12:52:53 -0700 | [diff] [blame] | 1476 | warn_report("requested NIC (%s, model %s) " |
| 1477 | "was not created (not supported by this machine?)", |
| 1478 | nd->name ? nd->name : "anonymous", |
| 1479 | nd->model ? nd->model : "unspecified"); |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 1480 | } |
| 1481 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1482 | } |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1483 | |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 1484 | static int net_init_client(void *dummy, QemuOpts *opts, Error **errp) |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1485 | { |
Thomas Huth | 34f708b | 2018-02-21 11:18:30 +0100 | [diff] [blame] | 1486 | return net_client_init(opts, false, errp); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1487 | } |
| 1488 | |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 1489 | static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp) |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1490 | { |
Thomas Huth | 34f708b | 2018-02-21 11:18:30 +0100 | [diff] [blame] | 1491 | return net_client_init(opts, true, errp); |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1492 | } |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1493 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1494 | /* For the convenience "--nic" parameter */ |
| 1495 | static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) |
| 1496 | { |
| 1497 | char *mac, *nd_id; |
| 1498 | int idx, ret; |
| 1499 | NICInfo *ni; |
| 1500 | const char *type; |
| 1501 | |
| 1502 | type = qemu_opt_get(opts, "type"); |
| 1503 | if (type && g_str_equal(type, "none")) { |
| 1504 | return 0; /* Nothing to do, default_net is cleared in vl.c */ |
| 1505 | } |
| 1506 | |
| 1507 | idx = nic_get_free_idx(); |
| 1508 | if (idx == -1 || nb_nics >= MAX_NICS) { |
| 1509 | error_setg(errp, "no more on-board/default NIC slots available"); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1510 | return -1; |
| 1511 | } |
Gerd Hoffmann | 3329f07 | 2010-08-20 13:52:01 +0200 | [diff] [blame] | 1512 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1513 | if (!type) { |
| 1514 | qemu_opt_set(opts, "type", "user", &error_abort); |
| 1515 | } |
| 1516 | |
| 1517 | ni = &nd_table[idx]; |
| 1518 | memset(ni, 0, sizeof(*ni)); |
| 1519 | ni->model = qemu_opt_get_del(opts, "model"); |
| 1520 | |
| 1521 | /* Create an ID if the user did not specify one */ |
| 1522 | nd_id = g_strdup(qemu_opts_id(opts)); |
| 1523 | if (!nd_id) { |
| 1524 | nd_id = g_strdup_printf("__org.qemu.nic%i\n", idx); |
| 1525 | qemu_opts_set_id(opts, nd_id); |
| 1526 | } |
| 1527 | |
| 1528 | /* Handle MAC address */ |
| 1529 | mac = qemu_opt_get_del(opts, "mac"); |
| 1530 | if (mac) { |
| 1531 | ret = net_parse_macaddr(ni->macaddr.a, mac); |
| 1532 | g_free(mac); |
| 1533 | if (ret) { |
| 1534 | error_setg(errp, "invalid syntax for ethernet address"); |
Thomas Huth | 9d94619 | 2018-04-30 09:26:45 +0200 | [diff] [blame] | 1535 | goto out; |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1536 | } |
| 1537 | if (is_multicast_ether_addr(ni->macaddr.a)) { |
| 1538 | error_setg(errp, "NIC cannot have multicast MAC address"); |
Thomas Huth | 9d94619 | 2018-04-30 09:26:45 +0200 | [diff] [blame] | 1539 | ret = -1; |
| 1540 | goto out; |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | qemu_macaddr_default_if_unset(&ni->macaddr); |
| 1544 | |
| 1545 | ret = net_client_init(opts, true, errp); |
| 1546 | if (ret == 0) { |
| 1547 | ni->netdev = qemu_find_netdev(nd_id); |
| 1548 | ni->used = true; |
| 1549 | nb_nics++; |
| 1550 | } |
| 1551 | |
Thomas Huth | 9d94619 | 2018-04-30 09:26:45 +0200 | [diff] [blame] | 1552 | out: |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1553 | g_free(nd_id); |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1554 | return ret; |
| 1555 | } |
| 1556 | |
Thomas Huth | 34f708b | 2018-02-21 11:18:30 +0100 | [diff] [blame] | 1557 | int net_init_clients(Error **errp) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1558 | { |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1559 | net_change_state_entry = |
| 1560 | qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL); |
| 1561 | |
Stefan Hajnoczi | 9487899 | 2012-07-24 16:35:12 +0100 | [diff] [blame] | 1562 | QTAILQ_INIT(&net_clients); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1563 | |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 1564 | if (qemu_opts_foreach(qemu_find_opts("netdev"), |
Thomas Huth | 34f708b | 2018-02-21 11:18:30 +0100 | [diff] [blame] | 1565 | net_init_netdev, NULL, errp)) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1566 | return -1; |
Markus Armbruster | a4c7367 | 2015-03-13 11:07:24 +0100 | [diff] [blame] | 1567 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1568 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1569 | if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, errp)) { |
| 1570 | return -1; |
| 1571 | } |
| 1572 | |
Thomas Huth | 34f708b | 2018-02-21 11:18:30 +0100 | [diff] [blame] | 1573 | if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, errp)) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1574 | return -1; |
| 1575 | } |
| 1576 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
| 1581 | { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 1582 | if (!qemu_opts_parse_noisily(opts_list, optarg, true)) { |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1583 | return -1; |
| 1584 | } |
| 1585 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1586 | return 0; |
| 1587 | } |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1588 | |
| 1589 | /* From FreeBSD */ |
| 1590 | /* XXX: optimize */ |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1591 | uint32_t net_crc32(const uint8_t *p, int len) |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1592 | { |
| 1593 | uint32_t crc; |
| 1594 | int carry, i, j; |
| 1595 | uint8_t b; |
| 1596 | |
| 1597 | crc = 0xffffffff; |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1598 | for (i = 0; i < len; i++) { |
| 1599 | b = *p++; |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1600 | for (j = 0; j < 8; j++) { |
| 1601 | carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); |
| 1602 | crc <<= 1; |
| 1603 | b >>= 1; |
| 1604 | if (carry) { |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1605 | crc = ((crc ^ POLYNOMIAL_BE) | carry); |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | } |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1609 | |
| 1610 | return crc; |
| 1611 | } |
| 1612 | |
Mark Cave-Ayland | f1a7deb | 2017-12-15 18:41:44 +0000 | [diff] [blame] | 1613 | uint32_t net_crc32_le(const uint8_t *p, int len) |
| 1614 | { |
| 1615 | uint32_t crc; |
| 1616 | int carry, i, j; |
| 1617 | uint8_t b; |
| 1618 | |
| 1619 | crc = 0xffffffff; |
| 1620 | for (i = 0; i < len; i++) { |
| 1621 | b = *p++; |
| 1622 | for (j = 0; j < 8; j++) { |
| 1623 | carry = (crc & 0x1) ^ (b & 0x01); |
| 1624 | crc >>= 1; |
| 1625 | b >>= 1; |
| 1626 | if (carry) { |
| 1627 | crc ^= POLYNOMIAL_LE; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | return crc; |
| 1633 | } |
| 1634 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 1635 | QemuOptsList qemu_netdev_opts = { |
| 1636 | .name = "netdev", |
| 1637 | .implied_opt_name = "type", |
| 1638 | .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), |
| 1639 | .desc = { |
| 1640 | /* |
| 1641 | * no elements => accept any params |
| 1642 | * validation will happen later |
| 1643 | */ |
| 1644 | { /* end of list */ } |
| 1645 | }, |
| 1646 | }; |
| 1647 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1648 | QemuOptsList qemu_nic_opts = { |
| 1649 | .name = "nic", |
| 1650 | .implied_opt_name = "type", |
| 1651 | .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head), |
| 1652 | .desc = { |
| 1653 | /* |
| 1654 | * no elements => accept any params |
| 1655 | * validation will happen later |
| 1656 | */ |
| 1657 | { /* end of list */ } |
| 1658 | }, |
| 1659 | }; |
| 1660 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 1661 | QemuOptsList qemu_net_opts = { |
| 1662 | .name = "net", |
| 1663 | .implied_opt_name = "type", |
| 1664 | .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), |
| 1665 | .desc = { |
| 1666 | /* |
| 1667 | * no elements => accept any params |
| 1668 | * validation will happen later |
| 1669 | */ |
| 1670 | { /* end of list */ } |
| 1671 | }, |
| 1672 | }; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1673 | |
| 1674 | void net_socket_rs_init(SocketReadState *rs, |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 1675 | SocketReadStateFinalize *finalize, |
| 1676 | bool vnet_hdr) |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1677 | { |
| 1678 | rs->state = 0; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 1679 | rs->vnet_hdr = vnet_hdr; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1680 | rs->index = 0; |
| 1681 | rs->packet_len = 0; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 1682 | rs->vnet_hdr_len = 0; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1683 | memset(rs->buf, 0, sizeof(rs->buf)); |
| 1684 | rs->finalize = finalize; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Returns |
Zhang Chen | e9e0a58 | 2016-08-18 11:23:25 +0800 | [diff] [blame] | 1689 | * 0: success |
| 1690 | * -1: error occurs |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1691 | */ |
| 1692 | int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size) |
| 1693 | { |
| 1694 | unsigned int l; |
| 1695 | |
| 1696 | while (size > 0) { |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 1697 | /* Reassemble a packet from the network. |
| 1698 | * 0 = getting length. |
| 1699 | * 1 = getting vnet header length. |
| 1700 | * 2 = getting data. |
| 1701 | */ |
| 1702 | switch (rs->state) { |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1703 | case 0: |
| 1704 | l = 4 - rs->index; |
| 1705 | if (l > size) { |
| 1706 | l = size; |
| 1707 | } |
| 1708 | memcpy(rs->buf + rs->index, buf, l); |
| 1709 | buf += l; |
| 1710 | size -= l; |
| 1711 | rs->index += l; |
| 1712 | if (rs->index == 4) { |
| 1713 | /* got length */ |
| 1714 | rs->packet_len = ntohl(*(uint32_t *)rs->buf); |
| 1715 | rs->index = 0; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 1716 | if (rs->vnet_hdr) { |
| 1717 | rs->state = 1; |
| 1718 | } else { |
| 1719 | rs->state = 2; |
| 1720 | rs->vnet_hdr_len = 0; |
| 1721 | } |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1722 | } |
| 1723 | break; |
| 1724 | case 1: |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 1725 | l = 4 - rs->index; |
| 1726 | if (l > size) { |
| 1727 | l = size; |
| 1728 | } |
| 1729 | memcpy(rs->buf + rs->index, buf, l); |
| 1730 | buf += l; |
| 1731 | size -= l; |
| 1732 | rs->index += l; |
| 1733 | if (rs->index == 4) { |
| 1734 | /* got vnet header length */ |
| 1735 | rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf); |
| 1736 | rs->index = 0; |
| 1737 | rs->state = 2; |
| 1738 | } |
| 1739 | break; |
| 1740 | case 2: |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1741 | l = rs->packet_len - rs->index; |
| 1742 | if (l > size) { |
| 1743 | l = size; |
| 1744 | } |
| 1745 | if (rs->index + l <= sizeof(rs->buf)) { |
| 1746 | memcpy(rs->buf + rs->index, buf, l); |
| 1747 | } else { |
| 1748 | fprintf(stderr, "serious error: oversized packet received," |
| 1749 | "connection terminated.\n"); |
| 1750 | rs->index = rs->state = 0; |
| 1751 | return -1; |
| 1752 | } |
| 1753 | |
| 1754 | rs->index += l; |
| 1755 | buf += l; |
| 1756 | size -= l; |
| 1757 | if (rs->index >= rs->packet_len) { |
| 1758 | rs->index = 0; |
| 1759 | rs->state = 0; |
Daniel P. Berrange | e79cd40 | 2016-11-04 15:46:33 +0000 | [diff] [blame] | 1760 | assert(rs->finalize); |
| 1761 | rs->finalize(rs); |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1762 | } |
| 1763 | break; |
| 1764 | } |
| 1765 | } |
Zhang Chen | e9e0a58 | 2016-08-18 11:23:25 +0800 | [diff] [blame] | 1766 | |
| 1767 | assert(size == 0); |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 1768 | return 0; |
| 1769 | } |