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