xref: /freebsd/sbin/ipfw/dummynet.c (revision fc5e1956d95ab48296b6733a21fcaf1996476fcb)
14e9c8ae7SLuigi Rizzo /*
2cc4d3c30SLuigi Rizzo  * Copyright (c) 2002-2003,2010 Luigi Rizzo
34e9c8ae7SLuigi Rizzo  *
44e9c8ae7SLuigi Rizzo  * Redistribution and use in source forms, with and without modification,
54e9c8ae7SLuigi Rizzo  * are permitted provided that this entire comment appears intact.
64e9c8ae7SLuigi Rizzo  *
74e9c8ae7SLuigi Rizzo  * Redistribution in binary form may occur without any restrictions.
84e9c8ae7SLuigi Rizzo  * Obviously, it would be nice if you gave credit where credit is due
94e9c8ae7SLuigi Rizzo  * but requiring it would be too onerous.
104e9c8ae7SLuigi Rizzo  *
114e9c8ae7SLuigi Rizzo  * This software is provided ``AS IS'' without any warranties of any kind.
124e9c8ae7SLuigi Rizzo  *
134e9c8ae7SLuigi Rizzo  * $FreeBSD$
144e9c8ae7SLuigi Rizzo  *
154e9c8ae7SLuigi Rizzo  * dummynet support
164e9c8ae7SLuigi Rizzo  */
174e9c8ae7SLuigi Rizzo 
184e9c8ae7SLuigi Rizzo #include <sys/types.h>
194e9c8ae7SLuigi Rizzo #include <sys/socket.h>
2023c608c8SLuigi Rizzo /* XXX there are several sysctl leftover here */
214e9c8ae7SLuigi Rizzo #include <sys/sysctl.h>
224e9c8ae7SLuigi Rizzo 
234e9c8ae7SLuigi Rizzo #include "ipfw2.h"
244e9c8ae7SLuigi Rizzo 
254e9c8ae7SLuigi Rizzo #include <ctype.h>
264e9c8ae7SLuigi Rizzo #include <err.h>
276882bf4dSOleg Bulyzhin #include <errno.h>
286882bf4dSOleg Bulyzhin #include <libutil.h>
294e9c8ae7SLuigi Rizzo #include <netdb.h>
304e9c8ae7SLuigi Rizzo #include <stdio.h>
314e9c8ae7SLuigi Rizzo #include <stdlib.h>
324e9c8ae7SLuigi Rizzo #include <string.h>
334e9c8ae7SLuigi Rizzo #include <sysexits.h>
344e9c8ae7SLuigi Rizzo 
354e9c8ae7SLuigi Rizzo #include <net/if.h>
364e9c8ae7SLuigi Rizzo #include <netinet/in.h>
374e9c8ae7SLuigi Rizzo #include <netinet/ip_fw.h>
384e9c8ae7SLuigi Rizzo #include <netinet/ip_dummynet.h>
3923c608c8SLuigi Rizzo #include <arpa/inet.h>	/* inet_ntoa */
404e9c8ae7SLuigi Rizzo 
41cc4d3c30SLuigi Rizzo 
424e9c8ae7SLuigi Rizzo static struct _s_x dummynet_params[] = {
434e9c8ae7SLuigi Rizzo 	{ "plr",		TOK_PLR },
444e9c8ae7SLuigi Rizzo 	{ "noerror",		TOK_NOERROR },
454e9c8ae7SLuigi Rizzo 	{ "buckets",		TOK_BUCKETS },
464e9c8ae7SLuigi Rizzo 	{ "dst-ip",		TOK_DSTIP },
474e9c8ae7SLuigi Rizzo 	{ "src-ip",		TOK_SRCIP },
484e9c8ae7SLuigi Rizzo 	{ "dst-port",		TOK_DSTPORT },
494e9c8ae7SLuigi Rizzo 	{ "src-port",		TOK_SRCPORT },
504e9c8ae7SLuigi Rizzo 	{ "proto",		TOK_PROTO },
514e9c8ae7SLuigi Rizzo 	{ "weight",		TOK_WEIGHT },
52cc4d3c30SLuigi Rizzo 	{ "lmax",		TOK_LMAX },
53cc4d3c30SLuigi Rizzo 	{ "maxlen",		TOK_LMAX },
544e9c8ae7SLuigi Rizzo 	{ "all",		TOK_ALL },
55cc4d3c30SLuigi Rizzo 	{ "mask",		TOK_MASK }, /* alias for both */
56cc4d3c30SLuigi Rizzo 	{ "sched_mask",		TOK_SCHED_MASK },
57cc4d3c30SLuigi Rizzo 	{ "flow_mask",		TOK_FLOW_MASK },
584e9c8ae7SLuigi Rizzo 	{ "droptail",		TOK_DROPTAIL },
59*fc5e1956SHiren Panchasara 	{ "ecn",		TOK_ECN },
604e9c8ae7SLuigi Rizzo 	{ "red",		TOK_RED },
614e9c8ae7SLuigi Rizzo 	{ "gred",		TOK_GRED },
624e9c8ae7SLuigi Rizzo 	{ "bw",			TOK_BW },
634e9c8ae7SLuigi Rizzo 	{ "bandwidth",		TOK_BW },
644e9c8ae7SLuigi Rizzo 	{ "delay",		TOK_DELAY },
65cc4d3c30SLuigi Rizzo 	{ "link",		TOK_LINK },
664e9c8ae7SLuigi Rizzo 	{ "pipe",		TOK_PIPE },
674e9c8ae7SLuigi Rizzo 	{ "queue",		TOK_QUEUE },
68cc4d3c30SLuigi Rizzo 	{ "flowset",		TOK_FLOWSET },
69cc4d3c30SLuigi Rizzo 	{ "sched",		TOK_SCHED },
70cc4d3c30SLuigi Rizzo 	{ "pri",		TOK_PRI },
71cc4d3c30SLuigi Rizzo 	{ "priority",		TOK_PRI },
72cc4d3c30SLuigi Rizzo 	{ "type",		TOK_TYPE },
734e9c8ae7SLuigi Rizzo 	{ "flow-id",		TOK_FLOWID},
744e9c8ae7SLuigi Rizzo 	{ "dst-ipv6",		TOK_DSTIP6},
754e9c8ae7SLuigi Rizzo 	{ "dst-ip6",		TOK_DSTIP6},
764e9c8ae7SLuigi Rizzo 	{ "src-ipv6",		TOK_SRCIP6},
774e9c8ae7SLuigi Rizzo 	{ "src-ip6",		TOK_SRCIP6},
78cc4d3c30SLuigi Rizzo 	{ "profile",		TOK_PROFILE},
796882bf4dSOleg Bulyzhin 	{ "burst",		TOK_BURST},
804e9c8ae7SLuigi Rizzo 	{ "dummynet-params",	TOK_NULL },
814e9c8ae7SLuigi Rizzo 	{ NULL, 0 }	/* terminator */
824e9c8ae7SLuigi Rizzo };
834e9c8ae7SLuigi Rizzo 
84cc4d3c30SLuigi Rizzo #define O_NEXT(p, len) ((void *)((char *)p + len))
85cc4d3c30SLuigi Rizzo 
86cc4d3c30SLuigi Rizzo static void
87cc4d3c30SLuigi Rizzo oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
88cc4d3c30SLuigi Rizzo {
89cc4d3c30SLuigi Rizzo 	oid->len = len;
90cc4d3c30SLuigi Rizzo 	oid->type = type;
91cc4d3c30SLuigi Rizzo 	oid->subtype = 0;
92cc4d3c30SLuigi Rizzo 	oid->id = id;
93cc4d3c30SLuigi Rizzo }
94cc4d3c30SLuigi Rizzo 
95cc4d3c30SLuigi Rizzo /* make room in the buffer and move the pointer forward */
96cc4d3c30SLuigi Rizzo static void *
97cc4d3c30SLuigi Rizzo o_next(struct dn_id **o, int len, int type)
98cc4d3c30SLuigi Rizzo {
99cc4d3c30SLuigi Rizzo 	struct dn_id *ret = *o;
100cc4d3c30SLuigi Rizzo 	oid_fill(ret, len, type, 0);
101cc4d3c30SLuigi Rizzo 	*o = O_NEXT(*o, len);
102cc4d3c30SLuigi Rizzo 	return ret;
103cc4d3c30SLuigi Rizzo }
104cc4d3c30SLuigi Rizzo 
105cc4d3c30SLuigi Rizzo #if 0
1064e9c8ae7SLuigi Rizzo static int
10701ab7632SLuigi Rizzo sort_q(void *arg, const void *pa, const void *pb)
1084e9c8ae7SLuigi Rizzo {
1094e9c8ae7SLuigi Rizzo 	int rev = (co.do_sort < 0);
1104e9c8ae7SLuigi Rizzo 	int field = rev ? -co.do_sort : co.do_sort;
1114e9c8ae7SLuigi Rizzo 	long long res = 0;
1124e9c8ae7SLuigi Rizzo 	const struct dn_flow_queue *a = pa;
1134e9c8ae7SLuigi Rizzo 	const struct dn_flow_queue *b = pb;
1144e9c8ae7SLuigi Rizzo 
1154e9c8ae7SLuigi Rizzo 	switch (field) {
1164e9c8ae7SLuigi Rizzo 	case 1: /* pkts */
1174e9c8ae7SLuigi Rizzo 		res = a->len - b->len;
1184e9c8ae7SLuigi Rizzo 		break;
1194e9c8ae7SLuigi Rizzo 	case 2: /* bytes */
1204e9c8ae7SLuigi Rizzo 		res = a->len_bytes - b->len_bytes;
1214e9c8ae7SLuigi Rizzo 		break;
1224e9c8ae7SLuigi Rizzo 
1234e9c8ae7SLuigi Rizzo 	case 3: /* tot pkts */
1244e9c8ae7SLuigi Rizzo 		res = a->tot_pkts - b->tot_pkts;
1254e9c8ae7SLuigi Rizzo 		break;
1264e9c8ae7SLuigi Rizzo 
1274e9c8ae7SLuigi Rizzo 	case 4: /* tot bytes */
1284e9c8ae7SLuigi Rizzo 		res = a->tot_bytes - b->tot_bytes;
1294e9c8ae7SLuigi Rizzo 		break;
1304e9c8ae7SLuigi Rizzo 	}
1314e9c8ae7SLuigi Rizzo 	if (res < 0)
1324e9c8ae7SLuigi Rizzo 		res = -1;
1334e9c8ae7SLuigi Rizzo 	if (res > 0)
1344e9c8ae7SLuigi Rizzo 		res = 1;
1354e9c8ae7SLuigi Rizzo 	return (int)(rev ? res : -res);
1364e9c8ae7SLuigi Rizzo }
137cc4d3c30SLuigi Rizzo #endif
1384e9c8ae7SLuigi Rizzo 
139cc4d3c30SLuigi Rizzo /* print a mask and header for the subsequent list of flows */
1404e9c8ae7SLuigi Rizzo static void
141cc4d3c30SLuigi Rizzo print_mask(struct ipfw_flow_id *id)
1424e9c8ae7SLuigi Rizzo {
143cc4d3c30SLuigi Rizzo 	if (!IS_IP6_FLOW_ID(id)) {
1444e9c8ae7SLuigi Rizzo 		printf("    "
145f9f7bde3SLuigi Rizzo 		    "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
146f9f7bde3SLuigi Rizzo 		    id->extra ? "queue," : "",
147cc4d3c30SLuigi Rizzo 		    id->proto,
148cc4d3c30SLuigi Rizzo 		    id->src_ip, id->src_port,
149cc4d3c30SLuigi Rizzo 		    id->dst_ip, id->dst_port);
150cc4d3c30SLuigi Rizzo 	} else {
151cc4d3c30SLuigi Rizzo 		char buf[255];
152f9f7bde3SLuigi Rizzo 		printf("\n        mask: %sproto: 0x%02x, flow_id: 0x%08x,  ",
153f9f7bde3SLuigi Rizzo 		    id->extra ? "queue," : "",
154cc4d3c30SLuigi Rizzo 		    id->proto, id->flow_id6);
155cc4d3c30SLuigi Rizzo 		inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
156cc4d3c30SLuigi Rizzo 		printf("%s/0x%04x -> ", buf, id->src_port);
157cc4d3c30SLuigi Rizzo 		inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
158cc4d3c30SLuigi Rizzo 		printf("%s/0x%04x\n", buf, id->dst_port);
159f40be7f6SLuigi Rizzo 	}
160f40be7f6SLuigi Rizzo }
1614e9c8ae7SLuigi Rizzo 
162f40be7f6SLuigi Rizzo static void
163f40be7f6SLuigi Rizzo print_header(struct ipfw_flow_id *id)
164f40be7f6SLuigi Rizzo {
165f40be7f6SLuigi Rizzo 	if (!IS_IP6_FLOW_ID(id))
166f40be7f6SLuigi Rizzo 		printf("BKT Prot ___Source IP/port____ "
167f40be7f6SLuigi Rizzo 		    "____Dest. IP/port____ "
168f40be7f6SLuigi Rizzo 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
169f40be7f6SLuigi Rizzo 	else
1704e9c8ae7SLuigi Rizzo 		printf("BKT ___Prot___ _flow-id_ "
1714e9c8ae7SLuigi Rizzo 		    "______________Source IPv6/port_______________ "
1724e9c8ae7SLuigi Rizzo 		    "_______________Dest. IPv6/port_______________ "
1734e9c8ae7SLuigi Rizzo 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
1744e9c8ae7SLuigi Rizzo }
1754e9c8ae7SLuigi Rizzo 
1764e9c8ae7SLuigi Rizzo static void
177f40be7f6SLuigi Rizzo list_flow(struct dn_flow *ni, int *print)
178cc4d3c30SLuigi Rizzo {
179cc4d3c30SLuigi Rizzo 	char buff[255];
180f40be7f6SLuigi Rizzo 	struct protoent *pe = NULL;
181cc4d3c30SLuigi Rizzo 	struct in_addr ina;
182cc4d3c30SLuigi Rizzo 	struct ipfw_flow_id *id = &ni->fid;
183cc4d3c30SLuigi Rizzo 
184f40be7f6SLuigi Rizzo 	if (*print) {
185f40be7f6SLuigi Rizzo 		print_header(&ni->fid);
186f40be7f6SLuigi Rizzo 		*print = 0;
187f40be7f6SLuigi Rizzo 	}
188cc4d3c30SLuigi Rizzo 	pe = getprotobynumber(id->proto);
189cc4d3c30SLuigi Rizzo 		/* XXX: Should check for IPv4 flows */
190f9f7bde3SLuigi Rizzo 	printf("%3u%c", (ni->oid.id) & 0xff,
191f9f7bde3SLuigi Rizzo 		id->extra ? '*' : ' ');
192cc4d3c30SLuigi Rizzo 	if (!IS_IP6_FLOW_ID(id)) {
193cc4d3c30SLuigi Rizzo 		if (pe)
194cc4d3c30SLuigi Rizzo 			printf("%-4s ", pe->p_name);
195cc4d3c30SLuigi Rizzo 		else
196cc4d3c30SLuigi Rizzo 			printf("%4u ", id->proto);
197cc4d3c30SLuigi Rizzo 		ina.s_addr = htonl(id->src_ip);
198cc4d3c30SLuigi Rizzo 		printf("%15s/%-5d ",
199cc4d3c30SLuigi Rizzo 		    inet_ntoa(ina), id->src_port);
200cc4d3c30SLuigi Rizzo 		ina.s_addr = htonl(id->dst_ip);
201cc4d3c30SLuigi Rizzo 		printf("%15s/%-5d ",
202cc4d3c30SLuigi Rizzo 		    inet_ntoa(ina), id->dst_port);
203cc4d3c30SLuigi Rizzo 	} else {
204cc4d3c30SLuigi Rizzo 		/* Print IPv6 flows */
205cc4d3c30SLuigi Rizzo 		if (pe != NULL)
206cc4d3c30SLuigi Rizzo 			printf("%9s ", pe->p_name);
207cc4d3c30SLuigi Rizzo 		else
208cc4d3c30SLuigi Rizzo 			printf("%9u ", id->proto);
209cc4d3c30SLuigi Rizzo 		printf("%7d  %39s/%-5d ", id->flow_id6,
210cc4d3c30SLuigi Rizzo 		    inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
211cc4d3c30SLuigi Rizzo 		    id->src_port);
212cc4d3c30SLuigi Rizzo 		printf(" %39s/%-5d ",
213cc4d3c30SLuigi Rizzo 		    inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
214cc4d3c30SLuigi Rizzo 		    id->dst_port);
215cc4d3c30SLuigi Rizzo 	}
21637133ba7SLuigi Rizzo 	pr_u64(&ni->tot_pkts, 4);
21737133ba7SLuigi Rizzo 	pr_u64(&ni->tot_bytes, 8);
21837133ba7SLuigi Rizzo 	printf("%2u %4u %3u\n",
219cc4d3c30SLuigi Rizzo 	    ni->length, ni->len_bytes, ni->drops);
220cc4d3c30SLuigi Rizzo }
221cc4d3c30SLuigi Rizzo 
222cc4d3c30SLuigi Rizzo static void
223cc4d3c30SLuigi Rizzo print_flowset_parms(struct dn_fs *fs, char *prefix)
2244e9c8ae7SLuigi Rizzo {
2254e9c8ae7SLuigi Rizzo 	int l;
2264e9c8ae7SLuigi Rizzo 	char qs[30];
2274e9c8ae7SLuigi Rizzo 	char plr[30];
2284e9c8ae7SLuigi Rizzo 	char red[90];	/* Display RED parameters */
2294e9c8ae7SLuigi Rizzo 
2304e9c8ae7SLuigi Rizzo 	l = fs->qsize;
231cc4d3c30SLuigi Rizzo 	if (fs->flags & DN_QSIZE_BYTES) {
2324e9c8ae7SLuigi Rizzo 		if (l >= 8192)
2334e9c8ae7SLuigi Rizzo 			sprintf(qs, "%d KB", l / 1024);
2344e9c8ae7SLuigi Rizzo 		else
2354e9c8ae7SLuigi Rizzo 			sprintf(qs, "%d B", l);
2364e9c8ae7SLuigi Rizzo 	} else
2374e9c8ae7SLuigi Rizzo 		sprintf(qs, "%3d sl.", l);
2384e9c8ae7SLuigi Rizzo 	if (fs->plr)
2394e9c8ae7SLuigi Rizzo 		sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
2404e9c8ae7SLuigi Rizzo 	else
2414e9c8ae7SLuigi Rizzo 		plr[0] = '\0';
242cc4d3c30SLuigi Rizzo 
243*fc5e1956SHiren Panchasara 	if (fs->flags & DN_IS_RED) {	/* RED parameters */
2444e9c8ae7SLuigi Rizzo 		sprintf(red,
2454e9c8ae7SLuigi Rizzo 		    "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
246cc4d3c30SLuigi Rizzo 		    (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
2474e9c8ae7SLuigi Rizzo 		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
248cc4d3c30SLuigi Rizzo 		    fs->min_th,
249cc4d3c30SLuigi Rizzo 		    fs->max_th,
2504e9c8ae7SLuigi Rizzo 		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
251*fc5e1956SHiren Panchasara 		if (fs->flags & DN_IS_ECN)
252*fc5e1956SHiren Panchasara 			strncat(red, " (ecn)", 6);
253*fc5e1956SHiren Panchasara 	} else
2544e9c8ae7SLuigi Rizzo 		sprintf(red, "droptail");
2554e9c8ae7SLuigi Rizzo 
256cc4d3c30SLuigi Rizzo 	if (prefix[0]) {
2574e9c8ae7SLuigi Rizzo 	    printf("%s %s%s %d queues (%d buckets) %s\n",
258cc4d3c30SLuigi Rizzo 		prefix, qs, plr, fs->oid.id, fs->buckets, red);
259cc4d3c30SLuigi Rizzo 	    prefix[0] = '\0';
260cc4d3c30SLuigi Rizzo 	} else {
261cc4d3c30SLuigi Rizzo 	    printf("q%05d %s%s %d flows (%d buckets) sched %d "
262cc4d3c30SLuigi Rizzo 			"weight %d lmax %d pri %d %s\n",
263cc4d3c30SLuigi Rizzo 		fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
264cc4d3c30SLuigi Rizzo 		fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
265cc4d3c30SLuigi Rizzo 	    if (fs->flags & DN_HAVE_MASK)
266cc4d3c30SLuigi Rizzo 		print_mask(&fs->flow_mask);
267cc4d3c30SLuigi Rizzo 	}
2684e9c8ae7SLuigi Rizzo }
2694e9c8ae7SLuigi Rizzo 
2704bb7ae9dSLuigi Rizzo static void
271cc4d3c30SLuigi Rizzo print_extra_delay_parms(struct dn_profile *p)
2724bb7ae9dSLuigi Rizzo {
2734bb7ae9dSLuigi Rizzo 	double loss;
2744bb7ae9dSLuigi Rizzo 	if (p->samples_no <= 0)
2754bb7ae9dSLuigi Rizzo 		return;
2764bb7ae9dSLuigi Rizzo 
2774bb7ae9dSLuigi Rizzo 	loss = p->loss_level;
2784bb7ae9dSLuigi Rizzo 	loss /= p->samples_no;
2796882bf4dSOleg Bulyzhin 	printf("\t profile: name \"%s\" loss %f samples %d\n",
2806882bf4dSOleg Bulyzhin 		p->name, loss, p->samples_no);
2814bb7ae9dSLuigi Rizzo }
2824bb7ae9dSLuigi Rizzo 
283cc4d3c30SLuigi Rizzo static void
284cc4d3c30SLuigi Rizzo flush_buf(char *buf)
2854e9c8ae7SLuigi Rizzo {
286cc4d3c30SLuigi Rizzo 	if (buf[0])
287cc4d3c30SLuigi Rizzo 		printf("%s\n", buf);
288cc4d3c30SLuigi Rizzo 	buf[0] = '\0';
289cc4d3c30SLuigi Rizzo }
2904e9c8ae7SLuigi Rizzo 
291cc4d3c30SLuigi Rizzo /*
292cc4d3c30SLuigi Rizzo  * generic list routine. We expect objects in a specific order, i.e.
293cc4d3c30SLuigi Rizzo  * PIPES AND SCHEDULERS:
294cc4d3c30SLuigi Rizzo  *	link; scheduler; internal flowset if any; instances
295cc4d3c30SLuigi Rizzo  * we can tell a pipe from the number.
296cc4d3c30SLuigi Rizzo  *
297cc4d3c30SLuigi Rizzo  * FLOWSETS:
298cc4d3c30SLuigi Rizzo  *	flowset; queues;
299cc4d3c30SLuigi Rizzo  * link i (int queue); scheduler i; si(i) { flowsets() : queues }
300cc4d3c30SLuigi Rizzo  */
301cc4d3c30SLuigi Rizzo static void
302cc4d3c30SLuigi Rizzo list_pipes(struct dn_id *oid, struct dn_id *end)
303cc4d3c30SLuigi Rizzo {
304cc4d3c30SLuigi Rizzo     char buf[160];	/* pending buffer */
305f40be7f6SLuigi Rizzo     int toPrint = 1;	/* print header */
306cc4d3c30SLuigi Rizzo 
30737133ba7SLuigi Rizzo     buf[0] = '\0';
308cc4d3c30SLuigi Rizzo     for (; oid != end; oid = O_NEXT(oid, oid->len)) {
309cc4d3c30SLuigi Rizzo 	if (oid->len < sizeof(*oid))
310cc4d3c30SLuigi Rizzo 		errx(1, "invalid oid len %d\n", oid->len);
311cc4d3c30SLuigi Rizzo 
312cc4d3c30SLuigi Rizzo 	switch (oid->type) {
313cc4d3c30SLuigi Rizzo 	default:
314cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
315cc4d3c30SLuigi Rizzo 	    printf("unrecognized object %d size %d\n", oid->type, oid->len);
316cc4d3c30SLuigi Rizzo 	    break;
317cc4d3c30SLuigi Rizzo 	case DN_TEXT: /* list of attached flowsets */
318cc4d3c30SLuigi Rizzo 	    {
319cc4d3c30SLuigi Rizzo 		int i, l;
320cc4d3c30SLuigi Rizzo 		struct {
321cc4d3c30SLuigi Rizzo 			struct dn_id id;
322cc4d3c30SLuigi Rizzo 			uint32_t p[0];
323cc4d3c30SLuigi Rizzo 		} *d = (void *)oid;
324cc4d3c30SLuigi Rizzo 		l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
325cc4d3c30SLuigi Rizzo 		if (l == 0)
326cc4d3c30SLuigi Rizzo 		    break;
327cc4d3c30SLuigi Rizzo 		printf("   Children flowsets: ");
328cc4d3c30SLuigi Rizzo 		for (i = 0; i < l; i++)
329cc4d3c30SLuigi Rizzo 			printf("%u ", d->p[i]);
330cc4d3c30SLuigi Rizzo 		printf("\n");
331cc4d3c30SLuigi Rizzo 		break;
332cc4d3c30SLuigi Rizzo 	    }
333cc4d3c30SLuigi Rizzo 	case DN_CMD_GET:
334cc4d3c30SLuigi Rizzo 	    if (co.verbose)
335cc4d3c30SLuigi Rizzo 		printf("answer for cmd %d, len %d\n", oid->type, oid->id);
336cc4d3c30SLuigi Rizzo 	    break;
337cc4d3c30SLuigi Rizzo 	case DN_SCH: {
338cc4d3c30SLuigi Rizzo 	    struct dn_sch *s = (struct dn_sch *)oid;
339cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
340cc4d3c30SLuigi Rizzo 	    printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
341cc4d3c30SLuigi Rizzo 			s->sched_nr,
342cc4d3c30SLuigi Rizzo 			s->name, s->flags, s->buckets, s->oid.id);
343cc4d3c30SLuigi Rizzo 	    if (s->flags & DN_HAVE_MASK)
344cc4d3c30SLuigi Rizzo 		print_mask(&s->sched_mask);
345cc4d3c30SLuigi Rizzo 	    }
346cc4d3c30SLuigi Rizzo 	    break;
347cc4d3c30SLuigi Rizzo 
348cc4d3c30SLuigi Rizzo 	case DN_FLOW:
349f40be7f6SLuigi Rizzo 	    list_flow((struct dn_flow *)oid, &toPrint);
350cc4d3c30SLuigi Rizzo 	    break;
351cc4d3c30SLuigi Rizzo 
352cc4d3c30SLuigi Rizzo 	case DN_LINK: {
353cc4d3c30SLuigi Rizzo 	    struct dn_link *p = (struct dn_link *)oid;
3544e9c8ae7SLuigi Rizzo 	    double b = p->bandwidth;
355cc4d3c30SLuigi Rizzo 	    char bwbuf[30];
3566882bf4dSOleg Bulyzhin 	    char burst[5 + 7];
3574e9c8ae7SLuigi Rizzo 
358cc4d3c30SLuigi Rizzo 	    /* This starts a new object so flush buffer */
359cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
360cc4d3c30SLuigi Rizzo 	    /* data rate */
361cc4d3c30SLuigi Rizzo 	    if (b == 0)
362cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "unlimited     ");
3634e9c8ae7SLuigi Rizzo 	    else if (b >= 1000000)
364cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
3654e9c8ae7SLuigi Rizzo 	    else if (b >= 1000)
366cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
3674e9c8ae7SLuigi Rizzo 	    else
368cc4d3c30SLuigi Rizzo 		sprintf(bwbuf, "%7.3f bit/s ", b);
3694e9c8ae7SLuigi Rizzo 
3706882bf4dSOleg Bulyzhin 	    if (humanize_number(burst, sizeof(burst), p->burst,
371cc4d3c30SLuigi Rizzo 		    "", HN_AUTOSCALE, 0) < 0 || co.verbose)
372cc4d3c30SLuigi Rizzo 		sprintf(burst, "%d", (int)p->burst);
373cc4d3c30SLuigi Rizzo 	    sprintf(buf, "%05d: %s %4d ms burst %s",
374cc4d3c30SLuigi Rizzo 		p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
3754e9c8ae7SLuigi Rizzo 	    }
3764e9c8ae7SLuigi Rizzo 	    break;
3774e9c8ae7SLuigi Rizzo 
378cc4d3c30SLuigi Rizzo 	case DN_FS:
379cc4d3c30SLuigi Rizzo 	    print_flowset_parms((struct dn_fs *)oid, buf);
380cc4d3c30SLuigi Rizzo 	    break;
381cc4d3c30SLuigi Rizzo 	case DN_PROFILE:
382cc4d3c30SLuigi Rizzo 	    flush_buf(buf);
383cc4d3c30SLuigi Rizzo 	    print_extra_delay_parms((struct dn_profile *)oid);
3844e9c8ae7SLuigi Rizzo 	}
385cc4d3c30SLuigi Rizzo 	flush_buf(buf); // XXX does it really go here ?
3864e9c8ae7SLuigi Rizzo     }
3874e9c8ae7SLuigi Rizzo }
3884e9c8ae7SLuigi Rizzo 
3894e9c8ae7SLuigi Rizzo /*
390cc4d3c30SLuigi Rizzo  * Delete pipe, queue or scheduler i
3914e9c8ae7SLuigi Rizzo  */
3924e9c8ae7SLuigi Rizzo int
393cc4d3c30SLuigi Rizzo ipfw_delete_pipe(int do_pipe, int i)
3944e9c8ae7SLuigi Rizzo {
395cc4d3c30SLuigi Rizzo 	struct {
396cc4d3c30SLuigi Rizzo 		struct dn_id oid;
397cc4d3c30SLuigi Rizzo 		uintptr_t a[1];	/* add more if we want a list */
398cc4d3c30SLuigi Rizzo 	} cmd;
399cc4d3c30SLuigi Rizzo 	oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
400cc4d3c30SLuigi Rizzo 	cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
401cc4d3c30SLuigi Rizzo 		( (do_pipe == 2) ? DN_FS : DN_SCH);
402cc4d3c30SLuigi Rizzo 	cmd.a[0] = i;
403cc4d3c30SLuigi Rizzo 	i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
4044e9c8ae7SLuigi Rizzo 	if (i) {
4054e9c8ae7SLuigi Rizzo 		i = 1;
4064e9c8ae7SLuigi Rizzo 		warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
4074e9c8ae7SLuigi Rizzo 	}
4084e9c8ae7SLuigi Rizzo 	return i;
4094e9c8ae7SLuigi Rizzo }
4104e9c8ae7SLuigi Rizzo 
4114bb7ae9dSLuigi Rizzo /*
4124bb7ae9dSLuigi Rizzo  * Code to parse delay profiles.
4134bb7ae9dSLuigi Rizzo  *
4144bb7ae9dSLuigi Rizzo  * Some link types introduce extra delays in the transmission
4154bb7ae9dSLuigi Rizzo  * of a packet, e.g. because of MAC level framing, contention on
4164bb7ae9dSLuigi Rizzo  * the use of the channel, MAC level retransmissions and so on.
4174bb7ae9dSLuigi Rizzo  * From our point of view, the channel is effectively unavailable
4184bb7ae9dSLuigi Rizzo  * for this extra time, which is constant or variable depending
4194bb7ae9dSLuigi Rizzo  * on the link type. Additionally, packets may be dropped after this
4204bb7ae9dSLuigi Rizzo  * time (e.g. on a wireless link after too many retransmissions).
4214bb7ae9dSLuigi Rizzo  * We can model the additional delay with an empirical curve
4224bb7ae9dSLuigi Rizzo  * that represents its distribution.
4234bb7ae9dSLuigi Rizzo  *
4244bb7ae9dSLuigi Rizzo  *      cumulative probability
4254bb7ae9dSLuigi Rizzo  *      1.0 ^
4264bb7ae9dSLuigi Rizzo  *          |
4274bb7ae9dSLuigi Rizzo  *      L   +-- loss-level          x
4284bb7ae9dSLuigi Rizzo  *          |                 ******
4294bb7ae9dSLuigi Rizzo  *          |                *
4304bb7ae9dSLuigi Rizzo  *          |           *****
4314bb7ae9dSLuigi Rizzo  *          |          *
4324bb7ae9dSLuigi Rizzo  *          |        **
4334bb7ae9dSLuigi Rizzo  *          |       *
4344bb7ae9dSLuigi Rizzo  *          +-------*------------------->
4354bb7ae9dSLuigi Rizzo  *                      delay
4364bb7ae9dSLuigi Rizzo  *
4374bb7ae9dSLuigi Rizzo  * The empirical curve may have both vertical and horizontal lines.
4384bb7ae9dSLuigi Rizzo  * Vertical lines represent constant delay for a range of
4394bb7ae9dSLuigi Rizzo  * probabilities; horizontal lines correspond to a discontinuty
440cc4d3c30SLuigi Rizzo  * in the delay distribution: the link will use the largest delay
4414bb7ae9dSLuigi Rizzo  * for a given probability.
4424bb7ae9dSLuigi Rizzo  *
4434bb7ae9dSLuigi Rizzo  * To pass the curve to dummynet, we must store the parameters
4444bb7ae9dSLuigi Rizzo  * in a file as described below, and issue the command
4454bb7ae9dSLuigi Rizzo  *
4464bb7ae9dSLuigi Rizzo  *      ipfw pipe <n> config ... bw XXX profile <filename> ...
4474bb7ae9dSLuigi Rizzo  *
4484bb7ae9dSLuigi Rizzo  * The file format is the following, with whitespace acting as
4494bb7ae9dSLuigi Rizzo  * a separator and '#' indicating the beginning a comment:
4504bb7ae9dSLuigi Rizzo  *
4514bb7ae9dSLuigi Rizzo  *	samples N
4524bb7ae9dSLuigi Rizzo  *		the number of samples used in the internal
4534bb7ae9dSLuigi Rizzo  *		representation (2..1024; default 100);
4544bb7ae9dSLuigi Rizzo  *
4554bb7ae9dSLuigi Rizzo  *	loss-level L
4564bb7ae9dSLuigi Rizzo  *		The probability above which packets are lost.
4574bb7ae9dSLuigi Rizzo  *	       (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
4584bb7ae9dSLuigi Rizzo  *
4594bb7ae9dSLuigi Rizzo  *	name identifier
4604bb7ae9dSLuigi Rizzo  *		Optional a name (listed by "ipfw pipe show")
4614bb7ae9dSLuigi Rizzo  *		to identify the distribution;
4624bb7ae9dSLuigi Rizzo  *
4634bb7ae9dSLuigi Rizzo  *	"delay prob" | "prob delay"
4644bb7ae9dSLuigi Rizzo  *		One of these two lines is mandatory and defines
4654bb7ae9dSLuigi Rizzo  *		the format of the following lines with data points.
4664bb7ae9dSLuigi Rizzo  *
4674bb7ae9dSLuigi Rizzo  *	XXX YYY
4684bb7ae9dSLuigi Rizzo  *		2 or more lines representing points in the curve,
4694bb7ae9dSLuigi Rizzo  *		with either delay or probability first, according
4704bb7ae9dSLuigi Rizzo  *		to the chosen format.
4714bb7ae9dSLuigi Rizzo  *		The unit for delay is milliseconds.
4724bb7ae9dSLuigi Rizzo  *
4734bb7ae9dSLuigi Rizzo  * Data points does not need to be ordered or equal to the number
4744bb7ae9dSLuigi Rizzo  * specified in the "samples" line. ipfw will sort and interpolate
4754bb7ae9dSLuigi Rizzo  * the curve as needed.
4764bb7ae9dSLuigi Rizzo  *
4774bb7ae9dSLuigi Rizzo  * Example of a profile file:
4784bb7ae9dSLuigi Rizzo 
4794bb7ae9dSLuigi Rizzo 	name    bla_bla_bla
4804bb7ae9dSLuigi Rizzo 	samples 100
4814bb7ae9dSLuigi Rizzo 	loss-level    0.86
4824bb7ae9dSLuigi Rizzo 	prob    delay
4834bb7ae9dSLuigi Rizzo 	0       200	# minimum overhead is 200ms
4844bb7ae9dSLuigi Rizzo 	0.5     200
4854bb7ae9dSLuigi Rizzo 	0.5     300
4864bb7ae9dSLuigi Rizzo 	0.8     1000
4874bb7ae9dSLuigi Rizzo 	0.9     1300
4884bb7ae9dSLuigi Rizzo 	1       1300
4894bb7ae9dSLuigi Rizzo 
4904bb7ae9dSLuigi Rizzo  * Internally, we will convert the curve to a fixed number of
4914bb7ae9dSLuigi Rizzo  * samples, and when it is time to transmit a packet we will
4924bb7ae9dSLuigi Rizzo  * model the extra delay as extra bits in the packet.
4934bb7ae9dSLuigi Rizzo  *
4944bb7ae9dSLuigi Rizzo  */
4954bb7ae9dSLuigi Rizzo 
4964bb7ae9dSLuigi Rizzo #define ED_MAX_LINE_LEN	256+ED_MAX_NAME_LEN
4974bb7ae9dSLuigi Rizzo #define ED_TOK_SAMPLES	"samples"
4984bb7ae9dSLuigi Rizzo #define ED_TOK_LOSS	"loss-level"
4994bb7ae9dSLuigi Rizzo #define ED_TOK_NAME	"name"
5004bb7ae9dSLuigi Rizzo #define ED_TOK_DELAY	"delay"
5014bb7ae9dSLuigi Rizzo #define ED_TOK_PROB	"prob"
5027a459517SLuigi Rizzo #define ED_TOK_BW	"bw"
5034bb7ae9dSLuigi Rizzo #define ED_SEPARATORS	" \t\n"
5044bb7ae9dSLuigi Rizzo #define ED_MIN_SAMPLES_NO	2
5054bb7ae9dSLuigi Rizzo 
5064bb7ae9dSLuigi Rizzo /*
5074bb7ae9dSLuigi Rizzo  * returns 1 if s is a non-negative number, with at least one '.'
5084bb7ae9dSLuigi Rizzo  */
5094bb7ae9dSLuigi Rizzo static int
5104bb7ae9dSLuigi Rizzo is_valid_number(const char *s)
5114bb7ae9dSLuigi Rizzo {
5124bb7ae9dSLuigi Rizzo 	int i, dots_found = 0;
5134bb7ae9dSLuigi Rizzo 	int len = strlen(s);
5144bb7ae9dSLuigi Rizzo 
5154bb7ae9dSLuigi Rizzo 	for (i = 0; i<len; ++i)
5164bb7ae9dSLuigi Rizzo 		if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
5174bb7ae9dSLuigi Rizzo 			return 0;
5184bb7ae9dSLuigi Rizzo 	return 1;
5194bb7ae9dSLuigi Rizzo }
5204bb7ae9dSLuigi Rizzo 
5217a459517SLuigi Rizzo /*
5227a459517SLuigi Rizzo  * Take as input a string describing a bandwidth value
5237a459517SLuigi Rizzo  * and return the numeric bandwidth value.
5247a459517SLuigi Rizzo  * set clocking interface or bandwidth value
5257a459517SLuigi Rizzo  */
52601ab7632SLuigi Rizzo static void
5277a459517SLuigi Rizzo read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
5287a459517SLuigi Rizzo {
5297a459517SLuigi Rizzo 	if (*bandwidth != -1)
530cc4d3c30SLuigi Rizzo 		warnx("duplicate token, override bandwidth value!");
5317a459517SLuigi Rizzo 
5327a459517SLuigi Rizzo 	if (arg[0] >= 'a' && arg[0] <= 'z') {
533cc4d3c30SLuigi Rizzo 		if (!if_name) {
534cc4d3c30SLuigi Rizzo 			errx(1, "no if support");
535cc4d3c30SLuigi Rizzo 		}
5367a459517SLuigi Rizzo 		if (namelen >= IFNAMSIZ)
5377a459517SLuigi Rizzo 			warn("interface name truncated");
5387a459517SLuigi Rizzo 		namelen--;
5397a459517SLuigi Rizzo 		/* interface name */
5407a459517SLuigi Rizzo 		strncpy(if_name, arg, namelen);
5417a459517SLuigi Rizzo 		if_name[namelen] = '\0';
5427a459517SLuigi Rizzo 		*bandwidth = 0;
5437a459517SLuigi Rizzo 	} else {	/* read bandwidth value */
5447a459517SLuigi Rizzo 		int bw;
5457a459517SLuigi Rizzo 		char *end = NULL;
5467a459517SLuigi Rizzo 
5477a459517SLuigi Rizzo 		bw = strtoul(arg, &end, 0);
5487a459517SLuigi Rizzo 		if (*end == 'K' || *end == 'k') {
5497a459517SLuigi Rizzo 			end++;
5507a459517SLuigi Rizzo 			bw *= 1000;
551b74331bfSLuigi Rizzo 		} else if (*end == 'M' || *end == 'm') {
5527a459517SLuigi Rizzo 			end++;
5537a459517SLuigi Rizzo 			bw *= 1000000;
5547a459517SLuigi Rizzo 		}
5557a459517SLuigi Rizzo 		if ((*end == 'B' &&
5567a459517SLuigi Rizzo 			_substrcmp2(end, "Bi", "Bit/s") != 0) ||
5577a459517SLuigi Rizzo 		    _substrcmp2(end, "by", "bytes") == 0)
5587a459517SLuigi Rizzo 			bw *= 8;
5597a459517SLuigi Rizzo 
5607a459517SLuigi Rizzo 		if (bw < 0)
5617a459517SLuigi Rizzo 			errx(EX_DATAERR, "bandwidth too large");
5627a459517SLuigi Rizzo 
5637a459517SLuigi Rizzo 		*bandwidth = bw;
564cc4d3c30SLuigi Rizzo 		if (if_name)
5657a459517SLuigi Rizzo 			if_name[0] = '\0';
5667a459517SLuigi Rizzo 	}
5677a459517SLuigi Rizzo }
5687a459517SLuigi Rizzo 
5694bb7ae9dSLuigi Rizzo struct point {
5704bb7ae9dSLuigi Rizzo 	double prob;
5714bb7ae9dSLuigi Rizzo 	double delay;
5724bb7ae9dSLuigi Rizzo };
5734bb7ae9dSLuigi Rizzo 
57401ab7632SLuigi Rizzo static int
5754bb7ae9dSLuigi Rizzo compare_points(const void *vp1, const void *vp2)
5764bb7ae9dSLuigi Rizzo {
5774bb7ae9dSLuigi Rizzo 	const struct point *p1 = vp1;
5784bb7ae9dSLuigi Rizzo 	const struct point *p2 = vp2;
5794bb7ae9dSLuigi Rizzo 	double res = 0;
5804bb7ae9dSLuigi Rizzo 
5814bb7ae9dSLuigi Rizzo 	res = p1->prob - p2->prob;
5824bb7ae9dSLuigi Rizzo 	if (res == 0)
5834bb7ae9dSLuigi Rizzo 		res = p1->delay - p2->delay;
5844bb7ae9dSLuigi Rizzo 	if (res < 0)
5854bb7ae9dSLuigi Rizzo 		return -1;
5864bb7ae9dSLuigi Rizzo 	else if (res > 0)
5874bb7ae9dSLuigi Rizzo 		return 1;
5884bb7ae9dSLuigi Rizzo 	else
5894bb7ae9dSLuigi Rizzo 		return 0;
5904bb7ae9dSLuigi Rizzo }
5914bb7ae9dSLuigi Rizzo 
5924bb7ae9dSLuigi Rizzo #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
5934bb7ae9dSLuigi Rizzo 
5944bb7ae9dSLuigi Rizzo static void
595cc4d3c30SLuigi Rizzo load_extra_delays(const char *filename, struct dn_profile *p,
596cc4d3c30SLuigi Rizzo 	struct dn_link *link)
5974bb7ae9dSLuigi Rizzo {
5984bb7ae9dSLuigi Rizzo 	char    line[ED_MAX_LINE_LEN];
5994bb7ae9dSLuigi Rizzo 	FILE    *f;
6004bb7ae9dSLuigi Rizzo 	int     lineno = 0;
6014bb7ae9dSLuigi Rizzo 	int     i;
6024bb7ae9dSLuigi Rizzo 
6034bb7ae9dSLuigi Rizzo 	int     samples = -1;
6044bb7ae9dSLuigi Rizzo 	double  loss = -1.0;
6054bb7ae9dSLuigi Rizzo 	char    profile_name[ED_MAX_NAME_LEN];
6064bb7ae9dSLuigi Rizzo 	int     delay_first = -1;
6074bb7ae9dSLuigi Rizzo 	int     do_points = 0;
6084bb7ae9dSLuigi Rizzo 	struct point    points[ED_MAX_SAMPLES_NO];
6094bb7ae9dSLuigi Rizzo 	int     points_no = 0;
6104bb7ae9dSLuigi Rizzo 
611cc4d3c30SLuigi Rizzo 	/* XXX link never NULL? */
612cc4d3c30SLuigi Rizzo 	p->link_nr = link->link_nr;
613cc4d3c30SLuigi Rizzo 
6144bb7ae9dSLuigi Rizzo 	profile_name[0] = '\0';
6154bb7ae9dSLuigi Rizzo 	f = fopen(filename, "r");
6164bb7ae9dSLuigi Rizzo 	if (f == NULL)
6174bb7ae9dSLuigi Rizzo 		err(EX_UNAVAILABLE, "fopen: %s", filename);
6184bb7ae9dSLuigi Rizzo 
6194bb7ae9dSLuigi Rizzo 	while (fgets(line, ED_MAX_LINE_LEN, f)) {	 /* read commands */
6204bb7ae9dSLuigi Rizzo 		char *s, *cur = line, *name = NULL, *arg = NULL;
6214bb7ae9dSLuigi Rizzo 
6224bb7ae9dSLuigi Rizzo 		++lineno;
6234bb7ae9dSLuigi Rizzo 
6244bb7ae9dSLuigi Rizzo 		/* parse the line */
6254bb7ae9dSLuigi Rizzo 		while (cur) {
6264bb7ae9dSLuigi Rizzo 			s = strsep(&cur, ED_SEPARATORS);
6274bb7ae9dSLuigi Rizzo 			if (s == NULL || *s == '#')
6284bb7ae9dSLuigi Rizzo 				break;
6294bb7ae9dSLuigi Rizzo 			if (*s == '\0')
6304bb7ae9dSLuigi Rizzo 				continue;
6314bb7ae9dSLuigi Rizzo 			if (arg)
6324bb7ae9dSLuigi Rizzo 				errx(ED_EFMT("too many arguments"));
6334bb7ae9dSLuigi Rizzo 			if (name == NULL)
6344bb7ae9dSLuigi Rizzo 				name = s;
6354bb7ae9dSLuigi Rizzo 			else
6364bb7ae9dSLuigi Rizzo 				arg = s;
6374bb7ae9dSLuigi Rizzo 		}
6384bb7ae9dSLuigi Rizzo 		if (name == NULL)	/* empty line */
6394bb7ae9dSLuigi Rizzo 			continue;
6404bb7ae9dSLuigi Rizzo 		if (arg == NULL)
6414bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("missing arg for %s"), name);
6424bb7ae9dSLuigi Rizzo 
6434bb7ae9dSLuigi Rizzo 		if (!strcasecmp(name, ED_TOK_SAMPLES)) {
6444bb7ae9dSLuigi Rizzo 		    if (samples > 0)
6454bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicate ``samples'' line"));
6464bb7ae9dSLuigi Rizzo 		    if (atoi(arg) <=0)
6474bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("invalid number of samples"));
6484bb7ae9dSLuigi Rizzo 		    samples = atoi(arg);
6494bb7ae9dSLuigi Rizzo 		    if (samples>ED_MAX_SAMPLES_NO)
6504bb7ae9dSLuigi Rizzo 			    errx(ED_EFMT("too many samples, maximum is %d"),
6514bb7ae9dSLuigi Rizzo 				ED_MAX_SAMPLES_NO);
6524bb7ae9dSLuigi Rizzo 		    do_points = 0;
6537a459517SLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_BW)) {
654cc4d3c30SLuigi Rizzo 		    char buf[IFNAMSIZ];
655cc4d3c30SLuigi Rizzo 		    read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
6564bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_LOSS)) {
6574bb7ae9dSLuigi Rizzo 		    if (loss != -1.0)
6584bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
6594bb7ae9dSLuigi Rizzo 		    if (!is_valid_number(arg))
6604bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("invalid %s"), arg);
6614bb7ae9dSLuigi Rizzo 		    loss = atof(arg);
6624bb7ae9dSLuigi Rizzo 		    if (loss > 1)
6634bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("%s greater than 1.0"), name);
6644bb7ae9dSLuigi Rizzo 		    do_points = 0;
6654bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_NAME)) {
6664bb7ae9dSLuigi Rizzo 		    if (profile_name[0] != '\0')
6674bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
6684bb7ae9dSLuigi Rizzo 		    strncpy(profile_name, arg, sizeof(profile_name) - 1);
6694bb7ae9dSLuigi Rizzo 		    profile_name[sizeof(profile_name)-1] = '\0';
6704bb7ae9dSLuigi Rizzo 		    do_points = 0;
6714bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_DELAY)) {
6724bb7ae9dSLuigi Rizzo 		    if (do_points)
6734bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
6744bb7ae9dSLuigi Rizzo 		    delay_first = 1;
6754bb7ae9dSLuigi Rizzo 		    do_points = 1;
6764bb7ae9dSLuigi Rizzo 		} else if (!strcasecmp(name, ED_TOK_PROB)) {
6774bb7ae9dSLuigi Rizzo 		    if (do_points)
6784bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("duplicated token: %s"), name);
6794bb7ae9dSLuigi Rizzo 		    delay_first = 0;
6804bb7ae9dSLuigi Rizzo 		    do_points = 1;
6814bb7ae9dSLuigi Rizzo 		} else if (do_points) {
6824bb7ae9dSLuigi Rizzo 		    if (!is_valid_number(name) || !is_valid_number(arg))
6834bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("invalid point found"));
6844bb7ae9dSLuigi Rizzo 		    if (delay_first) {
6854bb7ae9dSLuigi Rizzo 			points[points_no].delay = atof(name);
6864bb7ae9dSLuigi Rizzo 			points[points_no].prob = atof(arg);
6874bb7ae9dSLuigi Rizzo 		    } else {
6884bb7ae9dSLuigi Rizzo 			points[points_no].delay = atof(arg);
6894bb7ae9dSLuigi Rizzo 			points[points_no].prob = atof(name);
6904bb7ae9dSLuigi Rizzo 		    }
6914bb7ae9dSLuigi Rizzo 		    if (points[points_no].prob > 1.0)
6924bb7ae9dSLuigi Rizzo 			errx(ED_EFMT("probability greater than 1.0"));
6934bb7ae9dSLuigi Rizzo 		    ++points_no;
6944bb7ae9dSLuigi Rizzo 		} else {
6954bb7ae9dSLuigi Rizzo 		    errx(ED_EFMT("unrecognised command '%s'"), name);
6964bb7ae9dSLuigi Rizzo 		}
6974bb7ae9dSLuigi Rizzo 	}
6984bb7ae9dSLuigi Rizzo 
699ac2e492bSAlexander Leidinger 	fclose (f);
700ac2e492bSAlexander Leidinger 
7014bb7ae9dSLuigi Rizzo 	if (samples == -1) {
7024bb7ae9dSLuigi Rizzo 	    warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
7034bb7ae9dSLuigi Rizzo 	    samples = 100;
7044bb7ae9dSLuigi Rizzo 	}
7054bb7ae9dSLuigi Rizzo 
7064bb7ae9dSLuigi Rizzo 	if (loss == -1.0) {
7074bb7ae9dSLuigi Rizzo 	    warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
7084bb7ae9dSLuigi Rizzo 	    loss = 1;
7094bb7ae9dSLuigi Rizzo 	}
7104bb7ae9dSLuigi Rizzo 
7114bb7ae9dSLuigi Rizzo 	/* make sure that there are enough points. */
7124bb7ae9dSLuigi Rizzo 	if (points_no < ED_MIN_SAMPLES_NO)
7134bb7ae9dSLuigi Rizzo 	    errx(ED_EFMT("too few samples, need at least %d"),
7144bb7ae9dSLuigi Rizzo 		ED_MIN_SAMPLES_NO);
7154bb7ae9dSLuigi Rizzo 
7164bb7ae9dSLuigi Rizzo 	qsort(points, points_no, sizeof(struct point), compare_points);
7174bb7ae9dSLuigi Rizzo 
7184bb7ae9dSLuigi Rizzo 	/* interpolation */
7194bb7ae9dSLuigi Rizzo 	for (i = 0; i<points_no-1; ++i) {
7204bb7ae9dSLuigi Rizzo 	    double y1 = points[i].prob * samples;
7214bb7ae9dSLuigi Rizzo 	    double x1 = points[i].delay;
7224bb7ae9dSLuigi Rizzo 	    double y2 = points[i+1].prob * samples;
7234bb7ae9dSLuigi Rizzo 	    double x2 = points[i+1].delay;
7244bb7ae9dSLuigi Rizzo 
725cc4d3c30SLuigi Rizzo 	    int ix = y1;
7264bb7ae9dSLuigi Rizzo 	    int stop = y2;
7274bb7ae9dSLuigi Rizzo 
7284bb7ae9dSLuigi Rizzo 	    if (x1 == x2) {
729cc4d3c30SLuigi Rizzo 		for (; ix<stop; ++ix)
730cc4d3c30SLuigi Rizzo 		    p->samples[ix] = x1;
7314bb7ae9dSLuigi Rizzo 	    } else {
7324bb7ae9dSLuigi Rizzo 		double m = (y2-y1)/(x2-x1);
7334bb7ae9dSLuigi Rizzo 		double c = y1 - m*x1;
734cc4d3c30SLuigi Rizzo 		for (; ix<stop ; ++ix)
735cc4d3c30SLuigi Rizzo 		    p->samples[ix] = (ix - c)/m;
7364bb7ae9dSLuigi Rizzo 	    }
7374bb7ae9dSLuigi Rizzo 	}
7384bb7ae9dSLuigi Rizzo 	p->samples_no = samples;
7394bb7ae9dSLuigi Rizzo 	p->loss_level = loss * samples;
7404bb7ae9dSLuigi Rizzo 	strncpy(p->name, profile_name, sizeof(p->name));
7414bb7ae9dSLuigi Rizzo }
7424bb7ae9dSLuigi Rizzo 
743cc4d3c30SLuigi Rizzo /*
744cc4d3c30SLuigi Rizzo  * configuration of pipes, schedulers, flowsets.
745cc4d3c30SLuigi Rizzo  * When we configure a new scheduler, an empty pipe is created, so:
746cc4d3c30SLuigi Rizzo  *
747cc4d3c30SLuigi Rizzo  * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
748cc4d3c30SLuigi Rizzo  *	sched N+Delta type fifo sched_mask ...
749cc4d3c30SLuigi Rizzo  *	pipe N+Delta <parameters>
750cc4d3c30SLuigi Rizzo  *	flowset N+Delta pipe N+Delta (no parameters)
751cc4d3c30SLuigi Rizzo  *	sched N type wf2q+ sched_mask ...
752cc4d3c30SLuigi Rizzo  *	pipe N <parameters>
753cc4d3c30SLuigi Rizzo  *
754cc4d3c30SLuigi Rizzo  * do_pipe = 2 -> flowset N config
755cc4d3c30SLuigi Rizzo  *	flowset N parameters
756cc4d3c30SLuigi Rizzo  *
757cc4d3c30SLuigi Rizzo  * do_pipe = 3 -> sched N config
758cc4d3c30SLuigi Rizzo  *	sched N parameters (default no pipe)
759cc4d3c30SLuigi Rizzo  *	optional Pipe N config ...
760cc4d3c30SLuigi Rizzo  * pipe ==>
761cc4d3c30SLuigi Rizzo  */
7624e9c8ae7SLuigi Rizzo void
7634e9c8ae7SLuigi Rizzo ipfw_config_pipe(int ac, char **av)
7644e9c8ae7SLuigi Rizzo {
7656a7bb02dSLuigi Rizzo 	int i;
7666a7bb02dSLuigi Rizzo 	u_int j;
7674e9c8ae7SLuigi Rizzo 	char *end;
768cc4d3c30SLuigi Rizzo 	struct dn_id *buf, *base;
769cc4d3c30SLuigi Rizzo 	struct dn_sch *sch = NULL;
770cc4d3c30SLuigi Rizzo 	struct dn_link *p = NULL;
771cc4d3c30SLuigi Rizzo 	struct dn_fs *fs = NULL;
772cc4d3c30SLuigi Rizzo 	struct dn_profile *pf = NULL;
773cc4d3c30SLuigi Rizzo 	struct ipfw_flow_id *mask = NULL;
774cc4d3c30SLuigi Rizzo 	int lmax;
775cc4d3c30SLuigi Rizzo 	uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
7764e9c8ae7SLuigi Rizzo 
777cc4d3c30SLuigi Rizzo 	/*
778cc4d3c30SLuigi Rizzo 	 * allocate space for 1 header,
779cc4d3c30SLuigi Rizzo 	 * 1 scheduler, 1 link, 1 flowset, 1 profile
780cc4d3c30SLuigi Rizzo 	 */
781cc4d3c30SLuigi Rizzo 	lmax = sizeof(struct dn_id);	/* command header */
782cc4d3c30SLuigi Rizzo 	lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
783cc4d3c30SLuigi Rizzo 		sizeof(struct dn_fs) + sizeof(struct dn_profile);
7844e9c8ae7SLuigi Rizzo 
7854e9c8ae7SLuigi Rizzo 	av++; ac--;
7864e9c8ae7SLuigi Rizzo 	/* Pipe number */
7874e9c8ae7SLuigi Rizzo 	if (ac && isdigit(**av)) {
7884e9c8ae7SLuigi Rizzo 		i = atoi(*av); av++; ac--;
789cc4d3c30SLuigi Rizzo 	} else
790cc4d3c30SLuigi Rizzo 		i = -1;
791cc4d3c30SLuigi Rizzo 	if (i <= 0)
792cc4d3c30SLuigi Rizzo 		errx(EX_USAGE, "need a pipe/flowset/sched number");
793cc4d3c30SLuigi Rizzo 	base = buf = safe_calloc(1, lmax);
794cc4d3c30SLuigi Rizzo 	/* all commands start with a 'CONFIGURE' and a version */
795cc4d3c30SLuigi Rizzo 	o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
796cc4d3c30SLuigi Rizzo 	base->id = DN_API_VERSION;
797cc4d3c30SLuigi Rizzo 
798cc4d3c30SLuigi Rizzo 	switch (co.do_pipe) {
799cc4d3c30SLuigi Rizzo 	case 1: /* "pipe N config ..." */
800cc4d3c30SLuigi Rizzo 		/* Allocate space for the WF2Q+ scheduler, its link
801cc4d3c30SLuigi Rizzo 		 * and the FIFO flowset. Set the number, but leave
802cc4d3c30SLuigi Rizzo 		 * the scheduler subtype and other parameters to 0
803cc4d3c30SLuigi Rizzo 		 * so the kernel will use appropriate defaults.
804cc4d3c30SLuigi Rizzo 		 * XXX todo: add a flag to record if a parameter
805cc4d3c30SLuigi Rizzo 		 * is actually configured.
806cc4d3c30SLuigi Rizzo 		 * If we do a 'pipe config' mask -> sched_mask.
807cc4d3c30SLuigi Rizzo 		 * The FIFO scheduler and link are derived from the
808cc4d3c30SLuigi Rizzo 		 * WF2Q+ one in the kernel.
809cc4d3c30SLuigi Rizzo 		 */
810cc4d3c30SLuigi Rizzo 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
811cc4d3c30SLuigi Rizzo 		p = o_next(&buf, sizeof(*p), DN_LINK);
812cc4d3c30SLuigi Rizzo 		fs = o_next(&buf, sizeof(*fs), DN_FS);
813cc4d3c30SLuigi Rizzo 
814cc4d3c30SLuigi Rizzo 		sch->sched_nr = i;
815cc4d3c30SLuigi Rizzo 		sch->oid.subtype = 0;	/* defaults to WF2Q+ */
816cc4d3c30SLuigi Rizzo 		mask = &sch->sched_mask;
817cc4d3c30SLuigi Rizzo 		flags = &sch->flags;
818cc4d3c30SLuigi Rizzo 		buckets = &sch->buckets;
819cc4d3c30SLuigi Rizzo 		*flags |= DN_PIPE_CMD;
820cc4d3c30SLuigi Rizzo 
821cc4d3c30SLuigi Rizzo 		p->link_nr = i;
822cc4d3c30SLuigi Rizzo 
823cc4d3c30SLuigi Rizzo 		/* This flowset is only for the FIFO scheduler */
824cc4d3c30SLuigi Rizzo 		fs->fs_nr = i + 2*DN_MAX_ID;
825cc4d3c30SLuigi Rizzo 		fs->sched_nr = i + DN_MAX_ID;
826cc4d3c30SLuigi Rizzo 		break;
827cc4d3c30SLuigi Rizzo 
828cc4d3c30SLuigi Rizzo 	case 2: /* "queue N config ... " */
829cc4d3c30SLuigi Rizzo 		fs = o_next(&buf, sizeof(*fs), DN_FS);
830cc4d3c30SLuigi Rizzo 		fs->fs_nr = i;
831cc4d3c30SLuigi Rizzo 		mask = &fs->flow_mask;
832cc4d3c30SLuigi Rizzo 		flags = &fs->flags;
833cc4d3c30SLuigi Rizzo 		buckets = &fs->buckets;
834cc4d3c30SLuigi Rizzo 		break;
835cc4d3c30SLuigi Rizzo 
836cc4d3c30SLuigi Rizzo 	case 3: /* "sched N config ..." */
837cc4d3c30SLuigi Rizzo 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
838cc4d3c30SLuigi Rizzo 		fs = o_next(&buf, sizeof(*fs), DN_FS);
839cc4d3c30SLuigi Rizzo 		sch->sched_nr = i;
840cc4d3c30SLuigi Rizzo 		mask = &sch->sched_mask;
841cc4d3c30SLuigi Rizzo 		flags = &sch->flags;
842cc4d3c30SLuigi Rizzo 		buckets = &sch->buckets;
843cc4d3c30SLuigi Rizzo 		/* fs is used only with !MULTIQUEUE schedulers */
844cc4d3c30SLuigi Rizzo 		fs->fs_nr = i + DN_MAX_ID;
845cc4d3c30SLuigi Rizzo 		fs->sched_nr = i;
846cc4d3c30SLuigi Rizzo 		break;
8474e9c8ae7SLuigi Rizzo 	}
848cc4d3c30SLuigi Rizzo 	/* set to -1 those fields for which we want to reuse existing
849cc4d3c30SLuigi Rizzo 	 * values from the kernel.
850cc4d3c30SLuigi Rizzo 	 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
851cc4d3c30SLuigi Rizzo 	 * XXX todo: support reuse of the mask.
852cc4d3c30SLuigi Rizzo 	 */
853cc4d3c30SLuigi Rizzo 	if (p)
854cc4d3c30SLuigi Rizzo 		p->bandwidth = -1;
855cc4d3c30SLuigi Rizzo 	for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
856cc4d3c30SLuigi Rizzo 		fs->par[j] = -1;
8574e9c8ae7SLuigi Rizzo 	while (ac > 0) {
8584e9c8ae7SLuigi Rizzo 		double d;
8594e9c8ae7SLuigi Rizzo 		int tok = match_token(dummynet_params, *av);
8604e9c8ae7SLuigi Rizzo 		ac--; av++;
8614e9c8ae7SLuigi Rizzo 
8624e9c8ae7SLuigi Rizzo 		switch(tok) {
8634e9c8ae7SLuigi Rizzo 		case TOK_NOERROR:
864cc4d3c30SLuigi Rizzo 			NEED(fs, "noerror is only for pipes");
865cc4d3c30SLuigi Rizzo 			fs->flags |= DN_NOERROR;
8664e9c8ae7SLuigi Rizzo 			break;
8674e9c8ae7SLuigi Rizzo 
8684e9c8ae7SLuigi Rizzo 		case TOK_PLR:
869cc4d3c30SLuigi Rizzo 			NEED(fs, "plr is only for pipes");
8704e9c8ae7SLuigi Rizzo 			NEED1("plr needs argument 0..1\n");
8714e9c8ae7SLuigi Rizzo 			d = strtod(av[0], NULL);
8724e9c8ae7SLuigi Rizzo 			if (d > 1)
8734e9c8ae7SLuigi Rizzo 				d = 1;
8744e9c8ae7SLuigi Rizzo 			else if (d < 0)
8754e9c8ae7SLuigi Rizzo 				d = 0;
876cc4d3c30SLuigi Rizzo 			fs->plr = (int)(d*0x7fffffff);
8774e9c8ae7SLuigi Rizzo 			ac--; av++;
8784e9c8ae7SLuigi Rizzo 			break;
8794e9c8ae7SLuigi Rizzo 
8804e9c8ae7SLuigi Rizzo 		case TOK_QUEUE:
881cc4d3c30SLuigi Rizzo 			NEED(fs, "queue is only for pipes or flowsets");
8824e9c8ae7SLuigi Rizzo 			NEED1("queue needs queue size\n");
8834e9c8ae7SLuigi Rizzo 			end = NULL;
884cc4d3c30SLuigi Rizzo 			fs->qsize = strtoul(av[0], &end, 0);
8854e9c8ae7SLuigi Rizzo 			if (*end == 'K' || *end == 'k') {
886cc4d3c30SLuigi Rizzo 				fs->flags |= DN_QSIZE_BYTES;
887cc4d3c30SLuigi Rizzo 				fs->qsize *= 1024;
8884e9c8ae7SLuigi Rizzo 			} else if (*end == 'B' ||
8894e9c8ae7SLuigi Rizzo 			    _substrcmp2(end, "by", "bytes") == 0) {
890cc4d3c30SLuigi Rizzo 				fs->flags |= DN_QSIZE_BYTES;
8914e9c8ae7SLuigi Rizzo 			}
8924e9c8ae7SLuigi Rizzo 			ac--; av++;
8934e9c8ae7SLuigi Rizzo 			break;
8944e9c8ae7SLuigi Rizzo 
8954e9c8ae7SLuigi Rizzo 		case TOK_BUCKETS:
896cc4d3c30SLuigi Rizzo 			NEED(fs, "buckets is only for pipes or flowsets");
8974e9c8ae7SLuigi Rizzo 			NEED1("buckets needs argument\n");
898cc4d3c30SLuigi Rizzo 			*buckets = strtoul(av[0], NULL, 0);
8994e9c8ae7SLuigi Rizzo 			ac--; av++;
9004e9c8ae7SLuigi Rizzo 			break;
9014e9c8ae7SLuigi Rizzo 
902cc4d3c30SLuigi Rizzo 		case TOK_FLOW_MASK:
903cc4d3c30SLuigi Rizzo 		case TOK_SCHED_MASK:
9044e9c8ae7SLuigi Rizzo 		case TOK_MASK:
905cc4d3c30SLuigi Rizzo 			NEED(mask, "tok_mask");
9064e9c8ae7SLuigi Rizzo 			NEED1("mask needs mask specifier\n");
9074e9c8ae7SLuigi Rizzo 			/*
9084e9c8ae7SLuigi Rizzo 			 * per-flow queue, mask is dst_ip, dst_port,
9094e9c8ae7SLuigi Rizzo 			 * src_ip, src_port, proto measured in bits
9104e9c8ae7SLuigi Rizzo 			 */
9114e9c8ae7SLuigi Rizzo 
912cc4d3c30SLuigi Rizzo 			bzero(mask, sizeof(*mask));
9134e9c8ae7SLuigi Rizzo 			end = NULL;
9144e9c8ae7SLuigi Rizzo 
9154e9c8ae7SLuigi Rizzo 			while (ac >= 1) {
9164e9c8ae7SLuigi Rizzo 			    uint32_t *p32 = NULL;
9174e9c8ae7SLuigi Rizzo 			    uint16_t *p16 = NULL;
9184e9c8ae7SLuigi Rizzo 			    uint32_t *p20 = NULL;
9194e9c8ae7SLuigi Rizzo 			    struct in6_addr *pa6 = NULL;
9204e9c8ae7SLuigi Rizzo 			    uint32_t a;
9214e9c8ae7SLuigi Rizzo 
9224e9c8ae7SLuigi Rizzo 			    tok = match_token(dummynet_params, *av);
9234e9c8ae7SLuigi Rizzo 			    ac--; av++;
9244e9c8ae7SLuigi Rizzo 			    switch(tok) {
9254e9c8ae7SLuigi Rizzo 			    case TOK_ALL:
9264e9c8ae7SLuigi Rizzo 				    /*
9274e9c8ae7SLuigi Rizzo 				     * special case, all bits significant
928f9f7bde3SLuigi Rizzo 				     * except 'extra' (the queue number)
9294e9c8ae7SLuigi Rizzo 				     */
930cc4d3c30SLuigi Rizzo 				    mask->dst_ip = ~0;
931cc4d3c30SLuigi Rizzo 				    mask->src_ip = ~0;
932cc4d3c30SLuigi Rizzo 				    mask->dst_port = ~0;
933cc4d3c30SLuigi Rizzo 				    mask->src_port = ~0;
934cc4d3c30SLuigi Rizzo 				    mask->proto = ~0;
935cc4d3c30SLuigi Rizzo 				    n2mask(&mask->dst_ip6, 128);
936cc4d3c30SLuigi Rizzo 				    n2mask(&mask->src_ip6, 128);
937cc4d3c30SLuigi Rizzo 				    mask->flow_id6 = ~0;
938cc4d3c30SLuigi Rizzo 				    *flags |= DN_HAVE_MASK;
9394e9c8ae7SLuigi Rizzo 				    goto end_mask;
9404e9c8ae7SLuigi Rizzo 
941f9f7bde3SLuigi Rizzo 			    case TOK_QUEUE:
942f9f7bde3SLuigi Rizzo 				    mask->extra = ~0;
943f9f7bde3SLuigi Rizzo 				    *flags |= DN_HAVE_MASK;
944f9f7bde3SLuigi Rizzo 				    goto end_mask;
945f9f7bde3SLuigi Rizzo 
9464e9c8ae7SLuigi Rizzo 			    case TOK_DSTIP:
947cc4d3c30SLuigi Rizzo 				    mask->addr_type = 4;
948cc4d3c30SLuigi Rizzo 				    p32 = &mask->dst_ip;
9494e9c8ae7SLuigi Rizzo 				    break;
9504e9c8ae7SLuigi Rizzo 
9514e9c8ae7SLuigi Rizzo 			    case TOK_SRCIP:
952cc4d3c30SLuigi Rizzo 				    mask->addr_type = 4;
953cc4d3c30SLuigi Rizzo 				    p32 = &mask->src_ip;
9544e9c8ae7SLuigi Rizzo 				    break;
9554e9c8ae7SLuigi Rizzo 
9564e9c8ae7SLuigi Rizzo 			    case TOK_DSTIP6:
957cc4d3c30SLuigi Rizzo 				    mask->addr_type = 6;
958cc4d3c30SLuigi Rizzo 				    pa6 = &mask->dst_ip6;
9594e9c8ae7SLuigi Rizzo 				    break;
9604e9c8ae7SLuigi Rizzo 
9614e9c8ae7SLuigi Rizzo 			    case TOK_SRCIP6:
962cc4d3c30SLuigi Rizzo 				    mask->addr_type = 6;
963cc4d3c30SLuigi Rizzo 				    pa6 = &mask->src_ip6;
9644e9c8ae7SLuigi Rizzo 				    break;
9654e9c8ae7SLuigi Rizzo 
9664e9c8ae7SLuigi Rizzo 			    case TOK_FLOWID:
967cc4d3c30SLuigi Rizzo 				    mask->addr_type = 6;
968cc4d3c30SLuigi Rizzo 				    p20 = &mask->flow_id6;
9694e9c8ae7SLuigi Rizzo 				    break;
9704e9c8ae7SLuigi Rizzo 
9714e9c8ae7SLuigi Rizzo 			    case TOK_DSTPORT:
972cc4d3c30SLuigi Rizzo 				    p16 = &mask->dst_port;
9734e9c8ae7SLuigi Rizzo 				    break;
9744e9c8ae7SLuigi Rizzo 
9754e9c8ae7SLuigi Rizzo 			    case TOK_SRCPORT:
976cc4d3c30SLuigi Rizzo 				    p16 = &mask->src_port;
9774e9c8ae7SLuigi Rizzo 				    break;
9784e9c8ae7SLuigi Rizzo 
9794e9c8ae7SLuigi Rizzo 			    case TOK_PROTO:
9804e9c8ae7SLuigi Rizzo 				    break;
9814e9c8ae7SLuigi Rizzo 
9824e9c8ae7SLuigi Rizzo 			    default:
9834e9c8ae7SLuigi Rizzo 				    ac++; av--; /* backtrack */
9844e9c8ae7SLuigi Rizzo 				    goto end_mask;
9854e9c8ae7SLuigi Rizzo 			    }
9864e9c8ae7SLuigi Rizzo 			    if (ac < 1)
9874e9c8ae7SLuigi Rizzo 				    errx(EX_USAGE, "mask: value missing");
9884e9c8ae7SLuigi Rizzo 			    if (*av[0] == '/') {
9894e9c8ae7SLuigi Rizzo 				    a = strtoul(av[0]+1, &end, 0);
9904e9c8ae7SLuigi Rizzo 				    if (pa6 == NULL)
9914e9c8ae7SLuigi Rizzo 					    a = (a == 32) ? ~0 : (1 << a) - 1;
9924e9c8ae7SLuigi Rizzo 			    } else
9934e9c8ae7SLuigi Rizzo 				    a = strtoul(av[0], &end, 0);
9944e9c8ae7SLuigi Rizzo 			    if (p32 != NULL)
9954e9c8ae7SLuigi Rizzo 				    *p32 = a;
9964e9c8ae7SLuigi Rizzo 			    else if (p16 != NULL) {
9974e9c8ae7SLuigi Rizzo 				    if (a > 0xFFFF)
9984e9c8ae7SLuigi Rizzo 					    errx(EX_DATAERR,
9994e9c8ae7SLuigi Rizzo 						"port mask must be 16 bit");
10004e9c8ae7SLuigi Rizzo 				    *p16 = (uint16_t)a;
10014e9c8ae7SLuigi Rizzo 			    } else if (p20 != NULL) {
10024e9c8ae7SLuigi Rizzo 				    if (a > 0xfffff)
10034e9c8ae7SLuigi Rizzo 					errx(EX_DATAERR,
10044e9c8ae7SLuigi Rizzo 					    "flow_id mask must be 20 bit");
10054e9c8ae7SLuigi Rizzo 				    *p20 = (uint32_t)a;
10064e9c8ae7SLuigi Rizzo 			    } else if (pa6 != NULL) {
10074e9c8ae7SLuigi Rizzo 				    if (a > 128)
10084e9c8ae7SLuigi Rizzo 					errx(EX_DATAERR,
10094e9c8ae7SLuigi Rizzo 					    "in6addr invalid mask len");
10104e9c8ae7SLuigi Rizzo 				    else
10114e9c8ae7SLuigi Rizzo 					n2mask(pa6, a);
10124e9c8ae7SLuigi Rizzo 			    } else {
10134e9c8ae7SLuigi Rizzo 				    if (a > 0xFF)
10144e9c8ae7SLuigi Rizzo 					    errx(EX_DATAERR,
10154e9c8ae7SLuigi Rizzo 						"proto mask must be 8 bit");
1016f9f7bde3SLuigi Rizzo 				    mask->proto = (uint8_t)a;
10174e9c8ae7SLuigi Rizzo 			    }
10184e9c8ae7SLuigi Rizzo 			    if (a != 0)
1019cc4d3c30SLuigi Rizzo 				    *flags |= DN_HAVE_MASK;
10204e9c8ae7SLuigi Rizzo 			    ac--; av++;
10214e9c8ae7SLuigi Rizzo 			} /* end while, config masks */
10224e9c8ae7SLuigi Rizzo end_mask:
10234e9c8ae7SLuigi Rizzo 			break;
10244e9c8ae7SLuigi Rizzo 
10254e9c8ae7SLuigi Rizzo 		case TOK_RED:
10264e9c8ae7SLuigi Rizzo 		case TOK_GRED:
10274e9c8ae7SLuigi Rizzo 			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1028cc4d3c30SLuigi Rizzo 			fs->flags |= DN_IS_RED;
10294e9c8ae7SLuigi Rizzo 			if (tok == TOK_GRED)
1030cc4d3c30SLuigi Rizzo 				fs->flags |= DN_IS_GENTLE_RED;
10314e9c8ae7SLuigi Rizzo 			/*
10324e9c8ae7SLuigi Rizzo 			 * the format for parameters is w_q/min_th/max_th/max_p
10334e9c8ae7SLuigi Rizzo 			 */
10344e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
10354e9c8ae7SLuigi Rizzo 			    double w_q = strtod(end, NULL);
10364e9c8ae7SLuigi Rizzo 			    if (w_q > 1 || w_q <= 0)
10374e9c8ae7SLuigi Rizzo 				errx(EX_DATAERR, "0 < w_q <= 1");
1038cc4d3c30SLuigi Rizzo 			    fs->w_q = (int) (w_q * (1 << SCALE_RED));
10394e9c8ae7SLuigi Rizzo 			}
10404e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
1041cc4d3c30SLuigi Rizzo 			    fs->min_th = strtoul(end, &end, 0);
10424e9c8ae7SLuigi Rizzo 			    if (*end == 'K' || *end == 'k')
1043cc4d3c30SLuigi Rizzo 				fs->min_th *= 1024;
10444e9c8ae7SLuigi Rizzo 			}
10454e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
1046cc4d3c30SLuigi Rizzo 			    fs->max_th = strtoul(end, &end, 0);
10474e9c8ae7SLuigi Rizzo 			    if (*end == 'K' || *end == 'k')
1048cc4d3c30SLuigi Rizzo 				fs->max_th *= 1024;
10494e9c8ae7SLuigi Rizzo 			}
10504e9c8ae7SLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
10514e9c8ae7SLuigi Rizzo 			    double max_p = strtod(end, NULL);
1052*fc5e1956SHiren Panchasara 			    if (max_p > 1 || max_p < 0)
1053*fc5e1956SHiren Panchasara 				errx(EX_DATAERR, "0 <= max_p <= 1");
1054cc4d3c30SLuigi Rizzo 			    fs->max_p = (int)(max_p * (1 << SCALE_RED));
10554e9c8ae7SLuigi Rizzo 			}
10564e9c8ae7SLuigi Rizzo 			ac--; av++;
10574e9c8ae7SLuigi Rizzo 			break;
10584e9c8ae7SLuigi Rizzo 
1059*fc5e1956SHiren Panchasara 		case TOK_ECN:
1060*fc5e1956SHiren Panchasara 			fs->flags |= DN_IS_ECN;
1061*fc5e1956SHiren Panchasara 			break;
1062*fc5e1956SHiren Panchasara 
10634e9c8ae7SLuigi Rizzo 		case TOK_DROPTAIL:
1064cc4d3c30SLuigi Rizzo 			NEED(fs, "droptail is only for flowsets");
1065cc4d3c30SLuigi Rizzo 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
10664e9c8ae7SLuigi Rizzo 			break;
10674e9c8ae7SLuigi Rizzo 
10684e9c8ae7SLuigi Rizzo 		case TOK_BW:
1069cc4d3c30SLuigi Rizzo 			NEED(p, "bw is only for links");
10704e9c8ae7SLuigi Rizzo 			NEED1("bw needs bandwidth or interface\n");
1071cc4d3c30SLuigi Rizzo 			read_bandwidth(av[0], &p->bandwidth, NULL, 0);
10724e9c8ae7SLuigi Rizzo 			ac--; av++;
10734e9c8ae7SLuigi Rizzo 			break;
10744e9c8ae7SLuigi Rizzo 
10754e9c8ae7SLuigi Rizzo 		case TOK_DELAY:
1076cc4d3c30SLuigi Rizzo 			NEED(p, "delay is only for links");
10774e9c8ae7SLuigi Rizzo 			NEED1("delay needs argument 0..10000ms\n");
1078cc4d3c30SLuigi Rizzo 			p->delay = strtoul(av[0], NULL, 0);
10794e9c8ae7SLuigi Rizzo 			ac--; av++;
10804e9c8ae7SLuigi Rizzo 			break;
10814e9c8ae7SLuigi Rizzo 
1082cc4d3c30SLuigi Rizzo 		case TOK_TYPE: {
1083cc4d3c30SLuigi Rizzo 			int l;
1084cc4d3c30SLuigi Rizzo 			NEED(sch, "type is only for schedulers");
1085cc4d3c30SLuigi Rizzo 			NEED1("type needs a string");
1086cc4d3c30SLuigi Rizzo 			l = strlen(av[0]);
1087cc4d3c30SLuigi Rizzo 			if (l == 0 || l > 15)
1088cc4d3c30SLuigi Rizzo 				errx(1, "type %s too long\n", av[0]);
1089cc4d3c30SLuigi Rizzo 			strcpy(sch->name, av[0]);
1090cc4d3c30SLuigi Rizzo 			sch->oid.subtype = 0; /* use string */
1091cc4d3c30SLuigi Rizzo 			ac--; av++;
1092cc4d3c30SLuigi Rizzo 			break;
1093cc4d3c30SLuigi Rizzo 		    }
1094cc4d3c30SLuigi Rizzo 
10954e9c8ae7SLuigi Rizzo 		case TOK_WEIGHT:
1096cc4d3c30SLuigi Rizzo 			NEED(fs, "weight is only for flowsets");
1097cc4d3c30SLuigi Rizzo 			NEED1("weight needs argument\n");
1098cc4d3c30SLuigi Rizzo 			fs->par[0] = strtol(av[0], &end, 0);
10994e9c8ae7SLuigi Rizzo 			ac--; av++;
11004e9c8ae7SLuigi Rizzo 			break;
11014e9c8ae7SLuigi Rizzo 
1102cc4d3c30SLuigi Rizzo 		case TOK_LMAX:
1103cc4d3c30SLuigi Rizzo 			NEED(fs, "lmax is only for flowsets");
1104cc4d3c30SLuigi Rizzo 			NEED1("lmax needs argument\n");
1105cc4d3c30SLuigi Rizzo 			fs->par[1] = strtol(av[0], &end, 0);
1106cc4d3c30SLuigi Rizzo 			ac--; av++;
1107cc4d3c30SLuigi Rizzo 			break;
1108cc4d3c30SLuigi Rizzo 
1109cc4d3c30SLuigi Rizzo 		case TOK_PRI:
1110cc4d3c30SLuigi Rizzo 			NEED(fs, "priority is only for flowsets");
1111cc4d3c30SLuigi Rizzo 			NEED1("priority needs argument\n");
1112cc4d3c30SLuigi Rizzo 			fs->par[2] = strtol(av[0], &end, 0);
1113cc4d3c30SLuigi Rizzo 			ac--; av++;
1114cc4d3c30SLuigi Rizzo 			break;
1115cc4d3c30SLuigi Rizzo 
1116cc4d3c30SLuigi Rizzo 		case TOK_SCHED:
11174e9c8ae7SLuigi Rizzo 		case TOK_PIPE:
1118cc4d3c30SLuigi Rizzo 			NEED(fs, "pipe/sched");
1119cc4d3c30SLuigi Rizzo 			NEED1("pipe/link/sched needs number\n");
1120cc4d3c30SLuigi Rizzo 			fs->sched_nr = strtoul(av[0], &end, 0);
11214e9c8ae7SLuigi Rizzo 			ac--; av++;
11224e9c8ae7SLuigi Rizzo 			break;
11234e9c8ae7SLuigi Rizzo 
1124cc4d3c30SLuigi Rizzo 		case TOK_PROFILE:
1125cc4d3c30SLuigi Rizzo 			NEED((!pf), "profile already set");
1126cc4d3c30SLuigi Rizzo 			NEED(p, "profile");
1127cc4d3c30SLuigi Rizzo 		    {
11284bb7ae9dSLuigi Rizzo 			NEED1("extra delay needs the file name\n");
1129cc4d3c30SLuigi Rizzo 			pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1130cc4d3c30SLuigi Rizzo 			load_extra_delays(av[0], pf, p); //XXX can't fail?
11314bb7ae9dSLuigi Rizzo 			--ac; ++av;
1132cc4d3c30SLuigi Rizzo 		    }
11334bb7ae9dSLuigi Rizzo 			break;
11344bb7ae9dSLuigi Rizzo 
11356882bf4dSOleg Bulyzhin 		case TOK_BURST:
1136cc4d3c30SLuigi Rizzo 			NEED(p, "burst");
11376882bf4dSOleg Bulyzhin 			NEED1("burst needs argument\n");
11386882bf4dSOleg Bulyzhin 			errno = 0;
11391727cdb6SDag-Erling Smørgrav 			if (expand_number(av[0], &p->burst) < 0)
11406882bf4dSOleg Bulyzhin 				if (errno != ERANGE)
11416882bf4dSOleg Bulyzhin 					errx(EX_DATAERR,
11426882bf4dSOleg Bulyzhin 					    "burst: invalid argument");
1143cc4d3c30SLuigi Rizzo 			if (errno || p->burst > (1ULL << 48) - 1)
11446882bf4dSOleg Bulyzhin 				errx(EX_DATAERR,
11456882bf4dSOleg Bulyzhin 				    "burst: out of range (0..2^48-1)");
11466882bf4dSOleg Bulyzhin 			ac--; av++;
11476882bf4dSOleg Bulyzhin 			break;
11486882bf4dSOleg Bulyzhin 
11494e9c8ae7SLuigi Rizzo 		default:
11504e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
11514e9c8ae7SLuigi Rizzo 		}
11524e9c8ae7SLuigi Rizzo 	}
1153cc4d3c30SLuigi Rizzo 
1154cc4d3c30SLuigi Rizzo 	/* check validity of parameters */
1155cc4d3c30SLuigi Rizzo 	if (p) {
1156cc4d3c30SLuigi Rizzo 		if (p->delay > 10000)
11574e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "delay must be < 10000");
1158cc4d3c30SLuigi Rizzo 		if (p->bandwidth == -1)
1159cc4d3c30SLuigi Rizzo 			p->bandwidth = 0;
11604e9c8ae7SLuigi Rizzo 	}
1161cc4d3c30SLuigi Rizzo 	if (fs) {
1162cc4d3c30SLuigi Rizzo 		/* XXX accept a 0 scheduler to keep the default */
1163cc4d3c30SLuigi Rizzo 	    if (fs->flags & DN_QSIZE_BYTES) {
11644e9c8ae7SLuigi Rizzo 		size_t len;
11654e9c8ae7SLuigi Rizzo 		long limit;
11664e9c8ae7SLuigi Rizzo 
11674e9c8ae7SLuigi Rizzo 		len = sizeof(limit);
11684e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
11694e9c8ae7SLuigi Rizzo 			&limit, &len, NULL, 0) == -1)
11704e9c8ae7SLuigi Rizzo 			limit = 1024*1024;
1171cc4d3c30SLuigi Rizzo 		if (fs->qsize > limit)
11724e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "queue size must be < %ldB", limit);
11734e9c8ae7SLuigi Rizzo 	    } else {
11744e9c8ae7SLuigi Rizzo 		size_t len;
11754e9c8ae7SLuigi Rizzo 		long limit;
11764e9c8ae7SLuigi Rizzo 
11774e9c8ae7SLuigi Rizzo 		len = sizeof(limit);
11784e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
11794e9c8ae7SLuigi Rizzo 			&limit, &len, NULL, 0) == -1)
11804e9c8ae7SLuigi Rizzo 			limit = 100;
1181cc4d3c30SLuigi Rizzo 		if (fs->qsize > limit)
11824e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
11834e9c8ae7SLuigi Rizzo 	    }
1184cc4d3c30SLuigi Rizzo 
1185*fc5e1956SHiren Panchasara 	    if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1186*fc5e1956SHiren Panchasara 		errx(EX_USAGE, "enable red/gred for ECN");
1187*fc5e1956SHiren Panchasara 
1188cc4d3c30SLuigi Rizzo 	    if (fs->flags & DN_IS_RED) {
11894e9c8ae7SLuigi Rizzo 		size_t len;
11904e9c8ae7SLuigi Rizzo 		int lookup_depth, avg_pkt_size;
11914e9c8ae7SLuigi Rizzo 
1192*fc5e1956SHiren Panchasara 		if (!(fs->flags & DN_IS_ECN) && (fs->min_th >= fs->max_th))
11934e9c8ae7SLuigi Rizzo 		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1194cc4d3c30SLuigi Rizzo 			fs->min_th, fs->max_th);
1195*fc5e1956SHiren Panchasara 		else if ((fs->flags & DN_IS_ECN) && (fs->min_th > fs->max_th))
1196*fc5e1956SHiren Panchasara 		    errx(EX_DATAERR, "min_th %d must be =< than max_th %d",
1197*fc5e1956SHiren Panchasara 			fs->min_th, fs->max_th);
1198*fc5e1956SHiren Panchasara 
1199cc4d3c30SLuigi Rizzo 		if (fs->max_th == 0)
12004e9c8ae7SLuigi Rizzo 		    errx(EX_DATAERR, "max_th must be > 0");
12014e9c8ae7SLuigi Rizzo 
12024e9c8ae7SLuigi Rizzo 		len = sizeof(int);
12034e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
12044e9c8ae7SLuigi Rizzo 			&lookup_depth, &len, NULL, 0) == -1)
1205cc4d3c30SLuigi Rizzo 			lookup_depth = 256;
12064e9c8ae7SLuigi Rizzo 		if (lookup_depth == 0)
12074e9c8ae7SLuigi Rizzo 		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
12084e9c8ae7SLuigi Rizzo 			" must be greater than zero");
12094e9c8ae7SLuigi Rizzo 
12104e9c8ae7SLuigi Rizzo 		len = sizeof(int);
12114e9c8ae7SLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
12124e9c8ae7SLuigi Rizzo 			&avg_pkt_size, &len, NULL, 0) == -1)
1213cc4d3c30SLuigi Rizzo 			avg_pkt_size = 512;
12144e9c8ae7SLuigi Rizzo 
12154e9c8ae7SLuigi Rizzo 		if (avg_pkt_size == 0)
12164e9c8ae7SLuigi Rizzo 			errx(EX_DATAERR,
12174e9c8ae7SLuigi Rizzo 			    "net.inet.ip.dummynet.red_avg_pkt_size must"
12184e9c8ae7SLuigi Rizzo 			    " be greater than zero");
12194e9c8ae7SLuigi Rizzo 
1220fa8d2a59SLuigi Rizzo #if 0 /* the following computation is now done in the kernel */
12214e9c8ae7SLuigi Rizzo 		/*
12224e9c8ae7SLuigi Rizzo 		 * Ticks needed for sending a medium-sized packet.
12234e9c8ae7SLuigi Rizzo 		 * Unfortunately, when we are configuring a WF2Q+ queue, we
12244e9c8ae7SLuigi Rizzo 		 * do not have bandwidth information, because that is stored
12254e9c8ae7SLuigi Rizzo 		 * in the parent pipe, and also we have multiple queues
12264e9c8ae7SLuigi Rizzo 		 * competing for it. So we set s=0, which is not very
12274e9c8ae7SLuigi Rizzo 		 * correct. But on the other hand, why do we want RED with
12284e9c8ae7SLuigi Rizzo 		 * WF2Q+ ?
12294e9c8ae7SLuigi Rizzo 		 */
12304e9c8ae7SLuigi Rizzo 		if (p.bandwidth==0) /* this is a WF2Q+ queue */
12314e9c8ae7SLuigi Rizzo 			s = 0;
12324e9c8ae7SLuigi Rizzo 		else
12334e9c8ae7SLuigi Rizzo 			s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
12344e9c8ae7SLuigi Rizzo 		/*
12354e9c8ae7SLuigi Rizzo 		 * max idle time (in ticks) before avg queue size becomes 0.
12364e9c8ae7SLuigi Rizzo 		 * NOTA:  (3/w_q) is approx the value x so that
12374e9c8ae7SLuigi Rizzo 		 * (1-w_q)^x < 10^-3.
12384e9c8ae7SLuigi Rizzo 		 */
1239cc4d3c30SLuigi Rizzo 		w_q = ((double)fs->w_q) / (1 << SCALE_RED);
12404e9c8ae7SLuigi Rizzo 		idle = s * 3. / w_q;
1241cc4d3c30SLuigi Rizzo 		fs->lookup_step = (int)idle / lookup_depth;
1242cc4d3c30SLuigi Rizzo 		if (!fs->lookup_step)
1243cc4d3c30SLuigi Rizzo 			fs->lookup_step = 1;
12444e9c8ae7SLuigi Rizzo 		weight = 1 - w_q;
1245cc4d3c30SLuigi Rizzo 		for (t = fs->lookup_step; t > 1; --t)
12464e9c8ae7SLuigi Rizzo 			weight *= 1 - w_q;
1247cc4d3c30SLuigi Rizzo 		fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1248fa8d2a59SLuigi Rizzo #endif /* code moved in the kernel */
12494e9c8ae7SLuigi Rizzo 	    }
12504bb7ae9dSLuigi Rizzo 	}
12514bb7ae9dSLuigi Rizzo 
1252cc4d3c30SLuigi Rizzo 	i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1253cc4d3c30SLuigi Rizzo 
12544e9c8ae7SLuigi Rizzo 	if (i)
12554e9c8ae7SLuigi Rizzo 		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
12564e9c8ae7SLuigi Rizzo }
1257cc4d3c30SLuigi Rizzo 
1258cc4d3c30SLuigi Rizzo void
1259cc4d3c30SLuigi Rizzo dummynet_flush(void)
1260cc4d3c30SLuigi Rizzo {
1261cc4d3c30SLuigi Rizzo 	struct dn_id oid;
1262cc4d3c30SLuigi Rizzo 	oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1263cc4d3c30SLuigi Rizzo 	do_cmd(IP_DUMMYNET3, &oid, oid.len);
1264cc4d3c30SLuigi Rizzo }
1265cc4d3c30SLuigi Rizzo 
12665007b59fSLuigi Rizzo /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
12675007b59fSLuigi Rizzo  * Returns the number of ranges, and possibly stores them
12685007b59fSLuigi Rizzo  * in the array v of size len.
12695007b59fSLuigi Rizzo  */
12705007b59fSLuigi Rizzo static int
12715007b59fSLuigi Rizzo parse_range(int ac, char *av[], uint32_t *v, int len)
12725007b59fSLuigi Rizzo {
12735007b59fSLuigi Rizzo 	int n = 0;
12745007b59fSLuigi Rizzo 	char *endptr, *s;
12755007b59fSLuigi Rizzo 	uint32_t base[2];
12765007b59fSLuigi Rizzo 
12775007b59fSLuigi Rizzo 	if (v == NULL || len < 2) {
12785007b59fSLuigi Rizzo 		v = base;
12795007b59fSLuigi Rizzo 		len = 2;
12805007b59fSLuigi Rizzo 	}
12815007b59fSLuigi Rizzo 
12825007b59fSLuigi Rizzo 	for (s = *av; s != NULL; av++, ac--) {
12835007b59fSLuigi Rizzo 		v[0] = strtoul(s, &endptr, 10);
12845007b59fSLuigi Rizzo 		v[1] = (*endptr != '-') ? v[0] :
12855007b59fSLuigi Rizzo 			 strtoul(endptr+1, &endptr, 10);
12865007b59fSLuigi Rizzo 		if (*endptr == '\0') { /* prepare for next round */
12875007b59fSLuigi Rizzo 			s = (ac > 0) ? *(av+1) : NULL;
12885007b59fSLuigi Rizzo 		} else {
12895007b59fSLuigi Rizzo 			if (*endptr != ',') {
12905007b59fSLuigi Rizzo 				warn("invalid number: %s", s);
12915007b59fSLuigi Rizzo 				s = ++endptr;
12925007b59fSLuigi Rizzo 				continue;
12935007b59fSLuigi Rizzo 			}
12945007b59fSLuigi Rizzo 			/* continue processing from here */
12955007b59fSLuigi Rizzo 			s = ++endptr;
12965007b59fSLuigi Rizzo 			ac++;
12975007b59fSLuigi Rizzo 			av--;
12985007b59fSLuigi Rizzo 		}
12995007b59fSLuigi Rizzo 		if (v[1] < v[0] ||
13006a7bb02dSLuigi Rizzo 			v[1] >= DN_MAX_ID-1 ||
13016a7bb02dSLuigi Rizzo 			v[1] >= DN_MAX_ID-1) {
13025007b59fSLuigi Rizzo 			continue; /* invalid entry */
13035007b59fSLuigi Rizzo 		}
13045007b59fSLuigi Rizzo 		n++;
13055007b59fSLuigi Rizzo 		/* translate if 'pipe list' */
13065007b59fSLuigi Rizzo 		if (co.do_pipe == 1) {
13075007b59fSLuigi Rizzo 			v[0] += DN_MAX_ID;
13085007b59fSLuigi Rizzo 			v[1] += DN_MAX_ID;
13095007b59fSLuigi Rizzo 		}
13105007b59fSLuigi Rizzo 		v = (n*2 < len) ? v + 2 : base;
13115007b59fSLuigi Rizzo 	}
13125007b59fSLuigi Rizzo 	return n;
13135007b59fSLuigi Rizzo }
13145007b59fSLuigi Rizzo 
1315cc4d3c30SLuigi Rizzo /* main entry point for dummynet list functions. co.do_pipe indicates
1316cc4d3c30SLuigi Rizzo  * which function we want to support.
13175007b59fSLuigi Rizzo  * av may contain filtering arguments, either individual entries
13185007b59fSLuigi Rizzo  * or ranges, or lists (space or commas are valid separators).
13195007b59fSLuigi Rizzo  * Format for a range can be n1-n2 or n3 n4 n5 ...
13205007b59fSLuigi Rizzo  * In a range n1 must be <= n2, otherwise the range is ignored.
13215007b59fSLuigi Rizzo  * A number 'n4' is translate in a range 'n4-n4'
13225007b59fSLuigi Rizzo  * All number must be > 0 and < DN_MAX_ID-1
1323cc4d3c30SLuigi Rizzo  */
1324cc4d3c30SLuigi Rizzo void
1325cc4d3c30SLuigi Rizzo dummynet_list(int ac, char *av[], int show_counters)
1326cc4d3c30SLuigi Rizzo {
13275007b59fSLuigi Rizzo 	struct dn_id *oid, *x = NULL;
13286a7bb02dSLuigi Rizzo 	int ret, i;
13295007b59fSLuigi Rizzo 	int n; 		/* # of ranges */
13306a7bb02dSLuigi Rizzo 	u_int buflen, l;
13316a7bb02dSLuigi Rizzo 	u_int max_size;	/* largest obj passed up */
1332cc4d3c30SLuigi Rizzo 
13336a7bb02dSLuigi Rizzo 	(void)show_counters;	// XXX unused, but we should use it.
13345007b59fSLuigi Rizzo 	ac--;
13355007b59fSLuigi Rizzo 	av++; 		/* skip 'list' | 'show' word */
13365007b59fSLuigi Rizzo 
13375007b59fSLuigi Rizzo 	n = parse_range(ac, av, NULL, 0);	/* Count # of ranges. */
13385007b59fSLuigi Rizzo 
13395007b59fSLuigi Rizzo 	/* Allocate space to store ranges */
13405007b59fSLuigi Rizzo 	l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
13415007b59fSLuigi Rizzo 	oid = safe_calloc(1, l);
13425007b59fSLuigi Rizzo 	oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
13435007b59fSLuigi Rizzo 
13445007b59fSLuigi Rizzo 	if (n > 0)	/* store ranges in idx */
13455007b59fSLuigi Rizzo 		parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
13465007b59fSLuigi Rizzo 	/*
13475007b59fSLuigi Rizzo 	 * Compute the size of the largest object returned. If the
13485007b59fSLuigi Rizzo 	 * response leaves at least this much spare space in the
13495007b59fSLuigi Rizzo 	 * buffer, then surely the response is complete; otherwise
13505007b59fSLuigi Rizzo 	 * there might be a risk of truncation and we will need to
13515007b59fSLuigi Rizzo 	 * retry with a larger buffer.
13525007b59fSLuigi Rizzo 	 * XXX don't bother with smaller structs.
13535007b59fSLuigi Rizzo 	 */
13545007b59fSLuigi Rizzo 	max_size = sizeof(struct dn_fs);
13555007b59fSLuigi Rizzo 	if (max_size < sizeof(struct dn_sch))
13565007b59fSLuigi Rizzo 		max_size = sizeof(struct dn_sch);
13575007b59fSLuigi Rizzo 	if (max_size < sizeof(struct dn_flow))
13585007b59fSLuigi Rizzo 		max_size = sizeof(struct dn_flow);
13595007b59fSLuigi Rizzo 
1360cc4d3c30SLuigi Rizzo 	switch (co.do_pipe) {
1361cc4d3c30SLuigi Rizzo 	case 1:
13625007b59fSLuigi Rizzo 		oid->subtype = DN_LINK;	/* list pipe */
1363cc4d3c30SLuigi Rizzo 		break;
1364cc4d3c30SLuigi Rizzo 	case 2:
13655007b59fSLuigi Rizzo 		oid->subtype = DN_FS;	/* list queue */
1366cc4d3c30SLuigi Rizzo 		break;
1367cc4d3c30SLuigi Rizzo 	case 3:
13685007b59fSLuigi Rizzo 		oid->subtype = DN_SCH;	/* list sched */
1369cc4d3c30SLuigi Rizzo 		break;
1370cc4d3c30SLuigi Rizzo 	}
1371f10f583fSLuigi Rizzo 
13725007b59fSLuigi Rizzo 	/*
13735007b59fSLuigi Rizzo 	 * Ask the kernel an estimate of the required space (result
13745007b59fSLuigi Rizzo 	 * in oid.id), unless we are requesting a subset of objects,
13755007b59fSLuigi Rizzo 	 * in which case the kernel does not give an exact answer.
13765007b59fSLuigi Rizzo 	 * In any case, space might grow in the meantime due to the
13775007b59fSLuigi Rizzo 	 * creation of new queues, so we must be prepared to retry.
1378f10f583fSLuigi Rizzo 	 */
13795007b59fSLuigi Rizzo 	if (n > 0) {
13805007b59fSLuigi Rizzo 		buflen = 4*1024;
13815007b59fSLuigi Rizzo 	} else {
13825007b59fSLuigi Rizzo 		ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
13835007b59fSLuigi Rizzo 		if (ret != 0 || oid->id <= sizeof(*oid))
13845007b59fSLuigi Rizzo 			goto done;
13855007b59fSLuigi Rizzo 		buflen = oid->id + max_size;
13865007b59fSLuigi Rizzo 		oid->len = sizeof(*oid); /* restore */
1387f10f583fSLuigi Rizzo 	}
13885007b59fSLuigi Rizzo 	/* Try a few times, until the buffer fits */
13895007b59fSLuigi Rizzo 	for (i = 0; i < 20; i++) {
13905007b59fSLuigi Rizzo 		l = buflen;
13915007b59fSLuigi Rizzo 		x = safe_realloc(x, l);
13925007b59fSLuigi Rizzo 		bcopy(oid, x, oid->len);
13935007b59fSLuigi Rizzo 		ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
13945007b59fSLuigi Rizzo 		if (ret != 0 || x->id <= sizeof(*oid))
13955007b59fSLuigi Rizzo 			goto done; /* no response */
13965007b59fSLuigi Rizzo 		if (l + max_size <= buflen)
13975007b59fSLuigi Rizzo 			break; /* ok */
13985007b59fSLuigi Rizzo 		buflen *= 2;	 /* double for next attempt */
13995007b59fSLuigi Rizzo 	}
1400cc4d3c30SLuigi Rizzo 	list_pipes(x, O_NEXT(x, l));
14015007b59fSLuigi Rizzo done:
14025007b59fSLuigi Rizzo 	if (x)
1403cc4d3c30SLuigi Rizzo 		free(x);
14045007b59fSLuigi Rizzo 	free(oid);
1405cc4d3c30SLuigi Rizzo }
1406