Skip to content

Commit 834f893

Browse files
vladimirolteandavem330
authored andcommitted
net: dsa: sja1105: implement tc-gate using time-triggered virtual links
Restrict the TTEthernet hardware support on this switch to operate as closely as possible to IEEE 802.1Qci as possible. This means that it can perform PTP-time-based ingress admission control on streams identified by {DMAC, VID, PCP}, which is useful when trying to ensure the determinism of traffic scheduled via IEEE 802.1Qbv. The oddity comes from the fact that in hardware (and in TTEthernet at large), virtual links always need a full-blown action, including not only the type of policing, but also the list of destination ports. So in practice, a single tc-gate action will result in all packets getting dropped. Additional actions (either "trap" or "redirect") need to be specified in the same filter rule such that the conforming packets are actually forwarded somewhere. Apart from the VL Lookup, Policing and Forwarding tables which need to be programmed for each flow (virtual link), the Schedule engine also needs to be told to open/close the admission gates for each individual virtual link. A fairly accurate (and detailed) description of how that works is already present in sja1105_tas.c, since it is already used to trigger the egress gates for the tc-taprio offload (IEEE 802.1Qbv). Key point here, we remember that the schedule engine supports 8 "subschedules" (execution threads that iterate through the global schedule in parallel, and that no 2 hardware threads must execute a schedule entry at the same time). For tc-taprio, each egress port used one of these 8 subschedules, leaving a total of 4 subschedules unused. In principle we could have allocated 1 subschedule for the tc-gate offload of each ingress port, but actually the schedules of all virtual links installed on each ingress port would have needed to be merged together, before they could have been programmed to hardware. So simplify our life and just merge the entire tc-gate configuration, for all virtual links on all ingress ports, into a single subschedule. Be sure to check that against the usual hardware scheduling conflicts, and program it to hardware alongside any tc-taprio subschedule that may be present. The following scenarios were tested: 1. Quantitative testing: tc qdisc add dev swp2 clsact tc filter add dev swp2 ingress flower skip_sw \ dst_mac 42:be:24:9b:76:20 \ action gate index 1 base-time 0 \ sched-entry OPEN 1200 -1 -1 \ sched-entry CLOSE 1200 -1 -1 \ action trap ping 192.168.1.2 -f PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data. ............................. --- 192.168.1.2 ping statistics --- 948 packets transmitted, 467 received, 50.7384% packet loss, time 9671ms 2. Qualitative testing (with a phase-aligned schedule - the clocks are synchronized by ptp4l, not shown here): Receiver (sja1105): tc qdisc add dev swp2 clsact now=$(phc_ctl /dev/ptp1 get | awk '/clock time is/ {print $5}') && \ sec=$(echo $now | awk -F. '{print $1}') && \ base_time="$(((sec + 2) * 1000000000))" && \ echo "base time ${base_time}" tc filter add dev swp2 ingress flower skip_sw \ dst_mac 42:be:24:9b:76:20 \ action gate base-time ${base_time} \ sched-entry OPEN 60000 -1 -1 \ sched-entry CLOSE 40000 -1 -1 \ action trap Sender (enetc): now=$(phc_ctl /dev/ptp0 get | awk '/clock time is/ {print $5}') && \ sec=$(echo $now | awk -F. '{print $1}') && \ base_time="$(((sec + 2) * 1000000000))" && \ echo "base time ${base_time}" tc qdisc add dev eno0 parent root taprio \ num_tc 8 \ map 0 1 2 3 4 5 6 7 \ queues 1@0 1@1 1@2 1@3 1@4 1@5 1@6 1@7 \ base-time ${base_time} \ sched-entry S 01 50000 \ sched-entry S 00 50000 \ flags 2 ping -A 192.168.1.1 PING 192.168.1.1 (192.168.1.1): 56 data bytes ... ^C --- 192.168.1.1 ping statistics --- 1425 packets transmitted, 1424 packets received, 0% packet loss round-trip min/avg/max = 0.322/0.361/0.990 ms And just for comparison, with the tc-taprio schedule deleted: ping -A 192.168.1.1 PING 192.168.1.1 (192.168.1.1): 56 data bytes ... ^C --- 192.168.1.1 ping statistics --- 33 packets transmitted, 19 packets received, 42% packet loss round-trip min/avg/max = 0.336/0.464/0.597 ms Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent dfacc5a commit 834f893

