| 1 | /* |
| 2 | * Copyright (c) 2019 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 | #include <stdint.h> |
| 29 | #include <sys/cdefs.h> /* prerequisite */ |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/param.h> |
| 32 | #include <sys/systm.h> |
| 33 | #include <skywalk/nexus/nexus_pktq.h> |
| 34 | |
| 35 | static void __nx_pktq_init(struct nx_pktq *q, uint32_t, lck_grp_t *lck_grp); |
| 36 | |
| 37 | __attribute__((always_inline)) |
| 38 | static inline void |
| 39 | __nx_pktq_init(struct nx_pktq *q, uint32_t lim, lck_grp_t *lck_grp) |
| 40 | { |
| 41 | bzero(s: q, n: sizeof(*q)); |
| 42 | _qinit(&q->nx_pktq_q, Q_DROPTAIL, lim, QP_PACKET); |
| 43 | q->nx_pktq_grp = lck_grp; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | nx_pktq_safe_init(struct __kern_channel_ring *kr, struct nx_pktq *q, |
| 48 | uint32_t lim, lck_grp_t *lck_grp, lck_attr_t *lck_attr) |
| 49 | { |
| 50 | q->nx_pktq_kring = kr; |
| 51 | __nx_pktq_init(q, lim, lck_grp); |
| 52 | lck_mtx_init(lck: &q->nx_pktq_lock, grp: lck_grp, attr: lck_attr); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | nx_pktq_init(struct nx_pktq *q, uint32_t lim) |
| 57 | { |
| 58 | __nx_pktq_init(q, lim, NULL); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | nx_pktq_concat(struct nx_pktq *q1, struct nx_pktq *q2) |
| 63 | { |
| 64 | uint32_t qlen; |
| 65 | uint64_t qsize; |
| 66 | classq_pkt_t first = CLASSQ_PKT_INITIALIZER(first); |
| 67 | classq_pkt_t last = CLASSQ_PKT_INITIALIZER(last); |
| 68 | |
| 69 | /* caller is responsible for locking */ |
| 70 | if (!nx_pktq_empty(q: q2)) { |
| 71 | _getq_all(&q2->nx_pktq_q, &first, &last, &qlen, &qsize); |
| 72 | ASSERT(first.cp_kpkt != NULL && last.cp_kpkt != NULL); |
| 73 | _addq_multi(&q1->nx_pktq_q, &first, &last, qlen, qsize); |
| 74 | ASSERT(nx_pktq_empty(q2)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | boolean_t |
| 79 | nx_pktq_empty(struct nx_pktq *q) |
| 80 | { |
| 81 | return qempty(&q->nx_pktq_q) && qhead(&q->nx_pktq_q) == NULL; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | nx_pktq_purge(struct nx_pktq *q) |
| 86 | { |
| 87 | _flushq(&q->nx_pktq_q); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | nx_pktq_safe_purge(struct nx_pktq *q) |
| 92 | { |
| 93 | nx_pktq_lock(q); |
| 94 | _flushq(&q->nx_pktq_q); |
| 95 | nx_pktq_unlock(q); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | nx_pktq_safe_destroy(struct nx_pktq *q) |
| 100 | { |
| 101 | VERIFY(nx_pktq_empty(q)); |
| 102 | lck_mtx_destroy(lck: &q->nx_pktq_lock, grp: q->nx_pktq_grp); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | nx_pktq_destroy(struct nx_pktq *q) |
| 107 | { |
| 108 | VERIFY(nx_pktq_empty(q)); |
| 109 | } |
| 110 | |