Current File : //usr/include/libibadm.h
/*
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
 */

#ifndef	_LIBIBADM_H
#define	_LIBIBADM_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <libibadm.h>
#include <sys/stropts.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>

/*
 * WARNING
 *
 * The interfaces defined in this file are private to the implementation
 * of Solaris and are subject to change at any time without notice.
 * Applications using these interfaces may fail to run on future releases.
 */

/*
 * option flags taken by the libibadm functions
 *
 *  - IBADM_OPT_PERSIST:
 *	Requests to persist the configuration so that it can be
 *	re-enabled or reapplied on boot and used by list-* command
 *	to retrieve persistent values.
 *
 *  - IBADM_OPT_ACTIVE:
 *	Requests to apply configuration without persisting it and
 *	used by list-* subcommands to retrieve current values.
 *
 *  - IBADM_OPT_DEFAULT:
 *	Used to specify retrieval of default property.
 *
 *  - IBADM_OPT_PERM:
 *	Used to specify retrieval of permissions on property.  Valid values
 *	are IBADM_PROPVAL_RW, IBADM_PROPVAL_RDONLY.
 *
 *  - IBADM_OPT_POSSIBLE:
 *	Used to specify retrieval of posssible ranges/values of property value.
 *
 *  - IBADM_OPT_ORIGINAL
 *      Set by libibadm when values are modified in the active or
 *      persistent store to capture the original value before the
 *      change was made. Used by ibmgmtd for auditing.
 *
 *  - IBADM_OPT_RW:
 *	Next property retrieved by ibadm_conf_next_prop() must be read-write.
 *
 *  - IBADM_OPT_RDONLY:
 *	Next property retrieved by ibadm_conf_next_prop() must be read-only.
 *
 *  - IBADM_OPT_FORCE
 *	Forces a change into the persistent store even if the HCA
 *	is in a state that cannot accept the change yet. A flag will
 *	will be set on the active HCA indicating a pending change will
 *	be applied on reboot.
 *
 *  - IBADM_OPT_CREATE_VHCA
 *	Create vHCA, either using valid vHCA name specified, or create a
 *	vHCA associated with the specified HCA, assigning it an available
 *	vHCA index.  If setting properties for a vHCA prior to calling
 *	ibadm_write_conf(), this flag should also be set to ensure that
 *	per-property validation knows that the properties are virtual.
 *
 *  - IBADM_OPT_IOV_COMMAND
 *      enable-iov or disable-iov command was entered on the calling
 *      application's command line. This option allows ibmgmtd to
 *      provide more specific audit information.
 *
 *  - IBADM_OPT_RESET_COMMAND
 *      reset-hcaprop or reset-portprop command was entered on the calling
 *      application's command line. This option allows ibmgmtd to provide
 *	more specific audit information.
 */
#define	IBADM_OPT_PERSIST		0x00000001
#define	IBADM_OPT_ACTIVE		0x00000002
#define	IBADM_OPT_DEFAULT		0x00000004
#define	IBADM_OPT_PERM			0x00000008
#define	IBADM_OPT_POSSIBLE		0x00000010
#define	IBADM_OPT_ORIGINAL		0x00000020
#define	IBADM_OPT_RW			0x00000040
#define	IBADM_OPT_RDONLY		0x00000080
#define	IBADM_OPT_FORCE			0x00000100
#define	IBADM_OPT_CREATE_VHCA		0x00000200
#define	IBADM_OPT_IOV_COMMAND		0x00000400
#define	IBADM_OPT_RESET_COMMAND		0x00000800

/*
 * Options to ibadm_open().
 *
 * - IBADM_OPT_DEBUG
 *	Debug messags will either be displayed or logged via syslog(1M).
 *
 * - IBADM_OPT_IBMGMTD
 *	Handle is opened in ibmgmtd execution context.
 *
 * - IBADM_OPT_REOPEN
 *	Existing handle is re-opened with specified options.
 */
#define	IBADM_OPT_DEBUG			0x00001000
#define	IBADM_OPT_IBMGMTD		0x00002000
#define	IBADM_OPT_REOPEN		0x00004000

#define	IBADM_OPT_ALL			(IBADM_OPT_PERSIST | IBADM_OPT_ACTIVE)
#define	IBADM_OPT_INFO			(IBADM_OPT_DEFAULT | IBADM_OPT_PERM | \
					IBADM_OPT_POSSIBLE)
#define	IBADM_OPT_PROP_MASK		(IBADM_OPT_ALL | IBADM_OPT_INFO | \
					IBADM_OPT_ORIGINAL)

#define	IBMGMT_FMRI			"svc:/network/ib/ib-management:default"

#define	IBMGMT_DOOR_FILE		"/system/volatile/ibmgmt_door"