10 files changed

Lines changed: 759 additions & 17 deletions

File tree

‎drivers/net/dsa/sja1105/sja1105.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct sja1105_regs {
3636
u64 status;
3737
u64 port_control;
3838
u64 rgu;
39+
u64 vl_status;
3940
u64 config;
4041
u64 sgmii;
4142
u64 rmii_pll1;
@@ -156,8 +157,16 @@ struct sja1105_rule {
156157

157158
/* SJA1105_RULE_VL */
158159
struct {
159-
unsigned long destports;
160160
enum sja1105_vl_type type;
161+
unsigned long destports;
162+
int sharindx;
163+
int maxlen;
164+
int ipv;
165+
u64 base_time;
166+
u64 cycle_time;
167+
int num_entries;
168+
struct action_gate_entry *entries;
169+
struct flow_stats stats;
161170
} vl;
162171
};
163172
};
@@ -304,6 +313,8 @@ int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
304313
struct flow_cls_offload *cls, bool ingress);
305314
int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
306315
struct flow_cls_offload *cls, bool ingress);
316+
int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
317+
struct flow_cls_offload *cls, bool ingress);
307318
void sja1105_flower_setup(struct dsa_switch *ds);
308319
void sja1105_flower_teardown(struct dsa_switch *ds);
309320
struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,

‎drivers/net/dsa/sja1105/sja1105_flower.c‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
309309
struct sja1105_private *priv = ds->priv;
310310
const struct flow_action_entry *act;
311311
unsigned long cookie = cls->cookie;
312+
bool routing_rule = false;
312313
struct sja1105_key key;
314+
bool gate_rule = false;
313315
bool vl_rule = false;
314316
int rc, i;
315317

@@ -332,6 +334,7 @@ int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
332334
case FLOW_ACTION_TRAP: {
333335
int cpu = dsa_upstream_port(ds, port);
334336

337+
routing_rule = true;
335338
vl_rule = true;
336339

337340
rc = sja1105_vl_redirect(priv, port, extack, cookie,
@@ -350,6 +353,7 @@ int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
350353
return -EOPNOTSUPP;
351354
}
352355

356+
routing_rule = true;
353357
vl_rule = true;
354358

355359
rc = sja1105_vl_redirect(priv, port, extack, cookie,
@@ -366,6 +370,21 @@ int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
366370
if (rc)
367371
goto out;
368372
break;
373+
case FLOW_ACTION_GATE:
374+
gate_rule = true;
375+
vl_rule = true;
376+
377+
rc = sja1105_vl_gate(priv, port, extack, cookie,
378+
&key, act->gate.index,
379+
act->gate.prio,
380+
act->gate.basetime,
381+
act->gate.cycletime,
382+
act->gate.cycletimeext,
383+
act->gate.num_entries,
384+
act->gate.entries);
385+
if (rc)
386+
goto out;
387+
break;
369388
default:
370389
NL_SET_ERR_MSG_MOD(extack,
371390
"Action not supported");
@@ -374,8 +393,23 @@ int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
374393
}
375394
}
376395

377-
if (vl_rule && !rc)
396+
if (vl_rule && !rc) {
397+
/* Delay scheduling configuration until DESTPORTS has been
398+
* populated by all other actions.
399+
*/
400+
if (gate_rule) {
401+
if (!routing_rule) {
402+
NL_SET_ERR_MSG_MOD(extack,
403+
"Can only offload gate action together with redirect or trap");
404+
return -EOPNOTSUPP;
405+
}
406+
rc = sja1105_init_scheduling(priv);
407+
if (rc)
408+
goto out;
409+
}
410+
378411
rc = sja1105_static_config_reload(priv, SJA1105_VIRTUAL_LINKS);
412+
}
379413

