Commit d4ebe1934a89b1ab699a9485c85c42f1ec9ae2ad

Authored by aliguori
1 parent 764a4d1d

slirp: Enhance host-guest redirection setup (Jan Kiszka)

Allow to establish a TCP/UDP connection redirection also via a monitor
command 'host_net_redir'. Moreover, assume TCP as connection type if
that parameter is omitted.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@7204 c046a42c-6fe2-441c-8c8c-71466251a162
monitor.c
... ... @@ -1735,6 +1735,10 @@ static const mon_cmd_t mon_cmds[] = {
1735 1735 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
1736 1736 { "host_net_remove", "is", net_host_device_remove,
1737 1737 "vlan_id name", "remove host VLAN client" },
  1738 +#ifdef CONFIG_SLIRP
  1739 + { "host_net_redir", "s", net_slirp_redir,
  1740 + "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
  1741 +#endif
1738 1742 { "balloon", "i", do_balloon,
1739 1743 "target", "request VM to change it's memory allocation (in MB)" },
1740 1744 { "set_link", "ss", do_set_link,
... ...
... ... @@ -556,11 +556,11 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
556 556 return 0;
557 557 }
558 558  
559   -void net_slirp_redir(const char *redir_str)
  559 +void net_slirp_redir(Monitor *mon, const char *redir_str)
560 560 {
561 561 int is_udp;
562 562 char buf[256], *r;
563   - const char *p;
  563 + const char *p, *errmsg;
564 564 struct in_addr guest_addr;
565 565 int host_port, guest_port;
566 566  
... ... @@ -571,41 +571,48 @@ void net_slirp_redir(const char *redir_str)
571 571  
572 572 p = redir_str;
573 573 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
574   - goto fail;
575   - if (!strcmp(buf, "tcp")) {
  574 + goto fail_syntax;
  575 + if (!strcmp(buf, "tcp") || buf[0] == '\0') {
576 576 is_udp = 0;
577 577 } else if (!strcmp(buf, "udp")) {
578 578 is_udp = 1;
579 579 } else {
580   - goto fail;
  580 + goto fail_syntax;
581 581 }
582 582  
583 583 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
584   - goto fail;
  584 + goto fail_syntax;
585 585 host_port = strtol(buf, &r, 0);
586 586 if (r == buf)
587   - goto fail;
  587 + goto fail_syntax;
588 588  
589 589 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
590   - goto fail;
  590 + goto fail_syntax;
591 591 if (buf[0] == '\0') {
592 592 pstrcpy(buf, sizeof(buf), "10.0.2.15");
593 593 }
594 594 if (!inet_aton(buf, &guest_addr))
595   - goto fail;
  595 + goto fail_syntax;
596 596  
597 597 guest_port = strtol(p, &r, 0);
598 598 if (r == p)
599   - goto fail;
  599 + goto fail_syntax;
600 600  
601 601 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
602   - fprintf(stderr, "qemu: could not set up redirection\n");
603   - exit(1);
  602 + errmsg = "could not set up redirection\n";
  603 + goto fail;
604 604 }
605 605 return;
  606 +
  607 + fail_syntax:
  608 + errmsg = "invalid redirection format\n";
606 609 fail:
607   - fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
608   - exit(1);
  610 + if (mon) {
  611 + monitor_printf(mon, errmsg);
  612 + } else {
  613 + fprintf(stderr, "qemu: %s", errmsg);
  614 + exit(1);
  615 + }
609 616 }
610 617  
611 618 #ifndef _WIN32
... ...
... ... @@ -112,7 +112,7 @@ int net_client_init(const char *device, const char *p);
112 112 void net_client_uninit(NICInfo *nd);
113 113 int net_client_parse(const char *str);
114 114 void net_slirp_smb(const char *exported_dir);
115   -void net_slirp_redir(const char *redir_str);
  115 +void net_slirp_redir(Monitor *mon, const char *redir_str);
116 116 void net_cleanup(void);
117 117 int slirp_is_inited(void);
118 118 void net_client_check(void);
... ...
qemu-options.hx
... ... @@ -943,7 +943,7 @@ When using the user mode network stack, redirect incoming TCP or UDP
943 943 connections to the host port @var{host-port} to the guest
944 944 @var{guest-host} on guest port @var{guest-port}. If @var{guest-host}
945 945 is not specified, its value is 10.0.2.15 (default address given by the
946   -built-in DHCP server).
  946 +built-in DHCP server). If no connection type is specified, TCP is used.
947 947  
948 948 For example, to redirect host X11 connection from screen 1 to guest
949 949 screen 0, use the following:
... ...
... ... @@ -4589,7 +4589,7 @@ int main(int argc, char **argv, char **envp)
4589 4589 break;
4590 4590 #endif
4591 4591 case QEMU_OPTION_redir:
4592   - net_slirp_redir(optarg);
  4592 + net_slirp_redir(NULL, optarg);
4593 4593 break;
4594 4594 #endif
4595 4595 case QEMU_OPTION_bt:
... ...