xref: /linux/net/sched/cls_u32.c (revision b2d0f5d5dc53532e6f07bc546a476a55ebdfe0f3)
1 /*
2  * net/sched/cls_u32.c	Ugly (or Universal) 32bit key Packet Classifier.
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  *	The filters are packed to hash tables of key nodes
12  *	with a set of 32bit key/mask pairs at every node.
13  *	Nodes reference next level hash tables etc.
14  *
15  *	This scheme is the best universal classifier I managed to
16  *	invent; it is not super-fast, but it is not slow (provided you
17  *	program it correctly), and general enough.  And its relative
18  *	speed grows as the number of rules becomes larger.
19  *
20  *	It seems that it represents the best middle point between
21  *	speed and manageability both by human and by machine.
22  *
23  *	It is especially useful for link sharing combined with QoS;
24  *	pure RSVP doesn't need such a general approach and can use
25  *	much simpler (and faster) schemes, sort of cls_rsvp.c.
26  *
27  *	JHS: We should remove the CONFIG_NET_CLS_IND from here
28  *	eventually when the meta match extension is made available
29  *
30  *	nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
31  */
32 
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/string.h>
38 #include <linux/errno.h>
39 #include <linux/percpu.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/skbuff.h>
42 #include <linux/bitmap.h>
43 #include <linux/netdevice.h>
44 #include <linux/hash.h>
45 #include <net/netlink.h>
46 #include <net/act_api.h>
47 #include <net/pkt_cls.h>
48 #include <linux/netdevice.h>
49 #include <linux/idr.h>
50 
51 struct tc_u_knode {
52 	struct tc_u_knode __rcu	*next;
53 	u32			handle;
54 	struct tc_u_hnode __rcu	*ht_up;
55 	struct tcf_exts		exts;
56 #ifdef CONFIG_NET_CLS_IND
57 	int			ifindex;
58 #endif
59 	u8			fshift;
60 	struct tcf_result	res;
61 	struct tc_u_hnode __rcu	*ht_down;
62 #ifdef CONFIG_CLS_U32_PERF
63 	struct tc_u32_pcnt __percpu *pf;
64 #endif
65 	u32			flags;
66 #ifdef CONFIG_CLS_U32_MARK
67 	u32			val;
68 	u32			mask;
69 	u32 __percpu		*pcpu_success;
70 #endif
71 	struct tcf_proto	*tp;
72 	union {
73 		struct work_struct	work;
74 		struct rcu_head		rcu;
75 	};
76 	/* The 'sel' field MUST be the last field in structure to allow for
77 	 * tc_u32_keys allocated at end of structure.
78 	 */
79 	struct tc_u32_sel	sel;
80 };
81 
82 struct tc_u_hnode {
83 	struct tc_u_hnode __rcu	*next;
84 	u32			handle;
85 	u32			prio;
86 	struct tc_u_common	*tp_c;
87 	int			refcnt;
88 	unsigned int		divisor;
89 	struct idr		handle_idr;
90 	struct rcu_head		rcu;
91 	/* The 'ht' field MUST be the last field in structure to allow for
92 	 * more entries allocated at end of structure.
93 	 */
94 	struct tc_u_knode __rcu	*ht[1];
95 };
96 
97 struct tc_u_common {
98 	struct tc_u_hnode __rcu	*hlist;
99 	struct tcf_block	*block;
100 	int			refcnt;
101 	struct idr		handle_idr;
102 	struct hlist_node	hnode;
103 	struct rcu_head		rcu;
104 };
105 
106 static inline unsigned int u32_hash_fold(__be32 key,
107 					 const struct tc_u32_sel *sel,
108 					 u8 fshift)
109 {
110 	unsigned int h = ntohl(key & sel->hmask) >> fshift;
111 
112 	return h;
113 }
114 
115 static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
116 			struct tcf_result *res)
117 {
118 	struct {
119 		struct tc_u_knode *knode;
120 		unsigned int	  off;
121 	} stack[TC_U32_MAXDEPTH];
122 
123 	struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
124 	unsigned int off = skb_network_offset(skb);
125 	struct tc_u_knode *n;
126 	int sdepth = 0;
127 	int off2 = 0;
128 	int sel = 0;
129 #ifdef CONFIG_CLS_U32_PERF
130 	int j;
131 #endif
132 	int i, r;
133 
134 next_ht:
135 	n = rcu_dereference_bh(ht->ht[sel]);
136 
137 next_knode:
138 	if (n) {
139 		struct tc_u32_key *key = n->sel.keys;
140 
141 #ifdef CONFIG_CLS_U32_PERF
142 		__this_cpu_inc(n->pf->rcnt);
143 		j = 0;
144 #endif
145 
146 		if (tc_skip_sw(n->flags)) {
147 			n = rcu_dereference_bh(n->next);
148 			goto next_knode;
149 		}
150 
151 #ifdef CONFIG_CLS_U32_MARK
152 		if ((skb->mark & n->mask) != n->val) {
153 			n = rcu_dereference_bh(n->next);
154 			goto next_knode;
155 		} else {
156 			__this_cpu_inc(*n->pcpu_success);
157 		}
158 #endif
159 
160 		for (i = n->sel.nkeys; i > 0; i--, key++) {
161 			int toff = off + key->off + (off2 & key->offmask);
162 			__be32 *data, hdata;
163 
164 			if (skb_headroom(skb) + toff > INT_MAX)
165 				goto out;
166 
167 			data = skb_header_pointer(skb, toff, 4, &hdata);
168 			if (!data)
169 				goto out;
170 			if ((*data ^ key->val) & key->mask) {
171 				n = rcu_dereference_bh(n->next);
172 				goto next_knode;
173 			}
174 #ifdef CONFIG_CLS_U32_PERF
175 			__this_cpu_inc(n->pf->kcnts[j]);
176 			j++;
177 #endif
178 		}
179 
180 		ht = rcu_dereference_bh(n->ht_down);
181 		if (!ht) {
182 check_terminal:
183 			if (n->sel.flags & TC_U32_TERMINAL) {
184 
185 				*res = n->res;
186 #ifdef CONFIG_NET_CLS_IND
187 				if (!tcf_match_indev(skb, n->ifindex)) {
188 					n = rcu_dereference_bh(n->next);
189 					goto next_knode;
190 				}
191 #endif
192 #ifdef CONFIG_CLS_U32_PERF
193 				__this_cpu_inc(n->pf->rhit);
194 #endif
195 				r = tcf_exts_exec(skb, &n->exts, res);
196 				if (r < 0) {
197 					n = rcu_dereference_bh(n->next);
198 					goto next_knode;
199 				}
200 
201 				return r;
202 			}
203 			n = rcu_dereference_bh(n->next);
204 			goto next_knode;
205 		}
206 
207 		/* PUSH */
208 		if (sdepth >= TC_U32_MAXDEPTH)
209 			goto deadloop;
210 		stack[sdepth].knode = n;
211 		stack[sdepth].off = off;
212 		sdepth++;
213 
214 		ht = rcu_dereference_bh(n->ht_down);
215 		sel = 0;
216 		if (ht->divisor) {
217 			__be32 *data, hdata;
218 
219 			data = skb_header_pointer(skb, off + n->sel.hoff, 4,
220 						  &hdata);
221 			if (!data)
222 				goto out;
223 			sel = ht->divisor & u32_hash_fold(*data, &n->sel,
224 							  n->fshift);
225 		}
226 		if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
227 			goto next_ht;
228 
229 		if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
230 			off2 = n->sel.off + 3;
231 			if (n->sel.flags & TC_U32_VAROFFSET) {
232 				__be16 *data, hdata;
233 
234 				data = skb_header_pointer(skb,
235 							  off + n->sel.offoff,
236 							  2, &hdata);
237 				if (!data)
238 					goto out;
239 				off2 += ntohs(n->sel.offmask & *data) >>
240 					n->sel.offshift;
241 			}
242 			off2 &= ~3;
243 		}
244 		if (n->sel.flags & TC_U32_EAT) {
245 			off += off2;
246 			off2 = 0;
247 		}
248 
249 		if (off < skb->len)
250 			goto next_ht;
251 	}
252 
253 	/* POP */
254 	if (sdepth--) {
255 		n = stack[sdepth].knode;
256 		ht = rcu_dereference_bh(n->ht_up);
257 		off = stack[sdepth].off;
258 		goto check_terminal;
259 	}
260 out:
261 	return -1;
262 
263 deadloop:
264 	net_warn_ratelimited("cls_u32: dead loop\n");
265 	return -1;
266 }
267 
268 static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
269 {
270 	struct tc_u_hnode *ht;
271 
272 	for (ht = rtnl_dereference(tp_c->hlist);
273 	     ht;
274 	     ht = rtnl_dereference(ht->next))
275 		if (ht->handle == handle)
276 			break;
277 
278 	return ht;
279 }
280 
281 static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
282 {
283 	unsigned int sel;
284 	struct tc_u_knode *n = NULL;
285 
286 	sel = TC_U32_HASH(handle);
287 	if (sel > ht->divisor)
288 		goto out;
289 
290 	for (n = rtnl_dereference(ht->ht[sel]);
291 	     n;
292 	     n = rtnl_dereference(n->next))
293 		if (n->handle == handle)
294 			break;
295 out:
296 	return n;
297 }
298 
299 
300 static void *u32_get(struct tcf_proto *tp, u32 handle)
301 {
302 	struct tc_u_hnode *ht;
303 	struct tc_u_common *tp_c = tp->data;
304 
305 	if (TC_U32_HTID(handle) == TC_U32_ROOT)
306 		ht = rtnl_dereference(tp->root);
307 	else
308 		ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
309 
310 	if (!ht)
311 		return NULL;
312 
313 	if (TC_U32_KEY(handle) == 0)
314 		return ht;
315 
316 	return u32_lookup_key(ht, handle);
317 }
318 
319 static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
320 {
321 	unsigned long idr_index;
322 	int err;
323 
324 	/* This is only used inside rtnl lock it is safe to increment
325 	 * without read _copy_ update semantics
326 	 */
327 	err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index,
328 			    1, 0x7FF, GFP_KERNEL);
329 	if (err)
330 		return 0;
331 	return (u32)(idr_index | 0x800) << 20;
332 }
333 
334 static struct hlist_head *tc_u_common_hash;
335 
336 #define U32_HASH_SHIFT 10
337 #define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
338 
339 static unsigned int tc_u_hash(const struct tcf_proto *tp)
340 {
341 	return hash_ptr(tp->chain->block, U32_HASH_SHIFT);
342 }
343 
344 static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp)
345 {
346 	struct tc_u_common *tc;
347 	unsigned int h;
348 
349 	h = tc_u_hash(tp);
350 	hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) {
351 		if (tc->block == tp->chain->block)
352 			return tc;
353 	}
354 	return NULL;
355 }
356 
357 static int u32_init(struct tcf_proto *tp)
358 {
359 	struct tc_u_hnode *root_ht;
360 	struct tc_u_common *tp_c;
361 	unsigned int h;
362 
363 	tp_c = tc_u_common_find(tp);
364 
365 	root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
366 	if (root_ht == NULL)
367 		return -ENOBUFS;
368 
369 	root_ht->refcnt++;
370 	root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
371 	root_ht->prio = tp->prio;
372 	idr_init(&root_ht->handle_idr);
373 
374 	if (tp_c == NULL) {
375 		tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
376 		if (tp_c == NULL) {
377 			kfree(root_ht);
378 			return -ENOBUFS;
379 		}
380 		tp_c->block = tp->chain->block;
381 		INIT_HLIST_NODE(&tp_c->hnode);
382 		idr_init(&tp_c->handle_idr);
383 
384 		h = tc_u_hash(tp);
385 		hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
386 	}
387 
388 	tp_c->refcnt++;
389 	RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
390 	rcu_assign_pointer(tp_c->hlist, root_ht);
391 	root_ht->tp_c = tp_c;
392 
393 	rcu_assign_pointer(tp->root, root_ht);
394 	tp->data = tp_c;
395 	return 0;
396 }
397 
398 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
399 			   bool free_pf)
400 {
401 	tcf_exts_destroy(&n->exts);
402 	if (n->ht_down)
403 		n->ht_down->refcnt--;
404 #ifdef CONFIG_CLS_U32_PERF
405 	if (free_pf)
406 		free_percpu(n->pf);
407 #endif
408 #ifdef CONFIG_CLS_U32_MARK
409 	if (free_pf)
410 		free_percpu(n->pcpu_success);
411 #endif
412 	kfree(n);
413 	return 0;
414 }
415 
416 /* u32_delete_key_rcu should be called when free'ing a copied
417  * version of a tc_u_knode obtained from u32_init_knode(). When
418  * copies are obtained from u32_init_knode() the statistics are
419  * shared between the old and new copies to allow readers to
420  * continue to update the statistics during the copy. To support
421  * this the u32_delete_key_rcu variant does not free the percpu
422  * statistics.
423  */
424 static void u32_delete_key_work(struct work_struct *work)
425 {
426 	struct tc_u_knode *key = container_of(work, struct tc_u_knode, work);
427 
428 	rtnl_lock();
429 	u32_destroy_key(key->tp, key, false);
430 	rtnl_unlock();
431 }
432 
433 static void u32_delete_key_rcu(struct rcu_head *rcu)
434 {
435 	struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
436 
437 	INIT_WORK(&key->work, u32_delete_key_work);
438 	tcf_queue_work(&key->work);
439 }
440 
441 /* u32_delete_key_freepf_rcu is the rcu callback variant
442  * that free's the entire structure including the statistics
443  * percpu variables. Only use this if the key is not a copy
444  * returned by u32_init_knode(). See u32_delete_key_rcu()
445  * for the variant that should be used with keys return from
446  * u32_init_knode()
447  */
448 static void u32_delete_key_freepf_work(struct work_struct *work)
449 {
450 	struct tc_u_knode *key = container_of(work, struct tc_u_knode, work);
451 
452 	rtnl_lock();
453 	u32_destroy_key(key->tp, key, true);
454 	rtnl_unlock();
455 }
456 
457 static void u32_delete_key_freepf_rcu(struct rcu_head *rcu)
458 {
459 	struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
460 
461 	INIT_WORK(&key->work, u32_delete_key_freepf_work);
462 	tcf_queue_work(&key->work);
463 }
464 
465 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
466 {
467 	struct tc_u_knode __rcu **kp;
468 	struct tc_u_knode *pkp;
469 	struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
470 
471 	if (ht) {
472 		kp = &ht->ht[TC_U32_HASH(key->handle)];
473 		for (pkp = rtnl_dereference(*kp); pkp;
474 		     kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
475 			if (pkp == key) {
476 				RCU_INIT_POINTER(*kp, key->next);
477 
478 				tcf_unbind_filter(tp, &key->res);
479 				call_rcu(&key->rcu, u32_delete_key_freepf_rcu);
480 				return 0;
481 			}
482 		}
483 	}
484 	WARN_ON(1);
485 	return 0;
486 }
487 
488 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
489 {
490 	struct tcf_block *block = tp->chain->block;
491 	struct tc_cls_u32_offload cls_u32 = {};
492 
493 	tc_cls_common_offload_init(&cls_u32.common, tp);
494 	cls_u32.command = TC_CLSU32_DELETE_HNODE;
495 	cls_u32.hnode.divisor = h->divisor;
496 	cls_u32.hnode.handle = h->handle;
497 	cls_u32.hnode.prio = h->prio;
498 
499 	tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
500 }
501 
502 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
503 				u32 flags)
504 {
505 	struct tcf_block *block = tp->chain->block;
506 	struct tc_cls_u32_offload cls_u32 = {};
507 	bool skip_sw = tc_skip_sw(flags);
508 	bool offloaded = false;
509 	int err;
510 
511 	tc_cls_common_offload_init(&cls_u32.common, tp);
512 	cls_u32.command = TC_CLSU32_NEW_HNODE;
513 	cls_u32.hnode.divisor = h->divisor;
514 	cls_u32.hnode.handle = h->handle;
515 	cls_u32.hnode.prio = h->prio;
516 
517 	err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
518 	if (err < 0) {
519 		u32_clear_hw_hnode(tp, h);
520 		return err;
521 	} else if (err > 0) {
522 		offloaded = true;
523 	}
524 
525 	if (skip_sw && !offloaded)
526 		return -EINVAL;
527 
528 	return 0;
529 }
530 
531 static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
532 {
533 	struct tcf_block *block = tp->chain->block;
534 	struct tc_cls_u32_offload cls_u32 = {};
535 
536 	tc_cls_common_offload_init(&cls_u32.common, tp);
537 	cls_u32.command = TC_CLSU32_DELETE_KNODE;
538 	cls_u32.knode.handle = handle;
539 
540 	tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
541 }
542 
543 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
544 				u32 flags)
545 {
546 	struct tcf_block *block = tp->chain->block;
547 	struct tc_cls_u32_offload cls_u32 = {};
548 	bool skip_sw = tc_skip_sw(flags);
549 	int err;
550 
551 	tc_cls_common_offload_init(&cls_u32.common, tp);
552 	cls_u32.command = TC_CLSU32_REPLACE_KNODE;
553 	cls_u32.knode.handle = n->handle;
554 	cls_u32.knode.fshift = n->fshift;
555 #ifdef CONFIG_CLS_U32_MARK
556 	cls_u32.knode.val = n->val;
557 	cls_u32.knode.mask = n->mask;
558 #else
559 	cls_u32.knode.val = 0;
560 	cls_u32.knode.mask = 0;
561 #endif
562 	cls_u32.knode.sel = &n->sel;
563 	cls_u32.knode.exts = &n->exts;
564 	if (n->ht_down)
565 		cls_u32.knode.link_handle = n->ht_down->handle;
566 
567 	err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
568 	if (err < 0) {
569 		u32_remove_hw_knode(tp, n->handle);
570 		return err;
571 	} else if (err > 0) {
572 		n->flags |= TCA_CLS_FLAGS_IN_HW;
573 	}
574 
575 	if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
576 		return -EINVAL;
577 
578 	return 0;
579 }
580 
581 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
582 {
583 	struct tc_u_knode *n;
584 	unsigned int h;
585 
586 	for (h = 0; h <= ht->divisor; h++) {
587 		while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
588 			RCU_INIT_POINTER(ht->ht[h],
589 					 rtnl_dereference(n->next));
590 			tcf_unbind_filter(tp, &n->res);
591 			u32_remove_hw_knode(tp, n->handle);
592 			idr_remove_ext(&ht->handle_idr, n->handle);
593 			call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
594 		}
595 	}
596 }
597 
598 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
599 {
600 	struct tc_u_common *tp_c = tp->data;
601 	struct tc_u_hnode __rcu **hn;
602 	struct tc_u_hnode *phn;
603 
604 	WARN_ON(ht->refcnt);
605 
606 	u32_clear_hnode(tp, ht);
607 
608 	hn = &tp_c->hlist;
609 	for (phn = rtnl_dereference(*hn);
610 	     phn;
611 	     hn = &phn->next, phn = rtnl_dereference(*hn)) {
612 		if (phn == ht) {
613 			u32_clear_hw_hnode(tp, ht);
614 			idr_destroy(&ht->handle_idr);
615 			idr_remove_ext(&tp_c->handle_idr, ht->handle);
616 			RCU_INIT_POINTER(*hn, ht->next);
617 			kfree_rcu(ht, rcu);
618 			return 0;
619 		}
620 	}
621 
622 	return -ENOENT;
623 }
624 
625 static bool ht_empty(struct tc_u_hnode *ht)
626 {
627 	unsigned int h;
628 
629 	for (h = 0; h <= ht->divisor; h++)
630 		if (rcu_access_pointer(ht->ht[h]))
631 			return false;
632 
633 	return true;
634 }
635 
636 static void u32_destroy(struct tcf_proto *tp)
637 {
638 	struct tc_u_common *tp_c = tp->data;
639 	struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
640 
641 	WARN_ON(root_ht == NULL);
642 
643 	if (root_ht && --root_ht->refcnt == 0)
644 		u32_destroy_hnode(tp, root_ht);
645 
646 	if (--tp_c->refcnt == 0) {
647 		struct tc_u_hnode *ht;
648 
649 		hlist_del(&tp_c->hnode);
650 
651 		for (ht = rtnl_dereference(tp_c->hlist);
652 		     ht;
653 		     ht = rtnl_dereference(ht->next)) {
654 			ht->refcnt--;
655 			u32_clear_hnode(tp, ht);
656 		}
657 
658 		while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
659 			RCU_INIT_POINTER(tp_c->hlist, ht->next);
660 			kfree_rcu(ht, rcu);
661 		}
662 
663 		idr_destroy(&tp_c->handle_idr);
664 		kfree(tp_c);
665 	}
666 
667 	tp->data = NULL;
668 }
669 
670 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last)
671 {
672 	struct tc_u_hnode *ht = arg;
673 	struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
674 	struct tc_u_common *tp_c = tp->data;
675 	int ret = 0;
676 
677 	if (ht == NULL)
678 		goto out;
679 
680 	if (TC_U32_KEY(ht->handle)) {
681 		u32_remove_hw_knode(tp, ht->handle);
682 		ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
683 		goto out;
684 	}
685 
686 	if (root_ht == ht)
687 		return -EINVAL;
688 
689 	if (ht->refcnt == 1) {
690 		ht->refcnt--;
691 		u32_destroy_hnode(tp, ht);
692 	} else {
693 		return -EBUSY;
694 	}
695 
696 out:
697 	*last = true;
698 	if (root_ht) {
699 		if (root_ht->refcnt > 1) {
700 			*last = false;
701 			goto ret;
702 		}
703 		if (root_ht->refcnt == 1) {
704 			if (!ht_empty(root_ht)) {
705 				*last = false;
706 				goto ret;
707 			}
708 		}
709 	}
710 
711 	if (tp_c->refcnt > 1) {
712 		*last = false;
713 		goto ret;
714 	}
715 
716 	if (tp_c->refcnt == 1) {
717 		struct tc_u_hnode *ht;
718 
719 		for (ht = rtnl_dereference(tp_c->hlist);
720 		     ht;
721 		     ht = rtnl_dereference(ht->next))
722 			if (!ht_empty(ht)) {
723 				*last = false;
724 				break;
725 			}
726 	}
727 
728 ret:
729 	return ret;
730 }
731 
732 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
733 {
734 	unsigned long idr_index;
735 	u32 start = htid | 0x800;
736 	u32 max = htid | 0xFFF;
737 	u32 min = htid;
738 
739 	if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
740 			  start, max + 1, GFP_KERNEL)) {
741 		if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
742 				  min + 1, max + 1, GFP_KERNEL))
743 			return max;
744 	}
745 
746 	return (u32)idr_index;
747 }
748 
749 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
750 	[TCA_U32_CLASSID]	= { .type = NLA_U32 },
751 	[TCA_U32_HASH]		= { .type = NLA_U32 },
752 	[TCA_U32_LINK]		= { .type = NLA_U32 },
753 	[TCA_U32_DIVISOR]	= { .type = NLA_U32 },
754 	[TCA_U32_SEL]		= { .len = sizeof(struct tc_u32_sel) },
755 	[TCA_U32_INDEV]		= { .type = NLA_STRING, .len = IFNAMSIZ },
756 	[TCA_U32_MARK]		= { .len = sizeof(struct tc_u32_mark) },
757 	[TCA_U32_FLAGS]		= { .type = NLA_U32 },
758 };
759 
760 static int u32_set_parms(struct net *net, struct tcf_proto *tp,
761 			 unsigned long base, struct tc_u_hnode *ht,
762 			 struct tc_u_knode *n, struct nlattr **tb,
763 			 struct nlattr *est, bool ovr)
764 {
765 	int err;
766 
767 	err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr);
768 	if (err < 0)
769 		return err;
770 
771 	if (tb[TCA_U32_LINK]) {
772 		u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
773 		struct tc_u_hnode *ht_down = NULL, *ht_old;
774 
775 		if (TC_U32_KEY(handle))
776 			return -EINVAL;
777 
778 		if (handle) {
779 			ht_down = u32_lookup_ht(ht->tp_c, handle);
780 
781 			if (ht_down == NULL)
782 				return -EINVAL;
783 			ht_down->refcnt++;
784 		}
785 
786 		ht_old = rtnl_dereference(n->ht_down);
787 		rcu_assign_pointer(n->ht_down, ht_down);
788 
789 		if (ht_old)
790 			ht_old->refcnt--;
791 	}
792 	if (tb[TCA_U32_CLASSID]) {
793 		n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
794 		tcf_bind_filter(tp, &n->res, base);
795 	}
796 
797 #ifdef CONFIG_NET_CLS_IND
798 	if (tb[TCA_U32_INDEV]) {
799 		int ret;
800 		ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
801 		if (ret < 0)
802 			return -EINVAL;
803 		n->ifindex = ret;
804 	}
805 #endif
806 	return 0;
807 }
808 
809 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
810 			      struct tc_u_knode *n)
811 {
812 	struct tc_u_knode __rcu **ins;
813 	struct tc_u_knode *pins;
814 	struct tc_u_hnode *ht;
815 
816 	if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
817 		ht = rtnl_dereference(tp->root);
818 	else
819 		ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
820 
821 	ins = &ht->ht[TC_U32_HASH(n->handle)];
822 
823 	/* The node must always exist for it to be replaced if this is not the
824 	 * case then something went very wrong elsewhere.
825 	 */
826 	for (pins = rtnl_dereference(*ins); ;
827 	     ins = &pins->next, pins = rtnl_dereference(*ins))
828 		if (pins->handle == n->handle)
829 			break;
830 
831 	idr_replace_ext(&ht->handle_idr, n, n->handle);
832 	RCU_INIT_POINTER(n->next, pins->next);
833 	rcu_assign_pointer(*ins, n);
834 }
835 
836 static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
837 					 struct tc_u_knode *n)
838 {
839 	struct tc_u_knode *new;
840 	struct tc_u32_sel *s = &n->sel;
841 
842 	new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
843 		      GFP_KERNEL);
844 
845 	if (!new)
846 		return NULL;
847 
848 	RCU_INIT_POINTER(new->next, n->next);
849 	new->handle = n->handle;
850 	RCU_INIT_POINTER(new->ht_up, n->ht_up);
851 
852 #ifdef CONFIG_NET_CLS_IND
853 	new->ifindex = n->ifindex;
854 #endif
855 	new->fshift = n->fshift;
856 	new->res = n->res;
857 	new->flags = n->flags;
858 	RCU_INIT_POINTER(new->ht_down, n->ht_down);
859 
860 	/* bump reference count as long as we hold pointer to structure */
861 	if (new->ht_down)
862 		new->ht_down->refcnt++;
863 
864 #ifdef CONFIG_CLS_U32_PERF
865 	/* Statistics may be incremented by readers during update
866 	 * so we must keep them in tact. When the node is later destroyed
867 	 * a special destroy call must be made to not free the pf memory.
868 	 */
869 	new->pf = n->pf;
870 #endif
871 
872 #ifdef CONFIG_CLS_U32_MARK
873 	new->val = n->val;
874 	new->mask = n->mask;
875 	/* Similarly success statistics must be moved as pointers */
876 	new->pcpu_success = n->pcpu_success;
877 #endif
878 	new->tp = tp;
879 	memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
880 
881 	if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
882 		kfree(new);
883 		return NULL;
884 	}
885 
886 	return new;
887 }
888 
889 static int u32_change(struct net *net, struct sk_buff *in_skb,
890 		      struct tcf_proto *tp, unsigned long base, u32 handle,
891 		      struct nlattr **tca, void **arg, bool ovr)
892 {
893 	struct tc_u_common *tp_c = tp->data;
894 	struct tc_u_hnode *ht;
895 	struct tc_u_knode *n;
896 	struct tc_u32_sel *s;
897 	struct nlattr *opt = tca[TCA_OPTIONS];
898 	struct nlattr *tb[TCA_U32_MAX + 1];
899 	u32 htid, flags = 0;
900 	int err;
901 #ifdef CONFIG_CLS_U32_PERF
902 	size_t size;
903 #endif
904 
905 	if (opt == NULL)
906 		return handle ? -EINVAL : 0;
907 
908 	err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL);
909 	if (err < 0)
910 		return err;
911 
912 	if (tb[TCA_U32_FLAGS]) {
913 		flags = nla_get_u32(tb[TCA_U32_FLAGS]);
914 		if (!tc_flags_valid(flags))
915 			return -EINVAL;
916 	}
917 
918 	n = *arg;
919 	if (n) {
920 		struct tc_u_knode *new;
921 
922 		if (TC_U32_KEY(n->handle) == 0)
923 			return -EINVAL;
924 
925 		if (n->flags != flags)
926 			return -EINVAL;
927 
928 		new = u32_init_knode(tp, n);
929 		if (!new)
930 			return -ENOMEM;
931 
932 		err = u32_set_parms(net, tp, base,
933 				    rtnl_dereference(n->ht_up), new, tb,
934 				    tca[TCA_RATE], ovr);
935 
936 		if (err) {
937 			u32_destroy_key(tp, new, false);
938 			return err;
939 		}
940 
941 		err = u32_replace_hw_knode(tp, new, flags);
942 		if (err) {
943 			u32_destroy_key(tp, new, false);
944 			return err;
945 		}
946 
947 		if (!tc_in_hw(new->flags))
948 			new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
949 
950 		u32_replace_knode(tp, tp_c, new);
951 		tcf_unbind_filter(tp, &n->res);
952 		call_rcu(&n->rcu, u32_delete_key_rcu);
953 		return 0;
954 	}
955 
956 	if (tb[TCA_U32_DIVISOR]) {
957 		unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
958 
959 		if (--divisor > 0x100)
960 			return -EINVAL;
961 		if (TC_U32_KEY(handle))
962 			return -EINVAL;
963 		ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
964 		if (ht == NULL)
965 			return -ENOBUFS;
966 		if (handle == 0) {
967 			handle = gen_new_htid(tp->data, ht);
968 			if (handle == 0) {
969 				kfree(ht);
970 				return -ENOMEM;
971 			}
972 		} else {
973 			err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL,
974 					    handle, handle + 1, GFP_KERNEL);
975 			if (err) {
976 				kfree(ht);
977 				return err;
978 			}
979 		}
980 		ht->tp_c = tp_c;
981 		ht->refcnt = 1;
982 		ht->divisor = divisor;
983 		ht->handle = handle;
984 		ht->prio = tp->prio;
985 		idr_init(&ht->handle_idr);
986 
987 		err = u32_replace_hw_hnode(tp, ht, flags);
988 		if (err) {
989 			idr_remove_ext(&tp_c->handle_idr, handle);
990 			kfree(ht);
991 			return err;
992 		}
993 
994 		RCU_INIT_POINTER(ht->next, tp_c->hlist);
995 		rcu_assign_pointer(tp_c->hlist, ht);
996 		*arg = ht;
997 
998 		return 0;
999 	}
1000 
1001 	if (tb[TCA_U32_HASH]) {
1002 		htid = nla_get_u32(tb[TCA_U32_HASH]);
1003 		if (TC_U32_HTID(htid) == TC_U32_ROOT) {
1004 			ht = rtnl_dereference(tp->root);
1005 			htid = ht->handle;
1006 		} else {
1007 			ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
1008 			if (ht == NULL)
1009 				return -EINVAL;
1010 		}
1011 	} else {
1012 		ht = rtnl_dereference(tp->root);
1013 		htid = ht->handle;
1014 	}
1015 
1016 	if (ht->divisor < TC_U32_HASH(htid))
1017 		return -EINVAL;
1018 
1019 	if (handle) {
1020 		if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
1021 			return -EINVAL;
1022 		handle = htid | TC_U32_NODE(handle);
1023 		err = idr_alloc_ext(&ht->handle_idr, NULL, NULL,
1024 				    handle, handle + 1,
1025 				    GFP_KERNEL);
1026 		if (err)
1027 			return err;
1028 	} else
1029 		handle = gen_new_kid(ht, htid);
1030 
1031 	if (tb[TCA_U32_SEL] == NULL) {
1032 		err = -EINVAL;
1033 		goto erridr;
1034 	}
1035 
1036 	s = nla_data(tb[TCA_U32_SEL]);
1037 
1038 	n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
1039 	if (n == NULL) {
1040 		err = -ENOBUFS;
1041 		goto erridr;
1042 	}
1043 
1044 #ifdef CONFIG_CLS_U32_PERF
1045 	size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1046 	n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1047 	if (!n->pf) {
1048 		err = -ENOBUFS;
1049 		goto errfree;
1050 	}
1051 #endif
1052 
1053 	memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
1054 	RCU_INIT_POINTER(n->ht_up, ht);
1055 	n->handle = handle;
1056 	n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
1057 	n->flags = flags;
1058 	n->tp = tp;
1059 
1060 	err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1061 	if (err < 0)
1062 		goto errout;
1063 
1064 #ifdef CONFIG_CLS_U32_MARK
1065 	n->pcpu_success = alloc_percpu(u32);
1066 	if (!n->pcpu_success) {
1067 		err = -ENOMEM;
1068 		goto errout;
1069 	}
1070 
1071 	if (tb[TCA_U32_MARK]) {
1072 		struct tc_u32_mark *mark;
1073 
1074 		mark = nla_data(tb[TCA_U32_MARK]);
1075 		n->val = mark->val;
1076 		n->mask = mark->mask;
1077 	}
1078 #endif
1079 
1080 	err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
1081 	if (err == 0) {
1082 		struct tc_u_knode __rcu **ins;
1083 		struct tc_u_knode *pins;
1084 
1085 		err = u32_replace_hw_knode(tp, n, flags);
1086 		if (err)
1087 			goto errhw;
1088 
1089 		if (!tc_in_hw(n->flags))
1090 			n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1091 
1092 		ins = &ht->ht[TC_U32_HASH(handle)];
1093 		for (pins = rtnl_dereference(*ins); pins;
1094 		     ins = &pins->next, pins = rtnl_dereference(*ins))
1095 			if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
1096 				break;
1097 
1098 		RCU_INIT_POINTER(n->next, pins);
1099 		rcu_assign_pointer(*ins, n);
1100 		*arg = n;
1101 		return 0;
1102 	}
1103 
1104 errhw:
1105 #ifdef CONFIG_CLS_U32_MARK
1106 	free_percpu(n->pcpu_success);
1107 #endif
1108 
1109 errout:
1110 	tcf_exts_destroy(&n->exts);
1111 #ifdef CONFIG_CLS_U32_PERF
1112 errfree:
1113 	free_percpu(n->pf);
1114 #endif
1115 	kfree(n);
1116 erridr:
1117 	idr_remove_ext(&ht->handle_idr, handle);
1118 	return err;
1119 }
1120 
1121 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1122 {
1123 	struct tc_u_common *tp_c = tp->data;
1124 	struct tc_u_hnode *ht;
1125 	struct tc_u_knode *n;
1126 	unsigned int h;
1127 
1128 	if (arg->stop)
1129 		return;
1130 
1131 	for (ht = rtnl_dereference(tp_c->hlist);
1132 	     ht;
1133 	     ht = rtnl_dereference(ht->next)) {
1134 		if (ht->prio != tp->prio)
1135 			continue;
1136 		if (arg->count >= arg->skip) {
1137 			if (arg->fn(tp, ht, arg) < 0) {
1138 				arg->stop = 1;
1139 				return;
1140 			}
1141 		}
1142 		arg->count++;
1143 		for (h = 0; h <= ht->divisor; h++) {
1144 			for (n = rtnl_dereference(ht->ht[h]);
1145 			     n;
1146 			     n = rtnl_dereference(n->next)) {
1147 				if (arg->count < arg->skip) {
1148 					arg->count++;
1149 					continue;
1150 				}
1151 				if (arg->fn(tp, n, arg) < 0) {
1152 					arg->stop = 1;
1153 					return;
1154 				}
1155 				arg->count++;
1156 			}
1157 		}
1158 	}
1159 }
1160 
1161 static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1162 {
1163 	struct tc_u_knode *n = fh;
1164 
1165 	if (n && n->res.classid == classid)
1166 		n->res.class = cl;
1167 }
1168 
1169 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
1170 		    struct sk_buff *skb, struct tcmsg *t)
1171 {
1172 	struct tc_u_knode *n = fh;
1173 	struct tc_u_hnode *ht_up, *ht_down;
1174 	struct nlattr *nest;
1175 
1176 	if (n == NULL)
1177 		return skb->len;
1178 
1179 	t->tcm_handle = n->handle;
1180 
1181 	nest = nla_nest_start(skb, TCA_OPTIONS);
1182 	if (nest == NULL)
1183 		goto nla_put_failure;
1184 
1185 	if (TC_U32_KEY(n->handle) == 0) {
1186 		struct tc_u_hnode *ht = fh;
1187 		u32 divisor = ht->divisor + 1;
1188 
1189 		if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1190 			goto nla_put_failure;
1191 	} else {
1192 #ifdef CONFIG_CLS_U32_PERF
1193 		struct tc_u32_pcnt *gpf;
1194 		int cpu;
1195 #endif
1196 
1197 		if (nla_put(skb, TCA_U32_SEL,
1198 			    sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1199 			    &n->sel))
1200 			goto nla_put_failure;
1201 
1202 		ht_up = rtnl_dereference(n->ht_up);
1203 		if (ht_up) {
1204 			u32 htid = n->handle & 0xFFFFF000;
1205 			if (nla_put_u32(skb, TCA_U32_HASH, htid))
1206 				goto nla_put_failure;
1207 		}
1208 		if (n->res.classid &&
1209 		    nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1210 			goto nla_put_failure;
1211 
1212 		ht_down = rtnl_dereference(n->ht_down);
1213 		if (ht_down &&
1214 		    nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1215 			goto nla_put_failure;
1216 
1217 		if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1218 			goto nla_put_failure;
1219 
1220 #ifdef CONFIG_CLS_U32_MARK
1221 		if ((n->val || n->mask)) {
1222 			struct tc_u32_mark mark = {.val = n->val,
1223 						   .mask = n->mask,
1224 						   .success = 0};
1225 			int cpum;
1226 
1227 			for_each_possible_cpu(cpum) {
1228 				__u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
1229 
1230 				mark.success += cnt;
1231 			}
1232 
1233 			if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1234 				goto nla_put_failure;
1235 		}
1236 #endif
1237 
1238 		if (tcf_exts_dump(skb, &n->exts) < 0)
1239 			goto nla_put_failure;
1240 
1241 #ifdef CONFIG_NET_CLS_IND
1242 		if (n->ifindex) {
1243 			struct net_device *dev;
1244 			dev = __dev_get_by_index(net, n->ifindex);
1245 			if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1246 				goto nla_put_failure;
1247 		}
1248 #endif
1249 #ifdef CONFIG_CLS_U32_PERF
1250 		gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1251 			      n->sel.nkeys * sizeof(u64),
1252 			      GFP_KERNEL);
1253 		if (!gpf)
1254 			goto nla_put_failure;
1255 
1256 		for_each_possible_cpu(cpu) {
1257 			int i;
1258 			struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1259 
1260 			gpf->rcnt += pf->rcnt;
1261 			gpf->rhit += pf->rhit;
1262 			for (i = 0; i < n->sel.nkeys; i++)
1263 				gpf->kcnts[i] += pf->kcnts[i];
1264 		}
1265 
1266 		if (nla_put_64bit(skb, TCA_U32_PCNT,
1267 				  sizeof(struct tc_u32_pcnt) +
1268 				  n->sel.nkeys * sizeof(u64),
1269 				  gpf, TCA_U32_PAD)) {
1270 			kfree(gpf);
1271 			goto nla_put_failure;
1272 		}
1273 		kfree(gpf);
1274 #endif
1275 	}
1276 
1277 	nla_nest_end(skb, nest);
1278 
1279 	if (TC_U32_KEY(n->handle))
1280 		if (tcf_exts_dump_stats(skb, &n->exts) < 0)
1281 			goto nla_put_failure;
1282 	return skb->len;
1283 
1284 nla_put_failure:
1285 	nla_nest_cancel(skb, nest);
1286 	return -1;
1287 }
1288 
1289 static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1290 	.kind		=	"u32",
1291 	.classify	=	u32_classify,
1292 	.init		=	u32_init,
1293 	.destroy	=	u32_destroy,
1294 	.get		=	u32_get,
1295 	.change		=	u32_change,
1296 	.delete		=	u32_delete,
1297 	.walk		=	u32_walk,
1298 	.dump		=	u32_dump,
1299 	.bind_class	=	u32_bind_class,
1300 	.owner		=	THIS_MODULE,
1301 };
1302 
1303 static int __init init_u32(void)
1304 {
1305 	int i, ret;
1306 
1307 	pr_info("u32 classifier\n");
1308 #ifdef CONFIG_CLS_U32_PERF
1309 	pr_info("    Performance counters on\n");
1310 #endif
1311 #ifdef CONFIG_NET_CLS_IND
1312 	pr_info("    input device check on\n");
1313 #endif
1314 #ifdef CONFIG_NET_CLS_ACT
1315 	pr_info("    Actions configured\n");
1316 #endif
1317 	tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1318 					  sizeof(struct hlist_head),
1319 					  GFP_KERNEL);
1320 	if (!tc_u_common_hash)
1321 		return -ENOMEM;
1322 
1323 	for (i = 0; i < U32_HASH_SIZE; i++)
1324 		INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1325 
1326 	ret = register_tcf_proto_ops(&cls_u32_ops);
1327 	if (ret)
1328 		kvfree(tc_u_common_hash);
1329 	return ret;
1330 }
1331 
1332 static void __exit exit_u32(void)
1333 {
1334 	unregister_tcf_proto_ops(&cls_u32_ops);
1335 	kvfree(tc_u_common_hash);
1336 }
1337 
1338 module_init(init_u32)
1339 module_exit(exit_u32)
1340 MODULE_LICENSE("GPL");
1341