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,
	monis-smomgflXvOZWk0Htik3J/w@public.gmane.org,
	ogerlitz-smomgflXvOZWk0Htik3J/w@public.gmane.org,
	nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org
Subject: [PATCH V2 1/5] IB Netlink Infrastructure
Date: Mon, 29 Nov 2010 18:16:35 +0200	[thread overview]
Message-ID: <1291047399-430-2-git-send-email-nirm@voltaire.com> (raw)
In-Reply-To: <1291047399-430-1-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>

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

Signed-off-by: Nir Muchtar <nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
---
 drivers/infiniband/core/netlink.c |  202 +++++++++++++++++++++++++++++++++++++
 include/linux/netlink.h           |    1 +
 include/rdma/ib_netlink.h         |   60 +++++++++++
 3 files changed, 263 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..4c53666
--- /dev/null
+++ b/drivers/infiniband/core/netlink.c
@@ -0,0 +1,202 @@
+/*
+ * 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 <rdma/ib_netlink.h>
+
+struct ibnl_cb {
+	struct list_head list;
+	int module;
+	int (*get_data)(int op, struct sk_buff **nl_skb, int pid);
+};
+
+static DEFINE_MUTEX(ibnl_mutex);
+static struct sock *nls;
+static LIST_HEAD(cb_list);
+
+int ibnl_add_cb(int module,
+		int (*get_data)(int op, struct sk_buff **skb, int pid))
+{
+	struct ibnl_cb *cur;
+	struct ibnl_cb *nl_cb = kmalloc(sizeof *nl_cb, GFP_KERNEL);
+
+	if (!nl_cb)
+		return -ENOMEM;
+	nl_cb->module = module;
+	nl_cb->get_data = get_data;
+	mutex_lock(&ibnl_mutex);
+	list_for_each_entry(cur, &cb_list, list) {
+		if (cur->module == module) {
+			pr_warn("Callback for %d already exists\n", module);
+			mutex_unlock(&ibnl_mutex);
+			kfree(nl_cb);
+			return -EINVAL;
+		}
+	}
+	list_add_tail(&nl_cb->list, &cb_list);
+	mutex_unlock(&ibnl_mutex);
+	return 0;
+}
+EXPORT_SYMBOL(ibnl_add_cb);
+
+int ibnl_remove_cb(int module)
+{
+	struct ibnl_cb *cur, *next;
+
+	mutex_lock(&ibnl_mutex);
+	list_for_each_entry_safe(cur, next, &cb_list, list) {
+		if (cur->module == module) {
+			list_del(&(cur->list));
+			mutex_unlock(&ibnl_mutex);
+			kfree(cur);
+			return 0;
+		}
+	}
+	pr_warn("Can't remove callback for module %d. Not found\n", module);
+	mutex_unlock(&ibnl_mutex);
+	return -EINVAL;
+}
+EXPORT_SYMBOL(ibnl_remove_cb);
+
+static void ibnl_unicast(int pid, struct sk_buff *skb)
+{
+	netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
+}
+
+static int ibnl_handle_request(int pid, int op, struct ibnl_cb *cb)
+{
+	int ret;
+	struct sk_buff **nl_skb;
+
+	nl_skb = kmalloc(sizeof *nl_skb, GFP_KERNEL);
+	if (!nl_skb) {
+		pr_info("Couldn't allocate skb\n");
+		ret = -ENOMEM;
+		goto err;
+	}
+	*nl_skb = NULL;
+	ret = cb->get_data(op, nl_skb, pid);
+	if (ret) {
+		if (ret != -EMSGSIZE)
+			goto err;
+	}
+	if (*nl_skb) {
+		ibnl_put(nl_skb, pid, 0, 0, 0, NLMSG_DONE);
+		ibnl_unicast(pid, *nl_skb);
+	}
+	kfree(nl_skb);
+	return 0;
+
+err:
+	kfree(nl_skb);
+	return ret;
+}
+
+void *ibnl_put(struct sk_buff **skb, int pid, int seq,
+	       int len, int module, int op)
+{
+	unsigned char *prev_tail;
+	struct nlmsghdr *nlh;
+
+	if (*skb && skb_tailroom(*skb) < (int)NLMSG_SPACE(len)) {
+		ibnl_unicast(pid, *skb);
+		*skb = NULL;
+	}
+	if (!*skb) {
+		*skb = alloc_skb(NLMSG_SPACE(NLMSG_GOODSIZE), GFP_KERNEL);
+		if (!*skb) {
+			pr_info("Couldn't allocate skb\n");
+			return NULL;
+		}
+	}
+	prev_tail = skb_tail_pointer(*skb);
+	nlh = NLMSG_NEW(*skb, 0, seq, IBNL_GET_TYPE(module, 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);
+
+static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
+{
+	struct ibnl_cb *cb;
+	int type = nlh->nlmsg_type;
+	int pid = nlh->nlmsg_pid;
+	int module = IBNL_GET_MODULE(type);
+
+	list_for_each_entry(cb, &cb_list, list) {
+		if (cb->module == module)
+			return ibnl_handle_request(pid, IBNL_GET_OP(type), cb);
+	}
+	pr_info("Callback for module %d not found\n", module);
+	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_cb *cur, *next;
+
+	mutex_lock(&ibnl_mutex);
+	list_for_each_entry_safe(cur, next, &cb_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..ebe564d
--- /dev/null
+++ b/include/rdma/ib_netlink.h
@@ -0,0 +1,60 @@
+#ifndef _IBNETLINK_H
+#define _IBNETLINK_H
+
+#include <linux/rtnetlink.h>
+#include <rdma/rdma_cm.h>
+
+enum {
+	IBNL_RDMA_CM = 1
+};
+
+enum {
+	IBNL_RDMA_CM_STATS = 0,
+	IBNL_RDMA_CM_DEVICE_NAME,
+	IBNL_RDMA_CM_ID_STATS,
+};
+
+#define IBNL_GET_MODULE(type) ((type & (((1 << 6) - 1) << 10)) >> 10)
+#define IBNL_GET_OP(type) (type & ((1 << 10) - 1))
+#define IBNL_GET_TYPE(module, op) ((module << 10) + op)
+
+#ifdef __KERNEL__
+
+int ibnl_init(void);
+void ibnl_cleanup(void);
+
+/**
+ * Add a callback for an IB module to the IB netlink module.
+ * @module: The added IB module
+ * @get_size: A callback for obtaining the necessary size for the returned data
+ * @get_data: A callback for obtaining the data
+ *
+ * Returns 0 on success or a negative error code.
+ */
+int ibnl_add_cb(int module,
+		int (*get_data)(int op, struct sk_buff **skb, int pid));
+
+/**
+ * Remove a callback for a registered IB module
+ * @module: The removed IB module
+ *
+ * Returns 0 on success or a negative error code.
+ */
+int ibnl_remove_cb(int module);
+
+/**
+ * Put a new message in a supplied skb.
+ * @skb: The netlink skb.
+ * @pid: The destination pid
+ * @seq: The message sequence number.
+ * @len: The requested message length to allocate.
+ * @module: Calling IB netlink module.
+ * @op: message content op.
+ * Returns the allocated buffer on success and NULL on failure.
+ */
+void *ibnl_put(struct sk_buff **skb, int pid, int seq,
+	       int len, int module, int op);
+
+#endif /* __KERNEL__ */
+
+#endif /* _IBNETLINK_H */
-- 
1.7.0.4

