conf, tap: Add option to quit once the client closes the connection

This is practical to avoid explicit lifecycle management in users,
e.g. libvirtd, and is trivial to implement.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
Stefano Brivio 2022-10-07 04:01:56 +02:00
parent 9de65dd3f4
commit a62ed181db
4 changed files with 27 additions and 1 deletions

17
conf.c
View file

@ -712,6 +712,7 @@ static void usage(const char *name)
if (strstr(name, "pasta")) if (strstr(name, "pasta"))
goto pasta_opts; goto pasta_opts;
info( " -1, --one-off Quit after handling one single client");
info( " -t, --tcp-ports SPEC TCP port forwarding to guest"); info( " -t, --tcp-ports SPEC TCP port forwarding to guest");
info( " can be specified multiple times"); info( " can be specified multiple times");
info( " SPEC can be:"); info( " SPEC can be:");
@ -1023,6 +1024,7 @@ void conf(struct ctx *c, int argc, char **argv)
{"no-map-gw", no_argument, &c->no_map_gw, 1 }, {"no-map-gw", no_argument, &c->no_map_gw, 1 },
{"ipv4-only", no_argument, NULL, '4' }, {"ipv4-only", no_argument, NULL, '4' },
{"ipv6-only", no_argument, NULL, '6' }, {"ipv6-only", no_argument, NULL, '6' },
{"one-off", no_argument, NULL, '1' },
{"tcp-ports", required_argument, NULL, 't' }, {"tcp-ports", required_argument, NULL, 't' },
{"udp-ports", required_argument, NULL, 'u' }, {"udp-ports", required_argument, NULL, 'u' },
{"tcp-ns", required_argument, NULL, 'T' }, {"tcp-ns", required_argument, NULL, 'T' },
@ -1062,7 +1064,7 @@ void conf(struct ctx *c, int argc, char **argv)
c->no_dhcp_dns = c->no_dhcp_dns_search = 1; c->no_dhcp_dns = c->no_dhcp_dns_search = 1;
optstring = "dqfel:hI:p:P:m:a:n:M:g:i:D:S:46t:u:T:U:"; optstring = "dqfel:hI:p:P:m:a:n:M:g:i:D:S:46t:u:T:U:";
} else { } else {
optstring = "dqfel:hs:p:P:m:a:n:M:g:i:D:S:46t:u:"; optstring = "dqfel:hs:p:P:m:a:n:M:g:i:D:S:461t:u:";
} }
c->tcp.fwd_in.mode = c->tcp.fwd_out.mode = 0; c->tcp.fwd_in.mode = c->tcp.fwd_out.mode = 0;
@ -1488,6 +1490,19 @@ void conf(struct ctx *c, int argc, char **argv)
case '6': case '6':
v6_only = true; v6_only = true;
break; break;
case '1':
if (c->mode != MODE_PASST) {
err("--one-off is for passt mode only");
usage(argv[0]);
}
if (c->one_off) {
err("Redundant --one-off option");
usage(argv[0]);
}
c->one_off = true;
break;
case 't': case 't':
case 'u': case 'u':
case 'T': case 'T':

View file

@ -302,6 +302,11 @@ Path for UNIX domain socket used by \fBqemu\fR(1) or \fBqrap\fR(1) to connect to
Default is to probe a free socket, not accepting connections, starting from Default is to probe a free socket, not accepting connections, starting from
\fI/tmp/passt_1.socket\fR to \fI/tmp/passt_64.socket\fR. \fI/tmp/passt_1.socket\fR to \fI/tmp/passt_64.socket\fR.
.TP
.BR \-1 ", " \-\-one-off
Quit after handling a single client connection, that is, once the client closes
the socket, or once we get a socket error.
.TP .TP
.BR \-t ", " \-\-tcp-ports " " \fIspec .BR \-t ", " \-\-tcp-ports " " \fIspec
Configure TCP port forwarding to guest. \fIspec\fR can be one of: Configure TCP port forwarding to guest. \fIspec\fR can be one of:

View file

@ -196,6 +196,7 @@ struct ctx {
char sock_path[UNIX_PATH_MAX]; char sock_path[UNIX_PATH_MAX];
char pcap[PATH_MAX]; char pcap[PATH_MAX];
char pid_file[PATH_MAX]; char pid_file[PATH_MAX];
int one_off;
int pasta_netns_fd; int pasta_netns_fd;

5
tap.c
View file

@ -1011,5 +1011,10 @@ void tap_handler(struct ctx *c, int fd, uint32_t events,
return; return;
reinit: reinit:
if (c->one_off) {
info("Client closed connection, exiting");
exit(EXIT_SUCCESS);
}
tap_sock_init(c); tap_sock_init(c);
} }