kernel: bump 5.15 to 5.15.141
[openwrt/openwrt.git] / target / linux / generic / pending-5.15 / 736-03-net-ethernet-mtk_eth_soc-improve-keeping-track-of-of.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Thu, 23 Mar 2023 10:24:11 +0100
3 Subject: [PATCH] net: ethernet: mtk_eth_soc: improve keeping track of
4 offloaded flows
5
6 Unify tracking of L2 and L3 flows. Use the generic list field in struct
7 mtk_foe_entry for tracking L2 subflows. Preparation for improving
8 flow accounting support.
9
10 Signed-off-by: Felix Fietkau <nbd@nbd.name>
11 ---
12
13 --- a/drivers/net/ethernet/mediatek/mtk_ppe.c
14 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
15 @@ -483,42 +483,43 @@ int mtk_foe_entry_set_queue(struct mtk_e
16 return 0;
17 }
18
19 +static int
20 +mtk_flow_entry_match_len(struct mtk_eth *eth, struct mtk_foe_entry *entry)
21 +{
22 + int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
23 +
24 + if (type > MTK_PPE_PKT_TYPE_IPV4_DSLITE)
25 + return offsetof(struct mtk_foe_entry, ipv6._rsv);
26 + else
27 + return offsetof(struct mtk_foe_entry, ipv4.ib2);
28 +}
29 +
30 static bool
31 mtk_flow_entry_match(struct mtk_eth *eth, struct mtk_flow_entry *entry,
32 - struct mtk_foe_entry *data)
33 + struct mtk_foe_entry *data, int len)
34 {
35 - int type, len;
36 -
37 if ((data->ib1 ^ entry->data.ib1) & MTK_FOE_IB1_UDP)
38 return false;
39
40 - type = mtk_get_ib1_pkt_type(eth, entry->data.ib1);
41 - if (type > MTK_PPE_PKT_TYPE_IPV4_DSLITE)
42 - len = offsetof(struct mtk_foe_entry, ipv6._rsv);
43 - else
44 - len = offsetof(struct mtk_foe_entry, ipv4.ib2);
45 -
46 return !memcmp(&entry->data.data, &data->data, len - 4);
47 }
48
49 static void
50 -__mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
51 +__mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
52 + bool set_state)
53 {
54 - struct hlist_head *head;
55 struct hlist_node *tmp;
56
57 if (entry->type == MTK_FLOW_TYPE_L2) {
58 rhashtable_remove_fast(&ppe->l2_flows, &entry->l2_node,
59 mtk_flow_l2_ht_params);
60
61 - head = &entry->l2_flows;
62 - hlist_for_each_entry_safe(entry, tmp, head, l2_data.list)
63 - __mtk_foe_entry_clear(ppe, entry);
64 + hlist_for_each_entry_safe(entry, tmp, &entry->l2_flows, l2_list)
65 + __mtk_foe_entry_clear(ppe, entry, set_state);
66 return;
67 }
68
69 - hlist_del_init(&entry->list);
70 - if (entry->hash != 0xffff) {
71 + if (entry->hash != 0xffff && set_state) {
72 struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, entry->hash);
73
74 hwe->ib1 &= ~MTK_FOE_IB1_STATE;
75 @@ -538,7 +539,8 @@ __mtk_foe_entry_clear(struct mtk_ppe *pp
76 if (entry->type != MTK_FLOW_TYPE_L2_SUBFLOW)
77 return;
78
79 - hlist_del_init(&entry->l2_data.list);
80 + hlist_del_init(&entry->l2_list);
81 + hlist_del_init(&entry->list);
82 kfree(entry);
83 }
84
85 @@ -554,66 +556,55 @@ static int __mtk_foe_entry_idle_time(str
86 return now - timestamp;
87 }
88
89 +static bool
90 +mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
91 +{
92 + struct mtk_foe_entry foe = {};
93 + struct mtk_foe_entry *hwe;
94 + u16 hash = entry->hash;
95 + int len;
96 +
97 + if (hash == 0xffff)
98 + return false;
99 +
100 + hwe = mtk_foe_get_entry(ppe, hash);
101 + len = mtk_flow_entry_match_len(ppe->eth, &entry->data);
102 + memcpy(&foe, hwe, len);
103 +
104 + if (!mtk_flow_entry_match(ppe->eth, entry, &foe, len) ||
105 + FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND)
106 + return false;
107 +
108 + entry->data.ib1 = foe.ib1;
109 +
110 + return true;
111 +}
112 +
113 static void
114 mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
115 {
116 u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
117 struct mtk_flow_entry *cur;
118 - struct mtk_foe_entry *hwe;
119 struct hlist_node *tmp;
120 int idle;
121
122 idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
123 - hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, l2_data.list) {
124 + hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, l2_list) {
125 int cur_idle;
126 - u32 ib1;
127 -
128 - hwe = mtk_foe_get_entry(ppe, cur->hash);
129 - ib1 = READ_ONCE(hwe->ib1);
130
131 - if (FIELD_GET(MTK_FOE_IB1_STATE, ib1) != MTK_FOE_STATE_BIND) {
132 - cur->hash = 0xffff;
133 - __mtk_foe_entry_clear(ppe, cur);
134 + if (!mtk_flow_entry_update(ppe, cur)) {
135 + __mtk_foe_entry_clear(ppe, entry, false);
136 continue;
137 }
138
139 - cur_idle = __mtk_foe_entry_idle_time(ppe, ib1);
140 + cur_idle = __mtk_foe_entry_idle_time(ppe, cur->data.ib1);
141 if (cur_idle >= idle)
142 continue;
143
144 idle = cur_idle;
145 entry->data.ib1 &= ~ib1_ts_mask;
146 - entry->data.ib1 |= hwe->ib1 & ib1_ts_mask;
147 - }
148 -}
149 -
150 -static void
151 -mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
152 -{
153 - struct mtk_foe_entry foe = {};
154 - struct mtk_foe_entry *hwe;
155 -
156 - spin_lock_bh(&ppe_lock);
157 -
158 - if (entry->type == MTK_FLOW_TYPE_L2) {
159 - mtk_flow_entry_update_l2(ppe, entry);
160 - goto out;
161 + entry->data.ib1 |= cur->data.ib1 & ib1_ts_mask;
162 }
163 -
164 - if (entry->hash == 0xffff)
165 - goto out;
166 -
167 - hwe = mtk_foe_get_entry(ppe, entry->hash);
168 - memcpy(&foe, hwe, ppe->eth->soc->foe_entry_size);
169 - if (!mtk_flow_entry_match(ppe->eth, entry, &foe)) {
170 - entry->hash = 0xffff;
171 - goto out;
172 - }
173 -
174 - entry->data.ib1 = foe.ib1;
175 -
176 -out:
177 - spin_unlock_bh(&ppe_lock);
178 }
179
180 static void
181 @@ -656,7 +647,8 @@ __mtk_foe_entry_commit(struct mtk_ppe *p
182 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
183 {
184 spin_lock_bh(&ppe_lock);
185 - __mtk_foe_entry_clear(ppe, entry);
186 + __mtk_foe_entry_clear(ppe, entry, true);
187 + hlist_del_init(&entry->list);
188 spin_unlock_bh(&ppe_lock);
189 }
190
191 @@ -703,8 +695,8 @@ mtk_foe_entry_commit_subflow(struct mtk_
192 {
193 const struct mtk_soc_data *soc = ppe->eth->soc;
194 struct mtk_flow_entry *flow_info;
195 - struct mtk_foe_entry foe = {}, *hwe;
196 struct mtk_foe_mac_info *l2;
197 + struct mtk_foe_entry *hwe;
198 u32 ib1_mask = mtk_get_ib1_pkt_type_mask(ppe->eth) | MTK_FOE_IB1_UDP;
199 int type;
200
201 @@ -712,30 +704,30 @@ mtk_foe_entry_commit_subflow(struct mtk_
202 if (!flow_info)
203 return;
204
205 - flow_info->l2_data.base_flow = entry;
206 flow_info->type = MTK_FLOW_TYPE_L2_SUBFLOW;
207 flow_info->hash = hash;
208 hlist_add_head(&flow_info->list,
209 &ppe->foe_flow[hash / soc->hash_offset]);
210 - hlist_add_head(&flow_info->l2_data.list, &entry->l2_flows);
211 + hlist_add_head(&flow_info->l2_list, &entry->l2_flows);
212
213 hwe = mtk_foe_get_entry(ppe, hash);
214 - memcpy(&foe, hwe, soc->foe_entry_size);
215 - foe.ib1 &= ib1_mask;
216 - foe.ib1 |= entry->data.ib1 & ~ib1_mask;
217 + memcpy(&flow_info->data, hwe, soc->foe_entry_size);
218 + flow_info->data.ib1 &= ib1_mask;
219 + flow_info->data.ib1 |= entry->data.ib1 & ~ib1_mask;
220
221 - l2 = mtk_foe_entry_l2(ppe->eth, &foe);
222 + l2 = mtk_foe_entry_l2(ppe->eth, &flow_info->data);
223 memcpy(l2, &entry->data.bridge.l2, sizeof(*l2));
224
225 - type = mtk_get_ib1_pkt_type(ppe->eth, foe.ib1);
226 + type = mtk_get_ib1_pkt_type(ppe->eth, flow_info->data.ib1);
227 if (type == MTK_PPE_PKT_TYPE_IPV4_HNAPT)
228 - memcpy(&foe.ipv4.new, &foe.ipv4.orig, sizeof(foe.ipv4.new));
229 + memcpy(&flow_info->data.ipv4.new, &flow_info->data.ipv4.orig,
230 + sizeof(flow_info->data.ipv4.new));
231 else if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T && l2->etype == ETH_P_IP)
232 l2->etype = ETH_P_IPV6;
233
234 - *mtk_foe_entry_ib2(ppe->eth, &foe) = entry->data.bridge.ib2;
235 + *mtk_foe_entry_ib2(ppe->eth, &flow_info->data) = entry->data.bridge.ib2;
236
237 - __mtk_foe_entry_commit(ppe, &foe, hash);
238 + __mtk_foe_entry_commit(ppe, &flow_info->data, hash);
239 }
240
241 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
242 @@ -745,9 +737,11 @@ void __mtk_ppe_check_skb(struct mtk_ppe
243 struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, hash);
244 struct mtk_flow_entry *entry;
245 struct mtk_foe_bridge key = {};
246 + struct mtk_foe_entry foe = {};
247 struct hlist_node *n;
248 struct ethhdr *eh;
249 bool found = false;
250 + int entry_len;
251 u8 *tag;
252
253 spin_lock_bh(&ppe_lock);
254 @@ -755,20 +749,14 @@ void __mtk_ppe_check_skb(struct mtk_ppe
255 if (FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) == MTK_FOE_STATE_BIND)
256 goto out;
257
258 - hlist_for_each_entry_safe(entry, n, head, list) {
259 - if (entry->type == MTK_FLOW_TYPE_L2_SUBFLOW) {
260 - if (unlikely(FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) ==
261 - MTK_FOE_STATE_BIND))
262 - continue;
263 -
264 - entry->hash = 0xffff;
265 - __mtk_foe_entry_clear(ppe, entry);
266 - continue;
267 - }
268 + entry_len = mtk_flow_entry_match_len(ppe->eth, hwe);
269 + memcpy(&foe, hwe, entry_len);
270
271 - if (found || !mtk_flow_entry_match(ppe->eth, entry, hwe)) {
272 + hlist_for_each_entry_safe(entry, n, head, list) {
273 + if (found ||
274 + !mtk_flow_entry_match(ppe->eth, entry, &foe, entry_len)) {
275 if (entry->hash != 0xffff)
276 - entry->hash = 0xffff;
277 + __mtk_foe_entry_clear(ppe, entry, false);
278 continue;
279 }
280
281 @@ -819,9 +807,17 @@ out:
282
283 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
284 {
285 - mtk_flow_entry_update(ppe, entry);
286 + int idle;
287 +
288 + spin_lock_bh(&ppe_lock);
289 + if (entry->type == MTK_FLOW_TYPE_L2)
290 + mtk_flow_entry_update_l2(ppe, entry);
291 + else
292 + mtk_flow_entry_update(ppe, entry);
293 + idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
294 + spin_unlock_bh(&ppe_lock);
295
296 - return __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
297 + return idle;
298 }
299
300 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe)
301 --- a/drivers/net/ethernet/mediatek/mtk_ppe.h
302 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
303 @@ -286,7 +286,12 @@ enum {
304
305 struct mtk_flow_entry {
306 union {
307 - struct hlist_node list;
308 + /* regular flows + L2 subflows */
309 + struct {
310 + struct hlist_node list;
311 + struct hlist_node l2_list;
312 + };
313 + /* L2 flows */
314 struct {
315 struct rhash_head l2_node;
316 struct hlist_head l2_flows;
317 @@ -296,13 +301,7 @@ struct mtk_flow_entry {
318 s8 wed_index;
319 u8 ppe_index;
320 u16 hash;
321 - union {
322 - struct mtk_foe_entry data;
323 - struct {
324 - struct mtk_flow_entry *base_flow;
325 - struct hlist_node list;
326 - } l2_data;
327 - };
328 + struct mtk_foe_entry data;
329 struct rhash_head node;
330 unsigned long cookie;
331 };