⬢ DragonFlyBSD Kernel Audit
sys/net/ifq_var.h
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*-
 * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _NET_IFQ_VAR_H_
#define _NET_IFQ_VAR_H_

#ifndef _KERNEL
#error "This file should not be included by userland programs."
#endif

#ifndef _SYS_SYSTM_H_
#include <sys/systm.h>
#endif
#ifndef _SYS_SERIALIZE_H_
#include <sys/serialize.h>
#endif
#ifndef _SYS_MBUF_H_
#include <sys/mbuf.h>
#endif
#ifndef _NET_IF_VAR_H_
#include <net/if_var.h>
#endif
#ifndef _NET_ALTQ_IF_ALTQ_H_
#include <net/altq/if_altq.h>
#endif

#define ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq) \
	KASSERT(ifsq_get_ifp((ifsq)) == (ifp) && \
	    ifsq_get_index((ifsq)) == ALTQ_SUBQ_INDEX_DEFAULT, \
	    ("not ifp's default subqueue"));

struct ifaltq;
struct ifaltq_subque;

/*
 * Subqueue watchdog
 */
typedef void	(*ifsq_watchdog_t)(struct ifaltq_subque *);

struct ifsubq_watchdog {
	struct callout	wd_callout;
	int		wd_timer;
	int		wd_flags;
	struct ifaltq_subque *wd_subq;
	ifsq_watchdog_t	wd_watchdog;
};

#define IF_WDOG_ALLTICKS	0x00000001
#define IF_WDOG_LASTTICK	0x00000002

/*
 * Support for "classic" ALTQ interfaces.
 */
int		ifsq_classic_enqueue(struct ifaltq_subque *, struct mbuf *,
		    struct altq_pktattr *);
struct mbuf	*ifsq_classic_dequeue(struct ifaltq_subque *, int);
int		ifsq_classic_request(struct ifaltq_subque *, int, void *);
void		ifq_set_classic(struct ifaltq *);

void		ifq_set_maxlen(struct ifaltq *, int);
void		ifq_set_methods(struct ifaltq *, altq_mapsubq_t,
		    ifsq_enqueue_t, ifsq_dequeue_t, ifsq_request_t);
int		ifq_mapsubq_default(struct ifaltq *, int);
int		ifq_mapsubq_modulo(struct ifaltq *, int);

void		ifsq_devstart(struct ifaltq_subque *ifsq);
void		ifsq_devstart_sched(struct ifaltq_subque *ifsq);

void		ifsq_watchdog_init(struct ifsubq_watchdog *,
		    struct ifaltq_subque *, ifsq_watchdog_t, int);
void		ifsq_watchdog_start(struct ifsubq_watchdog *);
void		ifsq_watchdog_stop(struct ifsubq_watchdog *);
void		ifsq_watchdog_set_count(struct ifsubq_watchdog *, int);

/*
 * Dispatch a packet to an interface.
 */
int		ifq_dispatch(struct ifnet *, struct mbuf *,
		    struct altq_pktattr *);

#ifdef ALTQ

static __inline int
ifq_is_enabled(struct ifaltq *_ifq)
{
	return(_ifq->altq_flags & ALTQF_ENABLED);
}

static __inline int
ifq_is_attached(struct ifaltq *_ifq)
{
	return(_ifq->altq_disc != NULL);
}

#else	/* !ALTQ */

static __inline int
ifq_is_enabled(struct ifaltq *_ifq)
{
	return(0);
}

static __inline int
ifq_is_attached(struct ifaltq *_ifq)
{
	return(0);
}

#endif	/* ALTQ */

static __inline int
ifq_is_ready(struct ifaltq *_ifq)
{
	return(_ifq->altq_flags & ALTQF_READY);
}

static __inline void
ifq_set_ready(struct ifaltq *_ifq)
{
	_ifq->altq_flags |= ALTQF_READY;
}

/*
 * Subqueue lock must be held
 */
static __inline int
ifsq_enqueue_locked(struct ifaltq_subque *_ifsq, struct mbuf *_m,
    struct altq_pktattr *_pa)
{
#ifdef ALTQ
	if (!ifq_is_enabled(_ifsq->ifsq_altq))
		return ifsq_classic_enqueue(_ifsq, _m, _pa);
	else
#endif
	return _ifsq->ifsq_enqueue(_ifsq, _m, _pa);
}

static __inline int
ifsq_enqueue(struct ifaltq_subque *_ifsq, struct mbuf *_m,
    struct altq_pktattr *_pa)
{
	int _error;

	ALTQ_SQ_LOCK(_ifsq);
	_error = ifsq_enqueue_locked(_ifsq, _m, _pa);
	ALTQ_SQ_UNLOCK(_ifsq);
	return _error;
}

