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 Protocol (IP)                                              */
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_ip.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nx_ip_raw_packet_cleanup                           PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function processes raw packet receive timeout and thread       */
46 /*    terminate actions that require the IP data structures to be         */
47 /*    cleaned up to remove the suspended thread.                          */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    thread_ptr                            Pointer to suspended thread's */
52 /*                                            control block               */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _tx_thread_system_resume              Resume thread service         */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _nx_ip_delete                         Delete IP instance            */
65 /*    _nx_ip_raw_packet_disable             Disable raw packet processing */
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_ip_raw_packet_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)78 VOID  _nx_ip_raw_packet_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
79 {
80 
81 TX_INTERRUPT_SAVE_AREA
82 
83 NX_IP *ip_ptr;    /* Working IP pointer  */
84 
85     NX_CLEANUP_EXTENSION
86 
87     /* Setup pointer to the 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 block 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_raw_packet_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             ip_ptr -> nx_ip_raw_packet_suspension_list =  thread_ptr -> tx_thread_suspended_next;
121 
122             /* Update the links of the adjacent threads.  */
123             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
124                 thread_ptr -> tx_thread_suspended_previous;
125             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
126                 thread_ptr -> tx_thread_suspended_next;
127         }
128 
129         /* Decrement the suspension count.  */
130         ip_ptr -> nx_ip_raw_packet_suspended_count--;
131 
132         /* Now we need to determine if this cleanup is from a terminate, timeout,
133            or from a wait abort.  */
134         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
135         {
136 
137             /* Thread still suspended on the packet pool.  Setup return error status and
138                resume the thread.  */
139 
140             /* Setup return status.  */
141             thread_ptr -> tx_thread_suspend_status =  NX_NO_PACKET;
142 
143             /* Temporarily disable preemption.  */
144             _tx_thread_preempt_disable++;
145 
146             /* Restore interrupts.  */
147             TX_RESTORE
148 
149             /* Resume the thread!  Check for preemption even though we are executing
150                from the system timer thread right now which normally executes at the
151                highest priority.  */
152             _tx_thread_system_resume(thread_ptr);
153 
154             /* Finished, just return.  */
155             return;
156         }
157     }
158 
159     /* Restore interrupts.  */
160     TX_RESTORE
161 }
162 
163