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