#include <sys/param.h>
#include <sys/iov.h>
#include <sys/dnv.h>
#include <sys/nv.h>
#include <err.h>
#include <regex.h>
#include <stdlib.h>
#include "iovctl.h"
static nvlist_t *
find_config(nvlist_t *config, const char * device)
{
nvlist_t *subsystem, *empty_driver, *empty_iov;
subsystem = dnvlist_take_nvlist(config, device, NULL);
if (subsystem != NULL)
return (subsystem);
empty_driver = nvlist_create(NV_FLAG_IGNORE_CASE);
if (empty_driver == NULL)
err(1, "Could not allocate config nvlist");
empty_iov = nvlist_create(NV_FLAG_IGNORE_CASE);
if (empty_iov == NULL)
err(1, "Could not allocate config nvlist");
subsystem = nvlist_create(NV_FLAG_IGNORE_CASE);
if (subsystem == NULL)
err(1, "Could not allocate config nvlist");
nvlist_move_nvlist(subsystem, DRIVER_CONFIG_NAME, empty_driver);
nvlist_move_nvlist(subsystem, IOV_CONFIG_NAME, empty_iov);
return (subsystem);
}
static uint16_t
parse_vf_num(const char *key, regmatch_t *matches)
{
u_long vf_num;
vf_num = strtoul(key + matches[1].rm_so, NULL, 10);
if (vf_num > UINT16_MAX)
errx(1, "VF number %lu is too large to be valid",
vf_num);
return (vf_num);
}
static void
apply_subsystem_defaults(nvlist_t *device_config, const char *subsystem,
const nvlist_t *device_defaults)
{
nvlist_t *config;
const nvlist_t *defaults;
const char *name;
void *cookie;
size_t len;
const void *bin;
int type;
config = nvlist_take_nvlist(device_config, subsystem);
defaults = nvlist_get_nvlist(device_defaults, subsystem);
cookie = NULL;
while ((name = nvlist_next(defaults, &type, &cookie)) != NULL) {
if (nvlist_exists(config, name))
continue;
switch (type) {
case NV_TYPE_BOOL:
nvlist_add_bool(config, name,
nvlist_get_bool(defaults, name));
break;
case NV_TYPE_NUMBER:
nvlist_add_number(config, name,
nvlist_get_number(defaults, name));
break;
case NV_TYPE_STRING:
nvlist_add_string(config, name,
nvlist_get_string(defaults, name));
break;
case NV_TYPE_NVLIST:
nvlist_add_nvlist(config, name,
nvlist_get_nvlist(defaults, name));
break;
case NV_TYPE_BINARY:
bin = nvlist_get_binary(defaults, name, &len);
nvlist_add_binary(config, name, bin, len);
break;
default:
errx(1, "Unexpected type '%d'", type);
}
}
nvlist_move_nvlist(device_config, subsystem, config);
}
static void
apply_defaults(nvlist_t *vf, const nvlist_t *defaults)
{
apply_subsystem_defaults(vf, DRIVER_CONFIG_NAME, defaults);
apply_subsystem_defaults(vf, IOV_CONFIG_NAME, defaults);
}
static void
validate_subsystem(const nvlist_t *device, const nvlist_t *device_schema,
const char *subsystem_name, const char *config_name)
{
const nvlist_t *subsystem, *schema, *config;
const char *name;
void *cookie;
int type;
subsystem = nvlist_get_nvlist(device, subsystem_name);
schema = nvlist_get_nvlist(device_schema, subsystem_name);
cookie = NULL;
while ((name = nvlist_next(schema, &type, &cookie)) != NULL) {
config = nvlist_get_nvlist(schema, name);
if (dnvlist_get_bool(config, REQUIRED_SCHEMA_NAME, false)) {
if (!nvlist_exists(subsystem, name))
errx(1,
"Required parameter '%s' not found in '%s'",
name, config_name);
}
}
}
static void
validate_device(const nvlist_t *device, const nvlist_t *schema,
const char *config_name)
{
validate_subsystem(device, schema, DRIVER_CONFIG_NAME, config_name);
validate_subsystem(device, schema, IOV_CONFIG_NAME, config_name);
}
static uint16_t
get_num_vfs(const nvlist_t *pf)
{
const nvlist_t *iov;
iov = nvlist_get_nvlist(pf, IOV_CONFIG_NAME);
return (nvlist_get_number(iov, "num_vfs"));
}
void
validate_config(nvlist_t *config, const nvlist_t *schema, const regex_t *vf_pat)
{
char device_name[VF_MAX_NAME];
regmatch_t matches[2];
nvlist_t *defaults, *pf, *vf;
const nvlist_t *vf_schema;
const char *key;
void *cookie;
int i, type;
uint16_t vf_num, num_vfs;
pf = find_config(config, PF_CONFIG_NAME);
validate_device(pf, nvlist_get_nvlist(schema, PF_CONFIG_NAME),
PF_CONFIG_NAME);
nvlist_move_nvlist(config, PF_CONFIG_NAME, pf);
num_vfs = get_num_vfs(pf);
vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
if (num_vfs == 0)
errx(1, "PF.num_vfs must be at least 1");
defaults = dnvlist_take_nvlist(config, DEFAULT_SCHEMA_NAME, NULL);
for (i = 0; i < num_vfs; i++) {
snprintf(device_name, sizeof(device_name), VF_PREFIX"%d",
i);
vf = find_config(config, device_name);
if (defaults != NULL)
apply_defaults(vf, defaults);
validate_device(vf, vf_schema, device_name);
nvlist_move_nvlist(config, device_name, vf);
}
nvlist_destroy(defaults);
cookie = NULL;
while ((key = nvlist_next(config, &type, &cookie)) != NULL) {
if (regexec(vf_pat, key, nitems(matches), matches, 0) == 0) {
vf_num = parse_vf_num(key, matches);
if (vf_num >= num_vfs)
errx(1,
"VF number %d is out of bounds (num_vfs=%d)",
vf_num, num_vfs);
}
}
}