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
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.
10 Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 --- a/drivers/net/ethernet/mediatek/mtk_ppe.c
14 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
15 @@ -460,42 +460,43 @@ int mtk_foe_entry_set_queue(struct mtk_e
20 +mtk_flow_entry_match_len(struct mtk_eth *eth, struct mtk_foe_entry *entry)
22 + int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
24 + if (type > MTK_PPE_PKT_TYPE_IPV4_DSLITE)
25 + return offsetof(struct mtk_foe_entry, ipv6._rsv);
27 + return offsetof(struct mtk_foe_entry, ipv4.ib2);
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)
37 if ((data->ib1 ^ entry->data.ib1) & MTK_FOE_IB1_UDP)
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);
44 - len = offsetof(struct mtk_foe_entry, ipv4.ib2);
46 return !memcmp(&entry->data.data, &data->data, len - 4);
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,
54 - struct hlist_head *head;
55 struct hlist_node *tmp;
57 if (entry->type == MTK_FLOW_TYPE_L2) {
58 rhashtable_remove_fast(&ppe->l2_flows, &entry->l2_node,
59 mtk_flow_l2_ht_params);
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);
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);
74 hwe->ib1 &= ~MTK_FOE_IB1_STATE;
75 @@ -515,7 +516,8 @@ __mtk_foe_entry_clear(struct mtk_ppe *pp
76 if (entry->type != MTK_FLOW_TYPE_L2_SUBFLOW)
79 - hlist_del_init(&entry->l2_data.list);
80 + hlist_del_init(&entry->l2_list);
81 + hlist_del_init(&entry->list);
85 @@ -531,66 +533,55 @@ static int __mtk_foe_entry_idle_time(str
86 return now - timestamp;
90 +mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
92 + struct mtk_foe_entry foe = {};
93 + struct mtk_foe_entry *hwe;
94 + u16 hash = entry->hash;
100 + hwe = mtk_foe_get_entry(ppe, hash);
101 + len = mtk_flow_entry_match_len(ppe->eth, &entry->data);
102 + memcpy(&foe, hwe, len);
104 + if (!mtk_flow_entry_match(ppe->eth, entry, &foe, len) ||
105 + FIELD_GET(MTK_FOE_IB1_STATE, foe.ib1) != MTK_FOE_STATE_BIND)
108 + entry->data.ib1 = foe.ib1;
114 mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
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;
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) {
128 - hwe = mtk_foe_get_entry(ppe, cur->hash);
129 - ib1 = READ_ONCE(hwe->ib1);
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);
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)
145 entry->data.ib1 &= ~ib1_ts_mask;
146 - entry->data.ib1 |= hwe->ib1 & ib1_ts_mask;
151 -mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
153 - struct mtk_foe_entry foe = {};
154 - struct mtk_foe_entry *hwe;
156 - spin_lock_bh(&ppe_lock);
158 - if (entry->type == MTK_FLOW_TYPE_L2) {
159 - mtk_flow_entry_update_l2(ppe, entry);
161 + entry->data.ib1 |= cur->data.ib1 & ib1_ts_mask;
164 - if (entry->hash == 0xffff)
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;
174 - entry->data.ib1 = foe.ib1;
177 - spin_unlock_bh(&ppe_lock);
181 @@ -627,7 +618,8 @@ __mtk_foe_entry_commit(struct mtk_ppe *p
182 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
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);
191 @@ -674,8 +666,8 @@ mtk_foe_entry_commit_subflow(struct mtk_
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;
201 @@ -683,30 +675,30 @@ mtk_foe_entry_commit_subflow(struct mtk_
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);
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;
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));
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;
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;
237 - __mtk_foe_entry_commit(ppe, &foe, hash);
238 + __mtk_foe_entry_commit(ppe, &flow_info->data, hash);
241 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
242 @@ -716,9 +708,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;
253 spin_lock_bh(&ppe_lock);
254 @@ -726,20 +720,14 @@ void __mtk_ppe_check_skb(struct mtk_ppe
255 if (FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) == MTK_FOE_STATE_BIND)
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))
264 - entry->hash = 0xffff;
265 - __mtk_foe_entry_clear(ppe, entry);
268 + entry_len = mtk_flow_entry_match_len(ppe->eth, hwe);
269 + memcpy(&foe, hwe, entry_len);
271 - if (found || !mtk_flow_entry_match(ppe->eth, entry, hwe)) {
272 + hlist_for_each_entry_safe(entry, n, head, list) {
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);
281 @@ -790,9 +778,17 @@ out:
283 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
285 - mtk_flow_entry_update(ppe, entry);
288 + spin_lock_bh(&ppe_lock);
289 + if (entry->type == MTK_FLOW_TYPE_L2)
290 + mtk_flow_entry_update_l2(ppe, entry);
292 + mtk_flow_entry_update(ppe, entry);
293 + idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
294 + spin_unlock_bh(&ppe_lock);
296 - return __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
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 @@ -265,7 +265,12 @@ enum {
305 struct mtk_flow_entry {
307 - struct hlist_node list;
308 + /* regular flows + L2 subflows */
310 + struct hlist_node list;
311 + struct hlist_node l2_list;
315 struct rhash_head l2_node;
316 struct hlist_head l2_flows;
317 @@ -275,13 +280,7 @@ struct mtk_flow_entry {
322 - struct mtk_foe_entry data;
324 - struct mtk_flow_entry *base_flow;
325 - struct hlist_node list;
328 + struct mtk_foe_entry data;
329 struct rhash_head node;
330 unsigned long cookie;