summaryrefslogtreecommitdiff
path: root/common/src/nx_ip_raw_packet_receive.c
blob: f7bcca1e14e504610a23d89d7f4ee54f4a78b28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   Internet Protocol (IP)                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "tx_thread.h"
#include "nx_ip.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_ip_raw_packet_receive                           PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function retrieves the first packet from the received raw      */
/*    packet list and returns it to the caller.  If no packet is present  */
/*    the routine may optionally suspend waiting on the list for a        */
/*    packet.                                                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ip_ptr                                IP control block pointer      */
/*    packet_ptr                            Pointer to return allocated   */
/*                                            packet                      */
/*    wait_option                           Suspension option             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _tx_thread_system_suspend             Suspend thread                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_ip_raw_packet_receive(NX_IP *ip_ptr, NX_PACKET **packet_ptr, ULONG wait_option)
{
TX_INTERRUPT_SAVE_AREA

UINT                   status;      /* Return status           */
TX_THREAD             *thread_ptr;  /* Working thread pointer  */
NX_PACKET             *work_ptr;    /* Working packet pointer  */

#ifdef TX_ENABLE_EVENT_TRACE
TX_TRACE_BUFFER_ENTRY *trace_event;
ULONG                  trace_timestamp;
#endif


    /* If trace is enabled, insert this event into the trace buffer.  */
    NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_RAW_PACKET_RECEIVE, ip_ptr, 0, wait_option, 0, NX_TRACE_IP_EVENTS, &trace_event, &trace_timestamp);

    /* Disable interrupts to get a packet from the pool.  */
    TX_DISABLE

    /* Determine if there is an available packet.  */
    if (ip_ptr -> nx_ip_raw_received_packet_count)
    {

        /* Yes, a packet is available.  Decrement the raw packet receive count.  */
        ip_ptr -> nx_ip_raw_received_packet_count--;

        /* Pickup the first raw packet pointer.  */
        work_ptr =  ip_ptr -> nx_ip_raw_received_packet_head;

        /* Modify the available list to point at the next packet in the pool. */
        ip_ptr -> nx_ip_raw_received_packet_head =  work_ptr -> nx_packet_queue_next;

        /* Determine if the tail pointer points to the same packet.  */
        if (ip_ptr -> nx_ip_raw_received_packet_tail == work_ptr)
        {

            /* Yes, just set tail pointer to NULL since we must be at the end of the queue.  */
            ip_ptr -> nx_ip_raw_received_packet_tail =  NX_NULL;
        }

        /* Place the raw packet pointer in the return destination.  */
        *packet_ptr =  work_ptr;

        /* Update the trace event with the status.  */
        NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_IP_RAW_PACKET_RECEIVE, 0, *packet_ptr, 0, 0);

        /* Set status to success.  */
        status =  NX_SUCCESS;
    }
    else
    {

        /* Determine if the request specifies suspension.  */
        if (wait_option)
        {

            /* Prepare for suspension of this thread.  */

            /* Pickup thread pointer.  */
            thread_ptr =  _tx_thread_current_ptr;

            /* Setup cleanup routine pointer.  */
            thread_ptr -> tx_thread_suspend_cleanup =  _nx_ip_raw_packet_cleanup;

            /* Setup cleanup information, i.e. this IP control
               block.  */
            thread_ptr -> tx_thread_suspend_control_block =  (void *)ip_ptr;

            /* Save the return packet pointer address as well.  */
            thread_ptr -> tx_thread_additional_suspend_info =  (void *)packet_ptr;

            /* Setup suspension list.  */
            if (ip_ptr -> nx_ip_raw_packet_suspension_list)
            {

                /* This list is not NULL, add current thread to the end. */
                thread_ptr -> tx_thread_suspended_next =
                    ip_ptr -> nx_ip_raw_packet_suspension_list;
                thread_ptr -> tx_thread_suspended_previous =
                    (ip_ptr -> nx_ip_raw_packet_suspension_list) -> tx_thread_suspended_previous;
                ((ip_ptr -> nx_ip_raw_packet_suspension_list) -> tx_thread_suspended_previous) -> tx_thread_suspended_next = thread_ptr;
                (ip_ptr -> nx_ip_raw_packet_suspension_list) -> tx_thread_suspended_previous =   thread_ptr;
            }
            else
            {

                /* No other threads are suspended.  Setup the head pointer and
                   just setup this threads pointers to itself.  */
                ip_ptr -> nx_ip_raw_packet_suspension_list =    thread_ptr;
                thread_ptr -> tx_thread_suspended_next =        thread_ptr;
                thread_ptr -> tx_thread_suspended_previous =    thread_ptr;
            }

            /* Increment the suspended thread count.  */
            ip_ptr -> nx_ip_raw_packet_suspended_count++;

            /* Set the state to suspended.  */
            thread_ptr -> tx_thread_state =  TX_TCP_IP;

            /* Set the suspending flag.  */
            thread_ptr -> tx_thread_suspending =  TX_TRUE;

            /* Temporarily disable preemption.  */
            _tx_thread_preempt_disable++;

            /* Save the timeout value.  */
            thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks =  wait_option;

            /* Restore interrupts.  */
            TX_RESTORE

            /* Call actual thread suspension routine.  */
            _tx_thread_system_suspend(thread_ptr);

            /* Update the trace event with the status.  */
            NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_IP_RAW_PACKET_RECEIVE, 0, *packet_ptr, 0, 0);

            /* Return the completion status.  */
            return(thread_ptr -> tx_thread_suspend_status);
        }
        else
        {

            /* Immediate return, return error completion.  */
            status =  NX_NO_PACKET;
        }
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return completion status.  */
    return(status);
}