1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Wed, 24 Mar 2021 02:30:54 +0100
3 Subject: [PATCH] net: ethernet: mtk_eth_soc: add flow offloading support
5 This adds support for offloading IPv4 routed flows, including SNAT/DNAT,
6 one VLAN, PPPoE and DSA.
8 Signed-off-by: Felix Fietkau <nbd@nbd.name>
9 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 create mode 100644 drivers/net/ethernet/mediatek/mtk_ppe_offload.c
13 --- a/drivers/net/ethernet/mediatek/Makefile
14 +++ b/drivers/net/ethernet/mediatek/Makefile
18 obj-$(CONFIG_NET_MEDIATEK_SOC) += mtk_eth.o
19 -mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o mtk_ppe.o mtk_ppe_debugfs.o
20 +mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o mtk_ppe.o mtk_ppe_debugfs.o mtk_ppe_offload.o
21 obj-$(CONFIG_NET_MEDIATEK_STAR_EMAC) += mtk_star_emac.o
22 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
23 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
24 @@ -2858,6 +2858,7 @@ static const struct net_device_ops mtk_n
25 #ifdef CONFIG_NET_POLL_CONTROLLER
26 .ndo_poll_controller = mtk_poll_controller,
28 + .ndo_setup_tc = mtk_eth_setup_tc,
31 static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
32 @@ -3116,6 +3117,10 @@ static int mtk_probe(struct platform_dev
33 eth->base + MTK_ETH_PPE_BASE, 2);
37 + err = mtk_eth_offload_init(eth);
42 for (i = 0; i < MTK_MAX_DEVS; i++) {
43 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
44 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
46 #include <linux/u64_stats_sync.h>
47 #include <linux/refcount.h>
48 #include <linux/phylink.h>
49 +#include <linux/rhashtable.h>
52 #define MTK_QDMA_PAGE_SIZE 2048
54 NETIF_F_HW_VLAN_CTAG_RX | \
55 NETIF_F_SG | NETIF_F_TSO | \
58 + NETIF_F_IPV6_CSUM |\
60 #define MTK_HW_FEATURES_MT7628 (NETIF_F_SG | NETIF_F_RXCSUM)
61 #define NEXT_DESP_IDX(X, Y) (((X) + 1) & ((Y) - 1))
63 @@ -929,6 +931,7 @@ struct mtk_eth {
67 + struct rhashtable flow_table;
70 /* struct mtk_mac - the structure that holds the info about the MACs of the
71 @@ -973,4 +976,9 @@ int mtk_gmac_sgmii_path_setup(struct mtk
72 int mtk_gmac_gephy_path_setup(struct mtk_eth *eth, int mac_id);
73 int mtk_gmac_rgmii_path_setup(struct mtk_eth *eth, int mac_id);
75 +int mtk_eth_offload_init(struct mtk_eth *eth);
76 +int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
80 #endif /* MTK_ETH_H */
82 +++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
84 +// SPDX-License-Identifier: GPL-2.0-only
86 + * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
89 +#include <linux/if_ether.h>
90 +#include <linux/rhashtable.h>
91 +#include <linux/if_ether.h>
92 +#include <linux/ip.h>
93 +#include <net/flow_offload.h>
94 +#include <net/pkt_cls.h>
96 +#include "mtk_eth_soc.h"
98 +struct mtk_flow_data {
122 +struct mtk_flow_entry {
123 + struct rhash_head node;
124 + unsigned long cookie;
128 +static const struct rhashtable_params mtk_flow_ht_params = {
129 + .head_offset = offsetof(struct mtk_flow_entry, node),
130 + .head_offset = offsetof(struct mtk_flow_entry, cookie),
131 + .key_len = sizeof(unsigned long),
132 + .automatic_shrinking = true,
136 +mtk_eth_timestamp(struct mtk_eth *eth)
138 + return mtk_r32(eth, 0x0010) & MTK_FOE_IB1_BIND_TIMESTAMP;
142 +mtk_flow_set_ipv4_addr(struct mtk_foe_entry *foe, struct mtk_flow_data *data,
145 + return mtk_foe_entry_set_ipv4_tuple(foe, egress,
146 + data->v4.src_addr, data->src_port,
147 + data->v4.dst_addr, data->dst_port);
151 +mtk_flow_offload_mangle_eth(const struct flow_action_entry *act, void *eth)
153 + void *dest = eth + act->mangle.offset;
154 + const void *src = &act->mangle.val;
156 + if (act->mangle.offset > 8)
159 + if (act->mangle.mask == 0xffff) {
164 + memcpy(dest, src, act->mangle.mask ? 2 : 4);
169 +mtk_flow_mangle_ports(const struct flow_action_entry *act,
170 + struct mtk_flow_data *data)
172 + u32 val = ntohl(act->mangle.val);
174 + switch (act->mangle.offset) {
176 + if (act->mangle.mask == ~htonl(0xffff))
177 + data->dst_port = cpu_to_be16(val);
179 + data->src_port = cpu_to_be16(val >> 16);
182 + data->dst_port = cpu_to_be16(val);
192 +mtk_flow_mangle_ipv4(const struct flow_action_entry *act,
193 + struct mtk_flow_data *data)
197 + switch (act->mangle.offset) {
198 + case offsetof(struct iphdr, saddr):
199 + dest = &data->v4.src_addr;
201 + case offsetof(struct iphdr, daddr):
202 + dest = &data->v4.dst_addr;
208 + memcpy(dest, &act->mangle.val, sizeof(u32));
214 +mtk_flow_get_dsa_port(struct net_device **dev)
216 +#if IS_ENABLED(CONFIG_NET_DSA)
217 + struct dsa_port *dp;
219 + dp = dsa_port_from_netdev(*dev);
223 + if (dp->cpu_dp->tag_ops->proto != DSA_TAG_PROTO_MTK)
226 + *dev = dp->cpu_dp->master;
235 +mtk_flow_set_output_device(struct mtk_eth *eth, struct mtk_foe_entry *foe,
236 + struct net_device *dev)
238 + int pse_port, dsa_port;
240 + dsa_port = mtk_flow_get_dsa_port(&dev);
242 + mtk_foe_entry_set_dsa(foe, dsa_port);
244 + if (dev == eth->netdev[0])
246 + else if (dev == eth->netdev[1])
249 + return -EOPNOTSUPP;
251 + mtk_foe_entry_set_pse_port(foe, pse_port);
257 +mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f)
259 + struct flow_rule *rule = flow_cls_offload_flow_rule(f);
260 + struct flow_action_entry *act;
261 + struct mtk_flow_data data = {};
262 + struct mtk_foe_entry foe;
263 + struct net_device *odev = NULL;
264 + struct mtk_flow_entry *entry;
265 + int offload_type = 0;
273 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_META)) {
274 + struct flow_match_meta match;
276 + flow_rule_match_meta(rule, &match);
278 + return -EOPNOTSUPP;
281 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
282 + struct flow_match_control match;
284 + flow_rule_match_control(rule, &match);
285 + addr_type = match.key->addr_type;
287 + return -EOPNOTSUPP;
290 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
291 + struct flow_match_basic match;
293 + flow_rule_match_basic(rule, &match);
294 + l4proto = match.key->ip_proto;
296 + return -EOPNOTSUPP;
299 + flow_action_for_each(i, act, &rule->action) {
301 + case FLOW_ACTION_MANGLE:
302 + if (act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_ETH)
303 + mtk_flow_offload_mangle_eth(act, &data.eth);
305 + case FLOW_ACTION_REDIRECT:
308 + case FLOW_ACTION_CSUM:
310 + case FLOW_ACTION_VLAN_PUSH:
311 + if (data.vlan.num == 1 ||
312 + act->vlan.proto != htons(ETH_P_8021Q))
313 + return -EOPNOTSUPP;
315 + data.vlan.id = act->vlan.vid;
316 + data.vlan.proto = act->vlan.proto;
319 + case FLOW_ACTION_PPPOE_PUSH:
320 + if (data.pppoe.num == 1)
321 + return -EOPNOTSUPP;
323 + data.pppoe.sid = act->pppoe.sid;
327 + return -EOPNOTSUPP;
331 + switch (addr_type) {
332 + case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
333 + offload_type = MTK_PPE_PKT_TYPE_IPV4_HNAPT;
336 + return -EOPNOTSUPP;
339 + if (!is_valid_ether_addr(data.eth.h_source) ||
340 + !is_valid_ether_addr(data.eth.h_dest))
343 + err = mtk_foe_entry_prepare(&foe, offload_type, l4proto, 0,
349 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
350 + struct flow_match_ports ports;
352 + flow_rule_match_ports(rule, &ports);
353 + data.src_port = ports.key->src;
354 + data.dst_port = ports.key->dst;
356 + return -EOPNOTSUPP;
359 + if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
360 + struct flow_match_ipv4_addrs addrs;
362 + flow_rule_match_ipv4_addrs(rule, &addrs);
364 + data.v4.src_addr = addrs.key->src;
365 + data.v4.dst_addr = addrs.key->dst;
367 + mtk_flow_set_ipv4_addr(&foe, &data, false);
370 + flow_action_for_each(i, act, &rule->action) {
371 + if (act->id != FLOW_ACTION_MANGLE)
374 + switch (act->mangle.htype) {
375 + case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
376 + case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
377 + err = mtk_flow_mangle_ports(act, &data);
379 + case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
380 + err = mtk_flow_mangle_ipv4(act, &data);
382 + case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
383 + /* handled earlier */
386 + return -EOPNOTSUPP;
393 + if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
394 + err = mtk_flow_set_ipv4_addr(&foe, &data, true);
399 + if (data.vlan.num == 1) {
400 + if (data.vlan.proto != htons(ETH_P_8021Q))
401 + return -EOPNOTSUPP;
403 + mtk_foe_entry_set_vlan(&foe, data.vlan.id);
405 + if (data.pppoe.num == 1)
406 + mtk_foe_entry_set_pppoe(&foe, data.pppoe.sid);
408 + err = mtk_flow_set_output_device(eth, &foe, odev);
412 + entry = kzalloc(sizeof(*entry), GFP_KERNEL);
416 + entry->cookie = f->cookie;
417 + timestamp = mtk_eth_timestamp(eth);
418 + hash = mtk_foe_entry_commit(ð->ppe, &foe, timestamp);
424 + entry->hash = hash;
425 + err = rhashtable_insert_fast(ð->flow_table, &entry->node,
426 + mtk_flow_ht_params);
432 + mtk_foe_entry_clear(ð->ppe, hash);
439 +mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f)
441 + struct mtk_flow_entry *entry;
443 + entry = rhashtable_lookup(ð->flow_table, &f->cookie,
444 + mtk_flow_ht_params);
448 + mtk_foe_entry_clear(ð->ppe, entry->hash);
449 + rhashtable_remove_fast(ð->flow_table, &entry->node,
450 + mtk_flow_ht_params);
457 +mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
459 + struct mtk_flow_entry *entry;
463 + entry = rhashtable_lookup(ð->flow_table, &f->cookie,
464 + mtk_flow_ht_params);
468 + timestamp = mtk_foe_entry_timestamp(ð->ppe, entry->hash);
472 + idle = mtk_eth_timestamp(eth) - timestamp;
473 + f->stats.lastused = jiffies - idle * HZ;
479 +mtk_eth_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
481 + struct flow_cls_offload *cls = type_data;
482 + struct net_device *dev = cb_priv;
483 + struct mtk_mac *mac = netdev_priv(dev);
484 + struct mtk_eth *eth = mac->hw;
486 + if (!tc_can_offload(dev))
487 + return -EOPNOTSUPP;
489 + if (type != TC_SETUP_CLSFLOWER)
490 + return -EOPNOTSUPP;
492 + switch (cls->command) {
493 + case FLOW_CLS_REPLACE:
494 + return mtk_flow_offload_replace(eth, cls);
495 + case FLOW_CLS_DESTROY:
496 + return mtk_flow_offload_destroy(eth, cls);
497 + case FLOW_CLS_STATS:
498 + return mtk_flow_offload_stats(eth, cls);
500 + return -EOPNOTSUPP;
507 +mtk_eth_setup_tc_block(struct net_device *dev, struct flow_block_offload *f)
509 + struct mtk_mac *mac = netdev_priv(dev);
510 + struct mtk_eth *eth = mac->hw;
511 + static LIST_HEAD(block_cb_list);
512 + struct flow_block_cb *block_cb;
513 + flow_setup_cb_t *cb;
515 + if (!eth->ppe.foe_table)
516 + return -EOPNOTSUPP;
518 + if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
519 + return -EOPNOTSUPP;
521 + cb = mtk_eth_setup_tc_block_cb;
522 + f->driver_block_list = &block_cb_list;
524 + switch (f->command) {
525 + case FLOW_BLOCK_BIND:
526 + block_cb = flow_block_cb_lookup(f->block, cb, dev);
528 + flow_block_cb_incref(block_cb);
531 + block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
532 + if (IS_ERR(block_cb))
533 + return PTR_ERR(block_cb);
535 + flow_block_cb_add(block_cb, f);
536 + list_add_tail(&block_cb->driver_list, &block_cb_list);
538 + case FLOW_BLOCK_UNBIND:
539 + block_cb = flow_block_cb_lookup(f->block, cb, dev);
543 + if (flow_block_cb_decref(block_cb)) {
544 + flow_block_cb_remove(block_cb, f);
545 + list_del(&block_cb->driver_list);
549 + return -EOPNOTSUPP;
553 +int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
556 + if (type == TC_SETUP_FT)
557 + return mtk_eth_setup_tc_block(dev, type_data);
559 + return -EOPNOTSUPP;
562 +int mtk_eth_offload_init(struct mtk_eth *eth)
564 + if (!eth->ppe.foe_table)
567 + return rhashtable_init(ð->flow_table, &mtk_flow_ht_params);