Commit 1c6ed9f3379faac83da0ed3e95cbd49003ac0dd1

Authored by Alexander Graf
Committed by Anthony Liguori
1 parent c1261d8d

User networking: Show active connections

In case you're wondering what connections exactly you have open
or maybe redir'ed in the past, you can't really find out from qemu
right now.

This patch enables you to see all current connections the host
only networking holds open, so you can kill them using the previous
patch.

Signed-off-by: Alexander Graf <agraf@suse.de>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
monitor.c
... ... @@ -1761,7 +1761,8 @@ static const mon_cmd_t mon_cmds[] = {
1761 1761 #ifdef CONFIG_SLIRP
1762 1762 { "host_net_redir", "ss?", net_slirp_redir,
1763 1763 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
1764   - "host_net_redir remove [tcp:|udp:]host-port -- remove redirection" },
  1764 + "host_net_redir remove [tcp:|udp:]host-port -- remove redirection\n"
  1765 + "host_net_redir list -- show all redirections" },
1765 1766 #endif
1766 1767 { "balloon", "i", do_balloon,
1767 1768 "target", "request VM to change it's memory allocation (in MB)" },
... ...
... ... @@ -568,6 +568,45 @@ static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
568 568 return 0;
569 569 }
570 570  
  571 +static void net_slirp_redir_print(void *opaque, int is_udp,
  572 + struct in_addr *laddr, u_int lport,
  573 + struct in_addr *faddr, u_int fport)
  574 +{
  575 + Monitor *mon = (Monitor *)opaque;
  576 + uint32_t h_addr;
  577 + uint32_t g_addr;
  578 + char buf[16];
  579 +
  580 + h_addr = ntohl(faddr->s_addr);
  581 + g_addr = ntohl(laddr->s_addr);
  582 +
  583 + monitor_printf(mon, " %s |", is_udp ? "udp" : "tcp" );
  584 + snprintf(buf, 15, "%d.%d.%d.%d", (h_addr >> 24) & 0xff,
  585 + (h_addr >> 16) & 0xff,
  586 + (h_addr >> 8) & 0xff,
  587 + (h_addr) & 0xff);
  588 + monitor_printf(mon, " %15s |", buf);
  589 + monitor_printf(mon, " %5d |", fport);
  590 +
  591 + snprintf(buf, 15, "%d.%d.%d.%d", (g_addr >> 24) & 0xff,
  592 + (g_addr >> 16) & 0xff,
  593 + (g_addr >> 8) & 0xff,
  594 + (g_addr) & 0xff);
  595 + monitor_printf(mon, " %15s |", buf);
  596 + monitor_printf(mon, " %5d\n", lport);
  597 +
  598 +}
  599 +
  600 +static void net_slirp_redir_list(Monitor *mon)
  601 +{
  602 + if (!mon)
  603 + return;
  604 +
  605 + monitor_printf(mon, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
  606 + monitor_printf(mon, " | | | | \n");
  607 + slirp_redir_loop(net_slirp_redir_print, mon);
  608 +}
  609 +
571 610 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
572 611 {
573 612 int host_port;
... ... @@ -622,6 +661,11 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
622 661 return;
623 662 }
624 663  
  664 + if (!strcmp(redir_str, "list")) {
  665 + net_slirp_redir_list(mon);
  666 + return;
  667 + }
  668 +
625 669 p = redir_str;
626 670 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
627 671 goto fail_syntax;
... ...
slirp/libslirp.h
... ... @@ -18,6 +18,10 @@ void slirp_input(const uint8_t *pkt, int pkt_len);
18 18 int slirp_can_output(void);
19 19 void slirp_output(const uint8_t *pkt, int pkt_len);
20 20  
  21 +void slirp_redir_loop(void (*func)(void *opaque, int is_udp,
  22 + struct in_addr *laddr, u_int lport,
  23 + struct in_addr *faddr, u_int fport),
  24 + void *opaque);
21 25 int slirp_redir_rm(int is_udp, int host_port);
22 26 int slirp_redir(int is_udp, int host_port,
23 27 struct in_addr guest_addr, int guest_port);
... ...
slirp/slirp.c
... ... @@ -734,6 +734,30 @@ void if_encap(const uint8_t *ip_data, int ip_data_len)
734 734 }
735 735 }
736 736  
  737 +static void _slirp_redir_loop(void (*func)(void *opaque, int is_udp,
  738 + struct in_addr *laddr, u_int lport,
  739 + struct in_addr *faddr, u_int fport),
  740 + void *opaque, int is_udp)
  741 +{
  742 + struct socket *head = (is_udp ? &udb : &tcb);
  743 + struct socket *so;
  744 +
  745 + for (so = head->so_next; so != head; so = so->so_next) {
  746 + func(opaque, is_udp,
  747 + &so->so_laddr, ntohs(so->so_lport),
  748 + &so->so_faddr, ntohs(so->so_fport));
  749 + }
  750 +}
  751 +
  752 +void slirp_redir_loop(void (*func)(void *opaque, int is_udp,
  753 + struct in_addr *laddr, u_int lport,
  754 + struct in_addr *faddr, u_int fport),
  755 + void *opaque)
  756 +{
  757 + _slirp_redir_loop(func, opaque, 0);
  758 + _slirp_redir_loop(func, opaque, 1);
  759 +}
  760 +
737 761 /* Unlistens a redirection
738 762 *
739 763 * Return value: number of redirs removed */
... ...