380414
out:
381415
return rc;
@@ -421,6 +455,27 @@ int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
421455
return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
422456
}
423457

458+
int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
459+
struct flow_cls_offload *cls, bool ingress)
460+
{
461+
struct sja1105_private *priv = ds->priv;
462+
struct sja1105_rule *rule = sja1105_rule_find(priv, cls->cookie);
463+
int rc;
464+
465+
if (!rule)
466+
return 0;
467+
468+
if (rule->type != SJA1105_RULE_VL)
469+
return 0;
470+
471+
rc = sja1105_vl_stats(priv, port, rule, &cls->stats,
472+
cls->common.extack);
473+
if (rc)
474+
return rc;
475+
476+
return 0;
477+
}
478+
424479
void sja1105_flower_setup(struct dsa_switch *ds)
425480
{
426481
struct sja1105_private *priv = ds->priv;

‎drivers/net/dsa/sja1105/sja1105_main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,7 @@ static const struct dsa_switch_ops sja1105_switch_ops = {
23692369
.port_policer_del = sja1105_port_policer_del,
23702370
.cls_flower_add = sja1105_cls_flower_add,
23712371
.cls_flower_del = sja1105_cls_flower_del,
2372+
.cls_flower_stats = sja1105_cls_flower_stats,
23722373
};
23732374

23742375
static int sja1105_check_device_id(struct sja1105_private *priv)

‎drivers/net/dsa/sja1105/sja1105_ptp.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ static inline s64 future_base_time(s64 base_time, s64 cycle_time, s64 now)
4848
return base_time + n * cycle_time;
4949
}
5050

