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>
18 static struct tc_action_ops act_gate_ops;
20 static ktime_t gate_get_time(struct tcf_gate *gact)
22 ktime_t mono = ktime_get();
24 switch (gact->tk_offset) {
28 return ktime_mono_to_any(mono, gact->tk_offset);
34 static void gate_get_start_time(struct tcf_gate *gact, ktime_t *start)
36 struct tcf_gate_params *param = &gact->param;
37 ktime_t now, base, cycle;
40 base = ns_to_ktime(param->tcfg_basetime);
41 now = gate_get_time(gact);
43 if (ktime_after(base, now)) {
48 cycle = param->tcfg_cycletime;
50 n = div64_u64(ktime_sub_ns(now, base), cycle);
51 *start = ktime_add_ns(base, (n + 1) * cycle);
54 static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
58 expires = hrtimer_get_expires(&gact->hitimer);
62 start = min_t(ktime_t, start, expires);
64 hrtimer_start(&gact->hitimer, start, HRTIMER_MODE_ABS_SOFT);
67 static enum hrtimer_restart gate_timer_func(struct hrtimer *timer)
69 struct tcf_gate *gact = container_of(timer, struct tcf_gate,
71 struct tcf_gate_params *p = &gact->param;
72 struct tcfg_gate_entry *next;
73 ktime_t close_time, now;
75 spin_lock(&gact->tcf_lock);
77 next = gact->next_entry;
79 /* cycle start, clear pending bit, clear total octets */
80 gact->current_gate_status = next->gate_state ? GATE_ACT_GATE_OPEN : 0;
81 gact->current_entry_octets = 0;
82 gact->current_max_octets = next->maxoctets;
84 gact->current_close_time = ktime_add_ns(gact->current_close_time,
87 close_time = gact->current_close_time;
89 if (list_is_last(&next->list, &p->entries))
90 next = list_first_entry(&p->entries,
91 struct tcfg_gate_entry, list);
93 next = list_next_entry(next, list);
95 now = gate_get_time(gact);
97 if (ktime_after(now, close_time)) {
101 cycle = p->tcfg_cycletime;
102 base = ns_to_ktime(p->tcfg_basetime);
103 n = div64_u64(ktime_sub_ns(now, base), cycle);
104 close_time = ktime_add_ns(base, (n + 1) * cycle);
107 gact->next_entry = next;
109 hrtimer_set_expires(&gact->hitimer, close_time);
111 spin_unlock(&gact->tcf_lock);
113 return HRTIMER_RESTART;
116 static int tcf_gate_act(struct sk_buff *skb, const struct tc_action *a,
117 struct tcf_result *res)
119 struct tcf_gate *gact = to_gate(a);
121 spin_lock(&gact->tcf_lock);
123 tcf_lastuse_update(&gact->tcf_tm);
124 bstats_update(&gact->tcf_bstats, skb);
126 if (unlikely(gact->current_gate_status & GATE_ACT_PENDING)) {
127 spin_unlock(&gact->tcf_lock);
128 return gact->tcf_action;
131 if (!(gact->current_gate_status & GATE_ACT_GATE_OPEN))
134 if (gact->current_max_octets >= 0) {
135 gact->current_entry_octets += qdisc_pkt_len(skb);
136 if (gact->current_entry_octets > gact->current_max_octets) {
137 gact->tcf_qstats.overlimits++;
142 spin_unlock(&gact->tcf_lock);
144 return gact->tcf_action;
146 gact->tcf_qstats.drops++;
147 spin_unlock(&gact->tcf_lock);
152 static const struct nla_policy entry_policy[TCA_GATE_ENTRY_MAX + 1] = {
153 [TCA_GATE_ENTRY_INDEX] = { .type = NLA_U32 },
154 [TCA_GATE_ENTRY_GATE] = { .type = NLA_FLAG },
155 [TCA_GATE_ENTRY_INTERVAL] = { .type = NLA_U32 },
156 [TCA_GATE_ENTRY_IPV] = { .type = NLA_S32 },
157 [TCA_GATE_ENTRY_MAX_OCTETS] = { .type = NLA_S32 },
160 static const struct nla_policy gate_policy[TCA_GATE_MAX + 1] = {
162 NLA_POLICY_EXACT_LEN(sizeof(struct tc_gate)),
163 [TCA_GATE_PRIORITY] = { .type = NLA_S32 },
164 [TCA_GATE_ENTRY_LIST] = { .type = NLA_NESTED },
165 [TCA_GATE_BASE_TIME] = { .type = NLA_U64 },
166 [TCA_GATE_CYCLE_TIME] = { .type = NLA_U64 },
167 [TCA_GATE_CYCLE_TIME_EXT] = { .type = NLA_U64 },
168 [TCA_GATE_FLAGS] = { .type = NLA_U32 },
169 [TCA_GATE_CLOCKID] = { .type = NLA_S32 },
172 static int fill_gate_entry(struct nlattr **tb, struct tcfg_gate_entry *entry,
173 struct netlink_ext_ack *extack)
177 entry->gate_state = nla_get_flag(tb[TCA_GATE_ENTRY_GATE]);
179 if (tb[TCA_GATE_ENTRY_INTERVAL])
180 interval = nla_get_u32(tb[TCA_GATE_ENTRY_INTERVAL]);
183 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
187 entry->interval = interval;
189 if (tb[TCA_GATE_ENTRY_IPV])
190 entry->ipv = nla_get_s32(tb[TCA_GATE_ENTRY_IPV]);
194 if (tb[TCA_GATE_ENTRY_MAX_OCTETS])
195 entry->maxoctets = nla_get_s32(tb[TCA_GATE_ENTRY_MAX_OCTETS]);
197 entry->maxoctets = -1;
202 static int parse_gate_entry(struct nlattr *n, struct tcfg_gate_entry *entry,
203 int index, struct netlink_ext_ack *extack)
205 struct nlattr *tb[TCA_GATE_ENTRY_MAX + 1] = { };
208 err = nla_parse_nested(tb, TCA_GATE_ENTRY_MAX, n, entry_policy, extack);
210 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
214 entry->index = index;
216 return fill_gate_entry(tb, entry, extack);
219 static void release_entry_list(struct list_head *entries)
221 struct tcfg_gate_entry *entry, *e;
223 list_for_each_entry_safe(entry, e, entries, list) {
224 list_del(&entry->list);
229 static int parse_gate_list(struct nlattr *list_attr,
230 struct tcf_gate_params *sched,
231 struct netlink_ext_ack *extack)
233 struct tcfg_gate_entry *entry;
241 nla_for_each_nested(n, list_attr, rem) {
242 if (nla_type(n) != TCA_GATE_ONE_ENTRY) {
243 NL_SET_ERR_MSG(extack, "Attribute isn't type 'entry'");
247 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
249 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
254 err = parse_gate_entry(n, entry, i, extack);
260 list_add_tail(&entry->list, &sched->entries);
264 sched->num_entries = i;
269 release_entry_list(&sched->entries);
274 static void gate_setup_timer(struct tcf_gate *gact, u64 basetime,
275 enum tk_offsets tko, s32 clockid,
279 if (basetime == gact->param.tcfg_basetime &&
280 tko == gact->tk_offset &&
281 clockid == gact->param.tcfg_clockid)
284 spin_unlock_bh(&gact->tcf_lock);
285 hrtimer_cancel(&gact->hitimer);
286 spin_lock_bh(&gact->tcf_lock);
288 gact->param.tcfg_basetime = basetime;
289 gact->param.tcfg_clockid = clockid;
290 gact->tk_offset = tko;
291 hrtimer_init(&gact->hitimer, clockid, HRTIMER_MODE_ABS_SOFT);
292 gact->hitimer.function = gate_timer_func;
295 static int tcf_gate_init(struct net *net, struct nlattr *nla,
296 struct nlattr *est, struct tc_action **a,
297 struct tcf_proto *tp, u32 flags,
298 struct netlink_ext_ack *extack)
300 struct tc_action_net *tn = net_generic(net, act_gate_ops.net_id);
301 enum tk_offsets tk_offset = TK_OFFS_TAI;
302 bool bind = flags & TCA_ACT_FLAGS_BIND;
303 struct nlattr *tb[TCA_GATE_MAX + 1];
304 struct tcf_chain *goto_ch = NULL;
305 u64 cycletime = 0, basetime = 0;
306 struct tcf_gate_params *p;
307 s32 clockid = CLOCK_TAI;
308 struct tcf_gate *gact;
309 struct tc_gate *parm;
319 err = nla_parse_nested(tb, TCA_GATE_MAX, nla, gate_policy, extack);
323 if (!tb[TCA_GATE_PARMS])
326 if (tb[TCA_GATE_CLOCKID]) {
327 clockid = nla_get_s32(tb[TCA_GATE_CLOCKID]);
330 tk_offset = TK_OFFS_REAL;
332 case CLOCK_MONOTONIC:
333 tk_offset = TK_OFFS_MAX;
336 tk_offset = TK_OFFS_BOOT;
339 tk_offset = TK_OFFS_TAI;
342 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
347 parm = nla_data(tb[TCA_GATE_PARMS]);
350 err = tcf_idr_check_alloc(tn, &index, a, bind);
358 ret = tcf_idr_create(tn, index, est, a,
359 &act_gate_ops, bind, false, flags);
361 tcf_idr_cleanup(tn, index);
366 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
367 tcf_idr_release(*a, bind);
371 if (tb[TCA_GATE_PRIORITY])
372 prio = nla_get_s32(tb[TCA_GATE_PRIORITY]);
374 if (tb[TCA_GATE_BASE_TIME])
375 basetime = nla_get_u64(tb[TCA_GATE_BASE_TIME]);
377 if (tb[TCA_GATE_FLAGS])
378 gflags = nla_get_u32(tb[TCA_GATE_FLAGS]);
381 if (ret == ACT_P_CREATED)
382 INIT_LIST_HEAD(&gact->param.entries);
384 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
388 spin_lock_bh(&gact->tcf_lock);
391 if (tb[TCA_GATE_CYCLE_TIME])
392 cycletime = nla_get_u64(tb[TCA_GATE_CYCLE_TIME]);
394 if (tb[TCA_GATE_ENTRY_LIST]) {
395 err = parse_gate_list(tb[TCA_GATE_ENTRY_LIST], p, extack);
401 struct tcfg_gate_entry *entry;
404 list_for_each_entry(entry, &p->entries, list)
405 cycle = ktime_add_ns(cycle, entry->interval);
412 p->tcfg_cycletime = cycletime;
414 if (tb[TCA_GATE_CYCLE_TIME_EXT])
415 p->tcfg_cycletime_ext =
416 nla_get_u64(tb[TCA_GATE_CYCLE_TIME_EXT]);
418 gate_setup_timer(gact, basetime, tk_offset, clockid,
419 ret == ACT_P_CREATED);
420 p->tcfg_priority = prio;
421 p->tcfg_flags = gflags;
422 gate_get_start_time(gact, &start);
424 gact->current_close_time = start;
425 gact->current_gate_status = GATE_ACT_GATE_OPEN | GATE_ACT_PENDING;
427 gact->next_entry = list_first_entry(&p->entries,
428 struct tcfg_gate_entry, list);
430 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
432 gate_start_timer(gact, start);
434 spin_unlock_bh(&gact->tcf_lock);
437 tcf_chain_put_by_act(goto_ch);
442 spin_unlock_bh(&gact->tcf_lock);
445 tcf_chain_put_by_act(goto_ch);
447 /* action is not inserted in any list: it's safe to init hitimer
448 * without taking tcf_lock.
450 if (ret == ACT_P_CREATED)
451 gate_setup_timer(gact, gact->param.tcfg_basetime,
452 gact->tk_offset, gact->param.tcfg_clockid,
454 tcf_idr_release(*a, bind);
458 static void tcf_gate_cleanup(struct tc_action *a)
460 struct tcf_gate *gact = to_gate(a);
461 struct tcf_gate_params *p;
464 hrtimer_cancel(&gact->hitimer);
465 release_entry_list(&p->entries);
468 static int dumping_entry(struct sk_buff *skb,
469 struct tcfg_gate_entry *entry)
473 item = nla_nest_start_noflag(skb, TCA_GATE_ONE_ENTRY);
477 if (nla_put_u32(skb, TCA_GATE_ENTRY_INDEX, entry->index))
478 goto nla_put_failure;
480 if (entry->gate_state && nla_put_flag(skb, TCA_GATE_ENTRY_GATE))
481 goto nla_put_failure;
483 if (nla_put_u32(skb, TCA_GATE_ENTRY_INTERVAL, entry->interval))
484 goto nla_put_failure;
486 if (nla_put_s32(skb, TCA_GATE_ENTRY_MAX_OCTETS, entry->maxoctets))
487 goto nla_put_failure;
489 if (nla_put_s32(skb, TCA_GATE_ENTRY_IPV, entry->ipv))
490 goto nla_put_failure;
492 return nla_nest_end(skb, item);
495 nla_nest_cancel(skb, item);
499 static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
502 unsigned char *b = skb_tail_pointer(skb);
503 struct tcf_gate *gact = to_gate(a);
504 struct tc_gate opt = {
505 .index = gact->tcf_index,
506 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
507 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
509 struct tcfg_gate_entry *entry;
510 struct tcf_gate_params *p;
511 struct nlattr *entry_list;
514 spin_lock_bh(&gact->tcf_lock);
515 opt.action = gact->tcf_action;
519 if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
520 goto nla_put_failure;
522 if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
523 p->tcfg_basetime, TCA_GATE_PAD))
524 goto nla_put_failure;
526 if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME,
527 p->tcfg_cycletime, TCA_GATE_PAD))
528 goto nla_put_failure;
530 if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME_EXT,
531 p->tcfg_cycletime_ext, TCA_GATE_PAD))
532 goto nla_put_failure;
534 if (nla_put_s32(skb, TCA_GATE_CLOCKID, p->tcfg_clockid))
535 goto nla_put_failure;
537 if (nla_put_u32(skb, TCA_GATE_FLAGS, p->tcfg_flags))
538 goto nla_put_failure;
540 if (nla_put_s32(skb, TCA_GATE_PRIORITY, p->tcfg_priority))
541 goto nla_put_failure;
543 entry_list = nla_nest_start_noflag(skb, TCA_GATE_ENTRY_LIST);
545 goto nla_put_failure;
547 list_for_each_entry(entry, &p->entries, list) {
548 if (dumping_entry(skb, entry) < 0)
549 goto nla_put_failure;
552 nla_nest_end(skb, entry_list);
554 tcf_tm_dump(&t, &gact->tcf_tm);
555 if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
556 goto nla_put_failure;
557 spin_unlock_bh(&gact->tcf_lock);
562 spin_unlock_bh(&gact->tcf_lock);
567 static void tcf_gate_stats_update(struct tc_action *a, u64 bytes, u64 packets,
568 u64 drops, u64 lastuse, bool hw)
570 struct tcf_gate *gact = to_gate(a);
571 struct tcf_t *tm = &gact->tcf_tm;
573 tcf_action_update_stats(a, bytes, packets, drops, hw);
574 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
577 static size_t tcf_gate_get_fill_size(const struct tc_action *act)
579 return nla_total_size(sizeof(struct tc_gate));
582 static void tcf_gate_entry_destructor(void *priv)
584 struct action_gate_entry *oe = priv;
589 static int tcf_gate_get_entries(struct flow_action_entry *entry,
590 const struct tc_action *act)
592 entry->gate.entries = tcf_gate_get_list(act);
594 if (!entry->gate.entries)
597 entry->destructor = tcf_gate_entry_destructor;
598 entry->destructor_priv = entry->gate.entries;
603 static int tcf_gate_offload_act_setup(struct tc_action *act, void *entry_data,
604 u32 *index_inc, bool bind,
605 struct netlink_ext_ack *extack)
610 struct flow_action_entry *entry = entry_data;
612 entry->id = FLOW_ACTION_GATE;
613 entry->gate.prio = tcf_gate_prio(act);
614 entry->gate.basetime = tcf_gate_basetime(act);
615 entry->gate.cycletime = tcf_gate_cycletime(act);
616 entry->gate.cycletimeext = tcf_gate_cycletimeext(act);
617 entry->gate.num_entries = tcf_gate_num_entries(act);
618 err = tcf_gate_get_entries(entry, act);
623 struct flow_offload_action *fl_action = entry_data;
625 fl_action->id = FLOW_ACTION_GATE;
631 static struct tc_action_ops act_gate_ops = {
634 .owner = THIS_MODULE,
636 .dump = tcf_gate_dump,
637 .init = tcf_gate_init,
638 .cleanup = tcf_gate_cleanup,
639 .stats_update = tcf_gate_stats_update,
640 .get_fill_size = tcf_gate_get_fill_size,
641 .offload_act_setup = tcf_gate_offload_act_setup,
642 .size = sizeof(struct tcf_gate),
645 static __net_init int gate_init_net(struct net *net)
647 struct tc_action_net *tn = net_generic(net, act_gate_ops.net_id);
649 return tc_action_net_init(net, tn, &act_gate_ops);
652 static void __net_exit gate_exit_net(struct list_head *net_list)
654 tc_action_net_exit(net_list, act_gate_ops.net_id);
657 static struct pernet_operations gate_net_ops = {
658 .init = gate_init_net,
659 .exit_batch = gate_exit_net,
660 .id = &act_gate_ops.net_id,
661 .size = sizeof(struct tc_action_net),
664 static int __init gate_init_module(void)
666 return tcf_register_action(&act_gate_ops, &gate_net_ops);
669 static void __exit gate_cleanup_module(void)
671 tcf_unregister_action(&act_gate_ops, &gate_net_ops);
674 module_init(gate_init_module);
675 module_exit(gate_cleanup_module);
676 MODULE_LICENSE("GPL v2");