forked from xl4-shiro/excelfore-gptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md_pdelay_resp_sm.c
381 lines (351 loc) · 12 KB
/
md_pdelay_resp_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* 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_pdelay_resp_sm.h"
#include "md_abnormal_hooks.h"
typedef enum {
INIT,
NOT_ENABLED,
INITIAL_WAITING_FOR_PDELAY_REQ,
WAITING_FOR_PDELAY_REQ,
SENT_PDELAY_RESP_WAITING_FOR_TIMESTAMP,
REACTION,
}md_pdelay_resp_state_t;
struct md_pdelay_resp_data{
gptpnet_data_t *gpnetd;
PerTimeAwareSystemGlobal *ptasg;
PerPortGlobal *ppg;
md_pdelay_resp_state_t state;
md_pdelay_resp_state_t last_state;
MDPdelayRespSM *thisSM;
int portIndex;
MDPTPMsgPdelayReq rcvdPdelayReq;
MDPTPMsgPdelayResp txPdelayResp;
MDPTPMsgPdelayRespFollowUp txPdelayRespFollowUp;
int64_t ts2;
int64_t ts3;
uint64_t txPdelayResp_time;
int cmlds_mode;
int last_seqid;
md_pdelay_resp_stat_data_t statd;
uint64_t mock_txts64;
};
#define PORT_OPER sm->ppg->forAllDomain->portOper
static MDPTPMsgPdelayRespFollowUp *setPdelayRespFollowUp(md_pdelay_resp_data_t *sm)
{
MDPTPMsgPdelayRespFollowUp *sdata;
int ssize=sizeof(MDPTPMsgPdelayRespFollowUp);
struct timespec ts;
sdata=md_header_compose(sm->gpnetd, sm->portIndex, PDELAY_RESP_FOLLOW_UP, ssize,
sm->ptasg->thisClock, sm->ppg->thisPort,
ntohs(sm->thisSM->rcvdPdelayReqPtr->head.sequenceId_ns),
0x7f);
if(!sdata) return NULL;
if(sm->ppg->forAllDomain->receivedNonCMLDSPdelayReq==-1 && sm->cmlds_mode){
// PdelayReq came with CMLDS and we are also in CMLDS
sdata->head.majorSdoId_messageType =
(sdata->head.majorSdoId_messageType & 0x0F) | 0x20;
}
// As the fractional ns portion can't be measure, leave the 'correctionField' as 0
UB_NSEC2TS(sm->ts3, ts);
sdata->requestOriginTimestamp.seconds_lsb_nl=htonl(ts.tv_sec);
sdata->requestOriginTimestamp.nanoseconds_nl=htonl(ts.tv_nsec);
memcpy(&sdata->requestingPortIdentity,
&sm->thisSM->rcvdPdelayReqPtr->head.sourcePortIdentity,
sizeof(MDPortIdentity));
memcpy(&sm->txPdelayRespFollowUp, sdata, sizeof(MDPTPMsgPdelayRespFollowUp));
return &sm->txPdelayRespFollowUp;
}
static int txPdelayRespFollowUp(gptpnet_data_t *gpnetd, int portIndex)
{
int ssize=sizeof(MDPTPMsgPdelayRespFollowUp);
UB_LOG(UBL_DEBUGV, "%s:portIndex=%d\n",__func__, portIndex);
return gptpnet_send_whook(gpnetd, portIndex-1, ssize);
}
static MDPTPMsgPdelayResp *setPdelayResp(md_pdelay_resp_data_t *sm)
{
MDPTPMsgPdelayResp *sdata;
int ssize=sizeof(MDPTPMsgPdelayResp);
struct timespec ts;
sdata=md_header_compose(sm->gpnetd, sm->portIndex, PDELAY_RESP, ssize,
sm->ptasg->thisClock, sm->ppg->thisPort,
ntohs(sm->thisSM->rcvdPdelayReqPtr->head.sequenceId_ns),
0x7f);
if(!sdata) return NULL;
if(sm->ppg->forAllDomain->receivedNonCMLDSPdelayReq==-1 && sm->cmlds_mode){
// PdelayReq came with CMLDS and we are also in CMLDS
sdata->head.majorSdoId_messageType =
(sdata->head.majorSdoId_messageType & 0x0F) | 0x20;
}
// As the fractional ns portion can't be measure, leave the 'correctionField' as 0
UB_NSEC2TS(sm->ts2, ts);
sdata->requestReceiptTimestamp.seconds_lsb_nl=htonl(ts.tv_sec);
sdata->requestReceiptTimestamp.nanoseconds_nl=htonl(ts.tv_nsec);
memcpy(&sdata->requestingPortIdentity,
&sm->thisSM->rcvdPdelayReqPtr->head.sourcePortIdentity,
sizeof(MDPortIdentity));
memcpy(&sm->txPdelayResp, sdata, sizeof(MDPTPMsgPdelayResp));
return &sm->txPdelayResp;
}
static int txPdelayResp(gptpnet_data_t *gpnetd, int portIndex)
{
int ssize=sizeof(MDPTPMsgPdelayReq);
UB_LOG(UBL_DEBUGV, "%s:portIndex=%d\n",__func__, portIndex);
return gptpnet_send_whook(gpnetd, portIndex-1, ssize);
}
static md_pdelay_resp_state_t allstate_condition(md_pdelay_resp_data_t *sm)
{
if(sm->ptasg->BEGIN || !sm->ppg->forAllDomain->portOper || !sm->thisSM->portEnabled1)
return NOT_ENABLED;
return sm->state;
}
static void *not_enabled_proc(md_pdelay_resp_data_t *sm)
{
if(PORT_OPER)
UB_LOG(UBL_DEBUGV, "md_pdelay_resp:%s:portIndex=%d\n", __func__, sm->portIndex);
return NULL;
}
static md_pdelay_resp_state_t not_enabled_condition(md_pdelay_resp_data_t *sm)
{
if(sm->thisSM->portEnabled1) return INITIAL_WAITING_FOR_PDELAY_REQ;
return NOT_ENABLED;
}
static void *initial_waiting_for_pdelay_req_proc(md_pdelay_resp_data_t *sm)
{
if(PORT_OPER)
UB_LOG(UBL_DEBUGV, "md_pdelay_resp:%s:portIndex=%d\n", __func__, sm->portIndex);
sm->thisSM->rcvdPdelayReq = false;
sm->last_seqid = -1;
return NULL;
}
static md_pdelay_resp_state_t initial_waiting_for_pdelay_req_condition(md_pdelay_resp_data_t *sm)
{
if(sm->thisSM->rcvdPdelayReq) return SENT_PDELAY_RESP_WAITING_FOR_TIMESTAMP;
return INITIAL_WAITING_FOR_PDELAY_REQ;
}
static int waiting_for_pdelay_req_proc(md_pdelay_resp_data_t *sm)
{
if(PORT_OPER)
UB_LOG(UBL_DEBUGV, "md_pdelay_resp:%s:portIndex=%d\n", __func__, sm->portIndex);
sm->thisSM->txPdelayRespFollowUpPtr =
setPdelayRespFollowUp(sm);
if(txPdelayRespFollowUp(sm->gpnetd, sm->portIndex)==-1) return -1;
sm->statd.pdelay_resp_fup_send++;
return 0;
}
static md_pdelay_resp_state_t waiting_for_pdelay_req_condition(md_pdelay_resp_data_t *sm)
{
if(sm->thisSM->rcvdPdelayReq) return SENT_PDELAY_RESP_WAITING_FOR_TIMESTAMP;
return WAITING_FOR_PDELAY_REQ;
}
static int sent_pdelay_resp_waiting_for_timestamp_proc(md_pdelay_resp_data_t *sm,
uint64_t cts64)
{
int res;
UB_LOG(UBL_DEBUGV, "md_pdelay_resp:%s:portIndex=%d\n", __func__, sm->portIndex);
sm->thisSM->rcvdPdelayReq = false;
sm->thisSM->txPdelayRespPtr = setPdelayResp(sm);
sm->thisSM->rcvdMDTimestampReceive = false;
res=txPdelayResp(sm->gpnetd, sm->portIndex);
if(res==-1) return -1;
sm->txPdelayResp_time=cts64;
sm->statd.pdelay_resp_send++;
if(res<0){
sm->mock_txts64=gptpclock_getts64(sm->ptasg->thisClockIndex,0);
res=-res;
}
return res;
}
static md_pdelay_resp_state_t sent_pdelay_resp_waiting_for_timestamp_condition(
md_pdelay_resp_data_t *sm, uint64_t cts64)
{
if(sm->thisSM->rcvdMDTimestampReceive) return WAITING_FOR_PDELAY_REQ;
if(cts64 - sm->txPdelayResp_time >=
gptpnet_txtslost_time(sm->gpnetd, sm->portIndex-1)){
if(sm->mock_txts64){
sm->ts3=sm->mock_txts64;
sm->mock_txts64=0;
return WAITING_FOR_PDELAY_REQ;
}
UB_TLOG(UBL_WARN, "md_pdelay_resp:%s:missing TxTS, portIndex=%d, seqID=%d\n",
__func__, sm->portIndex,
ntohs(sm->thisSM->rcvdPdelayReqPtr->head.sequenceId_ns));
// send again the same PdelayResp
sm->last_state=REACTION;
return SENT_PDELAY_RESP_WAITING_FOR_TIMESTAMP;
// Or ignore this PdelayReq, and wait next one
//return INITIAL_WAITING_FOR_PDELAY_REQ
}
return SENT_PDELAY_RESP_WAITING_FOR_TIMESTAMP;
}
// return 1 when PdelayResp is sent, otherwise return 0;
int md_pdelay_resp_sm(md_pdelay_resp_data_t *sm, uint64_t cts64)
{
bool state_change;
if(!sm) return 0;
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 = NOT_ENABLED;
break;
case NOT_ENABLED:
if(state_change)
not_enabled_proc(sm);
sm->state = not_enabled_condition(sm);
break;
case INITIAL_WAITING_FOR_PDELAY_REQ:
if(state_change)
initial_waiting_for_pdelay_req_proc(sm);
sm->state = initial_waiting_for_pdelay_req_condition(sm);
break;
case WAITING_FOR_PDELAY_REQ:
if(state_change)
if(waiting_for_pdelay_req_proc(sm)){
sm->last_state=REACTION;
return 0;
}
sm->state = waiting_for_pdelay_req_condition(sm);
break;
case SENT_PDELAY_RESP_WAITING_FOR_TIMESTAMP:
if(state_change){
if(sent_pdelay_resp_waiting_for_timestamp_proc(sm, cts64)<0){
sm->last_state=REACTION;
return 0;
}
return 1;
}
sm->state = sent_pdelay_resp_waiting_for_timestamp_condition(sm, cts64);
break;
case REACTION:
break;
}
if(sm->last_state == sm->state) break;
}
return 0;
}
void md_pdelay_resp_sm_init(md_pdelay_resp_data_t **sm,
int portIndex,
gptpnet_data_t *gpnetd,
PerTimeAwareSystemGlobal *ptasg,
PerPortGlobal *ppg)
{
UB_LOG(UBL_DEBUGV, "%s:portIndex=%d\n", __func__, portIndex);
INIT_SM_DATA(md_pdelay_resp_data_t, MDPdelayRespSM, sm);
(*sm)->gpnetd = gpnetd;
(*sm)->ptasg = ptasg;
(*sm)->ppg = ppg;
(*sm)->portIndex = portIndex;
(*sm)->cmlds_mode = gptpconf_get_intitem(CONF_CMLDS_MODE);
// 11.2.18.2.5
if((*sm)->cmlds_mode){
// ??? cmldsLinkPortEnabled for CMLDS, but we use ptpPortEnabled here
(*sm)->thisSM->portEnabled1 = (*sm)->ppg->ptpPortEnabled;
}else{
(*sm)->thisSM->portEnabled1 = (*sm)->ppg->ptpPortEnabled;
}
}
int md_pdelay_resp_sm_close(md_pdelay_resp_data_t **sm)
{
UB_LOG(UBL_DEBUGV, "%s:portIndex=%d\n", __func__, (*sm)->portIndex);
CLOSE_SM_DATA(sm);
return 0;
}
int md_pdelay_resp_sm_recv_req(md_pdelay_resp_data_t *sm, event_data_recv_t *edrecv,
uint64_t cts64)
{
uint16_t recsqid;
UB_TLOG(UBL_DEBUGV, "%s:port=%d, state=%d\n",__func__, sm->portIndex, sm->state);
sm->statd.pdelay_req_rec++;
memcpy(&sm->rcvdPdelayReq, edrecv->recbptr, sizeof(MDPTPMsgPdelayReq));
sm->thisSM->rcvdPdelayReqPtr = &sm->rcvdPdelayReq;
if(!sm->cmlds_mode){
// 802.1AS-2020 8.1 the value of majorSdoId for gPTP domain must be 0x1
// When device is accepting message under CMLDS domain, allow values
// other than 0x1
if((sm->thisSM->rcvdPdelayReqPtr->head.majorSdoId_messageType & 0xF0)!=0x10){
UB_LOG(UBL_DEBUGV, "%s:port=%d, invalid majorSdoId on gPTP domain, ignore event.\n",
__func__, sm->portIndex);
return 0;
}
}else{
if((sm->thisSM->rcvdPdelayReqPtr->head.majorSdoId_messageType & 0xF0)==0x20){
if(sm->ppg->forAllDomain->receivedNonCMLDSPdelayReq!=-1){
UB_LOG(UBL_INFO, "%s:port=%d, set receivedNonCMLDSPdelayReq=-1\n",
__func__, sm->portIndex);
sm->ppg->forAllDomain->receivedNonCMLDSPdelayReq=-1;
}
}
}
if((sm->thisSM->rcvdPdelayReqPtr->head.majorSdoId_messageType & 0xF0)==0x10){
if(sm->ppg->forAllDomain->receivedNonCMLDSPdelayReq!=1){
UB_LOG(UBL_INFO, "%s:port=%d, set receivedNonCMLDSPdelayReq=1\n",
__func__, sm->portIndex);
sm->ppg->forAllDomain->receivedNonCMLDSPdelayReq=1;
}
}
recsqid=ntohs(sm->thisSM->rcvdPdelayReqPtr->head.sequenceId_ns);
if(sm->last_seqid >= 0){
uint16_t expsqid;
expsqid=(sm->last_seqid+1)&0xffff;
if(expsqid != recsqid){
UB_TLOG(UBL_WARN, "%s: port=%d, expected SeqID=%u, but received=%u\n",
__func__, sm->portIndex, expsqid, recsqid);
sm->last_seqid=-1;
return 0;
}
}
sm->statd.pdelay_req_rec_valid++;
sm->last_seqid=recsqid;
sm->thisSM->rcvdPdelayReq = true;
sm->ts2=edrecv->ts64;
return md_pdelay_resp_sm(sm, cts64);
}
void md_pdelay_resp_sm_txts(md_pdelay_resp_data_t *sm, event_data_txts_t *edtxts,
uint64_t cts64)
{
UB_LOG(UBL_DEBUGV, "%s:portIndex=%d, received seqID=%d\n",
__func__, sm->portIndex, edtxts->seqid);
if(md_abnormal_timestamp(PDELAY_RESP, sm->portIndex-1, -1)) return;
if(sm->state!=SENT_PDELAY_RESP_WAITING_FOR_TIMESTAMP ||
edtxts->seqid != ntohs(sm->thisSM->rcvdPdelayReqPtr->head.sequenceId_ns)){
UB_TLOG(UBL_WARN, "%s:TxTS is not expected, seqid expected=%d, received=%d\n",
__func__, ntohs(sm->thisSM->rcvdPdelayReqPtr->head.sequenceId_ns),
edtxts->seqid);
return;
}
sm->ts3=edtxts->ts64;
sm->thisSM->rcvdMDTimestampReceive = true;
md_pdelay_resp_sm(sm, cts64);
}
void md_pdelay_resp_stat_reset(md_pdelay_resp_data_t *sm)
{
memset(&sm->statd, 0, sizeof(md_pdelay_resp_stat_data_t));
}
md_pdelay_resp_stat_data_t *md_pdelay_resp_get_stat(md_pdelay_resp_data_t *sm)
{
return &sm->statd;
}