vnic_rcm.c revision da14cebe459d3275048785f25bd869cb09b5307f
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This RCM module adds support to the RCM framework for VNIC links
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <synch.h>
#include <assert.h>
#include <strings.h>
#include "rcm_module.h"
#include <libintl.h>
#include <libdllink.h>
#include <libdlvnic.h>
#include <libdlpi.h>
/*
* Definitions
*/
#ifndef lint
#define _(x) gettext(x)
#else
#define _(x) x
#endif
/* Some generic well-knowns and defaults used in this module */
/* VNIC link flags */
typedef enum {
VNIC_OFFLINED = 0x1,
VNIC_CONSUMER_OFFLINED = 0x2,
VNIC_STALE = 0x4
} vnic_flag_t;
/* link representation */
typedef struct dl_vnic {
} dl_vnic_t;
/* VNIC Cache state flags */
typedef enum {
/* Network Cache lookup options */
/* Cache element */
typedef struct link_cache {
char *vc_resource; /* resource name */
} link_cache_t;
/*
* Global cache for network VNICs
*/
static link_cache_t cache_head;
static link_cache_t cache_tail;
static mutex_t cache_lock;
static int events_registered = 0;
/*
* RCM module interface prototypes
*/
static int vnic_register(rcm_handle_t *);
static int vnic_unregister(rcm_handle_t *);
char **, char **, nvlist_t *, rcm_info_t **);
char **, rcm_info_t **);
char **, rcm_info_t **);
char **, rcm_info_t **);
char **, rcm_info_t **);
char **, nvlist_t *, rcm_info_t **);
/* Module private routines */
static void cache_free();
static int cache_update(rcm_handle_t *);
static void cache_remove(link_cache_t *);
static void node_free(link_cache_t *);
static void cache_insert(link_cache_t *);
char **, uint_t, rcm_info_t **);
char **, uint_t, rcm_info_t **);
static void vnic_online_vnic(link_cache_t *);
static char *vnic_usage(link_cache_t *);
static void vnic_log_err(datalink_id_t, char **, char *);
char **, uint_t, rcm_info_t **);
/* Module-Private data */
static struct rcm_mod_ops vnic_ops =
{
NULL,
NULL,
};
/*
* rcm_mod_init() - Update registrations, and return the ops structure.
*/
struct rcm_mod_ops *
rcm_mod_init(void)
{
/* Return the ops vectors */
return (&vnic_ops);
}
/*
* rcm_mod_info() - Return a string describing this module.
*/
const char *
rcm_mod_info(void)
{
return ("VNIC module");
}
/*
* rcm_mod_fini() - Destroy the network VNIC cache.
*/
int
rcm_mod_fini(void)
{
/*
* Note that vnic_unregister() does not seem to be called anywhere,
* therefore we free the cache nodes here. In theory we should call
* rcm_register_interest() for each node before we free it, the
* framework does not provide the rcm_handle to allow us to do so.
*/
cache_free();
(void) mutex_destroy(&cache_lock);
return (RCM_SUCCESS);
}
/*
* vnic_register() - Make sure the cache is properly sync'ed, and its
* registrations are in order.
*/
static int
{
if (cache_update(hd) < 0)
return (RCM_FAILURE);
/*
* Need to register interest in all new resources
* getting attached, so we get attach event notifications
*/
if (!events_registered) {
!= RCM_SUCCESS) {
_("VNIC: failed to register %s\n"),
return (RCM_FAILURE);
} else {
}
}
return (RCM_SUCCESS);
}
/*
* vnic_unregister() - Walk the cache, unregistering all the networks.
*/
static int
{
/* Walk the cache, unregistering everything */
(void) mutex_lock(&cache_lock);
while (node != &cache_tail) {
!= RCM_SUCCESS) {
_("VNIC: failed to unregister %s\n"),
node->vc_resource);
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
}
(void) mutex_unlock(&cache_lock);
/*
* Unregister interest in all new resources
*/
if (events_registered) {
!= RCM_SUCCESS) {
_("VNIC: failed to unregister %s\n"),
return (RCM_FAILURE);
} else {
}
}
return (RCM_SUCCESS);
}
/*
* vnic_offline() - Offline VNICs on a specific node.
*/
static int
{
/* Lock the cache and lookup the resource */
(void) mutex_lock(&cache_lock);
/* should not happen because the resource is registered. */
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* Inform consumers (IP interfaces) of associated VNICs to be offlined
*/
RCM_SUCCESS) {
"VNIC: consumers agreed on offline\n");
} else {
"consumers failed to offline");
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
/* Check if it's a query */
"VNIC: offline query succeeded(%s)\n", rsrc);
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
RCM_SUCCESS) {
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* vnic_undo_offline() - Undo offline of a previously offlined node.
*/
/*ARGSUSED*/
static int
{
(void) mutex_lock(&cache_lock);
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
/* Check if no attempt should be made to online the link here */
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* Inform IP interfaces on associated VNICs to be onlined
*/
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
static void
{
char errmsg[DLADM_STRSIZE];
/*
* Try to bring on all offlined VNICs
*/
continue;
/*
* Print a warning message and continue to online
* other VNICs.
*/
_("VNIC: VNIC online failed (%u): %s\n"),
} else {
}
}
}
static int
{
char errmsg[DLADM_STRSIZE];
/*
* Try to delete all explicit created VNIC
*/
DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) {
_("VNIC: VNIC offline failed (%u): %s\n"),
return (RCM_FAILURE);
} else {
"VNIC: VNIC offline succeeded(%u)\n",
vnic->dlv_vnic_id);
}
}
return (RCM_SUCCESS);
}
/*
* vnic_get_info() - Gather usage information for this resource.
*/
/*ARGSUSED*/
int
{
(void) mutex_lock(&cache_lock);
_("VNIC: get_info(%s) unrecognized resource\n"), rsrc);
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
(void) mutex_unlock(&cache_lock);
/* most likely malloc failure */
_("VNIC: get_info(%s) malloc failure\n"), rsrc);
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
return (RCM_SUCCESS);
}
/*
* vnic_suspend() - Nothing to do, always okay
*/
/*ARGSUSED*/
static int
{
return (RCM_SUCCESS);
}
/*
* vnic_resume() - Nothing to do, always okay
*/
/*ARGSUSED*/
static int
{
return (RCM_SUCCESS);
}
/*
* vnic_consumer_remove()
*
* Notify VNIC consumers to remove cache.
*/
static int
rcm_info_t **info)
{
char rsrc[RCM_LINK_RESOURCE_MAX];
int ret = RCM_SUCCESS;
node->vc_resource);
/*
* This will only be called when the offline operation
* succeeds, so the VNIC consumers must have been offlined
* at this point.
*/
if (ret != RCM_SUCCESS) {
_("VNIC: notify remove failed (%s)\n"), rsrc);
break;
}
}
return (ret);
}
/*
* vnic_remove() - remove a resource from cache
*/
/*ARGSUSED*/
static int
{
int rv;
(void) mutex_lock(&cache_lock);
_("VNIC: remove(%s) unrecognized resource\n"), rsrc);
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
/* remove the cached entry for the resource */
(void) mutex_unlock(&cache_lock);
return (rv);
}
/*
* vnic_notify_event - Project private implementation to receive new resource
* events. It intercepts all new resource events. If the
* new resource is a network resource, pass up a notify
* for it too. The new resource need not be cached, since
* it is done at register again.
*/
/*ARGSUSED*/
static int
{
int rv = RCM_SUCCESS;
"unrecognized event");
return (RCM_FAILURE);
}
/* Update cache to reflect latest VNICs */
if (cache_update(hd) < 0) {
"private Cache update failed");
return (RCM_FAILURE);
}
/*
* Try best to recover all configuration.
*/
continue;
"cannot get linkid");
rv = RCM_FAILURE;
continue;
}
rv = RCM_FAILURE;
continue;
}
/* Notify all VNIC consumers */
info) != 0) {
rv = RCM_FAILURE;
}
}
"VNIC: notify_event: link configuration complete\n");
return (rv);
}
/*
* vnic_usage - Determine the usage of a link.
* The returned buffer is owned by caller, and the caller
* must free it up when done.
*/
static char *
{
int nvnic;
char *buf;
const char *fmt;
char *sep;
char errmsg[DLADM_STRSIZE];
char name[MAXLINKNAMELEN];
_("VNIC: usage(%s) get link name failure(%s)\n"),
return (NULL);
}
fmt = _("%1$s offlined");
else
fmt = _("%1$s VNICs: ");
/* TRANSLATION_NOTE: separator used between VNIC linkids */
sep = _(", ");
nvnic = 0;
nvnic++;
/* space for VNICs and separators, plus message */
_("VNIC: usage(%s) malloc failure(%s)\n"),
return (NULL);
}
/* Nothing else to do */
return (buf);
}
_("VNIC: usage(%s) get vnic %u name failure(%s)\n"),
return (NULL);
}
}
return (buf);
}
/*
* Cache management routines, all cache management functions should be
* be called with cache_lock held.
*/
/*
* cache_lookup() - Get a cache node for a resource.
* Call with cache lock held.
*
* This ensures that the cache is consistent with the system state and
* returns a pointer to the cache element corresponding to the resource.
*/
static link_cache_t *
{
if (options & CACHE_REFRESH) {
/* drop lock since update locks cache again */
(void) mutex_unlock(&cache_lock);
(void) cache_update(hd);
(void) mutex_lock(&cache_lock);
}
"VNIC: cache lookup succeeded(%s)\n", rsrc);
return (node);
}
}
return (NULL);
}
/*
* node_free - Free a node from the cache
*/
static void
{
/* free the VNIC list */
}
}
}
/*
* cache_insert - Insert a resource node in cache
*/
static void
{
/* insert at the head for best performance */
}
/*
* cache_remove() - Remove a resource node from cache.
*/
static void
{
}
typedef struct vnic_update_arg_s {
int retval;
/*
* vnic_update() - Update physical interface properties
*/
static int
{
char *rsrc;
char errmsg[DLADM_STRSIZE];
int ret = -1;
if (status != DLADM_STATUS_OK) {
"VNIC: vnic_update() cannot get vnic information for "
return (DLADM_WALK_CONTINUE);
}
/*
* Skip the etherstubs.
*/
"VNIC: vnic_update(): skip the etherstub %u\n", vnicid);
return (DLADM_WALK_CONTINUE);
}
goto done;
}
"VNIC: %s already registered (vnicid:%d)\n",
} else {
"VNIC: %s is a new resource (vnicid:%d)\n",
goto done;
}
}
break;
}
}
if (newnode) {
}
goto done;
}
}
if (newnode)
vnicid);
ret = 0;
done:
}
/*
* vnic_update_all() - Determine all VNIC links in the system
*/
static int
{
}
/*
* cache_update() - Update cache with latest interface info
*/
static int
{
int rv;
(void) mutex_lock(&cache_lock);
/* first we walk the entire cache, marking each entry stale */
}
/*
* Continue to delete all stale nodes from the cache even
* vnic_update_all() failed. Unregister link that are not offlined
* and still in cache
*/
/* clear stale VNICs */
else
}
}
0);
node->vc_resource);
continue;
}
continue;
RCM_SUCCESS) {
_("VNIC: failed to register %s\n"),
node->vc_resource);
rv = -1;
} else {
node->vc_resource);
}
}
(void) mutex_unlock(&cache_lock);
return (rv);
}
/*
* cache_free() - Empty the cache
*/
static void
{
(void) mutex_lock(&cache_lock);
while (node != &cache_tail) {
}
(void) mutex_unlock(&cache_lock);
}
/*
* vnic_log_err() - RCM error log wrapper
*/
static void
{
char link[MAXLINKNAMELEN];
char errstr[DLADM_STRSIZE];
int len;
const char *errfmt;
char *error;
link[0] = '\0';
if (linkid != DATALINK_INVALID_LINKID) {
char rsrc[RCM_LINK_RESOURCE_MAX];
_("VNIC: cannot get link name for (%s) %s\n"),
}
} else {
}
else
}
}
/*
* vnic_consumer_online()
*
* Notify online to VNIC consumers.
*/
/* ARGSUSED */
static void
{
char rsrc[RCM_LINK_RESOURCE_MAX];
node->vc_resource);
continue;
}
}
/*
* vnic_consumer_offline()
*
* Offline VNIC consumers.
*/
static int
{
char rsrc[RCM_LINK_RESOURCE_MAX];
int ret = RCM_SUCCESS;
node->vc_resource);
if (ret != RCM_SUCCESS)
break;
}
return (ret);
}
/*
* Send RCM_RESOURCE_LINK_NEW events to other modules about new VNICs.
* Return 0 on success, -1 on failure.
*/
static int
{
int ret = -1;
(void) mutex_lock(&cache_lock);
(void) mutex_unlock(&cache_lock);
return (0);
}
if (nvlist_alloc(&nvl, 0, 0) != 0) {
(void) mutex_unlock(&cache_lock);
_("VNIC: failed to allocate nvlist\n"));
goto done;
}
_("VNIC: failed to construct nvlist\n"));
(void) mutex_unlock(&cache_lock);
goto done;
}
}
(void) mutex_unlock(&cache_lock);
RCM_SUCCESS) {
_("VNIC: failed to notify %s event for %s\n"),
goto done;
}
ret = 0;
done:
return (ret);
}
/*
* vnic_consumer_notify() - Notify consumers of VNICs coming back online.
*/
static int
{
char rsrc[RCM_LINK_RESOURCE_MAX];
/* Check for the interface in the cache */
linkid);
/*
* Inform IP consumers of the new link.
*/
(void) mutex_lock(&cache_lock);
}
(void) mutex_unlock(&cache_lock);
"VNIC: vnic_notify_new_vnic failed(%s)\n", rsrc);
return (-1);
}
return (0);
}
typedef struct vnic_up_arg_s {
int retval;
static int
{
char errmsg[DLADM_STRSIZE];
if (status != DLADM_STATUS_OK) {
"VNIC: vnic_up(): cannot get information for VNIC %u "
return (DLADM_WALK_CONTINUE);
}
return (DLADM_WALK_CONTINUE);
return (DLADM_WALK_CONTINUE);
/*
* Prompt the warning message and continue to UP other VNICs.
*/
_("VNIC: VNIC up failed (%u): %s\n"),
return (DLADM_WALK_CONTINUE);
}
/*
* vnic_configure() - Configure VNICs over a physical link after it attaches
*/
static int
{
char rsrc[RCM_LINK_RESOURCE_MAX];
/* Check for the VNICs in the cache */
/* Check if the link is new or was previously offlined */
(void) mutex_lock(&cache_lock);
"VNIC: Skipping configured interface(%s)\n", rsrc);
(void) mutex_unlock(&cache_lock);
return (0);
}
(void) mutex_unlock(&cache_lock);
"VNIC: vnic_configure succeeded(%s)\n", rsrc);
}
}