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,6 +1735,10 @@ static const mon_cmd_t mon_cmds[] = {
1735 "tap|user|socket|vde|dump [options]", "add host VLAN client" }, 1735 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
1736 { "host_net_remove", "is", net_host_device_remove, 1736 { "host_net_remove", "is", net_host_device_remove,
1737 "vlan_id name", "remove host VLAN client" }, 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 { "balloon", "i", do_balloon, 1742 { "balloon", "i", do_balloon,
1739 "target", "request VM to change it's memory allocation (in MB)" }, 1743 "target", "request VM to change it's memory allocation (in MB)" },
1740 { "set_link", "ss", do_set_link, 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,11 +556,11 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
556 return 0; 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 int is_udp; 561 int is_udp;
562 char buf[256], *r; 562 char buf[256], *r;
563 - const char *p; 563 + const char *p, *errmsg;
564 struct in_addr guest_addr; 564 struct in_addr guest_addr;
565 int host_port, guest_port; 565 int host_port, guest_port;
566 566
@@ -571,41 +571,48 @@ void net_slirp_redir(const char *redir_str) @@ -571,41 +571,48 @@ void net_slirp_redir(const char *redir_str)
571 571
572 p = redir_str; 572 p = redir_str;
573 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) 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 is_udp = 0; 576 is_udp = 0;
577 } else if (!strcmp(buf, "udp")) { 577 } else if (!strcmp(buf, "udp")) {
578 is_udp = 1; 578 is_udp = 1;
579 } else { 579 } else {
580 - goto fail; 580 + goto fail_syntax;
581 } 581 }
582 582
583 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) 583 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
584 - goto fail; 584 + goto fail_syntax;
585 host_port = strtol(buf, &r, 0); 585 host_port = strtol(buf, &r, 0);
586 if (r == buf) 586 if (r == buf)
587 - goto fail; 587 + goto fail_syntax;
588 588
589 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) 589 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
590 - goto fail; 590 + goto fail_syntax;
591 if (buf[0] == '\0') { 591 if (buf[0] == '\0') {
592 pstrcpy(buf, sizeof(buf), "10.0.2.15"); 592 pstrcpy(buf, sizeof(buf), "10.0.2.15");
593 } 593 }
594 if (!inet_aton(buf, &guest_addr)) 594 if (!inet_aton(buf, &guest_addr))
595 - goto fail; 595 + goto fail_syntax;
596 596
597 guest_port = strtol(p, &r, 0); 597 guest_port = strtol(p, &r, 0);
598 if (r == p) 598 if (r == p)
599 - goto fail; 599 + goto fail_syntax;
600 600
601 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { 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 return; 605 return;
  606 +
  607 + fail_syntax:
  608 + errmsg = "invalid redirection format\n";
606 fail: 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 #ifndef _WIN32 618 #ifndef _WIN32
@@ -112,7 +112,7 @@ int net_client_init(const char *device, const char *p); @@ -112,7 +112,7 @@ int net_client_init(const char *device, const char *p);
112 void net_client_uninit(NICInfo *nd); 112 void net_client_uninit(NICInfo *nd);
113 int net_client_parse(const char *str); 113 int net_client_parse(const char *str);
114 void net_slirp_smb(const char *exported_dir); 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 void net_cleanup(void); 116 void net_cleanup(void);
117 int slirp_is_inited(void); 117 int slirp_is_inited(void);
118 void net_client_check(void); 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,7 +943,7 @@ When using the user mode network stack, redirect incoming TCP or UDP
943 connections to the host port @var{host-port} to the guest 943 connections to the host port @var{host-port} to the guest
944 @var{guest-host} on guest port @var{guest-port}. If @var{guest-host} 944 @var{guest-host} on guest port @var{guest-port}. If @var{guest-host}
945 is not specified, its value is 10.0.2.15 (default address given by the 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 For example, to redirect host X11 connection from screen 1 to guest 948 For example, to redirect host X11 connection from screen 1 to guest
949 screen 0, use the following: 949 screen 0, use the following:
@@ -4589,7 +4589,7 @@ int main(int argc, char **argv, char **envp) @@ -4589,7 +4589,7 @@ int main(int argc, char **argv, char **envp)
4589 break; 4589 break;
4590 #endif 4590 #endif
4591 case QEMU_OPTION_redir: 4591 case QEMU_OPTION_redir:
4592 - net_slirp_redir(optarg); 4592 + net_slirp_redir(NULL, optarg);
4593 break; 4593 break;
4594 #endif 4594 #endif
4595 case QEMU_OPTION_bt: 4595 case QEMU_OPTION_bt: