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