xref: /dragonfly/sys/netgraph7/netgraph2.h (revision 9348a738)
1 /*
2  * Copyright (c) 2014 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of The DragonFly Project nor the names of its
13  *    contributors may be used to endorse or promote products derived
14  *    from this software without specific, prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef _NETGRAPH_NETGRAPH2_H_
31 #define _NETGRAPH_NETGRAPH2_H_
32 
33 /*
34  * This header implements the netgraph kernel public inline functions.
35  */
36 
37 #ifndef _KERNEL
38 #error "kernel only header file"
39 #endif
40 
41 #ifndef _SYS_THREAD_H_
42 #include <sys/thread.h>
43 #endif
44 #ifndef _NETGRAPH_NETGRAPH_H_
45 #include <netgraph7/netgraph.h>
46 #endif
47 
48 extern struct lwkt_port *ng_msgport[MAXCPU];
49 
50 /*
51  * Return the message port for the netgraph message servicing
52  * thread for a particular cpu.
53  */
54 static __inline lwkt_port_t
55 ng_cpuport(int cpu)
56 {
57 	KKASSERT(cpu >= 0 && cpu < ncpus);
58 	return ng_msgport[cpu];
59 }
60 
61 /*
62  * Grab a reference on the item.
63  */
64 static __inline void
65 ng_ref_item(item_p item)
66 {
67 	refcount_acquire(&item->refs);
68 }
69 
70 /*
71  * Release a reference on the item.
72  *
73  * When the last reference is released, call the item's callback function if
74  * there is one and free the item.
75  */
76 static __inline void
77 ng_unref_item(item_p item, int error)
78 {
79 	if (refcount_release(&item->refs)) {
80 		/* We have released the last reference */
81 		if (item->apply != NULL) {
82 			KKASSERT(item->apply->apply != NULL);
83 			(*item->apply->apply)(item->apply->context, error);
84 			ng_free_apply(item->apply);
85 			item->apply = NULL;
86 		}
87 		ng_free_item(item);
88 	}
89 }
90 
91 #endif	/* _NETGRAPH_NETGRAPH2_H_ */
92