1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright 2020 NXP */
4 #include <linux/module.h>
5 #include <linux/types.h>
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/errno.h>
9 #include <linux/skbuff.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <net/act_api.h>
14 #include <net/netlink.h>
15 #include <net/pkt_cls.h>
16 #include <net/tc_act/tc_gate.h>
17 #include <net/tc_wrapper.h>
19 static struct tc_action_ops act_gate_ops;
21 static ktime_t gate_get_time(struct tcf_gate *gact)
23 ktime_t mono = ktime_get();
25 switch (gact->tk_offset) {
29 return ktime_mono_to_any(mono, gact->tk_offset);
35 static void gate_get_start_time(struct tcf_gate *gact, ktime_t *start)
37 struct tcf_gate_params *param = &gact->param;
38 ktime_t now, base, cycle;
41 base = ns_to_ktime(param->tcfg_basetime);
42 now = gate_get_time(gact);
44 if (ktime_after(base, now)) {
49 cycle = param->tcfg_cycletime;
51 n = div64_u64(ktime_sub_ns(now, base), cycle);
52 *start = ktime_add_ns(base, (n + 1) * cycle);
55 static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
59 expires = hrtimer_get_expires(&gact->hitimer);
63 start = min_t(ktime_t, start, expires);
65 hrtimer_start(&gact->hitimer, start, HRTIMER_MODE_ABS_SOFT);
68 static enum hrtimer_restart gate_timer_func(struct hrtimer *timer)
70 struct tcf_gate *gact = container_of(timer, struct tcf_gate,
72 struct tcf_gate_params *p = &gact->param;
73 struct tcfg_gate_entry *next;
74 ktime_t close_time, now;
76 spin_lock(&gact->tcf_lock);
78 next = gact->next_entry;
80 /* cycle start, clear pending bit, clear total octets */
81 gact->current_gate_status = next->gate_state ? GATE_ACT_GATE_OPEN : 0;
82 gact->current_entry_octets = 0;
83 gact->current_max_octets = next->maxoctets;
85 gact->current_close_time = ktime_add_ns(gact->current_close_time,
88 close_time = gact->current_close_time;
90 if (list_is_last(&next->list, &p->entries))
91 next = list_first_entry(&p->entries,
92 struct tcfg_gate_entry, list);
94 next = list_next_entry(next, list);
96 now = gate_get_time(gact);
98 if (ktime_after(now, close_time)) {
102 cycle = p->tcfg_cycletime;
103 base = ns_to_ktime(p->tcfg_basetime);
104 n = div64_u64(ktime_sub_ns(now, base), cycle);
105 close_time = ktime_add_ns(base, (n + 1) * cycle);
108 gact->next_entry = next;
110 hrtimer_set_expires(&gact->hitimer, close_time);
112 spin_unlock(&gact->tcf_lock);
114 return HRTIMER_RESTART;
117 TC_INDIRECT_SCOPE int tcf_gate_act(struct sk_buff *skb,
118 const struct tc_action *a,
119 struct tcf_result *res)
121 struct tcf_gate *gact = to_gate(a);
123 spin_lock(&gact->tcf_lock);
125 tcf_lastuse_update(&gact->tcf_tm);
126 bstats_update(&gact->tcf_bstats, skb);
128 if (unlikely(gact->current_gate_status & GATE_ACT_PENDING)) {
129 spin_unlock(&gact->tcf_lock);
130 return gact->tcf_action;
133 if (!(gact->current_gate_status & GATE_ACT_GATE_OPEN))
136 if (gact->current_max_octets >= 0) {
137 gact->current_entry_octets += qdisc_pkt_len(skb);
138 if (gact->current_entry_octets > gact->current_max_octets) {
139 gact->tcf_qstats.overlimits++;
144 spin_unlock(&gact->tcf_lock);
146 return gact->tcf_action;
148 gact->tcf_qstats.drops++;
149 spin_unlock(&gact->tcf_lock);
154 static const struct nla_policy entry_policy[TCA_GATE_ENTRY_MAX + 1] = {
155 [TCA_GATE_ENTRY_INDEX] = { .type = NLA_U32 },
156 [TCA_GATE_ENTRY_GATE] = { .type = NLA_FLAG },
157 [TCA_GATE_ENTRY_INTERVAL] = { .type = NLA_U32 },
158 [TCA_GATE_ENTRY_IPV] = { .type = NLA_S32 },
159 [TCA_GATE_ENTRY_MAX_OCTETS] = { .type = NLA_S32 },
162 static const struct nla_policy gate_policy[TCA_GATE_MAX + 1] = {
164 NLA_POLICY_EXACT_LEN(sizeof(struct tc_gate)),
165 [TCA_GATE_PRIORITY] = { .type = NLA_S32 },
166 [TCA_GATE_ENTRY_LIST] = { .type = NLA_NESTED },
167 [TCA_GATE_BASE_TIME] = { .type = NLA_U64 },
168 [TCA_GATE_CYCLE_TIME] = { .type = NLA_U64 },
169 [TCA_GATE_CYCLE_TIME_EXT] = { .type = NLA_U64 },
170 [TCA_GATE_FLAGS] = { .type = NLA_U32 },
171 [TCA_GATE_CLOCKID] = { .type = NLA_S32 },
174 static int fill_gate_entry(struct nlattr **tb, struct tcfg_gate_entry *entry,
175 struct netlink_ext_ack *extack)
179 entry->gate_state = nla_get_flag(tb[TCA_GATE_ENTRY_GATE]);
181 if (tb[TCA_GATE_ENTRY_INTERVAL])
182 interval = nla_get_u32(tb[TCA_GATE_ENTRY_INTERVAL]);
185 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
189 entry->interval = interval;
191 if (tb[TCA_GATE_ENTRY_IPV])
192 entry->ipv = nla_get_s32(tb[TCA_GATE_ENTRY_IPV]);
196 if (tb[TCA_GATE_ENTRY_MAX_OCTETS])
197 entry->maxoctets = nla_get_s32(tb[TCA_GATE_ENTRY_MAX_OCTETS]);
199 entry->maxoctets = -1;
204 static int parse_gate_entry(struct nlattr *n, struct tcfg_gate_entry *entry,
205 int index, struct netlink_ext_ack *extack)
207 struct nlattr *tb[TCA_GATE_ENTRY_MAX + 1] = { };
210 err = nla_parse_nested(tb, TCA_GATE_ENTRY_MAX, n, entry_policy, extack);
212 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
216 entry->index = index;
218 return fill_gate_entry(tb, entry, extack);
221 static void release_entry_list(struct list_head *entries)
223 struct tcfg_gate_entry *entry, *e;
225 list_for_each_entry_safe(entry, e, entries, list) {
226 list_del(&entry->list);
231 static int parse_gate_list(struct nlattr *list_attr,
232 struct tcf_gate_params *sched,
233 struct netlink_ext_ack *extack)
235 struct tcfg_gate_entry *entry;
243 nla_for_each_nested(n, list_attr, rem) {
244 if (nla_type(n) != TCA_GATE_ONE_ENTRY) {
245 NL_SET_ERR_MSG(extack, "Attribute isn't type 'entry'");
249 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
251 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
256 err = parse_gate_entry(n, entry, i, extack);
262 list_add_tail(&entry->list, &sched->entries);
266 sched->num_entries = i;
271 release_entry_list(&sched->entries);
276 static void gate_setup_timer(struct tcf_gate *gact, u64 basetime,
277 enum tk_offsets tko, s32 clockid,
281 if (basetime == gact->param.tcfg_basetime &&
282 tko == gact->tk_offset &&
283 clockid == gact->param.tcfg_clockid)
286 spin_unlock_bh(&gact->tcf_lock);
287 hrtimer_cancel(&gact->hitimer);
288 spin_lock_bh(&gact->tcf_lock);
290 gact->param.tcfg_basetime = basetime;
291 gact->param.tcfg_clockid = clockid;
292 gact->tk_offset = tko;
293 hrtimer_init(&gact->hitimer, clockid, HRTIMER_MODE_ABS_SOFT);
294 gact->hitimer.function = gate_timer_func;
297 static int tcf_gate_init(struct net *net, struct nlattr *nla,
298 struct nlattr *est, struct tc_action **a,
299 struct tcf_proto *tp, u32 flags,
300 struct netlink_ext_ack *extack)
302 struct tc_action_net *tn = net_generic(net, act_gate_ops.net_id);
303 enum tk_offsets tk_offset = TK_OFFS_TAI;
304 bool bind = flags & TCA_ACT_FLAGS_BIND;
305 struct nlattr *tb[TCA_GATE_MAX + 1];
306 struct tcf_chain *goto_ch = NULL;
307 u64 cycletime = 0, basetime = 0;
308 struct tcf_gate_params *p;
309 s32 clockid = CLOCK_TAI;
310 struct tcf_gate *gact;
311 struct tc_gate *parm;
321 err = nla_parse_nested(tb, TCA_GATE_MAX, nla, gate_policy, extack);
325 if (!tb[TCA_GATE_PARMS])
328 if (tb[TCA_GATE_CLOCKID]) {
329 clockid = nla_get_s32(tb[TCA_GATE_CLOCKID]);
332 tk_offset = TK_OFFS_REAL;
334 case CLOCK_MONOTONIC:
335 tk_offset = TK_OFFS_MAX;
338 tk_offset = TK_OFFS_BOOT;
341 tk_offset = TK_OFFS_TAI;
344 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
349 parm = nla_data(tb[TCA_GATE_PARMS]);
352 err = tcf_idr_check_alloc(tn, &index, a, bind);
360 ret = tcf_idr_create(tn, index, est, a,
361 &act_gate_ops, bind, false, flags);
363 tcf_idr_cleanup(tn, index);
368 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
369 tcf_idr_release(*a, bind);
373 if (tb[TCA_GATE_PRIORITY])
374 prio = nla_get_s32(tb[TCA_GATE_PRIORITY]);
376 if (tb[TCA_GATE_BASE_TIME])
377 basetime = nla_get_u64(tb[TCA_GATE_BASE_TIME]);
379 if (tb[TCA_GATE_FLAGS])
380 gflags = nla_get_u32(tb[TCA_GATE_FLAGS]);
383 if (ret == ACT_P_CREATED)
384 INIT_LIST_HEAD(&gact->param.entries);
386 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
390 spin_lock_bh(&gact->tcf_lock);
393 if (tb[TCA_GATE_CYCLE_TIME])
394 cycletime = nla_get_u64(tb[TCA_GATE_CYCLE_TIME]);
396 if (tb[TCA_GATE_ENTRY_LIST]) {
397 err = parse_gate_list(tb[TCA_GATE_ENTRY_LIST], p, extack);
403 struct tcfg_gate_entry *entry;
406 list_for_each_entry(entry, &p->entries, list)
407 cycle = ktime_add_ns(cycle, entry->interval);
414 p->tcfg_cycletime = cycletime;
416 if (tb[TCA_GATE_CYCLE_TIME_EXT])
417 p->tcfg_cycletime_ext =
418 nla_get_u64(tb[TCA_GATE_CYCLE_TIME_EXT]);
420 gate_setup_timer(gact, basetime, tk_offset, clockid,
421 ret == ACT_P_CREATED);
422 p->tcfg_priority = prio;
423 p->tcfg_flags = gflags;
424 gate_get_start_time(gact, &start);
426 gact->current_close_time = start;
427 gact->current_gate_status = GATE_ACT_GATE_OPEN | GATE_ACT_PENDING;
429 gact->next_entry = list_first_entry(&p->entries,
430 struct tcfg_gate_entry, list);
432 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
434 gate_start_timer(gact, start);
436 spin_unlock_bh(&gact->tcf_lock);
439 tcf_chain_put_by_act(goto_ch);
444 spin_unlock_bh(&gact->tcf_lock);
447 tcf_chain_put_by_act(goto_ch);
449 /* action is not inserted in any list: it's safe to init hitimer
450 * without taking tcf_lock.
452 if (ret == ACT_P_CREATED)
453 gate_setup_timer(gact, gact->param.tcfg_basetime,
454 gact->tk_offset, gact->param.tcfg_clockid,
456 tcf_idr_release(*a, bind);
460 static void tcf_gate_cleanup(struct tc_action *a)
462 struct tcf_gate *gact = to_gate(a);
463 struct tcf_gate_params *p;
466 hrtimer_cancel(&gact->hitimer);
467 release_entry_list(&p->entries);
470 static int dumping_entry(struct sk_buff *skb,
471 struct tcfg_gate_entry *entry)
475 item = nla_nest_start_noflag(skb, TCA_GATE_ONE_ENTRY);
479 if (nla_put_u32(skb, TCA_GATE_ENTRY_INDEX, entry->index))
480 goto nla_put_failure;
482 if (entry->gate_state && nla_put_flag(skb, TCA_GATE_ENTRY_GATE))
483 goto nla_put_failure;
485 if (nla_put_u32(skb, TCA_GATE_ENTRY_INTERVAL, entry->interval))
486 goto nla_put_failure;
488 if (nla_put_s32(skb, TCA_GATE_ENTRY_MAX_OCTETS, entry->maxoctets))
489 goto nla_put_failure;
491 if (nla_put_s32(skb, TCA_GATE_ENTRY_IPV, entry->ipv))
492 goto nla_put_failure;
494 return nla_nest_end(skb, item);
497 nla_nest_cancel(skb, item);
501 static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
504 unsigned char *b = skb_tail_pointer(skb);
505 struct tcf_gate *gact = to_gate(a);
506 struct tc_gate opt = {
507 .index = gact->tcf_index,
508 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
509 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
511 struct tcfg_gate_entry *entry;
512 struct tcf_gate_params *p;
513 struct nlattr *entry_list;
516 spin_lock_bh(&gact->tcf_lock);
517 opt.action = gact->tcf_action;
521 if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
522 goto nla_put_failure;
524 if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
525 p->tcfg_basetime, TCA_GATE_PAD))
526 goto nla_put_failure;
528 if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME,
529 p->tcfg_cycletime, TCA_GATE_PAD))
530 goto nla_put_failure;
532 if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME_EXT,
533 p->tcfg_cycletime_ext, TCA_GATE_PAD))
534 goto nla_put_failure;
536 if (nla_put_s32(skb, TCA_GATE_CLOCKID, p->tcfg_clockid))
537 goto nla_put_failure;
539 if (nla_put_u32(skb, TCA_GATE_FLAGS, p->tcfg_flags))
540 goto nla_put_failure;
542 if (nla_put_s32(skb, TCA_GATE_PRIORITY, p->tcfg_priority))
543 goto nla_put_failure;
545 entry_list = nla_nest_start_noflag(skb, TCA_GATE_ENTRY_LIST);
547 goto nla_put_failure;
549 list_for_each_entry(entry, &p->entries, list) {
550 if (dumping_entry(skb, entry) < 0)
551 goto nla_put_failure;
554 nla_nest_end(skb, entry_list);
556 tcf_tm_dump(&t, &gact->tcf_tm);
557 if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
558 goto nla_put_failure;
559 spin_unlock_bh(&gact->tcf_lock);
564 spin_unlock_bh(&gact->tcf_lock);
569 static void tcf_gate_stats_update(struct tc_action *a, u64 bytes, u64 packets,
570 u64 drops, u64 lastuse, bool hw)
572 struct tcf_gate *gact = to_gate(a);
573 struct tcf_t *tm = &gact->tcf_tm;
575 tcf_action_update_stats(a, bytes, packets, drops, hw);
576 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
579 static size_t tcf_gate_get_fill_size(const struct tc_action *act)
581 return nla_total_size(sizeof(struct tc_gate));
584 static void tcf_gate_entry_destructor(void *priv)
586 struct action_gate_entry *oe = priv;
591 static int tcf_gate_get_entries(struct flow_action_entry *entry,
592 const struct tc_action *act)
594 entry->gate.entries = tcf_gate_get_list(act);
596 if (!entry->gate.entries)
599 entry->destructor = tcf_gate_entry_destructor;
600 entry->destructor_priv = entry->gate.entries;
605 static int tcf_gate_offload_act_setup(struct tc_action *act, void *entry_data,
606 u32 *index_inc, bool bind,
607 struct netlink_ext_ack *extack)
612 struct flow_action_entry *entry = entry_data;
614 entry->id = FLOW_ACTION_GATE;
615 entry->gate.prio = tcf_gate_prio(act);
616 entry->gate.basetime = tcf_gate_basetime(act);
617 entry->gate.cycletime = tcf_gate_cycletime(act);
618 entry->gate.cycletimeext = tcf_gate_cycletimeext(act);
619 entry->gate.num_entries = tcf_gate_num_entries(act);
620 err = tcf_gate_get_entries(entry, act);
625 struct flow_offload_action *fl_action = entry_data;
627 fl_action->id = FLOW_ACTION_GATE;
633 static struct tc_action_ops act_gate_ops = {
636 .owner = THIS_MODULE,
638 .dump = tcf_gate_dump,
639 .init = tcf_gate_init,
640 .cleanup = tcf_gate_cleanup,
641 .stats_update = tcf_gate_stats_update,
642 .get_fill_size = tcf_gate_get_fill_size,
643 .offload_act_setup = tcf_gate_offload_act_setup,
644 .size = sizeof(struct tcf_gate),
647 static __net_init int gate_init_net(struct net *net)
649 struct tc_action_net *tn = net_generic(net, act_gate_ops.net_id);
651 return tc_action_net_init(net, tn, &act_gate_ops);
654 static void __net_exit gate_exit_net(struct list_head *net_list)
656 tc_action_net_exit(net_list, act_gate_ops.net_id);
659 static struct pernet_operations gate_net_ops = {
660 .init = gate_init_net,
661 .exit_batch = gate_exit_net,
662 .id = &act_gate_ops.net_id,
663 .size = sizeof(struct tc_action_net),
666 static int __init gate_init_module(void)
668 return tcf_register_action(&act_gate_ops, &gate_net_ops);
671 static void __exit gate_cleanup_module(void)
673 tcf_unregister_action(&act_gate_ops, &gate_net_ops);
676 module_init(gate_init_module);
677 module_exit(gate_cleanup_module);
678 MODULE_LICENSE("GPL v2");