diff options
author | glebius <glebius@FreeBSD.org> | 2015-02-19 01:19:42 +0000 |
---|---|---|
committer | glebius <glebius@FreeBSD.org> | 2015-02-19 01:19:42 +0000 |
commit | e48762ccfce869621f18aca1af50c69827b0b023 (patch) | |
tree | 58c4847d6216a3ece2c63355dd9c2b2f9a3f766a /sys/sys/mbuf.h | |
parent | 4145efe339257de7b699d476ad4581458f9236bb (diff) | |
download | FreeBSD-src-e48762ccfce869621f18aca1af50c69827b0b023.zip FreeBSD-src-e48762ccfce869621f18aca1af50c69827b0b023.tar.gz |
Provide a set of inline functions to manage simple mbuf(9) queues, based
on queue(3)'s STAILQ. Utilize them in cxgb(4) and Xen, deleting home
grown implementations.
Sponsored by: Netflix
Sponsored by: Nginx, Inc.
Diffstat (limited to 'sys/sys/mbuf.h')
-rw-r--r-- | sys/sys/mbuf.h | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h index 11d8297..1932de4 100644 --- a/sys/sys/mbuf.h +++ b/sys/sys/mbuf.h @@ -1199,5 +1199,101 @@ rt_m_getfib(struct mbuf *m) #define M_PROFILE(m) #endif +struct mbufq { + STAILQ_HEAD(, mbuf) mq_head; + int mq_len; + int mq_maxlen; +}; + +static inline void +mbufq_init(struct mbufq *mq, int maxlen) +{ + + STAILQ_INIT(&mq->mq_head); + mq->mq_maxlen = maxlen; + mq->mq_len = 0; +} + +static inline struct mbuf * +mbufq_flush(struct mbufq *mq) +{ + struct mbuf *m; + + m = STAILQ_FIRST(&mq->mq_head); + STAILQ_INIT(&mq->mq_head); + mq->mq_len = 0; + return (m); +} + +static inline void +mbufq_drain(struct mbufq *mq) +{ + struct mbuf *m, *n; + + n = mbufq_flush(mq); + while ((m = n) != NULL) { + n = STAILQ_NEXT(m, m_stailqpkt); + m_freem(m); + } +} + +static inline struct mbuf * +mbufq_first(const struct mbufq *mq) +{ + + return (STAILQ_FIRST(&mq->mq_head)); +} + +static inline struct mbuf * +mbufq_last(const struct mbufq *mq) +{ + + return (STAILQ_LAST(&mq->mq_head, mbuf, m_stailqpkt)); +} +static inline int +mbufq_full(const struct mbufq *mq) +{ + + return (mq->mq_len >= mq->mq_maxlen); +} + +static inline int +mbufq_len(const struct mbufq *mq) +{ + + return (mq->mq_len); +} + +static inline int +mbufq_enqueue(struct mbufq *mq, struct mbuf *m) +{ + + if (mbufq_full(mq)) + return (ENOBUFS); + STAILQ_INSERT_TAIL(&mq->mq_head, m, m_stailqpkt); + mq->mq_len++; + return (0); +} + +static inline struct mbuf * +mbufq_dequeue(struct mbufq *mq) +{ + struct mbuf *m; + + m = STAILQ_FIRST(&mq->mq_head); + if (m) { + STAILQ_REMOVE_HEAD(&mq->mq_head, m_stailqpkt); + mq->mq_len--; + } + return (m); +} + +static inline void +mbufq_prepend(struct mbufq *mq, struct mbuf *m) +{ + + STAILQ_INSERT_HEAD(&mq->mq_head, m, m_stailqpkt); + mq->mq_len++; +} #endif /* !_SYS_MBUF_H_ */ |