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 /**   Internet Control Message Protocol (ICMP)                            */
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 "nx_icmp.h"
31 #include "nx_ip.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _nx_icmp_cleanup                                    PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Yuxin Zhou, Microsoft Corporation                                   */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function processes ICMP ping timeout and thread terminate      */
47 /*    actions that require the IP/ICMP data structures to be cleaned      */
48 /*    up.                                                                 */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    thread_ptr                            Pointer to suspended thread's */
53 /*                                            control block               */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _tx_thread_system_resume              Resume thread service         */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    _nx_ip_delete                         Delete IP instance            */
66 /*    _tx_thread_timeout                    Thread timeout processing     */
67 /*    _tx_thread_terminate                  Thread terminate processing   */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_nx_icmp_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)78 VOID  _nx_icmp_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
79 {
80 
81 TX_INTERRUPT_SAVE_AREA
82 
83 NX_IP *ip_ptr;
84 
85     NX_CLEANUP_EXTENSION
86 
87     /* Setup pointer to IP control block.  */
88     ip_ptr =  (NX_IP *)thread_ptr -> tx_thread_suspend_control_block;
89 
90     /* Disable interrupts to remove the suspended thread from the packet pool.  */
91     TX_DISABLE
92 
93     /* Determine if the cleanup is still required.  */
94     if ((thread_ptr -> tx_thread_suspend_cleanup) && (ip_ptr) &&
95         (ip_ptr -> nx_ip_id == NX_IP_ID))
96     {
97 
98         /* Yes, we still have thread suspension!  */
99 
100         /* Clear the suspension cleanup flag.  */
101         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
102 
103         /* Remove the suspended thread from the list.  */
104 
105         /* See if this is the only suspended thread on the list.  */
106         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
107         {
108 
109             /* Yes, the only suspended thread.  */
110 
111             /* Update the head pointer.  */
112             ip_ptr -> nx_ip_icmp_ping_suspension_list =  TX_NULL;
113         }
114         else
115         {
116 
117             /* At least one more thread is on the same suspension list.  */
118 
119             /* Update the list head pointer.  */
120             if (ip_ptr -> nx_ip_icmp_ping_suspension_list == thread_ptr)
121             {
122                 ip_ptr -> nx_ip_icmp_ping_suspension_list =  thread_ptr -> tx_thread_suspended_next;
123             }
124 
125             /* Update the links of the adjacent threads.  */
126             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
127                 thread_ptr -> tx_thread_suspended_previous;
128             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
129                 thread_ptr -> tx_thread_suspended_next;
130         }
131 
132         /* Decrement the suspension count.  */
133         ip_ptr -> nx_ip_icmp_ping_suspended_count--;
134 
135         /* Now we need to determine if this cleanup is from a terminate, timeout,
136            or from a wait abort.  */
137         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
138         {
139 
140             /* Thread still suspended on the IP ping message.  Setup return error status and
141                resume the thread.  */
142 
143             /* Setup return status.  */
144             thread_ptr -> tx_thread_suspend_status =  NX_NO_RESPONSE;
145 
146 #ifndef NX_DISABLE_ICMP_INFO
147             /* Increment the ICMP timeout count.  */
148             ip_ptr -> nx_ip_ping_timeouts++;
149 #endif
150 
151             /* Temporarily disable preemption.  */
152             _tx_thread_preempt_disable++;
153 
154             /* Restore interrupts.  */
155             TX_RESTORE
156 
157             /* Resume the thread!  Check for preemption even though we are executing
158                from the system timer thread right now which normally executes at the
159                highest priority.  */
160             _tx_thread_system_resume(thread_ptr);
161 
162             /* Finished, just return.  */
163             return;
164         }
165     }
166 
167     /* Restore interrupts.  */
168     TX_RESTORE
169 }
170 
171