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 unsigned int gate_net_id;
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 int 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 /* cycle time should not be zero */
55 n = div64_u64(ktime_sub_ns(now, base), cycle);
56 *start = ktime_add_ns(base, (n + 1) * cycle);
60 static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
64 expires = hrtimer_get_expires(&gact->hitimer);
68 start = min_t(ktime_t, start, expires);
70 hrtimer_start(&gact->hitimer, start, HRTIMER_MODE_ABS_SOFT);
73 static enum hrtimer_restart gate_timer_func(struct hrtimer *timer)
75 struct tcf_gate *gact = container_of(timer, struct tcf_gate,
77 struct tcf_gate_params *p = &gact->param;
78 struct tcfg_gate_entry *next;
79 ktime_t close_time, now;
81 spin_lock(&gact->tcf_lock);
83 next = gact->next_entry;
85 /* cycle start, clear pending bit, clear total octets */
86 gact->current_gate_status = next->gate_state ? GATE_ACT_GATE_OPEN : 0;
87 gact->current_entry_octets = 0;
88 gact->current_max_octets = next->maxoctets;
90 gact->current_close_time = ktime_add_ns(gact->current_close_time,
93 close_time = gact->current_close_time;
95 if (list_is_last(&next->list, &p->entries))
96 next = list_first_entry(&p->entries,
97 struct tcfg_gate_entry, list);
99 next = list_next_entry(next, list);
101 now = gate_get_time(gact);
103 if (ktime_after(now, close_time)) {
107 cycle = p->tcfg_cycletime;
108 base = ns_to_ktime(p->tcfg_basetime);
109 n = div64_u64(ktime_sub_ns(now, base), cycle);
110 close_time = ktime_add_ns(base, (n + 1) * cycle);
113 gact->next_entry = next;
115 hrtimer_set_expires(&gact->hitimer, close_time);
117 spin_unlock(&gact->tcf_lock);
119 return HRTIMER_RESTART;
122 static int tcf_gate_act(struct sk_buff *skb, const struct tc_action *a,
123 struct tcf_result *res)
125 struct tcf_gate *gact = to_gate(a);
127 spin_lock(&gact->tcf_lock);
129 tcf_lastuse_update(&gact->tcf_tm);
130 bstats_update(&gact->tcf_bstats, skb);
132 if (unlikely(gact->current_gate_status & GATE_ACT_PENDING)) {
133 spin_unlock(&gact->tcf_lock);
134 return gact->tcf_action;
137 if (!(gact->current_gate_status & GATE_ACT_GATE_OPEN))
140 if (gact->current_max_octets >= 0) {
141 gact->current_entry_octets += qdisc_pkt_len(skb);
142 if (gact->current_entry_octets > gact->current_max_octets) {
143 gact->tcf_qstats.overlimits++;
148 spin_unlock(&gact->tcf_lock);
150 return gact->tcf_action;
152 gact->tcf_qstats.drops++;
153 spin_unlock(&gact->tcf_lock);
158 static const struct nla_policy entry_policy[TCA_GATE_ENTRY_MAX + 1] = {
159 [TCA_GATE_ENTRY_INDEX] = { .type = NLA_U32 },
160 [TCA_GATE_ENTRY_GATE] = { .type = NLA_FLAG },
161 [TCA_GATE_ENTRY_INTERVAL] = { .type = NLA_U32 },
162 [TCA_GATE_ENTRY_IPV] = { .type = NLA_S32 },
163 [TCA_GATE_ENTRY_MAX_OCTETS] = { .type = NLA_S32 },
166 static const struct nla_policy gate_policy[TCA_GATE_MAX + 1] = {
167 [TCA_GATE_PARMS] = { .len = sizeof(struct tc_gate),
168 .type = NLA_EXACT_LEN },
169 [TCA_GATE_PRIORITY] = { .type = NLA_S32 },
170 [TCA_GATE_ENTRY_LIST] = { .type = NLA_NESTED },
171 [TCA_GATE_BASE_TIME] = { .type = NLA_U64 },
172 [TCA_GATE_CYCLE_TIME] = { .type = NLA_U64 },
173 [TCA_GATE_CYCLE_TIME_EXT] = { .type = NLA_U64 },
174 [TCA_GATE_FLAGS] = { .type = NLA_U32 },
175 [TCA_GATE_CLOCKID] = { .type = NLA_S32 },
178 static int fill_gate_entry(struct nlattr **tb, struct tcfg_gate_entry *entry,
179 struct netlink_ext_ack *extack)
183 entry->gate_state = nla_get_flag(tb[TCA_GATE_ENTRY_GATE]);
185 if (tb[TCA_GATE_ENTRY_INTERVAL])
186 interval = nla_get_u32(tb[TCA_GATE_ENTRY_INTERVAL]);
189 NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
193 entry->interval = interval;
195 if (tb[TCA_GATE_ENTRY_IPV])
196 entry->ipv = nla_get_s32(tb[TCA_GATE_ENTRY_IPV]);
200 if (tb[TCA_GATE_ENTRY_MAX_OCTETS])
201 entry->maxoctets = nla_get_s32(tb[TCA_GATE_ENTRY_MAX_OCTETS]);
203 entry->maxoctets = -1;
208 static int parse_gate_entry(struct nlattr *n, struct tcfg_gate_entry *entry,
209 int index, struct netlink_ext_ack *extack)
211 struct nlattr *tb[TCA_GATE_ENTRY_MAX + 1] = { };
214 err = nla_parse_nested(tb, TCA_GATE_ENTRY_MAX, n, entry_policy, extack);
216 NL_SET_ERR_MSG(extack, "Could not parse nested entry");
220 entry->index = index;
222 return fill_gate_entry(tb, entry, extack);
225 static void release_entry_list(struct list_head *entries)
227 struct tcfg_gate_entry *entry, *e;
229 list_for_each_entry_safe(entry, e, entries, list) {
230 list_del(&entry->list);
235 static int parse_gate_list(struct nlattr *list_attr,
236 struct tcf_gate_params *sched,
237 struct netlink_ext_ack *extack)
239 struct tcfg_gate_entry *entry;
247 nla_for_each_nested(n, list_attr, rem) {
248 if (nla_type(n) != TCA_GATE_ONE_ENTRY) {
249 NL_SET_ERR_MSG(extack, "Attribute isn't type 'entry'");
253 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
255 NL_SET_ERR_MSG(extack, "Not enough memory for entry");
260 err = parse_gate_entry(n, entry, i, extack);
266 list_add_tail(&entry->list, &sched->entries);
270 sched->num_entries = i;
275 release_entry_list(&sched->entries);
280 static int tcf_gate_init(struct net *net, struct nlattr *nla,
281 struct nlattr *est, struct tc_action **a,
282 int ovr, int bind, bool rtnl_held,
283 struct tcf_proto *tp, u32 flags,
284 struct netlink_ext_ack *extack)
286 struct tc_action_net *tn = net_generic(net, gate_net_id);
287 enum tk_offsets tk_offset = TK_OFFS_TAI;
288 struct nlattr *tb[TCA_GATE_MAX + 1];
289 struct tcf_chain *goto_ch = NULL;
290 struct tcf_gate_params *p;
291 s32 clockid = CLOCK_TAI;
292 struct tcf_gate *gact;
293 struct tc_gate *parm;
304 err = nla_parse_nested(tb, TCA_GATE_MAX, nla, gate_policy, extack);
308 if (!tb[TCA_GATE_PARMS])
311 parm = nla_data(tb[TCA_GATE_PARMS]);
314 err = tcf_idr_check_alloc(tn, &index, a, bind);
322 ret = tcf_idr_create(tn, index, est, a,
323 &act_gate_ops, bind, false, 0);
325 tcf_idr_cleanup(tn, index);
331 tcf_idr_release(*a, bind);
334 if (ret == ACT_P_CREATED) {
335 to_gate(*a)->param.tcfg_clockid = -1;
336 INIT_LIST_HEAD(&(to_gate(*a)->param.entries));
339 if (tb[TCA_GATE_PRIORITY])
340 prio = nla_get_s32(tb[TCA_GATE_PRIORITY]);
342 if (tb[TCA_GATE_BASE_TIME])
343 basetime = nla_get_u64(tb[TCA_GATE_BASE_TIME]);
345 if (tb[TCA_GATE_FLAGS])
346 gflags = nla_get_u32(tb[TCA_GATE_FLAGS]);
348 if (tb[TCA_GATE_CLOCKID]) {
349 clockid = nla_get_s32(tb[TCA_GATE_CLOCKID]);
352 tk_offset = TK_OFFS_REAL;
354 case CLOCK_MONOTONIC:
355 tk_offset = TK_OFFS_MAX;
358 tk_offset = TK_OFFS_BOOT;
361 tk_offset = TK_OFFS_TAI;
364 NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
369 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
375 spin_lock_bh(&gact->tcf_lock);
378 if (tb[TCA_GATE_CYCLE_TIME]) {
379 p->tcfg_cycletime = nla_get_u64(tb[TCA_GATE_CYCLE_TIME]);
380 if (!p->tcfg_cycletime_ext)
384 if (tb[TCA_GATE_ENTRY_LIST]) {
385 err = parse_gate_list(tb[TCA_GATE_ENTRY_LIST], p, extack);
390 if (!p->tcfg_cycletime) {
391 struct tcfg_gate_entry *entry;
394 list_for_each_entry(entry, &p->entries, list)
395 cycle = ktime_add_ns(cycle, entry->interval);
396 p->tcfg_cycletime = cycle;
399 if (tb[TCA_GATE_CYCLE_TIME_EXT])
400 p->tcfg_cycletime_ext =
401 nla_get_u64(tb[TCA_GATE_CYCLE_TIME_EXT]);
403 p->tcfg_priority = prio;
404 p->tcfg_basetime = basetime;
405 p->tcfg_clockid = clockid;
406 p->tcfg_flags = gflags;
408 gact->tk_offset = tk_offset;
409 hrtimer_init(&gact->hitimer, clockid, HRTIMER_MODE_ABS_SOFT);
410 gact->hitimer.function = gate_timer_func;
412 err = gate_get_start_time(gact, &start);
414 NL_SET_ERR_MSG(extack,
415 "Internal error: failed get start time");
416 release_entry_list(&p->entries);
420 gact->current_close_time = start;
421 gact->current_gate_status = GATE_ACT_GATE_OPEN | GATE_ACT_PENDING;
423 gact->next_entry = list_first_entry(&p->entries,
424 struct tcfg_gate_entry, list);
426 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
428 gate_start_timer(gact, start);
430 spin_unlock_bh(&gact->tcf_lock);
433 tcf_chain_put_by_act(goto_ch);
435 if (ret == ACT_P_CREATED)
436 tcf_idr_insert(tn, *a);
441 spin_unlock_bh(&gact->tcf_lock);
444 tcf_chain_put_by_act(goto_ch);
446 tcf_idr_release(*a, bind);
450 static void tcf_gate_cleanup(struct tc_action *a)
452 struct tcf_gate *gact = to_gate(a);
453 struct tcf_gate_params *p;
456 if (p->tcfg_clockid != -1)
457 hrtimer_cancel(&gact->hitimer);
459 release_entry_list(&p->entries);
462 static int dumping_entry(struct sk_buff *skb,
463 struct tcfg_gate_entry *entry)
467 item = nla_nest_start_noflag(skb, TCA_GATE_ONE_ENTRY);
471 if (nla_put_u32(skb, TCA_GATE_ENTRY_INDEX, entry->index))
472 goto nla_put_failure;
474 if (entry->gate_state && nla_put_flag(skb, TCA_GATE_ENTRY_GATE))
475 goto nla_put_failure;
477 if (nla_put_u32(skb, TCA_GATE_ENTRY_INTERVAL, entry->interval))
478 goto nla_put_failure;
480 if (nla_put_s32(skb, TCA_GATE_ENTRY_MAX_OCTETS, entry->maxoctets))
481 goto nla_put_failure;
483 if (nla_put_s32(skb, TCA_GATE_ENTRY_IPV, entry->ipv))
484 goto nla_put_failure;
486 return nla_nest_end(skb, item);
489 nla_nest_cancel(skb, item);
493 static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
496 unsigned char *b = skb_tail_pointer(skb);
497 struct tcf_gate *gact = to_gate(a);
498 struct tc_gate opt = {
499 .index = gact->tcf_index,
500 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
501 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
503 struct tcfg_gate_entry *entry;
504 struct tcf_gate_params *p;
505 struct nlattr *entry_list;
508 spin_lock_bh(&gact->tcf_lock);
509 opt.action = gact->tcf_action;
513 if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
514 goto nla_put_failure;
516 if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
517 p->tcfg_basetime, TCA_GATE_PAD))
518 goto nla_put_failure;
520 if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME,
521 p->tcfg_cycletime, TCA_GATE_PAD))
522 goto nla_put_failure;
524 if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME_EXT,
525 p->tcfg_cycletime_ext, TCA_GATE_PAD))
526 goto nla_put_failure;
528 if (nla_put_s32(skb, TCA_GATE_CLOCKID, p->tcfg_clockid))
529 goto nla_put_failure;
531 if (nla_put_u32(skb, TCA_GATE_FLAGS, p->tcfg_flags))
532 goto nla_put_failure;
534 if (nla_put_s32(skb, TCA_GATE_PRIORITY, p->tcfg_priority))
535 goto nla_put_failure;
537 entry_list = nla_nest_start_noflag(skb, TCA_GATE_ENTRY_LIST);
539 goto nla_put_failure;
541 list_for_each_entry(entry, &p->entries, list) {
542 if (dumping_entry(skb, entry) < 0)
543 goto nla_put_failure;
546 nla_nest_end(skb, entry_list);
548 tcf_tm_dump(&t, &gact->tcf_tm);
549 if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
550 goto nla_put_failure;
551 spin_unlock_bh(&gact->tcf_lock);
556 spin_unlock_bh(&gact->tcf_lock);
561 static int tcf_gate_walker(struct net *net, struct sk_buff *skb,
562 struct netlink_callback *cb, int type,
563 const struct tc_action_ops *ops,
564 struct netlink_ext_ack *extack)
566 struct tc_action_net *tn = net_generic(net, gate_net_id);
568 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
571 static void tcf_gate_stats_update(struct tc_action *a, u64 bytes, u32 packets,
572 u64 lastuse, bool hw)
574 struct tcf_gate *gact = to_gate(a);
575 struct tcf_t *tm = &gact->tcf_tm;
577 tcf_action_update_stats(a, bytes, packets, false, hw);
578 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
581 static int tcf_gate_search(struct net *net, struct tc_action **a, u32 index)
583 struct tc_action_net *tn = net_generic(net, gate_net_id);
585 return tcf_idr_search(tn, a, index);
588 static size_t tcf_gate_get_fill_size(const struct tc_action *act)
590 return nla_total_size(sizeof(struct tc_gate));
593 static struct tc_action_ops act_gate_ops = {
596 .owner = THIS_MODULE,
598 .dump = tcf_gate_dump,
599 .init = tcf_gate_init,
600 .cleanup = tcf_gate_cleanup,
601 .walk = tcf_gate_walker,
602 .stats_update = tcf_gate_stats_update,
603 .get_fill_size = tcf_gate_get_fill_size,
604 .lookup = tcf_gate_search,
605 .size = sizeof(struct tcf_gate),
608 static __net_init int gate_init_net(struct net *net)
610 struct tc_action_net *tn = net_generic(net, gate_net_id);
612 return tc_action_net_init(net, tn, &act_gate_ops);
615 static void __net_exit gate_exit_net(struct list_head *net_list)
617 tc_action_net_exit(net_list, gate_net_id);
620 static struct pernet_operations gate_net_ops = {
621 .init = gate_init_net,
622 .exit_batch = gate_exit_net,
624 .size = sizeof(struct tc_action_net),
627 static int __init gate_init_module(void)
629 return tcf_register_action(&act_gate_ops, &gate_net_ops);
632 static void __exit gate_cleanup_module(void)
634 tcf_unregister_action(&act_gate_ops, &gate_net_ops);
637 module_init(gate_init_module);
638 module_exit(gate_cleanup_module);
639 MODULE_LICENSE("GPL v2");