-
Notifications
You must be signed in to change notification settings - Fork 154
/
port_signaling.c
247 lines (217 loc) · 6.73 KB
/
port_signaling.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
/**
* @file port_signaling.c
* @brief Implements signaling messages
* @note Copyright (C) 2018 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-1335 USA.
*/
#include "port.h"
#include "port_private.h"
#include "print.h"
#include "unicast_client.h"
#include "unicast_service.h"
const struct PortIdentity wildcard_pid = {
.clockIdentity = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
},
.portNumber = 0xffff,
};
struct ptp_message *port_signaling_construct(struct port *p,
const struct PortIdentity *tpid)
{
struct ptp_message *msg;
msg = msg_allocate();
if (!msg) {
return NULL;
}
msg->hwts.type = p->timestamping;
msg->header.tsmt = SIGNALING | p->transportSpecific;
msg->header.ver = PTP_VERSION;
msg->header.messageLength = sizeof(struct signaling_msg);
msg->header.domainNumber = clock_domain_number(p->clock);
msg->header.sourcePortIdentity = p->portIdentity;
msg->header.sequenceId = p->seqnum.signaling++;
msg->header.logMessageInterval = 0x7F;
msg->signaling.targetPortIdentity = *tpid;
return msg;
}
struct ptp_message *port_signaling_uc_construct(struct port *p,
struct address *address,
struct PortIdentity *tpid)
{
struct ptp_message *msg;
msg = port_signaling_construct(p, tpid);
if (!msg) {
return NULL;
}
msg->header.flagField[0] |= UNICAST;
msg->address = *address;
return msg;
}
static int8_t set_interval(int8_t current_interval,
int8_t new_interval,
int8_t initial_interval)
{
switch (new_interval) {
case SIGNAL_NO_CHANGE:
return current_interval;
case SIGNAL_SET_INITIAL:
return initial_interval;
default:
return new_interval;
}
}
static int process_interval_request(struct port *p,
struct msg_interval_req_tlv *r)
{
p->logAnnounceInterval = set_interval(p->logAnnounceInterval,
r->announceInterval,
p->initialLogAnnounceInterval);
p->logSyncInterval = set_interval(p->logSyncInterval,
r->timeSyncInterval,
p->initialLogSyncInterval);
p->logPdelayReqInterval = set_interval(p->logPdelayReqInterval,
r->linkDelayInterval,
p->logMinPdelayReqInterval);
return 0;
}
static int process_interface_rate(struct port *p,
struct msg_interface_rate_tlv *r)
{
Integer64 delayAsymmetry;
double nsDelay;
Integer64 slaveBitPeriod;
Integer64 masterBitPeriod;
if (p->iface_rate_tlv && interface_ifinfo_valid(p->iface)) {
slaveBitPeriod = interface_bitperiod(p->iface);
masterBitPeriod = r->interfaceBitPeriod;
/* Delay Asymmetry Calculation */
nsDelay = (double)(masterBitPeriod - slaveBitPeriod) / (2 * 1.0e9);
delayAsymmetry =
(r->numberOfBitsAfterTimestamp - r->numberOfBitsBeforeTimestamp) * nsDelay;
if (delayAsymmetry != p->portAsymmetry) {
p->asymmetry += ((delayAsymmetry - p->portAsymmetry) << 16);
p->portAsymmetry = delayAsymmetry;
}
}
return 0;
}
int process_signaling(struct port *p, struct ptp_message *m)
{
struct tlv_extra *extra;
struct organization_tlv *org;
struct msg_interval_req_tlv *r;
struct msg_interface_rate_tlv *rate;
int err = 0, result;
switch (p->state) {
case PS_INITIALIZING:
case PS_FAULTY:
case PS_DISABLED:
return 0;
case PS_LISTENING:
case PS_PRE_MASTER:
case PS_MASTER:
case PS_GRAND_MASTER:
case PS_PASSIVE:
case PS_UNCALIBRATED:
case PS_SLAVE:
break;
}
/* Ignore signaling messages not addressed to this port. */
if (!pid_eq(&m->signaling.targetPortIdentity, &p->portIdentity) &&
!pid_eq(&m->signaling.targetPortIdentity, &wildcard_pid)) {
return 0;
}
TAILQ_FOREACH(extra, &m->tlv_list, list) {
switch (extra->tlv->type) {
case TLV_REQUEST_UNICAST_TRANSMISSION:
result = unicast_service_add(p, m, extra);
switch (result) {
case SERVICE_GRANTED:
err = unicast_service_grant(p, m, extra);
break;
case SERVICE_DENIED:
err = unicast_service_deny(p, m, extra);
break;
case SERVICE_DISABLED:
default:
break;
}
break;
case TLV_GRANT_UNICAST_TRANSMISSION:
unicast_client_grant(p, m, extra);
break;
case TLV_CANCEL_UNICAST_TRANSMISSION:
err = unicast_client_cancel(p, m, extra);
unicast_service_remove(p, m, extra);
break;
case TLV_ACKNOWLEDGE_CANCEL_UNICAST_TRANSMISSION:
break;
case TLV_ORGANIZATION_EXTENSION:
org = (struct organization_tlv *)extra->tlv;
if (0 == memcmp(org->id, ieee8021_id, sizeof(ieee8021_id)) &&
org->subtype[0] == 0 && org->subtype[1] == 0 && org->subtype[2] == 2) {
r = (struct msg_interval_req_tlv *) extra->tlv;
err = process_interval_request(p, r);
} else if (0 == memcmp(org->id, itu_t_id, sizeof(itu_t_id)) &&
org->subtype[0] == 0 && org->subtype[1] == 0 && org->subtype[2] == 2) {
rate = (struct msg_interface_rate_tlv *) extra->tlv;
err = process_interface_rate(p, rate);
}
break;
}
}
return err;
}
int port_tx_interval_request(struct port *p,
Integer8 announceInterval,
Integer8 timeSyncInterval,
Integer8 linkDelayInterval)
{
struct msg_interval_req_tlv *mir;
struct PortIdentity tpid;
struct ptp_message *msg;
struct tlv_extra *extra;
int err;
if (!port_capable(p)) {
return 0;
}
memset(&tpid, 0xff, sizeof(tpid));
msg = port_signaling_construct(p, &tpid);
if (!msg) {
return -1;
}
extra = msg_tlv_append(msg, sizeof(*mir));
if (!extra) {
err = -1;
goto out;
}
mir = (struct msg_interval_req_tlv *) extra->tlv;
mir->type = TLV_ORGANIZATION_EXTENSION;
mir->length = sizeof(*mir) - sizeof(mir->type) - sizeof(mir->length);
memcpy(mir->id, ieee8021_id, sizeof(ieee8021_id));
mir->subtype[2] = 2;
mir->timeSyncInterval = timeSyncInterval;
mir->announceInterval = announceInterval;
mir->linkDelayInterval = linkDelayInterval;
mir->flags = 0;
err = port_prepare_and_send(p, msg, TRANS_GENERAL);
if (err) {
pr_err("%s: send signaling failed", p->log_name);
}
out:
msg_put(msg);
return err;
}