1da14cebeSEric Cheng /*
2da14cebeSEric Cheng  * CDDL HEADER START
3da14cebeSEric Cheng  *
4da14cebeSEric Cheng  * The contents of this file are subject to the terms of the
5da14cebeSEric Cheng  * Common Development and Distribution License (the "License").
6da14cebeSEric Cheng  * You may not use this file except in compliance with the License.
7da14cebeSEric Cheng  *
8da14cebeSEric Cheng  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da14cebeSEric Cheng  * or http://www.opensolaris.org/os/licensing.
10da14cebeSEric Cheng  * See the License for the specific language governing permissions
11da14cebeSEric Cheng  * and limitations under the License.
12da14cebeSEric Cheng  *
13da14cebeSEric Cheng  * When distributing Covered Code, include this CDDL HEADER in each
14da14cebeSEric Cheng  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da14cebeSEric Cheng  * If applicable, add the following below this CDDL HEADER, with the
16da14cebeSEric Cheng  * fields enclosed by brackets "[]" replaced with your own identifying
17da14cebeSEric Cheng  * information: Portions Copyright [yyyy] [name of copyright owner]
18da14cebeSEric Cheng  *
19da14cebeSEric Cheng  * CDDL HEADER END
20da14cebeSEric Cheng  */
21da14cebeSEric Cheng /*
22da000602SGirish Moodalbail  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23da14cebeSEric Cheng  * Use is subject to license terms.
24da14cebeSEric Cheng  */
25da14cebeSEric Cheng 
26da14cebeSEric Cheng #include <stdio.h>
27da14cebeSEric Cheng #include <sys/types.h>
28da14cebeSEric Cheng #include <sys/socket.h>
29da14cebeSEric Cheng #include <sys/ethernet.h>
30da14cebeSEric Cheng #include <netinet/in.h>
31da14cebeSEric Cheng #include <arpa/inet.h>
32da14cebeSEric Cheng #include <sys/stat.h>
33*82a2fc47SJames Carlson #include <sys/dld_ioc.h>
34da14cebeSEric Cheng #include <string.h>
35da14cebeSEric Cheng #include <fcntl.h>
36da14cebeSEric Cheng #include <unistd.h>
37da14cebeSEric Cheng #include <stropts.h>
38da14cebeSEric Cheng #include <stdlib.h>
39da14cebeSEric Cheng #include <errno.h>
40da14cebeSEric Cheng #include <strings.h>
41da14cebeSEric Cheng #include <libintl.h>
42da14cebeSEric Cheng #include <netdb.h>
43da14cebeSEric Cheng #include <net/if_types.h>
44da14cebeSEric Cheng #include <net/if_dl.h>
45da14cebeSEric Cheng #include <inet/ip.h>
46da14cebeSEric Cheng #include <inet/ip6.h>
47da14cebeSEric Cheng #include <libdlflow.h>
48da14cebeSEric Cheng #include <libdlflow_impl.h>
49da14cebeSEric Cheng #include <libdladm_impl.h>
50da14cebeSEric Cheng 
51da14cebeSEric Cheng /* minimum buffer size for DLDIOCWALKFLOW */
52da14cebeSEric Cheng #define	MIN_INFO_SIZE	(4 * 1024)
53da14cebeSEric Cheng 
54da14cebeSEric Cheng #define	DLADM_FLOW_DB		"/etc/dladm/flowadm.conf"
55da14cebeSEric Cheng #define	DLADM_FLOW_DB_TMP	"/etc/dladm/flowadm.conf.new"
56da14cebeSEric Cheng #define	DLADM_FLOW_DB_LOCK	"/tmp/flowadm.conf.lock"
57da14cebeSEric Cheng 
58da14cebeSEric Cheng #define	DLADM_FLOW_DB_PERMS	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
59da14cebeSEric Cheng #define	DLADM_FLOW_DB_OWNER	UID_DLADM
60da14cebeSEric Cheng #define	DLADM_FLOW_DB_GROUP	GID_SYS
61da14cebeSEric Cheng 
62da14cebeSEric Cheng #define	BLANK_LINE(s)	((s[0] == '\0') || (s[0] == '#') || (s[0] == '\n'))
63da14cebeSEric Cheng #define	MAXLINELEN	1024
64da14cebeSEric Cheng #define	MAXPATHLEN	1024
65da14cebeSEric Cheng 
66da14cebeSEric Cheng #define	V4_PART_OF_V6(v6)	((v6)._S6_un._S6_u32[3])
67da14cebeSEric Cheng 
68da14cebeSEric Cheng /* database file parameters */
69da14cebeSEric Cheng static const char *BW_LIMIT = "bw_limit";
70da14cebeSEric Cheng static const char *PRIORITY = "priority";
71da14cebeSEric Cheng static const char *LOCAL_IP_ADDR = "local_ip";
72da14cebeSEric Cheng static const char *REMOTE_IP_ADDR = "remote_ip";
73da14cebeSEric Cheng static const char *TRANSPORT = "transport";
74da14cebeSEric Cheng static const char *LOCAL_PORT = "local_port";
75da14cebeSEric Cheng static const char *DSFIELD = "dsfield";
76da14cebeSEric Cheng 
77da14cebeSEric Cheng /*
78da14cebeSEric Cheng  * Open and lock the flowadm configuration file lock. The lock is
79da14cebeSEric Cheng  * acquired as a reader (F_RDLCK) or writer (F_WRLCK).
80da14cebeSEric Cheng  */
81da14cebeSEric Cheng static int
82da14cebeSEric Cheng i_dladm_flow_lock_db(short type)
83da14cebeSEric Cheng {
84da14cebeSEric Cheng 	int lock_fd;
85da14cebeSEric Cheng 	struct flock lock;
86da14cebeSEric Cheng 
87da14cebeSEric Cheng 	if ((lock_fd = open(DLADM_FLOW_DB_LOCK, O_RDWR | O_CREAT | O_TRUNC,
88da14cebeSEric Cheng 	    DLADM_FLOW_DB_PERMS)) < 0)
89da14cebeSEric Cheng 		return (-1);
90da14cebeSEric Cheng 
91da14cebeSEric Cheng 	lock.l_type = type;
92da14cebeSEric Cheng 	lock.l_whence = SEEK_SET;
93da14cebeSEric Cheng 	lock.l_start = 0;
94da14cebeSEric Cheng 	lock.l_len = 0;
95da14cebeSEric Cheng 
96da14cebeSEric Cheng 	if (fcntl(lock_fd, F_SETLKW, &lock) < 0) {
97da14cebeSEric Cheng 		(void) close(lock_fd);
98da14cebeSEric Cheng 		(void) unlink(DLADM_FLOW_DB_LOCK);
99da14cebeSEric Cheng 		return (-1);
100da14cebeSEric Cheng 	}
101da14cebeSEric Cheng 	return (lock_fd);
102da14cebeSEric Cheng }
103da14cebeSEric Cheng 
104da14cebeSEric Cheng /*
105da14cebeSEric Cheng  * Unlock and close the specified file.
106da14cebeSEric Cheng  */
107da14cebeSEric Cheng static void
108da14cebeSEric Cheng i_dladm_flow_unlock_db(int fd)
109da14cebeSEric Cheng {
110da14cebeSEric Cheng 	struct flock lock;
111da14cebeSEric Cheng 
112da14cebeSEric Cheng 	if (fd < 0)
113da14cebeSEric Cheng 		return;
114da14cebeSEric Cheng 
115da14cebeSEric Cheng 	lock.l_type = F_UNLCK;
116da14cebeSEric Cheng 	lock.l_whence = SEEK_SET;
117da14cebeSEric Cheng 	lock.l_start = 0;
118da14cebeSEric Cheng 	lock.l_len = 0;
119da14cebeSEric Cheng 
120da14cebeSEric Cheng 	(void) fcntl(fd, F_SETLKW, &lock);
121da14cebeSEric Cheng 	(void) close(fd);
122da14cebeSEric Cheng 	(void) unlink(DLADM_FLOW_DB_LOCK);
123da14cebeSEric Cheng }
124da14cebeSEric Cheng 
125da14cebeSEric Cheng /*
126da14cebeSEric Cheng  * Parse one line of the link flowadm DB
127da14cebeSEric Cheng  * Returns -1 on failure, 0 on success.
128da14cebeSEric Cheng  */
129da14cebeSEric Cheng dladm_status_t
130da14cebeSEric Cheng dladm_flow_parse_db(char *line, dld_flowinfo_t *attr)
131da14cebeSEric Cheng {
132da14cebeSEric Cheng 	char		*token;
133da14cebeSEric Cheng 	char		*value, *name = NULL;
134da14cebeSEric Cheng 	char		*endp = NULL;
135da14cebeSEric Cheng 	char		*lasts = NULL;
136da14cebeSEric Cheng 	dladm_status_t	status = DLADM_STATUS_FLOW_DB_PARSE_ERR;
137da14cebeSEric Cheng 
138da14cebeSEric Cheng 	bzero(attr, sizeof (*attr));
139da14cebeSEric Cheng 
140da14cebeSEric Cheng 	/* flow name */
141da14cebeSEric Cheng 	if ((token = strtok_r(line, " \t", &lasts)) == NULL)
142da14cebeSEric Cheng 		goto done;
143da14cebeSEric Cheng 
144da000602SGirish Moodalbail 	if (strlcpy(attr->fi_flowname, token, MAXFLOWNAMELEN) >= MAXFLOWNAMELEN)
145da14cebeSEric Cheng 		goto done;
146da14cebeSEric Cheng 
147da14cebeSEric Cheng 	/* resource control and flow descriptor parameters */
148da14cebeSEric Cheng 	while ((token = strtok_r(NULL, " \t", &lasts)) != NULL) {
149da14cebeSEric Cheng 		if ((name = strdup(token)) == NULL)
150da14cebeSEric Cheng 			goto done;
151da14cebeSEric Cheng 
152da14cebeSEric Cheng 		(void) strtok(name, "=");
153da14cebeSEric Cheng 		value = strtok(NULL, "=");
154da14cebeSEric Cheng 		if (value == NULL)
155da14cebeSEric Cheng 			goto done;
156da14cebeSEric Cheng 
157da14cebeSEric Cheng 		if (strcmp(name, "linkid") == 0) {
158da14cebeSEric Cheng 			if ((attr->fi_linkid =
159da14cebeSEric Cheng 			    (uint32_t)strtol(value, &endp, 10)) ==
160da14cebeSEric Cheng 			    DATALINK_INVALID_LINKID)
161da14cebeSEric Cheng 				goto done;
162da14cebeSEric Cheng 
163da14cebeSEric Cheng 		} else if (strcmp(name, BW_LIMIT) == 0) {
164da14cebeSEric Cheng 			attr->fi_resource_props.mrp_mask |=
165da14cebeSEric Cheng 			    MRP_MAXBW;
166da14cebeSEric Cheng 			attr->fi_resource_props.mrp_maxbw =
167da14cebeSEric Cheng 			    (uint64_t)strtol(value, &endp, 0);
168da14cebeSEric Cheng 
169da14cebeSEric Cheng 		} else if (strcmp(name, PRIORITY) == 0) {
170da14cebeSEric Cheng 			attr->fi_resource_props.mrp_mask |= MRP_PRIORITY;
171da14cebeSEric Cheng 			status = dladm_str2pri(value,
172da14cebeSEric Cheng 			    &attr->fi_resource_props.mrp_priority);
173da14cebeSEric Cheng 			if (status != DLADM_STATUS_OK)
174da14cebeSEric Cheng 				goto done;
175da14cebeSEric Cheng 
176da14cebeSEric Cheng 		} else if (strcmp(name, DSFIELD) == 0) {
177da14cebeSEric Cheng 			status = do_check_dsfield(value,
178da14cebeSEric Cheng 			    &attr->fi_flow_desc);
179da14cebeSEric Cheng 			if (status != DLADM_STATUS_OK)
180da14cebeSEric Cheng 				goto done;
181da14cebeSEric Cheng 
182da14cebeSEric Cheng 		} else if (strcmp(name, LOCAL_IP_ADDR) == 0) {
183da14cebeSEric Cheng 			status = do_check_ip_addr(value, B_TRUE,
184da14cebeSEric Cheng 			    &attr->fi_flow_desc);
185da14cebeSEric Cheng 			if (status != DLADM_STATUS_OK)
186da14cebeSEric Cheng 				goto done;
187da14cebeSEric Cheng 
188da14cebeSEric Cheng 		} else if (strcmp(name, REMOTE_IP_ADDR) == 0) {
189da14cebeSEric Cheng 			status = do_check_ip_addr(value, B_FALSE,
190da14cebeSEric Cheng 			    &attr->fi_flow_desc);
191da14cebeSEric Cheng 			if (status != DLADM_STATUS_OK)
192da14cebeSEric Cheng 				goto done;
193da14cebeSEric Cheng 
194da14cebeSEric Cheng 		} else if (strcmp(name, TRANSPORT) == 0) {
195da14cebeSEric Cheng 			attr->fi_flow_desc.fd_mask |= FLOW_IP_PROTOCOL;
196da14cebeSEric Cheng 			attr->fi_flow_desc.fd_protocol =
197da14cebeSEric Cheng 			    (uint8_t)strtol(value, &endp, 0);
198da14cebeSEric Cheng 
199da14cebeSEric Cheng 		} else if (strcmp(name, LOCAL_PORT) == 0) {
200da14cebeSEric Cheng 			attr->fi_flow_desc.fd_mask |= FLOW_ULP_PORT_LOCAL;
201da14cebeSEric Cheng 			attr->fi_flow_desc.fd_local_port =
202da14cebeSEric Cheng 			    (uint16_t)strtol(value, &endp, 10);
203da14cebeSEric Cheng 			attr->fi_flow_desc.fd_local_port =
204da14cebeSEric Cheng 			    htons(attr->fi_flow_desc.fd_local_port);
205da14cebeSEric Cheng 		}
206da14cebeSEric Cheng 		free(name);
207da14cebeSEric Cheng 		name = NULL;
208da14cebeSEric Cheng 	}
209da14cebeSEric Cheng 	if (attr->fi_linkid != DATALINK_INVALID_LINKID)
210da14cebeSEric Cheng 		status = DLADM_STATUS_OK;
211da14cebeSEric Cheng done:
212da14cebeSEric Cheng 	free(name);
213da14cebeSEric Cheng 	return (status);
214da14cebeSEric Cheng }
215da14cebeSEric Cheng 
216da14cebeSEric Cheng #define	FPRINTF_ERR(fcall) if ((fcall) < 0) return (-1);
217da14cebeSEric Cheng 
218da14cebeSEric Cheng /*
219da14cebeSEric Cheng  * Write the attribute of a group to the specified file. Returns 0 on
220da14cebeSEric Cheng  * success, -1 on failure.
221da14cebeSEric Cheng  */
222da14cebeSEric Cheng static int
223da14cebeSEric Cheng i_dladm_flow_fput_grp(FILE *fp, dld_flowinfo_t *attr)
224da14cebeSEric Cheng {
225da14cebeSEric Cheng 
226da14cebeSEric Cheng 	FPRINTF_ERR(fprintf(fp, "%s\tlinkid=%d\t",
227da14cebeSEric Cheng 	    attr->fi_flowname, attr->fi_linkid));
228da14cebeSEric Cheng 
229da14cebeSEric Cheng 	/* flow policy */
230da14cebeSEric Cheng 	if (attr->fi_resource_props.mrp_mask & MRP_MAXBW)
231da14cebeSEric Cheng 		FPRINTF_ERR(fprintf(fp, "%s=%" PRIu64 "\t", BW_LIMIT,
232da14cebeSEric Cheng 		    attr->fi_resource_props.mrp_maxbw));
233da14cebeSEric Cheng 
234da14cebeSEric Cheng 	if (attr->fi_resource_props.mrp_mask & MRP_PRIORITY)
235da14cebeSEric Cheng 		FPRINTF_ERR(fprintf(fp, "%s=%d\t", PRIORITY,
236da14cebeSEric Cheng 		    attr->fi_resource_props.mrp_priority));
237da14cebeSEric Cheng 
238da14cebeSEric Cheng 	/* flow descriptor */
239da14cebeSEric Cheng 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_DSFIELD)
240da14cebeSEric Cheng 		FPRINTF_ERR(fprintf(fp, "%s=%x:%x\t", DSFIELD,
241da14cebeSEric Cheng 		    attr->fi_flow_desc.fd_dsfield,
242da14cebeSEric Cheng 		    attr->fi_flow_desc.fd_dsfield_mask));
243da14cebeSEric Cheng 
244da14cebeSEric Cheng 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_LOCAL) {
245da14cebeSEric Cheng 		char abuf[INET6_ADDRSTRLEN], *ap;
246da14cebeSEric Cheng 		struct in_addr ipaddr;
247da14cebeSEric Cheng 		int prefix_len, prefix_max;
248da14cebeSEric Cheng 
249da14cebeSEric Cheng 		if (attr->fi_flow_desc.fd_ipversion != 6) {
250da14cebeSEric Cheng 			ipaddr.s_addr =
251da14cebeSEric Cheng 			    attr->fi_flow_desc.
252da14cebeSEric Cheng 			    fd_local_addr._S6_un._S6_u32[3];
253da14cebeSEric Cheng 
254da14cebeSEric Cheng 			ap = inet_ntoa(ipaddr);
255da14cebeSEric Cheng 			prefix_max = IP_ABITS;
256da14cebeSEric Cheng 		} else {
257da14cebeSEric Cheng 			(void) inet_ntop(AF_INET6,
258da14cebeSEric Cheng 			    &attr->fi_flow_desc.fd_local_addr,
259da14cebeSEric Cheng 			    abuf, INET6_ADDRSTRLEN);
260da14cebeSEric Cheng 
261da14cebeSEric Cheng 			ap = abuf;
262da14cebeSEric Cheng 			prefix_max = IPV6_ABITS;
263da14cebeSEric Cheng 		}
264da14cebeSEric Cheng 		(void) dladm_mask2prefixlen(
265da14cebeSEric Cheng 		    &attr->fi_flow_desc.fd_local_netmask, prefix_max,
266da14cebeSEric Cheng 		    &prefix_len);
267da14cebeSEric Cheng 
268da14cebeSEric Cheng 		FPRINTF_ERR(fprintf(fp, "%s=%s/%d\t", LOCAL_IP_ADDR,
269da14cebeSEric Cheng 		    ap, prefix_len));
270da14cebeSEric Cheng 	}
271da14cebeSEric Cheng 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_REMOTE) {
272da14cebeSEric Cheng 		char abuf[INET6_ADDRSTRLEN], *ap;
273da14cebeSEric Cheng 		struct in_addr ipaddr;
274da14cebeSEric Cheng 		int prefix_len, prefix_max;
275da14cebeSEric Cheng 
276da14cebeSEric Cheng 		if (attr->fi_flow_desc.fd_ipversion != 6) {
277da14cebeSEric Cheng 			ipaddr.s_addr =
278da14cebeSEric Cheng 			    attr->fi_flow_desc.
279da14cebeSEric Cheng 			    fd_remote_addr._S6_un._S6_u32[3];
280da14cebeSEric Cheng 
281da14cebeSEric Cheng 			ap = inet_ntoa(ipaddr);
282da14cebeSEric Cheng 			prefix_max = IP_ABITS;
283da14cebeSEric Cheng 		} else {
284da14cebeSEric Cheng 			(void) inet_ntop(AF_INET6,
285da14cebeSEric Cheng 			    &(attr->fi_flow_desc.fd_remote_addr),
286da14cebeSEric Cheng 			    abuf, INET6_ADDRSTRLEN);
287da14cebeSEric Cheng 
288da14cebeSEric Cheng 			ap = abuf;
289da14cebeSEric Cheng 			prefix_max = IPV6_ABITS;
290da14cebeSEric Cheng 		}
291da14cebeSEric Cheng 		(void) dladm_mask2prefixlen(
292da14cebeSEric Cheng 		    &attr->fi_flow_desc.fd_remote_netmask, prefix_max,
293da14cebeSEric Cheng 		    &prefix_len);
294da14cebeSEric Cheng 
295da14cebeSEric Cheng 		FPRINTF_ERR(fprintf(fp, "%s=%s/%d\t", REMOTE_IP_ADDR,
296da14cebeSEric Cheng 		    ap, prefix_len));
297da14cebeSEric Cheng 	}
298da14cebeSEric Cheng 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_PROTOCOL)
299da14cebeSEric Cheng 		FPRINTF_ERR(fprintf(fp, "%s=%d\t", TRANSPORT,
300da14cebeSEric Cheng 		    attr->fi_flow_desc.fd_protocol));
301da14cebeSEric Cheng 
302da14cebeSEric Cheng 	if (attr->fi_flow_desc.fd_mask & FLOW_ULP_PORT_LOCAL)
303da14cebeSEric Cheng 		FPRINTF_ERR(fprintf(fp, "%s=%d\t", LOCAL_PORT,
304da14cebeSEric Cheng 		    ntohs(attr->fi_flow_desc.fd_local_port)));
305da14cebeSEric Cheng 
306da14cebeSEric Cheng 	FPRINTF_ERR(fprintf(fp, "\n"));
307da14cebeSEric Cheng 
308da14cebeSEric Cheng 	return (0);
309da14cebeSEric Cheng 
310da14cebeSEric Cheng }
311da14cebeSEric Cheng 
312da14cebeSEric Cheng static dladm_status_t
313da14cebeSEric Cheng i_dladm_flow_walk_rw_db(int (*fn)(void *, dld_flowinfo_t *),
314da14cebeSEric Cheng     void *arg,
315da14cebeSEric Cheng     const char *root)
316da14cebeSEric Cheng {
317da14cebeSEric Cheng 	FILE *fp, *nfp;
318da14cebeSEric Cheng 	int nfd, fn_rc, lock_fd;
319da14cebeSEric Cheng 	char line[MAXLINELEN];
320da14cebeSEric Cheng 	dld_flowinfo_t attr;
321da14cebeSEric Cheng 	char *db_file, *tmp_db_file;
322da14cebeSEric Cheng 	char db_file_buf[MAXPATHLEN];
323da14cebeSEric Cheng 	char tmp_db_file_buf[MAXPATHLEN];
324da14cebeSEric Cheng 	dladm_status_t	status = DLADM_STATUS_FLOW_DB_ERR;
325da14cebeSEric Cheng 
326da14cebeSEric Cheng 	if (root == NULL) {
327da14cebeSEric Cheng 		db_file = DLADM_FLOW_DB;
328da14cebeSEric Cheng 		tmp_db_file = DLADM_FLOW_DB_TMP;
329da14cebeSEric Cheng 	} else {
330da14cebeSEric Cheng 		(void) snprintf(db_file_buf, MAXPATHLEN, "%s%s", root,
331da14cebeSEric Cheng 		    DLADM_FLOW_DB);
332da14cebeSEric Cheng 		(void) snprintf(tmp_db_file_buf, MAXPATHLEN, "%s%s", root,
333da14cebeSEric Cheng 		    DLADM_FLOW_DB_TMP);
334da14cebeSEric Cheng 		db_file = db_file_buf;
335da14cebeSEric Cheng 		tmp_db_file = tmp_db_file_buf;
336da14cebeSEric Cheng 	}
337da14cebeSEric Cheng 
338da14cebeSEric Cheng 	if ((lock_fd = i_dladm_flow_lock_db(F_WRLCK)) < 0)
339da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_DB_ERR);
340da14cebeSEric Cheng 
341da14cebeSEric Cheng 	if ((fp = fopen(db_file, "r")) == NULL) {
342da14cebeSEric Cheng 		i_dladm_flow_unlock_db(lock_fd);
343da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
344da14cebeSEric Cheng 	}
345da14cebeSEric Cheng 
346da14cebeSEric Cheng 	if ((nfd = open(tmp_db_file, O_WRONLY|O_CREAT|O_TRUNC,
347da14cebeSEric Cheng 	    DLADM_FLOW_DB_PERMS)) == -1) {
348da14cebeSEric Cheng 		(void) fclose(fp);
349da14cebeSEric Cheng 		i_dladm_flow_unlock_db(lock_fd);
350da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
351da14cebeSEric Cheng 	}
352da14cebeSEric Cheng 
353da14cebeSEric Cheng 	if ((nfp = fdopen(nfd, "w")) == NULL) {
354da14cebeSEric Cheng 		(void) close(nfd);
355da14cebeSEric Cheng 		(void) fclose(fp);
356da14cebeSEric Cheng 		(void) unlink(tmp_db_file);
357da14cebeSEric Cheng 		i_dladm_flow_unlock_db(lock_fd);
358da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
359da14cebeSEric Cheng 	}
360da14cebeSEric Cheng 
361da14cebeSEric Cheng 	while (fgets(line, MAXLINELEN, fp) != NULL) {
362da14cebeSEric Cheng 
363da14cebeSEric Cheng 		/* skip comments */
364da14cebeSEric Cheng 		if (BLANK_LINE(line)) {
365da14cebeSEric Cheng 			if (fputs(line, nfp) == EOF)
366da14cebeSEric Cheng 				goto failed;
367da14cebeSEric Cheng 			continue;
368da14cebeSEric Cheng 		}
369da14cebeSEric Cheng 		(void) strtok(line, " \n");
370da14cebeSEric Cheng 
371da14cebeSEric Cheng 		if ((status = dladm_flow_parse_db(line, &attr)) !=
372da14cebeSEric Cheng 		    DLADM_STATUS_OK)
373da14cebeSEric Cheng 			goto failed;
374da14cebeSEric Cheng 
375da14cebeSEric Cheng 		fn_rc = fn(arg, &attr);
376da14cebeSEric Cheng 
377da14cebeSEric Cheng 		switch (fn_rc) {
378da14cebeSEric Cheng 		case -1:
379da14cebeSEric Cheng 			/* failure, stop walking */
380da14cebeSEric Cheng 			goto failed;
381da14cebeSEric Cheng 		case 0:
382da14cebeSEric Cheng 			/*
383da14cebeSEric Cheng 			 * Success, write group attributes, which could
384da14cebeSEric Cheng 			 * have been modified by fn().
385da14cebeSEric Cheng 			 */
386da14cebeSEric Cheng 			if (i_dladm_flow_fput_grp(nfp, &attr) != 0)
387da14cebeSEric Cheng 				goto failed;
388da14cebeSEric Cheng 			break;
389da14cebeSEric Cheng 		case 1:
390da14cebeSEric Cheng 			/* skip current group */
391da14cebeSEric Cheng 			break;
392da14cebeSEric Cheng 		}
393da14cebeSEric Cheng 	}
394da14cebeSEric Cheng 	if (fchmod(nfd, DLADM_FLOW_DB_PERMS) == -1)
395da14cebeSEric Cheng 		goto failed;
396da14cebeSEric Cheng 
397da14cebeSEric Cheng 	if (fchown(nfd, DLADM_FLOW_DB_OWNER, DLADM_FLOW_DB_GROUP) == -1)
398da14cebeSEric Cheng 		goto failed;
399da14cebeSEric Cheng 
400da14cebeSEric Cheng 	if (fflush(nfp) == EOF)
401da14cebeSEric Cheng 		goto failed;
402da14cebeSEric Cheng 
403da14cebeSEric Cheng 	(void) fclose(fp);
404da14cebeSEric Cheng 	(void) fclose(nfp);
405da14cebeSEric Cheng 
406da14cebeSEric Cheng 	if (rename(tmp_db_file, db_file) == -1) {
407da14cebeSEric Cheng 		(void) unlink(tmp_db_file);
408da14cebeSEric Cheng 		i_dladm_flow_unlock_db(lock_fd);
409da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_DB_ERR);
410da14cebeSEric Cheng 	}
411da14cebeSEric Cheng 	i_dladm_flow_unlock_db(lock_fd);
412da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
413da14cebeSEric Cheng 
414da14cebeSEric Cheng failed:
415da14cebeSEric Cheng 	(void) fclose(fp);
416da14cebeSEric Cheng 	(void) fclose(nfp);
417da14cebeSEric Cheng 	(void) unlink(tmp_db_file);
418da14cebeSEric Cheng 	i_dladm_flow_unlock_db(lock_fd);
419da14cebeSEric Cheng 
420da14cebeSEric Cheng 	return (status);
421da14cebeSEric Cheng }
422da14cebeSEric Cheng 
423da14cebeSEric Cheng /*
424da14cebeSEric Cheng  * Remove existing flow from DB.
425da14cebeSEric Cheng  */
426da14cebeSEric Cheng 
427da14cebeSEric Cheng typedef struct remove_db_state {
428da14cebeSEric Cheng 	dld_flowinfo_t	rs_newattr;
429da14cebeSEric Cheng 	dld_flowinfo_t	rs_oldattr;
430da14cebeSEric Cheng 	boolean_t	rs_found;
431da14cebeSEric Cheng } remove_db_state_t;
432da14cebeSEric Cheng 
433da14cebeSEric Cheng static int
434da14cebeSEric Cheng i_dladm_flow_remove_db_fn(void *arg, dld_flowinfo_t *grp)
435da14cebeSEric Cheng {
436da14cebeSEric Cheng 	remove_db_state_t *state = (remove_db_state_t *)arg;
437da14cebeSEric Cheng 	dld_flowinfo_t *attr = &state->rs_newattr;
438da14cebeSEric Cheng 
439da14cebeSEric Cheng 	if ((strcmp(grp->fi_flowname, attr->fi_flowname)) != 0)
440da14cebeSEric Cheng 		return (0);
441da14cebeSEric Cheng 	else {
442da14cebeSEric Cheng 		bcopy(grp, &state->rs_oldattr,
443da14cebeSEric Cheng 		    sizeof (dld_flowinfo_t));
444da14cebeSEric Cheng 		state->rs_found = B_TRUE;
445da14cebeSEric Cheng 		return (1);
446da14cebeSEric Cheng 	}
447da14cebeSEric Cheng }
448da14cebeSEric Cheng 
449da14cebeSEric Cheng /* ARGSUSED */
450da14cebeSEric Cheng static int
451da14cebeSEric Cheng i_dladm_flow_remove_db(remove_db_state_t *state, const char *root)
452da14cebeSEric Cheng {
453da14cebeSEric Cheng 	if (i_dladm_flow_walk_rw_db(i_dladm_flow_remove_db_fn, state, root)
454da14cebeSEric Cheng 	    != 0)
455da14cebeSEric Cheng 		return (-1);
456da14cebeSEric Cheng 
457da14cebeSEric Cheng 	if (!state->rs_found) {
458da14cebeSEric Cheng 		errno = ENOENT;
459da14cebeSEric Cheng 		return (-1);
460da14cebeSEric Cheng 	}
461da14cebeSEric Cheng 
462da14cebeSEric Cheng 	return (0);
463da14cebeSEric Cheng }
464da14cebeSEric Cheng 
465da14cebeSEric Cheng /*
466da14cebeSEric Cheng  * Create a flow in the DB.
467da14cebeSEric Cheng  */
468da14cebeSEric Cheng 
469da14cebeSEric Cheng typedef struct modify_db_state {
470da14cebeSEric Cheng 	dld_flowinfo_t	ms_newattr;
471da14cebeSEric Cheng 	dld_flowinfo_t	ms_oldattr;
472da14cebeSEric Cheng 	boolean_t	ms_found;
473da14cebeSEric Cheng } modify_db_state_t;
474da14cebeSEric Cheng 
475da14cebeSEric Cheng static dladm_status_t
476da14cebeSEric Cheng i_dladm_flow_create_db(dld_flowinfo_t *attr, const char *root)
477da14cebeSEric Cheng {
478da14cebeSEric Cheng 	FILE 	*fp;
479da14cebeSEric Cheng 	char 	line[MAXLINELEN];
480da14cebeSEric Cheng 	char	*db_file;
481da14cebeSEric Cheng 	char	db_file_buf[MAXPATHLEN];
482da14cebeSEric Cheng 	int	lock_fd;
483da14cebeSEric Cheng 	dladm_status_t	status = DLADM_STATUS_OK;
484da14cebeSEric Cheng 
485da14cebeSEric Cheng 	if (root == NULL) {
486da14cebeSEric Cheng 		db_file = DLADM_FLOW_DB;
487da14cebeSEric Cheng 	} else {
488da14cebeSEric Cheng 		(void) snprintf(db_file_buf, MAXPATHLEN, "%s%s", root,
489da14cebeSEric Cheng 		    DLADM_FLOW_DB);
490da14cebeSEric Cheng 		db_file = db_file_buf;
491da14cebeSEric Cheng 	}
492da14cebeSEric Cheng 
493da14cebeSEric Cheng 	if ((lock_fd = i_dladm_flow_lock_db(F_WRLCK)) < 0)
494da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_DB_ERR);
495da14cebeSEric Cheng 
496da14cebeSEric Cheng 	if ((fp = fopen(db_file, "r+")) == NULL &&
497da14cebeSEric Cheng 	    (fp = fopen(db_file, "w")) == NULL) {
498da14cebeSEric Cheng 		i_dladm_flow_unlock_db(lock_fd);
499da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
500da14cebeSEric Cheng 	}
501da14cebeSEric Cheng 
502da14cebeSEric Cheng 	/* look for existing group with same flowname */
503da14cebeSEric Cheng 	while (fgets(line, MAXLINELEN, fp) != NULL) {
504da14cebeSEric Cheng 		char *holder, *lasts;
505da14cebeSEric Cheng 
506da14cebeSEric Cheng 		/* skip comments */
507da14cebeSEric Cheng 		if (BLANK_LINE(line))
508da14cebeSEric Cheng 			continue;
509da14cebeSEric Cheng 
510da14cebeSEric Cheng 		/* ignore corrupted lines */
511da14cebeSEric Cheng 		holder = strtok_r(line, " \t", &lasts);
512da14cebeSEric Cheng 		if (holder == NULL)
513da14cebeSEric Cheng 			continue;
514da14cebeSEric Cheng 
515da14cebeSEric Cheng 		/* flow id */
516da14cebeSEric Cheng 		if (strcmp(holder, attr->fi_flowname) == 0) {
517da14cebeSEric Cheng 			/* group with flow id already exists */
518da14cebeSEric Cheng 			status = DLADM_STATUS_PERSIST_FLOW_EXISTS;
519da14cebeSEric Cheng 			goto failed;
520da14cebeSEric Cheng 		}
521da14cebeSEric Cheng 	}
522da14cebeSEric Cheng 	/*
523da14cebeSEric Cheng 	 * If we get here, we've verified that no existing group with
524da14cebeSEric Cheng 	 * the same flow id already exists. Its now time to add the new
525da14cebeSEric Cheng 	 * group to the DB.
526da14cebeSEric Cheng 	 */
527da14cebeSEric Cheng 	if (i_dladm_flow_fput_grp(fp, attr) != 0)
528da14cebeSEric Cheng 		status = DLADM_STATUS_FLOW_DB_PARSE_ERR;
529da14cebeSEric Cheng 
530da14cebeSEric Cheng failed:
531da14cebeSEric Cheng 	(void) fclose(fp);
532da14cebeSEric Cheng 	i_dladm_flow_unlock_db(lock_fd);
533da14cebeSEric Cheng 	return (status);
534da14cebeSEric Cheng }
535da14cebeSEric Cheng 
536da14cebeSEric Cheng static dladm_status_t
5374ac67f02SAnurag S. Maskey i_dladm_flow_add(dladm_handle_t handle, char *flowname, datalink_id_t linkid,
5384ac67f02SAnurag S. Maskey     flow_desc_t *flowdesc, mac_resource_props_t *mrp)
539da14cebeSEric Cheng {
540da14cebeSEric Cheng 	dld_ioc_addflow_t	attr;
541da14cebeSEric Cheng 
542da14cebeSEric Cheng 	/* create flow */
543da14cebeSEric Cheng 	bzero(&attr, sizeof (attr));
544da14cebeSEric Cheng 	bcopy(flowdesc, &attr.af_flow_desc, sizeof (flow_desc_t));
545da14cebeSEric Cheng 	if (mrp != NULL) {
546da14cebeSEric Cheng 		bcopy(mrp, &attr.af_resource_props,
547da14cebeSEric Cheng 		    sizeof (mac_resource_props_t));
548da14cebeSEric Cheng 	}
549da14cebeSEric Cheng 
550da14cebeSEric Cheng 	(void) strlcpy(attr.af_name, flowname, sizeof (attr.af_name));
551da14cebeSEric Cheng 	attr.af_linkid = linkid;
552da14cebeSEric Cheng 
5534ac67f02SAnurag S. Maskey 	if (ioctl(dladm_dld_fd(handle), DLDIOC_ADDFLOW, &attr) < 0)
554da14cebeSEric Cheng 		return (dladm_errno2status(errno));
555da14cebeSEric Cheng 
556da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
557da14cebeSEric Cheng }
558da14cebeSEric Cheng 
559da14cebeSEric Cheng static dladm_status_t
5604ac67f02SAnurag S. Maskey i_dladm_flow_remove(dladm_handle_t handle, char *flowname)
561da14cebeSEric Cheng {
562da14cebeSEric Cheng 	dld_ioc_removeflow_t	attr;
563da14cebeSEric Cheng 	dladm_status_t		status = DLADM_STATUS_OK;
564da14cebeSEric Cheng 
565da14cebeSEric Cheng 	(void) strlcpy(attr.rf_name, flowname,
566da14cebeSEric Cheng 	    sizeof (attr.rf_name));
567da14cebeSEric Cheng 
5684ac67f02SAnurag S. Maskey 	if (ioctl(dladm_dld_fd(handle), DLDIOC_REMOVEFLOW, &attr) < 0)
569da14cebeSEric Cheng 		status = dladm_errno2status(errno);
570da14cebeSEric Cheng 
571da14cebeSEric Cheng 	return (status);
572da14cebeSEric Cheng }
573da14cebeSEric Cheng 
574da14cebeSEric Cheng 
575da14cebeSEric Cheng /* ARGSUSED */
576da14cebeSEric Cheng dladm_status_t
5774ac67f02SAnurag S. Maskey dladm_flow_add(dladm_handle_t handle, datalink_id_t linkid,
5784ac67f02SAnurag S. Maskey     dladm_arg_list_t *attrlist, dladm_arg_list_t *proplist, char *flowname,
5794ac67f02SAnurag S. Maskey     boolean_t tempop, const char *root)
580da14cebeSEric Cheng {
581da14cebeSEric Cheng 	dld_flowinfo_t		db_attr;
582da14cebeSEric Cheng 	flow_desc_t		flowdesc;
583da14cebeSEric Cheng 	mac_resource_props_t	mrp;
584da14cebeSEric Cheng 	dladm_status_t		status;
585da14cebeSEric Cheng 
586da14cebeSEric Cheng 	/* Extract flow attributes from attrlist */
587da14cebeSEric Cheng 	bzero(&flowdesc, sizeof (flow_desc_t));
588da14cebeSEric Cheng 	if (attrlist != NULL && (status = dladm_flow_attrlist_extract(attrlist,
589da14cebeSEric Cheng 	    &flowdesc)) != DLADM_STATUS_OK) {
590da14cebeSEric Cheng 		return (status);
591da14cebeSEric Cheng 	}
592da14cebeSEric Cheng 
593da14cebeSEric Cheng 	/* Extract resource_ctl and cpu_list from proplist */
594da14cebeSEric Cheng 	bzero(&mrp, sizeof (mac_resource_props_t));
595da14cebeSEric Cheng 	if (proplist != NULL && (status = dladm_flow_proplist_extract(proplist,
596da14cebeSEric Cheng 	    &mrp)) != DLADM_STATUS_OK) {
597da14cebeSEric Cheng 		return (status);
598da14cebeSEric Cheng 	}
599da14cebeSEric Cheng 
600da14cebeSEric Cheng 	/* Add flow in kernel */
6014ac67f02SAnurag S. Maskey 	status = i_dladm_flow_add(handle, flowname, linkid, &flowdesc, &mrp);
602da14cebeSEric Cheng 	if (status != DLADM_STATUS_OK)
603da14cebeSEric Cheng 		return (status);
604da14cebeSEric Cheng 
605da14cebeSEric Cheng 	/* Add flow to DB */
606da14cebeSEric Cheng 	if (!tempop) {
607da14cebeSEric Cheng 		bzero(&db_attr, sizeof (db_attr));
608da14cebeSEric Cheng 		bcopy(&flowdesc, &db_attr.fi_flow_desc, sizeof (flow_desc_t));
609da14cebeSEric Cheng 		(void) strlcpy(db_attr.fi_flowname, flowname,
610da14cebeSEric Cheng 		    sizeof (db_attr.fi_flowname));
611da14cebeSEric Cheng 		db_attr.fi_linkid = linkid;
612da14cebeSEric Cheng 
613da14cebeSEric Cheng 		if ((status = i_dladm_flow_create_db(&db_attr, root)) !=
614da14cebeSEric Cheng 		    DLADM_STATUS_OK) {
6154ac67f02SAnurag S. Maskey 			(void) i_dladm_flow_remove(handle, flowname);
616da14cebeSEric Cheng 			return (status);
617da14cebeSEric Cheng 		}
618da14cebeSEric Cheng 		/* set flow properties */
619da14cebeSEric Cheng 		if (proplist != NULL) {
6204ac67f02SAnurag S. Maskey 			status = i_dladm_set_flow_proplist_db(handle, flowname,
621da14cebeSEric Cheng 			    proplist);
622da14cebeSEric Cheng 			if (status != DLADM_STATUS_OK) {
6234ac67f02SAnurag S. Maskey 				(void) i_dladm_flow_remove(handle, flowname);
624da14cebeSEric Cheng 				return (status);
625da14cebeSEric Cheng 			}
626da14cebeSEric Cheng 		}
627da14cebeSEric Cheng 	}
628da14cebeSEric Cheng 	return (status);
629da14cebeSEric Cheng }
630da14cebeSEric Cheng 
631da14cebeSEric Cheng /*
632da14cebeSEric Cheng  * Remove a flow.
633da14cebeSEric Cheng  */
634da14cebeSEric Cheng /* ARGSUSED */
635da14cebeSEric Cheng dladm_status_t
6364ac67f02SAnurag S. Maskey dladm_flow_remove(dladm_handle_t handle, char *flowname, boolean_t tempop,
637da14cebeSEric Cheng     const char *root)
638da14cebeSEric Cheng {
639da14cebeSEric Cheng 	remove_db_state_t		state;
640da14cebeSEric Cheng 	dladm_status_t			status = DLADM_STATUS_OK;
641da14cebeSEric Cheng 	dladm_status_t			s = DLADM_STATUS_OK;
642da14cebeSEric Cheng 
643da14cebeSEric Cheng 	/* remove flow */
6444ac67f02SAnurag S. Maskey 	status = i_dladm_flow_remove(handle, flowname);
645da14cebeSEric Cheng 	if ((status != DLADM_STATUS_OK) &&
646da14cebeSEric Cheng 	    (tempop || status != DLADM_STATUS_NOTFOUND))
647da14cebeSEric Cheng 		goto done;
648da14cebeSEric Cheng 
649da14cebeSEric Cheng 	/* remove flow from DB */
650da14cebeSEric Cheng 	if (!tempop) {
651da14cebeSEric Cheng 		bzero(&state, sizeof (state));
652da14cebeSEric Cheng 		(void) strlcpy(state.rs_newattr.fi_flowname, flowname,
653da14cebeSEric Cheng 		    sizeof (state.rs_newattr.fi_flowname));
654da14cebeSEric Cheng 		state.rs_found = B_FALSE;
655da14cebeSEric Cheng 
656da14cebeSEric Cheng 		/* flow DB */
657da14cebeSEric Cheng 		if (i_dladm_flow_remove_db(&state, root) < 0) {
658da14cebeSEric Cheng 			s = dladm_errno2status(errno);
659da14cebeSEric Cheng 			goto done;
660da14cebeSEric Cheng 		}
661da14cebeSEric Cheng 
662da14cebeSEric Cheng 		/* flow prop DB */
6634ac67f02SAnurag S. Maskey 		s = dladm_set_flowprop(handle, flowname, NULL, NULL, 0,
664da14cebeSEric Cheng 		    DLADM_OPT_PERSIST, NULL);
665da14cebeSEric Cheng 	}
666da14cebeSEric Cheng 
667da14cebeSEric Cheng done:
668da14cebeSEric Cheng 	if (!tempop) {
669da14cebeSEric Cheng 		if (s == DLADM_STATUS_OK) {
670da14cebeSEric Cheng 			if (status == DLADM_STATUS_NOTFOUND)
671da14cebeSEric Cheng 				status = s;
672da14cebeSEric Cheng 		} else {
673da14cebeSEric Cheng 			if (s != DLADM_STATUS_NOTFOUND)
674da14cebeSEric Cheng 				status = s;
675da14cebeSEric Cheng 		}
676da14cebeSEric Cheng 	}
677da14cebeSEric Cheng 	return (status);
678da14cebeSEric Cheng }
679da14cebeSEric Cheng 
680da14cebeSEric Cheng /*
681da14cebeSEric Cheng  * Get an existing flow in the DB.
682da14cebeSEric Cheng  */
683da14cebeSEric Cheng 
684da14cebeSEric Cheng typedef struct get_db_state {
685da14cebeSEric Cheng 	int		(*gs_fn)(dladm_flow_attr_t *, void *);
686da14cebeSEric Cheng 	void		*gs_arg;
687da14cebeSEric Cheng 	datalink_id_t	gs_linkid;
688da14cebeSEric Cheng } get_db_state_t;
689da14cebeSEric Cheng 
690da14cebeSEric Cheng /*
691da14cebeSEric Cheng  * For each flow which matches the linkid, copy all flow information
692da14cebeSEric Cheng  * to a new dladm_flow_attr_t structure and call the provided
693da14cebeSEric Cheng  * function.  This is used to display perisistent flows from
694da14cebeSEric Cheng  * the database.
695da14cebeSEric Cheng  */
696da14cebeSEric Cheng 
697da14cebeSEric Cheng static int
698da14cebeSEric Cheng i_dladm_flow_get_db_fn(void *arg, dld_flowinfo_t *grp)
699da14cebeSEric Cheng {
700da14cebeSEric Cheng 	get_db_state_t		*state = (get_db_state_t *)arg;
701da14cebeSEric Cheng 	dladm_flow_attr_t	attr;
702da14cebeSEric Cheng 
703da14cebeSEric Cheng 	if (grp->fi_linkid == state->gs_linkid) {
704da14cebeSEric Cheng 		attr.fa_linkid = state->gs_linkid;
705da14cebeSEric Cheng 		bcopy(grp->fi_flowname, &attr.fa_flowname,
706da14cebeSEric Cheng 		    sizeof (attr.fa_flowname));
707da14cebeSEric Cheng 		bcopy(&grp->fi_flow_desc, &attr.fa_flow_desc,
708da14cebeSEric Cheng 		    sizeof (attr.fa_flow_desc));
709da14cebeSEric Cheng 		bcopy(&grp->fi_resource_props, &attr.fa_resource_props,
710da14cebeSEric Cheng 		    sizeof (attr.fa_resource_props));
711da14cebeSEric Cheng 		(void) state->gs_fn(&attr, state->gs_arg);
712da14cebeSEric Cheng 	}
713da14cebeSEric Cheng 	return (0);
714da14cebeSEric Cheng }
715da14cebeSEric Cheng 
716da14cebeSEric Cheng /*
717da14cebeSEric Cheng  * Walk through the flows defined on the system and for each flow
718da14cebeSEric Cheng  * invoke <fn>(<arg>, <flow>);
719da14cebeSEric Cheng  * Currently used for show-flow.
720da14cebeSEric Cheng  */
721da14cebeSEric Cheng /* ARGSUSED */
722da14cebeSEric Cheng dladm_status_t
7234ac67f02SAnurag S. Maskey dladm_walk_flow(int (*fn)(dladm_flow_attr_t *, void *), dladm_handle_t handle,
724da14cebeSEric Cheng     datalink_id_t linkid, void *arg, boolean_t persist)
725da14cebeSEric Cheng {
726da14cebeSEric Cheng 	dld_flowinfo_t		*flow;
7274ac67f02SAnurag S. Maskey 	int			i, bufsize;
728da14cebeSEric Cheng 	dld_ioc_walkflow_t	*ioc = NULL;
729da14cebeSEric Cheng 	dladm_flow_attr_t 	attr;
730da14cebeSEric Cheng 	dladm_status_t		status = DLADM_STATUS_OK;
731da14cebeSEric Cheng 
732da14cebeSEric Cheng 	if (fn == NULL)
733da14cebeSEric Cheng 		return (DLADM_STATUS_BADARG);
734da14cebeSEric Cheng 
735da14cebeSEric Cheng 	if (persist) {
736da14cebeSEric Cheng 		get_db_state_t state;
737da14cebeSEric Cheng 
738da14cebeSEric Cheng 		bzero(&state, sizeof (state));
739da14cebeSEric Cheng 
740da14cebeSEric Cheng 		state.gs_linkid = linkid;
741da14cebeSEric Cheng 		state.gs_fn = fn;
742da14cebeSEric Cheng 		state.gs_arg = arg;
743da14cebeSEric Cheng 		status = i_dladm_flow_walk_rw_db(i_dladm_flow_get_db_fn,
744da14cebeSEric Cheng 		    &state, NULL);
745da14cebeSEric Cheng 		if (status != DLADM_STATUS_OK)
746da14cebeSEric Cheng 			return (status);
747da14cebeSEric Cheng 	} else {
748da14cebeSEric Cheng 		bufsize = MIN_INFO_SIZE;
749da14cebeSEric Cheng 		if ((ioc = calloc(1, bufsize)) == NULL) {
750da14cebeSEric Cheng 			status = dladm_errno2status(errno);
751da14cebeSEric Cheng 			return (status);
752da14cebeSEric Cheng 		}
753da14cebeSEric Cheng 
754da14cebeSEric Cheng 		ioc->wf_linkid = linkid;
755da14cebeSEric Cheng 		ioc->wf_len = bufsize - sizeof (*ioc);
756da14cebeSEric Cheng 
7574ac67f02SAnurag S. Maskey 		while (ioctl(dladm_dld_fd(handle), DLDIOC_WALKFLOW, ioc) < 0) {
758da14cebeSEric Cheng 			if (errno == ENOSPC) {
759da14cebeSEric Cheng 				bufsize *= 2;
760da14cebeSEric Cheng 				ioc = realloc(ioc, bufsize);
761da14cebeSEric Cheng 				if (ioc != NULL) {
762da14cebeSEric Cheng 					ioc->wf_linkid = linkid;
763da14cebeSEric Cheng 					ioc->wf_len = bufsize - sizeof (*ioc);
764da14cebeSEric Cheng 					continue;
765da14cebeSEric Cheng 				}
766da14cebeSEric Cheng 			}
767da14cebeSEric Cheng 			goto bail;
768da14cebeSEric Cheng 		}
769da14cebeSEric Cheng 
770da14cebeSEric Cheng 		flow = (dld_flowinfo_t *)(void *)(ioc + 1);
771da14cebeSEric Cheng 		for (i = 0; i < ioc->wf_nflows; i++, flow++) {
772da14cebeSEric Cheng 			bzero(&attr, sizeof (attr));
773da14cebeSEric Cheng 
774da14cebeSEric Cheng 			attr.fa_linkid = flow->fi_linkid;
775da14cebeSEric Cheng 			bcopy(&flow->fi_flowname, &attr.fa_flowname,
776da14cebeSEric Cheng 			    sizeof (attr.fa_flowname));
777da14cebeSEric Cheng 			bcopy(&flow->fi_flow_desc, &attr.fa_flow_desc,
778da14cebeSEric Cheng 			    sizeof (attr.fa_flow_desc));
779da14cebeSEric Cheng 			bcopy(&flow->fi_resource_props, &attr.fa_resource_props,
780da14cebeSEric Cheng 			    sizeof (attr.fa_resource_props));
781da14cebeSEric Cheng 
782da14cebeSEric Cheng 			if (fn(&attr, arg) == DLADM_WALK_TERMINATE)
783da14cebeSEric Cheng 				break;
784da14cebeSEric Cheng 		}
785da14cebeSEric Cheng 	}
786da14cebeSEric Cheng 
787da14cebeSEric Cheng bail:
788da14cebeSEric Cheng 	free(ioc);
789da14cebeSEric Cheng 	return (status);
790da14cebeSEric Cheng }
791da14cebeSEric Cheng 
792da14cebeSEric Cheng dladm_status_t
7934ac67f02SAnurag S. Maskey dladm_flow_init(dladm_handle_t handle)
794da14cebeSEric Cheng {
795da14cebeSEric Cheng 	flow_desc_t		flowdesc;
796da14cebeSEric Cheng 	datalink_id_t		linkid;
797da14cebeSEric Cheng 	dladm_status_t		s, status = DLADM_STATUS_OK;
798da000602SGirish Moodalbail 	char			name[MAXFLOWNAMELEN];
799da14cebeSEric Cheng 	char			line[MAXLINELEN];
800da14cebeSEric Cheng 	dld_flowinfo_t		attr;
801da14cebeSEric Cheng 	FILE			*fp;
802da14cebeSEric Cheng 
803da14cebeSEric Cheng 	if ((fp = fopen(DLADM_FLOW_DB, "r")) == NULL)
804da14cebeSEric Cheng 		return (DLADM_STATUS_DB_NOTFOUND);
805da14cebeSEric Cheng 
806da14cebeSEric Cheng 	while (fgets(line, MAXLINELEN, fp) != NULL) {
807da14cebeSEric Cheng 		/* skip comments */
808da14cebeSEric Cheng 		if (BLANK_LINE(line))
809da14cebeSEric Cheng 			continue;
810da14cebeSEric Cheng 
811da14cebeSEric Cheng 		(void) strtok(line, " \n");
812da14cebeSEric Cheng 
813da14cebeSEric Cheng 		s = dladm_flow_parse_db(line, &attr);
814da14cebeSEric Cheng 		if (s != DLADM_STATUS_OK) {
815da14cebeSEric Cheng 			status = s;
816da14cebeSEric Cheng 			continue;
817da14cebeSEric Cheng 		}
818da14cebeSEric Cheng 		bzero(&flowdesc, sizeof (flowdesc));
819da14cebeSEric Cheng 		bcopy(&attr.fi_flow_desc, &flowdesc, sizeof (flow_desc_t));
820da14cebeSEric Cheng 		(void) strlcpy(name, attr.fi_flowname,
821da14cebeSEric Cheng 		    sizeof (attr.fi_flowname));
822da14cebeSEric Cheng 		linkid = attr.fi_linkid;
823da14cebeSEric Cheng 
8244ac67f02SAnurag S. Maskey 		s = i_dladm_flow_add(handle, name, linkid, &flowdesc, NULL);
825da14cebeSEric Cheng 		if (s != DLADM_STATUS_OK)
826da14cebeSEric Cheng 			status = s;
827da14cebeSEric Cheng 	}
8284ac67f02SAnurag S. Maskey 	s = i_dladm_init_flowprop_db(handle);
829da14cebeSEric Cheng 	if (s != DLADM_STATUS_OK)
830da14cebeSEric Cheng 		status = s;
831da14cebeSEric Cheng 
832da14cebeSEric Cheng 	(void) fclose(fp);
833da14cebeSEric Cheng 	return (status);
834da14cebeSEric Cheng }
835da14cebeSEric Cheng 
836da14cebeSEric Cheng dladm_status_t
837da14cebeSEric Cheng dladm_prefixlen2mask(int prefixlen, int maxlen, uchar_t *mask)
838da14cebeSEric Cheng {
839da14cebeSEric Cheng 	if (prefixlen < 0 || prefixlen > maxlen)
840da14cebeSEric Cheng 		return (DLADM_STATUS_BADARG);
841da14cebeSEric Cheng 
842da14cebeSEric Cheng 	while (prefixlen > 0) {
843da14cebeSEric Cheng 		if (prefixlen >= 8) {
844da14cebeSEric Cheng 			*mask++ = 0xFF;
845da14cebeSEric Cheng 			prefixlen -= 8;
846da14cebeSEric Cheng 			continue;
847da14cebeSEric Cheng 		}
848da14cebeSEric Cheng 		*mask |= 1 << (8 - prefixlen);
849da14cebeSEric Cheng 		prefixlen--;
850da14cebeSEric Cheng 	}
851da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
852da14cebeSEric Cheng }
853da14cebeSEric Cheng 
854da14cebeSEric Cheng dladm_status_t
855da14cebeSEric Cheng dladm_mask2prefixlen(in6_addr_t *mask, int plen, int *prefixlen)
856da14cebeSEric Cheng {
857da14cebeSEric Cheng 	int		bits;
858da14cebeSEric Cheng 	int		i, end;
859da14cebeSEric Cheng 
860da14cebeSEric Cheng 	switch (plen) {
861da14cebeSEric Cheng 	case IP_ABITS:
862da14cebeSEric Cheng 		end = 3;
863da14cebeSEric Cheng 		break;
864da14cebeSEric Cheng 	case IPV6_ABITS:
865da14cebeSEric Cheng 		end = 0;
866da14cebeSEric Cheng 		break;
867da14cebeSEric Cheng 	default:
868da14cebeSEric Cheng 		return (DLADM_STATUS_BADARG);
869da14cebeSEric Cheng 	}
870da14cebeSEric Cheng 
871da14cebeSEric Cheng 	for (i = 3; i >= end; i--) {
872da14cebeSEric Cheng 		if (mask->_S6_un._S6_u32[i] == 0) {
873da14cebeSEric Cheng 			plen -= 32;
874da14cebeSEric Cheng 			continue;
875da14cebeSEric Cheng 		}
876da14cebeSEric Cheng 		bits = ffs(ntohl(mask->_S6_un._S6_u32[i])) - 1;
877da14cebeSEric Cheng 		if (bits == 0)
878da14cebeSEric Cheng 			break;
879da14cebeSEric Cheng 		plen -= bits;
880da14cebeSEric Cheng 	}
881da14cebeSEric Cheng 	*prefixlen = plen;
882da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
883da14cebeSEric Cheng }
884