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" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 30 | #include "hw/qdev-properties.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 31 | #include "net/slirp.h" |
Dmitry Krivenok | d60b20c | 2013-10-21 12:08:44 +0400 | [diff] [blame] | 32 | #include "net/eth.h" |
Paolo Bonzini | fd9400b | 2012-10-24 11:27:28 +0200 | [diff] [blame] | 33 | #include "util.h" |
Paolo Bonzini | a245fc1 | 2012-09-17 18:43:51 +0200 | [diff] [blame] | 34 | |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 35 | #include "monitor/monitor.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 36 | #include "qemu/help_option.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 37 | #include "qapi/qapi-commands-net.h" |
Jason Wang | f9bb0c1 | 2021-04-02 11:05:20 +0800 | [diff] [blame] | 38 | #include "qapi/qapi-visit-net.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 39 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 40 | #include "qapi/qmp/qerror.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 41 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 42 | #include "qemu/sockets.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 43 | #include "qemu/cutils.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 44 | #include "qemu/config-file.h" |
Markus Armbruster | 856dfd8 | 2019-05-23 16:35:06 +0200 | [diff] [blame] | 45 | #include "qemu/ctype.h" |
Thomas Huth | 27eb372 | 2021-02-15 10:02:25 +0100 | [diff] [blame] | 46 | #include "qemu/id.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 47 | #include "qemu/iov.h" |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 48 | #include "qemu/qemu-print.h" |
Alex Bligh | 6a1751b | 2013-08-21 16:02:47 +0100 | [diff] [blame] | 49 | #include "qemu/main-loop.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 50 | #include "qemu/option.h" |
Laurent Vivier | 5166fe0 | 2022-10-21 11:09:11 +0200 | [diff] [blame] | 51 | #include "qemu/keyval.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 52 | #include "qapi/error.h" |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 53 | #include "qapi/opts-visitor.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 54 | #include "sysemu/runstate.h" |
Rao, Lei | 0c7af1a | 2021-06-08 16:23:30 +0800 | [diff] [blame] | 55 | #include "net/colo-compare.h" |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 56 | #include "net/filter.h" |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 57 | #include "qapi/string-output-visitor.h" |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 58 | #include "qapi/qobject-input-visitor.h" |
Akihiko Odaki | a677537 | 2024-04-28 16:00:46 +0900 | [diff] [blame] | 59 | #include "standard-headers/linux/virtio_net.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 60 | |
Stefan Weil | 2944e4e | 2012-02-04 09:24:46 +0100 | [diff] [blame] | 61 | /* Net bridge is currently not supported for W32. */ |
| 62 | #if !defined(_WIN32) |
| 63 | # define CONFIG_NET_BRIDGE |
| 64 | #endif |
| 65 | |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 66 | static VMChangeStateEntry *net_change_state_entry; |
Markus Armbruster | ae71d13 | 2023-01-24 13:19:30 +0100 | [diff] [blame] | 67 | NetClientStateList net_clients; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 68 | |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 69 | typedef struct NetdevQueueEntry { |
| 70 | Netdev *nd; |
| 71 | Location loc; |
| 72 | QSIMPLEQ_ENTRY(NetdevQueueEntry) entry; |
| 73 | } NetdevQueueEntry; |
| 74 | |
| 75 | typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue; |
| 76 | |
| 77 | static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue); |
| 78 | |
David Woodhouse | 2cdeca0 | 2023-10-21 23:09:38 +0100 | [diff] [blame] | 79 | static GHashTable *nic_model_help; |
| 80 | |
David Woodhouse | e8c5c45 | 2023-10-23 09:38:01 +0100 | [diff] [blame] | 81 | static int nb_nics; |
| 82 | static NICInfo nd_table[MAX_NICS]; |
| 83 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 84 | /***********************************************************/ |
| 85 | /* network device redirectors */ |
| 86 | |
Laurent Vivier | 30e4226 | 2022-10-21 11:09:06 +0200 | [diff] [blame] | 87 | int convert_host_port(struct sockaddr_in *saddr, const char *host, |
| 88 | const char *port, Error **errp) |
| 89 | { |
| 90 | struct hostent *he; |
| 91 | const char *r; |
| 92 | long p; |
| 93 | |
| 94 | memset(saddr, 0, sizeof(*saddr)); |
| 95 | |
| 96 | saddr->sin_family = AF_INET; |
| 97 | if (host[0] == '\0') { |
| 98 | saddr->sin_addr.s_addr = 0; |
| 99 | } else { |
| 100 | if (qemu_isdigit(host[0])) { |
| 101 | if (!inet_aton(host, &saddr->sin_addr)) { |
| 102 | error_setg(errp, "host address '%s' is not a valid " |
| 103 | "IPv4 address", host); |
| 104 | return -1; |
| 105 | } |
| 106 | } else { |
| 107 | he = gethostbyname(host); |
| 108 | if (he == NULL) { |
| 109 | error_setg(errp, "can't resolve host address '%s'", host); |
| 110 | return -1; |
| 111 | } |
| 112 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
| 113 | } |
| 114 | } |
| 115 | if (qemu_strtol(port, &r, 0, &p) != 0) { |
| 116 | error_setg(errp, "port number '%s' is invalid", port); |
| 117 | return -1; |
| 118 | } |
| 119 | saddr->sin_port = htons(p); |
| 120 | return 0; |
| 121 | } |
| 122 | |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 123 | int parse_host_port(struct sockaddr_in *saddr, const char *str, |
| 124 | Error **errp) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 125 | { |
Stefano Garzarella | add9934 | 2019-05-17 15:47:47 +0200 | [diff] [blame] | 126 | gchar **substrings; |
Laurent Vivier | 30e4226 | 2022-10-21 11:09:06 +0200 | [diff] [blame] | 127 | int ret; |
Peter Maydell | 5929238 | 2021-08-13 16:05:03 +0100 | [diff] [blame] | 128 | |
Stefano Garzarella | add9934 | 2019-05-17 15:47:47 +0200 | [diff] [blame] | 129 | substrings = g_strsplit(str, ":", 2); |
| 130 | if (!substrings || !substrings[0] || !substrings[1]) { |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 131 | error_setg(errp, "host address '%s' doesn't contain ':' " |
| 132 | "separating host from port", str); |
Stefano Garzarella | add9934 | 2019-05-17 15:47:47 +0200 | [diff] [blame] | 133 | ret = -1; |
| 134 | goto out; |
Mao Zhongyi | bcd4dfd | 2017-09-04 22:35:39 +0800 | [diff] [blame] | 135 | } |
Stefano Garzarella | add9934 | 2019-05-17 15:47:47 +0200 | [diff] [blame] | 136 | |
Laurent Vivier | 30e4226 | 2022-10-21 11:09:06 +0200 | [diff] [blame] | 137 | ret = convert_host_port(saddr, substrings[0], substrings[1], errp); |
Stefano Garzarella | add9934 | 2019-05-17 15:47:47 +0200 | [diff] [blame] | 138 | |
| 139 | out: |
| 140 | g_strfreev(substrings); |
| 141 | return ret; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Scott Feldman | 890ee6a | 2015-03-13 21:09:25 -0700 | [diff] [blame] | 144 | char *qemu_mac_strdup_printf(const uint8_t *macaddr) |
| 145 | { |
| 146 | return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", |
| 147 | macaddr[0], macaddr[1], macaddr[2], |
| 148 | macaddr[3], macaddr[4], macaddr[5]); |
| 149 | } |
| 150 | |
Laurent Vivier | 53b85d9 | 2022-10-21 11:09:10 +0200 | [diff] [blame] | 151 | void qemu_set_info_str(NetClientState *nc, const char *fmt, ...) |
| 152 | { |
| 153 | va_list ap; |
| 154 | |
| 155 | va_start(ap, fmt); |
| 156 | vsnprintf(nc->info_str, sizeof(nc->info_str), fmt, ap); |
| 157 | va_end(ap); |
| 158 | } |
| 159 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 160 | void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) |
aliguori | 7cb7434b | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 161 | { |
Laurent Vivier | 53b85d9 | 2022-10-21 11:09:10 +0200 | [diff] [blame] | 162 | qemu_set_info_str(nc, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
| 163 | nc->model, macaddr[0], macaddr[1], macaddr[2], |
| 164 | macaddr[3], macaddr[4], macaddr[5]); |
aliguori | 7cb7434b | 2009-01-07 17:46:21 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 167 | static int mac_table[256] = {0}; |
| 168 | |
| 169 | static void qemu_macaddr_set_used(MACAddr *macaddr) |
| 170 | { |
| 171 | int index; |
| 172 | |
| 173 | for (index = 0x56; index < 0xFF; index++) { |
| 174 | if (macaddr->a[5] == index) { |
| 175 | mac_table[index]++; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | static void qemu_macaddr_set_free(MACAddr *macaddr) |
| 181 | { |
| 182 | int index; |
| 183 | static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; |
| 184 | |
| 185 | if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { |
| 186 | return; |
| 187 | } |
| 188 | for (index = 0x56; index < 0xFF; index++) { |
| 189 | if (macaddr->a[5] == index) { |
| 190 | mac_table[index]--; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static int qemu_macaddr_get_free(void) |
| 196 | { |
| 197 | int index; |
| 198 | |
| 199 | for (index = 0x56; index < 0xFF; index++) { |
| 200 | if (mac_table[index] == 0) { |
| 201 | return index; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return -1; |
| 206 | } |
| 207 | |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 208 | void qemu_macaddr_default_if_unset(MACAddr *macaddr) |
| 209 | { |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 210 | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 211 | static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 212 | |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 213 | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) { |
| 214 | if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) { |
| 215 | return; |
| 216 | } else { |
| 217 | qemu_macaddr_set_used(macaddr); |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 222 | macaddr->a[0] = 0x52; |
| 223 | macaddr->a[1] = 0x54; |
| 224 | macaddr->a[2] = 0x00; |
| 225 | macaddr->a[3] = 0x12; |
| 226 | macaddr->a[4] = 0x34; |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 227 | macaddr->a[5] = qemu_macaddr_get_free(); |
| 228 | qemu_macaddr_set_used(macaddr); |
Gerd Hoffmann | 76d32cb | 2009-10-21 15:25:22 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 231 | /** |
| 232 | * Generate a name for net client |
| 233 | * |
Amos Kong | c963530 | 2013-04-15 18:55:19 +0800 | [diff] [blame] | 234 | * 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] | 235 | */ |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 236 | static char *assign_name(NetClientState *nc1, const char *model) |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 237 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 238 | NetClientState *nc; |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 239 | int id = 0; |
| 240 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 241 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 242 | if (nc == nc1) { |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 243 | continue; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 244 | } |
Amos Kong | c963530 | 2013-04-15 18:55:19 +0800 | [diff] [blame] | 245 | if (strcmp(nc->model, model) == 0) { |
Markus Armbruster | 53e51d8 | 2011-06-16 18:45:36 +0200 | [diff] [blame] | 246 | id++; |
| 247 | } |
| 248 | } |
| 249 | |
Hani Benhabiles | 4bf2c13 | 2014-01-09 19:34:27 +0100 | [diff] [blame] | 250 | return g_strdup_printf("%s.%d", model, id); |
aliguori | 676cff2 | 2009-01-07 17:43:44 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 253 | static void qemu_net_client_destructor(NetClientState *nc) |
| 254 | { |
| 255 | g_free(nc); |
| 256 | } |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 257 | static ssize_t qemu_deliver_packet_iov(NetClientState *sender, |
| 258 | unsigned flags, |
| 259 | const struct iovec *iov, |
| 260 | int iovcnt, |
| 261 | void *opaque); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 262 | |
Jason Wang | 18a1541 | 2013-01-30 19:12:26 +0800 | [diff] [blame] | 263 | static void qemu_net_client_setup(NetClientState *nc, |
| 264 | NetClientInfo *info, |
| 265 | NetClientState *peer, |
| 266 | const char *model, |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 267 | const char *name, |
Jason Wang | 2f849db | 2021-10-20 12:55:55 +0800 | [diff] [blame] | 268 | NetClientDestructor *destructor, |
| 269 | bool is_datapath) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 270 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 271 | nc->info = info; |
| 272 | nc->model = g_strdup(model); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 273 | if (name) { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 274 | nc->name = g_strdup(name); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 275 | } else { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 276 | nc->name = assign_name(nc, model); |
Mark McLoughlin | 45460d1 | 2009-11-25 18:49:02 +0000 | [diff] [blame] | 277 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 278 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame] | 279 | if (peer) { |
| 280 | assert(!peer->peer); |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 281 | nc->peer = peer; |
| 282 | peer->peer = nc; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 283 | } |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 284 | QTAILQ_INSERT_TAIL(&net_clients, nc, next); |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 285 | |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame] | 286 | nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 287 | nc->destructor = destructor; |
Jason Wang | 2f849db | 2021-10-20 12:55:55 +0800 | [diff] [blame] | 288 | nc->is_datapath = is_datapath; |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 289 | QTAILQ_INIT(&nc->filters); |
Jason Wang | 18a1541 | 2013-01-30 19:12:26 +0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | NetClientState *qemu_new_net_client(NetClientInfo *info, |
| 293 | NetClientState *peer, |
| 294 | const char *model, |
| 295 | const char *name) |
| 296 | { |
| 297 | NetClientState *nc; |
| 298 | |
| 299 | assert(info->size >= sizeof(NetClientState)); |
| 300 | |
| 301 | nc = g_malloc0(info->size); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 302 | qemu_net_client_setup(nc, info, peer, model, name, |
Jason Wang | 2f849db | 2021-10-20 12:55:55 +0800 | [diff] [blame] | 303 | qemu_net_client_destructor, true); |
| 304 | |
| 305 | return nc; |
| 306 | } |
| 307 | |
| 308 | NetClientState *qemu_new_net_control_client(NetClientInfo *info, |
| 309 | NetClientState *peer, |
| 310 | const char *model, |
| 311 | const char *name) |
| 312 | { |
| 313 | NetClientState *nc; |
| 314 | |
| 315 | assert(info->size >= sizeof(NetClientState)); |
| 316 | |
| 317 | nc = g_malloc0(info->size); |
| 318 | qemu_net_client_setup(nc, info, peer, model, name, |
| 319 | qemu_net_client_destructor, false); |
Jason Wang | 18a1541 | 2013-01-30 19:12:26 +0800 | [diff] [blame] | 320 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 321 | return nc; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 324 | NICState *qemu_new_nic(NetClientInfo *info, |
| 325 | NICConf *conf, |
| 326 | const char *model, |
| 327 | const char *name, |
Akihiko Odaki | 7d0fefd | 2023-06-01 12:18:58 +0900 | [diff] [blame] | 328 | MemReentrancyGuard *reentrancy_guard, |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 329 | void *opaque) |
| 330 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 331 | NetClientState **peers = conf->peers.ncs; |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 332 | NICState *nic; |
Jiri Pirko | 575a1c0 | 2014-05-26 12:04:08 +0200 | [diff] [blame] | 333 | int i, queues = MAX(1, conf->peers.queues); |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 334 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 335 | assert(info->type == NET_CLIENT_DRIVER_NIC); |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 336 | assert(info->size >= sizeof(NICState)); |
| 337 | |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 338 | nic = g_malloc0(info->size + sizeof(NetClientState) * queues); |
| 339 | nic->ncs = (void *)nic + info->size; |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 340 | nic->conf = conf; |
Akihiko Odaki | 9050f97 | 2023-06-01 12:18:59 +0900 | [diff] [blame] | 341 | nic->reentrancy_guard = reentrancy_guard, |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 342 | nic->opaque = opaque; |
| 343 | |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 344 | for (i = 0; i < queues; i++) { |
| 345 | qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name, |
Jason Wang | 2f849db | 2021-10-20 12:55:55 +0800 | [diff] [blame] | 346 | NULL, true); |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 347 | nic->ncs[i].queue_index = i; |
| 348 | } |
| 349 | |
Mark McLoughlin | ebef2c0 | 2009-11-25 18:49:10 +0000 | [diff] [blame] | 350 | return nic; |
| 351 | } |
| 352 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 353 | NetClientState *qemu_get_subqueue(NICState *nic, int queue_index) |
| 354 | { |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 355 | return nic->ncs + queue_index; |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 356 | } |
| 357 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 358 | NetClientState *qemu_get_queue(NICState *nic) |
| 359 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 360 | return qemu_get_subqueue(nic, 0); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 361 | } |
| 362 | |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 363 | NICState *qemu_get_nic(NetClientState *nc) |
| 364 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 365 | NetClientState *nc0 = nc - nc->queue_index; |
| 366 | |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 367 | return (NICState *)((void *)nc0 - nc->info->size); |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void *qemu_get_nic_opaque(NetClientState *nc) |
| 371 | { |
| 372 | NICState *nic = qemu_get_nic(nc); |
| 373 | |
| 374 | return nic->opaque; |
| 375 | } |
| 376 | |
Cindy Lu | 0165daa | 2020-07-01 22:55:25 +0800 | [diff] [blame] | 377 | NetClientState *qemu_get_peer(NetClientState *nc, int queue_index) |
| 378 | { |
| 379 | assert(nc != NULL); |
| 380 | NetClientState *ncs = nc + queue_index; |
| 381 | return ncs->peer; |
| 382 | } |
| 383 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 384 | static void qemu_cleanup_net_client(NetClientState *nc) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 385 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 386 | QTAILQ_REMOVE(&net_clients, nc, next); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 387 | |
Andreas Färber | cc2a904 | 2013-02-12 23:16:06 +0100 | [diff] [blame] | 388 | if (nc->info->cleanup) { |
| 389 | nc->info->cleanup(nc); |
| 390 | } |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 391 | } |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 392 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 393 | static void qemu_free_net_client(NetClientState *nc) |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 394 | { |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 395 | if (nc->incoming_queue) { |
| 396 | qemu_del_net_queue(nc->incoming_queue); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 397 | } |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 398 | if (nc->peer) { |
| 399 | nc->peer->peer = NULL; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 400 | } |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 401 | g_free(nc->name); |
| 402 | g_free(nc->model); |
Jason Wang | f786045 | 2013-01-30 19:12:27 +0800 | [diff] [blame] | 403 | if (nc->destructor) { |
| 404 | nc->destructor(nc); |
| 405 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 408 | void qemu_del_net_client(NetClientState *nc) |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 409 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 410 | NetClientState *ncs[MAX_QUEUE_NUM]; |
| 411 | int queues, i; |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 412 | NetFilterState *nf, *next; |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 413 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 414 | assert(nc->info->type != NET_CLIENT_DRIVER_NIC); |
Paolo Bonzini | 7fb4391 | 2014-12-23 17:53:20 +0100 | [diff] [blame] | 415 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 416 | /* If the NetClientState belongs to a multiqueue backend, we will change all |
| 417 | * other NetClientStates also. |
| 418 | */ |
| 419 | queues = qemu_find_net_clients_except(nc->name, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 420 | NET_CLIENT_DRIVER_NIC, |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 421 | MAX_QUEUE_NUM); |
| 422 | assert(queues != 0); |
| 423 | |
Yang Hongyang | fdccce4 | 2015-10-07 11:52:14 +0800 | [diff] [blame] | 424 | QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) { |
| 425 | object_unparent(OBJECT(nf)); |
| 426 | } |
| 427 | |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 428 | /* 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] | 429 | if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 430 | NICState *nic = qemu_get_nic(nc->peer); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 431 | if (nic->peer_deleted) { |
| 432 | return; |
| 433 | } |
| 434 | nic->peer_deleted = true; |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 435 | |
| 436 | for (i = 0; i < queues; i++) { |
| 437 | ncs[i]->peer->link_down = true; |
| 438 | } |
| 439 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 440 | if (nc->peer->info->link_status_changed) { |
| 441 | nc->peer->info->link_status_changed(nc->peer); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 442 | } |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 443 | |
| 444 | for (i = 0; i < queues; i++) { |
| 445 | qemu_cleanup_net_client(ncs[i]); |
| 446 | } |
| 447 | |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 451 | for (i = 0; i < queues; i++) { |
| 452 | qemu_cleanup_net_client(ncs[i]); |
| 453 | qemu_free_net_client(ncs[i]); |
| 454 | } |
Jason Wang | 948ecf2 | 2013-01-30 19:12:24 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void qemu_del_nic(NICState *nic) |
| 458 | { |
Jiri Pirko | 575a1c0 | 2014-05-26 12:04:08 +0200 | [diff] [blame] | 459 | int i, queues = MAX(nic->conf->peers.queues, 1); |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 460 | |
Shannon Zhao | 2bc22a5 | 2015-05-21 17:44:48 +0800 | [diff] [blame] | 461 | qemu_macaddr_set_free(&nic->conf->macaddr); |
| 462 | |
Yuri Benditovich | d2abc56 | 2020-11-12 11:46:53 +0200 | [diff] [blame] | 463 | for (i = 0; i < queues; i++) { |
| 464 | NetClientState *nc = qemu_get_subqueue(nic, i); |
| 465 | /* If this is a peer NIC and peer has already been deleted, free it now. */ |
| 466 | if (nic->peer_deleted) { |
| 467 | qemu_free_net_client(nc->peer); |
| 468 | } else if (nc->peer) { |
| 469 | /* if there are RX packets pending, complete them */ |
| 470 | qemu_purge_queued_packets(nc->peer); |
Michael S. Tsirkin | a083a89 | 2010-09-20 18:08:41 +0200 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 474 | for (i = queues - 1; i >= 0; i--) { |
| 475 | NetClientState *nc = qemu_get_subqueue(nic, i); |
| 476 | |
| 477 | qemu_cleanup_net_client(nc); |
| 478 | qemu_free_net_client(nc); |
| 479 | } |
Jason Wang | f6b26cf | 2013-02-22 23:15:06 +0800 | [diff] [blame] | 480 | |
| 481 | g_free(nic); |
Jan Kiszka | 1a60952 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 484 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
| 485 | { |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 486 | NetClientState *nc; |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 487 | |
Stefan Hajnoczi | 9487899 | 2012-07-24 16:35:12 +0100 | [diff] [blame] | 488 | QTAILQ_FOREACH(nc, &net_clients, next) { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 489 | if (nc->info->type == NET_CLIENT_DRIVER_NIC) { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 490 | if (nc->queue_index == 0) { |
| 491 | func(qemu_get_nic(nc), opaque); |
| 492 | } |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
Mark McLoughlin | 57f9ef1 | 2009-11-25 18:49:31 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 497 | bool qemu_has_ufo(NetClientState *nc) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 498 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 499 | if (!nc || !nc->info->has_ufo) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 503 | return nc->info->has_ufo(nc); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Yuri Benditovich | f03e0cf | 2023-08-01 01:31:46 +0300 | [diff] [blame] | 506 | bool qemu_has_uso(NetClientState *nc) |
| 507 | { |
| 508 | if (!nc || !nc->info->has_uso) { |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | return nc->info->has_uso(nc); |
| 513 | } |
| 514 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 515 | bool qemu_has_vnet_hdr(NetClientState *nc) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 516 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 517 | if (!nc || !nc->info->has_vnet_hdr) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 518 | return false; |
| 519 | } |
| 520 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 521 | return nc->info->has_vnet_hdr(nc); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 524 | bool qemu_has_vnet_hdr_len(NetClientState *nc, int len) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 525 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 526 | if (!nc || !nc->info->has_vnet_hdr_len) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 527 | return false; |
| 528 | } |
| 529 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 530 | return nc->info->has_vnet_hdr_len(nc, len); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 533 | void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, |
Andrew Melnychenko | 2ab0ec3 | 2023-08-01 01:31:45 +0300 | [diff] [blame] | 534 | int ecn, int ufo, int uso4, int uso6) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 535 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 536 | if (!nc || !nc->info->set_offload) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
Andrew Melnychenko | 2ab0ec3 | 2023-08-01 01:31:45 +0300 | [diff] [blame] | 540 | nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo, uso4, uso6); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Akihiko Odaki | 481c523 | 2023-02-23 19:20:05 +0900 | [diff] [blame] | 543 | int qemu_get_vnet_hdr_len(NetClientState *nc) |
| 544 | { |
Akihiko Odaki | 76240dd | 2024-08-17 16:00:43 +0900 | [diff] [blame] | 545 | if (!nc) { |
| 546 | return 0; |
| 547 | } |
| 548 | |
Akihiko Odaki | 4b52d63 | 2024-04-28 16:00:45 +0900 | [diff] [blame] | 549 | return nc->vnet_hdr_len; |
Akihiko Odaki | 481c523 | 2023-02-23 19:20:05 +0900 | [diff] [blame] | 550 | } |
| 551 | |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 552 | void qemu_set_vnet_hdr_len(NetClientState *nc, int len) |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 553 | { |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 554 | if (!nc || !nc->info->set_vnet_hdr_len) { |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | |
Akihiko Odaki | a677537 | 2024-04-28 16:00:46 +0900 | [diff] [blame] | 558 | assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || |
| 559 | len == sizeof(struct virtio_net_hdr) || |
| 560 | len == sizeof(struct virtio_net_hdr_v1_hash)); |
| 561 | |
Zhang Chen | d6b732e | 2017-07-04 14:53:45 +0800 | [diff] [blame] | 562 | nc->vnet_hdr_len = len; |
Stefan Hajnoczi | d6085e3 | 2014-02-20 12:14:07 +0100 | [diff] [blame] | 563 | nc->info->set_vnet_hdr_len(nc, len); |
Vincenzo Maffione | 1f55ac4 | 2014-02-06 17:02:16 +0100 | [diff] [blame] | 564 | } |
| 565 | |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 566 | int qemu_set_vnet_le(NetClientState *nc, bool is_le) |
| 567 | { |
Marc-André Lureau | e03b568 | 2022-03-23 19:57:17 +0400 | [diff] [blame] | 568 | #if HOST_BIG_ENDIAN |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 569 | if (!nc || !nc->info->set_vnet_le) { |
| 570 | return -ENOSYS; |
| 571 | } |
| 572 | |
| 573 | return nc->info->set_vnet_le(nc, is_le); |
Michael S. Tsirkin | 052bd52 | 2015-10-14 12:11:27 +0300 | [diff] [blame] | 574 | #else |
| 575 | return 0; |
| 576 | #endif |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | int qemu_set_vnet_be(NetClientState *nc, bool is_be) |
| 580 | { |
Marc-André Lureau | e03b568 | 2022-03-23 19:57:17 +0400 | [diff] [blame] | 581 | #if HOST_BIG_ENDIAN |
Michael S. Tsirkin | 052bd52 | 2015-10-14 12:11:27 +0300 | [diff] [blame] | 582 | return 0; |
| 583 | #else |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 584 | if (!nc || !nc->info->set_vnet_be) { |
| 585 | return -ENOSYS; |
| 586 | } |
| 587 | |
| 588 | return nc->info->set_vnet_be(nc, is_be); |
Michael S. Tsirkin | 052bd52 | 2015-10-14 12:11:27 +0300 | [diff] [blame] | 589 | #endif |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Jason Wang | 705df54 | 2021-02-24 11:44:36 +0800 | [diff] [blame] | 592 | int qemu_can_receive_packet(NetClientState *nc) |
| 593 | { |
| 594 | if (nc->receive_disabled) { |
| 595 | return 0; |
| 596 | } else if (nc->info->can_receive && |
| 597 | !nc->info->can_receive(nc)) { |
| 598 | return 0; |
| 599 | } |
| 600 | return 1; |
| 601 | } |
| 602 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 603 | int qemu_can_send_packet(NetClientState *sender) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 604 | { |
zhanghailiang | e1d64c0 | 2014-08-26 16:06:17 +0800 | [diff] [blame] | 605 | int vm_running = runstate_is_running(); |
| 606 | |
| 607 | if (!vm_running) { |
| 608 | return 0; |
| 609 | } |
| 610 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 611 | if (!sender->peer) { |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 612 | return 1; |
| 613 | } |
| 614 | |
Jason Wang | 705df54 | 2021-02-24 11:44:36 +0800 | [diff] [blame] | 615 | return qemu_can_receive_packet(sender->peer); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 618 | static ssize_t filter_receive_iov(NetClientState *nc, |
| 619 | NetFilterDirection direction, |
| 620 | NetClientState *sender, |
| 621 | unsigned flags, |
| 622 | const struct iovec *iov, |
| 623 | int iovcnt, |
| 624 | NetPacketSent *sent_cb) |
| 625 | { |
| 626 | ssize_t ret = 0; |
| 627 | NetFilterState *nf = NULL; |
| 628 | |
Li Zhijian | 25aaadf | 2016-01-26 13:00:22 +0800 | [diff] [blame] | 629 | if (direction == NET_FILTER_DIRECTION_TX) { |
| 630 | QTAILQ_FOREACH(nf, &nc->filters, next) { |
| 631 | ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, |
| 632 | iovcnt, sent_cb); |
| 633 | if (ret) { |
| 634 | return ret; |
| 635 | } |
| 636 | } |
| 637 | } else { |
Paolo Bonzini | eae3eb3 | 2018-12-06 13:10:34 +0100 | [diff] [blame] | 638 | QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) { |
Li Zhijian | 25aaadf | 2016-01-26 13:00:22 +0800 | [diff] [blame] | 639 | ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, |
| 640 | iovcnt, sent_cb); |
| 641 | if (ret) { |
| 642 | return ret; |
| 643 | } |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | static ssize_t filter_receive(NetClientState *nc, |
| 651 | NetFilterDirection direction, |
| 652 | NetClientState *sender, |
| 653 | unsigned flags, |
| 654 | const uint8_t *data, |
| 655 | size_t size, |
| 656 | NetPacketSent *sent_cb) |
| 657 | { |
| 658 | struct iovec iov = { |
| 659 | .iov_base = (void *)data, |
| 660 | .iov_len = size |
| 661 | }; |
| 662 | |
| 663 | return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb); |
| 664 | } |
| 665 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 666 | void qemu_purge_queued_packets(NetClientState *nc) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 667 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 668 | if (!nc->peer) { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 672 | qemu_net_queue_purge(nc->peer->incoming_queue, nc); |
Mark McLoughlin | 8cad551 | 2009-06-18 18:21:29 +0100 | [diff] [blame] | 673 | } |
| 674 | |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 675 | void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge) |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 676 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 677 | nc->receive_disabled = 0; |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 678 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 679 | if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) { |
Luigi Rizzo | 199ee60 | 2013-02-05 17:53:31 +0100 | [diff] [blame] | 680 | if (net_hub_flush(nc->peer)) { |
| 681 | qemu_notify_event(); |
| 682 | } |
Luigi Rizzo | 199ee60 | 2013-02-05 17:53:31 +0100 | [diff] [blame] | 683 | } |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 684 | if (qemu_net_queue_flush(nc->incoming_queue)) { |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 685 | /* We emptied the queue successfully, signal to the IO thread to repoll |
| 686 | * the file descriptor (for tap, for example). |
| 687 | */ |
| 688 | qemu_notify_event(); |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 689 | } else if (purge) { |
| 690 | /* Unable to empty the queue, purge remaining packets */ |
Jason Wang | 5fe19fb | 2020-05-11 12:04:53 +0800 | [diff] [blame] | 691 | qemu_net_queue_purge(nc->incoming_queue, nc->peer); |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 692 | } |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 695 | void qemu_flush_queued_packets(NetClientState *nc) |
| 696 | { |
| 697 | qemu_flush_or_purge_queued_packets(nc, false); |
| 698 | } |
| 699 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 700 | static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender, |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 701 | unsigned flags, |
| 702 | const uint8_t *buf, int size, |
| 703 | NetPacketSent *sent_cb) |
aliguori | 764a4d1 | 2009-04-21 19:56:41 +0000 | [diff] [blame] | 704 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 705 | NetQueue *queue; |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 706 | int ret; |
Mark McLoughlin | e94667b | 2009-04-29 11:48:12 +0100 | [diff] [blame] | 707 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 708 | #ifdef DEBUG_NET |
Mark McLoughlin | d80b9fc | 2009-10-08 19:58:24 +0100 | [diff] [blame] | 709 | printf("qemu_send_packet_async:\n"); |
Philippe Mathieu-Daudé | b42581f | 2020-08-22 20:09:50 +0200 | [diff] [blame] | 710 | qemu_hexdump(stdout, "net", buf, size); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 711 | #endif |
| 712 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 713 | if (sender->link_down || !sender->peer) { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 714 | return size; |
| 715 | } |
| 716 | |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 717 | /* Let filters handle the packet first */ |
| 718 | ret = filter_receive(sender, NET_FILTER_DIRECTION_TX, |
| 719 | sender, flags, buf, size, sent_cb); |
| 720 | if (ret) { |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX, |
| 725 | sender, flags, buf, size, sent_cb); |
| 726 | if (ret) { |
| 727 | return ret; |
| 728 | } |
| 729 | |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 730 | queue = sender->peer->incoming_queue; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 731 | |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 732 | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); |
| 733 | } |
| 734 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 735 | ssize_t qemu_send_packet_async(NetClientState *sender, |
Mark McLoughlin | ca77d17 | 2009-10-22 17:43:41 +0100 | [diff] [blame] | 736 | const uint8_t *buf, int size, |
| 737 | NetPacketSent *sent_cb) |
| 738 | { |
| 739 | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, |
| 740 | buf, size, sent_cb); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 743 | 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] | 744 | { |
Marc-André Lureau | 625a526 | 2019-01-17 15:43:54 +0400 | [diff] [blame] | 745 | return qemu_send_packet_async(nc, buf, size, NULL); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Jason Wang | 705df54 | 2021-02-24 11:44:36 +0800 | [diff] [blame] | 748 | ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size) |
| 749 | { |
| 750 | if (!qemu_can_receive_packet(nc)) { |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | return qemu_net_queue_receive(nc->incoming_queue, buf, size); |
| 755 | } |
| 756 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 757 | 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] | 758 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 759 | 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] | 760 | buf, size, NULL); |
| 761 | } |
| 762 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 763 | static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov, |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 764 | int iovcnt, unsigned flags) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 765 | { |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 766 | uint8_t *buf = NULL; |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 767 | uint8_t *buffer; |
Benjamin Poirier | ce05366 | 2011-02-23 19:57:21 -0500 | [diff] [blame] | 768 | size_t offset; |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 769 | ssize_t ret; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 770 | |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 771 | if (iovcnt == 1) { |
| 772 | buffer = iov[0].iov_base; |
| 773 | offset = iov[0].iov_len; |
| 774 | } else { |
Peter Lieven | 47f9f15 | 2016-06-30 11:49:40 +0200 | [diff] [blame] | 775 | offset = iov_size(iov, iovcnt); |
| 776 | if (offset > NET_BUFSIZE) { |
| 777 | return -1; |
| 778 | } |
| 779 | buf = g_malloc(offset); |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 780 | buffer = buf; |
Peter Lieven | 47f9f15 | 2016-06-30 11:49:40 +0200 | [diff] [blame] | 781 | offset = iov_to_buf(iov, iovcnt, 0, buf, offset); |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 782 | } |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 783 | |
Akihiko Odaki | b9ad513 | 2024-04-28 16:00:47 +0900 | [diff] [blame] | 784 | ret = nc->info->receive(nc, buffer, offset); |
Pooja Dhannawat | 74044c8 | 2016-03-28 18:04:11 +0530 | [diff] [blame] | 785 | |
| 786 | g_free(buf); |
| 787 | return ret; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 790 | static ssize_t qemu_deliver_packet_iov(NetClientState *sender, |
| 791 | unsigned flags, |
| 792 | const struct iovec *iov, |
| 793 | int iovcnt, |
| 794 | void *opaque) |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 795 | { |
Akihiko Odaki | 9050f97 | 2023-06-01 12:18:59 +0900 | [diff] [blame] | 796 | MemReentrancyGuard *owned_reentrancy_guard; |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 797 | NetClientState *nc = opaque; |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 798 | int ret; |
Akihiko Odaki | b9ad513 | 2024-04-28 16:00:47 +0900 | [diff] [blame] | 799 | struct virtio_net_hdr_v1_hash vnet_hdr = { }; |
| 800 | g_autofree struct iovec *iov_copy = NULL; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 801 | |
Jason Wang | 1592a99 | 2018-05-30 13:16:36 +0800 | [diff] [blame] | 802 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 803 | if (nc->link_down) { |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 804 | return iov_size(iov, iovcnt); |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 807 | if (nc->receive_disabled) { |
| 808 | return 0; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 809 | } |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 810 | |
Akihiko Odaki | 9050f97 | 2023-06-01 12:18:59 +0900 | [diff] [blame] | 811 | if (nc->info->type != NET_CLIENT_DRIVER_NIC || |
| 812 | qemu_get_nic(nc)->reentrancy_guard->engaged_in_io) { |
| 813 | owned_reentrancy_guard = NULL; |
| 814 | } else { |
| 815 | owned_reentrancy_guard = qemu_get_nic(nc)->reentrancy_guard; |
| 816 | owned_reentrancy_guard->engaged_in_io = true; |
| 817 | } |
| 818 | |
Akihiko Odaki | b9ad513 | 2024-04-28 16:00:47 +0900 | [diff] [blame] | 819 | if ((flags & QEMU_NET_PACKET_FLAG_RAW) && nc->vnet_hdr_len) { |
| 820 | iov_copy = g_new(struct iovec, iovcnt + 1); |
| 821 | iov_copy[0].iov_base = &vnet_hdr; |
| 822 | iov_copy[0].iov_len = nc->vnet_hdr_len; |
| 823 | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); |
| 824 | iov = iov_copy; |
| 825 | } |
| 826 | |
| 827 | if (nc->info->receive_iov) { |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 828 | ret = nc->info->receive_iov(nc, iov, iovcnt); |
| 829 | } else { |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 830 | ret = nc_sendv_compat(nc, iov, iovcnt, flags); |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 831 | } |
| 832 | |
Akihiko Odaki | 9050f97 | 2023-06-01 12:18:59 +0900 | [diff] [blame] | 833 | if (owned_reentrancy_guard) { |
| 834 | owned_reentrancy_guard->engaged_in_io = false; |
| 835 | } |
| 836 | |
Stefan Hajnoczi | c67f5dc | 2012-08-17 21:16:42 +0100 | [diff] [blame] | 837 | if (ret == 0) { |
| 838 | nc->receive_disabled = 1; |
| 839 | } |
| 840 | |
| 841 | return ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 844 | ssize_t qemu_sendv_packet_async(NetClientState *sender, |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 845 | const struct iovec *iov, int iovcnt, |
| 846 | NetPacketSent *sent_cb) |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 847 | { |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 848 | NetQueue *queue; |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 849 | size_t size = iov_size(iov, iovcnt); |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 850 | int ret; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 851 | |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 852 | if (size > NET_BUFSIZE) { |
| 853 | return size; |
| 854 | } |
| 855 | |
Stefan Hajnoczi | a005d07 | 2012-07-24 16:35:11 +0100 | [diff] [blame] | 856 | if (sender->link_down || !sender->peer) { |
Jason Wang | 25c01bd | 2018-12-04 11:53:43 +0800 | [diff] [blame] | 857 | return size; |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Yang Hongyang | e64c770 | 2015-10-07 11:52:15 +0800 | [diff] [blame] | 860 | /* Let filters handle the packet first */ |
| 861 | ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender, |
| 862 | QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); |
| 863 | if (ret) { |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender, |
| 868 | QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); |
| 869 | if (ret) { |
| 870 | return ret; |
| 871 | } |
| 872 | |
Jan Kiszka | 067404b | 2013-08-02 21:47:08 +0200 | [diff] [blame] | 873 | queue = sender->peer->incoming_queue; |
Mark McLoughlin | 9a6ecb3 | 2009-10-08 19:58:32 +0100 | [diff] [blame] | 874 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 875 | return qemu_net_queue_send_iov(queue, sender, |
| 876 | QEMU_NET_PACKET_FLAG_NONE, |
| 877 | iov, iovcnt, sent_cb); |
aliguori | fbe78f4 | 2008-12-17 19:13:11 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 880 | ssize_t |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 881 | qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt) |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 882 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 883 | return qemu_sendv_packet_async(nc, iov, iovcnt, NULL); |
Mark McLoughlin | f3b6c7f | 2009-04-29 12:15:26 +0100 | [diff] [blame] | 884 | } |
| 885 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 886 | NetClientState *qemu_find_netdev(const char *id) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 887 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 888 | NetClientState *nc; |
Mark McLoughlin | 5610c3a | 2009-10-08 19:58:23 +0100 | [diff] [blame] | 889 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 890 | QTAILQ_FOREACH(nc, &net_clients, next) { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 891 | if (nc->info->type == NET_CLIENT_DRIVER_NIC) |
Markus Armbruster | 85dde9a | 2011-06-16 18:45:37 +0200 | [diff] [blame] | 892 | continue; |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 893 | if (!strcmp(nc->name, id)) { |
| 894 | return nc; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | return NULL; |
| 899 | } |
| 900 | |
Jason Wang | 6c51ae7 | 2013-01-30 19:12:25 +0800 | [diff] [blame] | 901 | int qemu_find_net_clients_except(const char *id, NetClientState **ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 902 | NetClientDriver type, int max) |
Jason Wang | 6c51ae7 | 2013-01-30 19:12:25 +0800 | [diff] [blame] | 903 | { |
| 904 | NetClientState *nc; |
| 905 | int ret = 0; |
| 906 | |
| 907 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 908 | if (nc->info->type == type) { |
| 909 | continue; |
| 910 | } |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 911 | if (!id || !strcmp(nc->name, id)) { |
Jason Wang | 6c51ae7 | 2013-01-30 19:12:25 +0800 | [diff] [blame] | 912 | if (ret < max) { |
| 913 | ncs[ret] = nc; |
| 914 | } |
| 915 | ret++; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | return ret; |
| 920 | } |
| 921 | |
aliguori | 7697079 | 2009-02-11 15:20:03 +0000 | [diff] [blame] | 922 | static int nic_get_free_idx(void) |
| 923 | { |
| 924 | int index; |
| 925 | |
| 926 | for (index = 0; index < MAX_NICS; index++) |
| 927 | if (!nd_table[index].used) |
| 928 | return index; |
| 929 | return -1; |
| 930 | } |
| 931 | |
Thomas Huth | c6941b3 | 2022-11-10 13:52:22 +0100 | [diff] [blame] | 932 | GPtrArray *qemu_get_nic_models(const char *device_type) |
| 933 | { |
| 934 | GPtrArray *nic_models = g_ptr_array_new(); |
| 935 | GSList *list = object_class_get_list_sorted(device_type, false); |
| 936 | |
| 937 | while (list) { |
| 938 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data, |
| 939 | TYPE_DEVICE); |
| 940 | GSList *next; |
| 941 | if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) && |
| 942 | dc->user_creatable) { |
| 943 | const char *name = object_class_get_name(list->data); |
| 944 | /* |
| 945 | * A network device might also be something else than a NIC, see |
| 946 | * e.g. the "rocker" device. Thus we have to look for the "netdev" |
| 947 | * property, too. Unfortunately, some devices like virtio-net only |
| 948 | * create this property during instance_init, so we have to create |
| 949 | * a temporary instance here to be able to check it. |
| 950 | */ |
| 951 | Object *obj = object_new_with_class(OBJECT_CLASS(dc)); |
| 952 | if (object_property_find(obj, "netdev")) { |
| 953 | g_ptr_array_add(nic_models, (gpointer)name); |
| 954 | } |
| 955 | object_unref(obj); |
| 956 | } |
| 957 | next = list->next; |
| 958 | g_slist_free_1(list); |
| 959 | list = next; |
| 960 | } |
| 961 | g_ptr_array_add(nic_models, NULL); |
| 962 | |
| 963 | return nic_models; |
| 964 | } |
| 965 | |
Kővágó, Zoltán | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 966 | static int net_init_nic(const Netdev *netdev, const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 967 | NetClientState *peer, Error **errp) |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 968 | { |
| 969 | int idx; |
| 970 | NICInfo *nd; |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 971 | const NetLegacyNicOptions *nic; |
| 972 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 973 | assert(netdev->type == NET_CLIENT_DRIVER_NIC); |
| 974 | nic = &netdev->u.nic; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 975 | |
| 976 | idx = nic_get_free_idx(); |
| 977 | if (idx == -1 || nb_nics >= MAX_NICS) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 978 | error_setg(errp, "too many NICs"); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 979 | return -1; |
| 980 | } |
| 981 | |
| 982 | nd = &nd_table[idx]; |
| 983 | |
| 984 | memset(nd, 0, sizeof(*nd)); |
| 985 | |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 986 | if (nic->netdev) { |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 987 | nd->netdev = qemu_find_netdev(nic->netdev); |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 988 | if (!nd->netdev) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 989 | error_setg(errp, "netdev '%s' not found", nic->netdev); |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 990 | return -1; |
| 991 | } |
| 992 | } else { |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 993 | assert(peer); |
| 994 | nd->netdev = peer; |
Mark McLoughlin | 5869c4d | 2009-10-08 19:58:29 +0100 | [diff] [blame] | 995 | } |
Markus Armbruster | c64f50d | 2013-01-22 11:07:57 +0100 | [diff] [blame] | 996 | nd->name = g_strdup(name); |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 997 | if (nic->model) { |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 998 | nd->model = g_strdup(nic->model); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 999 | } |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1000 | if (nic->addr) { |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 1001 | nd->devaddr = g_strdup(nic->addr); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1004 | if (nic->macaddr && |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 1005 | net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 1006 | error_setg(errp, "invalid syntax for ethernet address"); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1007 | return -1; |
| 1008 | } |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1009 | if (nic->macaddr && |
Dmitry Krivenok | d60b20c | 2013-10-21 12:08:44 +0400 | [diff] [blame] | 1010 | is_multicast_ether_addr(nd->macaddr.a)) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 1011 | error_setg(errp, |
| 1012 | "NIC cannot have multicast MAC address (odd 1st byte)"); |
Dmitry Krivenok | d60b20c | 2013-10-21 12:08:44 +0400 | [diff] [blame] | 1013 | return -1; |
| 1014 | } |
Jan Kiszka | 6eed185 | 2011-07-20 12:20:22 +0200 | [diff] [blame] | 1015 | qemu_macaddr_default_if_unset(&nd->macaddr); |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1016 | |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 1017 | if (nic->has_vectors) { |
| 1018 | if (nic->vectors > 0x7ffffff) { |
Markus Armbruster | 6630886 | 2015-05-15 13:58:51 +0200 | [diff] [blame] | 1019 | error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors); |
Laszlo Ersek | 2456f36 | 2012-07-17 16:17:14 +0200 | [diff] [blame] | 1020 | return -1; |
| 1021 | } |
| 1022 | nd->nvectors = nic->vectors; |
| 1023 | } else { |
| 1024 | nd->nvectors = DEV_NVECTORS_UNSPECIFIED; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | nd->used = 1; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1028 | nb_nics++; |
| 1029 | |
| 1030 | return idx; |
| 1031 | } |
| 1032 | |
David Woodhouse | 2cdeca0 | 2023-10-21 23:09:38 +0100 | [diff] [blame] | 1033 | static gboolean add_nic_result(gpointer key, gpointer value, gpointer user_data) |
| 1034 | { |
| 1035 | GPtrArray *results = user_data; |
| 1036 | GPtrArray *alias_list = value; |
| 1037 | const char *model = key; |
| 1038 | char *result; |
| 1039 | |
| 1040 | if (!alias_list) { |
| 1041 | result = g_strdup(model); |
| 1042 | } else { |
| 1043 | GString *result_str = g_string_new(model); |
| 1044 | int i; |
| 1045 | |
| 1046 | g_string_append(result_str, " (aka "); |
| 1047 | for (i = 0; i < alias_list->len; i++) { |
| 1048 | if (i) { |
| 1049 | g_string_append(result_str, ", "); |
| 1050 | } |
| 1051 | g_string_append(result_str, alias_list->pdata[i]); |
| 1052 | } |
| 1053 | g_string_append(result_str, ")"); |
| 1054 | result = result_str->str; |
| 1055 | g_string_free(result_str, false); |
| 1056 | g_ptr_array_unref(alias_list); |
| 1057 | } |
| 1058 | g_ptr_array_add(results, result); |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
| 1062 | static int model_cmp(char **a, char **b) |
| 1063 | { |
| 1064 | return strcmp(*a, *b); |
| 1065 | } |
| 1066 | |
| 1067 | static void show_nic_models(void) |
| 1068 | { |
| 1069 | GPtrArray *results = g_ptr_array_new(); |
| 1070 | int i; |
| 1071 | |
| 1072 | g_hash_table_foreach_remove(nic_model_help, add_nic_result, results); |
| 1073 | g_ptr_array_sort(results, (GCompareFunc)model_cmp); |
| 1074 | |
| 1075 | printf("Available NIC models for this configuration:\n"); |
| 1076 | for (i = 0 ; i < results->len; i++) { |
| 1077 | printf("%s\n", (char *)results->pdata[i]); |
| 1078 | } |
| 1079 | g_hash_table_unref(nic_model_help); |
| 1080 | nic_model_help = NULL; |
| 1081 | } |
| 1082 | |
| 1083 | static void add_nic_model_help(const char *model, const char *alias) |
| 1084 | { |
| 1085 | GPtrArray *alias_list = NULL; |
| 1086 | |
| 1087 | if (g_hash_table_lookup_extended(nic_model_help, model, NULL, |
| 1088 | (gpointer *)&alias_list)) { |
| 1089 | /* Already exists, no alias to add: return */ |
| 1090 | if (!alias) { |
| 1091 | return; |
| 1092 | } |
| 1093 | if (alias_list) { |
| 1094 | /* Check if this alias is already in the list. Add if not. */ |
| 1095 | if (!g_ptr_array_find_with_equal_func(alias_list, alias, |
| 1096 | g_str_equal, NULL)) { |
| 1097 | g_ptr_array_add(alias_list, g_strdup(alias)); |
| 1098 | } |
| 1099 | return; |
| 1100 | } |
| 1101 | } |
| 1102 | /* Either this model wasn't in the list already, or a first alias added */ |
| 1103 | if (alias) { |
| 1104 | alias_list = g_ptr_array_new(); |
| 1105 | g_ptr_array_set_free_func(alias_list, g_free); |
| 1106 | g_ptr_array_add(alias_list, g_strdup(alias)); |
| 1107 | } |
| 1108 | g_hash_table_replace(nic_model_help, g_strdup(model), alias_list); |
| 1109 | } |
| 1110 | |
David Woodhouse | 93e9d73 | 2023-10-19 21:28:29 +0100 | [diff] [blame] | 1111 | NICInfo *qemu_find_nic_info(const char *typename, bool match_default, |
| 1112 | const char *alias) |
| 1113 | { |
| 1114 | NICInfo *nd; |
| 1115 | int i; |
| 1116 | |
David Woodhouse | 2cdeca0 | 2023-10-21 23:09:38 +0100 | [diff] [blame] | 1117 | if (nic_model_help) { |
| 1118 | add_nic_model_help(typename, alias); |
| 1119 | } |
| 1120 | |
David Woodhouse | 93e9d73 | 2023-10-19 21:28:29 +0100 | [diff] [blame] | 1121 | for (i = 0; i < nb_nics; i++) { |
| 1122 | nd = &nd_table[i]; |
| 1123 | |
| 1124 | if (!nd->used || nd->instantiated) { |
| 1125 | continue; |
| 1126 | } |
| 1127 | |
| 1128 | if ((match_default && !nd->model) || !g_strcmp0(nd->model, typename) |
| 1129 | || (alias && !g_strcmp0(nd->model, alias))) { |
| 1130 | return nd; |
| 1131 | } |
| 1132 | } |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | |
David Woodhouse | 64f75f5 | 2024-07-09 13:34:44 +0100 | [diff] [blame] | 1136 | static bool is_nic_model_help_option(const char *model) |
| 1137 | { |
| 1138 | if (model && is_help_option(model)) { |
| 1139 | /* |
| 1140 | * Trigger the help output by instantiating the hash table which |
| 1141 | * will gather tha available models as they get registered. |
| 1142 | */ |
| 1143 | if (!nic_model_help) { |
| 1144 | nic_model_help = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 1145 | g_free, NULL); |
| 1146 | } |
| 1147 | return true; |
| 1148 | } |
| 1149 | return false; |
| 1150 | } |
David Woodhouse | 93e9d73 | 2023-10-19 21:28:29 +0100 | [diff] [blame] | 1151 | |
| 1152 | /* "I have created a device. Please configure it if you can" */ |
| 1153 | bool qemu_configure_nic_device(DeviceState *dev, bool match_default, |
| 1154 | const char *alias) |
| 1155 | { |
| 1156 | NICInfo *nd = qemu_find_nic_info(object_get_typename(OBJECT(dev)), |
| 1157 | match_default, alias); |
| 1158 | |
| 1159 | if (nd) { |
| 1160 | qdev_set_nic_properties(dev, nd); |
| 1161 | return true; |
| 1162 | } |
| 1163 | return false; |
| 1164 | } |
| 1165 | |
| 1166 | /* "Please create a device, if you have a configuration for it" */ |
| 1167 | DeviceState *qemu_create_nic_device(const char *typename, bool match_default, |
| 1168 | const char *alias) |
| 1169 | { |
| 1170 | NICInfo *nd = qemu_find_nic_info(typename, match_default, alias); |
| 1171 | DeviceState *dev; |
| 1172 | |
| 1173 | if (!nd) { |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | dev = qdev_new(typename); |
| 1178 | qdev_set_nic_properties(dev, nd); |
| 1179 | return dev; |
| 1180 | } |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1181 | |
David Woodhouse | 93125e4 | 2023-10-22 09:13:41 +0100 | [diff] [blame] | 1182 | void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type, |
| 1183 | const char *default_model, |
| 1184 | const char *alias, const char *alias_target) |
| 1185 | { |
| 1186 | GPtrArray *nic_models = qemu_get_nic_models(parent_type); |
| 1187 | const char *model; |
| 1188 | DeviceState *dev; |
| 1189 | NICInfo *nd; |
| 1190 | int i; |
| 1191 | |
| 1192 | if (nic_model_help) { |
| 1193 | if (alias_target) { |
| 1194 | add_nic_model_help(alias_target, alias); |
| 1195 | } |
| 1196 | for (i = 0; i < nic_models->len - 1; i++) { |
| 1197 | add_nic_model_help(nic_models->pdata[i], NULL); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | /* Drop the NULL terminator which would make g_str_equal() unhappy */ |
| 1202 | nic_models->len--; |
| 1203 | |
| 1204 | for (i = 0; i < nb_nics; i++) { |
| 1205 | nd = &nd_table[i]; |
| 1206 | |
| 1207 | if (!nd->used || nd->instantiated) { |
| 1208 | continue; |
| 1209 | } |
| 1210 | |
| 1211 | model = nd->model ? nd->model : default_model; |
| 1212 | if (!model) { |
| 1213 | continue; |
| 1214 | } |
| 1215 | |
| 1216 | /* Each bus type is allowed *one* substitution */ |
| 1217 | if (g_str_equal(model, alias)) { |
| 1218 | model = alias_target; |
| 1219 | } |
| 1220 | |
| 1221 | if (!g_ptr_array_find_with_equal_func(nic_models, model, |
| 1222 | g_str_equal, NULL)) { |
| 1223 | /* This NIC does not live on this bus. */ |
| 1224 | continue; |
| 1225 | } |
| 1226 | |
| 1227 | dev = qdev_new(model); |
| 1228 | qdev_set_nic_properties(dev, nd); |
| 1229 | qdev_realize_and_unref(dev, bus, &error_fatal); |
| 1230 | } |
| 1231 | |
| 1232 | g_ptr_array_free(nic_models, true); |
| 1233 | } |
| 1234 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1235 | 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] | 1236 | const Netdev *netdev, |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1237 | const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 1238 | NetClientState *peer, Error **errp) = { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1239 | [NET_CLIENT_DRIVER_NIC] = net_init_nic, |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 1240 | #ifdef CONFIG_SLIRP |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1241 | [NET_CLIENT_DRIVER_USER] = net_init_slirp, |
Mark McLoughlin | ec302ff | 2009-10-06 12:17:07 +0100 | [diff] [blame] | 1242 | #endif |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1243 | [NET_CLIENT_DRIVER_TAP] = net_init_tap, |
| 1244 | [NET_CLIENT_DRIVER_SOCKET] = net_init_socket, |
Laurent Vivier | 5166fe0 | 2022-10-21 11:09:11 +0200 | [diff] [blame] | 1245 | [NET_CLIENT_DRIVER_STREAM] = net_init_stream, |
| 1246 | [NET_CLIENT_DRIVER_DGRAM] = net_init_dgram, |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 1247 | #ifdef CONFIG_VDE |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1248 | [NET_CLIENT_DRIVER_VDE] = net_init_vde, |
Mark McLoughlin | dd51058 | 2009-10-06 12:17:10 +0100 | [diff] [blame] | 1249 | #endif |
Vincenzo Maffione | 5895213 | 2013-11-06 11:44:06 +0100 | [diff] [blame] | 1250 | #ifdef CONFIG_NETMAP |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1251 | [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap, |
Vincenzo Maffione | 5895213 | 2013-11-06 11:44:06 +0100 | [diff] [blame] | 1252 | #endif |
Ilya Maximets | cb039ef | 2023-09-13 20:34:37 +0200 | [diff] [blame] | 1253 | #ifdef CONFIG_AF_XDP |
| 1254 | [NET_CLIENT_DRIVER_AF_XDP] = net_init_af_xdp, |
| 1255 | #endif |
Stefan Weil | 2944e4e | 2012-02-04 09:24:46 +0100 | [diff] [blame] | 1256 | #ifdef CONFIG_NET_BRIDGE |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1257 | [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge, |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1258 | #endif |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1259 | [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport, |
Paolo Bonzini | 56f41de | 2019-02-14 18:35:49 +0100 | [diff] [blame] | 1260 | #ifdef CONFIG_VHOST_NET_USER |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1261 | [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user, |
Nikolay Nikolaev | 03ce574 | 2014-06-10 13:02:16 +0300 | [diff] [blame] | 1262 | #endif |
Cindy Lu | 1e0a84e | 2020-07-01 22:55:38 +0800 | [diff] [blame] | 1263 | #ifdef CONFIG_VHOST_NET_VDPA |
| 1264 | [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa, |
| 1265 | #endif |
Gonglei | 015a33b | 2014-07-01 20:58:08 +0800 | [diff] [blame] | 1266 | #ifdef CONFIG_L2TPV3 |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1267 | [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3, |
Anton Ivanov | 3fb69aa | 2014-06-20 10:34:41 +0100 | [diff] [blame] | 1268 | #endif |
Vladislav Yaroshchuk | 81ad296 | 2022-03-17 20:28:34 +0300 | [diff] [blame] | 1269 | #ifdef CONFIG_VMNET |
| 1270 | [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host, |
| 1271 | [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared, |
| 1272 | [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged, |
| 1273 | #endif /* CONFIG_VMNET */ |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1274 | }; |
| 1275 | |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1276 | |
Thomas Huth | 71830d8 | 2020-05-18 20:01:03 +0200 | [diff] [blame] | 1277 | static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp) |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1278 | { |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 1279 | NetClientState *peer = NULL; |
Markus Armbruster | 831734c | 2020-11-25 11:02:20 +0100 | [diff] [blame] | 1280 | NetClientState *nc; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1281 | |
Markus Armbruster | 5294e2c | 2010-03-25 17:22:38 +0100 | [diff] [blame] | 1282 | if (is_netdev) { |
Thomas Huth | 857d208 | 2018-02-21 11:18:34 +0100 | [diff] [blame] | 1283 | if (netdev->type == NET_CLIENT_DRIVER_NIC || |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1284 | !net_client_init_fun[netdev->type]) { |
Daniel P. Berrangé | 7d0e12a | 2022-10-03 11:06:12 +0100 | [diff] [blame] | 1285 | error_setg(errp, "network backend '%s' is not compiled into this binary", |
| 1286 | NetClientDriver_str(netdev->type)); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1287 | return -1; |
| 1288 | } |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1289 | } else { |
Thomas Huth | 71830d8 | 2020-05-18 20:01:03 +0200 | [diff] [blame] | 1290 | if (netdev->type == NET_CLIENT_DRIVER_NONE) { |
Stefan Hajnoczi | 1e81aba | 2015-05-27 17:16:52 +0100 | [diff] [blame] | 1291 | return 0; /* nothing to do */ |
Markus Armbruster | ca7eb18 | 2015-05-15 13:58:49 +0200 | [diff] [blame] | 1292 | } |
Daniel P. Berrangé | 7d0e12a | 2022-10-03 11:06:12 +0100 | [diff] [blame] | 1293 | if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) { |
| 1294 | error_setg(errp, "network backend '%s' is only supported with -netdev/-nic", |
| 1295 | NetClientDriver_str(netdev->type)); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | |
| 1299 | if (!net_client_init_fun[netdev->type]) { |
| 1300 | error_setg(errp, "network backend '%s' is not compiled into this binary", |
| 1301 | NetClientDriver_str(netdev->type)); |
Stefan Hajnoczi | d139e9a | 2015-05-27 17:16:50 +0100 | [diff] [blame] | 1302 | return -1; |
| 1303 | } |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1304 | |
Thomas Huth | af1a5c3 | 2018-04-30 20:02:23 +0200 | [diff] [blame] | 1305 | /* 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] | 1306 | if (netdev->type != NET_CLIENT_DRIVER_NIC || |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1307 | !netdev->u.nic.netdev) { |
Thomas Huth | af1a5c3 | 2018-04-30 20:02:23 +0200 | [diff] [blame] | 1308 | peer = net_hub_add_port(0, NULL, NULL); |
Thomas Huth | a2dbe13 | 2017-01-24 10:42:49 +0100 | [diff] [blame] | 1309 | } |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 1310 | } |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1311 | |
Markus Armbruster | 831734c | 2020-11-25 11:02:20 +0100 | [diff] [blame] | 1312 | nc = qemu_find_netdev(netdev->id); |
| 1313 | if (nc) { |
| 1314 | error_setg(errp, "Duplicate ID '%s'", netdev->id); |
| 1315 | return -1; |
| 1316 | } |
| 1317 | |
Thomas Huth | 9d903f3 | 2020-05-18 20:01:02 +0200 | [diff] [blame] | 1318 | if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) { |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 1319 | /* FIXME drop when all init functions store an Error */ |
| 1320 | if (errp && !*errp) { |
Markus Armbruster | f820af8 | 2020-11-13 09:26:18 +0100 | [diff] [blame] | 1321 | error_setg(errp, "Device '%s' could not be initialized", |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 1322 | NetClientDriver_str(netdev->type)); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1323 | } |
Stefan Hajnoczi | 4ef0def | 2015-05-27 17:16:51 +0100 | [diff] [blame] | 1324 | return -1; |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1325 | } |
Eric Blake | 08712fc | 2020-03-17 15:17:11 -0500 | [diff] [blame] | 1326 | |
| 1327 | if (is_netdev) { |
Eric Blake | 08712fc | 2020-03-17 15:17:11 -0500 | [diff] [blame] | 1328 | nc = qemu_find_netdev(netdev->id); |
| 1329 | assert(nc); |
| 1330 | nc->is_netdev = true; |
| 1331 | } |
| 1332 | |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1333 | return 0; |
Mark McLoughlin | f83c6e1 | 2009-10-06 12:17:06 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1336 | void show_netdevs(void) |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1337 | { |
| 1338 | int idx; |
| 1339 | const char *available_netdevs[] = { |
| 1340 | "socket", |
Laurent Vivier | 5166fe0 | 2022-10-21 11:09:11 +0200 | [diff] [blame] | 1341 | "stream", |
| 1342 | "dgram", |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1343 | "hubport", |
| 1344 | "tap", |
| 1345 | #ifdef CONFIG_SLIRP |
| 1346 | "user", |
| 1347 | #endif |
| 1348 | #ifdef CONFIG_L2TPV3 |
| 1349 | "l2tpv3", |
| 1350 | #endif |
| 1351 | #ifdef CONFIG_VDE |
| 1352 | "vde", |
| 1353 | #endif |
| 1354 | #ifdef CONFIG_NET_BRIDGE |
| 1355 | "bridge", |
| 1356 | #endif |
| 1357 | #ifdef CONFIG_NETMAP |
| 1358 | "netmap", |
| 1359 | #endif |
Ilya Maximets | cb039ef | 2023-09-13 20:34:37 +0200 | [diff] [blame] | 1360 | #ifdef CONFIG_AF_XDP |
| 1361 | "af-xdp", |
| 1362 | #endif |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1363 | #ifdef CONFIG_POSIX |
| 1364 | "vhost-user", |
| 1365 | #endif |
Cindy Lu | 1bc211a | 2020-10-16 11:09:09 +0800 | [diff] [blame] | 1366 | #ifdef CONFIG_VHOST_VDPA |
| 1367 | "vhost-vdpa", |
| 1368 | #endif |
Vladislav Yaroshchuk | 81ad296 | 2022-03-17 20:28:34 +0300 | [diff] [blame] | 1369 | #ifdef CONFIG_VMNET |
| 1370 | "vmnet-host", |
| 1371 | "vmnet-shared", |
| 1372 | "vmnet-bridged", |
| 1373 | #endif |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1374 | }; |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1375 | |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1376 | qemu_printf("Available netdev backend types:\n"); |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1377 | for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) { |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1378 | qemu_printf("%s\n", available_netdevs[idx]); |
Thomas Huth | 547203e | 2018-02-21 11:18:31 +0100 | [diff] [blame] | 1379 | } |
| 1380 | } |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1381 | |
Thomas Huth | aa09a48 | 2018-02-21 11:18:33 +0100 | [diff] [blame] | 1382 | static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp) |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1383 | { |
Stefano Garzarella | c1112b2 | 2019-05-17 15:47:46 +0200 | [diff] [blame] | 1384 | gchar **substrings = NULL; |
Thomas Huth | 71830d8 | 2020-05-18 20:01:03 +0200 | [diff] [blame] | 1385 | Netdev *object = NULL; |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1386 | int ret = -1; |
Eric Blake | 09204ea | 2016-06-09 10:48:36 -0600 | [diff] [blame] | 1387 | Visitor *v = opts_visitor_new(opts); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1388 | |
Markus Armbruster | 0a4a151 | 2023-10-31 12:10:57 +0100 | [diff] [blame] | 1389 | /* Parse convenience option format ipv6-net=fec0::0[/64] */ |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1390 | const char *ip6_net = qemu_opt_get(opts, "ipv6-net"); |
Lin Ma | 8b43f96 | 2018-06-11 17:23:05 +0800 | [diff] [blame] | 1391 | |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1392 | if (ip6_net) { |
| 1393 | char *prefix_addr; |
| 1394 | unsigned long prefix_len = 64; /* Default 64bit prefix length. */ |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1395 | |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1396 | substrings = g_strsplit(ip6_net, "/", 2); |
| 1397 | if (!substrings || !substrings[0]) { |
| 1398 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net", |
| 1399 | "a valid IPv6 prefix"); |
| 1400 | goto out; |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1401 | } |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1402 | |
| 1403 | prefix_addr = substrings[0]; |
| 1404 | |
| 1405 | /* Handle user-specified prefix length. */ |
| 1406 | if (substrings[1] && |
| 1407 | qemu_strtoul(substrings[1], NULL, 10, &prefix_len)) |
| 1408 | { |
Markus Armbruster | 0a4a151 | 2023-10-31 12:10:57 +0100 | [diff] [blame] | 1409 | error_setg(errp, |
| 1410 | "parameter 'ipv6-net' expects a number after '/'"); |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1411 | goto out; |
| 1412 | } |
| 1413 | |
| 1414 | qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort); |
| 1415 | qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len, |
| 1416 | &error_abort); |
| 1417 | qemu_opt_unset(opts, "ipv6-net"); |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 1418 | } |
| 1419 | |
Thomas Huth | 71830d8 | 2020-05-18 20:01:03 +0200 | [diff] [blame] | 1420 | /* Create an ID for -net if the user did not specify one */ |
| 1421 | if (!is_netdev && !qemu_opts_id(opts)) { |
Thomas Huth | 27eb372 | 2021-02-15 10:02:25 +0100 | [diff] [blame] | 1422 | qemu_opts_set_id(opts, id_generate(ID_NET)); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
Markus Armbruster | 1421703 | 2020-07-07 18:05:47 +0200 | [diff] [blame] | 1425 | if (visit_type_Netdev(v, NULL, &object, errp)) { |
| 1426 | ret = net_client_init1(object, is_netdev, errp); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
Thomas Huth | 71830d8 | 2020-05-18 20:01:03 +0200 | [diff] [blame] | 1429 | qapi_free_Netdev(object); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1430 | |
Stefano Garzarella | 21c520d | 2019-05-17 15:47:45 +0200 | [diff] [blame] | 1431 | out: |
Stefano Garzarella | c1112b2 | 2019-05-17 15:47:46 +0200 | [diff] [blame] | 1432 | g_strfreev(substrings); |
Eric Blake | 09204ea | 2016-06-09 10:48:36 -0600 | [diff] [blame] | 1433 | visit_free(v); |
Laszlo Ersek | 6687b79 | 2012-07-17 16:17:13 +0200 | [diff] [blame] | 1434 | return ret; |
| 1435 | } |
| 1436 | |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1437 | void netdev_add(QemuOpts *opts, Error **errp) |
| 1438 | { |
Eric Blake | 0e55c38 | 2016-07-13 21:50:24 -0600 | [diff] [blame] | 1439 | net_client_init(opts, true, errp); |
Luiz Capitulino | 928059a | 2012-04-18 17:34:15 -0300 | [diff] [blame] | 1440 | } |
| 1441 | |
Eric Blake | db2a380 | 2020-03-17 15:17:10 -0500 | [diff] [blame] | 1442 | void qmp_netdev_add(Netdev *netdev, Error **errp) |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1443 | { |
Paolo Bonzini | e73b431 | 2021-03-12 09:51:38 -0500 | [diff] [blame] | 1444 | if (!id_wellformed(netdev->id)) { |
| 1445 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); |
| 1446 | return; |
| 1447 | } |
| 1448 | |
Eric Blake | 08712fc | 2020-03-17 15:17:11 -0500 | [diff] [blame] | 1449 | net_client_init1(netdev, true, errp); |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Luiz Capitulino | 5f96415 | 2012-04-16 14:36:32 -0300 | [diff] [blame] | 1452 | void qmp_netdev_del(const char *id, Error **errp) |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1453 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1454 | NetClientState *nc; |
Markus Armbruster | 831734c | 2020-11-25 11:02:20 +0100 | [diff] [blame] | 1455 | QemuOpts *opts; |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1456 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1457 | nc = qemu_find_netdev(id); |
| 1458 | if (!nc) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 1459 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1460 | "Device '%s' not found", id); |
Luiz Capitulino | 5f96415 | 2012-04-16 14:36:32 -0300 | [diff] [blame] | 1461 | return; |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1462 | } |
Luiz Capitulino | 5f96415 | 2012-04-16 14:36:32 -0300 | [diff] [blame] | 1463 | |
Eric Blake | 08712fc | 2020-03-17 15:17:11 -0500 | [diff] [blame] | 1464 | if (!nc->is_netdev) { |
Stefan Hajnoczi | 645c949 | 2012-10-24 14:34:12 +0200 | [diff] [blame] | 1465 | error_setg(errp, "Device '%s' is not a netdev", id); |
| 1466 | return; |
| 1467 | } |
| 1468 | |
Stefan Hajnoczi | b20c6b9 | 2012-07-24 16:35:15 +0100 | [diff] [blame] | 1469 | qemu_del_net_client(nc); |
Markus Armbruster | 831734c | 2020-11-25 11:02:20 +0100 | [diff] [blame] | 1470 | |
| 1471 | /* |
| 1472 | * Wart: we need to delete the QemuOpts associated with netdevs |
| 1473 | * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in |
| 1474 | * HMP netdev_add. |
| 1475 | */ |
| 1476 | opts = qemu_opts_find(qemu_find_opts("netdev"), id); |
| 1477 | if (opts) { |
| 1478 | qemu_opts_del(opts); |
| 1479 | } |
Markus Armbruster | ae82d32 | 2010-03-25 17:22:40 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1482 | static void netfilter_print_info(Monitor *mon, NetFilterState *nf) |
| 1483 | { |
| 1484 | char *str; |
| 1485 | ObjectProperty *prop; |
| 1486 | ObjectPropertyIterator iter; |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 1487 | Visitor *v; |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1488 | |
| 1489 | /* generate info str */ |
| 1490 | object_property_iter_init(&iter, OBJECT(nf)); |
| 1491 | while ((prop = object_property_iter_next(&iter))) { |
| 1492 | if (!strcmp(prop->name, "type")) { |
| 1493 | continue; |
| 1494 | } |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 1495 | v = string_output_visitor_new(false, &str); |
Markus Armbruster | 5325cc3 | 2020-07-07 18:05:54 +0200 | [diff] [blame] | 1496 | object_property_get(OBJECT(nf), prop->name, v, NULL); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 1497 | visit_complete(v, &str); |
| 1498 | visit_free(v); |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1499 | monitor_printf(mon, ",%s=%s", prop->name, str); |
| 1500 | g_free(str); |
| 1501 | } |
| 1502 | monitor_printf(mon, "\n"); |
| 1503 | } |
| 1504 | |
Zhi Yong Wu | 1a85959 | 2012-07-24 16:35:16 +0100 | [diff] [blame] | 1505 | void print_net_client(Monitor *mon, NetClientState *nc) |
Jan Kiszka | 44e798d | 2011-07-20 12:20:21 +0200 | [diff] [blame] | 1506 | { |
Yang Hongyang | a4960f5 | 2015-10-07 11:52:19 +0800 | [diff] [blame] | 1507 | NetFilterState *nf; |
| 1508 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1509 | monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name, |
| 1510 | nc->queue_index, |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 1511 | NetClientDriver_str(nc->info->type), |
Jason Wang | 56e6f59 | 2021-04-02 11:03:33 +0800 | [diff] [blame] | 1512 | nc->info_str); |
Yang Hongyang | a4960f5 | 2015-10-07 11:52:19 +0800 | [diff] [blame] | 1513 | if (!QTAILQ_EMPTY(&nc->filters)) { |
| 1514 | monitor_printf(mon, "filters:\n"); |
| 1515 | } |
| 1516 | QTAILQ_FOREACH(nf, &nc->filters, next) { |
Markus Armbruster | 7a309cc | 2020-07-14 18:02:00 +0200 | [diff] [blame] | 1517 | monitor_printf(mon, " - %s: type=%s", |
| 1518 | object_get_canonical_path_component(OBJECT(nf)), |
zhanghailiang | aa9156f | 2016-01-26 14:43:33 +0800 | [diff] [blame] | 1519 | object_get_typename(OBJECT(nf))); |
| 1520 | netfilter_print_info(mon, nf); |
Yang Hongyang | a4960f5 | 2015-10-07 11:52:19 +0800 | [diff] [blame] | 1521 | } |
Jan Kiszka | 44e798d | 2011-07-20 12:20:21 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1524 | RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp) |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1525 | { |
| 1526 | NetClientState *nc; |
Eric Blake | 95b3a8c | 2021-01-13 16:10:13 -0600 | [diff] [blame] | 1527 | RxFilterInfoList *filter_list = NULL, **tail = &filter_list; |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1528 | |
| 1529 | QTAILQ_FOREACH(nc, &net_clients, next) { |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1530 | RxFilterInfo *info; |
| 1531 | |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1532 | if (name && strcmp(nc->name, name) != 0) { |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1533 | continue; |
| 1534 | } |
| 1535 | |
| 1536 | /* only query rx-filter information of NIC */ |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1537 | if (nc->info->type != NET_CLIENT_DRIVER_NIC) { |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1538 | if (name) { |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1539 | error_setg(errp, "net client(%s) isn't a NIC", name); |
Eric Blake | e9d635e | 2021-01-13 16:10:09 -0600 | [diff] [blame] | 1540 | assert(!filter_list); |
Markus Armbruster | 9083da1 | 2014-04-24 15:44:18 +0200 | [diff] [blame] | 1541 | return NULL; |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1542 | } |
| 1543 | continue; |
| 1544 | } |
| 1545 | |
Vladislav Yasevich | 5320c2c | 2015-10-19 09:04:38 -0400 | [diff] [blame] | 1546 | /* only query information on queue 0 since the info is per nic, |
| 1547 | * not per queue |
| 1548 | */ |
| 1549 | if (nc->queue_index != 0) |
| 1550 | continue; |
| 1551 | |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1552 | if (nc->info->query_rx_filter) { |
| 1553 | info = nc->info->query_rx_filter(nc); |
Eric Blake | 95b3a8c | 2021-01-13 16:10:13 -0600 | [diff] [blame] | 1554 | QAPI_LIST_APPEND(tail, info); |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1555 | } else if (name) { |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1556 | error_setg(errp, "net client(%s) doesn't support" |
| 1557 | " rx-filter querying", name); |
Eric Blake | e9d635e | 2021-01-13 16:10:09 -0600 | [diff] [blame] | 1558 | assert(!filter_list); |
Markus Armbruster | 9083da1 | 2014-04-24 15:44:18 +0200 | [diff] [blame] | 1559 | return NULL; |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1560 | } |
Markus Armbruster | 638fb14 | 2014-04-24 15:44:17 +0200 | [diff] [blame] | 1561 | |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1562 | if (name) { |
Markus Armbruster | 638fb14 | 2014-04-24 15:44:17 +0200 | [diff] [blame] | 1563 | break; |
| 1564 | } |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Markus Armbruster | 7480874 | 2022-11-04 17:07:00 +0100 | [diff] [blame] | 1567 | if (filter_list == NULL && name) { |
Amos Kong | b1be428 | 2013-06-14 15:45:52 +0800 | [diff] [blame] | 1568 | error_setg(errp, "invalid net client name: %s", name); |
| 1569 | } |
| 1570 | |
| 1571 | return filter_list; |
| 1572 | } |
| 1573 | |
Zhang Chen | 5fbba3d | 2018-09-03 12:38:56 +0800 | [diff] [blame] | 1574 | void colo_notify_filters_event(int event, Error **errp) |
| 1575 | { |
| 1576 | NetClientState *nc; |
| 1577 | NetFilterState *nf; |
| 1578 | NetFilterClass *nfc = NULL; |
| 1579 | Error *local_err = NULL; |
| 1580 | |
| 1581 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 1582 | QTAILQ_FOREACH(nf, &nc->filters, next) { |
| 1583 | nfc = NETFILTER_GET_CLASS(OBJECT(nf)); |
| 1584 | nfc->handle_event(nf, event, &local_err); |
| 1585 | if (local_err) { |
| 1586 | error_propagate(errp, local_err); |
| 1587 | return; |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
Luiz Capitulino | 4b37156 | 2011-11-23 13:11:55 -0200 | [diff] [blame] | 1593 | void qmp_set_link(const char *name, bool up, Error **errp) |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1594 | { |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1595 | NetClientState *ncs[MAX_QUEUE_NUM]; |
| 1596 | NetClientState *nc; |
| 1597 | int queues, i; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1598 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1599 | queues = qemu_find_net_clients_except(name, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1600 | NET_CLIENT_DRIVER__MAX, |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1601 | MAX_QUEUE_NUM); |
| 1602 | |
| 1603 | if (queues == 0) { |
Markus Armbruster | 75158eb | 2015-03-16 08:57:47 +0100 | [diff] [blame] | 1604 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
| 1605 | "Device '%s' not found", name); |
Luiz Capitulino | 4b37156 | 2011-11-23 13:11:55 -0200 | [diff] [blame] | 1606 | return; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1607 | } |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1608 | nc = ncs[0]; |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1609 | |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1610 | for (i = 0; i < queues; i++) { |
| 1611 | ncs[i]->link_down = !up; |
| 1612 | } |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1613 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1614 | if (nc->info->link_status_changed) { |
| 1615 | nc->info->link_status_changed(nc); |
Mark McLoughlin | 665a3b0 | 2009-11-25 18:49:30 +0000 | [diff] [blame] | 1616 | } |
Michael S. Tsirkin | ab1cbe1 | 2011-02-09 18:45:04 +0200 | [diff] [blame] | 1617 | |
Vlad Yasevich | 02d38fc | 2013-11-21 21:05:51 -0500 | [diff] [blame] | 1618 | if (nc->peer) { |
| 1619 | /* Change peer link only if the peer is NIC and then notify peer. |
| 1620 | * If the peer is a HUBPORT or a backend, we do not change the |
| 1621 | * link status. |
| 1622 | * |
Thomas Huth | af1a5c3 | 2018-04-30 20:02:23 +0200 | [diff] [blame] | 1623 | * This behavior is compatible with qemu hubs where there could be |
Vlad Yasevich | 02d38fc | 2013-11-21 21:05:51 -0500 | [diff] [blame] | 1624 | * multiple clients that can still communicate with each other in |
| 1625 | * disconnected mode. For now maintain this compatibility. |
| 1626 | */ |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1627 | if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { |
Vlad Yasevich | 02d38fc | 2013-11-21 21:05:51 -0500 | [diff] [blame] | 1628 | for (i = 0; i < queues; i++) { |
| 1629 | ncs[i]->peer->link_down = !up; |
| 1630 | } |
| 1631 | } |
| 1632 | if (nc->peer->info->link_status_changed) { |
| 1633 | nc->peer->info->link_status_changed(nc->peer); |
| 1634 | } |
Michael S. Tsirkin | ab1cbe1 | 2011-02-09 18:45:04 +0200 | [diff] [blame] | 1635 | } |
aliguori | 436e5e5 | 2009-01-08 19:44:06 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Philippe Mathieu-Daudé | 538f049 | 2021-01-11 16:20:20 +0100 | [diff] [blame] | 1638 | static void net_vm_change_state_handler(void *opaque, bool running, |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1639 | RunState state) |
| 1640 | { |
Fam Zheng | 625de44 | 2015-07-07 09:21:07 +0800 | [diff] [blame] | 1641 | NetClientState *nc; |
| 1642 | NetClientState *tmp; |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1643 | |
Fam Zheng | 625de44 | 2015-07-07 09:21:07 +0800 | [diff] [blame] | 1644 | QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { |
| 1645 | if (running) { |
| 1646 | /* Flush queued packets and wake up backends. */ |
| 1647 | if (nc->peer && qemu_can_send_packet(nc)) { |
| 1648 | qemu_flush_queued_packets(nc->peer); |
| 1649 | } |
| 1650 | } else { |
| 1651 | /* Complete all queued packets, to guarantee we don't modify |
| 1652 | * state later when VM is not running. |
| 1653 | */ |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1654 | qemu_flush_or_purge_queued_packets(nc, true); |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1659 | void net_cleanup(void) |
| 1660 | { |
David Woodhouse | 84f85eb | 2023-11-15 17:24:35 +0000 | [diff] [blame] | 1661 | NetClientState *nc, **p = &QTAILQ_FIRST(&net_clients); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1662 | |
Rao, Lei | 0c7af1a | 2021-06-08 16:23:30 +0800 | [diff] [blame] | 1663 | /*cleanup colo compare module for COLO*/ |
| 1664 | colo_compare_cleanup(); |
| 1665 | |
David Woodhouse | 84f85eb | 2023-11-15 17:24:35 +0000 | [diff] [blame] | 1666 | /* |
| 1667 | * Walk the net_clients list and remove the netdevs but *not* any |
| 1668 | * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device |
| 1669 | * model which created them, and in some cases (e.g. xen-net-device) |
| 1670 | * the device itself may do cleanup at exit and will be upset if we |
| 1671 | * just delete its NIC from underneath it. |
| 1672 | * |
| 1673 | * Since qemu_del_net_client() may delete multiple entries, using |
| 1674 | * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer |
| 1675 | * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep |
| 1676 | * 'p' pointing to either the head of the list, or the 'next' field |
| 1677 | * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk |
| 1678 | * the list. |
| 1679 | * |
| 1680 | * The 'nc' variable isn't part of the list traversal; it's purely |
| 1681 | * for convenience as too much '(*p)->' has a tendency to make the |
| 1682 | * readers' eyes bleed. |
Jason Wang | 1ceef9f | 2013-01-30 19:12:28 +0800 | [diff] [blame] | 1683 | */ |
David Woodhouse | 84f85eb | 2023-11-15 17:24:35 +0000 | [diff] [blame] | 1684 | while (*p) { |
| 1685 | nc = *p; |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 1686 | if (nc->info->type == NET_CLIENT_DRIVER_NIC) { |
David Woodhouse | 84f85eb | 2023-11-15 17:24:35 +0000 | [diff] [blame] | 1687 | /* Skip NET_CLIENT_DRIVER_NIC entries */ |
| 1688 | p = &QTAILQ_NEXT(nc, next); |
Jason Wang | 948ecf2 | 2013-01-30 19:12:24 +0800 | [diff] [blame] | 1689 | } else { |
| 1690 | qemu_del_net_client(nc); |
| 1691 | } |
Mark McLoughlin | 577c4af | 2009-10-08 19:58:28 +0100 | [diff] [blame] | 1692 | } |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1693 | |
| 1694 | qemu_del_vm_change_state_handler(net_change_state_entry); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Markus Armbruster | 668680f | 2010-02-11 14:44:58 +0100 | [diff] [blame] | 1697 | void net_check_clients(void) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1698 | { |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1699 | NetClientState *nc; |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 1700 | int i; |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1701 | |
David Woodhouse | 2cdeca0 | 2023-10-21 23:09:38 +0100 | [diff] [blame] | 1702 | if (nic_model_help) { |
| 1703 | show_nic_models(); |
| 1704 | exit(0); |
| 1705 | } |
Stefan Hajnoczi | 8101764 | 2012-07-24 16:35:07 +0100 | [diff] [blame] | 1706 | net_hub_check_clients(); |
Tristan Gingold | ac60cc1 | 2011-03-15 14:20:54 +0100 | [diff] [blame] | 1707 | |
Stefan Hajnoczi | 35277d1 | 2012-07-24 16:35:14 +0100 | [diff] [blame] | 1708 | QTAILQ_FOREACH(nc, &net_clients, next) { |
| 1709 | if (!nc->peer) { |
Alistair Francis | 8297be8 | 2017-09-11 12:52:53 -0700 | [diff] [blame] | 1710 | warn_report("%s %s has no peer", |
Alistair Francis | b62e39b | 2017-09-11 12:52:56 -0700 | [diff] [blame] | 1711 | nc->info->type == NET_CLIENT_DRIVER_NIC |
| 1712 | ? "nic" : "netdev", |
| 1713 | nc->name); |
Markus Armbruster | efe32fd | 2010-02-11 14:45:00 +0100 | [diff] [blame] | 1714 | } |
| 1715 | } |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 1716 | |
| 1717 | /* Check that all NICs requested via -net nic actually got created. |
| 1718 | * NICs created via -device don't need to be checked here because |
| 1719 | * they are always instantiated. |
| 1720 | */ |
| 1721 | for (i = 0; i < MAX_NICS; i++) { |
| 1722 | NICInfo *nd = &nd_table[i]; |
| 1723 | if (nd->used && !nd->instantiated) { |
Alistair Francis | 8297be8 | 2017-09-11 12:52:53 -0700 | [diff] [blame] | 1724 | warn_report("requested NIC (%s, model %s) " |
| 1725 | "was not created (not supported by this machine?)", |
| 1726 | nd->name ? nd->name : "anonymous", |
| 1727 | nd->model ? nd->model : "unspecified"); |
Peter Maydell | 48e2faf | 2011-05-20 16:50:01 +0100 | [diff] [blame] | 1728 | } |
| 1729 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1730 | } |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1731 | |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 1732 | static int net_init_client(void *dummy, QemuOpts *opts, Error **errp) |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1733 | { |
David Woodhouse | fa62cb98 | 2024-08-06 18:21:37 +0100 | [diff] [blame] | 1734 | const char *model = qemu_opt_get(opts, "model"); |
David Woodhouse | 64f75f5 | 2024-07-09 13:34:44 +0100 | [diff] [blame] | 1735 | |
| 1736 | if (is_nic_model_help_option(model)) { |
| 1737 | return 0; |
| 1738 | } |
| 1739 | |
Thomas Huth | 34f708b | 2018-02-21 11:18:30 +0100 | [diff] [blame] | 1740 | return net_client_init(opts, false, errp); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 1743 | static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp) |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1744 | { |
Paolo Bonzini | ad6f932 | 2020-11-11 05:52:22 -0500 | [diff] [blame] | 1745 | const char *type = qemu_opt_get(opts, "type"); |
| 1746 | |
| 1747 | if (type && is_help_option(type)) { |
| 1748 | show_netdevs(); |
| 1749 | exit(0); |
| 1750 | } |
Thomas Huth | 34f708b | 2018-02-21 11:18:30 +0100 | [diff] [blame] | 1751 | return net_client_init(opts, true, errp); |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1752 | } |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1753 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1754 | /* For the convenience "--nic" parameter */ |
| 1755 | static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) |
| 1756 | { |
| 1757 | char *mac, *nd_id; |
| 1758 | int idx, ret; |
| 1759 | NICInfo *ni; |
| 1760 | const char *type; |
| 1761 | |
| 1762 | type = qemu_opt_get(opts, "type"); |
Thomas Huth | 27c8192 | 2022-11-10 13:52:23 +0100 | [diff] [blame] | 1763 | if (type) { |
| 1764 | if (g_str_equal(type, "none")) { |
| 1765 | return 0; /* Nothing to do, default_net is cleared in vl.c */ |
| 1766 | } |
| 1767 | if (is_help_option(type)) { |
| 1768 | GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE); |
David Woodhouse | 481434f | 2023-10-23 09:38:00 +0100 | [diff] [blame] | 1769 | int i; |
Thomas Huth | 27c8192 | 2022-11-10 13:52:23 +0100 | [diff] [blame] | 1770 | show_netdevs(); |
| 1771 | printf("\n"); |
David Woodhouse | 481434f | 2023-10-23 09:38:00 +0100 | [diff] [blame] | 1772 | printf("Available NIC models " |
| 1773 | "(use -nic model=help for a filtered list):\n"); |
| 1774 | for (i = 0 ; nic_models->pdata[i]; i++) { |
| 1775 | printf("%s\n", (char *)nic_models->pdata[i]); |
| 1776 | } |
Thomas Huth | 27c8192 | 2022-11-10 13:52:23 +0100 | [diff] [blame] | 1777 | g_ptr_array_free(nic_models, true); |
| 1778 | exit(0); |
| 1779 | } |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | idx = nic_get_free_idx(); |
| 1783 | if (idx == -1 || nb_nics >= MAX_NICS) { |
| 1784 | error_setg(errp, "no more on-board/default NIC slots available"); |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 1785 | return -1; |
| 1786 | } |
Gerd Hoffmann | 3329f07 | 2010-08-20 13:52:01 +0200 | [diff] [blame] | 1787 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1788 | if (!type) { |
| 1789 | qemu_opt_set(opts, "type", "user", &error_abort); |
| 1790 | } |
| 1791 | |
| 1792 | ni = &nd_table[idx]; |
| 1793 | memset(ni, 0, sizeof(*ni)); |
| 1794 | ni->model = qemu_opt_get_del(opts, "model"); |
| 1795 | |
David Woodhouse | 64f75f5 | 2024-07-09 13:34:44 +0100 | [diff] [blame] | 1796 | if (is_nic_model_help_option(ni->model)) { |
David Woodhouse | 2cdeca0 | 2023-10-21 23:09:38 +0100 | [diff] [blame] | 1797 | return 0; |
| 1798 | } |
| 1799 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1800 | /* Create an ID if the user did not specify one */ |
| 1801 | nd_id = g_strdup(qemu_opts_id(opts)); |
| 1802 | if (!nd_id) { |
Thomas Huth | 27eb372 | 2021-02-15 10:02:25 +0100 | [diff] [blame] | 1803 | nd_id = id_generate(ID_NET); |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1804 | qemu_opts_set_id(opts, nd_id); |
| 1805 | } |
| 1806 | |
| 1807 | /* Handle MAC address */ |
| 1808 | mac = qemu_opt_get_del(opts, "mac"); |
| 1809 | if (mac) { |
| 1810 | ret = net_parse_macaddr(ni->macaddr.a, mac); |
| 1811 | g_free(mac); |
| 1812 | if (ret) { |
| 1813 | error_setg(errp, "invalid syntax for ethernet address"); |
Thomas Huth | 9d94619 | 2018-04-30 09:26:45 +0200 | [diff] [blame] | 1814 | goto out; |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1815 | } |
| 1816 | if (is_multicast_ether_addr(ni->macaddr.a)) { |
| 1817 | error_setg(errp, "NIC cannot have multicast MAC address"); |
Thomas Huth | 9d94619 | 2018-04-30 09:26:45 +0200 | [diff] [blame] | 1818 | ret = -1; |
| 1819 | goto out; |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | qemu_macaddr_default_if_unset(&ni->macaddr); |
| 1823 | |
| 1824 | ret = net_client_init(opts, true, errp); |
| 1825 | if (ret == 0) { |
| 1826 | ni->netdev = qemu_find_netdev(nd_id); |
| 1827 | ni->used = true; |
| 1828 | nb_nics++; |
| 1829 | } |
| 1830 | |
Thomas Huth | 9d94619 | 2018-04-30 09:26:45 +0200 | [diff] [blame] | 1831 | out: |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1832 | g_free(nd_id); |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 1833 | return ret; |
| 1834 | } |
| 1835 | |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 1836 | static void netdev_init_modern(void) |
| 1837 | { |
| 1838 | while (!QSIMPLEQ_EMPTY(&nd_queue)) { |
| 1839 | NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue); |
| 1840 | |
| 1841 | QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry); |
| 1842 | loc_push_restore(&nd->loc); |
| 1843 | net_client_init1(nd->nd, true, &error_fatal); |
| 1844 | loc_pop(&nd->loc); |
| 1845 | qapi_free_Netdev(nd->nd); |
| 1846 | g_free(nd); |
| 1847 | } |
| 1848 | } |
| 1849 | |
Laurent Vivier | d63ef17 | 2022-10-21 11:09:07 +0200 | [diff] [blame] | 1850 | void net_init_clients(void) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1851 | { |
Michael S. Tsirkin | ca77d85 | 2014-09-04 11:39:13 +0300 | [diff] [blame] | 1852 | net_change_state_entry = |
| 1853 | qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL); |
| 1854 | |
Stefan Hajnoczi | 9487899 | 2012-07-24 16:35:12 +0100 | [diff] [blame] | 1855 | QTAILQ_INIT(&net_clients); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1856 | |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 1857 | netdev_init_modern(); |
| 1858 | |
Laurent Vivier | d63ef17 | 2022-10-21 11:09:07 +0200 | [diff] [blame] | 1859 | qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, |
| 1860 | &error_fatal); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1861 | |
Laurent Vivier | d63ef17 | 2022-10-21 11:09:07 +0200 | [diff] [blame] | 1862 | qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, |
| 1863 | &error_fatal); |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1864 | |
Laurent Vivier | d63ef17 | 2022-10-21 11:09:07 +0200 | [diff] [blame] | 1865 | qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, |
| 1866 | &error_fatal); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 1869 | /* |
| 1870 | * Does this -netdev argument use modern rather than traditional syntax? |
| 1871 | * Modern syntax is to be parsed with netdev_parse_modern(). |
| 1872 | * Traditional syntax is to be parsed with net_client_parse(). |
| 1873 | */ |
Philippe Mathieu-Daudé | 73071f1 | 2023-10-04 14:00:06 +0200 | [diff] [blame] | 1874 | bool netdev_is_modern(const char *optstr) |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 1875 | { |
Laurent Vivier | 5166fe0 | 2022-10-21 11:09:11 +0200 | [diff] [blame] | 1876 | QemuOpts *opts; |
| 1877 | bool is_modern; |
| 1878 | const char *type; |
| 1879 | static QemuOptsList dummy_opts = { |
| 1880 | .name = "netdev", |
| 1881 | .implied_opt_name = "type", |
| 1882 | .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), |
| 1883 | .desc = { { } }, |
| 1884 | }; |
| 1885 | |
Philippe Mathieu-Daudé | 73071f1 | 2023-10-04 14:00:06 +0200 | [diff] [blame] | 1886 | if (optstr[0] == '{') { |
Laurent Vivier | 5166fe0 | 2022-10-21 11:09:11 +0200 | [diff] [blame] | 1887 | /* This is JSON, which means it's modern syntax */ |
| 1888 | return true; |
| 1889 | } |
| 1890 | |
| 1891 | opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort); |
Philippe Mathieu-Daudé | 73071f1 | 2023-10-04 14:00:06 +0200 | [diff] [blame] | 1892 | qemu_opts_do_parse(opts, optstr, dummy_opts.implied_opt_name, |
Laurent Vivier | 5166fe0 | 2022-10-21 11:09:11 +0200 | [diff] [blame] | 1893 | &error_abort); |
| 1894 | type = qemu_opt_get(opts, "type"); |
| 1895 | is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram"); |
| 1896 | |
| 1897 | qemu_opts_reset(&dummy_opts); |
| 1898 | |
| 1899 | return is_modern; |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | /* |
| 1903 | * netdev_parse_modern() uses modern, more expressive syntax than |
| 1904 | * net_client_parse(), but supports only the -netdev option. |
| 1905 | * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse() |
| 1906 | * appends to @qemu_netdev_opts. |
| 1907 | */ |
Philippe Mathieu-Daudé | 73071f1 | 2023-10-04 14:00:06 +0200 | [diff] [blame] | 1908 | void netdev_parse_modern(const char *optstr) |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 1909 | { |
| 1910 | Visitor *v; |
| 1911 | NetdevQueueEntry *nd; |
| 1912 | |
Philippe Mathieu-Daudé | 73071f1 | 2023-10-04 14:00:06 +0200 | [diff] [blame] | 1913 | v = qobject_input_visitor_new_str(optstr, "type", &error_fatal); |
Laurent Vivier | f3eedcd | 2022-10-21 11:09:09 +0200 | [diff] [blame] | 1914 | nd = g_new(NetdevQueueEntry, 1); |
| 1915 | visit_type_Netdev(v, NULL, &nd->nd, &error_fatal); |
| 1916 | visit_free(v); |
| 1917 | loc_save(&nd->loc); |
| 1918 | |
| 1919 | QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry); |
| 1920 | } |
| 1921 | |
Philippe Mathieu-Daudé | 73071f1 | 2023-10-04 14:00:06 +0200 | [diff] [blame] | 1922 | void net_client_parse(QemuOptsList *opts_list, const char *optstr) |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1923 | { |
Philippe Mathieu-Daudé | 73071f1 | 2023-10-04 14:00:06 +0200 | [diff] [blame] | 1924 | if (!qemu_opts_parse_noisily(opts_list, optstr, true)) { |
Laurent Vivier | 21fccb2 | 2022-10-21 11:09:08 +0200 | [diff] [blame] | 1925 | exit(1); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1926 | } |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 1927 | } |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1928 | |
| 1929 | /* From FreeBSD */ |
| 1930 | /* XXX: optimize */ |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1931 | uint32_t net_crc32(const uint8_t *p, int len) |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1932 | { |
| 1933 | uint32_t crc; |
| 1934 | int carry, i, j; |
| 1935 | uint8_t b; |
| 1936 | |
| 1937 | crc = 0xffffffff; |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1938 | for (i = 0; i < len; i++) { |
| 1939 | b = *p++; |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1940 | for (j = 0; j < 8; j++) { |
| 1941 | carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); |
| 1942 | crc <<= 1; |
| 1943 | b >>= 1; |
| 1944 | if (carry) { |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1945 | crc = ((crc ^ POLYNOMIAL_BE) | carry); |
Jason Wang | 7fc8d91 | 2012-03-05 11:08:50 +0800 | [diff] [blame] | 1946 | } |
| 1947 | } |
| 1948 | } |
Mark Cave-Ayland | eaba8f3 | 2017-12-15 18:41:43 +0000 | [diff] [blame] | 1949 | |
| 1950 | return crc; |
| 1951 | } |
| 1952 | |
Mark Cave-Ayland | f1a7deb | 2017-12-15 18:41:44 +0000 | [diff] [blame] | 1953 | uint32_t net_crc32_le(const uint8_t *p, int len) |
| 1954 | { |
| 1955 | uint32_t crc; |
| 1956 | int carry, i, j; |
| 1957 | uint8_t b; |
| 1958 | |
| 1959 | crc = 0xffffffff; |
| 1960 | for (i = 0; i < len; i++) { |
| 1961 | b = *p++; |
| 1962 | for (j = 0; j < 8; j++) { |
| 1963 | carry = (crc & 0x1) ^ (b & 0x01); |
| 1964 | crc >>= 1; |
| 1965 | b >>= 1; |
| 1966 | if (carry) { |
| 1967 | crc ^= POLYNOMIAL_LE; |
| 1968 | } |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | return crc; |
| 1973 | } |
| 1974 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 1975 | QemuOptsList qemu_netdev_opts = { |
| 1976 | .name = "netdev", |
| 1977 | .implied_opt_name = "type", |
| 1978 | .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), |
| 1979 | .desc = { |
| 1980 | /* |
| 1981 | * no elements => accept any params |
| 1982 | * validation will happen later |
| 1983 | */ |
| 1984 | { /* end of list */ } |
| 1985 | }, |
| 1986 | }; |
| 1987 | |
Thomas Huth | 78cd6f7 | 2018-02-21 11:18:36 +0100 | [diff] [blame] | 1988 | QemuOptsList qemu_nic_opts = { |
| 1989 | .name = "nic", |
| 1990 | .implied_opt_name = "type", |
| 1991 | .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head), |
| 1992 | .desc = { |
| 1993 | /* |
| 1994 | * no elements => accept any params |
| 1995 | * validation will happen later |
| 1996 | */ |
| 1997 | { /* end of list */ } |
| 1998 | }, |
| 1999 | }; |
| 2000 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 2001 | QemuOptsList qemu_net_opts = { |
| 2002 | .name = "net", |
| 2003 | .implied_opt_name = "type", |
| 2004 | .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), |
| 2005 | .desc = { |
| 2006 | /* |
| 2007 | * no elements => accept any params |
| 2008 | * validation will happen later |
| 2009 | */ |
| 2010 | { /* end of list */ } |
| 2011 | }, |
| 2012 | }; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2013 | |
| 2014 | void net_socket_rs_init(SocketReadState *rs, |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 2015 | SocketReadStateFinalize *finalize, |
| 2016 | bool vnet_hdr) |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2017 | { |
| 2018 | rs->state = 0; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 2019 | rs->vnet_hdr = vnet_hdr; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2020 | rs->index = 0; |
| 2021 | rs->packet_len = 0; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 2022 | rs->vnet_hdr_len = 0; |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2023 | memset(rs->buf, 0, sizeof(rs->buf)); |
| 2024 | rs->finalize = finalize; |
| 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | * Returns |
Zhang Chen | e9e0a58 | 2016-08-18 11:23:25 +0800 | [diff] [blame] | 2029 | * 0: success |
| 2030 | * -1: error occurs |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2031 | */ |
| 2032 | int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size) |
| 2033 | { |
| 2034 | unsigned int l; |
| 2035 | |
| 2036 | while (size > 0) { |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 2037 | /* Reassemble a packet from the network. |
| 2038 | * 0 = getting length. |
| 2039 | * 1 = getting vnet header length. |
| 2040 | * 2 = getting data. |
| 2041 | */ |
| 2042 | switch (rs->state) { |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2043 | case 0: |
| 2044 | l = 4 - rs->index; |
| 2045 | if (l > size) { |
| 2046 | l = size; |
| 2047 | } |
| 2048 | memcpy(rs->buf + rs->index, buf, l); |
| 2049 | buf += l; |
| 2050 | size -= l; |
| 2051 | rs->index += l; |
| 2052 | if (rs->index == 4) { |
| 2053 | /* got length */ |
| 2054 | rs->packet_len = ntohl(*(uint32_t *)rs->buf); |
| 2055 | rs->index = 0; |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 2056 | if (rs->vnet_hdr) { |
| 2057 | rs->state = 1; |
| 2058 | } else { |
| 2059 | rs->state = 2; |
| 2060 | rs->vnet_hdr_len = 0; |
| 2061 | } |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2062 | } |
| 2063 | break; |
| 2064 | case 1: |
Zhang Chen | 3cde5ea | 2017-07-04 14:53:46 +0800 | [diff] [blame] | 2065 | l = 4 - rs->index; |
| 2066 | if (l > size) { |
| 2067 | l = size; |
| 2068 | } |
| 2069 | memcpy(rs->buf + rs->index, buf, l); |
| 2070 | buf += l; |
| 2071 | size -= l; |
| 2072 | rs->index += l; |
| 2073 | if (rs->index == 4) { |
| 2074 | /* got vnet header length */ |
| 2075 | rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf); |
| 2076 | rs->index = 0; |
| 2077 | rs->state = 2; |
| 2078 | } |
| 2079 | break; |
| 2080 | case 2: |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2081 | l = rs->packet_len - rs->index; |
| 2082 | if (l > size) { |
| 2083 | l = size; |
| 2084 | } |
| 2085 | if (rs->index + l <= sizeof(rs->buf)) { |
| 2086 | memcpy(rs->buf + rs->index, buf, l); |
| 2087 | } else { |
| 2088 | fprintf(stderr, "serious error: oversized packet received," |
| 2089 | "connection terminated.\n"); |
| 2090 | rs->index = rs->state = 0; |
| 2091 | return -1; |
| 2092 | } |
| 2093 | |
| 2094 | rs->index += l; |
| 2095 | buf += l; |
| 2096 | size -= l; |
| 2097 | if (rs->index >= rs->packet_len) { |
| 2098 | rs->index = 0; |
| 2099 | rs->state = 0; |
Daniel P. Berrange | e79cd40 | 2016-11-04 15:46:33 +0000 | [diff] [blame] | 2100 | assert(rs->finalize); |
| 2101 | rs->finalize(rs); |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2102 | } |
| 2103 | break; |
| 2104 | } |
| 2105 | } |
Zhang Chen | e9e0a58 | 2016-08-18 11:23:25 +0800 | [diff] [blame] | 2106 | |
| 2107 | assert(size == 0); |
Zhang Chen | 16a3df4 | 2016-05-13 15:35:19 +0800 | [diff] [blame] | 2108 | return 0; |
| 2109 | } |