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