@@ -393,23 +393,13 @@ static void tcp_send_queue_flush(struct tcp *conn)
393
393
}
394
394
}
395
395
396
- #if CONFIG_NET_TCP_LOG_LEVEL >= LOG_LEVEL_DBG
397
- #define tcp_conn_unref (conn , status ) \
398
- tcp_conn_unref_debug(conn, status, __func__, __LINE__)
399
396
400
- static int tcp_conn_unref_debug (struct tcp * conn , int status ,
401
- const char * caller , int line )
402
- #else
403
- static int tcp_conn_unref (struct tcp * conn , int status )
404
- #endif
397
+ static int tcp_conn_unref (struct tcp * conn )
405
398
{
406
399
int ref_count = atomic_get (& conn -> ref_count );
407
400
struct net_pkt * pkt ;
408
401
409
- #if CONFIG_NET_TCP_LOG_LEVEL >= LOG_LEVEL_DBG
410
- NET_DBG ("conn: %p, ref_count=%d (%s():%d)" , conn , ref_count ,
411
- caller , line );
412
- #endif
402
+ NET_DBG ("conn: %p, ref_count=%d" , conn , ref_count );
413
403
414
404
#if !defined(CONFIG_NET_TEST_PROTOCOL )
415
405
if (conn -> in_connect ) {
@@ -444,11 +434,6 @@ static int tcp_conn_unref(struct tcp *conn, int status)
444
434
conn -> context -> conn_handler = NULL ;
445
435
}
446
436
447
- if (conn -> context -> recv_cb ) {
448
- conn -> context -> recv_cb (conn -> context , NULL , NULL , NULL ,
449
- status , conn -> recv_user_data );
450
- }
451
-
452
437
conn -> context -> tcp = NULL ;
453
438
454
439
net_context_unref (conn -> context );
@@ -485,12 +470,34 @@ int net_tcp_unref(struct net_context *context)
485
470
NET_DBG ("context: %p, conn: %p" , context , context -> tcp );
486
471
487
472
if (context -> tcp ) {
488
- ref_count = tcp_conn_unref (context -> tcp , 0 );
473
+ ref_count = tcp_conn_unref (context -> tcp );
489
474
}
490
475
491
476
return ref_count ;
492
477
}
493
478
479
+ #if CONFIG_NET_TCP_LOG_LEVEL >= LOG_LEVEL_DBG
480
+ #define tcp_conn_close (conn , status ) \
481
+ tcp_conn_close_debug(conn, status, __func__, __LINE__)
482
+
483
+ static int tcp_conn_close_debug (struct tcp * conn , int status ,
484
+ const char * caller , int line )
485
+ #else
486
+ static int tcp_conn_close (struct tcp * conn , int status )
487
+ #endif
488
+ {
489
+ #if CONFIG_NET_TCP_LOG_LEVEL >= LOG_LEVEL_DBG
490
+ NET_DBG ("conn: %p closed by TCP stack (%s():%d)" , conn , caller , line );
491
+ #endif
492
+
493
+ if (conn -> context -> recv_cb ) {
494
+ conn -> context -> recv_cb (conn -> context , NULL , NULL , NULL ,
495
+ status , conn -> recv_user_data );
496
+ }
497
+
498
+ return tcp_conn_unref (conn );
499
+ }
500
+
494
501
static bool tcp_send_process_no_lock (struct tcp * conn )
495
502
{
496
503
bool unref = false;
@@ -569,7 +576,7 @@ static void tcp_send_process(struct k_work *work)
569
576
k_mutex_unlock (& conn -> lock );
570
577
571
578
if (unref ) {
572
- tcp_conn_unref (conn , - ETIMEDOUT );
579
+ tcp_conn_close (conn , - ETIMEDOUT );
573
580
}
574
581
}
575
582
@@ -1074,7 +1081,7 @@ static int tcp_out_ext(struct tcp *conn, uint8_t flags, struct net_pkt *data,
1074
1081
k_work_schedule_for_queue (& tcp_work_q ,
1075
1082
& conn -> send_timer , K_NO_WAIT );
1076
1083
} else if (tcp_send_process_no_lock (conn )) {
1077
- tcp_conn_unref (conn , - ETIMEDOUT );
1084
+ tcp_conn_close (conn , - ETIMEDOUT );
1078
1085
}
1079
1086
out :
1080
1087
return ret ;
@@ -1334,7 +1341,7 @@ static void tcp_resend_data(struct k_work *work)
1334
1341
k_mutex_unlock (& conn -> lock );
1335
1342
1336
1343
if (conn_unref ) {
1337
- tcp_conn_unref (conn , - ETIMEDOUT );
1344
+ tcp_conn_close (conn , - ETIMEDOUT );
1338
1345
}
1339
1346
}
1340
1347
@@ -1345,16 +1352,15 @@ static void tcp_timewait_timeout(struct k_work *work)
1345
1352
1346
1353
NET_DBG ("conn: %p %s" , conn , tcp_conn_state (conn , NULL ));
1347
1354
1348
- /* Extra unref from net_tcp_put() */
1349
- net_context_unref (conn -> context );
1355
+ (void )tcp_conn_close (conn , - ETIMEDOUT );
1350
1356
}
1351
1357
1352
1358
static void tcp_establish_timeout (struct tcp * conn )
1353
1359
{
1354
1360
NET_DBG ("Did not receive %s in %dms" , "ACK" , ACK_TIMEOUT_MS );
1355
1361
NET_DBG ("conn: %p %s" , conn , tcp_conn_state (conn , NULL ));
1356
1362
1357
- (void )tcp_conn_unref (conn , - ETIMEDOUT );
1363
+ (void )tcp_conn_close (conn , - ETIMEDOUT );
1358
1364
}
1359
1365
1360
1366
static void tcp_fin_timeout (struct k_work * work )
@@ -1370,8 +1376,7 @@ static void tcp_fin_timeout(struct k_work *work)
1370
1376
NET_DBG ("Did not receive %s in %dms" , "FIN" , tcp_fin_timeout_ms );
1371
1377
NET_DBG ("conn: %p %s" , conn , tcp_conn_state (conn , NULL ));
1372
1378
1373
- /* Extra unref from net_tcp_put() */
1374
- net_context_unref (conn -> context );
1379
+ (void )tcp_conn_close (conn , - ETIMEDOUT );
1375
1380
}
1376
1381
1377
1382
static void tcp_send_zwp (struct k_work * work )
@@ -1731,13 +1736,13 @@ static struct tcp *tcp_conn_new(struct net_pkt *pkt)
1731
1736
net_context_set_family (conn -> context , net_pkt_family (pkt ));
1732
1737
1733
1738
if (tcp_endpoint_set (& conn -> dst , pkt , TCP_EP_SRC ) < 0 ) {
1734
- net_context_unref (context );
1739
+ net_context_put (context );
1735
1740
conn = NULL ;
1736
1741
goto err ;
1737
1742
}
1738
1743
1739
1744
if (tcp_endpoint_set (& conn -> src , pkt , TCP_EP_DST ) < 0 ) {
1740
- net_context_unref (context );
1745
+ net_context_put (context );
1741
1746
conn = NULL ;
1742
1747
goto err ;
1743
1748
}
@@ -1772,7 +1777,7 @@ static struct tcp *tcp_conn_new(struct net_pkt *pkt)
1772
1777
ret = net_context_bind (context , & local_addr , sizeof (local_addr ));
1773
1778
if (ret < 0 ) {
1774
1779
NET_DBG ("Cannot bind accepted context, connection reset" );
1775
- net_context_unref (context );
1780
+ net_context_put (context );
1776
1781
conn = NULL ;
1777
1782
goto err ;
1778
1783
}
@@ -1796,7 +1801,7 @@ static struct tcp *tcp_conn_new(struct net_pkt *pkt)
1796
1801
& context -> conn_handler );
1797
1802
if (ret < 0 ) {
1798
1803
NET_ERR ("net_conn_register(): %d" , ret );
1799
- net_context_unref (context );
1804
+ net_context_put (context );
1800
1805
conn = NULL ;
1801
1806
goto err ;
1802
1807
}
@@ -2188,6 +2193,7 @@ static enum net_verdict tcp_in(struct tcp *conn, struct net_pkt *pkt)
2188
2193
k_work_cancel_delayable (& conn -> establish_timer );
2189
2194
tcp_send_timer_cancel (conn );
2190
2195
next = TCP_ESTABLISHED ;
2196
+ tcp_conn_ref (conn );
2191
2197
net_context_set_state (conn -> context ,
2192
2198
NET_CONTEXT_CONNECTED );
2193
2199
@@ -2228,6 +2234,7 @@ static enum net_verdict tcp_in(struct tcp *conn, struct net_pkt *pkt)
2228
2234
}
2229
2235
2230
2236
next = TCP_ESTABLISHED ;
2237
+ tcp_conn_ref (conn );
2231
2238
net_context_set_state (conn -> context ,
2232
2239
NET_CONTEXT_CONNECTED );
2233
2240
tcp_out (conn , ACK );
@@ -2518,7 +2525,7 @@ static enum net_verdict tcp_in(struct tcp *conn, struct net_pkt *pkt)
2518
2525
* to a deadlock.
2519
2526
*/
2520
2527
if (do_close ) {
2521
- tcp_conn_unref (conn , close_status );
2528
+ tcp_conn_close (conn , close_status );
2522
2529
}
2523
2530
2524
2531
return verdict ;
@@ -2569,16 +2576,11 @@ int net_tcp_put(struct net_context *context)
2569
2576
2570
2577
conn_state (conn , TCP_FIN_WAIT_1 );
2571
2578
}
2572
-
2573
- /* Make sure we do not delete the connection yet until we have
2574
- * sent the final ACK.
2575
- */
2576
- net_context_ref (context );
2577
2579
}
2578
2580
2579
2581
k_mutex_unlock (& conn -> lock );
2580
2582
2581
- net_context_unref (context );
2583
+ net_tcp_unref (context );
2582
2584
2583
2585
return 0 ;
2584
2586
}
@@ -2677,7 +2679,7 @@ int net_tcp_queue_data(struct net_context *context, struct net_pkt *pkt)
2677
2679
2678
2680
ret = tcp_send_queued_data (conn );
2679
2681
if (ret < 0 && ret != - ENOBUFS ) {
2680
- tcp_conn_unref (conn , ret );
2682
+ tcp_conn_close (conn , ret );
2681
2683
goto out ;
2682
2684
}
2683
2685
@@ -2861,7 +2863,7 @@ int net_tcp_connect(struct net_context *context,
2861
2863
if (k_sem_take (& conn -> connect_sem , timeout ) != 0 &&
2862
2864
conn -> state != TCP_ESTABLISHED ) {
2863
2865
conn -> in_connect = false;
2864
- tcp_conn_unref (conn , - ETIMEDOUT );
2866
+ tcp_conn_close (conn , - ETIMEDOUT );
2865
2867
ret = - ETIMEDOUT ;
2866
2868
goto out ;
2867
2869
}
@@ -3169,7 +3171,7 @@ enum net_verdict tp_input(struct net_conn *net_conn,
3169
3171
3170
3172
conn = (void * )sys_slist_peek_head (& tcp_conns );
3171
3173
context = conn -> context ;
3172
- while (tcp_conn_unref (conn , 0 ))
3174
+ while (tcp_conn_close (conn , 0 ))
3173
3175
;
3174
3176
tcp_free (context );
3175
3177
}
0 commit comments