rstp_ptx.c
Go to the documentation of this file.
1 /**
2  * @file rstp_ptx.c
3  * @brief Port Transmit state machine (PTX)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSTP 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.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL RSTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "rstp/rstp.h"
36 #include "rstp/rstp_fsm.h"
37 #include "rstp/rstp_ptx.h"
38 #include "rstp/rstp_procedures.h"
39 #include "rstp/rstp_conditions.h"
40 #include "rstp/rstp_misc.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (RSTP_SUPPORT == ENABLED)
45 
46 //PTX state machine's states
48 {
49  {RSTP_PTX_STATE_TRANSMIT_INIT, "TRANSMIT_INIT"},
50  {RSTP_PTX_STATE_TRANSMIT_PERIODIC, "TRANSMIT_PERIODIC"},
51  {RSTP_PTX_STATE_TRANSMIT_CONFIG, "TRANSMIT_CONFIG"},
52  {RSTP_PTX_STATE_TRANSMIT_TCN, "TRANSMIT_TCN"},
53  {RSTP_PTX_STATE_TRANSMIT_RSTP, "TRANSMIT_RSTP"},
54  {RSTP_PTX_STATE_IDLE, "IDLE"}
55 };
56 
57 
58 /**
59  * @brief PTX state machine initialization
60  * @param[in] port Pointer to the bridge port context
61  **/
62 
64 {
65  //Enter initial state
67 }
68 
69 
70 /**
71  * @brief PTX state machine implementation
72  * @param[in] port Pointer to the bridge port context
73  **/
74 
76 {
77  RstpBridgeContext *context;
78 
79  //Point to the RSTP bridge context
80  context = port->context;
81 
82  //A global transition can occur from any of the possible states
83  if(!port->portEnabled)
84  {
85  //When the condition associated with a global transition is met, it
86  //supersedes all other exit conditions
88  }
89  else
90  {
91  //All conditions for the current state are evaluated continuously until one
92  //of the conditions is met (refer to IEEE Std 802.1D-2004, section 17.16)
93  switch(port->ptxState)
94  {
95  //TRANSMIT_INIT, TRANSMIT_PERIODIC, TRANSMIT_CONFIG, TRANSMIT_TCN or
96  //TRANSMIT_RSTP state?
102  //Unconditional transition (UCT) to IDLE state
104  break;
105 
106  //IDLE state?
107  case RSTP_PTX_STATE_IDLE:
108  //All transitions, except UCT, are qualified by the following condition
109  if(port->selected && !port->updtInfo)
110  {
111  //The state machine transmits BPDUs at regular intervals and when
112  //newInfo is set
113  if(port->helloWhen == 0)
114  {
115  //Switch to the TRANSMIT_PERIODIC state
117  }
118  else
119  {
120  //The newInfo flag is set if a BPDU is to be transmitted
121  if(port->newInfo)
122  {
123  //The TxHoldCount is used to limit transmission rate
124  if(port->txCount < rstpTxHoldCount(context))
125  {
126  //The sendRSTP determines the type of the BPDU to send
127  if(port->sendRstp)
128  {
129  //Transmit an RST BPDU
131  }
132  else
133  {
134  //Check port role
135  if(port->role == STP_PORT_ROLE_ROOT)
136  {
137  //Transmit a TCN BPDU
139  }
140  else if(port->role == STP_PORT_ROLE_DESIGNATED)
141  {
142  //Transmit a Configuration BPDU
144  }
145  else
146  {
147  //Just for sanity
148  }
149  }
150  }
151  }
152  }
153  }
154 
155  break;
156 
157  //Invalid state?
158  default:
159  //Just for sanity
160  rstpFsmError(port->context);
161  break;
162  }
163  }
164 }
165 
166 
167 /**
168  * @brief Update PTX state machine state
169  * @param[in] port Pointer to the bridge port context
170  * @param[in] newState New state to switch to
171  **/
172 
174 {
175  //Any state change?
176  if(port->ptxState != newState)
177  {
178  //Dump the state transition
179  TRACE_VERBOSE("Port %" PRIu8 ": PTX state machine %s -> %s\r\n",
180  port->portIndex,
183  }
184 
185  //Switch to the new state
186  port->ptxState = newState;
187 
188  //On entry to a state, the procedures defined for the state are executed
189  //exactly once (refer to IEEE Std 802.1D-2004, section 17.16)
190  switch(port->ptxState)
191  {
192  //TRANSMIT_INIT state?
194  //Initialize variables
195  port->newInfo = TRUE;
196  port->txCount = 0;
197  break;
198 
199  //TRANSMIT_PERIODIC state?
201  //Check port role
202  if(port->role == STP_PORT_ROLE_DESIGNATED)
203  {
204  //Transmit a Configuration BPDU
205  port->newInfo = TRUE;
206  }
207  else if(port->role == STP_PORT_ROLE_ROOT)
208  {
209  //Check whether the Topology Change timer is running
210  if(port->tcWhile != 0)
211  {
212  //Transmit a TCN BPDU
213  port->newInfo = TRUE;
214  }
215  }
216  else
217  {
218  //Just for sanity
219  }
220 
221  break;
222 
223  //TRANSMIT_CONFIG state?
225  //Send a Configuration BPDU
226  port->newInfo = FALSE;
228  port->txCount++;
229  port->tcAck = FALSE;
230  break;
231 
232  //TRANSMIT_TCN state?
234  //Send a TCN BPDU
235  port->newInfo = FALSE;
236  rstpTxTcn(port);
237  port->txCount++;
238  break;
239 
240  //TRANSMIT_RSTP state?
242  //Send an RSTP BPDU
243  port->newInfo = FALSE;
244  rstpTxRstp(port);
245  port->txCount++;
246  port->tcAck = FALSE;
247  break;
248 
249  //IDLE state?
250  case RSTP_PTX_STATE_IDLE:
251  //Reload the Hello timer
252  port->helloWhen = rstpHelloTime(port);
253  break;
254 
255  //Invalid state?
256  default:
257  //Just for sanity
258  break;
259  }
260 
261  //Check whether the port is enabled
262  if(port->portEnabled)
263  {
264  //The RSTP state machine is busy
265  port->context->busy = TRUE;
266  }
267 }
268 
269 #endif
Debugging facilities.
#define TRACE_VERBOSE(...)
Definition: debug.h:124
uint16_t port
Definition: dns_common.h:267
#define arraysize(a)
Definition: os_port.h:71
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
RSTP (Rapid Spanning Tree Protocol)
#define RstpBridgeContext
Definition: rstp.h:36
#define RstpBridgePort
Definition: rstp.h:40
uint_t rstpHelloTime(RstpBridgePort *port)
HelloTime variable evaluation (17.20.7)
uint_t rstpTxHoldCount(RstpBridgeContext *context)
TxHoldCount variable evaluation (17.20.13)
RSTP state machine conditions.
void rstpFsmError(RstpBridgeContext *context)
RSTP state machine error handler.
Definition: rstp_fsm.c:226
Rapid Spanning Tree state machines.
const char_t * rstpGetParamName(uint_t value, const RstpParamName *paramList, size_t paramListLen)
Convert a parameter to string representation.
Definition: rstp_misc.c:883
RSTP helper functions.
void rstpTxTcn(RstpBridgePort *port)
Transmit a Topology Change Notification BPDU (17.21.21)
void rstpTxConfig(RstpBridgePort *port)
Transmit a Configuration BPDU (17.21.19)
void rstpTxRstp(RstpBridgePort *port)
Transmit a Rapid Spanning Tree BPDU (17.21.20)
RSTP state machine procedures.
void rstpPtxChangeState(RstpBridgePort *port, RstpPtxState newState)
Update PTX state machine state.
Definition: rstp_ptx.c:173
const RstpParamName rstpPtxStates[]
Definition: rstp_ptx.c:47
void rstpPtxFsm(RstpBridgePort *port)
PTX state machine implementation.
Definition: rstp_ptx.c:75
void rstpPtxInit(RstpBridgePort *port)
PTX state machine initialization.
Definition: rstp_ptx.c:63
Port Transmit state machine (PTX)
RstpPtxState
Port Transmit machine states.
Definition: rstp_ptx.h:48
@ RSTP_PTX_STATE_TRANSMIT_PERIODIC
Definition: rstp_ptx.h:50
@ RSTP_PTX_STATE_TRANSMIT_INIT
Definition: rstp_ptx.h:49
@ RSTP_PTX_STATE_TRANSMIT_CONFIG
Definition: rstp_ptx.h:51
@ RSTP_PTX_STATE_TRANSMIT_RSTP
Definition: rstp_ptx.h:53
@ RSTP_PTX_STATE_TRANSMIT_TCN
Definition: rstp_ptx.h:52
@ RSTP_PTX_STATE_IDLE
Definition: rstp_ptx.h:54
@ STP_PORT_ROLE_ROOT
Definition: stp_common.h:125
@ STP_PORT_ROLE_DESIGNATED
Definition: stp_common.h:126
Parameter value/name binding.
Definition: rstp_misc.h:48