]> git.ozlabs.org Git - ppp.git/blob - NeXT/inlines.h
priv option stuff; add option_error proto
[ppp.git] / NeXT / inlines.h
1 /*
2  * Netbufs don't come with nifty queuing functions
3  * like mbufs. We therefore make our own quques by
4  * squirreling away an extra pointer before the data
5  * in a netbuf. As we can't guarantee that this will
6  * be aligned to anything in particular I use bcopy to
7  * read and write it. bcopy can use 32 bit if it really
8  * feels like it...
9  * PCF
10  *
11
12 #if defined(m68k)
13 #import "spl.h"
14 #else
15 #import <kernserv/machine/spl.h>
16 #endif
17 #include <kernserv/kern_server_types.h>
18 #include <kernserv/kalloc.h>
19 #include "nbq.h"
20
21 /*
22  * There is no driver kit for the Moto release.
23  */
24 #ifndef IOLog
25 #define IOLog printf
26 #define IOLogDbg                if (sc->sc_flags & SC_DEBUG) printf
27 #else
28 #define IOLogDbg                if (sc->sc_flags & SC_DEBUG) IOLog
29 #endif
30
31 extern kern_server_t instance;
32
33 /*
34  * Careful about using this function.  Some places
35  * in the code drop packets based on this count
36  * but they never free them.
37  */
38
39 static inline int
40 nbq_full(struct nb_queue* nbq)
41 {
42     int rv;
43     rv = (nbq->len >= nbq->max);
44     return rv;
45 }
46
47 static inline int
48 nbq_empty(struct nb_queue* nbq)
49 {
50     int rv;
51     rv = (!nbq->head);
52     return rv;
53 }
54
55 static inline int
56 nbq_low(struct nb_queue* nbq)
57 {
58     int rv;
59     rv = (nbq->len <= nbq->low);
60     return rv;
61 }
62
63 static inline int
64 nbq_high(struct nb_queue* nbq)
65 {
66     int rv;
67     rv = (nbq->len >= nbq->high);
68     return rv;
69 }
70
71 static inline netbuf_t
72 nbq_peek(struct nb_queue* nbq)
73 {
74     int s;
75     netbuf_t nb;
76
77     s = splimp();
78     nb = nbq->head;
79     splx(s);
80     return nb;
81 }
82
83 static inline netbuf_t
84 nbq_dequeue(struct nb_queue* nbq)
85 {
86   int s;
87   netbuf_t nb;
88
89   if (!nbq->head)
90       return NULL;
91
92   s = splimp();
93   nb = nbq->head;
94   nb_get_next(nb,&nbq->head);
95   if (!nbq->head)
96         nbq->tail = NULL;
97   --nbq->len;
98   splx(s);
99
100   return nb;
101 }
102
103 /*
104  * One simple note about nbq_enqueue: it will enqueue packets even if
105  * it is full, so the caller is responsible for checking this first...
106  *
107  * We return 1 if we added, else we return 0
108  * if there was a problem. We leave it up to the caller
109  * to detect an error return value (0) and print
110  * an appropriate message/update stats.  However, in the spirit of
111  * keeping the code as close to the netbsd version as is possible,
112  * WE WILL FREE a packet that can't be enqueued.  This should be the
113  * responsibility of the caller but that is currently not the case.
114  *
115  * Actually, now I'm using the hidden pointer arrangement then theres
116  * no circumstances under which this can return 0, oh well...
117  * PCF
118  */
119
120 static inline int
121 nbq_enqueue(struct nb_queue* nbq, netbuf_t nb)
122 {
123   int s;
124
125   nb_set_next(nb,NULL);
126   s = splimp();
127   if (nbq->tail)
128     nb_set_next(nbq->tail,nb);
129   else
130     nbq->head = nb;
131   nbq->tail = nb;
132   ++nbq->len;
133   splx(s);
134   return 1;
135 }
136
137 static inline void
138 nbq_flush(struct nb_queue *nbq)
139 {
140     netbuf_t nb,temp;
141     int s;
142
143     s  = splimp();
144     nb = nbq->head;
145     while(nb) {
146         temp=nb;
147         nb_get_next(nb,&nb);
148         nb_free(temp);
149     }
150
151     nbq->head = nbq->tail = NULL;
152     nbq->len = 0;
153     nbq->dropped = 0;
154     splx(s);
155 }
156
157 /*
158  * Must not be called at interrupt priority
159  */
160
161 static inline void
162 nbq_init(struct nb_queue *nbq, struct qparms *qp)
163 {
164   nbq->name = qp->q_name;
165   nbq->head = nbq->tail = NULL;
166   nbq->low = qp->q_low;
167   nbq->high = qp->q_high;
168   nbq->max = qp->q_max;
169   nbq->len = 0;
170   nbq->dropped = 0;
171 }
172
173 static inline void
174 nbq_free(struct nb_queue *nbq)
175 {
176   nbq_flush(nbq);
177 }
178
179 static inline void
180 nbq_drop(struct nb_queue *nbq)
181 {
182     ++nbq->dropped;
183 }
184
185 /*
186  * Not very pretty, but it makes for less "diffs"...
187  */
188 #define mtod(m,type)    ((type) nb_map(m))
189
190 typedef void (*pfv)(void *);
191
192 /* used by both ppp_tty.c and if_ppp.c */
193 static inline kern_return_t
194 pppsched(pfv func, struct ppp_softc *sc)
195 {
196     extern kern_server_t instance;
197     kern_return_t result;
198
199     if ((result = kern_serv_callout(&instance, func, (void *)sc)) != KERN_SUCCESS)
200       IOLog("kern_serv_callout failed: ret = %x\n", result);
201
202     return result;
203 }
204
205 #undef  u
206
207 static inline thread_t
208 current_thread(void)
209 {
210         extern thread_t active_threads[];
211
212         return active_threads[0];
213 }
214
215 extern struct proc *proc_from_thread(thread_t);
216 extern struct uthread *uthread_from_thread(thread_t);
217
218 #define curproc         (proc_from_thread(current_thread()))
219
220 #ifdef NBPFILTER
221 #include <bpf/bpf.h>
222
223 extern struct bpf_fns fnarg;
224
225 static inline void
226 bpfattach(caddr_t *driverp, netif_t ifp, u_int dlt, u_int hdrlen)
227 {
228   struct bpf_attachargs atarg = {driverp, (caddr_t) ifp, dlt, hdrlen};
229
230   if (cdevsw[BPF_MAJOR_CHAR].d_ioctl != 0)
231     {
232       (*cdevsw[BPF_MAJOR_CHAR].d_ioctl)(0, BIOCATTACH, &atarg, 0);
233       (*cdevsw[BPF_MAJOR_CHAR].d_ioctl)(0, BIOCGFNS, &fnarg, 0);
234     }
235 }
236
237
238 static inline void
239 bpf_tap(caddr_t arg, u_char *pkt, u_int pktlen)
240 {
241   (*fnarg.tapfn)(arg, pkt, pktlen);
242 }
243 #endif