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