736-03-net-ethernet-mtk_eth_soc-improve-keeping-track-of-of.patch 9.3 KB

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