/* error codes */
typedef enum {
	IBADM_SUCCESS,			/* No error occurred */
	IBADM_NOT_FOUND,		/* No such object */
	IBADM_EXISTS,			/* Object already exists */
	IBADM_NO_MEMORY,		/* Insufficient memory */
	IBADM_BUSY,			/* Entity is busy */
	IBADM_FAILURE,			/* Generic failure */
	IBADM_INVALID_ARG,		/* Invalid argument */
	IBADM_INSUFF_AUTH,		/* Insufficient user authorizations */
	IBADM_PERM_DENIED,		/* Permission denied */
	IBADM_NOT_OWNER,		/* Object not administered by ibadm. */
	IBADM_BOOT_DEV,			/* Object is boot device */
	IBADM_CONF_BOOT_FAIL,		/* Change boot device config failed */
	IBADM_NO_VHCAS,			/* No vHCAs are available. */
	IBADM_IOV_UNSET,		/* IOV could not be set */
	IBADM_IOV_UNSUP,		/* IOV not supported */
	IBADM_WALK_DONE,		/* Walk successfully completed. */
	IBADM_WALK_FAILED,		/* Error encountered during walk. */
	IBADM_INVALID_NAME,		/* Invalid name for entity. */
	IBADM_INVALID_VALUE,		/* Invalid property value. */
	IBADM_INVALID_VALUE_POSSIBLE,	/* Not one of possible values/ranges. */
	IBADM_INVALID_NUM_VALUES,	/* Invalid number of values. */
	IBADM_INVALID_PROPERTY,		/* Invalid property for object. */
	IBADM_INVALID_PROPERTY_NAME,	/* Invalid property name. */
	IBADM_VALUE_READONLY,		/* Value is read-only. */
	IBADM_CHANGE_PENDING,		/* Change will take effect on reboot */
	IBADM_UNAVAIL,			/* ib-management service unavailable */
	IBADM_IOV_OFF			/* IOV is not enabled */
} ibadm_status_t;

typedef enum {
	IBADM_CLASS_INVALID	=		0,
	IBADM_CLASS_HCA		=		0x1,
	IBADM_CLASS_PORT	=		0x2
} ibadm_class_t;

#define	IBADM_CLASS_HCA_STRING			"hca"
#define	IBADM_CLASS_PORT_STRING			"port"

#define	IBADM_MAX_NAMELEN			64
#define	IBADM_MAX_VALUELEN			512
#define	IBADM_MAX_NUM_PROPERTIES		32
#define	IBADM_MAX_NUM_VALUES			32
#define	IBADM_MAX_NUM_PORTS			2

#define	IBADM_CONF_NAME				"name"
#define	IBADM_CONF_CLASS			"class"
#define	IBADM_CONF_PROPERTIES			"properties"
#define	IBADM_CONF_CHILDREN			"children"
#define	IBADM_CONF_PARENT			"parent"
#define	IBADM_CONF_NONAME			"-"

#define	IBADM_OPT_ACTIVE_STRING			"active"
#define	IBADM_OPT_PERSIST_STRING		"persistent"
#define	IBADM_OPT_DEFAULT_STRING		"default"
#define	IBADM_OPT_PERM_STRING			"perm"
#define	IBADM_OPT_POSSIBLE_STRING		"possible"
#define	IBADM_OPT_RW_STRING			"rw"
#define	IBADM_OPT_READONLY_STRING		"r"
#define	IBADM_OPT_FORCE_STRING			"force"
#define	IBADM_OPT_ORIGINAL_STRING		"original"

#define	IBADM_PROP_TYPE				"type"
#define	IBADM_PROP_OVER				"over"
#define	IBADM_PROP_HW_MAX_VHCAS			"hw-max-vhcas"
#define	IBADM_PROP_WANTED_VHCAS			"wanted-vhcas"
#define	IBADM_PROP_MAX_VHCAS			"max-vhcas"
#define	IBADM_PROP_ACTIVE_VHCAS			"active-vhcas"
#define	IBADM_PROP_CONF_VHCAS			"configured-vhcas"
/* libibadm walker generated vhcas-inuse includes active and persistent */
#define	IBADM_PROP_VHCAS_INUSE			"vhcas-inuse"
#define	IBADM_PROP_NUM_PORTS			"num-ports"
#define	IBADM_PROP_DEVICE_PATH			"device-path"
#define	IBADM_PROP_IOV				"iov"
#define	IBADM_PROP_OWNER			"owner"
#define	IBADM_PROP_NODE_GUID			"node-guid"
#define	IBADM_PROP_ZONE				"zone"
#define	IBADM_PROP_GUIDS			"guids"
#define	IBADM_PROP_PKEYS			"pkeys"
#define	IBADM_PROP_SMI_ENABLED			"smi-enabled"

