4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/miscdevice.h>
35 #include <lnet/lib-lnet.h>
36 #include <uapi/linux/lnet/lnet-dlc.h>
38 static int config_on_load = 0;
39 module_param(config_on_load, int, 0444);
40 MODULE_PARM_DESC(config_on_load, "configure network at module load");
42 static DEFINE_MUTEX(lnet_config_mutex);
44 static int lnet_configure(void *arg)
46 /* 'arg' only there so I can be passed to cfs_create_thread() */
49 mutex_lock(&lnet_config_mutex);
51 if (!the_lnet.ln_niinit_self) {
52 rc = try_module_get(THIS_MODULE);
57 rc = LNetNIInit(LNET_PID_LUSTRE);
59 the_lnet.ln_niinit_self = 1;
62 module_put(THIS_MODULE);
67 mutex_unlock(&lnet_config_mutex);
71 static int lnet_unconfigure(void)
75 mutex_lock(&lnet_config_mutex);
77 if (the_lnet.ln_niinit_self) {
78 the_lnet.ln_niinit_self = 0;
80 module_put(THIS_MODULE);
83 mutex_lock(&the_lnet.ln_api_mutex);
84 refcount = the_lnet.ln_refcount;
85 mutex_unlock(&the_lnet.ln_api_mutex);
87 mutex_unlock(&lnet_config_mutex);
89 return (refcount == 0) ? 0 : -EBUSY;
93 lnet_dyn_configure_net(struct libcfs_ioctl_hdr *hdr)
95 struct lnet_ioctl_config_data *conf =
96 (struct lnet_ioctl_config_data *)hdr;
99 if (conf->cfg_hdr.ioc_len < sizeof(*conf))
102 mutex_lock(&lnet_config_mutex);
103 if (the_lnet.ln_niinit_self)
104 rc = lnet_dyn_add_net(conf);
107 mutex_unlock(&lnet_config_mutex);
113 lnet_dyn_unconfigure_net(struct libcfs_ioctl_hdr *hdr)
115 struct lnet_ioctl_config_data *conf =
116 (struct lnet_ioctl_config_data *) hdr;
119 if (conf->cfg_hdr.ioc_len < sizeof(*conf))
122 mutex_lock(&lnet_config_mutex);
123 if (the_lnet.ln_niinit_self)
124 rc = lnet_dyn_del_net(conf->cfg_net);
127 mutex_unlock(&lnet_config_mutex);
133 lnet_dyn_configure_ni(struct libcfs_ioctl_hdr *hdr)
135 struct lnet_ioctl_config_ni *conf =
136 (struct lnet_ioctl_config_ni *)hdr;
139 if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf))
142 mutex_lock(&lnet_config_mutex);
143 if (the_lnet.ln_niinit_self) {
144 struct lnet_ioctl_config_lnd_tunables *tun = NULL;
148 /* get the tunables if they are available */
149 if (conf->lic_cfg_hdr.ioc_len >=
150 sizeof(*conf) + sizeof(*tun))
151 tun = (struct lnet_ioctl_config_lnd_tunables *) conf->lic_bulk;
153 lnet_nid4_to_nid(conf->lic_nid, &nid);
154 net_id = LNET_NID_NET(&nid);
155 rc = lnet_dyn_add_ni(conf, net_id, tun);
157 mutex_unlock(&lnet_config_mutex);
163 lnet_dyn_unconfigure_ni(struct libcfs_ioctl_hdr *hdr)
165 struct lnet_ioctl_config_ni *conf =
166 (struct lnet_ioctl_config_ni *) hdr;
170 if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf) ||
171 !the_lnet.ln_niinit_self)
174 lnet_nid4_to_nid(conf->lic_nid, &nid);
175 mutex_lock(&lnet_config_mutex);
176 if (the_lnet.ln_niinit_self)
177 rc = lnet_dyn_del_ni(&nid);
180 mutex_unlock(&lnet_config_mutex);
186 lnet_ioctl(unsigned int cmd, struct libcfs_ioctl_hdr *hdr)
191 case IOC_LIBCFS_CONFIGURE: {
192 struct libcfs_ioctl_data *data =
193 (struct libcfs_ioctl_data *)hdr;
195 if (data->ioc_hdr.ioc_len < sizeof(*data)) {
198 the_lnet.ln_nis_from_mod_params = data->ioc_flags;
199 rc = lnet_configure(NULL);
204 case IOC_LIBCFS_UNCONFIGURE:
205 rc = lnet_unconfigure();
208 case IOC_LIBCFS_ADD_NET:
209 rc = lnet_dyn_configure_net(hdr);
212 case IOC_LIBCFS_DEL_NET:
213 rc = lnet_dyn_unconfigure_net(hdr);
216 case IOC_LIBCFS_ADD_LOCAL_NI:
217 rc = lnet_dyn_configure_ni(hdr);
220 case IOC_LIBCFS_DEL_LOCAL_NI:
221 rc = lnet_dyn_unconfigure_ni(hdr);
225 /* Passing LNET_PID_ANY only gives me a ref if the net is up
226 * already; I'll need it to ensure the net can't go down while
227 * I'm called into it */
228 rc = LNetNIInit(LNET_PID_ANY);
230 rc = LNetCtl(cmd, hdr);
237 BLOCKING_NOTIFIER_HEAD(lnet_ioctl_list);
238 EXPORT_SYMBOL(lnet_ioctl_list);
240 static inline size_t lnet_ioctl_packlen(struct libcfs_ioctl_data *data)
242 size_t len = sizeof(*data);
244 len += (data->ioc_inllen1 + 7) & ~7;
245 len += (data->ioc_inllen2 + 7) & ~7;
249 static bool lnet_ioctl_is_invalid(struct libcfs_ioctl_data *data)
251 const int maxlen = 1 << 30;
253 if (data->ioc_hdr.ioc_len > maxlen)
256 if (data->ioc_inllen1 > maxlen)
259 if (data->ioc_inllen2 > maxlen)
262 if (data->ioc_inlbuf1 && !data->ioc_inllen1)
265 if (data->ioc_inlbuf2 && !data->ioc_inllen2)
268 if (data->ioc_pbuf1 && !data->ioc_plen1)
271 if (data->ioc_pbuf2 && !data->ioc_plen2)
274 if (data->ioc_plen1 && !data->ioc_pbuf1)
277 if (data->ioc_plen2 && !data->ioc_pbuf2)
280 if (lnet_ioctl_packlen(data) != data->ioc_hdr.ioc_len)
283 if (data->ioc_inllen1 &&
284 data->ioc_bulk[((data->ioc_inllen1 + 7) & ~7) +
285 data->ioc_inllen2 - 1] != '\0')
291 static int lnet_ioctl_data_adjust(struct libcfs_ioctl_data *data)
295 if (lnet_ioctl_is_invalid(data)) {
296 CERROR("lnet ioctl: parameter not correctly formatted\n");
300 if (data->ioc_inllen1 != 0)
301 data->ioc_inlbuf1 = &data->ioc_bulk[0];
303 if (data->ioc_inllen2 != 0)
304 data->ioc_inlbuf2 = (&data->ioc_bulk[0] +
305 round_up(data->ioc_inllen1, 8));
310 static int lnet_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
311 struct libcfs_ioctl_hdr __user *uhdr)
313 struct libcfs_ioctl_hdr hdr;
317 if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
320 if (hdr.ioc_version != LNET_IOCTL_VERSION &&
321 hdr.ioc_version != LNET_IOCTL_VERSION2) {
322 CERROR("lnet ioctl: version mismatch expected %#x, got %#x\n",
323 LNET_IOCTL_VERSION, hdr.ioc_version);
327 if (hdr.ioc_len < sizeof(struct libcfs_ioctl_hdr)) {
328 CERROR("lnet ioctl: user buffer too small for ioctl\n");
332 if (hdr.ioc_len > LIBCFS_IOC_DATA_MAX) {
333 CERROR("lnet ioctl: user buffer is too large %d/%d\n",
334 hdr.ioc_len, LIBCFS_IOC_DATA_MAX);
338 LIBCFS_ALLOC(*hdr_pp, hdr.ioc_len);
342 if (copy_from_user(*hdr_pp, uhdr, hdr.ioc_len))
343 GOTO(free, err = -EFAULT);
345 if ((*hdr_pp)->ioc_version != hdr.ioc_version ||
346 (*hdr_pp)->ioc_len != hdr.ioc_len) {
347 GOTO(free, err = -EINVAL);
353 LIBCFS_FREE(*hdr_pp, hdr.ioc_len);
358 lnet_psdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
360 struct libcfs_ioctl_data *data = NULL;
361 struct libcfs_ioctl_hdr *hdr;
363 void __user *uparam = (void __user *)arg;
366 if (!capable(CAP_SYS_ADMIN))
369 if (_IOC_TYPE(cmd) != IOC_LIBCFS_TYPE ||
370 _IOC_NR(cmd) < IOC_LIBCFS_MIN_NR ||
371 _IOC_NR(cmd) > IOC_LIBCFS_MAX_NR) {
372 CDEBUG(D_IOCTL, "invalid ioctl ( type %d, nr %d, size %d )\n",
373 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
377 /* 'cmd' and permissions get checked in our arch-specific caller */
378 err = lnet_ioctl_getdata(&hdr, uparam);
380 CDEBUG_LIMIT(D_ERROR,
381 "lnet ioctl: data header error %d\n", err);
385 if (hdr->ioc_version == LNET_IOCTL_VERSION) {
386 /* The lnet_ioctl_data_adjust() function performs adjustment
387 * operations on the libcfs_ioctl_data structure to make
388 * it usable by the code. This doesn't need to be called
389 * for new data structures added.
391 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
392 err = lnet_ioctl_data_adjust(data);
397 CDEBUG(D_IOCTL, "lnet ioctl cmd %u\n", cmd);
399 err = libcfs_ioctl(cmd, data);
401 err = lnet_ioctl(cmd, hdr);
402 if (err == -EINVAL) {
403 err = blocking_notifier_call_chain(&lnet_ioctl_list,
405 if (!(err & NOTIFY_STOP_MASK))
406 /* No-one claimed the ioctl */
409 err = notifier_to_errno(err);
411 if (copy_to_user(uparam, hdr, hdr->ioc_len) && !err)
414 LIBCFS_FREE(hdr, hdr->ioc_len);
418 static const struct file_operations lnet_fops = {
419 .owner = THIS_MODULE,
420 .unlocked_ioctl = lnet_psdev_ioctl,
423 static struct miscdevice lnet_dev = {
424 .minor = MISC_DYNAMIC_MINOR,
429 static int __init lnet_init(void)
438 rc = lnet_lib_init();
440 CERROR("lnet_lib_init: rc = %d\n", rc);
444 rc = misc_register(&lnet_dev);
446 CERROR("misc_register: rc = %d\n", rc);
450 if (live_router_check_interval != INT_MIN ||
451 dead_router_check_interval != INT_MIN)
452 LCONSOLE_WARN("live_router_check_interval and dead_router_check_interval have been deprecated. Use alive_router_check_interval instead. Ignoring these deprecated parameters.\n");
454 if (config_on_load) {
455 /* Have to schedule a separate thread to avoid deadlocking
457 (void)kthread_run(lnet_configure, NULL, "lnet_initd");
463 static void __exit lnet_exit(void)
465 misc_deregister(&lnet_dev);
471 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
472 MODULE_DESCRIPTION("Lustre Networking layer");
473 MODULE_VERSION(LNET_VERSION);
474 MODULE_LICENSE("GPL");
476 module_init(lnet_init);
477 module_exit(lnet_exit);