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