#include <sys/types.h>
#include <sys/conf.h>
#include <sys/id_space.h>
#include <sys/esunddi.h>
#include <sys/stat.h>
#include <sys/mkdev.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <sys/strsubr.h>
#include <sys/pattr.h>
#include <sys/dlpi.h>
#include <sys/modhash.h>
#include <sys/mac_impl.h>
#include <sys/mac_client_impl.h>
#include <sys/mac_soft_ring.h>
#include <sys/mac_stat.h>
#include <sys/dls.h>
#include <sys/dld.h>
#include <sys/modctl.h>
#include <sys/fs/dv_node.h>
#include <sys/thread.h>
#include <sys/proc.h>
#include <sys/callb.h>
#include <sys/cpuvar.h>
#include <sys/atomic.h>
#include <sys/sdt.h>
#include <sys/mac_flow.h>
#include <sys/ddi_intr_impl.h>
#include <sys/disp.h>
#include <sys/sdt.h>
#include <sys/vnic.h>
#include <sys/vnic_impl.h>
#include <sys/vlan.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <sys/exacct.h>
#include <sys/exacct_impl.h>
#include <inet/nd.h>
#include <sys/ethernet.h>
kmem_cache_t *mac_client_impl_cache;
kmem_cache_t *mac_promisc_impl_cache;
static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
mac_unicast_impl_t *);
static void mac_client_remove_flow_from_list(mac_client_impl_t *,
flow_entry_t *);
static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
static void mac_rename_flow_names(mac_client_impl_t *, const char *);
static void mac_virtual_link_update(mac_impl_t *);
static int mac_client_datapath_setup(mac_client_impl_t *, uint16_t,
uint8_t *, mac_resource_props_t *, boolean_t, mac_unicast_impl_t *);
static void mac_client_datapath_teardown(mac_client_handle_t,
mac_unicast_impl_t *, flow_entry_t *);
static int mac_resource_ctl_set(mac_client_handle_t, mac_resource_props_t *);
static int
i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
{
int i;
mac_client_impl_t *mcip = buf;
bzero(buf, MAC_CLIENT_IMPL_SIZE);
mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
ASSERT(mac_tx_percpu_cnt >= 0);
for (i = 0; i <= mac_tx_percpu_cnt; i++) {
mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
MUTEX_DRIVER, NULL);
}
cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
return (0);
}
static void
i_mac_client_impl_dtor(void *buf, void *arg)
{
int i;
mac_client_impl_t *mcip = buf;
ASSERT(mcip->mci_promisc_list == NULL);
ASSERT(mcip->mci_unicast_list == NULL);
ASSERT(mcip->mci_state_flags == 0);
ASSERT(mcip->mci_tx_flag == 0);
mutex_destroy(&mcip->mci_tx_cb_lock);
ASSERT(mac_tx_percpu_cnt >= 0);
for (i = 0; i <= mac_tx_percpu_cnt; i++) {
ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
}
cv_destroy(&mcip->mci_tx_cv);
}
static int
i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
{
mac_promisc_impl_t *mpip = buf;
bzero(buf, sizeof (mac_promisc_impl_t));
mpip->mpi_mci_link.mcb_objp = buf;
mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
mpip->mpi_mi_link.mcb_objp = buf;
mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
return (0);
}
static void
i_mac_promisc_impl_dtor(void *buf, void *arg)
{
mac_promisc_impl_t *mpip = buf;
ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
mpip->mpi_mci_link.mcb_objp = NULL;
mpip->mpi_mci_link.mcb_objsize = 0;
mpip->mpi_mi_link.mcb_objp = NULL;
mpip->mpi_mi_link.mcb_objsize = 0;
ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
mpip->mpi_mci_link.mcb_objsize = 0;
}
void
mac_client_init(void)
{
ASSERT(mac_tx_percpu_cnt >= 0);
mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
ASSERT(mac_client_impl_cache != NULL);
mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
ASSERT(mac_promisc_impl_cache != NULL);
}
void
mac_client_fini(void)
{
kmem_cache_destroy(mac_client_impl_cache);
kmem_cache_destroy(mac_promisc_impl_cache);
}
mac_client_impl_t *
mac_vnic_lower(mac_impl_t *mip)
{
mac_capab_vnic_t cap;
mac_client_impl_t *mcip;
VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
return (mcip);
}
void
mac_vnic_secondary_update(mac_impl_t *mip)
{
mac_capab_vnic_t cap;
VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
cap.mcv_mac_secondary_update(cap.mcv_arg);
}
mac_client_impl_t *
mac_primary_client_handle(mac_impl_t *mip)
{
mac_client_impl_t *mcip;
if (mip->mi_state_flags & MIS_IS_VNIC)
return (mac_vnic_lower(mip));
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
for (mcip = mip->mi_clients_list; mcip != NULL;
mcip = mcip->mci_client_next) {
if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
return (mcip);
}
return (NULL);
}
int
mac_open(const char *macname, mac_handle_t *mhp)
{
mac_impl_t *mip;
int err;
if ((err = mac_hold(macname, &mip)) != 0)
return (err);
e_ddi_hold_devi(mip->mi_dip);
if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
*mhp = (mac_handle_t)mip;
return (0);
}
i_mac_perim_enter(mip);
mip->mi_oref++;
if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
*mhp = (mac_handle_t)mip;
i_mac_perim_exit(mip);
return (0);
}
mip->mi_oref--;
ddi_release_devi(mip->mi_dip);
mac_rele(mip);
i_mac_perim_exit(mip);
return (err);
}
int
mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
{
dls_dl_handle_t dlh;
int err;
if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
return (err);
dls_devnet_prop_task_wait(dlh);
err = mac_open(dls_devnet_mac(dlh), mhp);
dls_devnet_rele_tmp(dlh);
return (err);
}
int
mac_open_by_linkname(const char *link, mac_handle_t *mhp)
{
datalink_id_t linkid;
int err;
if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
return (err);
return (mac_open_by_linkid(linkid, mhp));
}
void
mac_close(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
i_mac_perim_enter(mip);
if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
ASSERT(mip->mi_oref != 0);
if (--mip->mi_oref == 0) {
if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
mip->mi_close(mip->mi_driver);
}
}
i_mac_perim_exit(mip);
ddi_release_devi(mip->mi_dip);
mac_rele(mip);
}
const mac_info_t *
mac_info(mac_handle_t mh)
{
return (&((mac_impl_t *)mh)->mi_info);
}
dev_info_t *
mac_devinfo_get(mac_handle_t mh)
{
return (((mac_impl_t *)mh)->mi_dip);
}
void *
mac_driver(mac_handle_t mh)
{
return (((mac_impl_t *)mh)->mi_driver);
}
const char *
mac_name(mac_handle_t mh)
{
return (((mac_impl_t *)mh)->mi_name);
}
int
mac_type(mac_handle_t mh)
{
return (((mac_impl_t *)mh)->mi_type->mt_type);
}
int
mac_nativetype(mac_handle_t mh)
{
return (((mac_impl_t *)mh)->mi_type->mt_nativetype);
}
char *
mac_client_name(mac_client_handle_t mch)
{
return (((mac_client_impl_t *)mch)->mci_name);
}
minor_t
mac_minor(mac_handle_t mh)
{
return (((mac_impl_t *)mh)->mi_minor);
}
uint16_t
mac_client_vid(mac_client_handle_t mch)
{
uint16_t vid = VLAN_ID_NONE;
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
flow_desc_t flow_desc;
if (mcip->mci_nflents == 0)
return (vid);
ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
mac_flow_get_desc(mcip->mci_flent, &flow_desc);
if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
vid = flow_desc.fd_vid;
return (vid);
}
boolean_t
mac_client_is_vlan_vnic(mac_client_handle_t mch)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
}
static uint64_t
mac_client_ifspeed(mac_client_impl_t *mcip)
{
mac_impl_t *mip = mcip->mci_mip;
uint64_t nic_speed;
nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
if (nic_speed == 0) {
return (0);
} else {
uint64_t policy_limit = (uint64_t)-1;
if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
return (MIN(policy_limit, nic_speed));
}
}
static uint64_t
mac_client_link_state(mac_client_impl_t *mcip)
{
mac_impl_t *mip = mcip->mci_mip;
uint16_t vid;
mac_client_impl_t *mci_list;
mac_unicast_impl_t *mui_list, *oth_mui_list;
rw_enter(&mcip->mci_rw_lock, RW_READER);
for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
mui_list = mui_list->mui_next) {
vid = mui_list->mui_vid;
for (mci_list = mip->mi_clients_list; mci_list != NULL;
mci_list = mci_list->mci_client_next) {
if (mci_list == mcip)
continue;
for (oth_mui_list = mci_list->mci_unicast_list;
oth_mui_list != NULL; oth_mui_list = oth_mui_list->
mui_next) {
if (vid == oth_mui_list->mui_vid) {
rw_exit(&mcip->mci_rw_lock);
return (LINK_STATE_UP);
}
}
}
}
rw_exit(&mcip->mci_rw_lock);
return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
}
uint64_t
mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
flow_entry_t *flent = mcip->mci_flent;
mac_soft_ring_set_t *mac_srs;
mac_rx_stats_t *mac_rx_stat, *old_rx_stat;
mac_tx_stats_t *mac_tx_stat, *old_tx_stat;
int i;
uint64_t val = 0;
mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
mac_tx_stat = &mac_srs->srs_tx.st_stat;
old_rx_stat = &mcip->mci_misc_stat.mms_defunctrxlanestats;
old_tx_stat = &mcip->mci_misc_stat.mms_defuncttxlanestats;
switch (stat) {
case MAC_STAT_LINK_STATE:
val = mac_client_link_state(mcip);
break;
case MAC_STAT_LINK_UP:
val = (mac_client_link_state(mcip) == LINK_STATE_UP);
break;
case MAC_STAT_PROMISC:
val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
break;
case MAC_STAT_LOWLINK_STATE:
val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
break;
case MAC_STAT_IFSPEED:
val = mac_client_ifspeed(mcip);
break;
case MAC_STAT_MULTIRCV:
val = mcip->mci_misc_stat.mms_multircv;
break;
case MAC_STAT_BRDCSTRCV:
val = mcip->mci_misc_stat.mms_brdcstrcv;
break;
case MAC_STAT_MULTIXMT:
val = mcip->mci_misc_stat.mms_multixmt;
break;
case MAC_STAT_BRDCSTXMT:
val = mcip->mci_misc_stat.mms_brdcstxmt;
break;
case MAC_STAT_OBYTES:
val = mac_tx_stat->mts_obytes;
val += old_tx_stat->mts_obytes;
break;
case MAC_STAT_OPACKETS:
val = mac_tx_stat->mts_opackets;
val += old_tx_stat->mts_opackets;
break;
case MAC_STAT_OERRORS:
val = mac_tx_stat->mts_oerrors;
val += old_tx_stat->mts_oerrors;
break;
case MAC_STAT_IPACKETS:
for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
mac_rx_stat = &mac_srs->srs_rx.sr_stat;
val += mac_rx_stat->mrs_intrcnt +
mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
}
val += old_rx_stat->mrs_intrcnt + old_rx_stat->mrs_pollcnt +
old_rx_stat->mrs_lclcnt;
break;
case MAC_STAT_RBYTES:
for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
mac_rx_stat = &mac_srs->srs_rx.sr_stat;
val += mac_rx_stat->mrs_intrbytes +
mac_rx_stat->mrs_pollbytes +
mac_rx_stat->mrs_lclbytes;
}
val += old_rx_stat->mrs_intrbytes + old_rx_stat->mrs_pollbytes +
old_rx_stat->mrs_lclbytes;
break;
case MAC_STAT_IERRORS:
for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
mac_rx_stat = &mac_srs->srs_rx.sr_stat;
val += mac_rx_stat->mrs_ierrors;
}
val += old_rx_stat->mrs_ierrors;
break;
default:
val = mac_driver_stat_default(mip, stat);
break;
}
return (val);
}
uint64_t
mac_stat_get(mac_handle_t mh, uint_t stat)
{
mac_impl_t *mip = (mac_impl_t *)mh;
uint64_t val;
int ret;
if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
switch (stat) {
case MAC_STAT_LINK_STATE:
return (mip->mi_linkstate);
case MAC_STAT_LINK_UP:
return (mip->mi_linkstate == LINK_STATE_UP);
case MAC_STAT_PROMISC:
return (mip->mi_devpromisc != 0);
case MAC_STAT_LOWLINK_STATE:
return (mip->mi_lowlinkstate);
default:
ASSERT(B_FALSE);
}
}
ret = mip->mi_getstat(mip->mi_driver, stat, &val);
if (ret != 0) {
val = mac_driver_stat_default(mip, stat);
}
return (val);
}
uint64_t
mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
{
return (mac_rx_ring_stat_get(handle, stat));
}
uint64_t
mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
{
return (mac_tx_ring_stat_get(handle, stat));
}
uint16_t
i_mac_flow_vid(flow_entry_t *flent)
{
flow_desc_t flow_desc;
mac_flow_get_desc(flent, &flow_desc);
if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
return (flow_desc.fd_vid);
return (VLAN_ID_NONE);
}
boolean_t
mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
{
mac_impl_t *mip = (mac_impl_t *)mh;
if ((len != mip->mi_type->mt_addr_length) ||
(mip->mi_type->mt_ops.mtops_unicst_verify(addr,
mip->mi_pdata)) != 0) {
return (B_FALSE);
} else {
return (B_TRUE);
}
}
void
mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
{
mac_impl_t *mip = (mac_impl_t *)mh;
if (min_sdu != NULL)
*min_sdu = mip->mi_sdu_min;
if (max_sdu != NULL)
*max_sdu = mip->mi_sdu_max;
}
void
mac_sdu_get2(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu,
uint_t *multicast_sdu)
{
mac_impl_t *mip = (mac_impl_t *)mh;
if (min_sdu != NULL)
*min_sdu = mip->mi_sdu_min;
if (max_sdu != NULL)
*max_sdu = mip->mi_sdu_max;
if (multicast_sdu != NULL)
*multicast_sdu = mip->mi_sdu_multicast;
}
static void
mac_unicast_update_client_flow(mac_client_impl_t *mcip)
{
mac_impl_t *mip = mcip->mci_mip;
flow_entry_t *flent = mcip->mci_flent;
mac_address_t *map = mcip->mci_unicast;
flow_desc_t flow_desc;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
ASSERT(flent != NULL);
mac_flow_get_desc(flent, &flow_desc);
ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
mac_flow_set_desc(flent, &flow_desc);
mac_protect_update_mac_token(mcip);
for (flent = mcip->mci_flent_list; flent != NULL;
flent = flent->fe_client_next) {
mac_flow_get_desc(flent, &flow_desc);
if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
flent->fe_type & FLOW_VNIC_MAC))
continue;
bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
mac_flow_set_desc(flent, &flow_desc);
}
}
void
mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
{
mac_client_impl_t *mcip;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
for (mcip = mip->mi_clients_list; mcip != NULL;
mcip = mcip->mci_client_next) {
if (map != mcip->mci_unicast)
continue;
mac_unicast_update_client_flow(mcip);
}
}
int
mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
mac_address_t *map = mcip->mci_unicast;
int err;
ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
i_mac_perim_enter(mip);
if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
i_mac_perim_exit(mip);
return (ENOTSUP);
}
if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
i_mac_perim_exit(mip);
return (EACCES);
}
if (mac_check_macaddr_shared(map)) {
i_mac_perim_exit(mip);
return (EBUSY);
}
if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
i_mac_perim_exit(mip);
return (ENOTSUP);
}
err = mac_update_macaddr(map, (uint8_t *)addr);
if (err != 0) {
i_mac_perim_exit(mip);
return (err);
}
mac_unicast_update_client_flow(mcip);
i_mac_perim_exit(mip);
return (0);
}
int
mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_address_t *map;
int err;
if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
return (EINVAL);
i_mac_perim_enter(mip);
if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
i_mac_perim_exit(mip);
return (0);
}
if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
i_mac_perim_exit(mip);
return (EBUSY);
}
map = mac_find_macaddr(mip, mip->mi_addr);
ASSERT(map != NULL);
if (mip->mi_state_flags & MIS_IS_AGGR) {
mac_capab_aggr_t aggr_cap;
ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
err = aggr_cap.mca_unicst(mip->mi_driver, addr);
if (err == 0)
bcopy(addr, map->ma_addr, map->ma_len);
} else {
err = mac_update_macaddr(map, (uint8_t *)addr);
}
if (err != 0) {
i_mac_perim_exit(mip);
return (err);
}
mac_unicast_update_clients(mip, map);
bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
i_mac_perim_exit(mip);
if (err == 0)
i_mac_notify(mip, MAC_NOTE_UNICST);
return (err);
}
void
mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
{
mac_impl_t *mip = (mac_impl_t *)mh;
rw_enter(&mip->mi_rw_lock, RW_READER);
bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
rw_exit(&mip->mi_rw_lock);
}
void
mac_unicast_secondary_get(mac_client_handle_t mh, uint8_t *addr)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mh;
ASSERT(mcip->mci_unicast != NULL);
bcopy(mcip->mci_unicast->ma_addr, addr, mcip->mci_unicast->ma_len);
}
void
mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_client_impl_t *cur_client;
if (in_use != NULL)
*in_use = B_FALSE;
if (client_name != NULL)
bzero(client_name, MAXNAMELEN);
rw_enter(&mip->mi_rw_lock, RW_READER);
for (cur_client = mip->mi_clients_list; cur_client != NULL;
cur_client = cur_client->mci_client_next) {
if (mac_is_primary_client(cur_client) ||
(mip->mi_state_flags & MIS_IS_VNIC)) {
rw_exit(&mip->mi_rw_lock);
if (in_use != NULL)
*in_use = B_TRUE;
if (client_name != NULL) {
bcopy(cur_client->mci_name, client_name,
MAXNAMELEN);
}
return;
}
}
rw_exit(&mip->mi_rw_lock);
}
boolean_t
mac_dst_get(mac_handle_t mh, uint8_t *addr)
{
mac_impl_t *mip = (mac_impl_t *)mh;
rw_enter(&mip->mi_rw_lock, RW_READER);
if (mip->mi_dstaddr_set)
bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
rw_exit(&mip->mi_rw_lock);
return (mip->mi_dstaddr_set);
}
static void
mac_client_add(mac_client_impl_t *mcip)
{
mac_impl_t *mip = mcip->mci_mip;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
rw_enter(&mip->mi_rw_lock, RW_WRITER);
mcip->mci_client_next = mip->mi_clients_list;
mip->mi_clients_list = mcip;
mip->mi_nclients++;
rw_exit(&mip->mi_rw_lock);
}
static void
mac_client_remove(mac_client_impl_t *mcip)
{
mac_impl_t *mip = mcip->mci_mip;
mac_client_impl_t **prev, *cclient;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
rw_enter(&mip->mi_rw_lock, RW_WRITER);
prev = &mip->mi_clients_list;
cclient = *prev;
while (cclient != NULL && cclient != mcip) {
prev = &cclient->mci_client_next;
cclient = *prev;
}
ASSERT(cclient != NULL);
*prev = cclient->mci_client_next;
mip->mi_nclients--;
rw_exit(&mip->mi_rw_lock);
}
static mac_unicast_impl_t *
mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
{
mac_unicast_impl_t *muip = mcip->mci_unicast_list;
while ((muip != NULL) && (muip->mui_vid != vid))
muip = muip->mui_next;
return (muip);
}
static boolean_t
mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
{
mac_client_impl_t *client;
mac_address_t *map;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
for (client = mip->mi_clients_list; client != NULL;
client = client->mci_client_next) {
if (client->mci_unicast_list == NULL)
continue;
map = client->mci_unicast;
if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
(mac_client_find_vid(client, vid) != NULL)) {
return (B_TRUE);
}
}
return (B_FALSE);
}
int
mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
uint8_t *mac_addr, mac_diag_t *diag)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
size_t addr_len = mip->mi_type->mt_addr_length;
if (prefix_len >= addr_len) {
*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
return (EINVAL);
}
if (prefix_len > 0) {
bzero(mac_addr + prefix_len, addr_len - prefix_len);
if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
addr_len)) {
*diag = MAC_DIAG_MACPREFIX_INVALID;
return (EINVAL);
}
}
if (prefix_len < addr_len) {
(void) random_get_pseudo_bytes(mac_addr +
prefix_len, addr_len - prefix_len);
}
*diag = MAC_DIAG_NONE;
return (0);
}
#define MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) { \
(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI, \
MAXCLSYSPRI, (pri)); \
(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI, \
MAXCLSYSPRI, (mcip)->mci_min_pri); \
}
int
mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
uint16_t flags)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_client_impl_t *mcip;
int err = 0;
boolean_t share_desired;
flow_entry_t *flent = NULL;
share_desired = (flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0;
*mchp = NULL;
i_mac_perim_enter(mip);
if (mip->mi_state_flags & MIS_IS_VNIC) {
mcip = mac_vnic_lower(mip);
if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
mcip->mci_state_flags |= MCIS_EXCLUSIVE;
if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
mip->mi_clients_list = mcip;
i_mac_perim_exit(mip);
*mchp = (mac_client_handle_t)mcip;
DTRACE_PROBE2(mac__client__open__nonallocated, mac_impl_t *,
mcip->mci_mip, mac_client_impl_t *, mcip);
return (err);
}
mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
mcip->mci_mip = mip;
mcip->mci_upper_mip = NULL;
mcip->mci_rx_fn = mac_rx_def;
mcip->mci_rx_arg = NULL;
mcip->mci_rx_p_fn = NULL;
mcip->mci_rx_p_arg = NULL;
mcip->mci_p_unicast_list = NULL;
mcip->mci_direct_rx.mdrx_v4 = NULL;
mcip->mci_direct_rx.mdrx_v6 = NULL;
mcip->mci_direct_rx.mdrx_arg_v4 = NULL;
mcip->mci_direct_rx.mdrx_arg_v6 = NULL;
mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
mcip->mci_unicast_list = NULL;
if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
mcip->mci_state_flags |= MCIS_IS_VNIC;
if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
mcip->mci_state_flags |= MCIS_EXCLUSIVE;
if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
if (mip->mi_state_flags & MIS_IS_AGGR)
mcip->mci_state_flags |= MCIS_IS_AGGR_CLIENT;
if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
datalink_id_t linkid;
ASSERT(name == NULL);
if ((err = dls_devnet_macname2linkid(mip->mi_name,
&linkid)) != 0) {
goto done;
}
if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
NULL, NULL)) != 0) {
if (err == EBADF) {
(void) strlcpy(mcip->mci_name, mip->mi_name,
sizeof (mcip->mci_name));
err = 0;
} else {
goto done;
}
}
mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
} else {
ASSERT(name != NULL);
if (strlen(name) > MAXNAMELEN) {
err = EINVAL;
goto done;
}
(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
}
if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
if (flags & MAC_OPEN_FLAGS_NO_UNICAST_ADDR)
mcip->mci_state_flags |= MCIS_NO_UNICAST_ADDR;
mac_protect_init(mcip);
mcip->mci_subflow_tab = NULL;
mcip->mci_misc_stat.mms_multircv = 0;
mcip->mci_misc_stat.mms_brdcstrcv = 0;
mcip->mci_misc_stat.mms_multixmt = 0;
mcip->mci_misc_stat.mms_brdcstxmt = 0;
err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
FLOW_PRIMARY_MAC, &flent);
if (err != 0)
goto done;
mcip->mci_flent = flent;
FLOW_MARK(flent, FE_MC_NO_DATAPATH);
flent->fe_mcip = mcip;
FLOW_REFHOLD(flent);
mac_client_add(mcip);
mcip->mci_share = 0;
if (share_desired)
i_mac_share_alloc(mcip);
if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
if ((err = mac_client_datapath_setup(mcip, VLAN_ID_NONE,
NULL, NULL, B_TRUE, NULL)) != 0) {
goto done;
}
}
DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
mcip->mci_mip, mac_client_impl_t *, mcip);
*mchp = (mac_client_handle_t)mcip;
i_mac_perim_exit(mip);
return (0);
done:
i_mac_perim_exit(mip);
mcip->mci_state_flags = 0;
mcip->mci_tx_flag = 0;
kmem_cache_free(mac_client_impl_cache, mcip);
return (err);
}
void
mac_client_close(mac_client_handle_t mch, uint16_t flags)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
flow_entry_t *flent;
i_mac_perim_enter(mip);
if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
!(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
i_mac_perim_exit(mip);
return;
}
if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
mac_client_datapath_teardown((mac_client_handle_t)mcip, NULL,
mcip->mci_flent);
mcip->mci_state_flags &= ~MCIS_NO_UNICAST_ADDR;
}
flent = mcip->mci_flent;
mcip->mci_flent = NULL;
FLOW_FINAL_REFRELE(flent);
ASSERT(mcip->mci_unicast_list == NULL);
ASSERT(mcip->mci_promisc_list == NULL);
ASSERT(mcip->mci_tx_notify_cb_list == NULL);
i_mac_share_free(mcip);
mac_protect_fini(mcip);
mac_client_remove(mcip);
i_mac_perim_exit(mip);
mcip->mci_subflow_tab = NULL;
mcip->mci_state_flags = 0;
mcip->mci_tx_flag = 0;
kmem_cache_free(mac_client_impl_cache, mch);
}
boolean_t
mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1,
boolean_t v6)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
if (mcip->mci_nvids > 1)
return (B_FALSE);
if (v6) {
mcip->mci_direct_rx.mdrx_v6 = rx_fn;
mcip->mci_direct_rx.mdrx_arg_v6 = arg1;
} else {
mcip->mci_direct_rx.mdrx_v4 = rx_fn;
mcip->mci_direct_rx.mdrx_arg_v4 = arg1;
}
return (B_TRUE);
}
void
mac_rx_bypass_enable(mac_client_handle_t mch)
{
((mac_client_impl_t *)mch)->mci_state_flags &= ~MCIS_RX_BYPASS_DISABLE;
}
void
mac_rx_bypass_disable(mac_client_handle_t mch)
{
((mac_client_impl_t *)mch)->mci_state_flags |= MCIS_RX_BYPASS_DISABLE;
}
void
mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
mac_impl_t *umip = mcip->mci_upper_mip;
i_mac_perim_enter(mip);
mac_rx_client_quiesce(mch);
mcip->mci_rx_fn = rx_fn;
mcip->mci_rx_arg = arg;
mac_rx_client_restart(mch);
if (umip != NULL) {
ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
mac_vnic_secondary_update(umip);
}
i_mac_perim_exit(mip);
}
void
mac_rx_clear(mac_client_handle_t mch)
{
mac_rx_set(mch, mac_rx_def, NULL);
}
void
mac_rx_barrier(mac_client_handle_t mch)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
i_mac_perim_enter(mip);
if (mcip->mci_rx_fn != mac_rx_def) {
mac_rx_client_quiesce(mch);
mac_rx_client_restart(mch);
}
if (mcip->mci_promisc_list != NULL || mip->mi_promisc_list != NULL) {
mac_cb_info_t *mcbi = &mip->mi_promisc_cb_info;
mutex_enter(mcbi->mcbi_lockp);
mac_callback_barrier(mcbi);
mutex_exit(mcbi->mcbi_lockp);
}
i_mac_perim_exit(mip);
}
void
mac_secondary_dup(mac_client_handle_t smch, mac_client_handle_t dmch)
{
mac_client_impl_t *smcip = (mac_client_impl_t *)smch;
mac_client_impl_t *dmcip = (mac_client_impl_t *)dmch;
flow_entry_t *flent = dmcip->mci_flent;
ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
mac_rx_set(dmch, smcip->mci_rx_fn, smcip->mci_rx_arg);
dmcip->mci_promisc_list = smcip->mci_promisc_list;
(void) mac_resource_ctl_set(dmch, MCIP_RESOURCE_PROPS(smcip));
}
void
mac_secondary_cleanup(mac_client_handle_t mch)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
flow_entry_t *flent = mcip->mci_flent;
ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
mcip->mci_promisc_list = NULL;
}
static int
mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
{
mac_flow_update_priority(arg, flent);
return (0);
}
void
mac_update_subflow_priority(mac_client_impl_t *mcip)
{
(void) mac_flow_walk(mcip->mci_subflow_tab,
mac_update_subflow_priority_cb, mcip);
}
int
mac_client_set_rings_prop(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
mac_resource_props_t *tmrp)
{
mac_impl_t *mip = mcip->mci_mip;
flow_entry_t *flent = mcip->mci_flent;
uint8_t *mac_addr;
int err = 0;
mac_group_t *defgrp;
mac_group_t *group;
mac_group_t *ngrp;
mac_resource_props_t *cmrp = MCIP_RESOURCE_PROPS(mcip);
uint_t ringcnt;
boolean_t unspec;
if (mcip->mci_share != 0)
return (EINVAL);
if (mrp->mrp_mask & MRP_RX_RINGS) {
unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
group = flent->fe_rx_ring_group;
defgrp = MAC_DEFAULT_RX_GROUP(mip);
mac_addr = flent->fe_flow_desc.fd_dst_mac;
if (mrp->mrp_mask & MRP_RINGS_RESET) {
if (!(tmrp->mrp_mask & MRP_RX_RINGS))
return (0);
} else {
if (unspec) {
if (tmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
return (0);
} else if (mip->mi_rx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
if ((tmrp->mrp_mask & MRP_RX_RINGS) &&
!(tmrp->mrp_mask & MRP_RXRINGS_UNSPEC) &&
mrp->mrp_nrxrings == tmrp->mrp_nrxrings) {
return (0);
}
}
}
if (mrp->mrp_mask & MRP_RINGS_RESET) {
if (group != defgrp) {
if (mip->mi_rx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
MAC_RX_RING_RELEASED(mip,
group->mrg_cur_count);
MAC_RX_GRP_RELEASED(mip);
if ((flent->fe_type &
FLOW_PRIMARY_MAC) != 0) {
if (mip->mi_nactiveclients ==
1) {
(void)
mac_rx_switch_group(
mcip, group,
defgrp);
return (0);
} else {
cmrp->mrp_nrxrings =
group->
mrg_cur_count +
defgrp->
mrg_cur_count - 1;
}
} else {
cmrp->mrp_nrxrings = 1;
}
(void) mac_group_ring_modify(mcip,
group, defgrp);
} else {
MAC_RX_GRP_RELEASED(mip);
}
} else if (group == defgrp) {
if (mac_check_macaddr_shared(mcip->mci_unicast))
return (0);
ngrp = mac_reserve_rx_group(mcip, mac_addr,
B_TRUE);
if (ngrp == NULL)
return (0);
if (mac_rx_switch_group(mcip, defgrp, ngrp) !=
0) {
mac_stop_group(ngrp);
return (0);
}
}
return (0);
}
if (group == defgrp && ((mrp->mrp_nrxrings > 0) || unspec)) {
if (mac_check_macaddr_shared(mcip->mci_unicast))
return (EINVAL);
ngrp = mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
if (ngrp == NULL)
return (ENOSPC);
if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
mac_release_rx_group(mcip, ngrp);
return (ENOSPC);
}
MAC_RX_GRP_RESERVED(mip);
if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC)
MAC_RX_RING_RESERVED(mip, ngrp->mrg_cur_count);
} else if (group != defgrp && !unspec &&
mrp->mrp_nrxrings == 0) {
ringcnt = group->mrg_cur_count;
if (mac_rx_switch_group(mcip, group, defgrp) != 0)
return (ENOSPC);
if (tmrp->mrp_mask & MRP_RX_RINGS) {
MAC_RX_GRP_RELEASED(mip);
if (mip->mi_rx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
MAC_RX_RING_RELEASED(mip, ringcnt);
}
}
} else if (group != defgrp && mip->mi_rx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
ringcnt = group->mrg_cur_count;
err = mac_group_ring_modify(mcip, group, defgrp);
if (err != 0)
return (err);
if (tmrp->mrp_mask & MRP_RX_RINGS) {
if (ringcnt > group->mrg_cur_count) {
MAC_RX_RING_RELEASED(mip,
ringcnt - group->mrg_cur_count);
} else {
MAC_RX_RING_RESERVED(mip,
group->mrg_cur_count - ringcnt);
}
} else {
MAC_RX_RING_RESERVED(mip, group->mrg_cur_count);
MAC_RX_GRP_RESERVED(mip);
}
}
}
if (mrp->mrp_mask & MRP_TX_RINGS) {
unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
group = flent->fe_tx_ring_group;
defgrp = MAC_DEFAULT_TX_GROUP(mip);
if (mrp->mrp_ntxrings > 0 &&
mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
return (ENOTSUP);
}
if (mrp->mrp_mask & MRP_RINGS_RESET) {
if (!(tmrp->mrp_mask & MRP_TX_RINGS))
return (0);
} else {
if (unspec) {
if (tmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
return (0);
} else if (mip->mi_tx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
if ((tmrp->mrp_mask & MRP_TX_RINGS) &&
!(tmrp->mrp_mask & MRP_TXRINGS_UNSPEC) &&
mrp->mrp_ntxrings == tmrp->mrp_ntxrings) {
return (0);
}
}
}
if (mrp->mrp_mask & MRP_RINGS_RESET) {
if (group != defgrp) {
if (mip->mi_tx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
ringcnt = group->mrg_cur_count;
if ((flent->fe_type &
FLOW_PRIMARY_MAC) != 0) {
mac_tx_client_quiesce(
(mac_client_handle_t)
mcip);
mac_tx_switch_group(mcip,
group, defgrp);
mac_tx_client_restart(
(mac_client_handle_t)
mcip);
MAC_TX_GRP_RELEASED(mip);
MAC_TX_RING_RELEASED(mip,
ringcnt);
return (0);
}
cmrp->mrp_ntxrings = 1;
(void) mac_group_ring_modify(mcip,
group, defgrp);
MAC_TX_RING_RELEASED(mip, ringcnt);
}
MAC_TX_GRP_RELEASED(mip);
} else if (group == defgrp &&
(flent->fe_type & FLOW_PRIMARY_MAC) == 0) {
ngrp = mac_reserve_tx_group(mcip, B_TRUE);
if (ngrp == NULL)
return (0);
mac_tx_client_quiesce(
(mac_client_handle_t)mcip);
mac_tx_switch_group(mcip, defgrp, ngrp);
mac_tx_client_restart(
(mac_client_handle_t)mcip);
}
return (0);
}
if (group == defgrp && ((mrp->mrp_ntxrings > 0) || unspec)) {
ngrp = mac_reserve_tx_group(mcip, B_TRUE);
if (ngrp == NULL)
return (ENOSPC);
mac_tx_client_quiesce((mac_client_handle_t)mcip);
mac_tx_switch_group(mcip, defgrp, ngrp);
mac_tx_client_restart((mac_client_handle_t)mcip);
MAC_TX_GRP_RESERVED(mip);
if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)
MAC_TX_RING_RESERVED(mip, ngrp->mrg_cur_count);
} else if (group != defgrp && !unspec &&
mrp->mrp_ntxrings == 0) {
ringcnt = group->mrg_cur_count;
mac_tx_client_quiesce((mac_client_handle_t)mcip);
mac_tx_switch_group(mcip, group, defgrp);
mac_tx_client_restart((mac_client_handle_t)mcip);
if (tmrp->mrp_mask & MRP_TX_RINGS) {
MAC_TX_GRP_RELEASED(mip);
if (mip->mi_tx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
MAC_TX_RING_RELEASED(mip, ringcnt);
}
}
} else if (group != defgrp && mip->mi_tx_group_type ==
MAC_GROUP_TYPE_DYNAMIC) {
ringcnt = group->mrg_cur_count;
err = mac_group_ring_modify(mcip, group, defgrp);
if (err != 0)
return (err);
if (tmrp->mrp_mask & MRP_TX_RINGS) {
if (ringcnt > group->mrg_cur_count) {
MAC_TX_RING_RELEASED(mip,
ringcnt - group->mrg_cur_count);
} else {
MAC_TX_RING_RESERVED(mip,
group->mrg_cur_count - ringcnt);
}
} else {
MAC_TX_RING_RESERVED(mip, group->mrg_cur_count);
MAC_TX_GRP_RESERVED(mip);
}
}
}
return (0);
}
static int
mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = (mac_impl_t *)mcip->mci_mip;
mac_impl_t *umip = mcip->mci_upper_mip;
int err = 0;
flow_entry_t *flent = mcip->mci_flent;
mac_resource_props_t *omrp, *nmrp = MCIP_RESOURCE_PROPS(mcip);
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
mcip->mci_upper_mip : mip, mrp);
if (err != 0)
return (err);
omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
bcopy(nmrp, omrp, sizeof (*omrp));
mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
if (MCIP_DATAPATH_SETUP(mcip)) {
if (mrp->mrp_mask & MRP_RX_RINGS ||
mrp->mrp_mask & MRP_TX_RINGS) {
if ((err = mac_client_set_rings_prop(mcip, mrp,
omrp)) != 0) {
if (omrp->mrp_mask & MRP_RX_RINGS) {
nmrp->mrp_mask |= MRP_RX_RINGS;
nmrp->mrp_nrxrings = omrp->mrp_nrxrings;
} else {
nmrp->mrp_mask &= ~MRP_RX_RINGS;
nmrp->mrp_nrxrings = 0;
}
if (omrp->mrp_mask & MRP_TX_RINGS) {
nmrp->mrp_mask |= MRP_TX_RINGS;
nmrp->mrp_ntxrings = omrp->mrp_ntxrings;
} else {
nmrp->mrp_mask &= ~MRP_TX_RINGS;
nmrp->mrp_ntxrings = 0;
}
if (omrp->mrp_mask & MRP_RXRINGS_UNSPEC)
omrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
else
omrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
if (omrp->mrp_mask & MRP_TXRINGS_UNSPEC)
omrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
else
omrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
kmem_free(omrp, sizeof (*omrp));
return (err);
}
if (mac_is_primary_client(mcip)) {
mac_set_prim_vlan_rings(mip,
MCIP_RESOURCE_PROPS(mcip));
}
}
if (mrp->mrp_mask & MRP_PRIORITY) {
if (mrp->mrp_priority == MPL_RESET) {
MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
MPL_LINK_DEFAULT);
} else {
MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
mrp->mrp_priority);
}
}
mac_flow_modify(mip->mi_flow_tab, flent, mrp);
if (mrp->mrp_mask & MRP_PRIORITY)
mac_update_subflow_priority(mcip);
if (umip != NULL) {
ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
mac_vnic_secondary_update(umip);
}
}
kmem_free(omrp, sizeof (*omrp));
return (0);
}
static int
mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
uint16_t vid, boolean_t is_primary, boolean_t first_flow,
flow_entry_t **flent, mac_resource_props_t *mrp)
{
mac_impl_t *mip = (mac_impl_t *)mcip->mci_mip;
flow_desc_t flow_desc;
char flowname[MAXFLOWNAMELEN];
int err;
uint_t flent_flags;
bzero(&flow_desc, sizeof (flow_desc));
ASSERT(mac_addr != NULL ||
(mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR));
if (mac_addr != NULL) {
flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
}
flow_desc.fd_mask = FLOW_LINK_DST;
if (vid != 0) {
flow_desc.fd_vid = vid;
flow_desc.fd_mask |= FLOW_LINK_VID;
}
if (is_primary)
flent_flags = FLOW_PRIMARY_MAC;
else
flent_flags = FLOW_VNIC_MAC;
if (first_flow) {
bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
} else {
(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
flent_flags = FLOW_NO_STATS;
}
if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
flent_flags, flent)) != 0)
return (err);
mac_misc_stat_create(*flent);
FLOW_MARK(*flent, FE_INCIPIENT);
(*flent)->fe_mcip = mcip;
if (!first_flow)
FLOW_REFHOLD(*flent);
return (0);
}
int
mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
{
flow_entry_t *flent = arg;
mac_client_impl_t *mcip = flent->fe_mcip;
uint16_t vid;
flow_desc_t flow_desc;
mac_flow_get_desc(flent, &flow_desc);
vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
flow_desc.fd_vid : VLAN_ID_NONE;
if (add) {
return (mac_bcast_add(mcip, addrp, vid,
MAC_ADDRTYPE_MULTICAST));
} else {
mac_bcast_delete(mcip, addrp, vid);
return (0);
}
}
static void
mac_update_single_active_client(mac_impl_t *mip)
{
mac_client_impl_t *client = NULL;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
rw_enter(&mip->mi_rw_lock, RW_WRITER);
if (mip->mi_nactiveclients == 1) {
for (client = mip->mi_clients_list; client != NULL;
client = client->mci_client_next) {
if (client->mci_unicast_list != NULL)
break;
}
ASSERT(client != NULL);
}
mip->mi_single_active_client = client;
rw_exit(&mip->mi_rw_lock);
}
static int
mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
mac_unicast_impl_t *muip)
{
mac_impl_t *mip = mcip->mci_mip;
boolean_t mac_started = B_FALSE;
boolean_t bcast_added = B_FALSE;
boolean_t nactiveclients_added = B_FALSE;
flow_entry_t *flent;
int err = 0;
boolean_t no_unicast;
no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
if ((err = mac_start((mac_handle_t)mip)) != 0)
goto bail;
mac_started = B_TRUE;
if (mip->mi_type->mt_brdcst_addr != NULL) {
err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
MAC_ADDRTYPE_BROADCAST);
if (err != 0)
goto bail;
bcast_added = B_TRUE;
}
flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
if (!MCIP_DATAPATH_SETUP(mcip)) {
if (mrp != NULL) {
MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
(mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
MPL_LINK_DEFAULT);
}
if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
isprimary, B_TRUE, &flent, mrp)) != 0)
goto bail;
mip->mi_nactiveclients++;
nactiveclients_added = B_TRUE;
if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
goto bail;
if (no_unicast)
goto done_setup;
ASSERT(mcip->mci_unicast != NULL);
mac_link_init_flows((mac_client_handle_t)mcip);
} else {
mac_address_t *map = mcip->mci_unicast;
ASSERT(!no_unicast);
if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
err = EINVAL;
goto bail;
}
if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
isprimary, B_FALSE, &flent, NULL)) != 0) {
goto bail;
}
if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
FLOW_FINAL_REFRELE(flent);
goto bail;
}
mac_client_bcast_refresh(mcip, mac_client_update_mcast,
(void *)flent, B_TRUE);
}
muip->mui_map = mcip->mci_unicast;
rw_enter(&mcip->mci_rw_lock, RW_WRITER);
muip->mui_next = mcip->mci_unicast_list;
mcip->mci_unicast_list = muip;
rw_exit(&mcip->mci_rw_lock);
done_setup:
mac_client_add_to_flow_list(mcip, flent);
if (nactiveclients_added)
mac_update_single_active_client(mip);
if (mip->mi_nactiveclients == 2) {
mac_capab_update((mac_handle_t)mip);
mac_virtual_link_update(mip);
}
if (flent->fe_rx_ring_group != NULL)
mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
FLOW_UNMARK(flent, FE_INCIPIENT);
if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0)
FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
mac_tx_client_unblock(mcip);
return (0);
bail:
if (bcast_added)
mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
if (nactiveclients_added)
mip->mi_nactiveclients--;
if (mac_started)
mac_stop((mac_handle_t)mip);
return (err);
}
static mac_client_impl_t *
mac_get_passive_primary_client(mac_impl_t *mip)
{
mac_client_impl_t *mcip;
for (mcip = mip->mi_clients_list; mcip != NULL;
mcip = mcip->mci_client_next) {
if (mac_is_primary_client(mcip) &&
(mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
return (mcip);
}
}
return (NULL);
}
int
i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
int err;
uint_t mac_len = mip->mi_type->mt_addr_length;
boolean_t check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
boolean_t fastpath_disabled = B_FALSE;
boolean_t is_primary = (flags & MAC_UNICAST_PRIMARY);
boolean_t is_unicast_hw = (flags & MAC_UNICAST_HW);
mac_resource_props_t *mrp;
boolean_t passive_client = B_FALSE;
mac_unicast_impl_t *muip;
boolean_t is_vnic_primary =
(flags & MAC_UNICAST_VNIC_PRIMARY);
ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != VLAN_ID_NONE)));
*diag = MAC_DIAG_NONE;
if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR)
return (ENOTSUP);
if (mip->mi_pvid != VLAN_ID_NONE && vid == mip->mi_pvid)
return (EBUSY);
if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
vid == VLAN_ID_NONE)
mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
!is_vnic_primary) {
ASSERT(mcip->mci_unicast != NULL);
if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
mcip->mci_state_flags |= MCIS_TAG_DISABLE;
if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
if ((mcip->mci_flags &
MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
(mcip->mci_flags &
MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
return (EBUSY);
}
mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
passive_client = B_TRUE;
}
mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
ASSERT(vid == VLAN_ID_NONE);
muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
muip->mui_vid = vid;
*mah = (mac_unicast_handle_t)muip;
if (passive_client)
return (EAGAIN);
return (0);
}
if ((is_vnic_primary || is_primary) &&
i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
return (ENXIO);
}
if (mcip->mci_state_flags & MCIS_IS_VNIC) {
err = mac_fastpath_disable((mac_handle_t)mip);
if (err != 0)
return (err);
fastpath_disabled = B_TRUE;
}
if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
if (fastpath_disabled)
mac_fastpath_enable((mac_handle_t)mip);
return (EBUSY);
}
if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
ASSERT(!fastpath_disabled);
if (mip->mi_nactiveclients != 0)
return (EBUSY);
if ((mip->mi_state_flags & MIS_LEGACY) &&
!(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
return (EBUSY);
}
mip->mi_state_flags |= MIS_EXCLUSIVE;
}
mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
MCIS_IS_AGGR_PORT))) {
mac_get_resources((mac_handle_t)mip, mrp);
(void) mac_client_set_resources(mch, mrp);
} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
if (is_vnic_primary) {
mac_resource_props_t *vmrp;
vmrp = MCIP_RESOURCE_PROPS(mcip);
if (vmrp->mrp_mask & MRP_RX_RINGS ||
vmrp->mrp_mask & MRP_TX_RINGS) {
if (fastpath_disabled)
mac_fastpath_enable((mac_handle_t)mip);
kmem_free(mrp, sizeof (*mrp));
return (ENOTSUP);
}
mac_get_resources((mac_handle_t)mip, mrp);
if (mrp->mrp_mask & MRP_RX_RINGS) {
vmrp->mrp_mask |= MRP_RX_RINGS;
vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
}
if (mrp->mrp_mask & MRP_TX_RINGS) {
vmrp->mrp_mask |= MRP_TX_RINGS;
vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
}
}
bcopy(MCIP_RESOURCE_PROPS(mcip), mrp, sizeof (*mrp));
}
muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
muip->mui_vid = vid;
if (is_primary || is_vnic_primary) {
mac_addr = mip->mi_addr;
} else {
if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
*diag = MAC_DIAG_MACADDR_INVALID;
err = EINVAL;
goto bail_out;
}
if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
*diag = MAC_DIAG_MACADDR_NIC;
err = EINVAL;
goto bail_out;
}
}
if (!MCIP_DATAPATH_SETUP(mcip)) {
if (is_unicast_hw) {
mcip->mci_state_flags |= MCIS_UNICAST_HW;
}
if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
mcip->mci_state_flags |= MCIS_TAG_DISABLE;
if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
} else {
ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
(mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
(mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
(mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
(mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
(mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
(mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
if (((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
!is_unicast_hw) ||
((mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
is_unicast_hw)) {
err = EINVAL;
goto bail_out;
}
}
if ((check_dups || is_primary || is_vnic_primary) &&
mac_addr_in_use(mip, mac_addr, vid)) {
if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
(mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
mac_get_passive_primary_client(mip) != NULL) {
*diag = MAC_DIAG_MACADDR_INUSE;
err = EEXIST;
goto bail_out;
}
ASSERT((mcip->mci_flags &
MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
kmem_free(mrp, sizeof (*mrp));
mcip->mci_p_unicast_list = muip;
*mah = (mac_unicast_handle_t)muip;
return (0);
}
err = mac_client_datapath_setup(mcip, vid, mac_addr, mrp,
is_primary || is_vnic_primary, muip);
if (err != 0)
goto bail_out;
kmem_free(mrp, sizeof (*mrp));
*mah = (mac_unicast_handle_t)muip;
return (0);
bail_out:
if (fastpath_disabled)
mac_fastpath_enable((mac_handle_t)mip);
if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
mip->mi_state_flags &= ~MIS_EXCLUSIVE;
if (mip->mi_state_flags & MIS_LEGACY) {
mip->mi_capab_legacy.ml_active_clear(
mip->mi_driver);
}
}
kmem_free(mrp, sizeof (*mrp));
kmem_free(muip, sizeof (mac_unicast_impl_t));
return (err);
}
int
mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
uint16_t flags, mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag,
mac_rx_t rx_fn, void *arg)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
uint_t err;
err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
if (err != 0 && err != EAGAIN)
return (err);
if (err == EAGAIN) {
if (rx_fn != NULL) {
mcip->mci_rx_p_fn = rx_fn;
mcip->mci_rx_p_arg = arg;
}
return (0);
}
if (rx_fn != NULL)
mac_rx_set(mch, rx_fn, arg);
return (err);
}
int
mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
{
mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
uint_t err;
i_mac_perim_enter(mip);
err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
i_mac_perim_exit(mip);
return (err);
}
static void
mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
flow_entry_t *flent)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
boolean_t no_unicast;
no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
if (!no_unicast) {
mac_link_release_flows(mch);
}
mip->mi_nactiveclients--;
mac_update_single_active_client(mip);
mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
rw_enter(&mip->mi_rw_lock, RW_WRITER);
flent = mcip->mci_flent;
mac_client_remove_flow_from_list(mcip, flent);
if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
rw_exit(&mip->mi_rw_lock);
mutex_enter(&flent->fe_lock);
ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
flent->fe_flags = FE_MC_NO_DATAPATH;
flow_stat_destroy(flent);
mac_misc_stat_delete(flent);
flent->fe_cb_fn = (flow_fn_t)mac_rx_def;
flent->fe_cb_arg1 = NULL;
flent->fe_cb_arg2 = NULL;
flent->fe_index = -1;
mutex_exit(&flent->fe_lock);
if (mip->mi_type->mt_brdcst_addr != NULL) {
ASSERT(muip != NULL || no_unicast);
mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
muip != NULL ? muip->mui_vid : VLAN_ID_NONE);
}
if (mip->mi_nactiveclients == 1) {
mac_capab_update((mac_handle_t)mip);
mac_virtual_link_update(mip);
}
if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
mip->mi_state_flags &= ~MIS_EXCLUSIVE;
if (mip->mi_state_flags & MIS_LEGACY)
mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
}
mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
if (muip != NULL)
kmem_free(muip, sizeof (mac_unicast_impl_t));
mac_protect_cancel_timer(mcip);
mac_protect_flush_dynamic(mcip);
bzero(&mcip->mci_misc_stat, sizeof (mcip->mci_misc_stat));
if (mcip->mci_state_flags & MCIS_IS_VNIC)
mac_fastpath_enable((mac_handle_t)mip);
mac_stop((mac_handle_t)mip);
}
int
mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
mac_unicast_impl_t *pre;
mac_impl_t *mip = mcip->mci_mip;
flow_entry_t *flent;
uint16_t mui_vid;
i_mac_perim_enter(mip);
if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
ASSERT(muip->mui_vid == VLAN_ID_NONE);
mac_tx_client_flush(mcip);
if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
if (mcip->mci_rx_p_fn != NULL) {
mac_rx_set(mch, mcip->mci_rx_p_fn,
mcip->mci_rx_p_arg);
mcip->mci_rx_p_fn = NULL;
mcip->mci_rx_p_arg = NULL;
}
kmem_free(muip, sizeof (mac_unicast_impl_t));
i_mac_perim_exit(mip);
return (0);
}
mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
kmem_free(muip, sizeof (mac_unicast_impl_t));
i_mac_perim_exit(mip);
return (0);
}
ASSERT(muip != NULL);
if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
ASSERT(mcip->mci_p_unicast_list == muip);
mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
mcip->mci_p_unicast_list = NULL;
mcip->mci_rx_p_fn = NULL;
mcip->mci_rx_p_arg = NULL;
mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
kmem_free(muip, sizeof (mac_unicast_impl_t));
i_mac_perim_exit(mip);
return (0);
}
pre = mcip->mci_unicast_list;
if (muip == pre) {
mcip->mci_unicast_list = muip->mui_next;
} else {
while ((pre->mui_next != NULL) && (pre->mui_next != muip))
pre = pre->mui_next;
ASSERT(pre->mui_next == muip);
rw_enter(&mcip->mci_rw_lock, RW_WRITER);
pre->mui_next = muip->mui_next;
rw_exit(&mcip->mci_rw_lock);
}
if (!mac_client_single_rcvr(mcip)) {
flent = mac_client_get_flow(mcip, muip);
VERIFY3P(flent, !=, NULL);
if (flent == mcip->mci_flent)
flent = mac_client_swap_mciflent(mcip);
mac_client_remove_flow_from_list(mcip, flent);
mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
mac_client_bcast_refresh(mcip, mac_client_update_mcast,
(void *)flent, B_FALSE);
if (mip->mi_type->mt_brdcst_addr != NULL) {
mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
muip->mui_vid);
}
FLOW_FINAL_REFRELE(flent);
ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
if (mcip->mci_state_flags & MCIS_IS_VNIC)
mac_fastpath_enable((mac_handle_t)mip);
mac_stop((mac_handle_t)mip);
i_mac_perim_exit(mip);
return (0);
}
mui_vid = muip->mui_vid;
mac_client_datapath_teardown(mch, muip, flent);
if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) &&
mui_vid == VLAN_ID_NONE) {
mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
} else {
i_mac_perim_exit(mip);
return (0);
}
mcip = mac_get_passive_primary_client(mip);
if (mcip != NULL) {
mac_resource_props_t *mrp;
mac_unicast_impl_t *muip;
mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
mac_get_resources((mac_handle_t)mip, mrp);
(void) mac_client_set_resources(mch, mrp);
ASSERT(mcip->mci_p_unicast_list != NULL);
muip = mcip->mci_p_unicast_list;
mcip->mci_p_unicast_list = NULL;
if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
mip->mi_addr, mrp, B_TRUE, muip) == 0) {
if (mcip->mci_rx_p_fn != NULL) {
mac_rx_set(mch, mcip->mci_rx_p_fn,
mcip->mci_rx_p_arg);
mcip->mci_rx_p_fn = NULL;
mcip->mci_rx_p_arg = NULL;
}
} else {
kmem_free(muip, sizeof (mac_unicast_impl_t));
}
kmem_free(mrp, sizeof (*mrp));
}
i_mac_perim_exit(mip);
return (0);
}
int
mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
flow_entry_t *flent = mcip->mci_flent_list;
flow_entry_t *prev_fe = NULL;
uint16_t vid;
int err = 0;
if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
mip->mi_pdata)) != 0)
return (err);
i_mac_perim_enter(mip);
while (flent != NULL) {
vid = i_mac_flow_vid(flent);
err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
MAC_ADDRTYPE_MULTICAST);
if (err != 0)
break;
prev_fe = flent;
flent = flent->fe_client_next;
}
if (flent != NULL && prev_fe != NULL) {
flent = mcip->mci_flent_list;
while (flent != prev_fe->fe_client_next) {
vid = i_mac_flow_vid(flent);
mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
flent = flent->fe_client_next;
}
}
i_mac_perim_exit(mip);
return (err);
}
void
mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
flow_entry_t *flent;
uint16_t vid;
i_mac_perim_enter(mip);
for (flent = mcip->mci_flent_list; flent != NULL;
flent = flent->fe_client_next) {
vid = i_mac_flow_vid(flent);
mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
}
i_mac_perim_exit(mip);
}
int
mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
mac_promisc_impl_t *mpip;
mac_cb_info_t *mcbi;
int rc;
boolean_t no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
boolean_t rx_only = ((flags & MAC_PROMISC_FLAGS_RX_ONLY) != 0);
boolean_t tx_only = ((flags & MAC_PROMISC_FLAGS_TX_ONLY) != 0);
if (no_tx_loop && tx_only)
return (EINVAL);
if (rx_only && tx_only)
return (EINVAL);
i_mac_perim_enter(mip);
if ((rc = mac_start((mac_handle_t)mip)) != 0) {
i_mac_perim_exit(mip);
return (rc);
}
if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
type == MAC_CLIENT_PROMISC_ALL &&
(mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED)) {
type = MAC_CLIENT_PROMISC_FILTERED;
}
if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
mac_stop((mac_handle_t)mip);
i_mac_perim_exit(mip);
return (rc);
}
}
mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
mpip->mpi_type = type;
mpip->mpi_fn = fn;
mpip->mpi_arg = arg;
mpip->mpi_mcip = mcip;
mpip->mpi_no_tx_loop = no_tx_loop;
mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
mpip->mpi_strip_vlan_tag =
((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
mpip->mpi_rx_only = rx_only;
mpip->mpi_tx_only = tx_only;
mcbi = &mip->mi_promisc_cb_info;
mutex_enter(mcbi->mcbi_lockp);
mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
&mpip->mpi_mci_link);
mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
&mpip->mpi_mi_link);
mutex_exit(mcbi->mcbi_lockp);
*mphp = (mac_promisc_handle_t)mpip;
if (mcip->mci_state_flags & MCIS_IS_VNIC) {
mac_impl_t *umip = mcip->mci_upper_mip;
ASSERT(umip != NULL);
mac_vnic_secondary_update(umip);
}
i_mac_perim_exit(mip);
return (0);
}
void
mac_promisc_remove(mac_promisc_handle_t mph)
{
mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
mac_client_impl_t *mcip = mpip->mpi_mcip;
mac_impl_t *mip = mcip->mci_mip;
mac_cb_info_t *mcbi;
int rv;
i_mac_perim_enter(mip);
if (!(mpip->mpi_no_phys)) {
if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
" mode because of error 0x%x", mip->mi_name, rv);
}
}
mcbi = &mip->mi_promisc_cb_info;
mutex_enter(mcbi->mcbi_lockp);
if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
&mpip->mpi_mi_link)) {
VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
&mcip->mci_promisc_list, &mpip->mpi_mci_link));
kmem_cache_free(mac_promisc_impl_cache, mpip);
} else {
mac_callback_remove_wait(&mip->mi_promisc_cb_info);
}
if (mcip->mci_state_flags & MCIS_IS_VNIC) {
mac_impl_t *umip = mcip->mci_upper_mip;
ASSERT(umip != NULL);
mac_vnic_secondary_update(umip);
}
mutex_exit(mcbi->mcbi_lockp);
mac_stop((mac_handle_t)mip);
i_mac_perim_exit(mip);
}
#define MAC_TX_TRY_HOLD(mcip, mytx, error) \
{ \
(error) = 0; \
(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
mutex_enter(&(mytx)->pcpu_tx_lock); \
if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) { \
(mytx)->pcpu_tx_refcnt++; \
} else { \
(error) = -1; \
} \
mutex_exit(&(mytx)->pcpu_tx_lock); \
}
#define MAC_TX_RELE(mcip, mytx) { \
mutex_enter(&(mytx)->pcpu_tx_lock); \
if (--(mytx)->pcpu_tx_refcnt == 0 && \
(mcip)->mci_tx_flag & MCI_TX_QUIESCE) { \
mutex_exit(&(mytx)->pcpu_tx_lock); \
mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock); \
cv_signal(&(mcip)->mci_tx_cv); \
mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock); \
} else { \
mutex_exit(&(mytx)->pcpu_tx_lock); \
} \
}
mac_tx_cookie_t
mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
uint16_t flag, mblk_t **ret_mp)
{
mac_tx_cookie_t cookie = 0;
int error;
mac_tx_percpu_t *mytx;
mac_soft_ring_set_t *srs;
flow_entry_t *flent;
boolean_t is_subflow = B_FALSE;
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
mac_srs_tx_t *srs_tx;
if (!(flag & MAC_TX_NO_HOLD)) {
MAC_TX_TRY_HOLD(mcip, mytx, error);
if (error != 0) {
freemsgchain(mp_chain);
return (0);
}
}
if ((mcip->mci_flent->
fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
(mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
goto done;
if (mcip->mci_subflow_tab != NULL &&
mcip->mci_subflow_tab->ft_flow_count > 0 &&
mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
FLOW_OUTBOUND, &flent) == 0) {
ASSERT(flent != NULL);
is_subflow = B_TRUE;
} else {
flent = mcip->mci_flent;
}
srs = flent->fe_tx_srs;
if (srs == NULL) {
freemsgchain(mp_chain);
goto done;
}
srs_tx = &srs->srs_tx;
if (srs_tx->st_mode == SRS_TX_DEFAULT &&
(srs->srs_state & SRS_ENQUEUED) == 0 &&
mip->mi_nactiveclients == 1 &&
mp_chain->b_next == NULL &&
(DB_CKSUMFLAGS(mp_chain) & HW_LSO) == 0) {
uint64_t obytes;
if (mip->mi_nclients > 1) {
if (MAC_VID_CHECK_NEEDED(mcip)) {
int err = 0;
MAC_VID_CHECK(mcip, mp_chain, err);
if (err != 0) {
freemsg(mp_chain);
mcip->mci_misc_stat.mms_txerrors++;
goto done;
}
}
if (MAC_TAG_NEEDED(mcip)) {
mp_chain = mac_add_vlan_tag(mp_chain, 0,
mac_client_vid(mch));
if (mp_chain == NULL) {
mcip->mci_misc_stat.mms_txerrors++;
goto done;
}
}
}
obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
msgdsize(mp_chain));
mp_chain = mac_provider_tx(mip,
(mac_ring_handle_t)srs_tx->st_arg2, mp_chain, mcip);
if (mp_chain == NULL) {
cookie = 0;
SRS_TX_STAT_UPDATE(srs, opackets, 1);
SRS_TX_STAT_UPDATE(srs, obytes, obytes);
} else {
mutex_enter(&srs->srs_lock);
cookie = mac_tx_srs_no_desc(srs, mp_chain,
flag, ret_mp);
mutex_exit(&srs->srs_lock);
}
} else {
mblk_t *mp = mp_chain;
mblk_t *new_head = NULL;
mblk_t *new_tail = NULL;
while (mp != NULL) {
mblk_t *next = mp->b_next;
mblk_t *tail = NULL;
const uint16_t needed =
(DB_CKSUMFLAGS(mp) ^ mip->mi_tx_cksum_flags) &
DB_CKSUMFLAGS(mp);
mp->b_next = NULL;
if ((needed & (HCK_TX_FLAGS | HW_LSO_FLAGS)) != 0) {
mac_emul_t emul = 0;
if (needed & HCK_IPV4_HDRCKSUM)
emul |= MAC_IPCKSUM_EMUL;
if (needed & (HCK_PARTIALCKSUM | HCK_FULLCKSUM))
emul |= MAC_HWCKSUM_EMUL;
if (needed & HW_LSO)
emul = MAC_LSO_EMUL;
mac_hw_emul(&mp, &tail, NULL, emul);
if (mp == NULL) {
mp = next;
continue;
}
}
if (new_head == NULL) {
new_head = mp;
} else {
new_tail->b_next = mp;
}
new_tail = (tail == NULL) ? mp : tail;
mp = next;
}
if (new_head == NULL) {
cookie = 0;
goto done;
}
cookie = srs_tx->st_func(srs, new_head, hint, flag, ret_mp);
}
done:
if (is_subflow)
FLOW_REFRELE(flent);
if (!(flag & MAC_TX_NO_HOLD))
MAC_TX_RELE(mcip, mytx);
return (cookie);
}
boolean_t
mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_soft_ring_set_t *mac_srs;
mac_soft_ring_t *sringp;
boolean_t blocked = B_FALSE;
mac_tx_percpu_t *mytx;
int err;
int i;
MAC_TX_TRY_HOLD(mcip, mytx, err);
if (err != 0)
return (B_TRUE);
if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
MAC_TX_RELE(mcip, mytx);
return (B_FALSE);
}
mutex_enter(&mac_srs->srs_lock);
if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
if (cookie != 0) {
sringp = (mac_soft_ring_t *)cookie;
mutex_enter(&sringp->s_ring_lock);
if (sringp->s_ring_state & S_RING_TX_HIWAT)
blocked = B_TRUE;
mutex_exit(&sringp->s_ring_lock);
} else {
for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
sringp = mac_srs->srs_tx_soft_rings[i];
mutex_enter(&sringp->s_ring_lock);
if (sringp->s_ring_state & S_RING_TX_HIWAT) {
blocked = B_TRUE;
mutex_exit(&sringp->s_ring_lock);
break;
}
mutex_exit(&sringp->s_ring_lock);
}
}
} else {
blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
}
mutex_exit(&mac_srs->srs_lock);
MAC_TX_RELE(mcip, mytx);
return (blocked);
}
boolean_t
mac_is_primary_client(mac_client_impl_t *mcip)
{
return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
}
void
mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
{
mac_impl_t *mip = (mac_impl_t *)mh;
int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
(cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
mac_ndd_ioctl(mip, wq, bp);
return;
}
if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
mip->mi_ioctl(mip->mi_driver, wq, bp);
else
miocnak(wq, bp, 0, EINVAL);
}
link_state_t
mac_link_get(mac_handle_t mh)
{
return (((mac_impl_t *)mh)->mi_linkstate);
}
mac_notify_handle_t
mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_notify_cb_t *mncb;
mac_cb_info_t *mcbi;
mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
mncb->mncb_fn = notify_fn;
mncb->mncb_arg = arg;
mncb->mncb_mip = mip;
mncb->mncb_link.mcb_objp = mncb;
mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
mcbi = &mip->mi_notify_cb_info;
i_mac_perim_enter(mip);
mutex_enter(mcbi->mcbi_lockp);
mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
&mncb->mncb_link);
mutex_exit(mcbi->mcbi_lockp);
i_mac_perim_exit(mip);
return ((mac_notify_handle_t)mncb);
}
void
mac_notify_remove_wait(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_cb_info_t *mcbi = &mip->mi_notify_cb_info;
mutex_enter(mcbi->mcbi_lockp);
mac_callback_remove_wait(&mip->mi_notify_cb_info);
mutex_exit(mcbi->mcbi_lockp);
}
int
mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
{
mac_notify_cb_t *mncb = (mac_notify_cb_t *)mnh;
mac_impl_t *mip = mncb->mncb_mip;
mac_cb_info_t *mcbi;
int err = 0;
mcbi = &mip->mi_notify_cb_info;
i_mac_perim_enter(mip);
mutex_enter(mcbi->mcbi_lockp);
ASSERT(mncb->mncb_link.mcb_objp == mncb);
if (mac_callback_remove(&mip->mi_notify_cb_info,
&mip->mi_notify_cb_list, &mncb->mncb_link)) {
kmem_free(mncb, sizeof (mac_notify_cb_t));
} else {
err = EBUSY;
}
mutex_exit(mcbi->mcbi_lockp);
i_mac_perim_exit(mip);
if (err != 0 && wait) {
mac_notify_remove_wait((mac_handle_t)mip);
return (0);
}
return (err);
}
void
mac_resource_set(mac_client_handle_t mch, mac_resource_cb_t *rcbs,
boolean_t is_v6)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
if (is_v6) {
mcip->mci_rcb6 = *rcbs;
} else {
mcip->mci_rcb4 = *rcbs;
}
}
void
mac_resource_clear(mac_client_handle_t mch, boolean_t is_v6)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
if (is_v6) {
bzero(&mcip->mci_rcb6, sizeof (mcip->mci_rcb6));
} else {
bzero(&mcip->mci_rcb4, sizeof (mcip->mci_rcb4));
}
}
void
mac_client_poll_enable(mac_client_handle_t mch, boolean_t is_v6)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_soft_ring_set_t *mac_srs;
flow_entry_t *flent;
int i;
flent = mcip->mci_flent;
ASSERT(flent != NULL);
mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
ASSERT(mac_srs->srs_mcip == mcip);
mac_srs_client_poll_enable(mcip, mac_srs, is_v6);
}
}
void
mac_client_poll_disable(mac_client_handle_t mch, boolean_t is_v6)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_soft_ring_set_t *mac_srs;
flow_entry_t *flent;
int i;
flent = mcip->mci_flent;
ASSERT(flent != NULL);
mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
ASSERT(mac_srs->srs_mcip == mcip);
mac_srs_client_poll_disable(mcip, mac_srs, is_v6);
}
}
int
mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
int err = 0;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
mcip->mci_upper_mip : mip, mrp)) != 0) {
return (err);
}
if (MCIP_DATAPATH_SETUP(mcip))
mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
return (0);
}
int
mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = mcip->mci_mip;
int err = 0;
i_mac_perim_enter(mip);
if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
err = mac_resource_ctl_set(mch, mrp);
if (err != 0)
goto done;
}
if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
err = mac_cpu_set(mch, mrp);
if (err != 0)
goto done;
}
if (mrp->mrp_mask & MRP_PROTECT) {
err = mac_protect_set(mch, mrp);
if (err != 0)
goto done;
}
if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
err = mac_resource_ctl_set(mch, mrp);
done:
i_mac_perim_exit(mip);
return (err);
}
void
mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_resource_props_t *mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
}
void
mac_client_get_effective_resources(mac_client_handle_t mch,
mac_resource_props_t *mrp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_resource_props_t *mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
}
static void
mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
boolean_t loopback, boolean_t local)
{
mblk_t *mp_next;
if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
mblk_t *mp_copy;
mp_copy = copymsg(mp);
if (mp_copy == NULL)
return;
if (mpip->mpi_strip_vlan_tag) {
mp_copy = mac_strip_vlan_tag_chain(mp_copy);
if (mp_copy == NULL)
return;
}
for (mblk_t *tmp = mp_copy; tmp != NULL; tmp = mp_next) {
mp_next = tmp->b_next;
tmp->b_next = NULL;
mpip->mpi_fn(mpip->mpi_arg, NULL, tmp, loopback);
}
return;
}
mp_next = mp->b_next;
mp->b_next = NULL;
mpip->mpi_fn(mpip->mpi_arg, NULL, mp, loopback);
mp->b_next = mp_next;
}
static uint16_t
mac_ether_vid(mblk_t *mp)
{
struct ether_header *eth = (struct ether_header *)mp->b_rptr;
if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
struct ether_vlan_header *t_evhp =
(struct ether_vlan_header *)mp->b_rptr;
return (VLAN_ID(ntohs(t_evhp->ether_tci)));
}
return (0);
}
static boolean_t
mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
{
mac_header_info_t hdr_info;
if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
return (B_FALSE);
return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
(hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
}
void
mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
mac_client_impl_t *sender, boolean_t local)
{
mac_promisc_impl_t *mpip;
mac_cb_t *mcb;
mblk_t *mp;
boolean_t is_mcast, is_sender;
MAC_PROMISC_WALKER_INC(mip);
for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
is_mcast = mac_is_mcast(mip, mp);
for (mcb = mip->mi_promisc_list; mcb != NULL;
mcb = mcb->mcb_nextp) {
mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
is_sender = (mpip->mpi_mcip == sender);
if (sender != NULL && mpip->mpi_rx_only)
continue;
if (sender == NULL && mpip->mpi_tx_only)
continue;
if (is_sender && mpip->mpi_no_tx_loop)
continue;
if (mpip->mpi_fn == NULL)
continue;
if (is_mcast &&
mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
!mac_client_check_flow_vid(mpip->mpi_mcip,
mac_ether_vid(mp)))
continue;
if (is_sender ||
mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
is_mcast) {
mac_promisc_dispatch_one(mpip, mp, is_sender,
local);
}
}
}
MAC_PROMISC_WALKER_DCR(mip);
}
void
mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
{
mac_impl_t *mip = mcip->mci_mip;
mac_promisc_impl_t *mpip;
boolean_t is_mcast;
mblk_t *mp;
mac_cb_t *mcb;
MAC_PROMISC_WALKER_INC(mip);
for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
is_mcast = mac_is_mcast(mip, mp);
for (mcb = mcip->mci_promisc_list; mcb != NULL;
mcb = mcb->mcb_nextp) {
mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
if (!mpip->mpi_tx_only &&
mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
!is_mcast) {
mac_promisc_dispatch_one(mpip, mp, B_FALSE,
B_FALSE);
}
}
}
MAC_PROMISC_WALKER_DCR(mip);
}
void
mac_margin_get(mac_handle_t mh, uint32_t *marginp)
{
mac_impl_t *mip = (mac_impl_t *)mh;
rw_enter(&(mip->mi_rw_lock), RW_READER);
*marginp = mip->mi_margin;
rw_exit(&(mip->mi_rw_lock));
}
typedef struct i_mac_info_state_s {
const char *mi_name;
mac_info_t *mi_infop;
} i_mac_info_state_t;
static uint_t
i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
{
i_mac_info_state_t *statep = arg;
mac_impl_t *mip = (mac_impl_t *)val;
if (mip->mi_state_flags & MIS_DISABLED)
return (MH_WALK_CONTINUE);
if (strcmp(statep->mi_name,
ddi_driver_name(mip->mi_dip)) != 0)
return (MH_WALK_CONTINUE);
statep->mi_infop = &mip->mi_info;
return (MH_WALK_TERMINATE);
}
boolean_t
mac_info_get(const char *name, mac_info_t *minfop)
{
i_mac_info_state_t state;
rw_enter(&i_mac_impl_lock, RW_READER);
state.mi_name = name;
state.mi_infop = NULL;
mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
if (state.mi_infop == NULL) {
rw_exit(&i_mac_impl_lock);
return (B_FALSE);
}
*minfop = *state.mi_infop;
rw_exit(&i_mac_impl_lock);
return (B_TRUE);
}
boolean_t
i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
{
mac_impl_t *mip = (mac_impl_t *)mh;
if (mip->mi_bridge_link != NULL) {
return (cap == MAC_CAPAB_NO_ZCOPY);
} else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB) {
return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
} else {
return (B_FALSE);
}
}
boolean_t
mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
{
mac_impl_t *mip = (mac_impl_t *)mh;
if (mip->mi_nactiveclients > 1) {
switch (cap) {
case MAC_CAPAB_NO_ZCOPY:
return (B_TRUE);
case MAC_CAPAB_LEGACY:
case MAC_CAPAB_HCKSUM:
case MAC_CAPAB_LSO:
case MAC_CAPAB_NO_NATIVEVLAN:
break;
default:
return (B_FALSE);
}
}
return (i_mac_capab_get(mh, cap, cap_data));
}
boolean_t
mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
mip->mi_pdata));
}
mblk_t *
mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
size_t extra_len)
{
mac_impl_t *mip = (mac_impl_t *)mh;
const uint8_t *hdr_daddr;
hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
mip->mi_pdata, payload, extra_len));
}
int
mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
mhip));
}
int
mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
{
mac_impl_t *mip = (mac_impl_t *)mh;
boolean_t is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
int err = 0;
ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
if ((err = mac_header_info(mh, mp, mhip)) != 0)
return (err);
if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
struct ether_vlan_header *evhp;
uint16_t sap;
mblk_t *tmp = NULL;
size_t size;
size = sizeof (struct ether_vlan_header);
if (MBLKL(mp) < size) {
if ((tmp = msgpullup(mp, size)) == NULL)
return (EINVAL);
mp = tmp;
}
evhp = (struct ether_vlan_header *)mp->b_rptr;
sap = ntohs(evhp->ether_type);
(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
mhip->mhi_tci = ntohs(evhp->ether_tci);
mhip->mhi_istagged = B_TRUE;
freemsg(tmp);
if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
return (EINVAL);
} else {
mhip->mhi_istagged = B_FALSE;
mhip->mhi_tci = 0;
}
return (0);
}
mblk_t *
mac_header_cook(mac_handle_t mh, mblk_t *mp)
{
mac_impl_t *mip = (mac_impl_t *)mh;
if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
if (DB_REF(mp) > 1) {
mblk_t *newmp = copymsg(mp);
if (newmp == NULL)
return (NULL);
freemsg(mp);
mp = newmp;
}
return (mip->mi_type->mt_ops.mtops_header_cook(mp,
mip->mi_pdata));
}
return (mp);
}
mblk_t *
mac_header_uncook(mac_handle_t mh, mblk_t *mp)
{
mac_impl_t *mip = (mac_impl_t *)mh;
if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
if (DB_REF(mp) > 1) {
mblk_t *newmp = copymsg(mp);
if (newmp == NULL)
return (NULL);
freemsg(mp);
mp = newmp;
}
return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
mip->mi_pdata));
}
return (mp);
}
uint_t
mac_addr_len(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_type->mt_addr_length);
}
boolean_t
mac_is_vnic(mac_handle_t mh)
{
return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC) != 0);
}
boolean_t
mac_is_overlay(mac_handle_t mh)
{
return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_OVERLAY) != 0);
}
mac_handle_t
mac_get_lower_mac_handle(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
ASSERT(mac_is_vnic(mh));
return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
}
boolean_t
mac_is_vnic_primary(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
ASSERT(mac_is_vnic(mh));
return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
VNIC_MAC_ADDR_TYPE_PRIMARY);
}
void
mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
boolean_t is_user_flow)
{
if (nmrp != NULL && cmrp != NULL) {
if (nmrp->mrp_mask & MRP_PRIORITY) {
if (nmrp->mrp_priority == MPL_RESET) {
cmrp->mrp_mask &= ~MRP_PRIORITY;
if (is_user_flow) {
cmrp->mrp_priority =
MPL_SUBFLOW_DEFAULT;
} else {
cmrp->mrp_priority = MPL_LINK_DEFAULT;
}
} else {
cmrp->mrp_mask |= MRP_PRIORITY;
cmrp->mrp_priority = nmrp->mrp_priority;
}
}
if (nmrp->mrp_mask & MRP_MAXBW) {
if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
cmrp->mrp_mask &= ~MRP_MAXBW;
cmrp->mrp_maxbw = 0;
} else {
cmrp->mrp_mask |= MRP_MAXBW;
cmrp->mrp_maxbw = nmrp->mrp_maxbw;
}
}
if (nmrp->mrp_mask & MRP_CPUS)
MAC_COPY_CPUS(nmrp, cmrp);
if (nmrp->mrp_mask & MRP_POOL) {
if (strlen(nmrp->mrp_pool) == 0) {
cmrp->mrp_mask &= ~MRP_POOL;
bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
} else {
cmrp->mrp_mask |= MRP_POOL;
(void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
sizeof (cmrp->mrp_pool));
}
}
if (nmrp->mrp_mask & MRP_PROTECT)
mac_protect_update(nmrp, cmrp);
if (nmrp->mrp_mask & MRP_RX_RINGS) {
if (nmrp->mrp_mask & MRP_RINGS_RESET) {
cmrp->mrp_mask &= ~MRP_RX_RINGS;
if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
cmrp->mrp_nrxrings = 0;
} else {
cmrp->mrp_mask |= MRP_RX_RINGS;
cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
}
}
if (nmrp->mrp_mask & MRP_TX_RINGS) {
if (nmrp->mrp_mask & MRP_RINGS_RESET) {
cmrp->mrp_mask &= ~MRP_TX_RINGS;
if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
cmrp->mrp_ntxrings = 0;
} else {
cmrp->mrp_mask |= MRP_TX_RINGS;
cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
}
}
if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
}
}
int
i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_client_impl_t *mcip;
int err = 0;
uint32_t resmask, newresmask;
mac_resource_props_t *tmrp, *umrp;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
err = mac_validate_props(mip, mrp);
if (err != 0)
return (err);
umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
resmask = umrp->mrp_mask;
mac_update_resources(mrp, umrp, B_FALSE);
newresmask = umrp->mrp_mask;
if (resmask == 0 && newresmask != 0) {
if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
kmem_free(umrp, sizeof (*umrp));
return (err);
}
}
tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
bcopy(mrp, tmrp, sizeof (*tmrp));
mcip = mac_primary_client_handle(mip);
if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
} else if ((mrp->mrp_mask & MRP_RX_RINGS ||
mrp->mrp_mask & MRP_TX_RINGS)) {
mac_client_impl_t *vmcip;
for (vmcip = mip->mi_clients_list; vmcip != NULL;
vmcip = vmcip->mci_client_next) {
if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
mac_client_vid((mac_client_handle_t)vmcip) !=
VLAN_ID_NONE) {
break;
}
}
if (vmcip != NULL) {
mac_resource_props_t *omrp;
mac_resource_props_t *vmrp;
omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
vmrp = MCIP_RESOURCE_PROPS(vmcip);
if (mrp->mrp_mask & MRP_RX_RINGS) {
if (mrp->mrp_mask & MRP_RINGS_RESET) {
vmrp->mrp_mask &= ~MRP_RX_RINGS;
if (vmrp->mrp_mask &
MRP_RXRINGS_UNSPEC) {
vmrp->mrp_mask &=
~MRP_RXRINGS_UNSPEC;
}
vmrp->mrp_nrxrings = 0;
} else {
vmrp->mrp_mask |= MRP_RX_RINGS;
vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
}
}
if (mrp->mrp_mask & MRP_TX_RINGS) {
if (mrp->mrp_mask & MRP_RINGS_RESET) {
vmrp->mrp_mask &= ~MRP_TX_RINGS;
if (vmrp->mrp_mask &
MRP_TXRINGS_UNSPEC) {
vmrp->mrp_mask &=
~MRP_TXRINGS_UNSPEC;
}
vmrp->mrp_ntxrings = 0;
} else {
vmrp->mrp_mask |= MRP_TX_RINGS;
vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
}
}
if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
if ((err = mac_client_set_rings_prop(vmcip, mrp,
omrp)) != 0) {
bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
sizeof (*omrp));
} else {
mac_set_prim_vlan_rings(mip, vmrp);
}
kmem_free(omrp, sizeof (*omrp));
}
}
if (err == 0) {
bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
if (resmask != 0 && newresmask == 0)
mac_fastpath_enable((mac_handle_t)mip);
} else if (resmask == 0 && newresmask != 0) {
mac_fastpath_enable((mac_handle_t)mip);
}
kmem_free(tmrp, sizeof (*tmrp));
kmem_free(umrp, sizeof (*umrp));
return (err);
}
int
mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
{
int err;
i_mac_perim_enter((mac_impl_t *)mh);
err = i_mac_set_resources(mh, mrp);
i_mac_perim_exit((mac_impl_t *)mh);
return (err);
}
void
mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_client_impl_t *mcip;
mcip = mac_primary_client_handle(mip);
if (mcip != NULL) {
mac_client_get_resources((mac_client_handle_t)mcip, mrp);
return;
}
bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
}
void
mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_client_impl_t *mcip;
mcip = mac_primary_client_handle(mip);
if (mcip != NULL) {
mac_client_get_effective_resources((mac_client_handle_t)mcip,
mrp);
return;
}
bzero(mrp, sizeof (mac_resource_props_t));
}
int
mac_set_pvid(mac_handle_t mh, uint16_t pvid)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_client_impl_t *mcip;
mac_unicast_impl_t *muip;
i_mac_perim_enter(mip);
if (pvid != 0) {
for (mcip = mip->mi_clients_list; mcip != NULL;
mcip = mcip->mci_client_next) {
for (muip = mcip->mci_unicast_list; muip != NULL;
muip = muip->mui_next) {
if (muip->mui_vid == pvid) {
i_mac_perim_exit(mip);
return (EBUSY);
}
}
}
}
mip->mi_pvid = pvid;
i_mac_perim_exit(mip);
return (0);
}
uint16_t
mac_get_pvid(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_pvid);
}
uint32_t
mac_get_llimit(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_llimit);
}
uint32_t
mac_get_ldecay(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_ldecay);
}
int
mac_rename_primary(mac_handle_t mh, const char *new_name)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_client_impl_t *cur_clnt = NULL;
flow_entry_t *fep;
i_mac_perim_enter(mip);
if (mip->mi_state_flags & MIS_IS_VNIC) {
mac_client_impl_t *mcip = mac_vnic_lower(mip);
ASSERT(new_name != NULL);
mac_rename_flow_names(mcip, new_name);
mac_stat_rename(mcip);
goto done;
}
if (mip->mi_state_flags & MIS_IS_AGGR) {
mac_capab_aggr_t aggr_cap;
mac_rename_fn_t rename_fn;
boolean_t ret;
ASSERT(new_name != NULL);
ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
(void *)(&aggr_cap));
ASSERT(ret == B_TRUE);
rename_fn = aggr_cap.mca_rename_fn;
rename_fn(new_name, mip->mi_driver);
}
for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
cur_clnt = cur_clnt->mci_client_next) {
if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
if (new_name != NULL) {
char *str_st = cur_clnt->mci_name;
char *str_del = strchr(str_st, '-');
ASSERT(str_del != NULL);
bzero(str_del + 1, MAXNAMELEN -
(str_del - str_st + 1));
bcopy(new_name, str_del + 1,
strlen(new_name));
}
fep = cur_clnt->mci_flent;
mac_rename_flow(fep, cur_clnt->mci_name);
break;
} else if (new_name != NULL &&
cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
mac_rename_flow_names(cur_clnt, new_name);
break;
}
}
if (mip->mi_state_flags & MIS_IS_AGGR)
mac_pseudo_ring_stat_rename(mip);
done:
i_mac_perim_exit(mip);
return (0);
}
static void
mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
{
flow_entry_t *flent;
uint16_t vid;
char flowname[MAXFLOWNAMELEN];
mac_impl_t *mip = mcip->mci_mip;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
rw_enter(&mip->mi_rw_lock, RW_WRITER);
(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
rw_exit(&mip->mi_rw_lock);
mac_rename_flow(mcip->mci_flent, new_name);
if (mcip->mci_nflents == 1)
return;
for (flent = mcip->mci_flent_list; flent != NULL;
flent = flent->fe_client_next) {
if (flent != mcip->mci_flent) {
vid = i_mac_flow_vid(flent);
(void) sprintf(flowname, "%s%u", new_name, vid);
mac_flow_set_name(flent, flowname);
}
}
}
static void
mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
{
ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
rw_enter(&mcip->mci_rw_lock, RW_WRITER);
mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
flent->fe_client_next = mcip->mci_flent_list;
mcip->mci_flent_list = flent;
mcip->mci_nflents++;
if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
mcip->mci_nvids++;
rw_exit(&mcip->mci_rw_lock);
}
static void
mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
{
flow_entry_t *fe = mcip->mci_flent_list;
flow_entry_t *prev_fe = NULL;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
rw_enter(&mcip->mci_rw_lock, RW_WRITER);
mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
while ((fe != NULL) && (fe != flent)) {
prev_fe = fe;
fe = fe->fe_client_next;
}
ASSERT(fe != NULL);
if (prev_fe == NULL) {
mcip->mci_flent_list = fe->fe_client_next;
} else {
prev_fe->fe_client_next = fe->fe_client_next;
}
mcip->mci_nflents--;
if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
mcip->mci_nvids--;
rw_exit(&mcip->mci_rw_lock);
}
boolean_t
mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
{
flow_entry_t *flent;
uint16_t mci_vid;
uint32_t cache = mcip->mci_vidcache;
if (MCIP_VIDCACHE_ISVALID(cache) && MCIP_VIDCACHE_VID(cache) == vid)
return (MCIP_VIDCACHE_BOOL(cache) ? B_TRUE : B_FALSE);
rw_enter(&mcip->mci_rw_lock, RW_WRITER);
for (flent = mcip->mci_flent_list; flent != NULL;
flent = flent->fe_client_next) {
mci_vid = i_mac_flow_vid(flent);
if (vid == mci_vid) {
mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_TRUE);
rw_exit(&mcip->mci_rw_lock);
return (B_TRUE);
}
}
mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_FALSE);
rw_exit(&mcip->mci_rw_lock);
return (B_FALSE);
}
static flow_entry_t *
mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
{
mac_address_t *map = mcip->mci_unicast;
flow_entry_t *flent;
uint16_t vid;
flow_desc_t flow_desc;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
mac_flow_get_desc(mcip->mci_flent, &flow_desc);
if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
return (NULL);
for (flent = mcip->mci_flent_list; flent != NULL;
flent = flent->fe_client_next) {
vid = i_mac_flow_vid(flent);
if (vid == muip->mui_vid) {
return (flent);
}
}
return (NULL);
}
static flow_entry_t *
mac_client_swap_mciflent(mac_client_impl_t *mcip)
{
flow_entry_t *flent = mcip->mci_flent;
flow_tab_t *ft = flent->fe_flow_tab;
flow_entry_t *flent1;
flow_desc_t fl_desc;
char fl_name[MAXFLOWNAMELEN];
int err;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
ASSERT(mcip->mci_nflents > 1);
flent1 = mcip->mci_flent_list->fe_client_next;
ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
mac_flow_remove(ft, flent, B_TRUE);
mac_flow_remove(ft, flent1, B_TRUE);
bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
mutex_enter(&flent->fe_lock);
bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
sizeof (flow_desc_t));
bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
mutex_exit(&flent->fe_lock);
mutex_enter(&flent1->fe_lock);
bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
mutex_exit(&flent1->fe_lock);
err = mac_flow_add(ft, flent);
ASSERT(err == 0);
err = mac_flow_add(ft, flent1);
ASSERT(err == 0);
return (flent1);
}
static boolean_t
mac_client_single_rcvr(mac_client_impl_t *mcip)
{
return (mcip->mci_nflents == 1);
}
int
mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
{
boolean_t reset;
uint32_t rings_needed;
uint32_t rings_avail;
mac_group_type_t gtype;
mac_resource_props_t *mip_mrp;
if (mrp == NULL)
return (0);
if (mrp->mrp_mask & MRP_PRIORITY) {
mac_priority_level_t pri = mrp->mrp_priority;
if (pri < MPL_LOW || pri > MPL_RESET)
return (EINVAL);
}
if (mrp->mrp_mask & MRP_MAXBW) {
uint64_t maxbw = mrp->mrp_maxbw;
if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
return (EINVAL);
}
if (mrp->mrp_mask & MRP_CPUS) {
int i, j;
mac_cpu_mode_t fanout;
if (mrp->mrp_ncpus > ncpus)
return (EINVAL);
for (i = 0; i < mrp->mrp_ncpus; i++) {
for (j = 0; j < mrp->mrp_ncpus; j++) {
if (i != j &&
mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
return (EINVAL);
}
}
}
for (i = 0; i < mrp->mrp_ncpus; i++) {
cpu_t *cp;
int rv;
mutex_enter(&cpu_lock);
cp = cpu_get(mrp->mrp_cpu[i]);
if (cp != NULL)
rv = cpu_is_online(cp);
else
rv = 0;
mutex_exit(&cpu_lock);
if (rv == 0)
return (EINVAL);
}
fanout = mrp->mrp_fanout_mode;
if (fanout < 0 || fanout > MCM_CPUS)
return (EINVAL);
}
if (mrp->mrp_mask & MRP_PROTECT) {
int err = mac_protect_validate(mrp);
if (err != 0)
return (err);
}
if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
!(mrp->mrp_mask & MRP_TX_RINGS)) {
return (0);
}
if (mip == NULL)
return (0);
if ((mip->mi_state_flags & MIS_IS_VNIC) &&
mac_is_vnic_primary((mac_handle_t)mip)) {
return (ENOTSUP);
}
mip_mrp = &mip->mi_resource_props;
if (mip->mi_state_flags & MIS_IS_VNIC)
mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
reset = mrp->mrp_mask & MRP_RINGS_RESET;
if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
return (EINVAL);
}
if (reset)
return (0);
if (mrp->mrp_mask & MRP_RX_RINGS) {
rings_needed = mrp->mrp_nrxrings;
if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
rings_needed -= mip_mrp->mrp_nrxrings;
else
rings_needed = 0;
}
rings_avail = mip->mi_rxrings_avail;
gtype = mip->mi_rx_group_type;
} else {
rings_needed = mrp->mrp_ntxrings;
if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
rings_needed -= mip_mrp->mrp_ntxrings;
else
rings_needed = 0;
}
rings_avail = mip->mi_txrings_avail;
gtype = mip->mi_tx_group_type;
}
if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
if (rings_needed > rings_avail)
return (EINVAL);
} else {
if (rings_needed > 0)
return (EINVAL);
}
return (0);
}
void
mac_virtual_link_update(mac_impl_t *mip)
{
if (mip->mi_linkstate != LINK_STATE_UP)
i_mac_notify(mip, MAC_NOTE_LINK);
}
void
mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
mac_resource_props_t *mrp)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_impl_t *mip = (mac_impl_t *)mh;
mcip->mci_upper_mip = mip;
if (mrp != NULL) {
bcopy(mrp, &mip->mi_resource_props,
sizeof (mac_resource_props_t));
}
}
int
mac_mark_exclusive(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
ASSERT(MAC_PERIM_HELD(mh));
rw_enter(&i_mac_impl_lock, RW_WRITER);
if (mip->mi_state_flags & MIS_DISABLED) {
rw_exit(&i_mac_impl_lock);
return (ENOENT);
}
if (mip->mi_ref != 2) {
rw_exit(&i_mac_impl_lock);
return (EBUSY);
}
ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
rw_exit(&i_mac_impl_lock);
return (0);
}
void
mac_unmark_exclusive(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
ASSERT(MAC_PERIM_HELD(mh));
rw_enter(&i_mac_impl_lock, RW_WRITER);
ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
rw_exit(&i_mac_impl_lock);
}
int
mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
{
mac_impl_t *mip = (mac_impl_t *)mh;
uint_t old_mtu;
int rv = 0;
i_mac_perim_enter(mip);
if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
rv = ENOTSUP;
goto bail;
}
old_mtu = mip->mi_sdu_max;
if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
rv = EINVAL;
goto bail;
}
rw_enter(&mip->mi_rw_lock, RW_READER);
if (mip->mi_mtrp != NULL && new_mtu < mip->mi_mtrp->mtr_mtu) {
rv = EBUSY;
rw_exit(&mip->mi_rw_lock);
goto bail;
}
rw_exit(&mip->mi_rw_lock);
if (old_mtu != new_mtu) {
rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
"mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
if (rv != 0)
goto bail;
rv = mac_maxsdu_update(mh, new_mtu);
ASSERT(rv == 0);
}
bail:
i_mac_perim_exit(mip);
if (rv == 0 && old_mtu_arg != NULL)
*old_mtu_arg = old_mtu;
return (rv);
}
void
mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
char *clnts_name)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_grp_client_t *mcip;
uint_t i = 0, index = 0;
mac_ring_t *ring;
ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
rw_enter(&mip->mi_rw_lock, RW_READER);
*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
*type = mip->mi_rx_groups[grp_index].mrg_type;
*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
ring = mip->mi_rx_groups[grp_index].mrg_rings;
for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
index++) {
rings[index] = ring->mr_index;
ring = ring->mr_next;
}
index = 0;
if (grp_index == 0) {
(void) strlcpy(clnts_name, "<default,mcast>,",
MAXCLIENTNAMELEN);
index += strlen("<default,mcast>,");
}
for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
mcip = mcip->mgc_next) {
int name_len = strlen(mcip->mgc_client->mci_name);
if (index + name_len >= MAXCLIENTNAMELEN) {
index = MAXCLIENTNAMELEN;
break;
}
bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
name_len);
index += name_len;
clnts_name[index++] = ',';
i++;
}
if (index > 0)
clnts_name[index - 1] = '\0';
*n_clnts = i;
rw_exit(&mip->mi_rw_lock);
}
void
mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
char *clnts_name)
{
mac_impl_t *mip = (mac_impl_t *)mh;
mac_grp_client_t *mcip;
uint_t i = 0, index = 0;
mac_ring_t *ring;
ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
rw_enter(&mip->mi_rw_lock, RW_READER);
*grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
mip->mi_tx_groups[grp_index].mrg_index : grp_index;
*type = mip->mi_tx_groups[grp_index].mrg_type;
*n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
ring = mip->mi_tx_groups[grp_index].mrg_rings;
for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
index++) {
rings[index] = ring->mr_index;
ring = ring->mr_next;
}
index = 0;
if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
(void) strlcpy(clnts_name, "<default>,",
MAXCLIENTNAMELEN);
index += strlen("<default>,");
}
for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
mcip = mcip->mgc_next) {
int name_len = strlen(mcip->mgc_client->mci_name);
if (index + name_len >= MAXCLIENTNAMELEN) {
index = MAXCLIENTNAMELEN;
break;
}
bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
name_len);
index += name_len;
clnts_name[index++] = ',';
i++;
}
if (index > 0)
clnts_name[index - 1] = '\0';
*n_clnts = i;
rw_exit(&mip->mi_rw_lock);
}
uint_t
mac_hwgrp_num(mac_handle_t mh, int type)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
}
uint_t
mac_txavail_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_txrings_avail);
}
uint_t
mac_rxavail_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_rxrings_avail);
}
uint_t
mac_rxrsvd_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_rxrings_rsvd);
}
uint_t
mac_txrsvd_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_txrings_rsvd);
}
uint_t
mac_rxhwlnksavail_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_rxhwclnt_avail);
}
uint_t
mac_rxhwlnksrsvd_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_rxhwclnt_used);
}
uint_t
mac_txhwlnksavail_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_txhwclnt_avail);
}
uint_t
mac_txhwlnksrsvd_get(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return (mip->mi_txhwclnt_used);
}
void
mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip);
if (rxrings != MAC_RXRINGS_DONTCARE) {
mrp->mrp_mask |= MRP_RX_RINGS;
mrp->mrp_nrxrings = rxrings;
}
if (txrings != MAC_TXRINGS_DONTCARE) {
mrp->mrp_mask |= MRP_TX_RINGS;
mrp->mrp_ntxrings = txrings;
}
}
boolean_t
mac_get_promisc_filtered(mac_client_handle_t mch)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
return (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED);
}
void
mac_set_promisc_filtered(mac_client_handle_t mch, boolean_t enable)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
if (enable)
mcip->mci_protect_flags |= MPT_FLAG_PROMISC_FILTERED;
else
mcip->mci_protect_flags &= ~MPT_FLAG_PROMISC_FILTERED;
}