#define	IBADM_PROPVAL_NONE			"-"
/* Values for IBADM_OPT_PERM (permissions) */
#define	IBADM_PROPVAL_RW			"rw"
#define	IBADM_PROPVAL_RDONLY			"r-"
/* Values for IBADM_PROP_IOV. */
#define	IBADM_PROPVAL_ON			"on"
#define	IBADM_PROPVAL_OFF			"off"
#define	IBADM_PROPVAL_TRUE			"true"
#define	IBADM_PROPVAL_FALSE			"false"
/* Values for IBADM_PROP_TYPE. */
#define	IBADM_PROPVAL_PHYSICAL			"physical"
#define	IBADM_PROPVAL_VIRTUAL			"virtual"
/* Values for port IBADM_PROP_GUIDS. */
#define	IBADM_PROPVAL_SM_GENERATE		"sm-generate"
/* Values for IBADM_PROP_OWNER. */
#define	IBADM_PROPVAL_EXTERNAL			"external"
#define	IBADM_PROPVAL_SOLARIS			"solaris"
#define	IBADM_PROPVAL_SOLARIS_BOOT		"solaris-boot"
/* Default for max-vhcas */
#define	IBADM_PROPVAL_DEFAULT_MAX_VHCAS		"0"
/* Fallback value for max-vhcas that succeeds on most systems. */
#define	IBADM_PROPVAL_FALLBACK_MAX_VHCAS	"7"
/* Values for pkeys */
#define	IBADM_PROPVAL_AUTO			"auto"
#define	IBADM_PROPVAL_DEFAULT_PKEYS		IBADM_PROPVAL_AUTO
/* Values for smi-enabled */
#define	IBADM_PROPVAL_READONLY			"readonly"

#define	IBADM_PORT_PREFIX			"port"
#define	IBADM_VHCA_PREFIX			"vhca"

typedef void * ibadm_conf_t;

/* opaque ibadm handle to libibadm functions */
struct ibadm_handle_s;
typedef struct ibadm_handle_s	*ibadm_handle_t;

typedef enum {
	IBADM_WRITE_CMD,
	IBADM_WALK_CMD,
	IBADM_DELETE_CMD
} ibadm_door_cmd_t;

/* Must use 32/64 bit architecture insensitive types */
typedef struct ibadm_door_arg {
	uint32_t	ida_cmd;	/* ibadm_door_cmd_t */
	uint32_t	ida_datalen;	/* size_t */
	uint32_t	ida_status;	/* ibadm_status_t */
	uint32_t	ida_class;	/* ibadm_class_t */
	uint32_t	ida_opt;	/* uint_t */
	uint32_t	ida_pad1;
} ibadm_door_arg_t;

/* Convert errno to status. */
extern ibadm_status_t ibadm_errno2status(int);

/* Open/close libibadm handle */
extern ibadm_status_t	ibadm_open(ibadm_handle_t *, uint_t);
extern void		ibadm_close(ibadm_handle_t);

extern void		ibadm_log(ibadm_handle_t, int, const char *, ...);

/* Convert between libibadm status, strings and values. */
extern char *ibadm_status2str(ibadm_status_t);
extern char *ibadm_class2str(ibadm_class_t);
extern ibadm_class_t ibadm_str2class(const char *);
extern char *ibadm_opt2str(uint_t);
extern uint_t ibadm_str2opt(const char *);

/* Convert a port id (e.g. 1, 2) to port name and vice versa. */
extern ibadm_status_t ibadm_portid2portname(uint_t, char *);
extern ibadm_status_t ibadm_portname2portid(const char *, uint_t *);

/* Convert between HCA and vHCA (hermon0, 3 -> hermon.vhca3). */
extern ibadm_status_t ibadm_hca2vhca(const char *, uint_t, char *);
extern ibadm_status_t ibadm_vhca2hca(const char *, char *, uint_t *);

/* Convert between a HCA and device name (hermon0 -> dev hermon, index 0). */
extern ibadm_status_t ibadm_hca2devname(const char *, char *, uint_t *);
extern ibadm_status_t ibadm_devname2hca(const char *, uint_t, char *);

/* Lookup HCA that has matching node GUID. */
extern ibadm_status_t ibadm_guid2hca(ibadm_handle_t, uint_t, char *, char *);

/* Configuration creation, destruction, traversal. */
extern void ibadm_destroy_conf(ibadm_conf_t);
extern ibadm_status_t ibadm_create_conf(ibadm_class_t, const char *,
    ibadm_conf_t *);
