xref: /f-stack/tools/ipfw/dummynet.c (revision d4a07e70)
122ce4affSfengbojiang /*-
2127dd473Swhl739  * Codel/FQ_Codel and PIE/FQ_PIE Code:
3127dd473Swhl739  * Copyright (C) 2016 Centre for Advanced Internet Architectures,
4127dd473Swhl739  *  Swinburne University of Technology, Melbourne, Australia.
5127dd473Swhl739  * Portions of this code were made possible in part by a gift from
6127dd473Swhl739  *  The Comcast Innovation Fund.
7127dd473Swhl739  * Implemented by Rasool Al-Saadi <[email protected]>
8127dd473Swhl739  *
9127dd473Swhl739  * Copyright (c) 2002-2003,2010 Luigi Rizzo
10127dd473Swhl739  *
11127dd473Swhl739  * Redistribution and use in source forms, with and without modification,
12127dd473Swhl739  * are permitted provided that this entire comment appears intact.
13127dd473Swhl739  *
14127dd473Swhl739  * Redistribution in binary form may occur without any restrictions.
15127dd473Swhl739  * Obviously, it would be nice if you gave credit where credit is due
16127dd473Swhl739  * but requiring it would be too onerous.
17127dd473Swhl739  *
18127dd473Swhl739  * This software is provided ``AS IS'' without any warranties of any kind.
19127dd473Swhl739  *
20127dd473Swhl739  * $FreeBSD$
21127dd473Swhl739  *
22127dd473Swhl739  * dummynet support
23127dd473Swhl739  */
24127dd473Swhl739 
25127dd473Swhl739 #define NEW_AQM
26127dd473Swhl739 #include <sys/types.h>
27127dd473Swhl739 #include <sys/socket.h>
28127dd473Swhl739 /* XXX there are several sysctl leftover here */
29127dd473Swhl739 #include <sys/sysctl.h>
30127dd473Swhl739 
31127dd473Swhl739 #include "ipfw2.h"
32127dd473Swhl739 
33127dd473Swhl739 #ifdef NEW_AQM
34127dd473Swhl739 #include <stdint.h>
35127dd473Swhl739 #endif
36127dd473Swhl739 
37127dd473Swhl739 #include <ctype.h>
38127dd473Swhl739 #include <err.h>
39127dd473Swhl739 #include <errno.h>
40127dd473Swhl739 #include <libutil.h>
41127dd473Swhl739 #include <netdb.h>
42127dd473Swhl739 #include <stdio.h>
43127dd473Swhl739 #include <stdlib.h>
44127dd473Swhl739 #include <string.h>
45127dd473Swhl739 #include <sysexits.h>
46127dd473Swhl739 
47127dd473Swhl739 #include <net/if.h>
48127dd473Swhl739 #include <netinet/in.h>
49127dd473Swhl739 #include <netinet/ip_fw.h>
50127dd473Swhl739 #include <netinet/ip_dummynet.h>
51127dd473Swhl739 #include <arpa/inet.h>	/* inet_ntoa */
52127dd473Swhl739 
53127dd473Swhl739 
54127dd473Swhl739 static struct _s_x dummynet_params[] = {
55127dd473Swhl739 	{ "plr",		TOK_PLR },
56127dd473Swhl739 	{ "noerror",		TOK_NOERROR },
57127dd473Swhl739 	{ "buckets",		TOK_BUCKETS },
58127dd473Swhl739 	{ "dst-ip",		TOK_DSTIP },
59127dd473Swhl739 	{ "src-ip",		TOK_SRCIP },
60127dd473Swhl739 	{ "dst-port",		TOK_DSTPORT },
61127dd473Swhl739 	{ "src-port",		TOK_SRCPORT },
62127dd473Swhl739 	{ "proto",		TOK_PROTO },
63127dd473Swhl739 	{ "weight",		TOK_WEIGHT },
64127dd473Swhl739 	{ "lmax",		TOK_LMAX },
65127dd473Swhl739 	{ "maxlen",		TOK_LMAX },
66127dd473Swhl739 	{ "all",		TOK_ALL },
67127dd473Swhl739 	{ "mask",		TOK_MASK }, /* alias for both */
68127dd473Swhl739 	{ "sched_mask",		TOK_SCHED_MASK },
69127dd473Swhl739 	{ "flow_mask",		TOK_FLOW_MASK },
70127dd473Swhl739 	{ "droptail",		TOK_DROPTAIL },
71127dd473Swhl739 	{ "ecn",		TOK_ECN },
72127dd473Swhl739 	{ "red",		TOK_RED },
73127dd473Swhl739 	{ "gred",		TOK_GRED },
74127dd473Swhl739 #ifdef NEW_AQM
75127dd473Swhl739 	{ "codel",		TOK_CODEL}, /* Codel AQM */
76127dd473Swhl739 	{ "fq_codel",	TOK_FQ_CODEL}, /* FQ-Codel  */
77127dd473Swhl739 	{ "pie",		TOK_PIE}, /* PIE AQM */
78127dd473Swhl739 	{ "fq_pie",		TOK_FQ_PIE}, /* FQ-PIE */
79127dd473Swhl739 #endif
80127dd473Swhl739 	{ "bw",			TOK_BW },
81127dd473Swhl739 	{ "bandwidth",		TOK_BW },
82127dd473Swhl739 	{ "delay",		TOK_DELAY },
83127dd473Swhl739 	{ "link",		TOK_LINK },
84127dd473Swhl739 	{ "pipe",		TOK_PIPE },
85127dd473Swhl739 	{ "queue",		TOK_QUEUE },
86127dd473Swhl739 	{ "flowset",		TOK_FLOWSET },
87127dd473Swhl739 	{ "sched",		TOK_SCHED },
88127dd473Swhl739 	{ "pri",		TOK_PRI },
89127dd473Swhl739 	{ "priority",		TOK_PRI },
90127dd473Swhl739 	{ "type",		TOK_TYPE },
91127dd473Swhl739 	{ "flow-id",		TOK_FLOWID},
92127dd473Swhl739 	{ "dst-ipv6",		TOK_DSTIP6},
93127dd473Swhl739 	{ "dst-ip6",		TOK_DSTIP6},
94127dd473Swhl739 	{ "src-ipv6",		TOK_SRCIP6},
95127dd473Swhl739 	{ "src-ip6",		TOK_SRCIP6},
96127dd473Swhl739 	{ "profile",		TOK_PROFILE},
97127dd473Swhl739 	{ "burst",		TOK_BURST},
98127dd473Swhl739 	{ "dummynet-params",	TOK_NULL },
99127dd473Swhl739 	{ NULL, 0 }	/* terminator */
100127dd473Swhl739 };
101127dd473Swhl739 
102127dd473Swhl739 #ifdef NEW_AQM
103127dd473Swhl739 /* AQM/extra sched parameters  tokens*/
104127dd473Swhl739 static struct _s_x aqm_params[] = {
105127dd473Swhl739 	{ "target",		TOK_TARGET},
106127dd473Swhl739 	{ "interval",		TOK_INTERVAL},
107127dd473Swhl739 	{ "limit",		TOK_LIMIT},
108127dd473Swhl739 	{ "flows",		TOK_FLOWS},
109127dd473Swhl739 	{ "quantum",		TOK_QUANTUM},
110127dd473Swhl739 	{ "ecn",		TOK_ECN},
111127dd473Swhl739 	{ "noecn",		TOK_NO_ECN},
112127dd473Swhl739 	{ "tupdate",		TOK_TUPDATE},
113127dd473Swhl739 	{ "max_burst",		TOK_MAX_BURST},
114127dd473Swhl739 	{ "max_ecnth",	TOK_MAX_ECNTH},
115127dd473Swhl739 	{ "alpha",		TOK_ALPHA},
116127dd473Swhl739 	{ "beta",		TOK_BETA},
117127dd473Swhl739 	{ "capdrop",	TOK_CAPDROP},
118127dd473Swhl739 	{ "nocapdrop",	TOK_NO_CAPDROP},
119127dd473Swhl739 	{ "onoff",	TOK_ONOFF},
120127dd473Swhl739 	{ "dre",	TOK_DRE},
121127dd473Swhl739 	{ "ts",	TOK_TS},
122127dd473Swhl739 	{ "derand",	TOK_DERAND},
123127dd473Swhl739 	{ "noderand",	TOK_NO_DERAND},
124127dd473Swhl739 	{ NULL, 0 }	/* terminator */
125127dd473Swhl739 };
126127dd473Swhl739 #endif
127127dd473Swhl739 
128127dd473Swhl739 #define O_NEXT(p, len) ((void *)((char *)p + len))
129127dd473Swhl739 
130127dd473Swhl739 static void
oid_fill(struct dn_id * oid,int len,int type,uintptr_t id)131127dd473Swhl739 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
132127dd473Swhl739 {
133127dd473Swhl739 	oid->len = len;
134127dd473Swhl739 	oid->type = type;
135127dd473Swhl739 	oid->subtype = 0;
136127dd473Swhl739 	oid->id = id;
137127dd473Swhl739 }
138127dd473Swhl739 
139127dd473Swhl739 /* make room in the buffer and move the pointer forward */
140127dd473Swhl739 static void *
o_next(struct dn_id ** o,int len,int type)141127dd473Swhl739 o_next(struct dn_id **o, int len, int type)
142127dd473Swhl739 {
143127dd473Swhl739 	struct dn_id *ret = *o;
144127dd473Swhl739 	oid_fill(ret, len, type, 0);
145127dd473Swhl739 	*o = O_NEXT(*o, len);
146127dd473Swhl739 	return ret;
147127dd473Swhl739 }
148127dd473Swhl739 
149127dd473Swhl739 #ifdef NEW_AQM
150127dd473Swhl739 
151127dd473Swhl739 /* Codel flags */
152127dd473Swhl739 enum {
153127dd473Swhl739 	CODEL_ECN_ENABLED = 1
154127dd473Swhl739 };
155127dd473Swhl739 
156127dd473Swhl739 /* PIE flags, from PIE kernel module */
157127dd473Swhl739 enum {
158127dd473Swhl739 	PIE_ECN_ENABLED = 1,
159127dd473Swhl739 	PIE_CAPDROP_ENABLED = 2,
160127dd473Swhl739 	PIE_ON_OFF_MODE_ENABLED = 4,
161127dd473Swhl739 	PIE_DEPRATEEST_ENABLED = 8,
162127dd473Swhl739 	PIE_DERAND_ENABLED = 16
163127dd473Swhl739 };
164127dd473Swhl739 
165127dd473Swhl739 #define PIE_FIX_POINT_BITS 13
166127dd473Swhl739 #define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)
167127dd473Swhl739 
168127dd473Swhl739 /* integer to time */
16922ce4affSfengbojiang static void
us_to_time(int t,char * strt)170127dd473Swhl739 us_to_time(int t, char *strt)
171127dd473Swhl739 {
172127dd473Swhl739 	if (t < 0)
173127dd473Swhl739 		strt[0]='\0';
174127dd473Swhl739 	else if ( t==0 )
175127dd473Swhl739 		sprintf(strt,"%d", t);
176127dd473Swhl739 	else if (t< 1000)
177127dd473Swhl739 		sprintf(strt,"%dus", t);
178127dd473Swhl739 	else if (t < 1000000)
179127dd473Swhl739 		sprintf(strt,"%gms", (float) t / 1000);
180127dd473Swhl739 	else
181127dd473Swhl739 		sprintf(strt,"%gfs", (float) t / 1000000);
182127dd473Swhl739 }
183127dd473Swhl739 
184127dd473Swhl739 /*
185127dd473Swhl739  * returns -1 if s is not a valid time, otherwise, return time in us
186127dd473Swhl739  */
187127dd473Swhl739 static long
time_to_us(const char * s)188127dd473Swhl739 time_to_us(const char *s)
189127dd473Swhl739 {
190127dd473Swhl739 	int i, dots = 0;
191127dd473Swhl739 	int len = strlen(s);
192127dd473Swhl739 	char strt[16]="", stru[16]="";
193127dd473Swhl739 
194127dd473Swhl739 	if (len>15)
195127dd473Swhl739 		return -1;
196127dd473Swhl739 	for (i = 0; i<len && (isdigit(s[i]) || s[i]=='.') ; i++)
197127dd473Swhl739 		if (s[i]=='.') {
198127dd473Swhl739 			if (dots)
199127dd473Swhl739 				return -1;
200127dd473Swhl739 			else
201127dd473Swhl739 				dots++;
202127dd473Swhl739 		}
203127dd473Swhl739 
204127dd473Swhl739 	if (!i)
205127dd473Swhl739 		return -1;
206127dd473Swhl739 	strncpy(strt, s, i);
207127dd473Swhl739 	if (i<len)
208127dd473Swhl739 		strcpy(stru, s+i);
209127dd473Swhl739 	else
210127dd473Swhl739 		strcpy(stru, "ms");
211127dd473Swhl739 
212127dd473Swhl739 	if (!strcasecmp(stru, "us"))
213127dd473Swhl739 		return atol(strt);
214127dd473Swhl739 	if (!strcasecmp(stru, "ms"))
215127dd473Swhl739 		return (strtod(strt, NULL) * 1000);
216127dd473Swhl739 	if (!strcasecmp(stru, "s"))
217127dd473Swhl739 		return (strtod(strt, NULL)*1000000);
218127dd473Swhl739 
219127dd473Swhl739 	return -1;
220127dd473Swhl739 }
221127dd473Swhl739 
222127dd473Swhl739 
223127dd473Swhl739 /* Get AQM or scheduler extra parameters  */
22422ce4affSfengbojiang static void
get_extra_parms(uint32_t nr,char * out,int subtype)225127dd473Swhl739 get_extra_parms(uint32_t nr, char *out, int subtype)
226127dd473Swhl739 {
227127dd473Swhl739 	struct dn_extra_parms *ep;
228127dd473Swhl739 	int ret;
229127dd473Swhl739 	char strt1[15], strt2[15], strt3[15];
230127dd473Swhl739 	u_int l;
231127dd473Swhl739 
232127dd473Swhl739 	/* prepare the request */
233127dd473Swhl739 	l = sizeof(struct dn_extra_parms);
234127dd473Swhl739 	ep = safe_calloc(1, l);
235127dd473Swhl739 	memset(ep, 0, sizeof(*ep));
236127dd473Swhl739 	*out = '\0';
237127dd473Swhl739 
238127dd473Swhl739 	oid_fill(&ep->oid, l, DN_CMD_GET, DN_API_VERSION);
239127dd473Swhl739 	ep->oid.len = l;
240127dd473Swhl739 	ep->oid.subtype = subtype;
241127dd473Swhl739 	ep->nr = nr;
242127dd473Swhl739 
243127dd473Swhl739 	ret = do_cmd(-IP_DUMMYNET3, ep, (uintptr_t)&l);
244127dd473Swhl739 	if (ret) {
245127dd473Swhl739 		free(ep);
246127dd473Swhl739 		errx(EX_DATAERR, "Error getting extra parameters\n");
247127dd473Swhl739 	}
248127dd473Swhl739 
249127dd473Swhl739 	switch (subtype) {
250127dd473Swhl739 	case DN_AQM_PARAMS:
251127dd473Swhl739 		if( !strcasecmp(ep->name, "codel")) {
252127dd473Swhl739 			us_to_time(ep->par[0], strt1);
253127dd473Swhl739 			us_to_time(ep->par[1], strt2);
254127dd473Swhl739 			l = sprintf(out, " AQM CoDel target %s interval %s",
255127dd473Swhl739 				strt1, strt2);
256127dd473Swhl739 			if (ep->par[2] & CODEL_ECN_ENABLED)
257127dd473Swhl739 				l = sprintf(out + l, " ECN");
258127dd473Swhl739 			else
259127dd473Swhl739 				l += sprintf(out + l, " NoECN");
260127dd473Swhl739 		} else if( !strcasecmp(ep->name, "pie")) {
261127dd473Swhl739 			us_to_time(ep->par[0], strt1);
262127dd473Swhl739 			us_to_time(ep->par[1], strt2);
263127dd473Swhl739 			us_to_time(ep->par[2], strt3);
264127dd473Swhl739 			l = sprintf(out, " AQM type PIE target %s tupdate %s alpha "
265127dd473Swhl739 					"%g beta %g max_burst %s max_ecnth %.3g",
266127dd473Swhl739 					strt1,
267127dd473Swhl739 					strt2,
268127dd473Swhl739 					ep->par[4] / (float) PIE_SCALE,
269127dd473Swhl739 					ep->par[5] / (float) PIE_SCALE,
270127dd473Swhl739 					strt3,
271127dd473Swhl739 					ep->par[3] / (float) PIE_SCALE
272127dd473Swhl739 				);
273127dd473Swhl739 
274127dd473Swhl739 			if (ep->par[6] & PIE_ECN_ENABLED)
275127dd473Swhl739 				l += sprintf(out + l, " ECN");
276127dd473Swhl739 			else
277127dd473Swhl739 				l += sprintf(out + l, " NoECN");
278127dd473Swhl739 			if (ep->par[6] & PIE_CAPDROP_ENABLED)
279127dd473Swhl739 				l += sprintf(out + l, " CapDrop");
280127dd473Swhl739 			else
281127dd473Swhl739 				l += sprintf(out + l, " NoCapDrop");
282127dd473Swhl739 			if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
283127dd473Swhl739 				l += sprintf(out + l, " OnOff");
284127dd473Swhl739 			if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
285127dd473Swhl739 				l += sprintf(out + l, " DRE");
286127dd473Swhl739 			else
287127dd473Swhl739 				l += sprintf(out + l, " TS");
288127dd473Swhl739 			if (ep->par[6] & PIE_DERAND_ENABLED)
289127dd473Swhl739 				l += sprintf(out + l, " Derand");
290127dd473Swhl739 			else
291127dd473Swhl739 				l += sprintf(out + l, " NoDerand");
292127dd473Swhl739 		}
293127dd473Swhl739 		break;
294127dd473Swhl739 
295127dd473Swhl739 	case	DN_SCH_PARAMS:
296127dd473Swhl739 		if (!strcasecmp(ep->name,"FQ_CODEL")) {
297127dd473Swhl739 			us_to_time(ep->par[0], strt1);
298127dd473Swhl739 			us_to_time(ep->par[1], strt2);
299127dd473Swhl739 			l = sprintf(out," FQ_CODEL target %s interval %s"
300127dd473Swhl739 				" quantum %jd limit %jd flows %jd",
301127dd473Swhl739 				strt1, strt2,
302127dd473Swhl739 				(intmax_t) ep->par[3],
303127dd473Swhl739 				(intmax_t) ep->par[4],
304127dd473Swhl739 				(intmax_t) ep->par[5]
305127dd473Swhl739 				);
306127dd473Swhl739 			if (ep->par[2] & CODEL_ECN_ENABLED)
307127dd473Swhl739 				l += sprintf(out + l, " ECN");
308127dd473Swhl739 			else
309127dd473Swhl739 				l += sprintf(out + l, " NoECN");
310127dd473Swhl739 			l += sprintf(out + l, "\n");
311127dd473Swhl739 		} else 	if (!strcasecmp(ep->name,"FQ_PIE")) {
312127dd473Swhl739 			us_to_time(ep->par[0], strt1);
313127dd473Swhl739 			us_to_time(ep->par[1], strt2);
314127dd473Swhl739 			us_to_time(ep->par[2], strt3);
315127dd473Swhl739 			l = sprintf(out, "  FQ_PIE target %s tupdate %s alpha "
316127dd473Swhl739 				"%g beta %g max_burst %s max_ecnth %.3g"
317127dd473Swhl739 				" quantum %jd limit %jd flows %jd",
318127dd473Swhl739 				strt1,
319127dd473Swhl739 				strt2,
320127dd473Swhl739 				ep->par[4] / (float) PIE_SCALE,
321127dd473Swhl739 				ep->par[5] / (float) PIE_SCALE,
322127dd473Swhl739 				strt3,
323127dd473Swhl739 				ep->par[3] / (float) PIE_SCALE,
324127dd473Swhl739 				(intmax_t) ep->par[7],
325127dd473Swhl739 				(intmax_t) ep->par[8],
326127dd473Swhl739 				(intmax_t) ep->par[9]
327127dd473Swhl739 			);
328127dd473Swhl739 
329127dd473Swhl739 			if (ep->par[6] & PIE_ECN_ENABLED)
330127dd473Swhl739 				l += sprintf(out + l, " ECN");
331127dd473Swhl739 			else
332127dd473Swhl739 				l += sprintf(out + l, " NoECN");
333127dd473Swhl739 			if (ep->par[6] & PIE_CAPDROP_ENABLED)
334127dd473Swhl739 				l += sprintf(out + l, " CapDrop");
335127dd473Swhl739 			else
336127dd473Swhl739 				l += sprintf(out + l, " NoCapDrop");
337127dd473Swhl739 			if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
338127dd473Swhl739 				l += sprintf(out + l, " OnOff");
339127dd473Swhl739 			if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
340127dd473Swhl739 				l += sprintf(out + l, " DRE");
341127dd473Swhl739 			else
342127dd473Swhl739 				l += sprintf(out + l, " TS");
343127dd473Swhl739 			if (ep->par[6] & PIE_DERAND_ENABLED)
344127dd473Swhl739 				l += sprintf(out + l, " Derand");
345127dd473Swhl739 			else
346127dd473Swhl739 				l += sprintf(out + l, " NoDerand");
347127dd473Swhl739 			l += sprintf(out + l, "\n");
348127dd473Swhl739 		}
349127dd473Swhl739 		break;
350127dd473Swhl739 	}
351127dd473Swhl739 
352127dd473Swhl739 	free(ep);
353127dd473Swhl739 }
354127dd473Swhl739 #endif
355127dd473Swhl739 
356127dd473Swhl739 
357127dd473Swhl739 #if 0
358127dd473Swhl739 static int
359127dd473Swhl739 sort_q(void *arg, const void *pa, const void *pb)
360127dd473Swhl739 {
361127dd473Swhl739 	int rev = (co.do_sort < 0);
362127dd473Swhl739 	int field = rev ? -co.do_sort : co.do_sort;
363127dd473Swhl739 	long long res = 0;
364127dd473Swhl739 	const struct dn_flow_queue *a = pa;
365127dd473Swhl739 	const struct dn_flow_queue *b = pb;
366127dd473Swhl739 
367127dd473Swhl739 	switch (field) {
368127dd473Swhl739 	case 1: /* pkts */
369127dd473Swhl739 		res = a->len - b->len;
370127dd473Swhl739 		break;
371127dd473Swhl739 	case 2: /* bytes */
372127dd473Swhl739 		res = a->len_bytes - b->len_bytes;
373127dd473Swhl739 		break;
374127dd473Swhl739 
375127dd473Swhl739 	case 3: /* tot pkts */
376127dd473Swhl739 		res = a->tot_pkts - b->tot_pkts;
377127dd473Swhl739 		break;
378127dd473Swhl739 
379127dd473Swhl739 	case 4: /* tot bytes */
380127dd473Swhl739 		res = a->tot_bytes - b->tot_bytes;
381127dd473Swhl739 		break;
382127dd473Swhl739 	}
383127dd473Swhl739 	if (res < 0)
384127dd473Swhl739 		res = -1;
385127dd473Swhl739 	if (res > 0)
386127dd473Swhl739 		res = 1;
387127dd473Swhl739 	return (int)(rev ? res : -res);
388127dd473Swhl739 }
389127dd473Swhl739 #endif
390127dd473Swhl739 
391127dd473Swhl739 /* print a mask and header for the subsequent list of flows */
392127dd473Swhl739 static void
print_mask(struct ipfw_flow_id * id)393127dd473Swhl739 print_mask(struct ipfw_flow_id *id)
394127dd473Swhl739 {
395127dd473Swhl739 	if (!IS_IP6_FLOW_ID(id)) {
396127dd473Swhl739 		printf("    "
397127dd473Swhl739 		    "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
398127dd473Swhl739 		    id->extra ? "queue," : "",
399127dd473Swhl739 		    id->proto,
400127dd473Swhl739 		    id->src_ip, id->src_port,
401127dd473Swhl739 		    id->dst_ip, id->dst_port);
402127dd473Swhl739 	} else {
403127dd473Swhl739 		char buf[255];
404127dd473Swhl739 		printf("\n        mask: %sproto: 0x%02x, flow_id: 0x%08x,  ",
405127dd473Swhl739 		    id->extra ? "queue," : "",
406127dd473Swhl739 		    id->proto, id->flow_id6);
407*d4a07e70Sfengbojiang 		inet_ntop(AF_INET6_LINUX, &(id->src_ip6), buf, sizeof(buf));
408127dd473Swhl739 		printf("%s/0x%04x -> ", buf, id->src_port);
409*d4a07e70Sfengbojiang 		inet_ntop(AF_INET6_LINUX, &(id->dst_ip6), buf, sizeof(buf));
410127dd473Swhl739 		printf("%s/0x%04x\n", buf, id->dst_port);
411127dd473Swhl739 	}
412127dd473Swhl739 }
413127dd473Swhl739 
414127dd473Swhl739 static void
print_header(struct ipfw_flow_id * id)415127dd473Swhl739 print_header(struct ipfw_flow_id *id)
416127dd473Swhl739 {
417127dd473Swhl739 	if (!IS_IP6_FLOW_ID(id))
418127dd473Swhl739 		printf("BKT Prot ___Source IP/port____ "
419127dd473Swhl739 		    "____Dest. IP/port____ "
420127dd473Swhl739 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
421127dd473Swhl739 	else
422127dd473Swhl739 		printf("BKT ___Prot___ _flow-id_ "
423127dd473Swhl739 		    "______________Source IPv6/port_______________ "
424127dd473Swhl739 		    "_______________Dest. IPv6/port_______________ "
425127dd473Swhl739 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
426127dd473Swhl739 }
427127dd473Swhl739 
428127dd473Swhl739 static void
list_flow(struct buf_pr * bp,struct dn_flow * ni)429127dd473Swhl739 list_flow(struct buf_pr *bp, struct dn_flow *ni)
430127dd473Swhl739 {
431127dd473Swhl739 	char buff[255];
432127dd473Swhl739 	struct protoent *pe = NULL;
433127dd473Swhl739 	struct in_addr ina;
434127dd473Swhl739 	struct ipfw_flow_id *id = &ni->fid;
435127dd473Swhl739 
436127dd473Swhl739 	pe = getprotobynumber(id->proto);
437127dd473Swhl739 		/* XXX: Should check for IPv4 flows */
438127dd473Swhl739 	bprintf(bp, "%3u%c", (ni->oid.id) & 0xff,
439127dd473Swhl739 		id->extra ? '*' : ' ');
440127dd473Swhl739 	if (!IS_IP6_FLOW_ID(id)) {
441127dd473Swhl739 		if (pe)
442127dd473Swhl739 			bprintf(bp, "%-4s ", pe->p_name);
443127dd473Swhl739 		else
444127dd473Swhl739 			bprintf(bp, "%4u ", id->proto);
445127dd473Swhl739 		ina.s_addr = htonl(id->src_ip);
446127dd473Swhl739 		bprintf(bp, "%15s/%-5d ",
447127dd473Swhl739 		    inet_ntoa(ina), id->src_port);
448127dd473Swhl739 		ina.s_addr = htonl(id->dst_ip);
449127dd473Swhl739 		bprintf(bp, "%15s/%-5d ",
450127dd473Swhl739 		    inet_ntoa(ina), id->dst_port);
451127dd473Swhl739 	} else {
452127dd473Swhl739 		/* Print IPv6 flows */
453127dd473Swhl739 		if (pe != NULL)
454127dd473Swhl739 			bprintf(bp, "%9s ", pe->p_name);
455127dd473Swhl739 		else
456127dd473Swhl739 			bprintf(bp, "%9u ", id->proto);
457127dd473Swhl739 		bprintf(bp, "%7d  %39s/%-5d ", id->flow_id6,
458*d4a07e70Sfengbojiang 		    inet_ntop(AF_INET6_LINUX, &(id->src_ip6), buff, sizeof(buff)),
459127dd473Swhl739 		    id->src_port);
460127dd473Swhl739 		bprintf(bp, " %39s/%-5d ",
461*d4a07e70Sfengbojiang 		    inet_ntop(AF_INET6_LINUX, &(id->dst_ip6), buff, sizeof(buff)),
462127dd473Swhl739 		    id->dst_port);
463127dd473Swhl739 	}
464127dd473Swhl739 	pr_u64(bp, &ni->tot_pkts, 4);
465127dd473Swhl739 	pr_u64(bp, &ni->tot_bytes, 8);
466127dd473Swhl739 	bprintf(bp, "%2u %4u %3u",
467127dd473Swhl739 	    ni->length, ni->len_bytes, ni->drops);
468127dd473Swhl739 }
469127dd473Swhl739 
470127dd473Swhl739 static void
print_flowset_parms(struct dn_fs * fs,char * prefix)471127dd473Swhl739 print_flowset_parms(struct dn_fs *fs, char *prefix)
472127dd473Swhl739 {
473127dd473Swhl739 	int l;
474127dd473Swhl739 	char qs[30];
475127dd473Swhl739 	char plr[30];
476127dd473Swhl739 	char red[200];	/* Display RED parameters */
477127dd473Swhl739 
478127dd473Swhl739 	l = fs->qsize;
479127dd473Swhl739 	if (fs->flags & DN_QSIZE_BYTES) {
480127dd473Swhl739 		if (l >= 8192)
481127dd473Swhl739 			sprintf(qs, "%d KB", l / 1024);
482127dd473Swhl739 		else
483127dd473Swhl739 			sprintf(qs, "%d B", l);
484127dd473Swhl739 	} else
485127dd473Swhl739 		sprintf(qs, "%3d sl.", l);
486127dd473Swhl739 	if (fs->plr)
487127dd473Swhl739 		sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
488127dd473Swhl739 	else
489127dd473Swhl739 		plr[0] = '\0';
490127dd473Swhl739 
491127dd473Swhl739 	if (fs->flags & DN_IS_RED) {	/* RED parameters */
492127dd473Swhl739 		sprintf(red,
493127dd473Swhl739 		    "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
494127dd473Swhl739 		    (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
495127dd473Swhl739 		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
496127dd473Swhl739 		    fs->min_th,
497127dd473Swhl739 		    fs->max_th,
498127dd473Swhl739 		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
499127dd473Swhl739 		if (fs->flags & DN_IS_ECN)
50022ce4affSfengbojiang 			strlcat(red, " (ecn)", sizeof(red));
501127dd473Swhl739 #ifdef NEW_AQM
502127dd473Swhl739 	/* get AQM parameters */
503127dd473Swhl739 	} else if (fs->flags & DN_IS_AQM) {
504127dd473Swhl739 			get_extra_parms(fs->fs_nr, red, DN_AQM_PARAMS);
505127dd473Swhl739 #endif
506127dd473Swhl739 	} else
507127dd473Swhl739 		sprintf(red, "droptail");
508127dd473Swhl739 
509127dd473Swhl739 	if (prefix[0]) {
510127dd473Swhl739 	    printf("%s %s%s %d queues (%d buckets) %s\n",
511127dd473Swhl739 		prefix, qs, plr, fs->oid.id, fs->buckets, red);
512127dd473Swhl739 	    prefix[0] = '\0';
513127dd473Swhl739 	} else {
514127dd473Swhl739 	    printf("q%05d %s%s %d flows (%d buckets) sched %d "
515127dd473Swhl739 			"weight %d lmax %d pri %d %s\n",
516127dd473Swhl739 		fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
517127dd473Swhl739 		fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
518127dd473Swhl739 	    if (fs->flags & DN_HAVE_MASK)
519127dd473Swhl739 		print_mask(&fs->flow_mask);
520127dd473Swhl739 	}
521127dd473Swhl739 }
522127dd473Swhl739 
523127dd473Swhl739 static void
print_extra_delay_parms(struct dn_profile * p)524127dd473Swhl739 print_extra_delay_parms(struct dn_profile *p)
525127dd473Swhl739 {
526127dd473Swhl739 	double loss;
527127dd473Swhl739 	if (p->samples_no <= 0)
528127dd473Swhl739 		return;
529127dd473Swhl739 
530127dd473Swhl739 	loss = p->loss_level;
531127dd473Swhl739 	loss /= p->samples_no;
532127dd473Swhl739 	printf("\t profile: name \"%s\" loss %f samples %d\n",
533127dd473Swhl739 		p->name, loss, p->samples_no);
534127dd473Swhl739 }
535127dd473Swhl739 
536127dd473Swhl739 static void
flush_buf(char * buf)537127dd473Swhl739 flush_buf(char *buf)
538127dd473Swhl739 {
539127dd473Swhl739 	if (buf[0])
540127dd473Swhl739 		printf("%s\n", buf);
541127dd473Swhl739 	buf[0] = '\0';
542127dd473Swhl739 }
543127dd473Swhl739 
544127dd473Swhl739 /*
545127dd473Swhl739  * generic list routine. We expect objects in a specific order, i.e.
546127dd473Swhl739  * PIPES AND SCHEDULERS:
547127dd473Swhl739  *	link; scheduler; internal flowset if any; instances
548127dd473Swhl739  * we can tell a pipe from the number.
549127dd473Swhl739  *
550127dd473Swhl739  * FLOWSETS:
551127dd473Swhl739  *	flowset; queues;
552127dd473Swhl739  * link i (int queue); scheduler i; si(i) { flowsets() : queues }
553127dd473Swhl739  */
554127dd473Swhl739 static void
list_pipes(struct dn_id * oid,struct dn_id * end)555127dd473Swhl739 list_pipes(struct dn_id *oid, struct dn_id *end)
556127dd473Swhl739 {
557127dd473Swhl739     char buf[160];	/* pending buffer */
558127dd473Swhl739     int toPrint = 1;	/* print header */
559127dd473Swhl739     struct buf_pr bp;
560127dd473Swhl739 
561127dd473Swhl739     buf[0] = '\0';
562127dd473Swhl739     bp_alloc(&bp, 4096);
563127dd473Swhl739     for (; oid != end; oid = O_NEXT(oid, oid->len)) {
564127dd473Swhl739 	if (oid->len < sizeof(*oid))
565127dd473Swhl739 		errx(1, "invalid oid len %d\n", oid->len);
566127dd473Swhl739 
567127dd473Swhl739 	switch (oid->type) {
568127dd473Swhl739 	default:
569127dd473Swhl739 	    flush_buf(buf);
570127dd473Swhl739 	    printf("unrecognized object %d size %d\n", oid->type, oid->len);
571127dd473Swhl739 	    break;
572127dd473Swhl739 	case DN_TEXT: /* list of attached flowsets */
573127dd473Swhl739 	    {
574127dd473Swhl739 		int i, l;
575127dd473Swhl739 		struct {
576127dd473Swhl739 			struct dn_id id;
577127dd473Swhl739 			uint32_t p[0];
578127dd473Swhl739 		} *d = (void *)oid;
579127dd473Swhl739 		l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
580127dd473Swhl739 		if (l == 0)
581127dd473Swhl739 		    break;
582127dd473Swhl739 		printf("   Children flowsets: ");
583127dd473Swhl739 		for (i = 0; i < l; i++)
584127dd473Swhl739 			printf("%u ", d->p[i]);
585127dd473Swhl739 		printf("\n");
586127dd473Swhl739 		break;
587127dd473Swhl739 	    }
588127dd473Swhl739 	case DN_CMD_GET:
58922ce4affSfengbojiang 	    if (g_co.verbose)
590127dd473Swhl739 		printf("answer for cmd %d, len %d\n", oid->type, oid->id);
591127dd473Swhl739 	    break;
592127dd473Swhl739 	case DN_SCH: {
593127dd473Swhl739 	    struct dn_sch *s = (struct dn_sch *)oid;
594127dd473Swhl739 	    flush_buf(buf);
595127dd473Swhl739 	    printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
596127dd473Swhl739 			s->sched_nr,
597127dd473Swhl739 			s->name, s->flags, s->buckets, s->oid.id);
598127dd473Swhl739 #ifdef NEW_AQM
599127dd473Swhl739 		char parms[200];
600127dd473Swhl739 		get_extra_parms(s->sched_nr, parms, DN_SCH_PARAMS);
601127dd473Swhl739 		printf("%s",parms);
602127dd473Swhl739 #endif
603127dd473Swhl739 	    if (s->flags & DN_HAVE_MASK)
604127dd473Swhl739 		print_mask(&s->sched_mask);
605127dd473Swhl739 	    }
606127dd473Swhl739 	    break;
607127dd473Swhl739 
608127dd473Swhl739 	case DN_FLOW:
609127dd473Swhl739 	    if (toPrint != 0) {
610127dd473Swhl739 		    print_header(&((struct dn_flow *)oid)->fid);
611127dd473Swhl739 		    toPrint = 0;
612127dd473Swhl739 	    }
613127dd473Swhl739 	    list_flow(&bp, (struct dn_flow *)oid);
614127dd473Swhl739 	    printf("%s\n", bp.buf);
615127dd473Swhl739 	    bp_flush(&bp);
616127dd473Swhl739 	    break;
617127dd473Swhl739 
618127dd473Swhl739 	case DN_LINK: {
619127dd473Swhl739 	    struct dn_link *p = (struct dn_link *)oid;
620127dd473Swhl739 	    double b = p->bandwidth;
621127dd473Swhl739 	    char bwbuf[30];
622127dd473Swhl739 	    char burst[5 + 7];
623127dd473Swhl739 
624127dd473Swhl739 	    /* This starts a new object so flush buffer */
625127dd473Swhl739 	    flush_buf(buf);
626127dd473Swhl739 	    /* data rate */
627127dd473Swhl739 	    if (b == 0)
628127dd473Swhl739 		sprintf(bwbuf, "unlimited     ");
62922ce4affSfengbojiang 	    else if (b >= 1000000000)
63022ce4affSfengbojiang 		sprintf(bwbuf, "%7.3f Gbit/s", b/1000000000);
631127dd473Swhl739 	    else if (b >= 1000000)
632127dd473Swhl739 		sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
633127dd473Swhl739 	    else if (b >= 1000)
634127dd473Swhl739 		sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
635127dd473Swhl739 	    else
636127dd473Swhl739 		sprintf(bwbuf, "%7.3f bit/s ", b);
637127dd473Swhl739 
638127dd473Swhl739 	    if (humanize_number(burst, sizeof(burst), p->burst,
63922ce4affSfengbojiang 		    "", HN_AUTOSCALE, 0) < 0 || g_co.verbose)
640127dd473Swhl739 		sprintf(burst, "%d", (int)p->burst);
641127dd473Swhl739 	    sprintf(buf, "%05d: %s %4d ms burst %s",
642127dd473Swhl739 		p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
643127dd473Swhl739 	    }
644127dd473Swhl739 	    break;
645127dd473Swhl739 
646127dd473Swhl739 	case DN_FS:
647127dd473Swhl739 	    print_flowset_parms((struct dn_fs *)oid, buf);
648127dd473Swhl739 	    break;
649127dd473Swhl739 	case DN_PROFILE:
650127dd473Swhl739 	    flush_buf(buf);
651127dd473Swhl739 	    print_extra_delay_parms((struct dn_profile *)oid);
652127dd473Swhl739 	}
653127dd473Swhl739 	flush_buf(buf); // XXX does it really go here ?
654127dd473Swhl739     }
655127dd473Swhl739 
656127dd473Swhl739     bp_free(&bp);
657127dd473Swhl739 }
658127dd473Swhl739 
659127dd473Swhl739 /*
660127dd473Swhl739  * Delete pipe, queue or scheduler i
661127dd473Swhl739  */
662127dd473Swhl739 int
ipfw_delete_pipe(int do_pipe,int i)663127dd473Swhl739 ipfw_delete_pipe(int do_pipe, int i)
664127dd473Swhl739 {
665127dd473Swhl739 	struct {
666127dd473Swhl739 		struct dn_id oid;
667127dd473Swhl739 		uintptr_t a[1];	/* add more if we want a list */
668127dd473Swhl739 	} cmd;
669127dd473Swhl739 	oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
670127dd473Swhl739 	cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
671127dd473Swhl739 		( (do_pipe == 2) ? DN_FS : DN_SCH);
672127dd473Swhl739 	cmd.a[0] = i;
673127dd473Swhl739 	i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
674127dd473Swhl739 	if (i) {
675127dd473Swhl739 		i = 1;
676127dd473Swhl739 		warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
677127dd473Swhl739 	}
678127dd473Swhl739 	return i;
679127dd473Swhl739 }
680127dd473Swhl739 
681127dd473Swhl739 /*
682127dd473Swhl739  * Code to parse delay profiles.
683127dd473Swhl739  *
684127dd473Swhl739  * Some link types introduce extra delays in the transmission
685127dd473Swhl739  * of a packet, e.g. because of MAC level framing, contention on
686127dd473Swhl739  * the use of the channel, MAC level retransmissions and so on.
687127dd473Swhl739  * From our point of view, the channel is effectively unavailable
688127dd473Swhl739  * for this extra time, which is constant or variable depending
689127dd473Swhl739  * on the link type. Additionally, packets may be dropped after this
690127dd473Swhl739  * time (e.g. on a wireless link after too many retransmissions).
691127dd473Swhl739  * We can model the additional delay with an empirical curve
692127dd473Swhl739  * that represents its distribution.
693127dd473Swhl739  *
694127dd473Swhl739  *      cumulative probability
695127dd473Swhl739  *      1.0 ^
696127dd473Swhl739  *          |
697127dd473Swhl739  *      L   +-- loss-level          x
698127dd473Swhl739  *          |                 ******
699127dd473Swhl739  *          |                *
700127dd473Swhl739  *          |           *****
701127dd473Swhl739  *          |          *
702127dd473Swhl739  *          |        **
703127dd473Swhl739  *          |       *
704127dd473Swhl739  *          +-------*------------------->
705127dd473Swhl739  *                      delay
706127dd473Swhl739  *
707127dd473Swhl739  * The empirical curve may have both vertical and horizontal lines.
708127dd473Swhl739  * Vertical lines represent constant delay for a range of
709127dd473Swhl739  * probabilities; horizontal lines correspond to a discontinuty
710127dd473Swhl739  * in the delay distribution: the link will use the largest delay
711127dd473Swhl739  * for a given probability.
712127dd473Swhl739  *
713127dd473Swhl739  * To pass the curve to dummynet, we must store the parameters
714127dd473Swhl739  * in a file as described below, and issue the command
715127dd473Swhl739  *
716127dd473Swhl739  *      ipfw pipe <n> config ... bw XXX profile <filename> ...
717127dd473Swhl739  *
718127dd473Swhl739  * The file format is the following, with whitespace acting as
719127dd473Swhl739  * a separator and '#' indicating the beginning a comment:
720127dd473Swhl739  *
721127dd473Swhl739  *	samples N
722127dd473Swhl739  *		the number of samples used in the internal
723127dd473Swhl739  *		representation (2..1024; default 100);
724127dd473Swhl739  *
725127dd473Swhl739  *	loss-level L
726127dd473Swhl739  *		The probability above which packets are lost.
727127dd473Swhl739  *	       (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
728127dd473Swhl739  *
729127dd473Swhl739  *	name identifier
730127dd473Swhl739  *		Optional a name (listed by "ipfw pipe show")
731127dd473Swhl739  *		to identify the distribution;
732127dd473Swhl739  *
733127dd473Swhl739  *	"delay prob" | "prob delay"
734127dd473Swhl739  *		One of these two lines is mandatory and defines
735127dd473Swhl739  *		the format of the following lines with data points.
736127dd473Swhl739  *
737127dd473Swhl739  *	XXX YYY
738127dd473Swhl739  *		2 or more lines representing points in the curve,
739127dd473Swhl739  *		with either delay or probability first, according
740127dd473Swhl739  *		to the chosen format.
741127dd473Swhl739  *		The unit for delay is milliseconds.
742127dd473Swhl739  *
743127dd473Swhl739  * Data points does not need to be ordered or equal to the number
744127dd473Swhl739  * specified in the "samples" line. ipfw will sort and interpolate
745127dd473Swhl739  * the curve as needed.
746127dd473Swhl739  *
747127dd473Swhl739  * Example of a profile file:
748127dd473Swhl739 
749127dd473Swhl739 	name    bla_bla_bla
750127dd473Swhl739 	samples 100
751127dd473Swhl739 	loss-level    0.86
752127dd473Swhl739 	prob    delay
753127dd473Swhl739 	0       200	# minimum overhead is 200ms
754127dd473Swhl739 	0.5     200
755127dd473Swhl739 	0.5     300
756127dd473Swhl739 	0.8     1000
757127dd473Swhl739 	0.9     1300
758127dd473Swhl739 	1       1300
759127dd473Swhl739 
760127dd473Swhl739  * Internally, we will convert the curve to a fixed number of
761127dd473Swhl739  * samples, and when it is time to transmit a packet we will
762127dd473Swhl739  * model the extra delay as extra bits in the packet.
763127dd473Swhl739  *
764127dd473Swhl739  */
765127dd473Swhl739 
766127dd473Swhl739 #define ED_MAX_LINE_LEN	256+ED_MAX_NAME_LEN
767127dd473Swhl739 #define ED_TOK_SAMPLES	"samples"
768127dd473Swhl739 #define ED_TOK_LOSS	"loss-level"
769127dd473Swhl739 #define ED_TOK_NAME	"name"
770127dd473Swhl739 #define ED_TOK_DELAY	"delay"
771127dd473Swhl739 #define ED_TOK_PROB	"prob"
772127dd473Swhl739 #define ED_TOK_BW	"bw"
773127dd473Swhl739 #define ED_SEPARATORS	" \t\n"
774127dd473Swhl739 #define ED_MIN_SAMPLES_NO	2
775127dd473Swhl739 
776127dd473Swhl739 /*
777127dd473Swhl739  * returns 1 if s is a non-negative number, with at least one '.'
778127dd473Swhl739  */
779127dd473Swhl739 static int
is_valid_number(const char * s)780127dd473Swhl739 is_valid_number(const char *s)
781127dd473Swhl739 {
782127dd473Swhl739 	int i, dots_found = 0;
783127dd473Swhl739 	int len = strlen(s);
784127dd473Swhl739 
785127dd473Swhl739 	for (i = 0; i<len; ++i)
786127dd473Swhl739 		if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
787127dd473Swhl739 			return 0;
788127dd473Swhl739 	return 1;
789127dd473Swhl739 }
790127dd473Swhl739 
791127dd473Swhl739 /*
792127dd473Swhl739  * Take as input a string describing a bandwidth value
793127dd473Swhl739  * and return the numeric bandwidth value.
794127dd473Swhl739  * set clocking interface or bandwidth value
795127dd473Swhl739  */
796127dd473Swhl739 static void
read_bandwidth(char * arg,int * bandwidth,char * if_name,int namelen)797127dd473Swhl739 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
798127dd473Swhl739 {
799127dd473Swhl739 	if (*bandwidth != -1)
800127dd473Swhl739 		warnx("duplicate token, override bandwidth value!");
801127dd473Swhl739 
802127dd473Swhl739 	if (arg[0] >= 'a' && arg[0] <= 'z') {
803127dd473Swhl739 		if (!if_name) {
804127dd473Swhl739 			errx(1, "no if support");
805127dd473Swhl739 		}
806127dd473Swhl739 		if (namelen >= IFNAMSIZ)
807127dd473Swhl739 			warn("interface name truncated");
808127dd473Swhl739 		namelen--;
809127dd473Swhl739 		/* interface name */
81022ce4affSfengbojiang 		strlcpy(if_name, arg, namelen);
811127dd473Swhl739 		*bandwidth = 0;
812127dd473Swhl739 	} else {	/* read bandwidth value */
813127dd473Swhl739 		int bw;
814127dd473Swhl739 		char *end = NULL;
815127dd473Swhl739 
816127dd473Swhl739 		bw = strtoul(arg, &end, 0);
817127dd473Swhl739 		if (*end == 'K' || *end == 'k') {
818127dd473Swhl739 			end++;
819127dd473Swhl739 			bw *= 1000;
820127dd473Swhl739 		} else if (*end == 'M' || *end == 'm') {
821127dd473Swhl739 			end++;
822127dd473Swhl739 			bw *= 1000000;
82322ce4affSfengbojiang 		} else if (*end == 'G' || *end == 'g') {
82422ce4affSfengbojiang 			end++;
82522ce4affSfengbojiang 			bw *= 1000000000;
826127dd473Swhl739 		}
827127dd473Swhl739 		if ((*end == 'B' &&
828127dd473Swhl739 			_substrcmp2(end, "Bi", "Bit/s") != 0) ||
829127dd473Swhl739 		    _substrcmp2(end, "by", "bytes") == 0)
830127dd473Swhl739 			bw *= 8;
831127dd473Swhl739 
832127dd473Swhl739 		if (bw < 0)
833127dd473Swhl739 			errx(EX_DATAERR, "bandwidth too large");
834127dd473Swhl739 
835127dd473Swhl739 		*bandwidth = bw;
836127dd473Swhl739 		if (if_name)
837127dd473Swhl739 			if_name[0] = '\0';
838127dd473Swhl739 	}
839127dd473Swhl739 }
840127dd473Swhl739 
841127dd473Swhl739 struct point {
842127dd473Swhl739 	double prob;
843127dd473Swhl739 	double delay;
844127dd473Swhl739 };
845127dd473Swhl739 
846127dd473Swhl739 static int
compare_points(const void * vp1,const void * vp2)847127dd473Swhl739 compare_points(const void *vp1, const void *vp2)
848127dd473Swhl739 {
849127dd473Swhl739 	const struct point *p1 = vp1;
850127dd473Swhl739 	const struct point *p2 = vp2;
851127dd473Swhl739 	double res = 0;
852127dd473Swhl739 
853127dd473Swhl739 	res = p1->prob - p2->prob;
854127dd473Swhl739 	if (res == 0)
855127dd473Swhl739 		res = p1->delay - p2->delay;
856127dd473Swhl739 	if (res < 0)
857127dd473Swhl739 		return -1;
858127dd473Swhl739 	else if (res > 0)
859127dd473Swhl739 		return 1;
860127dd473Swhl739 	else
861127dd473Swhl739 		return 0;
862127dd473Swhl739 }
863127dd473Swhl739 
864127dd473Swhl739 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
865127dd473Swhl739 
866127dd473Swhl739 static void
load_extra_delays(const char * filename,struct dn_profile * p,struct dn_link * link)867127dd473Swhl739 load_extra_delays(const char *filename, struct dn_profile *p,
868127dd473Swhl739 	struct dn_link *link)
869127dd473Swhl739 {
870127dd473Swhl739 	char    line[ED_MAX_LINE_LEN];
871127dd473Swhl739 	FILE    *f;
872127dd473Swhl739 	int     lineno = 0;
873127dd473Swhl739 	int     i;
874127dd473Swhl739 
875127dd473Swhl739 	int     samples = -1;
876127dd473Swhl739 	double  loss = -1.0;
877127dd473Swhl739 	char    profile_name[ED_MAX_NAME_LEN];
878127dd473Swhl739 	int     delay_first = -1;
879127dd473Swhl739 	int     do_points = 0;
880127dd473Swhl739 	struct point    points[ED_MAX_SAMPLES_NO];
881127dd473Swhl739 	int     points_no = 0;
882127dd473Swhl739 
883127dd473Swhl739 	/* XXX link never NULL? */
884127dd473Swhl739 	p->link_nr = link->link_nr;
885127dd473Swhl739 
886127dd473Swhl739 	profile_name[0] = '\0';
887127dd473Swhl739 	f = fopen(filename, "r");
888127dd473Swhl739 	if (f == NULL)
889127dd473Swhl739 		err(EX_UNAVAILABLE, "fopen: %s", filename);
890127dd473Swhl739 
891127dd473Swhl739 	while (fgets(line, ED_MAX_LINE_LEN, f)) {	 /* read commands */
892127dd473Swhl739 		char *s, *cur = line, *name = NULL, *arg = NULL;
893127dd473Swhl739 
894127dd473Swhl739 		++lineno;
895127dd473Swhl739 
896127dd473Swhl739 		/* parse the line */
897127dd473Swhl739 		while (cur) {
898127dd473Swhl739 			s = strsep(&cur, ED_SEPARATORS);
899127dd473Swhl739 			if (s == NULL || *s == '#')
900127dd473Swhl739 				break;
901127dd473Swhl739 			if (*s == '\0')
902127dd473Swhl739 				continue;
903127dd473Swhl739 			if (arg)
904127dd473Swhl739 				errx(ED_EFMT("too many arguments"));
905127dd473Swhl739 			if (name == NULL)
906127dd473Swhl739 				name = s;
907127dd473Swhl739 			else
908127dd473Swhl739 				arg = s;
909127dd473Swhl739 		}
910127dd473Swhl739 		if (name == NULL)	/* empty line */
911127dd473Swhl739 			continue;
912127dd473Swhl739 		if (arg == NULL)
913127dd473Swhl739 			errx(ED_EFMT("missing arg for %s"), name);
914127dd473Swhl739 
915127dd473Swhl739 		if (!strcasecmp(name, ED_TOK_SAMPLES)) {
916127dd473Swhl739 		    if (samples > 0)
917127dd473Swhl739 			errx(ED_EFMT("duplicate ``samples'' line"));
918127dd473Swhl739 		    if (atoi(arg) <=0)
919127dd473Swhl739 			errx(ED_EFMT("invalid number of samples"));
920127dd473Swhl739 		    samples = atoi(arg);
921127dd473Swhl739 		    if (samples>ED_MAX_SAMPLES_NO)
922127dd473Swhl739 			    errx(ED_EFMT("too many samples, maximum is %d"),
923127dd473Swhl739 				ED_MAX_SAMPLES_NO);
924127dd473Swhl739 		    do_points = 0;
925127dd473Swhl739 		} else if (!strcasecmp(name, ED_TOK_BW)) {
926127dd473Swhl739 		    char buf[IFNAMSIZ];
927127dd473Swhl739 		    read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
928127dd473Swhl739 		} else if (!strcasecmp(name, ED_TOK_LOSS)) {
929127dd473Swhl739 		    if (loss != -1.0)
930127dd473Swhl739 			errx(ED_EFMT("duplicated token: %s"), name);
931127dd473Swhl739 		    if (!is_valid_number(arg))
932127dd473Swhl739 			errx(ED_EFMT("invalid %s"), arg);
933127dd473Swhl739 		    loss = atof(arg);
934127dd473Swhl739 		    if (loss > 1)
935127dd473Swhl739 			errx(ED_EFMT("%s greater than 1.0"), name);
936127dd473Swhl739 		    do_points = 0;
937127dd473Swhl739 		} else if (!strcasecmp(name, ED_TOK_NAME)) {
938127dd473Swhl739 		    if (profile_name[0] != '\0')
939127dd473Swhl739 			errx(ED_EFMT("duplicated token: %s"), name);
94022ce4affSfengbojiang 		    strlcpy(profile_name, arg, sizeof(profile_name));
941127dd473Swhl739 		    do_points = 0;
942127dd473Swhl739 		} else if (!strcasecmp(name, ED_TOK_DELAY)) {
943127dd473Swhl739 		    if (do_points)
944127dd473Swhl739 			errx(ED_EFMT("duplicated token: %s"), name);
945127dd473Swhl739 		    delay_first = 1;
946127dd473Swhl739 		    do_points = 1;
947127dd473Swhl739 		} else if (!strcasecmp(name, ED_TOK_PROB)) {
948127dd473Swhl739 		    if (do_points)
949127dd473Swhl739 			errx(ED_EFMT("duplicated token: %s"), name);
950127dd473Swhl739 		    delay_first = 0;
951127dd473Swhl739 		    do_points = 1;
952127dd473Swhl739 		} else if (do_points) {
953127dd473Swhl739 		    if (!is_valid_number(name) || !is_valid_number(arg))
954127dd473Swhl739 			errx(ED_EFMT("invalid point found"));
955127dd473Swhl739 		    if (delay_first) {
956127dd473Swhl739 			points[points_no].delay = atof(name);
957127dd473Swhl739 			points[points_no].prob = atof(arg);
958127dd473Swhl739 		    } else {
959127dd473Swhl739 			points[points_no].delay = atof(arg);
960127dd473Swhl739 			points[points_no].prob = atof(name);
961127dd473Swhl739 		    }
962127dd473Swhl739 		    if (points[points_no].prob > 1.0)
963127dd473Swhl739 			errx(ED_EFMT("probability greater than 1.0"));
964127dd473Swhl739 		    ++points_no;
965127dd473Swhl739 		} else {
966127dd473Swhl739 		    errx(ED_EFMT("unrecognised command '%s'"), name);
967127dd473Swhl739 		}
968127dd473Swhl739 	}
969127dd473Swhl739 
970127dd473Swhl739 	fclose (f);
971127dd473Swhl739 
972127dd473Swhl739 	if (samples == -1) {
973127dd473Swhl739 	    warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
974127dd473Swhl739 	    samples = 100;
975127dd473Swhl739 	}
976127dd473Swhl739 
977127dd473Swhl739 	if (loss == -1.0) {
978127dd473Swhl739 	    warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
979127dd473Swhl739 	    loss = 1;
980127dd473Swhl739 	}
981127dd473Swhl739 
982127dd473Swhl739 	/* make sure that there are enough points. */
983127dd473Swhl739 	if (points_no < ED_MIN_SAMPLES_NO)
984127dd473Swhl739 	    errx(ED_EFMT("too few samples, need at least %d"),
985127dd473Swhl739 		ED_MIN_SAMPLES_NO);
986127dd473Swhl739 
987127dd473Swhl739 	qsort(points, points_no, sizeof(struct point), compare_points);
988127dd473Swhl739 
989127dd473Swhl739 	/* interpolation */
990127dd473Swhl739 	for (i = 0; i<points_no-1; ++i) {
991127dd473Swhl739 	    double y1 = points[i].prob * samples;
992127dd473Swhl739 	    double x1 = points[i].delay;
993127dd473Swhl739 	    double y2 = points[i+1].prob * samples;
994127dd473Swhl739 	    double x2 = points[i+1].delay;
995127dd473Swhl739 
996127dd473Swhl739 	    int ix = y1;
997127dd473Swhl739 	    int stop = y2;
998127dd473Swhl739 
999127dd473Swhl739 	    if (x1 == x2) {
1000127dd473Swhl739 		for (; ix<stop; ++ix)
1001127dd473Swhl739 		    p->samples[ix] = x1;
1002127dd473Swhl739 	    } else {
1003127dd473Swhl739 		double m = (y2-y1)/(x2-x1);
1004127dd473Swhl739 		double c = y1 - m*x1;
1005127dd473Swhl739 		for (; ix<stop ; ++ix)
1006127dd473Swhl739 		    p->samples[ix] = (ix - c)/m;
1007127dd473Swhl739 	    }
1008127dd473Swhl739 	}
1009127dd473Swhl739 	p->samples_no = samples;
1010127dd473Swhl739 	p->loss_level = loss * samples;
101122ce4affSfengbojiang 	strlcpy(p->name, profile_name, sizeof(p->name));
1012127dd473Swhl739 }
1013127dd473Swhl739 
1014127dd473Swhl739 #ifdef NEW_AQM
1015127dd473Swhl739 
1016127dd473Swhl739 /* Parse AQM/extra scheduler parameters */
1017127dd473Swhl739 static int
process_extra_parms(int * ac,char ** av,struct dn_extra_parms * ep,uint16_t type)1018127dd473Swhl739 process_extra_parms(int *ac, char **av, struct dn_extra_parms *ep,
1019127dd473Swhl739 	uint16_t type)
1020127dd473Swhl739 {
1021127dd473Swhl739 	int i;
1022127dd473Swhl739 
1023127dd473Swhl739 	/* use kernel defaults */
1024127dd473Swhl739 	for (i=0; i<DN_MAX_EXTRA_PARM; i++)
1025127dd473Swhl739 		ep->par[i] = -1;
1026127dd473Swhl739 
1027127dd473Swhl739 	switch(type) {
1028127dd473Swhl739 	case TOK_CODEL:
1029127dd473Swhl739 	case TOK_FQ_CODEL:
1030127dd473Swhl739 	/* Codel
1031127dd473Swhl739 	 * 0- target, 1- interval, 2- flags,
1032127dd473Swhl739 	 * FQ_CODEL
1033127dd473Swhl739 	 * 3- quantum, 4- limit, 5- flows
1034127dd473Swhl739 	 */
1035127dd473Swhl739 		if (type==TOK_CODEL)
1036127dd473Swhl739 			ep->par[2] = 0;
1037127dd473Swhl739 		else
1038127dd473Swhl739 			ep->par[2] = CODEL_ECN_ENABLED;
1039127dd473Swhl739 
1040127dd473Swhl739 		while (*ac > 0) {
1041127dd473Swhl739 			int tok = match_token(aqm_params, *av);
1042127dd473Swhl739 			(*ac)--; av++;
1043127dd473Swhl739 			switch(tok) {
1044127dd473Swhl739 			case TOK_TARGET:
1045127dd473Swhl739 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1046127dd473Swhl739 					errx(EX_DATAERR, "target needs time\n");
1047127dd473Swhl739 
1048127dd473Swhl739 				ep->par[0] = time_to_us(av[0]);
1049127dd473Swhl739 				(*ac)--; av++;
1050127dd473Swhl739 				break;
1051127dd473Swhl739 
1052127dd473Swhl739 			case TOK_INTERVAL:
1053127dd473Swhl739 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1054127dd473Swhl739 					errx(EX_DATAERR, "interval needs time\n");
1055127dd473Swhl739 
1056127dd473Swhl739 				ep->par[1] = time_to_us(av[0]);
1057127dd473Swhl739 				(*ac)--; av++;
1058127dd473Swhl739 				break;
1059127dd473Swhl739 
1060127dd473Swhl739 			case TOK_ECN:
1061127dd473Swhl739 				ep->par[2] = CODEL_ECN_ENABLED;
1062127dd473Swhl739 				break;
1063127dd473Swhl739 			case TOK_NO_ECN:
1064127dd473Swhl739 				ep->par[2] &= ~CODEL_ECN_ENABLED;
1065127dd473Swhl739 				break;
1066127dd473Swhl739 			/* Config fq_codel parameters */
1067127dd473Swhl739 			case TOK_QUANTUM:
1068127dd473Swhl739 				if (type != TOK_FQ_CODEL)
1069127dd473Swhl739 					errx(EX_DATAERR, "quantum is not for codel\n");
1070127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1071127dd473Swhl739 					errx(EX_DATAERR, "quantum needs number\n");
1072127dd473Swhl739 
1073127dd473Swhl739 				ep->par[3]= atoi(av[0]);
1074127dd473Swhl739 				(*ac)--; av++;
1075127dd473Swhl739 				break;
1076127dd473Swhl739 
1077127dd473Swhl739 			case TOK_LIMIT:
1078127dd473Swhl739 				if (type != TOK_FQ_CODEL)
1079127dd473Swhl739 					errx(EX_DATAERR, "limit is not for codel, use queue instead\n");
1080127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1081127dd473Swhl739 					errx(EX_DATAERR, "limit needs number\n");
1082127dd473Swhl739 
1083127dd473Swhl739 				ep->par[4] = atoi(av[0]);
1084127dd473Swhl739 				(*ac)--; av++;
1085127dd473Swhl739 				break;
1086127dd473Swhl739 
1087127dd473Swhl739 			case TOK_FLOWS:
1088127dd473Swhl739 				if (type != TOK_FQ_CODEL)
1089127dd473Swhl739 					errx(EX_DATAERR, "flows is not for codel\n");
1090127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1091127dd473Swhl739 					errx(EX_DATAERR, "flows needs number\n");
1092127dd473Swhl739 
1093127dd473Swhl739 				ep->par[5] = atoi(av[0]);
1094127dd473Swhl739 				(*ac)--; av++;
1095127dd473Swhl739 				break;
1096127dd473Swhl739 
1097127dd473Swhl739 			default:
1098127dd473Swhl739 				printf("%s is Invalid parameter\n", av[-1]);
1099127dd473Swhl739 			}
1100127dd473Swhl739 		}
1101127dd473Swhl739 		break;
1102127dd473Swhl739 	case TOK_PIE:
1103127dd473Swhl739 	case TOK_FQ_PIE:
1104127dd473Swhl739 		/* PIE
1105127dd473Swhl739 		 * 0- target , 1- tupdate, 2- max_burst,
1106127dd473Swhl739 		 * 3- max_ecnth, 4- alpha,
1107127dd473Swhl739 		 * 5- beta, 6- flags
1108127dd473Swhl739 		 * FQ_CODEL
1109127dd473Swhl739 		 * 7- quantum, 8- limit, 9- flows
1110127dd473Swhl739 		 */
1111127dd473Swhl739 
1112127dd473Swhl739 		if ( type == TOK_PIE)
1113127dd473Swhl739 			ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED
1114127dd473Swhl739 				| PIE_DERAND_ENABLED;
1115127dd473Swhl739 		else
1116127dd473Swhl739 			/* for FQ-PIE, use TS mode */
1117127dd473Swhl739 			ep->par[6] = PIE_CAPDROP_ENABLED |  PIE_DERAND_ENABLED
1118127dd473Swhl739 				| PIE_ECN_ENABLED;
1119127dd473Swhl739 
1120127dd473Swhl739 		while (*ac > 0) {
1121127dd473Swhl739 			int tok = match_token(aqm_params, *av);
1122127dd473Swhl739 			(*ac)--; av++;
1123127dd473Swhl739 			switch(tok) {
1124127dd473Swhl739 			case TOK_TARGET:
1125127dd473Swhl739 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1126127dd473Swhl739 					errx(EX_DATAERR, "target needs time\n");
1127127dd473Swhl739 
1128127dd473Swhl739 				ep->par[0] = time_to_us(av[0]);
1129127dd473Swhl739 				(*ac)--; av++;
1130127dd473Swhl739 				break;
1131127dd473Swhl739 
1132127dd473Swhl739 			case TOK_TUPDATE:
1133127dd473Swhl739 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1134127dd473Swhl739 					errx(EX_DATAERR, "tupdate needs time\n");
1135127dd473Swhl739 
1136127dd473Swhl739 				ep->par[1] = time_to_us(av[0]);
1137127dd473Swhl739 				(*ac)--; av++;
1138127dd473Swhl739 				break;
1139127dd473Swhl739 
1140127dd473Swhl739 			case TOK_MAX_BURST:
1141127dd473Swhl739 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1142127dd473Swhl739 					errx(EX_DATAERR, "max_burst needs time\n");
1143127dd473Swhl739 
1144127dd473Swhl739 				ep->par[2] = time_to_us(av[0]);
1145127dd473Swhl739 				(*ac)--; av++;
1146127dd473Swhl739 				break;
1147127dd473Swhl739 
1148127dd473Swhl739 			case TOK_MAX_ECNTH:
1149127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1150127dd473Swhl739 					errx(EX_DATAERR, "max_ecnth needs number\n");
1151127dd473Swhl739 
1152127dd473Swhl739 				ep->par[3] = atof(av[0]) * PIE_SCALE;
1153127dd473Swhl739 				(*ac)--; av++;
1154127dd473Swhl739 				break;
1155127dd473Swhl739 
1156127dd473Swhl739 			case TOK_ALPHA:
1157127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1158127dd473Swhl739 					errx(EX_DATAERR, "alpha needs number\n");
1159127dd473Swhl739 
1160127dd473Swhl739 				ep->par[4] = atof(av[0]) * PIE_SCALE;
1161127dd473Swhl739 				(*ac)--; av++;
1162127dd473Swhl739 				break;
1163127dd473Swhl739 
1164127dd473Swhl739 			case TOK_BETA:
1165127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1166127dd473Swhl739 					errx(EX_DATAERR, "beta needs number\n");
1167127dd473Swhl739 
1168127dd473Swhl739 				ep->par[5] = atof(av[0]) * PIE_SCALE;
1169127dd473Swhl739 				(*ac)--; av++;
1170127dd473Swhl739 				break;
1171127dd473Swhl739 
1172127dd473Swhl739 			case TOK_ECN:
1173127dd473Swhl739 				ep->par[6] |= PIE_ECN_ENABLED;
1174127dd473Swhl739 				break;
1175127dd473Swhl739 			case TOK_NO_ECN:
1176127dd473Swhl739 				ep->par[6] &= ~PIE_ECN_ENABLED;
1177127dd473Swhl739 				break;
1178127dd473Swhl739 
1179127dd473Swhl739 			case TOK_CAPDROP:
1180127dd473Swhl739 				ep->par[6] |= PIE_CAPDROP_ENABLED;
1181127dd473Swhl739 				break;
1182127dd473Swhl739 			case TOK_NO_CAPDROP:
1183127dd473Swhl739 				ep->par[6] &= ~PIE_CAPDROP_ENABLED;
1184127dd473Swhl739 				break;
1185127dd473Swhl739 
1186127dd473Swhl739 			case TOK_ONOFF:
1187127dd473Swhl739 				ep->par[6] |= PIE_ON_OFF_MODE_ENABLED;
1188127dd473Swhl739 				break;
1189127dd473Swhl739 
1190127dd473Swhl739 			case TOK_DRE:
1191127dd473Swhl739 				ep->par[6] |= PIE_DEPRATEEST_ENABLED;
1192127dd473Swhl739 				break;
1193127dd473Swhl739 
1194127dd473Swhl739 			case TOK_TS:
1195127dd473Swhl739 				ep->par[6] &= ~PIE_DEPRATEEST_ENABLED;
1196127dd473Swhl739 				break;
1197127dd473Swhl739 
1198127dd473Swhl739 			case TOK_DERAND:
1199127dd473Swhl739 				ep->par[6] |= PIE_DERAND_ENABLED;
1200127dd473Swhl739 				break;
1201127dd473Swhl739 			case TOK_NO_DERAND:
1202127dd473Swhl739 				ep->par[6] &= ~PIE_DERAND_ENABLED;
1203127dd473Swhl739 				break;
1204127dd473Swhl739 
1205127dd473Swhl739 			/* Config fq_pie parameters */
1206127dd473Swhl739 			case TOK_QUANTUM:
1207127dd473Swhl739 				if (type != TOK_FQ_PIE)
1208127dd473Swhl739 					errx(EX_DATAERR, "quantum is not for pie\n");
1209127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1210127dd473Swhl739 					errx(EX_DATAERR, "quantum needs number\n");
1211127dd473Swhl739 
1212127dd473Swhl739 				ep->par[7]= atoi(av[0]);
1213127dd473Swhl739 				(*ac)--; av++;
1214127dd473Swhl739 				break;
1215127dd473Swhl739 
1216127dd473Swhl739 			case TOK_LIMIT:
1217127dd473Swhl739 				if (type != TOK_FQ_PIE)
1218127dd473Swhl739 					errx(EX_DATAERR, "limit is not for pie, use queue instead\n");
1219127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1220127dd473Swhl739 					errx(EX_DATAERR, "limit needs number\n");
1221127dd473Swhl739 
1222127dd473Swhl739 				ep->par[8] = atoi(av[0]);
1223127dd473Swhl739 				(*ac)--; av++;
1224127dd473Swhl739 				break;
1225127dd473Swhl739 
1226127dd473Swhl739 			case TOK_FLOWS:
1227127dd473Swhl739 				if (type != TOK_FQ_PIE)
1228127dd473Swhl739 					errx(EX_DATAERR, "flows is not for pie\n");
1229127dd473Swhl739 				if (*ac <= 0 || !is_valid_number(av[0]))
1230127dd473Swhl739 					errx(EX_DATAERR, "flows needs number\n");
1231127dd473Swhl739 
1232127dd473Swhl739 				ep->par[9] = atoi(av[0]);
1233127dd473Swhl739 				(*ac)--; av++;
1234127dd473Swhl739 				break;
1235127dd473Swhl739 
1236127dd473Swhl739 
1237127dd473Swhl739 			default:
1238127dd473Swhl739 				printf("%s is invalid parameter\n", av[-1]);
1239127dd473Swhl739 			}
1240127dd473Swhl739 		}
1241127dd473Swhl739 		break;
1242127dd473Swhl739 	}
1243127dd473Swhl739 
1244127dd473Swhl739 	return 0;
1245127dd473Swhl739 }
1246127dd473Swhl739 
1247127dd473Swhl739 #endif
1248127dd473Swhl739 
1249127dd473Swhl739 
1250127dd473Swhl739 /*
1251127dd473Swhl739  * configuration of pipes, schedulers, flowsets.
1252127dd473Swhl739  * When we configure a new scheduler, an empty pipe is created, so:
1253127dd473Swhl739  *
1254127dd473Swhl739  * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
1255127dd473Swhl739  *	sched N+Delta type fifo sched_mask ...
1256127dd473Swhl739  *	pipe N+Delta <parameters>
1257127dd473Swhl739  *	flowset N+Delta pipe N+Delta (no parameters)
1258127dd473Swhl739  *	sched N type wf2q+ sched_mask ...
1259127dd473Swhl739  *	pipe N <parameters>
1260127dd473Swhl739  *
1261127dd473Swhl739  * do_pipe = 2 -> flowset N config
1262127dd473Swhl739  *	flowset N parameters
1263127dd473Swhl739  *
1264127dd473Swhl739  * do_pipe = 3 -> sched N config
1265127dd473Swhl739  *	sched N parameters (default no pipe)
1266127dd473Swhl739  *	optional Pipe N config ...
1267127dd473Swhl739  * pipe ==>
1268127dd473Swhl739  */
1269127dd473Swhl739 void
ipfw_config_pipe(int ac,char ** av)1270127dd473Swhl739 ipfw_config_pipe(int ac, char **av)
1271127dd473Swhl739 {
1272127dd473Swhl739 	int i;
1273127dd473Swhl739 	u_int j;
1274127dd473Swhl739 	char *end;
1275127dd473Swhl739 	struct dn_id *buf, *base;
1276127dd473Swhl739 	struct dn_sch *sch = NULL;
1277127dd473Swhl739 	struct dn_link *p = NULL;
1278127dd473Swhl739 	struct dn_fs *fs = NULL;
1279127dd473Swhl739 	struct dn_profile *pf = NULL;
1280127dd473Swhl739 	struct ipfw_flow_id *mask = NULL;
1281127dd473Swhl739 #ifdef NEW_AQM
128222ce4affSfengbojiang 	struct dn_extra_parms *aqm_extra = NULL;
128322ce4affSfengbojiang 	struct dn_extra_parms *sch_extra = NULL;
1284127dd473Swhl739 	int lmax_extra;
1285127dd473Swhl739 #endif
1286127dd473Swhl739 
1287127dd473Swhl739 	int lmax;
1288127dd473Swhl739 	uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
1289127dd473Swhl739 
1290127dd473Swhl739 	/*
1291127dd473Swhl739 	 * allocate space for 1 header,
1292127dd473Swhl739 	 * 1 scheduler, 1 link, 1 flowset, 1 profile
1293127dd473Swhl739 	 */
1294127dd473Swhl739 	lmax = sizeof(struct dn_id);	/* command header */
1295127dd473Swhl739 	lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
1296127dd473Swhl739 		sizeof(struct dn_fs) + sizeof(struct dn_profile);
1297127dd473Swhl739 
1298127dd473Swhl739 #ifdef NEW_AQM
1299127dd473Swhl739 	/* Extra Params */
1300127dd473Swhl739 	lmax_extra = sizeof(struct dn_extra_parms);
1301127dd473Swhl739 	/* two lmax_extra because one for AQM params and another
1302127dd473Swhl739 	 * sch params
1303127dd473Swhl739 	 */
1304127dd473Swhl739 	lmax += lmax_extra*2;
1305127dd473Swhl739 #endif
1306127dd473Swhl739 
1307127dd473Swhl739 	av++; ac--;
1308127dd473Swhl739 	/* Pipe number */
1309127dd473Swhl739 	if (ac && isdigit(**av)) {
1310127dd473Swhl739 		i = atoi(*av); av++; ac--;
1311127dd473Swhl739 	} else
1312127dd473Swhl739 		i = -1;
1313127dd473Swhl739 	if (i <= 0)
1314127dd473Swhl739 		errx(EX_USAGE, "need a pipe/flowset/sched number");
1315127dd473Swhl739 	base = buf = safe_calloc(1, lmax);
1316127dd473Swhl739 	/* all commands start with a 'CONFIGURE' and a version */
1317127dd473Swhl739 	o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
1318127dd473Swhl739 	base->id = DN_API_VERSION;
1319127dd473Swhl739 
132022ce4affSfengbojiang 	switch (g_co.do_pipe) {
1321127dd473Swhl739 	case 1: /* "pipe N config ..." */
1322127dd473Swhl739 		/* Allocate space for the WF2Q+ scheduler, its link
1323127dd473Swhl739 		 * and the FIFO flowset. Set the number, but leave
1324127dd473Swhl739 		 * the scheduler subtype and other parameters to 0
1325127dd473Swhl739 		 * so the kernel will use appropriate defaults.
1326127dd473Swhl739 		 * XXX todo: add a flag to record if a parameter
1327127dd473Swhl739 		 * is actually configured.
1328127dd473Swhl739 		 * If we do a 'pipe config' mask -> sched_mask.
1329127dd473Swhl739 		 * The FIFO scheduler and link are derived from the
1330127dd473Swhl739 		 * WF2Q+ one in the kernel.
1331127dd473Swhl739 		 */
1332127dd473Swhl739 #ifdef NEW_AQM
1333127dd473Swhl739 		sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1334127dd473Swhl739 		sch_extra ->oid.subtype = 0; /* don't configure scheduler */
1335127dd473Swhl739 #endif
1336127dd473Swhl739 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
1337127dd473Swhl739 		p = o_next(&buf, sizeof(*p), DN_LINK);
1338127dd473Swhl739 #ifdef NEW_AQM
1339127dd473Swhl739 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1340127dd473Swhl739 		aqm_extra ->oid.subtype = 0; /* don't configure AQM */
1341127dd473Swhl739 #endif
1342127dd473Swhl739 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1343127dd473Swhl739 
1344127dd473Swhl739 		sch->sched_nr = i;
1345127dd473Swhl739 		sch->oid.subtype = 0;	/* defaults to WF2Q+ */
1346127dd473Swhl739 		mask = &sch->sched_mask;
1347127dd473Swhl739 		flags = &sch->flags;
1348127dd473Swhl739 		buckets = &sch->buckets;
1349127dd473Swhl739 		*flags |= DN_PIPE_CMD;
1350127dd473Swhl739 
1351127dd473Swhl739 		p->link_nr = i;
1352127dd473Swhl739 
1353127dd473Swhl739 		/* This flowset is only for the FIFO scheduler */
1354127dd473Swhl739 		fs->fs_nr = i + 2*DN_MAX_ID;
1355127dd473Swhl739 		fs->sched_nr = i + DN_MAX_ID;
1356127dd473Swhl739 		break;
1357127dd473Swhl739 
1358127dd473Swhl739 	case 2: /* "queue N config ... " */
1359127dd473Swhl739 #ifdef NEW_AQM
1360127dd473Swhl739 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1361127dd473Swhl739 		aqm_extra ->oid.subtype = 0;
1362127dd473Swhl739 #endif
1363127dd473Swhl739 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1364127dd473Swhl739 		fs->fs_nr = i;
1365127dd473Swhl739 		mask = &fs->flow_mask;
1366127dd473Swhl739 		flags = &fs->flags;
1367127dd473Swhl739 		buckets = &fs->buckets;
1368127dd473Swhl739 		break;
1369127dd473Swhl739 
1370127dd473Swhl739 	case 3: /* "sched N config ..." */
1371127dd473Swhl739 #ifdef NEW_AQM
1372127dd473Swhl739 		sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1373127dd473Swhl739 		sch_extra ->oid.subtype = 0;
1374127dd473Swhl739 #endif
1375127dd473Swhl739 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
1376127dd473Swhl739 #ifdef NEW_AQM
1377127dd473Swhl739 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1378127dd473Swhl739 		aqm_extra ->oid.subtype = 0;
1379127dd473Swhl739 #endif
1380127dd473Swhl739 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1381127dd473Swhl739 		sch->sched_nr = i;
1382127dd473Swhl739 		mask = &sch->sched_mask;
1383127dd473Swhl739 		flags = &sch->flags;
1384127dd473Swhl739 		buckets = &sch->buckets;
1385127dd473Swhl739 		/* fs is used only with !MULTIQUEUE schedulers */
1386127dd473Swhl739 		fs->fs_nr = i + DN_MAX_ID;
1387127dd473Swhl739 		fs->sched_nr = i;
1388127dd473Swhl739 		break;
1389127dd473Swhl739 	}
1390127dd473Swhl739 	/* set to -1 those fields for which we want to reuse existing
1391127dd473Swhl739 	 * values from the kernel.
1392127dd473Swhl739 	 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
1393127dd473Swhl739 	 * XXX todo: support reuse of the mask.
1394127dd473Swhl739 	 */
1395127dd473Swhl739 	if (p)
1396127dd473Swhl739 		p->bandwidth = -1;
1397127dd473Swhl739 	for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
1398127dd473Swhl739 		fs->par[j] = -1;
1399127dd473Swhl739 	while (ac > 0) {
1400127dd473Swhl739 		double d;
1401127dd473Swhl739 		int tok = match_token(dummynet_params, *av);
1402127dd473Swhl739 		ac--; av++;
1403127dd473Swhl739 
1404127dd473Swhl739 		switch(tok) {
1405127dd473Swhl739 		case TOK_NOERROR:
1406127dd473Swhl739 			NEED(fs, "noerror is only for pipes");
1407127dd473Swhl739 			fs->flags |= DN_NOERROR;
1408127dd473Swhl739 			break;
1409127dd473Swhl739 
1410127dd473Swhl739 		case TOK_PLR:
1411127dd473Swhl739 			NEED(fs, "plr is only for pipes");
1412127dd473Swhl739 			NEED1("plr needs argument 0..1\n");
1413127dd473Swhl739 			d = strtod(av[0], NULL);
1414127dd473Swhl739 			if (d > 1)
1415127dd473Swhl739 				d = 1;
1416127dd473Swhl739 			else if (d < 0)
1417127dd473Swhl739 				d = 0;
1418127dd473Swhl739 			fs->plr = (int)(d*0x7fffffff);
1419127dd473Swhl739 			ac--; av++;
1420127dd473Swhl739 			break;
1421127dd473Swhl739 
1422127dd473Swhl739 		case TOK_QUEUE:
1423127dd473Swhl739 			NEED(fs, "queue is only for pipes or flowsets");
1424127dd473Swhl739 			NEED1("queue needs queue size\n");
1425127dd473Swhl739 			end = NULL;
1426127dd473Swhl739 			fs->qsize = strtoul(av[0], &end, 0);
1427127dd473Swhl739 			if (*end == 'K' || *end == 'k') {
1428127dd473Swhl739 				fs->flags |= DN_QSIZE_BYTES;
1429127dd473Swhl739 				fs->qsize *= 1024;
1430127dd473Swhl739 			} else if (*end == 'B' ||
1431127dd473Swhl739 			    _substrcmp2(end, "by", "bytes") == 0) {
1432127dd473Swhl739 				fs->flags |= DN_QSIZE_BYTES;
1433127dd473Swhl739 			}
1434127dd473Swhl739 			ac--; av++;
1435127dd473Swhl739 			break;
1436127dd473Swhl739 
1437127dd473Swhl739 		case TOK_BUCKETS:
1438127dd473Swhl739 			NEED(fs, "buckets is only for pipes or flowsets");
1439127dd473Swhl739 			NEED1("buckets needs argument\n");
1440127dd473Swhl739 			*buckets = strtoul(av[0], NULL, 0);
1441127dd473Swhl739 			ac--; av++;
1442127dd473Swhl739 			break;
1443127dd473Swhl739 
1444127dd473Swhl739 		case TOK_FLOW_MASK:
1445127dd473Swhl739 		case TOK_SCHED_MASK:
1446127dd473Swhl739 		case TOK_MASK:
1447127dd473Swhl739 			NEED(mask, "tok_mask");
1448127dd473Swhl739 			NEED1("mask needs mask specifier\n");
1449127dd473Swhl739 			/*
1450127dd473Swhl739 			 * per-flow queue, mask is dst_ip, dst_port,
1451127dd473Swhl739 			 * src_ip, src_port, proto measured in bits
1452127dd473Swhl739 			 */
1453127dd473Swhl739 
1454127dd473Swhl739 			bzero(mask, sizeof(*mask));
1455127dd473Swhl739 			end = NULL;
1456127dd473Swhl739 
1457127dd473Swhl739 			while (ac >= 1) {
1458127dd473Swhl739 			    uint32_t *p32 = NULL;
1459127dd473Swhl739 			    uint16_t *p16 = NULL;
1460127dd473Swhl739 			    uint32_t *p20 = NULL;
1461127dd473Swhl739 			    struct in6_addr *pa6 = NULL;
1462127dd473Swhl739 			    uint32_t a;
1463127dd473Swhl739 
1464127dd473Swhl739 			    tok = match_token(dummynet_params, *av);
1465127dd473Swhl739 			    ac--; av++;
1466127dd473Swhl739 			    switch(tok) {
1467127dd473Swhl739 			    case TOK_ALL:
1468127dd473Swhl739 				    /*
1469127dd473Swhl739 				     * special case, all bits significant
1470127dd473Swhl739 				     * except 'extra' (the queue number)
1471127dd473Swhl739 				     */
1472127dd473Swhl739 				    mask->dst_ip = ~0;
1473127dd473Swhl739 				    mask->src_ip = ~0;
1474127dd473Swhl739 				    mask->dst_port = ~0;
1475127dd473Swhl739 				    mask->src_port = ~0;
1476127dd473Swhl739 				    mask->proto = ~0;
1477127dd473Swhl739 				    n2mask(&mask->dst_ip6, 128);
1478127dd473Swhl739 				    n2mask(&mask->src_ip6, 128);
1479127dd473Swhl739 				    mask->flow_id6 = ~0;
1480127dd473Swhl739 				    *flags |= DN_HAVE_MASK;
1481127dd473Swhl739 				    goto end_mask;
1482127dd473Swhl739 
1483127dd473Swhl739 			    case TOK_QUEUE:
1484127dd473Swhl739 				    mask->extra = ~0;
1485127dd473Swhl739 				    *flags |= DN_HAVE_MASK;
1486127dd473Swhl739 				    goto end_mask;
1487127dd473Swhl739 
1488127dd473Swhl739 			    case TOK_DSTIP:
1489127dd473Swhl739 				    mask->addr_type = 4;
1490127dd473Swhl739 				    p32 = &mask->dst_ip;
1491127dd473Swhl739 				    break;
1492127dd473Swhl739 
1493127dd473Swhl739 			    case TOK_SRCIP:
1494127dd473Swhl739 				    mask->addr_type = 4;
1495127dd473Swhl739 				    p32 = &mask->src_ip;
1496127dd473Swhl739 				    break;
1497127dd473Swhl739 
1498127dd473Swhl739 			    case TOK_DSTIP6:
1499127dd473Swhl739 				    mask->addr_type = 6;
1500127dd473Swhl739 				    pa6 = &mask->dst_ip6;
1501127dd473Swhl739 				    break;
1502127dd473Swhl739 
1503127dd473Swhl739 			    case TOK_SRCIP6:
1504127dd473Swhl739 				    mask->addr_type = 6;
1505127dd473Swhl739 				    pa6 = &mask->src_ip6;
1506127dd473Swhl739 				    break;
1507127dd473Swhl739 
1508127dd473Swhl739 			    case TOK_FLOWID:
1509127dd473Swhl739 				    mask->addr_type = 6;
1510127dd473Swhl739 				    p20 = &mask->flow_id6;
1511127dd473Swhl739 				    break;
1512127dd473Swhl739 
1513127dd473Swhl739 			    case TOK_DSTPORT:
1514127dd473Swhl739 				    p16 = &mask->dst_port;
1515127dd473Swhl739 				    break;
1516127dd473Swhl739 
1517127dd473Swhl739 			    case TOK_SRCPORT:
1518127dd473Swhl739 				    p16 = &mask->src_port;
1519127dd473Swhl739 				    break;
1520127dd473Swhl739 
1521127dd473Swhl739 			    case TOK_PROTO:
1522127dd473Swhl739 				    break;
1523127dd473Swhl739 
1524127dd473Swhl739 			    default:
1525127dd473Swhl739 				    ac++; av--; /* backtrack */
1526127dd473Swhl739 				    goto end_mask;
1527127dd473Swhl739 			    }
1528127dd473Swhl739 			    if (ac < 1)
1529127dd473Swhl739 				    errx(EX_USAGE, "mask: value missing");
1530127dd473Swhl739 			    if (*av[0] == '/') {
1531127dd473Swhl739 				    a = strtoul(av[0]+1, &end, 0);
1532127dd473Swhl739 				    if (pa6 == NULL)
1533127dd473Swhl739 					    a = (a == 32) ? ~0 : (1 << a) - 1;
1534127dd473Swhl739 			    } else
1535127dd473Swhl739 				    a = strtoul(av[0], &end, 0);
1536127dd473Swhl739 			    if (p32 != NULL)
1537127dd473Swhl739 				    *p32 = a;
1538127dd473Swhl739 			    else if (p16 != NULL) {
1539127dd473Swhl739 				    if (a > 0xFFFF)
1540127dd473Swhl739 					    errx(EX_DATAERR,
1541127dd473Swhl739 						"port mask must be 16 bit");
1542127dd473Swhl739 				    *p16 = (uint16_t)a;
1543127dd473Swhl739 			    } else if (p20 != NULL) {
1544127dd473Swhl739 				    if (a > 0xfffff)
1545127dd473Swhl739 					errx(EX_DATAERR,
1546127dd473Swhl739 					    "flow_id mask must be 20 bit");
1547127dd473Swhl739 				    *p20 = (uint32_t)a;
1548127dd473Swhl739 			    } else if (pa6 != NULL) {
1549127dd473Swhl739 				    if (a > 128)
1550127dd473Swhl739 					errx(EX_DATAERR,
1551127dd473Swhl739 					    "in6addr invalid mask len");
1552127dd473Swhl739 				    else
1553127dd473Swhl739 					n2mask(pa6, a);
1554127dd473Swhl739 			    } else {
1555127dd473Swhl739 				    if (a > 0xFF)
1556127dd473Swhl739 					    errx(EX_DATAERR,
1557127dd473Swhl739 						"proto mask must be 8 bit");
1558127dd473Swhl739 				    mask->proto = (uint8_t)a;
1559127dd473Swhl739 			    }
1560127dd473Swhl739 			    if (a != 0)
1561127dd473Swhl739 				    *flags |= DN_HAVE_MASK;
1562127dd473Swhl739 			    ac--; av++;
1563127dd473Swhl739 			} /* end while, config masks */
1564127dd473Swhl739 end_mask:
1565127dd473Swhl739 			break;
1566127dd473Swhl739 #ifdef NEW_AQM
1567127dd473Swhl739 		case TOK_CODEL:
1568127dd473Swhl739 		case TOK_PIE:
1569127dd473Swhl739 			NEED(fs, "codel/pie is only for flowsets");
1570127dd473Swhl739 
1571127dd473Swhl739 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1572127dd473Swhl739 			fs->flags |= DN_IS_AQM;
1573127dd473Swhl739 
157422ce4affSfengbojiang 			strlcpy(aqm_extra->name, av[-1],
157522ce4affSfengbojiang 			    sizeof(aqm_extra->name));
1576127dd473Swhl739 			aqm_extra->oid.subtype = DN_AQM_PARAMS;
1577127dd473Swhl739 
1578127dd473Swhl739 			process_extra_parms(&ac, av, aqm_extra, tok);
1579127dd473Swhl739 			break;
1580127dd473Swhl739 
1581127dd473Swhl739 		case TOK_FQ_CODEL:
1582127dd473Swhl739 		case TOK_FQ_PIE:
1583127dd473Swhl739 			if (!strcmp(av[-1],"type"))
1584127dd473Swhl739 				errx(EX_DATAERR, "use type before fq_codel/fq_pie");
1585127dd473Swhl739 
1586127dd473Swhl739 			NEED(sch, "fq_codel/fq_pie is only for schd");
158722ce4affSfengbojiang 			strlcpy(sch_extra->name, av[-1],
158822ce4affSfengbojiang 			    sizeof(sch_extra->name));
1589127dd473Swhl739 			sch_extra->oid.subtype = DN_SCH_PARAMS;
1590127dd473Swhl739 			process_extra_parms(&ac, av, sch_extra, tok);
1591127dd473Swhl739 			break;
1592127dd473Swhl739 #endif
1593127dd473Swhl739 		case TOK_RED:
1594127dd473Swhl739 		case TOK_GRED:
1595127dd473Swhl739 			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1596127dd473Swhl739 			fs->flags |= DN_IS_RED;
1597127dd473Swhl739 			if (tok == TOK_GRED)
1598127dd473Swhl739 				fs->flags |= DN_IS_GENTLE_RED;
1599127dd473Swhl739 			/*
1600127dd473Swhl739 			 * the format for parameters is w_q/min_th/max_th/max_p
1601127dd473Swhl739 			 */
1602127dd473Swhl739 			if ((end = strsep(&av[0], "/"))) {
1603127dd473Swhl739 			    double w_q = strtod(end, NULL);
1604127dd473Swhl739 			    if (w_q > 1 || w_q <= 0)
1605127dd473Swhl739 				errx(EX_DATAERR, "0 < w_q <= 1");
1606127dd473Swhl739 			    fs->w_q = (int) (w_q * (1 << SCALE_RED));
1607127dd473Swhl739 			}
1608127dd473Swhl739 			if ((end = strsep(&av[0], "/"))) {
1609127dd473Swhl739 			    fs->min_th = strtoul(end, &end, 0);
1610127dd473Swhl739 			    if (*end == 'K' || *end == 'k')
1611127dd473Swhl739 				fs->min_th *= 1024;
1612127dd473Swhl739 			}
1613127dd473Swhl739 			if ((end = strsep(&av[0], "/"))) {
1614127dd473Swhl739 			    fs->max_th = strtoul(end, &end, 0);
1615127dd473Swhl739 			    if (*end == 'K' || *end == 'k')
1616127dd473Swhl739 				fs->max_th *= 1024;
1617127dd473Swhl739 			}
1618127dd473Swhl739 			if ((end = strsep(&av[0], "/"))) {
1619127dd473Swhl739 			    double max_p = strtod(end, NULL);
1620127dd473Swhl739 			    if (max_p > 1 || max_p < 0)
1621127dd473Swhl739 				errx(EX_DATAERR, "0 <= max_p <= 1");
1622127dd473Swhl739 			    fs->max_p = (int)(max_p * (1 << SCALE_RED));
1623127dd473Swhl739 			}
1624127dd473Swhl739 			ac--; av++;
1625127dd473Swhl739 			break;
1626127dd473Swhl739 
1627127dd473Swhl739 		case TOK_ECN:
1628127dd473Swhl739 			fs->flags |= DN_IS_ECN;
1629127dd473Swhl739 			break;
1630127dd473Swhl739 
1631127dd473Swhl739 		case TOK_DROPTAIL:
1632127dd473Swhl739 			NEED(fs, "droptail is only for flowsets");
1633127dd473Swhl739 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1634127dd473Swhl739 			break;
1635127dd473Swhl739 
1636127dd473Swhl739 		case TOK_BW:
1637127dd473Swhl739 			NEED(p, "bw is only for links");
1638127dd473Swhl739 			NEED1("bw needs bandwidth or interface\n");
1639127dd473Swhl739 			read_bandwidth(av[0], &p->bandwidth, NULL, 0);
1640127dd473Swhl739 			ac--; av++;
1641127dd473Swhl739 			break;
1642127dd473Swhl739 
1643127dd473Swhl739 		case TOK_DELAY:
1644127dd473Swhl739 			NEED(p, "delay is only for links");
1645127dd473Swhl739 			NEED1("delay needs argument 0..10000ms\n");
1646127dd473Swhl739 			p->delay = strtoul(av[0], NULL, 0);
1647127dd473Swhl739 			ac--; av++;
1648127dd473Swhl739 			break;
1649127dd473Swhl739 
1650127dd473Swhl739 		case TOK_TYPE: {
1651127dd473Swhl739 			int l;
1652127dd473Swhl739 			NEED(sch, "type is only for schedulers");
1653127dd473Swhl739 			NEED1("type needs a string");
1654127dd473Swhl739 			l = strlen(av[0]);
1655127dd473Swhl739 			if (l == 0 || l > 15)
1656127dd473Swhl739 				errx(1, "type %s too long\n", av[0]);
165722ce4affSfengbojiang 			strlcpy(sch->name, av[0], sizeof(sch->name));
1658127dd473Swhl739 			sch->oid.subtype = 0; /* use string */
1659127dd473Swhl739 #ifdef NEW_AQM
1660127dd473Swhl739 			/* if fq_codel is selected, consider all tokens after it
1661127dd473Swhl739 			 * as parameters
1662127dd473Swhl739 			 */
1663127dd473Swhl739 			if (!strcasecmp(av[0],"fq_codel") || !strcasecmp(av[0],"fq_pie")){
166422ce4affSfengbojiang 				strlcpy(sch_extra->name, av[0],
166522ce4affSfengbojiang 				    sizeof(sch_extra->name));
1666127dd473Swhl739 				sch_extra->oid.subtype = DN_SCH_PARAMS;
1667127dd473Swhl739 				process_extra_parms(&ac, av, sch_extra, tok);
1668127dd473Swhl739 			} else {
1669127dd473Swhl739 				ac--;av++;
1670127dd473Swhl739 			}
1671127dd473Swhl739 #else
1672127dd473Swhl739 			ac--;av++;
1673127dd473Swhl739 #endif
1674127dd473Swhl739 			break;
1675127dd473Swhl739 		    }
1676127dd473Swhl739 
1677127dd473Swhl739 		case TOK_WEIGHT:
1678127dd473Swhl739 			NEED(fs, "weight is only for flowsets");
1679127dd473Swhl739 			NEED1("weight needs argument\n");
1680127dd473Swhl739 			fs->par[0] = strtol(av[0], &end, 0);
1681127dd473Swhl739 			ac--; av++;
1682127dd473Swhl739 			break;
1683127dd473Swhl739 
1684127dd473Swhl739 		case TOK_LMAX:
1685127dd473Swhl739 			NEED(fs, "lmax is only for flowsets");
1686127dd473Swhl739 			NEED1("lmax needs argument\n");
1687127dd473Swhl739 			fs->par[1] = strtol(av[0], &end, 0);
1688127dd473Swhl739 			ac--; av++;
1689127dd473Swhl739 			break;
1690127dd473Swhl739 
1691127dd473Swhl739 		case TOK_PRI:
1692127dd473Swhl739 			NEED(fs, "priority is only for flowsets");
1693127dd473Swhl739 			NEED1("priority needs argument\n");
1694127dd473Swhl739 			fs->par[2] = strtol(av[0], &end, 0);
1695127dd473Swhl739 			ac--; av++;
1696127dd473Swhl739 			break;
1697127dd473Swhl739 
1698127dd473Swhl739 		case TOK_SCHED:
1699127dd473Swhl739 		case TOK_PIPE:
1700127dd473Swhl739 			NEED(fs, "pipe/sched");
1701127dd473Swhl739 			NEED1("pipe/link/sched needs number\n");
1702127dd473Swhl739 			fs->sched_nr = strtoul(av[0], &end, 0);
1703127dd473Swhl739 			ac--; av++;
1704127dd473Swhl739 			break;
1705127dd473Swhl739 
1706127dd473Swhl739 		case TOK_PROFILE:
1707127dd473Swhl739 			NEED((!pf), "profile already set");
1708127dd473Swhl739 			NEED(p, "profile");
1709127dd473Swhl739 		    {
1710127dd473Swhl739 			NEED1("extra delay needs the file name\n");
1711127dd473Swhl739 			pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1712127dd473Swhl739 			load_extra_delays(av[0], pf, p); //XXX can't fail?
1713127dd473Swhl739 			--ac; ++av;
1714127dd473Swhl739 		    }
1715127dd473Swhl739 			break;
1716127dd473Swhl739 
1717127dd473Swhl739 		case TOK_BURST:
1718127dd473Swhl739 			NEED(p, "burst");
1719127dd473Swhl739 			NEED1("burst needs argument\n");
1720127dd473Swhl739 			errno = 0;
1721127dd473Swhl739 			if (expand_number(av[0], &p->burst) < 0)
1722127dd473Swhl739 				if (errno != ERANGE)
1723127dd473Swhl739 					errx(EX_DATAERR,
1724127dd473Swhl739 					    "burst: invalid argument");
1725127dd473Swhl739 			if (errno || p->burst > (1ULL << 48) - 1)
1726127dd473Swhl739 				errx(EX_DATAERR,
1727127dd473Swhl739 				    "burst: out of range (0..2^48-1)");
1728127dd473Swhl739 			ac--; av++;
1729127dd473Swhl739 			break;
1730127dd473Swhl739 
1731127dd473Swhl739 		default:
1732127dd473Swhl739 			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1733127dd473Swhl739 		}
1734127dd473Swhl739 	}
1735127dd473Swhl739 
1736127dd473Swhl739 	/* check validity of parameters */
1737127dd473Swhl739 	if (p) {
1738127dd473Swhl739 		if (p->delay > 10000)
1739127dd473Swhl739 			errx(EX_DATAERR, "delay must be < 10000");
1740127dd473Swhl739 		if (p->bandwidth == -1)
1741127dd473Swhl739 			p->bandwidth = 0;
1742127dd473Swhl739 	}
1743127dd473Swhl739 	if (fs) {
1744127dd473Swhl739 		/* XXX accept a 0 scheduler to keep the default */
1745127dd473Swhl739 	    if (fs->flags & DN_QSIZE_BYTES) {
1746127dd473Swhl739 		size_t len;
1747127dd473Swhl739 		long limit;
1748127dd473Swhl739 
1749127dd473Swhl739 		len = sizeof(limit);
1750127dd473Swhl739 		if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1751127dd473Swhl739 			&limit, &len, NULL, 0) == -1)
1752127dd473Swhl739 			limit = 1024*1024;
1753127dd473Swhl739 		if (fs->qsize > limit)
1754127dd473Swhl739 			errx(EX_DATAERR, "queue size must be < %ldB", limit);
1755127dd473Swhl739 	    } else {
1756127dd473Swhl739 		size_t len;
1757127dd473Swhl739 		long limit;
1758127dd473Swhl739 
1759127dd473Swhl739 		len = sizeof(limit);
1760127dd473Swhl739 		if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1761127dd473Swhl739 			&limit, &len, NULL, 0) == -1)
1762127dd473Swhl739 			limit = 100;
1763127dd473Swhl739 		if (fs->qsize > limit)
1764127dd473Swhl739 			errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1765127dd473Swhl739 	    }
1766127dd473Swhl739 
1767127dd473Swhl739 #ifdef NEW_AQM
1768127dd473Swhl739 		if ((fs->flags & DN_IS_ECN) && !((fs->flags & DN_IS_RED)||
1769127dd473Swhl739 			(fs->flags & DN_IS_AQM)))
1770127dd473Swhl739 			errx(EX_USAGE, "ECN can be used with red/gred/"
1771127dd473Swhl739 				"codel/fq_codel only!");
1772127dd473Swhl739 #else
1773127dd473Swhl739 	    if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1774127dd473Swhl739 		errx(EX_USAGE, "enable red/gred for ECN");
1775127dd473Swhl739 
1776127dd473Swhl739 #endif
1777127dd473Swhl739 
1778127dd473Swhl739 	    if (fs->flags & DN_IS_RED) {
1779127dd473Swhl739 		size_t len;
1780127dd473Swhl739 		int lookup_depth, avg_pkt_size;
1781127dd473Swhl739 
1782127dd473Swhl739 		if (!(fs->flags & DN_IS_ECN) && (fs->min_th >= fs->max_th))
1783127dd473Swhl739 		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1784127dd473Swhl739 			fs->min_th, fs->max_th);
1785127dd473Swhl739 		else if ((fs->flags & DN_IS_ECN) && (fs->min_th > fs->max_th))
1786127dd473Swhl739 		    errx(EX_DATAERR, "min_th %d must be =< than max_th %d",
1787127dd473Swhl739 			fs->min_th, fs->max_th);
1788127dd473Swhl739 
1789127dd473Swhl739 		if (fs->max_th == 0)
1790127dd473Swhl739 		    errx(EX_DATAERR, "max_th must be > 0");
1791127dd473Swhl739 
1792127dd473Swhl739 		len = sizeof(int);
1793127dd473Swhl739 		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1794127dd473Swhl739 			&lookup_depth, &len, NULL, 0) == -1)
1795127dd473Swhl739 			lookup_depth = 256;
1796127dd473Swhl739 		if (lookup_depth == 0)
1797127dd473Swhl739 		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1798127dd473Swhl739 			" must be greater than zero");
1799127dd473Swhl739 
1800127dd473Swhl739 		len = sizeof(int);
1801127dd473Swhl739 		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1802127dd473Swhl739 			&avg_pkt_size, &len, NULL, 0) == -1)
1803127dd473Swhl739 			avg_pkt_size = 512;
1804127dd473Swhl739 
1805127dd473Swhl739 		if (avg_pkt_size == 0)
1806127dd473Swhl739 			errx(EX_DATAERR,
1807127dd473Swhl739 			    "net.inet.ip.dummynet.red_avg_pkt_size must"
1808127dd473Swhl739 			    " be greater than zero");
1809127dd473Swhl739 
1810127dd473Swhl739 #if 0 /* the following computation is now done in the kernel */
1811127dd473Swhl739 		/*
1812127dd473Swhl739 		 * Ticks needed for sending a medium-sized packet.
1813127dd473Swhl739 		 * Unfortunately, when we are configuring a WF2Q+ queue, we
1814127dd473Swhl739 		 * do not have bandwidth information, because that is stored
1815127dd473Swhl739 		 * in the parent pipe, and also we have multiple queues
1816127dd473Swhl739 		 * competing for it. So we set s=0, which is not very
1817127dd473Swhl739 		 * correct. But on the other hand, why do we want RED with
1818127dd473Swhl739 		 * WF2Q+ ?
1819127dd473Swhl739 		 */
1820127dd473Swhl739 		if (p.bandwidth==0) /* this is a WF2Q+ queue */
1821127dd473Swhl739 			s = 0;
1822127dd473Swhl739 		else
1823127dd473Swhl739 			s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1824127dd473Swhl739 		/*
1825127dd473Swhl739 		 * max idle time (in ticks) before avg queue size becomes 0.
1826127dd473Swhl739 		 * NOTA:  (3/w_q) is approx the value x so that
1827127dd473Swhl739 		 * (1-w_q)^x < 10^-3.
1828127dd473Swhl739 		 */
1829127dd473Swhl739 		w_q = ((double)fs->w_q) / (1 << SCALE_RED);
1830127dd473Swhl739 		idle = s * 3. / w_q;
1831127dd473Swhl739 		fs->lookup_step = (int)idle / lookup_depth;
1832127dd473Swhl739 		if (!fs->lookup_step)
1833127dd473Swhl739 			fs->lookup_step = 1;
1834127dd473Swhl739 		weight = 1 - w_q;
1835127dd473Swhl739 		for (t = fs->lookup_step; t > 1; --t)
1836127dd473Swhl739 			weight *= 1 - w_q;
1837127dd473Swhl739 		fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1838127dd473Swhl739 #endif /* code moved in the kernel */
1839127dd473Swhl739 	    }
1840127dd473Swhl739 	}
1841127dd473Swhl739 
1842127dd473Swhl739 	i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1843127dd473Swhl739 
1844127dd473Swhl739 	if (i)
1845127dd473Swhl739 		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1846127dd473Swhl739 }
1847127dd473Swhl739 
1848127dd473Swhl739 void
dummynet_flush(void)1849127dd473Swhl739 dummynet_flush(void)
1850127dd473Swhl739 {
1851127dd473Swhl739 	struct dn_id oid;
1852127dd473Swhl739 	oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1853127dd473Swhl739 	do_cmd(IP_DUMMYNET3, &oid, oid.len);
1854127dd473Swhl739 }
1855127dd473Swhl739 
1856127dd473Swhl739 /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
1857127dd473Swhl739  * Returns the number of ranges, and possibly stores them
1858127dd473Swhl739  * in the array v of size len.
1859127dd473Swhl739  */
1860127dd473Swhl739 static int
parse_range(int ac,char * av[],uint32_t * v,int len)1861127dd473Swhl739 parse_range(int ac, char *av[], uint32_t *v, int len)
1862127dd473Swhl739 {
1863127dd473Swhl739 	int n = 0;
1864127dd473Swhl739 	char *endptr, *s;
1865127dd473Swhl739 	uint32_t base[2];
1866127dd473Swhl739 
1867127dd473Swhl739 	if (v == NULL || len < 2) {
1868127dd473Swhl739 		v = base;
1869127dd473Swhl739 		len = 2;
1870127dd473Swhl739 	}
1871127dd473Swhl739 
1872127dd473Swhl739 	for (s = *av; s != NULL; av++, ac--) {
1873127dd473Swhl739 		v[0] = strtoul(s, &endptr, 10);
1874127dd473Swhl739 		v[1] = (*endptr != '-') ? v[0] :
1875127dd473Swhl739 			 strtoul(endptr+1, &endptr, 10);
1876127dd473Swhl739 		if (*endptr == '\0') { /* prepare for next round */
1877127dd473Swhl739 			s = (ac > 0) ? *(av+1) : NULL;
1878127dd473Swhl739 		} else {
1879127dd473Swhl739 			if (*endptr != ',') {
1880127dd473Swhl739 				warn("invalid number: %s", s);
1881127dd473Swhl739 				s = ++endptr;
1882127dd473Swhl739 				continue;
1883127dd473Swhl739 			}
1884127dd473Swhl739 			/* continue processing from here */
1885127dd473Swhl739 			s = ++endptr;
1886127dd473Swhl739 			ac++;
1887127dd473Swhl739 			av--;
1888127dd473Swhl739 		}
1889127dd473Swhl739 		if (v[1] < v[0] ||
189022ce4affSfengbojiang 			v[0] >= DN_MAX_ID-1 ||
1891127dd473Swhl739 			v[1] >= DN_MAX_ID-1) {
1892127dd473Swhl739 			continue; /* invalid entry */
1893127dd473Swhl739 		}
1894127dd473Swhl739 		n++;
1895127dd473Swhl739 		/* translate if 'pipe list' */
189622ce4affSfengbojiang 		if (g_co.do_pipe == 1) {
1897127dd473Swhl739 			v[0] += DN_MAX_ID;
1898127dd473Swhl739 			v[1] += DN_MAX_ID;
1899127dd473Swhl739 		}
1900127dd473Swhl739 		v = (n*2 < len) ? v + 2 : base;
1901127dd473Swhl739 	}
1902127dd473Swhl739 	return n;
1903127dd473Swhl739 }
1904127dd473Swhl739 
1905127dd473Swhl739 /* main entry point for dummynet list functions. co.do_pipe indicates
1906127dd473Swhl739  * which function we want to support.
1907127dd473Swhl739  * av may contain filtering arguments, either individual entries
1908127dd473Swhl739  * or ranges, or lists (space or commas are valid separators).
1909127dd473Swhl739  * Format for a range can be n1-n2 or n3 n4 n5 ...
1910127dd473Swhl739  * In a range n1 must be <= n2, otherwise the range is ignored.
1911127dd473Swhl739  * A number 'n4' is translate in a range 'n4-n4'
1912127dd473Swhl739  * All number must be > 0 and < DN_MAX_ID-1
1913127dd473Swhl739  */
1914127dd473Swhl739 void
dummynet_list(int ac,char * av[],int show_counters)1915127dd473Swhl739 dummynet_list(int ac, char *av[], int show_counters)
1916127dd473Swhl739 {
1917127dd473Swhl739 	struct dn_id *oid, *x = NULL;
1918127dd473Swhl739 	int ret, i;
1919127dd473Swhl739 	int n; 		/* # of ranges */
1920127dd473Swhl739 	u_int buflen, l;
1921127dd473Swhl739 	u_int max_size;	/* largest obj passed up */
1922127dd473Swhl739 
1923127dd473Swhl739 	(void)show_counters;	// XXX unused, but we should use it.
1924127dd473Swhl739 	ac--;
1925127dd473Swhl739 	av++; 		/* skip 'list' | 'show' word */
1926127dd473Swhl739 
1927127dd473Swhl739 	n = parse_range(ac, av, NULL, 0);	/* Count # of ranges. */
1928127dd473Swhl739 
1929127dd473Swhl739 	/* Allocate space to store ranges */
1930127dd473Swhl739 	l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
1931127dd473Swhl739 	oid = safe_calloc(1, l);
1932127dd473Swhl739 	oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
1933127dd473Swhl739 
1934127dd473Swhl739 	if (n > 0)	/* store ranges in idx */
1935127dd473Swhl739 		parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1936127dd473Swhl739 	/*
1937127dd473Swhl739 	 * Compute the size of the largest object returned. If the
1938127dd473Swhl739 	 * response leaves at least this much spare space in the
1939127dd473Swhl739 	 * buffer, then surely the response is complete; otherwise
1940127dd473Swhl739 	 * there might be a risk of truncation and we will need to
1941127dd473Swhl739 	 * retry with a larger buffer.
1942127dd473Swhl739 	 * XXX don't bother with smaller structs.
1943127dd473Swhl739 	 */
1944127dd473Swhl739 	max_size = sizeof(struct dn_fs);
1945127dd473Swhl739 	if (max_size < sizeof(struct dn_sch))
1946127dd473Swhl739 		max_size = sizeof(struct dn_sch);
1947127dd473Swhl739 	if (max_size < sizeof(struct dn_flow))
1948127dd473Swhl739 		max_size = sizeof(struct dn_flow);
1949127dd473Swhl739 
195022ce4affSfengbojiang 	switch (g_co.do_pipe) {
1951127dd473Swhl739 	case 1:
1952127dd473Swhl739 		oid->subtype = DN_LINK;	/* list pipe */
1953127dd473Swhl739 		break;
1954127dd473Swhl739 	case 2:
1955127dd473Swhl739 		oid->subtype = DN_FS;	/* list queue */
1956127dd473Swhl739 		break;
1957127dd473Swhl739 	case 3:
1958127dd473Swhl739 		oid->subtype = DN_SCH;	/* list sched */
1959127dd473Swhl739 		break;
1960127dd473Swhl739 	}
1961127dd473Swhl739 
1962127dd473Swhl739 	/*
1963127dd473Swhl739 	 * Ask the kernel an estimate of the required space (result
1964127dd473Swhl739 	 * in oid.id), unless we are requesting a subset of objects,
1965127dd473Swhl739 	 * in which case the kernel does not give an exact answer.
1966127dd473Swhl739 	 * In any case, space might grow in the meantime due to the
1967127dd473Swhl739 	 * creation of new queues, so we must be prepared to retry.
1968127dd473Swhl739 	 */
1969127dd473Swhl739 	if (n > 0) {
1970127dd473Swhl739 		buflen = 4*1024;
1971127dd473Swhl739 	} else {
1972127dd473Swhl739 		ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1973127dd473Swhl739 		if (ret != 0 || oid->id <= sizeof(*oid))
1974127dd473Swhl739 			goto done;
1975127dd473Swhl739 		buflen = oid->id + max_size;
1976127dd473Swhl739 		oid->len = sizeof(*oid); /* restore */
1977127dd473Swhl739 	}
1978127dd473Swhl739 	/* Try a few times, until the buffer fits */
1979127dd473Swhl739 	for (i = 0; i < 20; i++) {
1980127dd473Swhl739 		l = buflen;
1981127dd473Swhl739 		x = safe_realloc(x, l);
1982127dd473Swhl739 		bcopy(oid, x, oid->len);
1983127dd473Swhl739 		ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
1984127dd473Swhl739 		if (ret != 0 || x->id <= sizeof(*oid))
1985127dd473Swhl739 			goto done; /* no response */
1986127dd473Swhl739 		if (l + max_size <= buflen)
1987127dd473Swhl739 			break; /* ok */
1988127dd473Swhl739 		buflen *= 2;	 /* double for next attempt */
1989127dd473Swhl739 	}
1990127dd473Swhl739 	list_pipes(x, O_NEXT(x, l));
1991127dd473Swhl739 done:
1992127dd473Swhl739 	if (x)
1993127dd473Swhl739 		free(x);
1994127dd473Swhl739 	free(oid);
1995127dd473Swhl739 }
1996