xref: /linux/net/sched/act_gate.c (revision 7bb377107c72a40ab7505341f8626c8eb79a0cb7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright 2020 NXP */
3 
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 
18 static unsigned int gate_net_id;
19 static struct tc_action_ops act_gate_ops;
20 
21 static ktime_t gate_get_time(struct tcf_gate *gact)
22 {
23 	ktime_t mono = ktime_get();
24 
25 	switch (gact->tk_offset) {
26 	case TK_OFFS_MAX:
27 		return mono;
28 	default:
29 		return ktime_mono_to_any(mono, gact->tk_offset);
30 	}
31 
32 	return KTIME_MAX;
33 }
34 
35 static int gate_get_start_time(struct tcf_gate *gact, ktime_t *start)
36 {
37 	struct tcf_gate_params *param = &gact->param;
38 	ktime_t now, base, cycle;
39 	u64 n;
40 
41 	base = ns_to_ktime(param->tcfg_basetime);
42 	now = gate_get_time(gact);
43 
44 	if (ktime_after(base, now)) {
45 		*start = base;
46 		return 0;
47 	}
48 
49 	cycle = param->tcfg_cycletime;
50 
51 	/* cycle time should not be zero */
52 	if (!cycle)
53 		return -EFAULT;
54 
55 	n = div64_u64(ktime_sub_ns(now, base), cycle);
56 	*start = ktime_add_ns(base, (n + 1) * cycle);
57 	return 0;
58 }
59 
60 static void gate_start_timer(struct tcf_gate *gact, ktime_t start)
61 {
62 	ktime_t expires;
63 
64 	expires = hrtimer_get_expires(&gact->hitimer);
65 	if (expires == 0)
66 		expires = KTIME_MAX;
67 
68 	start = min_t(ktime_t, start, expires);
69 
70 	hrtimer_start(&gact->hitimer, start, HRTIMER_MODE_ABS_SOFT);
71 }
72 
73 static enum hrtimer_restart gate_timer_func(struct hrtimer *timer)
74 {
75 	struct tcf_gate *gact = container_of(timer, struct tcf_gate,
76 					     hitimer);
77 	struct tcf_gate_params *p = &gact->param;
78 	struct tcfg_gate_entry *next;
79 	ktime_t close_time, now;
80 
81 	spin_lock(&gact->tcf_lock);
82 
83 	next = gact->next_entry;
84 
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;
89 
90 	gact->current_close_time = ktime_add_ns(gact->current_close_time,
91 						next->interval);
92 
93 	close_time = gact->current_close_time;
94 
95 	if (list_is_last(&next->list, &p->entries))
96 		next = list_first_entry(&p->entries,
97 					struct tcfg_gate_entry, list);
98 	else
99 		next = list_next_entry(next, list);
100 
101 	now = gate_get_time(gact);
102 
103 	if (ktime_after(now, close_time)) {
104 		ktime_t cycle, base;
105 		u64 n;
106 
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);
111 	}
112 
113 	gact->next_entry = next;
114 
115 	hrtimer_set_expires(&gact->hitimer, close_time);
116 
117 	spin_unlock(&gact->tcf_lock);
118 
119 	return HRTIMER_RESTART;
120 }
121 
122 static int tcf_gate_act(struct sk_buff *skb, const struct tc_action *a,
123 			struct tcf_result *res)
124 {
125 	struct tcf_gate *gact = to_gate(a);
126 
127 	spin_lock(&gact->tcf_lock);
128 
129 	tcf_lastuse_update(&gact->tcf_tm);
130 	bstats_update(&gact->tcf_bstats, skb);
131 
132 	if (unlikely(gact->current_gate_status & GATE_ACT_PENDING)) {
133 		spin_unlock(&gact->tcf_lock);
134 		return gact->tcf_action;
135 	}
136 
137 	if (!(gact->current_gate_status & GATE_ACT_GATE_OPEN))
138 		goto drop;
139 
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++;
144 			goto drop;
145 		}
146 	}
147 
148 	spin_unlock(&gact->tcf_lock);
149 
150 	return gact->tcf_action;
151 drop:
152 	gact->tcf_qstats.drops++;
153 	spin_unlock(&gact->tcf_lock);
154 
155 	return TC_ACT_SHOT;
156 }
157 
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 },
164 };
165 
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 },
176 };
177 
178 static int fill_gate_entry(struct nlattr **tb, struct tcfg_gate_entry *entry,
179 			   struct netlink_ext_ack *extack)
180 {
181 	u32 interval = 0;
182 
183 	entry->gate_state = nla_get_flag(tb[TCA_GATE_ENTRY_GATE]);
184 
185 	if (tb[TCA_GATE_ENTRY_INTERVAL])
186 		interval = nla_get_u32(tb[TCA_GATE_ENTRY_INTERVAL]);
187 
188 	if (interval == 0) {
189 		NL_SET_ERR_MSG(extack, "Invalid interval for schedule entry");
190 		return -EINVAL;
191 	}
192 
193 	entry->interval = interval;
194 
195 	if (tb[TCA_GATE_ENTRY_IPV])
196 		entry->ipv = nla_get_s32(tb[TCA_GATE_ENTRY_IPV]);
197 	else
198 		entry->ipv = -1;
199 
200 	if (tb[TCA_GATE_ENTRY_MAX_OCTETS])
201 		entry->maxoctets = nla_get_s32(tb[TCA_GATE_ENTRY_MAX_OCTETS]);
202 	else
203 		entry->maxoctets = -1;
204 
205 	return 0;
206 }
207 
208 static int parse_gate_entry(struct nlattr *n, struct  tcfg_gate_entry *entry,
209 			    int index, struct netlink_ext_ack *extack)
210 {
211 	struct nlattr *tb[TCA_GATE_ENTRY_MAX + 1] = { };
212 	int err;
213 
214 	err = nla_parse_nested(tb, TCA_GATE_ENTRY_MAX, n, entry_policy, extack);
215 	if (err < 0) {
216 		NL_SET_ERR_MSG(extack, "Could not parse nested entry");
217 		return -EINVAL;
218 	}
219 
220 	entry->index = index;
221 
222 	return fill_gate_entry(tb, entry, extack);
223 }
224 
225 static void release_entry_list(struct list_head *entries)
226 {
227 	struct tcfg_gate_entry *entry, *e;
228 
229 	list_for_each_entry_safe(entry, e, entries, list) {
230 		list_del(&entry->list);
231 		kfree(entry);
232 	}
233 }
234 
235 static int parse_gate_list(struct nlattr *list_attr,
236 			   struct tcf_gate_params *sched,
237 			   struct netlink_ext_ack *extack)
238 {
239 	struct tcfg_gate_entry *entry;
240 	struct nlattr *n;
241 	int err, rem;
242 	int i = 0;
243 
244 	if (!list_attr)
245 		return -EINVAL;
246 
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'");
250 			continue;
251 		}
252 
253 		entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
254 		if (!entry) {
255 			NL_SET_ERR_MSG(extack, "Not enough memory for entry");
256 			err = -ENOMEM;
257 			goto release_list;
258 		}
259 
260 		err = parse_gate_entry(n, entry, i, extack);
261 		if (err < 0) {
262 			kfree(entry);
263 			goto release_list;
264 		}
265 
266 		list_add_tail(&entry->list, &sched->entries);
267 		i++;
268 	}
269 
270 	sched->num_entries = i;
271 
272 	return i;
273 
274 release_list:
275 	release_entry_list(&sched->entries);
276 
277 	return err;
278 }
279 
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)
285 {
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;
294 	int ret = 0, err;
295 	u64 basetime = 0;
296 	u32 gflags = 0;
297 	s32 prio = -1;
298 	ktime_t start;
299 	u32 index;
300 
301 	if (!nla)
302 		return -EINVAL;
303 
304 	err = nla_parse_nested(tb, TCA_GATE_MAX, nla, gate_policy, extack);
305 	if (err < 0)
306 		return err;
307 
308 	if (!tb[TCA_GATE_PARMS])
309 		return -EINVAL;
310 
311 	parm = nla_data(tb[TCA_GATE_PARMS]);
312 	index = parm->index;
313 
314 	err = tcf_idr_check_alloc(tn, &index, a, bind);
315 	if (err < 0)
316 		return err;
317 
318 	if (err && bind)
319 		return 0;
320 
321 	if (!err) {
322 		ret = tcf_idr_create(tn, index, est, a,
323 				     &act_gate_ops, bind, false, 0);
324 		if (ret) {
325 			tcf_idr_cleanup(tn, index);
326 			return ret;
327 		}
328 
329 		ret = ACT_P_CREATED;
330 	} else if (!ovr) {
331 		tcf_idr_release(*a, bind);
332 		return -EEXIST;
333 	}
334 
335 	if (tb[TCA_GATE_PRIORITY])
336 		prio = nla_get_s32(tb[TCA_GATE_PRIORITY]);
337 
338 	if (tb[TCA_GATE_BASE_TIME])
339 		basetime = nla_get_u64(tb[TCA_GATE_BASE_TIME]);
340 
341 	if (tb[TCA_GATE_FLAGS])
342 		gflags = nla_get_u32(tb[TCA_GATE_FLAGS]);
343 
344 	if (tb[TCA_GATE_CLOCKID]) {
345 		clockid = nla_get_s32(tb[TCA_GATE_CLOCKID]);
346 		switch (clockid) {
347 		case CLOCK_REALTIME:
348 			tk_offset = TK_OFFS_REAL;
349 			break;
350 		case CLOCK_MONOTONIC:
351 			tk_offset = TK_OFFS_MAX;
352 			break;
353 		case CLOCK_BOOTTIME:
354 			tk_offset = TK_OFFS_BOOT;
355 			break;
356 		case CLOCK_TAI:
357 			tk_offset = TK_OFFS_TAI;
358 			break;
359 		default:
360 			NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
361 			goto release_idr;
362 		}
363 	}
364 
365 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
366 	if (err < 0)
367 		goto release_idr;
368 
369 	gact = to_gate(*a);
370 
371 	spin_lock_bh(&gact->tcf_lock);
372 	p = &gact->param;
373 
374 	if (tb[TCA_GATE_CYCLE_TIME]) {
375 		p->tcfg_cycletime = nla_get_u64(tb[TCA_GATE_CYCLE_TIME]);
376 		if (!p->tcfg_cycletime_ext)
377 			goto chain_put;
378 	}
379 
380 	INIT_LIST_HEAD(&p->entries);
381 	if (tb[TCA_GATE_ENTRY_LIST]) {
382 		err = parse_gate_list(tb[TCA_GATE_ENTRY_LIST], p, extack);
383 		if (err < 0)
384 			goto chain_put;
385 	}
386 
387 	if (!p->tcfg_cycletime) {
388 		struct tcfg_gate_entry *entry;
389 		ktime_t cycle = 0;
390 
391 		list_for_each_entry(entry, &p->entries, list)
392 			cycle = ktime_add_ns(cycle, entry->interval);
393 		p->tcfg_cycletime = cycle;
394 	}
395 
396 	if (tb[TCA_GATE_CYCLE_TIME_EXT])
397 		p->tcfg_cycletime_ext =
398 			nla_get_u64(tb[TCA_GATE_CYCLE_TIME_EXT]);
399 
400 	p->tcfg_priority = prio;
401 	p->tcfg_basetime = basetime;
402 	p->tcfg_clockid = clockid;
403 	p->tcfg_flags = gflags;
404 
405 	gact->tk_offset = tk_offset;
406 	hrtimer_init(&gact->hitimer, clockid, HRTIMER_MODE_ABS_SOFT);
407 	gact->hitimer.function = gate_timer_func;
408 
409 	err = gate_get_start_time(gact, &start);
410 	if (err < 0) {
411 		NL_SET_ERR_MSG(extack,
412 			       "Internal error: failed get start time");
413 		release_entry_list(&p->entries);
414 		goto chain_put;
415 	}
416 
417 	gact->current_close_time = start;
418 	gact->current_gate_status = GATE_ACT_GATE_OPEN | GATE_ACT_PENDING;
419 
420 	gact->next_entry = list_first_entry(&p->entries,
421 					    struct tcfg_gate_entry, list);
422 
423 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
424 
425 	gate_start_timer(gact, start);
426 
427 	spin_unlock_bh(&gact->tcf_lock);
428 
429 	if (goto_ch)
430 		tcf_chain_put_by_act(goto_ch);
431 
432 	if (ret == ACT_P_CREATED)
433 		tcf_idr_insert(tn, *a);
434 
435 	return ret;
436 
437 chain_put:
438 	spin_unlock_bh(&gact->tcf_lock);
439 
440 	if (goto_ch)
441 		tcf_chain_put_by_act(goto_ch);
442 release_idr:
443 	tcf_idr_release(*a, bind);
444 	return err;
445 }
446 
447 static void tcf_gate_cleanup(struct tc_action *a)
448 {
449 	struct tcf_gate *gact = to_gate(a);
450 	struct tcf_gate_params *p;
451 
452 	hrtimer_cancel(&gact->hitimer);
453 
454 	p = &gact->param;
455 
456 	release_entry_list(&p->entries);
457 }
458 
459 static int dumping_entry(struct sk_buff *skb,
460 			 struct tcfg_gate_entry *entry)
461 {
462 	struct nlattr *item;
463 
464 	item = nla_nest_start_noflag(skb, TCA_GATE_ONE_ENTRY);
465 	if (!item)
466 		return -ENOSPC;
467 
468 	if (nla_put_u32(skb, TCA_GATE_ENTRY_INDEX, entry->index))
469 		goto nla_put_failure;
470 
471 	if (entry->gate_state && nla_put_flag(skb, TCA_GATE_ENTRY_GATE))
472 		goto nla_put_failure;
473 
474 	if (nla_put_u32(skb, TCA_GATE_ENTRY_INTERVAL, entry->interval))
475 		goto nla_put_failure;
476 
477 	if (nla_put_s32(skb, TCA_GATE_ENTRY_MAX_OCTETS, entry->maxoctets))
478 		goto nla_put_failure;
479 
480 	if (nla_put_s32(skb, TCA_GATE_ENTRY_IPV, entry->ipv))
481 		goto nla_put_failure;
482 
483 	return nla_nest_end(skb, item);
484 
485 nla_put_failure:
486 	nla_nest_cancel(skb, item);
487 	return -1;
488 }
489 
490 static int tcf_gate_dump(struct sk_buff *skb, struct tc_action *a,
491 			 int bind, int ref)
492 {
493 	unsigned char *b = skb_tail_pointer(skb);
494 	struct tcf_gate *gact = to_gate(a);
495 	struct tc_gate opt = {
496 		.index    = gact->tcf_index,
497 		.refcnt   = refcount_read(&gact->tcf_refcnt) - ref,
498 		.bindcnt  = atomic_read(&gact->tcf_bindcnt) - bind,
499 	};
500 	struct tcfg_gate_entry *entry;
501 	struct tcf_gate_params *p;
502 	struct nlattr *entry_list;
503 	struct tcf_t t;
504 
505 	spin_lock_bh(&gact->tcf_lock);
506 	opt.action = gact->tcf_action;
507 
508 	p = &gact->param;
509 
510 	if (nla_put(skb, TCA_GATE_PARMS, sizeof(opt), &opt))
511 		goto nla_put_failure;
512 
513 	if (nla_put_u64_64bit(skb, TCA_GATE_BASE_TIME,
514 			      p->tcfg_basetime, TCA_GATE_PAD))
515 		goto nla_put_failure;
516 
517 	if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME,
518 			      p->tcfg_cycletime, TCA_GATE_PAD))
519 		goto nla_put_failure;
520 
521 	if (nla_put_u64_64bit(skb, TCA_GATE_CYCLE_TIME_EXT,
522 			      p->tcfg_cycletime_ext, TCA_GATE_PAD))
523 		goto nla_put_failure;
524 
525 	if (nla_put_s32(skb, TCA_GATE_CLOCKID, p->tcfg_clockid))
526 		goto nla_put_failure;
527 
528 	if (nla_put_u32(skb, TCA_GATE_FLAGS, p->tcfg_flags))
529 		goto nla_put_failure;
530 
531 	if (nla_put_s32(skb, TCA_GATE_PRIORITY, p->tcfg_priority))
532 		goto nla_put_failure;
533 
534 	entry_list = nla_nest_start_noflag(skb, TCA_GATE_ENTRY_LIST);
535 	if (!entry_list)
536 		goto nla_put_failure;
537 
538 	list_for_each_entry(entry, &p->entries, list) {
539 		if (dumping_entry(skb, entry) < 0)
540 			goto nla_put_failure;
541 	}
542 
543 	nla_nest_end(skb, entry_list);
544 
545 	tcf_tm_dump(&t, &gact->tcf_tm);
546 	if (nla_put_64bit(skb, TCA_GATE_TM, sizeof(t), &t, TCA_GATE_PAD))
547 		goto nla_put_failure;
548 	spin_unlock_bh(&gact->tcf_lock);
549 
550 	return skb->len;
551 
552 nla_put_failure:
553 	spin_unlock_bh(&gact->tcf_lock);
554 	nlmsg_trim(skb, b);
555 	return -1;
556 }
557 
558 static int tcf_gate_walker(struct net *net, struct sk_buff *skb,
559 			   struct netlink_callback *cb, int type,
560 			   const struct tc_action_ops *ops,
561 			   struct netlink_ext_ack *extack)
562 {
563 	struct tc_action_net *tn = net_generic(net, gate_net_id);
564 
565 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
566 }
567 
568 static void tcf_gate_stats_update(struct tc_action *a, u64 bytes, u32 packets,
569 				  u64 lastuse, bool hw)
570 {
571 	struct tcf_gate *gact = to_gate(a);
572 	struct tcf_t *tm = &gact->tcf_tm;
573 
574 	tcf_action_update_stats(a, bytes, packets, false, hw);
575 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
576 }
577 
578 static int tcf_gate_search(struct net *net, struct tc_action **a, u32 index)
579 {
580 	struct tc_action_net *tn = net_generic(net, gate_net_id);
581 
582 	return tcf_idr_search(tn, a, index);
583 }
584 
585 static size_t tcf_gate_get_fill_size(const struct tc_action *act)
586 {
587 	return nla_total_size(sizeof(struct tc_gate));
588 }
589 
590 static struct tc_action_ops act_gate_ops = {
591 	.kind		=	"gate",
592 	.id		=	TCA_ID_GATE,
593 	.owner		=	THIS_MODULE,
594 	.act		=	tcf_gate_act,
595 	.dump		=	tcf_gate_dump,
596 	.init		=	tcf_gate_init,
597 	.cleanup	=	tcf_gate_cleanup,
598 	.walk		=	tcf_gate_walker,
599 	.stats_update	=	tcf_gate_stats_update,
600 	.get_fill_size	=	tcf_gate_get_fill_size,
601 	.lookup		=	tcf_gate_search,
602 	.size		=	sizeof(struct tcf_gate),
603 };
604 
605 static __net_init int gate_init_net(struct net *net)
606 {
607 	struct tc_action_net *tn = net_generic(net, gate_net_id);
608 
609 	return tc_action_net_init(net, tn, &act_gate_ops);
610 }
611 
612 static void __net_exit gate_exit_net(struct list_head *net_list)
613 {
614 	tc_action_net_exit(net_list, gate_net_id);
615 }
616 
617 static struct pernet_operations gate_net_ops = {
618 	.init = gate_init_net,
619 	.exit_batch = gate_exit_net,
620 	.id   = &gate_net_id,
621 	.size = sizeof(struct tc_action_net),
622 };
623 
624 static int __init gate_init_module(void)
625 {
626 	return tcf_register_action(&act_gate_ops, &gate_net_ops);
627 }
628 
629 static void __exit gate_cleanup_module(void)
630 {
631 	tcf_unregister_action(&act_gate_ops, &gate_net_ops);
632 }
633 
634 module_init(gate_init_module);
635 module_exit(gate_cleanup_module);
636 MODULE_LICENSE("GPL v2");
637