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