root/src/libs/compat/openbsd_network/compat/net/if_var-obsd.h
/*      $OpenBSD: if.c,v 1.683 2022/11/23 16:57:37 kn Exp $     */
/*      $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $  */

/*
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Copyright (c) 1980, 1986, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      @(#)if.c        8.3 (Berkeley) 1/4/94
 */


struct if_rxring {
        int     rxr_adjusted;
        u_int   rxr_alive;
        u_int   rxr_cwm;
        u_int   rxr_lwm;
        u_int   rxr_hwm;
};

#define if_rxr_put(_r, _c)      do { (_r)->rxr_alive -= (_c); } while (0)
#define if_rxr_needrefill(_r)   ((_r)->rxr_alive < (_r)->rxr_lwm)
#define if_rxr_inuse(_r)        ((_r)->rxr_alive)
#define if_rxr_cwm(_r)          ((_r)->rxr_cwm)

static inline void
if_rxr_init(struct if_rxring *rxr, u_int lwm, u_int hwm)
{
        extern int ticks;

        memset(rxr, 0, sizeof(*rxr));

        rxr->rxr_adjusted = ticks;
        rxr->rxr_cwm = rxr->rxr_lwm = lwm;
        rxr->rxr_hwm = hwm;
}

static inline void
if_rxr_adjust_cwm(struct if_rxring *rxr)
{
        extern int ticks;

        if (rxr->rxr_alive >= rxr->rxr_lwm)
                return;
        else if (rxr->rxr_cwm < rxr->rxr_hwm)
                rxr->rxr_cwm++;

        rxr->rxr_adjusted = ticks;
}

static inline void
if_rxr_livelocked(struct if_rxring *rxr)
{
        extern int ticks;

        if (ticks - rxr->rxr_adjusted >= 1) {
                if (rxr->rxr_cwm > rxr->rxr_lwm)
                        rxr->rxr_cwm--;

                rxr->rxr_adjusted = ticks;
        }
}

static inline u_int
if_rxr_get(struct if_rxring *rxr, u_int max)
{
        extern int ticks;
        u_int diff;

        if (ticks - rxr->rxr_adjusted >= 1) {
                /* we're free to try for an adjustment */
                if_rxr_adjust_cwm(rxr);
        }

        if (rxr->rxr_alive >= rxr->rxr_cwm)
                return (0);

        diff = min(rxr->rxr_cwm - rxr->rxr_alive, max);
        rxr->rxr_alive += diff;

        return (diff);
}