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 */ 24 25 #include "qemu/osdep.h" 26 27 #include "net/net.h" 28 #include "clients.h" 29 #include "hub.h" 30 #include "hw/qdev-properties.h" 31 #include "net/slirp.h" 32 #include "net/eth.h" 33 #include "util.h" 34 35 #include "monitor/monitor.h" 36 #include "qemu/help_option.h" 37 #include "qapi/qapi-commands-net.h" 38 #include "qapi/qapi-visit-net.h" 39 #include "qobject/qdict.h" 40 #include "qapi/qmp/qerror.h" 41 #include "qemu/error-report.h" 42 #include "qemu/sockets.h" 43 #include "qemu/cutils.h" 44 #include "qemu/config-file.h" 45 #include "qemu/ctype.h" 46 #include "qemu/id.h" 47 #include "qemu/iov.h" 48 #include "qemu/qemu-print.h" 49 #include "qemu/main-loop.h" 50 #include "qemu/option.h" 51 #include "qemu/keyval.h" 52 #include "qapi/error.h" 53 #include "qapi/opts-visitor.h" 54 #include "system/runstate.h" 55 #include "net/colo-compare.h" 56 #include "net/filter.h" 57 #include "qapi/string-output-visitor.h" 58 #include "qapi/qobject-input-visitor.h" 59 #include "standard-headers/linux/virtio_net.h" 60 61 /* Net bridge is currently not supported for W32. */ 62 #if !defined(_WIN32) 63 # define CONFIG_NET_BRIDGE 64 #endif 65 66 static VMChangeStateEntry *net_change_state_entry; 67 NetClientStateList net_clients; 68 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 79 static GHashTable *nic_model_help; 80 81 static int nb_nics; 82 static NICInfo nd_table[MAX_NICS]; 83 84 /***********************************************************/ 85 /* network device redirectors */ 86 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 123 int parse_host_port(struct sockaddr_in *saddr, const char *str, 124 Error **errp) 125 { 126 gchar **substrings; 127 int ret; 128 129 substrings = g_strsplit(str, ":", 2); 130 if (!substrings || !substrings[0] || !substrings[1]) { 131 error_setg(errp, "host address '%s' doesn't contain ':' " 132 "separating host from port", str); 133 ret = -1; 134 goto out; 135 } 136 137 ret = convert_host_port(saddr, substrings[0], substrings[1], errp); 138 139 out: 140 g_strfreev(substrings); 141 return ret; 142 } 143 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 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 160 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) 161 { 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]); 165 } 166 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 208 void qemu_macaddr_default_if_unset(MACAddr *macaddr) 209 { 210 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; 211 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } }; 212 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 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; 227 macaddr->a[5] = qemu_macaddr_get_free(); 228 qemu_macaddr_set_used(macaddr); 229 } 230 231 /** 232 * Generate a name for net client 233 * 234 * Only net clients created with the legacy -net option and NICs need this. 235 */ 236 static char *assign_name(NetClientState *nc1, const char *model) 237 { 238 NetClientState *nc; 239 int id = 0; 240 241 QTAILQ_FOREACH(nc, &net_clients, next) { 242 if (nc == nc1) { 243 continue; 244 } 245 if (strcmp(nc->model, model) == 0) { 246 id++; 247 } 248 } 249 250 return g_strdup_printf("%s.%d", model, id); 251 } 252 253 static void qemu_net_client_destructor(NetClientState *nc) 254 { 255 g_free(nc); 256 } 257 static ssize_t qemu_deliver_packet_iov(NetClientState *sender, 258 unsigned flags, 259 const struct iovec *iov, 260 int iovcnt, 261 void *opaque); 262 263 static void qemu_net_client_setup(NetClientState *nc, 264 NetClientInfo *info, 265 NetClientState *peer, 266 const char *model, 267 const char *name, 268 NetClientDestructor *destructor, 269 bool is_datapath) 270 { 271 nc->info = info; 272 nc->model = g_strdup(model); 273 if (name) { 274 nc->name = g_strdup(name); 275 } else { 276 nc->name = assign_name(nc, model); 277 } 278 279 if (peer) { 280 assert(!peer->peer); 281 nc->peer = peer; 282 peer->peer = nc; 283 } 284 QTAILQ_INSERT_TAIL(&net_clients, nc, next); 285 286 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc); 287 nc->destructor = destructor; 288 nc->is_datapath = is_datapath; 289 QTAILQ_INIT(&nc->filters); 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); 302 qemu_net_client_setup(nc, info, peer, model, name, 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); 320 321 return nc; 322 } 323 324 NICState *qemu_new_nic(NetClientInfo *info, 325 NICConf *conf, 326 const char *model, 327 const char *name, 328 MemReentrancyGuard *reentrancy_guard, 329 void *opaque) 330 { 331 NetClientState **peers = conf->peers.ncs; 332 NICState *nic; 333 int i, queues = MAX(1, conf->peers.queues); 334 335 assert(info->type == NET_CLIENT_DRIVER_NIC); 336 assert(info->size >= sizeof(NICState)); 337 338 nic = g_malloc0(info->size + sizeof(NetClientState) * queues); 339 nic->ncs = (void *)nic + info->size; 340 nic->conf = conf; 341 nic->reentrancy_guard = reentrancy_guard, 342 nic->opaque = opaque; 343 344 for (i = 0; i < queues; i++) { 345 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name, 346 NULL, true); 347 nic->ncs[i].queue_index = i; 348 } 349 350 return nic; 351 } 352 353 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index) 354 { 355 return nic->ncs + queue_index; 356 } 357 358 NetClientState *qemu_get_queue(NICState *nic) 359 { 360 return qemu_get_subqueue(nic, 0); 361 } 362 363 NICState *qemu_get_nic(NetClientState *nc) 364 { 365 NetClientState *nc0 = nc - nc->queue_index; 366 367 return (NICState *)((void *)nc0 - nc->info->size); 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 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 384 static void qemu_cleanup_net_client(NetClientState *nc, 385 bool remove_from_net_clients) 386 { 387 if (remove_from_net_clients) { 388 QTAILQ_REMOVE(&net_clients, nc, next); 389 } 390 391 if (nc->info->cleanup) { 392 nc->info->cleanup(nc); 393 } 394 } 395 396 static void qemu_free_net_client(NetClientState *nc) 397 { 398 if (nc->incoming_queue) { 399 qemu_del_net_queue(nc->incoming_queue); 400 } 401 if (nc->peer) { 402 nc->peer->peer = NULL; 403 } 404 g_free(nc->name); 405 g_free(nc->model); 406 if (nc->destructor) { 407 nc->destructor(nc); 408 } 409 } 410 411 void qemu_del_net_client(NetClientState *nc) 412 { 413 NetClientState *ncs[MAX_QUEUE_NUM]; 414 int queues, i; 415 NetFilterState *nf, *next; 416 417 assert(nc->info->type != NET_CLIENT_DRIVER_NIC); 418 419 /* If the NetClientState belongs to a multiqueue backend, we will change all 420 * other NetClientStates also. 421 */ 422 queues = qemu_find_net_clients_except(nc->name, ncs, 423 NET_CLIENT_DRIVER_NIC, 424 MAX_QUEUE_NUM); 425 assert(queues != 0); 426 427 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) { 428 object_unparent(OBJECT(nf)); 429 } 430 431 /* 432 * If there is a peer NIC, transfer ownership to it. Delete the client 433 * from net_client list but do not cleanup nor free. This way NIC can 434 * still access to members of the backend. 435 * 436 * The cleanup and free will be done when the NIC is free. 437 */ 438 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { 439 NICState *nic = qemu_get_nic(nc->peer); 440 if (nic->peer_deleted) { 441 return; 442 } 443 nic->peer_deleted = true; 444 445 for (i = 0; i < queues; i++) { 446 ncs[i]->peer->link_down = true; 447 QTAILQ_REMOVE(&net_clients, ncs[i], next); 448 } 449 450 if (nc->peer->info->link_status_changed) { 451 nc->peer->info->link_status_changed(nc->peer); 452 } 453 454 return; 455 } 456 457 for (i = 0; i < queues; i++) { 458 qemu_cleanup_net_client(ncs[i], true); 459 qemu_free_net_client(ncs[i]); 460 } 461 } 462 463 void qemu_del_nic(NICState *nic) 464 { 465 int i, queues = MAX(nic->conf->peers.queues, 1); 466 467 qemu_macaddr_set_free(&nic->conf->macaddr); 468 469 for (i = 0; i < queues; i++) { 470 NetClientState *nc = qemu_get_subqueue(nic, i); 471 /* 472 * If this is a peer NIC and peer has already been deleted, clean it up 473 * and free it now. 474 */ 475 if (nic->peer_deleted) { 476 qemu_cleanup_net_client(nc->peer, false); 477 qemu_free_net_client(nc->peer); 478 } else if (nc->peer) { 479 /* if there are RX packets pending, complete them */ 480 qemu_purge_queued_packets(nc->peer); 481 } 482 } 483 484 for (i = queues - 1; i >= 0; i--) { 485 NetClientState *nc = qemu_get_subqueue(nic, i); 486 487 qemu_cleanup_net_client(nc, true); 488 qemu_free_net_client(nc); 489 } 490 491 g_free(nic); 492 } 493 494 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) 495 { 496 NetClientState *nc; 497 498 QTAILQ_FOREACH(nc, &net_clients, next) { 499 if (nc->info->type == NET_CLIENT_DRIVER_NIC) { 500 if (nc->queue_index == 0) { 501 func(qemu_get_nic(nc), opaque); 502 } 503 } 504 } 505 } 506 507 bool qemu_has_ufo(NetClientState *nc) 508 { 509 if (!nc || !nc->info->has_ufo) { 510 return false; 511 } 512 513 return nc->info->has_ufo(nc); 514 } 515 516 bool qemu_has_uso(NetClientState *nc) 517 { 518 if (!nc || !nc->info->has_uso) { 519 return false; 520 } 521 522 return nc->info->has_uso(nc); 523 } 524 525 bool qemu_has_tunnel(NetClientState *nc) 526 { 527 if (!nc || !nc->info->has_tunnel) { 528 return false; 529 } 530 531 return nc->info->has_tunnel(nc); 532 } 533 534 bool qemu_has_vnet_hdr(NetClientState *nc) 535 { 536 if (!nc || !nc->info->has_vnet_hdr) { 537 return false; 538 } 539 540 return nc->info->has_vnet_hdr(nc); 541 } 542 543 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len) 544 { 545 if (!nc || !nc->info->has_vnet_hdr_len) { 546 return false; 547 } 548 549 return nc->info->has_vnet_hdr_len(nc, len); 550 } 551 552 void qemu_set_offload(NetClientState *nc, const NetOffloads *ol) 553 { 554 if (!nc || !nc->info->set_offload) { 555 return; 556 } 557 558 nc->info->set_offload(nc, ol); 559 } 560 561 int qemu_get_vnet_hdr_len(NetClientState *nc) 562 { 563 if (!nc) { 564 return 0; 565 } 566 567 return nc->vnet_hdr_len; 568 } 569 570 void qemu_set_vnet_hdr_len(NetClientState *nc, int len) 571 { 572 if (!nc || !nc->info->set_vnet_hdr_len) { 573 return; 574 } 575 576 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || 577 len == sizeof(struct virtio_net_hdr) || 578 len == sizeof(struct virtio_net_hdr_v1_hash) || 579 len == sizeof(struct virtio_net_hdr_v1_hash_tunnel)); 580 581 nc->vnet_hdr_len = len; 582 nc->info->set_vnet_hdr_len(nc, len); 583 } 584 585 bool qemu_get_vnet_hash_supported_types(NetClientState *nc, uint32_t *types) 586 { 587 if (!nc || !nc->info->get_vnet_hash_supported_types) { 588 return false; 589 } 590 591 return nc->info->get_vnet_hash_supported_types(nc, types); 592 } 593 594 int qemu_set_vnet_le(NetClientState *nc, bool is_le) 595 { 596 #if HOST_BIG_ENDIAN 597 if (!nc || !nc->info->set_vnet_le) { 598 return -ENOSYS; 599 } 600 601 return nc->info->set_vnet_le(nc, is_le); 602 #else 603 return 0; 604 #endif 605 } 606 607 int qemu_set_vnet_be(NetClientState *nc, bool is_be) 608 { 609 #if HOST_BIG_ENDIAN 610 return 0; 611 #else 612 if (!nc || !nc->info->set_vnet_be) { 613 return -ENOSYS; 614 } 615 616 return nc->info->set_vnet_be(nc, is_be); 617 #endif 618 } 619 620 int qemu_can_receive_packet(NetClientState *nc) 621 { 622 if (nc->receive_disabled) { 623 return 0; 624 } else if (nc->info->can_receive && 625 !nc->info->can_receive(nc)) { 626 return 0; 627 } 628 return 1; 629 } 630 631 int qemu_can_send_packet(NetClientState *sender) 632 { 633 int vm_running = runstate_is_running(); 634 635 if (!vm_running) { 636 return 0; 637 } 638 639 if (!sender->peer) { 640 return 1; 641 } 642 643 return qemu_can_receive_packet(sender->peer); 644 } 645 646 static ssize_t filter_receive_iov(NetClientState *nc, 647 NetFilterDirection direction, 648 NetClientState *sender, 649 unsigned flags, 650 const struct iovec *iov, 651 int iovcnt, 652 NetPacketSent *sent_cb) 653 { 654 ssize_t ret = 0; 655 NetFilterState *nf = NULL; 656 657 if (direction == NET_FILTER_DIRECTION_TX) { 658 QTAILQ_FOREACH(nf, &nc->filters, next) { 659 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, 660 iovcnt, sent_cb); 661 if (ret) { 662 return ret; 663 } 664 } 665 } else { 666 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) { 667 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, 668 iovcnt, sent_cb); 669 if (ret) { 670 return ret; 671 } 672 } 673 } 674 675 return ret; 676 } 677 678 static ssize_t filter_receive(NetClientState *nc, 679 NetFilterDirection direction, 680 NetClientState *sender, 681 unsigned flags, 682 const uint8_t *data, 683 size_t size, 684 NetPacketSent *sent_cb) 685 { 686 struct iovec iov = { 687 .iov_base = (void *)data, 688 .iov_len = size 689 }; 690 691 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb); 692 } 693 694 void qemu_purge_queued_packets(NetClientState *nc) 695 { 696 if (!nc->peer) { 697 return; 698 } 699 700 qemu_net_queue_purge(nc->peer->incoming_queue, nc); 701 } 702 703 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge) 704 { 705 nc->receive_disabled = 0; 706 707 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) { 708 if (net_hub_flush(nc->peer)) { 709 qemu_notify_event(); 710 } 711 } 712 if (qemu_net_queue_flush(nc->incoming_queue)) { 713 /* We emptied the queue successfully, signal to the IO thread to repoll 714 * the file descriptor (for tap, for example). 715 */ 716 qemu_notify_event(); 717 } else if (purge) { 718 /* Unable to empty the queue, purge remaining packets */ 719 qemu_net_queue_purge(nc->incoming_queue, nc->peer); 720 } 721 } 722 723 void qemu_flush_queued_packets(NetClientState *nc) 724 { 725 qemu_flush_or_purge_queued_packets(nc, false); 726 } 727 728 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender, 729 unsigned flags, 730 const uint8_t *buf, int size, 731 NetPacketSent *sent_cb) 732 { 733 NetQueue *queue; 734 int ret; 735 736 #ifdef DEBUG_NET 737 printf("qemu_send_packet_async:\n"); 738 qemu_hexdump(stdout, "net", buf, size); 739 #endif 740 741 if (sender->link_down || !sender->peer) { 742 return size; 743 } 744 745 /* Let filters handle the packet first */ 746 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX, 747 sender, flags, buf, size, sent_cb); 748 if (ret) { 749 return ret; 750 } 751 752 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX, 753 sender, flags, buf, size, sent_cb); 754 if (ret) { 755 return ret; 756 } 757 758 queue = sender->peer->incoming_queue; 759 760 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); 761 } 762 763 ssize_t qemu_send_packet_async(NetClientState *sender, 764 const uint8_t *buf, int size, 765 NetPacketSent *sent_cb) 766 { 767 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, 768 buf, size, sent_cb); 769 } 770 771 ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size) 772 { 773 return qemu_send_packet_async(nc, buf, size, NULL); 774 } 775 776 ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size) 777 { 778 uint8_t min_pkt[ETH_ZLEN]; 779 size_t min_pktsz = sizeof(min_pkt); 780 781 if (!qemu_can_receive_packet(nc)) { 782 return 0; 783 } 784 785 if (net_peer_needs_padding(nc)) { 786 if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) { 787 buf = min_pkt; 788 size = min_pktsz; 789 } 790 } 791 792 return qemu_net_queue_receive(nc->incoming_queue, buf, size); 793 } 794 795 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) 796 { 797 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW, 798 buf, size, NULL); 799 } 800 801 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov, 802 int iovcnt, unsigned flags) 803 { 804 uint8_t *buf = NULL; 805 uint8_t *buffer; 806 size_t offset; 807 ssize_t ret; 808 809 if (iovcnt == 1) { 810 buffer = iov[0].iov_base; 811 offset = iov[0].iov_len; 812 } else { 813 offset = iov_size(iov, iovcnt); 814 if (offset > NET_BUFSIZE) { 815 return -1; 816 } 817 buf = g_malloc(offset); 818 buffer = buf; 819 offset = iov_to_buf(iov, iovcnt, 0, buf, offset); 820 } 821 822 ret = nc->info->receive(nc, buffer, offset); 823 824 g_free(buf); 825 return ret; 826 } 827 828 static ssize_t qemu_deliver_packet_iov(NetClientState *sender, 829 unsigned flags, 830 const struct iovec *iov, 831 int iovcnt, 832 void *opaque) 833 { 834 MemReentrancyGuard *owned_reentrancy_guard; 835 NetClientState *nc = opaque; 836 int ret; 837 struct virtio_net_hdr_v1_hash vnet_hdr = { }; 838 g_autofree struct iovec *iov_copy = NULL; 839 840 841 if (nc->link_down) { 842 return iov_size(iov, iovcnt); 843 } 844 845 if (nc->receive_disabled) { 846 return 0; 847 } 848 849 if (nc->info->type != NET_CLIENT_DRIVER_NIC || 850 qemu_get_nic(nc)->reentrancy_guard->engaged_in_io) { 851 owned_reentrancy_guard = NULL; 852 } else { 853 owned_reentrancy_guard = qemu_get_nic(nc)->reentrancy_guard; 854 owned_reentrancy_guard->engaged_in_io = true; 855 } 856 857 if ((flags & QEMU_NET_PACKET_FLAG_RAW) && nc->vnet_hdr_len) { 858 iov_copy = g_new(struct iovec, iovcnt + 1); 859 iov_copy[0].iov_base = &vnet_hdr; 860 iov_copy[0].iov_len = nc->vnet_hdr_len; 861 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); 862 iov = iov_copy; 863 iovcnt++; 864 } 865 866 if (nc->info->receive_iov) { 867 ret = nc->info->receive_iov(nc, iov, iovcnt); 868 } else { 869 ret = nc_sendv_compat(nc, iov, iovcnt, flags); 870 } 871 872 if (owned_reentrancy_guard) { 873 owned_reentrancy_guard->engaged_in_io = false; 874 } 875 876 if (ret == 0) { 877 nc->receive_disabled = 1; 878 } 879 880 return ret; 881 } 882 883 ssize_t qemu_sendv_packet_async(NetClientState *sender, 884 const struct iovec *iov, int iovcnt, 885 NetPacketSent *sent_cb) 886 { 887 NetQueue *queue; 888 size_t size = iov_size(iov, iovcnt); 889 int ret; 890 891 if (size > NET_BUFSIZE) { 892 return size; 893 } 894 895 if (sender->link_down || !sender->peer) { 896 return size; 897 } 898 899 /* Let filters handle the packet first */ 900 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender, 901 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); 902 if (ret) { 903 return ret; 904 } 905 906 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender, 907 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); 908 if (ret) { 909 return ret; 910 } 911 912 queue = sender->peer->incoming_queue; 913 914 return qemu_net_queue_send_iov(queue, sender, 915 QEMU_NET_PACKET_FLAG_NONE, 916 iov, iovcnt, sent_cb); 917 } 918 919 ssize_t 920 qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt) 921 { 922 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL); 923 } 924 925 NetClientState *qemu_find_netdev(const char *id) 926 { 927 NetClientState *nc; 928 929 QTAILQ_FOREACH(nc, &net_clients, next) { 930 if (nc->info->type == NET_CLIENT_DRIVER_NIC) 931 continue; 932 if (!strcmp(nc->name, id)) { 933 return nc; 934 } 935 } 936 937 return NULL; 938 } 939 940 int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 941 NetClientDriver type, int max) 942 { 943 NetClientState *nc; 944 int ret = 0; 945 946 QTAILQ_FOREACH(nc, &net_clients, next) { 947 if (nc->info->type == type) { 948 continue; 949 } 950 if (!id || !strcmp(nc->name, id)) { 951 if (ret < max) { 952 ncs[ret] = nc; 953 } 954 ret++; 955 } 956 } 957 958 return ret; 959 } 960 961 static int nic_get_free_idx(void) 962 { 963 int index; 964 965 for (index = 0; index < MAX_NICS; index++) 966 if (!nd_table[index].used) 967 return index; 968 return -1; 969 } 970 971 GPtrArray *qemu_get_nic_models(const char *device_type) 972 { 973 GPtrArray *nic_models = g_ptr_array_new(); 974 GSList *list = object_class_get_list_sorted(device_type, false); 975 976 while (list) { 977 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data, 978 TYPE_DEVICE); 979 GSList *next; 980 if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) && 981 dc->user_creatable) { 982 const char *name = object_class_get_name(list->data); 983 /* 984 * A network device might also be something else than a NIC, see 985 * e.g. the "rocker" device. Thus we have to look for the "netdev" 986 * property, too. Unfortunately, some devices like virtio-net only 987 * create this property during instance_init, so we have to create 988 * a temporary instance here to be able to check it. 989 */ 990 Object *obj = object_new_with_class(OBJECT_CLASS(dc)); 991 if (object_property_find(obj, "netdev")) { 992 g_ptr_array_add(nic_models, (gpointer)name); 993 } 994 object_unref(obj); 995 } 996 next = list->next; 997 g_slist_free_1(list); 998 list = next; 999 } 1000 g_ptr_array_add(nic_models, NULL); 1001 1002 return nic_models; 1003 } 1004 1005 static int net_init_nic(const Netdev *netdev, const char *name, 1006 NetClientState *peer, Error **errp) 1007 { 1008 int idx; 1009 NICInfo *nd; 1010 const NetLegacyNicOptions *nic; 1011 1012 assert(netdev->type == NET_CLIENT_DRIVER_NIC); 1013 nic = &netdev->u.nic; 1014 1015 idx = nic_get_free_idx(); 1016 if (idx == -1 || nb_nics >= MAX_NICS) { 1017 error_setg(errp, "too many NICs"); 1018 return -1; 1019 } 1020 1021 nd = &nd_table[idx]; 1022 1023 memset(nd, 0, sizeof(*nd)); 1024 1025 if (nic->netdev) { 1026 nd->netdev = qemu_find_netdev(nic->netdev); 1027 if (!nd->netdev) { 1028 error_setg(errp, "netdev '%s' not found", nic->netdev); 1029 return -1; 1030 } 1031 } else { 1032 assert(peer); 1033 nd->netdev = peer; 1034 } 1035 nd->name = g_strdup(name); 1036 if (nic->model) { 1037 nd->model = g_strdup(nic->model); 1038 } 1039 if (nic->addr) { 1040 nd->devaddr = g_strdup(nic->addr); 1041 } 1042 1043 if (nic->macaddr && 1044 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { 1045 error_setg(errp, "invalid syntax for ethernet address"); 1046 return -1; 1047 } 1048 if (nic->macaddr && 1049 is_multicast_ether_addr(nd->macaddr.a)) { 1050 error_setg(errp, 1051 "NIC cannot have multicast MAC address (odd 1st byte)"); 1052 return -1; 1053 } 1054 qemu_macaddr_default_if_unset(&nd->macaddr); 1055 1056 if (nic->has_vectors) { 1057 if (nic->vectors > 0x7ffffff) { 1058 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors); 1059 return -1; 1060 } 1061 nd->nvectors = nic->vectors; 1062 } else { 1063 nd->nvectors = DEV_NVECTORS_UNSPECIFIED; 1064 } 1065 1066 nd->used = 1; 1067 nb_nics++; 1068 1069 return idx; 1070 } 1071 1072 static gboolean add_nic_result(gpointer key, gpointer value, gpointer user_data) 1073 { 1074 GPtrArray *results = user_data; 1075 GPtrArray *alias_list = value; 1076 const char *model = key; 1077 char *result; 1078 1079 if (!alias_list) { 1080 result = g_strdup(model); 1081 } else { 1082 GString *result_str = g_string_new(model); 1083 int i; 1084 1085 g_string_append(result_str, " (aka "); 1086 for (i = 0; i < alias_list->len; i++) { 1087 if (i) { 1088 g_string_append(result_str, ", "); 1089 } 1090 g_string_append(result_str, alias_list->pdata[i]); 1091 } 1092 g_string_append(result_str, ")"); 1093 result = result_str->str; 1094 g_string_free(result_str, false); 1095 g_ptr_array_unref(alias_list); 1096 } 1097 g_ptr_array_add(results, result); 1098 return true; 1099 } 1100 1101 static int model_cmp(char **a, char **b) 1102 { 1103 return strcmp(*a, *b); 1104 } 1105 1106 static void show_nic_models(void) 1107 { 1108 GPtrArray *results = g_ptr_array_new(); 1109 int i; 1110 1111 g_hash_table_foreach_remove(nic_model_help, add_nic_result, results); 1112 g_ptr_array_sort(results, (GCompareFunc)model_cmp); 1113 1114 printf("Available NIC models for this configuration:\n"); 1115 for (i = 0 ; i < results->len; i++) { 1116 printf("%s\n", (char *)results->pdata[i]); 1117 } 1118 g_hash_table_unref(nic_model_help); 1119 nic_model_help = NULL; 1120 } 1121 1122 static void add_nic_model_help(const char *model, const char *alias) 1123 { 1124 GPtrArray *alias_list = NULL; 1125 1126 if (g_hash_table_lookup_extended(nic_model_help, model, NULL, 1127 (gpointer *)&alias_list)) { 1128 /* Already exists, no alias to add: return */ 1129 if (!alias) { 1130 return; 1131 } 1132 if (alias_list) { 1133 /* Check if this alias is already in the list. Add if not. */ 1134 if (!g_ptr_array_find_with_equal_func(alias_list, alias, 1135 g_str_equal, NULL)) { 1136 g_ptr_array_add(alias_list, g_strdup(alias)); 1137 } 1138 return; 1139 } 1140 } 1141 /* Either this model wasn't in the list already, or a first alias added */ 1142 if (alias) { 1143 alias_list = g_ptr_array_new(); 1144 g_ptr_array_set_free_func(alias_list, g_free); 1145 g_ptr_array_add(alias_list, g_strdup(alias)); 1146 } 1147 g_hash_table_replace(nic_model_help, g_strdup(model), alias_list); 1148 } 1149 1150 NICInfo *qemu_find_nic_info(const char *typename, bool match_default, 1151 const char *alias) 1152 { 1153 NICInfo *nd; 1154 int i; 1155 1156 if (nic_model_help) { 1157 add_nic_model_help(typename, alias); 1158 } 1159 1160 for (i = 0; i < nb_nics; i++) { 1161 nd = &nd_table[i]; 1162 1163 if (!nd->used || nd->instantiated) { 1164 continue; 1165 } 1166 1167 if ((match_default && !nd->model) || !g_strcmp0(nd->model, typename) 1168 || (alias && !g_strcmp0(nd->model, alias))) { 1169 return nd; 1170 } 1171 } 1172 return NULL; 1173 } 1174 1175 static bool is_nic_model_help_option(const char *model) 1176 { 1177 if (model && is_help_option(model)) { 1178 /* 1179 * Trigger the help output by instantiating the hash table which 1180 * will gather tha available models as they get registered. 1181 */ 1182 if (!nic_model_help) { 1183 nic_model_help = g_hash_table_new_full(g_str_hash, g_str_equal, 1184 g_free, NULL); 1185 } 1186 return true; 1187 } 1188 return false; 1189 } 1190 1191 /* "I have created a device. Please configure it if you can" */ 1192 bool qemu_configure_nic_device(DeviceState *dev, bool match_default, 1193 const char *alias) 1194 { 1195 NICInfo *nd = qemu_find_nic_info(object_get_typename(OBJECT(dev)), 1196 match_default, alias); 1197 1198 if (nd) { 1199 qdev_set_nic_properties(dev, nd); 1200 return true; 1201 } 1202 return false; 1203 } 1204 1205 /* "Please create a device, if you have a configuration for it" */ 1206 DeviceState *qemu_create_nic_device(const char *typename, bool match_default, 1207 const char *alias) 1208 { 1209 NICInfo *nd = qemu_find_nic_info(typename, match_default, alias); 1210 DeviceState *dev; 1211 1212 if (!nd) { 1213 return NULL; 1214 } 1215 1216 dev = qdev_new(typename); 1217 qdev_set_nic_properties(dev, nd); 1218 return dev; 1219 } 1220 1221 void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type, 1222 const char *default_model, 1223 const char *alias, const char *alias_target) 1224 { 1225 GPtrArray *nic_models = qemu_get_nic_models(parent_type); 1226 const char *model; 1227 DeviceState *dev; 1228 NICInfo *nd; 1229 int i; 1230 1231 if (nic_model_help) { 1232 if (alias_target) { 1233 add_nic_model_help(alias_target, alias); 1234 } 1235 for (i = 0; i < nic_models->len - 1; i++) { 1236 add_nic_model_help(nic_models->pdata[i], NULL); 1237 } 1238 } 1239 1240 /* Drop the NULL terminator which would make g_str_equal() unhappy */ 1241 nic_models->len--; 1242 1243 for (i = 0; i < nb_nics; i++) { 1244 nd = &nd_table[i]; 1245 1246 if (!nd->used || nd->instantiated) { 1247 continue; 1248 } 1249 1250 model = nd->model ? nd->model : default_model; 1251 if (!model) { 1252 continue; 1253 } 1254 1255 /* Each bus type is allowed *one* substitution */ 1256 if (g_str_equal(model, alias)) { 1257 model = alias_target; 1258 } 1259 1260 if (!g_ptr_array_find_with_equal_func(nic_models, model, 1261 g_str_equal, NULL)) { 1262 /* This NIC does not live on this bus. */ 1263 continue; 1264 } 1265 1266 dev = qdev_new(model); 1267 qdev_set_nic_properties(dev, nd); 1268 qdev_realize_and_unref(dev, bus, &error_fatal); 1269 } 1270 1271 g_ptr_array_free(nic_models, true); 1272 } 1273 1274 static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])( 1275 const Netdev *netdev, 1276 const char *name, 1277 NetClientState *peer, Error **errp) = { 1278 [NET_CLIENT_DRIVER_NIC] = net_init_nic, 1279 #ifdef CONFIG_PASST 1280 [NET_CLIENT_DRIVER_PASST] = net_init_passt, 1281 #endif 1282 #ifdef CONFIG_SLIRP 1283 [NET_CLIENT_DRIVER_USER] = net_init_slirp, 1284 #endif 1285 [NET_CLIENT_DRIVER_TAP] = net_init_tap, 1286 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket, 1287 [NET_CLIENT_DRIVER_STREAM] = net_init_stream, 1288 [NET_CLIENT_DRIVER_DGRAM] = net_init_dgram, 1289 #ifdef CONFIG_VDE 1290 [NET_CLIENT_DRIVER_VDE] = net_init_vde, 1291 #endif 1292 #ifdef CONFIG_NETMAP 1293 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap, 1294 #endif 1295 #ifdef CONFIG_AF_XDP 1296 [NET_CLIENT_DRIVER_AF_XDP] = net_init_af_xdp, 1297 #endif 1298 #ifdef CONFIG_NET_BRIDGE 1299 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge, 1300 #endif 1301 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport, 1302 #ifdef CONFIG_VHOST_NET_USER 1303 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user, 1304 #endif 1305 #ifdef CONFIG_VHOST_NET_VDPA 1306 [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa, 1307 #endif 1308 #ifdef CONFIG_L2TPV3 1309 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3, 1310 #endif 1311 #ifdef CONFIG_VMNET 1312 [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host, 1313 [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared, 1314 [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged, 1315 #endif /* CONFIG_VMNET */ 1316 }; 1317 1318 1319 static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp) 1320 { 1321 NetClientState *peer = NULL; 1322 NetClientState *nc; 1323 1324 if (is_netdev) { 1325 if (netdev->type == NET_CLIENT_DRIVER_NIC || 1326 !net_client_init_fun[netdev->type]) { 1327 error_setg(errp, "network backend '%s' is not compiled into this binary", 1328 NetClientDriver_str(netdev->type)); 1329 return -1; 1330 } 1331 } else { 1332 if (netdev->type == NET_CLIENT_DRIVER_NONE) { 1333 return 0; /* nothing to do */ 1334 } 1335 if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) { 1336 error_setg(errp, "network backend '%s' is only supported with -netdev/-nic", 1337 NetClientDriver_str(netdev->type)); 1338 return -1; 1339 } 1340 1341 if (!net_client_init_fun[netdev->type]) { 1342 error_setg(errp, "network backend '%s' is not compiled into this binary", 1343 NetClientDriver_str(netdev->type)); 1344 return -1; 1345 } 1346 1347 /* Do not add to a hub if it's a nic with a netdev= parameter. */ 1348 if (netdev->type != NET_CLIENT_DRIVER_NIC || 1349 !netdev->u.nic.netdev) { 1350 peer = net_hub_add_port(0, NULL, NULL); 1351 } 1352 } 1353 1354 nc = qemu_find_netdev(netdev->id); 1355 if (nc) { 1356 error_setg(errp, "Duplicate ID '%s'", netdev->id); 1357 return -1; 1358 } 1359 1360 if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) { 1361 /* FIXME drop when all init functions store an Error */ 1362 if (errp && !*errp) { 1363 error_setg(errp, "Device '%s' could not be initialized", 1364 NetClientDriver_str(netdev->type)); 1365 } 1366 return -1; 1367 } 1368 1369 if (is_netdev) { 1370 nc = qemu_find_netdev(netdev->id); 1371 assert(nc); 1372 nc->is_netdev = true; 1373 } 1374 1375 return 0; 1376 } 1377 1378 void show_netdevs(void) 1379 { 1380 int idx; 1381 const char *available_netdevs[] = { 1382 "socket", 1383 "stream", 1384 "dgram", 1385 "hubport", 1386 "tap", 1387 "passt", 1388 #ifdef CONFIG_SLIRP 1389 "user", 1390 #endif 1391 #ifdef CONFIG_L2TPV3 1392 "l2tpv3", 1393 #endif 1394 #ifdef CONFIG_VDE 1395 "vde", 1396 #endif 1397 #ifdef CONFIG_NET_BRIDGE 1398 "bridge", 1399 #endif 1400 #ifdef CONFIG_NETMAP 1401 "netmap", 1402 #endif 1403 #ifdef CONFIG_AF_XDP 1404 "af-xdp", 1405 #endif 1406 #ifdef CONFIG_POSIX 1407 "vhost-user", 1408 #endif 1409 #ifdef CONFIG_VHOST_VDPA 1410 "vhost-vdpa", 1411 #endif 1412 #ifdef CONFIG_VMNET 1413 "vmnet-host", 1414 "vmnet-shared", 1415 "vmnet-bridged", 1416 #endif 1417 }; 1418 1419 qemu_printf("Available netdev backend types:\n"); 1420 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) { 1421 qemu_printf("%s\n", available_netdevs[idx]); 1422 } 1423 } 1424 1425 static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp) 1426 { 1427 gchar **substrings = NULL; 1428 Netdev *object = NULL; 1429 int ret = -1; 1430 Visitor *v = opts_visitor_new(opts); 1431 1432 /* Parse convenience option format ipv6-net=fec0::0[/64] */ 1433 const char *ip6_net = qemu_opt_get(opts, "ipv6-net"); 1434 1435 if (ip6_net) { 1436 char *prefix_addr; 1437 unsigned long prefix_len = 64; /* Default 64bit prefix length. */ 1438 1439 substrings = g_strsplit(ip6_net, "/", 2); 1440 if (!substrings || !substrings[0]) { 1441 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net", 1442 "a valid IPv6 prefix"); 1443 goto out; 1444 } 1445 1446 prefix_addr = substrings[0]; 1447 1448 /* Handle user-specified prefix length. */ 1449 if (substrings[1] && 1450 qemu_strtoul(substrings[1], NULL, 10, &prefix_len)) 1451 { 1452 error_setg(errp, 1453 "parameter 'ipv6-net' expects a number after '/'"); 1454 goto out; 1455 } 1456 1457 qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort); 1458 qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len, 1459 &error_abort); 1460 qemu_opt_unset(opts, "ipv6-net"); 1461 } 1462 1463 /* Create an ID for -net if the user did not specify one */ 1464 if (!is_netdev && !qemu_opts_id(opts)) { 1465 qemu_opts_set_id(opts, id_generate(ID_NET)); 1466 } 1467 1468 if (visit_type_Netdev(v, NULL, &object, errp)) { 1469 ret = net_client_init1(object, is_netdev, errp); 1470 } 1471 1472 qapi_free_Netdev(object); 1473 1474 out: 1475 g_strfreev(substrings); 1476 visit_free(v); 1477 return ret; 1478 } 1479 1480 void netdev_add(QemuOpts *opts, Error **errp) 1481 { 1482 net_client_init(opts, true, errp); 1483 } 1484 1485 void qmp_netdev_add(Netdev *netdev, Error **errp) 1486 { 1487 if (!id_wellformed(netdev->id)) { 1488 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); 1489 return; 1490 } 1491 1492 net_client_init1(netdev, true, errp); 1493 } 1494 1495 void qmp_netdev_del(const char *id, Error **errp) 1496 { 1497 NetClientState *nc; 1498 QemuOpts *opts; 1499 1500 nc = qemu_find_netdev(id); 1501 if (!nc) { 1502 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1503 "Device '%s' not found", id); 1504 return; 1505 } 1506 1507 if (!nc->is_netdev) { 1508 error_setg(errp, "Device '%s' is not a netdev", id); 1509 return; 1510 } 1511 1512 qemu_del_net_client(nc); 1513 1514 /* 1515 * Wart: we need to delete the QemuOpts associated with netdevs 1516 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in 1517 * HMP netdev_add. 1518 */ 1519 opts = qemu_opts_find(qemu_find_opts("netdev"), id); 1520 if (opts) { 1521 qemu_opts_del(opts); 1522 } 1523 } 1524 1525 static void netfilter_print_info(Monitor *mon, NetFilterState *nf) 1526 { 1527 char *str; 1528 ObjectProperty *prop; 1529 ObjectPropertyIterator iter; 1530 Visitor *v; 1531 1532 /* generate info str */ 1533 object_property_iter_init(&iter, OBJECT(nf)); 1534 while ((prop = object_property_iter_next(&iter))) { 1535 if (!strcmp(prop->name, "type")) { 1536 continue; 1537 } 1538 v = string_output_visitor_new(false, &str); 1539 object_property_get(OBJECT(nf), prop->name, v, NULL); 1540 visit_complete(v, &str); 1541 visit_free(v); 1542 monitor_printf(mon, ",%s=%s", prop->name, str); 1543 g_free(str); 1544 } 1545 monitor_printf(mon, "\n"); 1546 } 1547 1548 void print_net_client(Monitor *mon, NetClientState *nc) 1549 { 1550 NetFilterState *nf; 1551 1552 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name, 1553 nc->queue_index, 1554 NetClientDriver_str(nc->info->type), 1555 nc->info_str); 1556 if (!QTAILQ_EMPTY(&nc->filters)) { 1557 monitor_printf(mon, "filters:\n"); 1558 } 1559 QTAILQ_FOREACH(nf, &nc->filters, next) { 1560 monitor_printf(mon, " - %s: type=%s", 1561 object_get_canonical_path_component(OBJECT(nf)), 1562 object_get_typename(OBJECT(nf))); 1563 netfilter_print_info(mon, nf); 1564 } 1565 } 1566 1567 RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp) 1568 { 1569 NetClientState *nc; 1570 RxFilterInfoList *filter_list = NULL, **tail = &filter_list; 1571 1572 QTAILQ_FOREACH(nc, &net_clients, next) { 1573 RxFilterInfo *info; 1574 1575 if (name && strcmp(nc->name, name) != 0) { 1576 continue; 1577 } 1578 1579 /* only query rx-filter information of NIC */ 1580 if (nc->info->type != NET_CLIENT_DRIVER_NIC) { 1581 if (name) { 1582 error_setg(errp, "net client(%s) isn't a NIC", name); 1583 assert(!filter_list); 1584 return NULL; 1585 } 1586 continue; 1587 } 1588 1589 /* only query information on queue 0 since the info is per nic, 1590 * not per queue 1591 */ 1592 if (nc->queue_index != 0) 1593 continue; 1594 1595 if (nc->info->query_rx_filter) { 1596 info = nc->info->query_rx_filter(nc); 1597 QAPI_LIST_APPEND(tail, info); 1598 } else if (name) { 1599 error_setg(errp, "net client(%s) doesn't support" 1600 " rx-filter querying", name); 1601 assert(!filter_list); 1602 return NULL; 1603 } 1604 1605 if (name) { 1606 break; 1607 } 1608 } 1609 1610 if (filter_list == NULL && name) { 1611 error_setg(errp, "invalid net client name: %s", name); 1612 } 1613 1614 return filter_list; 1615 } 1616 1617 void colo_notify_filters_event(int event, Error **errp) 1618 { 1619 NetClientState *nc; 1620 NetFilterState *nf; 1621 NetFilterClass *nfc = NULL; 1622 Error *local_err = NULL; 1623 1624 QTAILQ_FOREACH(nc, &net_clients, next) { 1625 QTAILQ_FOREACH(nf, &nc->filters, next) { 1626 nfc = NETFILTER_GET_CLASS(OBJECT(nf)); 1627 nfc->handle_event(nf, event, &local_err); 1628 if (local_err) { 1629 error_propagate(errp, local_err); 1630 return; 1631 } 1632 } 1633 } 1634 } 1635 1636 void net_client_set_link(NetClientState **ncs, int queues, bool up) 1637 { 1638 NetClientState *nc; 1639 int i; 1640 1641 nc = ncs[0]; 1642 1643 for (i = 0; i < queues; i++) { 1644 ncs[i]->link_down = !up; 1645 } 1646 1647 if (nc->info->link_status_changed) { 1648 nc->info->link_status_changed(nc); 1649 } 1650 1651 if (nc->peer) { 1652 /* Change peer link only if the peer is NIC and then notify peer. 1653 * If the peer is a HUBPORT or a backend, we do not change the 1654 * link status. 1655 * 1656 * This behavior is compatible with qemu hubs where there could be 1657 * multiple clients that can still communicate with each other in 1658 * disconnected mode. For now maintain this compatibility. 1659 */ 1660 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { 1661 for (i = 0; i < queues; i++) { 1662 ncs[i]->peer->link_down = !up; 1663 } 1664 } 1665 if (nc->peer->info->link_status_changed) { 1666 nc->peer->info->link_status_changed(nc->peer); 1667 } 1668 } 1669 } 1670 1671 void qmp_set_link(const char *name, bool up, Error **errp) 1672 { 1673 NetClientState *ncs[MAX_QUEUE_NUM]; 1674 int queues; 1675 1676 queues = qemu_find_net_clients_except(name, ncs, 1677 NET_CLIENT_DRIVER__MAX, 1678 MAX_QUEUE_NUM); 1679 1680 if (queues == 0) { 1681 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1682 "Device '%s' not found", name); 1683 return; 1684 } 1685 1686 net_client_set_link(ncs, queues, up); 1687 } 1688 1689 static void net_vm_change_state_handler(void *opaque, bool running, 1690 RunState state) 1691 { 1692 NetClientState *nc; 1693 NetClientState *tmp; 1694 1695 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { 1696 if (running) { 1697 /* Flush queued packets and wake up backends. */ 1698 if (nc->peer && qemu_can_send_packet(nc)) { 1699 qemu_flush_queued_packets(nc->peer); 1700 } 1701 } else { 1702 /* Complete all queued packets, to guarantee we don't modify 1703 * state later when VM is not running. 1704 */ 1705 qemu_flush_or_purge_queued_packets(nc, true); 1706 } 1707 } 1708 } 1709 1710 void net_cleanup(void) 1711 { 1712 NetClientState *nc, **p = &QTAILQ_FIRST(&net_clients); 1713 1714 /*cleanup colo compare module for COLO*/ 1715 colo_compare_cleanup(); 1716 1717 /* 1718 * Walk the net_clients list and remove the netdevs but *not* any 1719 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device 1720 * model which created them, and in some cases (e.g. xen-net-device) 1721 * the device itself may do cleanup at exit and will be upset if we 1722 * just delete its NIC from underneath it. 1723 * 1724 * Since qemu_del_net_client() may delete multiple entries, using 1725 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer 1726 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep 1727 * 'p' pointing to either the head of the list, or the 'next' field 1728 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk 1729 * the list. 1730 * 1731 * However, the NIC may have peers that trust to be clean beyond this 1732 * point. For example, if they have been removed with device_del. 1733 * 1734 * The 'nc' variable isn't part of the list traversal; it's purely 1735 * for convenience as too much '(*p)->' has a tendency to make the 1736 * readers' eyes bleed. 1737 */ 1738 while (*p) { 1739 nc = *p; 1740 if (nc->info->type == NET_CLIENT_DRIVER_NIC) { 1741 NICState *nic = qemu_get_nic(nc); 1742 1743 if (nic->peer_deleted) { 1744 int queues = MAX(nic->conf->peers.queues, 1); 1745 1746 for (int i = 0; i < queues; i++) { 1747 nc = qemu_get_subqueue(nic, i); 1748 qemu_cleanup_net_client(nc->peer, false); 1749 } 1750 } 1751 1752 /* Skip NET_CLIENT_DRIVER_NIC entries */ 1753 p = &QTAILQ_NEXT(nc, next); 1754 } else { 1755 qemu_del_net_client(nc); 1756 } 1757 } 1758 1759 qemu_del_vm_change_state_handler(net_change_state_entry); 1760 } 1761 1762 void net_check_clients(void) 1763 { 1764 NetClientState *nc; 1765 int i; 1766 1767 if (nic_model_help) { 1768 show_nic_models(); 1769 exit(0); 1770 } 1771 net_hub_check_clients(); 1772 1773 QTAILQ_FOREACH(nc, &net_clients, next) { 1774 if (!nc->peer) { 1775 warn_report("%s %s has no peer", 1776 nc->info->type == NET_CLIENT_DRIVER_NIC 1777 ? "nic" : "netdev", 1778 nc->name); 1779 } 1780 } 1781 1782 /* Check that all NICs requested via -net nic actually got created. 1783 * NICs created via -device don't need to be checked here because 1784 * they are always instantiated. 1785 */ 1786 for (i = 0; i < MAX_NICS; i++) { 1787 NICInfo *nd = &nd_table[i]; 1788 if (nd->used && !nd->instantiated) { 1789 warn_report("requested NIC (%s, model %s) " 1790 "was not created (not supported by this machine?)", 1791 nd->name ? nd->name : "anonymous", 1792 nd->model ? nd->model : "unspecified"); 1793 } 1794 } 1795 } 1796 1797 static int net_init_client(void *dummy, QemuOpts *opts, Error **errp) 1798 { 1799 const char *model = qemu_opt_get(opts, "model"); 1800 1801 if (is_nic_model_help_option(model)) { 1802 return 0; 1803 } 1804 1805 return net_client_init(opts, false, errp); 1806 } 1807 1808 static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp) 1809 { 1810 const char *type = qemu_opt_get(opts, "type"); 1811 1812 if (type && is_help_option(type)) { 1813 show_netdevs(); 1814 exit(0); 1815 } 1816 return net_client_init(opts, true, errp); 1817 } 1818 1819 /* For the convenience "--nic" parameter */ 1820 static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) 1821 { 1822 char *mac, *nd_id; 1823 int idx, ret; 1824 NICInfo *ni; 1825 const char *type; 1826 1827 type = qemu_opt_get(opts, "type"); 1828 if (type) { 1829 if (g_str_equal(type, "none")) { 1830 return 0; /* Nothing to do, default_net is cleared in vl.c */ 1831 } 1832 if (is_help_option(type)) { 1833 GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE); 1834 int i; 1835 show_netdevs(); 1836 printf("\n"); 1837 printf("Available NIC models " 1838 "(use -nic model=help for a filtered list):\n"); 1839 for (i = 0 ; nic_models->pdata[i]; i++) { 1840 printf("%s\n", (char *)nic_models->pdata[i]); 1841 } 1842 g_ptr_array_free(nic_models, true); 1843 exit(0); 1844 } 1845 } 1846 1847 idx = nic_get_free_idx(); 1848 if (idx == -1 || nb_nics >= MAX_NICS) { 1849 error_setg(errp, "no more on-board/default NIC slots available"); 1850 return -1; 1851 } 1852 1853 if (!type) { 1854 qemu_opt_set(opts, "type", "user", &error_abort); 1855 } 1856 1857 ni = &nd_table[idx]; 1858 memset(ni, 0, sizeof(*ni)); 1859 ni->model = qemu_opt_get_del(opts, "model"); 1860 1861 if (is_nic_model_help_option(ni->model)) { 1862 return 0; 1863 } 1864 1865 /* Create an ID if the user did not specify one */ 1866 nd_id = g_strdup(qemu_opts_id(opts)); 1867 if (!nd_id) { 1868 nd_id = id_generate(ID_NET); 1869 qemu_opts_set_id(opts, nd_id); 1870 } 1871 1872 /* Handle MAC address */ 1873 mac = qemu_opt_get_del(opts, "mac"); 1874 if (mac) { 1875 ret = net_parse_macaddr(ni->macaddr.a, mac); 1876 g_free(mac); 1877 if (ret) { 1878 error_setg(errp, "invalid syntax for ethernet address"); 1879 goto out; 1880 } 1881 if (is_multicast_ether_addr(ni->macaddr.a)) { 1882 error_setg(errp, "NIC cannot have multicast MAC address"); 1883 ret = -1; 1884 goto out; 1885 } 1886 } 1887 qemu_macaddr_default_if_unset(&ni->macaddr); 1888 1889 ret = net_client_init(opts, true, errp); 1890 if (ret == 0) { 1891 ni->netdev = qemu_find_netdev(nd_id); 1892 ni->used = true; 1893 nb_nics++; 1894 } 1895 1896 out: 1897 g_free(nd_id); 1898 return ret; 1899 } 1900 1901 static void netdev_init_modern(void) 1902 { 1903 while (!QSIMPLEQ_EMPTY(&nd_queue)) { 1904 NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue); 1905 1906 QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry); 1907 loc_push_restore(&nd->loc); 1908 net_client_init1(nd->nd, true, &error_fatal); 1909 loc_pop(&nd->loc); 1910 qapi_free_Netdev(nd->nd); 1911 g_free(nd); 1912 } 1913 } 1914 1915 void net_init_clients(void) 1916 { 1917 net_change_state_entry = 1918 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL); 1919 1920 QTAILQ_INIT(&net_clients); 1921 1922 netdev_init_modern(); 1923 1924 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1925 &error_fatal); 1926 1927 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, 1928 &error_fatal); 1929 1930 qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, 1931 &error_fatal); 1932 } 1933 1934 /* 1935 * Does this -netdev argument use modern rather than traditional syntax? 1936 * Modern syntax is to be parsed with netdev_parse_modern(). 1937 * Traditional syntax is to be parsed with net_client_parse(). 1938 */ 1939 bool netdev_is_modern(const char *optstr) 1940 { 1941 QemuOpts *opts; 1942 bool is_modern; 1943 const char *type; 1944 static QemuOptsList dummy_opts = { 1945 .name = "netdev", 1946 .implied_opt_name = "type", 1947 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), 1948 .desc = { { } }, 1949 }; 1950 1951 if (optstr[0] == '{') { 1952 /* This is JSON, which means it's modern syntax */ 1953 return true; 1954 } 1955 1956 opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort); 1957 qemu_opts_do_parse(opts, optstr, dummy_opts.implied_opt_name, 1958 &error_abort); 1959 type = qemu_opt_get(opts, "type"); 1960 is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram"); 1961 1962 qemu_opts_reset(&dummy_opts); 1963 1964 return is_modern; 1965 } 1966 1967 /* 1968 * netdev_parse_modern() uses modern, more expressive syntax than 1969 * net_client_parse(), but supports only the -netdev option. 1970 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse() 1971 * appends to @qemu_netdev_opts. 1972 */ 1973 void netdev_parse_modern(const char *optstr) 1974 { 1975 Visitor *v; 1976 NetdevQueueEntry *nd; 1977 1978 v = qobject_input_visitor_new_str(optstr, "type", &error_fatal); 1979 nd = g_new(NetdevQueueEntry, 1); 1980 visit_type_Netdev(v, NULL, &nd->nd, &error_fatal); 1981 visit_free(v); 1982 loc_save(&nd->loc); 1983 1984 QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry); 1985 } 1986 1987 void net_client_parse(QemuOptsList *opts_list, const char *optstr) 1988 { 1989 if (!qemu_opts_parse_noisily(opts_list, optstr, true)) { 1990 exit(1); 1991 } 1992 } 1993 1994 /* From FreeBSD */ 1995 /* XXX: optimize */ 1996 uint32_t net_crc32(const uint8_t *p, int len) 1997 { 1998 uint32_t crc; 1999 int carry, i, j; 2000 uint8_t b; 2001 2002 crc = 0xffffffff; 2003 for (i = 0; i < len; i++) { 2004 b = *p++; 2005 for (j = 0; j < 8; j++) { 2006 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); 2007 crc <<= 1; 2008 b >>= 1; 2009 if (carry) { 2010 crc = ((crc ^ POLYNOMIAL_BE) | carry); 2011 } 2012 } 2013 } 2014 2015 return crc; 2016 } 2017 2018 uint32_t net_crc32_le(const uint8_t *p, int len) 2019 { 2020 uint32_t crc; 2021 int carry, i, j; 2022 uint8_t b; 2023 2024 crc = 0xffffffff; 2025 for (i = 0; i < len; i++) { 2026 b = *p++; 2027 for (j = 0; j < 8; j++) { 2028 carry = (crc & 0x1) ^ (b & 0x01); 2029 crc >>= 1; 2030 b >>= 1; 2031 if (carry) { 2032 crc ^= POLYNOMIAL_LE; 2033 } 2034 } 2035 } 2036 2037 return crc; 2038 } 2039 2040 QemuOptsList qemu_netdev_opts = { 2041 .name = "netdev", 2042 .implied_opt_name = "type", 2043 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), 2044 .desc = { 2045 /* 2046 * no elements => accept any params 2047 * validation will happen later 2048 */ 2049 { /* end of list */ } 2050 }, 2051 }; 2052 2053 QemuOptsList qemu_nic_opts = { 2054 .name = "nic", 2055 .implied_opt_name = "type", 2056 .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head), 2057 .desc = { 2058 /* 2059 * no elements => accept any params 2060 * validation will happen later 2061 */ 2062 { /* end of list */ } 2063 }, 2064 }; 2065 2066 QemuOptsList qemu_net_opts = { 2067 .name = "net", 2068 .implied_opt_name = "type", 2069 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), 2070 .desc = { 2071 /* 2072 * no elements => accept any params 2073 * validation will happen later 2074 */ 2075 { /* end of list */ } 2076 }, 2077 }; 2078 2079 void net_socket_rs_init(SocketReadState *rs, 2080 SocketReadStateFinalize *finalize, 2081 bool vnet_hdr) 2082 { 2083 rs->state = 0; 2084 rs->vnet_hdr = vnet_hdr; 2085 rs->index = 0; 2086 rs->packet_len = 0; 2087 rs->vnet_hdr_len = 0; 2088 memset(rs->buf, 0, sizeof(rs->buf)); 2089 rs->finalize = finalize; 2090 } 2091 2092 /* 2093 * Returns 2094 * 0: success 2095 * -1: error occurs 2096 */ 2097 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size) 2098 { 2099 unsigned int l; 2100 2101 while (size > 0) { 2102 /* Reassemble a packet from the network. 2103 * 0 = getting length. 2104 * 1 = getting vnet header length. 2105 * 2 = getting data. 2106 */ 2107 switch (rs->state) { 2108 case 0: 2109 l = 4 - rs->index; 2110 if (l > size) { 2111 l = size; 2112 } 2113 memcpy(rs->buf + rs->index, buf, l); 2114 buf += l; 2115 size -= l; 2116 rs->index += l; 2117 if (rs->index == 4) { 2118 /* got length */ 2119 rs->packet_len = ntohl(*(uint32_t *)rs->buf); 2120 rs->index = 0; 2121 if (rs->vnet_hdr) { 2122 rs->state = 1; 2123 } else { 2124 rs->state = 2; 2125 rs->vnet_hdr_len = 0; 2126 } 2127 } 2128 break; 2129 case 1: 2130 l = 4 - rs->index; 2131 if (l > size) { 2132 l = size; 2133 } 2134 memcpy(rs->buf + rs->index, buf, l); 2135 buf += l; 2136 size -= l; 2137 rs->index += l; 2138 if (rs->index == 4) { 2139 /* got vnet header length */ 2140 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf); 2141 rs->index = 0; 2142 rs->state = 2; 2143 } 2144 break; 2145 case 2: 2146 l = rs->packet_len - rs->index; 2147 if (l > size) { 2148 l = size; 2149 } 2150 if (rs->index + l <= sizeof(rs->buf)) { 2151 memcpy(rs->buf + rs->index, buf, l); 2152 } else { 2153 fprintf(stderr, "serious error: oversized packet received," 2154 "connection terminated.\n"); 2155 rs->index = rs->state = 0; 2156 return -1; 2157 } 2158 2159 rs->index += l; 2160 buf += l; 2161 size -= l; 2162 if (rs->index >= rs->packet_len) { 2163 rs->index = 0; 2164 rs->state = 0; 2165 assert(rs->finalize); 2166 rs->finalize(rs); 2167 } 2168 break; 2169 } 2170 } 2171 2172 assert(size == 0); 2173 return 0; 2174 } 2175