^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * net/sched/cls_matchll.c Match-all classifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/sch_generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/pkt_cls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct cls_mall_head {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct tcf_exts exts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct tcf_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) u32 handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) u32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int in_hw_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct tc_matchall_pcnt __percpu *pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct rcu_work rwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) bool deleting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct tcf_result *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct cls_mall_head *head = rcu_dereference_bh(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (unlikely(!head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (tc_skip_sw(head->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *res = head->res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __this_cpu_inc(head->pf->rhit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return tcf_exts_exec(skb, &head->exts, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int mall_init(struct tcf_proto *tp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static void __mall_destroy(struct cls_mall_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) tcf_exts_destroy(&head->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) tcf_exts_put_net(&head->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) free_percpu(head->pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) kfree(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void mall_destroy_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct cls_mall_head *head = container_of(to_rcu_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct cls_mall_head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) rwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) __mall_destroy(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static void mall_destroy_hw_filter(struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct cls_mall_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned long cookie,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct tc_cls_matchall_offload cls_mall = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) cls_mall.command = TC_CLSMATCHALL_DESTROY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) cls_mall.cookie = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) tc_setup_cb_destroy(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) &head->flags, &head->in_hw_count, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static int mall_replace_hw_filter(struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct cls_mall_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned long cookie,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct tc_cls_matchall_offload cls_mall = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) bool skip_sw = tc_skip_sw(head->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!cls_mall.rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) cls_mall.command = TC_CLSMATCHALL_REPLACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) cls_mall.cookie = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) kfree(cls_mall.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mall_destroy_hw_filter(tp, head, cookie, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (skip_sw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) skip_sw, &head->flags, &head->in_hw_count, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) tc_cleanup_flow_action(&cls_mall.rule->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kfree(cls_mall.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) mall_destroy_hw_filter(tp, head, cookie, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void mall_destroy(struct tcf_proto *tp, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct cls_mall_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) tcf_unbind_filter(tp, &head->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!tc_skip_hw(head->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (tcf_exts_get_net(&head->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) tcf_queue_work(&head->rwork, mall_destroy_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) __mall_destroy(head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void *mall_get(struct tcf_proto *tp, u32 handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct cls_mall_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (head && head->handle == handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int mall_set_parms(struct net *net, struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct cls_mall_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned long base, struct nlattr **tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct nlattr *est, bool ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (tb[TCA_MATCHALL_CLASSID]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) tcf_bind_filter(tp, &head->res, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int mall_change(struct net *net, struct sk_buff *in_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct tcf_proto *tp, unsigned long base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) u32 handle, struct nlattr **tca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) void **arg, bool ovr, bool rtnl_held,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct cls_mall_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct nlattr *tb[TCA_MATCHALL_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct cls_mall_head *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!tca[TCA_OPTIONS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) tca[TCA_OPTIONS], mall_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (tb[TCA_MATCHALL_FLAGS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!tc_flags_valid(flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) new = kzalloc(sizeof(*new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) goto err_exts_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) handle = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) new->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) new->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) new->pf = alloc_percpu(struct tc_matchall_pcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!new->pf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) goto err_alloc_percpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) goto err_set_parms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!tc_skip_hw(new->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) err = mall_replace_hw_filter(tp, new, (unsigned long)new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto err_replace_hw_filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!tc_in_hw(new->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *arg = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) rcu_assign_pointer(tp->root, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) err_replace_hw_filter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) err_set_parms:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) free_percpu(new->pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) err_alloc_percpu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) tcf_exts_destroy(&new->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) err_exts_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) bool rtnl_held, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct cls_mall_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) head->deleting = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) *last = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct cls_mall_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (arg->count < arg->skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) goto skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (!head || head->deleting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (arg->fn(tp, head, arg) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) arg->stop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) arg->count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) void *cb_priv, struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct cls_mall_head *head = rtnl_dereference(tp->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct tc_cls_matchall_offload cls_mall = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (tc_skip_hw(head->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!cls_mall.rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) cls_mall.command = add ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) cls_mall.cookie = (unsigned long)head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) kfree(cls_mall.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (add && tc_skip_sw(head->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) &cls_mall, cb_priv, &head->flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) &head->in_hw_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) tc_cleanup_flow_action(&cls_mall.rule->action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) kfree(cls_mall.rule);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void mall_stats_hw_filter(struct tcf_proto *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct cls_mall_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) unsigned long cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct tc_cls_matchall_offload cls_mall = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct tcf_block *block = tp->chain->block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) cls_mall.command = TC_CLSMATCHALL_STATS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) cls_mall.cookie = cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) tcf_exts_stats_update(&head->exts, cls_mall.stats.bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) cls_mall.stats.pkts, cls_mall.stats.drops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) cls_mall.stats.lastused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) cls_mall.stats.used_hw_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) cls_mall.stats.used_hw_stats_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct tc_matchall_pcnt gpf = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct cls_mall_head *head = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct nlattr *nest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!tc_skip_hw(head->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) mall_stats_hw_filter(tp, head, (unsigned long)head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) t->tcm_handle = head->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (!nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (head->res.classid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) gpf.rhit += pf->rhit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) sizeof(struct tc_matchall_pcnt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) &gpf, TCA_MATCHALL_PAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (tcf_exts_dump(skb, &head->exts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) nla_nest_end(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (tcf_exts_dump_stats(skb, &head->exts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) goto nla_put_failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) nla_put_failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) nla_nest_cancel(skb, nest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) unsigned long base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct cls_mall_head *head = fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (head && head->res.classid == classid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) __tcf_bind_filter(q, &head->res, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) __tcf_unbind_filter(q, &head->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) static struct tcf_proto_ops cls_mall_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) .kind = "matchall",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) .classify = mall_classify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) .init = mall_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .destroy = mall_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) .get = mall_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .change = mall_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) .delete = mall_delete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) .walk = mall_walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) .reoffload = mall_reoffload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .dump = mall_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) .bind_class = mall_bind_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static int __init cls_mall_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return register_tcf_proto_ops(&cls_mall_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static void __exit cls_mall_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) unregister_tcf_proto_ops(&cls_mall_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) module_init(cls_mall_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) module_exit(cls_mall_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) MODULE_DESCRIPTION("Match-all classifier");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) MODULE_LICENSE("GPL v2");