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 transport.c
16e76cdd6e3cfaac7d91c3b0644ee1bc6cf52347agiri * Oracle elects to have and use the contents of transport.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/ksynch.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/list.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/rds.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/sysmacros.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/rdsv3.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/loop.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/rdsv3_impl.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota#include <sys/ib/clients/rdsv3/rdsv3_debug.h>
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Otastruct rdsv3_transport *transports[RDS_TRANS_COUNT];
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otakrwlock_t trans_sem; /* this was a semaphore */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otaint
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_trans_register(struct rdsv3_transport *trans)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF4("rdsv3_trans_register", "Enter(trans: %p)", trans);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rw_enter(&trans_sem, RW_WRITER);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota if (transports[trans->t_type]) {
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota cmn_err(CE_WARN,
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota "RDSV3 Transport type %d already registered\n",
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota trans->t_type);
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota rw_exit(&trans_sem);
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota return (1);
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota } else {
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota transports[trans->t_type] = trans;
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota RDSV3_DPRINTF2("rdsv3_trans_register",
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota "Registered RDS/%s transport\n", trans->t_name);
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rw_exit(&trans_sem);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF4("rdsv3_trans_register", "Return(trans: %p)", trans);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (0);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otavoid
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_trans_unregister(struct rdsv3_transport *trans)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF4("rdsv3_trans_register", "Enter(trans: %p)", trans);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rw_enter(&trans_sem, RW_WRITER);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota transports[trans->t_type] = NULL;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rw_exit(&trans_sem);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF4("rdsv3_trans_register", "Return(trans: %p)", trans);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otastruct rdsv3_transport *
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_trans_get_preferred(uint32_be_t addr)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota struct rdsv3_transport *ret = NULL;
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota int i;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF4("rdsv3_trans_get_preferred", "Enter(addr: %x)",
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota ntohl(addr));
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota if (rdsv3_isloopback(addr))
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (&rdsv3_loop_transport);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rw_enter(&trans_sem, RW_READER);
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota for (i = 0; i < RDS_TRANS_COUNT; i++) {
5d5562f583b2b6affe19bdce0b3c8b1840d667a4Eiji Ota if (transports[i] &&
5d5562f583b2b6affe19bdce0b3c8b1840d667a4Eiji Ota transports[i]->laddr_check(addr) == 0) {
cadbfdc3bdb156e92d7a88978bc98ea87f6e037fEiji Ota ret = transports[i];
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota break;
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota }
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota rw_exit(&trans_sem);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota RDSV3_DPRINTF4("rdsv3_trans_get_preferred",
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota "Return(addr: %x, ret: %p)", ntohl(addr), ret);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota return (ret);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/*
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * This returns the number of stats entries in the snapshot and only
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * copies them using the iter if there is enough space for them. The
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * caller passes in the global stats so that we can size and copy while
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota * holding the lock.
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota/* ARGSUSED */
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otaunsigned int
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Otardsv3_trans_stats_info_copy(struct rdsv3_info_iterator *iter,
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota unsigned int avail)
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota{
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota struct rdsv3_transport *trans;
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota unsigned int total = 0;
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota unsigned int part;
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota int i;
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota rw_enter(&trans_sem, RW_READER);
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota for (i = 0; i < RDS_TRANS_COUNT; i++) {
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota trans = transports[i];
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota if (!trans || !trans->stats_info_copy)
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota continue;
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota part = trans->stats_info_copy(iter, avail);
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota avail -= min(avail, part);
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota total += part;
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota }
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota rw_exit(&trans_sem);
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota
5e12ddada2833f3aa285210603ce9aaeb8be35ccEiji Ota return (total);
c0dd49bdd68c0d758a67d56f07826f3b45cfc664Eiji Ota}