extern ibadm_status_t ibadm_get_conf_class(ibadm_conf_t, ibadm_class_t *);
extern ibadm_status_t ibadm_get_conf_name(ibadm_conf_t, char **);
extern ibadm_status_t ibadm_get_conf_parentname(ibadm_conf_t, char **);
extern ibadm_status_t ibadm_get_conf_opt(ibadm_conf_t, uint_t *);
/* The child ibadm_conf_t should be freed after successful addition. */
extern ibadm_status_t ibadm_add_conf_child(ibadm_conf_t, ibadm_conf_t);
extern ibadm_status_t ibadm_remove_conf_child(ibadm_conf_t, const char *);
extern ibadm_status_t ibadm_get_conf_child(ibadm_conf_t, const char *,
    ibadm_conf_t *);
/*
 * Retrieve next child object.  If previous ibadm_conf_t is NULL, retrieve
 * first child object.
 */
extern ibadm_status_t ibadm_next_conf_child(ibadm_conf_t, ibadm_conf_t,
    ibadm_conf_t *);

/*
 * Retrieve next index available for vHCA creation, i.e. not in use
 * by an active or persistent vHCA.
 */
extern ibadm_status_t ibadm_next_vhca_index_available(ibadm_handle_t,
    const char *, uint_t, uint_t *);

/*
 * Walk config, sorted by sort function (if any provided).  Walker function
 * should return 0 on success, 1 for early (successful) termination or anything
 * else to denote early erroneous termination.  Valid only for IBADM_CLASS_HCA.
 */
typedef int (ibadm_walk_conf_cmp_cb_t)(ibadm_conf_t *, ibadm_conf_t *);
typedef int (ibadm_walk_conf_cb_t)(ibadm_handle_t, ibadm_conf_t,
    uint_t, void *);
extern ibadm_status_t	ibadm_walk_conf(ibadm_handle_t, ibadm_class_t,
    ibadm_walk_conf_cmp_cb_t *, ibadm_walk_conf_cb_t *, uint_t, void *);

/*
 * Walk children of parent object, using same semantics as global walk above.
 */
extern ibadm_status_t	ibadm_walk_conf_children(ibadm_handle_t, ibadm_class_t,
    ibadm_conf_t, ibadm_walk_conf_cmp_cb_t *, ibadm_walk_conf_cb_t *,
    uint_t, void *);

/*
 * Pre-canned walker sort functions for the default display order.
 */
extern ibadm_walk_conf_cmp_cb_t ibadm_walk_conf_cmp_hca_cb;
extern ibadm_walk_conf_cmp_cb_t ibadm_walk_conf_cmp_port_cb;

/* Read/write config. */
extern ibadm_status_t ibadm_read_conf(ibadm_handle_t, ibadm_class_t,
    const char *, uint_t, ibadm_conf_t *);
extern ibadm_status_t ibadm_write_conf(ibadm_handle_t, ibadm_conf_t, uint_t);
extern ibadm_status_t ibadm_delete_conf(ibadm_handle_t, ibadm_conf_t, uint_t);
/* Verify will point to erroneous property on property error. */
extern ibadm_status_t ibadm_verify_conf(ibadm_handle_t, ibadm_conf_t, uint_t,
    char **);

/* Used by libibadm, ibmgmtd. */
extern ibadm_status_t ibadm_populate_conf(ibadm_handle_t, ibadm_class_t,
    uint_t, ibadm_conf_t *);

/*
 * If previous conf prop is NULL, return first prop.  Specifying
 * IBADM_OPT_RW or IBADM_OPT_RDONLY returns the next read/write or readonly
 * property.  Specifying opt value of 0 returns next property regardless
 * of writability.
 */
extern ibadm_status_t ibadm_next_conf_prop(ibadm_conf_t, const char *, uint_t,
    char **);

extern ibadm_status_t	ibadm_get_prop_values(ibadm_handle_t, ibadm_conf_t,
    const char *, uint_t, char ***, uint_t *);
extern ibadm_status_t	ibadm_get_prop_value(ibadm_handle_t, ibadm_conf_t,
    const char *, uint_t, char **);

extern ibadm_status_t	ibadm_set_prop_values(ibadm_handle_t, ibadm_conf_t,
    const char *, uint_t, char **, uint_t);
extern ibadm_status_t	ibadm_set_prop_value(ibadm_handle_t, ibadm_conf_t,
    const char *, uint_t, char *);

extern ibadm_status_t	ibadm_reset_prop(ibadm_handle_t, ibadm_conf_t,
    const char *, uint_t);

/* Used by libibadm, ibmgmtd. */
extern ibadm_status_t	ibadm_read_conf_from_door_arg(ibadm_door_arg_t *,
    ibadm_conf_t *);
extern ibadm_status_t	ibadm_register(ibadm_handle_t, const char *,
    const char *);

typedef void (ibadm_reregister_cb_t)(void *);
extern ibadm_status_t	ibadm_reregister(ibadm_handle_t, ibadm_reregister_cb_t);

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBIBADM_H */