1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * CDDL HEADER START
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * The contents of this file are subject to the terms of the
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Common Development and Distribution License (the "License").
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * You may not use this file except in compliance with the License.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * See the License for the specific language governing permissions
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * and limitations under the License.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * When distributing Covered Code, include this CDDL HEADER in each
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * If applicable, add the following below this CDDL HEADER, with the
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * fields enclosed by brackets "[]" replaced with your own identifying
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * information: Portions Copyright [yyyy] [name of copyright owner]
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * CDDL HEADER END
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Use is subject to license terms.
2954adb080121c02025bb94537c313e6f51c9fd7Rob Gulewich * Copyright (c) 2012, Joyent, Inc. All rights reserved.
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou if (smf_enable_instance(VRRP_SERVICE, 0) == -1) {
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * Wait up to MAX_WAIT_TIME seconds for the VRRP service being brought
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou for (i = 0; i < MAX_WAIT_TIME; i++) {
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * Disable the VRRP service if there is no VRRP router left.
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * Get the number of the existing routers. If there is no routers
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * left, disable the service.
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou if (vrrp_list(NULL, VRRP_VRID_NONE, NULL, AF_UNSPEC, &cnt,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhouvrrp_cmd_request(void *cmd, size_t csize, vrrp_cmd_func_t func, void *arg)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Set it to be non-blocking.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou (void) fcntl(sock, F_SETFL, (flags | O_NONBLOCK));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou (void) strlcpy(to.sun_path, VRRPD_SOCKET, sizeof (to.sun_path));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Connect to vrrpd
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (connect(sock, (const struct sockaddr *)&to, sizeof (to)) < 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Send the request
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou len = write(sock, (char *)cmd + cur_size, csize - cur_size);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou } else if (len > 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Expect the ack, first get the error code.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou } else if (len > 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * The specific callback gets the rest of the information.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * public APIs
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "not enough memory"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "invalid router name"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "already exists"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "no virtual IPs"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "ip configuration failure"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "data-link configuration "
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou "failure"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "configuration update error"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "VRRP router already exists"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "not enough space"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "router name already exists"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "VRRP router not found"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "invalid IP address"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "invalid IP address family"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "invalid data-link"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "permission denied"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "operation already in progress"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "VRRP VNIC has not been "
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou "created"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "the data-link does not exist"));
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou return (dgettext(TEXT_DOMAIN, "the VRRP service cannot "
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou "be enabled"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou return (dgettext(TEXT_DOMAIN, "invalid argument"));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((*vh = malloc(sizeof (struct vrrp_handle))) == NULL) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou const char *c;
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * The legal characters in a valid router name are:
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * alphanumeric (a-z, A-Z, 0-9), underscore ('_'), and '.'.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhouvrrp_create(vrrp_handle_t vh, vrrp_vr_conf_t *conf)
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * Enable the VRRP service if it is not already enabled.
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou if ((err = vrrp_enable_service()) != VRRP_SUCCESS)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou (void) memcpy(&cmd.vcc_conf, conf, sizeof (vrrp_vr_conf_t));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * This may be due to another process is deleting the last
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * router and disabled the VRRP service, try again.
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * If router cannot be created, check if the VRRP service
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * should be disabled, and disable if needed.
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * If the VRRP service is not enabled, we assume there is no router
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * configured.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (strlcpy(cmd.vcd_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * If the VRRP service is not enabled, we assume there is no router
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * configured.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (strlcpy(cmd.vcs_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * If the VRRP service is not enabled, we assume there is no router
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * configured.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (strlcpy(cmd.vcx_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhouvrrp_modify(vrrp_handle_t vh, vrrp_vr_conf_t *conf, uint32_t mask)
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * If the VRRP service is not enabled, we assume there is no router
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * configured.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou (void) memcpy(&cmd.vcm_conf, conf, sizeof (vrrp_vr_conf_t));
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou err = vrrp_cmd_request(&cmd, sizeof (cmd), NULL, NULL);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Get the rest of vrrp_ret_list_t besides the error code.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou } else if (len > 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou out_cnt = (in_cnt <= out_cnt) ? in_cnt : out_cnt;
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou len = read(sock, (char *)list_arg->vfl_names + cur_size,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou } else if (len > 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Looks up the vrrp instances that matches the given variable.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * If the given cnt is 0, names should be set to NULL. In this case, only
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * the count of the matched instances is returned.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * If the given cnt is non-zero, caller must allocate "names" whose size
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * is (cnt * VRRP_NAME_MAX).
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Return value: the current count of matched instances, and names will be
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * points to the list of the current vrrp instances names. Note that
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * only MIN(in_cnt, out_cnt) number of names will be returned.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhouvrrp_list(vrrp_handle_t vh, vrid_t vrid, const char *intf, int af,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((cnt == NULL) || (*cnt != 0 && names == NULL))
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (intf != NULL && (strlcpy(cmd.vcl_ifname, intf,
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * If the service is not online, we assume there is no router
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * configured.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou err = vrrp_cmd_request(&cmd, sizeof (cmd), vrrp_list_func, &list_arg);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Expect the ack, first get the vrrp_ret_t.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou len = read(sock, (char *)qinfo + cur_size, total - cur_size);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou } else if (len > 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Even if there is no IP virtual IP address, there is always
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * space in the vrrp_queryinfo_t structure for one virtual
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * IP address.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou out_cnt = (in_cnt < out_cnt ? in_cnt : out_cnt) - 1;
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou len = read(sock, (char *)qinfo + cur_size, total - cur_size);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou } else if (len > 0) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * *vqp is allocated inside this function and must be freed by the caller.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhouvrrp_query(vrrp_handle_t vh, const char *vn, vrrp_queryinfo_t **vqp)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (strlcpy(cmd.vcq_name, vn, VRRP_NAME_MAX) >= VRRP_NAME_MAX)
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * If the service is not online, we assume there is no router
c5e0ece05310eec3c585344bcff875855f3f507aCathy Zhou * configured.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Allocate enough room for virtual IPs.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou size += (vipcnt == 0) ? 0 : (vipcnt - 1) * sizeof (vrrp_addr_t);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou err = vrrp_cmd_request(&cmd, sizeof (cmd), vrrp_query_func, qinfo);
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * If the returned number of virtual IPs is greater than we expected,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * allocate more room and try again.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Is this a special VNIC interface created for VRRP? If so, return
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * the linkid the VNIC was created on, the VRRP ID and address family.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhouvrrp_is_vrrp_vnic(vrrp_handle_t vh, datalink_id_t vnicid,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou datalink_id_t *linkidp, uint16_t *vidp, vrid_t *vridp, int *afp)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (dladm_vnic_info(vh->vh_dh, vnicid, &vattr, DLADM_OPT_ACTIVE) !=
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhoulookup_vnic(dladm_handle_t dh, datalink_id_t vnicid, void *arg)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (vrrp_is_vrrp_vnic(lva->lva_vh, vnicid, &linkid, &vid, &vrid,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou &af) && lva->lva_vrid == vrid && lva->lva_linkid == linkid &&
2954adb080121c02025bb94537c313e6f51c9fd7Rob Gulewich (lva->lva_vid == VLAN_ID_NONE || lva->lva_vid == vid) &&
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (dladm_datalink_id2info(dh, vnicid, NULL, NULL, NULL,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou lva->lva_vnic, sizeof (lva->lva_vnic)) == DLADM_STATUS_OK) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Given the primary link name, find the assoicated VRRP vnic name, if
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * the vnic does not exist yet, return the linkid, vid of the primary link.
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhouvrrp_get_vnicname(vrrp_handle_t vh, vrid_t vrid, int af, char *link,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou datalink_id_t *linkidp, uint16_t *vidp, char *vnic, size_t len)
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((strlen(link) == 0) || dladm_name2info(vh->vh_dh,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou DLADM_STATUS_OK || !(flags & DLADM_OPT_ACTIVE)) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if (dladm_vlan_info(vh->vh_dh, linkid, &vlan_attr,
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou if ((dladm_datalink_id2info(vh->vh_dh, linkid, NULL,
2954adb080121c02025bb94537c313e6f51c9fd7Rob Gulewich if (dladm_vnic_info(vh->vh_dh, linkid, &vnic_attr,
2954adb080121c02025bb94537c313e6f51c9fd7Rob Gulewich * Only VRRP over vnics, aggrs and physical ethernet links is supported
2954adb080121c02025bb94537c313e6f51c9fd7Rob Gulewich if ((class != DATALINK_CLASS_PHYS && class != DATALINK_CLASS_AGGR &&
2954adb080121c02025bb94537c313e6f51c9fd7Rob Gulewich class != DATALINK_CLASS_VNIC) || media != DL_ETHER) {
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou * Find the assoicated vnic with the given vrid/vid/af/linkid
1cb875ae88fb9463b368e725c2444776595895cbCathy Zhou (void) dladm_walk_datalink_id(lookup_vnic, vh->vh_dh, &lva,