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: Hartmut 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 signalling source to the NG environment.
32 *
33 * $FreeBSD$
34 */
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/malloc.h>
40#include <sys/queue.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/mbuf.h>
44#include <netgraph/ng_message.h>
45#include <netgraph/netgraph.h>
46#include <netgraph/atm/ngatmbase.h>
47
48#define	ASSERT(E, M) KASSERT(E,M)
49
50/*
51 * Memory
52 */
53enum unimem {
54	UNIMEM_INS = 0,
55	UNIMEM_ALL,
56	UNIMEM_SIG,
57	UNIMEM_CALL,
58	UNIMEM_PARTY,
59};
60#define	UNIMEM_TYPES	5
61
62void *ng_uni_malloc(enum unimem, const char *, u_int);
63void ng_uni_free(enum unimem, void *, const char *, u_int);
64
65#define	INS_ALLOC()	ng_uni_malloc(UNIMEM_INS, __FILE__, __LINE__)
66#define	INS_FREE(P)	ng_uni_free(UNIMEM_INS, P, __FILE__, __LINE__)
67
68#define	UNI_ALLOC()	ng_uni_malloc(UNIMEM_ALL, __FILE__, __LINE__)
69#define	UNI_FREE(P)	ng_uni_free(UNIMEM_ALL, P, __FILE__, __LINE__)
70
71#define	SIG_ALLOC()	ng_uni_malloc(UNIMEM_SIG, __FILE__, __LINE__)
72#define	SIG_FREE(P)	ng_uni_free(UNIMEM_SIG, P, __FILE__, __LINE__)
73
74#define	CALL_ALLOC()	ng_uni_malloc(UNIMEM_CALL, __FILE__, __LINE__)
75#define	CALL_FREE(P)	ng_uni_free(UNIMEM_CALL, P, __FILE__, __LINE__)
76
77#define	PARTY_ALLOC()	ng_uni_malloc(UNIMEM_PARTY, __FILE__, __LINE__)
78#define	PARTY_FREE(P)	ng_uni_free(UNIMEM_PARTY, P, __FILE__, __LINE__)
79
80/*
81 * Timers
82 */
83struct uni_timer {
84	struct callout c;
85};
86
87#define	_TIMER_INIT(X,T)	ng_callout_init(&(X)->T.c)
88#define	_TIMER_DESTROY(UNI,FIELD) _TIMER_STOP(UNI,FIELD)
89#define	_TIMER_STOP(UNI,FIELD) do {						\
90	ng_uncallout(&FIELD.c, (UNI)->arg);					\
91    } while (0)
92#define	TIMER_ISACT(UNI,T)	(callout_active(&(UNI)->T.c) ||		\
93	callout_pending(&(UNI)->T.c))
94#define	_TIMER_START(UNI,ARG,FIELD,DUE,FUNC) do {			\
95	_TIMER_STOP(UNI, FIELD);					\
96	ng_callout(&FIELD.c, (UNI)->arg, NULL,				\
97	    hz * (DUE) / 1000, FUNC, (ARG), 0);				\
98    } while (0)
99
100#define	TIMER_FUNC_UNI(T,F)						\
101static void F(struct uni *);						\
102static void								\
103_##T##_func(node_p node, hook_p hook, void *arg1, int arg2)		\
104{									\
105	struct uni *uni = (struct uni *)arg1;				\
106									\
107	(F)(uni);							\
108	uni_work(uni);							\
109}
110
111/*
112 * Be careful: call may be invalid after the call to F
113 */
114#define	TIMER_FUNC_CALL(T,F)						\
115static void F(struct call *);						\
116static void								\
117_##T##_func(node_p node, hook_p hook, void *arg1, int arg2)		\
118{									\
119	struct call *call = (struct call *)arg1;			\
120	struct uni *uni = call->uni;					\
121									\
122	(F)(call);							\
123	uni_work(uni);							\
124}
125
126/*
127 * Be careful: call/party may be invalid after the call to F
128 */
129#define	TIMER_FUNC_PARTY(T,F)						\
130static void F(struct party *);						\
131static void								\
132_##T##_func(node_p node, hook_p hook, void *arg1, int arg2)		\
133{									\
134	struct party *party = (struct party *)arg1;			\
135	struct uni *uni = party->call->uni;				\
136									\
137	(F)(party);							\
138	uni_work(uni);							\
139}
140
141extern size_t unimem_sizes[UNIMEM_TYPES];
142
143#define	UNICORE								\
144size_t unimem_sizes[UNIMEM_TYPES] = {					\
145	[UNIMEM_INS]	= sizeof(struct uni),				\
146	[UNIMEM_ALL]	= sizeof(struct uni_all),			\
147	[UNIMEM_SIG]	= sizeof(struct sig),				\
148	[UNIMEM_CALL]	= sizeof(struct call),				\
149	[UNIMEM_PARTY]	= sizeof(struct party)				\
150};
151