Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: qop_priq.c,v 1.6 2013/10/19 17:16:37 christos Exp $	*/
/*	$KAME: qop_priq.c,v 1.4 2001/12/03 08:20:55 kjc Exp $	*/
/*
 * Copyright (C) 2000
 *	Sony Computer Science Laboratories, Inc.  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.
 *
 * THIS SOFTWARE IS PROVIDED BY SONY CSL 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 SONY CSL 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.
 */

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <syslog.h>
#include <netdb.h>

#include <altq/altq.h>
#include <altq/altq_priq.h>
#include "altq_qop.h"
#include "qop_priq.h"

static int qop_priq_enable_hook(struct ifinfo *);

static int priq_attach(struct ifinfo *);
static int priq_detach(struct ifinfo *);
static int priq_clear(struct ifinfo *);
static int priq_enable(struct ifinfo *);
static int priq_disable(struct ifinfo *);
static int priq_add_class(struct classinfo *);
static int priq_modify_class(struct classinfo *, void *);
static int priq_delete_class(struct classinfo *);
static int priq_add_filter(struct fltrinfo *);
static int priq_delete_filter(struct fltrinfo *);

#define PRIQ_DEVICE	"/dev/altq/priq"

static int priq_fd = -1;
static int priq_refcount = 0;

static struct qdisc_ops priq_qdisc = {
	ALTQT_PRIQ,
	"priq",
	priq_attach,
	priq_detach,
	priq_clear,
	priq_enable,
	priq_disable,
	priq_add_class,
	priq_modify_class,
	priq_delete_class,
	priq_add_filter,
	priq_delete_filter,
};

#define EQUAL(s1, s2)	(strcmp((s1), (s2)) == 0)

/*
 * parser interface
 */
int
priq_interface_parser(const char *ifname, int argc, char **argv)
{
	u_int  	bandwidth = 100000000;	/* 100Mbps */
	u_int	tbrsize = 0;
	int	flags = 0;

	/*
	 * process options
	 */
	while (argc > 0) {
		if (EQUAL(*argv, "bandwidth")) {
			argc--; argv++;
			if (argc > 0)
				bandwidth = atobps(*argv);
		} else if (EQUAL(*argv, "tbrsize")) {
			argc--; argv++;
			if (argc > 0)
				tbrsize = atobytes(*argv);
		} else if (EQUAL(*argv, "priq")) {
			/* just skip */
		} else {
			LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
			return (0);
		}
		argc--; argv++;
	}

	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
		return (0);

	if (qcmd_priq_add_if(ifname, bandwidth, flags) != 0)
		return (0);
	return (1);
}

int
priq_class_parser(const char *ifname, const char *class_name,
		  const char *parent_name, int argc, char **argv)
{
	int	pri = 0, qlimit = 50;
	int	flags = 0, error;

	while (argc > 0) {
		if (EQUAL(*argv, "priority")) {
			argc--; argv++;
			if (argc > 0)
				pri = strtoul(*argv, NULL, 0);
		} else if (EQUAL(*argv, "qlimit")) {
			argc--; argv++;
			if (argc > 0)
				qlimit = strtoul(*argv, NULL, 0);
		} else if (EQUAL(*argv, "default")) {
			flags |= PRCF_DEFAULTCLASS;
		} else if (EQUAL(*argv, "red")) {
			flags |= PRCF_RED;
		} else if (EQUAL(*argv, "ecn")) {
			flags |= PRCF_ECN;
		} else if (EQUAL(*argv, "rio")) {
			flags |= PRCF_RIO;
		} else if (EQUAL(*argv, "cleardscp")) {
			flags |= PRCF_CLEARDSCP;
		} else {
			LOG(LOG_ERR, 0,
			    "Unknown keyword '%s' in %s, line %d",
			    *argv, altqconfigfile, line_no);
			return (0);
		}

		argc--; argv++;
	}

	if ((flags & PRCF_ECN) && (flags & (PRCF_RED|PRCF_RIO)) == 0)
		flags |= PRCF_RED;

	error = qcmd_priq_add_class(ifname, class_name, pri, qlimit, flags);

	if (error) {
		LOG(LOG_ERR, errno, "priq_class_parser: %s",
		    qoperror(error));
		return (0);
	}
	return (1);
}

/*
 * qcmd api
 */
int
qcmd_priq_add_if(const char *ifname, u_int bandwidth, int flags)
{
	int error;
	
	error = qop_priq_add_if(NULL, ifname, bandwidth, flags);
	if (error != 0)
		LOG(LOG_ERR, errno, "%s: can't add priq on interface '%s'",
		    qoperror(error), ifname);
	return (error);
}

int
qcmd_priq_add_class(const char *ifname, const char *class_name,
		    int pri, int qlimit, int flags)
{
	struct ifinfo *ifinfo;
	int error = 0;

	if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
		error = QOPERR_BADIF;

	if (error == 0)
		error = qop_priq_add_class(NULL, class_name, ifinfo,
					   pri, qlimit, flags);
	if (error != 0)
		LOG(LOG_ERR, errno,
		    "priq: %s: can't add class '%s' on interface '%s'",
		    qoperror(error), class_name, ifname);
	return (error);
}

