xref: /freebsd/sbin/ipfw/dummynet.c (revision c3c816173d25fcb908a94c34b75fca98c5860f18)
133d72c30SPedro F. Giffuni /*-
291336b40SDon Lewis  * Codel/FQ_Codel and PIE/FQ_PIE Code:
391336b40SDon Lewis  * Copyright (C) 2016 Centre for Advanced Internet Architectures,
491336b40SDon Lewis  *  Swinburne University of Technology, Melbourne, Australia.
591336b40SDon Lewis  * Portions of this code were made possible in part by a gift from
691336b40SDon Lewis  *  The Comcast Innovation Fund.
791336b40SDon Lewis  * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
891336b40SDon Lewis  *
9cc4d3c30SLuigi Rizzo  * Copyright (c) 2002-2003,2010 Luigi Rizzo
104e9c8ae7SLuigi Rizzo  *
114e9c8ae7SLuigi Rizzo  * Redistribution and use in source forms, with and without modification,
124e9c8ae7SLuigi Rizzo  * are permitted provided that this entire comment appears intact.
134e9c8ae7SLuigi Rizzo  *
144e9c8ae7SLuigi Rizzo  * Redistribution in binary form may occur without any restrictions.
154e9c8ae7SLuigi Rizzo  * Obviously, it would be nice if you gave credit where credit is due
164e9c8ae7SLuigi Rizzo  * but requiring it would be too onerous.
174e9c8ae7SLuigi Rizzo  *
184e9c8ae7SLuigi Rizzo  * This software is provided ``AS IS'' without any warranties of any kind.
194e9c8ae7SLuigi Rizzo  *
204e9c8ae7SLuigi Rizzo  * dummynet support
214e9c8ae7SLuigi Rizzo  */
224e9c8ae7SLuigi Rizzo 
2391336b40SDon Lewis #define NEW_AQM
2420ffd88eSLuiz Otavio O Souza #include <sys/limits.h>
25*c3c81617SElyes Haouas #include <sys/param.h>
264e9c8ae7SLuigi Rizzo #include <sys/socket.h>
2723c608c8SLuigi Rizzo /* XXX there are several sysctl leftover here */
284e9c8ae7SLuigi Rizzo #include <sys/sysctl.h>
294e9c8ae7SLuigi Rizzo 
304e9c8ae7SLuigi Rizzo #include "ipfw2.h"
314e9c8ae7SLuigi Rizzo 
3291336b40SDon Lewis #ifdef NEW_AQM
3391336b40SDon Lewis #include <stdint.h>
3491336b40SDon Lewis #endif
3591336b40SDon Lewis 
364e9c8ae7SLuigi Rizzo #include <ctype.h>
374e9c8ae7SLuigi Rizzo #include <err.h>
386882bf4dSOleg Bulyzhin #include <errno.h>
396882bf4dSOleg Bulyzhin #include <libutil.h>
404e9c8ae7SLuigi Rizzo #include <netdb.h>
414e9c8ae7SLuigi Rizzo #include <stdio.h>
424e9c8ae7SLuigi Rizzo #include <stdlib.h>
434e9c8ae7SLuigi Rizzo #include <string.h>
444e9c8ae7SLuigi Rizzo #include <sysexits.h>
454e9c8ae7SLuigi Rizzo 
464e9c8ae7SLuigi Rizzo #include <net/if.h>
474e9c8ae7SLuigi Rizzo #include <netinet/in.h>
484e9c8ae7SLuigi Rizzo #include <netinet/ip_fw.h>
494e9c8ae7SLuigi Rizzo #include <netinet/ip_dummynet.h>
5023c608c8SLuigi Rizzo #include <arpa/inet.h>	/* inet_ntoa */
514e9c8ae7SLuigi Rizzo 
52cc4d3c30SLuigi Rizzo 
534e9c8ae7SLuigi Rizzo static struct _s_x dummynet_params[] = {
544e9c8ae7SLuigi Rizzo 	{ "plr",		TOK_PLR },
554e9c8ae7SLuigi Rizzo 	{ "noerror",		TOK_NOERROR },
564e9c8ae7SLuigi Rizzo 	{ "buckets",		TOK_BUCKETS },
574e9c8ae7SLuigi Rizzo 	{ "dst-ip",		TOK_DSTIP },
584e9c8ae7SLuigi Rizzo 	{ "src-ip",		TOK_SRCIP },
594e9c8ae7SLuigi Rizzo 	{ "dst-port",		TOK_DSTPORT },
604e9c8ae7SLuigi Rizzo 	{ "src-port",		TOK_SRCPORT },
614e9c8ae7SLuigi Rizzo 	{ "proto",		TOK_PROTO },
624e9c8ae7SLuigi Rizzo 	{ "weight",		TOK_WEIGHT },
63cc4d3c30SLuigi Rizzo 	{ "lmax",		TOK_LMAX },
64cc4d3c30SLuigi Rizzo 	{ "maxlen",		TOK_LMAX },
654e9c8ae7SLuigi Rizzo 	{ "all",		TOK_ALL },
66cc4d3c30SLuigi Rizzo 	{ "mask",		TOK_MASK }, /* alias for both */
67cc4d3c30SLuigi Rizzo 	{ "sched_mask",		TOK_SCHED_MASK },
68cc4d3c30SLuigi Rizzo 	{ "flow_mask",		TOK_FLOW_MASK },
694e9c8ae7SLuigi Rizzo 	{ "droptail",		TOK_DROPTAIL },
70fc5e1956SHiren Panchasara 	{ "ecn",		TOK_ECN },
714e9c8ae7SLuigi Rizzo 	{ "red",		TOK_RED },
724e9c8ae7SLuigi Rizzo 	{ "gred",		TOK_GRED },
7391336b40SDon Lewis #ifdef NEW_AQM
7491336b40SDon Lewis 	{ "codel",		TOK_CODEL}, /* Codel AQM */
7591336b40SDon Lewis 	{ "fq_codel",	TOK_FQ_CODEL}, /* FQ-Codel  */
7691336b40SDon Lewis 	{ "pie",		TOK_PIE}, /* PIE AQM */
7791336b40SDon Lewis 	{ "fq_pie",		TOK_FQ_PIE}, /* FQ-PIE */
7891336b40SDon Lewis #endif
794e9c8ae7SLuigi Rizzo 	{ "bw",			TOK_BW },
804e9c8ae7SLuigi Rizzo 	{ "bandwidth",		TOK_BW },
814e9c8ae7SLuigi Rizzo 	{ "delay",		TOK_DELAY },
82cc4d3c30SLuigi Rizzo 	{ "link",		TOK_LINK },
834e9c8ae7SLuigi Rizzo 	{ "pipe",		TOK_PIPE },
844e9c8ae7SLuigi Rizzo 	{ "queue",		TOK_QUEUE },
85cc4d3c30SLuigi Rizzo 	{ "flowset",		TOK_FLOWSET },
86cc4d3c30SLuigi Rizzo 	{ "sched",		TOK_SCHED },
87cc4d3c30SLuigi Rizzo 	{ "pri",		TOK_PRI },
88cc4d3c30SLuigi Rizzo 	{ "priority",		TOK_PRI },
89cc4d3c30SLuigi Rizzo 	{ "type",		TOK_TYPE },
904e9c8ae7SLuigi Rizzo 	{ "flow-id",		TOK_FLOWID},
914e9c8ae7SLuigi Rizzo 	{ "dst-ipv6",		TOK_DSTIP6},
924e9c8ae7SLuigi Rizzo 	{ "dst-ip6",		TOK_DSTIP6},
934e9c8ae7SLuigi Rizzo 	{ "src-ipv6",		TOK_SRCIP6},
944e9c8ae7SLuigi Rizzo 	{ "src-ip6",		TOK_SRCIP6},
95cc4d3c30SLuigi Rizzo 	{ "profile",		TOK_PROFILE},
966882bf4dSOleg Bulyzhin 	{ "burst",		TOK_BURST},
974e9c8ae7SLuigi Rizzo 	{ "dummynet-params",	TOK_NULL },
984e9c8ae7SLuigi Rizzo 	{ NULL, 0 }	/* terminator */
994e9c8ae7SLuigi Rizzo };
1004e9c8ae7SLuigi Rizzo 
10191336b40SDon Lewis #ifdef NEW_AQM
10291336b40SDon Lewis /* AQM/extra sched parameters  tokens*/
10391336b40SDon Lewis static struct _s_x aqm_params[] = {
10491336b40SDon Lewis 	{ "target",		TOK_TARGET},
10591336b40SDon Lewis 	{ "interval",		TOK_INTERVAL},
10691336b40SDon Lewis 	{ "limit",		TOK_LIMIT},
10791336b40SDon Lewis 	{ "flows",		TOK_FLOWS},
10891336b40SDon Lewis 	{ "quantum",		TOK_QUANTUM},
10991336b40SDon Lewis 	{ "ecn",		TOK_ECN},
11091336b40SDon Lewis 	{ "noecn",		TOK_NO_ECN},
11191336b40SDon Lewis 	{ "tupdate",		TOK_TUPDATE},
11291336b40SDon Lewis 	{ "max_burst",		TOK_MAX_BURST},
11391336b40SDon Lewis 	{ "max_ecnth",	TOK_MAX_ECNTH},
11491336b40SDon Lewis 	{ "alpha",		TOK_ALPHA},
11591336b40SDon Lewis 	{ "beta",		TOK_BETA},
11691336b40SDon Lewis 	{ "capdrop",	TOK_CAPDROP},
11791336b40SDon Lewis 	{ "nocapdrop",	TOK_NO_CAPDROP},
11891336b40SDon Lewis 	{ "onoff",	TOK_ONOFF},
11991336b40SDon Lewis 	{ "dre",	TOK_DRE},
12091336b40SDon Lewis 	{ "ts",	TOK_TS},
12191336b40SDon Lewis 	{ "derand",	TOK_DERAND},
12291336b40SDon Lewis 	{ "noderand",	TOK_NO_DERAND},
12391336b40SDon Lewis 	{ NULL, 0 }	/* terminator */
12491336b40SDon Lewis };
12591336b40SDon Lewis #endif
12691336b40SDon Lewis 
127cc4d3c30SLuigi Rizzo #define O_NEXT(p, len) ((void *)((char *)p + len))
128cc4d3c30SLuigi Rizzo 
129cc4d3c30SLuigi Rizzo static void
130cc4d3c30SLuigi Rizzo oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
131cc4d3c30SLuigi Rizzo {
132cc4d3c30SLuigi Rizzo 	oid->len = len;
133cc4d3c30SLuigi Rizzo 	oid->type = type;
134cc4d3c30SLuigi Rizzo 	oid->subtype = 0;
135cc4d3c30SLuigi Rizzo 	oid->id = id;
136cc4d3c30SLuigi Rizzo }
137cc4d3c30SLuigi Rizzo 
138cc4d3c30SLuigi Rizzo /* make room in the buffer and move the pointer forward */
139cc4d3c30SLuigi Rizzo static void *
140cc4d3c30SLuigi Rizzo o_next(struct dn_id **o, int len, int type)
141cc4d3c30SLuigi Rizzo {
142cc4d3c30SLuigi Rizzo 	struct dn_id *ret = *o;
143cc4d3c30SLuigi Rizzo 	oid_fill(ret, len, type, 0);
144cc4d3c30SLuigi Rizzo 	*o = O_NEXT(*o, len);
145cc4d3c30SLuigi Rizzo 	return ret;
146cc4d3c30SLuigi Rizzo }
147cc4d3c30SLuigi Rizzo 
14891336b40SDon Lewis #ifdef NEW_AQM
14991336b40SDon Lewis 
15091336b40SDon Lewis /* Codel flags */
15191336b40SDon Lewis enum {
15291336b40SDon Lewis 	CODEL_ECN_ENABLED = 1
15391336b40SDon Lewis };
15491336b40SDon Lewis 
15591336b40SDon Lewis /* PIE flags, from PIE kernel module */
15691336b40SDon Lewis enum {
15791336b40SDon Lewis 	PIE_ECN_ENABLED = 1,
15891336b40SDon Lewis 	PIE_CAPDROP_ENABLED = 2,
15991336b40SDon Lewis 	PIE_ON_OFF_MODE_ENABLED = 4,
16091336b40SDon Lewis 	PIE_DEPRATEEST_ENABLED = 8,
16191336b40SDon Lewis 	PIE_DERAND_ENABLED = 16
16291336b40SDon Lewis };
16391336b40SDon Lewis 
16491336b40SDon Lewis #define PIE_FIX_POINT_BITS 13
16591336b40SDon Lewis #define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)
16691336b40SDon Lewis 
16791336b40SDon Lewis /* integer to time */
16856707beeSMark Johnston static void
16991336b40SDon Lewis us_to_time(int t, char *strt)
17091336b40SDon Lewis {
17191336b40SDon Lewis 	if (t < 0)
17291336b40SDon Lewis 		strt[0]='\0';
17391336b40SDon Lewis 	else if ( t==0 )
17491336b40SDon Lewis 		sprintf(strt,"%d", t);
17591336b40SDon Lewis 	else if (t< 1000)
17691336b40SDon Lewis 		sprintf(strt,"%dus", t);
17791336b40SDon Lewis 	else if (t < 1000000)
17891336b40SDon Lewis 		sprintf(strt,"%gms", (float) t / 1000);
17991336b40SDon Lewis 	else
18091336b40SDon Lewis 		sprintf(strt,"%gfs", (float) t / 1000000);
18191336b40SDon Lewis }
18291336b40SDon Lewis 
18391336b40SDon Lewis /*
18491336b40SDon Lewis  * returns -1 if s is not a valid time, otherwise, return time in us
18591336b40SDon Lewis  */
18691336b40SDon Lewis static long
18791336b40SDon Lewis time_to_us(const char *s)
18891336b40SDon Lewis {
18991336b40SDon Lewis 	int i, dots = 0;
19091336b40SDon Lewis 	int len = strlen(s);
19191336b40SDon Lewis 	char strt[16]="", stru[16]="";
19291336b40SDon Lewis 
19391336b40SDon Lewis 	if (len>15)
19491336b40SDon Lewis 		return -1;
19591336b40SDon Lewis 	for (i = 0; i<len && (isdigit(s[i]) || s[i]=='.') ; i++)
19691336b40SDon Lewis 		if (s[i]=='.') {
19791336b40SDon Lewis 			if (dots)
19891336b40SDon Lewis 				return -1;
19991336b40SDon Lewis 			else
20091336b40SDon Lewis 				dots++;
20191336b40SDon Lewis 		}
20291336b40SDon Lewis 
20391336b40SDon Lewis 	if (!i)
20491336b40SDon Lewis 		return -1;
20591336b40SDon Lewis 	strncpy(strt, s, i);
20691336b40SDon Lewis 	if (i<len)
20791336b40SDon Lewis 		strcpy(stru, s+i);
20891336b40SDon Lewis 	else
20991336b40SDon Lewis 		strcpy(stru, "ms");
21091336b40SDon Lewis 
21191336b40SDon Lewis 	if (!strcasecmp(stru, "us"))
21291336b40SDon Lewis 		return atol(strt);
21391336b40SDon Lewis 	if (!strcasecmp(stru, "ms"))
21491336b40SDon Lewis 		return (strtod(strt, NULL) * 1000);
21591336b40SDon Lewis 	if (!strcasecmp(stru, "s"))
21691336b40SDon Lewis 		return (strtod(strt, NULL)*1000000);
21791336b40SDon Lewis 
21891336b40SDon Lewis 	return -1;
21991336b40SDon Lewis }
22091336b40SDon Lewis 
22191336b40SDon Lewis 
22291336b40SDon Lewis /* Get AQM or scheduler extra parameters  */
22356707beeSMark Johnston static void
22491336b40SDon Lewis get_extra_parms(uint32_t nr, char *out, int subtype)
22591336b40SDon Lewis {
22691336b40SDon Lewis 	struct dn_extra_parms *ep;
22791336b40SDon Lewis 	int ret;
22891336b40SDon Lewis 	char strt1[15], strt2[15], strt3[15];
22991336b40SDon Lewis 	u_int l;
23091336b40SDon Lewis 
23191336b40SDon Lewis 	/* prepare the request */
23291336b40SDon Lewis 	l = sizeof(struct dn_extra_parms);
23391336b40SDon Lewis 	ep = safe_calloc(1, l);
23491336b40SDon Lewis 	memset(ep, 0, sizeof(*ep));
23591336b40SDon Lewis 	*out = '\0';
23691336b40SDon Lewis 
23791336b40SDon Lewis 	oid_fill(&ep->oid, l, DN_CMD_GET, DN_API_VERSION);
23891336b40SDon Lewis 	ep->oid.len = l;
23991336b40SDon Lewis 	ep->oid.subtype = subtype;
24091336b40SDon Lewis 	ep->nr = nr;
24191336b40SDon Lewis 
24291336b40SDon Lewis 	ret = do_cmd(-IP_DUMMYNET3, ep, (uintptr_t)&l);
24391336b40SDon Lewis 	if (ret) {
24491336b40SDon Lewis 		free(ep);
24591336b40SDon Lewis 		errx(EX_DATAERR, "Error getting extra parameters\n");
24691336b40SDon Lewis 	}
24791336b40SDon Lewis 
24891336b40SDon Lewis 	switch (subtype) {
24991336b40SDon Lewis 	case DN_AQM_PARAMS:
25091336b40SDon Lewis 		if( !strcasecmp(ep->name, "codel")) {
25191336b40SDon Lewis 			us_to_time(ep->par[0], strt1);
25291336b40SDon Lewis 			us_to_time(ep->par[1], strt2);
25391336b40SDon Lewis 			l = sprintf(out, " AQM CoDel target %s interval %s",
25491336b40SDon Lewis 				strt1, strt2);
25591336b40SDon Lewis 			if (ep->par[2] & CODEL_ECN_ENABLED)
25691336b40SDon Lewis 				l = sprintf(out + l, " ECN");
25791336b40SDon Lewis 			else
25891336b40SDon Lewis 				l += sprintf(out + l, " NoECN");
25991336b40SDon Lewis 		} else if( !strcasecmp(ep->name, "pie")) {
26091336b40SDon Lewis 			us_to_time(ep->par[0], strt1);
26191336b40SDon Lewis 			us_to_time(ep->par[1], strt2);
26291336b40SDon Lewis 			us_to_time(ep->par[2], strt3);
26391336b40SDon Lewis 			l = sprintf(out, " AQM type PIE target %s tupdate %s alpha "
26491336b40SDon Lewis 					"%g beta %g max_burst %s max_ecnth %.3g",
26591336b40SDon Lewis 					strt1,
26691336b40SDon Lewis 					strt2,
26791336b40SDon Lewis 					ep->par[4] / (float) PIE_SCALE,
26891336b40SDon Lewis 					ep->par[5] / (float) PIE_SCALE,
26991336b40SDon Lewis 					strt3,
27091336b40SDon Lewis 					ep->par[3] / (float) PIE_SCALE
27191336b40SDon Lewis 				);
27291336b40SDon Lewis 
27391336b40SDon Lewis 			if (ep->par[6] & PIE_ECN_ENABLED)
27491336b40SDon Lewis 				l += sprintf(out + l, " ECN");
27591336b40SDon Lewis 			else
27691336b40SDon Lewis 				l += sprintf(out + l, " NoECN");
27791336b40SDon Lewis 			if (ep->par[6] & PIE_CAPDROP_ENABLED)
27891336b40SDon Lewis 				l += sprintf(out + l, " CapDrop");
27991336b40SDon Lewis 			else
28091336b40SDon Lewis 				l += sprintf(out + l, " NoCapDrop");
28191336b40SDon Lewis 			if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
28291336b40SDon Lewis 				l += sprintf(out + l, " OnOff");
28391336b40SDon Lewis 			if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
28491336b40SDon Lewis 				l += sprintf(out + l, " DRE");
28591336b40SDon Lewis 			else
28691336b40SDon Lewis 				l += sprintf(out + l, " TS");
28791336b40SDon Lewis 			if (ep->par[6] & PIE_DERAND_ENABLED)
28891336b40SDon Lewis 				l += sprintf(out + l, " Derand");
28991336b40SDon Lewis 			else
29091336b40SDon Lewis 				l += sprintf(out + l, " NoDerand");
29191336b40SDon Lewis 		}
29291336b40SDon Lewis 		break;
29391336b40SDon Lewis 
29491336b40SDon Lewis 	case	DN_SCH_PARAMS:
29591336b40SDon Lewis 		if (!strcasecmp(ep->name,"FQ_CODEL")) {
29691336b40SDon Lewis 			us_to_time(ep->par[0], strt1);
29791336b40SDon Lewis 			us_to_time(ep->par[1], strt2);
29891336b40SDon Lewis 			l = sprintf(out," FQ_CODEL target %s interval %s"
29991336b40SDon Lewis 				" quantum %jd limit %jd flows %jd",
30091336b40SDon Lewis 				strt1, strt2,
30191336b40SDon Lewis 				(intmax_t) ep->par[3],
30291336b40SDon Lewis 				(intmax_t) ep->par[4],
30391336b40SDon Lewis 				(intmax_t) ep->par[5]
30491336b40SDon Lewis 				);
30591336b40SDon Lewis 			if (ep->par[2] & CODEL_ECN_ENABLED)
30691336b40SDon Lewis 				l += sprintf(out + l, " ECN");
30791336b40SDon Lewis 			else
30891336b40SDon Lewis 				l += sprintf(out + l, " NoECN");
30991336b40SDon Lewis 			l += sprintf(out + l, "\n");
31091336b40SDon Lewis 		} else 	if (!strcasecmp(ep->name,"FQ_PIE")) {
31191336b40SDon Lewis 			us_to_time(ep->par[0], strt1);
31291336b40SDon Lewis 			us_to_time(ep->par[1], strt2);
31391336b40SDon Lewis 			us_to_time(ep->par[2], strt3);
31491336b40SDon Lewis 			l = sprintf(out, "  FQ_PIE target %s tupdate %s alpha "
31591336b40SDon Lewis 				"%g beta %g max_burst %s max_ecnth %.3g"
31691336b40SDon Lewis 				" quantum %jd limit %jd flows %jd",
31791336b40SDon Lewis 				strt1,
31891336b40SDon Lewis 				strt2,
31991336b40SDon Lewis 				ep->par[4] / (float) PIE_SCALE,
32091336b40SDon Lewis 				ep->par[5] / (float) PIE_SCALE,
32191336b40SDon Lewis 				strt3,
32291336b40SDon Lewis 				ep->par[3] / (float) PIE_SCALE,
32391336b40SDon Lewis 				(intmax_t) ep->par[7],
32491336b40SDon Lewis 				(intmax_t) ep->par[8],
32591336b40SDon Lewis 				(intmax_t) ep->par[9]
32691336b40SDon Lewis 			);
32791336b40SDon Lewis 
32891336b40SDon Lewis 			if (ep->par[6] & PIE_ECN_ENABLED)
32991336b40SDon Lewis 				l += sprintf(out + l, " ECN");
33091336b40SDon Lewis 			else
33191336b40SDon Lewis 				l += sprintf(out + l, " NoECN");
33291336b40SDon Lewis 			if (ep->par[6] & PIE_CAPDROP_ENABLED)
33391336b40SDon Lewis 				l += sprintf(out + l, " CapDrop");
33491336b40SDon Lewis 			else
33591336b40SDon Lewis 				l += sprintf(out + l, " NoCapDrop");
33691336b40SDon Lewis 			if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
33791336b40SDon Lewis 				l += sprintf(out + l, " OnOff");
33891336b40SDon Lewis 			if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
33991336b40SDon Lewis 				l += sprintf(out + l, " DRE");
34091336b40SDon Lewis 			else
34191336b40SDon Lewis 				l += sprintf(out + l, " TS");
34291336b40SDon Lewis 			if (ep->par[6] & PIE_DERAND_ENABLED)
34391336b40SDon Lewis 				l += sprintf(out + l, " Derand");
34491336b40SDon Lewis 			else
34591336b40SDon Lewis 				l += sprintf(out + l, " NoDerand");
34691336b40SDon Lewis 			l += sprintf(out + l, "\n");
34791336b40SDon Lewis 		}
34891336b40SDon Lewis 		break;
34991336b40SDon Lewis 	}
35091336b40SDon Lewis 
35191336b40SDon Lewis 	free(ep);
35291336b40SDon Lewis }
35391336b40SDon Lewis #endif
35491336b40SDon Lewis 
35591336b40SDon Lewis 
356cc4d3c30SLuigi Rizzo #if 0
3574e9c8ae7SLuigi Rizzo static int
35801ab7632SLuigi Rizzo sort_q(void *arg, const void *pa, const void *pb)
3594e9c8ae7SLuigi Rizzo {
3604e9c8ae7SLuigi Rizzo 	int rev = (co.do_sort < 0);
3614e9c8ae7SLuigi Rizzo 	int field = rev ? -co.do_sort : co.do_sort;
3624e9c8ae7SLuigi Rizzo 	long long res = 0;
3634e9c8ae7SLuigi Rizzo 	const struct dn_flow_queue *a = pa;
3644e9c8ae7SLuigi Rizzo 	const struct dn_flow_queue *b = pb;
3654e9c8ae7SLuigi Rizzo 
3664e9c8ae7SLuigi Rizzo 	switch (field) {
3674e9c8ae7SLuigi Rizzo 	case 1: /* pkts */
3684e9c8ae7SLuigi Rizzo 		res = a->len - b->len;
3694e9c8ae7SLuigi Rizzo 		break;
3704e9c8ae7SLuigi Rizzo 	case 2: /* bytes */
3714e9c8ae7SLuigi Rizzo 		res = a->len_bytes - b->len_bytes;
3724e9c8ae7SLuigi Rizzo 		break;
3734e9c8ae7SLuigi Rizzo 
3744e9c8ae7SLuigi Rizzo 	case 3: /* tot pkts */
3754e9c8ae7SLuigi Rizzo 		res = a->tot_pkts - b->tot_pkts;
3764e9c8ae7SLuigi Rizzo 		break;
3774e9c8ae7SLuigi Rizzo 
3784e9c8ae7SLuigi Rizzo 	case 4: /* tot bytes */
3794e9c8ae7SLuigi Rizzo 		res = a->tot_bytes - b->tot_bytes;
3804e9c8ae7SLuigi Rizzo 		break;
3814e9c8ae7SLuigi Rizzo 	}
3824e9c8ae7SLuigi Rizzo 	if (res < 0)
3834e9c8ae7SLuigi Rizzo 		res = -1;
3844e9c8ae7SLuigi Rizzo 	if (res > 0)
3854e9c8ae7SLuigi Rizzo 		res = 1;
3864e9c8ae7SLuigi Rizzo 	return (int)(rev ? res : -res);
3874e9c8ae7SLuigi Rizzo }
388cc4d3c30SLuigi Rizzo #endif
3894e9c8ae7SLuigi Rizzo 
390cc4d3c30SLuigi Rizzo /* print a mask and header for the subsequent list of flows */
3914e9c8ae7SLuigi Rizzo static void
392cc4d3c30SLuigi Rizzo print_mask(struct ipfw_flow_id *id)
3934e9c8ae7SLuigi Rizzo {
394cc4d3c30SLuigi Rizzo 	if (!IS_IP6_FLOW_ID(id)) {
3954e9c8ae7SLuigi Rizzo 		printf("    "
396f9f7bde3SLuigi Rizzo 		    "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
397f9f7bde3SLuigi Rizzo 		    id->extra ? "queue," : "",
398cc4d3c30SLuigi Rizzo 		    id->proto,
399cc4d3c30SLuigi Rizzo 		    id->src_ip, id->src_port,
400cc4d3c30SLuigi Rizzo 		    id->dst_ip, id->dst_port);
401cc4d3c30SLuigi Rizzo 	} else {
402cc4d3c30SLuigi Rizzo 		char buf[255];
403f9f7bde3SLuigi Rizzo 		printf("\n        mask: %sproto: 0x%02x, flow_id: 0x%08x,  ",
404f9f7bde3SLuigi Rizzo 		    id->extra ? "queue," : "",
405cc4d3c30SLuigi Rizzo 		    id->proto, id->flow_id6);
406cc4d3c30SLuigi Rizzo 		inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
407cc4d3c30SLuigi Rizzo 		printf("%s/0x%04x -> ", buf, id->src_port);
408cc4d3c30SLuigi Rizzo 		inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
409cc4d3c30SLuigi Rizzo 		printf("%s/0x%04x\n", buf, id->dst_port);
410f40be7f6SLuigi Rizzo 	}
411f40be7f6SLuigi Rizzo }
4124e9c8ae7SLuigi Rizzo 
413f40be7f6SLuigi Rizzo static void
414f40be7f6SLuigi Rizzo print_header(struct ipfw_flow_id *id)
415f40be7f6SLuigi Rizzo {
416f40be7f6SLuigi Rizzo 	if (!IS_IP6_FLOW_ID(id))
417f40be7f6SLuigi Rizzo 		printf("BKT Prot ___Source IP/port____ "
418f40be7f6SLuigi Rizzo 		    "____Dest. IP/port____ "
419f40be7f6SLuigi Rizzo 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
420f40be7f6SLuigi Rizzo 	else
4214e9c8ae7SLuigi Rizzo 		printf("BKT ___Prot___ _flow-id_ "
4224e9c8ae7SLuigi Rizzo 		    "______________Source IPv6/port_______________ "
4234e9c8ae7SLuigi Rizzo 		    "_______________Dest. IPv6/port_______________ "
4244e9c8ae7SLuigi Rizzo 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
4254e9c8ae7SLuigi Rizzo }
4264e9c8ae7SLuigi Rizzo 
4274e9c8ae7SLuigi Rizzo static void
428912430f6SAlexander V. Chernikov list_flow(struct buf_pr *bp, struct dn_flow *ni)
429cc4d3c30SLuigi Rizzo {
430cc4d3c30SLuigi Rizzo 	char buff[255];
431f40be7f6SLuigi Rizzo 	struct protoent *pe = NULL;
432cc4d3c30SLuigi Rizzo 	struct in_addr ina;
433cc4d3c30SLuigi Rizzo 	struct ipfw_flow_id *id = &ni->fid;
434cc4d3c30SLuigi Rizzo 
435cc4d3c30SLuigi Rizzo 	pe = getprotobynumber(id->proto);
436cc4d3c30SLuigi Rizzo 		/* XXX: Should check for IPv4 flows */
437912430f6SAlexander V. Chernikov 	bprintf(bp, "%3u%c", (ni->oid.id) & 0xff,
438f9f7bde3SLuigi Rizzo 		id->extra ? '*' : ' ');
439cc4d3c30SLuigi Rizzo 	if (!IS_IP6_FLOW_ID(id)) {
440cc4d3c30SLuigi Rizzo 		if (pe)
441912430f6SAlexander V. Chernikov 			bprintf(bp, "%-4s ", pe->p_name);
442cc4d3c30SLuigi Rizzo 		else
443912430f6SAlexander V. Chernikov 			bprintf(bp, "%4u ", id->proto);
444cc4d3c30SLuigi Rizzo 		ina.s_addr = htonl(id->src_ip);
445912430f6SAlexander V. Chernikov 		bprintf(bp, "%15s/%-5d ",
446cc4d3c30SLuigi Rizzo 		    inet_ntoa(ina), id->src_port);
447cc4d3c30SLuigi Rizzo 		ina.s_addr = htonl(id->dst_ip);
448912430f6SAlexander V. Chernikov 		bprintf(bp, "%15s/%-5d ",
449cc4d3c30SLuigi Rizzo 		    inet_ntoa(ina), id->dst_port);
450cc4d3c30SLuigi Rizzo 	} else {
451cc4d3c30SLuigi Rizzo 		/* Print IPv6 flows */
452cc4d3c30SLuigi Rizzo 		if (pe != NULL)
453912430f6SAlexander V. Chernikov 			bprintf(bp, "%9s ", pe->p_name);
454cc4d3c30SLuigi Rizzo 		else
455912430f6SAlexander V. Chernikov 			bprintf(bp, "%9u ", id->proto);
456912430f6SAlexander V. Chernikov 		bprintf(bp, "%7d  %39s/%-5d ", id->flow_id6,
457cc4d3c30SLuigi Rizzo 		    inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
458cc4d3c30SLuigi Rizzo 		    id->src_port);
459912430f6SAlexander V. Chernikov 		bprintf(bp, " %39s/%-5d ",
460cc4d3c30SLuigi Rizzo 		    inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
461cc4d3c30SLuigi Rizzo 		    id->dst_port);
462cc4d3c30SLuigi Rizzo 	}
463912430f6SAlexander V. Chernikov 	pr_u64(bp, &ni->tot_pkts, 4);
464912430f6SAlexander V. Chernikov 	pr_u64(bp, &ni->tot_bytes, 8);
465912430f6SAlexander V. Chernikov 	bprintf(bp, "%2u %4u %3u",
466cc4d3c30SLuigi Rizzo 	    ni->length, ni->len_bytes, ni->drops);
467cc4d3c30SLuigi Rizzo }
468cc4d3c30SLuigi Rizzo 
469cc4d3c30SLuigi Rizzo static void
470cc4d3c30SLuigi Rizzo print_flowset_parms(struct dn_fs *fs, char *prefix)
4714e9c8ae7SLuigi Rizzo {
4724e9c8ae7SLuigi Rizzo 	int l;
4734e9c8ae7SLuigi Rizzo 	char qs[30];
47431cf66d7SRichard Scheffenegger 	char plr[40];
47591336b40SDon Lewis 	char red[200];	/* Display RED parameters */
4764e9c8ae7SLuigi Rizzo 
4774e9c8ae7SLuigi Rizzo 	l = fs->qsize;
478cc4d3c30SLuigi Rizzo 	if (fs->flags & DN_QSIZE_BYTES) {
4794e9c8ae7SLuigi Rizzo 		if (l >= 8192)
4804e9c8ae7SLuigi Rizzo 			sprintf(qs, "%d KB", l / 1024);
4814e9c8ae7SLuigi Rizzo 		else
4824e9c8ae7SLuigi Rizzo 			sprintf(qs, "%d B", l);
4834e9c8ae7SLuigi Rizzo 	} else
4844e9c8ae7SLuigi Rizzo 		sprintf(qs, "%3d sl.", l);
48531cf66d7SRichard Scheffenegger 	if (fs->plr[0] || fs->plr[1]) {
48631cf66d7SRichard Scheffenegger 		if (fs->plr[1] == 0)
48731cf66d7SRichard Scheffenegger 			sprintf(plr, "plr %f",
48831cf66d7SRichard Scheffenegger 				1.0 * fs->plr[0] / (double)(0x7fffffff));
4894e9c8ae7SLuigi Rizzo 		else
49031cf66d7SRichard Scheffenegger 			sprintf(plr, "plr %f,%f,%f,%f",
49131cf66d7SRichard Scheffenegger 				1.0 * fs->plr[0] / (double)(0x7fffffff),
49231cf66d7SRichard Scheffenegger 				1.0 * fs->plr[1] / (double)(0x7fffffff),
49331cf66d7SRichard Scheffenegger 				1.0 * fs->plr[2] / (double)(0x7fffffff),
49431cf66d7SRichard Scheffenegger 				1.0 * fs->plr[3] / (double)(0x7fffffff));
49531cf66d7SRichard Scheffenegger 	} else
4964e9c8ae7SLuigi Rizzo 		plr[0] = '\0';
497cc4d3c30SLuigi Rizzo 
498fc5e1956SHiren Panchasara 	if (fs->flags & DN_IS_RED) {	/* RED parameters */
4994e9c8ae7SLuigi Rizzo 		sprintf(red,
5004e9c8ae7SLuigi Rizzo 		    "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
501cc4d3c30SLuigi Rizzo 		    (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
5024e9c8ae7SLuigi Rizzo 		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
503cc4d3c30SLuigi Rizzo 		    fs->min_th,
504cc4d3c30SLuigi Rizzo 		    fs->max_th,
5054e9c8ae7SLuigi Rizzo 		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
506fc5e1956SHiren Panchasara 		if (fs->flags & DN_IS_ECN)
507f00c55e2SXin LI 			strlcat(red, " (ecn)", sizeof(red));
50891336b40SDon Lewis #ifdef NEW_AQM
50991336b40SDon Lewis 	/* get AQM parameters */
51091336b40SDon Lewis 	} else if (fs->flags & DN_IS_AQM) {
51191336b40SDon Lewis 			get_extra_parms(fs->fs_nr, red, DN_AQM_PARAMS);
51291336b40SDon Lewis #endif
513fc5e1956SHiren Panchasara 	} else
5144e9c8ae7SLuigi Rizzo 		sprintf(red, "droptail");
5154e9c8ae7SLuigi Rizzo 
516cc4d3c30SLuigi Rizzo 	if (prefix[0]) {
5174e9c8ae7SLuigi Rizzo 	    printf("%s %s%s %d queues (%d buckets) %s\n",
518cc4d3c30SLuigi Rizzo 		prefix, qs, plr, fs->oid.id, fs->buckets, red);
519cc4d3c30SLuigi Rizzo 	    prefix[0] = '\0';
520cc4d3c30SLuigi Rizzo 	} else {
521cc4d3c30SLuigi Rizzo 	    printf("q%05d %s%s %d flows (%d buckets) sched %d "
522cc4d3c30SLuigi Rizzo 			"weight %d lmax %d pri %d %s\n",
523cc4d3c30SLuigi Rizzo 		fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
524cc4d3c30SLuigi Rizzo 		fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
525cc4d3c30SLuigi Rizzo 	    if (fs->flags & DN_HAVE_MASK)
526cc4d3c30SLuigi Rizzo 		print_mask(&fs->flow_mask);
527cc4d3c30SLuigi Rizzo 	}
5284e9c8ae7SLuigi Rizzo }
5294e9c8ae7SLuigi Rizzo 
5304bb7ae9dSLuigi Rizzo static void
531cc4d3c30SLuigi Rizzo print_extra_delay_parms(struct dn_profile *p)
5324bb7ae9dSLuigi Rizzo {
5334bb7ae9dSLuigi Rizzo 	double loss;
5344bb7ae9dSLuigi Rizzo 	if (p->samples_no <= 0)
5354bb7ae9dSLuigi Rizzo 		return;
5364bb7ae9dSLuigi Rizzo 
5374bb7ae9dSLuigi Rizzo 	loss = p->loss_level;
5384bb7ae9dSLuigi Rizzo 	loss /= p->samples_no;
5396882bf4dSOleg Bulyzhin 	printf("\t profile: name \"%s\" loss %f samples %d\n",
5406882bf4dSOleg Bulyzhin 		p->name, loss, p->samples_no);
5414bb7ae9dSLuigi Rizzo }
5424bb7ae9dSLuigi Rizzo 
543cc4d3c30SLuigi Rizzo static void
544cc4d3c30SLuigi Rizzo flush_buf(char *buf)
5454e9c8ae7SLuigi Rizzo {
546cc4d3c30SLuigi Rizzo 	if (buf[0])
547cc4d3c30SLuigi Rizzo 		printf("%s\n", buf);
548cc4d3c30SLuigi Rizzo 	buf[0] = '\0';
549cc4d3c30SLuigi Rizzo }
5504e9c8ae7SLuigi Rizzo 
551cc4d3c30SLuigi Rizzo /*
552cc4d3c30SLuigi Rizzo  * generic list routine. We expect objects in a specific order, i.e.
553cc4d3c30SLuigi Rizzo  * PIPES AND SCHEDULERS:
554cc4d3c30SLuigi Rizzo  *	link; scheduler; internal flowset if any; instances
555cc4d3c30SLuigi Rizzo  * we can tell a pipe from the number.
556cc4d3c30SLuigi Rizzo  *
557cc4d3c30SLuigi Rizzo  * FLOWSETS:
558cc4d3c30SLuigi Rizzo  *	flowset; queues;
559cc4d3c30SLuigi Rizzo  * link i (int queue); scheduler i; si(i) { flowsets() : queues }
560cc4d3c30SLuigi Rizzo  */
561cc4d3c30SLuigi Rizzo static void
562cc4d3c30SLuigi Rizzo list_pipes(struct dn_id *oid, struct dn_id *end)
563cc4d3c30SLuigi Rizzo {
564cc4d3c30SLuigi Rizzo     char buf[160];	/* pending buffer */
565f40be7f6SLuigi Rizzo     int toPrint = 1;	/* print header */
566912430f6SAlexander V. Chernikov     struct buf_pr bp;
567cc4d3c30SLuigi Rizzo 
56837133ba7SLuigi Rizzo     buf[0] = '\0';
569912430f6SAlexander V. Chernikov     bp_alloc(&bp, 4096);
570cc4d3c30SLuigi Rizzo     for (; oid != end; oid = O_NEXT(oid, oid->len)) {
571cc4d3c30SLuigi Rizzo 	if (oid->len < sizeof(*oid))
572cc4d3c30SLuigi Rizzo 		errx(1, "invalid oid len %d\n", oid->len);
573cc4d3c30SLuigi Rizzo 
574cc4d3c30SLuigi Rizzo 	switch (oid->type) {
575cc4d3c30SLuigi Rizzo 	default:
576cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
577cc4d3c30SLuigi Rizzo 	    printf("unrecognized object %d size %d\n", oid->type, oid->len);
578cc4d3c30SLuigi Rizzo 	    break;
579cc4d3c30SLuigi Rizzo 	case DN_TEXT: /* list of attached flowsets */
580cc4d3c30SLuigi Rizzo 	    {
581cc4d3c30SLuigi Rizzo 		int i, l;
582cc4d3c30SLuigi Rizzo 		struct {
583cc4d3c30SLuigi Rizzo 			struct dn_id id;
584cc4d3c30SLuigi Rizzo 			uint32_t p[0];
585cc4d3c30SLuigi Rizzo 		} *d = (void *)oid;
586cc4d3c30SLuigi Rizzo 		l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
587cc4d3c30SLuigi Rizzo 		if (l == 0)
588cc4d3c30SLuigi Rizzo 		    break;
589cc4d3c30SLuigi Rizzo 		printf("   Children flowsets: ");
590cc4d3c30SLuigi Rizzo 		for (i = 0; i < l; i++)
591cc4d3c30SLuigi Rizzo 			printf("%u ", d->p[i]);
592cc4d3c30SLuigi Rizzo 		printf("\n");
593cc4d3c30SLuigi Rizzo 		break;
594cc4d3c30SLuigi Rizzo 	    }
595cc4d3c30SLuigi Rizzo 	case DN_CMD_GET:
59656707beeSMark Johnston 	    if (g_co.verbose)
597cc4d3c30SLuigi Rizzo 		printf("answer for cmd %d, len %d\n", oid->type, oid->id);
598cc4d3c30SLuigi Rizzo 	    break;
599cc4d3c30SLuigi Rizzo 	case DN_SCH: {
600cc4d3c30SLuigi Rizzo 	    struct dn_sch *s = (struct dn_sch *)oid;
601cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
602cc4d3c30SLuigi Rizzo 	    printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
603cc4d3c30SLuigi Rizzo 			s->sched_nr,
604cc4d3c30SLuigi Rizzo 			s->name, s->flags, s->buckets, s->oid.id);
60591336b40SDon Lewis #ifdef NEW_AQM
60691336b40SDon Lewis 		char parms[200];
60791336b40SDon Lewis 		get_extra_parms(s->sched_nr, parms, DN_SCH_PARAMS);
60891336b40SDon Lewis 		printf("%s",parms);
60991336b40SDon Lewis #endif
610cc4d3c30SLuigi Rizzo 	    if (s->flags & DN_HAVE_MASK)
611cc4d3c30SLuigi Rizzo 		print_mask(&s->sched_mask);
612cc4d3c30SLuigi Rizzo 	    }
613cc4d3c30SLuigi Rizzo 	    break;
614cc4d3c30SLuigi Rizzo 
615cc4d3c30SLuigi Rizzo 	case DN_FLOW:
616912430f6SAlexander V. Chernikov 	    if (toPrint != 0) {
617912430f6SAlexander V. Chernikov 		    print_header(&((struct dn_flow *)oid)->fid);
618912430f6SAlexander V. Chernikov 		    toPrint = 0;
619912430f6SAlexander V. Chernikov 	    }
620912430f6SAlexander V. Chernikov 	    list_flow(&bp, (struct dn_flow *)oid);
621912430f6SAlexander V. Chernikov 	    printf("%s\n", bp.buf);
622fcbdb770SAndrey V. Elsukov 	    bp_flush(&bp);
623cc4d3c30SLuigi Rizzo 	    break;
624cc4d3c30SLuigi Rizzo 
625cc4d3c30SLuigi Rizzo 	case DN_LINK: {
626cc4d3c30SLuigi Rizzo 	    struct dn_link *p = (struct dn_link *)oid;
6274e9c8ae7SLuigi Rizzo 	    double b = p->bandwidth;
628cc4d3c30SLuigi Rizzo 	    char bwbuf[30];
6296882bf4dSOleg Bulyzhin 	    char burst[5 + 7];
6304e9c8ae7SLuigi Rizzo 
631cc4d3c30SLuigi Rizzo 	    /* This starts a new object so flush buffer */
632cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
633cc4d3c30SLuigi Rizzo 	    /* data rate */
634cc4d3c30SLuigi Rizzo 	    if (b == 0)
635cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "unlimited     ");
636a6184721SEmmanuel Vadot 	    else if (b >= 1000000000)
637a6184721SEmmanuel Vadot 		sprintf(bwbuf, "%7.3f Gbit/s", b/1000000000);
6384e9c8ae7SLuigi Rizzo 	    else if (b >= 1000000)
639cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
6404e9c8ae7SLuigi Rizzo 	    else if (b >= 1000)
641cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
6424e9c8ae7SLuigi Rizzo 	    else
643cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "%7.3f bit/s ", b);
6444e9c8ae7SLuigi Rizzo 
6456882bf4dSOleg Bulyzhin 	    if (humanize_number(burst, sizeof(burst), p->burst,
64656707beeSMark Johnston 		    "", HN_AUTOSCALE, 0) < 0 || g_co.verbose)
647cc4d3c30SLuigi Rizzo 		sprintf(burst, "%d", (int)p->burst);
648cc4d3c30SLuigi Rizzo 	    sprintf(buf, "%05d: %s %4d ms burst %s",
649cc4d3c30SLuigi Rizzo 		p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
6504e9c8ae7SLuigi Rizzo 	    }
6514e9c8ae7SLuigi Rizzo 	    break;
6524e9c8ae7SLuigi Rizzo 
653cc4d3c30SLuigi Rizzo 	case DN_FS:
654cc4d3c30SLuigi Rizzo 	    print_flowset_parms((struct dn_fs *)oid, buf);
655cc4d3c30SLuigi Rizzo 	    break;
656cc4d3c30SLuigi Rizzo 	case DN_PROFILE:
657cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
658cc4d3c30SLuigi Rizzo 	    print_extra_delay_parms((struct dn_profile *)oid);
6594e9c8ae7SLuigi Rizzo 	}
660cc4d3c30SLuigi Rizzo 	flush_buf(buf); // XXX does it really go here ?
6614e9c8ae7SLuigi Rizzo     }
662912430f6SAlexander V. Chernikov 
663912430f6SAlexander V. Chernikov     bp_free(&bp);
6644e9c8ae7SLuigi Rizzo }
6654e9c8ae7SLuigi Rizzo 
6664e9c8ae7SLuigi Rizzo /*
667cc4d3c30SLuigi Rizzo  * Delete pipe, queue or scheduler i
6684e9c8ae7SLuigi Rizzo  */
6694e9c8ae7SLuigi Rizzo int
670cc4d3c30SLuigi Rizzo ipfw_delete_pipe(int do_pipe, int i)
6714e9c8ae7SLuigi Rizzo {
672cc4d3c30SLuigi Rizzo 	struct {
673cc4d3c30SLuigi Rizzo 		struct dn_id oid;
674cc4d3c30SLuigi Rizzo 		uintptr_t a[1];	/* add more if we want a list */
675cc4d3c30SLuigi Rizzo 	} cmd;
676cc4d3c30SLuigi Rizzo 	oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
677cc4d3c30SLuigi Rizzo 	cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
678cc4d3c30SLuigi Rizzo 		( (do_pipe == 2) ? DN_FS : DN_SCH);
679cc4d3c30SLuigi Rizzo 	cmd.a[0] = i;
680cc4d3c30SLuigi Rizzo 	i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
6814e9c8ae7SLuigi Rizzo 	if (i) {
6824e9c8ae7SLuigi Rizzo 		i = 1;
6834e9c8ae7SLuigi Rizzo 		warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
6844e9c8ae7SLuigi Rizzo 	}
6854e9c8ae7SLuigi Rizzo 	return i;
6864e9c8ae7SLuigi Rizzo }
6874e9c8ae7SLuigi Rizzo 
6884bb7ae9dSLuigi Rizzo /*
6894bb7ae9dSLuigi Rizzo  * Code to parse delay profiles.
6904bb7ae9dSLuigi Rizzo  *
6914bb7ae9dSLuigi Rizzo  * Some link types introduce extra delays in the transmission
6924bb7ae9dSLuigi Rizzo  * of a packet, e.g. because of MAC level framing, contention on
6934bb7ae9dSLuigi Rizzo  * the use of the channel, MAC level retransmissions and so on.
6944bb7ae9dSLuigi Rizzo  * From our point of view, the channel is effectively unavailable
6954bb7ae9dSLuigi Rizzo  * for this extra time, which is constant or variable depending
6964bb7ae9dSLuigi Rizzo  * on the link type. Additionally, packets may be dropped after this
6974bb7ae9dSLuigi Rizzo  * time (e.g. on a wireless link after too many retransmissions).
6984bb7ae9dSLuigi Rizzo  * We can model the additional delay with an empirical curve
6994bb7ae9dSLuigi Rizzo  * that represents its distribution.
7004bb7ae9dSLuigi Rizzo  *
7014bb7ae9dSLuigi Rizzo  *      cumulative probability
7024bb7ae9dSLuigi Rizzo  *      1.0 ^
7034bb7ae9dSLuigi Rizzo  *          |
7044bb7ae9dSLuigi Rizzo  *      L   +-- loss-level          x
7054bb7ae9dSLuigi Rizzo  *          |                 ******
7064bb7ae9dSLuigi Rizzo  *          |                *
7074bb7ae9dSLuigi Rizzo  *          |           *****
7084bb7ae9dSLuigi Rizzo  *          |          *
7094bb7ae9dSLuigi Rizzo  *          |        **
7104bb7ae9dSLuigi Rizzo  *          |       *
7114bb7ae9dSLuigi Rizzo  *          +-------*------------------->
7124bb7ae9dSLuigi Rizzo  *                      delay
7134bb7ae9dSLuigi Rizzo  *
7144bb7ae9dSLuigi Rizzo  * The empirical curve may have both vertical and horizontal lines.
7154bb7ae9dSLuigi Rizzo  * Vertical lines represent constant delay for a range of
7164bb7ae9dSLuigi Rizzo  * probabilities; horizontal lines correspond to a discontinuty
717cc4d3c30SLuigi Rizzo  * in the delay distribution: the link will use the largest delay
7184bb7ae9dSLuigi Rizzo  * for a given probability.
7194bb7ae9dSLuigi Rizzo  *
7204bb7ae9dSLuigi Rizzo  * To pass the curve to dummynet, we must store the parameters
7214bb7ae9dSLuigi Rizzo  * in a file as described below, and issue the command
7224bb7ae9dSLuigi Rizzo  *
7234bb7ae9dSLuigi Rizzo  *      ipfw pipe <n> config ... bw XXX profile <filename> ...
7244bb7ae9dSLuigi Rizzo  *
7254bb7ae9dSLuigi Rizzo  * The file format is the following, with whitespace acting as
7264bb7ae9dSLuigi Rizzo  * a separator and '#' indicating the beginning a comment:
7274bb7ae9dSLuigi Rizzo  *
7284bb7ae9dSLuigi Rizzo  *	samples N
7294bb7ae9dSLuigi Rizzo  *		the number of samples used in the internal
7304bb7ae9dSLuigi Rizzo  *		representation (2..1024; default 100);
7314bb7ae9dSLuigi Rizzo  *
7324bb7ae9dSLuigi Rizzo  *	loss-level L
7334bb7ae9dSLuigi Rizzo  *		The probability above which packets are lost.
7344bb7ae9dSLuigi Rizzo  *	       (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
7354bb7ae9dSLuigi Rizzo  *
7364bb7ae9dSLuigi Rizzo  *	name identifier
7374bb7ae9dSLuigi Rizzo  *		Optional a name (listed by "ipfw pipe show")
7384bb7ae9dSLuigi Rizzo  *		to identify the distribution;
7394bb7ae9dSLuigi Rizzo  *
7404bb7ae9dSLuigi Rizzo  *	"delay prob" | "prob delay"
7414bb7ae9dSLuigi Rizzo  *		One of these two lines is mandatory and defines
7424bb7ae9dSLuigi Rizzo  *		the format of the following lines with data points.
7434bb7ae9dSLuigi Rizzo  *
7444bb7ae9dSLuigi Rizzo  *	XXX YYY
7454bb7ae9dSLuigi Rizzo  *		2 or more lines representing points in the curve,
7464bb7ae9dSLuigi Rizzo  *		with either delay or probability first, according
7474bb7ae9dSLuigi Rizzo  *		to the chosen format.
7484bb7ae9dSLuigi Rizzo  *		The unit for delay is milliseconds.
7494bb7ae9dSLuigi Rizzo  *
7504bb7ae9dSLuigi Rizzo  * Data points does not need to be ordered or equal to the number
7514bb7ae9dSLuigi Rizzo  * specified in the "samples" line. ipfw will sort and interpolate
7524bb7ae9dSLuigi Rizzo  * the curve as needed.
7534bb7ae9dSLuigi Rizzo  *
7544bb7ae9dSLuigi Rizzo  * Example of a profile file:
7554bb7ae9dSLuigi Rizzo 
7564bb7ae9dSLuigi Rizzo 	name    bla_bla_bla
7574bb7ae9dSLuigi Rizzo 	samples 100
7584bb7ae9dSLuigi Rizzo 	loss-level    0.86
7594bb7ae9dSLuigi Rizzo 	prob    delay
7604bb7ae9dSLuigi Rizzo 	0       200	# minimum overhead is 200ms
7614bb7ae9dSLuigi Rizzo 	0.5     200
7624bb7ae9dSLuigi Rizzo 	0.5     300
7634bb7ae9dSLuigi Rizzo 	0.8     1000
7644bb7ae9dSLuigi Rizzo 	0.9     1300
7654bb7ae9dSLuigi Rizzo 	1       1300
7664bb7ae9dSLuigi Rizzo 
7674bb7ae9dSLuigi Rizzo  * Internally, we will convert the curve to a fixed number of
7684bb7ae9dSLuigi Rizzo  * samples, and when it is time to transmit a packet we will
7694bb7ae9dSLuigi Rizzo  * model the extra delay as extra bits in the packet.
7704bb7ae9dSLuigi Rizzo  *
7714bb7ae9dSLuigi Rizzo  */
7724bb7ae9dSLuigi Rizzo 
7734bb7ae9dSLuigi Rizzo #define ED_MAX_LINE_LEN	256+ED_MAX_NAME_LEN
7744bb7ae9dSLuigi Rizzo #define ED_TOK_SAMPLES	"samples"
7754bb7ae9dSLuigi Rizzo #define ED_TOK_LOSS	"loss-level"
7764bb7ae9dSLuigi Rizzo #define ED_TOK_NAME	"name"
7774bb7ae9dSLuigi Rizzo #define ED_TOK_DELAY	"delay"
7784bb7ae9dSLuigi Rizzo #define ED_TOK_PROB	"prob"
7797a459517SLuigi Rizzo #define ED_TOK_BW	"bw"
7804bb7ae9dSLuigi Rizzo #define ED_SEPARATORS	" \t\n"
7814bb7ae9dSLuigi Rizzo #define ED_MIN_SAMPLES_NO	2
7824bb7ae9dSLuigi Rizzo 
7834bb7ae9dSLuigi Rizzo /*
7844bb7ae9dSLuigi Rizzo  * returns 1 if s is a non-negative number, with at least one '.'
7854bb7ae9dSLuigi Rizzo  */
7864bb7ae9dSLuigi Rizzo static int
7874bb7ae9dSLuigi Rizzo is_valid_number(const char *s)
7884bb7ae9dSLuigi Rizzo {
7894bb7ae9dSLuigi Rizzo 	int i, dots_found = 0;
7904bb7ae9dSLuigi Rizzo 	int len = strlen(s);
7914bb7ae9dSLuigi Rizzo 
7924bb7ae9dSLuigi Rizzo 	for (i = 0; i<len; ++i)
7934bb7ae9dSLuigi Rizzo 		if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
7944bb7ae9dSLuigi Rizzo 			return 0;
7954bb7ae9dSLuigi Rizzo 	return 1;
7964bb7ae9dSLuigi Rizzo }
7974bb7ae9dSLuigi Rizzo 
7987a459517SLuigi Rizzo /*
7997a459517SLuigi Rizzo  * Take as input a string describing a bandwidth value
8007a459517SLuigi Rizzo  * and return the numeric bandwidth value.
8017a459517SLuigi Rizzo  * set clocking interface or bandwidth value
8027a459517SLuigi Rizzo  */
80301ab7632SLuigi Rizzo static void
80420ffd88eSLuiz Otavio O Souza read_bandwidth(char *arg, uint32_t *bandwidth, char *if_name, int namelen)
8057a459517SLuigi Rizzo {
80620ffd88eSLuiz Otavio O Souza 	if (*bandwidth != (uint32_t)-1)
807cc4d3c30SLuigi Rizzo 		warnx("duplicate token, override bandwidth value!");
8087a459517SLuigi Rizzo 
8097a459517SLuigi Rizzo 	if (arg[0] >= 'a' && arg[0] <= 'z') {
810cc4d3c30SLuigi Rizzo 		if (!if_name) {
811cc4d3c30SLuigi Rizzo 			errx(1, "no if support");
812cc4d3c30SLuigi Rizzo 		}
8137a459517SLuigi Rizzo 		if (namelen >= IFNAMSIZ)
8147a459517SLuigi Rizzo 			warn("interface name truncated");
8157a459517SLuigi Rizzo 		namelen--;
8167a459517SLuigi Rizzo 		/* interface name */
81792b66dbeSAlan Somers 		strlcpy(if_name, arg, namelen);
8187a459517SLuigi Rizzo 		*bandwidth = 0;
8197a459517SLuigi Rizzo 	} else {	/* read bandwidth value */
82020ffd88eSLuiz Otavio O Souza 		uint64_t bw;
8217a459517SLuigi Rizzo 		char *end = NULL;
8227a459517SLuigi Rizzo 
8237a459517SLuigi Rizzo 		bw = strtoul(arg, &end, 0);
8247a459517SLuigi Rizzo 		if (*end == 'K' || *end == 'k') {
8257a459517SLuigi Rizzo 			end++;
8267a459517SLuigi Rizzo 			bw *= 1000;
827b74331bfSLuigi Rizzo 		} else if (*end == 'M' || *end == 'm') {
8287a459517SLuigi Rizzo 			end++;
8297a459517SLuigi Rizzo 			bw *= 1000000;
830a6184721SEmmanuel Vadot 		} else if (*end == 'G' || *end == 'g') {
831a6184721SEmmanuel Vadot 			end++;
832a6184721SEmmanuel Vadot 			bw *= 1000000000;
8337a459517SLuigi Rizzo 		}
8347a459517SLuigi Rizzo 		if ((*end == 'B' &&
8357a459517SLuigi Rizzo 			_substrcmp2(end, "Bi", "Bit/s") != 0) ||
8367a459517SLuigi Rizzo 		    _substrcmp2(end, "by", "bytes") == 0)
8377a459517SLuigi Rizzo 			bw *= 8;
8387a459517SLuigi Rizzo 
83920ffd88eSLuiz Otavio O Souza 		if (bw > UINT_MAX)
8407a459517SLuigi Rizzo 			errx(EX_DATAERR, "bandwidth too large");
8417a459517SLuigi Rizzo 
84220ffd88eSLuiz Otavio O Souza 		*bandwidth = (uint32_t)bw;
843cc4d3c30SLuigi Rizzo 		if (if_name)
8447a459517SLuigi Rizzo 			if_name[0] = '\0';
8457a459517SLuigi Rizzo 	}
8467a459517SLuigi Rizzo }
8477a459517SLuigi Rizzo 
8484bb7ae9dSLuigi Rizzo struct point {
8494bb7ae9dSLuigi Rizzo 	double prob;
8504bb7ae9dSLuigi Rizzo 	double delay;
8514bb7ae9dSLuigi Rizzo };
8524bb7ae9dSLuigi Rizzo 
85301ab7632SLuigi Rizzo static int
8544bb7ae9dSLuigi Rizzo compare_points(const void *vp1, const void *vp2)
8554bb7ae9dSLuigi Rizzo {
8564bb7ae9dSLuigi Rizzo 	const struct point *p1 = vp1;
8574bb7ae9dSLuigi Rizzo 	const struct point *p2 = vp2;
8584bb7ae9dSLuigi Rizzo 	double res = 0;
8594bb7ae9dSLuigi Rizzo 
8604bb7ae9dSLuigi Rizzo 	res = p1->prob - p2->prob;
8614bb7ae9dSLuigi Rizzo 	if (res == 0)
8624bb7ae9dSLuigi Rizzo 		res = p1->delay - p2->delay;
8634bb7ae9dSLuigi Rizzo 	if (res < 0)
8644bb7ae9dSLuigi Rizzo 		return -1;
8654bb7ae9dSLuigi Rizzo 	else if (res > 0)
8664bb7ae9dSLuigi Rizzo 		return 1;
8674bb7ae9dSLuigi Rizzo 	else
8684bb7ae9dSLuigi Rizzo 		return 0;
8694bb7ae9dSLuigi Rizzo }
8704bb7ae9dSLuigi Rizzo 
8714bb7ae9dSLuigi Rizzo #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
8724bb7ae9dSLuigi Rizzo 
8734bb7ae9dSLuigi Rizzo static void
874cc4d3c30SLuigi Rizzo load_extra_delays(const char *filename, struct dn_profile *p,
875cc4d3c30SLuigi Rizzo 	struct dn_link *link)
8764bb7ae9dSLuigi Rizzo {
8774bb7ae9dSLuigi Rizzo 	char    line[ED_MAX_LINE_LEN];
8784bb7ae9dSLuigi Rizzo 	FILE    *f;
8794bb7ae9dSLuigi Rizzo 	int     lineno = 0;
8804bb7ae9dSLuigi Rizzo 	int     i;
8814bb7ae9dSLuigi Rizzo 
8824bb7ae9dSLuigi Rizzo 	int     samples = -1;
8834bb7ae9dSLuigi Rizzo 	double  loss = -1.0;
8844bb7ae9dSLuigi Rizzo 	char    profile_name[ED_MAX_NAME_LEN];
8854bb7ae9dSLuigi Rizzo 	int     delay_first = -1;
8864bb7ae9dSLuigi Rizzo 	int     do_points = 0;
8874bb7ae9dSLuigi Rizzo 	struct point    points[ED_MAX_SAMPLES_NO];
8884bb7ae9dSLuigi Rizzo 	int     points_no = 0;
8894bb7ae9dSLuigi Rizzo 
890cc4d3c30SLuigi Rizzo 	/* XXX link never NULL? */
891cc4d3c30SLuigi Rizzo 	p->link_nr = link->link_nr;
892cc4d3c30SLuigi Rizzo 
8934bb7ae9dSLuigi Rizzo 	profile_name[0] = '\0';
8944bb7ae9dSLuigi Rizzo 	f = fopen(filename, "r");
8954bb7ae9dSLuigi Rizzo 	if (f == NULL)
8964bb7ae9dSLuigi Rizzo 		err(EX_UNAVAILABLE, "fopen: %s", filename);
8974bb7ae9dSLuigi Rizzo 
8984bb7ae9dSLuigi Rizzo 	while (fgets(line, ED_MAX_LINE_LEN, f)) {	 /* read commands */
8994bb7ae9dSLuigi Rizzo 		char *s, *cur = line, *name = NULL, *arg = NULL;
9004bb7ae9dSLuigi Rizzo 
9014bb7ae9dSLuigi Rizzo 		++lineno;
9024bb7ae9dSLuigi Rizzo 
9034bb7ae9dSLuigi Rizzo 		/* parse the line */
9044bb7ae9dSLuigi Rizzo 		while (cur) {
9054bb7ae9dSLuigi Rizzo 			s = strsep(&cur, ED_SEPARATORS);
9064bb7ae9dSLuigi Rizzo 			if (s == NULL || *s == '#')
9074bb7ae9dSLuigi Rizzo 				break;
9084bb7ae9dSLuigi Rizzo 			if (*s == '\0')
9094bb7ae9dSLuigi Rizzo 				continue;
9104bb7ae9dSLuigi Rizzo 			if (arg)
9114bb7ae9dSLuigi Rizzo 				errx(ED_EFMT("too many arguments"));
9124bb7ae9dSLuigi Rizzo 			if (name == NULL)
9134bb7ae9dSLuigi Rizzo 				name = s;
9144bb7ae9dSLuigi Rizzo 			else
9154bb7ae9dSLuigi Rizzo 				arg = s;
9164bb7ae9dSLuigi Rizzo 		}
9174bb7ae9dSLuigi Rizzo 		if (name == NULL)	/* empty line */
9184bb7ae9dSLuigi Rizzo 			continue;
9194bb7ae9dSLuigi Rizzo 		if (arg == NULL)
9204bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("missing arg for %s"), name);
9214bb7ae9dSLuigi Rizzo 
9224bb7ae9dSLuigi Rizzo 		if (!strcasecmp(name, ED_TOK_SAMPLES)) {
9234bb7ae9dSLuigi Rizzo 		    if (samples > 0)
9244bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicate ``samples'' line"));
9254bb7ae9dSLuigi Rizzo 		    if (atoi(arg) <=0)
9264bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("invalid number of samples"));
9274bb7ae9dSLuigi Rizzo 		    samples = atoi(arg);
9284bb7ae9dSLuigi Rizzo 		    if (samples>ED_MAX_SAMPLES_NO)
9294bb7ae9dSLuigi Rizzo 			    errx(ED_EFMT("too many samples, maximum is %d"),
9304bb7ae9dSLuigi Rizzo 				ED_MAX_SAMPLES_NO);
9314bb7ae9dSLuigi Rizzo 		    do_points = 0;
9327a459517SLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_BW)) {
933cc4d3c30SLuigi Rizzo 		    char buf[IFNAMSIZ];
934cc4d3c30SLuigi Rizzo 		    read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
9354bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_LOSS)) {
9364bb7ae9dSLuigi Rizzo 		    if (loss != -1.0)
9374bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
9384bb7ae9dSLuigi Rizzo 		    if (!is_valid_number(arg))
9394bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("invalid %s"), arg);
9404bb7ae9dSLuigi Rizzo 		    loss = atof(arg);
9414bb7ae9dSLuigi Rizzo 		    if (loss > 1)
9424bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("%s greater than 1.0"), name);
9434bb7ae9dSLuigi Rizzo 		    do_points = 0;
9444bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_NAME)) {
9454bb7ae9dSLuigi Rizzo 		    if (profile_name[0] != '\0')
9464bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
94792b66dbeSAlan Somers 		    strlcpy(profile_name, arg, sizeof(profile_name));
9484bb7ae9dSLuigi Rizzo 		    do_points = 0;
9494bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_DELAY)) {
9504bb7ae9dSLuigi Rizzo 		    if (do_points)
9514bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
9524bb7ae9dSLuigi Rizzo 		    delay_first = 1;
9534bb7ae9dSLuigi Rizzo 		    do_points = 1;
9544bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_PROB)) {
9554bb7ae9dSLuigi Rizzo 		    if (do_points)
9564bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
9574bb7ae9dSLuigi Rizzo 		    delay_first = 0;
9584bb7ae9dSLuigi Rizzo 		    do_points = 1;
9594bb7ae9dSLuigi Rizzo 		} else if (do_points) {
9604bb7ae9dSLuigi Rizzo 		    if (!is_valid_number(name) || !is_valid_number(arg))
9614bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("invalid point found"));
9624bb7ae9dSLuigi Rizzo 		    if (delay_first) {
9634bb7ae9dSLuigi Rizzo 			points[points_no].delay = atof(name);
9644bb7ae9dSLuigi Rizzo 			points[points_no].prob = atof(arg);
9654bb7ae9dSLuigi Rizzo 		    } else {
9664bb7ae9dSLuigi Rizzo 			points[points_no].delay = atof(arg);
9674bb7ae9dSLuigi Rizzo 			points[points_no].prob = atof(name);
9684bb7ae9dSLuigi Rizzo 		    }
9694bb7ae9dSLuigi Rizzo 		    if (points[points_no].prob > 1.0)
9704bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("probability greater than 1.0"));
9714bb7ae9dSLuigi Rizzo 		    ++points_no;
9724bb7ae9dSLuigi Rizzo 		} else {
9734bb7ae9dSLuigi Rizzo 		    errx(ED_EFMT("unrecognised command '%s'"), name);
9744bb7ae9dSLuigi Rizzo 		}
9754bb7ae9dSLuigi Rizzo 	}
9764bb7ae9dSLuigi Rizzo 
977ac2e492bSAlexander Leidinger 	fclose (f);
978ac2e492bSAlexander Leidinger 
9794bb7ae9dSLuigi Rizzo 	if (samples == -1) {
9804bb7ae9dSLuigi Rizzo 	    warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
9814bb7ae9dSLuigi Rizzo 	    samples = 100;
9824bb7ae9dSLuigi Rizzo 	}
9834bb7ae9dSLuigi Rizzo 
9844bb7ae9dSLuigi Rizzo 	if (loss == -1.0) {
9854bb7ae9dSLuigi Rizzo 	    warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
9864bb7ae9dSLuigi Rizzo 	    loss = 1;
9874bb7ae9dSLuigi Rizzo 	}
9884bb7ae9dSLuigi Rizzo 
9894bb7ae9dSLuigi Rizzo 	/* make sure that there are enough points. */
9904bb7ae9dSLuigi Rizzo 	if (points_no < ED_MIN_SAMPLES_NO)
9914bb7ae9dSLuigi Rizzo 	    errx(ED_EFMT("too few samples, need at least %d"),
9924bb7ae9dSLuigi Rizzo 		ED_MIN_SAMPLES_NO);
9934bb7ae9dSLuigi Rizzo 
9944bb7ae9dSLuigi Rizzo 	qsort(points, points_no, sizeof(struct point), compare_points);
9954bb7ae9dSLuigi Rizzo 
9964bb7ae9dSLuigi Rizzo 	/* interpolation */
9974bb7ae9dSLuigi Rizzo 	for (i = 0; i<points_no-1; ++i) {
9984bb7ae9dSLuigi Rizzo 	    double y1 = points[i].prob * samples;
9994bb7ae9dSLuigi Rizzo 	    double x1 = points[i].delay;
10004bb7ae9dSLuigi Rizzo 	    double y2 = points[i+1].prob * samples;
10014bb7ae9dSLuigi Rizzo 	    double x2 = points[i+1].delay;
10024bb7ae9dSLuigi Rizzo 
1003cc4d3c30SLuigi Rizzo 	    int ix = y1;
10044bb7ae9dSLuigi Rizzo 	    int stop = y2;
10054bb7ae9dSLuigi Rizzo 
10064bb7ae9dSLuigi Rizzo 	    if (x1 == x2) {
1007cc4d3c30SLuigi Rizzo 		for (; ix<stop; ++ix)
1008cc4d3c30SLuigi Rizzo 		    p->samples[ix] = x1;
10094bb7ae9dSLuigi Rizzo 	    } else {
10104bb7ae9dSLuigi Rizzo 		double m = (y2-y1)/(x2-x1);
10114bb7ae9dSLuigi Rizzo 		double c = y1 - m*x1;
1012cc4d3c30SLuigi Rizzo 		for (; ix<stop ; ++ix)
1013cc4d3c30SLuigi Rizzo 		    p->samples[ix] = (ix - c)/m;
10144bb7ae9dSLuigi Rizzo 	    }
10154bb7ae9dSLuigi Rizzo 	}
10164bb7ae9dSLuigi Rizzo 	p->samples_no = samples;
10174bb7ae9dSLuigi Rizzo 	p->loss_level = loss * samples;
101892b66dbeSAlan Somers 	strlcpy(p->name, profile_name, sizeof(p->name));
10194bb7ae9dSLuigi Rizzo }
10204bb7ae9dSLuigi Rizzo 
102191336b40SDon Lewis #ifdef NEW_AQM
102291336b40SDon Lewis 
102391336b40SDon Lewis /* Parse AQM/extra scheduler parameters */
102491336b40SDon Lewis static int
102591336b40SDon Lewis process_extra_parms(int *ac, char **av, struct dn_extra_parms *ep,
102691336b40SDon Lewis 	uint16_t type)
102791336b40SDon Lewis {
102891336b40SDon Lewis 	int i;
102991336b40SDon Lewis 
103091336b40SDon Lewis 	/* use kernel defaults */
103191336b40SDon Lewis 	for (i=0; i<DN_MAX_EXTRA_PARM; i++)
103291336b40SDon Lewis 		ep->par[i] = -1;
103391336b40SDon Lewis 
103491336b40SDon Lewis 	switch(type) {
103591336b40SDon Lewis 	case TOK_CODEL:
103691336b40SDon Lewis 	case TOK_FQ_CODEL:
103791336b40SDon Lewis 	/* Codel
103891336b40SDon Lewis 	 * 0- target, 1- interval, 2- flags,
103991336b40SDon Lewis 	 * FQ_CODEL
104091336b40SDon Lewis 	 * 3- quantum, 4- limit, 5- flows
104191336b40SDon Lewis 	 */
104291336b40SDon Lewis 		if (type==TOK_CODEL)
104391336b40SDon Lewis 			ep->par[2] = 0;
104491336b40SDon Lewis 		else
104591336b40SDon Lewis 			ep->par[2] = CODEL_ECN_ENABLED;
104691336b40SDon Lewis 
104791336b40SDon Lewis 		while (*ac > 0) {
104891336b40SDon Lewis 			int tok = match_token(aqm_params, *av);
104991336b40SDon Lewis 			(*ac)--; av++;
105091336b40SDon Lewis 			switch(tok) {
105191336b40SDon Lewis 			case TOK_TARGET:
105291336b40SDon Lewis 				if (*ac <= 0 || time_to_us(av[0]) < 0)
105391336b40SDon Lewis 					errx(EX_DATAERR, "target needs time\n");
105491336b40SDon Lewis 
105591336b40SDon Lewis 				ep->par[0] = time_to_us(av[0]);
105691336b40SDon Lewis 				(*ac)--; av++;
105791336b40SDon Lewis 				break;
105891336b40SDon Lewis 
105991336b40SDon Lewis 			case TOK_INTERVAL:
106091336b40SDon Lewis 				if (*ac <= 0 || time_to_us(av[0]) < 0)
106191336b40SDon Lewis 					errx(EX_DATAERR, "interval needs time\n");
106291336b40SDon Lewis 
106391336b40SDon Lewis 				ep->par[1] = time_to_us(av[0]);
106491336b40SDon Lewis 				(*ac)--; av++;
106591336b40SDon Lewis 				break;
106691336b40SDon Lewis 
106791336b40SDon Lewis 			case TOK_ECN:
106891336b40SDon Lewis 				ep->par[2] = CODEL_ECN_ENABLED;
106991336b40SDon Lewis 				break;
107091336b40SDon Lewis 			case TOK_NO_ECN:
107191336b40SDon Lewis 				ep->par[2] &= ~CODEL_ECN_ENABLED;
107291336b40SDon Lewis 				break;
107391336b40SDon Lewis 			/* Config fq_codel parameters */
107491336b40SDon Lewis 			case TOK_QUANTUM:
107591336b40SDon Lewis 				if (type != TOK_FQ_CODEL)
107691336b40SDon Lewis 					errx(EX_DATAERR, "quantum is not for codel\n");
107791336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
107891336b40SDon Lewis 					errx(EX_DATAERR, "quantum needs number\n");
107991336b40SDon Lewis 
108091336b40SDon Lewis 				ep->par[3]= atoi(av[0]);
108191336b40SDon Lewis 				(*ac)--; av++;
108291336b40SDon Lewis 				break;
108391336b40SDon Lewis 
108491336b40SDon Lewis 			case TOK_LIMIT:
108591336b40SDon Lewis 				if (type != TOK_FQ_CODEL)
108691336b40SDon Lewis 					errx(EX_DATAERR, "limit is not for codel, use queue instead\n");
108791336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
108891336b40SDon Lewis 					errx(EX_DATAERR, "limit needs number\n");
108991336b40SDon Lewis 
109091336b40SDon Lewis 				ep->par[4] = atoi(av[0]);
109191336b40SDon Lewis 				(*ac)--; av++;
109291336b40SDon Lewis 				break;
109391336b40SDon Lewis 
109491336b40SDon Lewis 			case TOK_FLOWS:
109591336b40SDon Lewis 				if (type != TOK_FQ_CODEL)
109691336b40SDon Lewis 					errx(EX_DATAERR, "flows is not for codel\n");
109791336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
109891336b40SDon Lewis 					errx(EX_DATAERR, "flows needs number\n");
109991336b40SDon Lewis 
110091336b40SDon Lewis 				ep->par[5] = atoi(av[0]);
110191336b40SDon Lewis 				(*ac)--; av++;
110291336b40SDon Lewis 				break;
110391336b40SDon Lewis 
110491336b40SDon Lewis 			default:
110591336b40SDon Lewis 				printf("%s is Invalid parameter\n", av[-1]);
110691336b40SDon Lewis 			}
110791336b40SDon Lewis 		}
110891336b40SDon Lewis 		break;
110991336b40SDon Lewis 	case TOK_PIE:
111091336b40SDon Lewis 	case TOK_FQ_PIE:
111191336b40SDon Lewis 		/* PIE
111291336b40SDon Lewis 		 * 0- target , 1- tupdate, 2- max_burst,
111391336b40SDon Lewis 		 * 3- max_ecnth, 4- alpha,
111491336b40SDon Lewis 		 * 5- beta, 6- flags
111591336b40SDon Lewis 		 * FQ_CODEL
111691336b40SDon Lewis 		 * 7- quantum, 8- limit, 9- flows
111791336b40SDon Lewis 		 */
111891336b40SDon Lewis 
111991336b40SDon Lewis 		if ( type == TOK_PIE)
112091336b40SDon Lewis 			ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED
112191336b40SDon Lewis 				| PIE_DERAND_ENABLED;
112291336b40SDon Lewis 		else
112391336b40SDon Lewis 			/* for FQ-PIE, use TS mode */
112491336b40SDon Lewis 			ep->par[6] = PIE_CAPDROP_ENABLED |  PIE_DERAND_ENABLED
112591336b40SDon Lewis 				| PIE_ECN_ENABLED;
112691336b40SDon Lewis 
112791336b40SDon Lewis 		while (*ac > 0) {
112891336b40SDon Lewis 			int tok = match_token(aqm_params, *av);
112991336b40SDon Lewis 			(*ac)--; av++;
113091336b40SDon Lewis 			switch(tok) {
113191336b40SDon Lewis 			case TOK_TARGET:
113291336b40SDon Lewis 				if (*ac <= 0 || time_to_us(av[0]) < 0)
113391336b40SDon Lewis 					errx(EX_DATAERR, "target needs time\n");
113491336b40SDon Lewis 
113591336b40SDon Lewis 				ep->par[0] = time_to_us(av[0]);
113691336b40SDon Lewis 				(*ac)--; av++;
113791336b40SDon Lewis 				break;
113891336b40SDon Lewis 
113991336b40SDon Lewis 			case TOK_TUPDATE:
114091336b40SDon Lewis 				if (*ac <= 0 || time_to_us(av[0]) < 0)
114191336b40SDon Lewis 					errx(EX_DATAERR, "tupdate needs time\n");
114291336b40SDon Lewis 
114391336b40SDon Lewis 				ep->par[1] = time_to_us(av[0]);
114491336b40SDon Lewis 				(*ac)--; av++;
114591336b40SDon Lewis 				break;
114691336b40SDon Lewis 
114791336b40SDon Lewis 			case TOK_MAX_BURST:
114891336b40SDon Lewis 				if (*ac <= 0 || time_to_us(av[0]) < 0)
114991336b40SDon Lewis 					errx(EX_DATAERR, "max_burst needs time\n");
115091336b40SDon Lewis 
115191336b40SDon Lewis 				ep->par[2] = time_to_us(av[0]);
115291336b40SDon Lewis 				(*ac)--; av++;
115391336b40SDon Lewis 				break;
115491336b40SDon Lewis 
115591336b40SDon Lewis 			case TOK_MAX_ECNTH:
115691336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
115791336b40SDon Lewis 					errx(EX_DATAERR, "max_ecnth needs number\n");
115891336b40SDon Lewis 
115991336b40SDon Lewis 				ep->par[3] = atof(av[0]) * PIE_SCALE;
116091336b40SDon Lewis 				(*ac)--; av++;
116191336b40SDon Lewis 				break;
116291336b40SDon Lewis 
116391336b40SDon Lewis 			case TOK_ALPHA:
116491336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
116591336b40SDon Lewis 					errx(EX_DATAERR, "alpha needs number\n");
116691336b40SDon Lewis 
116791336b40SDon Lewis 				ep->par[4] = atof(av[0]) * PIE_SCALE;
116891336b40SDon Lewis 				(*ac)--; av++;
116991336b40SDon Lewis 				break;
117091336b40SDon Lewis 
117191336b40SDon Lewis 			case TOK_BETA:
117291336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
117391336b40SDon Lewis 					errx(EX_DATAERR, "beta needs number\n");
117491336b40SDon Lewis 
117591336b40SDon Lewis 				ep->par[5] = atof(av[0]) * PIE_SCALE;
117691336b40SDon Lewis 				(*ac)--; av++;
117791336b40SDon Lewis 				break;
117891336b40SDon Lewis 
117991336b40SDon Lewis 			case TOK_ECN:
118091336b40SDon Lewis 				ep->par[6] |= PIE_ECN_ENABLED;
118191336b40SDon Lewis 				break;
118291336b40SDon Lewis 			case TOK_NO_ECN:
118391336b40SDon Lewis 				ep->par[6] &= ~PIE_ECN_ENABLED;
118491336b40SDon Lewis 				break;
118591336b40SDon Lewis 
118691336b40SDon Lewis 			case TOK_CAPDROP:
118791336b40SDon Lewis 				ep->par[6] |= PIE_CAPDROP_ENABLED;
118891336b40SDon Lewis 				break;
118991336b40SDon Lewis 			case TOK_NO_CAPDROP:
119091336b40SDon Lewis 				ep->par[6] &= ~PIE_CAPDROP_ENABLED;
119191336b40SDon Lewis 				break;
119291336b40SDon Lewis 
119391336b40SDon Lewis 			case TOK_ONOFF:
119491336b40SDon Lewis 				ep->par[6] |= PIE_ON_OFF_MODE_ENABLED;
119591336b40SDon Lewis 				break;
119691336b40SDon Lewis 
119791336b40SDon Lewis 			case TOK_DRE:
119891336b40SDon Lewis 				ep->par[6] |= PIE_DEPRATEEST_ENABLED;
119991336b40SDon Lewis 				break;
120091336b40SDon Lewis 
120191336b40SDon Lewis 			case TOK_TS:
120291336b40SDon Lewis 				ep->par[6] &= ~PIE_DEPRATEEST_ENABLED;
120391336b40SDon Lewis 				break;
120491336b40SDon Lewis 
120591336b40SDon Lewis 			case TOK_DERAND:
120691336b40SDon Lewis 				ep->par[6] |= PIE_DERAND_ENABLED;
120791336b40SDon Lewis 				break;
120891336b40SDon Lewis 			case TOK_NO_DERAND:
120991336b40SDon Lewis 				ep->par[6] &= ~PIE_DERAND_ENABLED;
121091336b40SDon Lewis 				break;
121191336b40SDon Lewis 
121291336b40SDon Lewis 			/* Config fq_pie parameters */
121391336b40SDon Lewis 			case TOK_QUANTUM:
121491336b40SDon Lewis 				if (type != TOK_FQ_PIE)
121591336b40SDon Lewis 					errx(EX_DATAERR, "quantum is not for pie\n");
121691336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
121791336b40SDon Lewis 					errx(EX_DATAERR, "quantum needs number\n");
121891336b40SDon Lewis 
121991336b40SDon Lewis 				ep->par[7]= atoi(av[0]);
122091336b40SDon Lewis 				(*ac)--; av++;
122191336b40SDon Lewis 				break;
122291336b40SDon Lewis 
122391336b40SDon Lewis 			case TOK_LIMIT:
122491336b40SDon Lewis 				if (type != TOK_FQ_PIE)
122591336b40SDon Lewis 					errx(EX_DATAERR, "limit is not for pie, use queue instead\n");
122691336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
122791336b40SDon Lewis 					errx(EX_DATAERR, "limit needs number\n");
122891336b40SDon Lewis 
122991336b40SDon Lewis 				ep->par[8] = atoi(av[0]);
123091336b40SDon Lewis 				(*ac)--; av++;
123191336b40SDon Lewis 				break;
123291336b40SDon Lewis 
123391336b40SDon Lewis 			case TOK_FLOWS:
123491336b40SDon Lewis 				if (type != TOK_FQ_PIE)
123591336b40SDon Lewis 					errx(EX_DATAERR, "flows is not for pie\n");
123691336b40SDon Lewis 				if (*ac <= 0 || !is_valid_number(av[0]))
123791336b40SDon Lewis 					errx(EX_DATAERR, "flows needs number\n");
123891336b40SDon Lewis 
123991336b40SDon Lewis 				ep->par[9] = atoi(av[0]);
124091336b40SDon Lewis 				(*ac)--; av++;
124191336b40SDon Lewis 				break;
124291336b40SDon Lewis 
124391336b40SDon Lewis 
124491336b40SDon Lewis 			default:
124591336b40SDon Lewis 				printf("%s is invalid parameter\n", av[-1]);
124691336b40SDon Lewis 			}
124791336b40SDon Lewis 		}
124891336b40SDon Lewis 		break;
124991336b40SDon Lewis 	}
125091336b40SDon Lewis 
125191336b40SDon Lewis 	return 0;
125291336b40SDon Lewis }
125391336b40SDon Lewis 
125491336b40SDon Lewis #endif
125591336b40SDon Lewis 
125691336b40SDon Lewis 
1257cc4d3c30SLuigi Rizzo /*
1258cc4d3c30SLuigi Rizzo  * configuration of pipes, schedulers, flowsets.
1259cc4d3c30SLuigi Rizzo  * When we configure a new scheduler, an empty pipe is created, so:
1260cc4d3c30SLuigi Rizzo  *
1261cc4d3c30SLuigi Rizzo  * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
1262cc4d3c30SLuigi Rizzo  *	sched N+Delta type fifo sched_mask ...
1263cc4d3c30SLuigi Rizzo  *	pipe N+Delta <parameters>
1264cc4d3c30SLuigi Rizzo  *	flowset N+Delta pipe N+Delta (no parameters)
1265cc4d3c30SLuigi Rizzo  *	sched N type wf2q+ sched_mask ...
1266cc4d3c30SLuigi Rizzo  *	pipe N <parameters>
1267cc4d3c30SLuigi Rizzo  *
1268cc4d3c30SLuigi Rizzo  * do_pipe = 2 -> flowset N config
1269cc4d3c30SLuigi Rizzo  *	flowset N parameters
1270cc4d3c30SLuigi Rizzo  *
1271cc4d3c30SLuigi Rizzo  * do_pipe = 3 -> sched N config
1272cc4d3c30SLuigi Rizzo  *	sched N parameters (default no pipe)
1273cc4d3c30SLuigi Rizzo  *	optional Pipe N config ...
1274cc4d3c30SLuigi Rizzo  * pipe ==>
1275cc4d3c30SLuigi Rizzo  */
12764e9c8ae7SLuigi Rizzo void
12774e9c8ae7SLuigi Rizzo ipfw_config_pipe(int ac, char **av)
12784e9c8ae7SLuigi Rizzo {
12796a7bb02dSLuigi Rizzo 	int i;
12806a7bb02dSLuigi Rizzo 	u_int j;
12814e9c8ae7SLuigi Rizzo 	char *end;
1282cc4d3c30SLuigi Rizzo 	struct dn_id *buf, *base;
1283cc4d3c30SLuigi Rizzo 	struct dn_sch *sch = NULL;
1284cc4d3c30SLuigi Rizzo 	struct dn_link *p = NULL;
1285cc4d3c30SLuigi Rizzo 	struct dn_fs *fs = NULL;
1286cc4d3c30SLuigi Rizzo 	struct dn_profile *pf = NULL;
1287cc4d3c30SLuigi Rizzo 	struct ipfw_flow_id *mask = NULL;
128891336b40SDon Lewis #ifdef NEW_AQM
1289268a20a0SAdrian Chadd 	struct dn_extra_parms *aqm_extra = NULL;
1290268a20a0SAdrian Chadd 	struct dn_extra_parms *sch_extra = NULL;
129191336b40SDon Lewis 	int lmax_extra;
129291336b40SDon Lewis #endif
129391336b40SDon Lewis 
1294cc4d3c30SLuigi Rizzo 	int lmax;
1295cc4d3c30SLuigi Rizzo 	uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
12964e9c8ae7SLuigi Rizzo 
1297cc4d3c30SLuigi Rizzo 	/*
1298cc4d3c30SLuigi Rizzo 	 * allocate space for 1 header,
1299cc4d3c30SLuigi Rizzo 	 * 1 scheduler, 1 link, 1 flowset, 1 profile
1300cc4d3c30SLuigi Rizzo 	 */
1301cc4d3c30SLuigi Rizzo 	lmax = sizeof(struct dn_id);	/* command header */
1302cc4d3c30SLuigi Rizzo 	lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
1303cc4d3c30SLuigi Rizzo 		sizeof(struct dn_fs) + sizeof(struct dn_profile);
13044e9c8ae7SLuigi Rizzo 
130591336b40SDon Lewis #ifdef NEW_AQM
130691336b40SDon Lewis 	/* Extra Params */
130791336b40SDon Lewis 	lmax_extra = sizeof(struct dn_extra_parms);
130891336b40SDon Lewis 	/* two lmax_extra because one for AQM params and another
130991336b40SDon Lewis 	 * sch params
131091336b40SDon Lewis 	 */
131191336b40SDon Lewis 	lmax += lmax_extra*2;
131291336b40SDon Lewis #endif
131391336b40SDon Lewis 
13144e9c8ae7SLuigi Rizzo 	av++; ac--;
13154e9c8ae7SLuigi Rizzo 	/* Pipe number */
13164e9c8ae7SLuigi Rizzo 	if (ac && isdigit(**av)) {
13174e9c8ae7SLuigi Rizzo 		i = atoi(*av); av++; ac--;
1318cc4d3c30SLuigi Rizzo 	} else
1319cc4d3c30SLuigi Rizzo 		i = -1;
1320cc4d3c30SLuigi Rizzo 	if (i <= 0)
1321cc4d3c30SLuigi Rizzo 		errx(EX_USAGE, "need a pipe/flowset/sched number");
1322cc4d3c30SLuigi Rizzo 	base = buf = safe_calloc(1, lmax);
1323cc4d3c30SLuigi Rizzo 	/* all commands start with a 'CONFIGURE' and a version */
1324cc4d3c30SLuigi Rizzo 	o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
1325cc4d3c30SLuigi Rizzo 	base->id = DN_API_VERSION;
1326cc4d3c30SLuigi Rizzo 
132756707beeSMark Johnston 	switch (g_co.do_pipe) {
1328cc4d3c30SLuigi Rizzo 	case 1: /* "pipe N config ..." */
1329cc4d3c30SLuigi Rizzo 		/* Allocate space for the WF2Q+ scheduler, its link
1330cc4d3c30SLuigi Rizzo 		 * and the FIFO flowset. Set the number, but leave
1331cc4d3c30SLuigi Rizzo 		 * the scheduler subtype and other parameters to 0
1332cc4d3c30SLuigi Rizzo 		 * so the kernel will use appropriate defaults.
1333cc4d3c30SLuigi Rizzo 		 * XXX todo: add a flag to record if a parameter
1334cc4d3c30SLuigi Rizzo 		 * is actually configured.
1335cc4d3c30SLuigi Rizzo 		 * If we do a 'pipe config' mask -> sched_mask.
1336cc4d3c30SLuigi Rizzo 		 * The FIFO scheduler and link are derived from the
1337cc4d3c30SLuigi Rizzo 		 * WF2Q+ one in the kernel.
1338cc4d3c30SLuigi Rizzo 		 */
133991336b40SDon Lewis #ifdef NEW_AQM
134091336b40SDon Lewis 		sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
134191336b40SDon Lewis 		sch_extra ->oid.subtype = 0; /* don't configure scheduler */
134291336b40SDon Lewis #endif
1343cc4d3c30SLuigi Rizzo 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
1344cc4d3c30SLuigi Rizzo 		p = o_next(&buf, sizeof(*p), DN_LINK);
134591336b40SDon Lewis #ifdef NEW_AQM
134691336b40SDon Lewis 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
134791336b40SDon Lewis 		aqm_extra ->oid.subtype = 0; /* don't configure AQM */
134891336b40SDon Lewis #endif
1349cc4d3c30SLuigi Rizzo 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1350cc4d3c30SLuigi Rizzo 
1351cc4d3c30SLuigi Rizzo 		sch->sched_nr = i;
1352cc4d3c30SLuigi Rizzo 		sch->oid.subtype = 0;	/* defaults to WF2Q+ */
1353cc4d3c30SLuigi Rizzo 		mask = &sch->sched_mask;
1354cc4d3c30SLuigi Rizzo 		flags = &sch->flags;
1355cc4d3c30SLuigi Rizzo 		buckets = &sch->buckets;
1356cc4d3c30SLuigi Rizzo 		*flags |= DN_PIPE_CMD;
1357cc4d3c30SLuigi Rizzo 
1358cc4d3c30SLuigi Rizzo 		p->link_nr = i;
1359cc4d3c30SLuigi Rizzo 
1360cc4d3c30SLuigi Rizzo 		/* This flowset is only for the FIFO scheduler */
1361cc4d3c30SLuigi Rizzo 		fs->fs_nr = i + 2*DN_MAX_ID;
1362cc4d3c30SLuigi Rizzo 		fs->sched_nr = i + DN_MAX_ID;
1363cc4d3c30SLuigi Rizzo 		break;
1364cc4d3c30SLuigi Rizzo 
1365cc4d3c30SLuigi Rizzo 	case 2: /* "queue N config ... " */
136691336b40SDon Lewis #ifdef NEW_AQM
136791336b40SDon Lewis 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
136891336b40SDon Lewis 		aqm_extra ->oid.subtype = 0;
136991336b40SDon Lewis #endif
1370cc4d3c30SLuigi Rizzo 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1371cc4d3c30SLuigi Rizzo 		fs->fs_nr = i;
1372cc4d3c30SLuigi Rizzo 		mask = &fs->flow_mask;
1373cc4d3c30SLuigi Rizzo 		flags = &fs->flags;
1374cc4d3c30SLuigi Rizzo 		buckets = &fs->buckets;
1375cc4d3c30SLuigi Rizzo 		break;
1376cc4d3c30SLuigi Rizzo 
1377cc4d3c30SLuigi Rizzo 	case 3: /* "sched N config ..." */
137891336b40SDon Lewis #ifdef NEW_AQM
137991336b40SDon Lewis 		sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
138091336b40SDon Lewis 		sch_extra ->oid.subtype = 0;
138191336b40SDon Lewis #endif
1382cc4d3c30SLuigi Rizzo 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
138391336b40SDon Lewis #ifdef NEW_AQM
138491336b40SDon Lewis 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
138591336b40SDon Lewis 		aqm_extra ->oid.subtype = 0;
138691336b40SDon Lewis #endif
1387cc4d3c30SLuigi Rizzo 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1388cc4d3c30SLuigi Rizzo 		sch->sched_nr = i;
1389cc4d3c30SLuigi Rizzo 		mask = &sch->sched_mask;
1390cc4d3c30SLuigi Rizzo 		flags = &sch->flags;
1391cc4d3c30SLuigi Rizzo 		buckets = &sch->buckets;
1392cc4d3c30SLuigi Rizzo 		/* fs is used only with !MULTIQUEUE schedulers */
1393cc4d3c30SLuigi Rizzo 		fs->fs_nr = i + DN_MAX_ID;
1394cc4d3c30SLuigi Rizzo 		fs->sched_nr = i;
1395cc4d3c30SLuigi Rizzo 		break;
13964e9c8ae7SLuigi Rizzo 	}
1397cc4d3c30SLuigi Rizzo 	/* set to -1 those fields for which we want to reuse existing
1398cc4d3c30SLuigi Rizzo 	 * values from the kernel.
1399cc4d3c30SLuigi Rizzo 	 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
1400cc4d3c30SLuigi Rizzo 	 * XXX todo: support reuse of the mask.
1401cc4d3c30SLuigi Rizzo 	 */
1402cc4d3c30SLuigi Rizzo 	if (p)
1403cc4d3c30SLuigi Rizzo 		p->bandwidth = -1;
1404*c3c81617SElyes Haouas 	for (j = 0; j < nitems(fs->par); j++)
1405cc4d3c30SLuigi Rizzo 		fs->par[j] = -1;
14064e9c8ae7SLuigi Rizzo 	while (ac > 0) {
14074e9c8ae7SLuigi Rizzo 		double d;
14084e9c8ae7SLuigi Rizzo 		int tok = match_token(dummynet_params, *av);
14094e9c8ae7SLuigi Rizzo 		ac--; av++;
14104e9c8ae7SLuigi Rizzo 
14114e9c8ae7SLuigi Rizzo 		switch(tok) {
14124e9c8ae7SLuigi Rizzo 		case TOK_NOERROR:
1413cc4d3c30SLuigi Rizzo 			NEED(fs, "noerror is only for pipes");
1414cc4d3c30SLuigi Rizzo 			fs->flags |= DN_NOERROR;
14154e9c8ae7SLuigi Rizzo 			break;
14164e9c8ae7SLuigi Rizzo 
14174e9c8ae7SLuigi Rizzo 		case TOK_PLR:
1418cc4d3c30SLuigi Rizzo 			NEED(fs, "plr is only for pipes");
141931cf66d7SRichard Scheffenegger 			NEED1("plr needs one or four arguments 0..1\n");
142031cf66d7SRichard Scheffenegger 			if ((end = strsep(&av[0], ","))) {
142131cf66d7SRichard Scheffenegger 				d = strtod(end, NULL);
142231cf66d7SRichard Scheffenegger 				d = (d < 0) ? 0 : (d <= 1) ? d : 1;
142331cf66d7SRichard Scheffenegger 				fs->plr[0] = (int)(d*0x7fffffff);
142431cf66d7SRichard Scheffenegger 			}
142531cf66d7SRichard Scheffenegger 			if ((end = strsep(&av[0], ","))) {
142631cf66d7SRichard Scheffenegger 				d = strtod(end, NULL);
142731cf66d7SRichard Scheffenegger 				d = (d < 0) ? 0 : (d <= 1) ? d : 1;
142831cf66d7SRichard Scheffenegger 				fs->plr[1] = (int)(d*0x7fffffff);
142931cf66d7SRichard Scheffenegger 			}
143031cf66d7SRichard Scheffenegger 			if ((end = strsep(&av[0], ","))) {
143131cf66d7SRichard Scheffenegger 				d = strtod(end, NULL);
143231cf66d7SRichard Scheffenegger 				d = (d < 0) ? 0 : (d <= 1) ? d : 1;
143331cf66d7SRichard Scheffenegger 				fs->plr[2] = (int)(d*0x7fffffff);
143431cf66d7SRichard Scheffenegger 			}
143531cf66d7SRichard Scheffenegger 			if ((end = strsep(&av[0], ","))) {
143631cf66d7SRichard Scheffenegger 				d = strtod(end, NULL);
143731cf66d7SRichard Scheffenegger 				d = (d < 0) ? 0 : (d <= 1) ? d : 1;
143831cf66d7SRichard Scheffenegger 				fs->plr[3] = (int)(d*0x7fffffff);
143931cf66d7SRichard Scheffenegger 			}
14404e9c8ae7SLuigi Rizzo 			ac--; av++;
14414e9c8ae7SLuigi Rizzo 			break;
14424e9c8ae7SLuigi Rizzo 
14434e9c8ae7SLuigi Rizzo 		case TOK_QUEUE:
1444cc4d3c30SLuigi Rizzo 			NEED(fs, "queue is only for pipes or flowsets");
14454e9c8ae7SLuigi Rizzo 			NEED1("queue needs queue size\n");
14464e9c8ae7SLuigi Rizzo 			end = NULL;
1447cc4d3c30SLuigi Rizzo 			fs->qsize = strtoul(av[0], &end, 0);
14484e9c8ae7SLuigi Rizzo 			if (*end == 'K' || *end == 'k') {
1449cc4d3c30SLuigi Rizzo 				fs->flags |= DN_QSIZE_BYTES;
1450cc4d3c30SLuigi Rizzo 				fs->qsize *= 1024;
14514e9c8ae7SLuigi Rizzo 			} else if (*end == 'B' ||
14524e9c8ae7SLuigi Rizzo 			    _substrcmp2(end, "by", "bytes") == 0) {
1453cc4d3c30SLuigi Rizzo 				fs->flags |= DN_QSIZE_BYTES;
14544e9c8ae7SLuigi Rizzo 			}
14554e9c8ae7SLuigi Rizzo 			ac--; av++;
14564e9c8ae7SLuigi Rizzo 			break;
14574e9c8ae7SLuigi Rizzo 
14584e9c8ae7SLuigi Rizzo 		case TOK_BUCKETS:
1459cc4d3c30SLuigi Rizzo 			NEED(fs, "buckets is only for pipes or flowsets");
14604e9c8ae7SLuigi Rizzo 			NEED1("buckets needs argument\n");
1461cc4d3c30SLuigi Rizzo 			*buckets = strtoul(av[0], NULL, 0);
14624e9c8ae7SLuigi Rizzo 			ac--; av++;
14634e9c8ae7SLuigi Rizzo 			break;
14644e9c8ae7SLuigi Rizzo 
1465cc4d3c30SLuigi Rizzo 		case TOK_FLOW_MASK:
1466cc4d3c30SLuigi Rizzo 		case TOK_SCHED_MASK:
14674e9c8ae7SLuigi Rizzo 		case TOK_MASK:
1468cc4d3c30SLuigi Rizzo 			NEED(mask, "tok_mask");
14694e9c8ae7SLuigi Rizzo 			NEED1("mask needs mask specifier\n");
14704e9c8ae7SLuigi Rizzo 			/*
14714e9c8ae7SLuigi Rizzo 			 * per-flow queue, mask is dst_ip, dst_port,
14724e9c8ae7SLuigi Rizzo 			 * src_ip, src_port, proto measured in bits
14734e9c8ae7SLuigi Rizzo 			 */
14744e9c8ae7SLuigi Rizzo 
1475cc4d3c30SLuigi Rizzo 			bzero(mask, sizeof(*mask));
14764e9c8ae7SLuigi Rizzo 			end = NULL;
14774e9c8ae7SLuigi Rizzo 
14784e9c8ae7SLuigi Rizzo 			while (ac >= 1) {
14794e9c8ae7SLuigi Rizzo 			    uint32_t *p32 = NULL;
14804e9c8ae7SLuigi Rizzo 			    uint16_t *p16 = NULL;
14814e9c8ae7SLuigi Rizzo 			    uint32_t *p20 = NULL;
14824e9c8ae7SLuigi Rizzo 			    struct in6_addr *pa6 = NULL;
14834e9c8ae7SLuigi Rizzo 			    uint32_t a;
14844e9c8ae7SLuigi Rizzo 
14854e9c8ae7SLuigi Rizzo 			    tok = match_token(dummynet_params, *av);
14864e9c8ae7SLuigi Rizzo 			    ac--; av++;
14874e9c8ae7SLuigi Rizzo 			    switch(tok) {
14884e9c8ae7SLuigi Rizzo 			    case TOK_ALL:
14894e9c8ae7SLuigi Rizzo 				    /*
14904e9c8ae7SLuigi Rizzo 				     * special case, all bits significant
1491f9f7bde3SLuigi Rizzo 				     * except 'extra' (the queue number)
14924e9c8ae7SLuigi Rizzo 				     */
1493cc4d3c30SLuigi Rizzo 				    mask->dst_ip = ~0;
1494cc4d3c30SLuigi Rizzo 				    mask->src_ip = ~0;
1495cc4d3c30SLuigi Rizzo 				    mask->dst_port = ~0;
1496cc4d3c30SLuigi Rizzo 				    mask->src_port = ~0;
1497cc4d3c30SLuigi Rizzo 				    mask->proto = ~0;
1498cc4d3c30SLuigi Rizzo 				    n2mask(&mask->dst_ip6, 128);
1499cc4d3c30SLuigi Rizzo 				    n2mask(&mask->src_ip6, 128);
1500cc4d3c30SLuigi Rizzo 				    mask->flow_id6 = ~0;
1501cc4d3c30SLuigi Rizzo 				    *flags |= DN_HAVE_MASK;
15024e9c8ae7SLuigi Rizzo 				    goto end_mask;
15034e9c8ae7SLuigi Rizzo 
1504f9f7bde3SLuigi Rizzo 			    case TOK_QUEUE:
1505f9f7bde3SLuigi Rizzo 				    mask->extra = ~0;
1506f9f7bde3SLuigi Rizzo 				    *flags |= DN_HAVE_MASK;
1507f9f7bde3SLuigi Rizzo 				    goto end_mask;
1508f9f7bde3SLuigi Rizzo 
15094e9c8ae7SLuigi Rizzo 			    case TOK_DSTIP:
1510cc4d3c30SLuigi Rizzo 				    mask->addr_type = 4;
1511cc4d3c30SLuigi Rizzo 				    p32 = &mask->dst_ip;
15124e9c8ae7SLuigi Rizzo 				    break;
15134e9c8ae7SLuigi Rizzo 
15144e9c8ae7SLuigi Rizzo 			    case TOK_SRCIP:
1515cc4d3c30SLuigi Rizzo 				    mask->addr_type = 4;
1516cc4d3c30SLuigi Rizzo 				    p32 = &mask->src_ip;
15174e9c8ae7SLuigi Rizzo 				    break;
15184e9c8ae7SLuigi Rizzo 
15194e9c8ae7SLuigi Rizzo 			    case TOK_DSTIP6:
1520cc4d3c30SLuigi Rizzo 				    mask->addr_type = 6;
1521cc4d3c30SLuigi Rizzo 				    pa6 = &mask->dst_ip6;
15224e9c8ae7SLuigi Rizzo 				    break;
15234e9c8ae7SLuigi Rizzo 
15244e9c8ae7SLuigi Rizzo 			    case TOK_SRCIP6:
1525cc4d3c30SLuigi Rizzo 				    mask->addr_type = 6;
1526cc4d3c30SLuigi Rizzo 				    pa6 = &mask->src_ip6;
15274e9c8ae7SLuigi Rizzo 				    break;
15284e9c8ae7SLuigi Rizzo 
15294e9c8ae7SLuigi Rizzo 			    case TOK_FLOWID:
1530cc4d3c30SLuigi Rizzo 				    mask->addr_type = 6;
1531cc4d3c30SLuigi Rizzo 				    p20 = &mask->flow_id6;
15324e9c8ae7SLuigi Rizzo 				    break;
15334e9c8ae7SLuigi Rizzo 
15344e9c8ae7SLuigi Rizzo 			    case TOK_DSTPORT:
1535cc4d3c30SLuigi Rizzo 				    p16 = &mask->dst_port;
15364e9c8ae7SLuigi Rizzo 				    break;
15374e9c8ae7SLuigi Rizzo 
15384e9c8ae7SLuigi Rizzo 			    case TOK_SRCPORT:
1539cc4d3c30SLuigi Rizzo 				    p16 = &mask->src_port;
15404e9c8ae7SLuigi Rizzo 				    break;
15414e9c8ae7SLuigi Rizzo 
15424e9c8ae7SLuigi Rizzo 			    case TOK_PROTO:
15434e9c8ae7SLuigi Rizzo 				    break;
15444e9c8ae7SLuigi Rizzo 
15454e9c8ae7SLuigi Rizzo 			    default:
15464e9c8ae7SLuigi Rizzo 				    ac++; av--; /* backtrack */
15474e9c8ae7SLuigi Rizzo 				    goto end_mask;
15484e9c8ae7SLuigi Rizzo 			    }
15494e9c8ae7SLuigi Rizzo 			    if (ac < 1)
15504e9c8ae7SLuigi Rizzo 				    errx(EX_USAGE, "mask: value missing");
15514e9c8ae7SLuigi Rizzo 			    if (*av[0] == '/') {
15524e9c8ae7SLuigi Rizzo 				    a = strtoul(av[0]+1, &end, 0);
15534e9c8ae7SLuigi Rizzo 				    if (pa6 == NULL)
15544e9c8ae7SLuigi Rizzo 					    a = (a == 32) ? ~0 : (1 << a) - 1;
15554e9c8ae7SLuigi Rizzo 			    } else
15564e9c8ae7SLuigi Rizzo 				    a = strtoul(av[0], &end, 0);
15574e9c8ae7SLuigi Rizzo 			    if (p32 != NULL)
15584e9c8ae7SLuigi Rizzo 				    *p32 = a;
15594e9c8ae7SLuigi Rizzo 			    else if (p16 != NULL) {
15604e9c8ae7SLuigi Rizzo 				    if (a > 0xFFFF)
15614e9c8ae7SLuigi Rizzo 					    errx(EX_DATAERR,
15624e9c8ae7SLuigi Rizzo 						"port mask must be 16 bit");
15634e9c8ae7SLuigi Rizzo 				    *p16 = (uint16_t)a;
15644e9c8ae7SLuigi Rizzo 			    } else if (p20 != NULL) {
15654e9c8ae7SLuigi Rizzo 				    if (a > 0xfffff)
15664e9c8ae7SLuigi Rizzo 					errx(EX_DATAERR,
15674e9c8ae7SLuigi Rizzo 					    "flow_id mask must be 20 bit");
15684e9c8ae7SLuigi Rizzo 				    *p20 = (uint32_t)a;
15694e9c8ae7SLuigi Rizzo 			    } else if (pa6 != NULL) {
15704e9c8ae7SLuigi Rizzo 				    if (a > 128)
15714e9c8ae7SLuigi Rizzo 					errx(EX_DATAERR,
15724e9c8ae7SLuigi Rizzo 					    "in6addr invalid mask len");
15734e9c8ae7SLuigi Rizzo 				    else
15744e9c8ae7SLuigi Rizzo 					n2mask(pa6, a);
15754e9c8ae7SLuigi Rizzo 			    } else {
15764e9c8ae7SLuigi Rizzo 				    if (a > 0xFF)
15774e9c8ae7SLuigi Rizzo 					    errx(EX_DATAERR,
15784e9c8ae7SLuigi Rizzo 						"proto mask must be 8 bit");
1579f9f7bde3SLuigi Rizzo 				    mask->proto = (uint8_t)a;
15804e9c8ae7SLuigi Rizzo 			    }
15814e9c8ae7SLuigi Rizzo 			    if (a != 0)
1582cc4d3c30SLuigi Rizzo 				    *flags |= DN_HAVE_MASK;
15834e9c8ae7SLuigi Rizzo 			    ac--; av++;
15844e9c8ae7SLuigi Rizzo 			} /* end while, config masks */
15854e9c8ae7SLuigi Rizzo end_mask:
15864e9c8ae7SLuigi Rizzo 			break;
158791336b40SDon Lewis #ifdef NEW_AQM
158891336b40SDon Lewis 		case TOK_CODEL:
158991336b40SDon Lewis 		case TOK_PIE:
159091336b40SDon Lewis 			NEED(fs, "codel/pie is only for flowsets");
15914e9c8ae7SLuigi Rizzo 
159291336b40SDon Lewis 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
159391336b40SDon Lewis 			fs->flags |= DN_IS_AQM;
159491336b40SDon Lewis 
159592b66dbeSAlan Somers 			strlcpy(aqm_extra->name, av[-1],
159692b66dbeSAlan Somers 			    sizeof(aqm_extra->name));
159791336b40SDon Lewis 			aqm_extra->oid.subtype = DN_AQM_PARAMS;
159891336b40SDon Lewis 
159991336b40SDon Lewis 			process_extra_parms(&ac, av, aqm_extra, tok);
160091336b40SDon Lewis 			break;
160191336b40SDon Lewis 
160291336b40SDon Lewis 		case TOK_FQ_CODEL:
160391336b40SDon Lewis 		case TOK_FQ_PIE:
160491336b40SDon Lewis 			if (!strcmp(av[-1],"type"))
160591336b40SDon Lewis 				errx(EX_DATAERR, "use type before fq_codel/fq_pie");
160691336b40SDon Lewis 
160791336b40SDon Lewis 			NEED(sch, "fq_codel/fq_pie is only for schd");
160892b66dbeSAlan Somers 			strlcpy(sch_extra->name, av[-1],
160992b66dbeSAlan Somers 			    sizeof(sch_extra->name));
161091336b40SDon Lewis 			sch_extra->oid.subtype = DN_SCH_PARAMS;
161191336b40SDon Lewis 			process_extra_parms(&ac, av, sch_extra, tok);
161291336b40SDon Lewis 			break;
161391336b40SDon Lewis #endif
16144e9c8ae7SLuigi Rizzo 		case TOK_RED:
16154e9c8ae7SLuigi Rizzo 		case TOK_GRED:
16164e9c8ae7SLuigi Rizzo 			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1617cc4d3c30SLuigi Rizzo 			fs->flags |= DN_IS_RED;
16184e9c8ae7SLuigi Rizzo 			if (tok == TOK_GRED)
1619cc4d3c30SLuigi Rizzo 				fs->flags |= DN_IS_GENTLE_RED;
16204e9c8ae7SLuigi Rizzo 			/*
16214e9c8ae7SLuigi Rizzo 			 * the format for parameters is w_q/min_th/max_th/max_p
16224e9c8ae7SLuigi Rizzo 			 */
16234e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
16244e9c8ae7SLuigi Rizzo 			    double w_q = strtod(end, NULL);
16254e9c8ae7SLuigi Rizzo 			    if (w_q > 1 || w_q <= 0)
16264e9c8ae7SLuigi Rizzo 				errx(EX_DATAERR, "0 < w_q <= 1");
1627cc4d3c30SLuigi Rizzo 			    fs->w_q = (int) (w_q * (1 << SCALE_RED));
16284e9c8ae7SLuigi Rizzo 			}
16294e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
1630cc4d3c30SLuigi Rizzo 			    fs->min_th = strtoul(end, &end, 0);
16314e9c8ae7SLuigi Rizzo 			    if (*end == 'K' || *end == 'k')
1632cc4d3c30SLuigi Rizzo 				fs->min_th *= 1024;
16334e9c8ae7SLuigi Rizzo 			}
16344e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
1635cc4d3c30SLuigi Rizzo 			    fs->max_th = strtoul(end, &end, 0);
16364e9c8ae7SLuigi Rizzo 			    if (*end == 'K' || *end == 'k')
1637cc4d3c30SLuigi Rizzo 				fs->max_th *= 1024;
16384e9c8ae7SLuigi Rizzo 			}
16394e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
16404e9c8ae7SLuigi Rizzo 			    double max_p = strtod(end, NULL);
1641fc5e1956SHiren Panchasara 			    if (max_p > 1 || max_p < 0)
1642fc5e1956SHiren Panchasara 				errx(EX_DATAERR, "0 <= max_p <= 1");
1643cc4d3c30SLuigi Rizzo 			    fs->max_p = (int)(max_p * (1 << SCALE_RED));
16444e9c8ae7SLuigi Rizzo 			}
16454e9c8ae7SLuigi Rizzo 			ac--; av++;
16464e9c8ae7SLuigi Rizzo 			break;
16474e9c8ae7SLuigi Rizzo 
1648fc5e1956SHiren Panchasara 		case TOK_ECN:
1649fc5e1956SHiren Panchasara 			fs->flags |= DN_IS_ECN;
1650fc5e1956SHiren Panchasara 			break;
1651fc5e1956SHiren Panchasara 
16524e9c8ae7SLuigi Rizzo 		case TOK_DROPTAIL:
1653cc4d3c30SLuigi Rizzo 			NEED(fs, "droptail is only for flowsets");
1654cc4d3c30SLuigi Rizzo 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
16554e9c8ae7SLuigi Rizzo 			break;
16564e9c8ae7SLuigi Rizzo 
16574e9c8ae7SLuigi Rizzo 		case TOK_BW:
1658cc4d3c30SLuigi Rizzo 			NEED(p, "bw is only for links");
16594e9c8ae7SLuigi Rizzo 			NEED1("bw needs bandwidth or interface\n");
1660cc4d3c30SLuigi Rizzo 			read_bandwidth(av[0], &p->bandwidth, NULL, 0);
16614e9c8ae7SLuigi Rizzo 			ac--; av++;
16624e9c8ae7SLuigi Rizzo 			break;
16634e9c8ae7SLuigi Rizzo 
16644e9c8ae7SLuigi Rizzo 		case TOK_DELAY:
1665cc4d3c30SLuigi Rizzo 			NEED(p, "delay is only for links");
16664e9c8ae7SLuigi Rizzo 			NEED1("delay needs argument 0..10000ms\n");
1667cc4d3c30SLuigi Rizzo 			p->delay = strtoul(av[0], NULL, 0);
16684e9c8ae7SLuigi Rizzo 			ac--; av++;
16694e9c8ae7SLuigi Rizzo 			break;
16704e9c8ae7SLuigi Rizzo 
1671cc4d3c30SLuigi Rizzo 		case TOK_TYPE: {
1672cc4d3c30SLuigi Rizzo 			int l;
1673cc4d3c30SLuigi Rizzo 			NEED(sch, "type is only for schedulers");
1674cc4d3c30SLuigi Rizzo 			NEED1("type needs a string");
1675cc4d3c30SLuigi Rizzo 			l = strlen(av[0]);
1676cc4d3c30SLuigi Rizzo 			if (l == 0 || l > 15)
1677cc4d3c30SLuigi Rizzo 				errx(1, "type %s too long\n", av[0]);
167892b66dbeSAlan Somers 			strlcpy(sch->name, av[0], sizeof(sch->name));
1679cc4d3c30SLuigi Rizzo 			sch->oid.subtype = 0; /* use string */
168091336b40SDon Lewis #ifdef NEW_AQM
168191336b40SDon Lewis 			/* if fq_codel is selected, consider all tokens after it
168291336b40SDon Lewis 			 * as parameters
168391336b40SDon Lewis 			 */
168491336b40SDon Lewis 			if (!strcasecmp(av[0],"fq_codel") || !strcasecmp(av[0],"fq_pie")){
168592b66dbeSAlan Somers 				strlcpy(sch_extra->name, av[0],
168692b66dbeSAlan Somers 				    sizeof(sch_extra->name));
168791336b40SDon Lewis 				sch_extra->oid.subtype = DN_SCH_PARAMS;
168891336b40SDon Lewis 				process_extra_parms(&ac, av, sch_extra, tok);
168991336b40SDon Lewis 			} else {
1690cc4d3c30SLuigi Rizzo 				ac--;av++;
169191336b40SDon Lewis 			}
169291336b40SDon Lewis #else
169391336b40SDon Lewis 			ac--;av++;
169491336b40SDon Lewis #endif
1695cc4d3c30SLuigi Rizzo 			break;
1696cc4d3c30SLuigi Rizzo 		    }
1697cc4d3c30SLuigi Rizzo 
16984e9c8ae7SLuigi Rizzo 		case TOK_WEIGHT:
1699cc4d3c30SLuigi Rizzo 			NEED(fs, "weight is only for flowsets");
1700cc4d3c30SLuigi Rizzo 			NEED1("weight needs argument\n");
1701cc4d3c30SLuigi Rizzo 			fs->par[0] = strtol(av[0], &end, 0);
17024e9c8ae7SLuigi Rizzo 			ac--; av++;
17034e9c8ae7SLuigi Rizzo 			break;
17044e9c8ae7SLuigi Rizzo 
1705cc4d3c30SLuigi Rizzo 		case TOK_LMAX:
1706cc4d3c30SLuigi Rizzo 			NEED(fs, "lmax is only for flowsets");
1707cc4d3c30SLuigi Rizzo 			NEED1("lmax needs argument\n");
1708cc4d3c30SLuigi Rizzo 			fs->par[1] = strtol(av[0], &end, 0);
1709cc4d3c30SLuigi Rizzo 			ac--; av++;
1710cc4d3c30SLuigi Rizzo 			break;
1711cc4d3c30SLuigi Rizzo 
1712cc4d3c30SLuigi Rizzo 		case TOK_PRI:
1713cc4d3c30SLuigi Rizzo 			NEED(fs, "priority is only for flowsets");
1714cc4d3c30SLuigi Rizzo 			NEED1("priority needs argument\n");
1715cc4d3c30SLuigi Rizzo 			fs->par[2] = strtol(av[0], &end, 0);
1716cc4d3c30SLuigi Rizzo 			ac--; av++;
1717cc4d3c30SLuigi Rizzo 			break;
1718cc4d3c30SLuigi Rizzo 
1719cc4d3c30SLuigi Rizzo 		case TOK_SCHED:
17204e9c8ae7SLuigi Rizzo 		case TOK_PIPE:
1721cc4d3c30SLuigi Rizzo 			NEED(fs, "pipe/sched");
1722cc4d3c30SLuigi Rizzo 			NEED1("pipe/link/sched needs number\n");
1723cc4d3c30SLuigi Rizzo 			fs->sched_nr = strtoul(av[0], &end, 0);
17244e9c8ae7SLuigi Rizzo 			ac--; av++;
17254e9c8ae7SLuigi Rizzo 			break;
17264e9c8ae7SLuigi Rizzo 
1727cc4d3c30SLuigi Rizzo 		case TOK_PROFILE:
1728cc4d3c30SLuigi Rizzo 			NEED((!pf), "profile already set");
1729cc4d3c30SLuigi Rizzo 			NEED(p, "profile");
1730cc4d3c30SLuigi Rizzo 		    {
17314bb7ae9dSLuigi Rizzo 			NEED1("extra delay needs the file name\n");
1732cc4d3c30SLuigi Rizzo 			pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1733cc4d3c30SLuigi Rizzo 			load_extra_delays(av[0], pf, p); //XXX can't fail?
17344bb7ae9dSLuigi Rizzo 			--ac; ++av;
1735cc4d3c30SLuigi Rizzo 		    }
17364bb7ae9dSLuigi Rizzo 			break;
17374bb7ae9dSLuigi Rizzo 
17386882bf4dSOleg Bulyzhin 		case TOK_BURST:
1739cc4d3c30SLuigi Rizzo 			NEED(p, "burst");
17406882bf4dSOleg Bulyzhin 			NEED1("burst needs argument\n");
17416882bf4dSOleg Bulyzhin 			errno = 0;
17421727cdb6SDag-Erling Smørgrav 			if (expand_number(av[0], &p->burst) < 0)
17436882bf4dSOleg Bulyzhin 				if (errno != ERANGE)
17446882bf4dSOleg Bulyzhin 					errx(EX_DATAERR,
17456882bf4dSOleg Bulyzhin 					    "burst: invalid argument");
1746cc4d3c30SLuigi Rizzo 			if (errno || p->burst > (1ULL << 48) - 1)
17476882bf4dSOleg Bulyzhin 				errx(EX_DATAERR,
17486882bf4dSOleg Bulyzhin 				    "burst: out of range (0..2^48-1)");
17496882bf4dSOleg Bulyzhin 			ac--; av++;
17506882bf4dSOleg Bulyzhin 			break;
17516882bf4dSOleg Bulyzhin 
17524e9c8ae7SLuigi Rizzo 		default:
17534e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
17544e9c8ae7SLuigi Rizzo 		}
17554e9c8ae7SLuigi Rizzo 	}
1756cc4d3c30SLuigi Rizzo 
1757cc4d3c30SLuigi Rizzo 	/* check validity of parameters */
1758cc4d3c30SLuigi Rizzo 	if (p) {
1759cc4d3c30SLuigi Rizzo 		if (p->delay > 10000)
17604e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "delay must be < 10000");
176120ffd88eSLuiz Otavio O Souza 		if (p->bandwidth == (uint32_t)-1)
1762cc4d3c30SLuigi Rizzo 			p->bandwidth = 0;
17634e9c8ae7SLuigi Rizzo 	}
1764cc4d3c30SLuigi Rizzo 	if (fs) {
1765cc4d3c30SLuigi Rizzo 		/* XXX accept a 0 scheduler to keep the default */
1766cc4d3c30SLuigi Rizzo 	    if (fs->flags & DN_QSIZE_BYTES) {
17674e9c8ae7SLuigi Rizzo 		size_t len;
17684e9c8ae7SLuigi Rizzo 		long limit;
17694e9c8ae7SLuigi Rizzo 
17704e9c8ae7SLuigi Rizzo 		len = sizeof(limit);
17714e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
17724e9c8ae7SLuigi Rizzo 			&limit, &len, NULL, 0) == -1)
17734e9c8ae7SLuigi Rizzo 			limit = 1024*1024;
1774cc4d3c30SLuigi Rizzo 		if (fs->qsize > limit)
17754e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "queue size must be < %ldB", limit);
17764e9c8ae7SLuigi Rizzo 	    } else {
17774e9c8ae7SLuigi Rizzo 		size_t len;
17784e9c8ae7SLuigi Rizzo 		long limit;
17794e9c8ae7SLuigi Rizzo 
17804e9c8ae7SLuigi Rizzo 		len = sizeof(limit);
17814e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
17824e9c8ae7SLuigi Rizzo 			&limit, &len, NULL, 0) == -1)
17834e9c8ae7SLuigi Rizzo 			limit = 100;
1784cc4d3c30SLuigi Rizzo 		if (fs->qsize > limit)
17854e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
17864e9c8ae7SLuigi Rizzo 	    }
1787cc4d3c30SLuigi Rizzo 
178891336b40SDon Lewis #ifdef NEW_AQM
178991336b40SDon Lewis 		if ((fs->flags & DN_IS_ECN) && !((fs->flags & DN_IS_RED)||
179091336b40SDon Lewis 			(fs->flags & DN_IS_AQM)))
179191336b40SDon Lewis 			errx(EX_USAGE, "ECN can be used with red/gred/"
179291336b40SDon Lewis 				"codel/fq_codel only!");
179391336b40SDon Lewis #else
1794fc5e1956SHiren Panchasara 	    if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1795fc5e1956SHiren Panchasara 		errx(EX_USAGE, "enable red/gred for ECN");
1796fc5e1956SHiren Panchasara 
179791336b40SDon Lewis #endif
179891336b40SDon Lewis 
1799cc4d3c30SLuigi Rizzo 	    if (fs->flags & DN_IS_RED) {
18004e9c8ae7SLuigi Rizzo 		size_t len;
18014e9c8ae7SLuigi Rizzo 		int lookup_depth, avg_pkt_size;
18024e9c8ae7SLuigi Rizzo 
1803fc5e1956SHiren Panchasara 		if (!(fs->flags & DN_IS_ECN) && (fs->min_th >= fs->max_th))
18044e9c8ae7SLuigi Rizzo 		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1805cc4d3c30SLuigi Rizzo 			fs->min_th, fs->max_th);
1806fc5e1956SHiren Panchasara 		else if ((fs->flags & DN_IS_ECN) && (fs->min_th > fs->max_th))
1807fc5e1956SHiren Panchasara 		    errx(EX_DATAERR, "min_th %d must be =< than max_th %d",
1808fc5e1956SHiren Panchasara 			fs->min_th, fs->max_th);
1809fc5e1956SHiren Panchasara 
1810cc4d3c30SLuigi Rizzo 		if (fs->max_th == 0)
18114e9c8ae7SLuigi Rizzo 		    errx(EX_DATAERR, "max_th must be > 0");
18124e9c8ae7SLuigi Rizzo 
18134e9c8ae7SLuigi Rizzo 		len = sizeof(int);
18144e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
18154e9c8ae7SLuigi Rizzo 			&lookup_depth, &len, NULL, 0) == -1)
1816cc4d3c30SLuigi Rizzo 			lookup_depth = 256;
18174e9c8ae7SLuigi Rizzo 		if (lookup_depth == 0)
18184e9c8ae7SLuigi Rizzo 		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
18194e9c8ae7SLuigi Rizzo 			" must be greater than zero");
18204e9c8ae7SLuigi Rizzo 
18214e9c8ae7SLuigi Rizzo 		len = sizeof(int);
18224e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
18234e9c8ae7SLuigi Rizzo 			&avg_pkt_size, &len, NULL, 0) == -1)
1824cc4d3c30SLuigi Rizzo 			avg_pkt_size = 512;
18254e9c8ae7SLuigi Rizzo 
18264e9c8ae7SLuigi Rizzo 		if (avg_pkt_size == 0)
18274e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR,
18284e9c8ae7SLuigi Rizzo 			    "net.inet.ip.dummynet.red_avg_pkt_size must"
18294e9c8ae7SLuigi Rizzo 			    " be greater than zero");
18304e9c8ae7SLuigi Rizzo 
1831fa8d2a59SLuigi Rizzo #if 0 /* the following computation is now done in the kernel */
18324e9c8ae7SLuigi Rizzo 		/*
18334e9c8ae7SLuigi Rizzo 		 * Ticks needed for sending a medium-sized packet.
18344e9c8ae7SLuigi Rizzo 		 * Unfortunately, when we are configuring a WF2Q+ queue, we
18354e9c8ae7SLuigi Rizzo 		 * do not have bandwidth information, because that is stored
18364e9c8ae7SLuigi Rizzo 		 * in the parent pipe, and also we have multiple queues
18374e9c8ae7SLuigi Rizzo 		 * competing for it. So we set s=0, which is not very
18384e9c8ae7SLuigi Rizzo 		 * correct. But on the other hand, why do we want RED with
18394e9c8ae7SLuigi Rizzo 		 * WF2Q+ ?
18404e9c8ae7SLuigi Rizzo 		 */
18414e9c8ae7SLuigi Rizzo 		if (p.bandwidth==0) /* this is a WF2Q+ queue */
18424e9c8ae7SLuigi Rizzo 			s = 0;
18434e9c8ae7SLuigi Rizzo 		else
18444e9c8ae7SLuigi Rizzo 			s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
18454e9c8ae7SLuigi Rizzo 		/*
18464e9c8ae7SLuigi Rizzo 		 * max idle time (in ticks) before avg queue size becomes 0.
18474e9c8ae7SLuigi Rizzo 		 * NOTA:  (3/w_q) is approx the value x so that
18484e9c8ae7SLuigi Rizzo 		 * (1-w_q)^x < 10^-3.
18494e9c8ae7SLuigi Rizzo 		 */
1850cc4d3c30SLuigi Rizzo 		w_q = ((double)fs->w_q) / (1 << SCALE_RED);
18514e9c8ae7SLuigi Rizzo 		idle = s * 3. / w_q;
1852cc4d3c30SLuigi Rizzo 		fs->lookup_step = (int)idle / lookup_depth;
1853cc4d3c30SLuigi Rizzo 		if (!fs->lookup_step)
1854cc4d3c30SLuigi Rizzo 			fs->lookup_step = 1;
18554e9c8ae7SLuigi Rizzo 		weight = 1 - w_q;
1856cc4d3c30SLuigi Rizzo 		for (t = fs->lookup_step; t > 1; --t)
18574e9c8ae7SLuigi Rizzo 			weight *= 1 - w_q;
1858cc4d3c30SLuigi Rizzo 		fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1859fa8d2a59SLuigi Rizzo #endif /* code moved in the kernel */
18604e9c8ae7SLuigi Rizzo 	    }
18614bb7ae9dSLuigi Rizzo 	}
18624bb7ae9dSLuigi Rizzo 
1863cc4d3c30SLuigi Rizzo 	i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1864cc4d3c30SLuigi Rizzo 
18654e9c8ae7SLuigi Rizzo 	if (i)
18664e9c8ae7SLuigi Rizzo 		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
18674e9c8ae7SLuigi Rizzo }
1868cc4d3c30SLuigi Rizzo 
1869cc4d3c30SLuigi Rizzo void
1870cc4d3c30SLuigi Rizzo dummynet_flush(void)
1871cc4d3c30SLuigi Rizzo {
1872cc4d3c30SLuigi Rizzo 	struct dn_id oid;
1873cc4d3c30SLuigi Rizzo 	oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1874cc4d3c30SLuigi Rizzo 	do_cmd(IP_DUMMYNET3, &oid, oid.len);
1875cc4d3c30SLuigi Rizzo }
1876cc4d3c30SLuigi Rizzo 
18775007b59fSLuigi Rizzo /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
18785007b59fSLuigi Rizzo  * Returns the number of ranges, and possibly stores them
18795007b59fSLuigi Rizzo  * in the array v of size len.
18805007b59fSLuigi Rizzo  */
18815007b59fSLuigi Rizzo static int
18825007b59fSLuigi Rizzo parse_range(int ac, char *av[], uint32_t *v, int len)
18835007b59fSLuigi Rizzo {
18845007b59fSLuigi Rizzo 	int n = 0;
18855007b59fSLuigi Rizzo 	char *endptr, *s;
18865007b59fSLuigi Rizzo 	uint32_t base[2];
18875007b59fSLuigi Rizzo 
18885007b59fSLuigi Rizzo 	if (v == NULL || len < 2) {
18895007b59fSLuigi Rizzo 		v = base;
18905007b59fSLuigi Rizzo 		len = 2;
18915007b59fSLuigi Rizzo 	}
18925007b59fSLuigi Rizzo 
18935007b59fSLuigi Rizzo 	for (s = *av; s != NULL; av++, ac--) {
18945007b59fSLuigi Rizzo 		v[0] = strtoul(s, &endptr, 10);
18955007b59fSLuigi Rizzo 		v[1] = (*endptr != '-') ? v[0] :
18965007b59fSLuigi Rizzo 			 strtoul(endptr+1, &endptr, 10);
18975007b59fSLuigi Rizzo 		if (*endptr == '\0') { /* prepare for next round */
18985007b59fSLuigi Rizzo 			s = (ac > 0) ? *(av+1) : NULL;
18995007b59fSLuigi Rizzo 		} else {
19005007b59fSLuigi Rizzo 			if (*endptr != ',') {
19015007b59fSLuigi Rizzo 				warn("invalid number: %s", s);
19025007b59fSLuigi Rizzo 				s = ++endptr;
19035007b59fSLuigi Rizzo 				continue;
19045007b59fSLuigi Rizzo 			}
19055007b59fSLuigi Rizzo 			/* continue processing from here */
19065007b59fSLuigi Rizzo 			s = ++endptr;
19075007b59fSLuigi Rizzo 			ac++;
19085007b59fSLuigi Rizzo 			av--;
19095007b59fSLuigi Rizzo 		}
19105007b59fSLuigi Rizzo 		if (v[1] < v[0] ||
191116804dc7SAndrey V. Elsukov 			v[0] >= DN_MAX_ID-1 ||
19126a7bb02dSLuigi Rizzo 			v[1] >= DN_MAX_ID-1) {
19135007b59fSLuigi Rizzo 			continue; /* invalid entry */
19145007b59fSLuigi Rizzo 		}
19155007b59fSLuigi Rizzo 		n++;
19165007b59fSLuigi Rizzo 		/* translate if 'pipe list' */
191756707beeSMark Johnston 		if (g_co.do_pipe == 1) {
19185007b59fSLuigi Rizzo 			v[0] += DN_MAX_ID;
19195007b59fSLuigi Rizzo 			v[1] += DN_MAX_ID;
19205007b59fSLuigi Rizzo 		}
19215007b59fSLuigi Rizzo 		v = (n*2 < len) ? v + 2 : base;
19225007b59fSLuigi Rizzo 	}
19235007b59fSLuigi Rizzo 	return n;
19245007b59fSLuigi Rizzo }
19255007b59fSLuigi Rizzo 
1926cc4d3c30SLuigi Rizzo /* main entry point for dummynet list functions. co.do_pipe indicates
1927cc4d3c30SLuigi Rizzo  * which function we want to support.
19285007b59fSLuigi Rizzo  * av may contain filtering arguments, either individual entries
19295007b59fSLuigi Rizzo  * or ranges, or lists (space or commas are valid separators).
19305007b59fSLuigi Rizzo  * Format for a range can be n1-n2 or n3 n4 n5 ...
19315007b59fSLuigi Rizzo  * In a range n1 must be <= n2, otherwise the range is ignored.
19325007b59fSLuigi Rizzo  * A number 'n4' is translate in a range 'n4-n4'
19335007b59fSLuigi Rizzo  * All number must be > 0 and < DN_MAX_ID-1
1934cc4d3c30SLuigi Rizzo  */
1935cc4d3c30SLuigi Rizzo void
1936cc4d3c30SLuigi Rizzo dummynet_list(int ac, char *av[], int show_counters)
1937cc4d3c30SLuigi Rizzo {
19385007b59fSLuigi Rizzo 	struct dn_id *oid, *x = NULL;
19396a7bb02dSLuigi Rizzo 	int ret, i;
19405007b59fSLuigi Rizzo 	int n; 		/* # of ranges */
19416a7bb02dSLuigi Rizzo 	u_int buflen, l;
19426a7bb02dSLuigi Rizzo 	u_int max_size;	/* largest obj passed up */
1943cc4d3c30SLuigi Rizzo 
19446a7bb02dSLuigi Rizzo 	(void)show_counters;	// XXX unused, but we should use it.
19455007b59fSLuigi Rizzo 	ac--;
19465007b59fSLuigi Rizzo 	av++; 		/* skip 'list' | 'show' word */
19475007b59fSLuigi Rizzo 
19485007b59fSLuigi Rizzo 	n = parse_range(ac, av, NULL, 0);	/* Count # of ranges. */
19495007b59fSLuigi Rizzo 
19505007b59fSLuigi Rizzo 	/* Allocate space to store ranges */
19515007b59fSLuigi Rizzo 	l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
19525007b59fSLuigi Rizzo 	oid = safe_calloc(1, l);
19535007b59fSLuigi Rizzo 	oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
19545007b59fSLuigi Rizzo 
19555007b59fSLuigi Rizzo 	if (n > 0)	/* store ranges in idx */
19565007b59fSLuigi Rizzo 		parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
19575007b59fSLuigi Rizzo 	/*
19585007b59fSLuigi Rizzo 	 * Compute the size of the largest object returned. If the
19595007b59fSLuigi Rizzo 	 * response leaves at least this much spare space in the
19605007b59fSLuigi Rizzo 	 * buffer, then surely the response is complete; otherwise
19615007b59fSLuigi Rizzo 	 * there might be a risk of truncation and we will need to
19625007b59fSLuigi Rizzo 	 * retry with a larger buffer.
19635007b59fSLuigi Rizzo 	 * XXX don't bother with smaller structs.
19645007b59fSLuigi Rizzo 	 */
19655007b59fSLuigi Rizzo 	max_size = sizeof(struct dn_fs);
19665007b59fSLuigi Rizzo 	if (max_size < sizeof(struct dn_sch))
19675007b59fSLuigi Rizzo 		max_size = sizeof(struct dn_sch);
19685007b59fSLuigi Rizzo 	if (max_size < sizeof(struct dn_flow))
19695007b59fSLuigi Rizzo 		max_size = sizeof(struct dn_flow);
19705007b59fSLuigi Rizzo 
197156707beeSMark Johnston 	switch (g_co.do_pipe) {
1972cc4d3c30SLuigi Rizzo 	case 1:
19735007b59fSLuigi Rizzo 		oid->subtype = DN_LINK;	/* list pipe */
1974cc4d3c30SLuigi Rizzo 		break;
1975cc4d3c30SLuigi Rizzo 	case 2:
19765007b59fSLuigi Rizzo 		oid->subtype = DN_FS;	/* list queue */
1977cc4d3c30SLuigi Rizzo 		break;
1978cc4d3c30SLuigi Rizzo 	case 3:
19795007b59fSLuigi Rizzo 		oid->subtype = DN_SCH;	/* list sched */
1980cc4d3c30SLuigi Rizzo 		break;
1981cc4d3c30SLuigi Rizzo 	}
1982f10f583fSLuigi Rizzo 
19835007b59fSLuigi Rizzo 	/*
19845007b59fSLuigi Rizzo 	 * Ask the kernel an estimate of the required space (result
19855007b59fSLuigi Rizzo 	 * in oid.id), unless we are requesting a subset of objects,
19865007b59fSLuigi Rizzo 	 * in which case the kernel does not give an exact answer.
19875007b59fSLuigi Rizzo 	 * In any case, space might grow in the meantime due to the
19885007b59fSLuigi Rizzo 	 * creation of new queues, so we must be prepared to retry.
1989f10f583fSLuigi Rizzo 	 */
19905007b59fSLuigi Rizzo 	if (n > 0) {
19915007b59fSLuigi Rizzo 		buflen = 4*1024;
19925007b59fSLuigi Rizzo 	} else {
19935007b59fSLuigi Rizzo 		ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
19945007b59fSLuigi Rizzo 		if (ret != 0 || oid->id <= sizeof(*oid))
19955007b59fSLuigi Rizzo 			goto done;
19965007b59fSLuigi Rizzo 		buflen = oid->id + max_size;
19975007b59fSLuigi Rizzo 		oid->len = sizeof(*oid); /* restore */
1998f10f583fSLuigi Rizzo 	}
19995007b59fSLuigi Rizzo 	/* Try a few times, until the buffer fits */
20005007b59fSLuigi Rizzo 	for (i = 0; i < 20; i++) {
20015007b59fSLuigi Rizzo 		l = buflen;
20025007b59fSLuigi Rizzo 		x = safe_realloc(x, l);
20035007b59fSLuigi Rizzo 		bcopy(oid, x, oid->len);
20045007b59fSLuigi Rizzo 		ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
20055007b59fSLuigi Rizzo 		if (ret != 0 || x->id <= sizeof(*oid))
20065007b59fSLuigi Rizzo 			goto done; /* no response */
20075007b59fSLuigi Rizzo 		if (l + max_size <= buflen)
20085007b59fSLuigi Rizzo 			break; /* ok */
20095007b59fSLuigi Rizzo 		buflen *= 2;	 /* double for next attempt */
20105007b59fSLuigi Rizzo 	}
2011cc4d3c30SLuigi Rizzo 	list_pipes(x, O_NEXT(x, l));
20125007b59fSLuigi Rizzo done:
20135007b59fSLuigi Rizzo 	if (x)
2014cc4d3c30SLuigi Rizzo 		free(x);
20155007b59fSLuigi Rizzo 	free(oid);
2016cc4d3c30SLuigi Rizzo }
2017