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 #include "qemu/osdep.h" 25 26 #include "net/net.h" 27 #include "clients.h" 28 #include "hub.h" 29 #include "net/slirp.h" 30 #include "net/eth.h" 31 #include "util.h" 32 33 #include "monitor/monitor.h" 34 #include "qemu-common.h" 35 #include "qemu/help_option.h" 36 #include "qapi/qmp/qerror.h" 37 #include "qemu/error-report.h" 38 #include "qemu/sockets.h" 39 #include "qemu/cutils.h" 40 #include "qemu/config-file.h" 41 #include "qmp-commands.h" 42 #include "hw/qdev.h" 43 #include "qemu/iov.h" 44 #include "qemu/main-loop.h" 45 #include "qapi-visit.h" 46 #include "qapi/opts-visitor.h" 47 #include "sysemu/sysemu.h" 48 #include "sysemu/qtest.h" 49 #include "net/filter.h" 50 #include "qapi/string-output-visitor.h" 51 52 /* Net bridge is currently not supported for W32. */ 53 #if !defined(_WIN32) 54 # define CONFIG_NET_BRIDGE 55 #endif 56 57 static VMChangeStateEntry *net_change_state_entry; 58 static QTAILQ_HEAD(, NetClientState) net_clients; 59 60 const char *host_net_devices[] = { 61 "tap", 62 "socket", 63 "dump", 64 #ifdef CONFIG_NET_BRIDGE 65 "bridge", 66 #endif 67 #ifdef CONFIG_NETMAP 68 "netmap", 69 #endif 70 #ifdef CONFIG_SLIRP 71 "user", 72 #endif 73 #ifdef CONFIG_VDE 74 "vde", 75 #endif 76 "vhost-user", 77 NULL, 78 }; 79 80 /***********************************************************/ 81 /* network device redirectors */ 82 83 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) 84 { 85 const char *p, *p1; 86 int len; 87 p = *pp; 88 p1 = strchr(p, sep); 89 if (!p1) 90 return -1; 91 len = p1 - p; 92 p1++; 93 if (buf_size > 0) { 94 if (len > buf_size - 1) 95 len = buf_size - 1; 96 memcpy(buf, p, len); 97 buf[len] = '\0'; 98 } 99 *pp = p1; 100 return 0; 101 } 102 103 int parse_host_port(struct sockaddr_in *saddr, const char *str, 104 Error **errp) 105 { 106 char buf[512]; 107 struct hostent *he; 108 const char *p, *r; 109 int port; 110 111 p = str; 112 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { 113 error_setg(errp, "host address '%s' doesn't contain ':' " 114 "separating host from port", str); 115 return -1; 116 } 117 saddr->sin_family = AF_INET; 118 if (buf[0] == '\0') { 119 saddr->sin_addr.s_addr = 0; 120 } else { 121 if (qemu_isdigit(buf[0])) { 122 if (!inet_aton(buf, &saddr->sin_addr)) { 123 error_setg(errp, "host address '%s' is not a valid " 124 "IPv4 address", buf); 125 return -1; 126 } 127 } else { 128 he = gethostbyname(buf); 129 if (he == NULL) { 130 error_setg(errp, "can't resolve host address '%s'", buf); 131 return - 1; 132 } 133 saddr->sin_addr = *(struct in_addr *)he->h_addr; 134 } 135 } 136 port = strtol(p, (char **)&r, 0); 137 if (r == p) { 138 error_setg(errp, "port number '%s' is invalid", p); 139 return -1; 140 } 141 saddr->sin_port = htons(port); 142 return 0; 143 } 144 145 char *qemu_mac_strdup_printf(const uint8_t *macaddr) 146 { 147 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", 148 macaddr[0], macaddr[1], macaddr[2], 149 macaddr[3], macaddr[4], macaddr[5]); 150 } 151 152 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) 153 { 154 snprintf(nc->info_str, sizeof(nc->info_str), 155 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", 156 nc->model, 157 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 252 static void qemu_net_client_setup(NetClientState *nc, 253 NetClientInfo *info, 254 NetClientState *peer, 255 const char *model, 256 const char *name, 257 NetClientDestructor *destructor) 258 { 259 nc->info = info; 260 nc->model = g_strdup(model); 261 if (name) { 262 nc->name = g_strdup(name); 263 } else { 264 nc->name = assign_name(nc, model); 265 } 266 267 if (peer) { 268 assert(!peer->peer); 269 nc->peer = peer; 270 peer->peer = nc; 271 } 272 QTAILQ_INSERT_TAIL(&net_clients, nc, next); 273 274 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc); 275 nc->destructor = destructor; 276 QTAILQ_INIT(&nc->filters); 277 } 278 279 NetClientState *qemu_new_net_client(NetClientInfo *info, 280 NetClientState *peer, 281 const char *model, 282 const char *name) 283 { 284 NetClientState *nc; 285 286 assert(info->size >= sizeof(NetClientState)); 287 288 nc = g_malloc0(info->size); 289 qemu_net_client_setup(nc, info, peer, model, name, 290 qemu_net_client_destructor); 291 292 return nc; 293 } 294 295 NICState *qemu_new_nic(NetClientInfo *info, 296 NICConf *conf, 297 const char *model, 298 const char *name, 299 void *opaque) 300 { 301 NetClientState **peers = conf->peers.ncs; 302 NICState *nic; 303 int i, queues = MAX(1, conf->peers.queues); 304 305 assert(info->type == NET_CLIENT_DRIVER_NIC); 306 assert(info->size >= sizeof(NICState)); 307 308 nic = g_malloc0(info->size + sizeof(NetClientState) * queues); 309 nic->ncs = (void *)nic + info->size; 310 nic->conf = conf; 311 nic->opaque = opaque; 312 313 for (i = 0; i < queues; i++) { 314 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name, 315 NULL); 316 nic->ncs[i].queue_index = i; 317 } 318 319 return nic; 320 } 321 322 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index) 323 { 324 return nic->ncs + queue_index; 325 } 326 327 NetClientState *qemu_get_queue(NICState *nic) 328 { 329 return qemu_get_subqueue(nic, 0); 330 } 331 332 NICState *qemu_get_nic(NetClientState *nc) 333 { 334 NetClientState *nc0 = nc - nc->queue_index; 335 336 return (NICState *)((void *)nc0 - nc->info->size); 337 } 338 339 void *qemu_get_nic_opaque(NetClientState *nc) 340 { 341 NICState *nic = qemu_get_nic(nc); 342 343 return nic->opaque; 344 } 345 346 static void qemu_cleanup_net_client(NetClientState *nc) 347 { 348 QTAILQ_REMOVE(&net_clients, nc, next); 349 350 if (nc->info->cleanup) { 351 nc->info->cleanup(nc); 352 } 353 } 354 355 static void qemu_free_net_client(NetClientState *nc) 356 { 357 if (nc->incoming_queue) { 358 qemu_del_net_queue(nc->incoming_queue); 359 } 360 if (nc->peer) { 361 nc->peer->peer = NULL; 362 } 363 g_free(nc->name); 364 g_free(nc->model); 365 if (nc->destructor) { 366 nc->destructor(nc); 367 } 368 } 369 370 void qemu_del_net_client(NetClientState *nc) 371 { 372 NetClientState *ncs[MAX_QUEUE_NUM]; 373 int queues, i; 374 NetFilterState *nf, *next; 375 376 assert(nc->info->type != NET_CLIENT_DRIVER_NIC); 377 378 /* If the NetClientState belongs to a multiqueue backend, we will change all 379 * other NetClientStates also. 380 */ 381 queues = qemu_find_net_clients_except(nc->name, ncs, 382 NET_CLIENT_DRIVER_NIC, 383 MAX_QUEUE_NUM); 384 assert(queues != 0); 385 386 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) { 387 object_unparent(OBJECT(nf)); 388 } 389 390 /* If there is a peer NIC, delete and cleanup client, but do not free. */ 391 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { 392 NICState *nic = qemu_get_nic(nc->peer); 393 if (nic->peer_deleted) { 394 return; 395 } 396 nic->peer_deleted = true; 397 398 for (i = 0; i < queues; i++) { 399 ncs[i]->peer->link_down = true; 400 } 401 402 if (nc->peer->info->link_status_changed) { 403 nc->peer->info->link_status_changed(nc->peer); 404 } 405 406 for (i = 0; i < queues; i++) { 407 qemu_cleanup_net_client(ncs[i]); 408 } 409 410 return; 411 } 412 413 for (i = 0; i < queues; i++) { 414 qemu_cleanup_net_client(ncs[i]); 415 qemu_free_net_client(ncs[i]); 416 } 417 } 418 419 void qemu_del_nic(NICState *nic) 420 { 421 int i, queues = MAX(nic->conf->peers.queues, 1); 422 423 qemu_macaddr_set_free(&nic->conf->macaddr); 424 425 /* If this is a peer NIC and peer has already been deleted, free it now. */ 426 if (nic->peer_deleted) { 427 for (i = 0; i < queues; i++) { 428 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer); 429 } 430 } 431 432 for (i = queues - 1; i >= 0; i--) { 433 NetClientState *nc = qemu_get_subqueue(nic, i); 434 435 qemu_cleanup_net_client(nc); 436 qemu_free_net_client(nc); 437 } 438 439 g_free(nic); 440 } 441 442 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) 443 { 444 NetClientState *nc; 445 446 QTAILQ_FOREACH(nc, &net_clients, next) { 447 if (nc->info->type == NET_CLIENT_DRIVER_NIC) { 448 if (nc->queue_index == 0) { 449 func(qemu_get_nic(nc), opaque); 450 } 451 } 452 } 453 } 454 455 bool qemu_has_ufo(NetClientState *nc) 456 { 457 if (!nc || !nc->info->has_ufo) { 458 return false; 459 } 460 461 return nc->info->has_ufo(nc); 462 } 463 464 bool qemu_has_vnet_hdr(NetClientState *nc) 465 { 466 if (!nc || !nc->info->has_vnet_hdr) { 467 return false; 468 } 469 470 return nc->info->has_vnet_hdr(nc); 471 } 472 473 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len) 474 { 475 if (!nc || !nc->info->has_vnet_hdr_len) { 476 return false; 477 } 478 479 return nc->info->has_vnet_hdr_len(nc, len); 480 } 481 482 void qemu_using_vnet_hdr(NetClientState *nc, bool enable) 483 { 484 if (!nc || !nc->info->using_vnet_hdr) { 485 return; 486 } 487 488 nc->info->using_vnet_hdr(nc, enable); 489 } 490 491 void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, 492 int ecn, int ufo) 493 { 494 if (!nc || !nc->info->set_offload) { 495 return; 496 } 497 498 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo); 499 } 500 501 void qemu_set_vnet_hdr_len(NetClientState *nc, int len) 502 { 503 if (!nc || !nc->info->set_vnet_hdr_len) { 504 return; 505 } 506 507 nc->vnet_hdr_len = len; 508 nc->info->set_vnet_hdr_len(nc, len); 509 } 510 511 int qemu_set_vnet_le(NetClientState *nc, bool is_le) 512 { 513 #ifdef HOST_WORDS_BIGENDIAN 514 if (!nc || !nc->info->set_vnet_le) { 515 return -ENOSYS; 516 } 517 518 return nc->info->set_vnet_le(nc, is_le); 519 #else 520 return 0; 521 #endif 522 } 523 524 int qemu_set_vnet_be(NetClientState *nc, bool is_be) 525 { 526 #ifdef HOST_WORDS_BIGENDIAN 527 return 0; 528 #else 529 if (!nc || !nc->info->set_vnet_be) { 530 return -ENOSYS; 531 } 532 533 return nc->info->set_vnet_be(nc, is_be); 534 #endif 535 } 536 537 int qemu_can_send_packet(NetClientState *sender) 538 { 539 int vm_running = runstate_is_running(); 540 541 if (!vm_running) { 542 return 0; 543 } 544 545 if (!sender->peer) { 546 return 1; 547 } 548 549 if (sender->peer->receive_disabled) { 550 return 0; 551 } else if (sender->peer->info->can_receive && 552 !sender->peer->info->can_receive(sender->peer)) { 553 return 0; 554 } 555 return 1; 556 } 557 558 static ssize_t filter_receive_iov(NetClientState *nc, 559 NetFilterDirection direction, 560 NetClientState *sender, 561 unsigned flags, 562 const struct iovec *iov, 563 int iovcnt, 564 NetPacketSent *sent_cb) 565 { 566 ssize_t ret = 0; 567 NetFilterState *nf = NULL; 568 569 if (direction == NET_FILTER_DIRECTION_TX) { 570 QTAILQ_FOREACH(nf, &nc->filters, next) { 571 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, 572 iovcnt, sent_cb); 573 if (ret) { 574 return ret; 575 } 576 } 577 } else { 578 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, NetFilterHead, next) { 579 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov, 580 iovcnt, sent_cb); 581 if (ret) { 582 return ret; 583 } 584 } 585 } 586 587 return ret; 588 } 589 590 static ssize_t filter_receive(NetClientState *nc, 591 NetFilterDirection direction, 592 NetClientState *sender, 593 unsigned flags, 594 const uint8_t *data, 595 size_t size, 596 NetPacketSent *sent_cb) 597 { 598 struct iovec iov = { 599 .iov_base = (void *)data, 600 .iov_len = size 601 }; 602 603 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb); 604 } 605 606 void qemu_purge_queued_packets(NetClientState *nc) 607 { 608 if (!nc->peer) { 609 return; 610 } 611 612 qemu_net_queue_purge(nc->peer->incoming_queue, nc); 613 } 614 615 static 616 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge) 617 { 618 nc->receive_disabled = 0; 619 620 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) { 621 if (net_hub_flush(nc->peer)) { 622 qemu_notify_event(); 623 } 624 } 625 if (qemu_net_queue_flush(nc->incoming_queue)) { 626 /* We emptied the queue successfully, signal to the IO thread to repoll 627 * the file descriptor (for tap, for example). 628 */ 629 qemu_notify_event(); 630 } else if (purge) { 631 /* Unable to empty the queue, purge remaining packets */ 632 qemu_net_queue_purge(nc->incoming_queue, nc); 633 } 634 } 635 636 void qemu_flush_queued_packets(NetClientState *nc) 637 { 638 qemu_flush_or_purge_queued_packets(nc, false); 639 } 640 641 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender, 642 unsigned flags, 643 const uint8_t *buf, int size, 644 NetPacketSent *sent_cb) 645 { 646 NetQueue *queue; 647 int ret; 648 649 #ifdef DEBUG_NET 650 printf("qemu_send_packet_async:\n"); 651 qemu_hexdump((const char *)buf, stdout, "net", size); 652 #endif 653 654 if (sender->link_down || !sender->peer) { 655 return size; 656 } 657 658 /* Let filters handle the packet first */ 659 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX, 660 sender, flags, buf, size, sent_cb); 661 if (ret) { 662 return ret; 663 } 664 665 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX, 666 sender, flags, buf, size, sent_cb); 667 if (ret) { 668 return ret; 669 } 670 671 queue = sender->peer->incoming_queue; 672 673 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); 674 } 675 676 ssize_t qemu_send_packet_async(NetClientState *sender, 677 const uint8_t *buf, int size, 678 NetPacketSent *sent_cb) 679 { 680 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, 681 buf, size, sent_cb); 682 } 683 684 void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size) 685 { 686 qemu_send_packet_async(nc, buf, size, NULL); 687 } 688 689 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) 690 { 691 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW, 692 buf, size, NULL); 693 } 694 695 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov, 696 int iovcnt, unsigned flags) 697 { 698 uint8_t *buf = NULL; 699 uint8_t *buffer; 700 size_t offset; 701 ssize_t ret; 702 703 if (iovcnt == 1) { 704 buffer = iov[0].iov_base; 705 offset = iov[0].iov_len; 706 } else { 707 offset = iov_size(iov, iovcnt); 708 if (offset > NET_BUFSIZE) { 709 return -1; 710 } 711 buf = g_malloc(offset); 712 buffer = buf; 713 offset = iov_to_buf(iov, iovcnt, 0, buf, offset); 714 } 715 716 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) { 717 ret = nc->info->receive_raw(nc, buffer, offset); 718 } else { 719 ret = nc->info->receive(nc, buffer, offset); 720 } 721 722 g_free(buf); 723 return ret; 724 } 725 726 ssize_t qemu_deliver_packet_iov(NetClientState *sender, 727 unsigned flags, 728 const struct iovec *iov, 729 int iovcnt, 730 void *opaque) 731 { 732 NetClientState *nc = opaque; 733 int ret; 734 735 if (nc->link_down) { 736 return iov_size(iov, iovcnt); 737 } 738 739 if (nc->receive_disabled) { 740 return 0; 741 } 742 743 if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) { 744 ret = nc->info->receive_iov(nc, iov, iovcnt); 745 } else { 746 ret = nc_sendv_compat(nc, iov, iovcnt, flags); 747 } 748 749 if (ret == 0) { 750 nc->receive_disabled = 1; 751 } 752 753 return ret; 754 } 755 756 ssize_t qemu_sendv_packet_async(NetClientState *sender, 757 const struct iovec *iov, int iovcnt, 758 NetPacketSent *sent_cb) 759 { 760 NetQueue *queue; 761 int ret; 762 763 if (sender->link_down || !sender->peer) { 764 return iov_size(iov, iovcnt); 765 } 766 767 /* Let filters handle the packet first */ 768 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender, 769 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); 770 if (ret) { 771 return ret; 772 } 773 774 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender, 775 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb); 776 if (ret) { 777 return ret; 778 } 779 780 queue = sender->peer->incoming_queue; 781 782 return qemu_net_queue_send_iov(queue, sender, 783 QEMU_NET_PACKET_FLAG_NONE, 784 iov, iovcnt, sent_cb); 785 } 786 787 ssize_t 788 qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt) 789 { 790 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL); 791 } 792 793 NetClientState *qemu_find_netdev(const char *id) 794 { 795 NetClientState *nc; 796 797 QTAILQ_FOREACH(nc, &net_clients, next) { 798 if (nc->info->type == NET_CLIENT_DRIVER_NIC) 799 continue; 800 if (!strcmp(nc->name, id)) { 801 return nc; 802 } 803 } 804 805 return NULL; 806 } 807 808 int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 809 NetClientDriver type, int max) 810 { 811 NetClientState *nc; 812 int ret = 0; 813 814 QTAILQ_FOREACH(nc, &net_clients, next) { 815 if (nc->info->type == type) { 816 continue; 817 } 818 if (!id || !strcmp(nc->name, id)) { 819 if (ret < max) { 820 ncs[ret] = nc; 821 } 822 ret++; 823 } 824 } 825 826 return ret; 827 } 828 829 static int nic_get_free_idx(void) 830 { 831 int index; 832 833 for (index = 0; index < MAX_NICS; index++) 834 if (!nd_table[index].used) 835 return index; 836 return -1; 837 } 838 839 int qemu_show_nic_models(const char *arg, const char *const *models) 840 { 841 int i; 842 843 if (!arg || !is_help_option(arg)) { 844 return 0; 845 } 846 847 fprintf(stderr, "qemu: Supported NIC models: "); 848 for (i = 0 ; models[i]; i++) 849 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); 850 return 1; 851 } 852 853 void qemu_check_nic_model(NICInfo *nd, const char *model) 854 { 855 const char *models[2]; 856 857 models[0] = model; 858 models[1] = NULL; 859 860 if (qemu_show_nic_models(nd->model, models)) 861 exit(0); 862 if (qemu_find_nic_model(nd, models, model) < 0) 863 exit(1); 864 } 865 866 int qemu_find_nic_model(NICInfo *nd, const char * const *models, 867 const char *default_model) 868 { 869 int i; 870 871 if (!nd->model) 872 nd->model = g_strdup(default_model); 873 874 for (i = 0 ; models[i]; i++) { 875 if (strcmp(nd->model, models[i]) == 0) 876 return i; 877 } 878 879 error_report("Unsupported NIC model: %s", nd->model); 880 return -1; 881 } 882 883 static int net_init_nic(const Netdev *netdev, const char *name, 884 NetClientState *peer, Error **errp) 885 { 886 int idx; 887 NICInfo *nd; 888 const NetLegacyNicOptions *nic; 889 890 assert(netdev->type == NET_CLIENT_DRIVER_NIC); 891 nic = &netdev->u.nic; 892 893 idx = nic_get_free_idx(); 894 if (idx == -1 || nb_nics >= MAX_NICS) { 895 error_setg(errp, "too many NICs"); 896 return -1; 897 } 898 899 nd = &nd_table[idx]; 900 901 memset(nd, 0, sizeof(*nd)); 902 903 if (nic->has_netdev) { 904 nd->netdev = qemu_find_netdev(nic->netdev); 905 if (!nd->netdev) { 906 error_setg(errp, "netdev '%s' not found", nic->netdev); 907 return -1; 908 } 909 } else { 910 assert(peer); 911 nd->netdev = peer; 912 } 913 nd->name = g_strdup(name); 914 if (nic->has_model) { 915 nd->model = g_strdup(nic->model); 916 } 917 if (nic->has_addr) { 918 nd->devaddr = g_strdup(nic->addr); 919 } 920 921 if (nic->has_macaddr && 922 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { 923 error_setg(errp, "invalid syntax for ethernet address"); 924 return -1; 925 } 926 if (nic->has_macaddr && 927 is_multicast_ether_addr(nd->macaddr.a)) { 928 error_setg(errp, 929 "NIC cannot have multicast MAC address (odd 1st byte)"); 930 return -1; 931 } 932 qemu_macaddr_default_if_unset(&nd->macaddr); 933 934 if (nic->has_vectors) { 935 if (nic->vectors > 0x7ffffff) { 936 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors); 937 return -1; 938 } 939 nd->nvectors = nic->vectors; 940 } else { 941 nd->nvectors = DEV_NVECTORS_UNSPECIFIED; 942 } 943 944 nd->used = 1; 945 nb_nics++; 946 947 return idx; 948 } 949 950 951 static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])( 952 const Netdev *netdev, 953 const char *name, 954 NetClientState *peer, Error **errp) = { 955 [NET_CLIENT_DRIVER_NIC] = net_init_nic, 956 #ifdef CONFIG_SLIRP 957 [NET_CLIENT_DRIVER_USER] = net_init_slirp, 958 #endif 959 [NET_CLIENT_DRIVER_TAP] = net_init_tap, 960 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket, 961 #ifdef CONFIG_VDE 962 [NET_CLIENT_DRIVER_VDE] = net_init_vde, 963 #endif 964 #ifdef CONFIG_NETMAP 965 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap, 966 #endif 967 [NET_CLIENT_DRIVER_DUMP] = net_init_dump, 968 #ifdef CONFIG_NET_BRIDGE 969 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge, 970 #endif 971 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport, 972 #ifdef CONFIG_VHOST_NET_USED 973 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user, 974 #endif 975 #ifdef CONFIG_L2TPV3 976 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3, 977 #endif 978 }; 979 980 981 static int net_client_init1(const void *object, bool is_netdev, Error **errp) 982 { 983 Netdev legacy = {0}; 984 const Netdev *netdev; 985 const char *name; 986 NetClientState *peer = NULL; 987 static bool vlan_warned; 988 989 if (is_netdev) { 990 netdev = object; 991 name = netdev->id; 992 993 if (netdev->type == NET_CLIENT_DRIVER_DUMP || 994 netdev->type == NET_CLIENT_DRIVER_NIC || 995 !net_client_init_fun[netdev->type]) { 996 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", 997 "a netdev backend type"); 998 return -1; 999 } 1000 } else { 1001 const NetLegacy *net = object; 1002 const NetLegacyOptions *opts = net->opts; 1003 legacy.id = net->id; 1004 netdev = &legacy; 1005 /* missing optional values have been initialized to "all bits zero" */ 1006 name = net->has_id ? net->id : net->name; 1007 1008 /* Map the old options to the new flat type */ 1009 switch (opts->type) { 1010 case NET_LEGACY_OPTIONS_TYPE_NONE: 1011 return 0; /* nothing to do */ 1012 case NET_LEGACY_OPTIONS_TYPE_NIC: 1013 legacy.type = NET_CLIENT_DRIVER_NIC; 1014 legacy.u.nic = opts->u.nic; 1015 break; 1016 case NET_LEGACY_OPTIONS_TYPE_USER: 1017 legacy.type = NET_CLIENT_DRIVER_USER; 1018 legacy.u.user = opts->u.user; 1019 break; 1020 case NET_LEGACY_OPTIONS_TYPE_TAP: 1021 legacy.type = NET_CLIENT_DRIVER_TAP; 1022 legacy.u.tap = opts->u.tap; 1023 break; 1024 case NET_LEGACY_OPTIONS_TYPE_L2TPV3: 1025 legacy.type = NET_CLIENT_DRIVER_L2TPV3; 1026 legacy.u.l2tpv3 = opts->u.l2tpv3; 1027 break; 1028 case NET_LEGACY_OPTIONS_TYPE_SOCKET: 1029 legacy.type = NET_CLIENT_DRIVER_SOCKET; 1030 legacy.u.socket = opts->u.socket; 1031 break; 1032 case NET_LEGACY_OPTIONS_TYPE_VDE: 1033 legacy.type = NET_CLIENT_DRIVER_VDE; 1034 legacy.u.vde = opts->u.vde; 1035 break; 1036 case NET_LEGACY_OPTIONS_TYPE_DUMP: 1037 legacy.type = NET_CLIENT_DRIVER_DUMP; 1038 legacy.u.dump = opts->u.dump; 1039 break; 1040 case NET_LEGACY_OPTIONS_TYPE_BRIDGE: 1041 legacy.type = NET_CLIENT_DRIVER_BRIDGE; 1042 legacy.u.bridge = opts->u.bridge; 1043 break; 1044 case NET_LEGACY_OPTIONS_TYPE_NETMAP: 1045 legacy.type = NET_CLIENT_DRIVER_NETMAP; 1046 legacy.u.netmap = opts->u.netmap; 1047 break; 1048 case NET_LEGACY_OPTIONS_TYPE_VHOST_USER: 1049 legacy.type = NET_CLIENT_DRIVER_VHOST_USER; 1050 legacy.u.vhost_user = opts->u.vhost_user; 1051 break; 1052 default: 1053 abort(); 1054 } 1055 1056 if (!net_client_init_fun[netdev->type]) { 1057 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type", 1058 "a net backend type (maybe it is not compiled " 1059 "into this binary)"); 1060 return -1; 1061 } 1062 1063 /* Do not add to a vlan if it's a nic with a netdev= parameter. */ 1064 if (netdev->type != NET_CLIENT_DRIVER_NIC || 1065 !opts->u.nic.has_netdev) { 1066 peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL); 1067 } 1068 1069 if (net->has_vlan && !vlan_warned) { 1070 error_report("'vlan' is deprecated. Please use 'netdev' instead."); 1071 vlan_warned = true; 1072 } 1073 } 1074 1075 if (net_client_init_fun[netdev->type](netdev, name, peer, errp) < 0) { 1076 /* FIXME drop when all init functions store an Error */ 1077 if (errp && !*errp) { 1078 error_setg(errp, QERR_DEVICE_INIT_FAILED, 1079 NetClientDriver_str(netdev->type)); 1080 } 1081 return -1; 1082 } 1083 return 0; 1084 } 1085 1086 1087 int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp) 1088 { 1089 void *object = NULL; 1090 Error *err = NULL; 1091 int ret = -1; 1092 Visitor *v = opts_visitor_new(opts); 1093 1094 { 1095 /* Parse convenience option format ip6-net=fec0::0[/64] */ 1096 const char *ip6_net = qemu_opt_get(opts, "ipv6-net"); 1097 1098 if (ip6_net) { 1099 char buf[strlen(ip6_net) + 1]; 1100 1101 if (get_str_sep(buf, sizeof(buf), &ip6_net, '/') < 0) { 1102 /* Default 64bit prefix length. */ 1103 qemu_opt_set(opts, "ipv6-prefix", ip6_net, &error_abort); 1104 qemu_opt_set_number(opts, "ipv6-prefixlen", 64, &error_abort); 1105 } else { 1106 /* User-specified prefix length. */ 1107 unsigned long len; 1108 int err; 1109 1110 qemu_opt_set(opts, "ipv6-prefix", buf, &error_abort); 1111 err = qemu_strtoul(ip6_net, NULL, 10, &len); 1112 1113 if (err) { 1114 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1115 "ipv6-prefix", "a number"); 1116 } else { 1117 qemu_opt_set_number(opts, "ipv6-prefixlen", len, 1118 &error_abort); 1119 } 1120 } 1121 qemu_opt_unset(opts, "ipv6-net"); 1122 } 1123 } 1124 1125 if (is_netdev) { 1126 visit_type_Netdev(v, NULL, (Netdev **)&object, &err); 1127 } else { 1128 visit_type_NetLegacy(v, NULL, (NetLegacy **)&object, &err); 1129 } 1130 1131 if (!err) { 1132 ret = net_client_init1(object, is_netdev, &err); 1133 } 1134 1135 if (is_netdev) { 1136 qapi_free_Netdev(object); 1137 } else { 1138 qapi_free_NetLegacy(object); 1139 } 1140 1141 error_propagate(errp, err); 1142 visit_free(v); 1143 return ret; 1144 } 1145 1146 1147 static int net_host_check_device(const char *device) 1148 { 1149 int i; 1150 for (i = 0; host_net_devices[i]; i++) { 1151 if (!strncmp(host_net_devices[i], device, 1152 strlen(host_net_devices[i]))) { 1153 return 1; 1154 } 1155 } 1156 1157 return 0; 1158 } 1159 1160 void hmp_host_net_add(Monitor *mon, const QDict *qdict) 1161 { 1162 const char *device = qdict_get_str(qdict, "device"); 1163 const char *opts_str = qdict_get_try_str(qdict, "opts"); 1164 Error *local_err = NULL; 1165 QemuOpts *opts; 1166 static bool warned; 1167 1168 if (!warned && !qtest_enabled()) { 1169 error_report("host_net_add is deprecated, use netdev_add instead"); 1170 warned = true; 1171 } 1172 1173 if (!net_host_check_device(device)) { 1174 monitor_printf(mon, "invalid host network device %s\n", device); 1175 return; 1176 } 1177 1178 opts = qemu_opts_parse_noisily(qemu_find_opts("net"), 1179 opts_str ? opts_str : "", false); 1180 if (!opts) { 1181 return; 1182 } 1183 1184 qemu_opt_set(opts, "type", device, &error_abort); 1185 1186 net_client_init(opts, false, &local_err); 1187 if (local_err) { 1188 error_report_err(local_err); 1189 monitor_printf(mon, "adding host network device %s failed\n", device); 1190 } 1191 } 1192 1193 void hmp_host_net_remove(Monitor *mon, const QDict *qdict) 1194 { 1195 NetClientState *nc; 1196 int vlan_id = qdict_get_int(qdict, "vlan_id"); 1197 const char *device = qdict_get_str(qdict, "device"); 1198 static bool warned; 1199 1200 if (!warned && !qtest_enabled()) { 1201 error_report("host_net_remove is deprecated, use netdev_del instead"); 1202 warned = true; 1203 } 1204 1205 nc = net_hub_find_client_by_name(vlan_id, device); 1206 if (!nc) { 1207 error_report("Host network device '%s' on hub '%d' not found", 1208 device, vlan_id); 1209 return; 1210 } 1211 if (nc->info->type == NET_CLIENT_DRIVER_NIC) { 1212 error_report("invalid host network device '%s'", device); 1213 return; 1214 } 1215 1216 qemu_del_net_client(nc->peer); 1217 qemu_del_net_client(nc); 1218 qemu_opts_del(qemu_opts_find(qemu_find_opts("net"), device)); 1219 } 1220 1221 void netdev_add(QemuOpts *opts, Error **errp) 1222 { 1223 net_client_init(opts, true, errp); 1224 } 1225 1226 void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp) 1227 { 1228 Error *local_err = NULL; 1229 QemuOptsList *opts_list; 1230 QemuOpts *opts; 1231 1232 opts_list = qemu_find_opts_err("netdev", &local_err); 1233 if (local_err) { 1234 goto out; 1235 } 1236 1237 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err); 1238 if (local_err) { 1239 goto out; 1240 } 1241 1242 netdev_add(opts, &local_err); 1243 if (local_err) { 1244 qemu_opts_del(opts); 1245 goto out; 1246 } 1247 1248 out: 1249 error_propagate(errp, local_err); 1250 } 1251 1252 void qmp_netdev_del(const char *id, Error **errp) 1253 { 1254 NetClientState *nc; 1255 QemuOpts *opts; 1256 1257 nc = qemu_find_netdev(id); 1258 if (!nc) { 1259 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1260 "Device '%s' not found", id); 1261 return; 1262 } 1263 1264 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); 1265 if (!opts) { 1266 error_setg(errp, "Device '%s' is not a netdev", id); 1267 return; 1268 } 1269 1270 qemu_del_net_client(nc); 1271 qemu_opts_del(opts); 1272 } 1273 1274 static void netfilter_print_info(Monitor *mon, NetFilterState *nf) 1275 { 1276 char *str; 1277 ObjectProperty *prop; 1278 ObjectPropertyIterator iter; 1279 Visitor *v; 1280 1281 /* generate info str */ 1282 object_property_iter_init(&iter, OBJECT(nf)); 1283 while ((prop = object_property_iter_next(&iter))) { 1284 if (!strcmp(prop->name, "type")) { 1285 continue; 1286 } 1287 v = string_output_visitor_new(false, &str); 1288 object_property_get(OBJECT(nf), v, prop->name, NULL); 1289 visit_complete(v, &str); 1290 visit_free(v); 1291 monitor_printf(mon, ",%s=%s", prop->name, str); 1292 g_free(str); 1293 } 1294 monitor_printf(mon, "\n"); 1295 } 1296 1297 void print_net_client(Monitor *mon, NetClientState *nc) 1298 { 1299 NetFilterState *nf; 1300 1301 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name, 1302 nc->queue_index, 1303 NetClientDriver_str(nc->info->type), 1304 nc->info_str); 1305 if (!QTAILQ_EMPTY(&nc->filters)) { 1306 monitor_printf(mon, "filters:\n"); 1307 } 1308 QTAILQ_FOREACH(nf, &nc->filters, next) { 1309 char *path = object_get_canonical_path_component(OBJECT(nf)); 1310 1311 monitor_printf(mon, " - %s: type=%s", path, 1312 object_get_typename(OBJECT(nf))); 1313 netfilter_print_info(mon, nf); 1314 g_free(path); 1315 } 1316 } 1317 1318 RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name, 1319 Error **errp) 1320 { 1321 NetClientState *nc; 1322 RxFilterInfoList *filter_list = NULL, *last_entry = NULL; 1323 1324 QTAILQ_FOREACH(nc, &net_clients, next) { 1325 RxFilterInfoList *entry; 1326 RxFilterInfo *info; 1327 1328 if (has_name && strcmp(nc->name, name) != 0) { 1329 continue; 1330 } 1331 1332 /* only query rx-filter information of NIC */ 1333 if (nc->info->type != NET_CLIENT_DRIVER_NIC) { 1334 if (has_name) { 1335 error_setg(errp, "net client(%s) isn't a NIC", name); 1336 return NULL; 1337 } 1338 continue; 1339 } 1340 1341 /* only query information on queue 0 since the info is per nic, 1342 * not per queue 1343 */ 1344 if (nc->queue_index != 0) 1345 continue; 1346 1347 if (nc->info->query_rx_filter) { 1348 info = nc->info->query_rx_filter(nc); 1349 entry = g_malloc0(sizeof(*entry)); 1350 entry->value = info; 1351 1352 if (!filter_list) { 1353 filter_list = entry; 1354 } else { 1355 last_entry->next = entry; 1356 } 1357 last_entry = entry; 1358 } else if (has_name) { 1359 error_setg(errp, "net client(%s) doesn't support" 1360 " rx-filter querying", name); 1361 return NULL; 1362 } 1363 1364 if (has_name) { 1365 break; 1366 } 1367 } 1368 1369 if (filter_list == NULL && has_name) { 1370 error_setg(errp, "invalid net client name: %s", name); 1371 } 1372 1373 return filter_list; 1374 } 1375 1376 void hmp_info_network(Monitor *mon, const QDict *qdict) 1377 { 1378 NetClientState *nc, *peer; 1379 NetClientDriver type; 1380 1381 net_hub_info(mon); 1382 1383 QTAILQ_FOREACH(nc, &net_clients, next) { 1384 peer = nc->peer; 1385 type = nc->info->type; 1386 1387 /* Skip if already printed in hub info */ 1388 if (net_hub_id_for_client(nc, NULL) == 0) { 1389 continue; 1390 } 1391 1392 if (!peer || type == NET_CLIENT_DRIVER_NIC) { 1393 print_net_client(mon, nc); 1394 } /* else it's a netdev connected to a NIC, printed with the NIC */ 1395 if (peer && type == NET_CLIENT_DRIVER_NIC) { 1396 monitor_printf(mon, " \\ "); 1397 print_net_client(mon, peer); 1398 } 1399 } 1400 } 1401 1402 void qmp_set_link(const char *name, bool up, Error **errp) 1403 { 1404 NetClientState *ncs[MAX_QUEUE_NUM]; 1405 NetClientState *nc; 1406 int queues, i; 1407 1408 queues = qemu_find_net_clients_except(name, ncs, 1409 NET_CLIENT_DRIVER__MAX, 1410 MAX_QUEUE_NUM); 1411 1412 if (queues == 0) { 1413 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1414 "Device '%s' not found", name); 1415 return; 1416 } 1417 nc = ncs[0]; 1418 1419 for (i = 0; i < queues; i++) { 1420 ncs[i]->link_down = !up; 1421 } 1422 1423 if (nc->info->link_status_changed) { 1424 nc->info->link_status_changed(nc); 1425 } 1426 1427 if (nc->peer) { 1428 /* Change peer link only if the peer is NIC and then notify peer. 1429 * If the peer is a HUBPORT or a backend, we do not change the 1430 * link status. 1431 * 1432 * This behavior is compatible with qemu vlans where there could be 1433 * multiple clients that can still communicate with each other in 1434 * disconnected mode. For now maintain this compatibility. 1435 */ 1436 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) { 1437 for (i = 0; i < queues; i++) { 1438 ncs[i]->peer->link_down = !up; 1439 } 1440 } 1441 if (nc->peer->info->link_status_changed) { 1442 nc->peer->info->link_status_changed(nc->peer); 1443 } 1444 } 1445 } 1446 1447 static void net_vm_change_state_handler(void *opaque, int running, 1448 RunState state) 1449 { 1450 NetClientState *nc; 1451 NetClientState *tmp; 1452 1453 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { 1454 if (running) { 1455 /* Flush queued packets and wake up backends. */ 1456 if (nc->peer && qemu_can_send_packet(nc)) { 1457 qemu_flush_queued_packets(nc->peer); 1458 } 1459 } else { 1460 /* Complete all queued packets, to guarantee we don't modify 1461 * state later when VM is not running. 1462 */ 1463 qemu_flush_or_purge_queued_packets(nc, true); 1464 } 1465 } 1466 } 1467 1468 void net_cleanup(void) 1469 { 1470 NetClientState *nc; 1471 1472 /* We may del multiple entries during qemu_del_net_client(), 1473 * so QTAILQ_FOREACH_SAFE() is also not safe here. 1474 */ 1475 while (!QTAILQ_EMPTY(&net_clients)) { 1476 nc = QTAILQ_FIRST(&net_clients); 1477 if (nc->info->type == NET_CLIENT_DRIVER_NIC) { 1478 qemu_del_nic(qemu_get_nic(nc)); 1479 } else { 1480 qemu_del_net_client(nc); 1481 } 1482 } 1483 1484 qemu_del_vm_change_state_handler(net_change_state_entry); 1485 } 1486 1487 void net_check_clients(void) 1488 { 1489 NetClientState *nc; 1490 int i; 1491 1492 net_hub_check_clients(); 1493 1494 QTAILQ_FOREACH(nc, &net_clients, next) { 1495 if (!nc->peer) { 1496 warn_report("%s %s has no peer", 1497 nc->info->type == NET_CLIENT_DRIVER_NIC 1498 ? "nic" : "netdev", 1499 nc->name); 1500 } 1501 } 1502 1503 /* Check that all NICs requested via -net nic actually got created. 1504 * NICs created via -device don't need to be checked here because 1505 * they are always instantiated. 1506 */ 1507 for (i = 0; i < MAX_NICS; i++) { 1508 NICInfo *nd = &nd_table[i]; 1509 if (nd->used && !nd->instantiated) { 1510 warn_report("requested NIC (%s, model %s) " 1511 "was not created (not supported by this machine?)", 1512 nd->name ? nd->name : "anonymous", 1513 nd->model ? nd->model : "unspecified"); 1514 } 1515 } 1516 } 1517 1518 static int net_init_client(void *dummy, QemuOpts *opts, Error **errp) 1519 { 1520 Error *local_err = NULL; 1521 1522 net_client_init(opts, false, &local_err); 1523 if (local_err) { 1524 error_report_err(local_err); 1525 return -1; 1526 } 1527 1528 return 0; 1529 } 1530 1531 static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp) 1532 { 1533 Error *local_err = NULL; 1534 int ret; 1535 1536 ret = net_client_init(opts, true, &local_err); 1537 if (local_err) { 1538 error_report_err(local_err); 1539 return -1; 1540 } 1541 1542 return ret; 1543 } 1544 1545 int net_init_clients(void) 1546 { 1547 QemuOptsList *net = qemu_find_opts("net"); 1548 1549 net_change_state_entry = 1550 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL); 1551 1552 QTAILQ_INIT(&net_clients); 1553 1554 if (qemu_opts_foreach(qemu_find_opts("netdev"), 1555 net_init_netdev, NULL, NULL)) { 1556 return -1; 1557 } 1558 1559 if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) { 1560 return -1; 1561 } 1562 1563 return 0; 1564 } 1565 1566 int net_client_parse(QemuOptsList *opts_list, const char *optarg) 1567 { 1568 #if defined(CONFIG_SLIRP) 1569 int ret; 1570 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { 1571 return ret; 1572 } 1573 #endif 1574 1575 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) { 1576 return -1; 1577 } 1578 1579 return 0; 1580 } 1581 1582 /* From FreeBSD */ 1583 /* XXX: optimize */ 1584 unsigned compute_mcast_idx(const uint8_t *ep) 1585 { 1586 uint32_t crc; 1587 int carry, i, j; 1588 uint8_t b; 1589 1590 crc = 0xffffffff; 1591 for (i = 0; i < 6; i++) { 1592 b = *ep++; 1593 for (j = 0; j < 8; j++) { 1594 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); 1595 crc <<= 1; 1596 b >>= 1; 1597 if (carry) { 1598 crc = ((crc ^ POLYNOMIAL) | carry); 1599 } 1600 } 1601 } 1602 return crc >> 26; 1603 } 1604 1605 QemuOptsList qemu_netdev_opts = { 1606 .name = "netdev", 1607 .implied_opt_name = "type", 1608 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), 1609 .desc = { 1610 /* 1611 * no elements => accept any params 1612 * validation will happen later 1613 */ 1614 { /* end of list */ } 1615 }, 1616 }; 1617 1618 QemuOptsList qemu_net_opts = { 1619 .name = "net", 1620 .implied_opt_name = "type", 1621 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), 1622 .desc = { 1623 /* 1624 * no elements => accept any params 1625 * validation will happen later 1626 */ 1627 { /* end of list */ } 1628 }, 1629 }; 1630 1631 void net_socket_rs_init(SocketReadState *rs, 1632 SocketReadStateFinalize *finalize, 1633 bool vnet_hdr) 1634 { 1635 rs->state = 0; 1636 rs->vnet_hdr = vnet_hdr; 1637 rs->index = 0; 1638 rs->packet_len = 0; 1639 rs->vnet_hdr_len = 0; 1640 memset(rs->buf, 0, sizeof(rs->buf)); 1641 rs->finalize = finalize; 1642 } 1643 1644 /* 1645 * Returns 1646 * 0: success 1647 * -1: error occurs 1648 */ 1649 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size) 1650 { 1651 unsigned int l; 1652 1653 while (size > 0) { 1654 /* Reassemble a packet from the network. 1655 * 0 = getting length. 1656 * 1 = getting vnet header length. 1657 * 2 = getting data. 1658 */ 1659 switch (rs->state) { 1660 case 0: 1661 l = 4 - rs->index; 1662 if (l > size) { 1663 l = size; 1664 } 1665 memcpy(rs->buf + rs->index, buf, l); 1666 buf += l; 1667 size -= l; 1668 rs->index += l; 1669 if (rs->index == 4) { 1670 /* got length */ 1671 rs->packet_len = ntohl(*(uint32_t *)rs->buf); 1672 rs->index = 0; 1673 if (rs->vnet_hdr) { 1674 rs->state = 1; 1675 } else { 1676 rs->state = 2; 1677 rs->vnet_hdr_len = 0; 1678 } 1679 } 1680 break; 1681 case 1: 1682 l = 4 - rs->index; 1683 if (l > size) { 1684 l = size; 1685 } 1686 memcpy(rs->buf + rs->index, buf, l); 1687 buf += l; 1688 size -= l; 1689 rs->index += l; 1690 if (rs->index == 4) { 1691 /* got vnet header length */ 1692 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf); 1693 rs->index = 0; 1694 rs->state = 2; 1695 } 1696 break; 1697 case 2: 1698 l = rs->packet_len - rs->index; 1699 if (l > size) { 1700 l = size; 1701 } 1702 if (rs->index + l <= sizeof(rs->buf)) { 1703 memcpy(rs->buf + rs->index, buf, l); 1704 } else { 1705 fprintf(stderr, "serious error: oversized packet received," 1706 "connection terminated.\n"); 1707 rs->index = rs->state = 0; 1708 return -1; 1709 } 1710 1711 rs->index += l; 1712 buf += l; 1713 size -= l; 1714 if (rs->index >= rs->packet_len) { 1715 rs->index = 0; 1716 rs->state = 0; 1717 assert(rs->finalize); 1718 rs->finalize(rs); 1719 } 1720 break; 1721 } 1722 } 1723 1724 assert(size == 0); 1725 return 0; 1726 } 1727