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