static __inline struct mbuf *
ifsq_dequeue(struct ifaltq_subque *_ifsq)
{
	struct mbuf *_m;

	ALTQ_SQ_LOCK(_ifsq);
	if (_ifsq->ifsq_prepended != NULL) {
		_m = _ifsq->ifsq_prepended;
		_ifsq->ifsq_prepended = NULL;
		ALTQ_SQ_CNTR_DEC(_ifsq, _m->m_pkthdr.len);
		ALTQ_SQ_UNLOCK(_ifsq);
		return _m;
	}

#ifdef ALTQ
	if (_ifsq->ifsq_altq->altq_tbr != NULL)
		_m = tbr_dequeue(_ifsq, ALTDQ_REMOVE);
	else if (!ifq_is_enabled(_ifsq->ifsq_altq))
		_m = ifsq_classic_dequeue(_ifsq, ALTDQ_REMOVE);
	else
#endif
	_m = _ifsq->ifsq_dequeue(_ifsq, ALTDQ_REMOVE);
	ALTQ_SQ_UNLOCK(_ifsq);
	return _m;
}

/*
 * Subqueue lock must be held
 */
static __inline struct mbuf *
ifsq_poll_locked(struct ifaltq_subque *_ifsq)
{
	if (_ifsq->ifsq_prepended != NULL)
		return _ifsq->ifsq_prepended;

#ifdef ALTQ
	if (_ifsq->ifsq_altq->altq_tbr != NULL)
		return tbr_dequeue(_ifsq, ALTDQ_POLL);
	else if (!ifq_is_enabled(_ifsq->ifsq_altq))
		return ifsq_classic_dequeue(_ifsq, ALTDQ_POLL);
	else
#endif
	return _ifsq->ifsq_dequeue(_ifsq, ALTDQ_POLL);
}

static __inline struct mbuf *
ifsq_poll(struct ifaltq_subque *_ifsq)
{
	struct mbuf *_m;

	ALTQ_SQ_LOCK(_ifsq);
	_m = ifsq_poll_locked(_ifsq);
	ALTQ_SQ_UNLOCK(_ifsq);
	return _m;
}

static __inline int
ifsq_poll_pktlen(struct ifaltq_subque *_ifsq)
{
	struct mbuf *_m;
	int _len = 0;

	ALTQ_SQ_LOCK(_ifsq);

	_m = ifsq_poll_locked(_ifsq);
	if (_m != NULL) {
		M_ASSERTPKTHDR(_m);
		_len = _m->m_pkthdr.len;
	}

	ALTQ_SQ_UNLOCK(_ifsq);

	return _len;
}

/*
 * Subqueue lock must be held
 */
static __inline void
ifsq_purge_locked(struct ifaltq_subque *_ifsq)
{
	if (_ifsq->ifsq_prepended != NULL) {
		ALTQ_SQ_CNTR_DEC(_ifsq, _ifsq->ifsq_prepended->m_pkthdr.len);
		m_freem(_ifsq->ifsq_prepended);
		_ifsq->ifsq_prepended = NULL;
	}

#ifdef ALTQ
	if (!ifq_is_enabled(_ifsq->ifsq_altq))
		ifsq_classic_request(_ifsq, ALTRQ_PURGE, NULL);
	else
#endif
	_ifsq->ifsq_request(_ifsq, ALTRQ_PURGE, NULL);
}

static __inline void
ifsq_purge(struct ifaltq_subque *_ifsq)
{
	ALTQ_SQ_LOCK(_ifsq);
	ifsq_purge_locked(_ifsq);
	ALTQ_SQ_UNLOCK(_ifsq);
}

static __inline void
ifq_lock_all(struct ifaltq *_ifq)
{
	int _q;

	for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
		ALTQ_SQ_LOCK(&_ifq->altq_subq[_q]);
}

static __inline void
ifq_unlock_all(struct ifaltq *_ifq)
{
	int _q;

	for (_q = _ifq->altq_subq_cnt - 1; _q >= 0; --_q)
		ALTQ_SQ_UNLOCK(&_ifq->altq_subq[_q]);
}

/*
 * All of the subqueue locks must be held
 */
static __inline void
ifq_purge_all_locked(struct ifaltq *_ifq)
{
	int _q;

	for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
		ifsq_purge_locked(&_ifq->altq_subq[_q]);
}

static __inline void
ifq_purge_all(struct ifaltq *_ifq)
{
	ifq_lock_all(_ifq);
	ifq_purge_all_locked(_ifq);
	ifq_unlock_all(_ifq);
}

