xref: /onnv-gate/usr/src/uts/common/io/mac/mac_flow.c (revision 11878:ac93462db6d7)
18275SEric Cheng /*
28275SEric Cheng  * CDDL HEADER START
38275SEric Cheng  *
48275SEric Cheng  * The contents of this file are subject to the terms of the
58275SEric Cheng  * Common Development and Distribution License (the "License").
68275SEric Cheng  * You may not use this file except in compliance with the License.
78275SEric Cheng  *
88275SEric Cheng  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98275SEric Cheng  * or http://www.opensolaris.org/os/licensing.
108275SEric Cheng  * See the License for the specific language governing permissions
118275SEric Cheng  * and limitations under the License.
128275SEric Cheng  *
138275SEric Cheng  * When distributing Covered Code, include this CDDL HEADER in each
148275SEric Cheng  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158275SEric Cheng  * If applicable, add the following below this CDDL HEADER, with the
168275SEric Cheng  * fields enclosed by brackets "[]" replaced with your own identifying
178275SEric Cheng  * information: Portions Copyright [yyyy] [name of copyright owner]
188275SEric Cheng  *
198275SEric Cheng  * CDDL HEADER END
208275SEric Cheng  */
218275SEric Cheng 
228275SEric Cheng /*
2311528SBaban.Kenkre@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
248275SEric Cheng  * Use is subject to license terms.
258275SEric Cheng  */
268275SEric Cheng 
278275SEric Cheng #include <sys/strsun.h>
288275SEric Cheng #include <sys/sdt.h>
298275SEric Cheng #include <sys/mac.h>
308275SEric Cheng #include <sys/mac_impl.h>
318275SEric Cheng #include <sys/mac_client_impl.h>
32*11878SVenu.Iyer@Sun.COM #include <sys/mac_stat.h>
338275SEric Cheng #include <sys/dls.h>
348275SEric Cheng #include <sys/dls_impl.h>
358275SEric Cheng #include <sys/mac_soft_ring.h>
368275SEric Cheng #include <sys/ethernet.h>
37*11878SVenu.Iyer@Sun.COM #include <sys/cpupart.h>
38*11878SVenu.Iyer@Sun.COM #include <sys/pool.h>
39*11878SVenu.Iyer@Sun.COM #include <sys/pool_pset.h>
408275SEric Cheng #include <sys/vlan.h>
418275SEric Cheng #include <inet/ip.h>
428275SEric Cheng #include <inet/ip6.h>
438275SEric Cheng #include <netinet/tcp.h>
448275SEric Cheng #include <netinet/udp.h>
458275SEric Cheng #include <netinet/sctp.h>
468275SEric Cheng 
47*11878SVenu.Iyer@Sun.COM typedef struct flow_stats_s {
48*11878SVenu.Iyer@Sun.COM 	uint64_t	fs_obytes;
49*11878SVenu.Iyer@Sun.COM 	uint64_t	fs_opackets;
50*11878SVenu.Iyer@Sun.COM 	uint64_t	fs_oerrors;
51*11878SVenu.Iyer@Sun.COM 	uint64_t	fs_ibytes;
52*11878SVenu.Iyer@Sun.COM 	uint64_t	fs_ipackets;
53*11878SVenu.Iyer@Sun.COM 	uint64_t	fs_ierrors;
54*11878SVenu.Iyer@Sun.COM } flow_stats_t;
55*11878SVenu.Iyer@Sun.COM 
56*11878SVenu.Iyer@Sun.COM 
578275SEric Cheng /* global flow table, will be a per exclusive-zone table later */
588275SEric Cheng static mod_hash_t	*flow_hash;
598275SEric Cheng static krwlock_t	flow_tab_lock;
608275SEric Cheng 
618275SEric Cheng static kmem_cache_t	*flow_cache;
628275SEric Cheng static kmem_cache_t	*flow_tab_cache;
638275SEric Cheng static flow_ops_t	flow_l2_ops;
648275SEric Cheng 
658275SEric Cheng typedef struct {
668275SEric Cheng 	const char	*fs_name;
678275SEric Cheng 	uint_t		fs_offset;
688275SEric Cheng } flow_stats_info_t;
698275SEric Cheng 
708275SEric Cheng #define	FS_OFF(f)	(offsetof(flow_stats_t, f))
718275SEric Cheng static flow_stats_info_t flow_stats_list[] = {
72*11878SVenu.Iyer@Sun.COM 	{"rbytes",	FS_OFF(fs_ibytes)},
738275SEric Cheng 	{"ipackets",	FS_OFF(fs_ipackets)},
748275SEric Cheng 	{"ierrors",	FS_OFF(fs_ierrors)},
758275SEric Cheng 	{"obytes",	FS_OFF(fs_obytes)},
768275SEric Cheng 	{"opackets",	FS_OFF(fs_opackets)},
778275SEric Cheng 	{"oerrors",	FS_OFF(fs_oerrors)}
788275SEric Cheng };
798275SEric Cheng #define	FS_SIZE		(sizeof (flow_stats_list) / sizeof (flow_stats_info_t))
808275SEric Cheng 
818275SEric Cheng /*
828275SEric Cheng  * Checks whether a flow mask is legal.
838275SEric Cheng  */
848275SEric Cheng static flow_tab_info_t	*mac_flow_tab_info_get(flow_mask_t);
858275SEric Cheng 
868275SEric Cheng static void
flow_stat_init(kstat_named_t * knp)878275SEric Cheng flow_stat_init(kstat_named_t *knp)
888275SEric Cheng {
898275SEric Cheng 	int	i;
908275SEric Cheng 
918275SEric Cheng 	for (i = 0; i < FS_SIZE; i++, knp++) {
928275SEric Cheng 		kstat_named_init(knp, flow_stats_list[i].fs_name,
938275SEric Cheng 		    KSTAT_DATA_UINT64);
948275SEric Cheng 	}
958275SEric Cheng }
968275SEric Cheng 
978275SEric Cheng static int
flow_stat_update(kstat_t * ksp,int rw)988275SEric Cheng flow_stat_update(kstat_t *ksp, int rw)
998275SEric Cheng {
100*11878SVenu.Iyer@Sun.COM 	flow_entry_t		*fep = ksp->ks_private;
101*11878SVenu.Iyer@Sun.COM 	kstat_named_t		*knp = ksp->ks_data;
102*11878SVenu.Iyer@Sun.COM 	uint64_t		*statp;
103*11878SVenu.Iyer@Sun.COM 	int			i;
104*11878SVenu.Iyer@Sun.COM 	mac_rx_stats_t		*mac_rx_stat;
105*11878SVenu.Iyer@Sun.COM 	mac_tx_stats_t		*mac_tx_stat;
106*11878SVenu.Iyer@Sun.COM 	flow_stats_t		flow_stats;
107*11878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*mac_srs;
1088275SEric Cheng 
1098275SEric Cheng 	if (rw != KSTAT_READ)
1108275SEric Cheng 		return (EACCES);
1118275SEric Cheng 
112*11878SVenu.Iyer@Sun.COM 	bzero(&flow_stats, sizeof (flow_stats_t));
113*11878SVenu.Iyer@Sun.COM 
114*11878SVenu.Iyer@Sun.COM 	for (i = 0; i < fep->fe_rx_srs_cnt; i++) {
115*11878SVenu.Iyer@Sun.COM 		mac_srs = (mac_soft_ring_set_t *)fep->fe_rx_srs[i];
116*11878SVenu.Iyer@Sun.COM 		if (mac_srs == NULL) 		/* Multicast flow */
117*11878SVenu.Iyer@Sun.COM 			break;
118*11878SVenu.Iyer@Sun.COM 		mac_rx_stat = &mac_srs->srs_rx.sr_stat;
119*11878SVenu.Iyer@Sun.COM 
120*11878SVenu.Iyer@Sun.COM 		flow_stats.fs_ibytes += mac_rx_stat->mrs_intrbytes +
121*11878SVenu.Iyer@Sun.COM 		    mac_rx_stat->mrs_pollbytes + mac_rx_stat->mrs_lclbytes;
122*11878SVenu.Iyer@Sun.COM 
123*11878SVenu.Iyer@Sun.COM 		flow_stats.fs_ipackets += mac_rx_stat->mrs_intrcnt +
124*11878SVenu.Iyer@Sun.COM 		    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
125*11878SVenu.Iyer@Sun.COM 
126*11878SVenu.Iyer@Sun.COM 		flow_stats.fs_ierrors += mac_rx_stat->mrs_ierrors;
127*11878SVenu.Iyer@Sun.COM 	}
128*11878SVenu.Iyer@Sun.COM 
129*11878SVenu.Iyer@Sun.COM 	mac_srs = (mac_soft_ring_set_t *)fep->fe_tx_srs;
130*11878SVenu.Iyer@Sun.COM 	if (mac_srs == NULL) 		/* Multicast flow */
131*11878SVenu.Iyer@Sun.COM 		goto done;
132*11878SVenu.Iyer@Sun.COM 	mac_tx_stat = &mac_srs->srs_tx.st_stat;
133*11878SVenu.Iyer@Sun.COM 
134*11878SVenu.Iyer@Sun.COM 	flow_stats.fs_obytes = mac_tx_stat->mts_obytes;
135*11878SVenu.Iyer@Sun.COM 	flow_stats.fs_opackets = mac_tx_stat->mts_opackets;
136*11878SVenu.Iyer@Sun.COM 	flow_stats.fs_oerrors = mac_tx_stat->mts_oerrors;
137*11878SVenu.Iyer@Sun.COM 
138*11878SVenu.Iyer@Sun.COM done:
1398275SEric Cheng 	for (i = 0; i < FS_SIZE; i++, knp++) {
1408275SEric Cheng 		statp = (uint64_t *)
141*11878SVenu.Iyer@Sun.COM 		    ((uchar_t *)&flow_stats + flow_stats_list[i].fs_offset);
1428275SEric Cheng 		knp->value.ui64 = *statp;
1438275SEric Cheng 	}
1448275SEric Cheng 	return (0);
1458275SEric Cheng }
1468275SEric Cheng 
1478275SEric Cheng static void
flow_stat_create(flow_entry_t * fep)1488275SEric Cheng flow_stat_create(flow_entry_t *fep)
1498275SEric Cheng {
1508275SEric Cheng 	kstat_t		*ksp;
1518275SEric Cheng 	kstat_named_t	*knp;
1528275SEric Cheng 	uint_t		nstats = FS_SIZE;
1538275SEric Cheng 
15410616SSebastien.Roy@Sun.COM 	/*
15510616SSebastien.Roy@Sun.COM 	 * Fow now, flow entries are only manipulated and visible from the
15610616SSebastien.Roy@Sun.COM 	 * global zone.
15710616SSebastien.Roy@Sun.COM 	 */
15810616SSebastien.Roy@Sun.COM 	ksp = kstat_create_zone("unix", 0, (char *)fep->fe_flow_name, "flow",
15910616SSebastien.Roy@Sun.COM 	    KSTAT_TYPE_NAMED, nstats, 0, GLOBAL_ZONEID);
1608275SEric Cheng 	if (ksp == NULL)
1618275SEric Cheng 		return;
1628275SEric Cheng 
1638275SEric Cheng 	ksp->ks_update = flow_stat_update;
1648275SEric Cheng 	ksp->ks_private = fep;
1658275SEric Cheng 	fep->fe_ksp = ksp;
1668275SEric Cheng 
1678275SEric Cheng 	knp = (kstat_named_t *)ksp->ks_data;
1688275SEric Cheng 	flow_stat_init(knp);
1698275SEric Cheng 	kstat_install(ksp);
1708275SEric Cheng }
1718275SEric Cheng 
1728275SEric Cheng void
flow_stat_destroy(flow_entry_t * fep)1738275SEric Cheng flow_stat_destroy(flow_entry_t *fep)
1748275SEric Cheng {
1758275SEric Cheng 	if (fep->fe_ksp != NULL) {
1768275SEric Cheng 		kstat_delete(fep->fe_ksp);
1778275SEric Cheng 		fep->fe_ksp = NULL;
1788275SEric Cheng 	}
1798275SEric Cheng }
1808275SEric Cheng 
1818275SEric Cheng /*
1828275SEric Cheng  * Initialize the flow table
1838275SEric Cheng  */
1848275SEric Cheng void
mac_flow_init()1858275SEric Cheng mac_flow_init()
1868275SEric Cheng {
1878275SEric Cheng 	flow_cache = kmem_cache_create("flow_entry_cache",
1888275SEric Cheng 	    sizeof (flow_entry_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
1898275SEric Cheng 	flow_tab_cache = kmem_cache_create("flow_tab_cache",
1908275SEric Cheng 	    sizeof (flow_tab_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
1918275SEric Cheng 	flow_hash = mod_hash_create_extended("flow_hash",
1928275SEric Cheng 	    100, mod_hash_null_keydtor, mod_hash_null_valdtor,
1938275SEric Cheng 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
1948275SEric Cheng 	rw_init(&flow_tab_lock, NULL, RW_DEFAULT, NULL);
1958275SEric Cheng }
1968275SEric Cheng 
1978275SEric Cheng /*
1988275SEric Cheng  * Cleanup and release the flow table
1998275SEric Cheng  */
2008275SEric Cheng void
mac_flow_fini()2018275SEric Cheng mac_flow_fini()
2028275SEric Cheng {
2038275SEric Cheng 	kmem_cache_destroy(flow_cache);
2048275SEric Cheng 	kmem_cache_destroy(flow_tab_cache);
2058275SEric Cheng 	mod_hash_destroy_hash(flow_hash);
2068275SEric Cheng 	rw_destroy(&flow_tab_lock);
2078275SEric Cheng }
2088275SEric Cheng 
2098275SEric Cheng /*
2108275SEric Cheng  * mac_create_flow(): create a flow_entry_t.
2118275SEric Cheng  */
2128275SEric Cheng int
mac_flow_create(flow_desc_t * fd,mac_resource_props_t * mrp,char * name,void * client_cookie,uint_t type,flow_entry_t ** flentp)2138275SEric Cheng mac_flow_create(flow_desc_t *fd, mac_resource_props_t *mrp, char *name,
2148275SEric Cheng     void *client_cookie, uint_t type, flow_entry_t **flentp)
2158275SEric Cheng {
216*11878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = *flentp;
217*11878SVenu.Iyer@Sun.COM 	int			err = 0;
2188275SEric Cheng 
2198275SEric Cheng 	if (mrp != NULL) {
220*11878SVenu.Iyer@Sun.COM 		err = mac_validate_props(NULL, mrp);
2218275SEric Cheng 		if (err != 0)
2228275SEric Cheng 			return (err);
2238275SEric Cheng 	}
2248275SEric Cheng 
2258275SEric Cheng 	if (flent == NULL) {
2268275SEric Cheng 		flent = kmem_cache_alloc(flow_cache, KM_SLEEP);
2278275SEric Cheng 		bzero(flent, sizeof (*flent));
2288275SEric Cheng 		mutex_init(&flent->fe_lock, NULL, MUTEX_DEFAULT, NULL);
2298275SEric Cheng 		cv_init(&flent->fe_cv, NULL, CV_DEFAULT, NULL);
2308275SEric Cheng 
2318275SEric Cheng 		/* Initialize the receiver function to a safe routine */
2328275SEric Cheng 		flent->fe_cb_fn = (flow_fn_t)mac_pkt_drop;
2338275SEric Cheng 		flent->fe_index = -1;
2348275SEric Cheng 	}
2358558SGirish.Moodalbail@Sun.COM 	(void) strlcpy(flent->fe_flow_name, name, MAXFLOWNAMELEN);
2368275SEric Cheng 
2378275SEric Cheng 	/* This is an initial flow, will be configured later */
2388275SEric Cheng 	if (fd == NULL) {
2398275SEric Cheng 		*flentp = flent;
2408275SEric Cheng 		return (0);
2418275SEric Cheng 	}
2428275SEric Cheng 
2438275SEric Cheng 	flent->fe_client_cookie = client_cookie;
2448275SEric Cheng 	flent->fe_type = type;
2458275SEric Cheng 
2468275SEric Cheng 	/* Save flow desc */
2478275SEric Cheng 	bcopy(fd, &flent->fe_flow_desc, sizeof (*fd));
2488275SEric Cheng 
2498275SEric Cheng 	if (mrp != NULL) {
2508275SEric Cheng 		/*
2518275SEric Cheng 		 * We have already set fe_resource_props for a Link.
2528275SEric Cheng 		 */
2538275SEric Cheng 		if (type & FLOW_USER) {
2548275SEric Cheng 			bcopy(mrp, &flent->fe_resource_props,
2558275SEric Cheng 			    sizeof (mac_resource_props_t));
2568275SEric Cheng 		}
2578275SEric Cheng 		/*
2588275SEric Cheng 		 * The effective resource list should reflect the priority
2598275SEric Cheng 		 * that we set implicitly.
2608275SEric Cheng 		 */
2618275SEric Cheng 		if (!(mrp->mrp_mask & MRP_PRIORITY))
2628275SEric Cheng 			mrp->mrp_mask |= MRP_PRIORITY;
2638275SEric Cheng 		if (type & FLOW_USER)
2648275SEric Cheng 			mrp->mrp_priority = MPL_SUBFLOW_DEFAULT;
2658275SEric Cheng 		else
2668275SEric Cheng 			mrp->mrp_priority = MPL_LINK_DEFAULT;
267*11878SVenu.Iyer@Sun.COM 		bzero(mrp->mrp_pool, MAXPATHLEN);
268*11878SVenu.Iyer@Sun.COM 		bzero(&mrp->mrp_cpus, sizeof (mac_cpus_t));
2698275SEric Cheng 		bcopy(mrp, &flent->fe_effective_props,
2708275SEric Cheng 		    sizeof (mac_resource_props_t));
2718275SEric Cheng 	}
2728275SEric Cheng 	flow_stat_create(flent);
2738275SEric Cheng 
2748275SEric Cheng 	*flentp = flent;
2758275SEric Cheng 	return (0);
2768275SEric Cheng }
2778275SEric Cheng 
2788275SEric Cheng /*
2798275SEric Cheng  * Validate flow entry and add it to a flow table.
2808275SEric Cheng  */
2818275SEric Cheng int
mac_flow_add(flow_tab_t * ft,flow_entry_t * flent)2828275SEric Cheng mac_flow_add(flow_tab_t *ft, flow_entry_t *flent)
2838275SEric Cheng {
2848275SEric Cheng 	flow_entry_t	**headp, **p;
2858275SEric Cheng 	flow_ops_t	*ops = &ft->ft_ops;
2868275SEric Cheng 	flow_mask_t	mask;
2878275SEric Cheng 	uint32_t	index;
2888275SEric Cheng 	int		err;
2898275SEric Cheng 
2908275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)ft->ft_mip));
2918275SEric Cheng 
2928275SEric Cheng 	/*
2938275SEric Cheng 	 * Check for invalid bits in mask.
2948275SEric Cheng 	 */
2958275SEric Cheng 	mask = flent->fe_flow_desc.fd_mask;
2968275SEric Cheng 	if ((mask & ft->ft_mask) == 0 || (mask & ~ft->ft_mask) != 0)
2978275SEric Cheng 		return (EOPNOTSUPP);
2988275SEric Cheng 
2998275SEric Cheng 	/*
3008275SEric Cheng 	 * Validate flent.
3018275SEric Cheng 	 */
3028275SEric Cheng 	if ((err = ops->fo_accept_fe(ft, flent)) != 0) {
3038275SEric Cheng 		DTRACE_PROBE3(accept_failed, flow_tab_t *, ft,
3048275SEric Cheng 		    flow_entry_t *, flent, int, err);
3058275SEric Cheng 		return (err);
3068275SEric Cheng 	}
3078275SEric Cheng 
3088275SEric Cheng 	/*
3098275SEric Cheng 	 * Flent is valid. now calculate hash and insert it
3108275SEric Cheng 	 * into hash table.
3118275SEric Cheng 	 */
3128275SEric Cheng 	index = ops->fo_hash_fe(ft, flent);
3138275SEric Cheng 
3148275SEric Cheng 	/*
3158275SEric Cheng 	 * We do not need a lock up until now because we were
3168275SEric Cheng 	 * not accessing the flow table.
3178275SEric Cheng 	 */
3188275SEric Cheng 	rw_enter(&ft->ft_lock, RW_WRITER);
3198275SEric Cheng 	headp = &ft->ft_table[index];
3208275SEric Cheng 
3218275SEric Cheng 	/*
3228275SEric Cheng 	 * Check for duplicate flow.
3238275SEric Cheng 	 */
3248275SEric Cheng 	for (p = headp; *p != NULL; p = &(*p)->fe_next) {
3258275SEric Cheng 		if ((*p)->fe_flow_desc.fd_mask !=
3268275SEric Cheng 		    flent->fe_flow_desc.fd_mask)
3278275SEric Cheng 			continue;
3288275SEric Cheng 
3298275SEric Cheng 		if (ft->ft_ops.fo_match_fe(ft, *p, flent)) {
3308275SEric Cheng 			rw_exit(&ft->ft_lock);
3318275SEric Cheng 			DTRACE_PROBE3(dup_flow, flow_tab_t *, ft,
3328275SEric Cheng 			    flow_entry_t *, flent, int, err);
3338275SEric Cheng 			return (EALREADY);
3348275SEric Cheng 		}
3358275SEric Cheng 	}
3368275SEric Cheng 
3378275SEric Cheng 	/*
3388275SEric Cheng 	 * Insert flow to hash list.
3398275SEric Cheng 	 */
3408275SEric Cheng 	err = ops->fo_insert_fe(ft, headp, flent);
3418275SEric Cheng 	if (err != 0) {
3428275SEric Cheng 		rw_exit(&ft->ft_lock);
3438275SEric Cheng 		DTRACE_PROBE3(insert_failed, flow_tab_t *, ft,
3448275SEric Cheng 		    flow_entry_t *, flent, int, err);
3458275SEric Cheng 		return (err);
3468275SEric Cheng 	}
3478275SEric Cheng 
3488275SEric Cheng 	/*
3498275SEric Cheng 	 * Save the hash index so it can be used by mac_flow_remove().
3508275SEric Cheng 	 */
3518275SEric Cheng 	flent->fe_index = (int)index;
3528275SEric Cheng 
3538275SEric Cheng 	/*
3548275SEric Cheng 	 * Save the flow tab back reference.
3558275SEric Cheng 	 */
3568275SEric Cheng 	flent->fe_flow_tab = ft;
3578275SEric Cheng 	FLOW_MARK(flent, FE_FLOW_TAB);
3588275SEric Cheng 	ft->ft_flow_count++;
3598275SEric Cheng 	rw_exit(&ft->ft_lock);
3608275SEric Cheng 	return (0);
3618275SEric Cheng }
3628275SEric Cheng 
3638275SEric Cheng /*
3648275SEric Cheng  * Remove a flow from a mac client's subflow table
3658275SEric Cheng  */
3668275SEric Cheng void
mac_flow_rem_subflow(flow_entry_t * flent)3678275SEric Cheng mac_flow_rem_subflow(flow_entry_t *flent)
3688275SEric Cheng {
3698275SEric Cheng 	flow_tab_t		*ft = flent->fe_flow_tab;
3708275SEric Cheng 	mac_client_impl_t	*mcip = ft->ft_mcip;
3719073SCathy.Zhou@Sun.COM 	mac_handle_t		mh = (mac_handle_t)ft->ft_mip;
3728275SEric Cheng 
3739073SCathy.Zhou@Sun.COM 	ASSERT(MAC_PERIM_HELD(mh));
3748275SEric Cheng 
3758275SEric Cheng 	mac_flow_remove(ft, flent, B_FALSE);
3768275SEric Cheng 	if (flent->fe_mcip == NULL) {
3778275SEric Cheng 		/*
3788275SEric Cheng 		 * The interface is not yet plumbed and mac_client_flow_add
3798275SEric Cheng 		 * was not done.
3808275SEric Cheng 		 */
3818275SEric Cheng 		if (FLOW_TAB_EMPTY(ft)) {
3828275SEric Cheng 			mac_flow_tab_destroy(ft);
3838275SEric Cheng 			mcip->mci_subflow_tab = NULL;
3848275SEric Cheng 		}
3859073SCathy.Zhou@Sun.COM 	} else {
3869073SCathy.Zhou@Sun.COM 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3879073SCathy.Zhou@Sun.COM 		mac_link_flow_clean((mac_client_handle_t)mcip, flent);
3888275SEric Cheng 	}
3899073SCathy.Zhou@Sun.COM 	mac_fastpath_enable(mh);
3908275SEric Cheng }
3918275SEric Cheng 
3928275SEric Cheng /*
3938275SEric Cheng  * Add a flow to a mac client's subflow table and instantiate the flow
3948275SEric Cheng  * in the mac by creating the associated SRSs etc.
3958275SEric Cheng  */
3968275SEric Cheng int
mac_flow_add_subflow(mac_client_handle_t mch,flow_entry_t * flent,boolean_t instantiate_flow)3978275SEric Cheng mac_flow_add_subflow(mac_client_handle_t mch, flow_entry_t *flent,
3988275SEric Cheng     boolean_t instantiate_flow)
3998275SEric Cheng {
4008275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4019073SCathy.Zhou@Sun.COM 	mac_handle_t		mh = (mac_handle_t)mcip->mci_mip;
4028275SEric Cheng 	flow_tab_info_t		*ftinfo;
4038275SEric Cheng 	flow_mask_t		mask;
4048275SEric Cheng 	flow_tab_t		*ft;
4058275SEric Cheng 	int			err;
4068275SEric Cheng 	boolean_t		ft_created = B_FALSE;
4078275SEric Cheng 
4089073SCathy.Zhou@Sun.COM 	ASSERT(MAC_PERIM_HELD(mh));
4099073SCathy.Zhou@Sun.COM 
4109073SCathy.Zhou@Sun.COM 	if ((err = mac_fastpath_disable(mh)) != 0)
4119073SCathy.Zhou@Sun.COM 		return (err);
4128275SEric Cheng 
4138275SEric Cheng 	/*
4148275SEric Cheng 	 * If the subflow table exists already just add the new subflow
4158275SEric Cheng 	 * to the existing table, else we create a new subflow table below.
4168275SEric Cheng 	 */
4178275SEric Cheng 	ft = mcip->mci_subflow_tab;
4188275SEric Cheng 	if (ft == NULL) {
4198275SEric Cheng 		mask = flent->fe_flow_desc.fd_mask;
4208275SEric Cheng 		/*
4218275SEric Cheng 		 * Try to create a new table and then add the subflow to the
4228275SEric Cheng 		 * newly created subflow table
4238275SEric Cheng 		 */
4249073SCathy.Zhou@Sun.COM 		if ((ftinfo = mac_flow_tab_info_get(mask)) == NULL) {
4259073SCathy.Zhou@Sun.COM 			mac_fastpath_enable(mh);
4268275SEric Cheng 			return (EOPNOTSUPP);
4279073SCathy.Zhou@Sun.COM 		}
4288275SEric Cheng 
4298275SEric Cheng 		mac_flow_tab_create(ftinfo->fti_ops, mask, ftinfo->fti_size,
4308275SEric Cheng 		    mcip->mci_mip, &ft);
4318275SEric Cheng 		ft_created = B_TRUE;
4328275SEric Cheng 	}
4338275SEric Cheng 
4348275SEric Cheng 	err = mac_flow_add(ft, flent);
4358275SEric Cheng 	if (err != 0) {
4368275SEric Cheng 		if (ft_created)
4378275SEric Cheng 			mac_flow_tab_destroy(ft);
4389073SCathy.Zhou@Sun.COM 		mac_fastpath_enable(mh);
4398275SEric Cheng 		return (err);
4408275SEric Cheng 	}
4418275SEric Cheng 
4428275SEric Cheng 	if (instantiate_flow) {
4438275SEric Cheng 		/* Now activate the flow by creating its SRSs */
4448275SEric Cheng 		ASSERT(MCIP_DATAPATH_SETUP(mcip));
4458275SEric Cheng 		err = mac_link_flow_init((mac_client_handle_t)mcip, flent);
4468275SEric Cheng 		if (err != 0) {
4478275SEric Cheng 			mac_flow_remove(ft, flent, B_FALSE);
4488275SEric Cheng 			if (ft_created)
4498275SEric Cheng 				mac_flow_tab_destroy(ft);
4509073SCathy.Zhou@Sun.COM 			mac_fastpath_enable(mh);
4518275SEric Cheng 			return (err);
4528275SEric Cheng 		}
4538275SEric Cheng 	} else {
4548275SEric Cheng 		FLOW_MARK(flent, FE_UF_NO_DATAPATH);
4558275SEric Cheng 	}
4568275SEric Cheng 	if (ft_created) {
4578275SEric Cheng 		ASSERT(mcip->mci_subflow_tab == NULL);
4588275SEric Cheng 		ft->ft_mcip = mcip;
4598275SEric Cheng 		mcip->mci_subflow_tab = ft;
4608275SEric Cheng 		if (instantiate_flow)
4618275SEric Cheng 			mac_client_update_classifier(mcip, B_TRUE);
4628275SEric Cheng 	}
4638275SEric Cheng 	return (0);
4648275SEric Cheng }
4658275SEric Cheng 
4668275SEric Cheng /*
4678275SEric Cheng  * Remove flow entry from flow table.
4688275SEric Cheng  */
4698275SEric Cheng void
mac_flow_remove(flow_tab_t * ft,flow_entry_t * flent,boolean_t temp)4708275SEric Cheng mac_flow_remove(flow_tab_t *ft, flow_entry_t *flent, boolean_t temp)
4718275SEric Cheng {
4728275SEric Cheng 	flow_entry_t	**fp;
4738275SEric Cheng 
4748275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)ft->ft_mip));
4758275SEric Cheng 	if (!(flent->fe_flags & FE_FLOW_TAB))
4768275SEric Cheng 		return;
4778275SEric Cheng 
4788275SEric Cheng 	rw_enter(&ft->ft_lock, RW_WRITER);
4798275SEric Cheng 	/*
4808275SEric Cheng 	 * If this is a permanent removal from the flow table, mark it
4818275SEric Cheng 	 * CONDEMNED to prevent future references. If this is a temporary
4828275SEric Cheng 	 * removal from the table, say to update the flow descriptor then
4838275SEric Cheng 	 * we don't mark it CONDEMNED
4848275SEric Cheng 	 */
4858275SEric Cheng 	if (!temp)
4868275SEric Cheng 		FLOW_MARK(flent, FE_CONDEMNED);
4878275SEric Cheng 	/*
4888275SEric Cheng 	 * Locate the specified flent.
4898275SEric Cheng 	 */
4908275SEric Cheng 	fp = &ft->ft_table[flent->fe_index];
4918275SEric Cheng 	while (*fp != flent)
4928275SEric Cheng 		fp = &(*fp)->fe_next;
4938275SEric Cheng 
4948275SEric Cheng 	/*
4958275SEric Cheng 	 * The flent must exist. Otherwise it's a bug.
4968275SEric Cheng 	 */
4978275SEric Cheng 	ASSERT(fp != NULL);
4988275SEric Cheng 	*fp = flent->fe_next;
4998275SEric Cheng 	flent->fe_next = NULL;
5008275SEric Cheng 
5018275SEric Cheng 	/*
5028275SEric Cheng 	 * Reset fe_index to -1 so any attempt to call mac_flow_remove()
5038275SEric Cheng 	 * on a flent that is supposed to be in the table (FE_FLOW_TAB)
5048275SEric Cheng 	 * will panic.
5058275SEric Cheng 	 */
5068275SEric Cheng 	flent->fe_index = -1;
5078275SEric Cheng 	FLOW_UNMARK(flent, FE_FLOW_TAB);
5088275SEric Cheng 	ft->ft_flow_count--;
5098275SEric Cheng 	rw_exit(&ft->ft_lock);
5108275SEric Cheng }
5118275SEric Cheng 
5128275SEric Cheng /*
5138275SEric Cheng  * This is the flow lookup routine used by the mac sw classifier engine.
5148275SEric Cheng  */
5158275SEric Cheng int
mac_flow_lookup(flow_tab_t * ft,mblk_t * mp,uint_t flags,flow_entry_t ** flentp)5168275SEric Cheng mac_flow_lookup(flow_tab_t *ft, mblk_t *mp, uint_t flags, flow_entry_t **flentp)
5178275SEric Cheng {
5188275SEric Cheng 	flow_state_t	s;
5198275SEric Cheng 	flow_entry_t	*flent;
5208275SEric Cheng 	flow_ops_t	*ops = &ft->ft_ops;
5218275SEric Cheng 	boolean_t	retried = B_FALSE;
5228275SEric Cheng 	int		i, err;
5238275SEric Cheng 
5248275SEric Cheng 	s.fs_flags = flags;
5258833SVenu.Iyer@Sun.COM retry:
5268275SEric Cheng 	s.fs_mp = mp;
5278275SEric Cheng 
5288275SEric Cheng 	/*
5298275SEric Cheng 	 * Walk the list of predeclared accept functions.
5308275SEric Cheng 	 * Each of these would accumulate enough state to allow the next
5318275SEric Cheng 	 * accept routine to make progress.
5328275SEric Cheng 	 */
5338275SEric Cheng 	for (i = 0; i < FLOW_MAX_ACCEPT && ops->fo_accept[i] != NULL; i++) {
5348275SEric Cheng 		if ((err = (ops->fo_accept[i])(ft, &s)) != 0) {
5358833SVenu.Iyer@Sun.COM 			mblk_t	*last;
5368833SVenu.Iyer@Sun.COM 
5378275SEric Cheng 			/*
5388275SEric Cheng 			 * ENOBUFS indicates that the mp could be too short
5398275SEric Cheng 			 * and may need a pullup.
5408275SEric Cheng 			 */
5418275SEric Cheng 			if (err != ENOBUFS || retried)
5428275SEric Cheng 				return (err);
5438275SEric Cheng 
5448275SEric Cheng 			/*
5458833SVenu.Iyer@Sun.COM 			 * The pullup is done on the last processed mblk, not
5468833SVenu.Iyer@Sun.COM 			 * the starting one. pullup is not done if the mblk
5478833SVenu.Iyer@Sun.COM 			 * has references or if b_cont is NULL.
5488275SEric Cheng 			 */
5498833SVenu.Iyer@Sun.COM 			last = s.fs_mp;
5508833SVenu.Iyer@Sun.COM 			if (DB_REF(last) > 1 || last->b_cont == NULL ||
5518833SVenu.Iyer@Sun.COM 			    pullupmsg(last, -1) == 0)
5528275SEric Cheng 				return (EINVAL);
5538275SEric Cheng 
5548275SEric Cheng 			retried = B_TRUE;
5558275SEric Cheng 			DTRACE_PROBE2(need_pullup, flow_tab_t *, ft,
5568275SEric Cheng 			    flow_state_t *, &s);
5578275SEric Cheng 			goto retry;
5588275SEric Cheng 		}
5598275SEric Cheng 	}
5608275SEric Cheng 
5618275SEric Cheng 	/*
5628275SEric Cheng 	 * The packet is considered sane. We may now attempt to
5638275SEric Cheng 	 * find the corresponding flent.
5648275SEric Cheng 	 */
5658275SEric Cheng 	rw_enter(&ft->ft_lock, RW_READER);
5668275SEric Cheng 	flent = ft->ft_table[ops->fo_hash(ft, &s)];
5678275SEric Cheng 	for (; flent != NULL; flent = flent->fe_next) {
5688275SEric Cheng 		if (flent->fe_match(ft, flent, &s)) {
5698275SEric Cheng 			FLOW_TRY_REFHOLD(flent, err);
5708275SEric Cheng 			if (err != 0)
5718275SEric Cheng 				continue;
5728275SEric Cheng 			*flentp = flent;
5738275SEric Cheng 			rw_exit(&ft->ft_lock);
5748275SEric Cheng 			return (0);
5758275SEric Cheng 		}
5768275SEric Cheng 	}
5778275SEric Cheng 	rw_exit(&ft->ft_lock);
5788275SEric Cheng 	return (ENOENT);
5798275SEric Cheng }
5808275SEric Cheng 
5818275SEric Cheng /*
5828275SEric Cheng  * Walk flow table.
5838275SEric Cheng  * The caller is assumed to have proper perimeter protection.
5848275SEric Cheng  */
5858275SEric Cheng int
mac_flow_walk_nolock(flow_tab_t * ft,int (* fn)(flow_entry_t *,void *),void * arg)5868275SEric Cheng mac_flow_walk_nolock(flow_tab_t *ft, int (*fn)(flow_entry_t *, void *),
5878275SEric Cheng     void *arg)
5888275SEric Cheng {
5898275SEric Cheng 	int		err, i, cnt = 0;
5908275SEric Cheng 	flow_entry_t	*flent;
5918275SEric Cheng 
5928275SEric Cheng 	if (ft == NULL)
5938275SEric Cheng 		return (0);
5948275SEric Cheng 
5958275SEric Cheng 	for (i = 0; i < ft->ft_size; i++) {
5968275SEric Cheng 		for (flent = ft->ft_table[i]; flent != NULL;
5978275SEric Cheng 		    flent = flent->fe_next) {
5988275SEric Cheng 			cnt++;
5998275SEric Cheng 			err = (*fn)(flent, arg);
6008275SEric Cheng 			if (err != 0)
6018275SEric Cheng 				return (err);
6028275SEric Cheng 		}
6038275SEric Cheng 	}
6048275SEric Cheng 	VERIFY(cnt == ft->ft_flow_count);
6058275SEric Cheng 	return (0);
6068275SEric Cheng }
6078275SEric Cheng 
6088275SEric Cheng /*
6098275SEric Cheng  * Same as the above except a mutex is used for protection here.
6108275SEric Cheng  */
6118275SEric Cheng int
mac_flow_walk(flow_tab_t * ft,int (* fn)(flow_entry_t *,void *),void * arg)6128275SEric Cheng mac_flow_walk(flow_tab_t *ft, int (*fn)(flow_entry_t *, void *),
6138275SEric Cheng     void *arg)
6148275SEric Cheng {
6158275SEric Cheng 	int		err;
6168275SEric Cheng 
6178275SEric Cheng 	if (ft == NULL)
6188275SEric Cheng 		return (0);
6198275SEric Cheng 
6208275SEric Cheng 	rw_enter(&ft->ft_lock, RW_WRITER);
6218275SEric Cheng 	err = mac_flow_walk_nolock(ft, fn, arg);
6228275SEric Cheng 	rw_exit(&ft->ft_lock);
6238275SEric Cheng 	return (err);
6248275SEric Cheng }
6258275SEric Cheng 
6268275SEric Cheng static boolean_t	mac_flow_clean(flow_entry_t *);
6278275SEric Cheng 
6288275SEric Cheng /*
6298275SEric Cheng  * Destroy a flow entry. Called when the last reference on a flow is released.
6308275SEric Cheng  */
6318275SEric Cheng void
mac_flow_destroy(flow_entry_t * flent)6328275SEric Cheng mac_flow_destroy(flow_entry_t *flent)
6338275SEric Cheng {
6348275SEric Cheng 	ASSERT(flent->fe_refcnt == 0);
6358275SEric Cheng 
6368275SEric Cheng 	if ((flent->fe_type & FLOW_USER) != 0) {
6378275SEric Cheng 		ASSERT(mac_flow_clean(flent));
6388275SEric Cheng 	} else {
6398275SEric Cheng 		mac_flow_cleanup(flent);
6408275SEric Cheng 	}
641*11878SVenu.Iyer@Sun.COM 	mac_misc_stat_delete(flent);
6428275SEric Cheng 	mutex_destroy(&flent->fe_lock);
6438275SEric Cheng 	cv_destroy(&flent->fe_cv);
6448275SEric Cheng 	flow_stat_destroy(flent);
6458275SEric Cheng 	kmem_cache_free(flow_cache, flent);
6468275SEric Cheng }
6478275SEric Cheng 
6488275SEric Cheng /*
6498275SEric Cheng  * XXX eric
6508275SEric Cheng  * The MAC_FLOW_PRIORITY checks in mac_resource_ctl_set() and
6518275SEric Cheng  * mac_link_flow_modify() should really be moved/reworked into the
6528275SEric Cheng  * two functions below. This would consolidate all the mac property
6538275SEric Cheng  * checking in one place. I'm leaving this alone for now since it's
6548275SEric Cheng  * out of scope of the new flows work.
6558275SEric Cheng  */
6568275SEric Cheng /* ARGSUSED */
6578275SEric Cheng uint32_t
mac_flow_modify_props(flow_entry_t * flent,mac_resource_props_t * mrp)6588275SEric Cheng mac_flow_modify_props(flow_entry_t *flent, mac_resource_props_t *mrp)
6598275SEric Cheng {
6608275SEric Cheng 	uint32_t		changed_mask = 0;
6618275SEric Cheng 	mac_resource_props_t	*fmrp = &flent->fe_effective_props;
6628275SEric Cheng 	int			i;
6638275SEric Cheng 
6648275SEric Cheng 	if ((mrp->mrp_mask & MRP_MAXBW) != 0 &&
665*11878SVenu.Iyer@Sun.COM 	    (!(fmrp->mrp_mask & MRP_MAXBW) ||
666*11878SVenu.Iyer@Sun.COM 	    (fmrp->mrp_maxbw != mrp->mrp_maxbw))) {
6678275SEric Cheng 		changed_mask |= MRP_MAXBW;
6688275SEric Cheng 		if (mrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
6698275SEric Cheng 			fmrp->mrp_mask &= ~MRP_MAXBW;
670*11878SVenu.Iyer@Sun.COM 			fmrp->mrp_maxbw = 0;
6718275SEric Cheng 		} else {
6728275SEric Cheng 			fmrp->mrp_mask |= MRP_MAXBW;
673*11878SVenu.Iyer@Sun.COM 			fmrp->mrp_maxbw = mrp->mrp_maxbw;
6748275SEric Cheng 		}
6758275SEric Cheng 	}
6768275SEric Cheng 
6778275SEric Cheng 	if ((mrp->mrp_mask & MRP_PRIORITY) != 0) {
6788275SEric Cheng 		if (fmrp->mrp_priority != mrp->mrp_priority)
6798275SEric Cheng 			changed_mask |= MRP_PRIORITY;
6808275SEric Cheng 		if (mrp->mrp_priority == MPL_RESET) {
6818275SEric Cheng 			fmrp->mrp_priority = MPL_SUBFLOW_DEFAULT;
6828275SEric Cheng 			fmrp->mrp_mask &= ~MRP_PRIORITY;
6838275SEric Cheng 		} else {
6848275SEric Cheng 			fmrp->mrp_priority = mrp->mrp_priority;
6858275SEric Cheng 			fmrp->mrp_mask |= MRP_PRIORITY;
6868275SEric Cheng 		}
6878275SEric Cheng 	}
6888275SEric Cheng 
6898275SEric Cheng 	/* modify fanout */
6908275SEric Cheng 	if ((mrp->mrp_mask & MRP_CPUS) != 0) {
6918275SEric Cheng 		if ((fmrp->mrp_ncpus == mrp->mrp_ncpus) &&
6928275SEric Cheng 		    (fmrp->mrp_fanout_mode == mrp->mrp_fanout_mode)) {
6938275SEric Cheng 			for (i = 0; i < mrp->mrp_ncpus; i++) {
6948275SEric Cheng 				if (mrp->mrp_cpu[i] != fmrp->mrp_cpu[i])
6958275SEric Cheng 					break;
6968275SEric Cheng 			}
6978275SEric Cheng 			if (i == mrp->mrp_ncpus) {
6988275SEric Cheng 				/*
6998275SEric Cheng 				 * The new set of cpus passed is exactly
7008275SEric Cheng 				 * the same as the existing set.
7018275SEric Cheng 				 */
7028275SEric Cheng 				return (changed_mask);
7038275SEric Cheng 			}
7048275SEric Cheng 		}
7058275SEric Cheng 		changed_mask |= MRP_CPUS;
7068275SEric Cheng 		MAC_COPY_CPUS(mrp, fmrp);
7078275SEric Cheng 	}
708*11878SVenu.Iyer@Sun.COM 
709*11878SVenu.Iyer@Sun.COM 	/*
710*11878SVenu.Iyer@Sun.COM 	 * Modify the rings property.
711*11878SVenu.Iyer@Sun.COM 	 */
712*11878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_RX_RINGS || mrp->mrp_mask & MRP_TX_RINGS)
713*11878SVenu.Iyer@Sun.COM 		mac_set_rings_effective(flent->fe_mcip);
714*11878SVenu.Iyer@Sun.COM 
715*11878SVenu.Iyer@Sun.COM 	if ((mrp->mrp_mask & MRP_POOL) != 0) {
716*11878SVenu.Iyer@Sun.COM 		if (strcmp(fmrp->mrp_pool, mrp->mrp_pool) != 0)
717*11878SVenu.Iyer@Sun.COM 			changed_mask |= MRP_POOL;
718*11878SVenu.Iyer@Sun.COM 		if (strlen(mrp->mrp_pool) == 0)
719*11878SVenu.Iyer@Sun.COM 			fmrp->mrp_mask &= ~MRP_POOL;
720*11878SVenu.Iyer@Sun.COM 		else
721*11878SVenu.Iyer@Sun.COM 			fmrp->mrp_mask |= MRP_POOL;
722*11878SVenu.Iyer@Sun.COM 		(void) strncpy(fmrp->mrp_pool, mrp->mrp_pool, MAXPATHLEN);
723*11878SVenu.Iyer@Sun.COM 	}
7248275SEric Cheng 	return (changed_mask);
7258275SEric Cheng }
7268275SEric Cheng 
7278275SEric Cheng void
mac_flow_modify(flow_tab_t * ft,flow_entry_t * flent,mac_resource_props_t * mrp)7288275SEric Cheng mac_flow_modify(flow_tab_t *ft, flow_entry_t *flent, mac_resource_props_t *mrp)
7298275SEric Cheng {
7308275SEric Cheng 	uint32_t changed_mask;
7318275SEric Cheng 	mac_client_impl_t *mcip = flent->fe_mcip;
7328275SEric Cheng 	mac_resource_props_t *mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
733*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t *emrp = MCIP_EFFECTIVE_PROPS(mcip);
734*11878SVenu.Iyer@Sun.COM 	cpupart_t *cpupart = NULL;
735*11878SVenu.Iyer@Sun.COM 	boolean_t use_default = B_FALSE;
7368275SEric Cheng 
7378275SEric Cheng 	ASSERT(flent != NULL);
7388275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)ft->ft_mip));
7398275SEric Cheng 
7408275SEric Cheng 	rw_enter(&ft->ft_lock, RW_WRITER);
7418275SEric Cheng 
7428275SEric Cheng 	/* Update the cached values inside the subflow entry */
7438275SEric Cheng 	changed_mask = mac_flow_modify_props(flent, mrp);
7448275SEric Cheng 	rw_exit(&ft->ft_lock);
7458275SEric Cheng 	/*
7468275SEric Cheng 	 * Push the changed parameters to the scheduling code in the
7478275SEric Cheng 	 * SRS's, to take effect right away.
7488275SEric Cheng 	 */
7498275SEric Cheng 	if (changed_mask & MRP_MAXBW) {
7508275SEric Cheng 		mac_srs_update_bwlimit(flent, mrp);
7518275SEric Cheng 		/*
7528275SEric Cheng 		 * If bandwidth is changed, we may have to change
7538275SEric Cheng 		 * the number of soft ring to be used for fanout.
7548275SEric Cheng 		 * Call mac_flow_update_fanout() if MAC_BIND_CPU
7558275SEric Cheng 		 * is not set and there is no user supplied cpu
7568275SEric Cheng 		 * info. This applies only to link at this time.
7578275SEric Cheng 		 */
7588275SEric Cheng 		if (!(flent->fe_type & FLOW_USER) &&
7598275SEric Cheng 		    !(changed_mask & MRP_CPUS) &&
7608275SEric Cheng 		    !(mcip_mrp->mrp_mask & MRP_CPUS_USERSPEC)) {
7618275SEric Cheng 			mac_fanout_setup(mcip, flent, mcip_mrp,
762*11878SVenu.Iyer@Sun.COM 			    mac_rx_deliver, mcip, NULL, NULL);
7638275SEric Cheng 		}
7648275SEric Cheng 	}
7658275SEric Cheng 	if (mrp->mrp_mask & MRP_PRIORITY)
7668275SEric Cheng 		mac_flow_update_priority(mcip, flent);
7678275SEric Cheng 
7688275SEric Cheng 	if (changed_mask & MRP_CPUS)
769*11878SVenu.Iyer@Sun.COM 		mac_fanout_setup(mcip, flent, mrp, mac_rx_deliver, mcip, NULL,
770*11878SVenu.Iyer@Sun.COM 		    NULL);
771*11878SVenu.Iyer@Sun.COM 
772*11878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_POOL) {
773*11878SVenu.Iyer@Sun.COM 		pool_lock();
774*11878SVenu.Iyer@Sun.COM 		cpupart = mac_pset_find(mrp, &use_default);
775*11878SVenu.Iyer@Sun.COM 		mac_fanout_setup(mcip, flent, mrp, mac_rx_deliver, mcip, NULL,
776*11878SVenu.Iyer@Sun.COM 		    cpupart);
777*11878SVenu.Iyer@Sun.COM 		mac_set_pool_effective(use_default, cpupart, mrp, emrp);
778*11878SVenu.Iyer@Sun.COM 		pool_unlock();
779*11878SVenu.Iyer@Sun.COM 	}
7808275SEric Cheng }
7818275SEric Cheng 
7828275SEric Cheng /*
7838275SEric Cheng  * This function waits for a certain condition to be met and is generally
7848275SEric Cheng  * used before a destructive or quiescing operation.
7858275SEric Cheng  */
7868275SEric Cheng void
mac_flow_wait(flow_entry_t * flent,mac_flow_state_t event)7878275SEric Cheng mac_flow_wait(flow_entry_t *flent, mac_flow_state_t event)
7888275SEric Cheng {
7898275SEric Cheng 	mutex_enter(&flent->fe_lock);
7908275SEric Cheng 	flent->fe_flags |= FE_WAITER;
7918275SEric Cheng 
7928275SEric Cheng 	switch (event) {
7938275SEric Cheng 	case FLOW_DRIVER_UPCALL:
7948275SEric Cheng 		/*
7958275SEric Cheng 		 * We want to make sure the driver upcalls have finished before
7968275SEric Cheng 		 * we signal the Rx SRS worker to quit.
7978275SEric Cheng 		 */
7988275SEric Cheng 		while (flent->fe_refcnt != 1)
7998275SEric Cheng 			cv_wait(&flent->fe_cv, &flent->fe_lock);
8008275SEric Cheng 		break;
8018275SEric Cheng 
8028275SEric Cheng 	case FLOW_USER_REF:
8038275SEric Cheng 		/*
8048275SEric Cheng 		 * Wait for the fe_user_refcnt to drop to 0. The flow has
8058275SEric Cheng 		 * been removed from the global flow hash.
8068275SEric Cheng 		 */
8078275SEric Cheng 		ASSERT(!(flent->fe_flags & FE_G_FLOW_HASH));
8088275SEric Cheng 		while (flent->fe_user_refcnt != 0)
8098275SEric Cheng 			cv_wait(&flent->fe_cv, &flent->fe_lock);
8108275SEric Cheng 		break;
8118275SEric Cheng 
8128275SEric Cheng 	default:
8138275SEric Cheng 		ASSERT(0);
8148275SEric Cheng 	}
8158275SEric Cheng 
8168275SEric Cheng 	flent->fe_flags &= ~FE_WAITER;
8178275SEric Cheng 	mutex_exit(&flent->fe_lock);
8188275SEric Cheng }
8198275SEric Cheng 
8208275SEric Cheng static boolean_t
mac_flow_clean(flow_entry_t * flent)8218275SEric Cheng mac_flow_clean(flow_entry_t *flent)
8228275SEric Cheng {
8238275SEric Cheng 	ASSERT(flent->fe_next == NULL);
8248275SEric Cheng 	ASSERT(flent->fe_tx_srs == NULL);
8258275SEric Cheng 	ASSERT(flent->fe_rx_srs_cnt == 0 && flent->fe_rx_srs[0] == NULL);
8268275SEric Cheng 	ASSERT(flent->fe_mbg == NULL);
8278275SEric Cheng 
8288275SEric Cheng 	return (B_TRUE);
8298275SEric Cheng }
8308275SEric Cheng 
8318275SEric Cheng void
mac_flow_cleanup(flow_entry_t * flent)8328275SEric Cheng mac_flow_cleanup(flow_entry_t *flent)
8338275SEric Cheng {
8348275SEric Cheng 	if ((flent->fe_type & FLOW_USER) == 0) {
8358275SEric Cheng 		ASSERT((flent->fe_mbg == NULL && flent->fe_mcip != NULL) ||
8368275SEric Cheng 		    (flent->fe_mbg != NULL && flent->fe_mcip == NULL));
8378275SEric Cheng 		ASSERT(flent->fe_refcnt == 0);
8388275SEric Cheng 	} else {
8398275SEric Cheng 		ASSERT(flent->fe_refcnt == 1);
8408275SEric Cheng 	}
8418275SEric Cheng 
8428275SEric Cheng 	if (flent->fe_mbg != NULL) {
8438275SEric Cheng 		ASSERT(flent->fe_tx_srs == NULL);
8448275SEric Cheng 		/* This is a multicast or broadcast flow entry */
8458275SEric Cheng 		mac_bcast_grp_free(flent->fe_mbg);
8468275SEric Cheng 		flent->fe_mbg = NULL;
8478275SEric Cheng 	}
8488275SEric Cheng 
8498275SEric Cheng 	if (flent->fe_tx_srs != NULL) {
8508275SEric Cheng 		ASSERT(flent->fe_mbg == NULL);
8518275SEric Cheng 		mac_srs_free(flent->fe_tx_srs);
8528275SEric Cheng 		flent->fe_tx_srs = NULL;
8538275SEric Cheng 	}
8548275SEric Cheng 
8558275SEric Cheng 	/*
8568275SEric Cheng 	 * In the normal case fe_rx_srs_cnt is 1. However in the error case
8578275SEric Cheng 	 * when mac_unicast_add fails we may not have set up any SRS
8588275SEric Cheng 	 * in which case fe_rx_srs_cnt will be zero.
8598275SEric Cheng 	 */
8608275SEric Cheng 	if (flent->fe_rx_srs_cnt != 0) {
8618275SEric Cheng 		ASSERT(flent->fe_rx_srs_cnt == 1);
8628275SEric Cheng 		mac_srs_free(flent->fe_rx_srs[0]);
8638275SEric Cheng 		flent->fe_rx_srs[0] = NULL;
8648275SEric Cheng 		flent->fe_rx_srs_cnt = 0;
8658275SEric Cheng 	}
8668275SEric Cheng 	ASSERT(flent->fe_rx_srs[0] == NULL);
8678275SEric Cheng }
8688275SEric Cheng 
8698275SEric Cheng void
mac_flow_get_desc(flow_entry_t * flent,flow_desc_t * fd)8708275SEric Cheng mac_flow_get_desc(flow_entry_t *flent, flow_desc_t *fd)
8718275SEric Cheng {
8728275SEric Cheng 	/*
8738275SEric Cheng 	 * Grab the fe_lock to see a self-consistent fe_flow_desc.
8748275SEric Cheng 	 * Updates to the fe_flow_desc happen under the fe_lock
8758275SEric Cheng 	 * after removing the flent from the flow table
8768275SEric Cheng 	 */
8778275SEric Cheng 	mutex_enter(&flent->fe_lock);
8788275SEric Cheng 	bcopy(&flent->fe_flow_desc, fd, sizeof (*fd));
8798275SEric Cheng 	mutex_exit(&flent->fe_lock);
8808275SEric Cheng }
8818275SEric Cheng 
8828275SEric Cheng /*
8838275SEric Cheng  * Update a field of a flow entry. The mac perimeter ensures that
8848275SEric Cheng  * this is the only thread doing a modify operation on this mac end point.
8858275SEric Cheng  * So the flow table can't change or disappear. The ft_lock protects access
8868275SEric Cheng  * to the flow entry, and holding the lock ensures that there isn't any thread
8878275SEric Cheng  * accessing the flow entry or attempting a flow table lookup. However
8888275SEric Cheng  * data threads that are using the flow entry based on the old descriptor
8898275SEric Cheng  * will continue to use the flow entry. If strong coherence is required
8908275SEric Cheng  * then the flow will have to be quiesced before the descriptor can be
8918275SEric Cheng  * changed.
8928275SEric Cheng  */
8938275SEric Cheng void
mac_flow_set_desc(flow_entry_t * flent,flow_desc_t * fd)8948275SEric Cheng mac_flow_set_desc(flow_entry_t *flent, flow_desc_t *fd)
8958275SEric Cheng {
8968275SEric Cheng 	flow_tab_t	*ft = flent->fe_flow_tab;
8978275SEric Cheng 	flow_desc_t	old_desc;
8988275SEric Cheng 	int		err;
8998275SEric Cheng 
9008275SEric Cheng 	if (ft == NULL) {
9018275SEric Cheng 		/*
9028275SEric Cheng 		 * The flow hasn't yet been inserted into the table,
9038275SEric Cheng 		 * so only the caller knows about this flow, however for
9048275SEric Cheng 		 * uniformity we grab the fe_lock here.
9058275SEric Cheng 		 */
9068275SEric Cheng 		mutex_enter(&flent->fe_lock);
9078275SEric Cheng 		bcopy(fd, &flent->fe_flow_desc, sizeof (*fd));
9088275SEric Cheng 		mutex_exit(&flent->fe_lock);
9098275SEric Cheng 	}
9108275SEric Cheng 
9118275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)ft->ft_mip));
9128275SEric Cheng 
9138275SEric Cheng 	/*
9148275SEric Cheng 	 * Need to remove the flow entry from the table and reinsert it,
9158275SEric Cheng 	 * into a potentially diference hash line. The hash depends on
9168275SEric Cheng 	 * the new descriptor fields. However access to fe_desc itself
9178275SEric Cheng 	 * is always under the fe_lock. This helps log and stat functions
9188275SEric Cheng 	 * see a self-consistent fe_flow_desc.
9198275SEric Cheng 	 */
9208275SEric Cheng 	mac_flow_remove(ft, flent, B_TRUE);
9218275SEric Cheng 	old_desc = flent->fe_flow_desc;
9228275SEric Cheng 
9238275SEric Cheng 	mutex_enter(&flent->fe_lock);
9248275SEric Cheng 	bcopy(fd, &flent->fe_flow_desc, sizeof (*fd));
9258275SEric Cheng 	mutex_exit(&flent->fe_lock);
9268275SEric Cheng 
9278275SEric Cheng 	if (mac_flow_add(ft, flent) != 0) {
9288275SEric Cheng 		/*
9298275SEric Cheng 		 * The add failed say due to an invalid flow descriptor.
9308275SEric Cheng 		 * Undo the update
9318275SEric Cheng 		 */
9328275SEric Cheng 		flent->fe_flow_desc = old_desc;
9338275SEric Cheng 		err = mac_flow_add(ft, flent);
9348275SEric Cheng 		ASSERT(err == 0);
9358275SEric Cheng 	}
9368275SEric Cheng }
9378275SEric Cheng 
9388275SEric Cheng void
mac_flow_set_name(flow_entry_t * flent,const char * name)9398275SEric Cheng mac_flow_set_name(flow_entry_t *flent, const char *name)
9408275SEric Cheng {
9418275SEric Cheng 	flow_tab_t	*ft = flent->fe_flow_tab;
9428275SEric Cheng 
9438275SEric Cheng 	if (ft == NULL) {
9448275SEric Cheng 		/*
9458275SEric Cheng 		 *  The flow hasn't yet been inserted into the table,
9468275SEric Cheng 		 * so only the caller knows about this flow
9478275SEric Cheng 		 */
9488558SGirish.Moodalbail@Sun.COM 		(void) strlcpy(flent->fe_flow_name, name, MAXFLOWNAMELEN);
9498275SEric Cheng 	} else {
9508275SEric Cheng 		ASSERT(MAC_PERIM_HELD((mac_handle_t)ft->ft_mip));
9518275SEric Cheng 	}
9528275SEric Cheng 
9538275SEric Cheng 	mutex_enter(&flent->fe_lock);
9548558SGirish.Moodalbail@Sun.COM 	(void) strlcpy(flent->fe_flow_name, name, MAXFLOWNAMELEN);
9558275SEric Cheng 	mutex_exit(&flent->fe_lock);
9568275SEric Cheng }
9578275SEric Cheng 
9588275SEric Cheng /*
9598275SEric Cheng  * Return the client-private cookie that was associated with
9608275SEric Cheng  * the flow when it was created.
9618275SEric Cheng  */
9628275SEric Cheng void *
mac_flow_get_client_cookie(flow_entry_t * flent)9638275SEric Cheng mac_flow_get_client_cookie(flow_entry_t *flent)
9648275SEric Cheng {
9658275SEric Cheng 	return (flent->fe_client_cookie);
9668275SEric Cheng }
9678275SEric Cheng 
9688275SEric Cheng /*
9698275SEric Cheng  * Forward declarations.
9708275SEric Cheng  */
9718275SEric Cheng static uint32_t	flow_l2_hash(flow_tab_t *, flow_state_t *);
97210616SSebastien.Roy@Sun.COM static uint32_t	flow_l2_hash_fe(flow_tab_t *, flow_entry_t *);
9738275SEric Cheng static int	flow_l2_accept(flow_tab_t *, flow_state_t *);
9748275SEric Cheng static uint32_t	flow_ether_hash(flow_tab_t *, flow_state_t *);
97510616SSebastien.Roy@Sun.COM static uint32_t	flow_ether_hash_fe(flow_tab_t *, flow_entry_t *);
9768275SEric Cheng static int	flow_ether_accept(flow_tab_t *, flow_state_t *);
9778275SEric Cheng 
9788275SEric Cheng /*
9798275SEric Cheng  * Create flow table.
9808275SEric Cheng  */
9818275SEric Cheng void
mac_flow_tab_create(flow_ops_t * ops,flow_mask_t mask,uint_t size,mac_impl_t * mip,flow_tab_t ** ftp)9828275SEric Cheng mac_flow_tab_create(flow_ops_t *ops, flow_mask_t mask, uint_t size,
9838275SEric Cheng     mac_impl_t *mip, flow_tab_t **ftp)
9848275SEric Cheng {
9858275SEric Cheng 	flow_tab_t	*ft;
9868275SEric Cheng 	flow_ops_t	*new_ops;
9878275SEric Cheng 
9888275SEric Cheng 	ft = kmem_cache_alloc(flow_tab_cache, KM_SLEEP);
9898275SEric Cheng 	bzero(ft, sizeof (*ft));
9908275SEric Cheng 
9918275SEric Cheng 	ft->ft_table = kmem_zalloc(size * sizeof (flow_entry_t *), KM_SLEEP);
9928275SEric Cheng 
9938275SEric Cheng 	/*
9948275SEric Cheng 	 * We make a copy of the ops vector instead of just pointing to it
9958275SEric Cheng 	 * because we might want to customize the ops vector on a per table
9968275SEric Cheng 	 * basis (e.g. for optimization).
9978275SEric Cheng 	 */
9988275SEric Cheng 	new_ops = &ft->ft_ops;
9998275SEric Cheng 	bcopy(ops, new_ops, sizeof (*ops));
10008275SEric Cheng 	ft->ft_mask = mask;
10018275SEric Cheng 	ft->ft_size = size;
10028275SEric Cheng 	ft->ft_mip = mip;
10038275SEric Cheng 
10048275SEric Cheng 	/*
100510616SSebastien.Roy@Sun.COM 	 * Optimizations for DL_ETHER media.
10068275SEric Cheng 	 */
10078275SEric Cheng 	if (mip->mi_info.mi_nativemedia == DL_ETHER) {
10088275SEric Cheng 		if (new_ops->fo_hash == flow_l2_hash)
10098275SEric Cheng 			new_ops->fo_hash = flow_ether_hash;
101010616SSebastien.Roy@Sun.COM 		if (new_ops->fo_hash_fe == flow_l2_hash_fe)
101110616SSebastien.Roy@Sun.COM 			new_ops->fo_hash_fe = flow_ether_hash_fe;
10128275SEric Cheng 		if (new_ops->fo_accept[0] == flow_l2_accept)
10138275SEric Cheng 			new_ops->fo_accept[0] = flow_ether_accept;
10148275SEric Cheng 	}
10158275SEric Cheng 	*ftp = ft;
10168275SEric Cheng }
10178275SEric Cheng 
10188275SEric Cheng void
mac_flow_l2tab_create(mac_impl_t * mip,flow_tab_t ** ftp)10198275SEric Cheng mac_flow_l2tab_create(mac_impl_t *mip, flow_tab_t **ftp)
10208275SEric Cheng {
10218275SEric Cheng 	mac_flow_tab_create(&flow_l2_ops, FLOW_LINK_DST | FLOW_LINK_VID,
10228275SEric Cheng 	    1024, mip, ftp);
10238275SEric Cheng }
10248275SEric Cheng 
10258275SEric Cheng /*
10268275SEric Cheng  * Destroy flow table.
10278275SEric Cheng  */
10288275SEric Cheng void
mac_flow_tab_destroy(flow_tab_t * ft)10298275SEric Cheng mac_flow_tab_destroy(flow_tab_t *ft)
10308275SEric Cheng {
10318275SEric Cheng 	if (ft == NULL)
10328275SEric Cheng 		return;
10338275SEric Cheng 
10348275SEric Cheng 	ASSERT(ft->ft_flow_count == 0);
10358275SEric Cheng 	kmem_free(ft->ft_table, ft->ft_size * sizeof (flow_entry_t *));
10368275SEric Cheng 	bzero(ft, sizeof (*ft));
10378275SEric Cheng 	kmem_cache_free(flow_tab_cache, ft);
10388275SEric Cheng }
10398275SEric Cheng 
10408275SEric Cheng /*
10418275SEric Cheng  * Add a new flow entry to the global flow hash table
10428275SEric Cheng  */
10438275SEric Cheng int
mac_flow_hash_add(flow_entry_t * flent)10448275SEric Cheng mac_flow_hash_add(flow_entry_t *flent)
10458275SEric Cheng {
10468275SEric Cheng 	int	err;
10478275SEric Cheng 
10488275SEric Cheng 	rw_enter(&flow_tab_lock, RW_WRITER);
10498275SEric Cheng 	err = mod_hash_insert(flow_hash,
10508275SEric Cheng 	    (mod_hash_key_t)flent->fe_flow_name, (mod_hash_val_t)flent);
10518275SEric Cheng 	if (err != 0) {
10528275SEric Cheng 		rw_exit(&flow_tab_lock);
10538275SEric Cheng 		return (EEXIST);
10548275SEric Cheng 	}
10558275SEric Cheng 	/* Mark as inserted into the global flow hash table */
10568275SEric Cheng 	FLOW_MARK(flent, FE_G_FLOW_HASH);
10578275SEric Cheng 	rw_exit(&flow_tab_lock);
10588275SEric Cheng 	return (err);
10598275SEric Cheng }
10608275SEric Cheng 
10618275SEric Cheng /*
10628275SEric Cheng  * Remove a flow entry from the global flow hash table
10638275SEric Cheng  */
10648275SEric Cheng void
mac_flow_hash_remove(flow_entry_t * flent)10658275SEric Cheng mac_flow_hash_remove(flow_entry_t *flent)
10668275SEric Cheng {
10678275SEric Cheng 	mod_hash_val_t	val;
10688275SEric Cheng 
10698275SEric Cheng 	rw_enter(&flow_tab_lock, RW_WRITER);
10708275SEric Cheng 	VERIFY(mod_hash_remove(flow_hash,
10718275SEric Cheng 	    (mod_hash_key_t)flent->fe_flow_name, &val) == 0);
10728275SEric Cheng 
10738275SEric Cheng 	/* Clear the mark that says inserted into the global flow hash table */
10748275SEric Cheng 	FLOW_UNMARK(flent, FE_G_FLOW_HASH);
10758275SEric Cheng 	rw_exit(&flow_tab_lock);
10768275SEric Cheng }
10778275SEric Cheng 
10788275SEric Cheng /*
10798275SEric Cheng  * Retrieve a flow entry from the global flow hash table.
10808275SEric Cheng  */
10818275SEric Cheng int
mac_flow_lookup_byname(char * name,flow_entry_t ** flentp)10828275SEric Cheng mac_flow_lookup_byname(char *name, flow_entry_t **flentp)
10838275SEric Cheng {
10848275SEric Cheng 	int		err;
10858275SEric Cheng 	flow_entry_t	*flent;
10868275SEric Cheng 
10878275SEric Cheng 	rw_enter(&flow_tab_lock, RW_READER);
10888275SEric Cheng 	err = mod_hash_find(flow_hash, (mod_hash_key_t)name,
10898275SEric Cheng 	    (mod_hash_val_t *)&flent);
10908275SEric Cheng 	if (err != 0) {
10918275SEric Cheng 		rw_exit(&flow_tab_lock);
10928275SEric Cheng 		return (ENOENT);
10938275SEric Cheng 	}
10948275SEric Cheng 	ASSERT(flent != NULL);
10958275SEric Cheng 	FLOW_USER_REFHOLD(flent);
10968275SEric Cheng 	rw_exit(&flow_tab_lock);
10978275SEric Cheng 
10988275SEric Cheng 	*flentp = flent;
10998275SEric Cheng 	return (0);
11008275SEric Cheng }
11018275SEric Cheng 
11028275SEric Cheng /*
11038275SEric Cheng  * Initialize or release mac client flows by walking the subflow table.
11048275SEric Cheng  * These are typically invoked during plumb/unplumb of links.
11058275SEric Cheng  */
11068275SEric Cheng 
11078275SEric Cheng static int
mac_link_init_flows_cb(flow_entry_t * flent,void * arg)11088275SEric Cheng mac_link_init_flows_cb(flow_entry_t *flent, void *arg)
11098275SEric Cheng {
11108275SEric Cheng 	mac_client_impl_t	*mcip = arg;
11118275SEric Cheng 
11128275SEric Cheng 	if (mac_link_flow_init(arg, flent) != 0) {
11138275SEric Cheng 		cmn_err(CE_WARN, "Failed to initialize flow '%s' on link '%s'",
11148275SEric Cheng 		    flent->fe_flow_name, mcip->mci_name);
11158275SEric Cheng 	} else {
11168275SEric Cheng 		FLOW_UNMARK(flent, FE_UF_NO_DATAPATH);
11178275SEric Cheng 	}
11188275SEric Cheng 	return (0);
11198275SEric Cheng }
11208275SEric Cheng 
11218275SEric Cheng void
mac_link_init_flows(mac_client_handle_t mch)11228275SEric Cheng mac_link_init_flows(mac_client_handle_t mch)
11238275SEric Cheng {
11248275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
11258275SEric Cheng 
11268275SEric Cheng 	(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
11278275SEric Cheng 	    mac_link_init_flows_cb, mcip);
11288275SEric Cheng 	/*
11298275SEric Cheng 	 * If mac client had subflow(s) configured before plumb, change
11308275SEric Cheng 	 * function to mac_rx_srs_subflow_process and in case of hardware
11318275SEric Cheng 	 * classification, disable polling.
11328275SEric Cheng 	 */
11338275SEric Cheng 	mac_client_update_classifier(mcip, B_TRUE);
11348275SEric Cheng 
11358275SEric Cheng }
11368275SEric Cheng 
11378275SEric Cheng boolean_t
mac_link_has_flows(mac_client_handle_t mch)11388275SEric Cheng mac_link_has_flows(mac_client_handle_t mch)
11398275SEric Cheng {
11408275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
11418275SEric Cheng 
11428275SEric Cheng 	if (!FLOW_TAB_EMPTY(mcip->mci_subflow_tab))
11438275SEric Cheng 		return (B_TRUE);
11448275SEric Cheng 
11458275SEric Cheng 	return (B_FALSE);
11468275SEric Cheng }
11478275SEric Cheng 
11488275SEric Cheng static int
mac_link_release_flows_cb(flow_entry_t * flent,void * arg)11498275SEric Cheng mac_link_release_flows_cb(flow_entry_t *flent, void *arg)
11508275SEric Cheng {
11518275SEric Cheng 	FLOW_MARK(flent, FE_UF_NO_DATAPATH);
11528275SEric Cheng 	mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
11538275SEric Cheng 	mac_link_flow_clean(arg, flent);
11548275SEric Cheng 	return (0);
11558275SEric Cheng }
11568275SEric Cheng 
11578275SEric Cheng void
mac_link_release_flows(mac_client_handle_t mch)11588275SEric Cheng mac_link_release_flows(mac_client_handle_t mch)
11598275SEric Cheng {
11608275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
11618275SEric Cheng 
11628275SEric Cheng 	/*
11638275SEric Cheng 	 * Change the mci_flent callback back to mac_rx_srs_process()
11648275SEric Cheng 	 * because flows are about to be deactivated.
11658275SEric Cheng 	 */
11668275SEric Cheng 	mac_client_update_classifier(mcip, B_FALSE);
11678275SEric Cheng 	(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
11688275SEric Cheng 	    mac_link_release_flows_cb, mcip);
11698275SEric Cheng }
11708275SEric Cheng 
11718275SEric Cheng void
mac_rename_flow(flow_entry_t * fep,const char * new_name)11728275SEric Cheng mac_rename_flow(flow_entry_t *fep, const char *new_name)
11738275SEric Cheng {
11748275SEric Cheng 	mac_flow_set_name(fep, new_name);
11758275SEric Cheng 	if (fep->fe_ksp != NULL) {
11768275SEric Cheng 		flow_stat_destroy(fep);
11778275SEric Cheng 		flow_stat_create(fep);
11788275SEric Cheng 	}
11798275SEric Cheng }
11808275SEric Cheng 
11818275SEric Cheng /*
11828275SEric Cheng  * mac_link_flow_init()
11838275SEric Cheng  * Internal flow interface used for allocating SRSs and related
11848275SEric Cheng  * data structures. Not meant to be used by mac clients.
11858275SEric Cheng  */
11868275SEric Cheng int
mac_link_flow_init(mac_client_handle_t mch,flow_entry_t * sub_flow)11878275SEric Cheng mac_link_flow_init(mac_client_handle_t mch, flow_entry_t *sub_flow)
11888275SEric Cheng {
11898275SEric Cheng 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
11908275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
11918275SEric Cheng 	int			err;
11928275SEric Cheng 
11938275SEric Cheng 	ASSERT(mch != NULL);
11948275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11958275SEric Cheng 
11968275SEric Cheng 	if ((err = mac_datapath_setup(mcip, sub_flow, SRST_FLOW)) != 0)
11978275SEric Cheng 		return (err);
11988275SEric Cheng 
11998275SEric Cheng 	sub_flow->fe_mcip = mcip;
12008275SEric Cheng 
12018275SEric Cheng 	return (0);
12028275SEric Cheng }
12038275SEric Cheng 
12048275SEric Cheng /*
12058275SEric Cheng  * mac_link_flow_add()
12068275SEric Cheng  * Used by flowadm(1m) or kernel mac clients for creating flows.
12078275SEric Cheng  */
12088275SEric Cheng int
mac_link_flow_add(datalink_id_t linkid,char * flow_name,flow_desc_t * flow_desc,mac_resource_props_t * mrp)12098275SEric Cheng mac_link_flow_add(datalink_id_t linkid, char *flow_name,
12108275SEric Cheng     flow_desc_t *flow_desc, mac_resource_props_t *mrp)
12118275SEric Cheng {
12128275SEric Cheng 	flow_entry_t		*flent = NULL;
12138275SEric Cheng 	int			err;
12148275SEric Cheng 	dls_dl_handle_t		dlh;
12158275SEric Cheng 	dls_link_t		*dlp;
12168275SEric Cheng 	boolean_t		link_held = B_FALSE;
12178275SEric Cheng 	boolean_t		hash_added = B_FALSE;
12188275SEric Cheng 	mac_perim_handle_t	mph;
12198275SEric Cheng 
12208275SEric Cheng 	err = mac_flow_lookup_byname(flow_name, &flent);
12218275SEric Cheng 	if (err == 0) {
12228275SEric Cheng 		FLOW_USER_REFRELE(flent);
12238275SEric Cheng 		return (EEXIST);
12248275SEric Cheng 	}
12258275SEric Cheng 
12268275SEric Cheng 	/*
12278275SEric Cheng 	 * First create a flow entry given the description provided
12288275SEric Cheng 	 * by the caller.
12298275SEric Cheng 	 */
12308275SEric Cheng 	err = mac_flow_create(flow_desc, mrp, flow_name, NULL,
12318275SEric Cheng 	    FLOW_USER | FLOW_OTHER, &flent);
12328275SEric Cheng 
12338275SEric Cheng 	if (err != 0)
12348275SEric Cheng 		return (err);
12358275SEric Cheng 
12368275SEric Cheng 	/*
12378275SEric Cheng 	 * We've got a local variable referencing this flow now, so we need
12388275SEric Cheng 	 * to hold it. We'll release this flow before returning.
12398275SEric Cheng 	 * All failures until we return will undo any action that may internally
12408275SEric Cheng 	 * held the flow, so the last REFRELE will assure a clean freeing
12418275SEric Cheng 	 * of resources.
12428275SEric Cheng 	 */
12438275SEric Cheng 	FLOW_REFHOLD(flent);
12448275SEric Cheng 
12458275SEric Cheng 	flent->fe_link_id = linkid;
12468275SEric Cheng 	FLOW_MARK(flent, FE_INCIPIENT);
12478275SEric Cheng 
12488275SEric Cheng 	err = mac_perim_enter_by_linkid(linkid, &mph);
12498275SEric Cheng 	if (err != 0) {
12508275SEric Cheng 		FLOW_FINAL_REFRELE(flent);
12518275SEric Cheng 		return (err);
12528275SEric Cheng 	}
12538275SEric Cheng 
12548275SEric Cheng 	/*
12558275SEric Cheng 	 * dls will eventually be merged with mac so it's ok
12568275SEric Cheng 	 * to call dls' internal functions.
12578275SEric Cheng 	 */
12588275SEric Cheng 	err = dls_devnet_hold_link(linkid, &dlh, &dlp);
12598275SEric Cheng 	if (err != 0)
12608275SEric Cheng 		goto bail;
12618275SEric Cheng 
12628275SEric Cheng 	link_held = B_TRUE;
12638275SEric Cheng 
12648275SEric Cheng 	/*
12658275SEric Cheng 	 * Add the flow to the global flow table, this table will be per
12668275SEric Cheng 	 * exclusive zone so each zone can have its own flow namespace.
12678275SEric Cheng 	 * RFE 6625651 will fix this.
12688275SEric Cheng 	 *
12698275SEric Cheng 	 */
12708275SEric Cheng 	if ((err = mac_flow_hash_add(flent)) != 0)
12718275SEric Cheng 		goto bail;
12728275SEric Cheng 
12738275SEric Cheng 	hash_added = B_TRUE;
12748275SEric Cheng 
12758275SEric Cheng 	/*
12768275SEric Cheng 	 * do not allow flows to be configured on an anchor VNIC
12778275SEric Cheng 	 */
12788275SEric Cheng 	if (mac_capab_get(dlp->dl_mh, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
12798275SEric Cheng 		err = ENOTSUP;
12808275SEric Cheng 		goto bail;
12818275SEric Cheng 	}
12828275SEric Cheng 
12838275SEric Cheng 	/*
12848275SEric Cheng 	 * Add the subflow to the subflow table. Also instantiate the flow
12858833SVenu.Iyer@Sun.COM 	 * in the mac if there is an active user (we check if the MAC client's
12868833SVenu.Iyer@Sun.COM 	 * datapath has been setup).
12878275SEric Cheng 	 */
12888833SVenu.Iyer@Sun.COM 	err = mac_flow_add_subflow(dlp->dl_mch, flent,
12898833SVenu.Iyer@Sun.COM 	    MCIP_DATAPATH_SETUP((mac_client_impl_t *)dlp->dl_mch));
12908275SEric Cheng 	if (err != 0)
12918275SEric Cheng 		goto bail;
12928275SEric Cheng 
12938275SEric Cheng 	FLOW_UNMARK(flent, FE_INCIPIENT);
12948275SEric Cheng 	dls_devnet_rele_link(dlh, dlp);
12958275SEric Cheng 	mac_perim_exit(mph);
12968275SEric Cheng 	return (0);
12978275SEric Cheng 
12988275SEric Cheng bail:
12998275SEric Cheng 	if (hash_added)
13008275SEric Cheng 		mac_flow_hash_remove(flent);
13018275SEric Cheng 
13028275SEric Cheng 	if (link_held)
13038275SEric Cheng 		dls_devnet_rele_link(dlh, dlp);
13048275SEric Cheng 
13058275SEric Cheng 	/*
13068275SEric Cheng 	 * Wait for any transient global flow hash refs to clear
13078275SEric Cheng 	 * and then release the creation reference on the flow
13088275SEric Cheng 	 */
13098275SEric Cheng 	mac_flow_wait(flent, FLOW_USER_REF);
13108275SEric Cheng 	FLOW_FINAL_REFRELE(flent);
13118275SEric Cheng 	mac_perim_exit(mph);
13128275SEric Cheng 	return (err);
13138275SEric Cheng }
13148275SEric Cheng 
13158275SEric Cheng /*
13168275SEric Cheng  * mac_link_flow_clean()
13178275SEric Cheng  * Internal flow interface used for freeing SRSs and related
13188275SEric Cheng  * data structures. Not meant to be used by mac clients.
13198275SEric Cheng  */
13208275SEric Cheng void
mac_link_flow_clean(mac_client_handle_t mch,flow_entry_t * sub_flow)13218275SEric Cheng mac_link_flow_clean(mac_client_handle_t mch, flow_entry_t *sub_flow)
13228275SEric Cheng {
13238275SEric Cheng 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
13248275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
13258275SEric Cheng 	boolean_t		last_subflow;
13268275SEric Cheng 
13278275SEric Cheng 	ASSERT(mch != NULL);
13288275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
13298275SEric Cheng 
13308275SEric Cheng 	/*
13318275SEric Cheng 	 * This sub flow entry may fail to be fully initialized by
13328275SEric Cheng 	 * mac_link_flow_init(). If so, simply return.
13338275SEric Cheng 	 */
13348275SEric Cheng 	if (sub_flow->fe_mcip == NULL)
13358275SEric Cheng 		return;
13368275SEric Cheng 
13378275SEric Cheng 	last_subflow = FLOW_TAB_EMPTY(mcip->mci_subflow_tab);
13388275SEric Cheng 	/*
13398275SEric Cheng 	 * Tear down the data path
13408275SEric Cheng 	 */
13418275SEric Cheng 	mac_datapath_teardown(mcip, sub_flow, SRST_FLOW);
13428275SEric Cheng 	sub_flow->fe_mcip = NULL;
13438275SEric Cheng 
13448275SEric Cheng 	/*
13458275SEric Cheng 	 * Delete the SRSs associated with this subflow. If this is being
13468275SEric Cheng 	 * driven by flowadm(1M) then the subflow will be deleted by
13478275SEric Cheng 	 * dls_rem_flow. However if this is a result of the interface being
13488275SEric Cheng 	 * unplumbed then the subflow itself won't be deleted.
13498275SEric Cheng 	 */
13508275SEric Cheng 	mac_flow_cleanup(sub_flow);
13518275SEric Cheng 
13528275SEric Cheng 	/*
13538275SEric Cheng 	 * If all the subflows are gone, renable some of the stuff
13548275SEric Cheng 	 * we disabled when adding a subflow, polling etc.
13558275SEric Cheng 	 */
13568275SEric Cheng 	if (last_subflow) {
13578275SEric Cheng 		/*
13588275SEric Cheng 		 * The subflow table itself is not protected by any locks or
13598275SEric Cheng 		 * refcnts. Hence quiesce the client upfront before clearing
13608275SEric Cheng 		 * mci_subflow_tab.
13618275SEric Cheng 		 */
13628275SEric Cheng 		mac_client_quiesce(mcip);
13638275SEric Cheng 		mac_client_update_classifier(mcip, B_FALSE);
13648275SEric Cheng 		mac_flow_tab_destroy(mcip->mci_subflow_tab);
13658275SEric Cheng 		mcip->mci_subflow_tab = NULL;
13668275SEric Cheng 		mac_client_restart(mcip);
13678275SEric Cheng 	}
13688275SEric Cheng }
13698275SEric Cheng 
13708275SEric Cheng /*
13718275SEric Cheng  * mac_link_flow_remove()
13728275SEric Cheng  * Used by flowadm(1m) or kernel mac clients for removing flows.
13738275SEric Cheng  */
13748275SEric Cheng int
mac_link_flow_remove(char * flow_name)13758275SEric Cheng mac_link_flow_remove(char *flow_name)
13768275SEric Cheng {
13778275SEric Cheng 	flow_entry_t		*flent;
13788275SEric Cheng 	mac_perim_handle_t	mph;
13798275SEric Cheng 	int			err;
13808275SEric Cheng 	datalink_id_t		linkid;
13818275SEric Cheng 
13828275SEric Cheng 	err = mac_flow_lookup_byname(flow_name, &flent);
13838275SEric Cheng 	if (err != 0)
13848275SEric Cheng 		return (err);
13858275SEric Cheng 
13868275SEric Cheng 	linkid = flent->fe_link_id;
13878275SEric Cheng 	FLOW_USER_REFRELE(flent);
13888275SEric Cheng 
13898275SEric Cheng 	/*
13908275SEric Cheng 	 * The perim must be acquired before acquiring any other references
13918275SEric Cheng 	 * to maintain the lock and perimeter hierarchy. Please note the
13928275SEric Cheng 	 * FLOW_REFRELE above.
13938275SEric Cheng 	 */
13948275SEric Cheng 	err = mac_perim_enter_by_linkid(linkid, &mph);
13958275SEric Cheng 	if (err != 0)
13968275SEric Cheng 		return (err);
13978275SEric Cheng 
13988275SEric Cheng 	/*
13998275SEric Cheng 	 * Note the second lookup of the flow, because a concurrent thread
14008275SEric Cheng 	 * may have removed it already while we were waiting to enter the
14018275SEric Cheng 	 * link's perimeter.
14028275SEric Cheng 	 */
14038275SEric Cheng 	err = mac_flow_lookup_byname(flow_name, &flent);
14048275SEric Cheng 	if (err != 0) {
14058275SEric Cheng 		mac_perim_exit(mph);
14068275SEric Cheng 		return (err);
14078275SEric Cheng 	}
14088275SEric Cheng 	FLOW_USER_REFRELE(flent);
14098275SEric Cheng 
14108275SEric Cheng 	/*
14118275SEric Cheng 	 * Remove the flow from the subflow table and deactivate the flow
14128275SEric Cheng 	 * by quiescing and removings its SRSs
14138275SEric Cheng 	 */
14148275SEric Cheng 	mac_flow_rem_subflow(flent);
14158275SEric Cheng 
14168275SEric Cheng 	/*
14178275SEric Cheng 	 * Finally, remove the flow from the global table.
14188275SEric Cheng 	 */
14198275SEric Cheng 	mac_flow_hash_remove(flent);
14208275SEric Cheng 
14218275SEric Cheng 	/*
14228275SEric Cheng 	 * Wait for any transient global flow hash refs to clear
14238275SEric Cheng 	 * and then release the creation reference on the flow
14248275SEric Cheng 	 */
14258275SEric Cheng 	mac_flow_wait(flent, FLOW_USER_REF);
14268275SEric Cheng 	FLOW_FINAL_REFRELE(flent);
14278275SEric Cheng 
14288275SEric Cheng 	mac_perim_exit(mph);
14298275SEric Cheng 
14308275SEric Cheng 	return (0);
14318275SEric Cheng }
14328275SEric Cheng 
14338275SEric Cheng /*
14348275SEric Cheng  * mac_link_flow_modify()
14358275SEric Cheng  * Modifies the properties of a flow identified by its name.
14368275SEric Cheng  */
14378275SEric Cheng int
mac_link_flow_modify(char * flow_name,mac_resource_props_t * mrp)14388275SEric Cheng mac_link_flow_modify(char *flow_name, mac_resource_props_t *mrp)
14398275SEric Cheng {
14408275SEric Cheng 	flow_entry_t		*flent;
14418275SEric Cheng 	mac_client_impl_t 	*mcip;
14428275SEric Cheng 	int			err = 0;
14438275SEric Cheng 	mac_perim_handle_t	mph;
14448275SEric Cheng 	datalink_id_t		linkid;
14458275SEric Cheng 	flow_tab_t		*flow_tab;
14468275SEric Cheng 
1447*11878SVenu.Iyer@Sun.COM 	err = mac_validate_props(NULL, mrp);
14488275SEric Cheng 	if (err != 0)
14498275SEric Cheng 		return (err);
14508275SEric Cheng 
14518275SEric Cheng 	err = mac_flow_lookup_byname(flow_name, &flent);
14528275SEric Cheng 	if (err != 0)
14538275SEric Cheng 		return (err);
14548275SEric Cheng 
14558275SEric Cheng 	linkid = flent->fe_link_id;
14568275SEric Cheng 	FLOW_USER_REFRELE(flent);
14578275SEric Cheng 
14588275SEric Cheng 	/*
14598275SEric Cheng 	 * The perim must be acquired before acquiring any other references
14608275SEric Cheng 	 * to maintain the lock and perimeter hierarchy. Please note the
14618275SEric Cheng 	 * FLOW_REFRELE above.
14628275SEric Cheng 	 */
14638275SEric Cheng 	err = mac_perim_enter_by_linkid(linkid, &mph);
14648275SEric Cheng 	if (err != 0)
14658275SEric Cheng 		return (err);
14668275SEric Cheng 
14678275SEric Cheng 	/*
14688275SEric Cheng 	 * Note the second lookup of the flow, because a concurrent thread
14698275SEric Cheng 	 * may have removed it already while we were waiting to enter the
14708275SEric Cheng 	 * link's perimeter.
14718275SEric Cheng 	 */
14728275SEric Cheng 	err = mac_flow_lookup_byname(flow_name, &flent);
14738275SEric Cheng 	if (err != 0) {
14748275SEric Cheng 		mac_perim_exit(mph);
14758275SEric Cheng 		return (err);
14768275SEric Cheng 	}
14778275SEric Cheng 	FLOW_USER_REFRELE(flent);
14788275SEric Cheng 
14798275SEric Cheng 	/*
14808275SEric Cheng 	 * If this flow is attached to a MAC client, then pass the request
14818275SEric Cheng 	 * along to the client.
14828275SEric Cheng 	 * Otherwise, just update the cached values.
14838275SEric Cheng 	 */
14848275SEric Cheng 	mcip = flent->fe_mcip;
14858275SEric Cheng 	mac_update_resources(mrp, &flent->fe_resource_props, B_TRUE);
14868275SEric Cheng 	if (mcip != NULL) {
14878275SEric Cheng 		if ((flow_tab = mcip->mci_subflow_tab) == NULL) {
14888275SEric Cheng 			err = ENOENT;
14898275SEric Cheng 		} else {
14908275SEric Cheng 			mac_flow_modify(flow_tab, flent, mrp);
14918275SEric Cheng 		}
14928275SEric Cheng 	} else {
14938275SEric Cheng 		(void) mac_flow_modify_props(flent, mrp);
14948275SEric Cheng 	}
14958275SEric Cheng 
14968275SEric Cheng done:
14978275SEric Cheng 	mac_perim_exit(mph);
14988275SEric Cheng 	return (err);
14998275SEric Cheng }
15008275SEric Cheng 
15018275SEric Cheng 
15028275SEric Cheng /*
15038275SEric Cheng  * State structure and misc functions used by mac_link_flow_walk().
15048275SEric Cheng  */
15058275SEric Cheng typedef struct {
15068275SEric Cheng 	int	(*ws_func)(mac_flowinfo_t *, void *);
15078275SEric Cheng 	void	*ws_arg;
15088275SEric Cheng } flow_walk_state_t;
15098275SEric Cheng 
15108275SEric Cheng static void
mac_link_flowinfo_copy(mac_flowinfo_t * finfop,flow_entry_t * flent)15118275SEric Cheng mac_link_flowinfo_copy(mac_flowinfo_t *finfop, flow_entry_t *flent)
15128275SEric Cheng {
15138558SGirish.Moodalbail@Sun.COM 	(void) strlcpy(finfop->fi_flow_name, flent->fe_flow_name,
15148558SGirish.Moodalbail@Sun.COM 	    MAXFLOWNAMELEN);
15158275SEric Cheng 	finfop->fi_link_id = flent->fe_link_id;
15168275SEric Cheng 	finfop->fi_flow_desc = flent->fe_flow_desc;
15178275SEric Cheng 	finfop->fi_resource_props = flent->fe_resource_props;
15188275SEric Cheng }
15198275SEric Cheng 
15208275SEric Cheng static int
mac_link_flow_walk_cb(flow_entry_t * flent,void * arg)15218275SEric Cheng mac_link_flow_walk_cb(flow_entry_t *flent, void *arg)
15228275SEric Cheng {
15238275SEric Cheng 	flow_walk_state_t	*statep = arg;
1524*11878SVenu.Iyer@Sun.COM 	mac_flowinfo_t		*finfo;
1525*11878SVenu.Iyer@Sun.COM 	int			err;
15268275SEric Cheng 
1527*11878SVenu.Iyer@Sun.COM 	finfo = kmem_zalloc(sizeof (*finfo), KM_SLEEP);
1528*11878SVenu.Iyer@Sun.COM 	mac_link_flowinfo_copy(finfo, flent);
1529*11878SVenu.Iyer@Sun.COM 	err = statep->ws_func(finfo, statep->ws_arg);
1530*11878SVenu.Iyer@Sun.COM 	kmem_free(finfo, sizeof (*finfo));
1531*11878SVenu.Iyer@Sun.COM 	return (err);
15328275SEric Cheng }
15338275SEric Cheng 
15348275SEric Cheng /*
15358275SEric Cheng  * mac_link_flow_walk()
15368275SEric Cheng  * Invokes callback 'func' for all flows belonging to the specified link.
15378275SEric Cheng  */
15388275SEric Cheng int
mac_link_flow_walk(datalink_id_t linkid,int (* func)(mac_flowinfo_t *,void *),void * arg)15398275SEric Cheng mac_link_flow_walk(datalink_id_t linkid,
15408275SEric Cheng     int (*func)(mac_flowinfo_t *, void *), void *arg)
15418275SEric Cheng {
15428275SEric Cheng 	mac_client_impl_t	*mcip;
15438275SEric Cheng 	mac_perim_handle_t	mph;
15448275SEric Cheng 	flow_walk_state_t	state;
15458275SEric Cheng 	dls_dl_handle_t		dlh;
15468275SEric Cheng 	dls_link_t		*dlp;
15478275SEric Cheng 	int			err;
15488275SEric Cheng 
15498275SEric Cheng 	err = mac_perim_enter_by_linkid(linkid, &mph);
15508275SEric Cheng 	if (err != 0)
15518275SEric Cheng 		return (err);
15528275SEric Cheng 
15538275SEric Cheng 	err = dls_devnet_hold_link(linkid, &dlh, &dlp);
15548275SEric Cheng 	if (err != 0) {
15558275SEric Cheng 		mac_perim_exit(mph);
15568275SEric Cheng 		return (err);
15578275SEric Cheng 	}
15588275SEric Cheng 
15598275SEric Cheng 	mcip = (mac_client_impl_t *)dlp->dl_mch;
15608275SEric Cheng 	state.ws_func = func;
15618275SEric Cheng 	state.ws_arg = arg;
15628275SEric Cheng 
15638275SEric Cheng 	err = mac_flow_walk_nolock(mcip->mci_subflow_tab,
15648275SEric Cheng 	    mac_link_flow_walk_cb, &state);
15658275SEric Cheng 
15668275SEric Cheng 	dls_devnet_rele_link(dlh, dlp);
15678275SEric Cheng 	mac_perim_exit(mph);
15688275SEric Cheng 	return (err);
15698275SEric Cheng }
15708275SEric Cheng 
15718275SEric Cheng /*
15728275SEric Cheng  * mac_link_flow_info()
15738275SEric Cheng  * Retrieves information about a specific flow.
15748275SEric Cheng  */
15758275SEric Cheng int
mac_link_flow_info(char * flow_name,mac_flowinfo_t * finfo)15768275SEric Cheng mac_link_flow_info(char *flow_name, mac_flowinfo_t *finfo)
15778275SEric Cheng {
15788275SEric Cheng 	flow_entry_t	*flent;
15798275SEric Cheng 	int		err;
15808275SEric Cheng 
15818275SEric Cheng 	err = mac_flow_lookup_byname(flow_name, &flent);
15828275SEric Cheng 	if (err != 0)
15838275SEric Cheng 		return (err);
15848275SEric Cheng 
15858275SEric Cheng 	mac_link_flowinfo_copy(finfo, flent);
15868275SEric Cheng 	FLOW_USER_REFRELE(flent);
15878275SEric Cheng 	return (0);
15888275SEric Cheng }
15898275SEric Cheng 
159010616SSebastien.Roy@Sun.COM /*
159110616SSebastien.Roy@Sun.COM  * Hash function macro that takes an Ethernet address and VLAN id as input.
159210616SSebastien.Roy@Sun.COM  */
159310616SSebastien.Roy@Sun.COM #define	HASH_ETHER_VID(a, v, s)	\
15948275SEric Cheng 	((((uint32_t)(a)[3] + (a)[4] + (a)[5]) ^ (v)) % (s))
15958275SEric Cheng 
159610616SSebastien.Roy@Sun.COM /*
159710616SSebastien.Roy@Sun.COM  * Generic layer-2 address hashing function that takes an address and address
159810616SSebastien.Roy@Sun.COM  * length as input.  This is the DJB hash function.
159910616SSebastien.Roy@Sun.COM  */
160010616SSebastien.Roy@Sun.COM static uint32_t
flow_l2_addrhash(uint8_t * addr,size_t addrlen,size_t htsize)160110616SSebastien.Roy@Sun.COM flow_l2_addrhash(uint8_t *addr, size_t addrlen, size_t htsize)
160210616SSebastien.Roy@Sun.COM {
160310616SSebastien.Roy@Sun.COM 	uint32_t	hash = 5381;
160410616SSebastien.Roy@Sun.COM 	size_t		i;
160510616SSebastien.Roy@Sun.COM 
160610616SSebastien.Roy@Sun.COM 	for (i = 0; i < addrlen; i++)
160710616SSebastien.Roy@Sun.COM 		hash = ((hash << 5) + hash) + addr[i];
160810616SSebastien.Roy@Sun.COM 	return (hash % htsize);
160910616SSebastien.Roy@Sun.COM }
161010616SSebastien.Roy@Sun.COM 
16118275SEric Cheng #define	PKT_TOO_SMALL(s, end) ((s)->fs_mp->b_wptr < (end))
16128275SEric Cheng 
16138833SVenu.Iyer@Sun.COM #define	CHECK_AND_ADJUST_START_PTR(s, start) {		\
16148833SVenu.Iyer@Sun.COM 	if ((s)->fs_mp->b_wptr == (start)) {		\
16158833SVenu.Iyer@Sun.COM 		mblk_t	*next = (s)->fs_mp->b_cont;	\
16168833SVenu.Iyer@Sun.COM 		if (next == NULL)			\
16178833SVenu.Iyer@Sun.COM 			return (EINVAL);		\
16188833SVenu.Iyer@Sun.COM 							\
16198833SVenu.Iyer@Sun.COM 		(s)->fs_mp = next;			\
16208833SVenu.Iyer@Sun.COM 		(start) = next->b_rptr;			\
16218833SVenu.Iyer@Sun.COM 	}						\
16228833SVenu.Iyer@Sun.COM }
16238833SVenu.Iyer@Sun.COM 
16248275SEric Cheng /* ARGSUSED */
16258275SEric Cheng static boolean_t
flow_l2_match(flow_tab_t * ft,flow_entry_t * flent,flow_state_t * s)16268275SEric Cheng flow_l2_match(flow_tab_t *ft, flow_entry_t *flent, flow_state_t *s)
16278275SEric Cheng {
16288275SEric Cheng 	flow_l2info_t		*l2 = &s->fs_l2info;
16298275SEric Cheng 	flow_desc_t		*fd = &flent->fe_flow_desc;
16308275SEric Cheng 
16318275SEric Cheng 	return (l2->l2_vid == fd->fd_vid &&
16328275SEric Cheng 	    bcmp(l2->l2_daddr, fd->fd_dst_mac, fd->fd_mac_len) == 0);
16338275SEric Cheng }
16348275SEric Cheng 
16358275SEric Cheng /*
16368275SEric Cheng  * Layer 2 hash function.
16378275SEric Cheng  * Must be paired with flow_l2_accept() within a set of flow_ops
16388275SEric Cheng  * because it assumes the dest address is already extracted.
16398275SEric Cheng  */
16408275SEric Cheng static uint32_t
flow_l2_hash(flow_tab_t * ft,flow_state_t * s)16418275SEric Cheng flow_l2_hash(flow_tab_t *ft, flow_state_t *s)
16428275SEric Cheng {
164310616SSebastien.Roy@Sun.COM 	return (flow_l2_addrhash(s->fs_l2info.l2_daddr,
164410616SSebastien.Roy@Sun.COM 	    ft->ft_mip->mi_type->mt_addr_length, ft->ft_size));
16458275SEric Cheng }
16468275SEric Cheng 
16478275SEric Cheng /*
16488275SEric Cheng  * This is the generic layer 2 accept function.
16498275SEric Cheng  * It makes use of mac_header_info() to extract the header length,
16508275SEric Cheng  * sap, vlan ID and destination address.
16518275SEric Cheng  */
16528275SEric Cheng static int
flow_l2_accept(flow_tab_t * ft,flow_state_t * s)16538275SEric Cheng flow_l2_accept(flow_tab_t *ft, flow_state_t *s)
16548275SEric Cheng {
16558275SEric Cheng 	boolean_t		is_ether;
16568275SEric Cheng 	flow_l2info_t		*l2 = &s->fs_l2info;
16578275SEric Cheng 	mac_header_info_t	mhi;
16588275SEric Cheng 	int			err;
16598275SEric Cheng 
16608275SEric Cheng 	is_ether = (ft->ft_mip->mi_info.mi_nativemedia == DL_ETHER);
16618275SEric Cheng 	if ((err = mac_header_info((mac_handle_t)ft->ft_mip,
16628275SEric Cheng 	    s->fs_mp, &mhi)) != 0) {
16638275SEric Cheng 		if (err == EINVAL)
16648275SEric Cheng 			err = ENOBUFS;
16658275SEric Cheng 
16668275SEric Cheng 		return (err);
16678275SEric Cheng 	}
16688275SEric Cheng 
16698275SEric Cheng 	l2->l2_start = s->fs_mp->b_rptr;
16708275SEric Cheng 	l2->l2_daddr = (uint8_t *)mhi.mhi_daddr;
16718275SEric Cheng 
16728275SEric Cheng 	if (is_ether && mhi.mhi_bindsap == ETHERTYPE_VLAN &&
16738275SEric Cheng 	    ((s->fs_flags & FLOW_IGNORE_VLAN) == 0)) {
16748275SEric Cheng 		struct ether_vlan_header	*evhp =
16758275SEric Cheng 		    (struct ether_vlan_header *)l2->l2_start;
16768275SEric Cheng 
16778275SEric Cheng 		if (PKT_TOO_SMALL(s, l2->l2_start + sizeof (*evhp)))
16788275SEric Cheng 			return (ENOBUFS);
16798275SEric Cheng 
16808275SEric Cheng 		l2->l2_sap = ntohs(evhp->ether_type);
16818275SEric Cheng 		l2->l2_vid = VLAN_ID(ntohs(evhp->ether_tci));
16828275SEric Cheng 		l2->l2_hdrsize = sizeof (*evhp);
16838275SEric Cheng 	} else {
16848275SEric Cheng 		l2->l2_sap = mhi.mhi_bindsap;
16858275SEric Cheng 		l2->l2_vid = 0;
16868275SEric Cheng 		l2->l2_hdrsize = (uint32_t)mhi.mhi_hdrsize;
16878275SEric Cheng 	}
16888275SEric Cheng 	return (0);
16898275SEric Cheng }
16908275SEric Cheng 
16918275SEric Cheng /*
16928275SEric Cheng  * flow_ether_hash()/accept() are optimized versions of flow_l2_hash()/
16938275SEric Cheng  * accept(). The notable difference is that dest address is now extracted
16948275SEric Cheng  * by hash() rather than by accept(). This saves a few memory references
16958275SEric Cheng  * for flow tables that do not care about mac addresses.
16968275SEric Cheng  */
16978275SEric Cheng static uint32_t
flow_ether_hash(flow_tab_t * ft,flow_state_t * s)16988275SEric Cheng flow_ether_hash(flow_tab_t *ft, flow_state_t *s)
16998275SEric Cheng {
17008275SEric Cheng 	flow_l2info_t			*l2 = &s->fs_l2info;
17018275SEric Cheng 	struct ether_vlan_header	*evhp;
17028275SEric Cheng 
17038275SEric Cheng 	evhp = (struct ether_vlan_header *)l2->l2_start;
17048275SEric Cheng 	l2->l2_daddr = evhp->ether_dhost.ether_addr_octet;
170510616SSebastien.Roy@Sun.COM 	return (HASH_ETHER_VID(l2->l2_daddr, l2->l2_vid, ft->ft_size));
170610616SSebastien.Roy@Sun.COM }
170710616SSebastien.Roy@Sun.COM 
170810616SSebastien.Roy@Sun.COM static uint32_t
flow_ether_hash_fe(flow_tab_t * ft,flow_entry_t * flent)170910616SSebastien.Roy@Sun.COM flow_ether_hash_fe(flow_tab_t *ft, flow_entry_t *flent)
171010616SSebastien.Roy@Sun.COM {
171110616SSebastien.Roy@Sun.COM 	flow_desc_t	*fd = &flent->fe_flow_desc;
171210616SSebastien.Roy@Sun.COM 
171310616SSebastien.Roy@Sun.COM 	ASSERT((fd->fd_mask & FLOW_LINK_VID) != 0 || fd->fd_vid == 0);
171410616SSebastien.Roy@Sun.COM 	return (HASH_ETHER_VID(fd->fd_dst_mac, fd->fd_vid, ft->ft_size));
17158275SEric Cheng }
17168275SEric Cheng 
17178275SEric Cheng /* ARGSUSED */
17188275SEric Cheng static int
flow_ether_accept(flow_tab_t * ft,flow_state_t * s)17198275SEric Cheng flow_ether_accept(flow_tab_t *ft, flow_state_t *s)
17208275SEric Cheng {
17218275SEric Cheng 	flow_l2info_t			*l2 = &s->fs_l2info;
17228275SEric Cheng 	struct ether_vlan_header	*evhp;
17238275SEric Cheng 	uint16_t			sap;
17248275SEric Cheng 
17258275SEric Cheng 	evhp = (struct ether_vlan_header *)s->fs_mp->b_rptr;
17268275SEric Cheng 	l2->l2_start = (uchar_t *)evhp;
17278275SEric Cheng 
17288275SEric Cheng 	if (PKT_TOO_SMALL(s, l2->l2_start + sizeof (struct ether_header)))
17298275SEric Cheng 		return (ENOBUFS);
17308275SEric Cheng 
17318275SEric Cheng 	if ((sap = ntohs(evhp->ether_tpid)) == ETHERTYPE_VLAN &&
17328275SEric Cheng 	    ((s->fs_flags & FLOW_IGNORE_VLAN) == 0)) {
17338275SEric Cheng 		if (PKT_TOO_SMALL(s, l2->l2_start + sizeof (*evhp)))
17348275SEric Cheng 			return (ENOBUFS);
17358275SEric Cheng 
17368275SEric Cheng 		l2->l2_sap = ntohs(evhp->ether_type);
17378275SEric Cheng 		l2->l2_vid = VLAN_ID(ntohs(evhp->ether_tci));
17388275SEric Cheng 		l2->l2_hdrsize = sizeof (struct ether_vlan_header);
17398275SEric Cheng 	} else {
17408275SEric Cheng 		l2->l2_sap = sap;
17418275SEric Cheng 		l2->l2_vid = 0;
17428275SEric Cheng 		l2->l2_hdrsize = sizeof (struct ether_header);
17438275SEric Cheng 	}
17448275SEric Cheng 	return (0);
17458275SEric Cheng }
17468275SEric Cheng 
17478275SEric Cheng /*
17488275SEric Cheng  * Validates a layer 2 flow entry.
17498275SEric Cheng  */
17508275SEric Cheng static int
flow_l2_accept_fe(flow_tab_t * ft,flow_entry_t * flent)17518275SEric Cheng flow_l2_accept_fe(flow_tab_t *ft, flow_entry_t *flent)
17528275SEric Cheng {
17538275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
17548275SEric Cheng 
17558275SEric Cheng 	/*
175610616SSebastien.Roy@Sun.COM 	 * Dest address is mandatory, and 0 length addresses are not yet
175710616SSebastien.Roy@Sun.COM 	 * supported.
17588275SEric Cheng 	 */
175910616SSebastien.Roy@Sun.COM 	if ((fd->fd_mask & FLOW_LINK_DST) == 0 || fd->fd_mac_len == 0)
17608275SEric Cheng 		return (EINVAL);
17618275SEric Cheng 
17628275SEric Cheng 	if ((fd->fd_mask & FLOW_LINK_VID) != 0) {
17638275SEric Cheng 		/*
17648275SEric Cheng 		 * VLAN flows are only supported over ethernet macs.
17658275SEric Cheng 		 */
17668275SEric Cheng 		if (ft->ft_mip->mi_info.mi_nativemedia != DL_ETHER)
17678275SEric Cheng 			return (EINVAL);
17688275SEric Cheng 
17698275SEric Cheng 		if (fd->fd_vid == 0)
17708275SEric Cheng 			return (EINVAL);
17718275SEric Cheng 
17728275SEric Cheng 	}
17738275SEric Cheng 	flent->fe_match = flow_l2_match;
17748275SEric Cheng 	return (0);
17758275SEric Cheng }
17768275SEric Cheng 
17778275SEric Cheng /*
17788275SEric Cheng  * Calculates hash index of flow entry.
17798275SEric Cheng  */
17808275SEric Cheng static uint32_t
flow_l2_hash_fe(flow_tab_t * ft,flow_entry_t * flent)17818275SEric Cheng flow_l2_hash_fe(flow_tab_t *ft, flow_entry_t *flent)
17828275SEric Cheng {
17838275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
17848275SEric Cheng 
178510616SSebastien.Roy@Sun.COM 	ASSERT((fd->fd_mask & FLOW_LINK_VID) == 0 && fd->fd_vid == 0);
178610616SSebastien.Roy@Sun.COM 	return (flow_l2_addrhash(fd->fd_dst_mac,
178710616SSebastien.Roy@Sun.COM 	    ft->ft_mip->mi_type->mt_addr_length, ft->ft_size));
17888275SEric Cheng }
17898275SEric Cheng 
17908275SEric Cheng /*
17918275SEric Cheng  * This is used for duplicate flow checking.
17928275SEric Cheng  */
17938275SEric Cheng /* ARGSUSED */
17948275SEric Cheng static boolean_t
flow_l2_match_fe(flow_tab_t * ft,flow_entry_t * f1,flow_entry_t * f2)17958275SEric Cheng flow_l2_match_fe(flow_tab_t *ft, flow_entry_t *f1, flow_entry_t *f2)
17968275SEric Cheng {
17978275SEric Cheng 	flow_desc_t	*fd1 = &f1->fe_flow_desc, *fd2 = &f2->fe_flow_desc;
17988275SEric Cheng 
17998275SEric Cheng 	ASSERT(fd1->fd_mac_len == fd2->fd_mac_len && fd1->fd_mac_len != 0);
18008275SEric Cheng 	return (bcmp(&fd1->fd_dst_mac, &fd2->fd_dst_mac,
18018275SEric Cheng 	    fd1->fd_mac_len) == 0 && fd1->fd_vid == fd2->fd_vid);
18028275SEric Cheng }
18038275SEric Cheng 
18048275SEric Cheng /*
18058275SEric Cheng  * Generic flow entry insertion function.
18068275SEric Cheng  * Used by flow tables that do not have ordering requirements.
18078275SEric Cheng  */
18088275SEric Cheng /* ARGSUSED */
18098275SEric Cheng static int
flow_generic_insert_fe(flow_tab_t * ft,flow_entry_t ** headp,flow_entry_t * flent)18108275SEric Cheng flow_generic_insert_fe(flow_tab_t *ft, flow_entry_t **headp,
18118275SEric Cheng     flow_entry_t *flent)
18128275SEric Cheng {
18138275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)ft->ft_mip));
18148275SEric Cheng 
18158275SEric Cheng 	if (*headp != NULL) {
18168275SEric Cheng 		ASSERT(flent->fe_next == NULL);
18178275SEric Cheng 		flent->fe_next = *headp;
18188275SEric Cheng 	}
18198275SEric Cheng 	*headp = flent;
18208275SEric Cheng 	return (0);
18218275SEric Cheng }
18228275SEric Cheng 
18238275SEric Cheng /*
18248275SEric Cheng  * IP version independent DSField matching function.
18258275SEric Cheng  */
18268275SEric Cheng /* ARGSUSED */
18278275SEric Cheng static boolean_t
flow_ip_dsfield_match(flow_tab_t * ft,flow_entry_t * flent,flow_state_t * s)18288275SEric Cheng flow_ip_dsfield_match(flow_tab_t *ft, flow_entry_t *flent, flow_state_t *s)
18298275SEric Cheng {
18308275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
18318275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
18328275SEric Cheng 
18338275SEric Cheng 	switch (l3info->l3_version) {
18348275SEric Cheng 	case IPV4_VERSION: {
18358275SEric Cheng 		ipha_t		*ipha = (ipha_t *)l3info->l3_start;
18368275SEric Cheng 
18378275SEric Cheng 		return ((ipha->ipha_type_of_service &
18388275SEric Cheng 		    fd->fd_dsfield_mask) == fd->fd_dsfield);
18398275SEric Cheng 	}
18408275SEric Cheng 	case IPV6_VERSION: {
18418275SEric Cheng 		ip6_t		*ip6h = (ip6_t *)l3info->l3_start;
18428275SEric Cheng 
18438275SEric Cheng 		return ((IPV6_FLOW_TCLASS(ip6h->ip6_vcf) &
18448275SEric Cheng 		    fd->fd_dsfield_mask) == fd->fd_dsfield);
18458275SEric Cheng 	}
18468275SEric Cheng 	default:
18478275SEric Cheng 		return (B_FALSE);
18488275SEric Cheng 	}
18498275SEric Cheng }
18508275SEric Cheng 
18518275SEric Cheng /*
18528275SEric Cheng  * IP v4 and v6 address matching.
18538275SEric Cheng  * The netmask only needs to be applied on the packet but not on the
18548275SEric Cheng  * flow_desc since fd_local_addr/fd_remote_addr are premasked subnets.
18558275SEric Cheng  */
18568275SEric Cheng 
18578275SEric Cheng /* ARGSUSED */
18588275SEric Cheng static boolean_t
flow_ip_v4_match(flow_tab_t * ft,flow_entry_t * flent,flow_state_t * s)18598275SEric Cheng flow_ip_v4_match(flow_tab_t *ft, flow_entry_t *flent, flow_state_t *s)
18608275SEric Cheng {
18618275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
18628275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
18638275SEric Cheng 	ipha_t		*ipha = (ipha_t *)l3info->l3_start;
18648275SEric Cheng 	in_addr_t	addr;
18658275SEric Cheng 
18668275SEric Cheng 	addr = (l3info->l3_dst_or_src ? ipha->ipha_dst : ipha->ipha_src);
18678275SEric Cheng 	if ((fd->fd_mask & FLOW_IP_LOCAL) != 0) {
18688275SEric Cheng 		return ((addr & V4_PART_OF_V6(fd->fd_local_netmask)) ==
18698275SEric Cheng 		    V4_PART_OF_V6(fd->fd_local_addr));
18708275SEric Cheng 	}
18718275SEric Cheng 	return ((addr & V4_PART_OF_V6(fd->fd_remote_netmask)) ==
18728275SEric Cheng 	    V4_PART_OF_V6(fd->fd_remote_addr));
18738275SEric Cheng }
18748275SEric Cheng 
18758275SEric Cheng /* ARGSUSED */
18768275SEric Cheng static boolean_t
flow_ip_v6_match(flow_tab_t * ft,flow_entry_t * flent,flow_state_t * s)18778275SEric Cheng flow_ip_v6_match(flow_tab_t *ft, flow_entry_t *flent, flow_state_t *s)
18788275SEric Cheng {
18798275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
18808275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
18818275SEric Cheng 	ip6_t		*ip6h = (ip6_t *)l3info->l3_start;
18828275SEric Cheng 	in6_addr_t	*addrp;
18838275SEric Cheng 
18848275SEric Cheng 	addrp = (l3info->l3_dst_or_src ? &ip6h->ip6_dst : &ip6h->ip6_src);
18858275SEric Cheng 	if ((fd->fd_mask & FLOW_IP_LOCAL) != 0) {
18868275SEric Cheng 		return (V6_MASK_EQ(*addrp, fd->fd_local_netmask,
18878275SEric Cheng 		    fd->fd_local_addr));
18888275SEric Cheng 	}
18898275SEric Cheng 	return (V6_MASK_EQ(*addrp, fd->fd_remote_netmask, fd->fd_remote_addr));
18908275SEric Cheng }
18918275SEric Cheng 
18928275SEric Cheng /* ARGSUSED */
18938275SEric Cheng static boolean_t
flow_ip_proto_match(flow_tab_t * ft,flow_entry_t * flent,flow_state_t * s)18948275SEric Cheng flow_ip_proto_match(flow_tab_t *ft, flow_entry_t *flent, flow_state_t *s)
18958275SEric Cheng {
18968275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
18978275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
18988275SEric Cheng 
18998275SEric Cheng 	return (l3info->l3_protocol == fd->fd_protocol);
19008275SEric Cheng }
19018275SEric Cheng 
19028275SEric Cheng static uint32_t
flow_ip_hash(flow_tab_t * ft,flow_state_t * s)19038275SEric Cheng flow_ip_hash(flow_tab_t *ft, flow_state_t *s)
19048275SEric Cheng {
19058275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
19068275SEric Cheng 	flow_mask_t	mask = ft->ft_mask;
19078275SEric Cheng 
19088275SEric Cheng 	if ((mask & FLOW_IP_LOCAL) != 0) {
19098275SEric Cheng 		l3info->l3_dst_or_src = ((s->fs_flags & FLOW_INBOUND) != 0);
19108275SEric Cheng 	} else if ((mask & FLOW_IP_REMOTE) != 0) {
19118275SEric Cheng 		l3info->l3_dst_or_src = ((s->fs_flags & FLOW_OUTBOUND) != 0);
19128275SEric Cheng 	} else if ((mask & FLOW_IP_DSFIELD) != 0) {
19138275SEric Cheng 		/*
19148275SEric Cheng 		 * DSField flents are arranged as a single list.
19158275SEric Cheng 		 */
19168275SEric Cheng 		return (0);
19178275SEric Cheng 	}
19188275SEric Cheng 	/*
19198275SEric Cheng 	 * IP addr flents are hashed into two lists, v4 or v6.
19208275SEric Cheng 	 */
19218275SEric Cheng 	ASSERT(ft->ft_size >= 2);
19228275SEric Cheng 	return ((l3info->l3_version == IPV4_VERSION) ? 0 : 1);
19238275SEric Cheng }
19248275SEric Cheng 
19258275SEric Cheng static uint32_t
flow_ip_proto_hash(flow_tab_t * ft,flow_state_t * s)19268275SEric Cheng flow_ip_proto_hash(flow_tab_t *ft, flow_state_t *s)
19278275SEric Cheng {
19288275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
19298275SEric Cheng 
19308275SEric Cheng 	return (l3info->l3_protocol % ft->ft_size);
19318275SEric Cheng }
19328275SEric Cheng 
19338275SEric Cheng /* ARGSUSED */
19348275SEric Cheng static int
flow_ip_accept(flow_tab_t * ft,flow_state_t * s)19358275SEric Cheng flow_ip_accept(flow_tab_t *ft, flow_state_t *s)
19368275SEric Cheng {
19378275SEric Cheng 	flow_l2info_t	*l2info = &s->fs_l2info;
19388275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
19398275SEric Cheng 	uint16_t	sap = l2info->l2_sap;
19408275SEric Cheng 	uchar_t		*l3_start;
19418275SEric Cheng 
19428833SVenu.Iyer@Sun.COM 	l3_start = l2info->l2_start + l2info->l2_hdrsize;
19438833SVenu.Iyer@Sun.COM 
19448833SVenu.Iyer@Sun.COM 	/*
19458833SVenu.Iyer@Sun.COM 	 * Adjust start pointer if we're at the end of an mblk.
19468833SVenu.Iyer@Sun.COM 	 */
19478833SVenu.Iyer@Sun.COM 	CHECK_AND_ADJUST_START_PTR(s, l3_start);
19488833SVenu.Iyer@Sun.COM 
19498833SVenu.Iyer@Sun.COM 	l3info->l3_start = l3_start;
19508275SEric Cheng 	if (!OK_32PTR(l3_start))
19518275SEric Cheng 		return (EINVAL);
19528275SEric Cheng 
19538275SEric Cheng 	switch (sap) {
19548275SEric Cheng 	case ETHERTYPE_IP: {
19558275SEric Cheng 		ipha_t	*ipha = (ipha_t *)l3_start;
19568275SEric Cheng 
19578275SEric Cheng 		if (PKT_TOO_SMALL(s, l3_start + IP_SIMPLE_HDR_LENGTH))
19588275SEric Cheng 			return (ENOBUFS);
19598275SEric Cheng 
19608275SEric Cheng 		l3info->l3_hdrsize = IPH_HDR_LENGTH(ipha);
19618275SEric Cheng 		l3info->l3_protocol = ipha->ipha_protocol;
19628275SEric Cheng 		l3info->l3_version = IPV4_VERSION;
19638275SEric Cheng 		l3info->l3_fragmented =
19648275SEric Cheng 		    IS_V4_FRAGMENT(ipha->ipha_fragment_offset_and_flags);
19658275SEric Cheng 		break;
19668275SEric Cheng 	}
19678275SEric Cheng 	case ETHERTYPE_IPV6: {
1968*11878SVenu.Iyer@Sun.COM 		ip6_t		*ip6h = (ip6_t *)l3_start;
1969*11878SVenu.Iyer@Sun.COM 		ip6_frag_t	*frag = NULL;
1970*11878SVenu.Iyer@Sun.COM 		uint16_t	ip6_hdrlen;
1971*11878SVenu.Iyer@Sun.COM 		uint8_t		nexthdr;
19728275SEric Cheng 
1973*11878SVenu.Iyer@Sun.COM 		if (!mac_ip_hdr_length_v6(ip6h, s->fs_mp->b_wptr, &ip6_hdrlen,
1974*11878SVenu.Iyer@Sun.COM 		    &nexthdr, &frag)) {
19758275SEric Cheng 			return (ENOBUFS);
19768275SEric Cheng 		}
19778275SEric Cheng 		l3info->l3_hdrsize = ip6_hdrlen;
19788275SEric Cheng 		l3info->l3_protocol = nexthdr;
19798275SEric Cheng 		l3info->l3_version = IPV6_VERSION;
1980*11878SVenu.Iyer@Sun.COM 		l3info->l3_fragmented = (frag != NULL);
19818275SEric Cheng 		break;
19828275SEric Cheng 	}
19838275SEric Cheng 	default:
19848275SEric Cheng 		return (EINVAL);
19858275SEric Cheng 	}
19868275SEric Cheng 	return (0);
19878275SEric Cheng }
19888275SEric Cheng 
19898275SEric Cheng /* ARGSUSED */
19908275SEric Cheng static int
flow_ip_proto_accept_fe(flow_tab_t * ft,flow_entry_t * flent)19918275SEric Cheng flow_ip_proto_accept_fe(flow_tab_t *ft, flow_entry_t *flent)
19928275SEric Cheng {
19938275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
19948275SEric Cheng 
19958275SEric Cheng 	switch (fd->fd_protocol) {
19968275SEric Cheng 	case IPPROTO_TCP:
19978275SEric Cheng 	case IPPROTO_UDP:
19988275SEric Cheng 	case IPPROTO_SCTP:
19998275SEric Cheng 	case IPPROTO_ICMP:
20008275SEric Cheng 	case IPPROTO_ICMPV6:
20018275SEric Cheng 		flent->fe_match = flow_ip_proto_match;
20028275SEric Cheng 		return (0);
20038275SEric Cheng 	default:
20048275SEric Cheng 		return (EINVAL);
20058275SEric Cheng 	}
20068275SEric Cheng }
20078275SEric Cheng 
20088275SEric Cheng /* ARGSUSED */
20098275SEric Cheng static int
flow_ip_accept_fe(flow_tab_t * ft,flow_entry_t * flent)20108275SEric Cheng flow_ip_accept_fe(flow_tab_t *ft, flow_entry_t *flent)
20118275SEric Cheng {
20128275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
20138275SEric Cheng 	flow_mask_t	mask;
20148275SEric Cheng 	uint8_t		version;
20158275SEric Cheng 	in6_addr_t	*addr, *netmask;
20168275SEric Cheng 
20178275SEric Cheng 	/*
20188275SEric Cheng 	 * DSField does not require a IP version.
20198275SEric Cheng 	 */
20208275SEric Cheng 	if (fd->fd_mask == FLOW_IP_DSFIELD) {
20218275SEric Cheng 		if (fd->fd_dsfield_mask == 0)
20228275SEric Cheng 			return (EINVAL);
20238275SEric Cheng 
20248275SEric Cheng 		flent->fe_match = flow_ip_dsfield_match;
20258275SEric Cheng 		return (0);
20268275SEric Cheng 	}
20278275SEric Cheng 
20288275SEric Cheng 	/*
20298275SEric Cheng 	 * IP addresses must come with a version to avoid ambiguity.
20308275SEric Cheng 	 */
20318275SEric Cheng 	if ((fd->fd_mask & FLOW_IP_VERSION) == 0)
20328275SEric Cheng 		return (EINVAL);
20338275SEric Cheng 
20348275SEric Cheng 	version = fd->fd_ipversion;
20358275SEric Cheng 	if (version != IPV4_VERSION && version != IPV6_VERSION)
20368275SEric Cheng 		return (EINVAL);
20378275SEric Cheng 
20388275SEric Cheng 	mask = fd->fd_mask & ~FLOW_IP_VERSION;
20398275SEric Cheng 	switch (mask) {
20408275SEric Cheng 	case FLOW_IP_LOCAL:
20418275SEric Cheng 		addr = &fd->fd_local_addr;
20428275SEric Cheng 		netmask = &fd->fd_local_netmask;
20438275SEric Cheng 		break;
20448275SEric Cheng 	case FLOW_IP_REMOTE:
20458275SEric Cheng 		addr = &fd->fd_remote_addr;
20468275SEric Cheng 		netmask = &fd->fd_remote_netmask;
20478275SEric Cheng 		break;
20488275SEric Cheng 	default:
20498275SEric Cheng 		return (EINVAL);
20508275SEric Cheng 	}
20518275SEric Cheng 
20528275SEric Cheng 	/*
20538275SEric Cheng 	 * Apply netmask onto specified address.
20548275SEric Cheng 	 */
20558275SEric Cheng 	V6_MASK_COPY(*addr, *netmask, *addr);
20568275SEric Cheng 	if (version == IPV4_VERSION) {
20578275SEric Cheng 		ipaddr_t	v4addr = V4_PART_OF_V6((*addr));
20588275SEric Cheng 		ipaddr_t	v4mask = V4_PART_OF_V6((*netmask));
20598275SEric Cheng 
20608275SEric Cheng 		if (v4addr == 0 || v4mask == 0)
20618275SEric Cheng 			return (EINVAL);
20628275SEric Cheng 		flent->fe_match = flow_ip_v4_match;
20638275SEric Cheng 	} else {
20648275SEric Cheng 		if (IN6_IS_ADDR_UNSPECIFIED(addr) ||
20658275SEric Cheng 		    IN6_IS_ADDR_UNSPECIFIED(netmask))
20668275SEric Cheng 			return (EINVAL);
20678275SEric Cheng 		flent->fe_match = flow_ip_v6_match;
20688275SEric Cheng 	}
20698275SEric Cheng 	return (0);
20708275SEric Cheng }
20718275SEric Cheng 
20728275SEric Cheng static uint32_t
flow_ip_proto_hash_fe(flow_tab_t * ft,flow_entry_t * flent)20738275SEric Cheng flow_ip_proto_hash_fe(flow_tab_t *ft, flow_entry_t *flent)
20748275SEric Cheng {
20758275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
20768275SEric Cheng 
20778275SEric Cheng 	return (fd->fd_protocol % ft->ft_size);
20788275SEric Cheng }
20798275SEric Cheng 
20808275SEric Cheng static uint32_t
flow_ip_hash_fe(flow_tab_t * ft,flow_entry_t * flent)20818275SEric Cheng flow_ip_hash_fe(flow_tab_t *ft, flow_entry_t *flent)
20828275SEric Cheng {
20838275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
20848275SEric Cheng 
20858275SEric Cheng 	/*
20868275SEric Cheng 	 * DSField flents are arranged as a single list.
20878275SEric Cheng 	 */
20888275SEric Cheng 	if ((fd->fd_mask & FLOW_IP_DSFIELD) != 0)
20898275SEric Cheng 		return (0);
20908275SEric Cheng 
20918275SEric Cheng 	/*
20928275SEric Cheng 	 * IP addr flents are hashed into two lists, v4 or v6.
20938275SEric Cheng 	 */
20948275SEric Cheng 	ASSERT(ft->ft_size >= 2);
20958275SEric Cheng 	return ((fd->fd_ipversion == IPV4_VERSION) ? 0 : 1);
20968275SEric Cheng }
20978275SEric Cheng 
20988275SEric Cheng /* ARGSUSED */
20998275SEric Cheng static boolean_t
flow_ip_proto_match_fe(flow_tab_t * ft,flow_entry_t * f1,flow_entry_t * f2)21008275SEric Cheng flow_ip_proto_match_fe(flow_tab_t *ft, flow_entry_t *f1, flow_entry_t *f2)
21018275SEric Cheng {
21028275SEric Cheng 	flow_desc_t	*fd1 = &f1->fe_flow_desc, *fd2 = &f2->fe_flow_desc;
21038275SEric Cheng 
21048275SEric Cheng 	return (fd1->fd_protocol == fd2->fd_protocol);
21058275SEric Cheng }
21068275SEric Cheng 
21078275SEric Cheng /* ARGSUSED */
21088275SEric Cheng static boolean_t
flow_ip_match_fe(flow_tab_t * ft,flow_entry_t * f1,flow_entry_t * f2)21098275SEric Cheng flow_ip_match_fe(flow_tab_t *ft, flow_entry_t *f1, flow_entry_t *f2)
21108275SEric Cheng {
21118275SEric Cheng 	flow_desc_t	*fd1 = &f1->fe_flow_desc, *fd2 = &f2->fe_flow_desc;
21128275SEric Cheng 	in6_addr_t	*a1, *m1, *a2, *m2;
21138275SEric Cheng 
21148275SEric Cheng 	ASSERT(fd1->fd_mask == fd2->fd_mask);
21158275SEric Cheng 	if (fd1->fd_mask == FLOW_IP_DSFIELD) {
21168275SEric Cheng 		return (fd1->fd_dsfield == fd2->fd_dsfield &&
21178275SEric Cheng 		    fd1->fd_dsfield_mask == fd2->fd_dsfield_mask);
21188275SEric Cheng 	}
21198275SEric Cheng 
21208275SEric Cheng 	/*
21218275SEric Cheng 	 * flow_ip_accept_fe() already validated the version.
21228275SEric Cheng 	 */
21238275SEric Cheng 	ASSERT((fd1->fd_mask & FLOW_IP_VERSION) != 0);
21248275SEric Cheng 	if (fd1->fd_ipversion != fd2->fd_ipversion)
21258275SEric Cheng 		return (B_FALSE);
21268275SEric Cheng 
21278275SEric Cheng 	switch (fd1->fd_mask & ~FLOW_IP_VERSION) {
21288275SEric Cheng 	case FLOW_IP_LOCAL:
21298275SEric Cheng 		a1 = &fd1->fd_local_addr;
21308275SEric Cheng 		m1 = &fd1->fd_local_netmask;
21318275SEric Cheng 		a2 = &fd2->fd_local_addr;
21328275SEric Cheng 		m2 = &fd2->fd_local_netmask;
21338275SEric Cheng 		break;
21348275SEric Cheng 	case FLOW_IP_REMOTE:
21358275SEric Cheng 		a1 = &fd1->fd_remote_addr;
21368275SEric Cheng 		m1 = &fd1->fd_remote_netmask;
21378275SEric Cheng 		a2 = &fd2->fd_remote_addr;
21388275SEric Cheng 		m2 = &fd2->fd_remote_netmask;
21398275SEric Cheng 		break;
21408275SEric Cheng 	default:
21418275SEric Cheng 		/*
21428275SEric Cheng 		 * This is unreachable given the checks in
21438275SEric Cheng 		 * flow_ip_accept_fe().
21448275SEric Cheng 		 */
21458275SEric Cheng 		return (B_FALSE);
21468275SEric Cheng 	}
21478275SEric Cheng 
21488275SEric Cheng 	if (fd1->fd_ipversion == IPV4_VERSION) {
21498275SEric Cheng 		return (V4_PART_OF_V6((*a1)) == V4_PART_OF_V6((*a2)) &&
21508275SEric Cheng 		    V4_PART_OF_V6((*m1)) == V4_PART_OF_V6((*m2)));
21518275SEric Cheng 
21528275SEric Cheng 	} else {
21538275SEric Cheng 		return (IN6_ARE_ADDR_EQUAL(a1, a2) &&
21548275SEric Cheng 		    IN6_ARE_ADDR_EQUAL(m1, m2));
21558275SEric Cheng 	}
21568275SEric Cheng }
21578275SEric Cheng 
21588275SEric Cheng static int
flow_ip_mask2plen(in6_addr_t * v6mask)21598275SEric Cheng flow_ip_mask2plen(in6_addr_t *v6mask)
21608275SEric Cheng {
21618275SEric Cheng 	int		bits;
21628275SEric Cheng 	int		plen = IPV6_ABITS;
21638275SEric Cheng 	int		i;
21648275SEric Cheng 
21658275SEric Cheng 	for (i = 3; i >= 0; i--) {
21668275SEric Cheng 		if (v6mask->s6_addr32[i] == 0) {
21678275SEric Cheng 			plen -= 32;
21688275SEric Cheng 			continue;
21698275SEric Cheng 		}
21708275SEric Cheng 		bits = ffs(ntohl(v6mask->s6_addr32[i])) - 1;
21718275SEric Cheng 		if (bits == 0)
21728275SEric Cheng 			break;
21738275SEric Cheng 		plen -= bits;
21748275SEric Cheng 	}
21758275SEric Cheng 	return (plen);
21768275SEric Cheng }
21778275SEric Cheng 
21788275SEric Cheng /* ARGSUSED */
21798275SEric Cheng static int
flow_ip_insert_fe(flow_tab_t * ft,flow_entry_t ** headp,flow_entry_t * flent)21808275SEric Cheng flow_ip_insert_fe(flow_tab_t *ft, flow_entry_t **headp,
21818275SEric Cheng     flow_entry_t *flent)
21828275SEric Cheng {
21838275SEric Cheng 	flow_entry_t	**p = headp;
21848275SEric Cheng 	flow_desc_t	*fd0, *fd;
21858275SEric Cheng 	in6_addr_t	*m0, *m;
21868275SEric Cheng 	int		plen0, plen;
21878275SEric Cheng 
21888275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)ft->ft_mip));
21898275SEric Cheng 
21908275SEric Cheng 	/*
21918275SEric Cheng 	 * No special ordering needed for dsfield.
21928275SEric Cheng 	 */
21938275SEric Cheng 	fd0 = &flent->fe_flow_desc;
21948275SEric Cheng 	if ((fd0->fd_mask & FLOW_IP_DSFIELD) != 0) {
21958275SEric Cheng 		if (*p != NULL) {
21968275SEric Cheng 			ASSERT(flent->fe_next == NULL);
21978275SEric Cheng 			flent->fe_next = *p;
21988275SEric Cheng 		}
21998275SEric Cheng 		*p = flent;
22008275SEric Cheng 		return (0);
22018275SEric Cheng 	}
22028275SEric Cheng 
22038275SEric Cheng 	/*
22048275SEric Cheng 	 * IP address flows are arranged in descending prefix length order.
22058275SEric Cheng 	 */
22068275SEric Cheng 	m0 = ((fd0->fd_mask & FLOW_IP_LOCAL) != 0) ?
22078275SEric Cheng 	    &fd0->fd_local_netmask : &fd0->fd_remote_netmask;
22088275SEric Cheng 	plen0 = flow_ip_mask2plen(m0);
22098275SEric Cheng 	ASSERT(plen0 != 0);
22108275SEric Cheng 
22118275SEric Cheng 	for (; *p != NULL; p = &(*p)->fe_next) {
22128275SEric Cheng 		fd = &(*p)->fe_flow_desc;
22138275SEric Cheng 
22148275SEric Cheng 		/*
22158275SEric Cheng 		 * Normally a dsfield flent shouldn't end up on the same
22168275SEric Cheng 		 * list as an IP address because flow tables are (for now)
22178275SEric Cheng 		 * disjoint. If we decide to support both IP and dsfield
22188275SEric Cheng 		 * in the same table in the future, this check will allow
22198275SEric Cheng 		 * for that.
22208275SEric Cheng 		 */
22218275SEric Cheng 		if ((fd->fd_mask & FLOW_IP_DSFIELD) != 0)
22228275SEric Cheng 			continue;
22238275SEric Cheng 
22248275SEric Cheng 		/*
22258275SEric Cheng 		 * We also allow for the mixing of local and remote address
22268275SEric Cheng 		 * flents within one list.
22278275SEric Cheng 		 */
22288275SEric Cheng 		m = ((fd->fd_mask & FLOW_IP_LOCAL) != 0) ?
22298275SEric Cheng 		    &fd->fd_local_netmask : &fd->fd_remote_netmask;
22308275SEric Cheng 		plen = flow_ip_mask2plen(m);
22318275SEric Cheng 
22328275SEric Cheng 		if (plen <= plen0)
22338275SEric Cheng 			break;
22348275SEric Cheng 	}
22358275SEric Cheng 	if (*p != NULL) {
22368275SEric Cheng 		ASSERT(flent->fe_next == NULL);
22378275SEric Cheng 		flent->fe_next = *p;
22388275SEric Cheng 	}
22398275SEric Cheng 	*p = flent;
22408275SEric Cheng 	return (0);
22418275SEric Cheng }
22428275SEric Cheng 
22438275SEric Cheng /*
22448275SEric Cheng  * Transport layer protocol and port matching functions.
22458275SEric Cheng  */
22468275SEric Cheng 
22478275SEric Cheng /* ARGSUSED */
22488275SEric Cheng static boolean_t
flow_transport_lport_match(flow_tab_t * ft,flow_entry_t * flent,flow_state_t * s)22498275SEric Cheng flow_transport_lport_match(flow_tab_t *ft, flow_entry_t *flent, flow_state_t *s)
22508275SEric Cheng {
22518275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
22528275SEric Cheng 	flow_l4info_t	*l4info = &s->fs_l4info;
22538275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
22548275SEric Cheng 
22558275SEric Cheng 	return (fd->fd_protocol == l3info->l3_protocol &&
22568275SEric Cheng 	    fd->fd_local_port == l4info->l4_hash_port);
22578275SEric Cheng }
22588275SEric Cheng 
22598275SEric Cheng /* ARGSUSED */
22608275SEric Cheng static boolean_t
flow_transport_rport_match(flow_tab_t * ft,flow_entry_t * flent,flow_state_t * s)22618275SEric Cheng flow_transport_rport_match(flow_tab_t *ft, flow_entry_t *flent, flow_state_t *s)
22628275SEric Cheng {
22638275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
22648275SEric Cheng 	flow_l4info_t	*l4info = &s->fs_l4info;
22658275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
22668275SEric Cheng 
22678275SEric Cheng 	return (fd->fd_protocol == l3info->l3_protocol &&
22688275SEric Cheng 	    fd->fd_remote_port == l4info->l4_hash_port);
22698275SEric Cheng }
22708275SEric Cheng 
22718275SEric Cheng /*
22728275SEric Cheng  * Transport hash function.
22738275SEric Cheng  * Since we only support either local or remote port flows,
22748275SEric Cheng  * we only need to extract one of the ports to be used for
22758275SEric Cheng  * matching.
22768275SEric Cheng  */
22778275SEric Cheng static uint32_t
flow_transport_hash(flow_tab_t * ft,flow_state_t * s)22788275SEric Cheng flow_transport_hash(flow_tab_t *ft, flow_state_t *s)
22798275SEric Cheng {
22808275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
22818275SEric Cheng 	flow_l4info_t	*l4info = &s->fs_l4info;
22828275SEric Cheng 	uint8_t		proto = l3info->l3_protocol;
22838275SEric Cheng 	boolean_t	dst_or_src;
22848275SEric Cheng 
22858275SEric Cheng 	if ((ft->ft_mask & FLOW_ULP_PORT_LOCAL) != 0) {
22868275SEric Cheng 		dst_or_src = ((s->fs_flags & FLOW_INBOUND) != 0);
22878275SEric Cheng 	} else {
22888275SEric Cheng 		dst_or_src = ((s->fs_flags & FLOW_OUTBOUND) != 0);
22898275SEric Cheng 	}
22908275SEric Cheng 
22918275SEric Cheng 	l4info->l4_hash_port = dst_or_src ? l4info->l4_dst_port :
22928275SEric Cheng 	    l4info->l4_src_port;
22938275SEric Cheng 
22948275SEric Cheng 	return ((l4info->l4_hash_port ^ (proto << 4)) % ft->ft_size);
22958275SEric Cheng }
22968275SEric Cheng 
22978275SEric Cheng /*
22988275SEric Cheng  * Unlike other accept() functions above, we do not need to get the header
22998275SEric Cheng  * size because this is our highest layer so far. If we want to do support
23008275SEric Cheng  * other higher layer protocols, we would need to save the l4_hdrsize
23018275SEric Cheng  * in the code below.
23028275SEric Cheng  */
23038275SEric Cheng 
23048275SEric Cheng /* ARGSUSED */
23058275SEric Cheng static int
flow_transport_accept(flow_tab_t * ft,flow_state_t * s)23068275SEric Cheng flow_transport_accept(flow_tab_t *ft, flow_state_t *s)
23078275SEric Cheng {
23088275SEric Cheng 	flow_l3info_t	*l3info = &s->fs_l3info;
23098275SEric Cheng 	flow_l4info_t	*l4info = &s->fs_l4info;
23108275SEric Cheng 	uint8_t		proto = l3info->l3_protocol;
23118275SEric Cheng 	uchar_t		*l4_start;
23128275SEric Cheng 
23138833SVenu.Iyer@Sun.COM 	l4_start = l3info->l3_start + l3info->l3_hdrsize;
23148833SVenu.Iyer@Sun.COM 
23158833SVenu.Iyer@Sun.COM 	/*
23168833SVenu.Iyer@Sun.COM 	 * Adjust start pointer if we're at the end of an mblk.
23178833SVenu.Iyer@Sun.COM 	 */
23188833SVenu.Iyer@Sun.COM 	CHECK_AND_ADJUST_START_PTR(s, l4_start);
23198833SVenu.Iyer@Sun.COM 
23208833SVenu.Iyer@Sun.COM 	l4info->l4_start = l4_start;
23218275SEric Cheng 	if (!OK_32PTR(l4_start))
23228275SEric Cheng 		return (EINVAL);
23238275SEric Cheng 
23248275SEric Cheng 	if (l3info->l3_fragmented == B_TRUE)
23258275SEric Cheng 		return (EINVAL);
23268275SEric Cheng 
23278275SEric Cheng 	switch (proto) {
23288275SEric Cheng 	case IPPROTO_TCP: {
23298275SEric Cheng 		struct tcphdr	*tcph = (struct tcphdr *)l4_start;
23308275SEric Cheng 
23318275SEric Cheng 		if (PKT_TOO_SMALL(s, l4_start + sizeof (*tcph)))
23328275SEric Cheng 			return (ENOBUFS);
23338275SEric Cheng 
23348275SEric Cheng 		l4info->l4_src_port = tcph->th_sport;
23358275SEric Cheng 		l4info->l4_dst_port = tcph->th_dport;
23368275SEric Cheng 		break;
23378275SEric Cheng 	}
23388275SEric Cheng 	case IPPROTO_UDP: {
23398275SEric Cheng 		struct udphdr	*udph = (struct udphdr *)l4_start;
23408275SEric Cheng 
23418275SEric Cheng 		if (PKT_TOO_SMALL(s, l4_start + sizeof (*udph)))
23428275SEric Cheng 			return (ENOBUFS);
23438275SEric Cheng 
23448275SEric Cheng 		l4info->l4_src_port = udph->uh_sport;
23458275SEric Cheng 		l4info->l4_dst_port = udph->uh_dport;
23468275SEric Cheng 		break;
23478275SEric Cheng 	}
23488275SEric Cheng 	case IPPROTO_SCTP: {
23498275SEric Cheng 		sctp_hdr_t	*sctph = (sctp_hdr_t *)l4_start;
23508275SEric Cheng 
23518275SEric Cheng 		if (PKT_TOO_SMALL(s, l4_start + sizeof (*sctph)))
23528275SEric Cheng 			return (ENOBUFS);
23538275SEric Cheng 
23548275SEric Cheng 		l4info->l4_src_port = sctph->sh_sport;
23558275SEric Cheng 		l4info->l4_dst_port = sctph->sh_dport;
23568275SEric Cheng 		break;
23578275SEric Cheng 	}
23588275SEric Cheng 	default:
23598275SEric Cheng 		return (EINVAL);
23608275SEric Cheng 	}
23618275SEric Cheng 
23628275SEric Cheng 	return (0);
23638275SEric Cheng }
23648275SEric Cheng 
23658275SEric Cheng /*
23668275SEric Cheng  * Validates transport flow entry.
23678275SEric Cheng  * The protocol field must be present.
23688275SEric Cheng  */
23698275SEric Cheng 
23708275SEric Cheng /* ARGSUSED */
23718275SEric Cheng static int
flow_transport_accept_fe(flow_tab_t * ft,flow_entry_t * flent)23728275SEric Cheng flow_transport_accept_fe(flow_tab_t *ft, flow_entry_t *flent)
23738275SEric Cheng {
23748275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
23758275SEric Cheng 	flow_mask_t	mask = fd->fd_mask;
23768275SEric Cheng 
23778275SEric Cheng 	if ((mask & FLOW_IP_PROTOCOL) == 0)
23788275SEric Cheng 		return (EINVAL);
23798275SEric Cheng 
23808275SEric Cheng 	switch (fd->fd_protocol) {
23818275SEric Cheng 	case IPPROTO_TCP:
23828275SEric Cheng 	case IPPROTO_UDP:
23838275SEric Cheng 	case IPPROTO_SCTP:
23848275SEric Cheng 		break;
23858275SEric Cheng 	default:
23868275SEric Cheng 		return (EINVAL);
23878275SEric Cheng 	}
23888275SEric Cheng 
23898275SEric Cheng 	switch (mask & ~FLOW_IP_PROTOCOL) {
23908275SEric Cheng 	case FLOW_ULP_PORT_LOCAL:
23918275SEric Cheng 		if (fd->fd_local_port == 0)
23928275SEric Cheng 			return (EINVAL);
23938275SEric Cheng 
23948275SEric Cheng 		flent->fe_match = flow_transport_lport_match;
23958275SEric Cheng 		break;
23968275SEric Cheng 	case FLOW_ULP_PORT_REMOTE:
23978275SEric Cheng 		if (fd->fd_remote_port == 0)
23988275SEric Cheng 			return (EINVAL);
23998275SEric Cheng 
24008275SEric Cheng 		flent->fe_match = flow_transport_rport_match;
24018275SEric Cheng 		break;
24028275SEric Cheng 	case 0:
24038275SEric Cheng 		/*
24048275SEric Cheng 		 * transport-only flows conflicts with our table type.
24058275SEric Cheng 		 */
24068275SEric Cheng 		return (EOPNOTSUPP);
24078275SEric Cheng 	default:
24088275SEric Cheng 		return (EINVAL);
24098275SEric Cheng 	}
24108275SEric Cheng 
24118275SEric Cheng 	return (0);
24128275SEric Cheng }
24138275SEric Cheng 
24148275SEric Cheng static uint32_t
flow_transport_hash_fe(flow_tab_t * ft,flow_entry_t * flent)24158275SEric Cheng flow_transport_hash_fe(flow_tab_t *ft, flow_entry_t *flent)
24168275SEric Cheng {
24178275SEric Cheng 	flow_desc_t	*fd = &flent->fe_flow_desc;
24188275SEric Cheng 	uint16_t	port = 0;
24198275SEric Cheng 
24208275SEric Cheng 	port = ((fd->fd_mask & FLOW_ULP_PORT_LOCAL) != 0) ?
24218275SEric Cheng 	    fd->fd_local_port : fd->fd_remote_port;
24228275SEric Cheng 
24238275SEric Cheng 	return ((port ^ (fd->fd_protocol << 4)) % ft->ft_size);
24248275SEric Cheng }
24258275SEric Cheng 
24268275SEric Cheng /* ARGSUSED */
24278275SEric Cheng static boolean_t
flow_transport_match_fe(flow_tab_t * ft,flow_entry_t * f1,flow_entry_t * f2)24288275SEric Cheng flow_transport_match_fe(flow_tab_t *ft, flow_entry_t *f1, flow_entry_t *f2)
24298275SEric Cheng {
24308275SEric Cheng 	flow_desc_t	*fd1 = &f1->fe_flow_desc, *fd2 = &f2->fe_flow_desc;
24318275SEric Cheng 
24328275SEric Cheng 	if (fd1->fd_protocol != fd2->fd_protocol)
24338275SEric Cheng 		return (B_FALSE);
24348275SEric Cheng 
24358275SEric Cheng 	if ((fd1->fd_mask & FLOW_ULP_PORT_LOCAL) != 0)
24368275SEric Cheng 		return (fd1->fd_local_port == fd2->fd_local_port);
24378275SEric Cheng 
243810734SEric Cheng 	if ((fd1->fd_mask & FLOW_ULP_PORT_REMOTE) != 0)
243910734SEric Cheng 		return (fd1->fd_remote_port == fd2->fd_remote_port);
244010734SEric Cheng 
244110734SEric Cheng 	return (B_TRUE);
24428275SEric Cheng }
24438275SEric Cheng 
24448275SEric Cheng static flow_ops_t flow_l2_ops = {
24458275SEric Cheng 	flow_l2_accept_fe,
24468275SEric Cheng 	flow_l2_hash_fe,
24478275SEric Cheng 	flow_l2_match_fe,
24488275SEric Cheng 	flow_generic_insert_fe,
24498275SEric Cheng 	flow_l2_hash,
24508275SEric Cheng 	{flow_l2_accept}
24518275SEric Cheng };
24528275SEric Cheng 
24538275SEric Cheng static flow_ops_t flow_ip_ops = {
24548275SEric Cheng 	flow_ip_accept_fe,
24558275SEric Cheng 	flow_ip_hash_fe,
24568275SEric Cheng 	flow_ip_match_fe,
24578275SEric Cheng 	flow_ip_insert_fe,
24588275SEric Cheng 	flow_ip_hash,
24598275SEric Cheng 	{flow_l2_accept, flow_ip_accept}
24608275SEric Cheng };
24618275SEric Cheng 
24628275SEric Cheng static flow_ops_t flow_ip_proto_ops = {
24638275SEric Cheng 	flow_ip_proto_accept_fe,
24648275SEric Cheng 	flow_ip_proto_hash_fe,
24658275SEric Cheng 	flow_ip_proto_match_fe,
24668275SEric Cheng 	flow_generic_insert_fe,
24678275SEric Cheng 	flow_ip_proto_hash,
24688275SEric Cheng 	{flow_l2_accept, flow_ip_accept}
24698275SEric Cheng };
24708275SEric Cheng 
24718275SEric Cheng static flow_ops_t flow_transport_ops = {
24728275SEric Cheng 	flow_transport_accept_fe,
24738275SEric Cheng 	flow_transport_hash_fe,
24748275SEric Cheng 	flow_transport_match_fe,
24758275SEric Cheng 	flow_generic_insert_fe,
24768275SEric Cheng 	flow_transport_hash,
24778275SEric Cheng 	{flow_l2_accept, flow_ip_accept, flow_transport_accept}
24788275SEric Cheng };
24798275SEric Cheng 
24808275SEric Cheng static flow_tab_info_t flow_tab_info_list[] = {
24818275SEric Cheng 	{&flow_ip_ops, FLOW_IP_VERSION | FLOW_IP_LOCAL, 2},
24828275SEric Cheng 	{&flow_ip_ops, FLOW_IP_VERSION | FLOW_IP_REMOTE, 2},
24838275SEric Cheng 	{&flow_ip_ops, FLOW_IP_DSFIELD, 1},
24848275SEric Cheng 	{&flow_ip_proto_ops, FLOW_IP_PROTOCOL, 256},
248510734SEric Cheng 	{&flow_transport_ops, FLOW_IP_PROTOCOL | FLOW_ULP_PORT_LOCAL, 1024},
248610734SEric Cheng 	{&flow_transport_ops, FLOW_IP_PROTOCOL | FLOW_ULP_PORT_REMOTE, 1024}
24878275SEric Cheng };
24888275SEric Cheng 
24898275SEric Cheng #define	FLOW_MAX_TAB_INFO \
24908275SEric Cheng 	((sizeof (flow_tab_info_list)) / sizeof (flow_tab_info_t))
24918275SEric Cheng 
24928275SEric Cheng static flow_tab_info_t *
mac_flow_tab_info_get(flow_mask_t mask)24938275SEric Cheng mac_flow_tab_info_get(flow_mask_t mask)
24948275SEric Cheng {
24958275SEric Cheng 	int	i;
24968275SEric Cheng 
24978275SEric Cheng 	for (i = 0; i < FLOW_MAX_TAB_INFO; i++) {
24988275SEric Cheng 		if (mask == flow_tab_info_list[i].fti_mask)
24998275SEric Cheng 			return (&flow_tab_info_list[i]);
25008275SEric Cheng 	}
25018275SEric Cheng 	return (NULL);
25028275SEric Cheng }
2503