public inbox for linux-rdma@vger.kernel.org
 help / color / mirror / Atom feed
From: Nir Muchtar <nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
To: rolandd-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org
Cc: linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Nir Muchtar <nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
Subject: [PATCH V4 1/6] IB Netlink Infrastructure
Date: Mon, 3 Jan 2011 17:33:49 +0200	[thread overview]
Message-ID: <1294068834-17778-2-git-send-email-nirm@voltaire.com> (raw)
In-Reply-To: <1294068834-17778-1-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>

The basic IB netlink infrastructure.
It allows for registration of IB clients for which data is to be exported.
It supplies message construction callbacks.

Signed-off-by: Nir Muchtar <nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
---
 drivers/infiniband/core/netlink.c |  178 +++++++++++++++++++++++++++++++++++++
 include/linux/netlink.h           |    1 +
 include/rdma/ib_netlink.h         |   62 +++++++++++++
 3 files changed, 241 insertions(+), 0 deletions(-)
 create mode 100644 drivers/infiniband/core/netlink.c
 create mode 100644 include/rdma/ib_netlink.h

diff --git a/drivers/infiniband/core/netlink.c b/drivers/infiniband/core/netlink.c
new file mode 100644
index 0000000..66cbd45
--- /dev/null
+++ b/drivers/infiniband/core/netlink.c
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2010 Voltaire Inc.  All rights reserved.
+ *
+ * This software is available to you under a choice of one of two
+ * licenses.  You may choose to be licensed under the terms of the GNU
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
+
+#include <linux/netlink.h>
+
+#include <net/netlink.h>
+#include <net/net_namespace.h>
+#include <net/sock.h>
+#include <rdma/ib_netlink.h>
+
+struct ibnl_client {
+	struct list_head list;
+	int index;
+	int nops;
+	const struct ibnl_client_cbs *cb_table;
+};
+
+static DEFINE_MUTEX(ibnl_mutex);
+static struct sock *nls;
+static LIST_HEAD(client_list);
+
+int ibnl_add_client(int index, int nops,
+		    const struct ibnl_client_cbs cb_table[])
+{
+	struct ibnl_client *cur;
+	struct ibnl_client *nl_client = kmalloc(sizeof *nl_client, GFP_ATOMIC);
+
+	if (!nl_client)
+		return -ENOMEM;
+	nl_client->index = index;
+	nl_client->nops = nops;
+	nl_client->cb_table = cb_table;
+	mutex_lock(&ibnl_mutex);
+	list_for_each_entry(cur, &client_list, list) {
+		if (cur->index == index) {
+			pr_warn("Client for %d already exists\n", index);
+			mutex_unlock(&ibnl_mutex);
+			kfree(nl_client);
+			return -EINVAL;
+		}
+	}
+	list_add_tail(&nl_client->list, &client_list);
+	mutex_unlock(&ibnl_mutex);
+	return 0;
+}
+EXPORT_SYMBOL(ibnl_add_client);
+
+int ibnl_remove_client(int index)
+{
+	struct ibnl_client *cur, *next;
+
+	mutex_lock(&ibnl_mutex);
+	list_for_each_entry_safe(cur, next, &client_list, list) {
+		if (cur->index == index) {
+			list_del(&(cur->list));
+			mutex_unlock(&ibnl_mutex);
+			kfree(cur);
+			return 0;
+		}
+	}
+	pr_warn("Can't remove callback for client idx %d. Not found\n", index);
+	mutex_unlock(&ibnl_mutex);
+	return -EINVAL;
+}
+EXPORT_SYMBOL(ibnl_remove_client);
+
+void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
+		   int len, int client, int op)
+{
+	unsigned char *prev_tail;
+
+	prev_tail = skb_tail_pointer(skb);
+	*nlh = NLMSG_NEW(skb, 0, seq, IBNL_GET_TYPE(client, op),
+			len, NLM_F_MULTI);
+	(*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;
+	return NLMSG_DATA(*nlh);
+nlmsg_failure:
+	nlmsg_trim(skb, prev_tail);
+	return NULL;
+}
+EXPORT_SYMBOL(ibnl_put_msg);
+
+int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
+		  int len, void *data, int type)
+{
+	unsigned char *prev_tail;
+
+	prev_tail = skb_tail_pointer(skb);
+	NLA_PUT(skb, type, len, data);
+	nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail;
+	return 0;
+nla_put_failure:
+	nlmsg_trim(skb, prev_tail - nlh->nlmsg_len);
+	return -EMSGSIZE;
+}
+EXPORT_SYMBOL(ibnl_put_attr);
+
+static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
+{
+	struct ibnl_client *client;
+	int type = nlh->nlmsg_type;
+	int index = IBNL_GET_CLIENT(type);
+	int op = IBNL_GET_OP(type);
+	list_for_each_entry(client, &client_list, list) {
+		if (client->index == index) {
+			if (op < 0 || op >= client->nops ||
+			    !client->cb_table[IBNL_GET_OP(op)].dump)
+				return -EINVAL;
+			return netlink_dump_start(nls, skb, nlh,
+						  client->cb_table[op].dump,
+						  NULL);
+		}
+	}
+	pr_info("Index %d wasn't found in client list\n", index);
+	return -EINVAL;
+}
+
+static void ibnl_rcv(struct sk_buff *skb)
+{
+	mutex_lock(&ibnl_mutex);
+	netlink_rcv_skb(skb, &ibnl_rcv_msg);
+	mutex_unlock(&ibnl_mutex);
+}
+
+int ibnl_init(void)
+{
+	nls = netlink_kernel_create(&init_net, NETLINK_INFINIBAND, 0, ibnl_rcv,
+				    NULL, THIS_MODULE);
+	if (!nls) {
+		pr_warn("Failed to create netlink socket\n");
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+void ibnl_cleanup(void)
+{
+	struct ibnl_client *cur, *next;
+
+	mutex_lock(&ibnl_mutex);
+	list_for_each_entry_safe(cur, next, &client_list, list) {
+		list_del(&(cur->list));
+		kfree(cur);
+	}
+	mutex_unlock(&ibnl_mutex);
+	netlink_kernel_release(nls);
+}
diff --git a/include/linux/netlink.h b/include/linux/netlink.h
index 1235669..c9693f9 100644
--- a/include/linux/netlink.h
+++ b/include/linux/netlink.h
@@ -24,6 +24,7 @@
 /* leave room for NETLINK_DM (DM Events) */
 #define NETLINK_SCSITRANSPORT	18	/* SCSI Transports */
 #define NETLINK_ECRYPTFS	19
+#define NETLINK_INFINIBAND	20
 
 #define MAX_LINKS 32		
 
diff --git a/include/rdma/ib_netlink.h b/include/rdma/ib_netlink.h
new file mode 100644
index 0000000..1ef76d0
--- /dev/null
+++ b/include/rdma/ib_netlink.h
@@ -0,0 +1,62 @@
+#ifndef _IBNETLINK_H
+#define _IBNETLINK_H
+
+#define IBNL_GET_CLIENT(type) ((type & (((1 << 6) - 1) << 10)) >> 10)
+#define IBNL_GET_OP(type) (type & ((1 << 10) - 1))
+#define IBNL_GET_TYPE(client, op) ((client << 10) + op)
+
+#ifdef __KERNEL__
+
+struct ibnl_client_cbs {
+	int (*dump)(struct sk_buff *skb, struct netlink_callback *nlcb);
+};
+
+int ibnl_init(void);
+void ibnl_cleanup(void);
+
+/**
+ * Add a a client to the list of IB netlink exporters.
+ * @index: Index of the added client
+ * @nops: Number of supported ops by the added client.
+ * @cb_table: A table for op->callback
+ *
+ * Returns 0 on success or a negative error code.
+ */
+int ibnl_add_client(int index, int nops,
+		    const struct ibnl_client_cbs cb_table[]);
+
+/**
+ * Remove a client from IB netlink.
+ * @index: Index of the removed IB client.
+ *
+ * Returns 0 on success or a negative error code.
+ */
+int ibnl_remove_client(int index);
+
+/**
+ * Put a new message in a supplied skb.
+ * @skb: The netlink skb.
+ * @nlh: Pointer to put the header of the new netlink message.
+ * @seq: The message sequence number.
+ * @len: The requested message length to allocate.
+ * @client: Calling IB netlink client.
+ * @op: message content op.
+ * Returns the allocated buffer on success and NULL on failure.
+ */
+void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
+		   int len, int client, int op);
+/**
+ * Put a new attribute in a supplied skb.
+ * @skb: The netlink skb.
+ * @nlh: Header of the netlink message to append the attribute to.
+ * @len: The length of the attribute data.
+ * @data: The attribute data to put.
+ * @type: The attribute type.
+ * Returns the 0 and a negative error code on failure.
+ */
+int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
+		  int len, void *data, int type);
+
+#endif /* __KERNEL__ */
+
+#endif /* _IBNETLINK_H */
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2011-01-03 15:33 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-03 15:33 [PATCH V4 0/6] IB Netlink Interface and RDMA CM exports Nir Muchtar
     [not found] ` <1294068834-17778-1-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-01-03 15:33   ` Nir Muchtar [this message]
     [not found]     ` <1294068834-17778-2-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-01-12 22:54       ` [PATCH V4 1/6] IB Netlink Infrastructure Roland Dreier
     [not found]         ` <ada7he94tt1.fsf-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org>
2011-01-13 14:00           ` Nir Muchtar
2011-05-09 19:35       ` Roland Dreier
     [not found]         ` <BANLkTi=J6ZTFCz12XkL4-ZeUsuAKODRjOw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-05-12 14:51           ` Nir Muchtar
     [not found]             ` <B166D7363A9EE34182C2C57BB1F09F1501CC02A1-FXD4EHQYOQ+layAChLZOBkEOCMrvLtNR@public.gmane.org>
