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 "config-host.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/sockets.h" 36 #include "qemu/config-file.h" 37 #include "qmp-commands.h" 38 #include "hw/qdev.h" 39 #include "qemu/iov.h" 40 #include "qemu/main-loop.h" 41 #include "qapi-visit.h" 42 #include "qapi/opts-visitor.h" 43 #include "qapi/dealloc-visitor.h" 44 45 /* Net bridge is currently not supported for W32. */ 46 #if !defined(_WIN32) 47 # define CONFIG_NET_BRIDGE 48 #endif 49 50 static QTAILQ_HEAD(, NetClientState) net_clients; 51 52 int default_net = 1; 53 54 /***********************************************************/ 55 /* network device redirectors */ 56 57 #if defined(DEBUG_NET) 58 static void hex_dump(FILE *f, const uint8_t *buf, int size) 59 { 60 int len, i, j, c; 61 62 for(i=0;i<size;i+=16) { 63 len = size - i; 64 if (len > 16) 65 len = 16; 66 fprintf(f, "%08x ", i); 67 for(j=0;j<16;j++) { 68 if (j < len) 69 fprintf(f, " %02x", buf[i+j]); 70 else 71 fprintf(f, " "); 72 } 73 fprintf(f, " "); 74 for(j=0;j<len;j++) { 75 c = buf[i+j]; 76 if (c < ' ' || c > '~') 77 c = '.'; 78 fprintf(f, "%c", c); 79 } 80 fprintf(f, "\n"); 81 } 82 } 83 #endif 84 85 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) 86 { 87 const char *p, *p1; 88 int len; 89 p = *pp; 90 p1 = strchr(p, sep); 91 if (!p1) 92 return -1; 93 len = p1 - p; 94 p1++; 95 if (buf_size > 0) { 96 if (len > buf_size - 1) 97 len = buf_size - 1; 98 memcpy(buf, p, len); 99 buf[len] = '\0'; 100 } 101 *pp = p1; 102 return 0; 103 } 104 105 int parse_host_port(struct sockaddr_in *saddr, const char *str) 106 { 107 char buf[512]; 108 struct hostent *he; 109 const char *p, *r; 110 int port; 111 112 p = str; 113 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) 114 return -1; 115 saddr->sin_family = AF_INET; 116 if (buf[0] == '\0') { 117 saddr->sin_addr.s_addr = 0; 118 } else { 119 if (qemu_isdigit(buf[0])) { 120 if (!inet_aton(buf, &saddr->sin_addr)) 121 return -1; 122 } else { 123 if ((he = gethostbyname(buf)) == NULL) 124 return - 1; 125 saddr->sin_addr = *(struct in_addr *)he->h_addr; 126 } 127 } 128 port = strtol(p, (char **)&r, 0); 129 if (r == p) 130 return -1; 131 saddr->sin_port = htons(port); 132 return 0; 133 } 134 135 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) 136 { 137 snprintf(nc->info_str, sizeof(nc->info_str), 138 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", 139 nc->model, 140 macaddr[0], macaddr[1], macaddr[2], 141 macaddr[3], macaddr[4], macaddr[5]); 142 } 143 144 void qemu_macaddr_default_if_unset(MACAddr *macaddr) 145 { 146 static int index = 0; 147 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; 148 149 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) 150 return; 151 macaddr->a[0] = 0x52; 152 macaddr->a[1] = 0x54; 153 macaddr->a[2] = 0x00; 154 macaddr->a[3] = 0x12; 155 macaddr->a[4] = 0x34; 156 macaddr->a[5] = 0x56 + index++; 157 } 158 159 /** 160 * Generate a name for net client 161 * 162 * Only net clients created with the legacy -net option and NICs need this. 163 */ 164 static char *assign_name(NetClientState *nc1, const char *model) 165 { 166 NetClientState *nc; 167 int id = 0; 168 169 QTAILQ_FOREACH(nc, &net_clients, next) { 170 if (nc == nc1) { 171 continue; 172 } 173 if (strcmp(nc->model, model) == 0) { 174 id++; 175 } 176 } 177 178 return g_strdup_printf("%s.%d", model, id); 179 } 180 181 static void qemu_net_client_destructor(NetClientState *nc) 182 { 183 g_free(nc); 184 } 185 186 static void qemu_net_client_setup(NetClientState *nc, 187 NetClientInfo *info, 188 NetClientState *peer, 189 const char *model, 190 const char *name, 191 NetClientDestructor *destructor) 192 { 193 nc->info = info; 194 nc->model = g_strdup(model); 195 if (name) { 196 nc->name = g_strdup(name); 197 } else { 198 nc->name = assign_name(nc, model); 199 } 200 201 if (peer) { 202 assert(!peer->peer); 203 nc->peer = peer; 204 peer->peer = nc; 205 } 206 QTAILQ_INSERT_TAIL(&net_clients, nc, next); 207 208 nc->incoming_queue = qemu_new_net_queue(nc); 209 nc->destructor = destructor; 210 } 211 212 NetClientState *qemu_new_net_client(NetClientInfo *info, 213 NetClientState *peer, 214 const char *model, 215 const char *name) 216 { 217 NetClientState *nc; 218 219 assert(info->size >= sizeof(NetClientState)); 220 221 nc = g_malloc0(info->size); 222 qemu_net_client_setup(nc, info, peer, model, name, 223 qemu_net_client_destructor); 224 225 return nc; 226 } 227 228 NICState *qemu_new_nic(NetClientInfo *info, 229 NICConf *conf, 230 const char *model, 231 const char *name, 232 void *opaque) 233 { 234 NetClientState **peers = conf->peers.ncs; 235 NICState *nic; 236 int i, queues = MAX(1, conf->queues); 237 238 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC); 239 assert(info->size >= sizeof(NICState)); 240 241 nic = g_malloc0(info->size + sizeof(NetClientState) * queues); 242 nic->ncs = (void *)nic + info->size; 243 nic->conf = conf; 244 nic->opaque = opaque; 245 246 for (i = 0; i < queues; i++) { 247 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name, 248 NULL); 249 nic->ncs[i].queue_index = i; 250 } 251 252 return nic; 253 } 254 255 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index) 256 { 257 return nic->ncs + queue_index; 258 } 259 260 NetClientState *qemu_get_queue(NICState *nic) 261 { 262 return qemu_get_subqueue(nic, 0); 263 } 264 265 NICState *qemu_get_nic(NetClientState *nc) 266 { 267 NetClientState *nc0 = nc - nc->queue_index; 268 269 return (NICState *)((void *)nc0 - nc->info->size); 270 } 271 272 void *qemu_get_nic_opaque(NetClientState *nc) 273 { 274 NICState *nic = qemu_get_nic(nc); 275 276 return nic->opaque; 277 } 278 279 static void qemu_cleanup_net_client(NetClientState *nc) 280 { 281 QTAILQ_REMOVE(&net_clients, nc, next); 282 283 if (nc->info->cleanup) { 284 nc->info->cleanup(nc); 285 } 286 } 287 288 static void qemu_free_net_client(NetClientState *nc) 289 { 290 if (nc->incoming_queue) { 291 qemu_del_net_queue(nc->incoming_queue); 292 } 293 if (nc->peer) { 294 nc->peer->peer = NULL; 295 } 296 g_free(nc->name); 297 g_free(nc->model); 298 if (nc->destructor) { 299 nc->destructor(nc); 300 } 301 } 302 303 void qemu_del_net_client(NetClientState *nc) 304 { 305 NetClientState *ncs[MAX_QUEUE_NUM]; 306 int queues, i; 307 308 /* If the NetClientState belongs to a multiqueue backend, we will change all 309 * other NetClientStates also. 310 */ 311 queues = qemu_find_net_clients_except(nc->name, ncs, 312 NET_CLIENT_OPTIONS_KIND_NIC, 313 MAX_QUEUE_NUM); 314 assert(queues != 0); 315 316 /* If there is a peer NIC, delete and cleanup client, but do not free. */ 317 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { 318 NICState *nic = qemu_get_nic(nc->peer); 319 if (nic->peer_deleted) { 320 return; 321 } 322 nic->peer_deleted = true; 323 324 for (i = 0; i < queues; i++) { 325 ncs[i]->peer->link_down = true; 326 } 327 328 if (nc->peer->info->link_status_changed) { 329 nc->peer->info->link_status_changed(nc->peer); 330 } 331 332 for (i = 0; i < queues; i++) { 333 qemu_cleanup_net_client(ncs[i]); 334 } 335 336 return; 337 } 338 339 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC); 340 341 for (i = 0; i < queues; i++) { 342 qemu_cleanup_net_client(ncs[i]); 343 qemu_free_net_client(ncs[i]); 344 } 345 } 346 347 void qemu_del_nic(NICState *nic) 348 { 349 int i, queues = MAX(nic->conf->queues, 1); 350 351 /* If this is a peer NIC and peer has already been deleted, free it now. */ 352 if (nic->peer_deleted) { 353 for (i = 0; i < queues; i++) { 354 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer); 355 } 356 } 357 358 for (i = queues - 1; i >= 0; i--) { 359 NetClientState *nc = qemu_get_subqueue(nic, i); 360 361 qemu_cleanup_net_client(nc); 362 qemu_free_net_client(nc); 363 } 364 365 g_free(nic); 366 } 367 368 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) 369 { 370 NetClientState *nc; 371 372 QTAILQ_FOREACH(nc, &net_clients, next) { 373 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { 374 if (nc->queue_index == 0) { 375 func(qemu_get_nic(nc), opaque); 376 } 377 } 378 } 379 } 380 381 int qemu_can_send_packet(NetClientState *sender) 382 { 383 if (!sender->peer) { 384 return 1; 385 } 386 387 if (sender->peer->receive_disabled) { 388 return 0; 389 } else if (sender->peer->info->can_receive && 390 !sender->peer->info->can_receive(sender->peer)) { 391 return 0; 392 } 393 return 1; 394 } 395 396 ssize_t qemu_deliver_packet(NetClientState *sender, 397 unsigned flags, 398 const uint8_t *data, 399 size_t size, 400 void *opaque) 401 { 402 NetClientState *nc = opaque; 403 ssize_t ret; 404 405 if (nc->link_down) { 406 return size; 407 } 408 409 if (nc->receive_disabled) { 410 return 0; 411 } 412 413 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) { 414 ret = nc->info->receive_raw(nc, data, size); 415 } else { 416 ret = nc->info->receive(nc, data, size); 417 } 418 419 if (ret == 0) { 420 nc->receive_disabled = 1; 421 }; 422 423 return ret; 424 } 425 426 void qemu_purge_queued_packets(NetClientState *nc) 427 { 428 if (!nc->peer) { 429 return; 430 } 431 432 qemu_net_queue_purge(nc->peer->incoming_queue, nc); 433 } 434 435 void qemu_flush_queued_packets(NetClientState *nc) 436 { 437 nc->receive_disabled = 0; 438 439 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) { 440 if (net_hub_flush(nc->peer)) { 441 qemu_notify_event(); 442 } 443 } 444 if (qemu_net_queue_flush(nc->incoming_queue)) { 445 /* We emptied the queue successfully, signal to the IO thread to repoll 446 * the file descriptor (for tap, for example). 447 */ 448 qemu_notify_event(); 449 } 450 } 451 452 static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender, 453 unsigned flags, 454 const uint8_t *buf, int size, 455 NetPacketSent *sent_cb) 456 { 457 NetQueue *queue; 458 459 #ifdef DEBUG_NET 460 printf("qemu_send_packet_async:\n"); 461 hex_dump(stdout, buf, size); 462 #endif 463 464 if (sender->link_down || !sender->peer) { 465 return size; 466 } 467 468 queue = sender->peer->incoming_queue; 469 470 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb); 471 } 472 473 ssize_t qemu_send_packet_async(NetClientState *sender, 474 const uint8_t *buf, int size, 475 NetPacketSent *sent_cb) 476 { 477 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE, 478 buf, size, sent_cb); 479 } 480 481 void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size) 482 { 483 qemu_send_packet_async(nc, buf, size, NULL); 484 } 485 486 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size) 487 { 488 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW, 489 buf, size, NULL); 490 } 491 492 static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov, 493 int iovcnt) 494 { 495 uint8_t buffer[NET_BUFSIZE]; 496 size_t offset; 497 498 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer)); 499 500 return nc->info->receive(nc, buffer, offset); 501 } 502 503 ssize_t qemu_deliver_packet_iov(NetClientState *sender, 504 unsigned flags, 505 const struct iovec *iov, 506 int iovcnt, 507 void *opaque) 508 { 509 NetClientState *nc = opaque; 510 int ret; 511 512 if (nc->link_down) { 513 return iov_size(iov, iovcnt); 514 } 515 516 if (nc->receive_disabled) { 517 return 0; 518 } 519 520 if (nc->info->receive_iov) { 521 ret = nc->info->receive_iov(nc, iov, iovcnt); 522 } else { 523 ret = nc_sendv_compat(nc, iov, iovcnt); 524 } 525 526 if (ret == 0) { 527 nc->receive_disabled = 1; 528 } 529 530 return ret; 531 } 532 533 ssize_t qemu_sendv_packet_async(NetClientState *sender, 534 const struct iovec *iov, int iovcnt, 535 NetPacketSent *sent_cb) 536 { 537 NetQueue *queue; 538 539 if (sender->link_down || !sender->peer) { 540 return iov_size(iov, iovcnt); 541 } 542 543 queue = sender->peer->incoming_queue; 544 545 return qemu_net_queue_send_iov(queue, sender, 546 QEMU_NET_PACKET_FLAG_NONE, 547 iov, iovcnt, sent_cb); 548 } 549 550 ssize_t 551 qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt) 552 { 553 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL); 554 } 555 556 NetClientState *qemu_find_netdev(const char *id) 557 { 558 NetClientState *nc; 559 560 QTAILQ_FOREACH(nc, &net_clients, next) { 561 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) 562 continue; 563 if (!strcmp(nc->name, id)) { 564 return nc; 565 } 566 } 567 568 return NULL; 569 } 570 571 int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 572 NetClientOptionsKind type, int max) 573 { 574 NetClientState *nc; 575 int ret = 0; 576 577 QTAILQ_FOREACH(nc, &net_clients, next) { 578 if (nc->info->type == type) { 579 continue; 580 } 581 if (!strcmp(nc->name, id)) { 582 if (ret < max) { 583 ncs[ret] = nc; 584 } 585 ret++; 586 } 587 } 588 589 return ret; 590 } 591 592 static int nic_get_free_idx(void) 593 { 594 int index; 595 596 for (index = 0; index < MAX_NICS; index++) 597 if (!nd_table[index].used) 598 return index; 599 return -1; 600 } 601 602 int qemu_show_nic_models(const char *arg, const char *const *models) 603 { 604 int i; 605 606 if (!arg || !is_help_option(arg)) { 607 return 0; 608 } 609 610 fprintf(stderr, "qemu: Supported NIC models: "); 611 for (i = 0 ; models[i]; i++) 612 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); 613 return 1; 614 } 615 616 void qemu_check_nic_model(NICInfo *nd, const char *model) 617 { 618 const char *models[2]; 619 620 models[0] = model; 621 models[1] = NULL; 622 623 if (qemu_show_nic_models(nd->model, models)) 624 exit(0); 625 if (qemu_find_nic_model(nd, models, model) < 0) 626 exit(1); 627 } 628 629 int qemu_find_nic_model(NICInfo *nd, const char * const *models, 630 const char *default_model) 631 { 632 int i; 633 634 if (!nd->model) 635 nd->model = g_strdup(default_model); 636 637 for (i = 0 ; models[i]; i++) { 638 if (strcmp(nd->model, models[i]) == 0) 639 return i; 640 } 641 642 error_report("Unsupported NIC model: %s", nd->model); 643 return -1; 644 } 645 646 static int net_init_nic(const NetClientOptions *opts, const char *name, 647 NetClientState *peer) 648 { 649 int idx; 650 NICInfo *nd; 651 const NetLegacyNicOptions *nic; 652 653 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC); 654 nic = opts->nic; 655 656 idx = nic_get_free_idx(); 657 if (idx == -1 || nb_nics >= MAX_NICS) { 658 error_report("Too Many NICs"); 659 return -1; 660 } 661 662 nd = &nd_table[idx]; 663 664 memset(nd, 0, sizeof(*nd)); 665 666 if (nic->has_netdev) { 667 nd->netdev = qemu_find_netdev(nic->netdev); 668 if (!nd->netdev) { 669 error_report("netdev '%s' not found", nic->netdev); 670 return -1; 671 } 672 } else { 673 assert(peer); 674 nd->netdev = peer; 675 } 676 nd->name = g_strdup(name); 677 if (nic->has_model) { 678 nd->model = g_strdup(nic->model); 679 } 680 if (nic->has_addr) { 681 nd->devaddr = g_strdup(nic->addr); 682 } 683 684 if (nic->has_macaddr && 685 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) { 686 error_report("invalid syntax for ethernet address"); 687 return -1; 688 } 689 if (nic->has_macaddr && 690 is_multicast_ether_addr(nd->macaddr.a)) { 691 error_report("NIC cannot have multicast MAC address (odd 1st byte)"); 692 return -1; 693 } 694 qemu_macaddr_default_if_unset(&nd->macaddr); 695 696 if (nic->has_vectors) { 697 if (nic->vectors > 0x7ffffff) { 698 error_report("invalid # of vectors: %"PRIu32, nic->vectors); 699 return -1; 700 } 701 nd->nvectors = nic->vectors; 702 } else { 703 nd->nvectors = DEV_NVECTORS_UNSPECIFIED; 704 } 705 706 nd->used = 1; 707 nb_nics++; 708 709 return idx; 710 } 711 712 713 static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])( 714 const NetClientOptions *opts, 715 const char *name, 716 NetClientState *peer) = { 717 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic, 718 #ifdef CONFIG_SLIRP 719 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp, 720 #endif 721 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap, 722 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket, 723 #ifdef CONFIG_VDE 724 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde, 725 #endif 726 #ifdef CONFIG_NETMAP 727 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap, 728 #endif 729 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump, 730 #ifdef CONFIG_NET_BRIDGE 731 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge, 732 #endif 733 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport, 734 }; 735 736 737 static int net_client_init1(const void *object, int is_netdev, Error **errp) 738 { 739 union { 740 const Netdev *netdev; 741 const NetLegacy *net; 742 } u; 743 const NetClientOptions *opts; 744 const char *name; 745 746 if (is_netdev) { 747 u.netdev = object; 748 opts = u.netdev->opts; 749 name = u.netdev->id; 750 751 switch (opts->kind) { 752 #ifdef CONFIG_SLIRP 753 case NET_CLIENT_OPTIONS_KIND_USER: 754 #endif 755 case NET_CLIENT_OPTIONS_KIND_TAP: 756 case NET_CLIENT_OPTIONS_KIND_SOCKET: 757 #ifdef CONFIG_VDE 758 case NET_CLIENT_OPTIONS_KIND_VDE: 759 #endif 760 #ifdef CONFIG_NETMAP 761 case NET_CLIENT_OPTIONS_KIND_NETMAP: 762 #endif 763 #ifdef CONFIG_NET_BRIDGE 764 case NET_CLIENT_OPTIONS_KIND_BRIDGE: 765 #endif 766 case NET_CLIENT_OPTIONS_KIND_HUBPORT: 767 break; 768 769 default: 770 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type", 771 "a netdev backend type"); 772 return -1; 773 } 774 } else { 775 u.net = object; 776 opts = u.net->opts; 777 /* missing optional values have been initialized to "all bits zero" */ 778 name = u.net->has_id ? u.net->id : u.net->name; 779 } 780 781 if (net_client_init_fun[opts->kind]) { 782 NetClientState *peer = NULL; 783 784 /* Do not add to a vlan if it's a -netdev or a nic with a netdev= 785 * parameter. */ 786 if (!is_netdev && 787 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC || 788 !opts->nic->has_netdev)) { 789 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL); 790 } 791 792 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) { 793 /* TODO push error reporting into init() methods */ 794 error_set(errp, QERR_DEVICE_INIT_FAILED, 795 NetClientOptionsKind_lookup[opts->kind]); 796 return -1; 797 } 798 } 799 return 0; 800 } 801 802 803 static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp) 804 { 805 if (is_netdev) { 806 visit_type_Netdev(v, (Netdev **)object, NULL, errp); 807 } else { 808 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp); 809 } 810 } 811 812 813 int net_client_init(QemuOpts *opts, int is_netdev, Error **errp) 814 { 815 void *object = NULL; 816 Error *err = NULL; 817 int ret = -1; 818 819 { 820 OptsVisitor *ov = opts_visitor_new(opts); 821 822 net_visit(opts_get_visitor(ov), is_netdev, &object, &err); 823 opts_visitor_cleanup(ov); 824 } 825 826 if (!err) { 827 ret = net_client_init1(object, is_netdev, &err); 828 } 829 830 if (object) { 831 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new(); 832 833 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL); 834 qapi_dealloc_visitor_cleanup(dv); 835 } 836 837 error_propagate(errp, err); 838 return ret; 839 } 840 841 842 static int net_host_check_device(const char *device) 843 { 844 int i; 845 const char *valid_param_list[] = { "tap", "socket", "dump" 846 #ifdef CONFIG_NET_BRIDGE 847 , "bridge" 848 #endif 849 #ifdef CONFIG_SLIRP 850 ,"user" 851 #endif 852 #ifdef CONFIG_VDE 853 ,"vde" 854 #endif 855 }; 856 for (i = 0; i < ARRAY_SIZE(valid_param_list); i++) { 857 if (!strncmp(valid_param_list[i], device, 858 strlen(valid_param_list[i]))) 859 return 1; 860 } 861 862 return 0; 863 } 864 865 void net_host_device_add(Monitor *mon, const QDict *qdict) 866 { 867 const char *device = qdict_get_str(qdict, "device"); 868 const char *opts_str = qdict_get_try_str(qdict, "opts"); 869 Error *local_err = NULL; 870 QemuOpts *opts; 871 872 if (!net_host_check_device(device)) { 873 monitor_printf(mon, "invalid host network device %s\n", device); 874 return; 875 } 876 877 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0); 878 if (!opts) { 879 return; 880 } 881 882 qemu_opt_set(opts, "type", device); 883 884 net_client_init(opts, 0, &local_err); 885 if (error_is_set(&local_err)) { 886 qerror_report_err(local_err); 887 error_free(local_err); 888 monitor_printf(mon, "adding host network device %s failed\n", device); 889 } 890 } 891 892 void net_host_device_remove(Monitor *mon, const QDict *qdict) 893 { 894 NetClientState *nc; 895 int vlan_id = qdict_get_int(qdict, "vlan_id"); 896 const char *device = qdict_get_str(qdict, "device"); 897 898 nc = net_hub_find_client_by_name(vlan_id, device); 899 if (!nc) { 900 return; 901 } 902 if (!net_host_check_device(nc->model)) { 903 monitor_printf(mon, "invalid host network device %s\n", device); 904 return; 905 } 906 qemu_del_net_client(nc); 907 } 908 909 void netdev_add(QemuOpts *opts, Error **errp) 910 { 911 net_client_init(opts, 1, errp); 912 } 913 914 int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret) 915 { 916 Error *local_err = NULL; 917 QemuOptsList *opts_list; 918 QemuOpts *opts; 919 920 opts_list = qemu_find_opts_err("netdev", &local_err); 921 if (error_is_set(&local_err)) { 922 goto exit_err; 923 } 924 925 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err); 926 if (error_is_set(&local_err)) { 927 goto exit_err; 928 } 929 930 netdev_add(opts, &local_err); 931 if (error_is_set(&local_err)) { 932 qemu_opts_del(opts); 933 goto exit_err; 934 } 935 936 return 0; 937 938 exit_err: 939 qerror_report_err(local_err); 940 error_free(local_err); 941 return -1; 942 } 943 944 void qmp_netdev_del(const char *id, Error **errp) 945 { 946 NetClientState *nc; 947 QemuOpts *opts; 948 949 nc = qemu_find_netdev(id); 950 if (!nc) { 951 error_set(errp, QERR_DEVICE_NOT_FOUND, id); 952 return; 953 } 954 955 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id); 956 if (!opts) { 957 error_setg(errp, "Device '%s' is not a netdev", id); 958 return; 959 } 960 961 qemu_del_net_client(nc); 962 qemu_opts_del(opts); 963 } 964 965 void print_net_client(Monitor *mon, NetClientState *nc) 966 { 967 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name, 968 nc->queue_index, 969 NetClientOptionsKind_lookup[nc->info->type], 970 nc->info_str); 971 } 972 973 RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name, 974 Error **errp) 975 { 976 NetClientState *nc; 977 RxFilterInfoList *filter_list = NULL, *last_entry = NULL; 978 979 QTAILQ_FOREACH(nc, &net_clients, next) { 980 RxFilterInfoList *entry; 981 RxFilterInfo *info; 982 983 if (has_name && strcmp(nc->name, name) != 0) { 984 continue; 985 } 986 987 /* only query rx-filter information of NIC */ 988 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) { 989 if (has_name) { 990 error_setg(errp, "net client(%s) isn't a NIC", name); 991 break; 992 } 993 continue; 994 } 995 996 if (nc->info->query_rx_filter) { 997 info = nc->info->query_rx_filter(nc); 998 entry = g_malloc0(sizeof(*entry)); 999 entry->value = info; 1000 1001 if (!filter_list) { 1002 filter_list = entry; 1003 } else { 1004 last_entry->next = entry; 1005 } 1006 last_entry = entry; 1007 } else if (has_name) { 1008 error_setg(errp, "net client(%s) doesn't support" 1009 " rx-filter querying", name); 1010 break; 1011 } 1012 } 1013 1014 if (filter_list == NULL && !error_is_set(errp) && has_name) { 1015 error_setg(errp, "invalid net client name: %s", name); 1016 } 1017 1018 return filter_list; 1019 } 1020 1021 void do_info_network(Monitor *mon, const QDict *qdict) 1022 { 1023 NetClientState *nc, *peer; 1024 NetClientOptionsKind type; 1025 1026 net_hub_info(mon); 1027 1028 QTAILQ_FOREACH(nc, &net_clients, next) { 1029 peer = nc->peer; 1030 type = nc->info->type; 1031 1032 /* Skip if already printed in hub info */ 1033 if (net_hub_id_for_client(nc, NULL) == 0) { 1034 continue; 1035 } 1036 1037 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) { 1038 print_net_client(mon, nc); 1039 } /* else it's a netdev connected to a NIC, printed with the NIC */ 1040 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) { 1041 monitor_printf(mon, " \\ "); 1042 print_net_client(mon, peer); 1043 } 1044 } 1045 } 1046 1047 void qmp_set_link(const char *name, bool up, Error **errp) 1048 { 1049 NetClientState *ncs[MAX_QUEUE_NUM]; 1050 NetClientState *nc; 1051 int queues, i; 1052 1053 queues = qemu_find_net_clients_except(name, ncs, 1054 NET_CLIENT_OPTIONS_KIND_MAX, 1055 MAX_QUEUE_NUM); 1056 1057 if (queues == 0) { 1058 error_set(errp, QERR_DEVICE_NOT_FOUND, name); 1059 return; 1060 } 1061 nc = ncs[0]; 1062 1063 for (i = 0; i < queues; i++) { 1064 ncs[i]->link_down = !up; 1065 } 1066 1067 if (nc->info->link_status_changed) { 1068 nc->info->link_status_changed(nc); 1069 } 1070 1071 if (nc->peer) { 1072 /* Change peer link only if the peer is NIC and then notify peer. 1073 * If the peer is a HUBPORT or a backend, we do not change the 1074 * link status. 1075 * 1076 * This behavior is compatible with qemu vlans where there could be 1077 * multiple clients that can still communicate with each other in 1078 * disconnected mode. For now maintain this compatibility. 1079 */ 1080 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { 1081 for (i = 0; i < queues; i++) { 1082 ncs[i]->peer->link_down = !up; 1083 } 1084 } 1085 if (nc->peer->info->link_status_changed) { 1086 nc->peer->info->link_status_changed(nc->peer); 1087 } 1088 } 1089 } 1090 1091 void net_cleanup(void) 1092 { 1093 NetClientState *nc; 1094 1095 /* We may del multiple entries during qemu_del_net_client(), 1096 * so QTAILQ_FOREACH_SAFE() is also not safe here. 1097 */ 1098 while (!QTAILQ_EMPTY(&net_clients)) { 1099 nc = QTAILQ_FIRST(&net_clients); 1100 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) { 1101 qemu_del_nic(qemu_get_nic(nc)); 1102 } else { 1103 qemu_del_net_client(nc); 1104 } 1105 } 1106 } 1107 1108 void net_check_clients(void) 1109 { 1110 NetClientState *nc; 1111 int i; 1112 1113 /* Don't warn about the default network setup that you get if 1114 * no command line -net or -netdev options are specified. There 1115 * are two cases that we would otherwise complain about: 1116 * (1) board doesn't support a NIC but the implicit "-net nic" 1117 * requested one 1118 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic" 1119 * sets up a nic that isn't connected to anything. 1120 */ 1121 if (default_net) { 1122 return; 1123 } 1124 1125 net_hub_check_clients(); 1126 1127 QTAILQ_FOREACH(nc, &net_clients, next) { 1128 if (!nc->peer) { 1129 fprintf(stderr, "Warning: %s %s has no peer\n", 1130 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ? 1131 "nic" : "netdev", nc->name); 1132 } 1133 } 1134 1135 /* Check that all NICs requested via -net nic actually got created. 1136 * NICs created via -device don't need to be checked here because 1137 * they are always instantiated. 1138 */ 1139 for (i = 0; i < MAX_NICS; i++) { 1140 NICInfo *nd = &nd_table[i]; 1141 if (nd->used && !nd->instantiated) { 1142 fprintf(stderr, "Warning: requested NIC (%s, model %s) " 1143 "was not created (not supported by this machine?)\n", 1144 nd->name ? nd->name : "anonymous", 1145 nd->model ? nd->model : "unspecified"); 1146 } 1147 } 1148 } 1149 1150 static int net_init_client(QemuOpts *opts, void *dummy) 1151 { 1152 Error *local_err = NULL; 1153 1154 net_client_init(opts, 0, &local_err); 1155 if (error_is_set(&local_err)) { 1156 qerror_report_err(local_err); 1157 error_free(local_err); 1158 return -1; 1159 } 1160 1161 return 0; 1162 } 1163 1164 static int net_init_netdev(QemuOpts *opts, void *dummy) 1165 { 1166 Error *local_err = NULL; 1167 int ret; 1168 1169 ret = net_client_init(opts, 1, &local_err); 1170 if (error_is_set(&local_err)) { 1171 qerror_report_err(local_err); 1172 error_free(local_err); 1173 return -1; 1174 } 1175 1176 return ret; 1177 } 1178 1179 int net_init_clients(void) 1180 { 1181 QemuOptsList *net = qemu_find_opts("net"); 1182 1183 if (default_net) { 1184 /* if no clients, we use a default config */ 1185 qemu_opts_set(net, NULL, "type", "nic"); 1186 #ifdef CONFIG_SLIRP 1187 qemu_opts_set(net, NULL, "type", "user"); 1188 #endif 1189 } 1190 1191 QTAILQ_INIT(&net_clients); 1192 1193 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1) 1194 return -1; 1195 1196 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) { 1197 return -1; 1198 } 1199 1200 return 0; 1201 } 1202 1203 int net_client_parse(QemuOptsList *opts_list, const char *optarg) 1204 { 1205 #if defined(CONFIG_SLIRP) 1206 int ret; 1207 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) { 1208 return ret; 1209 } 1210 #endif 1211 1212 if (!qemu_opts_parse(opts_list, optarg, 1)) { 1213 return -1; 1214 } 1215 1216 default_net = 0; 1217 return 0; 1218 } 1219 1220 /* From FreeBSD */ 1221 /* XXX: optimize */ 1222 unsigned compute_mcast_idx(const uint8_t *ep) 1223 { 1224 uint32_t crc; 1225 int carry, i, j; 1226 uint8_t b; 1227 1228 crc = 0xffffffff; 1229 for (i = 0; i < 6; i++) { 1230 b = *ep++; 1231 for (j = 0; j < 8; j++) { 1232 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01); 1233 crc <<= 1; 1234 b >>= 1; 1235 if (carry) { 1236 crc = ((crc ^ POLYNOMIAL) | carry); 1237 } 1238 } 1239 } 1240 return crc >> 26; 1241 } 1242 1243 QemuOptsList qemu_netdev_opts = { 1244 .name = "netdev", 1245 .implied_opt_name = "type", 1246 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), 1247 .desc = { 1248 /* 1249 * no elements => accept any params 1250 * validation will happen later 1251 */ 1252 { /* end of list */ } 1253 }, 1254 }; 1255 1256 QemuOptsList qemu_net_opts = { 1257 .name = "net", 1258 .implied_opt_name = "type", 1259 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), 1260 .desc = { 1261 /* 1262 * no elements => accept any params 1263 * validation will happen later 1264 */ 1265 { /* end of list */ } 1266 }, 1267 }; 1268