1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2009-2010 Fabio Checconi
5 * Copyright (c) 2009-2010 Luigi Rizzo, Universita` di Pisa
6 * All rights reserved.
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 AUTHORS 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 AUTHORS 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
30#ifndef	_G_SCHED_H_
31#define	_G_SCHED_H_
32
33/*
34 * $Id$
35 * $FreeBSD$
36 *
37 * Header for the geom_sched class (userland library and kernel part).
38 * See g_sched.c for documentation.
39 * The userland code only needs the three G_SCHED_* values below.
40 */
41
42#define	G_SCHED_CLASS_NAME	"SCHED"
43#define	G_SCHED_VERSION		0
44#define	G_SCHED_SUFFIX		".sched."
45
46#ifdef _KERNEL
47#define	G_SCHED_DEBUG(lvl, ...)	do {				\
48	if (me.gs_debug >= (lvl)) {				\
49		printf("GEOM_SCHED");				\
50		if (me.gs_debug > 0)				\
51			printf("[%u]", lvl);			\
52		printf(": ");					\
53		printf(__VA_ARGS__);				\
54		printf("\n");					\
55	}							\
56} while (0)
57
58#define	G_SCHED_LOGREQ(bp, ...)	do {				\
59	if (me.gs_debug >= 2) {					\
60		printf("GEOM_SCHED[2]: ");			\
61		printf(__VA_ARGS__);				\
62		printf(" ");					\
63		g_print_bio(bp);				\
64		printf("\n");					\
65	}							\
66} while (0)
67
68LIST_HEAD(g_hash, g_sched_class);
69
70/*
71 * Descriptor of a scheduler.
72 * In addition to the obvious fields, sc_flushing and sc_pending
73 * support dynamic switching of scheduling algorithm.
74 * Normally, sc_flushing is 0, and requests that are scheduled are
75 * also added to the sc_pending queue, and removed when we receive
76 * the 'done' event.
77 *
78 * When we are transparently inserted on an existing provider,
79 * sc_proxying is set. The detach procedure is slightly different.
80 *
81 * When switching schedulers, sc_flushing is set so requests bypass us,
82 * and at the same time we update the pointer in the pending bios
83 * to ignore us when they return up.
84 * XXX it would be more efficient to implement sc_pending with
85 * a generation number: the softc generation is increased when
86 * we change scheduling algorithm, we store the current generation
87 * number in the pending bios, and when they come back we ignore
88 * the done() call if the generation number do not match.
89 */
90struct g_sched_softc {
91	/*
92	 * Generic fields used by any scheduling algorithm:
93	 * a mutex, the class descriptor, flags, list of pending
94	 * requests (used when flushing the module) and support
95	 * for hash tables where we store per-flow queues.
96	 */
97	struct mtx	sc_mtx;
98	struct g_gsched	*sc_gsched;	/* Scheduler descriptor. */
99	int		sc_pending;	/* Pending requests. */
100	int		sc_flags;	/* Various flags. */
101
102	/*
103	 * Hash tables to store per-flow queues are generally useful
104	 * so we handle them in the common code.
105	 * sc_hash and sc_mask are parameters of the hash table,
106	 * the last two fields are used to periodically remove
107	 * expired items from the hash table.
108	 */
109	struct g_hash	*sc_hash;
110	u_long		sc_mask;
111	int		sc_flush_ticks;	/* Next tick for a flush. */
112	int		sc_flush_bucket; /* Next bucket to flush. */
113
114	/*
115	 * Pointer to the algorithm's private data, which is the value
116	 * returned by sc_gsched->gs_init() . A NULL here means failure.
117	 * XXX intptr_t might be more appropriate.
118	 */
119	void		*sc_data;
120};
121
122#define	G_SCHED_PROXYING	1
123#define	G_SCHED_FLUSHING	2
124
125#endif	/* _KERNEL */
126
127#endif	/* _G_SCHED_H_ */
128