forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlv.c
262 lines (248 loc) · 8.18 KB
/
tlv.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
/**
* @file tlv.c
* @note Copyright (C) 2012 Richard Cochran <[email protected]>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <arpa/inet.h>
#include <string.h>
#include "port.h"
#include "tlv.h"
uint8_t ieee8021_id[3] = { IEEE_802_1_COMMITTEE };
static void scaled_ns_n2h(ScaledNs *sns)
{
sns->nanoseconds_msb = ntohs(sns->nanoseconds_msb);
sns->nanoseconds_lsb = net2host64(sns->nanoseconds_msb);
sns->fractional_nanoseconds = ntohs(sns->fractional_nanoseconds);
}
static void scaled_ns_h2n(ScaledNs *sns)
{
sns->nanoseconds_msb = htons(sns->nanoseconds_msb);
sns->nanoseconds_lsb = host2net64(sns->nanoseconds_msb);
sns->fractional_nanoseconds = htons(sns->fractional_nanoseconds);
}
static void mgt_post_recv(struct management_tlv *m)
{
struct defaultDS *dds;
struct currentDS *cds;
struct parentDS *pds;
struct timePropertiesDS *tp;
struct portDS *p;
struct time_status_np *tsn;
switch (m->id) {
case DEFAULT_DATA_SET:
dds = (struct defaultDS *) m->data;
dds->numberPorts = ntohs(dds->numberPorts);
dds->clockQuality.offsetScaledLogVariance =
ntohs(dds->clockQuality.offsetScaledLogVariance);
break;
case CURRENT_DATA_SET:
cds = (struct currentDS *) m->data;
cds->stepsRemoved = ntohs(cds->stepsRemoved);
cds->offsetFromMaster = net2host64(cds->offsetFromMaster);
cds->meanPathDelay = net2host64(cds->meanPathDelay);
break;
case PARENT_DATA_SET:
pds = (struct parentDS *) m->data;
pds->parentPortIdentity.portNumber =
ntohs(pds->parentPortIdentity.portNumber);
pds->observedParentOffsetScaledLogVariance =
ntohs(pds->observedParentOffsetScaledLogVariance);
pds->observedParentClockPhaseChangeRate =
ntohl(pds->observedParentClockPhaseChangeRate);
pds->grandmasterClockQuality.offsetScaledLogVariance =
ntohs(pds->grandmasterClockQuality.offsetScaledLogVariance);
break;
case TIME_PROPERTIES_DATA_SET:
tp = (struct timePropertiesDS *) m->data;
tp->currentUtcOffset = ntohs(tp->currentUtcOffset);
break;
case PORT_DATA_SET:
p = (struct portDS *) m->data;
p->portIdentity.portNumber = ntohs(p->portIdentity.portNumber);
p->peerMeanPathDelay = net2host64(p->peerMeanPathDelay);
break;
case TIME_STATUS_NP:
tsn = (struct time_status_np *) m->data;
tsn->master_offset = net2host64(tsn->master_offset);
tsn->ingress_time = net2host64(tsn->ingress_time);
tsn->cumulativeScaledRateOffset = ntohl(tsn->cumulativeScaledRateOffset);
tsn->scaledLastGmPhaseChange = ntohl(tsn->scaledLastGmPhaseChange);
tsn->gmTimeBaseIndicator = ntohs(tsn->gmTimeBaseIndicator);
scaled_ns_n2h(&tsn->lastGmPhaseChange);
tsn->gmPresent = ntohl(tsn->gmPresent);
break;
}
}
static void mgt_pre_send(struct management_tlv *m)
{
struct defaultDS *dds;
struct currentDS *cds;
struct parentDS *pds;
struct timePropertiesDS *tp;
struct portDS *p;
struct time_status_np *tsn;
switch (m->id) {
case DEFAULT_DATA_SET:
dds = (struct defaultDS *) m->data;
dds->numberPorts = htons(dds->numberPorts);
dds->clockQuality.offsetScaledLogVariance =
htons(dds->clockQuality.offsetScaledLogVariance);
break;
case CURRENT_DATA_SET:
cds = (struct currentDS *) m->data;
cds->stepsRemoved = htons(cds->stepsRemoved);
cds->offsetFromMaster = host2net64(cds->offsetFromMaster);
cds->meanPathDelay = host2net64(cds->meanPathDelay);
break;
case PARENT_DATA_SET:
pds = (struct parentDS *) m->data;
pds->parentPortIdentity.portNumber =
htons(pds->parentPortIdentity.portNumber);
pds->observedParentOffsetScaledLogVariance =
htons(pds->observedParentOffsetScaledLogVariance);
pds->observedParentClockPhaseChangeRate =
htonl(pds->observedParentClockPhaseChangeRate);
pds->grandmasterClockQuality.offsetScaledLogVariance =
htons(pds->grandmasterClockQuality.offsetScaledLogVariance);
break;
case TIME_PROPERTIES_DATA_SET:
tp = (struct timePropertiesDS *) m->data;
tp->currentUtcOffset = htons(tp->currentUtcOffset);
break;
case PORT_DATA_SET:
p = (struct portDS *) m->data;
p->portIdentity.portNumber = htons(p->portIdentity.portNumber);
p->peerMeanPathDelay = host2net64(p->peerMeanPathDelay);
break;
case TIME_STATUS_NP:
tsn = (struct time_status_np *) m->data;
tsn->master_offset = host2net64(tsn->master_offset);
tsn->ingress_time = host2net64(tsn->ingress_time);
tsn->cumulativeScaledRateOffset = htonl(tsn->cumulativeScaledRateOffset);
tsn->scaledLastGmPhaseChange = htonl(tsn->scaledLastGmPhaseChange);
tsn->gmTimeBaseIndicator = htons(tsn->gmTimeBaseIndicator);
scaled_ns_h2n(&tsn->lastGmPhaseChange);
tsn->gmPresent = htonl(tsn->gmPresent);
break;
}
}
static void org_post_recv(struct organization_tlv *org)
{
struct follow_up_info_tlv *f;
if (0 == memcmp(org->id, ieee8021_id, sizeof(ieee8021_id))) {
if (org->subtype[0] || org->subtype[1]) {
return;
}
switch (org->subtype[2]) {
case 1:
f = (struct follow_up_info_tlv *) org;
f->cumulativeScaledRateOffset = ntohl(f->cumulativeScaledRateOffset);
f->gmTimeBaseIndicator = ntohs(f->gmTimeBaseIndicator);
scaled_ns_n2h(&f->lastGmPhaseChange);
f->scaledLastGmPhaseChange = ntohl(f->scaledLastGmPhaseChange);
break;
}
}
}
static void org_pre_send(struct organization_tlv *org)
{
struct follow_up_info_tlv *f;
if (0 == memcmp(org->id, ieee8021_id, sizeof(ieee8021_id))) {
if (org->subtype[0] || org->subtype[1]) {
return;
}
switch (org->subtype[2]) {
case 1:
f = (struct follow_up_info_tlv *) org;
f->cumulativeScaledRateOffset = htonl(f->cumulativeScaledRateOffset);
f->gmTimeBaseIndicator = htons(f->gmTimeBaseIndicator);
scaled_ns_h2n(&f->lastGmPhaseChange);
f->scaledLastGmPhaseChange = htonl(f->scaledLastGmPhaseChange);
break;
}
}
}
void tlv_post_recv(struct TLV *tlv)
{
struct management_tlv *mgt;
struct management_error_status *mes;
struct path_trace_tlv *ptt;
switch (tlv->type) {
case TLV_MANAGEMENT:
mgt = (struct management_tlv *) tlv;
mgt->id = ntohs(mgt->id);
mgt_post_recv(mgt);
break;
case TLV_MANAGEMENT_ERROR_STATUS:
mes = (struct management_error_status *) tlv;
mes->error = ntohs(mes->error);
mes->id = ntohs(mes->id);
break;
case TLV_ORGANIZATION_EXTENSION:
org_post_recv((struct organization_tlv *) tlv);
break;
case TLV_REQUEST_UNICAST_TRANSMISSION:
case TLV_GRANT_UNICAST_TRANSMISSION:
case TLV_CANCEL_UNICAST_TRANSMISSION:
case TLV_ACKNOWLEDGE_CANCEL_UNICAST_TRANSMISSION:
break;
case TLV_PATH_TRACE:
ptt = (struct path_trace_tlv *) tlv;
if (path_length(ptt) > PATH_TRACE_MAX) {
ptt->length = PATH_TRACE_MAX * sizeof(struct ClockIdentity);
}
break;
case TLV_ALTERNATE_TIME_OFFSET_INDICATOR:
case TLV_AUTHENTICATION:
case TLV_AUTHENTICATION_CHALLENGE:
case TLV_SECURITY_ASSOCIATION_UPDATE:
case TLV_CUM_FREQ_SCALE_FACTOR_OFFSET:
default:
break;
}
}
void tlv_pre_send(struct TLV *tlv)
{
struct management_tlv *mgt;
struct management_error_status *mes;
switch (tlv->type) {
case TLV_MANAGEMENT:
mgt = (struct management_tlv *) tlv;
mgt_pre_send(mgt);
mgt->id = htons(mgt->id);
break;
case TLV_MANAGEMENT_ERROR_STATUS:
mes = (struct management_error_status *) tlv;
mes->error = htons(mes->error);
mes->id = htons(mes->id);
break;
case TLV_ORGANIZATION_EXTENSION:
org_pre_send((struct organization_tlv *) tlv);
break;
case TLV_REQUEST_UNICAST_TRANSMISSION:
case TLV_GRANT_UNICAST_TRANSMISSION:
case TLV_CANCEL_UNICAST_TRANSMISSION:
case TLV_ACKNOWLEDGE_CANCEL_UNICAST_TRANSMISSION:
case TLV_PATH_TRACE:
case TLV_ALTERNATE_TIME_OFFSET_INDICATOR:
case TLV_AUTHENTICATION:
case TLV_AUTHENTICATION_CHALLENGE:
case TLV_SECURITY_ASSOCIATION_UPDATE:
case TLV_CUM_FREQ_SCALE_FACTOR_OFFSET:
default:
break;
}
}