1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Transmission Control Protocol (TCP)                                 */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "tx_thread.h"
30 #include "tx_timer.h"
31 #include "nx_ip.h"
32 #include "nx_tcp.h"
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nx_tcp_client_bind_cleanup                         PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function processes TCP bind timeout and thread terminate       */
48 /*    actions that require the TCP socket data structures to be cleaned   */
49 /*    up.                                                                 */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    thread_ptr                            Pointer to suspended thread's */
54 /*                                            control block               */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    None                                                                */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    tx_event_flags_set                    Set event flag                */
63 /*    _tx_thread_system_resume              Resume thread service         */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    _nx_tcp_deferred_cleanup_check        Deferred cleanup processing   */
68 /*    _nx_tcp_client_socket_unbind          Socket unbind processing      */
69 /*    _tx_thread_timeout                    Thread timeout processing     */
70 /*    _tx_thread_terminate                  Thread terminate processing   */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
77 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*                                                                        */
80 /**************************************************************************/
_nx_tcp_client_bind_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)81 VOID  _nx_tcp_client_bind_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
82 {
83 
84 TX_INTERRUPT_SAVE_AREA
85 
86 NX_IP         *ip_ptr;
87 NX_TCP_SOCKET *socket_ptr;        /* Working socket pointer  */
88 NX_TCP_SOCKET *owning_socket_ptr; /* Socket owning the port  */
89 
90     NX_CLEANUP_EXTENSION
91 
92     /* Disable interrupts to remove the suspended thread from the TCP socket.  */
93     TX_DISABLE
94 
95     /* Setup pointer to TCP socket control block.  */
96     socket_ptr =  (NX_TCP_SOCKET *)thread_ptr -> tx_thread_suspend_control_block;
97 
98     /* Determine if the socket pointer is valid.  */
99     if ((!socket_ptr) || (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID))
100     {
101 
102         /* Restore interrupts.  */
103         TX_RESTORE
104 
105         return;
106     }
107 
108     /* Determine if the cleanup is still required.  */
109     if (!(thread_ptr -> tx_thread_suspend_cleanup))
110     {
111 
112         /* Restore interrupts.  */
113         TX_RESTORE
114 
115         return;
116     }
117 
118     /* Determine if the caller is an ISR or the system timer thread.  */
119 #ifndef TX_TIMER_PROCESS_IN_ISR
120     if ((TX_THREAD_GET_SYSTEM_STATE()) || (_tx_thread_current_ptr == &_tx_timer_thread))
121 #else
122     if (TX_THREAD_GET_SYSTEM_STATE())
123 #endif
124     {
125 
126         /* Yes, defer the processing to the NetX IP thread.  */
127 
128         /* Yes, change the suspend cleanup routine to indicate the cleanup is deferred.  */
129         thread_ptr -> tx_thread_suspend_cleanup =  _nx_tcp_cleanup_deferred;
130 
131         /* Pickup the IP pointer.  */
132         ip_ptr =  socket_ptr -> nx_tcp_socket_ip_ptr;
133 
134         /* Restore interrupts.  */
135         TX_RESTORE
136 
137         /* Set the deferred cleanup flag for the IP thread.  */
138         tx_event_flags_set(&(ip_ptr -> nx_ip_events), NX_IP_TCP_CLEANUP_DEFERRED, TX_OR);
139 
140         /* Return to caller.  */
141         return;
142     }
143     else
144     {
145 
146         /* Yes, we still have thread suspension!  */
147 
148         /* Clear the socket bind in progress flag.  */
149         socket_ptr -> nx_tcp_socket_bind_in_progress =  NX_NULL;
150 
151         /* Clear the suspension cleanup flag.  */
152         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
153 
154         /* Pickup the socket owning the port. This pointer was
155            saved in the bind processing prior to suspension.  */
156         owning_socket_ptr =  socket_ptr -> nx_tcp_socket_bound_previous;
157 
158         /* Remove the suspended thread from the list.  */
159 
160         /* See if this is the only suspended thread on the list.  */
161         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
162         {
163 
164             /* Yes, the only suspended thread.  */
165 
166             /* Update the head pointer.  */
167             owning_socket_ptr -> nx_tcp_socket_bind_suspension_list =  NX_NULL;
168         }
169         else
170         {
171 
172             /* At least one more thread is on the same suspension list.  */
173 
174             /* Update the list head pointer.  */
175             owning_socket_ptr -> nx_tcp_socket_bind_suspension_list =  thread_ptr -> tx_thread_suspended_next;
176 
177             /* Update the links of the adjacent threads.  */
178             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
179                 thread_ptr -> tx_thread_suspended_previous;
180             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
181                 thread_ptr -> tx_thread_suspended_next;
182         }
183 
184         /* Decrement the suspension count.  */
185         owning_socket_ptr -> nx_tcp_socket_bind_suspended_count--;
186 
187         /* Now we need to determine if this cleanup is from a terminate, timeout,
188            or from a wait abort.  */
189         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
190         {
191 
192             /* Thread still suspended on the TCP socket.  Setup return error status and
193                resume the thread.  */
194 
195             /* Setup return status.  */
196             thread_ptr -> tx_thread_suspend_status =  NX_PORT_UNAVAILABLE;
197 
198             /* Temporarily disable preemption.  */
199             _tx_thread_preempt_disable++;
200 
201             /* Restore interrupts.  */
202             TX_RESTORE
203 
204             /* Resume the thread!  Check for preemption even though we are executing
205                from the system timer thread right now which normally executes at the
206                highest priority.  */
207             _tx_thread_system_resume(thread_ptr);
208 
209             /* Finished, just return.  */
210             return;
211         }
212     }
213 
214     /* Restore interrupts.  */
215     TX_RESTORE
216 }
217 
218