1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001-2003
5  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6  * 	All rights reserved.
7  *
8  * Author: Harti Brandt <harti@freebsd.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * Customisation of the SSCFU code to ng_sscfu.
32  *
33  * $FreeBSD: stable/12/sys/netgraph/atm/sscfu/ng_sscfu_cust.h 326272 2017-11-27 15:23:17Z pfg $
34  */
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/kernel.h>
38 #include <sys/mbuf.h>
39 #include <sys/queue.h>
40 #include <sys/callout.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <machine/stdarg.h>
44 
45 /*
46  * Allocate zeroed or non-zeroed memory of some size and cast it.
47  * Return NULL on failure.
48  */
49 #ifndef SSCFU_DEBUG
50 
51 #define	MEMINIT() \
52 	MALLOC_DECLARE(M_NG_SSCFU); \
53 	DECL_SIGQ_GET
54 
55 #define	MEMZALLOC(PTR, CAST, SIZE) \
56 	((PTR) = (CAST)malloc((SIZE), M_NG_SSCFU, M_NOWAIT | M_ZERO))
57 #define	MEMFREE(PTR) \
58 	free(PTR, M_NG_SSCFU)
59 
60 #define	SIG_ALLOC(PTR) \
61 	MEMZALLOC(PTR, struct sscfu_sig *, sizeof(struct sscfu_sig))
62 #define	SIG_FREE(PTR) \
63 	MEMFREE(PTR)
64 
65 #else
66 
67 #define	MEMINIT() 							\
68 	MALLOC_DEFINE(M_NG_SSCFU_INS, "sscfu_ins", "SSCFU instances");	\
69 	MALLOC_DEFINE(M_NG_SSCFU_SIG, "sscfu_sig", "SSCFU signals");	\
70 	DECL_SIGQ_GET
71 
72 #define	MEMZALLOC(PTR, CAST, SIZE)					\
73 	((PTR) = (CAST)malloc((SIZE), M_NG_SSCFU_INS, M_NOWAIT | M_ZERO))
74 #define	MEMFREE(PTR)							\
75 	free(PTR, M_NG_SSCFU_INS)
76 
77 #define	SIG_ALLOC(PTR)							\
78 	((PTR) = malloc(sizeof(struct sscfu_sig),			\
79 	    M_NG_SSCFU_SIG, M_NOWAIT | M_ZERO))
80 #define	SIG_FREE(PTR)							\
81 	free(PTR, M_NG_SSCFU_SIG)
82 
83 #endif
84 
85 
86 /*
87  * Signal queues
88  */
89 typedef TAILQ_ENTRY(sscfu_sig) sscfu_sigq_link_t;
90 typedef TAILQ_HEAD(sscfu_sigq, sscfu_sig) sscfu_sigq_head_t;
91 #define	SIGQ_INIT(Q) 		TAILQ_INIT(Q)
92 #define	SIGQ_APPEND(Q, S)	TAILQ_INSERT_TAIL(Q, S, link)
93 
94 #define	SIGQ_GET(Q) ng_sscfu_sigq_get((Q))
95 
96 #define	DECL_SIGQ_GET							\
97 static __inline struct sscfu_sig *					\
98 ng_sscfu_sigq_get(struct sscfu_sigq *q)					\
99 {									\
100 	struct sscfu_sig *s;						\
101 									\
102 	s = TAILQ_FIRST(q);						\
103 	if (s != NULL)							\
104 		TAILQ_REMOVE(q, s, link);				\
105 	return (s);							\
106 }
107 
108 #define	SIGQ_CLEAR(Q)							\
109     do {								\
110 	struct sscfu_sig *_s1, *_s2;					\
111 									\
112 	_s1 = TAILQ_FIRST(Q);						\
113 	while (_s1 != NULL) {						\
114 		_s2 = TAILQ_NEXT(_s1, link);				\
115 		if (_s1->m)						\
116 			MBUF_FREE(_s1->m);				\
117 		SIG_FREE(_s1);						\
118 		_s1 = _s2;						\
119 	}								\
120 	TAILQ_INIT(Q);							\
121     } while (0)
122 
123 
124 /*
125  * Message buffers
126  */
127 #define	MBUF_FREE(M)	m_freem(M)
128 
129 #ifdef SSCFU_DEBUG
130 #define	ASSERT(S)	KASSERT(S, (#S))
131 #else
132 #define	ASSERT(S)
133 #endif
134