dhcp: Add priority to logging
Michael Johnson
PATCH v2 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 | 48 +++++++++++++++++++++++++++++------------------- ell/dhcp.h | 2 +- 2 files changed, 30 insertions(+), 20 deletions(-)
ell/dhcp.c +29 -19
@@ -43,10 +43,18 @@#include "netlink.h" #include "rtnl.h" #include "acd.h" +#include "log.h" +#define CLIENT_LOG(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_DEBUG(fmt, args...) \ - l_util_debug(client->debug_handler, client->debug_data, \ - "%s:%i " fmt, __func__, __LINE__, ## args) + CLIENT_LOG(L_LOG_DEBUG, fmt, ## args) +#define CLIENT_INFO(fmt, args...) \ + CLIENT_LOG(L_LOG_INFO, fmt, ## args) +#define CLIENT_WARN(fmt, args...) \ + CLIENT_LOG(L_LOG_WARNING, fmt, ## args)#define CLIENT_ENTER_STATE (s) \ l_util_debug(client->debug_handler, client->debug_data, \ "%s:%i Entering state: " #s, \
@@ -192,6 +200,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;
@@ -425,14 +434,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_WARN ("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_WARN ("Failed to append requested IP" );return -EINVAL ; }
@@ -455,7 +464,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_WARN ("Failed to append host name" );return -EINVAL ; } }
@@ -507,7 +516,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_WARN ("Failed to append server ID" );return ; }
@@ -529,7 +538,7 @@ static void dhcp_client_timeout_resend(struct l_timeout *timeout,case DHCP_STATE_SELECTING : r =dhcp_client_send_discover (client);if (r <0 ) { -CLIENT_DEBUG ("Sending discover failed: %s" , +CLIENT_WARN ("Sending discover failed: %s" ,strerror (-r)); goto error; }
@@ -540,7 +549,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_WARN ("Sending Request failed: %s" ,strerror (-r)); goto error; }
@@ -632,7 +641,7 @@ static void dhcp_client_t1_expired(struct l_timeout *timeout, void *user_data) r =dhcp_client_send_request (client);if (r <0 ) { -CLIENT_DEBUG ("Sending request failed: %s" ,strerror (-r)); +CLIENT_WARN ("Sending request failed: %s" ,strerror (-r)); goto error; }
@@ -665,7 +674,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_WARN ("Unable to set address on ifindex: %u: %d(%s)" , client->ifindex, error,strerror (-error));return ;
@@ -691,7 +700,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_WARN ("Failed to parse DHCP options." );return -ENOMSG ; }
@@ -746,7 +755,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_WARN ("Configuring address via RTNL failed" );l_rtnl_address_free (a); } }
@@ -785,7 +794,7 @@ static int dhcp_client_receive_offer(struct l_dhcp_client *client,return -ENOMSG ; } -CLIENT_DEBUG ("Server sent another offer, using it instead" ); +CLIENT_INFO ("Server sent another offer, using it instead" );_dhcp_lease_free (client->lease); }
@@ -890,7 +899,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_INFO ("Received NAK, Stopping..." );l_dhcp_client_stop (client); dhcp_client_event_notify(client,
@@ -914,7 +923,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_WARN ("Failed to bind dhcp socket. " "Error %d: %s" , e,strerror (-e)); } }
@@ -940,7 +949,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_INFO ("T1 expiring in %u ms" , next_timeout); client->timeout_lease = l_timeout_create_ms(next_timeout, dhcp_client_t1_expired,
@@ -953,7 +962,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 +985,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_WARN ("Failed to start ACD on %s, continuing" , buf);l_acd_destroy (client->acd); client->acd =NULL ;
@@ -1290,7 +1299,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 +1310,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
@@ -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
Michael Johnson
PATCH v2 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
@@ -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
@@ -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
Michael Johnson
PATCH v2 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
@@ -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
Denis Kenzior
Hi Michael,
Quoted 8 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 | 48 +++++++++++++++++++++++++++++------------------- ell/dhcp.h | 2 +- 2 files changed, 30 insertions(+), 20 deletions(-)
All applied, thanks! I did amend patch 1 & 3 slightly to break up lines that were > 80 chars.
Regards,
-Denis
Michael Johnson
Hi Denis,
Sorry about that, I thought I caught them all.
Michael
Quoted 18 lines · Denis Kenzior
Hi Michael,
Quoted 8 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 | 48 +++++++++++++++++++++++++++++------------------- ell/dhcp.h | 2 +- 2 files changed, 30 insertions(+), 20 deletions(-)All applied, thanks! I did amend patch 1 & 3 slightly to break up lines that were > 80 chars.
Regards,
-Denis