1/*
2 * Copyright (c) 2015-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * Copyright (C) 2013-2014 Vincenzo Maffione. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 */
53
54
55#include <stdint.h>
56#include <sys/cdefs.h> /* prerequisite */
57#include <sys/types.h>
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <skywalk/nexus/nexus_mbq.h>
61
62static void __nx_mbq_init(struct nx_mbq *q, uint32_t, lck_grp_t *lck_grp);
63
64__attribute__((always_inline))
65static inline void
66__nx_mbq_init(struct nx_mbq *q, uint32_t lim, lck_grp_t *lck_grp)
67{
68 bzero(s: q, n: sizeof(*q));
69 _qinit(&q->nx_mbq_q, Q_DROPTAIL, lim, QP_MBUF);
70 q->nx_mbq_grp = lck_grp;
71}
72
73void
74nx_mbq_safe_init(struct __kern_channel_ring *kr, struct nx_mbq *q,
75 uint32_t lim, lck_grp_t *lck_grp, lck_attr_t *lck_attr)
76{
77 q->nx_mbq_kring = kr;
78 __nx_mbq_init(q, lim, lck_grp);
79 lck_mtx_init(lck: &q->nx_mbq_lock, grp: lck_grp, attr: lck_attr);
80}
81
82void
83nx_mbq_init(struct nx_mbq *q, uint32_t lim)
84{
85 __nx_mbq_init(q, lim, NULL);
86}
87
88void
89nx_mbq_concat(struct nx_mbq *q1, struct nx_mbq *q2)
90{
91 uint32_t qlen;
92 uint64_t qsize;
93 classq_pkt_t first = CLASSQ_PKT_INITIALIZER(first);
94 classq_pkt_t last = CLASSQ_PKT_INITIALIZER(last);
95
96 /* caller is responsible for locking */
97 if (!nx_mbq_empty(q2)) {
98 _getq_all(&q2->nx_mbq_q, &first, &last, &qlen, &qsize);
99 ASSERT(first.cp_mbuf != NULL && last.cp_mbuf != NULL);
100 _addq_multi(&q1->nx_mbq_q, &first, &last, qlen, qsize);
101 ASSERT(nx_mbq_empty(q2));
102 }
103}
104
105boolean_t
106nx_mbq_empty(struct nx_mbq *q)
107{
108 return qempty(&q->nx_mbq_q) && qhead(&q->nx_mbq_q) == NULL;
109}
110
111void
112nx_mbq_purge(struct nx_mbq *q)
113{
114 _flushq(&q->nx_mbq_q);
115}
116
117void
118nx_mbq_safe_purge(struct nx_mbq *q)
119{
120 nx_mbq_lock(q);
121 _flushq(&q->nx_mbq_q);
122 nx_mbq_unlock(q);
123}
124
125void
126nx_mbq_safe_destroy(struct nx_mbq *q)
127{
128 VERIFY(nx_mbq_empty(q));
129 lck_mtx_destroy(lck: &q->nx_mbq_lock, grp: q->nx_mbq_grp);
130}
131
132void
133nx_mbq_destroy(struct nx_mbq *q)
134{
135 VERIFY(nx_mbq_empty(q));
136}
137