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