1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
6 #include <linux/if_ether.h>
7 #include <linux/rhashtable.h>
9 #include <linux/ipv6.h>
10 #include <net/flow_offload.h>
11 #include <net/pkt_cls.h>
13 #include "mtk_eth_soc.h"
16 struct mtk_flow_data {
26 struct in6_addr src_addr;
27 struct in6_addr dst_addr;
47 static const struct rhashtable_params mtk_flow_ht_params = {
48 .head_offset = offsetof(struct mtk_flow_entry, node),
49 .key_offset = offsetof(struct mtk_flow_entry, cookie),
50 .key_len = sizeof(unsigned long),
51 .automatic_shrinking = true,
55 mtk_flow_set_ipv4_addr(struct mtk_eth *eth, struct mtk_foe_entry *foe,
56 struct mtk_flow_data *data, bool egress)
58 return mtk_foe_entry_set_ipv4_tuple(eth, foe, egress,
59 data->v4.src_addr, data->src_port,
60 data->v4.dst_addr, data->dst_port);
64 mtk_flow_set_ipv6_addr(struct mtk_eth *eth, struct mtk_foe_entry *foe,
65 struct mtk_flow_data *data)
67 return mtk_foe_entry_set_ipv6_tuple(eth, foe,
68 data->v6.src_addr.s6_addr32, data->src_port,
69 data->v6.dst_addr.s6_addr32, data->dst_port);
73 mtk_flow_offload_mangle_eth(const struct flow_action_entry *act, void *eth)
75 void *dest = eth + act->mangle.offset;
76 const void *src = &act->mangle.val;
78 if (act->mangle.offset > 8)
81 if (act->mangle.mask == 0xffff) {
86 memcpy(dest, src, act->mangle.mask ? 2 : 4);
90 mtk_flow_get_wdma_info(struct net_device *dev, const u8 *addr, struct mtk_wdma_info *info)
92 struct net_device_path_stack stack;
93 struct net_device_path *path;
99 if (!IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED))
102 err = dev_fill_forward_path(dev, addr, &stack);
106 path = &stack.path[stack.num_paths - 1];
107 if (path->type != DEV_PATH_MTK_WDMA)
110 info->wdma_idx = path->mtk_wdma.wdma_idx;
111 info->queue = path->mtk_wdma.queue;
112 info->bss = path->mtk_wdma.bss;
113 info->wcid = path->mtk_wdma.wcid;
120 mtk_flow_mangle_ports(const struct flow_action_entry *act,
121 struct mtk_flow_data *data)
123 u32 val = ntohl(act->mangle.val);
125 switch (act->mangle.offset) {
127 if (act->mangle.mask == ~htonl(0xffff))
128 data->dst_port = cpu_to_be16(val);
130 data->src_port = cpu_to_be16(val >> 16);
133 data->dst_port = cpu_to_be16(val);
143 mtk_flow_mangle_ipv4(const struct flow_action_entry *act,
144 struct mtk_flow_data *data)
148 switch (act->mangle.offset) {
149 case offsetof(struct iphdr, saddr):
150 dest = &data->v4.src_addr;
152 case offsetof(struct iphdr, daddr):
153 dest = &data->v4.dst_addr;
159 memcpy(dest, &act->mangle.val, sizeof(u32));
165 mtk_flow_get_dsa_port(struct net_device **dev)
167 #if IS_ENABLED(CONFIG_NET_DSA)
170 dp = dsa_port_from_netdev(*dev);
174 if (dp->cpu_dp->tag_ops->proto != DSA_TAG_PROTO_MTK)
177 *dev = dsa_port_to_master(dp);
186 mtk_flow_set_output_device(struct mtk_eth *eth, struct mtk_foe_entry *foe,
187 struct net_device *dev, const u8 *dest_mac,
190 struct mtk_wdma_info info = {};
191 int pse_port, dsa_port;
193 if (mtk_flow_get_wdma_info(dev, dest_mac, &info) == 0) {
194 mtk_foe_entry_set_wdma(eth, foe, info.wdma_idx, info.queue,
195 info.bss, info.wcid);
196 if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
197 switch (info.wdma_idx) {
210 *wed_index = info.wdma_idx;
214 dsa_port = mtk_flow_get_dsa_port(&dev);
216 mtk_foe_entry_set_dsa(eth, foe, dsa_port);
218 if (dev == eth->netdev[0])
220 else if (dev == eth->netdev[1])
226 mtk_foe_entry_set_pse_port(eth, foe, pse_port);
232 mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f)
234 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
235 struct flow_action_entry *act;
236 struct mtk_flow_data data = {};
237 struct mtk_foe_entry foe;
238 struct net_device *odev = NULL;
239 struct mtk_flow_entry *entry;
240 int offload_type = 0;
247 if (rhashtable_lookup(ð->flow_table, &f->cookie, mtk_flow_ht_params))
250 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_META)) {
251 struct flow_match_meta match;
253 flow_rule_match_meta(rule, &match);
258 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
259 struct flow_match_control match;
261 flow_rule_match_control(rule, &match);
262 addr_type = match.key->addr_type;
267 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
268 struct flow_match_basic match;
270 flow_rule_match_basic(rule, &match);
271 l4proto = match.key->ip_proto;
278 offload_type = MTK_PPE_PKT_TYPE_BRIDGE;
279 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
280 struct flow_match_eth_addrs match;
282 flow_rule_match_eth_addrs(rule, &match);
283 memcpy(data.eth.h_dest, match.key->dst, ETH_ALEN);
284 memcpy(data.eth.h_source, match.key->src, ETH_ALEN);
289 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
290 struct flow_match_vlan match;
292 flow_rule_match_vlan(rule, &match);
294 if (match.key->vlan_tpid != cpu_to_be16(ETH_P_8021Q))
297 data.vlan_in = match.key->vlan_id;
300 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
301 offload_type = MTK_PPE_PKT_TYPE_IPV4_HNAPT;
303 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
304 offload_type = MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T;
310 flow_action_for_each(i, act, &rule->action) {
312 case FLOW_ACTION_MANGLE:
313 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
315 if (act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_ETH)
316 mtk_flow_offload_mangle_eth(act, &data.eth);
318 case FLOW_ACTION_REDIRECT:
321 case FLOW_ACTION_CSUM:
323 case FLOW_ACTION_VLAN_PUSH:
324 if (data.vlan.num == 1 ||
325 act->vlan.proto != htons(ETH_P_8021Q))
328 data.vlan.id = act->vlan.vid;
329 data.vlan.proto = act->vlan.proto;
332 case FLOW_ACTION_VLAN_POP:
334 case FLOW_ACTION_PPPOE_PUSH:
335 if (data.pppoe.num == 1)
338 data.pppoe.sid = act->pppoe.sid;
346 if (!is_valid_ether_addr(data.eth.h_source) ||
347 !is_valid_ether_addr(data.eth.h_dest))
350 err = mtk_foe_entry_prepare(eth, &foe, offload_type, l4proto, 0,
351 data.eth.h_source, data.eth.h_dest);
355 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
356 struct flow_match_ports ports;
358 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
361 flow_rule_match_ports(rule, &ports);
362 data.src_port = ports.key->src;
363 data.dst_port = ports.key->dst;
364 } else if (offload_type != MTK_PPE_PKT_TYPE_BRIDGE) {
368 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
369 struct flow_match_ipv4_addrs addrs;
371 flow_rule_match_ipv4_addrs(rule, &addrs);
373 data.v4.src_addr = addrs.key->src;
374 data.v4.dst_addr = addrs.key->dst;
376 mtk_flow_set_ipv4_addr(eth, &foe, &data, false);
379 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
380 struct flow_match_ipv6_addrs addrs;
382 flow_rule_match_ipv6_addrs(rule, &addrs);
384 data.v6.src_addr = addrs.key->src;
385 data.v6.dst_addr = addrs.key->dst;
387 mtk_flow_set_ipv6_addr(eth, &foe, &data);
390 flow_action_for_each(i, act, &rule->action) {
391 if (act->id != FLOW_ACTION_MANGLE)
394 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
397 switch (act->mangle.htype) {
398 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
399 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
400 err = mtk_flow_mangle_ports(act, &data);
402 case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
403 err = mtk_flow_mangle_ipv4(act, &data);
405 case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
406 /* handled earlier */
416 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
417 err = mtk_flow_set_ipv4_addr(eth, &foe, &data, true);
422 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
423 foe.bridge.vlan = data.vlan_in;
425 if (data.vlan.num == 1) {
426 if (data.vlan.proto != htons(ETH_P_8021Q))
429 mtk_foe_entry_set_vlan(eth, &foe, data.vlan.id);
431 if (data.pppoe.num == 1)
432 mtk_foe_entry_set_pppoe(eth, &foe, data.pppoe.sid);
434 err = mtk_flow_set_output_device(eth, &foe, odev, data.eth.h_dest,
439 if (wed_index >= 0 && (err = mtk_wed_flow_add(wed_index)) < 0)
442 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
446 entry->cookie = f->cookie;
447 memcpy(&entry->data, &foe, sizeof(entry->data));
448 entry->wed_index = wed_index;
450 err = mtk_foe_entry_commit(eth->ppe[entry->ppe_index], entry);
454 err = rhashtable_insert_fast(ð->flow_table, &entry->node,
462 mtk_foe_entry_clear(eth->ppe[entry->ppe_index], entry);
466 mtk_wed_flow_remove(wed_index);
471 mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f)
473 struct mtk_flow_entry *entry;
475 entry = rhashtable_lookup(ð->flow_table, &f->cookie,
480 mtk_foe_entry_clear(eth->ppe[entry->ppe_index], entry);
481 rhashtable_remove_fast(ð->flow_table, &entry->node,
483 if (entry->wed_index >= 0)
484 mtk_wed_flow_remove(entry->wed_index);
491 mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
493 struct mtk_flow_entry *entry;
496 entry = rhashtable_lookup(ð->flow_table, &f->cookie,
501 idle = mtk_foe_entry_idle_time(eth->ppe[entry->ppe_index], entry);
502 f->stats.lastused = jiffies - idle * HZ;
507 static DEFINE_MUTEX(mtk_flow_offload_mutex);
510 mtk_eth_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
512 struct flow_cls_offload *cls = type_data;
513 struct net_device *dev = cb_priv;
514 struct mtk_mac *mac = netdev_priv(dev);
515 struct mtk_eth *eth = mac->hw;
518 if (!tc_can_offload(dev))
521 if (type != TC_SETUP_CLSFLOWER)
524 mutex_lock(&mtk_flow_offload_mutex);
525 switch (cls->command) {
526 case FLOW_CLS_REPLACE:
527 err = mtk_flow_offload_replace(eth, cls);
529 case FLOW_CLS_DESTROY:
530 err = mtk_flow_offload_destroy(eth, cls);
533 err = mtk_flow_offload_stats(eth, cls);
539 mutex_unlock(&mtk_flow_offload_mutex);
545 mtk_eth_setup_tc_block(struct net_device *dev, struct flow_block_offload *f)
547 struct mtk_mac *mac = netdev_priv(dev);
548 struct mtk_eth *eth = mac->hw;
549 static LIST_HEAD(block_cb_list);
550 struct flow_block_cb *block_cb;
553 if (!eth->soc->offload_version)
556 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
559 cb = mtk_eth_setup_tc_block_cb;
560 f->driver_block_list = &block_cb_list;
562 switch (f->command) {
563 case FLOW_BLOCK_BIND:
564 block_cb = flow_block_cb_lookup(f->block, cb, dev);
566 flow_block_cb_incref(block_cb);
569 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
570 if (IS_ERR(block_cb))
571 return PTR_ERR(block_cb);
573 flow_block_cb_add(block_cb, f);
574 list_add_tail(&block_cb->driver_list, &block_cb_list);
576 case FLOW_BLOCK_UNBIND:
577 block_cb = flow_block_cb_lookup(f->block, cb, dev);
581 if (flow_block_cb_decref(block_cb)) {
582 flow_block_cb_remove(block_cb, f);
583 list_del(&block_cb->driver_list);
591 int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
597 return mtk_eth_setup_tc_block(dev, type_data);
603 int mtk_eth_offload_init(struct mtk_eth *eth)
605 return rhashtable_init(ð->flow_table, &mtk_flow_ht_params);