mac_rcm.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 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 datalinks
* managed by dladm(1M).
*/
#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <synch.h>
#include <libintl.h>
#include <errno.h>
#include <libdevinfo.h>
#include <libdladm.h>
#include <liblaadm.h>
#include "rcm_module.h"
#define _KERNEL
#include <sys/sysmacros.h>
typedef enum mac_op {
MAC_OP_SUSPEND = 0,
MAC_OP_OFFLINE = 1,
MAC_OP_ONLINE = 2,
MAC_OP_REMOVE = 3,
MAC_OP_RESUME = 4
} mac_op_t;
char *mac_op_str[] = {
"SUSPEND",
"OFFLINE",
"ONLINE",
"REMOVE",
"RESUME"
};
/* devfsadm post-attach nvpair values */
#define PROP_NV_DDI_MAC "ddi_mac"
typedef struct mac_cache {
char *resource;
char *driver;
int instance;
int flags;
} mac_cache_t;
static mac_cache_t cache_head;
static mac_cache_t cache_tail;
static mutex_t cache_lock;
static int events_registered = 0;
struct devfs_minor_data {
char *minor_name;
char *minor_node_type;
};
/* module interface routines */
static int mac_register(rcm_handle_t *);
static int mac_unregister(rcm_handle_t *);
char **, nvlist_t *, rcm_info_t **);
uint_t, char **, rcm_info_t **);
rcm_info_t **);
rcm_info_t **);
rcm_info_t **);
rcm_info_t **);
char **, nvlist_t *, rcm_info_t **);
/* module private routines */
static void free_cache(void);
static void free_node(mac_cache_t *);
static void cache_insert(mac_cache_t *);
static int process_nvlist(nvlist_t *);
/*
* Module-Private data
*/
static struct rcm_mod_ops mac_ops = {
NULL, /* request_capacity_change */
NULL, /* notify_capacity_change */
};
/*
* Module Interface Routines
*/
/*
* rcm_mod_init()
*
* Update registrations, and return the ops structure.
*/
struct rcm_mod_ops *
{
/* Return the ops vectors */
return (&mac_ops);
}
/*
* rcm_mod_info()
*
* Return a string describing this module.
*/
const char *
{
return ("Network namespace module %I%");
}
/*
* rcm_mod_fini()
*
* Destroy the cache.
*/
int
{
free_cache();
(void) mutex_destroy(&cache_lock);
return (RCM_SUCCESS);
}
/*
* mac_register()
*
* Make sure the cache is properly sync'ed, and its registrations
* are in order.
*
* Locking: the cache is locked by update_cache, and is held
* throughout update_cache's execution because it reads and
* possibly modifies cache links continuously.
*/
static int
{
if (!events_registered) {
RCM_SUCCESS) {
gettext("MAC: failed to register for events %s\n"),
return (RCM_FAILURE);
} else {
" for events %s\n", RCM_RESOURCE_MAC_NEW);
}
}
return (RCM_SUCCESS);
}
/*
* mac_unregister()
*
* Manually walk through the cache, unregistering all the networks.
*
* Locking: the cache is locked throughout the execution of this routine
* because it reads and modifies cache links continuously.
*/
static int
{
/* Walk the cache, unregistering everything */
(void) mutex_lock(&cache_lock);
while (probe != &cache_tail) {
}
(void) mutex_unlock(&cache_lock);
if (events_registered) {
}
return (RCM_SUCCESS);
}
typedef struct mac_dl_walker_state {
char *ws_dev_name;
char **ws_datalink;
char **ws_paths;
/*
* Adds a datalink of the specified name to the list hanging off
* the specified state. Invoked by mac_dl_walker_db() and mac_dl_walker().
*/
static void
{
char dl_path[MAXPATHLEN];
name);
state->ws_n_datalinks++;
return;
return;
}
/*
* Invoked for each DDI_NT_NET node found by the dladm library.
*/
static
void
{
return;
"(legacy)\n");
return;
}
/*
* We have a virtual data link that is defined on top
* of a MAC port. Ignore it unless the MAC port was
* registered by the device being acted upon.
*/
return;
}
}
/*
* Allocate and return a list of strings containing the virtual
* data links that are currently configured on top of a device.
*/
static int
{
/*
* Use the instance and driver from the cache node to find
* matching mac ports.
*/
dl_state.ws_n_datalinks = 0;
goto bail;
}
goto bail;
}
return (dl_state.ws_n_datalinks);
bail:
return (-1);
}
/*
* Invoked for each virtual datalink defined in database.
*/
static void
{
return;
/*
* We have a virtual data link that is defined on top
* of a MAC port. Ignore it unless the MAC port was
* registered by the device being acted upon.
*/
return;
}
}
/*
* Allocate and return a list of strings containing the virtual
* data links that are configured on top of a device.
*/
static int
{
/*
* Use the instance and driver from the cache node to find
* matching mac ports.
*/
dl_state.ws_n_datalinks = 0;
goto bail;
}
goto bail;
}
return (dl_state.ws_n_datalinks);
bail:
return (-1);
}
/*
* Link aggregation walker state.
*/
typedef struct mac_aggr_walker_state {
char *as_dev_name;
/*
* Link aggregation walker.
*/
static int
{
int i, j;
return (0);
/*
* Add an entry for each aggregated MAC port that was registered
* by the device being acted upon by RCM.
*/
continue;
/*
* Found matching port. Add aggregation key to list
* if it not already there, since multiple ports of
* the same device could be added to the same
* aggregation.
*/
break;
}
/* aggregation group already in list */
continue;
return (0);
}
return (0);
}
/*
* Allocate and return a list of key values of aggregations that
* are currently configured on top of the MAC ports registered
* by a device.
*/
static int
{
int rv;
return (-1);
}
if (rv != 0) {
gettext("MAC: cannot list aggregations "
return (-1);
}
return (-1);
}
}
static void
mac_list_free(char **list)
{
int i;
return;
}
static int
mac_dl_down_list(char **list)
{
int i;
return (RCM_SUCCESS);
list[i]);
char diag_str[256];
if (diag != 0) {
} else {
diag_str[0] = '\0';
}
gettext("MAC: failed to bring "
goto error;
}
}
return (RCM_SUCCESS);
/* bring data links back up */
for (i--; i >= 0; i--) {
}
return (RCM_FAILURE);
}
static int
mac_dl_up_list(char **list)
{
int i;
return (RCM_SUCCESS);
list[i]);
char diag_str[256];
if (diag != 0) {
} else {
diag_str[0] = '\0';
}
gettext("MAC: failed to bring "
goto error;
}
}
return (RCM_SUCCESS);
/* bring data links down */
for (i--; i >= 0; i--) {
}
return (RCM_FAILURE);
}
/*
* Since all we do is pass operations thru, we provide a general
* routine for passing through operations.
*/
/*ARGSUSED*/
static int
{
int rv = RCM_SUCCESS;
char dev_name[MAXNAMELEN];
/*
* Lock the cache just long enough to extract information about this
* resource.
*/
(void) mutex_lock(&cache_lock);
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* We need to propagate the notification to the MAC clients
* that are configured on top of the MACs of the specified
* device. These MAC clients can be virtual links,
* or link aggregation groups.
*/
/*
* Remove notifications are unconditional in the RCM state model,
* so it's safe to remove the node from the cache at this point.
* And we need to remove it so that we will recognize it as a new
* resource following the reattachment of the resource.
*/
if (op == MAC_OP_REMOVE) {
}
(void) mutex_unlock(&cache_lock);
/*
* Obtain the list of virtual datalinks configured on currently
* active on top of the MAC ports registered by the device.
*/
(op == MAC_OP_RESUME))
else
if (ndep == -1) {
rv = RCM_FAILURE;
goto done;
goto done;
}
switch (op) {
case MAC_OP_SUSPEND:
break;
case MAC_OP_OFFLINE:
/* refuse operation if aggregation defined on a MAC port */
if (naggr == -1) {
rv = RCM_FAILURE;
break;
} else if (naggr > 0) {
/*
* Active link aggregation(s) defined on at least
* one of the MAC ports registered by the device
* being offlined.
*/
char *errstr;
char errgrp[64];
int i;
"Resource is in use by aggregation"));
gettext("MAC: malloc failure"));
rv = RCM_FAILURE;
goto done;
}
for (i = 0; i < naggr; i++) {
aggr[i]);
gettext("MAC: malloc failure"));
rv = RCM_FAILURE;
goto done;
}
}
rv = RCM_FAILURE;
break;
}
if (ndep == 0)
break;
/* propagate offline request */
if (rv != RCM_SUCCESS)
break;
break;
break;
case MAC_OP_REMOVE:
break;
case MAC_OP_ONLINE:
if (rv != RCM_SUCCESS)
break;
break;
case MAC_OP_RESUME:
break;
default:
return (RCM_FAILURE);
}
done:
if (rv != RCM_SUCCESS) {
gettext("MAC: %s operation failed\n"),
mac_op_str[op]);
}
return (rv);
}
/*
* mac_offline()
*
* Determine dependents of the resource being offlined, and offline
* them all.
*/
static int
{
dependent_reason, NULL));
}
/*
* mac_online()
*
* Remount the previously offlined filesystem, and online its dependents.
*/
static int
{
dependent_reason, NULL));
}
/*
* mac_getinfo()
*
* Gather usage information for this resource.
*
* Locking: the cache is locked while this routine looks up the
* resource and extracts copies of any piece of information it needs.
* The cache is then unlocked, and this routine performs the rest of
* its functions without touching any part of the cache.
*/
/*ARGSUSED*/
static int
{
int len;
const char *info_fmt;
char **dl_paths;
info_fmt = "MAC %s";
(void) mutex_lock(&cache_lock);
if (!node) {
(void) mutex_unlock(&cache_lock);
return (RCM_FAILURE);
}
return (RCM_FAILURE);
}
/* Fill in the string */
if (flag & RCM_INCLUDE_DEPENDENT) {
char dev_name[MAXNAMELEN];
int ndep;
/* get list of configured datalinks */
if (ndep != 0) {
}
}
(void) mutex_unlock(&cache_lock);
return (RCM_SUCCESS);
}
/*
* mac_suspend()
*
* Notify all dependents that the resource is being suspended.
* Since no real operation is involved, QUERY or not doesn't matter.
*
* Locking: the cache is only used to retrieve some information about
* this resource, so it is only locked during that retrieval.
*/
static int
{
dependent_reason, (void *)interval));
}
/*
* mac_resume()
*
* Resume all the dependents of a suspended network.
*
* Locking: the cache is only used to retrieve some information about
* this resource, so it is only locked during that retrieval.
*/
static int
{
dependent_info, NULL));
}
/*
* mac_remove()
*
* This is another NO-OP for us, we propagate the information. We
* don't need to remove it from our cache. We don't unregister
* interest at this point either; the network device name is still
* around. This way we don't have to change this logic when we
* gain the ability to learn about DR attach operations.
*/
static int
{
dependent_info, NULL));
}
/*
* Process post-attach notifications sent by devfs for devices
* that created DDI_NT_MAC minor nodes. Bring up the links
* that are configured on top of the corresponding MAC ports.
*/
/*ARGSUSED*/
static int
{
return (RCM_FAILURE);
}
/* update cache to reflect attached nodes */
/* Process the nvlist for the event */
if (process_nvlist(nvl) != 0) {
gettext("MAC: Error processing resource attributes(%s)\n"),
rsrc);
gettext("MAC: One or more devices may not be "
"configured.\n"));
}
return (RCM_SUCCESS);
}
/*
* Cache management routines. Note that the cache is implemented as a
* trivial linked list, and is only required because RCM doesn't
* provide enough state about our own registrations back to us. This
* linked list implementation probably clobbers the CPU cache pretty
* well.
*/
/*
* cache_lookup()
*
* Get a cache node for a resource. Call with cache lock held.
*/
static mac_cache_t *
cache_lookup(const char *resource)
{
while (probe != &cache_tail) {
return (probe);
}
}
return (NULL);
}
/*
* free_node()
*
* Free a node. Make sure it isn't in the list!
*/
static void
{
}
}
/*
* cache_insert()
*
* Call with the cache_lock held.
*/
static void
{
/* insert at the head for best performance */
}
/*
* cache_remove()
*
* Call with the cache_lock held.
*/
static void
{
}
/*
* di_walk_minor() walker. Invoked for each DDI_NT_MAC device.
*/
/*ARGSUSED*/
static int
{
char *devfspath;
char resource[MAXPATHLEN];
char *name;
char *cp;
int instance;
/* doesn't look like a MAC device */
return (DI_WALK_CONTINUE);
}
/*
* We need to register interest for devices that
* can be unconfigured, suspended, etc, and registered
* one or more MAC ports with the kernel.
*
* In our cache, we keep one entry per device that registered
* MAC ports. Each cache entry is also associated with a
* list of MAC ports that have been registered by the
* device associated with that cache entry.
*/
/* what else can we do? */
return (DI_WALK_CONTINUE);
}
/* no devfs path?!? */
return (DI_WALK_CONTINUE);
}
/* ignore pseudo devices, they are not NICs */
return (DI_WALK_CONTINUE);
}
resource);
} else {
resource);
gettext("MAC: malloc failure"));
return (DI_WALK_CONTINUE);
}
return (DI_WALK_CONTINUE);
}
}
return (DI_WALK_CONTINUE);
}
static void
{
int rv;
(void) mutex_lock(&cache_lock);
/* first we walk the entire cache, marking each entry stale */
while (probe != &cache_tail) {
}
if (root == DI_NODE_NIL) {
goto done;
}
while (probe != &cache_tail) {
continue;
}
continue;
}
if (rv != RCM_SUCCESS) {
gettext("MAC: failed to register %s\n"),
} else {
}
}
done:
(void) mutex_unlock(&cache_lock);
}
static void
free_cache(void)
{
(void) mutex_lock(&cache_lock);
while (probe != &cache_tail) {
}
(void) mutex_unlock(&cache_lock);
}
/*
* Walker state and function used to bring up the virtual datalinks
* that are configured on top of a MAC port for which we received
* a post-attach notification.
*/
typedef struct dl_evt_walker_state {
char ws_dev_name[MAXNAMELEN];
int ws_port_num;
static void
{
int rc;
/* no match */
return;
}
/* we have a match, bring up the datalink */
if (rc != 0) {
char diag_str[256];
if (diag != 0) {
dladm_diag(diag));
} else {
diag_str[0] = '\0';
}
gettext("MAC: error (%s) configuring "
}
}
/*
* Process a notification received for a MAC minor node. Bring up
* each link that is configured on top of the MAC port.
*/
static void
struct devfs_minor_data *mdata)
{
/* Process MAC devices only */
return;
}
return;
}
"(dev=%s, name=%s, inst=%d, port=\"%s\")\n",
}
/*
* Process a post-attached notification nvlist sent by devfs.
*/
static int
{
char *driver_name;
char *devfs_path;
char *minor_byte_array;
/* Get driver name */
gettext("MAC: cannot get driver name\n"));
return (-1);
}
}
/* Get instance */
"MAC: cannot get device instance\n"));
return (-1);
}
}
/* Get devfs_path */
gettext("MAC: cannot get device path\n"));
return (-1);
}
}
/* Get minor data */
"MAC: cannot get device minor data\n"));
return (-1);
}
"MAC: cannot get minor node data\n"));
return (-1);
}
sizeof (struct devfs_minor_data));
gettext("MAC: calloc error(%s)\n"),
goto bail;
}
/* Enumerate minor node data */
NULL) {
/* Get minor type */
RCM_NV_MINOR_TYPE) == 0) {
if (nvpair_value_int32(mnvp,
&mdata->minor_type) != 0) {
gettext("MAC: cannot get "
"minor type \n"));
goto bail;
}
}
/* Get minor name */
RCM_NV_MINOR_NAME) == 0) {
if (nvpair_value_string(mnvp,
&mdata->minor_name) != 0) {
gettext("MAC: cannot get "
"minor name \n"));
goto bail;
}
}
/* Get minor node type */
RCM_NV_MINOR_NODE_TYPE) == 0) {
if (nvpair_value_string(mnvp,
&mdata->minor_node_type) != 0) {
gettext("MAC: cannot get "
"minor node type \n"));
goto bail;
}
}
}
mdata);
}
}
return (0);
bail:
return (-1);
}