c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * This file contains code imported from the OFED rds source file info.c
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * Oracle elects to have and use the contents of info.c under and governed
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * by the OpenIB.org BSD license (see below for full license text). However,
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * the following notice accompanied the original version of this file:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Copyright (c) 2006 Oracle. All rights reserved.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * This software is available to you under a choice of one of two
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * licenses. You may choose to be licensed under the terms of the GNU
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * General Public License (GPL) Version 2, available from the file
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * COPYING in the main directory of this source tree, or the
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * OpenIB.org BSD license below:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * Redistribution and use in source and binary forms, with or
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * without modification, are permitted provided that the following
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * conditions are met:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - Redistributions of source code must retain the above
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * copyright notice, this list of conditions and the following
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * disclaimer.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - Redistributions in binary form must reproduce the above
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * copyright notice, this list of conditions and the following
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * disclaimer in the documentation and/or other materials
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * provided with the distribution.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * SOFTWARE.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/rds.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/rdsv3.h>
b27516f55237249607f754e6e42e865f12456675agiri#include <sys/ib/clients/rdsv3/rdsv3_debug.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * This file implements a getsockopt() call which copies a set of fixed
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * sized structs into a user-specified buffer as a means of providing
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * read-only information about RDS.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * For a given information source there are a given number of fixed sized
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * structs at a given time. The structs are only copied if the user-specified
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * buffer is big enough. The destination pages that make up the buffer
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * are pinned for the duration of the copy.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * This gives us the following benefits:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - simple implementation, no copy "position" across multiple calls
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - consistent snapshot of an info source
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - atomic copy works well with whatever locking info source has
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - one portable tool to get rds info across implementations
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - long-lived tool can get info without allocating
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * at the following costs:
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * - info source copy must be pinned, may be "large"
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otastatic kmutex_t rdsv3_info_lock;
fe817b6022080da0a98b5d2d8cd179f594d6ca5eEiji Otastatic rdsv3_info_func rdsv3_info_funcs[RDS_INFO_LAST - RDS_INFO_FIRST + 1];
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otavoid
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_info_register_func(int optname, rdsv3_info_func func)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
fe817b6022080da0a98b5d2d8cd179f594d6ca5eEiji Ota int offset = optname - RDS_INFO_FIRST;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
fe817b6022080da0a98b5d2d8cd179f594d6ca5eEiji Ota ASSERT(optname >= RDS_INFO_FIRST && optname <= RDS_INFO_LAST);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota mutex_enter(&rdsv3_info_lock);
5d5562f583b2b6affe19bdce0b3c8b1840d667a4Eiji Ota ASSERT(!rdsv3_info_funcs[offset]);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rdsv3_info_funcs[offset] = func;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota mutex_exit(&rdsv3_info_lock);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/* ARGSUSED */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otavoid
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_info_deregister_func(int optname, rdsv3_info_func func)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
fe817b6022080da0a98b5d2d8cd179f594d6ca5eEiji Ota int offset = optname - RDS_INFO_FIRST;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
fe817b6022080da0a98b5d2d8cd179f594d6ca5eEiji Ota ASSERT(optname >= RDS_INFO_FIRST && optname <= RDS_INFO_LAST);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota mutex_enter(&rdsv3_info_lock);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rdsv3_info_funcs[offset] = NULL;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota mutex_exit(&rdsv3_info_lock);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * @optval points to the userspace buffer that the information snapshot
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * will be copied into.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * This function returns -errno if there is a failure, particularly -ENOSPC
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * if the given userspace buffer was not large enough to fit the snapshot.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * On success it returns the positive number of bytes of each array element
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * in the snapshot.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otaint
b27516f55237249607f754e6e42e865f12456675agirirdsv3_info_ioctl(struct rsock *sock, int optname, char *optval,
b27516f55237249607f754e6e42e865f12456675agiri int32_t *rvalp)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota struct rdsv3_info_iterator iter;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota struct rdsv3_info_lengths lens;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rdsv3_info_func func;
b27516f55237249607f754e6e42e865f12456675agiri struct rds_info_arg arg;
b27516f55237249607f754e6e42e865f12456675agiri uint32_t ulen = 0, klen;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
fe817b6022080da0a98b5d2d8cd179f594d6ca5eEiji Ota func = rdsv3_info_funcs[optname - RDS_INFO_FIRST];
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (func == NULL) {
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF2("rdsv3_info_ioctl",
b27516f55237249607f754e6e42e865f12456675agiri "No Info Function, optname: %d", optname);
b27516f55237249607f754e6e42e865f12456675agiri return (ENOPROTOOPT);
b27516f55237249607f754e6e42e865f12456675agiri }
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri if (optval == NULL) {
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF2("rdsv3_info_ioctl", "optval is NULL");
b27516f55237249607f754e6e42e865f12456675agiri return (EINVAL);
b27516f55237249607f754e6e42e865f12456675agiri }
b27516f55237249607f754e6e42e865f12456675agiri if (ddi_copyin(optval, &arg, sizeof (struct rds_info_arg), 0) != 0) {
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF2("rdsv3_info_ioctl",
b27516f55237249607f754e6e42e865f12456675agiri "ddi_copyin for address: 0x%p failed", optval);
b27516f55237249607f754e6e42e865f12456675agiri return (EFAULT);
b27516f55237249607f754e6e42e865f12456675agiri }
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF4("rdsv3_info_ioctl",
b27516f55237249607f754e6e42e865f12456675agiri "optname: %d lenp: %llx datap: %llx", optname, arg.lenp, arg.datap);
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri if (arg.lenp == NULL) {
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF2("rdsv3_info_ioctl", "arg.lenp is NULL");
b27516f55237249607f754e6e42e865f12456675agiri return (EFAULT);
b27516f55237249607f754e6e42e865f12456675agiri }
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri if (ddi_copyin((void *)(uintptr_t)arg.lenp, &ulen,
b27516f55237249607f754e6e42e865f12456675agiri sizeof (uint32_t), 0) != 0) {
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF2("rdsv3_info_ioctl",
b27516f55237249607f754e6e42e865f12456675agiri "ddi_copyin for address, lenp: 0x%p failed", arg.lenp);
b27516f55237249607f754e6e42e865f12456675agiri return (EFAULT);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF3("rdsv3_info_ioctl", "optname: %d len: %d datap: %p",
b27516f55237249607f754e6e42e865f12456675agiri optname, ulen, arg.datap);
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri bzero(&iter, sizeof (struct rdsv3_info_iterator));
b27516f55237249607f754e6e42e865f12456675agiri /* a 0 len call is just trying to probe its length */
b27516f55237249607f754e6e42e865f12456675agiri if (ulen == 0) {
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota iter.addr = NULL;
b27516f55237249607f754e6e42e865f12456675agiri } else if (arg.datap == NULL) {
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF2("rdsv3_info_ioctl",
b27516f55237249607f754e6e42e865f12456675agiri "arg.datap is NULL, ulen set to: %d", ulen);
b27516f55237249607f754e6e42e865f12456675agiri return (EINVAL);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota } else {
b27516f55237249607f754e6e42e865f12456675agiri iter.addr = (char *)(uintptr_t)arg.datap;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota iter.offset = 0;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
b27516f55237249607f754e6e42e865f12456675agiri bzero(&lens, sizeof (struct rdsv3_info_lengths));
b27516f55237249607f754e6e42e865f12456675agiri func(sock, ulen, &iter, &lens);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
b27516f55237249607f754e6e42e865f12456675agiri klen = lens.nr * lens.each;
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri if (ddi_copyout(&klen, (void *)(uintptr_t)arg.lenp,
b27516f55237249607f754e6e42e865f12456675agiri sizeof (uint32_t), 0) != 0) {
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF2("rdsv3_info_ioctl",
b27516f55237249607f754e6e42e865f12456675agiri "ddi_copyout(%p %p) failed", &klen, arg.lenp);
b27516f55237249607f754e6e42e865f12456675agiri return (EFAULT);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF3("rdsv3_info_ioctl",
b27516f55237249607f754e6e42e865f12456675agiri "optname: %d ulen: %d klen: %d each: %d", optname, ulen, klen,
b27516f55237249607f754e6e42e865f12456675agiri lens.each);
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri if (ulen < klen) {
b27516f55237249607f754e6e42e865f12456675agiri return (ENOSPC);
b27516f55237249607f754e6e42e865f12456675agiri }
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri RDSV3_DPRINTF4("rdsv3_info_ioctl", "Return optname: %d", optname);
b27516f55237249607f754e6e42e865f12456675agiri
b27516f55237249607f754e6e42e865f12456675agiri *rvalp = lens.each;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (0);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}