843e19887f64dde75055cf8842fc4db2171eff45johnlev * CDDL HEADER START
843e19887f64dde75055cf8842fc4db2171eff45johnlev * The contents of this file are subject to the terms of the
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Common Development and Distribution License (the "License").
843e19887f64dde75055cf8842fc4db2171eff45johnlev * You may not use this file except in compliance with the License.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
843e19887f64dde75055cf8842fc4db2171eff45johnlev * See the License for the specific language governing permissions
843e19887f64dde75055cf8842fc4db2171eff45johnlev * and limitations under the License.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * When distributing Covered Code, include this CDDL HEADER in each
843e19887f64dde75055cf8842fc4db2171eff45johnlev * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * If applicable, add the following below this CDDL HEADER, with the
843e19887f64dde75055cf8842fc4db2171eff45johnlev * fields enclosed by brackets "[]" replaced with your own identifying
843e19887f64dde75055cf8842fc4db2171eff45johnlev * information: Portions Copyright [yyyy] [name of copyright owner]
843e19887f64dde75055cf8842fc4db2171eff45johnlev * CDDL HEADER END
327151705b7439cb7ab35c370f682cac7ef9523aCathy Zhou * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * VNIC administration library.
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * Default random MAC address prefix (locally administered).
da14cebe459d3275048785f25bd869cb09b5307fEric Chengstatic char dladm_vnic_def_prefix[] = {0x02, 0x08, 0x20};
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeystatic dladm_status_t dladm_vnic_persist_conf(dladm_handle_t,
da14cebe459d3275048785f25bd869cb09b5307fEric Chengstatic const char *dladm_vnic_macaddr2str(const uchar_t *, char *);
da14cebe459d3275048785f25bd869cb09b5307fEric Chengstatic dladm_status_t dladm_vnic_str2macaddr(const char *, uchar_t *);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * Convert a diagnostic returned by the kernel into a dladm_status_t.
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * Send a create command to the VNIC driver.
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeyi_dladm_vnic_create_sys(dladm_handle_t handle, dladm_vnic_attr_t *attr)
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng ioc.vc_flags = attr->va_force ? VNIC_IOC_CREATE_FORCE : 0;
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng if (attr->va_mac_len > 0 || ioc.vc_mac_prefix_len > 0)
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng bcopy(attr->va_mac_addr, ioc.vc_mac_addr, MAXMACADDRLEN);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng bcopy(&attr->va_resource_props, &ioc.vc_resource_props,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_CREATE, &ioc);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng bcopy(ioc.vc_mac_addr, attr->va_mac_addr, MAXMACADDRLEN);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Get the configuration information of the given VNIC.
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeyi_dladm_vnic_info_active(dladm_handle_t handle, datalink_id_t linkid,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_INFO, &ioc);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng if (rc != 0) {
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng bcopy(vnic->vn_mac_addr, attrp->va_mac_addr, MAXMACADDRLEN);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng attrp->va_mac_prefix_len = vnic->vn_mac_prefix_len;
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeyi_dladm_vnic_info_persist(dladm_handle_t handle, datalink_id_t linkid,
327151705b7439cb7ab35c370f682cac7ef9523aCathy Zhou if ((status = dladm_getsnap_conf(handle, linkid, &conf)) !=
2b24ab6b3865caeede9eeb9db6b83e1d89dcd1eaSebastien Roy status = dladm_get_conf_field(handle, conf, FLINKOVER, linkover,
2b24ab6b3865caeede9eeb9db6b83e1d89dcd1eaSebastien Roy * This isn't an error, etherstubs don't have a FLINKOVER
2b24ab6b3865caeede9eeb9db6b83e1d89dcd1eaSebastien Roy if ((status = dladm_name2info(handle, linkover,
2b24ab6b3865caeede9eeb9db6b83e1d89dcd1eaSebastien Roy &attrp->va_link_id, NULL, NULL, NULL)) != DLADM_STATUS_OK)
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey if ((status = dladm_datalink_id2info(handle, linkid, NULL, &class,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng if (attrp->va_link_id == DATALINK_INVALID_LINKID) {
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng attrp->va_mac_addr_type = VNIC_MAC_ADDR_TYPE_PRIMARY;
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_get_conf_field(handle, conf, FMADDRTYPE, &u64,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng attrp->va_mac_addr_type = (vnic_mac_addr_type_t)u64;
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((status = dladm_get_conf_field(handle, conf, FVRID,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((status = dladm_get_conf_field(handle, conf, FVRAF,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_get_conf_field(handle, conf, FMADDRLEN, &u64,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng attrp->va_mac_len = ((status == DLADM_STATUS_OK) ?
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_get_conf_field(handle, conf, FMADDRSLOT, &u64,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng attrp->va_mac_slot = ((status == DLADM_STATUS_OK) ?
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_get_conf_field(handle, conf, FMADDRPREFIXLEN,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng attrp->va_mac_prefix_len = ((status == DLADM_STATUS_OK) ?
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_get_conf_field(handle, conf, FMACADDR, macstr,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng status = dladm_vnic_str2macaddr(macstr, attrp->va_mac_addr);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_get_conf_field(handle, conf, FVLANID, &u64,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng attrp->va_vid = ((status == DLADM_STATUS_OK) ? (uint16_t)u64 : 0);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeydladm_vnic_info(dladm_handle_t handle, datalink_id_t linkid,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey return (i_dladm_vnic_info_active(handle, linkid, attrp));
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey return (i_dladm_vnic_info_persist(handle, linkid, attrp));
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Remove a VNIC from the kernel.
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeyi_dladm_vnic_delete_sys(dladm_handle_t handle, datalink_id_t linkid)
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey rc = ioctl(dladm_dld_fd(handle), VNIC_IOC_DELETE, &ioc);
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Convert between MAC address types and their string representations.
843e19887f64dde75055cf8842fc4db2171eff45johnlev#define NADDR_TYPES (sizeof (addr_types) / sizeof (dladm_vnic_addr_type_t))
da14cebe459d3275048785f25bd869cb09b5307fEric Chengstatic const char *
da14cebe459d3275048785f25bd869cb09b5307fEric Chengdladm_vnic_macaddrtype2str(vnic_mac_addr_type_t type)
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng for (i = 0; i < NADDR_TYPES; i++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdladm_vnic_str2macaddrtype(const char *str, vnic_mac_addr_type_t *val)
843e19887f64dde75055cf8842fc4db2171eff45johnlev for (i = 0; i < NADDR_TYPES; i++) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (strncmp(str, type->va_str, strlen(type->va_str)) == 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Based on the VRRP specification, the virtual router MAC address associated
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * with a virtual router is an IEEE 802 MAC address in the following format:
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * IPv4 case: 00-00-5E-00-01-{VRID} (in hex in internet standard bit-order)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * IPv6 case: 00-00-5E-00-02-{VRID} (in hex in internet standard bit-order)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhoui_dladm_vnic_vrrp_mac(vrid_t vrid, int af, uint8_t *mac, uint_t maclen)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (maclen < ETHERADDRL || vrid < VRRP_VRID_MIN ||
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou vrid > VRRP_VRID_MAX || (af != AF_INET && af != AF_INET6)) {
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * Create a new VNIC / VLAN. Update the configuration file and bring it up.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * The "vrid" and "af" arguments are only required if the mac_addr_type is
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * VNIC_MAC_ADDR_TYPE_VRID. In that case, the MAC address will be caculated
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * based on the above algorithm.
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeydladm_vnic_create(dladm_handle_t handle, const char *vnic, datalink_id_t linkid,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou vnic_mac_addr_type_t mac_addr_type, uchar_t *mac_addr, uint_t mac_len,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou int *mac_slot, uint_t mac_prefix_len, uint16_t vid, vrid_t vrid,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou int af, datalink_id_t *vnic_id_out, dladm_arg_list_t *proplist,
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Sanity test arguments.
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng is_etherstub = (linkid == DATALINK_INVALID_LINKID);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((status = dladm_datalink_id2info(handle, linkid, NULL,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou /* it's an anchor VNIC */
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (linkid != DATALINK_INVALID_LINKID || vid != 0)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Only VRRP VNIC need VRID and address family specified.
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * If a random address might be generated, but no prefix
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * was specified by the caller, use the default MAC address
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng if ((mac_addr_type == VNIC_MAC_ADDR_TYPE_RANDOM ||
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng bcopy(dladm_vnic_def_prefix, mac_addr, mac_prefix_len);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * If this is a VRRP VNIC, generate its MAC address using the given
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * VRID and address family.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * VRRP VNICs must be created over ethernet data-links.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (vrid < VRRP_VRID_MIN || vrid > VRRP_VRID_MAX ||
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou (af != AF_INET && af != AF_INET6) || mac_addr != NULL ||
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou (mac_slot != NULL && *mac_slot != -1) || is_etherstub ||
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou status = i_dladm_vnic_vrrp_mac(vrid, af, mac_addr, mac_len);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng (is_etherstub ? DATALINK_CLASS_ETHERSTUB : DATALINK_CLASS_VNIC);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey if ((status = dladm_create_datalink_id(handle, name, class,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng (void) snprintf(name + 4, sizeof (name), "%llu", vnic_id);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng /* Extract resource_ctl and cpu_list from proplist */
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_link_proplist_extract(handle, proplist,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng bcopy(mac_addr, attr.va_mac_addr, mac_prefix_len);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = i_dladm_vnic_create_sys(handle, &attr);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng /* Save vnic configuration and its properties */
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_vnic_persist_conf(handle, name, &attr, class);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_set_linkprop(handle, vnic_id,
4eaa471005973e11a6110b69fe990530b3b95a38Rishi Srivatsavai (void) dladm_remove_conf(handle, vnic_id);
4eaa471005973e11a6110b69fe990530b3b95a38Rishi Srivatsavai (void) i_dladm_vnic_delete_sys(handle, vnic_id);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey (void) dladm_destroy_datalink_id(handle, vnic_id, flags);
4eaa471005973e11a6110b69fe990530b3b95a38Rishi Srivatsavai stat2 = dladm_bridge_refresh(handle, linkid);
4eaa471005973e11a6110b69fe990530b3b95a38Rishi Srivatsavai if (status == DLADM_STATUS_OK && stat2 != DLADM_STATUS_OK)
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * Delete a VNIC / VLAN.
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeydladm_vnic_delete(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags)
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey if ((dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, NULL, 0)
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = i_dladm_vnic_delete_sys(handle, linkid);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey (void) dladm_set_linkprop(handle, linkid, NULL, NULL, 0,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey (void) dladm_destroy_datalink_id(handle, linkid,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey (void) dladm_destroy_datalink_id(handle, linkid,
4eaa471005973e11a6110b69fe990530b3b95a38Rishi Srivatsavai return (dladm_bridge_refresh(handle, linkid));
da14cebe459d3275048785f25bd869cb09b5307fEric Chengstatic const char *
da14cebe459d3275048785f25bd869cb09b5307fEric Chengdladm_vnic_macaddr2str(const uchar_t *mac, char *buf)
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER));
da14cebe459d3275048785f25bd869cb09b5307fEric Chengdladm_vnic_str2macaddr(const char *str, uchar_t *buf)
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeydladm_vnic_persist_conf(dladm_handle_t handle, const char *name,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey dladm_vnic_attr_t *attrp, datalink_class_t class)
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey if ((status = dladm_create_conf(handle, name, attrp->va_vnic_id,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng if (attrp->va_link_id != DATALINK_INVALID_LINKID) {
2b24ab6b3865caeede9eeb9db6b83e1d89dcd1eaSebastien Roy status = dladm_datalink_id2info(handle, attrp->va_link_id, NULL,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_set_conf_field(handle, conf, FLINKOVER,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_set_conf_field(handle, conf, FMADDRTYPE,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou status = dladm_set_conf_field(handle, conf, FVRID,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou status = dladm_set_conf_field(handle, conf, FVRAF,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_set_conf_field(handle, conf, FMADDRLEN,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_set_conf_field(handle, conf,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng (void) dladm_vnic_macaddr2str(attrp->va_mac_addr, macstr);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_set_conf_field(handle, conf, FMACADDR,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_set_conf_field(handle, conf, FVLANID,
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng * Commit the link configuration.
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeyi_dladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, void *arg)
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng dladm_status_t *statusp = &(((dladm_vnic_up_arg_t *)arg)->status);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_vnic_info(handle, linkid, &attr, DLADM_OPT_PERSIST);
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng /* Get all properties for this vnic */
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_link_get_proplist(handle, linkid, &proplist);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = dladm_link_proplist_extract(handle, proplist,
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey status = i_dladm_vnic_create_sys(handle, &attr);
4eaa471005973e11a6110b69fe990530b3b95a38Rishi Srivatsavai status = dladm_up_datalink_id(handle, linkid);
4eaa471005973e11a6110b69fe990530b3b95a38Rishi Srivatsavai (void) i_dladm_vnic_delete_sys(handle, linkid);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskeydladm_vnic_up(dladm_handle_t handle, datalink_id_t linkid, uint32_t flags)
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng class = ((flags & DLADM_OPT_VLAN) != 0) ? DATALINK_CLASS_VLAN :
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng (DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB);
4ac67f0276a8313b5cefec38af347b94b7bfb526Anurag S. Maskey (void) dladm_walk_datalink_id(i_dladm_vnic_up, handle,