Whamcloud - gitweb
b53a7128b21a51aa49ed5ea701715049e68a4878
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-kernelcomm.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Author: Nathan Rutman <nathan.rutman@sun.com>
37  *
38  * Kernel <-> userspace communication routines.  We'll use a shorthand term
39  * "lnl" (Lustre NetLink) for the interface names for all arches (even though
40  * implemtation may not use NetLink).
41  * For Linux, we use Netlink sockets.
42  */
43
44 #define DEBUG_SUBSYSTEM S_CLASS
45
46
47 /* This is the kernel side.
48  * See libcfs/ulinux/ulinux-kernelcomm.c for the user side.
49  */
50
51 #if defined(HAVE_NETLINK) && defined(__KERNEL__)
52
53 #include <linux/module.h>
54 #include <linux/socket.h>
55 #include <linux/skbuff.h>
56 #include <net/netlink.h>
57
58 #include <libcfs/libcfs.h>
59
60 /* OFED backport #defines netlink_kernel_create with 6 args.
61    I haven't a clue why that header file gets included here,
62    but we must undo its mischief. */
63 #ifdef BACKPORT_LINUX_NETLINK_H
64 #undef netlink_kernel_create
65 #endif
66
67
68 /* Single Netlink Message type to send all Lustre messages */
69 #define LNL_MSG 26
70
71 static struct sock *lnl_socket = NULL;
72 static atomic_t lnl_start_count = ATOMIC_INIT(0);
73 static spinlock_t lnl_lock = SPIN_LOCK_UNLOCKED;
74
75 /** Start the netlink socket for this transport
76  * @param transport lnl_transport
77  */
78 int libcfs_klnl_start(int transport)
79 {
80         int rc = 0;
81         ENTRY;
82
83         /* If anyone needs it, we can add per-transport incoming message
84            callbacks.  Add the callback as a param here.  Store the transport
85            and callback in a table. Include a generalized incoming msg
86            callback here to dispatch messages to the appropriate
87            per-transport callback. */
88
89         spin_lock(&lnl_lock);
90         if (atomic_inc_return(&lnl_start_count) > 1)
91                 GOTO(out, rc = 0);
92
93         lnl_socket = netlink_kernel_create(LNL_SOCKET, LNL_GRP_CNT,
94                                            NULL /* incoming cb */,
95                                            THIS_MODULE);
96         if (lnl_socket == NULL) {
97                 CERROR("Cannot open socket %d\n", LNL_SOCKET);
98                 atomic_dec(&lnl_start_count);
99                 GOTO(out, rc = -ENODEV);
100         }
101
102 out:
103         spin_unlock(&lnl_lock);
104         RETURN(rc);
105 }
106 EXPORT_SYMBOL(libcfs_klnl_start);
107
108 static void send_shutdown_msg(int transport, int group) {
109         struct lnl_hdr lh;
110
111         lh.lnl_magic = LNL_MAGIC;
112         lh.lnl_transport = LNL_TRANSPORT_GENERIC;
113         lh.lnl_msgtype = LNL_MSG_SHUTDOWN;
114         lh.lnl_msglen = sizeof(lh);
115
116         libcfs_klnl_msg_put(0, group, &lh);
117 }
118
119 /* This should be called once per (started) transport
120  * @param transport lnl_transport
121  * @param group Broadcast group for shutdown message */
122 int libcfs_klnl_stop(int transport, int group)
123 {
124         if (group)
125                 send_shutdown_msg(transport, group);
126
127         spin_lock(&lnl_lock);
128
129         if (atomic_dec_and_test(&lnl_start_count)) {
130                 sock_release(lnl_socket->sk_socket);
131                 lnl_socket = NULL;
132         }
133
134         spin_unlock(&lnl_lock);
135         return 0;
136 }
137 EXPORT_SYMBOL(libcfs_klnl_stop);
138
139 static struct sk_buff *netlink_make_msg(int pid, int seq, void *payload,
140                                         int size)
141 {
142         struct sk_buff  *skb;
143         struct nlmsghdr *nlh;
144         int             len = NLMSG_SPACE(size);
145         void            *data;
146
147 #ifdef HAVE_NETLINK_NL2
148         skb = nlmsg_new(len, GFP_KERNEL);
149 #else   /* old */
150         skb = nlmsg_new(len);
151 #endif
152
153         if (!skb)
154                 return NULL;
155
156         nlh = nlmsg_put(skb, pid, seq, LNL_MSG, size, 0);
157         if (!nlh) {
158                 nlmsg_free(skb);
159                 return NULL;
160         }
161
162         data = nlmsg_data(nlh);
163         memcpy(data, payload, size);
164         return skb;
165 }
166
167 /**
168  * libcfs_klnl_msg_put - send an message from kernel to userspace
169  * @param pid Process id to send message to for unicast messages; must be 0 for
170  *   broadcast
171  * @param group Broadcast group; 0 for unicast messages
172  * @param payload Payload data.  First field of payload is always struct lnl_hdr
173  *
174  * Allocates an skb, builds the netlink message, and sends it to the pid.
175  */
176 int libcfs_klnl_msg_put(int pid, int group, void *payload)
177 {
178         struct lnl_hdr *lnlh = (struct lnl_hdr *)payload;
179         struct sk_buff  *skb;
180         int rc;
181
182         if (lnl_socket == NULL) {
183                 CERROR("LustreNetLink: not running\n");
184                 return -ENOSYS;
185         }
186
187         if (lnlh->lnl_magic != LNL_MAGIC) {
188                 CERROR("LustreNetLink: bad magic %x\n", lnlh->lnl_magic);
189                 return -ENOSYS;
190         }
191
192         if ((pid != 0) && (group != 0)) {
193                 CERROR("LustreNetLink: pid=%d or group=%d must be 0\n",
194                        pid, group);
195                 return -EINVAL;
196         }
197
198         skb = netlink_make_msg(pid, 0, payload, lnlh->lnl_msglen);
199         if (!skb)
200                 return -ENOMEM;
201
202         if (pid) {
203                 rc = netlink_unicast(lnl_socket, skb, pid,
204                              lnlh->lnl_flags & LNL_FL_BLOCK ? 0 : MSG_DONTWAIT);
205                 if (rc > 0)
206                         rc = 0;
207         } else {
208                 rc = nlmsg_multicast(lnl_socket, skb, 0, group);
209         }
210
211         CDEBUG(0, "Sent message pid=%d, group=%d, rc=%d\n", pid, group, rc);
212
213         if (rc < 0)
214                 CWARN("message send failed (%d) [pid=%d,group=%d]\n", rc,
215                       pid, group);
216
217         return rc;
218 }
219 EXPORT_SYMBOL(libcfs_klnl_msg_put);
220
221
222 #endif
223