Skip to contentSkip to conversation

dhcp: Add priority to logging

PATCH Superseded Michael Johnson 2 participants 4 messages May 17, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Michael Johnson

author
#0 lore raw

PATCH 1/3 dhcp: Add priority to logging

Most dhcp clients have some logging to show what state the lease is in so this just makes iwd more in line with them. The logging won't be enabled by default but requires a max logging level to be passed in when logging is set up.

---
 ell/dhcp.c | 64 +++++++++++++++++++++++++++++-------------------------
 ell/dhcp.h |  2 +-
 2 files changed, 35 insertions(+), 31 deletions(-)
ell/dhcp.c +34 -30
index 2d04900..13e96cb 100644
@@ -43,10 +43,12 @@
 #include "netlink.h"
 #include "rtnl.h"
 #include "acd.h"
+#include "log.h"
 
-#define CLIENT_DEBUG(fmt, args...)					\
-	l_util_debug(client->debug_handler, client->debug_data,		\
-			"%s:%i " fmt, __func__, __LINE__, ## args)
+#define CLIENT_DEBUG(priority, fmt, args...)					\
+	if (priority <= client->debug_level)		\
+		l_util_debug(client->debug_handler, client->debug_data,		\
+				"%s:%i " fmt, __func__, __LINE__, ## args)
 #define CLIENT_ENTER_STATE(s)						\
 	l_util_debug(client->debug_handler, client->debug_data,		\
 			"%s:%i Entering state: " #s,			\
@@ -192,6 +194,7 @@ struct l_dhcp_client {
 	l_dhcp_destroy_cb_t event_destroy;
 	l_dhcp_debug_cb_t debug_handler;
 	l_dhcp_destroy_cb_t debug_destroy;
+	int debug_level;
 	struct l_acd *acd;
 	void *debug_data;
 	bool have_addr : 1;
@@ -343,7 +346,7 @@ static int dhcp_client_send_discover(struct l_dhcp_client *client)
 	L_AUTO_FREE_VAR(struct dhcp_message *, discover);
 	int err;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	discover = (struct dhcp_message *) l_new(uint8_t, len);
 
@@ -393,7 +396,7 @@ static int dhcp_client_send_request(struct l_dhcp_client *client)
 	L_AUTO_FREE_VAR(struct dhcp_message *, request);
 	int err;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	request = (struct dhcp_message *) l_new(uint8_t, len);
 
@@ -425,14 +428,14 @@ static int dhcp_client_send_request(struct l_dhcp_client *client)
 		if (!_dhcp_message_builder_append(&builder,
 					L_DHCP_OPTION_SERVER_IDENTIFIER,
 					4, &client->lease->server_address)) {
-			CLIENT_DEBUG("Failed to append server ID");
+			CLIENT_DEBUG(L_LOG_WARNING, "Failed to append server ID");
 			return -EINVAL;
 		}
 
 		if (!_dhcp_message_builder_append(&builder,
 					L_DHCP_OPTION_REQUESTED_IP_ADDRESS,
 					4, &client->lease->address)) {
-			CLIENT_DEBUG("Failed to append requested IP");
+			CLIENT_DEBUG(L_LOG_WARNING, "Failed to append requested IP");
 			return -EINVAL;
 		}
 
@@ -455,7 +458,7 @@ static int dhcp_client_send_request(struct l_dhcp_client *client)
 						L_DHCP_OPTION_HOST_NAME,
 						strlen(client->hostname),
 						client->hostname)) {
-			CLIENT_DEBUG("Failed to append host name");
+			CLIENT_DEBUG(L_LOG_WARNING, "Failed to append host name");
 			return -EINVAL;
 		}
 	}
@@ -486,7 +489,7 @@ static void dhcp_client_send_release(struct l_dhcp_client *client)
 	int err;
 	struct sockaddr_in si;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	memset(&si, 0, sizeof(si));
 	si.sin_family = AF_INET;
@@ -507,7 +510,7 @@ static void dhcp_client_send_release(struct l_dhcp_client *client)
 	if (!_dhcp_message_builder_append(&builder,
 					L_DHCP_OPTION_SERVER_IDENTIFIER,
 					4, &client->lease->server_address)) {
-		CLIENT_DEBUG("Failed to append server ID");
+		CLIENT_DEBUG(L_LOG_WARNING, "Failed to append server ID");
 		return;
 	}
 
@@ -523,13 +526,13 @@ static void dhcp_client_timeout_resend(struct l_timeout *timeout,
 	unsigned int next_timeout = 0;
 	int r;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	switch (client->state) {
 	case DHCP_STATE_SELECTING:
 		r = dhcp_client_send_discover(client);
 		if (r < 0) {
-			CLIENT_DEBUG("Sending discover failed: %s",
+			CLIENT_DEBUG(L_LOG_WARNING, "Sending discover failed: %s",
 								strerror(-r));
 			goto error;
 		}
@@ -540,7 +543,7 @@ static void dhcp_client_timeout_resend(struct l_timeout *timeout,
 	case DHCP_STATE_REBINDING:
 		r = dhcp_client_send_request(client);
 		if (r < 0) {
-			CLIENT_DEBUG("Sending Request failed: %s",
+			CLIENT_DEBUG(L_LOG_WARNING, "Sending Request failed: %s",
 								strerror(-r));
 			goto error;
 		}
@@ -593,7 +596,7 @@ static void dhcp_client_lease_expired(struct l_timeout *timeout,
 {
 	struct l_dhcp_client *client = user_data;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	l_dhcp_client_stop(client);
 	dhcp_client_event_notify(client, L_DHCP_CLIENT_EVENT_LEASE_EXPIRED);
@@ -604,7 +607,7 @@ static void dhcp_client_t2_expired(struct l_timeout *timeout, void *user_data)
 	struct l_dhcp_client *client = user_data;
 	uint32_t next_timeout = client->lease->lifetime - client->lease->t2;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	/*
 	 * If we got here, then resend_timeout is active, with a timeout
@@ -625,14 +628,14 @@ static void dhcp_client_t1_expired(struct l_timeout *timeout, void *user_data)
 	uint32_t next_timeout;
 	int r;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	CLIENT_ENTER_STATE(DHCP_STATE_RENEWING);
 	client->attempt = 1;
 
 	r = dhcp_client_send_request(client);
 	if  (r < 0) {
-		CLIENT_DEBUG("Sending request failed: %s", strerror(-r));
+		CLIENT_DEBUG(L_LOG_WARNING, "Sending request failed: %s", strerror(-r));
 		goto error;
 	}
 
@@ -665,7 +668,7 @@ static void dhcp_client_address_add_cb(int error, uint16_t type,
 	if (error < 0 && error != -EEXIST) {
 		l_rtnl_address_free(client->rtnl_configured_address);
 		client->rtnl_configured_address = NULL;
-		CLIENT_DEBUG("Unable to set address on ifindex: %u: %d(%s)",
+		CLIENT_DEBUG(L_LOG_WARNING, "Unable to set address on ifindex: %u: %d(%s)",
 				client->ifindex, error,
 				strerror(-error));
 		return;
@@ -681,7 +684,7 @@ static int dhcp_client_receive_ack(struct l_dhcp_client *client,
 	struct l_dhcp_lease *lease;
 	int r;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	if (ack->yiaddr == 0)
 		return -ENOMSG;
@@ -691,7 +694,7 @@ static int dhcp_client_receive_ack(struct l_dhcp_client *client,
 
 	lease = _dhcp_lease_parse_options(&iter);
 	if (!lease) {
-		CLIENT_DEBUG("Failed to parse DHCP options.");
+		CLIENT_DEBUG(L_LOG_WARNING, "Failed to parse DHCP options.");
 
 		return -ENOMSG;
 	}
@@ -746,7 +749,7 @@ static int dhcp_client_receive_ack(struct l_dhcp_client *client,
 		if (client->rtnl_add_cmdid)
 			client->rtnl_configured_address = a;
 		else {
-			CLIENT_DEBUG("Configuring address via RTNL failed");
+			CLIENT_DEBUG(L_LOG_WARNING, "Configuring address via RTNL failed");
 			l_rtnl_address_free(a);
 		}
 	}
@@ -761,7 +764,7 @@ static int dhcp_client_receive_offer(struct l_dhcp_client *client,
 	struct dhcp_message_iter iter;
 	struct l_dhcp_lease *lease;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	if (offer->yiaddr == 0)
 		return -ENOMSG;
@@ -785,7 +788,7 @@ static int dhcp_client_receive_offer(struct l_dhcp_client *client,
 			return -ENOMSG;
 		}
 
-		CLIENT_DEBUG("Server sent another offer, using it instead");
+		CLIENT_DEBUG(L_LOG_INFO, "Server sent another offer, using it instead");
 
 		_dhcp_lease_free(client->lease);
 	}
@@ -832,7 +835,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 	int r, e;
 	struct in_addr ia;
 
-	CLIENT_DEBUG("");
+	CLIENT_DEBUG(L_LOG_DEBUG, "");
 
 	if (len < sizeof(struct dhcp_message))
 		return;
@@ -890,7 +893,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 	case DHCP_STATE_REBINDING:
 	receive_rapid_commit:
 		if (msg_type == DHCP_MESSAGE_TYPE_NAK) {
-			CLIENT_DEBUG("Received NAK, Stopping...");
+			CLIENT_DEBUG(L_LOG_INFO, "Received NAK, Stopping...");
 			l_dhcp_client_stop(client);
 
 			dhcp_client_event_notify(client,
@@ -914,7 +917,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 			e = client->transport->bind(client->transport,
 						client->lease->address);
 			if (e < 0) {
-				CLIENT_DEBUG("Failed to bind dhcp socket. "
+				CLIENT_DEBUG(L_LOG_WARNING, "Failed to bind dhcp socket. "
 					"Error %d: %s", e, strerror(-e));
 			}
 		}
@@ -940,7 +943,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 			uint32_t next_timeout =
 					dhcp_fuzz_secs(client->lease->t1);
 
-			CLIENT_DEBUG("T1 expiring in %u ms", next_timeout);
+			CLIENT_DEBUG(L_LOG_INFO, "T1 expiring in %u ms", next_timeout);
 			client->timeout_lease =
 				l_timeout_create_ms(next_timeout,
 							dhcp_client_t1_expired,
@@ -953,7 +956,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 
 		client->acd = l_acd_new(client->ifindex);
 
-		if (client->debug_handler)
+		if (client->debug_handler && client->debug_level == L_LOG_DEBUG)
 			l_acd_set_debug(client->acd, client->debug_handler,
 					client->debug_data,
 					client->debug_destroy);
@@ -976,7 +979,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata,
 
 		/* For unit testing we don't want this to be a fatal error */
 		if (!l_acd_start(client->acd, buf)) {
-			CLIENT_DEBUG("Failed to start ACD on %s, continuing",
+			CLIENT_DEBUG(L_LOG_WARNING, "Failed to start ACD on %s, continuing",
 						buf);
 			l_acd_destroy(client->acd);
 			client->acd = NULL;
@@ -1290,7 +1293,7 @@ LIB_EXPORT bool l_dhcp_client_set_event_handler(struct l_dhcp_client *client,
 LIB_EXPORT bool l_dhcp_client_set_debug(struct l_dhcp_client *client,
 						l_dhcp_debug_cb_t function,
 						void *user_data,
-						l_dhcp_destroy_cb_t destroy)
+						l_dhcp_destroy_cb_t destroy, int priority)
 {
 	if (unlikely(!client))
 		return false;
@@ -1301,6 +1304,7 @@ LIB_EXPORT bool l_dhcp_client_set_debug(struct l_dhcp_client *client,
 	client->debug_handler = function;
 	client->debug_destroy = destroy;
 	client->debug_data = user_data;
+	client->debug_level = priority;
 
 	return true;
 }
ell/dhcp.h +1 -1
index 946bf63..b9c3480 100644
@@ -102,7 +102,7 @@ bool l_dhcp_client_set_event_handler(struct l_dhcp_client *client,
 
 bool l_dhcp_client_set_debug(struct l_dhcp_client *client,
 				l_dhcp_debug_cb_t function,
-				void *user_data, l_dhcp_destroy_cb_t destroy);
+				void *user_data, l_dhcp_destroy_cb_t destroy, int priority);
 
 
 char *l_dhcp_lease_get_address(const struct l_dhcp_lease *lease);
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/3 examples: Pass in max logging level

---
 examples/dhcp-client.c    | 2 +-
 examples/netconfig-test.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
examples/dhcp-client.c +1 -1
index 0a16ce4..9dcea57 100644
@@ -107,7 +107,7 @@ int main(int argc, char *argv[])
 	client = l_dhcp_client_new(ifindex);
 	l_dhcp_client_set_address(client, ARPHRD_ETHER, mac, 6);
 	l_dhcp_client_set_event_handler(client, event_handler, NULL, NULL);
-	l_dhcp_client_set_debug(client, do_debug, "[DHCP] ", NULL);
+	l_dhcp_client_set_debug(client, do_debug, "[DHCP] ", NULL, L_LOG_DEBUG);
 	l_dhcp_client_start(client);
 
 	l_main_run_with_signal(signal_handler, NULL);
examples/netconfig-test.c +1 -1
index c9d02ab..d75e4ad 100644
@@ -195,7 +195,7 @@ int main(int argc, char *argv[])
 	netconfig = l_netconfig_new(ifindex);
 	l_netconfig_set_event_handler(netconfig, event_handler, NULL, NULL);
 	l_dhcp_client_set_debug(l_netconfig_get_dhcp_client(netconfig),
-				do_debug, "[DHCPv4] ", NULL);
+				do_debug, "[DHCPv4] ", NULL, L_LOG_DEBUG);
 	l_netconfig_start(netconfig);
 
 	l_main_run_with_signal(signal_handler, NULL);
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH 3/3 unit: Pass in max logging level for dhcp

In reply to Michael Johnson
---
 unit/test-dhcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
unit/test-dhcp.c +1 -1
index e734e03..3c340fc 100644
@@ -811,7 +811,7 @@ static struct l_dhcp_client *client_init(const uint8_t *mac)
 				event_handler_lease_obtained, NULL, NULL));
 
 	if (verbose)
-		l_dhcp_client_set_debug(client, do_debug, "[DHCP1] ", NULL);
+		l_dhcp_client_set_debug(client, do_debug, "[DHCP1] ", NULL, L_LOG_DEBUG);
 
 	transport->send = fake_transport_send;
 	transport->l2_send = fake_transport_l2_send;
Signature · 1 line
2.25.1
#3 lore raw
In reply to Michael Johnson

Hi Michael,

Quoted 9 lines · Michael Johnson

Most dhcp clients have some logging to show what state the lease is in so this just makes iwd more in line with them. The logging won't be enabled by default but requires a max logging level to be passed in when logging is set up.

---
  ell/dhcp.c | 64 +++++++++++++++++++++++++++++-------------------------
  ell/dhcp.h |  2 +-
  2 files changed, 35 insertions(+), 31 deletions(-)

<snip>

Looks good to me. I have a few nits below, which I can probably fix myself if you feel lazy :)

Do you want to send a patch for iwd as well?

ell/dhcp.c line 51

Quoted 8 lines
ell/dhcp.c +4 -3
-#define CLIENT_DEBUG(fmt, args...)					\
-	l_util_debug(client->debug_handler, client->debug_data,		\
-			"%s:%i " fmt, __func__, __LINE__, ## args)
+#define CLIENT_DEBUG(priority, fmt, args...)					\
+	if (priority <= client->debug_level)		\
+		l_util_debug(client->debug_handler, client->debug_data,		\
+				"%s:%i " fmt, __func__, __LINE__, ## args)

Looks like there's one tab too many before all the '\'.

Quoted 3 lines
  #define CLIENT_ENTER_STATE(s)						\
  	l_util_debug(client->debug_handler, client->debug_data,		\
  			"%s:%i Entering state: " #s,			\

<snip>

ell/dhcp.c static int dhcp_client_send_request(struct l_dhcp_client *client) · line 431

Quoted 6 lines
ell/dhcp.c +1 -1
@@ -425,14 +428,14 @@ static int dhcp_client_send_request(struct l_dhcp_client *client)
  		if (!_dhcp_message_builder_append(&builder,
  					L_DHCP_OPTION_SERVER_IDENTIFIER,
  					4, &client->lease->server_address)) {
-			CLIENT_DEBUG("Failed to append server ID");
+			CLIENT_DEBUG(L_LOG_WARNING, "Failed to append server ID");

Can we split this to multiple lines to avoid lines that are >80 chars? Something like

	CLIENT_DEBUG(L_LOG_WARNING,\n
			"<Message to print>",
			arg1, .. argn);

See doc/coding-style.txt item M4.

For strings that don't fit on a single line, it is fine to go over 80 chars.

Alternatively we could add CLIENT_WARN, CLIENT_INFO, etc

ell/dhcp.c static int dhcp_client_send_request(struct l_dhcp_client *client) · line 434

Quoted 3 lines
ell/dhcp.c +0 -0
  			return -EINVAL;
  		}
 

Regards,
-Denis