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
|
From: Felix Fietkau <nbd@nbd.name>
Date: Thu, 23 Mar 2023 11:05:22 +0100
Subject: [PATCH] net: ethernet: mediatek: fix ppe flow accounting for L2
flows
For L2 flows, the packet/byte counters should report the sum of the
counters of their subflows, both current and expired.
In order to make this work, change the way that accounting data is tracked.
Reset counters when a flow enters bind. Once it expires (or enters unbind),
store the last counter value in struct mtk_flow_entry.
Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
--- a/drivers/net/ethernet/mediatek/mtk_ppe.c
+++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
@@ -80,9 +80,9 @@ static int mtk_ppe_mib_wait_busy(struct
int ret;
u32 val;
- ret = readl_poll_timeout(ppe->base + MTK_PPE_MIB_SER_CR, val,
- !(val & MTK_PPE_MIB_SER_CR_ST),
- 20, MTK_PPE_WAIT_TIMEOUT_US);
+ ret = readl_poll_timeout_atomic(ppe->base + MTK_PPE_MIB_SER_CR, val,
+ !(val & MTK_PPE_MIB_SER_CR_ST),
+ 20, MTK_PPE_WAIT_TIMEOUT_US);
if (ret)
dev_err(ppe->dev, "MIB table busy");
@@ -90,18 +90,32 @@ static int mtk_ppe_mib_wait_busy(struct
return ret;
}
-static int mtk_mib_entry_read(struct mtk_ppe *ppe, u16 index, u64 *bytes, u64 *packets)
+static inline struct mtk_foe_accounting *
+mtk_ppe_acct_data(struct mtk_ppe *ppe, u16 index)
+{
+ if (!ppe->acct_table)
+ return NULL;
+
+ return ppe->acct_table + index * sizeof(struct mtk_foe_accounting);
+}
+
+struct mtk_foe_accounting *mtk_ppe_mib_entry_read(struct mtk_ppe *ppe, u16 index)
{
u32 byte_cnt_low, byte_cnt_high, pkt_cnt_low, pkt_cnt_high;
u32 val, cnt_r0, cnt_r1, cnt_r2;
+ struct mtk_foe_accounting *acct;
int ret;
val = FIELD_PREP(MTK_PPE_MIB_SER_CR_ADDR, index) | MTK_PPE_MIB_SER_CR_ST;
ppe_w32(ppe, MTK_PPE_MIB_SER_CR, val);
+ acct = mtk_ppe_acct_data(ppe, index);
+ if (!acct)
+ return NULL;
+
ret = mtk_ppe_mib_wait_busy(ppe);
if (ret)
- return ret;
+ return acct;
cnt_r0 = readl(ppe->base + MTK_PPE_MIB_SER_R0);
cnt_r1 = readl(ppe->base + MTK_PPE_MIB_SER_R1);
@@ -111,10 +125,11 @@ static int mtk_mib_entry_read(struct mtk
byte_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R1_BYTE_CNT_HIGH, cnt_r1);
pkt_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R1_PKT_CNT_LOW, cnt_r1);
pkt_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R2_PKT_CNT_HIGH, cnt_r2);
- *bytes = ((u64)byte_cnt_high << 32) | byte_cnt_low;
- *packets = (pkt_cnt_high << 16) | pkt_cnt_low;
- return 0;
+ acct->bytes += ((u64)byte_cnt_high << 32) | byte_cnt_low;
+ acct->packets += (pkt_cnt_high << 16) | pkt_cnt_low;
+
+ return acct;
}
static void mtk_ppe_cache_clear(struct mtk_ppe *ppe)
@@ -510,13 +525,6 @@ __mtk_foe_entry_clear(struct mtk_ppe *pp
hwe->ib1 &= ~MTK_FOE_IB1_STATE;
hwe->ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_INVALID);
dma_wmb();
- if (ppe->accounting) {
- struct mtk_foe_accounting *acct;
-
- acct = ppe->acct_table + entry->hash * sizeof(*acct);
- acct->packets = 0;
- acct->bytes = 0;
- }
}
entry->hash = 0xffff;
@@ -540,8 +548,10 @@ static int __mtk_foe_entry_idle_time(str
}
static bool
-mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
+mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
+ u64 *packets, u64 *bytes)
{
+ struct mtk_foe_accounting *acct;
struct mtk_foe_entry foe = {};
struct mtk_foe_entry *hwe;
u16 hash = entry->hash;
@@ -555,16 +565,29 @@ mtk_flow_entry_update(struct mtk_ppe *pp
memcpy(&foe, hwe, len);
if (!mtk_flow_entry_match(ppe->eth, entry, &foe, len) ||
- FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND)
+ FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND) {
+ acct = mtk_ppe_acct_data(ppe, hash);
+ if (acct) {
+ entry->packets += acct->packets;
+ entry->bytes += acct->bytes;
+ }
+
return false;
+ }
entry->data.ib1 = foe.ib1;
+ acct = mtk_ppe_mib_entry_read(ppe, hash);
+ if (acct) {
+ *packets += acct->packets;
+ *bytes += acct->bytes;
+ }
return true;
}
static void
-mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
+mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
+ u64 *packets, u64 *bytes)
{
u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
struct mtk_flow_entry *cur;
@@ -575,7 +598,9 @@ mtk_flow_entry_update_l2(struct mtk_ppe
hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, list) {
int cur_idle;
- if (!mtk_flow_entry_update(ppe, cur)) {
+ if (!mtk_flow_entry_update(ppe, cur, packets, bytes)) {
+ entry->packets += cur->packets;
+ entry->bytes += cur->bytes;
__mtk_foe_entry_clear(ppe, entry, false);
continue;
}
@@ -590,10 +615,31 @@ mtk_flow_entry_update_l2(struct mtk_ppe
}
}
+void mtk_foe_entry_get_stats(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
+ int *idle, u64 *packets, u64 *bytes)
+{
+ *packets = 0;
+ *bytes = 0;
+
+ spin_lock_bh(&ppe_lock);
+
+ if (entry->type == MTK_FLOW_TYPE_L2)
+ mtk_flow_entry_update_l2(ppe, entry, packets, bytes);
+ else
+ mtk_flow_entry_update(ppe, entry, packets, bytes);
+
+ *packets += entry->packets;
+ *bytes += entry->bytes;
+ *idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
+
+ spin_unlock_bh(&ppe_lock);
+}
+
static void
__mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
u16 hash)
{
+ struct mtk_foe_accounting *acct;
struct mtk_eth *eth = ppe->eth;
u16 timestamp = mtk_eth_timestamp(eth);
struct mtk_foe_entry *hwe;
@@ -618,6 +664,12 @@ __mtk_foe_entry_commit(struct mtk_ppe *p
dma_wmb();
+ acct = mtk_ppe_mib_entry_read(ppe, hash);
+ if (acct) {
+ acct->packets = 0;
+ acct->bytes = 0;
+ }
+
mtk_ppe_cache_clear(ppe);
}
@@ -782,21 +834,6 @@ out:
spin_unlock_bh(&ppe_lock);
}
-int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
-{
- int idle;
-
- spin_lock_bh(&ppe_lock);
- if (entry->type == MTK_FLOW_TYPE_L2)
- mtk_flow_entry_update_l2(ppe, entry);
- else
- mtk_flow_entry_update(ppe, entry);
- idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
- spin_unlock_bh(&ppe_lock);
-
- return idle;
-}
-
int mtk_ppe_prepare_reset(struct mtk_ppe *ppe)
{
if (!ppe)
@@ -824,32 +861,6 @@ int mtk_ppe_prepare_reset(struct mtk_ppe
return mtk_ppe_wait_busy(ppe);
}
-struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
- struct mtk_foe_accounting *diff)
-{
- struct mtk_foe_accounting *acct;
- int size = sizeof(struct mtk_foe_accounting);
- u64 bytes, packets;
-
- if (!ppe->accounting)
- return NULL;
-
- if (mtk_mib_entry_read(ppe, index, &bytes, &packets))
- return NULL;
-
- acct = ppe->acct_table + index * size;
-
- acct->bytes += bytes;
- acct->packets += packets;
-
- if (diff) {
- diff->bytes = bytes;
- diff->packets = packets;
- }
-
- return acct;
-}
-
struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index)
{
bool accounting = eth->soc->has_accounting;
--- a/drivers/net/ethernet/mediatek/mtk_ppe.h
+++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
@@ -278,6 +278,8 @@ struct mtk_flow_entry {
struct mtk_foe_entry data;
struct rhash_head node;
unsigned long cookie;
+ u64 packets;
+ u64 bytes;
};
struct mtk_mib_entry {
@@ -320,6 +322,7 @@ struct mtk_ppe *mtk_ppe_init(struct mtk_
void mtk_ppe_start(struct mtk_ppe *ppe);
int mtk_ppe_stop(struct mtk_ppe *ppe);
int mtk_ppe_prepare_reset(struct mtk_ppe *ppe);
+struct mtk_foe_accounting *mtk_ppe_mib_entry_read(struct mtk_ppe *ppe, u16 index);
void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash);
@@ -368,9 +371,8 @@ int mtk_foe_entry_set_queue(struct mtk_e
unsigned int queue);
int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
-int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
int mtk_ppe_debugfs_init(struct mtk_ppe *ppe, int index);
-struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
- struct mtk_foe_accounting *diff);
+void mtk_foe_entry_get_stats(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
+ int *idle, u64 *packets, u64 *bytes);
#endif
--- a/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
+++ b/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
@@ -96,7 +96,7 @@ mtk_ppe_debugfs_foe_show(struct seq_file
if (bind && state != MTK_FOE_STATE_BIND)
continue;
- acct = mtk_foe_entry_get_mib(ppe, i, NULL);
+ acct = mtk_ppe_mib_entry_read(ppe, i);
type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
seq_printf(m, "%05x %s %7s", i,
--- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
+++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
@@ -499,24 +499,17 @@ static int
mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
{
struct mtk_flow_entry *entry;
- struct mtk_foe_accounting diff;
- u32 idle;
+ int idle;
entry = rhashtable_lookup(ð->flow_table, &f->cookie,
mtk_flow_ht_params);
if (!entry)
return -ENOENT;
- idle = mtk_foe_entry_idle_time(eth->ppe[entry->ppe_index], entry);
+ mtk_foe_entry_get_stats(eth->ppe[entry->ppe_index], entry, &idle,
+ &f->stats.pkts, &f->stats.bytes);
f->stats.lastused = jiffies - idle * HZ;
- if (entry->hash != 0xFFFF &&
- mtk_foe_entry_get_mib(eth->ppe[entry->ppe_index], entry->hash,
- &diff)) {
- f->stats.pkts += diff.packets;
- f->stats.bytes += diff.bytes;
- }
-
return 0;
}
|