/*1* Copyright (c) 2006 Oracle. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*31*/32#include <linux/kernel.h>33#include <linux/module.h>34#include <linux/in.h>3536#include "rds.h"37#include "loop.h"3839static struct rds_transport *transports[RDS_TRANS_COUNT];40static DECLARE_RWSEM(rds_trans_sem);4142int rds_trans_register(struct rds_transport *trans)43{44BUG_ON(strlen(trans->t_name) + 1 > TRANSNAMSIZ);4546down_write(&rds_trans_sem);4748if (transports[trans->t_type])49printk(KERN_ERR "RDS Transport type %d already registered\n",50trans->t_type);51else {52transports[trans->t_type] = trans;53printk(KERN_INFO "Registered RDS/%s transport\n", trans->t_name);54}5556up_write(&rds_trans_sem);5758return 0;59}60EXPORT_SYMBOL_GPL(rds_trans_register);6162void rds_trans_unregister(struct rds_transport *trans)63{64down_write(&rds_trans_sem);6566transports[trans->t_type] = NULL;67printk(KERN_INFO "Unregistered RDS/%s transport\n", trans->t_name);6869up_write(&rds_trans_sem);70}71EXPORT_SYMBOL_GPL(rds_trans_unregister);7273void rds_trans_put(struct rds_transport *trans)74{75if (trans && trans->t_owner)76module_put(trans->t_owner);77}7879struct rds_transport *rds_trans_get_preferred(__be32 addr)80{81struct rds_transport *ret = NULL;82struct rds_transport *trans;83unsigned int i;8485if (IN_LOOPBACK(ntohl(addr)))86return &rds_loop_transport;8788down_read(&rds_trans_sem);89for (i = 0; i < RDS_TRANS_COUNT; i++) {90trans = transports[i];9192if (trans && (trans->laddr_check(addr) == 0) &&93(!trans->t_owner || try_module_get(trans->t_owner))) {94ret = trans;95break;96}97}98up_read(&rds_trans_sem);99100return ret;101}102103/*104* This returns the number of stats entries in the snapshot and only105* copies them using the iter if there is enough space for them. The106* caller passes in the global stats so that we can size and copy while107* holding the lock.108*/109unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter,110unsigned int avail)111112{113struct rds_transport *trans;114unsigned int total = 0;115unsigned int part;116int i;117118rds_info_iter_unmap(iter);119down_read(&rds_trans_sem);120121for (i = 0; i < RDS_TRANS_COUNT; i++)122{123trans = transports[i];124if (!trans || !trans->stats_info_copy)125continue;126127part = trans->stats_info_copy(iter, avail);128avail -= min(avail, part);129total += part;130}131132up_read(&rds_trans_sem);133134return total;135}136137138139