--
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:[~2010-11-29 16:16 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-29 16:16 [PATCH V2 0/5] IB Netlink Interface and RDMA CM exports Nir Muchtar
     [not found] ` <1291047399-430-1-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2010-11-29 16:16   ` Nir Muchtar [this message]
     [not found]     ` <1291047399-430-2-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2010-11-29 18:21       ` [PATCH V2 1/5] IB Netlink Infrastructure Jason Gunthorpe
     [not found]         ` <20101129182159.GB16788-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-11-30 12:56           ` Nir Muchtar
2010-11-30 17:51             ` Jason Gunthorpe
     [not found]               ` <20101130175152.GH16788-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-12-01 16:05                 ` Nir Muchtar
2010-11-29 16:16   ` [PATCH V2 2/5] IB Core: Error Handler Nir Muchtar
2010-11-29 16:16   ` [PATCH V2 3/5] IB Core: Run Netlink Nir Muchtar
2010-11-29 16:16   ` [PATCH V2 4/5] RDMA CM: Export State Enum Nir Muchtar
2010-11-29 16:16   ` [PATCH V2 5/5] RDMA CM: Netlink Client Nir Muchtar
     [not found]     ` <1291047399-430-6-git-send-email-nirm-smomgflXvOZWk0Htik3J/w@public.gmane.org>
2010-11-29 19:11       ` Jason Gunthorpe
     [not found]         ` <20101129191136.GC16788-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-11-30 12:34           ` Or Gerlitz
     [not found]             ` <4CF4EF73.6060406-hKgKHo2Ms0FWk0Htik3J/w@public.gmane.org>
2010-11-30 17:50               ` Jason Gunthorpe
2010-11-30 14:09           ` Nir Muchtar
2010-11-30 18:19             ` Jason Gunthorpe
     [not found]               ` <20101130181944.GI16788-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-12-01 15:58                 ` Nir Muchtar
2010-12-01 18:35                   ` Jason Gunthorpe
     [not found]                     ` <20101201183538.GQ16788-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-12-07 15:40                       ` Nir Muchtar
2010-12-07 18:54                         ` Jason Gunthorpe
2010-12-07 20:53                           ` Nir Muchtar
     [not found]                             ` <7E95F01E94AB484F83061FCFA35B39F8794E3B-QfUkFaTmzUSUvQqKE/ONIwC/G2K4zDHf@public.gmane.org>
2010-12-07 21:29                               ` Jason Gunthorpe
     [not found]                                 ` <20101207212924.GG16788-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-12-08 14:55                                   ` Nir Muchtar
2010-12-08 18:23                                     ` Jason Gunthorpe
     [not found]                                       ` <20101208182356.GK16788-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-12-09  8:47                                         ` Nir Muchtar
2010-12-09 17:26                                           ` Jason Gunthorpe
2010-11-30 16:13       ` Hefty, Sean
     [not found]         ` <CF9C39F99A89134C9CF9C4CCB68B8DDF25B8924212-osO9UTpF0USkrb+BlOpmy7fspsVTdybXVpNB7YpNyf8@public.gmane.org>
2010-11-30 19:01           ` Jason Gunthorpe

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=1291047399-430-2-git-send-email-nirm@voltaire.com \
    --to=nirm-smomgflxvozwk0htik3j/w@public.gmane.org \
    --cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=monis-smomgflXvOZWk0Htik3J/w@public.gmane.org \
    --cc=ogerlitz-smomgflXvOZWk0Htik3J/w@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