netmap_mbq.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (C) 2013-2014 Vincenzo Maffione. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *   1. Redistributions of source code must retain the above copyright
10 *      notice, this list of conditions and the following disclaimer.
11 *   2. Redistributions in binary form must reproduce the above copyright
12 *      notice, this list of conditions and the following disclaimer in the
13 *      documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * $FreeBSD: stable/11/sys/dev/netmap/netmap_mbq.c 330897 2018-03-14 03:19:51Z eadler $
30 */
31
32
33#ifdef linux
34#include "bsd_glue.h"
35#else   /* __FreeBSD__ */
36#include <sys/param.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/systm.h>
40#include <sys/mbuf.h>
41#endif  /* __FreeBSD__ */
42
43#include "netmap_mbq.h"
44
45
46static inline void __mbq_init(struct mbq *q)
47{
48    q->head = q->tail = NULL;
49    q->count = 0;
50}
51
52
53void mbq_safe_init(struct mbq *q)
54{
55    mtx_init(&q->lock, "mbq", NULL, MTX_SPIN);
56    __mbq_init(q);
57}
58
59
60void mbq_init(struct mbq *q)
61{
62    __mbq_init(q);
63}
64
65
66static inline void __mbq_enqueue(struct mbq *q, struct mbuf *m)
67{
68    m->m_nextpkt = NULL;
69    if (q->tail) {
70        q->tail->m_nextpkt = m;
71        q->tail = m;
72    } else {
73        q->head = q->tail = m;
74    }
75    q->count++;
76}
77
78
79void mbq_safe_enqueue(struct mbq *q, struct mbuf *m)
80{
81    mbq_lock(q);
82    __mbq_enqueue(q, m);
83    mbq_unlock(q);
84}
85
86
87void mbq_enqueue(struct mbq *q, struct mbuf *m)
88{
89    __mbq_enqueue(q, m);
90}
91
92
93static inline struct mbuf *__mbq_dequeue(struct mbq *q)
94{
95    struct mbuf *ret = NULL;
96
97    if (q->head) {
98        ret = q->head;
99        q->head = ret->m_nextpkt;
100        if (q->head == NULL) {
101            q->tail = NULL;
102        }
103        q->count--;
104        ret->m_nextpkt = NULL;
105    }
106
107    return ret;
108}
109
110
111struct mbuf *mbq_safe_dequeue(struct mbq *q)
112{
113    struct mbuf *ret;
114
115    mbq_lock(q);
116    ret =  __mbq_dequeue(q);
117    mbq_unlock(q);
118
119    return ret;
120}
121
122
123struct mbuf *mbq_dequeue(struct mbq *q)
124{
125    return __mbq_dequeue(q);
126}
127
128
129/* XXX seems pointless to have a generic purge */
130static void __mbq_purge(struct mbq *q, int safe)
131{
132    struct mbuf *m;
133
134    for (;;) {
135        m = safe ? mbq_safe_dequeue(q) : mbq_dequeue(q);
136        if (m) {
137            m_freem(m);
138        } else {
139            break;
140        }
141    }
142}
143
144
145void mbq_purge(struct mbq *q)
146{
147    __mbq_purge(q, 0);
148}
149
150
151void mbq_safe_purge(struct mbq *q)
152{
153    __mbq_purge(q, 1);
154}
155
156
157void mbq_safe_destroy(struct mbq *q)
158{
159    mtx_destroy(&q->lock);
160}
161
162
163void mbq_destroy(struct mbq *q)
164{
165}
166