static __inline void
ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
    struct altq_pktattr *_pa)
{
#ifdef ALTQ
	if (ifq_is_enabled(_ifq)) {
		_pa->pattr_af = _af;
		_pa->pattr_hdr = mtod(_m, caddr_t);
		if (ifq_is_enabled(_ifq) &&
		    (_ifq->altq_flags & ALTQF_CLASSIFY)) {
			/* XXX default subqueue */
			struct ifaltq_subque *_ifsq =
			    &_ifq->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];

			ALTQ_SQ_LOCK(_ifsq);
			if (ifq_is_enabled(_ifq) &&
			    (_ifq->altq_flags & ALTQF_CLASSIFY))
				_ifq->altq_classify(_ifq, _m, _pa);
			ALTQ_SQ_UNLOCK(_ifsq);
		}
	}
#endif
}

static __inline void
ifsq_prepend(struct ifaltq_subque *_ifsq, struct mbuf *_m)
{
	ALTQ_SQ_LOCK(_ifsq);
	KASSERT(_ifsq->ifsq_prepended == NULL, ("pending prepended mbuf"));
	_ifsq->ifsq_prepended = _m;
	ALTQ_SQ_CNTR_INC(_ifsq, _m->m_pkthdr.len);
	ALTQ_SQ_UNLOCK(_ifsq);
}

/*
 * Subqueue hardware serializer must be held
 */
static __inline void
ifsq_set_oactive(struct ifaltq_subque *_ifsq)
{
	_ifsq->ifsq_hw_oactive = 1;
}

/*
 * Subqueue hardware serializer must be held
 */
static __inline void
ifsq_clr_oactive(struct ifaltq_subque *_ifsq)
{
	_ifsq->ifsq_hw_oactive = 0;
}

/*
 * Subqueue hardware serializer must be held
 */
static __inline int
ifsq_is_oactive(const struct ifaltq_subque *_ifsq)
{
	return _ifsq->ifsq_hw_oactive;
}

/*
 * Hand a packet to the interface's default subqueue.
 *
 * The default subqueue hardware serializer must be held.  If the
 * subqueue hardware serializer is not held yet, ifq_dispatch()
 * should be used to get better performance.
 */
static __inline int
ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
{
	struct ifaltq_subque *_ifsq;
	int _error;
	int _qid = ALTQ_SUBQ_INDEX_DEFAULT; /* XXX default subqueue */

	_ifsq = &_ifp->if_snd.altq_subq[_qid];

	ASSERT_ALTQ_SQ_SERIALIZED_HW(_ifsq);
	_error = ifsq_enqueue(_ifsq, _m, _pa);
	if (_error == 0) {
		IFNET_STAT_INC(_ifp, obytes, _m->m_pkthdr.len);
		if (_m->m_flags & M_MCAST)
			IFNET_STAT_INC(_ifp, omcasts, 1);
		if (!ifsq_is_oactive(_ifsq))
			(*_ifp->if_start)(_ifp, _ifsq);
	} else {
		IFNET_STAT_INC(_ifp, oqdrops, 1);
	}
	return(_error);
}

static __inline int
ifsq_is_empty(const struct ifaltq_subque *_ifsq)
{
	return(_ifsq->ifsq_len == 0);
}

/*
 * Subqueue lock must be held
 */
static __inline int
ifsq_data_ready(struct ifaltq_subque *_ifsq)
{
#ifdef ALTQ
	if (_ifsq->ifsq_altq->altq_tbr != NULL)
		return (ifsq_poll_locked(_ifsq) != NULL);
	else
#endif
	return !ifsq_is_empty(_ifsq);
}

/*
 * Subqueue lock must be held
 */
static __inline int
ifsq_is_started(const struct ifaltq_subque *_ifsq)
{
	return _ifsq->ifsq_started;
}

/*
 * Subqueue lock must be held
 */
static __inline void
ifsq_set_started(struct ifaltq_subque *_ifsq)
{
	_ifsq->ifsq_started = 1;
}

/*
 * Subqueue lock must be held
 */
static __inline void
ifsq_clr_started(struct ifaltq_subque *_ifsq)
{
	_ifsq->ifsq_started = 0;
}

static __inline struct ifsubq_stage *
ifsq_get_stage(struct ifaltq_subque *_ifsq, int _cpuid)
{
	return &_ifsq->ifsq_stage[_cpuid];
}

static __inline int
ifsq_get_cpuid(const struct ifaltq_subque *_ifsq)
{
	return _ifsq->ifsq_cpuid;
}

static __inline void
ifsq_set_cpuid(struct ifaltq_subque *_ifsq, int _cpuid)
{
	KASSERT(_cpuid >= 0 && _cpuid < ncpus,
	    ("invalid ifsq_cpuid %d", _cpuid));
	_ifsq->ifsq_cpuid = _cpuid;
}

static __inline struct lwkt_msg *
ifsq_get_ifstart_lmsg(struct ifaltq_subque *_ifsq, int _cpuid)
{
	return &_ifsq->ifsq_ifstart_nmsg[_cpuid].lmsg;
}

static __inline int
ifsq_get_index(const struct ifaltq_subque *_ifsq)
{
	return _ifsq->ifsq_index;
}