int
qcmd_priq_modify_class(const char *ifname, const char *class_name,
		       int pri, int qlimit, int flags)
{
	struct ifinfo *ifinfo;
	struct classinfo *clinfo;

	if ((ifinfo = ifname2ifinfo(ifname)) == NULL)
		return (QOPERR_BADIF);

	if ((clinfo = clname2clinfo(ifinfo, class_name)) == NULL)
		return (QOPERR_BADCLASS);

	return qop_priq_modify_class(clinfo, pri, qlimit, flags);
}

/*
 * qop api
 */
int 
qop_priq_add_if(struct ifinfo **rp, const char *ifname,
		u_int bandwidth, int flags)
{
	struct ifinfo *ifinfo = NULL;
	struct priq_ifinfo *priq_ifinfo = NULL;
	int error;

	if ((priq_ifinfo = calloc(1, sizeof(*priq_ifinfo))) == NULL)
		return (QOPERR_NOMEM);

	error = qop_add_if(&ifinfo, ifname, bandwidth,
			   &priq_qdisc, priq_ifinfo);
	if (error != 0)
		goto err_ret;

	/* set enable hook */
	ifinfo->enable_hook = qop_priq_enable_hook;

	if (rp != NULL)
		*rp = ifinfo;
	return (0);

 err_ret:
	if (priq_ifinfo != NULL) {
		free(priq_ifinfo);
		if (ifinfo != NULL)
			ifinfo->private = NULL;
	}
	return (error);
}

int 
qop_priq_add_class(struct classinfo **rp, const char *class_name,
		   struct ifinfo *ifinfo, int pri, int qlimit, int flags)
{
	struct classinfo *clinfo;
	struct priq_ifinfo *priq_ifinfo;
	struct priq_classinfo *priq_clinfo = NULL;
	int error;

	priq_ifinfo = ifinfo->private;
	if ((flags & PRCF_DEFAULTCLASS) && priq_ifinfo->default_class != NULL)
		return (QOPERR_CLASS_INVAL);

	if ((priq_clinfo = calloc(1, sizeof(*priq_clinfo))) == NULL) {
		error = QOPERR_NOMEM;
		goto err_ret;
	}

	priq_clinfo->pri = pri;
	priq_clinfo->qlimit = qlimit;
	priq_clinfo->flags = flags;

	if ((error = qop_add_class(&clinfo, class_name, ifinfo, NULL,
				   priq_clinfo)) != 0)
		goto err_ret;

	if (flags & PRCF_DEFAULTCLASS)
		priq_ifinfo->default_class = clinfo;

	if (rp != NULL)
		*rp = clinfo;
	return (0);

 err_ret:
	if (priq_clinfo != NULL) {
		free(priq_clinfo);
		clinfo->private = NULL;
	}
	
	return (error);
}

int
qop_priq_modify_class(struct classinfo *clinfo, 
		      int pri, int qlimit, int flags)
{
	struct priq_classinfo *priq_clinfo;
	int error;

	priq_clinfo = clinfo->private;
	if (clinfo->parent == NULL)
		return (QOPERR_CLASS_INVAL);

	priq_clinfo->pri = pri;
	priq_clinfo->qlimit = qlimit;
	priq_clinfo->flags = flags;

	error = qop_modify_class(clinfo, NULL);
	if (error == 0)
		return (0);
	return (error);
}

/*
 * sanity check at enabling priq:
 *  1. there must one default class for an interface
 */
static int
qop_priq_enable_hook(struct ifinfo *ifinfo)
{
	struct priq_ifinfo *priq_ifinfo;
	
	priq_ifinfo = ifinfo->private;
	if (priq_ifinfo->default_class == NULL) {
		LOG(LOG_ERR, 0, "priq: no default class on interface %s!",
		    ifinfo->ifname);
		return (QOPERR_CLASS);
	}
	return (0);
}

/*
 *  system call interfaces for qdisc_ops
 */
static int
priq_attach(struct ifinfo *ifinfo)
{
	struct priq_interface iface;
	
	memset(&iface, 0, sizeof(iface));
	strncpy(iface.ifname, ifinfo->ifname, IFNAMSIZ);

	if (priq_fd < 0 &&
	    (priq_fd = open(PRIQ_DEVICE, O_RDWR)) < 0 &&
	    (priq_fd = open_module(PRIQ_DEVICE, O_RDWR)) < 0) {
		LOG(LOG_ERR, errno, "PRIQ open");
		return (QOPERR_SYSCALL);
	}

	priq_refcount++;
	memset(&iface, 0, sizeof(iface));
	strncpy(iface.ifname, ifinfo->ifname, IFNAMSIZ);
	iface.arg = ifinfo->bandwidth;

	if (ioctl(priq_fd, PRIQ_IF_ATTACH, &iface) < 0)
		return (QOPERR_SYSCALL);
	return (0);
}

