net: tcp: Add more debugging to TCP code

Useful when investigating issues.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
diff --git a/subsys/net/ip/tcp.c b/subsys/net/ip/tcp.c
index 0c366a9..78b949b 100644
--- a/subsys/net/ip/tcp.c
+++ b/subsys/net/ip/tcp.c
@@ -95,9 +95,9 @@
 		rel_ack = ack ? ack - tcp->sent_ack : 0;
 	}
 
-	NET_DBG("pkt %p src %u dst %u seq 0x%04x (%u) ack 0x%04x (%u/%u) "
+	NET_DBG("[%p] pkt %p src %u dst %u seq 0x%04x (%u) ack 0x%04x (%u/%u) "
 		"flags %c%c%c%c%c%c win %u chk 0x%04x",
-		pkt,
+		tcp, pkt,
 		ntohs(tcp_hdr->src_port),
 		ntohs(tcp_hdr->dst_port),
 		sys_get_be32(tcp_hdr->seq),
@@ -140,19 +140,22 @@
  * Note that this is macro so that we get information who called the
  * net_pkt_ref() if memory debugging is active.
  */
-#define do_ref_if_needed(pkt)				\
-	do {						\
-		if (!is_6lo_technology(pkt)) {		\
-			pkt = net_pkt_ref(pkt);		\
-		}					\
+#define do_ref_if_needed(tcp, pkt)					\
+	do {								\
+		if (!is_6lo_technology(pkt)) {				\
+			NET_DBG("[%p] ref pkt %p new ref %d (%s:%d)",	\
+				tcp, pkt, pkt->ref + 1, __func__,	\
+				__LINE__);				\
+			pkt = net_pkt_ref(pkt);				\
+		}							\
 	} while (0)
 
 static void abort_connection(struct net_tcp *tcp)
 {
 	struct net_context *ctx = tcp->context;
 
-	NET_DBG("Segment retransmission exceeds %d, resetting context %p",
-		CONFIG_NET_TCP_RETRY_COUNT, ctx);
+	NET_DBG("[%p] segment retransmission exceeds %d, resetting context %p",
+		tcp, CONFIG_NET_TCP_RETRY_COUNT, ctx);
 
 	if (ctx->recv_cb) {
 		ctx->recv_cb(ctx, NULL, -ECONNRESET, tcp->recv_user_data);
@@ -182,10 +185,13 @@
 		pkt = CONTAINER_OF(sys_slist_peek_head(&tcp->sent_list),
 				   struct net_pkt, sent_list);
 
-		do_ref_if_needed(pkt);
+		do_ref_if_needed(tcp, pkt);
+
 		if (net_tcp_send_pkt(pkt) < 0 && !is_6lo_technology(pkt)) {
+			NET_DBG("[%p] pkt %p send failed", tcp, pkt);
 			net_pkt_unref(pkt);
 		} else {
+			NET_DBG("[%p] sent pkt %p", tcp, pkt);
 			if (IS_ENABLED(CONFIG_NET_STATISTICS_TCP) &&
 			    !is_6lo_technology(pkt)) {
 				net_stats_update_tcp_seg_rexmit();
@@ -193,6 +199,8 @@
 		}
 	} else if (IS_ENABLED(CONFIG_NET_TCP_TIME_WAIT)) {
 		if (tcp->fin_sent && tcp->fin_rcvd) {
+			NET_DBG("[%p] Closing connection (context %p)",
+				tcp, tcp->context);
 			net_context_unref(tcp->context);
 		}
 	}
@@ -274,7 +282,7 @@
 	tcp->flags &= ~(NET_TCP_IN_USE | NET_TCP_RECV_MSS_SET);
 	irq_unlock(key);
 
-	NET_DBG("Disposed of TCP connection state");
+	NET_DBG("[%p] Disposed of TCP connection state", tcp);
 
 	return 0;
 }
@@ -364,7 +372,7 @@
 	} else
 #endif
 	{
-		NET_DBG("Protocol family %d not supported",
+		NET_DBG("[%p] Protocol family %d not supported", tcp,
 			net_pkt_family(pkt));
 		net_pkt_unref(pkt);
 		return NULL;
@@ -681,6 +689,8 @@
 	size_t data_len = net_pkt_get_len(pkt);
 	int ret;
 
+	NET_DBG("[%p] Queue %p len %zd", context->tcp, pkt, data_len);
+
 	/* Set PSH on all packets, our window is so small that there's
 	 * no point in the remote side trying to finesse things and
 	 * coalesce packets.
@@ -703,7 +713,7 @@
 			      retry_timeout(context->tcp), 0);
 	}
 
-	do_ref_if_needed(pkt);
+	do_ref_if_needed(context->tcp, pkt);
 
 	return 0;
 }
@@ -790,7 +800,7 @@
 				return -ENOMEM;
 			}
 
-			NET_DBG("Copied %zu bytes from %p to %p",
+			NET_DBG("[%p] Copied %zu bytes from %p to %p", ctx->tcp,
 				net_pkt_get_len(new_pkt), pkt, new_pkt);
 
 			/* This function is called from net_context.c and if we
@@ -840,6 +850,7 @@
 	 */
 	SYS_SLIST_FOR_EACH_CONTAINER(&context->tcp->sent_list, pkt, sent_list) {
 		if (!net_pkt_sent(pkt)) {
+			NET_DBG("[%p] Sending pkt %p", context->tcp, pkt);
 			if (net_tcp_send_pkt(pkt) < 0 &&
 			    !is_6lo_technology(pkt)) {
 				net_pkt_unref(pkt);
@@ -914,7 +925,7 @@
 			SYS_SLIST_FOR_EACH_CONTAINER(&ctx->tcp->sent_list, pkt,
 						     sent_list) {
 				if (net_pkt_sent(pkt)) {
-					do_ref_if_needed(pkt);
+					do_ref_if_needed(ctx->tcp, pkt);
 					net_pkt_set_sent(pkt, false);
 				}
 			}
@@ -982,7 +993,7 @@
 	NET_ASSERT(new_state >= NET_TCP_CLOSED &&
 		   new_state <= NET_TCP_CLOSING);
 
-	NET_DBG("state@%p %s (%d) => %s (%d)",
+	NET_DBG("[%p] state %s (%d) => %s (%d)",
 		tcp, net_tcp_state_str(tcp->state), tcp->state,
 		net_tcp_state_str(new_state), new_state);