static __inline void
ifsq_set_priv(struct ifaltq_subque *_ifsq, void *_priv)
{
	_ifsq->ifsq_hw_priv = _priv;
}

static __inline void *
ifsq_get_priv(const struct ifaltq_subque *_ifsq)
{
	return _ifsq->ifsq_hw_priv;
}

static __inline struct ifnet *
ifsq_get_ifp(const struct ifaltq_subque *_ifsq)
{
	return _ifsq->ifsq_ifp;
}

static __inline void
ifsq_set_hw_serialize(struct ifaltq_subque *_ifsq,
    struct lwkt_serialize *_hwslz)
{
	KASSERT(_hwslz != NULL, ("NULL hw serialize"));
	KASSERT(_ifsq->ifsq_hw_serialize == NULL,
	    ("hw serialize has been setup"));
	_ifsq->ifsq_hw_serialize = _hwslz;
}

static __inline void
ifsq_serialize_hw(struct ifaltq_subque *_ifsq)
{
	lwkt_serialize_enter(_ifsq->ifsq_hw_serialize);
}

static __inline void
ifsq_deserialize_hw(struct ifaltq_subque *_ifsq)
{
	lwkt_serialize_exit(_ifsq->ifsq_hw_serialize);
}

static __inline int
ifsq_tryserialize_hw(struct ifaltq_subque *_ifsq)
{
	return lwkt_serialize_try(_ifsq->ifsq_hw_serialize);
}

static __inline struct ifaltq_subque *
ifq_get_subq_default(const struct ifaltq *_ifq)
{
	return &_ifq->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
}

static __inline struct ifaltq_subque *
ifq_get_subq(const struct ifaltq *_ifq, int _idx)
{
	KASSERT(_idx >= 0 && _idx < _ifq->altq_subq_cnt,
	    ("invalid qid %d", _idx));
	return &_ifq->altq_subq[_idx];
}

static __inline struct ifaltq_subque *
ifq_map_subq(struct ifaltq *_ifq, int _cpuid)
{ 
	int _idx = _ifq->altq_mapsubq(_ifq, _cpuid);
	return ifq_get_subq(_ifq, _idx);
}

static __inline void
ifq_set_subq_cnt(struct ifaltq *_ifq, int _cnt)
{
	_ifq->altq_subq_cnt = _cnt;
}

static __inline void
ifq_set_subq_divisor(struct ifaltq *_ifq, uint32_t _divisor)
{

	KASSERT(_divisor > 0, ("invalid divisor %u", _divisor));
	KASSERT(_divisor <= _ifq->altq_subq_cnt,
	    ("invalid divisor %u, max %d", _divisor, _ifq->altq_subq_cnt));
	_ifq->altq_subq_mappriv = _divisor;
}

/* COMPAT */
static __inline int
ifq_is_oactive(const struct ifaltq *_ifq)
{
	return ifsq_is_oactive(ifq_get_subq_default(_ifq));
}

/* COMPAT */
static __inline void
ifq_set_oactive(struct ifaltq *_ifq)
{
	ifsq_set_oactive(ifq_get_subq_default(_ifq));
}

/* COMPAT */
static __inline void
ifq_clr_oactive(struct ifaltq *_ifq)
{
	ifsq_clr_oactive(ifq_get_subq_default(_ifq));
}

/* COMPAT */
static __inline int
ifq_is_empty(struct ifaltq *_ifq)
{
	return ifsq_is_empty(ifq_get_subq_default(_ifq));
}

/* COMPAT */
static __inline void
ifq_purge(struct ifaltq *_ifq)
{
	ifsq_purge(ifq_get_subq_default(_ifq));
}

/* COMPAT */
static __inline struct mbuf *
ifq_dequeue(struct ifaltq *_ifq)
{
	return ifsq_dequeue(ifq_get_subq_default(_ifq));
}

/* COMPAT */
static __inline void
ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
{
	ifsq_prepend(ifq_get_subq_default(_ifq), _m);
}

/* COMPAT */
static __inline void
ifq_set_cpuid(struct ifaltq *_ifq, int _cpuid)
{
	KASSERT(_ifq->altq_subq_cnt == 1,
	    ("invalid subqueue count %d", _ifq->altq_subq_cnt));
	ifsq_set_cpuid(ifq_get_subq_default(_ifq), _cpuid);
}

/* COMPAT */
static __inline void
ifq_set_hw_serialize(struct ifaltq *_ifq, struct lwkt_serialize *_hwslz)
{
	KASSERT(_ifq->altq_subq_cnt == 1,
	    ("invalid subqueue count %d", _ifq->altq_subq_cnt));
	ifsq_set_hw_serialize(ifq_get_subq_default(_ifq), _hwslz);
}

#endif	/* _NET_IFQ_VAR_H_ */