1262152Sluigi/*
2262152Sluigi * Copyright (C) 2013-2014 Vincenzo Maffione. All rights reserved.
3262152Sluigi *
4262152Sluigi * Redistribution and use in source and binary forms, with or without
5262152Sluigi * modification, are permitted provided that the following conditions
6262152Sluigi * are met:
7262152Sluigi *   1. Redistributions of source code must retain the above copyright
8262152Sluigi *      notice, this list of conditions and the following disclaimer.
9262152Sluigi *   2. Redistributions in binary form must reproduce the above copyright
10262152Sluigi *      notice, this list of conditions and the following disclaimer in the
11262152Sluigi *    documentation and/or other materials provided with the distribution.
12262152Sluigi *
13262152Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14262152Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15262152Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16262152Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17262152Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18262152Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19262152Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20262152Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21262152Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22262152Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23262152Sluigi * SUCH DAMAGE.
24262152Sluigi */
25262152Sluigi
26262152Sluigi/*
27262152Sluigi * $FreeBSD: releng/10.2/sys/dev/netmap/netmap_mbq.h 270252 2014-08-20 23:34:36Z luigi $
28262152Sluigi */
29262152Sluigi
30262152Sluigi
31262152Sluigi#ifndef __NETMAP_MBQ_H__
32262152Sluigi#define __NETMAP_MBQ_H__
33262152Sluigi
34262152Sluigi/*
35262152Sluigi * These function implement an mbuf tailq with an optional lock.
36262152Sluigi * The base functions act ONLY ON THE QUEUE, whereas the "safe"
37262152Sluigi * variants (mbq_safe_*) also handle the lock.
38262152Sluigi */
39262152Sluigi
40262152Sluigi/* XXX probably rely on a previous definition of SPINLOCK_T */
41262152Sluigi#ifdef linux
42262152Sluigi#define SPINLOCK_T  safe_spinlock_t
43262152Sluigi#else
44262152Sluigi#define SPINLOCK_T  struct mtx
45262152Sluigi#endif
46262152Sluigi
47262152Sluigi/* A FIFO queue of mbufs with an optional lock. */
48262152Sluigistruct mbq {
49262152Sluigi    struct mbuf *head;
50262152Sluigi    struct mbuf *tail;
51262152Sluigi    int count;
52262152Sluigi    SPINLOCK_T lock;
53262152Sluigi};
54262152Sluigi
55262152Sluigi/* XXX "destroy" does not match "init" as a name.
56262152Sluigi * We should also clarify whether init can be used while
57262152Sluigi * holding a lock, and whether mbq_safe_destroy() is a NOP.
58262152Sluigi */
59262152Sluigivoid mbq_init(struct mbq *q);
60262152Sluigivoid mbq_destroy(struct mbq *q);
61262152Sluigivoid mbq_enqueue(struct mbq *q, struct mbuf *m);
62262152Sluigistruct mbuf *mbq_dequeue(struct mbq *q);
63262152Sluigivoid mbq_purge(struct mbq *q);
64262152Sluigi
65267282Sluigistatic inline void
66267282Sluigimbq_lock(struct mbq *q)
67267282Sluigi{
68267282Sluigi	mtx_lock_spin(&q->lock);
69267282Sluigi}
70262152Sluigi
71267282Sluigistatic inline void
72267282Sluigimbq_unlock(struct mbq *q)
73267282Sluigi{
74267282Sluigi	mtx_unlock_spin(&q->lock);
75267282Sluigi}
76267282Sluigi
77270252Sluigi
78262152Sluigivoid mbq_safe_init(struct mbq *q);
79262152Sluigivoid mbq_safe_destroy(struct mbq *q);
80262152Sluigivoid mbq_safe_enqueue(struct mbq *q, struct mbuf *m);
81262152Sluigistruct mbuf *mbq_safe_dequeue(struct mbq *q);
82262152Sluigivoid mbq_safe_purge(struct mbq *q);
83262152Sluigi
84262152Sluigistatic inline unsigned int mbq_len(struct mbq *q)
85262152Sluigi{
86262152Sluigi    return q->count;
87262152Sluigi}
88262152Sluigi
89262152Sluigi#endif /* __NETMAP_MBQ_H_ */
90