/
BcmPtpTcMgr.cpp
300 lines (249 loc) · 8.99 KB
/
BcmPtpTcMgr.cpp
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
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "fboss/agent/hw/bcm/BcmPtpTcMgr.h"
#include <folly/logging/xlog.h>
#include "fboss/agent/hw/bcm/BcmAddressFBConvertors.h"
#include "fboss/agent/hw/bcm/BcmError.h"
#include "fboss/agent/hw/bcm/BcmFieldProcessorUtils.h"
#include "fboss/agent/hw/bcm/BcmPlatform.h"
#include "fboss/agent/hw/bcm/BcmPortTable.h"
#include "fboss/agent/state/SwitchState.h"
extern "C" {
#include <bcm/switch.h>
#include <bcm/time.h>
}
namespace facebook::fboss {
// static
int BcmPtpTcMgr::getTsBitModeArg(cfg::AsicType asicType) {
// TODO(rajukumarfb5368): integrate with HwAsic::isSupported() or a variant
switch (asicType) {
case cfg::AsicType::ASIC_TYPE_TOMAHAWK:
return bcmTimesyncTimestampingMode32bit;
case cfg::AsicType::ASIC_TYPE_TOMAHAWK3:
case cfg::AsicType::ASIC_TYPE_TOMAHAWK4:
return bcmTimesyncTimestampingMode48bit;
default:
throw FbossError(
"Device type ",
asicType,
" not supported yet, please only use TH or TH3");
}
return -1;
}
void BcmPtpTcMgr::enablePortTimesyncConfig(
bcm_port_timesync_config_t* port_timesync_config) {
/* Set port timesync configuration struct flags to 1-step TC and mandatory
* update of all packets' correction field (CF) */
port_timesync_config->flags |= BCM_PORT_TIMESYNC_DEFAULT;
port_timesync_config->flags |=
BCM_PORT_TIMESYNC_ONE_STEP_TIMESTAMP; /* Set to OSTS - One-Step
TimeStamping */
port_timesync_config->flags |=
BCM_PORT_TIMESYNC_TIMESTAMP_CFUPDATE_ALL; /* Always update correction
field */
}
void BcmPtpTcMgr::enablePcsBasedOneStepTimestamping(bcm_port_t port) {
const auto unit = hw_->getUnit();
// TODO(rajukumarfb5368): Use bcm_port_control_phy_timesync_set() in SDK
// > 6.5.18 to configure timestamp offset and adjust. Ref:
// https://www.internalfb.com/diff/D24411684 (Base vs V11)
bcm_port_phy_timesync_config_t phy;
bcm_port_phy_timesync_config_t_init(&phy);
phy.flags = BCM_PORT_PHY_TIMESYNC_ENABLE; /* Enable PHY based timesync */
phy.flags |= BCM_PORT_PHY_TIMESYNC_ONE_STEP_ENABLE; /* Set PHY timesync to
one-step TS */
BCM_CHECK_ERROR(
bcm_port_phy_timesync_config_set(unit, port, &phy),
"failed to enable TC on port ",
port);
}
void BcmPtpTcMgr::disablePcsBasedOneStepTimestamping(bcm_port_t port) {
const auto unit = hw_->getUnit();
bcm_port_phy_timesync_config_t phy;
bcm_port_phy_timesync_config_t_init(&phy);
BCM_CHECK_ERROR(
bcm_port_phy_timesync_config_set(unit, port, &phy),
"failed to disable TC on port ",
port);
}
// static
bool BcmPtpTcMgr::isPcsBasedOneStepTimestampingEnabled(
const BcmSwitchIf* hw,
bcm_port_t port) {
const auto unit = hw->getUnit();
bcm_port_phy_timesync_config_t phy;
bcm_port_phy_timesync_config_t_init(&phy);
BCM_CHECK_ERROR(
bcm_port_phy_timesync_config_get(unit, port, &phy),
"failed to get phy timesync config on port ",
port);
return (phy.flags & BCM_PORT_PHY_TIMESYNC_ENABLE) &&
(phy.flags & BCM_PORT_PHY_TIMESYNC_ONE_STEP_ENABLE);
}
void BcmPtpTcMgr::enableTimeInterface() {
bcm_time_interface_t time_interface; /* device time (abstract) interface
configuration struct */
bcm_time_interface_t_init(&time_interface);
time_interface.flags = BCM_TIME_ENABLE;
bcm_time_interface_add(hw_->getUnit(), &time_interface);
}
void BcmPtpTcMgr::disableTimeInterface() {
bcm_time_interface_delete_all(hw_->getUnit());
}
// static
bool BcmPtpTcMgr::isTimeInterfaceEnabled(const BcmSwitchIf* hw) {
bcm_time_interface_t time_interface;
bcm_time_interface_t_init(&time_interface);
const auto unit = hw->getUnit();
bcm_time_interface_get(unit, &time_interface);
return time_interface.flags & BCM_TIME_ENABLE;
}
// static
bool BcmPtpTcMgr::isPtpTcSupported(const BcmSwitchIf* hw) {
return hw->getPlatform()->getAsic()->isSupported(HwAsic::Feature::PTP_TC);
}
// static
bool BcmPtpTcMgr::isPtpTcPcsSupported(const BcmSwitchIf* hw) {
return hw->getPlatform()->getAsic()->isSupported(HwAsic::Feature::PTP_TC_PCS);
}
/* separate configuration for MAC based timestamping devices - TD2/TH, and
* PCS based timestamping devices - TH3 */
void BcmPtpTcMgr::enablePtpTc() {
if (!isPtpTcSupported(hw_)) {
XLOG(DBG2) << "[PTP] Ignore configuration of unsupported feature";
return;
}
XLOG(DBG2) << "[PTP] Start enable";
const auto unit = hw_->getUnit();
bcm_port_timesync_config_t port_timesync_config;
bcm_port_timesync_config_t_init(&port_timesync_config);
enablePortTimesyncConfig(&port_timesync_config);
bcm_port_config_t portConfig;
bcm_port_config_t_init(&portConfig);
BCM_CHECK_ERROR(
bcm_port_config_get(unit, &portConfig),
"failed to get port configuration");
int count = 0;
bcm_port_t port;
BCM_PBMP_ITER(portConfig.port, port) {
if (isPtpTcPcsSupported(hw_)) {
enablePcsBasedOneStepTimestamping(port);
}
// TODO(rajukumarfb5368): high level description of bcm_*() calls needed
BCM_CHECK_ERROR(
bcm_port_timesync_config_set(
unit, port, 1 /* config_count */, &port_timesync_config),
"Error in enabling TC correction for port ",
port);
static const int tsBitModeArg =
getTsBitModeArg(hw_->getPlatform()->getAsic()->getAsicType());
BCM_CHECK_ERROR(
bcm_switch_control_port_set(
unit, port, bcmSwitchTimesyncEgressTimestampingMode, tsBitModeArg),
"Error in enabling TC correction for port ",
port);
count++;
}
enableTimeInterface();
XLOG(DBG2) << "[PTP] Enabled timestamping on " << count << " ports";
}
void BcmPtpTcMgr::disablePtpTc() {
if (!isPtpTcSupported(hw_)) {
XLOG(DBG2) << "[PTP] Ignore configuration of unsupported feature";
return;
}
XLOG(DBG2) << "[PTP] Start disable";
const auto unit = hw_->getUnit();
bcm_port_config_t portConfig;
bcm_port_config_t_init(&portConfig);
BCM_CHECK_ERROR(
bcm_port_config_get(unit, &portConfig),
"failed to get port configuration");
int count = 0;
bcm_port_t port;
BCM_PBMP_ITER(portConfig.port, port) {
if (isPtpTcPcsSupported(hw_)) {
disablePcsBasedOneStepTimestamping(port);
}
BCM_CHECK_ERROR(
bcm_port_timesync_config_set(unit, port, 0 /* config_count */, nullptr),
"Error in disabling TC correction for port ",
port);
// Reset to whatever is default arg for the given asic/sdk
// For TH4 its 48-bit
static const int tsBitModeArg =
getTsBitModeArg(hw_->getPlatform()->getAsic()->getAsicType());
BCM_CHECK_ERROR(
bcm_switch_control_port_set(
unit, port, bcmSwitchTimesyncEgressTimestampingMode, tsBitModeArg),
"Error in disabling TC egress timestamping mode for port ",
port);
count++;
}
disableTimeInterface();
XLOG(DBG2) << "[PTP] Disabled timestamping on " << count << " ports";
}
// static
bool BcmPtpTcMgr::isPtpTcEnabled(const BcmSwitchIf* hw) {
bcm_port_config_t portConfig;
bcm_port_config_t_init(&portConfig);
XLOG(DBG3) << "Check if PTP TC is enabled";
const auto unit = hw->getUnit();
BCM_CHECK_ERROR(
bcm_port_config_get(unit, &portConfig),
"failed to get port configuration");
bcm_port_t port;
BCM_PBMP_ITER(portConfig.port, port) {
if (isPtpTcPcsSupported(hw)) {
if (!isPcsBasedOneStepTimestampingEnabled(hw, port)) {
return false;
}
}
bcm_port_timesync_config_t port_timesync_config;
bcm_port_timesync_config_t_init(&port_timesync_config);
int array_count = -1;
BCM_CHECK_ERROR(
bcm_port_timesync_config_get(
unit,
port,
1 /* array_size */,
&port_timesync_config,
&array_count),
"failed to get port timesync configuration");
if (array_count != 1) {
return false;
}
if (!(port_timesync_config.flags & BCM_PORT_TIMESYNC_DEFAULT)) {
return false;
}
if (!(port_timesync_config.flags & BCM_PORT_TIMESYNC_ONE_STEP_TIMESTAMP)) {
return false;
}
if (!(port_timesync_config.flags &
BCM_PORT_TIMESYNC_TIMESTAMP_CFUPDATE_ALL)) {
return false;
}
int tsBitModeArgFound = -1;
BCM_CHECK_ERROR(
bcm_switch_control_port_get(
unit,
port,
bcmSwitchTimesyncEgressTimestampingMode,
&tsBitModeArgFound),
"failed to get control port configuration");
static const int tsBitModeArg =
getTsBitModeArg(hw->getPlatform()->getAsic()->getAsicType());
if (tsBitModeArg != tsBitModeArgFound) {
return false;
}
}
return isTimeInterfaceEnabled(hw);
}
} // namespace facebook::fboss