vlan_rcm.c revision 648495d6a097a39eaebc7e56d25f2463ff3bba65
/*
* 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.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This RCM module adds support to the RCM framework for VLAN 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 <libdlvlan.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 */
/* VLAN link flags */
typedef enum {
VLAN_OFFLINED = 0x1,
VLAN_CONSUMER_OFFLINED = 0x2,
VLAN_STALE = 0x4
} vlan_flag_t;
/* link representation */
typedef struct dl_vlan {
} dl_vlan_t;
/* VLAN 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 VLANs
*/
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 vlan_register(rcm_handle_t *);
static int vlan_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 vlan_online_vlan(link_cache_t *);
static char *vlan_usage(link_cache_t *);
static void vlan_log_err(datalink_id_t, char **, char *);
char **, uint_t, rcm_info_t **);
/* Module-Private data */
static struct rcm_mod_ops vlan_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 (&vlan_ops);
}
/*
* rcm_mod_info() - Return a string describing this module.
*/
const char *
rcm_mod_info(void)
{
return ("VLAN module version 1.2");
}
/*
* rcm_mod_fini() - Destroy the network VLAN cache.
*/
int
rcm_mod_fini(void)
{
/*
* Note that vlan_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);
}
/*
* vlan_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) {
_("VLAN: failed to register %s\n"),
return (RCM_FAILURE);
} else {
}
}
return (RCM_SUCCESS);
}
/*
* vlan_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) {
_("VLAN: 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) {
_("VLAN: failed to unregister %s\n"),
return (RCM_FAILURE);
} else {
}
}
return (RCM_SUCCESS);
}
/*
* vlan_offline() - Offline VLANs 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 VLANs to be offlined
*/
RCM_SUCCESS) {
"VLAN: consumers agreed on offline\n");
} else {
"consumers failed to offline");
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
/* Check if it's a query */
"VLAN: 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);
}
/*
* vlan_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 VLANs to be onlined
*/
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
static void
{
char errmsg[DLADM_STRSIZE];
/*
* Try to bring on all offlined VLANs
*/
continue;
/*
* Print a warning message and continue to online
* other VLANs.
*/
_("VLAN: VLAN online failed (%u): %s\n"),
} else {
}
}
}
static int
{
char errmsg[DLADM_STRSIZE];
/*
* Try to delete all explicit created VLAN
*/
if (vlan->dv_implicit)
continue;
DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) {
_("VLAN: VLAN offline failed (%u): %s\n"),
return (RCM_FAILURE);
} else {
"VLAN: VLAN offline succeeded(%u)\n",
}
}
return (RCM_SUCCESS);
}
/*
* vlan_get_info() - Gather usage information for this resource.
*/
/*ARGSUSED*/
int
{
(void) mutex_lock(&cache_lock);
_("VLAN: get_info(%s) unrecognized resource\n"), rsrc);
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
(void) mutex_unlock(&cache_lock);
/* most likely malloc failure */
_("VLAN: get_info(%s) malloc failure\n"), rsrc);
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
return (RCM_SUCCESS);
}
/*
* vlan_suspend() - Nothing to do, always okay
*/
/*ARGSUSED*/
static int
{
return (RCM_SUCCESS);
}
/*
* vlan_resume() - Nothing to do, always okay
*/
/*ARGSUSED*/
static int
{
return (RCM_SUCCESS);
}
/*
* vlan_consumer_remove()
*
* Notify VLAN 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 VLAN consumers must have been offlined
* at this point.
*/
if (ret != RCM_SUCCESS) {
_("VLAN: notify remove failed (%s)\n"), rsrc);
break;
}
}
return (ret);
}
/*
* vlan_remove() - remove a resource from cache
*/
/*ARGSUSED*/
static int
{
int rv;
(void) mutex_lock(&cache_lock);
_("VLAN: 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);
}
/*
* vlan_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 VLANs */
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 VLAN consumers */
info) != 0) {
rv = RCM_FAILURE;
}
}
"VLAN: notify_event: link configuration complete\n");
return (rv);
}
/*
* vlan_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 nvlan;
char *buf;
const char *fmt;
char *sep;
char errmsg[DLADM_STRSIZE];
char name[MAXLINKNAMELEN];
_("VLAN: usage(%s) get link name failure(%s)\n"),
return (NULL);
}
fmt = _("%1$s offlined");
else
fmt = _("%1$s VLANs: ");
/* TRANSLATION_NOTE: separator used between VLAN linkids */
sep = _(", ");
nvlan = 0;
nvlan++;
/* space for VLANs and separators, plus message */
_("VLAN: usage(%s) malloc failure(%s)\n"),
return (NULL);
}
/* Nothing else to do */
return (buf);
}
_("VLAN: usage(%s) get vlan %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);
}
"VLAN: cache lookup succeeded(%s)\n", rsrc);
return (node);
}
}
return (NULL);
}
/*
* node_free - Free a node from the cache
*/
static void
{
/* free the VLAN 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 vlan_update_arg_s {
int retval;
/*
* vlan_update() - Update physical interface properties
*/
static int
{
char *rsrc;
char errmsg[DLADM_STRSIZE];
int ret = -1;
if (status != DLADM_STATUS_OK) {
"VLAN: vlan_update() cannot get vlan information for "
return (DLADM_WALK_CONTINUE);
}
goto done;
}
"VLAN: %s already registered (vlanid:%d)\n",
} else {
"VLAN: %s is a new resource (vlanid:%d)\n",
goto done;
}
}
break;
}
}
if (newnode) {
}
goto done;
}
}
if (newnode)
vlanid);
ret = 0;
done:
}
/*
* vlan_update_all() - Determine all VLAN 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
* vlan_update_all() failed. Unregister link that are not offlined
* and still in cache
*/
/* clear stale VLANs */
else
}
}
0);
node->vc_resource);
continue;
}
continue;
RCM_SUCCESS) {
_("VLAN: 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);
}
/*
* vlan_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];
_("VLAN: cannot get link name for (%s) %s\n"),
}
} else {
}
else
}
}
/*
* vlan_consumer_online()
*
* Notify online to VLAN consumers.
*/
/* ARGSUSED */
static void
{
char rsrc[RCM_LINK_RESOURCE_MAX];
node->vc_resource);
continue;
}
}
/*
* vlan_consumer_offline()
*
* Offline VLAN 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 VLANs.
* 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);
_("VLAN: failed to allocate nvlist\n"));
goto done;
}
if (!vlan->dv_implicit) {
"VLAN: vlan_notify_new_vlan add (%u)\n",
_("VLAN: failed to construct nvlist\n"));
(void) mutex_unlock(&cache_lock);
goto done;
}
}
}
(void) mutex_unlock(&cache_lock);
RCM_SUCCESS) {
_("VLAN: failed to notify %s event for %s\n"),
goto done;
}
ret = 0;
done:
return (ret);
}
/*
* vlan_consumer_notify() - Notify consumers of VLANs 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);
"VLAN: vlan_notify_new_vlan failed(%s)\n", rsrc);
return (-1);
}
return (0);
}
typedef struct vlan_up_arg_s {
int retval;
static int
{
char errmsg[DLADM_STRSIZE];
if (status != DLADM_STATUS_OK) {
"VLAN: vlan_up(): cannot get information for VLAN %u "
return (DLADM_WALK_CONTINUE);
}
return (DLADM_WALK_CONTINUE);
return (DLADM_WALK_CONTINUE);
/*
* Prompt the warning message and continue to UP other VLANs.
*/
_("VLAN: VLAN up failed (%u): %s\n"),
return (DLADM_WALK_CONTINUE);
}
/*
* vlan_configure() - Configure VLANs over a physical link after it attaches
*/
static int
{
char rsrc[RCM_LINK_RESOURCE_MAX];
/* Check for the VLANs in the cache */
/* Check if the link is new or was previously offlined */
(void) mutex_lock(&cache_lock);
"VLAN: Skipping configured interface(%s)\n", rsrc);
(void) mutex_unlock(&cache_lock);
return (0);
}
(void) mutex_unlock(&cache_lock);
"VLAN: vlan_configure succeeded(%s)\n", rsrc);
}
}