1*043036a2SApple OSS Distributions /* 2*043036a2SApple OSS Distributions * Copyright (c) 2016 Apple Inc. All rights reserved. 3*043036a2SApple OSS Distributions * 4*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*043036a2SApple OSS Distributions * 6*043036a2SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*043036a2SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*043036a2SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*043036a2SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*043036a2SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*043036a2SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*043036a2SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*043036a2SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*043036a2SApple OSS Distributions * 15*043036a2SApple OSS Distributions * Please obtain a copy of the License at 16*043036a2SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*043036a2SApple OSS Distributions * 18*043036a2SApple OSS Distributions * The Original Code and all software distributed under the License are 19*043036a2SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*043036a2SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*043036a2SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*043036a2SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*043036a2SApple OSS Distributions * Please see the License for the specific language governing rights and 24*043036a2SApple OSS Distributions * limitations under the License. 25*043036a2SApple OSS Distributions * 26*043036a2SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*043036a2SApple OSS Distributions */ 28*043036a2SApple OSS Distributions 29*043036a2SApple OSS Distributions #ifndef _SKYWALK_PACKET_PACKETQUEUE_H_ 30*043036a2SApple OSS Distributions #define _SKYWALK_PACKET_PACKETQUEUE_H_ 31*043036a2SApple OSS Distributions 32*043036a2SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE 33*043036a2SApple OSS Distributions 34*043036a2SApple OSS Distributions /* 35*043036a2SApple OSS Distributions * Simple __kern_packet queueing system 36*043036a2SApple OSS Distributions * This is basically a SIMPLEQ adapted to skywalk __kern_packet use. 37*043036a2SApple OSS Distributions */ 38*043036a2SApple OSS Distributions #define KPKTQ_HEAD(name) \ 39*043036a2SApple OSS Distributions struct name { \ 40*043036a2SApple OSS Distributions struct __kern_packet *kq_first; /* first packet */ \ 41*043036a2SApple OSS Distributions struct __kern_packet **kq_last; /* addr of last next packet */ \ 42*043036a2SApple OSS Distributions uint32_t kq_len; /* number of packets in queue */ \ 43*043036a2SApple OSS Distributions } 44*043036a2SApple OSS Distributions 45*043036a2SApple OSS Distributions #define KPKTQ_INIT(q) do { \ 46*043036a2SApple OSS Distributions KPKTQ_FIRST(q) = NULL; \ 47*043036a2SApple OSS Distributions (q)->kq_last = &KPKTQ_FIRST(q); \ 48*043036a2SApple OSS Distributions (q)->kq_len = 0; \ 49*043036a2SApple OSS Distributions } while (0) 50*043036a2SApple OSS Distributions 51*043036a2SApple OSS Distributions #define KPKTQ_FINI(q) do { \ 52*043036a2SApple OSS Distributions ASSERT(KPKTQ_EMPTY(q)); \ 53*043036a2SApple OSS Distributions ASSERT(KPKTQ_LEN(q) == 0); \ 54*043036a2SApple OSS Distributions KPKTQ_INIT(q); \ 55*043036a2SApple OSS Distributions } while (0) 56*043036a2SApple OSS Distributions 57*043036a2SApple OSS Distributions #define KPKTQ_DISPOSE(q) KPKTQ_INIT(q) 58*043036a2SApple OSS Distributions 59*043036a2SApple OSS Distributions #define KPKTQ_CONCAT(q1, q2) do { \ 60*043036a2SApple OSS Distributions if (!KPKTQ_EMPTY(q2)) { \ 61*043036a2SApple OSS Distributions *(q1)->kq_last = KPKTQ_FIRST(q2); \ 62*043036a2SApple OSS Distributions (q1)->kq_last = (q2)->kq_last; \ 63*043036a2SApple OSS Distributions (q1)->kq_len += (q2)->kq_len; \ 64*043036a2SApple OSS Distributions KPKTQ_DISPOSE((q2)); \ 65*043036a2SApple OSS Distributions } \ 66*043036a2SApple OSS Distributions } while (0) 67*043036a2SApple OSS Distributions 68*043036a2SApple OSS Distributions #define KPKTQ_PREPEND(q, p) do { \ 69*043036a2SApple OSS Distributions if ((KPKTQ_NEXT(p) = KPKTQ_FIRST(q)) == NULL) { \ 70*043036a2SApple OSS Distributions ASSERT((q)->kq_len == 0); \ 71*043036a2SApple OSS Distributions (q)->kq_last = &KPKTQ_NEXT(p); \ 72*043036a2SApple OSS Distributions } \ 73*043036a2SApple OSS Distributions KPKTQ_FIRST(q) = (p); \ 74*043036a2SApple OSS Distributions (q)->kq_len++; \ 75*043036a2SApple OSS Distributions } while (0) 76*043036a2SApple OSS Distributions 77*043036a2SApple OSS Distributions #define KPKTQ_ENQUEUE(q, p) do { \ 78*043036a2SApple OSS Distributions ASSERT(KPKTQ_NEXT(p) == NULL); \ 79*043036a2SApple OSS Distributions *(q)->kq_last = (p); \ 80*043036a2SApple OSS Distributions (q)->kq_last = &KPKTQ_NEXT(p); \ 81*043036a2SApple OSS Distributions (q)->kq_len++; \ 82*043036a2SApple OSS Distributions } while (0) 83*043036a2SApple OSS Distributions 84*043036a2SApple OSS Distributions #define KPKTQ_ENQUEUE_MULTI(q, p, n, c) do { \ 85*043036a2SApple OSS Distributions KPKTQ_NEXT(n) = NULL; \ 86*043036a2SApple OSS Distributions *(q)->kq_last = (p); \ 87*043036a2SApple OSS Distributions (q)->kq_last = &KPKTQ_NEXT(n); \ 88*043036a2SApple OSS Distributions (q)->kq_len += c; \ 89*043036a2SApple OSS Distributions } while (0) 90*043036a2SApple OSS Distributions 91*043036a2SApple OSS Distributions #define KPKTQ_ENQUEUE_LIST(q, p) do { \ 92*043036a2SApple OSS Distributions uint32_t _c = 1; \ 93*043036a2SApple OSS Distributions struct __kern_packet *_n = (p); \ 94*043036a2SApple OSS Distributions while (__improbable(KPKTQ_NEXT(_n) != NULL)) { \ 95*043036a2SApple OSS Distributions _c++; \ 96*043036a2SApple OSS Distributions _n = KPKTQ_NEXT(_n); \ 97*043036a2SApple OSS Distributions } \ 98*043036a2SApple OSS Distributions KPKTQ_ENQUEUE_MULTI(q, p, _n, _c); \ 99*043036a2SApple OSS Distributions } while (0) 100*043036a2SApple OSS Distributions 101*043036a2SApple OSS Distributions #define KPKTQ_DEQUEUE(q, p) do { \ 102*043036a2SApple OSS Distributions if (((p) = KPKTQ_FIRST(q)) != NULL) { \ 103*043036a2SApple OSS Distributions (q)->kq_len--; \ 104*043036a2SApple OSS Distributions if ((KPKTQ_FIRST(q) = KPKTQ_NEXT(p)) == NULL) { \ 105*043036a2SApple OSS Distributions ASSERT((q)->kq_len == 0); \ 106*043036a2SApple OSS Distributions (q)->kq_last = &KPKTQ_FIRST(q); \ 107*043036a2SApple OSS Distributions } else { \ 108*043036a2SApple OSS Distributions KPKTQ_NEXT(p) = NULL; \ 109*043036a2SApple OSS Distributions } \ 110*043036a2SApple OSS Distributions } \ 111*043036a2SApple OSS Distributions } while (0) 112*043036a2SApple OSS Distributions 113*043036a2SApple OSS Distributions #define KPKTQ_REMOVE(q, p) do { \ 114*043036a2SApple OSS Distributions if (KPKTQ_FIRST(q) == (p)) { \ 115*043036a2SApple OSS Distributions KPKTQ_DEQUEUE(q, p); \ 116*043036a2SApple OSS Distributions } else { \ 117*043036a2SApple OSS Distributions struct __kern_packet *_p = KPKTQ_FIRST(q); \ 118*043036a2SApple OSS Distributions while (KPKTQ_NEXT(_p) != (p)) \ 119*043036a2SApple OSS Distributions _p = KPKTQ_NEXT(_p); \ 120*043036a2SApple OSS Distributions if ((KPKTQ_NEXT(_p) = \ 121*043036a2SApple OSS Distributions KPKTQ_NEXT(KPKTQ_NEXT(_p))) == NULL) { \ 122*043036a2SApple OSS Distributions (q)->kq_last = &KPKTQ_NEXT(_p); \ 123*043036a2SApple OSS Distributions } \ 124*043036a2SApple OSS Distributions (q)->kq_len--; \ 125*043036a2SApple OSS Distributions KPKTQ_NEXT(p) = NULL; \ 126*043036a2SApple OSS Distributions } \ 127*043036a2SApple OSS Distributions } while (0) 128*043036a2SApple OSS Distributions 129*043036a2SApple OSS Distributions #define KPKTQ_FOREACH(p, q) \ 130*043036a2SApple OSS Distributions for ((p) = KPKTQ_FIRST(q); \ 131*043036a2SApple OSS Distributions (p); \ 132*043036a2SApple OSS Distributions (p) = KPKTQ_NEXT(p)) 133*043036a2SApple OSS Distributions 134*043036a2SApple OSS Distributions #define KPKTQ_FOREACH_SAFE(p, q, tvar) \ 135*043036a2SApple OSS Distributions for ((p) = KPKTQ_FIRST(q); \ 136*043036a2SApple OSS Distributions (p) && ((tvar) = KPKTQ_NEXT(p), 1); \ 137*043036a2SApple OSS Distributions (p) = (tvar)) 138*043036a2SApple OSS Distributions 139*043036a2SApple OSS Distributions #define KPKTQ_EMPTY(q) ((q)->kq_first == NULL) 140*043036a2SApple OSS Distributions #define KPKTQ_FIRST(q) ((q)->kq_first) 141*043036a2SApple OSS Distributions #define KPKTQ_NEXT(p) ((p)->pkt_nextpkt) 142*043036a2SApple OSS Distributions #define KPKTQ_LEN(q) ((q)->kq_len) 143*043036a2SApple OSS Distributions 144*043036a2SApple OSS Distributions /* 145*043036a2SApple OSS Distributions * kq_last is initialized to point to kq_first, so check if they're 146*043036a2SApple OSS Distributions * equal and return NULL when the list is empty. Otherwise, we need 147*043036a2SApple OSS Distributions * to subtract the offset of KPKTQ_NEXT (i.e. pkt_nextpkt field) to get 148*043036a2SApple OSS Distributions * to the base packet address to return to caller. 149*043036a2SApple OSS Distributions */ 150*043036a2SApple OSS Distributions #define KPKTQ_LAST(head) \ 151*043036a2SApple OSS Distributions (((head)->kq_last == &KPKTQ_FIRST(head)) ? NULL : \ 152*043036a2SApple OSS Distributions __container_of((head)->kq_last, struct __kern_packet, pkt_nextpkt)) 153*043036a2SApple OSS Distributions 154*043036a2SApple OSS Distributions /* 155*043036a2SApple OSS Distributions * struct pktq serves as basic common batching data structure using KPKTQ. 156*043036a2SApple OSS Distributions * Elementary types of batch data structure, e.g. packet array, should be named 157*043036a2SApple OSS Distributions * as pkts. 158*043036a2SApple OSS Distributions * For instance: 159*043036a2SApple OSS Distributions * rx_dequeue_pktq(struct pktq *pktq); 160*043036a2SApple OSS Distributions * rx_dequeue_pkts(struct __kern_packet *pkts[], uint32_t n_pkts); 161*043036a2SApple OSS Distributions */ 162*043036a2SApple OSS Distributions KPKTQ_HEAD(pktq); 163*043036a2SApple OSS Distributions 164*043036a2SApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */ 165*043036a2SApple OSS Distributions #endif /* !_SKYWALK_PACKET_PACKETQUEUE_H_ */ 166