1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Internet Protocol (IP)                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "tx_thread.h"
29 #include "nx_ip.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_ip_raw_packet_cleanup                           PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function processes raw packet receive timeout and thread       */
45 /*    terminate actions that require the IP data structures to be         */
46 /*    cleaned up to remove the suspended thread.                          */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    thread_ptr                            Pointer to suspended thread's */
51 /*                                            control block               */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _tx_thread_system_resume              Resume thread service         */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _nx_ip_delete                         Delete IP instance            */
64 /*    _nx_ip_raw_packet_disable             Disable raw packet processing */
65 /*    _tx_thread_timeout                    Thread timeout processing     */
66 /*    _tx_thread_terminate                  Thread terminate processing   */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_nx_ip_raw_packet_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)77 VOID  _nx_ip_raw_packet_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
78 {
79 
80 TX_INTERRUPT_SAVE_AREA
81 
82 NX_IP *ip_ptr;    /* Working IP pointer  */
83 
84     NX_CLEANUP_EXTENSION
85 
86     /* Setup pointer to the IP control block.  */
87     ip_ptr =  (NX_IP *)thread_ptr -> tx_thread_suspend_control_block;
88 
89     /* Disable interrupts to remove the suspended thread from the block pool.  */
90     TX_DISABLE
91 
92     /* Determine if the cleanup is still required.  */
93     if ((thread_ptr -> tx_thread_suspend_cleanup) && (ip_ptr) &&
94         (ip_ptr -> nx_ip_id == NX_IP_ID))
95     {
96 
97         /* Yes, we still have thread suspension!  */
98 
99         /* Clear the suspension cleanup flag.  */
100         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
101 
102         /* Remove the suspended thread from the list.  */
103 
104         /* See if this is the only suspended thread on the list.  */
105         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
106         {
107 
108             /* Yes, the only suspended thread.  */
109 
110             /* Update the head pointer.  */
111             ip_ptr -> nx_ip_raw_packet_suspension_list =  TX_NULL;
112         }
113         else
114         {
115 
116             /* At least one more thread is on the same suspension list.  */
117 
118             /* Update the list head pointer.  */
119             ip_ptr -> nx_ip_raw_packet_suspension_list =  thread_ptr -> tx_thread_suspended_next;
120 
121             /* Update the links of the adjacent threads.  */
122             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
123                 thread_ptr -> tx_thread_suspended_previous;
124             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
125                 thread_ptr -> tx_thread_suspended_next;
126         }
127 
128         /* Decrement the suspension count.  */
129         ip_ptr -> nx_ip_raw_packet_suspended_count--;
130 
131         /* Now we need to determine if this cleanup is from a terminate, timeout,
132            or from a wait abort.  */
133         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
134         {
135 
136             /* Thread still suspended on the packet pool.  Setup return error status and
137                resume the thread.  */
138 
139             /* Setup return status.  */
140             thread_ptr -> tx_thread_suspend_status =  NX_NO_PACKET;
141 
142             /* Temporarily disable preemption.  */
143             _tx_thread_preempt_disable++;
144 
145             /* Restore interrupts.  */
146             TX_RESTORE
147 
148             /* Resume the thread!  Check for preemption even though we are executing
149                from the system timer thread right now which normally executes at the
150                highest priority.  */
151             _tx_thread_system_resume(thread_ptr);
152 
153             /* Finished, just return.  */
154             return;
155         }
156     }
157 
158     /* Restore interrupts.  */
159     TX_RESTORE
160 }
161 
162