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