static int
priq_detach(struct ifinfo *ifinfo)
{
	struct priq_interface iface;
	
	memset(&iface, 0, sizeof(iface));
	strncpy(iface.ifname, ifinfo->ifname, IFNAMSIZ);

	if (ioctl(priq_fd, PRIQ_IF_DETACH, &iface) < 0)
		return (QOPERR_SYSCALL);

	if (--priq_refcount == 0) {
		close(priq_fd);
		priq_fd = -1;
	}
	return (0);
}

static int
priq_clear(struct ifinfo *ifinfo)
{
	struct priq_interface iface;

	memset(&iface, 0, sizeof(iface));
	strncpy(iface.ifname, ifinfo->ifname, IFNAMSIZ);

	if (ioctl(priq_fd, PRIQ_CLEAR, &iface) < 0)
		return (QOPERR_SYSCALL);
	return (0);
}

static int
priq_enable(struct ifinfo *ifinfo)
{
	struct priq_interface iface;

	memset(&iface, 0, sizeof(iface));
	strncpy(iface.ifname, ifinfo->ifname, IFNAMSIZ);

	if (ioctl(priq_fd, PRIQ_ENABLE, &iface) < 0)
		return (QOPERR_SYSCALL);
	return (0);
}

static int
priq_disable(struct ifinfo *ifinfo)
{
	struct priq_interface iface;

	memset(&iface, 0, sizeof(iface));
	strncpy(iface.ifname, ifinfo->ifname, IFNAMSIZ);

	if (ioctl(priq_fd, PRIQ_DISABLE, &iface) < 0)
		return (QOPERR_SYSCALL);
	return (0);
}

static int
priq_add_class(struct classinfo *clinfo)
{
	struct priq_add_class class_add;
	struct priq_classinfo *priq_clinfo;

	priq_clinfo = clinfo->private;
	
	memset(&class_add, 0, sizeof(class_add));
	strncpy(class_add.iface.ifname, clinfo->ifinfo->ifname, IFNAMSIZ);

	class_add.pri = priq_clinfo->pri;
	class_add.qlimit = priq_clinfo->qlimit;
	class_add.flags = priq_clinfo->flags;
	if (ioctl(priq_fd, PRIQ_ADD_CLASS, &class_add) < 0) {
		clinfo->handle = PRIQ_NULLCLASS_HANDLE;
		return (QOPERR_SYSCALL);
	}
	clinfo->handle = class_add.class_handle;
	return (0);
}

static int
priq_modify_class(struct classinfo *clinfo, void *arg)
{
	struct priq_modify_class class_mod;
	struct priq_classinfo *priq_clinfo;

	priq_clinfo = clinfo->private;

	memset(&class_mod, 0, sizeof(class_mod));
	strncpy(class_mod.iface.ifname, clinfo->ifinfo->ifname, IFNAMSIZ);
	class_mod.class_handle = clinfo->handle;

	class_mod.pri = priq_clinfo->pri;
	class_mod.qlimit = priq_clinfo->qlimit;
	class_mod.flags = priq_clinfo->flags;

	if (ioctl(priq_fd, PRIQ_MOD_CLASS, &class_mod) < 0)
		return (QOPERR_SYSCALL);
	return (0);
}

static int
priq_delete_class(struct classinfo *clinfo)
{
	struct priq_delete_class class_delete;

	if (clinfo->handle == PRIQ_NULLCLASS_HANDLE)
		return (0);

	memset(&class_delete, 0, sizeof(class_delete));
	strncpy(class_delete.iface.ifname, clinfo->ifinfo->ifname,
		IFNAMSIZ);
	class_delete.class_handle = clinfo->handle;

	if (ioctl(priq_fd, PRIQ_DEL_CLASS, &class_delete) < 0)
		return (QOPERR_SYSCALL);
	return (0);
}

static int
priq_add_filter(struct fltrinfo *fltrinfo)
{
	struct priq_add_filter fltr_add;
	
	memset(&fltr_add, 0, sizeof(fltr_add));
	strncpy(fltr_add.iface.ifname, fltrinfo->clinfo->ifinfo->ifname,
		IFNAMSIZ);
	fltr_add.class_handle = fltrinfo->clinfo->handle;
	fltr_add.filter = fltrinfo->fltr;

	if (ioctl(priq_fd, PRIQ_ADD_FILTER, &fltr_add) < 0)
		return (QOPERR_SYSCALL);
	fltrinfo->handle = fltr_add.filter_handle;
	return (0);
}

static int
priq_delete_filter(struct fltrinfo *fltrinfo)
{
	struct priq_delete_filter fltr_del;

	memset(&fltr_del, 0, sizeof(fltr_del));
	strncpy(fltr_del.iface.ifname, fltrinfo->clinfo->ifinfo->ifname,
		IFNAMSIZ);
	fltr_del.filter_handle = fltrinfo->handle;

	if (ioctl(priq_fd, PRIQ_DEL_FILTER, &fltr_del) < 0)
		return (QOPERR_SYSCALL);
	return (0);
}