lldp_rx_fsm.c
Go to the documentation of this file.
1 /**
2  * @file lldp_rx_fsm.c
3  * @brief LLDP receive state machine
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL LLDP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "lldp/lldp.h"
37 #include "lldp/lldp_fsm.h"
38 #include "lldp/lldp_rx_fsm.h"
39 #include "lldp/lldp_procedures.h"
40 #include "lldp/lldp_debug.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (LLDP_SUPPORT == ENABLED && LLDP_RX_MODE_SUPPORT == ENABLED)
45 
46 //LLDP receive states
48 {
49  {LLDP_STATE_LLDP_WAIT_PORT_OPERATIONAL, "LLDP_WAIT_PORT_OPERATIONAL"},
50  {LLDP_STATE_DELETE_AGED_INFO, "DELETE_AGED_INFO"},
51  {LLDP_STATE_RX_LLDP_INITIALIZE, "RX_LLDP_INITIALIZE"},
52  {LLDP_STATE_RX_WAIT_FOR_FRAME, "RX_WAIT_FOR_FRAME"},
53  {LLDP_STATE_RX_FRAME, "RX_FRAME"},
54  {LLDP_STATE_DELETE_INFO, "DELETE_INFO"},
55  {LLDP_STATE_UPDATE_INFO, "UPDATE_INFO"}
56 };
57 
58 
59 /**
60  * @brief LLDP receive state machine initialization
61  * @param[in] port Pointer to the port context
62  **/
63 
65 {
66  //Enter initial state
68 }
69 
70 
71 /**
72  * @brief LLDP receive state machine implementation
73  * @param[in] port Pointer to the port context
74  **/
75 
77 {
78  LldpAgentContext *context;
79 
80  //Point to the LLDP agent context
81  context = port->context;
82 
83  //A global transition can occur from any of the possible states
84  if(!port->rxInfoAge && !port->portEnabled)
85  {
86  //When the condition associated with a global transition is met, it
87  //supersedes all other exit conditions
89  }
90  else
91  {
92  //All exit conditions for the state are evaluated continuously until one
93  //of the conditions is met (refer to IEEE Std 802.1AB-2005, section 10.4)
94  switch(port->rxState)
95  {
96  //LLDP_WAIT_PORT_OPERATIONAL state?
98  //Evaluate conditions for the current state
99  if(port->rxInfoAge)
100  {
101  //Switch to the DELETE_AGED_INFO state
103  }
104  else if(port->portEnabled)
105  {
106  //Switch to the RX_LLDP_INITIALIZE state
108  }
109  else
110  {
111  //Just for sanity
112  }
113 
114  break;
115 
116  //DELETE_AGED_INFO state?
118  //Unconditional transition (UCT) to LLDP_WAIT_PORT_OPERATIONAL state
120  break;
121 
122  //RX_LLDP_INITIALIZE state?
124  //LLDP receive initialization shall be halted until the value of
125  //adminStatus is either enabledTxRx or enabledRxOnly
126  if(port->adminStatus == LLDP_ADMIN_STATUS_ENABLED_TX_RX ||
127  port->adminStatus == LLDP_ADMIN_STATUS_ENABLED_RX_ONLY)
128  {
129  //Switch to the RX_WAIT_FOR_FRAME state
131  }
132  break;
133 
134  //RX_WAIT_FOR_FRAME state?
136  //Evaluate conditions for the current state
137  if(port->rxInfoAge)
138  {
139  //Switch to the DELETE_INFO state
141  }
142  else if(port->rcvFrame)
143  {
144  //Switch to the RX_FRAME state
146  }
147  else if(port->adminStatus == LLDP_ADMIN_STATUS_DISABLED ||
148  port->adminStatus == LLDP_ADMIN_STATUS_ENABLED_TX_ONLY)
149  {
150  //Switch to the RX_LLDP_INITIALIZE state
152  }
153  else
154  {
155  //Just for sanity
156  }
157 
158  break;
159 
160  //RX_FRAME state?
161  case LLDP_STATE_RX_FRAME:
162  //Evaluate conditions for the current state
163  if(context->badFrame)
164  {
165  //Switch to the RX_WAIT_FOR_FRAME state
167  }
168  else if(context->rxTTL == 0)
169  {
170  //Switch to the DELETE_INFO state
172  }
173  else if(context->rxChanges)
174  {
175  //Switch to the UPDATE_INFO state
177  }
178  else
179  {
180  //Switch to the RX_WAIT_FOR_FRAME state
182  }
183 
184  break;
185 
186  //DELETE_INFO state?
188  //Unconditional transition (UCT) to RX_WAIT_FOR_FRAME state
190  break;
191 
192  //UPDATE_INFO state?
194  //Unconditional transition (UCT) to RX_WAIT_FOR_FRAME state
196  break;
197 
198  //Invalid state?
199  default:
200  //Just for sanity
201  lldpFsmError(port->context);
202  break;
203  }
204  }
205 }
206 
207 
208 /**
209  * @brief Update LLDP receive state
210  * @param[in] port Pointer to the port context
211  * @param[in] newState New state to switch to
212  **/
213 
215 {
216  LldpAgentContext *context;
217 
218  //Point to the LLDP agent context
219  context = port->context;
220 
221  //Any state change?
222  if(port->rxState != newState)
223  {
224  //Dump the state transition
225  TRACE_DEBUG("Port %" PRIu8 ": RX state machine %s -> %s\r\n",
226  port->portIndex,
229  }
230 
231  //Switch to the new state
232  port->rxState = newState;
233 
234  //On entry to a state, the procedures defined for the state are executed
235  //exactly once (refer to IEEE Std 802.1AB-2005, section 10.4)
236  switch(port->rxState)
237  {
238  //LLDP_WAIT_PORT_OPERATIONAL state?
240  //No action
241  break;
242 
243  //DELETE_AGED_INFO state?
245  //To avoid a race condition, the flag variable somethingChangedRemote is
246  //not set to TRUE until after the information in the LLDP remote systems
247  //MIB has been updated
248  context->somethingChangedRemote = FALSE;
250  port->rxInfoAge = FALSE;
251  context->somethingChangedRemote = TRUE;
252  break;
253 
254  //RX_LLDP_INITIALIZE state?
256  //Initialize the LLDP receive module
258  port->rcvFrame = FALSE;
259  break;
260 
261  //RX_WAIT_FOR_FRAME state?
263  //Reset flags
264  context->badFrame = FALSE;
265  port->rxInfoAge = FALSE;
266  context->somethingChangedRemote = FALSE;
267  break;
268 
269  //RX_FRAME state?
270  case LLDP_STATE_RX_FRAME:
271  //Reset flags
272  context->rxChanges = FALSE;
273  port->rcvFrame = FALSE;
274  //Process incoming frame
276  break;
277 
278  //DELETE_INFO state?
280  //Delete all information in the LLDP remote systems MIB associated with
281  //the MSAP identifier if an LLDPDU is received with an rxTTL value of
282  //zero or the timing counter rxInfoTTL expires
284  context->somethingChangedRemote = TRUE;
285  break;
286 
287  //UPDATE_INFO state?
289  //Update the MIB objects corresponding to the TLVs contained in the
290  //received LLDPDU
292  context->somethingChangedRemote = TRUE;
293  break;
294 
295  //Invalid state?
296  default:
297  //Just for sanity
298  break;
299  }
300 
301  //Check whether the port is enabled
302  if(port->portEnabled)
303  {
304  //The LLDP state machine is busy
305  port->context->busy = TRUE;
306  }
307 }
308 
309 #endif
void lldpInitRxFsm(LldpPortEntry *port)
LLDP receive state machine initialization.
Definition: lldp_rx_fsm.c:64
#define TRUE
Definition: os_port.h:50
@ LLDP_STATE_RX_LLDP_INITIALIZE
Definition: lldp_rx_fsm.h:52
LLDP state machine.
void lldpMibDeleteObjects(LldpPortEntry *port)
Delete aged entries from the remote systems MIB (10.5.5.2.1)
@ LLDP_STATE_UPDATE_INFO
Definition: lldp_rx_fsm.h:56
void lldpFsmError(LldpAgentContext *context)
LLDP state machine error handler.
Definition: lldp_fsm.c:128
@ LLDP_STATE_LLDP_WAIT_PORT_OPERATIONAL
Definition: lldp_rx_fsm.h:50
Data logging functions for debugging purpose (LLDP)
@ LLDP_ADMIN_STATUS_DISABLED
The local LLDP agent can neither transmit or receive LLDP frames.
Definition: lldp.h:188
#define FALSE
Definition: os_port.h:46
LLDP receive state machine.
LldpRxState
LLDP receive states.
Definition: lldp_rx_fsm.h:49
LLDP state machine procedures.
#define LldpPortEntry
Definition: lldp.h:44
@ LLDP_ADMIN_STATUS_ENABLED_RX_ONLY
The local LLDP agent can only receive LLDP frames.
Definition: lldp.h:190
const char_t * lldpGetParamName(uint_t value, const LldpParamName *paramList, size_t paramListLen)
Convert a parameter to string representation.
Definition: lldp_debug.c:245
@ LLDP_STATE_RX_FRAME
Definition: lldp_rx_fsm.h:54
@ LLDP_STATE_DELETE_AGED_INFO
Definition: lldp_rx_fsm.h:51
uint16_t port
Definition: dns_common.h:267
@ LLDP_ADMIN_STATUS_ENABLED_TX_RX
Definition: lldp.h:191
#define TRACE_DEBUG(...)
Definition: debug.h:107
void lldpRxFsm(LldpPortEntry *port)
LLDP receive state machine implementation.
Definition: lldp_rx_fsm.c:76
@ LLDP_STATE_RX_WAIT_FOR_FRAME
Definition: lldp_rx_fsm.h:53
void lldpRxInitializeLLDP(LldpPortEntry *port)
Initialize the LLDP receive module (10.5.5.2.3)
#define LldpAgentContext
Definition: lldp.h:40
void lldpRxProcessFrame(LldpPortEntry *port)
Process incoming LLDP frame (10.5.5.2.4)
void lldpChangeRxState(LldpPortEntry *port, LldpRxState newState)
Update LLDP receive state.
Definition: lldp_rx_fsm.c:214
void lldpMibUpdateObjects(LldpPortEntry *port)
Update MIB objects with TLVs contained in the received LLDPDU (10.5.5.2.2)
LLDP (Link Layer Discovery Protocol)
TCP/IP stack core.
Parameter value/name binding.
Definition: lldp_debug.h:49
const LldpParamName lldpRxStates[]
Definition: lldp_rx_fsm.c:47
Debugging facilities.
@ LLDP_STATE_DELETE_INFO
Definition: lldp_rx_fsm.h:55
#define arraysize(a)
Definition: os_port.h:71
@ LLDP_ADMIN_STATUS_ENABLED_TX_ONLY
The local LLDP agent can only transmit LLDP frames.
Definition: lldp.h:189