1
0
Fork 0
mirror of https://passt.top/passt synced 2025-06-20 05:55:33 +02:00

conf: Use 0 instead of -1 as "unassigned" mtu value

On the command line -m 0 means "don't assign an MTU" (letting the guest use
its default.  However, internally we use (c->mtu == -1) to represent that
state.  We use (c->mtu == 0) to represent "the user didn't specify on the
command line, so use the default" - but this is only used during conf(),
never afterwards.

This is unnecessarily confusing.  We can instead just initialise c->mtu to
its default (65520) before parsing options and use 0 on both the command
line and internally to represent the "don't assign" special case.  This
ensures that c->mtu is always 0..65535, so we can store it in a uint16_t
which is more natural.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2025-02-19 14:14:28 +11:00 committed by Stefano Brivio
parent 3dc7da68a2
commit 1cc5d4c9fe
6 changed files with 8 additions and 14 deletions

11
conf.c
View file

@ -1413,6 +1413,7 @@ void conf(struct ctx *c, int argc, char **argv)
optstring = "+dqfel:hs:F:p:P:m:a:n:M:g:i:o:D:S:H:461t:u:"; optstring = "+dqfel:hs:F:p:P:m:a:n:M:g:i:o:D:S:H:461t:u:";
} }
c->mtu = ROUND_DOWN(ETH_MAX_MTU - ETH_HLEN, sizeof(uint32_t));
c->tcp.fwd_in.mode = c->tcp.fwd_out.mode = FWD_UNSET; c->tcp.fwd_in.mode = c->tcp.fwd_out.mode = FWD_UNSET;
c->udp.fwd_in.mode = c->udp.fwd_out.mode = FWD_UNSET; c->udp.fwd_in.mode = c->udp.fwd_out.mode = FWD_UNSET;
memcpy(c->our_tap_mac, MAC_OUR_LAA, ETH_ALEN); memcpy(c->our_tap_mac, MAC_OUR_LAA, ETH_ALEN);
@ -1662,12 +1663,7 @@ void conf(struct ctx *c, int argc, char **argv)
if (errno || *e) if (errno || *e)
die("Invalid MTU: %s", optarg); die("Invalid MTU: %s", optarg);
if (!mtu) { if (mtu && (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU)) {
c->mtu = -1;
break;
}
if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
die("MTU %lu out of range (%u..%u)", mtu, die("MTU %lu out of range (%u..%u)", mtu,
ETH_MIN_MTU, ETH_MAX_MTU); ETH_MIN_MTU, ETH_MAX_MTU);
} }
@ -1980,9 +1976,6 @@ void conf(struct ctx *c, int argc, char **argv)
c->no_dhcpv6 = 1; c->no_dhcpv6 = 1;
} }
if (!c->mtu)
c->mtu = ROUND_DOWN(ETH_MAX_MTU - ETH_HLEN, sizeof(uint32_t));
get_dns(c); get_dns(c);
if (!*c->pasta_ifn) { if (!*c->pasta_ifn) {

2
dhcp.c
View file

@ -417,7 +417,7 @@ int dhcp(const struct ctx *c, const struct pool *p)
&c->ip4.guest_gw, sizeof(c->ip4.guest_gw)); &c->ip4.guest_gw, sizeof(c->ip4.guest_gw));
} }
if (c->mtu != -1) { if (c->mtu) {
opts[26].slen = 2; opts[26].slen = 2;
opts[26].s[0] = c->mtu / 256; opts[26].s[0] = c->mtu / 256;
opts[26].s[1] = c->mtu % 256; opts[26].s[1] = c->mtu % 256;

2
ndp.c
View file

@ -256,7 +256,7 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
ptr = &ra.var[0]; ptr = &ra.var[0];
if (c->mtu != -1) { if (c->mtu) {
struct opt_mtu *mtu = (struct opt_mtu *)ptr; struct opt_mtu *mtu = (struct opt_mtu *)ptr;
*mtu = (struct opt_mtu) { *mtu = (struct opt_mtu) {
.header = { .header = {

View file

@ -274,6 +274,8 @@ struct ctx {
int fd_repair; int fd_repair;
unsigned char our_tap_mac[ETH_ALEN]; unsigned char our_tap_mac[ETH_ALEN];
unsigned char guest_mac[ETH_ALEN]; unsigned char guest_mac[ETH_ALEN];
uint16_t mtu;
uint64_t hash_secret[2]; uint64_t hash_secret[2];
int ifi4; int ifi4;
@ -298,7 +300,6 @@ struct ctx {
int no_icmp; int no_icmp;
struct icmp_ctx icmp; struct icmp_ctx icmp;
int mtu;
int no_dns; int no_dns;
int no_dns_search; int no_dns_search;
int no_dhcp_dns; int no_dhcp_dns;

View file

@ -319,7 +319,7 @@ void pasta_ns_conf(struct ctx *c)
if (c->pasta_conf_ns) { if (c->pasta_conf_ns) {
unsigned int flags = IFF_UP; unsigned int flags = IFF_UP;
if (c->mtu != -1) if (c->mtu)
nl_link_set_mtu(nl_sock_ns, c->pasta_ifi, c->mtu); nl_link_set_mtu(nl_sock_ns, c->pasta_ifi, c->mtu);
if (c->ifi6) /* Avoid duplicate address detection on link up */ if (c->ifi6) /* Avoid duplicate address detection on link up */

2
tcp.c
View file

@ -1139,7 +1139,7 @@ int tcp_prepare_flags(const struct ctx *c, struct tcp_tap_conn *conn,
if (flags & SYN) { if (flags & SYN) {
int mss; int mss;
if (c->mtu == -1) { if (!c->mtu) {
mss = tinfo.tcpi_snd_mss; mss = tinfo.tcpi_snd_mss;
} else { } else {
mss = c->mtu - sizeof(struct tcphdr); mss = c->mtu - sizeof(struct tcphdr);