2011-05-12 15:55               ` Roland Dreier
2011-01-03 15:33   ` [PATCH V4 2/6] IB Core: Error Handler Nir Muchtar
2011-01-03 15:33   ` [PATCH V4 3/6] IB Core: Run Netlink Nir Muchtar
     [not found]     ` <1294068834-17778-4-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-05-10 19:28       ` Roland Dreier
     [not found]         ` <BANLkTim_zvRjOuTvNdC2fGWMfYptYrVTTA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-05-12 14:56           ` Nir Muchtar
2011-01-03 15:33   ` [PATCH V4 4/6] RDMA CM: Export State Enum Nir Muchtar
2011-01-03 15:33   ` [PATCH V4 5/6] RDMA CM: Netlink Client Nir Muchtar
     [not found]     ` <1294068834-17778-6-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-01-07 20:37       ` Jason Gunthorpe
     [not found]         ` <20110107203742.GH1211-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2011-01-07 22:18           ` Roland Dreier
     [not found]             ` <adamxnc9x33.fsf-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org>
2011-01-11 19:10               ` Jason Gunthorpe
     [not found]                 ` <20110111191059.GC19219-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2011-01-12 12:48                   ` Or Gerlitz
2011-01-17 10:20               ` Or Gerlitz
     [not found]                 ` <4D3417E1.9090903-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-01-17 21:59                   ` Roland Dreier
     [not found]                     ` <adaaaizyyx6.fsf-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org>
2011-01-17 22:11                       ` Jason Gunthorpe
     [not found]                         ` <20110117221141.GE995-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2011-01-17 22:14                           ` Roland Dreier
     [not found]                             ` <ada4o97yy7u.fsf-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org>
2011-01-17 22:33                               ` Jason Gunthorpe
2011-02-24 14:07                               ` Or Gerlitz
     [not found]                                 ` <4D666604.7000700-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-02-24 14:12                                   ` Or Gerlitz
     [not found]                                     ` <4D666744.6010907-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-02-27 16:49                                       ` Roland Dreier
     [not found]                                         ` <AANLkTik-uNbochFYz1VGLBGjyU69dp5=jG=1pkSNWsCv-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2011-02-28  7:13                                           ` Or Gerlitz
2011-02-28 10:49                                           ` Nir Muchtar
2011-01-18  7:26                       ` Or Gerlitz
2011-01-03 15:33   ` [PATCH V4 6/6] RDMA CM: Save Owning PID Nir Muchtar
     [not found]     ` <1294068834-17778-7-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2011-01-12 22:54       ` Roland Dreier
     [not found]         ` <ada39ox4ts7.fsf-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org>
2011-01-13 13:56           ` [PATCH V5 " Nir Muchtar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1294068834-17778-2-git-send-email-nirm@voltaire.com \
    --to=nirm-smomgflxvozwk0htik3j/w@public.gmane.org \
    --cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=rolandd-FYB4Gu1CFyUAvxtiuMwx3w@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox