1/*	$OpenBSD: if.c,v 1.683 2022/11/23 16:57:37 kn Exp $	*/
2/*	$NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1980, 1986, 1993
35 *	The Regents of the University of California.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 *    may be used to endorse or promote products derived from this software
47 *    without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 *	@(#)if.c	8.3 (Berkeley) 1/4/94
62 */
63
64
65struct if_rxring {
66	int     rxr_adjusted;
67	u_int	rxr_alive;
68	u_int	rxr_cwm;
69	u_int	rxr_lwm;
70	u_int	rxr_hwm;
71};
72
73#define if_rxr_put(_r, _c)	do { (_r)->rxr_alive -= (_c); } while (0)
74#define if_rxr_needrefill(_r)	((_r)->rxr_alive < (_r)->rxr_lwm)
75#define if_rxr_inuse(_r)	((_r)->rxr_alive)
76#define if_rxr_cwm(_r)		((_r)->rxr_cwm)
77
78static inline void
79if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm)
80{
81	extern int ticks;
82
83	memset(rxr, 0, sizeof(*rxr));
84
85	rxr->rxr_adjusted = ticks;
86	rxr->rxr_cwm = rxr->rxr_lwm = lwm;
87	rxr->rxr_hwm = hwm;
88}
89
90static inline void
91if_rxr_adjust_cwm(struct if_rxring *rxr)
92{
93	extern int ticks;
94
95	if (rxr->rxr_alive >= rxr->rxr_lwm)
96		return;
97	else if (rxr->rxr_cwm < rxr->rxr_hwm)
98		rxr->rxr_cwm++;
99
100	rxr->rxr_adjusted = ticks;
101}
102
103static inline void
104if_rxr_livelocked(struct if_rxring *rxr)
105{
106	extern int ticks;
107
108	if (ticks - rxr->rxr_adjusted >= 1) {
109		if (rxr->rxr_cwm > rxr->rxr_lwm)
110			rxr->rxr_cwm--;
111
112		rxr->rxr_adjusted = ticks;
113	}
114}
115
116static inline u_int
117if_rxr_get(struct if_rxring *rxr, u_int max)
118{
119	extern int ticks;
120	u_int diff;
121
122	if (ticks - rxr->rxr_adjusted >= 1) {
123		/* we're free to try for an adjustment */
124		if_rxr_adjust_cwm(rxr);
125	}
126
127	if (rxr->rxr_alive >= rxr->rxr_cwm)
128		return (0);
129
130	diff = min(rxr->rxr_cwm - rxr->rxr_alive, max);
131	rxr->rxr_alive += diff;
132
133	return (diff);
134}
135