51+
/* This is not a preprocessor macro because the "ns" argument may or may not be
52+
* s64 at caller side. This ensures it is properly type-cast before div_s64.
53+
*/
54+
static inline s64 ns_to_sja1105_delta(s64 ns)
55+
{
56+
return div_s64(ns, 200);
57+
}
58+
59+
static inline s64 sja1105_delta_to_ns(s64 delta)
60+
{
61+
return delta * 200;
62+
}
63+
5164
struct sja1105_ptp_cmd {
5265
u64 startptpcp; /* start toggling PTP_CLK pin */
5366
u64 stopptpcp; /* stop toggling PTP_CLK pin */

‎drivers/net/dsa/sja1105/sja1105_spi.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ static struct sja1105_regs sja1105et_regs = {
439439
.prod_id = 0x100BC3,
440440
.status = 0x1,
441441
.port_control = 0x11,
442+
.vl_status = 0x10000,
442443
.config = 0x020000,
443444
.rgu = 0x100440,
444445
/* UM10944.pdf, Table 86, ACU Register overview */
@@ -472,6 +473,7 @@ static struct sja1105_regs sja1105pqrs_regs = {
472473
.prod_id = 0x100BC3,
473474
.status = 0x1,
474475
.port_control = 0x12,
476+
.vl_status = 0x10000,
475477
.config = 0x020000,
476478
.rgu = 0x100440,
477479
/* UM10944.pdf, Table 86, ACU Register overview */

‎drivers/net/dsa/sja1105/sja1105_static_config.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ struct sja1105_vl_lookup_entry {
302302
u64 vlid;
303303
};
304304
};
305+
/* Not part of hardware structure */
306+
unsigned long flow_cookie;
305307
};
306308

307309
struct sja1105_vl_policing_entry {

‎drivers/net/dsa/sja1105/sja1105_tas.c‎

Lines changed: 112 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,17 @@
77
#define SJA1105_TAS_CLKSRC_STANDALONE 1
88
#define SJA1105_TAS_CLKSRC_AS6802 2
99
#define SJA1105_TAS_CLKSRC_PTP 3
10-
#define SJA1105_TAS_MAX_DELTA BIT(19)
1110
#define SJA1105_GATE_MASK GENMASK_ULL(SJA1105_NUM_TC - 1, 0)
1211

1312
#define work_to_sja1105_tas(d) \
1413
container_of((d), struct sja1105_tas_data, tas_work)
1514
#define tas_to_sja1105(d) \
1615
container_of((d), struct sja1105_private, tas_data)
1716

18-
/* This is not a preprocessor macro because the "ns" argument may or may not be
19-
* s64 at caller side. This ensures it is properly type-cast before div_s64.
20-
*/
21-
static s64 ns_to_sja1105_delta(s64 ns)
22-
{
23-
return div_s64(ns, 200);
24-
}
25-
26-
static s64 sja1105_delta_to_ns(s64 delta)
27-
{
28-
return delta * 200;
29-
}
30-
3117
static int sja1105_tas_set_runtime_params(struct sja1105_private *priv)
3218
{
3319
struct sja1105_tas_data *tas_data = &priv->tas_data;
20+
struct sja1105_gating_config *gating_cfg = &tas_data->gating_cfg;
3421
struct dsa_switch *ds = priv->ds;
3522
s64 earliest_base_time = S64_MAX;
3623
s64 latest_base_time = 0;
@@ -59,6 +46,19 @@ static int sja1105_tas_set_runtime_params(struct sja1105_private *priv)
5946
}
6047
}
6148

49+
if (!list_empty(&gating_cfg->entries)) {
50+
tas_data->enabled = true;
51+
52+
if (max_cycle_time < gating_cfg->cycle_time)
53+
max_cycle_time = gating_cfg->cycle_time;
54+
if (latest_base_time < gating_cfg->base_time)
55+
latest_base_time = gating_cfg->base_time;
56+
if (earliest_base_time > gating_cfg->base_time) {
57+
earliest_base_time = gating_cfg->base_time;
58+
its_cycle_time = gating_cfg->cycle_time;
59+
}
60+
}
61+
6262
if (!tas_data->enabled)
6363
return 0;
6464

@@ -155,13 +155,14 @@ static int sja1105_tas_set_runtime_params(struct sja1105_private *priv)
155155
* their "subschedule end index" (subscheind) equal to the last valid
156156
* subschedule's end index (in this case 5).
157157
*/
158-
static int sja1105_init_scheduling(struct sja1105_private *priv)
158+
int sja1105_init_scheduling(struct sja1105_private *priv)
159159
{
160160
struct sja1105_schedule_entry_points_entry *schedule_entry_points;
161161
struct sja1105_schedule_entry_points_params_entry
162162
*schedule_entry_points_params;
163163
struct sja1105_schedule_params_entry *schedule_params;
164164
struct sja1105_tas_data *tas_data = &priv->tas_data;
165+
struct sja1105_gating_config *gating_cfg = &tas_data->gating_cfg;
165166
struct sja1105_schedule_entry *schedule;
166167
struct sja1105_table *table;
167168
int schedule_start_idx;
@@ -213,6 +214,11 @@ static int sja1105_init_scheduling(struct sja1105_private *priv)
213214
}
214215
}
215216

217+
if (!list_empty(&gating_cfg->entries)) {
218+
num_entries += gating_cfg->num_entries;
219+
num_cycles++;
220+
}
221+
216222
/* Nothing to do */
217223
if (!num_cycles)
218224
return 0;
@@ -312,6 +318,42 @@ static int sja1105_init_scheduling(struct sja1105_private *priv)
312318
cycle++;
313319
}
314320

321+
if (!list_empty(&gating_cfg->entries)) {
322+
struct sja1105_gate_entry *e;
323+
324+
/* Relative base time */
325+
s64 rbt;
326+
327+
schedule_start_idx = k;
328+
schedule_end_idx = k + gating_cfg->num_entries - 1;
329+
rbt = future_base_time(gating_cfg->base_time,
330+
gating_cfg->cycle_time,
331+
tas_data->earliest_base_time);
332+
rbt -= tas_data->earliest_base_time;
333+
entry_point_delta = ns_to_sja1105_delta(rbt) + 1;
334+
335+
schedule_entry_points[cycle].subschindx = cycle;
336+
schedule_entry_points[cycle].delta = entry_point_delta;
337+
schedule_entry_points[cycle].address = schedule_start_idx;
338+
339+
for (i = cycle; i < 8; i++)
340+
schedule_params->subscheind[i] = schedule_end_idx;
341+
342+
list_for_each_entry(e, &gating_cfg->entries, list) {
343+
schedule[k].delta = ns_to_sja1105_delta(e->interval);
344+
schedule[k].destports = e->rule->vl.destports;
345+
schedule[k].setvalid = true;
346+
schedule[k].txen = true;
347+
schedule[k].vlindex = e->rule->vl.sharindx;
348+
schedule[k].winstindex = e->rule->vl.sharindx;
349+
if (e->gate_state) /* Gate open */
350+
schedule[k].winst = true;
351+
else /* Gate closed */
352+
schedule[k].winend = true;
353+
k++;
354+
}
355+
}
356+
315357
return 0;
316358
}
317359

