-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmd_announce_send_sm.c
233 lines (213 loc) · 6.69 KB
/
md_announce_send_sm.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Excelfore gptp - Implementation of gPTP(IEEE 802.1AS)
* Copyright (C) 2019 Excelfore Corporation (https://excelfore.com)
*
* This file is part of Excelfore-gptp.
*
* Excelfore-gptp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Excelfore-gptp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Excelfore-gptp. If not, see
* <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
*/
#include "mind.h"
#include "mdeth.h"
#include "gptpnet.h"
#include "gptpclock.h"
#include "md_announce_send_sm.h"
#include "md_abnormal_hooks.h"
typedef enum {
INIT,
INITIALIZE,
SEND_ANNOUNCE,
REACTION,
}md_announce_send_state_t;
struct md_announce_send_data{
gptpnet_data_t *gpnetd;
PerTimeAwareSystemGlobal *ptasg;
PerPortGlobal *ppg;
BmcsPerPortGlobal *bppg;
md_announce_send_state_t state;
md_announce_send_state_t last_state;
MDAnnounceSendSM *thisSM;
int domainIndex;
int portIndex;
md_announce_send_stat_data_t statd;
};
#define PORT_OPER sm->ppg->forAllDomain->portOper
#define PTP_PORT_ENABLED sm->ppg->ptpPortEnabled
#define AS_CAPABLE sm->ppg->asCapable
#define TXANN sm->thisSM->txAnnouncePtr
#define RCVDTXANN sm->thisSM->rcvdTxAnnounce
static int sendAnnounce(md_announce_send_data_t *sm)
{
MDPTPMsgAnnounce *sdata;
int N;
int ssize=sizeof(MDPTPMsgAnnounce);
// truncate uncessary container */
N = TXANN->tlvLength / sizeof(ClockIdentity);
ssize=ssize-((MAX_PATH_TRACE_N-N)*sizeof(ClockIdentity));
sdata=md_header_compose(sm->gpnetd, sm->portIndex, ANNOUNCE, ssize,
sm->ptasg->thisClock,
sm->ppg->thisPort,
TXANN->header.sequenceId,
sm->bppg->currentLogAnnounceInterval);
if(!sdata) return -1;
sdata->head.domainNumber=sm->ptasg->domainNumber;
sdata->head.flags[0] = TXANN->header.flags[0];
sdata->head.flags[1] = TXANN->header.flags[1];
sdata->currentUtcOffset_ns = htons(TXANN->currentUtcOffset);
sdata->grandmasterPriority1 = TXANN->grandmasterPriority1;
sdata->grandmasterClockQuality.clockClass =
TXANN->grandmasterClockQuality.clockClass;
sdata->grandmasterClockQuality.clockAccuracy =
TXANN->grandmasterClockQuality.clockAccuracy;
sdata->grandmasterClockQuality.offsetScaledLogVariance_ns =
htons(TXANN->grandmasterClockQuality.offsetScaledLogVariance);
sdata->grandmasterPriority2 = TXANN->grandmasterPriority2;
memcpy(&sdata->grandmasterIdentity, &TXANN->grandmasterIdentity,
sizeof(ClockIdentity));
sdata->stepsRemoved_ns = htons(TXANN->stepsRemoved);
sdata->timeSource = TXANN->timeSource;
sdata->tlvType_ns = htons(TXANN->tlvType);
sdata->tlvLength_ns = htons(TXANN->tlvLength);
if (TXANN->tlvLength > 0){
memcpy(&sdata->pathSequence, &TXANN->pathSequence, sizeof(ClockIdentity) * N);
} else {
/* 802.1AS and AVNU differs in the behavior when there is no PathTrace
* TLV appended in the Announce message.
* 802.1AS requires the PathTrace field to exist, with length=0 to
* indicate empty PathTrace.
* But AVNU, requires the PathTrace field to be absent in cases where
* there is no PathTrace TLV.
*
* Check configuration if it is necessary to conform with AVNU.
*/
if(sm->ptasg->conformToAvnu){
// truncate PathTrace field
ssize-=sizeof(sdata->tlvType_ns);
ssize-=sizeof(sdata->tlvLength_ns);
}
}
if(gptpnet_send_whook(sm->gpnetd, sm->portIndex-1, ssize)==-1) return -2;
sm->statd.announce_send++;
return 0;
}
static md_announce_send_state_t allstate_condition(md_announce_send_data_t *sm)
{
if(sm->ptasg->BEGIN || !sm->ptasg->instanceEnable ||
!PORT_OPER || !PTP_PORT_ENABLED || !AS_CAPABLE) {
sm->last_state=REACTION;
return INITIALIZE;
}
return sm->state;
}
static void *initialize_proc(md_announce_send_data_t *sm)
{
UB_LOG(UBL_DEBUGV, "md_announce_send:%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
return NULL;
}
static md_announce_send_state_t initialize_condition(md_announce_send_data_t *sm)
{
if(RCVDTXANN) return SEND_ANNOUNCE;
return INITIALIZE;
}
static int send_announce_proc(md_announce_send_data_t *sm)
{
UB_LOG(UBL_DEBUGV, "md_announce_send:%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
RCVDTXANN=false;
return sendAnnounce(sm);
}
static md_announce_send_state_t send_announce_condition(md_announce_send_data_t *sm)
{
if(RCVDTXANN) sm->last_state=REACTION;
return SEND_ANNOUNCE;
}
void *md_announce_send_sm(md_announce_send_data_t *sm, uint64_t cts64)
{
bool state_change;
void *retp=NULL;
if(!sm) return NULL;
sm->state = allstate_condition(sm);
while(true){
state_change=(sm->last_state != sm->state);
sm->last_state = sm->state;
switch(sm->state){
case INIT:
sm->state = INITIALIZE;
break;
case INITIALIZE:
if(state_change)
retp=initialize_proc(sm);
sm->state = initialize_condition(sm);
break;
case SEND_ANNOUNCE:
if(state_change){
if(send_announce_proc(sm)==-2) {
sm->last_state=REACTION;
return NULL;
}
break;
}
sm->state = send_announce_condition(sm);
break;
case REACTION:
break;
}
if(retp) return retp;
if(sm->last_state == sm->state) break;
}
return retp;
}
void md_announce_send_sm_init(md_announce_send_data_t **sm,
int domainIndex, int portIndex,
gptpnet_data_t *gpnetd,
PerTimeAwareSystemGlobal *ptasg,
PerPortGlobal *ppg,
BmcsPerPortGlobal *bppg)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d, portIndex=%d\n",
__func__, domainIndex, portIndex);
INIT_SM_DATA(md_announce_send_data_t, MDAnnounceSendSM, sm);
(*sm)->gpnetd = gpnetd;
(*sm)->ptasg = ptasg;
(*sm)->ppg = ppg;
(*sm)->bppg = bppg;
(*sm)->domainIndex = domainIndex;
(*sm)->portIndex = portIndex;
}
int md_announce_send_sm_close(md_announce_send_data_t **sm)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d, portIndex=%d\n",
__func__, (*sm)->domainIndex, (*sm)->portIndex);
CLOSE_SM_DATA(sm);
return 0;
}
void *md_announce_send_sm_mdAnnouncSend(md_announce_send_data_t *sm,
PTPMsgAnnounce *msgAnnounce, uint64_t cts64)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
RCVDTXANN=true;
TXANN=msgAnnounce;
sm->last_state=REACTION;
return md_announce_send_sm(sm, cts64);
}
void md_announce_send_stat_reset(md_announce_send_data_t *sm)
{
memset(&sm->statd, 0, sizeof(md_announce_send_stat_data_t));
}
md_announce_send_stat_data_t *md_announce_send_get_stat(md_announce_send_data_t *sm)
{
return &sm->statd;
}