Whamcloud - gitweb
LU-13255 lnet: introduce wait_var_event_warning.
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd.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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 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  * lnet/klnds/socklnd/socklnd.c
33  *
34  * Author: Zach Brown <zab@zabbo.net>
35  * Author: Peter J. Braam <braam@clusterfs.com>
36  * Author: Phil Schwan <phil@clusterfs.com>
37  * Author: Eric Barton <eric@bartonsoftware.com>
38  */
39
40 #include "socklnd.h"
41 #include <linux/inetdevice.h>
42
43 static const struct lnet_lnd the_ksocklnd;
44 struct ksock_nal_data ksocknal_data;
45
46 static struct ksock_interface *
47 ksocknal_ip2iface(struct lnet_ni *ni, __u32 ip)
48 {
49         struct ksock_net *net = ni->ni_data;
50         int i;
51         struct ksock_interface *iface;
52
53         for (i = 0; i < net->ksnn_ninterfaces; i++) {
54                 LASSERT(i < LNET_INTERFACES_NUM);
55                 iface = &net->ksnn_interfaces[i];
56
57                 if (iface->ksni_ipaddr == ip)
58                         return iface;
59         }
60
61         return NULL;
62 }
63
64 static struct ksock_route *
65 ksocknal_create_route(__u32 ipaddr, int port)
66 {
67         struct ksock_route *route;
68
69         LIBCFS_ALLOC (route, sizeof (*route));
70         if (route == NULL)
71                 return (NULL);
72
73         atomic_set (&route->ksnr_refcount, 1);
74         route->ksnr_peer = NULL;
75         route->ksnr_retry_interval = 0;         /* OK to connect at any time */
76         route->ksnr_ipaddr = ipaddr;
77         route->ksnr_port = port;
78         route->ksnr_scheduled = 0;
79         route->ksnr_connecting = 0;
80         route->ksnr_connected = 0;
81         route->ksnr_deleted = 0;
82         route->ksnr_conn_count = 0;
83         route->ksnr_share_count = 0;
84
85         return (route);
86 }
87
88 void
89 ksocknal_destroy_route(struct ksock_route *route)
90 {
91         LASSERT (atomic_read(&route->ksnr_refcount) == 0);
92
93         if (route->ksnr_peer != NULL)
94                 ksocknal_peer_decref(route->ksnr_peer);
95
96         LIBCFS_FREE (route, sizeof (*route));
97 }
98
99 static struct ksock_peer_ni *
100 ksocknal_create_peer(struct lnet_ni *ni, struct lnet_process_id id)
101 {
102         int cpt = lnet_cpt_of_nid(id.nid, ni);
103         struct ksock_net *net = ni->ni_data;
104         struct ksock_peer_ni *peer_ni;
105
106         LASSERT(id.nid != LNET_NID_ANY);
107         LASSERT(id.pid != LNET_PID_ANY);
108         LASSERT(!in_interrupt());
109
110         if (!atomic_inc_unless_negative(&net->ksnn_npeers)) {
111                 CERROR("Can't create peer_ni: network shutdown\n");
112                 return ERR_PTR(-ESHUTDOWN);
113         }
114
115         LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
116         if (!peer_ni) {
117                 atomic_dec(&net->ksnn_npeers);
118                 return ERR_PTR(-ENOMEM);
119         }
120
121         peer_ni->ksnp_ni = ni;
122         peer_ni->ksnp_id = id;
123         atomic_set(&peer_ni->ksnp_refcount, 1); /* 1 ref for caller */
124         peer_ni->ksnp_closing = 0;
125         peer_ni->ksnp_accepting = 0;
126         peer_ni->ksnp_proto = NULL;
127         peer_ni->ksnp_last_alive = 0;
128         peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
129
130         INIT_LIST_HEAD(&peer_ni->ksnp_conns);
131         INIT_LIST_HEAD(&peer_ni->ksnp_routes);
132         INIT_LIST_HEAD(&peer_ni->ksnp_tx_queue);
133         INIT_LIST_HEAD(&peer_ni->ksnp_zc_req_list);
134         spin_lock_init(&peer_ni->ksnp_lock);
135
136         return peer_ni;
137 }
138
139 void
140 ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni)
141 {
142         struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
143
144         CDEBUG (D_NET, "peer_ni %s %p deleted\n",
145                 libcfs_id2str(peer_ni->ksnp_id), peer_ni);
146
147         LASSERT(atomic_read(&peer_ni->ksnp_refcount) == 0);
148         LASSERT(peer_ni->ksnp_accepting == 0);
149         LASSERT(list_empty(&peer_ni->ksnp_conns));
150         LASSERT(list_empty(&peer_ni->ksnp_routes));
151         LASSERT(list_empty(&peer_ni->ksnp_tx_queue));
152         LASSERT(list_empty(&peer_ni->ksnp_zc_req_list));
153
154         LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
155
156         /* NB a peer_ni's connections and routes keep a reference on their
157          * peer_ni until they are destroyed, so we can be assured that _all_
158          * state to do with this peer_ni has been cleaned up when its refcount
159          * drops to zero.
160          */
161         if (atomic_dec_and_test(&net->ksnn_npeers))
162                 wake_up_var(&net->ksnn_npeers);
163 }
164
165 struct ksock_peer_ni *
166 ksocknal_find_peer_locked(struct lnet_ni *ni, struct lnet_process_id id)
167 {
168         struct ksock_peer_ni *peer_ni;
169
170         hash_for_each_possible(ksocknal_data.ksnd_peers, peer_ni,
171                                ksnp_list, id.nid) {
172                 LASSERT(!peer_ni->ksnp_closing);
173
174                 if (peer_ni->ksnp_ni != ni)
175                         continue;
176
177                 if (peer_ni->ksnp_id.nid != id.nid ||
178                     peer_ni->ksnp_id.pid != id.pid)
179                         continue;
180
181                 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d)\n",
182                        peer_ni, libcfs_id2str(id),
183                        atomic_read(&peer_ni->ksnp_refcount));
184                 return peer_ni;
185         }
186         return NULL;
187 }
188
189 struct ksock_peer_ni *
190 ksocknal_find_peer(struct lnet_ni *ni, struct lnet_process_id id)
191 {
192         struct ksock_peer_ni *peer_ni;
193
194         read_lock(&ksocknal_data.ksnd_global_lock);
195         peer_ni = ksocknal_find_peer_locked(ni, id);
196         if (peer_ni != NULL)                    /* +1 ref for caller? */
197                 ksocknal_peer_addref(peer_ni);
198         read_unlock(&ksocknal_data.ksnd_global_lock);
199
200         return (peer_ni);
201 }
202
203 static void
204 ksocknal_unlink_peer_locked(struct ksock_peer_ni *peer_ni)
205 {
206         int i;
207         __u32 ip;
208         struct ksock_interface *iface;
209
210         for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++) {
211                 LASSERT(i < LNET_INTERFACES_NUM);
212                 ip = peer_ni->ksnp_passive_ips[i];
213
214                 iface = ksocknal_ip2iface(peer_ni->ksnp_ni, ip);
215                 /*
216                  * All IPs in peer_ni->ksnp_passive_ips[] come from the
217                  * interface list, therefore the call must succeed.
218                  */
219                 LASSERT(iface != NULL);
220
221                 CDEBUG(D_NET, "peer_ni=%p iface=%p ksni_nroutes=%d\n",
222                        peer_ni, iface, iface->ksni_nroutes);
223                 iface->ksni_npeers--;
224         }
225
226         LASSERT(list_empty(&peer_ni->ksnp_conns));
227         LASSERT(list_empty(&peer_ni->ksnp_routes));
228         LASSERT(!peer_ni->ksnp_closing);
229         peer_ni->ksnp_closing = 1;
230         hlist_del(&peer_ni->ksnp_list);
231         /* lose peerlist's ref */
232         ksocknal_peer_decref(peer_ni);
233 }
234
235 static int
236 ksocknal_get_peer_info(struct lnet_ni *ni, int index,
237                        struct lnet_process_id *id, __u32 *myip, __u32 *peer_ip,
238                        int *port, int *conn_count, int *share_count)
239 {
240         struct ksock_peer_ni *peer_ni;
241         struct ksock_route *route;
242         struct list_head *rtmp;
243         int i;
244         int j;
245         int rc = -ENOENT;
246
247         read_lock(&ksocknal_data.ksnd_global_lock);
248
249         hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
250
251                 if (peer_ni->ksnp_ni != ni)
252                         continue;
253
254                 if (peer_ni->ksnp_n_passive_ips == 0 &&
255                     list_empty(&peer_ni->ksnp_routes)) {
256                         if (index-- > 0)
257                                 continue;
258
259                         *id = peer_ni->ksnp_id;
260                         *myip = 0;
261                         *peer_ip = 0;
262                         *port = 0;
263                         *conn_count = 0;
264                         *share_count = 0;
265                         rc = 0;
266                         goto out;
267                 }
268
269                 for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++) {
270                         if (index-- > 0)
271                                 continue;
272
273                         *id = peer_ni->ksnp_id;
274                         *myip = peer_ni->ksnp_passive_ips[j];
275                         *peer_ip = 0;
276                         *port = 0;
277                         *conn_count = 0;
278                         *share_count = 0;
279                         rc = 0;
280                         goto out;
281                 }
282
283                 list_for_each(rtmp, &peer_ni->ksnp_routes) {
284                         if (index-- > 0)
285                                 continue;
286
287                         route = list_entry(rtmp, struct ksock_route,
288                                            ksnr_list);
289
290                         *id = peer_ni->ksnp_id;
291                         *myip = route->ksnr_myipaddr;
292                         *peer_ip = route->ksnr_ipaddr;
293                         *port = route->ksnr_port;
294                         *conn_count = route->ksnr_conn_count;
295                         *share_count = route->ksnr_share_count;
296                         rc = 0;
297                         goto out;
298                 }
299         }
300 out:
301         read_unlock(&ksocknal_data.ksnd_global_lock);
302         return rc;
303 }
304
305 static void
306 ksocknal_associate_route_conn_locked(struct ksock_route *route, struct ksock_conn *conn)
307 {
308         struct ksock_peer_ni *peer_ni = route->ksnr_peer;
309         int type = conn->ksnc_type;
310         struct ksock_interface *iface;
311
312         conn->ksnc_route = route;
313         ksocknal_route_addref(route);
314
315         if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
316                 if (route->ksnr_myipaddr == 0) {
317                         /* route wasn't bound locally yet (the initial route) */
318                         CDEBUG(D_NET, "Binding %s %pI4h to %pI4h\n",
319                                libcfs_id2str(peer_ni->ksnp_id),
320                                &route->ksnr_ipaddr,
321                                &conn->ksnc_myipaddr);
322                 } else {
323                         CDEBUG(D_NET, "Rebinding %s %pI4h from %pI4h "
324                                "to %pI4h\n", libcfs_id2str(peer_ni->ksnp_id),
325                                &route->ksnr_ipaddr,
326                                &route->ksnr_myipaddr,
327                                &conn->ksnc_myipaddr);
328
329                         iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
330                                                   route->ksnr_myipaddr);
331                         if (iface != NULL)
332                                 iface->ksni_nroutes--;
333                 }
334                 route->ksnr_myipaddr = conn->ksnc_myipaddr;
335                 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
336                                           route->ksnr_myipaddr);
337                 if (iface != NULL)
338                         iface->ksni_nroutes++;
339         }
340
341         route->ksnr_connected |= (1<<type);
342         route->ksnr_conn_count++;
343
344         /* Successful connection => further attempts can
345          * proceed immediately */
346         route->ksnr_retry_interval = 0;
347 }
348
349 static void
350 ksocknal_add_route_locked(struct ksock_peer_ni *peer_ni, struct ksock_route *route)
351 {
352         struct list_head *tmp;
353         struct ksock_conn *conn;
354         struct ksock_route *route2;
355
356         LASSERT(!peer_ni->ksnp_closing);
357         LASSERT(route->ksnr_peer == NULL);
358         LASSERT(!route->ksnr_scheduled);
359         LASSERT(!route->ksnr_connecting);
360         LASSERT(route->ksnr_connected == 0);
361
362         /* LASSERT(unique) */
363         list_for_each(tmp, &peer_ni->ksnp_routes) {
364                 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
365
366                 if (route2->ksnr_ipaddr == route->ksnr_ipaddr) {
367                         CERROR("Duplicate route %s %pI4h\n",
368                                libcfs_id2str(peer_ni->ksnp_id),
369                                &route->ksnr_ipaddr);
370                         LBUG();
371                 }
372         }
373
374         route->ksnr_peer = peer_ni;
375         ksocknal_peer_addref(peer_ni);
376         /* peer_ni's routelist takes over my ref on 'route' */
377         list_add_tail(&route->ksnr_list, &peer_ni->ksnp_routes);
378
379         list_for_each(tmp, &peer_ni->ksnp_conns) {
380                 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
381
382                 if (conn->ksnc_ipaddr != route->ksnr_ipaddr)
383                         continue;
384
385                 ksocknal_associate_route_conn_locked(route, conn);
386                 /* keep going (typed routes) */
387         }
388 }
389
390 static void
391 ksocknal_del_route_locked(struct ksock_route *route)
392 {
393         struct ksock_peer_ni *peer_ni = route->ksnr_peer;
394         struct ksock_interface *iface;
395         struct ksock_conn *conn;
396         struct list_head *ctmp;
397         struct list_head *cnxt;
398
399         LASSERT(!route->ksnr_deleted);
400
401         /* Close associated conns */
402         list_for_each_safe(ctmp, cnxt, &peer_ni->ksnp_conns) {
403                 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
404
405                 if (conn->ksnc_route != route)
406                         continue;
407
408                 ksocknal_close_conn_locked(conn, 0);
409         }
410
411         if (route->ksnr_myipaddr != 0) {
412                 iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
413                                           route->ksnr_myipaddr);
414                 if (iface != NULL)
415                         iface->ksni_nroutes--;
416         }
417
418         route->ksnr_deleted = 1;
419         list_del(&route->ksnr_list);
420         ksocknal_route_decref(route);           /* drop peer_ni's ref */
421
422         if (list_empty(&peer_ni->ksnp_routes) &&
423             list_empty(&peer_ni->ksnp_conns)) {
424                 /* I've just removed the last route to a peer_ni with no active
425                  * connections */
426                 ksocknal_unlink_peer_locked(peer_ni);
427         }
428 }
429
430 int
431 ksocknal_add_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ipaddr,
432                   int port)
433 {
434         struct list_head *tmp;
435         struct ksock_peer_ni *peer_ni;
436         struct ksock_peer_ni *peer2;
437         struct ksock_route *route;
438         struct ksock_route *route2;
439
440         if (id.nid == LNET_NID_ANY ||
441             id.pid == LNET_PID_ANY)
442                 return (-EINVAL);
443
444         /* Have a brand new peer_ni ready... */
445         peer_ni = ksocknal_create_peer(ni, id);
446         if (IS_ERR(peer_ni))
447                 return PTR_ERR(peer_ni);
448
449         route = ksocknal_create_route (ipaddr, port);
450         if (route == NULL) {
451                 ksocknal_peer_decref(peer_ni);
452                 return (-ENOMEM);
453         }
454
455         write_lock_bh(&ksocknal_data.ksnd_global_lock);
456
457         /* always called with a ref on ni, so shutdown can't have started */
458         LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers)
459                 >= 0);
460
461         peer2 = ksocknal_find_peer_locked(ni, id);
462         if (peer2 != NULL) {
463                 ksocknal_peer_decref(peer_ni);
464                 peer_ni = peer2;
465         } else {
466                 /* peer_ni table takes my ref on peer_ni */
467                 hash_add(ksocknal_data.ksnd_peers, &peer_ni->ksnp_list, id.nid);
468         }
469
470         route2 = NULL;
471         list_for_each(tmp, &peer_ni->ksnp_routes) {
472                 route2 = list_entry(tmp, struct ksock_route, ksnr_list);
473
474                 if (route2->ksnr_ipaddr == ipaddr)
475                         break;
476
477                 route2 = NULL;
478         }
479         if (route2 == NULL) {
480                 ksocknal_add_route_locked(peer_ni, route);
481                 route->ksnr_share_count++;
482         } else {
483                 ksocknal_route_decref(route);
484                 route2->ksnr_share_count++;
485         }
486
487         write_unlock_bh(&ksocknal_data.ksnd_global_lock);
488
489         return 0;
490 }
491
492 static void
493 ksocknal_del_peer_locked(struct ksock_peer_ni *peer_ni, __u32 ip)
494 {
495         struct ksock_conn *conn;
496         struct ksock_route *route;
497         struct list_head *tmp;
498         struct list_head *nxt;
499         int nshared;
500
501         LASSERT(!peer_ni->ksnp_closing);
502
503         /* Extra ref prevents peer_ni disappearing until I'm done with it */
504         ksocknal_peer_addref(peer_ni);
505
506         list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
507                 route = list_entry(tmp, struct ksock_route, ksnr_list);
508
509                 /* no match */
510                 if (!(ip == 0 || route->ksnr_ipaddr == ip))
511                         continue;
512
513                 route->ksnr_share_count = 0;
514                 /* This deletes associated conns too */
515                 ksocknal_del_route_locked(route);
516         }
517
518         nshared = 0;
519         list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
520                 route = list_entry(tmp, struct ksock_route, ksnr_list);
521                 nshared += route->ksnr_share_count;
522         }
523
524         if (nshared == 0) {
525                 /* remove everything else if there are no explicit entries
526                  * left */
527
528                 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
529                         route = list_entry(tmp, struct ksock_route, ksnr_list);
530
531                         /* we should only be removing auto-entries */
532                         LASSERT(route->ksnr_share_count == 0);
533                         ksocknal_del_route_locked(route);
534                 }
535
536                 list_for_each_safe(tmp, nxt, &peer_ni->ksnp_conns) {
537                         conn = list_entry(tmp, struct ksock_conn, ksnc_list);
538
539                         ksocknal_close_conn_locked(conn, 0);
540                 }
541         }
542
543         ksocknal_peer_decref(peer_ni);
544         /* NB peer_ni unlinks itself when last conn/route is removed */
545 }
546
547 static int
548 ksocknal_del_peer(struct lnet_ni *ni, struct lnet_process_id id, __u32 ip)
549 {
550         LIST_HEAD(zombies);
551         struct hlist_node *pnxt;
552         struct ksock_peer_ni *peer_ni;
553         int lo;
554         int hi;
555         int i;
556         int rc = -ENOENT;
557
558         write_lock_bh(&ksocknal_data.ksnd_global_lock);
559
560         if (id.nid != LNET_NID_ANY) {
561                 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
562                 hi = lo;
563         } else {
564                 lo = 0;
565                 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
566         }
567
568         for (i = lo; i <= hi; i++) {
569                 hlist_for_each_entry_safe(peer_ni, pnxt,
570                                           &ksocknal_data.ksnd_peers[i],
571                                           ksnp_list) {
572                         if (peer_ni->ksnp_ni != ni)
573                                 continue;
574
575                         if (!((id.nid == LNET_NID_ANY ||
576                                peer_ni->ksnp_id.nid == id.nid) &&
577                               (id.pid == LNET_PID_ANY ||
578                                peer_ni->ksnp_id.pid == id.pid)))
579                                 continue;
580
581                         ksocknal_peer_addref(peer_ni);  /* a ref for me... */
582
583                         ksocknal_del_peer_locked(peer_ni, ip);
584
585                         if (peer_ni->ksnp_closing &&
586                             !list_empty(&peer_ni->ksnp_tx_queue)) {
587                                 LASSERT(list_empty(&peer_ni->ksnp_conns));
588                                 LASSERT(list_empty(&peer_ni->ksnp_routes));
589
590                                 list_splice_init(&peer_ni->ksnp_tx_queue,
591                                                  &zombies);
592                         }
593
594                         ksocknal_peer_decref(peer_ni);  /* ...till here */
595
596                         rc = 0;                         /* matched! */
597                 }
598         }
599
600         write_unlock_bh(&ksocknal_data.ksnd_global_lock);
601
602         ksocknal_txlist_done(ni, &zombies, -ENETDOWN);
603
604         return rc;
605 }
606
607 static struct ksock_conn *
608 ksocknal_get_conn_by_idx(struct lnet_ni *ni, int index)
609 {
610         struct ksock_peer_ni *peer_ni;
611         struct ksock_conn *conn;
612         struct list_head *ctmp;
613         int i;
614
615         read_lock(&ksocknal_data.ksnd_global_lock);
616
617         hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
618                 LASSERT(!peer_ni->ksnp_closing);
619
620                 if (peer_ni->ksnp_ni != ni)
621                         continue;
622
623                 list_for_each(ctmp, &peer_ni->ksnp_conns) {
624                         if (index-- > 0)
625                                 continue;
626
627                         conn = list_entry(ctmp, struct ksock_conn,
628                                           ksnc_list);
629                         ksocknal_conn_addref(conn);
630                         read_unlock(&ksocknal_data.ksnd_global_lock);
631                         return conn;
632                 }
633         }
634
635         read_unlock(&ksocknal_data.ksnd_global_lock);
636         return NULL;
637 }
638
639 static struct ksock_sched *
640 ksocknal_choose_scheduler_locked(unsigned int cpt)
641 {
642         struct ksock_sched *sched = ksocknal_data.ksnd_schedulers[cpt];
643         int i;
644
645         if (sched->kss_nthreads == 0) {
646                 cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
647                         if (sched->kss_nthreads > 0) {
648                                 CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
649                                        cpt, sched->kss_cpt);
650                                 return sched;
651                         }
652                 }
653                 return NULL;
654         }
655
656         return sched;
657 }
658
659 static int
660 ksocknal_local_ipvec(struct lnet_ni *ni, __u32 *ipaddrs)
661 {
662         struct ksock_net *net = ni->ni_data;
663         int i;
664         int nip;
665
666         read_lock(&ksocknal_data.ksnd_global_lock);
667
668         nip = net->ksnn_ninterfaces;
669         LASSERT(nip <= LNET_INTERFACES_NUM);
670
671         /*
672          * Only offer interfaces for additional connections if I have
673          * more than one.
674          */
675         if (nip < 2) {
676                 read_unlock(&ksocknal_data.ksnd_global_lock);
677                 return 0;
678         }
679
680         for (i = 0; i < nip; i++) {
681                 ipaddrs[i] = net->ksnn_interfaces[i].ksni_ipaddr;
682                 LASSERT(ipaddrs[i] != 0);
683         }
684
685         read_unlock(&ksocknal_data.ksnd_global_lock);
686         return nip;
687 }
688
689 static int
690 ksocknal_match_peerip(struct ksock_interface *iface, __u32 *ips, int nips)
691 {
692         int best_netmatch = 0;
693         int best_xor = 0;
694         int best = -1;
695         int this_xor;
696         int this_netmatch;
697         int i;
698
699         for (i = 0; i < nips; i++) {
700                 if (ips[i] == 0)
701                         continue;
702
703                 this_xor = (ips[i] ^ iface->ksni_ipaddr);
704                 this_netmatch = ((this_xor & iface->ksni_netmask) == 0) ? 1 : 0;
705
706                 if (!(best < 0 ||
707                       best_netmatch < this_netmatch ||
708                       (best_netmatch == this_netmatch &&
709                        best_xor > this_xor)))
710                         continue;
711
712                 best = i;
713                 best_netmatch = this_netmatch;
714                 best_xor = this_xor;
715         }
716
717         LASSERT (best >= 0);
718         return (best);
719 }
720
721 static int
722 ksocknal_select_ips(struct ksock_peer_ni *peer_ni, __u32 *peerips, int n_peerips)
723 {
724         rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
725         struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
726         struct ksock_interface *iface;
727         struct ksock_interface *best_iface;
728         int n_ips;
729         int i;
730         int j;
731         int k;
732         u32 ip;
733         u32 xor;
734         int this_netmatch;
735         int best_netmatch;
736         int best_npeers;
737
738         /* CAVEAT EMPTOR: We do all our interface matching with an
739          * exclusive hold of global lock at IRQ priority.  We're only
740          * expecting to be dealing with small numbers of interfaces, so the
741          * O(n**3)-ness shouldn't matter */
742
743         /* Also note that I'm not going to return more than n_peerips
744          * interfaces, even if I have more myself */
745
746         write_lock_bh(global_lock);
747
748         LASSERT(n_peerips <= LNET_INTERFACES_NUM);
749         LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
750
751         /* Only match interfaces for additional connections
752          * if I have > 1 interface
753          */
754         n_ips = (net->ksnn_ninterfaces < 2) ? 0 :
755                 min(n_peerips, net->ksnn_ninterfaces);
756
757         for (i = 0; peer_ni->ksnp_n_passive_ips < n_ips; i++) {
758                 /*              ^ yes really... */
759
760                 /* If we have any new interfaces, first tick off all the
761                  * peer_ni IPs that match old interfaces, then choose new
762                  * interfaces to match the remaining peer_ni IPS.
763                  * We don't forget interfaces we've stopped using; we might
764                  * start using them again... */
765
766                 if (i < peer_ni->ksnp_n_passive_ips) {
767                         /* Old interface. */
768                         ip = peer_ni->ksnp_passive_ips[i];
769                         best_iface = ksocknal_ip2iface(peer_ni->ksnp_ni, ip);
770
771                         /* peer_ni passive ips are kept up to date */
772                         LASSERT(best_iface != NULL);
773                 } else {
774                         /* choose a new interface */
775                         LASSERT (i == peer_ni->ksnp_n_passive_ips);
776
777                         best_iface = NULL;
778                         best_netmatch = 0;
779                         best_npeers = 0;
780
781                         for (j = 0; j < net->ksnn_ninterfaces; j++) {
782                                 iface = &net->ksnn_interfaces[j];
783                                 ip = iface->ksni_ipaddr;
784
785                                 for (k = 0; k < peer_ni->ksnp_n_passive_ips; k++)
786                                         if (peer_ni->ksnp_passive_ips[k] == ip)
787                                                 break;
788
789                                 if (k < peer_ni->ksnp_n_passive_ips) /* using it already */
790                                         continue;
791
792                                 k = ksocknal_match_peerip(iface, peerips, n_peerips);
793                                 xor = (ip ^ peerips[k]);
794                                 this_netmatch = ((xor & iface->ksni_netmask) == 0) ? 1 : 0;
795
796                                 if (!(best_iface == NULL ||
797                                       best_netmatch < this_netmatch ||
798                                       (best_netmatch == this_netmatch &&
799                                        best_npeers > iface->ksni_npeers)))
800                                         continue;
801
802                                 best_iface = iface;
803                                 best_netmatch = this_netmatch;
804                                 best_npeers = iface->ksni_npeers;
805                         }
806
807                         LASSERT(best_iface != NULL);
808
809                         best_iface->ksni_npeers++;
810                         ip = best_iface->ksni_ipaddr;
811                         peer_ni->ksnp_passive_ips[i] = ip;
812                         peer_ni->ksnp_n_passive_ips = i+1;
813                 }
814
815                 /* mark the best matching peer_ni IP used */
816                 j = ksocknal_match_peerip(best_iface, peerips, n_peerips);
817                 peerips[j] = 0;
818         }
819
820         /* Overwrite input peer_ni IP addresses */
821         memcpy(peerips, peer_ni->ksnp_passive_ips, n_ips * sizeof(*peerips));
822
823         write_unlock_bh(global_lock);
824
825         return (n_ips);
826 }
827
828 static void
829 ksocknal_create_routes(struct ksock_peer_ni *peer_ni, int port,
830                        __u32 *peer_ipaddrs, int npeer_ipaddrs)
831 {
832         struct ksock_route              *newroute = NULL;
833         rwlock_t                *global_lock = &ksocknal_data.ksnd_global_lock;
834         struct lnet_ni *ni = peer_ni->ksnp_ni;
835         struct ksock_net                *net = ni->ni_data;
836         struct list_head        *rtmp;
837         struct ksock_route              *route;
838         struct ksock_interface  *iface;
839         struct ksock_interface  *best_iface;
840         int                     best_netmatch;
841         int                     this_netmatch;
842         int                     best_nroutes;
843         int                     i;
844         int                     j;
845
846         /* CAVEAT EMPTOR: We do all our interface matching with an
847          * exclusive hold of global lock at IRQ priority.  We're only
848          * expecting to be dealing with small numbers of interfaces, so the
849          * O(n**3)-ness here shouldn't matter */
850
851         write_lock_bh(global_lock);
852
853         if (net->ksnn_ninterfaces < 2) {
854                 /* Only create additional connections
855                  * if I have > 1 interface */
856                 write_unlock_bh(global_lock);
857                 return;
858         }
859
860         LASSERT(npeer_ipaddrs <= LNET_INTERFACES_NUM);
861
862         for (i = 0; i < npeer_ipaddrs; i++) {
863                 if (newroute != NULL) {
864                         newroute->ksnr_ipaddr = peer_ipaddrs[i];
865                 } else {
866                         write_unlock_bh(global_lock);
867
868                         newroute = ksocknal_create_route(peer_ipaddrs[i], port);
869                         if (newroute == NULL)
870                                 return;
871
872                         write_lock_bh(global_lock);
873                 }
874
875                 if (peer_ni->ksnp_closing) {
876                         /* peer_ni got closed under me */
877                         break;
878                 }
879
880                 /* Already got a route? */
881                 route = NULL;
882                 list_for_each(rtmp, &peer_ni->ksnp_routes) {
883                         route = list_entry(rtmp, struct ksock_route, ksnr_list);
884
885                         if (route->ksnr_ipaddr == newroute->ksnr_ipaddr)
886                                 break;
887
888                         route = NULL;
889                 }
890                 if (route != NULL)
891                         continue;
892
893                 best_iface = NULL;
894                 best_nroutes = 0;
895                 best_netmatch = 0;
896
897                 LASSERT(net->ksnn_ninterfaces <= LNET_INTERFACES_NUM);
898
899                 /* Select interface to connect from */
900                 for (j = 0; j < net->ksnn_ninterfaces; j++) {
901                         iface = &net->ksnn_interfaces[j];
902
903                         /* Using this interface already? */
904                         list_for_each(rtmp, &peer_ni->ksnp_routes) {
905                                 route = list_entry(rtmp, struct ksock_route,
906                                                    ksnr_list);
907
908                                 if (route->ksnr_myipaddr == iface->ksni_ipaddr)
909                                         break;
910
911                                 route = NULL;
912                         }
913                         if (route != NULL)
914                                 continue;
915
916                         this_netmatch = (((iface->ksni_ipaddr ^
917                                            newroute->ksnr_ipaddr) &
918                                            iface->ksni_netmask) == 0) ? 1 : 0;
919
920                         if (!(best_iface == NULL ||
921                               best_netmatch < this_netmatch ||
922                               (best_netmatch == this_netmatch &&
923                                best_nroutes > iface->ksni_nroutes)))
924                                 continue;
925
926                         best_iface = iface;
927                         best_netmatch = this_netmatch;
928                         best_nroutes = iface->ksni_nroutes;
929                 }
930
931                 if (best_iface == NULL)
932                         continue;
933
934                 newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
935                 best_iface->ksni_nroutes++;
936
937                 ksocknal_add_route_locked(peer_ni, newroute);
938                 newroute = NULL;
939         }
940
941         write_unlock_bh(global_lock);
942         if (newroute != NULL)
943                 ksocknal_route_decref(newroute);
944 }
945
946 int
947 ksocknal_accept(struct lnet_ni *ni, struct socket *sock)
948 {
949         struct ksock_connreq *cr;
950         int rc;
951         u32 peer_ip;
952         int peer_port;
953
954         rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
955         LASSERT(rc == 0);               /* we succeeded before */
956
957         LIBCFS_ALLOC(cr, sizeof(*cr));
958         if (cr == NULL) {
959                 LCONSOLE_ERROR_MSG(0x12f, "Dropping connection request from "
960                                    "%pI4h: memory exhausted\n", &peer_ip);
961                 return -ENOMEM;
962         }
963
964         lnet_ni_addref(ni);
965         cr->ksncr_ni   = ni;
966         cr->ksncr_sock = sock;
967
968         spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
969
970         list_add_tail(&cr->ksncr_list, &ksocknal_data.ksnd_connd_connreqs);
971         wake_up(&ksocknal_data.ksnd_connd_waitq);
972
973         spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
974         return 0;
975 }
976
977 static int
978 ksocknal_connecting(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
979 {
980         struct ksock_route *route;
981
982         list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
983                 if (route->ksnr_ipaddr == ipaddr)
984                         return route->ksnr_connecting;
985         }
986         return 0;
987 }
988
989 int
990 ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route,
991                      struct socket *sock, int type)
992 {
993         rwlock_t *global_lock = &ksocknal_data.ksnd_global_lock;
994         LIST_HEAD(zombies);
995         struct lnet_process_id peerid;
996         struct list_head *tmp;
997         u64 incarnation;
998         struct ksock_conn *conn;
999         struct ksock_conn *conn2;
1000         struct ksock_peer_ni *peer_ni = NULL;
1001         struct ksock_peer_ni *peer2;
1002         struct ksock_sched *sched;
1003         struct ksock_hello_msg *hello;
1004         int cpt;
1005         struct ksock_tx *tx;
1006         struct ksock_tx *txtmp;
1007         int rc;
1008         int rc2;
1009         int active;
1010         char *warn = NULL;
1011
1012         active = (route != NULL);
1013
1014         LASSERT (active == (type != SOCKLND_CONN_NONE));
1015
1016         LIBCFS_ALLOC(conn, sizeof(*conn));
1017         if (conn == NULL) {
1018                 rc = -ENOMEM;
1019                 goto failed_0;
1020         }
1021
1022         conn->ksnc_peer = NULL;
1023         conn->ksnc_route = NULL;
1024         conn->ksnc_sock = sock;
1025         /* 2 ref, 1 for conn, another extra ref prevents socket
1026          * being closed before establishment of connection */
1027         atomic_set (&conn->ksnc_sock_refcount, 2);
1028         conn->ksnc_type = type;
1029         ksocknal_lib_save_callback(sock, conn);
1030         atomic_set (&conn->ksnc_conn_refcount, 1); /* 1 ref for me */
1031
1032         conn->ksnc_rx_ready = 0;
1033         conn->ksnc_rx_scheduled = 0;
1034
1035         INIT_LIST_HEAD(&conn->ksnc_tx_queue);
1036         conn->ksnc_tx_ready = 0;
1037         conn->ksnc_tx_scheduled = 0;
1038         conn->ksnc_tx_carrier = NULL;
1039         atomic_set (&conn->ksnc_tx_nob, 0);
1040
1041         LIBCFS_ALLOC(hello, offsetof(struct ksock_hello_msg,
1042                                      kshm_ips[LNET_INTERFACES_NUM]));
1043         if (hello == NULL) {
1044                 rc = -ENOMEM;
1045                 goto failed_1;
1046         }
1047
1048         /* stash conn's local and remote addrs */
1049         rc = ksocknal_lib_get_conn_addrs (conn);
1050         if (rc != 0)
1051                 goto failed_1;
1052
1053         /* Find out/confirm peer_ni's NID and connection type and get the
1054          * vector of interfaces she's willing to let me connect to.
1055          * Passive connections use the listener timeout since the peer_ni sends
1056          * eagerly */
1057
1058         if (active) {
1059                 peer_ni = route->ksnr_peer;
1060                 LASSERT(ni == peer_ni->ksnp_ni);
1061
1062                 /* Active connection sends HELLO eagerly */
1063                 hello->kshm_nips = ksocknal_local_ipvec(ni, hello->kshm_ips);
1064                 peerid = peer_ni->ksnp_id;
1065
1066                 write_lock_bh(global_lock);
1067                 conn->ksnc_proto = peer_ni->ksnp_proto;
1068                 write_unlock_bh(global_lock);
1069
1070                 if (conn->ksnc_proto == NULL) {
1071                          conn->ksnc_proto = &ksocknal_protocol_v3x;
1072 #if SOCKNAL_VERSION_DEBUG
1073                          if (*ksocknal_tunables.ksnd_protocol == 2)
1074                                  conn->ksnc_proto = &ksocknal_protocol_v2x;
1075                          else if (*ksocknal_tunables.ksnd_protocol == 1)
1076                                  conn->ksnc_proto = &ksocknal_protocol_v1x;
1077 #endif
1078                 }
1079
1080                 rc = ksocknal_send_hello (ni, conn, peerid.nid, hello);
1081                 if (rc != 0)
1082                         goto failed_1;
1083         } else {
1084                 peerid.nid = LNET_NID_ANY;
1085                 peerid.pid = LNET_PID_ANY;
1086
1087                 /* Passive, get protocol from peer_ni */
1088                 conn->ksnc_proto = NULL;
1089         }
1090
1091         rc = ksocknal_recv_hello (ni, conn, hello, &peerid, &incarnation);
1092         if (rc < 0)
1093                 goto failed_1;
1094
1095         LASSERT (rc == 0 || active);
1096         LASSERT (conn->ksnc_proto != NULL);
1097         LASSERT (peerid.nid != LNET_NID_ANY);
1098
1099         cpt = lnet_cpt_of_nid(peerid.nid, ni);
1100
1101         if (active) {
1102                 ksocknal_peer_addref(peer_ni);
1103                 write_lock_bh(global_lock);
1104         } else {
1105                 peer_ni = ksocknal_create_peer(ni, peerid);
1106                 if (IS_ERR(peer_ni)) {
1107                         rc = PTR_ERR(peer_ni);
1108                         goto failed_1;
1109                 }
1110
1111                 write_lock_bh(global_lock);
1112
1113                 /* called with a ref on ni, so shutdown can't have started */
1114                 LASSERT(atomic_read(&((struct ksock_net *)ni->ni_data)->ksnn_npeers) >= 0);
1115
1116                 peer2 = ksocknal_find_peer_locked(ni, peerid);
1117                 if (peer2 == NULL) {
1118                         /* NB this puts an "empty" peer_ni in the peer_ni
1119                          * table (which takes my ref) */
1120                         hash_add(ksocknal_data.ksnd_peers,
1121                                  &peer_ni->ksnp_list, peerid.nid);
1122                 } else {
1123                         ksocknal_peer_decref(peer_ni);
1124                         peer_ni = peer2;
1125                 }
1126
1127                 /* +1 ref for me */
1128                 ksocknal_peer_addref(peer_ni);
1129                 peer_ni->ksnp_accepting++;
1130
1131                 /* Am I already connecting to this guy?  Resolve in
1132                  * favour of higher NID... */
1133                 if (peerid.nid < ni->ni_nid &&
1134                     ksocknal_connecting(peer_ni, conn->ksnc_ipaddr)) {
1135                         rc = EALREADY;
1136                         warn = "connection race resolution";
1137                         goto failed_2;
1138                 }
1139         }
1140
1141         if (peer_ni->ksnp_closing ||
1142             (active && route->ksnr_deleted)) {
1143                 /* peer_ni/route got closed under me */
1144                 rc = -ESTALE;
1145                 warn = "peer_ni/route removed";
1146                 goto failed_2;
1147         }
1148
1149         if (peer_ni->ksnp_proto == NULL) {
1150                 /* Never connected before.
1151                  * NB recv_hello may have returned EPROTO to signal my peer_ni
1152                  * wants a different protocol than the one I asked for.
1153                  */
1154                 LASSERT(list_empty(&peer_ni->ksnp_conns));
1155
1156                 peer_ni->ksnp_proto = conn->ksnc_proto;
1157                 peer_ni->ksnp_incarnation = incarnation;
1158         }
1159
1160         if (peer_ni->ksnp_proto != conn->ksnc_proto ||
1161             peer_ni->ksnp_incarnation != incarnation) {
1162                 /* peer_ni rebooted or I've got the wrong protocol version */
1163                 ksocknal_close_peer_conns_locked(peer_ni, 0, 0);
1164
1165                 peer_ni->ksnp_proto = NULL;
1166                 rc = ESTALE;
1167                 warn = peer_ni->ksnp_incarnation != incarnation ?
1168                        "peer_ni rebooted" :
1169                        "wrong proto version";
1170                 goto failed_2;
1171         }
1172
1173         switch (rc) {
1174         default:
1175                 LBUG();
1176         case 0:
1177                 break;
1178         case EALREADY:
1179                 warn = "lost conn race";
1180                 goto failed_2;
1181         case EPROTO:
1182                 warn = "retry with different protocol version";
1183                 goto failed_2;
1184         }
1185
1186         /* Refuse to duplicate an existing connection, unless this is a
1187          * loopback connection */
1188         if (conn->ksnc_ipaddr != conn->ksnc_myipaddr) {
1189                 list_for_each(tmp, &peer_ni->ksnp_conns) {
1190                         conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1191
1192                         if (conn2->ksnc_ipaddr != conn->ksnc_ipaddr ||
1193                             conn2->ksnc_myipaddr != conn->ksnc_myipaddr ||
1194                             conn2->ksnc_type != conn->ksnc_type)
1195                                 continue;
1196
1197                         /* Reply on a passive connection attempt so the peer_ni
1198                          * realises we're connected. */
1199                         LASSERT (rc == 0);
1200                         if (!active)
1201                                 rc = EALREADY;
1202
1203                         warn = "duplicate";
1204                         goto failed_2;
1205                 }
1206         }
1207
1208         /* If the connection created by this route didn't bind to the IP
1209          * address the route connected to, the connection/route matching
1210          * code below probably isn't going to work. */
1211         if (active &&
1212             route->ksnr_ipaddr != conn->ksnc_ipaddr) {
1213                 CERROR("Route %s %pI4h connected to %pI4h\n",
1214                        libcfs_id2str(peer_ni->ksnp_id),
1215                        &route->ksnr_ipaddr,
1216                        &conn->ksnc_ipaddr);
1217         }
1218
1219         /* Search for a route corresponding to the new connection and
1220          * create an association.  This allows incoming connections created
1221          * by routes in my peer_ni to match my own route entries so I don't
1222          * continually create duplicate routes. */
1223         list_for_each(tmp, &peer_ni->ksnp_routes) {
1224                 route = list_entry(tmp, struct ksock_route, ksnr_list);
1225
1226                 if (route->ksnr_ipaddr != conn->ksnc_ipaddr)
1227                         continue;
1228
1229                 ksocknal_associate_route_conn_locked(route, conn);
1230                 break;
1231         }
1232
1233         conn->ksnc_peer = peer_ni;                 /* conn takes my ref on peer_ni */
1234         peer_ni->ksnp_last_alive = ktime_get_seconds();
1235         peer_ni->ksnp_send_keepalive = 0;
1236         peer_ni->ksnp_error = 0;
1237
1238         sched = ksocknal_choose_scheduler_locked(cpt);
1239         if (!sched) {
1240                 CERROR("no schedulers available. node is unhealthy\n");
1241                 goto failed_2;
1242         }
1243         /*
1244          * The cpt might have changed if we ended up selecting a non cpt
1245          * native scheduler. So use the scheduler's cpt instead.
1246          */
1247         cpt = sched->kss_cpt;
1248         sched->kss_nconns++;
1249         conn->ksnc_scheduler = sched;
1250
1251         conn->ksnc_tx_last_post = ktime_get_seconds();
1252         /* Set the deadline for the outgoing HELLO to drain */
1253         conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
1254         conn->ksnc_tx_deadline = ktime_get_seconds() +
1255                                  lnet_get_lnd_timeout();
1256         smp_mb();   /* order with adding to peer_ni's conn list */
1257
1258         list_add(&conn->ksnc_list, &peer_ni->ksnp_conns);
1259         ksocknal_conn_addref(conn);
1260
1261         ksocknal_new_packet(conn, 0);
1262
1263         conn->ksnc_zc_capable = ksocknal_lib_zc_capable(conn);
1264
1265         /* Take packets blocking for this connection. */
1266         list_for_each_entry_safe(tx, txtmp, &peer_ni->ksnp_tx_queue, tx_list) {
1267                 if (conn->ksnc_proto->pro_match_tx(conn, tx, tx->tx_nonblk) ==
1268                     SOCKNAL_MATCH_NO)
1269                         continue;
1270
1271                 list_del(&tx->tx_list);
1272                 ksocknal_queue_tx_locked(tx, conn);
1273         }
1274
1275         write_unlock_bh(global_lock);
1276
1277         /* We've now got a new connection.  Any errors from here on are just
1278          * like "normal" comms errors and we close the connection normally.
1279          * NB (a) we still have to send the reply HELLO for passive
1280          *        connections,
1281          *    (b) normal I/O on the conn is blocked until I setup and call the
1282          *        socket callbacks.
1283          */
1284
1285         CDEBUG(D_NET, "New conn %s p %d.x %pI4h -> %pI4h/%d"
1286                " incarnation:%lld sched[%d]\n",
1287                libcfs_id2str(peerid), conn->ksnc_proto->pro_version,
1288                &conn->ksnc_myipaddr, &conn->ksnc_ipaddr,
1289                conn->ksnc_port, incarnation, cpt);
1290
1291         if (active) {
1292                 /* additional routes after interface exchange? */
1293                 ksocknal_create_routes(peer_ni, conn->ksnc_port,
1294                                        hello->kshm_ips, hello->kshm_nips);
1295         } else {
1296                 hello->kshm_nips = ksocknal_select_ips(peer_ni, hello->kshm_ips,
1297                                                        hello->kshm_nips);
1298                 rc = ksocknal_send_hello(ni, conn, peerid.nid, hello);
1299         }
1300
1301         LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1302                                     kshm_ips[LNET_INTERFACES_NUM]));
1303
1304         /* setup the socket AFTER I've received hello (it disables
1305          * SO_LINGER).  I might call back to the acceptor who may want
1306          * to send a protocol version response and then close the
1307          * socket; this ensures the socket only tears down after the
1308          * response has been sent. */
1309         if (rc == 0)
1310                 rc = ksocknal_lib_setup_sock(sock);
1311
1312         write_lock_bh(global_lock);
1313
1314         /* NB my callbacks block while I hold ksnd_global_lock */
1315         ksocknal_lib_set_callback(sock, conn);
1316
1317         if (!active)
1318                 peer_ni->ksnp_accepting--;
1319
1320         write_unlock_bh(global_lock);
1321
1322         if (rc != 0) {
1323                 write_lock_bh(global_lock);
1324                 if (!conn->ksnc_closing) {
1325                         /* could be closed by another thread */
1326                         ksocknal_close_conn_locked(conn, rc);
1327                 }
1328                 write_unlock_bh(global_lock);
1329         } else if (ksocknal_connsock_addref(conn) == 0) {
1330                 /* Allow I/O to proceed. */
1331                 ksocknal_read_callback(conn);
1332                 ksocknal_write_callback(conn);
1333                 ksocknal_connsock_decref(conn);
1334         }
1335
1336         ksocknal_connsock_decref(conn);
1337         ksocknal_conn_decref(conn);
1338         return rc;
1339
1340 failed_2:
1341         if (!peer_ni->ksnp_closing &&
1342             list_empty(&peer_ni->ksnp_conns) &&
1343             list_empty(&peer_ni->ksnp_routes)) {
1344                 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
1345                 ksocknal_unlink_peer_locked(peer_ni);
1346         }
1347
1348         write_unlock_bh(global_lock);
1349
1350         if (warn != NULL) {
1351                 if (rc < 0)
1352                         CERROR("Not creating conn %s type %d: %s\n",
1353                                libcfs_id2str(peerid), conn->ksnc_type, warn);
1354                 else
1355                         CDEBUG(D_NET, "Not creating conn %s type %d: %s\n",
1356                               libcfs_id2str(peerid), conn->ksnc_type, warn);
1357         }
1358
1359         if (!active) {
1360                 if (rc > 0) {
1361                         /* Request retry by replying with CONN_NONE
1362                          * ksnc_proto has been set already */
1363                         conn->ksnc_type = SOCKLND_CONN_NONE;
1364                         hello->kshm_nips = 0;
1365                         ksocknal_send_hello(ni, conn, peerid.nid, hello);
1366                 }
1367
1368                 write_lock_bh(global_lock);
1369                 peer_ni->ksnp_accepting--;
1370                 write_unlock_bh(global_lock);
1371         }
1372
1373         /*
1374          * If we get here without an error code, just use -EALREADY.
1375          * Depending on how we got here, the error may be positive
1376          * or negative. Normalize the value for ksocknal_txlist_done().
1377          */
1378         rc2 = (rc == 0 ? -EALREADY : (rc > 0 ? -rc : rc));
1379         ksocknal_txlist_done(ni, &zombies, rc2);
1380         ksocknal_peer_decref(peer_ni);
1381
1382 failed_1:
1383         if (hello != NULL)
1384                 LIBCFS_FREE(hello, offsetof(struct ksock_hello_msg,
1385                                             kshm_ips[LNET_INTERFACES_NUM]));
1386
1387         LIBCFS_FREE(conn, sizeof(*conn));
1388
1389 failed_0:
1390         sock_release(sock);
1391         return rc;
1392 }
1393
1394 void
1395 ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
1396 {
1397         /* This just does the immmediate housekeeping, and queues the
1398          * connection for the reaper to terminate.
1399          * Caller holds ksnd_global_lock exclusively in irq context */
1400         struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1401         struct ksock_route *route;
1402         struct ksock_conn *conn2;
1403         struct list_head *tmp;
1404
1405         LASSERT(peer_ni->ksnp_error == 0);
1406         LASSERT(!conn->ksnc_closing);
1407         conn->ksnc_closing = 1;
1408
1409         /* ksnd_deathrow_conns takes over peer_ni's ref */
1410         list_del(&conn->ksnc_list);
1411
1412         route = conn->ksnc_route;
1413         if (route != NULL) {
1414                 /* dissociate conn from route... */
1415                 LASSERT(!route->ksnr_deleted);
1416                 LASSERT((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
1417
1418                 conn2 = NULL;
1419                 list_for_each(tmp, &peer_ni->ksnp_conns) {
1420                         conn2 = list_entry(tmp, struct ksock_conn, ksnc_list);
1421
1422                         if (conn2->ksnc_route == route &&
1423                             conn2->ksnc_type == conn->ksnc_type)
1424                                 break;
1425
1426                         conn2 = NULL;
1427                 }
1428                 if (conn2 == NULL)
1429                         route->ksnr_connected &= ~(1 << conn->ksnc_type);
1430
1431                 conn->ksnc_route = NULL;
1432
1433                 ksocknal_route_decref(route);   /* drop conn's ref on route */
1434         }
1435
1436         if (list_empty(&peer_ni->ksnp_conns)) {
1437                 /* No more connections to this peer_ni */
1438
1439                 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
1440                         struct ksock_tx *tx;
1441
1442                         LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
1443
1444                         /* throw them to the last connection...,
1445                          * these TXs will be send to /dev/null by scheduler */
1446                         list_for_each_entry(tx, &peer_ni->ksnp_tx_queue,
1447                                             tx_list)
1448                                 ksocknal_tx_prep(conn, tx);
1449
1450                         spin_lock_bh(&conn->ksnc_scheduler->kss_lock);
1451                         list_splice_init(&peer_ni->ksnp_tx_queue,
1452                                          &conn->ksnc_tx_queue);
1453                         spin_unlock_bh(&conn->ksnc_scheduler->kss_lock);
1454                 }
1455
1456                 /* renegotiate protocol version */
1457                 peer_ni->ksnp_proto = NULL;
1458                 /* stash last conn close reason */
1459                 peer_ni->ksnp_error = error;
1460
1461                 if (list_empty(&peer_ni->ksnp_routes)) {
1462                         /* I've just closed last conn belonging to a
1463                          * peer_ni with no routes to it */
1464                         ksocknal_unlink_peer_locked(peer_ni);
1465                 }
1466         }
1467
1468         spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1469
1470         list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_deathrow_conns);
1471         wake_up(&ksocknal_data.ksnd_reaper_waitq);
1472
1473         spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1474 }
1475
1476 void
1477 ksocknal_peer_failed(struct ksock_peer_ni *peer_ni)
1478 {
1479         int notify = 0;
1480         time64_t last_alive = 0;
1481
1482         /* There has been a connection failure or comms error; but I'll only
1483          * tell LNET I think the peer_ni is dead if it's to another kernel and
1484          * there are no connections or connection attempts in existence. */
1485
1486         read_lock(&ksocknal_data.ksnd_global_lock);
1487
1488         if ((peer_ni->ksnp_id.pid & LNET_PID_USERFLAG) == 0 &&
1489              list_empty(&peer_ni->ksnp_conns) &&
1490              peer_ni->ksnp_accepting == 0 &&
1491              ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
1492                 notify = 1;
1493                 last_alive = peer_ni->ksnp_last_alive;
1494         }
1495
1496         read_unlock(&ksocknal_data.ksnd_global_lock);
1497
1498         if (notify)
1499                 lnet_notify(peer_ni->ksnp_ni, peer_ni->ksnp_id.nid,
1500                             false, false, last_alive);
1501 }
1502
1503 void
1504 ksocknal_finalize_zcreq(struct ksock_conn *conn)
1505 {
1506         struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1507         struct ksock_tx *tx;
1508         struct ksock_tx *tmp;
1509         LIST_HEAD(zlist);
1510
1511         /* NB safe to finalize TXs because closing of socket will
1512          * abort all buffered data */
1513         LASSERT(conn->ksnc_sock == NULL);
1514
1515         spin_lock(&peer_ni->ksnp_lock);
1516
1517         list_for_each_entry_safe(tx, tmp, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
1518                 if (tx->tx_conn != conn)
1519                         continue;
1520
1521                 LASSERT(tx->tx_msg.ksm_zc_cookies[0] != 0);
1522
1523                 tx->tx_msg.ksm_zc_cookies[0] = 0;
1524                 tx->tx_zc_aborted = 1;  /* mark it as not-acked */
1525                 list_move(&tx->tx_zc_list, &zlist);
1526         }
1527
1528         spin_unlock(&peer_ni->ksnp_lock);
1529
1530         while (!list_empty(&zlist)) {
1531                 tx = list_entry(zlist.next, struct ksock_tx, tx_zc_list);
1532
1533                 list_del(&tx->tx_zc_list);
1534                 ksocknal_tx_decref(tx);
1535         }
1536 }
1537
1538 void
1539 ksocknal_terminate_conn(struct ksock_conn *conn)
1540 {
1541         /* This gets called by the reaper (guaranteed thread context) to
1542          * disengage the socket from its callbacks and close it.
1543          * ksnc_refcount will eventually hit zero, and then the reaper will
1544          * destroy it. */
1545         struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1546         struct ksock_sched *sched = conn->ksnc_scheduler;
1547         int failed = 0;
1548
1549         LASSERT(conn->ksnc_closing);
1550
1551         /* wake up the scheduler to "send" all remaining packets to /dev/null */
1552         spin_lock_bh(&sched->kss_lock);
1553
1554         /* a closing conn is always ready to tx */
1555         conn->ksnc_tx_ready = 1;
1556
1557         if (!conn->ksnc_tx_scheduled &&
1558             !list_empty(&conn->ksnc_tx_queue)) {
1559                 list_add_tail(&conn->ksnc_tx_list,
1560                                &sched->kss_tx_conns);
1561                 conn->ksnc_tx_scheduled = 1;
1562                 /* extra ref for scheduler */
1563                 ksocknal_conn_addref(conn);
1564
1565                 wake_up (&sched->kss_waitq);
1566         }
1567
1568         spin_unlock_bh(&sched->kss_lock);
1569
1570         /* serialise with callbacks */
1571         write_lock_bh(&ksocknal_data.ksnd_global_lock);
1572
1573         ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1574
1575         /* OK, so this conn may not be completely disengaged from its
1576          * scheduler yet, but it _has_ committed to terminate... */
1577         conn->ksnc_scheduler->kss_nconns--;
1578
1579         if (peer_ni->ksnp_error != 0) {
1580                 /* peer_ni's last conn closed in error */
1581                 LASSERT(list_empty(&peer_ni->ksnp_conns));
1582                 failed = 1;
1583                 peer_ni->ksnp_error = 0;     /* avoid multiple notifications */
1584         }
1585
1586         write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1587
1588         if (failed)
1589                 ksocknal_peer_failed(peer_ni);
1590
1591         /* The socket is closed on the final put; either here, or in
1592          * ksocknal_{send,recv}msg().  Since we set up the linger2 option
1593          * when the connection was established, this will close the socket
1594          * immediately, aborting anything buffered in it. Any hung
1595          * zero-copy transmits will therefore complete in finite time. */
1596         ksocknal_connsock_decref(conn);
1597 }
1598
1599 void
1600 ksocknal_queue_zombie_conn(struct ksock_conn *conn)
1601 {
1602         /* Queue the conn for the reaper to destroy */
1603         LASSERT(atomic_read(&conn->ksnc_conn_refcount) == 0);
1604         spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
1605
1606         list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1607         wake_up(&ksocknal_data.ksnd_reaper_waitq);
1608
1609         spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
1610 }
1611
1612 void
1613 ksocknal_destroy_conn(struct ksock_conn *conn)
1614 {
1615         time64_t last_rcv;
1616
1617         /* Final coup-de-grace of the reaper */
1618         CDEBUG (D_NET, "connection %p\n", conn);
1619
1620         LASSERT (atomic_read (&conn->ksnc_conn_refcount) == 0);
1621         LASSERT (atomic_read (&conn->ksnc_sock_refcount) == 0);
1622         LASSERT (conn->ksnc_sock == NULL);
1623         LASSERT (conn->ksnc_route == NULL);
1624         LASSERT (!conn->ksnc_tx_scheduled);
1625         LASSERT (!conn->ksnc_rx_scheduled);
1626         LASSERT(list_empty(&conn->ksnc_tx_queue));
1627
1628         /* complete current receive if any */
1629         switch (conn->ksnc_rx_state) {
1630         case SOCKNAL_RX_LNET_PAYLOAD:
1631                 last_rcv = conn->ksnc_rx_deadline -
1632                            lnet_get_lnd_timeout();
1633                 CERROR("Completing partial receive from %s[%d], "
1634                        "ip %pI4h:%d, with error, wanted: %d, left: %d, "
1635                        "last alive is %lld secs ago\n",
1636                        libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1637                        &conn->ksnc_ipaddr, conn->ksnc_port,
1638                        conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1639                        ktime_get_seconds() - last_rcv);
1640                 if (conn->ksnc_lnet_msg)
1641                         conn->ksnc_lnet_msg->msg_health_status =
1642                                 LNET_MSG_STATUS_REMOTE_ERROR;
1643                 lnet_finalize(conn->ksnc_lnet_msg, -EIO);
1644                 break;
1645         case SOCKNAL_RX_LNET_HEADER:
1646                 if (conn->ksnc_rx_started)
1647                         CERROR("Incomplete receive of lnet header from %s, "
1648                                "ip %pI4h:%d, with error, protocol: %d.x.\n",
1649                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1650                                &conn->ksnc_ipaddr, conn->ksnc_port,
1651                                conn->ksnc_proto->pro_version);
1652                 break;
1653         case SOCKNAL_RX_KSM_HEADER:
1654                 if (conn->ksnc_rx_started)
1655                         CERROR("Incomplete receive of ksock message from %s, "
1656                                "ip %pI4h:%d, with error, protocol: %d.x.\n",
1657                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1658                                &conn->ksnc_ipaddr, conn->ksnc_port,
1659                                conn->ksnc_proto->pro_version);
1660                 break;
1661         case SOCKNAL_RX_SLOP:
1662                 if (conn->ksnc_rx_started)
1663                         CERROR("Incomplete receive of slops from %s, "
1664                                "ip %pI4h:%d, with error\n",
1665                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1666                                &conn->ksnc_ipaddr, conn->ksnc_port);
1667                break;
1668         default:
1669                 LBUG ();
1670                 break;
1671         }
1672
1673         ksocknal_peer_decref(conn->ksnc_peer);
1674
1675         LIBCFS_FREE (conn, sizeof (*conn));
1676 }
1677
1678 int
1679 ksocknal_close_peer_conns_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr, int why)
1680 {
1681         struct ksock_conn *conn;
1682         struct list_head *ctmp;
1683         struct list_head *cnxt;
1684         int count = 0;
1685
1686         list_for_each_safe(ctmp, cnxt, &peer_ni->ksnp_conns) {
1687                 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
1688
1689                 if (ipaddr == 0 ||
1690                     conn->ksnc_ipaddr == ipaddr) {
1691                         count++;
1692                         ksocknal_close_conn_locked (conn, why);
1693                 }
1694         }
1695
1696         return (count);
1697 }
1698
1699 int
1700 ksocknal_close_conn_and_siblings(struct ksock_conn *conn, int why)
1701 {
1702         struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
1703         u32 ipaddr = conn->ksnc_ipaddr;
1704         int count;
1705
1706         write_lock_bh(&ksocknal_data.ksnd_global_lock);
1707
1708         count = ksocknal_close_peer_conns_locked (peer_ni, ipaddr, why);
1709
1710         write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1711
1712         return (count);
1713 }
1714
1715 int
1716 ksocknal_close_matching_conns(struct lnet_process_id id, __u32 ipaddr)
1717 {
1718         struct ksock_peer_ni *peer_ni;
1719         struct hlist_node *pnxt;
1720         int lo;
1721         int hi;
1722         int i;
1723         int count = 0;
1724
1725         write_lock_bh(&ksocknal_data.ksnd_global_lock);
1726
1727         if (id.nid != LNET_NID_ANY) {
1728                 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1729                 hi = lo;
1730         } else {
1731                 lo = 0;
1732                 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1733         }
1734
1735         for (i = lo; i <= hi; i++) {
1736                 hlist_for_each_entry_safe(peer_ni, pnxt,
1737                                           &ksocknal_data.ksnd_peers[i],
1738                                           ksnp_list) {
1739
1740                         if (!((id.nid == LNET_NID_ANY ||
1741                                id.nid == peer_ni->ksnp_id.nid) &&
1742                               (id.pid == LNET_PID_ANY ||
1743                                id.pid == peer_ni->ksnp_id.pid)))
1744                                 continue;
1745
1746                         count += ksocknal_close_peer_conns_locked(peer_ni,
1747                                                                   ipaddr, 0);
1748                 }
1749         }
1750
1751         write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1752
1753         /* wildcards always succeed */
1754         if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1755                 return 0;
1756
1757         return (count == 0 ? -ENOENT : 0);
1758 }
1759
1760 void
1761 ksocknal_notify_gw_down(lnet_nid_t gw_nid)
1762 {
1763         /* The router is telling me she's been notified of a change in
1764          * gateway state....
1765          */
1766         struct lnet_process_id id = {
1767                 .nid    = gw_nid,
1768                 .pid    = LNET_PID_ANY,
1769         };
1770
1771         CDEBUG(D_NET, "gw %s down\n", libcfs_nid2str(gw_nid));
1772
1773         /* If the gateway crashed, close all open connections... */
1774         ksocknal_close_matching_conns(id, 0);
1775         return;
1776
1777         /* We can only establish new connections
1778          * if we have autroutes, and these connect on demand. */
1779 }
1780
1781 static void
1782 ksocknal_push_peer(struct ksock_peer_ni *peer_ni)
1783 {
1784         int index;
1785         int i;
1786         struct list_head *tmp;
1787         struct ksock_conn *conn;
1788
1789         for (index = 0; ; index++) {
1790                 read_lock(&ksocknal_data.ksnd_global_lock);
1791
1792                 i = 0;
1793                 conn = NULL;
1794
1795                 list_for_each(tmp, &peer_ni->ksnp_conns) {
1796                         if (i++ == index) {
1797                                 conn = list_entry(tmp, struct ksock_conn,
1798                                                   ksnc_list);
1799                                 ksocknal_conn_addref(conn);
1800                                 break;
1801                         }
1802                 }
1803
1804                 read_unlock(&ksocknal_data.ksnd_global_lock);
1805
1806                 if (conn == NULL)
1807                         break;
1808
1809                 ksocknal_lib_push_conn (conn);
1810                 ksocknal_conn_decref(conn);
1811         }
1812 }
1813
1814 static int
1815 ksocknal_push(struct lnet_ni *ni, struct lnet_process_id id)
1816 {
1817         int lo;
1818         int hi;
1819         int bkt;
1820         int rc = -ENOENT;
1821
1822         if (id.nid != LNET_NID_ANY) {
1823                 lo = hash_min(id.nid, HASH_BITS(ksocknal_data.ksnd_peers));
1824                 hi = lo;
1825         } else {
1826                 lo = 0;
1827                 hi = HASH_SIZE(ksocknal_data.ksnd_peers) - 1;
1828         }
1829
1830         for (bkt = lo; bkt <= hi; bkt++) {
1831                 int peer_off; /* searching offset in peer_ni hash table */
1832
1833                 for (peer_off = 0; ; peer_off++) {
1834                         struct ksock_peer_ni *peer_ni;
1835                         int           i = 0;
1836
1837                         read_lock(&ksocknal_data.ksnd_global_lock);
1838                         hlist_for_each_entry(peer_ni,
1839                                              &ksocknal_data.ksnd_peers[bkt],
1840                                              ksnp_list) {
1841                                 if (!((id.nid == LNET_NID_ANY ||
1842                                        id.nid == peer_ni->ksnp_id.nid) &&
1843                                       (id.pid == LNET_PID_ANY ||
1844                                        id.pid == peer_ni->ksnp_id.pid)))
1845                                         continue;
1846
1847                                 if (i++ == peer_off) {
1848                                         ksocknal_peer_addref(peer_ni);
1849                                         break;
1850                                 }
1851                         }
1852                         read_unlock(&ksocknal_data.ksnd_global_lock);
1853
1854                         if (i <= peer_off) /* no match */
1855                                 break;
1856
1857                         rc = 0;
1858                         ksocknal_push_peer(peer_ni);
1859                         ksocknal_peer_decref(peer_ni);
1860                 }
1861         }
1862         return rc;
1863 }
1864
1865 static int
1866 ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
1867 {
1868         struct ksock_net *net = ni->ni_data;
1869         struct ksock_interface *iface;
1870         int rc;
1871         int i;
1872         int j;
1873         struct ksock_peer_ni *peer_ni;
1874         struct list_head *rtmp;
1875         struct ksock_route *route;
1876
1877         if (ipaddress == 0 ||
1878             netmask == 0)
1879                 return -EINVAL;
1880
1881         write_lock_bh(&ksocknal_data.ksnd_global_lock);
1882
1883         iface = ksocknal_ip2iface(ni, ipaddress);
1884         if (iface != NULL) {
1885                 /* silently ignore dups */
1886                 rc = 0;
1887         } else if (net->ksnn_ninterfaces == LNET_INTERFACES_NUM) {
1888                 rc = -ENOSPC;
1889         } else {
1890                 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1891
1892                 iface->ksni_ipaddr = ipaddress;
1893                 iface->ksni_netmask = netmask;
1894                 iface->ksni_nroutes = 0;
1895                 iface->ksni_npeers = 0;
1896
1897                 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
1898                         for (j = 0; j < peer_ni->ksnp_n_passive_ips; j++)
1899                                 if (peer_ni->ksnp_passive_ips[j] == ipaddress)
1900                                         iface->ksni_npeers++;
1901
1902                         list_for_each(rtmp, &peer_ni->ksnp_routes) {
1903                                 route = list_entry(rtmp,
1904                                                    struct ksock_route,
1905                                                    ksnr_list);
1906
1907                                 if (route->ksnr_myipaddr == ipaddress)
1908                                         iface->ksni_nroutes++;
1909                         }
1910                 }
1911
1912                 rc = 0;
1913                 /* NB only new connections will pay attention to the new
1914                  * interface!
1915                  */
1916         }
1917
1918         write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1919
1920         return rc;
1921 }
1922
1923 static void
1924 ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
1925 {
1926         struct list_head *tmp;
1927         struct list_head *nxt;
1928         struct ksock_route *route;
1929         struct ksock_conn *conn;
1930         int i;
1931         int j;
1932
1933         for (i = 0; i < peer_ni->ksnp_n_passive_ips; i++)
1934                 if (peer_ni->ksnp_passive_ips[i] == ipaddr) {
1935                         for (j = i+1; j < peer_ni->ksnp_n_passive_ips; j++)
1936                                 peer_ni->ksnp_passive_ips[j-1] =
1937                                         peer_ni->ksnp_passive_ips[j];
1938                         peer_ni->ksnp_n_passive_ips--;
1939                         break;
1940                 }
1941
1942         list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
1943                 route = list_entry(tmp, struct ksock_route, ksnr_list);
1944
1945                 if (route->ksnr_myipaddr != ipaddr)
1946                         continue;
1947
1948                 if (route->ksnr_share_count != 0) {
1949                         /* Manually created; keep, but unbind */
1950                         route->ksnr_myipaddr = 0;
1951                 } else {
1952                         ksocknal_del_route_locked(route);
1953                 }
1954         }
1955
1956         list_for_each_safe(tmp, nxt, &peer_ni->ksnp_conns) {
1957                 conn = list_entry(tmp, struct ksock_conn, ksnc_list);
1958
1959                 if (conn->ksnc_myipaddr == ipaddr)
1960                         ksocknal_close_conn_locked (conn, 0);
1961         }
1962 }
1963
1964 static int
1965 ksocknal_del_interface(struct lnet_ni *ni, __u32 ipaddress)
1966 {
1967         struct ksock_net *net = ni->ni_data;
1968         int rc = -ENOENT;
1969         struct hlist_node *nxt;
1970         struct ksock_peer_ni *peer_ni;
1971         u32 this_ip;
1972         int i;
1973         int j;
1974
1975         write_lock_bh(&ksocknal_data.ksnd_global_lock);
1976
1977         for (i = 0; i < net->ksnn_ninterfaces; i++) {
1978                 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
1979
1980                 if (!(ipaddress == 0 ||
1981                       ipaddress == this_ip))
1982                         continue;
1983
1984                 rc = 0;
1985
1986                 for (j = i+1; j < net->ksnn_ninterfaces; j++)
1987                         net->ksnn_interfaces[j-1] =
1988                                 net->ksnn_interfaces[j];
1989
1990                 net->ksnn_ninterfaces--;
1991
1992                 hash_for_each_safe(ksocknal_data.ksnd_peers, j,
1993                                    nxt, peer_ni, ksnp_list) {
1994                         if (peer_ni->ksnp_ni != ni)
1995                                 continue;
1996
1997                         ksocknal_peer_del_interface_locked(peer_ni, this_ip);
1998                 }
1999         }
2000
2001         write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2002
2003         return rc;
2004 }
2005
2006 int
2007 ksocknal_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
2008 {
2009         struct lnet_process_id id = {0};
2010         struct libcfs_ioctl_data *data = arg;
2011         int rc;
2012
2013         switch(cmd) {
2014         case IOC_LIBCFS_GET_INTERFACE: {
2015                 struct ksock_net *net = ni->ni_data;
2016                 struct ksock_interface *iface;
2017
2018                 read_lock(&ksocknal_data.ksnd_global_lock);
2019
2020                 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2021                         rc = -ENOENT;
2022                 } else {
2023                         rc = 0;
2024                         iface = &net->ksnn_interfaces[data->ioc_count];
2025
2026                         data->ioc_u32[0] = iface->ksni_ipaddr;
2027                         data->ioc_u32[1] = iface->ksni_netmask;
2028                         data->ioc_u32[2] = iface->ksni_npeers;
2029                         data->ioc_u32[3] = iface->ksni_nroutes;
2030                 }
2031
2032                 read_unlock(&ksocknal_data.ksnd_global_lock);
2033                 return rc;
2034         }
2035
2036         case IOC_LIBCFS_ADD_INTERFACE:
2037                 return ksocknal_add_interface(ni,
2038                                               data->ioc_u32[0], /* IP address */
2039                                               data->ioc_u32[1]); /* net mask */
2040
2041         case IOC_LIBCFS_DEL_INTERFACE:
2042                 return ksocknal_del_interface(ni,
2043                                               data->ioc_u32[0]); /* IP address */
2044
2045         case IOC_LIBCFS_GET_PEER: {
2046                 __u32            myip = 0;
2047                 __u32            ip = 0;
2048                 int              port = 0;
2049                 int              conn_count = 0;
2050                 int              share_count = 0;
2051
2052                 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2053                                             &id, &myip, &ip, &port,
2054                                             &conn_count,  &share_count);
2055                 if (rc != 0)
2056                         return rc;
2057
2058                 data->ioc_nid    = id.nid;
2059                 data->ioc_count  = share_count;
2060                 data->ioc_u32[0] = ip;
2061                 data->ioc_u32[1] = port;
2062                 data->ioc_u32[2] = myip;
2063                 data->ioc_u32[3] = conn_count;
2064                 data->ioc_u32[4] = id.pid;
2065                 return 0;
2066         }
2067
2068         case IOC_LIBCFS_ADD_PEER:
2069                 id.nid = data->ioc_nid;
2070                 id.pid = LNET_PID_LUSTRE;
2071                 return ksocknal_add_peer (ni, id,
2072                                           data->ioc_u32[0], /* IP */
2073                                           data->ioc_u32[1]); /* port */
2074
2075         case IOC_LIBCFS_DEL_PEER:
2076                 id.nid = data->ioc_nid;
2077                 id.pid = LNET_PID_ANY;
2078                 return ksocknal_del_peer (ni, id,
2079                                           data->ioc_u32[0]); /* IP */
2080
2081         case IOC_LIBCFS_GET_CONN: {
2082                 int           txmem;
2083                 int           rxmem;
2084                 int           nagle;
2085                 struct ksock_conn *conn = ksocknal_get_conn_by_idx(ni, data->ioc_count);
2086
2087                 if (conn == NULL)
2088                         return -ENOENT;
2089
2090                 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2091
2092                 data->ioc_count  = txmem;
2093                 data->ioc_nid    = conn->ksnc_peer->ksnp_id.nid;
2094                 data->ioc_flags  = nagle;
2095                 data->ioc_u32[0] = conn->ksnc_ipaddr;
2096                 data->ioc_u32[1] = conn->ksnc_port;
2097                 data->ioc_u32[2] = conn->ksnc_myipaddr;
2098                 data->ioc_u32[3] = conn->ksnc_type;
2099                 data->ioc_u32[4] = conn->ksnc_scheduler->kss_cpt;
2100                 data->ioc_u32[5] = rxmem;
2101                 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2102                 ksocknal_conn_decref(conn);
2103                 return 0;
2104         }
2105
2106         case IOC_LIBCFS_CLOSE_CONNECTION:
2107                 id.nid = data->ioc_nid;
2108                 id.pid = LNET_PID_ANY;
2109                 return ksocknal_close_matching_conns (id,
2110                                                       data->ioc_u32[0]);
2111
2112         case IOC_LIBCFS_REGISTER_MYNID:
2113                 /* Ignore if this is a noop */
2114                 if (data->ioc_nid == ni->ni_nid)
2115                         return 0;
2116
2117                 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2118                        libcfs_nid2str(data->ioc_nid),
2119                        libcfs_nid2str(ni->ni_nid));
2120                 return -EINVAL;
2121
2122         case IOC_LIBCFS_PUSH_CONNECTION:
2123                 id.nid = data->ioc_nid;
2124                 id.pid = LNET_PID_ANY;
2125                 return ksocknal_push(ni, id);
2126
2127         default:
2128                 return -EINVAL;
2129         }
2130         /* not reached */
2131 }
2132
2133 static void
2134 ksocknal_free_buffers (void)
2135 {
2136         LASSERT (atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2137
2138         if (ksocknal_data.ksnd_schedulers != NULL)
2139                 cfs_percpt_free(ksocknal_data.ksnd_schedulers);
2140
2141         spin_lock(&ksocknal_data.ksnd_tx_lock);
2142
2143         if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2144                 LIST_HEAD(zlist);
2145                 struct ksock_tx *tx;
2146
2147                 list_splice_init(&ksocknal_data.ksnd_idle_noop_txs, &zlist);
2148                 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2149
2150                 while (!list_empty(&zlist)) {
2151                         tx = list_entry(zlist.next, struct ksock_tx, tx_list);
2152                         list_del(&tx->tx_list);
2153                         LIBCFS_FREE(tx, tx->tx_desc_size);
2154                 }
2155         } else {
2156                 spin_unlock(&ksocknal_data.ksnd_tx_lock);
2157         }
2158 }
2159
2160 static void
2161 ksocknal_base_shutdown(void)
2162 {
2163         struct ksock_sched *sched;
2164         struct ksock_peer_ni *peer_ni;
2165         int i;
2166
2167         CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2168                atomic_read (&libcfs_kmemory));
2169         LASSERT (ksocknal_data.ksnd_nnets == 0);
2170
2171         switch (ksocknal_data.ksnd_init) {
2172         default:
2173                 LASSERT(0);
2174                 /* fallthrough */
2175
2176         case SOCKNAL_INIT_ALL:
2177         case SOCKNAL_INIT_DATA:
2178                 hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list)
2179                         LASSERT(0);
2180
2181                 LASSERT(list_empty(&ksocknal_data.ksnd_nets));
2182                 LASSERT(list_empty(&ksocknal_data.ksnd_enomem_conns));
2183                 LASSERT(list_empty(&ksocknal_data.ksnd_zombie_conns));
2184                 LASSERT(list_empty(&ksocknal_data.ksnd_connd_connreqs));
2185                 LASSERT(list_empty(&ksocknal_data.ksnd_connd_routes));
2186
2187                 if (ksocknal_data.ksnd_schedulers != NULL) {
2188                         cfs_percpt_for_each(sched, i,
2189                                             ksocknal_data.ksnd_schedulers) {
2190
2191                                 LASSERT(list_empty(&sched->kss_tx_conns));
2192                                 LASSERT(list_empty(&sched->kss_rx_conns));
2193                                 LASSERT(list_empty(&sched->kss_zombie_noop_txs));
2194                                 LASSERT(sched->kss_nconns == 0);
2195                         }
2196                 }
2197
2198                 /* flag threads to terminate; wake and wait for them to die */
2199                 ksocknal_data.ksnd_shuttingdown = 1;
2200                 wake_up_all(&ksocknal_data.ksnd_connd_waitq);
2201                 wake_up_all(&ksocknal_data.ksnd_reaper_waitq);
2202
2203                 if (ksocknal_data.ksnd_schedulers != NULL) {
2204                         cfs_percpt_for_each(sched, i,
2205                                             ksocknal_data.ksnd_schedulers)
2206                                         wake_up_all(&sched->kss_waitq);
2207                 }
2208
2209                 wait_var_event_warning(&ksocknal_data.ksnd_nthreads,
2210                                        ksocknal_data.ksnd_nthreads == 0,
2211                                        "waiting for %d threads to terminate\n",
2212                                        ksocknal_data.ksnd_nthreads);
2213
2214                 ksocknal_free_buffers();
2215
2216                 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2217                 break;
2218         }
2219
2220         CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2221                atomic_read (&libcfs_kmemory));
2222
2223         module_put(THIS_MODULE);
2224 }
2225
2226 static int
2227 ksocknal_base_startup(void)
2228 {
2229         struct ksock_sched *sched;
2230         int rc;
2231         int i;
2232
2233         LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2234         LASSERT(ksocknal_data.ksnd_nnets == 0);
2235
2236         memset(&ksocknal_data, 0, sizeof(ksocknal_data)); /* zero pointers */
2237
2238         hash_init(ksocknal_data.ksnd_peers);
2239
2240         rwlock_init(&ksocknal_data.ksnd_global_lock);
2241         INIT_LIST_HEAD(&ksocknal_data.ksnd_nets);
2242
2243         spin_lock_init(&ksocknal_data.ksnd_reaper_lock);
2244         INIT_LIST_HEAD(&ksocknal_data.ksnd_enomem_conns);
2245         INIT_LIST_HEAD(&ksocknal_data.ksnd_zombie_conns);
2246         INIT_LIST_HEAD(&ksocknal_data.ksnd_deathrow_conns);
2247         init_waitqueue_head(&ksocknal_data.ksnd_reaper_waitq);
2248
2249         spin_lock_init(&ksocknal_data.ksnd_connd_lock);
2250         INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_connreqs);
2251         INIT_LIST_HEAD(&ksocknal_data.ksnd_connd_routes);
2252         init_waitqueue_head(&ksocknal_data.ksnd_connd_waitq);
2253
2254         spin_lock_init(&ksocknal_data.ksnd_tx_lock);
2255         INIT_LIST_HEAD(&ksocknal_data.ksnd_idle_noop_txs);
2256
2257         /* NB memset above zeros whole of ksocknal_data */
2258
2259         /* flag lists/ptrs/locks initialised */
2260         ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2261         if (!try_module_get(THIS_MODULE))
2262                 goto failed;
2263
2264         /* Create a scheduler block per available CPT */
2265         ksocknal_data.ksnd_schedulers = cfs_percpt_alloc(lnet_cpt_table(),
2266                                                          sizeof(*sched));
2267         if (ksocknal_data.ksnd_schedulers == NULL)
2268                 goto failed;
2269
2270         cfs_percpt_for_each(sched, i, ksocknal_data.ksnd_schedulers) {
2271                 int nthrs;
2272
2273                 /*
2274                  * make sure not to allocate more threads than there are
2275                  * cores/CPUs in teh CPT
2276                  */
2277                 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
2278                 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2279                         nthrs = min(nthrs, *ksocknal_tunables.ksnd_nscheds);
2280                 } else {
2281                         /*
2282                          * max to half of CPUs, assume another half should be
2283                          * reserved for upper layer modules
2284                          */
2285                         nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2286                 }
2287
2288                 sched->kss_nthreads_max = nthrs;
2289                 sched->kss_cpt = i;
2290
2291                 spin_lock_init(&sched->kss_lock);
2292                 INIT_LIST_HEAD(&sched->kss_rx_conns);
2293                 INIT_LIST_HEAD(&sched->kss_tx_conns);
2294                 INIT_LIST_HEAD(&sched->kss_zombie_noop_txs);
2295                 init_waitqueue_head(&sched->kss_waitq);
2296         }
2297
2298         ksocknal_data.ksnd_connd_starting         = 0;
2299         ksocknal_data.ksnd_connd_failed_stamp     = 0;
2300         ksocknal_data.ksnd_connd_starting_stamp   = ktime_get_real_seconds();
2301         /* must have at least 2 connds to remain responsive to accepts while
2302          * connecting */
2303         if (*ksocknal_tunables.ksnd_nconnds < SOCKNAL_CONND_RESV + 1)
2304                 *ksocknal_tunables.ksnd_nconnds = SOCKNAL_CONND_RESV + 1;
2305
2306         if (*ksocknal_tunables.ksnd_nconnds_max <
2307             *ksocknal_tunables.ksnd_nconnds) {
2308                 ksocknal_tunables.ksnd_nconnds_max =
2309                         ksocknal_tunables.ksnd_nconnds;
2310         }
2311
2312         for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2313                 char name[16];
2314                 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2315                 ksocknal_data.ksnd_connd_starting++;
2316                 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2317
2318
2319                 snprintf(name, sizeof(name), "socknal_cd%02d", i);
2320                 rc = ksocknal_thread_start(ksocknal_connd,
2321                                            (void *)((uintptr_t)i), name);
2322                 if (rc != 0) {
2323                         spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2324                         ksocknal_data.ksnd_connd_starting--;
2325                         spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2326                         CERROR("Can't spawn socknal connd: %d\n", rc);
2327                         goto failed;
2328                 }
2329         }
2330
2331         rc = ksocknal_thread_start(ksocknal_reaper, NULL, "socknal_reaper");
2332         if (rc != 0) {
2333                 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2334                 goto failed;
2335         }
2336
2337         /* flag everything initialised */
2338         ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2339
2340         return 0;
2341
2342  failed:
2343         ksocknal_base_shutdown();
2344         return -ENETDOWN;
2345 }
2346
2347 static int
2348 ksocknal_debug_peerhash(struct lnet_ni *ni)
2349 {
2350         struct ksock_peer_ni *peer_ni;
2351         int i;
2352
2353         read_lock(&ksocknal_data.ksnd_global_lock);
2354
2355         hash_for_each(ksocknal_data.ksnd_peers, i, peer_ni, ksnp_list) {
2356                 struct ksock_route *route;
2357                 struct ksock_conn *conn;
2358
2359                 if (peer_ni->ksnp_ni != ni)
2360                         continue;
2361
2362                 CWARN("Active peer_ni on shutdown: %s, ref %d, "
2363                       "closing %d, accepting %d, err %d, zcookie %llu, "
2364                       "txq %d, zc_req %d\n", libcfs_id2str(peer_ni->ksnp_id),
2365                       atomic_read(&peer_ni->ksnp_refcount),
2366                       peer_ni->ksnp_closing,
2367                       peer_ni->ksnp_accepting, peer_ni->ksnp_error,
2368                       peer_ni->ksnp_zc_next_cookie,
2369                       !list_empty(&peer_ni->ksnp_tx_queue),
2370                       !list_empty(&peer_ni->ksnp_zc_req_list));
2371
2372                 list_for_each_entry(route, &peer_ni->ksnp_routes, ksnr_list) {
2373                         CWARN("Route: ref %d, schd %d, conn %d, cnted %d, "
2374                               "del %d\n", atomic_read(&route->ksnr_refcount),
2375                               route->ksnr_scheduled, route->ksnr_connecting,
2376                               route->ksnr_connected, route->ksnr_deleted);
2377                 }
2378
2379                 list_for_each_entry(conn, &peer_ni->ksnp_conns, ksnc_list) {
2380                         CWARN("Conn: ref %d, sref %d, t %d, c %d\n",
2381                               atomic_read(&conn->ksnc_conn_refcount),
2382                               atomic_read(&conn->ksnc_sock_refcount),
2383                               conn->ksnc_type, conn->ksnc_closing);
2384                 }
2385                 break;
2386         }
2387
2388         read_unlock(&ksocknal_data.ksnd_global_lock);
2389         return 0;
2390 }
2391
2392 void
2393 ksocknal_shutdown(struct lnet_ni *ni)
2394 {
2395         struct ksock_net *net = ni->ni_data;
2396         struct lnet_process_id anyid = {
2397                 .nid = LNET_NID_ANY,
2398                 .pid = LNET_PID_ANY,
2399         };
2400         int i;
2401
2402         LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2403         LASSERT(ksocknal_data.ksnd_nnets > 0);
2404
2405         /* prevent new peers */
2406         atomic_add(SOCKNAL_SHUTDOWN_BIAS, &net->ksnn_npeers);
2407
2408         /* Delete all peers */
2409         ksocknal_del_peer(ni, anyid, 0);
2410
2411         /* Wait for all peer_ni state to clean up */
2412         wait_var_event_warning(&net->ksnn_npeers,
2413                                atomic_read(&net->ksnn_npeers) ==
2414                                SOCKNAL_SHUTDOWN_BIAS,
2415                                "waiting for %d peers to disconnect\n",
2416                                ksocknal_debug_peerhash(ni) +
2417                                atomic_read(&net->ksnn_npeers) -
2418                                SOCKNAL_SHUTDOWN_BIAS);
2419
2420         for (i = 0; i < net->ksnn_ninterfaces; i++) {
2421                 LASSERT(net->ksnn_interfaces[i].ksni_npeers == 0);
2422                 LASSERT(net->ksnn_interfaces[i].ksni_nroutes == 0);
2423         }
2424
2425         list_del(&net->ksnn_list);
2426         LIBCFS_FREE(net, sizeof(*net));
2427
2428         ksocknal_data.ksnd_nnets--;
2429         if (ksocknal_data.ksnd_nnets == 0)
2430                 ksocknal_base_shutdown();
2431 }
2432
2433 static int
2434 ksocknal_search_new_ipif(struct ksock_net *net)
2435 {
2436         int new_ipif = 0;
2437         int i;
2438
2439         for (i = 0; i < net->ksnn_ninterfaces; i++) {
2440                 char *ifnam = &net->ksnn_interfaces[i].ksni_name[0];
2441                 char *colon = strchr(ifnam, ':');
2442                 int found  = 0;
2443                 struct ksock_net *tmp;
2444                 int j;
2445
2446                 if (colon != NULL) /* ignore alias device */
2447                         *colon = 0;
2448
2449                 list_for_each_entry(tmp, &ksocknal_data.ksnd_nets,
2450                                         ksnn_list) {
2451                         for (j = 0; !found && j < tmp->ksnn_ninterfaces; j++) {
2452                                 char *ifnam2 = &tmp->ksnn_interfaces[j].\
2453                                              ksni_name[0];
2454                                 char *colon2 = strchr(ifnam2, ':');
2455
2456                                 if (colon2 != NULL)
2457                                         *colon2 = 0;
2458
2459                                 found = strcmp(ifnam, ifnam2) == 0;
2460                                 if (colon2 != NULL)
2461                                         *colon2 = ':';
2462                         }
2463                         if (found)
2464                                 break;
2465                 }
2466
2467                 new_ipif += !found;
2468                 if (colon != NULL)
2469                         *colon = ':';
2470         }
2471
2472         return new_ipif;
2473 }
2474
2475 static int
2476 ksocknal_start_schedulers(struct ksock_sched *sched)
2477 {
2478         int     nthrs;
2479         int     rc = 0;
2480         int     i;
2481
2482         if (sched->kss_nthreads == 0) {
2483                 if (*ksocknal_tunables.ksnd_nscheds > 0) {
2484                         nthrs = sched->kss_nthreads_max;
2485                 } else {
2486                         nthrs = cfs_cpt_weight(lnet_cpt_table(),
2487                                                sched->kss_cpt);
2488                         nthrs = min(max(SOCKNAL_NSCHEDS, nthrs >> 1), nthrs);
2489                         nthrs = min(SOCKNAL_NSCHEDS_HIGH, nthrs);
2490                 }
2491                 nthrs = min(nthrs, sched->kss_nthreads_max);
2492         } else {
2493                 LASSERT(sched->kss_nthreads <= sched->kss_nthreads_max);
2494                 /* increase two threads if there is new interface */
2495                 nthrs = min(2, sched->kss_nthreads_max - sched->kss_nthreads);
2496         }
2497
2498         for (i = 0; i < nthrs; i++) {
2499                 long id;
2500                 char name[20];
2501
2502                 id = KSOCK_THREAD_ID(sched->kss_cpt, sched->kss_nthreads + i);
2503                 snprintf(name, sizeof(name), "socknal_sd%02d_%02d",
2504                          sched->kss_cpt, (int)KSOCK_THREAD_SID(id));
2505
2506                 rc = ksocknal_thread_start(ksocknal_scheduler,
2507                                            (void *)id, name);
2508                 if (rc == 0)
2509                         continue;
2510
2511                 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
2512                        sched->kss_cpt, (int) KSOCK_THREAD_SID(id), rc);
2513                 break;
2514         }
2515
2516         sched->kss_nthreads += i;
2517         return rc;
2518 }
2519
2520 static int
2521 ksocknal_net_start_threads(struct ksock_net *net, __u32 *cpts, int ncpts)
2522 {
2523         int newif = ksocknal_search_new_ipif(net);
2524         int rc;
2525         int i;
2526
2527         if (ncpts > 0 && ncpts > cfs_cpt_number(lnet_cpt_table()))
2528                 return -EINVAL;
2529
2530         for (i = 0; i < ncpts; i++) {
2531                 struct ksock_sched *sched;
2532                 int cpt = (cpts == NULL) ? i : cpts[i];
2533
2534                 LASSERT(cpt < cfs_cpt_number(lnet_cpt_table()));
2535                 sched = ksocknal_data.ksnd_schedulers[cpt];
2536
2537                 if (!newif && sched->kss_nthreads > 0)
2538                         continue;
2539
2540                 rc = ksocknal_start_schedulers(sched);
2541                 if (rc != 0)
2542                         return rc;
2543         }
2544         return 0;
2545 }
2546
2547 int
2548 ksocknal_startup(struct lnet_ni *ni)
2549 {
2550         struct ksock_net *net;
2551         struct lnet_ioctl_config_lnd_cmn_tunables *net_tunables;
2552         struct ksock_interface *ksi = NULL;
2553         struct lnet_inetdev *ifaces = NULL;
2554         int i = 0;
2555         int rc;
2556
2557         LASSERT (ni->ni_net->net_lnd == &the_ksocklnd);
2558
2559         if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2560                 rc = ksocknal_base_startup();
2561                 if (rc != 0)
2562                         return rc;
2563         }
2564
2565         LIBCFS_ALLOC(net, sizeof(*net));
2566         if (net == NULL)
2567                 goto fail_0;
2568
2569         net->ksnn_incarnation = ktime_get_real_ns();
2570         ni->ni_data = net;
2571         net_tunables = &ni->ni_net->net_tunables;
2572
2573         if (net_tunables->lct_peer_timeout == -1)
2574                 net_tunables->lct_peer_timeout =
2575                         *ksocknal_tunables.ksnd_peertimeout;
2576
2577         if (net_tunables->lct_max_tx_credits == -1)
2578                 net_tunables->lct_max_tx_credits =
2579                         *ksocknal_tunables.ksnd_credits;
2580
2581         if (net_tunables->lct_peer_tx_credits == -1)
2582                 net_tunables->lct_peer_tx_credits =
2583                         *ksocknal_tunables.ksnd_peertxcredits;
2584
2585         if (net_tunables->lct_peer_tx_credits >
2586             net_tunables->lct_max_tx_credits)
2587                 net_tunables->lct_peer_tx_credits =
2588                         net_tunables->lct_max_tx_credits;
2589
2590         if (net_tunables->lct_peer_rtr_credits == -1)
2591                 net_tunables->lct_peer_rtr_credits =
2592                         *ksocknal_tunables.ksnd_peerrtrcredits;
2593
2594         rc = lnet_inet_enumerate(&ifaces, ni->ni_net_ns);
2595         if (rc < 0)
2596                 goto fail_1;
2597
2598         if (!ni->ni_interfaces[0]) {
2599                 ksi = &net->ksnn_interfaces[0];
2600
2601                 /* Use the first discovered interface */
2602                 net->ksnn_ninterfaces = 1;
2603                 ni->ni_dev_cpt = ifaces[0].li_cpt;
2604                 ksi->ksni_ipaddr = ifaces[0].li_ipaddr;
2605                 ksi->ksni_netmask = ifaces[0].li_netmask;
2606                 strlcpy(ksi->ksni_name, ifaces[0].li_name,
2607                         sizeof(ksi->ksni_name));
2608         } else {
2609                 /* Before Multi-Rail ksocklnd would manage
2610                  * multiple interfaces with its own tcp bonding.
2611                  * If we encounter an old configuration using
2612                  * this tcp bonding approach then we need to
2613                  * handle more than one ni_interfaces.
2614                  *
2615                  * In Multi-Rail configuration only ONE ni_interface
2616                  * should exist. Each IP alias should be mapped to
2617                  * each 'struct net_ni'.
2618                  */
2619                 for (i = 0; i < LNET_INTERFACES_NUM; i++) {
2620                         int j;
2621
2622                         if (!ni->ni_interfaces[i])
2623                                 break;
2624
2625                         for (j = 0; j < LNET_INTERFACES_NUM;  j++) {
2626                                 if (i != j && ni->ni_interfaces[j] &&
2627                                     strcmp(ni->ni_interfaces[i],
2628                                            ni->ni_interfaces[j]) == 0) {
2629                                         rc = -EEXIST;
2630                                         CERROR("ksocklnd: found duplicate %s at %d and %d, rc = %d\n",
2631                                                ni->ni_interfaces[i], i, j, rc);
2632                                         goto fail_1;
2633                                 }
2634                         }
2635
2636                         for (j = 0; j < rc; j++) {
2637                                 if (strcmp(ifaces[j].li_name,
2638                                            ni->ni_interfaces[i]) != 0)
2639                                         continue;
2640
2641                                 ksi = &net->ksnn_interfaces[j];
2642                                 ni->ni_dev_cpt = ifaces[j].li_cpt;
2643                                 ksi->ksni_ipaddr = ifaces[j].li_ipaddr;
2644                                 ksi->ksni_netmask = ifaces[j].li_netmask;
2645                                 strlcpy(ksi->ksni_name, ifaces[j].li_name,
2646                                         sizeof(ksi->ksni_name));
2647                                 net->ksnn_ninterfaces++;
2648                                 break;
2649                         }
2650                 }
2651                 /* ni_interfaces don't map to all network interfaces */
2652                 if (!ksi || net->ksnn_ninterfaces != i) {
2653                         CERROR("ksocklnd: requested %d but only %d interfaces found\n",
2654                                i, net->ksnn_ninterfaces);
2655                         goto fail_1;
2656                 }
2657         }
2658
2659         /* call it before add it to ksocknal_data.ksnd_nets */
2660         rc = ksocknal_net_start_threads(net, ni->ni_cpts, ni->ni_ncpts);
2661         if (rc != 0)
2662                 goto fail_1;
2663
2664         LASSERT(ksi);
2665         ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ksi->ksni_ipaddr);
2666         list_add(&net->ksnn_list, &ksocknal_data.ksnd_nets);
2667
2668         ksocknal_data.ksnd_nnets++;
2669
2670         return 0;
2671
2672  fail_1:
2673         LIBCFS_FREE(net, sizeof(*net));
2674  fail_0:
2675         if (ksocknal_data.ksnd_nnets == 0)
2676                 ksocknal_base_shutdown();
2677
2678         return -ENETDOWN;
2679 }
2680
2681
2682 static void __exit ksocklnd_exit(void)
2683 {
2684         lnet_unregister_lnd(&the_ksocklnd);
2685 }
2686
2687 static const struct lnet_lnd the_ksocklnd = {
2688         .lnd_type               = SOCKLND,
2689         .lnd_startup            = ksocknal_startup,
2690         .lnd_shutdown           = ksocknal_shutdown,
2691         .lnd_ctl                = ksocknal_ctl,
2692         .lnd_send               = ksocknal_send,
2693         .lnd_recv               = ksocknal_recv,
2694         .lnd_notify_peer_down   = ksocknal_notify_gw_down,
2695         .lnd_accept             = ksocknal_accept,
2696 };
2697
2698 static int __init ksocklnd_init(void)
2699 {
2700         int rc;
2701
2702         /* check ksnr_connected/connecting field large enough */
2703         BUILD_BUG_ON(SOCKLND_CONN_NTYPES > 4);
2704         BUILD_BUG_ON(SOCKLND_CONN_ACK != SOCKLND_CONN_BULK_IN);
2705
2706         rc = ksocknal_tunables_init();
2707         if (rc != 0)
2708                 return rc;
2709
2710         lnet_register_lnd(&the_ksocklnd);
2711
2712         return 0;
2713 }
2714
2715 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2716 MODULE_DESCRIPTION("TCP Socket LNet Network Driver");
2717 MODULE_VERSION("2.8.0");
2718 MODULE_LICENSE("GPL");
2719
2720 module_init(ksocklnd_init);
2721 module_exit(ksocklnd_exit);