Whamcloud - gitweb
LU-10391 socklnd: use interface index to track local addr
[fs/lustre-release.git] / lnet / lnet / module.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <lnet/lib-lnet.h>
36 #include <uapi/linux/lnet/lnet-dlc.h>
37
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");
41
42 static DEFINE_MUTEX(lnet_config_mutex);
43
44 static int
45 lnet_configure(void *arg)
46 {
47         /* 'arg' only there so I can be passed to cfs_create_thread() */
48         int    rc = 0;
49
50         mutex_lock(&lnet_config_mutex);
51
52         if (!the_lnet.ln_niinit_self) {
53                 rc = try_module_get(THIS_MODULE);
54
55                 if (rc != 1)
56                         goto out;
57
58                 rc = LNetNIInit(LNET_PID_LUSTRE);
59                 if (rc >= 0) {
60                         the_lnet.ln_niinit_self = 1;
61                         rc = 0;
62                 } else {
63                         module_put(THIS_MODULE);
64                 }
65         }
66
67 out:
68         mutex_unlock(&lnet_config_mutex);
69         return rc;
70 }
71
72 static int
73 lnet_unconfigure (void)
74 {
75         int   refcount;
76
77         mutex_lock(&lnet_config_mutex);
78
79         if (the_lnet.ln_niinit_self) {
80                 the_lnet.ln_niinit_self = 0;
81                 LNetNIFini();
82                 module_put(THIS_MODULE);
83         }
84
85         mutex_lock(&the_lnet.ln_api_mutex);
86         refcount = the_lnet.ln_refcount;
87         mutex_unlock(&the_lnet.ln_api_mutex);
88
89         mutex_unlock(&lnet_config_mutex);
90
91         return (refcount == 0) ? 0 : -EBUSY;
92 }
93
94 static int
95 lnet_dyn_configure_net(struct libcfs_ioctl_hdr *hdr)
96 {
97         struct lnet_ioctl_config_data *conf =
98           (struct lnet_ioctl_config_data *)hdr;
99         int                           rc;
100
101         if (conf->cfg_hdr.ioc_len < sizeof(*conf))
102                 return -EINVAL;
103
104         mutex_lock(&lnet_config_mutex);
105         if (the_lnet.ln_niinit_self)
106                 rc = lnet_dyn_add_net(conf);
107         else
108                 rc = -EINVAL;
109         mutex_unlock(&lnet_config_mutex);
110
111         return rc;
112 }
113
114 static int
115 lnet_dyn_unconfigure_net(struct libcfs_ioctl_hdr *hdr)
116 {
117         struct lnet_ioctl_config_data *conf =
118           (struct lnet_ioctl_config_data *) hdr;
119         int                           rc;
120
121         if (conf->cfg_hdr.ioc_len < sizeof(*conf))
122                 return -EINVAL;
123
124         mutex_lock(&lnet_config_mutex);
125         if (the_lnet.ln_niinit_self)
126                 rc = lnet_dyn_del_net(conf->cfg_net);
127         else
128                 rc = -EINVAL;
129         mutex_unlock(&lnet_config_mutex);
130
131         return rc;
132 }
133
134 static int
135 lnet_dyn_configure_ni(struct libcfs_ioctl_hdr *hdr)
136 {
137         struct lnet_ioctl_config_ni *conf =
138           (struct lnet_ioctl_config_ni *)hdr;
139         int                           rc;
140
141         if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf))
142                 return -EINVAL;
143
144         mutex_lock(&lnet_config_mutex);
145         if (the_lnet.ln_niinit_self)
146                 rc = lnet_dyn_add_ni(conf);
147         else
148                 rc = -EINVAL;
149         mutex_unlock(&lnet_config_mutex);
150
151         return rc;
152 }
153
154 static int
155 lnet_dyn_unconfigure_ni(struct libcfs_ioctl_hdr *hdr)
156 {
157         struct lnet_ioctl_config_ni *conf =
158           (struct lnet_ioctl_config_ni *) hdr;
159         int                           rc;
160
161         if (conf->lic_cfg_hdr.ioc_len < sizeof(*conf))
162                 return -EINVAL;
163
164         mutex_lock(&lnet_config_mutex);
165         if (the_lnet.ln_niinit_self)
166                 rc = lnet_dyn_del_ni(conf);
167         else
168                 rc = -EINVAL;
169         mutex_unlock(&lnet_config_mutex);
170
171         return rc;
172 }
173
174 static int
175 lnet_ioctl(struct notifier_block *nb,
176            unsigned long cmd, void *vdata)
177 {
178         struct libcfs_ioctl_hdr *hdr = vdata;
179         int rc;
180
181         switch (cmd) {
182         case IOC_LIBCFS_CONFIGURE: {
183                 struct libcfs_ioctl_data *data =
184                   (struct libcfs_ioctl_data *)hdr;
185
186                 if (data->ioc_hdr.ioc_len < sizeof(*data)) {
187                         rc = -EINVAL;
188                 } else {
189                         the_lnet.ln_nis_from_mod_params = data->ioc_flags;
190                         rc = lnet_configure(NULL);
191                 }
192                 break;
193         }
194
195         case IOC_LIBCFS_UNCONFIGURE:
196                 rc = lnet_unconfigure();
197                 break;
198
199         case IOC_LIBCFS_ADD_NET:
200                 rc = lnet_dyn_configure_net(hdr);
201                 break;
202
203         case IOC_LIBCFS_DEL_NET:
204                 rc = lnet_dyn_unconfigure_net(hdr);
205                 break;
206
207         case IOC_LIBCFS_ADD_LOCAL_NI:
208                 rc = lnet_dyn_configure_ni(hdr);
209                 break;
210
211         case IOC_LIBCFS_DEL_LOCAL_NI:
212                 rc = lnet_dyn_unconfigure_ni(hdr);
213                 break;
214
215         default:
216                 /* Passing LNET_PID_ANY only gives me a ref if the net is up
217                  * already; I'll need it to ensure the net can't go down while
218                  * I'm called into it */
219                 rc = LNetNIInit(LNET_PID_ANY);
220                 if (rc >= 0) {
221                         rc = LNetCtl(cmd, hdr);
222                         LNetNIFini();
223                 }
224                 break;
225         }
226         return notifier_from_ioctl_errno(rc);
227 }
228
229 static struct notifier_block lnet_ioctl_handler = {
230         .notifier_call = lnet_ioctl,
231 };
232
233 static int __init lnet_init(void)
234 {
235         int rc;
236         ENTRY;
237
238         rc = lnet_lib_init();
239         if (rc != 0) {
240                 CERROR("lnet_lib_init: error %d\n", rc);
241                 RETURN(rc);
242         }
243
244         if (live_router_check_interval != INT_MIN ||
245             dead_router_check_interval != INT_MIN)
246                 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");
247
248         rc = blocking_notifier_chain_register(&libcfs_ioctl_list,
249                                               &lnet_ioctl_handler);
250         LASSERT(rc == 0);
251
252         if (config_on_load) {
253                 /* Have to schedule a separate thread to avoid deadlocking
254                  * in modload */
255                 (void)kthread_run(lnet_configure, NULL, "lnet_initd");
256         }
257
258         RETURN(0);
259 }
260
261 static void __exit lnet_exit(void)
262 {
263         int rc;
264
265         rc = blocking_notifier_chain_unregister(&libcfs_ioctl_list,
266                                                 &lnet_ioctl_handler);
267         LASSERT(rc == 0);
268
269         lnet_lib_exit();
270 }
271
272 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
273 MODULE_DESCRIPTION("Lustre Networking layer");
274 MODULE_VERSION(LNET_VERSION);
275 MODULE_LICENSE("GPL");
276
277 module_init(lnet_init);
278 module_exit(lnet_exit);