@@ -415,6 +457,54 @@ sja1105_tas_check_conflicts(struct sja1105_private *priv, int port,
415457
return false;
416458
}
417459

460+
/* Check the tc-taprio configuration on @port for conflicts with the tc-gate
461+
* global subschedule. If @port is -1, check it against all ports.
462+
* To reuse the sja1105_tas_check_conflicts logic without refactoring it,
463+
* convert the gating configuration to a dummy tc-taprio offload structure.
464+
*/
465+
bool sja1105_gating_check_conflicts(struct sja1105_private *priv, int port,
466+
struct netlink_ext_ack *extack)
467+
{
468+
struct sja1105_gating_config *gating_cfg = &priv->tas_data.gating_cfg;
469+
size_t num_entries = gating_cfg->num_entries;
470+
struct tc_taprio_qopt_offload *dummy;
471+
struct sja1105_gate_entry *e;
472+
bool conflict;
473+
int i = 0;
474+
475+
if (list_empty(&gating_cfg->entries))
476+
return false;
477+
478+
dummy = kzalloc(sizeof(struct tc_taprio_sched_entry) * num_entries +
479+
sizeof(struct tc_taprio_qopt_offload), GFP_KERNEL);
480+
if (!dummy) {
481+
NL_SET_ERR_MSG_MOD(extack, "Failed to allocate memory");
482+
return true;
483+
}
484+
485+
dummy->num_entries = num_entries;
486+
dummy->base_time = gating_cfg->base_time;
487+
dummy->cycle_time = gating_cfg->cycle_time;
488+
489+
list_for_each_entry(e, &gating_cfg->entries, list)
490+
dummy->entries[i++].interval = e->interval;
491+
492+
if (port != -1) {
493+
conflict = sja1105_tas_check_conflicts(priv, port, dummy);
494+
} else {
495+
for (port = 0; port < SJA1105_NUM_PORTS; port++) {
496+
conflict = sja1105_tas_check_conflicts(priv, port,
497+
dummy);
498+
if (conflict)
499+
break;
500+
}
501+
}
502+
503+
kfree(dummy);
504+
505+
return conflict;
506+
}
507+
418508
int sja1105_setup_tc_taprio(struct dsa_switch *ds, int port,
419509
struct tc_taprio_qopt_offload *admin)
420510
{
@@ -473,6 +563,11 @@ int sja1105_setup_tc_taprio(struct dsa_switch *ds, int port,
473563
return -ERANGE;
474564
}
475565

566+
if (sja1105_gating_check_conflicts(priv, port, NULL)) {
567+
dev_err(ds->dev, "Conflict with tc-gate schedule\n");
568+
return -ERANGE;
569+
}
570+
476571
tas_data->offload[port] = taprio_offload_get(admin);
477572

478573
rc = sja1105_init_scheduling(priv);
@@ -779,6 +874,8 @@ void sja1105_tas_setup(struct dsa_switch *ds)
779874
INIT_WORK(&tas_data->tas_work, sja1105_tas_state_machine);
780875
tas_data->state = SJA1105_TAS_STATE_DISABLED;
781876
tas_data->last_op = SJA1105_PTP_NONE;
877+
878+
INIT_LIST_HEAD(&tas_data->gating_cfg.entries);
782879
}
783880

784881
void sja1105_tas_teardown(struct dsa_switch *ds)

0 commit comments

Comments
 (0)