Received: from list by lists.gnu.org with archive (Exim 4.71) id 1h1ujP-0001pw-8I for mharc-grub-devel@gnu.org; Thu, 07 Mar 2019 10:14:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59009) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h1ujG-0001iL-Gr for grub-devel@gnu.org; Thu, 07 Mar 2019 10:14:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h1ujF-0005By-Kr for grub-devel@gnu.org; Thu, 07 Mar 2019 10:14:38 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:43476 helo=foss.arm.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h1ujF-0005BE-Bn for grub-devel@gnu.org; Thu, 07 Mar 2019 10:14:37 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 65EFA1684; Thu, 7 Mar 2019 07:14:36 -0800 (PST) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.197.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0FD503F706; Thu, 7 Mar 2019 07:14:34 -0800 (PST) From: Andre Przywara To: Daniel Kiper Cc: Vladimir Serbinenko , Andrei Borzenkov , Daniel Kiper , Mark Rutland , grub-devel@gnu.org Subject: [PATCH v3 06/10] net: dhcp: introduce per-interface timeout Date: Thu, 7 Mar 2019 15:14:12 +0000 Message-Id: <20190307151416.49014-7-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190307151416.49014-1-andre.przywara@arm.com> References: <20190307151416.49014-1-andre.przywara@arm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 217.140.101.70 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Mar 2019 15:14:44 -0000 From: Andrei Borzenkov Currently we have a global timeout for all network cards in the BOOTP/DHCP discovery process. Make this timeout a per-interface one, so better accommodate the upcoming 4-way DHCP handshake and to also cover the lease time limit a DHCP offer will come with. Signed-off-by: Andre Przywara Reviewed-by: Daniel Kiper --- grub-core/net/bootp.c | 38 ++++++++++++++++++++++++++++++++++---- include/grub/net.h | 2 ++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c index 5bb5b3d27..2fb79d162 100644 --- a/grub-core/net/bootp.c +++ b/grub-core/net/bootp.c @@ -33,6 +33,9 @@ enum #define GRUB_BOOTP_MAX_OPTIONS_SIZE 64 +/* Max timeout when waiting for BOOTP/DHCP reply */ +#define GRUB_DHCP_MAX_PACKET_TIMEOUT 32 + static const void * find_dhcp_option (const struct grub_net_bootp_packet *bp, grub_size_t size, grub_uint8_t opt_code, grub_uint8_t *opt_len) @@ -548,7 +551,6 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), struct grub_net_network_level_interface *ifaces; grub_size_t ncards = 0; unsigned j = 0; - int interval; grub_err_t err; unsigned i; @@ -587,6 +589,7 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), ifaces[j].address.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV; grub_memcpy (&ifaces[j].hwaddress, &card->default_address, sizeof (ifaces[j].hwaddress)); + ifaces[j].dhcp_tmo = ifaces[j].dhcp_tmo_left = 1; j++; } ifaces[ncards - 1].next = grub_net_network_level_interfaces; @@ -594,25 +597,52 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)), grub_net_network_level_interfaces->prev = & ifaces[ncards - 1].next; grub_net_network_level_interfaces = &ifaces[0]; ifaces[0].prev = &grub_net_network_level_interfaces; - for (interval = 200; interval < 10000; interval *= 2) + + /* + * Running DHCP restransmission timer is kept per interface in dhcp_tmo_left. + * When it runs off, dhcp_tmo is increased exponentionally and dhcp_tmo_left + * initialized to it. Max value is 32 which gives approximately 12s total per + * packet timeout assuming 200ms poll tick. Timeout is reset when DHCP OFFER + * is received, so total timeout is 25s in the worst case. + * + * DHCP NAK also resets timer and transaction starts again. + * + * Total wait time is limited to ~25s to prevent endless loop in case of + * permanent NAK + */ + for (i = 0; i < GRUB_DHCP_MAX_PACKET_TIMEOUT * 4; i++) { int need_poll = 0; for (j = 0; j < ncards; j++) { - if (!ifaces[j].prev) + if (!ifaces[j].prev || + ifaces[j].dhcp_tmo > GRUB_DHCP_MAX_PACKET_TIMEOUT) + continue; + + if (--ifaces[j].dhcp_tmo_left) + { + need_poll = 1; + continue; + } + + ifaces[j].dhcp_tmo *= 2; + if (ifaces[j].dhcp_tmo > GRUB_DHCP_MAX_PACKET_TIMEOUT) continue; err = send_dhcp_packet (&ifaces[j]); if (err) { grub_print_error (); + /* To ignore it during next poll */ + ifaces[j].dhcp_tmo = GRUB_DHCP_MAX_PACKET_TIMEOUT + 1; continue; } + ifaces[j].dhcp_tmo_left = ifaces[j].dhcp_tmo; need_poll = 1; } if (!need_poll) break; - grub_net_poll_cards (interval, 0); + grub_net_poll_cards (200, 0); } err = GRUB_ERR_NONE; diff --git a/include/grub/net.h b/include/grub/net.h index 3f649d753..a1138f5d4 100644 --- a/include/grub/net.h +++ b/include/grub/net.h @@ -292,6 +292,8 @@ struct grub_net_network_level_interface struct grub_net_bootp_packet *dhcp_ack; grub_size_t dhcp_acklen; grub_uint16_t vlantag; + unsigned dhcp_tmo_left; /* DHCPv4 running retransmission timeout */ + unsigned dhcp_tmo; /* DHCPv4 current retransmission timeout */ void *data; }; -- 2.17.1