Whamcloud - gitweb
b=20615 always send ZC_ACK on non-blocking connection
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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                 if (tx->tx_resid == 0)
1555                         tx->tx_resid = -1; /* mark it as not-acked */
1556                 cfs_list_del(&tx->tx_zc_list);
1557                 cfs_list_add(&tx->tx_zc_list, &zlist);
1558         }
1559
1560         cfs_spin_unlock(&peer->ksnp_lock);
1561
1562         while (!cfs_list_empty(&zlist)) {
1563                 tx = cfs_list_entry(zlist.next, ksock_tx_t, tx_zc_list);
1564
1565                 cfs_list_del(&tx->tx_zc_list);
1566                 ksocknal_tx_decref(tx);
1567         }
1568 }
1569
1570 void
1571 ksocknal_terminate_conn (ksock_conn_t *conn)
1572 {
1573         /* This gets called by the reaper (guaranteed thread context) to
1574          * disengage the socket from its callbacks and close it.
1575          * ksnc_refcount will eventually hit zero, and then the reaper will
1576          * destroy it. */
1577         ksock_peer_t     *peer = conn->ksnc_peer;
1578         ksock_sched_t    *sched = conn->ksnc_scheduler;
1579         int               failed = 0;
1580
1581         LASSERT(conn->ksnc_closing);
1582
1583         /* wake up the scheduler to "send" all remaining packets to /dev/null */
1584         cfs_spin_lock_bh (&sched->kss_lock);
1585
1586         /* a closing conn is always ready to tx */
1587         conn->ksnc_tx_ready = 1;
1588
1589         if (!conn->ksnc_tx_scheduled &&
1590             !cfs_list_empty(&conn->ksnc_tx_queue)){
1591                 cfs_list_add_tail (&conn->ksnc_tx_list,
1592                                &sched->kss_tx_conns);
1593                 conn->ksnc_tx_scheduled = 1;
1594                 /* extra ref for scheduler */
1595                 ksocknal_conn_addref(conn);
1596
1597                 cfs_waitq_signal (&sched->kss_waitq);
1598         }
1599
1600         cfs_spin_unlock_bh (&sched->kss_lock);
1601
1602         /* serialise with callbacks */
1603         cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1604
1605         ksocknal_lib_reset_callback(conn->ksnc_sock, conn);
1606
1607         /* OK, so this conn may not be completely disengaged from its
1608          * scheduler yet, but it _has_ committed to terminate... */
1609         conn->ksnc_scheduler->kss_nconns--;
1610
1611         if (peer->ksnp_error != 0) {
1612                 /* peer's last conn closed in error */
1613                 LASSERT (cfs_list_empty (&peer->ksnp_conns));
1614                 failed = 1;
1615                 peer->ksnp_error = 0;     /* avoid multiple notifications */
1616         }
1617
1618         cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1619
1620         if (failed)
1621                 ksocknal_peer_failed(peer);
1622
1623         /* The socket is closed on the final put; either here, or in
1624          * ksocknal_{send,recv}msg().  Since we set up the linger2 option
1625          * when the connection was established, this will close the socket
1626          * immediately, aborting anything buffered in it. Any hung
1627          * zero-copy transmits will therefore complete in finite time. */
1628         ksocknal_connsock_decref(conn);
1629 }
1630
1631 void
1632 ksocknal_queue_zombie_conn (ksock_conn_t *conn)
1633 {
1634         /* Queue the conn for the reaper to destroy */
1635
1636         LASSERT (cfs_atomic_read(&conn->ksnc_conn_refcount) == 0);
1637         cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
1638
1639         cfs_list_add_tail(&conn->ksnc_list, &ksocknal_data.ksnd_zombie_conns);
1640         cfs_waitq_signal(&ksocknal_data.ksnd_reaper_waitq);
1641
1642         cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
1643 }
1644
1645 void
1646 ksocknal_destroy_conn (ksock_conn_t *conn)
1647 {
1648         cfs_time_t      last_rcv;
1649
1650         /* Final coup-de-grace of the reaper */
1651         CDEBUG (D_NET, "connection %p\n", conn);
1652
1653         LASSERT (cfs_atomic_read (&conn->ksnc_conn_refcount) == 0);
1654         LASSERT (cfs_atomic_read (&conn->ksnc_sock_refcount) == 0);
1655         LASSERT (conn->ksnc_sock == NULL);
1656         LASSERT (conn->ksnc_route == NULL);
1657         LASSERT (!conn->ksnc_tx_scheduled);
1658         LASSERT (!conn->ksnc_rx_scheduled);
1659         LASSERT (cfs_list_empty(&conn->ksnc_tx_queue));
1660
1661         /* complete current receive if any */
1662         switch (conn->ksnc_rx_state) {
1663         case SOCKNAL_RX_LNET_PAYLOAD:
1664                 last_rcv = conn->ksnc_rx_deadline -
1665                            cfs_time_seconds(*ksocknal_tunables.ksnd_timeout);
1666                 CERROR("Completing partial receive from %s[%d]"
1667                        ", ip %d.%d.%d.%d:%d, with error, wanted: %d, left: %d, "
1668                        "last alive is %ld secs ago\n",
1669                        libcfs_id2str(conn->ksnc_peer->ksnp_id), conn->ksnc_type,
1670                        HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1671                        conn->ksnc_rx_nob_wanted, conn->ksnc_rx_nob_left,
1672                        cfs_duration_sec(cfs_time_sub(cfs_time_current(),
1673                                         last_rcv)));
1674                 lnet_finalize (conn->ksnc_peer->ksnp_ni,
1675                                conn->ksnc_cookie, -EIO);
1676                 break;
1677         case SOCKNAL_RX_LNET_HEADER:
1678                 if (conn->ksnc_rx_started)
1679                         CERROR("Incomplete receive of lnet header from %s"
1680                                ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1681                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1682                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1683                                conn->ksnc_proto->pro_version);
1684                 break;
1685         case SOCKNAL_RX_KSM_HEADER:
1686                 if (conn->ksnc_rx_started)
1687                         CERROR("Incomplete receive of ksock message from %s"
1688                                ", ip %d.%d.%d.%d:%d, with error, protocol: %d.x.\n",
1689                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1690                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port,
1691                                conn->ksnc_proto->pro_version);
1692                 break;
1693         case SOCKNAL_RX_SLOP:
1694                 if (conn->ksnc_rx_started)
1695                         CERROR("Incomplete receive of slops from %s"
1696                                ", ip %d.%d.%d.%d:%d, with error\n",
1697                                libcfs_id2str(conn->ksnc_peer->ksnp_id),
1698                                HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1699                break;
1700         default:
1701                 LBUG ();
1702                 break;
1703         }
1704
1705         ksocknal_peer_decref(conn->ksnc_peer);
1706
1707         LIBCFS_FREE (conn, sizeof (*conn));
1708 }
1709
1710 int
1711 ksocknal_close_peer_conns_locked (ksock_peer_t *peer, __u32 ipaddr, int why)
1712 {
1713         ksock_conn_t       *conn;
1714         cfs_list_t         *ctmp;
1715         cfs_list_t         *cnxt;
1716         int                 count = 0;
1717
1718         cfs_list_for_each_safe (ctmp, cnxt, &peer->ksnp_conns) {
1719                 conn = cfs_list_entry (ctmp, ksock_conn_t, ksnc_list);
1720
1721                 if (ipaddr == 0 ||
1722                     conn->ksnc_ipaddr == ipaddr) {
1723                         count++;
1724                         ksocknal_close_conn_locked (conn, why);
1725                 }
1726         }
1727
1728         return (count);
1729 }
1730
1731 int
1732 ksocknal_close_conn_and_siblings (ksock_conn_t *conn, int why)
1733 {
1734         ksock_peer_t     *peer = conn->ksnc_peer;
1735         __u32             ipaddr = conn->ksnc_ipaddr;
1736         int               count;
1737
1738         cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1739
1740         count = ksocknal_close_peer_conns_locked (peer, ipaddr, why);
1741
1742         cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1743
1744         return (count);
1745 }
1746
1747 int
1748 ksocknal_close_matching_conns (lnet_process_id_t id, __u32 ipaddr)
1749 {
1750         ksock_peer_t       *peer;
1751         cfs_list_t         *ptmp;
1752         cfs_list_t         *pnxt;
1753         int                 lo;
1754         int                 hi;
1755         int                 i;
1756         int                 count = 0;
1757
1758         cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1759
1760         if (id.nid != LNET_NID_ANY)
1761                 lo = hi = (int)(ksocknal_nid2peerlist(id.nid) - ksocknal_data.ksnd_peers);
1762         else {
1763                 lo = 0;
1764                 hi = ksocknal_data.ksnd_peer_hash_size - 1;
1765         }
1766
1767         for (i = lo; i <= hi; i++) {
1768                 cfs_list_for_each_safe (ptmp, pnxt,
1769                                         &ksocknal_data.ksnd_peers[i]) {
1770
1771                         peer = cfs_list_entry (ptmp, ksock_peer_t, ksnp_list);
1772
1773                         if (!((id.nid == LNET_NID_ANY || id.nid == peer->ksnp_id.nid) &&
1774                               (id.pid == LNET_PID_ANY || id.pid == peer->ksnp_id.pid)))
1775                                 continue;
1776
1777                         count += ksocknal_close_peer_conns_locked (peer, ipaddr, 0);
1778                 }
1779         }
1780
1781         cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1782
1783         /* wildcards always succeed */
1784         if (id.nid == LNET_NID_ANY || id.pid == LNET_PID_ANY || ipaddr == 0)
1785                 return (0);
1786
1787         return (count == 0 ? -ENOENT : 0);
1788 }
1789
1790 void
1791 ksocknal_notify (lnet_ni_t *ni, lnet_nid_t gw_nid, int alive)
1792 {
1793         /* The router is telling me she's been notified of a change in
1794          * gateway state.... */
1795         lnet_process_id_t  id = {0};
1796
1797         id.nid = gw_nid;
1798         id.pid = LNET_PID_ANY;
1799
1800         CDEBUG (D_NET, "gw %s %s\n", libcfs_nid2str(gw_nid),
1801                 alive ? "up" : "down");
1802
1803         if (!alive) {
1804                 /* If the gateway crashed, close all open connections... */
1805                 ksocknal_close_matching_conns (id, 0);
1806                 return;
1807         }
1808
1809         /* ...otherwise do nothing.  We can only establish new connections
1810          * if we have autroutes, and these connect on demand. */
1811 }
1812
1813 void
1814 ksocknal_query (lnet_ni_t *ni, lnet_nid_t nid, cfs_time_t *when)
1815 {
1816         int                connect = 1;
1817         cfs_time_t         last_alive = 0;
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 = cfs_time_current();
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         if (!connect)
1854                 return;
1855
1856         ksocknal_add_peer(ni, id, LNET_NIDADDR(nid), lnet_acceptor_port());
1857
1858         cfs_write_lock_bh(glock);
1859
1860         peer = ksocknal_find_peer_locked(ni, id);
1861         if (peer != NULL)
1862                 ksocknal_launch_all_connections_locked(peer);
1863
1864         cfs_write_unlock_bh(glock);
1865         return;
1866 }
1867
1868 void
1869 ksocknal_push_peer (ksock_peer_t *peer)
1870 {
1871         int               index;
1872         int               i;
1873         cfs_list_t       *tmp;
1874         ksock_conn_t     *conn;
1875
1876         for (index = 0; ; index++) {
1877                 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1878
1879                 i = 0;
1880                 conn = NULL;
1881
1882                 cfs_list_for_each (tmp, &peer->ksnp_conns) {
1883                         if (i++ == index) {
1884                                 conn = cfs_list_entry (tmp, ksock_conn_t,
1885                                                        ksnc_list);
1886                                 ksocknal_conn_addref(conn);
1887                                 break;
1888                         }
1889                 }
1890
1891                 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1892
1893                 if (conn == NULL)
1894                         break;
1895
1896                 ksocknal_lib_push_conn (conn);
1897                 ksocknal_conn_decref(conn);
1898         }
1899 }
1900
1901 int
1902 ksocknal_push (lnet_ni_t *ni, lnet_process_id_t id)
1903 {
1904         ksock_peer_t      *peer;
1905         cfs_list_t        *tmp;
1906         int                index;
1907         int                i;
1908         int                j;
1909         int                rc = -ENOENT;
1910
1911         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1912                 for (j = 0; ; j++) {
1913                         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
1914
1915                         index = 0;
1916                         peer = NULL;
1917
1918                         cfs_list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
1919                                 peer = cfs_list_entry(tmp, ksock_peer_t,
1920                                                       ksnp_list);
1921
1922                                 if (!((id.nid == LNET_NID_ANY ||
1923                                        id.nid == peer->ksnp_id.nid) &&
1924                                       (id.pid == LNET_PID_ANY ||
1925                                        id.pid == peer->ksnp_id.pid))) {
1926                                         peer = NULL;
1927                                         continue;
1928                                 }
1929
1930                                 if (index++ == j) {
1931                                         ksocknal_peer_addref(peer);
1932                                         break;
1933                                 }
1934                         }
1935
1936                         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
1937
1938                         if (peer != NULL) {
1939                                 rc = 0;
1940                                 ksocknal_push_peer (peer);
1941                                 ksocknal_peer_decref(peer);
1942                         }
1943                 }
1944
1945         }
1946
1947         return (rc);
1948 }
1949
1950 int
1951 ksocknal_add_interface(lnet_ni_t *ni, __u32 ipaddress, __u32 netmask)
1952 {
1953         ksock_net_t       *net = ni->ni_data;
1954         ksock_interface_t *iface;
1955         int                rc;
1956         int                i;
1957         int                j;
1958         cfs_list_t        *ptmp;
1959         ksock_peer_t      *peer;
1960         cfs_list_t        *rtmp;
1961         ksock_route_t     *route;
1962
1963         if (ipaddress == 0 ||
1964             netmask == 0)
1965                 return (-EINVAL);
1966
1967         cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1968
1969         iface = ksocknal_ip2iface(ni, ipaddress);
1970         if (iface != NULL) {
1971                 /* silently ignore dups */
1972                 rc = 0;
1973         } else if (net->ksnn_ninterfaces == LNET_MAX_INTERFACES) {
1974                 rc = -ENOSPC;
1975         } else {
1976                 iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
1977
1978                 iface->ksni_ipaddr = ipaddress;
1979                 iface->ksni_netmask = netmask;
1980                 iface->ksni_nroutes = 0;
1981                 iface->ksni_npeers = 0;
1982
1983                 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
1984                         cfs_list_for_each(ptmp, &ksocknal_data.ksnd_peers[i]) {
1985                                 peer = cfs_list_entry(ptmp, ksock_peer_t,
1986                                                       ksnp_list);
1987
1988                                 for (j = 0; j < peer->ksnp_n_passive_ips; j++)
1989                                         if (peer->ksnp_passive_ips[j] == ipaddress)
1990                                                 iface->ksni_npeers++;
1991
1992                                 cfs_list_for_each(rtmp, &peer->ksnp_routes) {
1993                                         route = cfs_list_entry(rtmp,
1994                                                                ksock_route_t,
1995                                                                ksnr_list);
1996
1997                                         if (route->ksnr_myipaddr == ipaddress)
1998                                                 iface->ksni_nroutes++;
1999                                 }
2000                         }
2001                 }
2002
2003                 rc = 0;
2004                 /* NB only new connections will pay attention to the new interface! */
2005         }
2006
2007         cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2008
2009         return (rc);
2010 }
2011
2012 void
2013 ksocknal_peer_del_interface_locked(ksock_peer_t *peer, __u32 ipaddr)
2014 {
2015         cfs_list_t         *tmp;
2016         cfs_list_t         *nxt;
2017         ksock_route_t      *route;
2018         ksock_conn_t       *conn;
2019         int                 i;
2020         int                 j;
2021
2022         for (i = 0; i < peer->ksnp_n_passive_ips; i++)
2023                 if (peer->ksnp_passive_ips[i] == ipaddr) {
2024                         for (j = i+1; j < peer->ksnp_n_passive_ips; j++)
2025                                 peer->ksnp_passive_ips[j-1] =
2026                                         peer->ksnp_passive_ips[j];
2027                         peer->ksnp_n_passive_ips--;
2028                         break;
2029                 }
2030
2031         cfs_list_for_each_safe(tmp, nxt, &peer->ksnp_routes) {
2032                 route = cfs_list_entry (tmp, ksock_route_t, ksnr_list);
2033
2034                 if (route->ksnr_myipaddr != ipaddr)
2035                         continue;
2036
2037                 if (route->ksnr_share_count != 0) {
2038                         /* Manually created; keep, but unbind */
2039                         route->ksnr_myipaddr = 0;
2040                 } else {
2041                         ksocknal_del_route_locked(route);
2042                 }
2043         }
2044
2045         cfs_list_for_each_safe(tmp, nxt, &peer->ksnp_conns) {
2046                 conn = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
2047
2048                 if (conn->ksnc_myipaddr == ipaddr)
2049                         ksocknal_close_conn_locked (conn, 0);
2050         }
2051 }
2052
2053 int
2054 ksocknal_del_interface(lnet_ni_t *ni, __u32 ipaddress)
2055 {
2056         ksock_net_t       *net = ni->ni_data;
2057         int                rc = -ENOENT;
2058         cfs_list_t        *tmp;
2059         cfs_list_t        *nxt;
2060         ksock_peer_t      *peer;
2061         __u32              this_ip;
2062         int                i;
2063         int                j;
2064
2065         cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
2066
2067         for (i = 0; i < net->ksnn_ninterfaces; i++) {
2068                 this_ip = net->ksnn_interfaces[i].ksni_ipaddr;
2069
2070                 if (!(ipaddress == 0 ||
2071                       ipaddress == this_ip))
2072                         continue;
2073
2074                 rc = 0;
2075
2076                 for (j = i+1; j < net->ksnn_ninterfaces; j++)
2077                         net->ksnn_interfaces[j-1] =
2078                                 net->ksnn_interfaces[j];
2079
2080                 net->ksnn_ninterfaces--;
2081
2082                 for (j = 0; j < ksocknal_data.ksnd_peer_hash_size; j++) {
2083                         cfs_list_for_each_safe(tmp, nxt,
2084                                                &ksocknal_data.ksnd_peers[j]) {
2085                                 peer = cfs_list_entry(tmp, ksock_peer_t,
2086                                                       ksnp_list);
2087
2088                                 if (peer->ksnp_ni != ni)
2089                                         continue;
2090
2091                                 ksocknal_peer_del_interface_locked(peer, this_ip);
2092                         }
2093                 }
2094         }
2095
2096         cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2097
2098         return (rc);
2099 }
2100
2101 int
2102 ksocknal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
2103 {
2104         lnet_process_id_t id = {0}; 
2105         struct libcfs_ioctl_data *data = arg;
2106         int rc;
2107
2108         switch(cmd) {
2109         case IOC_LIBCFS_GET_INTERFACE: {
2110                 ksock_net_t       *net = ni->ni_data;
2111                 ksock_interface_t *iface;
2112
2113                 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2114
2115                 if (data->ioc_count >= (__u32)net->ksnn_ninterfaces) {
2116                         rc = -ENOENT;
2117                 } else {
2118                         rc = 0;
2119                         iface = &net->ksnn_interfaces[data->ioc_count];
2120
2121                         data->ioc_u32[0] = iface->ksni_ipaddr;
2122                         data->ioc_u32[1] = iface->ksni_netmask;
2123                         data->ioc_u32[2] = iface->ksni_npeers;
2124                         data->ioc_u32[3] = iface->ksni_nroutes;
2125                 }
2126
2127                 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2128                 return rc;
2129         }
2130
2131         case IOC_LIBCFS_ADD_INTERFACE:
2132                 return ksocknal_add_interface(ni,
2133                                               data->ioc_u32[0], /* IP address */
2134                                               data->ioc_u32[1]); /* net mask */
2135
2136         case IOC_LIBCFS_DEL_INTERFACE:
2137                 return ksocknal_del_interface(ni,
2138                                               data->ioc_u32[0]); /* IP address */
2139
2140         case IOC_LIBCFS_GET_PEER: {
2141                 __u32            myip = 0;
2142                 __u32            ip = 0;
2143                 int              port = 0;
2144                 int              conn_count = 0;
2145                 int              share_count = 0;
2146
2147                 rc = ksocknal_get_peer_info(ni, data->ioc_count,
2148                                             &id, &myip, &ip, &port,
2149                                             &conn_count,  &share_count);
2150                 if (rc != 0)
2151                         return rc;
2152
2153                 data->ioc_nid    = id.nid;
2154                 data->ioc_count  = share_count;
2155                 data->ioc_u32[0] = ip;
2156                 data->ioc_u32[1] = port;
2157                 data->ioc_u32[2] = myip;
2158                 data->ioc_u32[3] = conn_count;
2159                 data->ioc_u32[4] = id.pid;
2160                 return 0;
2161         }
2162
2163         case IOC_LIBCFS_ADD_PEER:
2164                 id.nid = data->ioc_nid;
2165                 id.pid = LUSTRE_SRV_LNET_PID;
2166                 return ksocknal_add_peer (ni, id,
2167                                           data->ioc_u32[0], /* IP */
2168                                           data->ioc_u32[1]); /* port */
2169
2170         case IOC_LIBCFS_DEL_PEER:
2171                 id.nid = data->ioc_nid;
2172                 id.pid = LNET_PID_ANY;
2173                 return ksocknal_del_peer (ni, id,
2174                                           data->ioc_u32[0]); /* IP */
2175
2176         case IOC_LIBCFS_GET_CONN: {
2177                 int           txmem;
2178                 int           rxmem;
2179                 int           nagle;
2180                 ksock_conn_t *conn = ksocknal_get_conn_by_idx (ni, data->ioc_count);
2181
2182                 if (conn == NULL)
2183                         return -ENOENT;
2184
2185                 ksocknal_lib_get_conn_tunables(conn, &txmem, &rxmem, &nagle);
2186
2187                 data->ioc_count  = txmem;
2188                 data->ioc_nid    = conn->ksnc_peer->ksnp_id.nid;
2189                 data->ioc_flags  = nagle;
2190                 data->ioc_u32[0] = conn->ksnc_ipaddr;
2191                 data->ioc_u32[1] = conn->ksnc_port;
2192                 data->ioc_u32[2] = conn->ksnc_myipaddr;
2193                 data->ioc_u32[3] = conn->ksnc_type;
2194                 data->ioc_u32[4] = (__u32)(conn->ksnc_scheduler -
2195                                    ksocknal_data.ksnd_schedulers);
2196                 data->ioc_u32[5] = rxmem;
2197                 data->ioc_u32[6] = conn->ksnc_peer->ksnp_id.pid;
2198                 ksocknal_conn_decref(conn);
2199                 return 0;
2200         }
2201
2202         case IOC_LIBCFS_CLOSE_CONNECTION:
2203                 id.nid = data->ioc_nid;
2204                 id.pid = LNET_PID_ANY;
2205                 return ksocknal_close_matching_conns (id,
2206                                                       data->ioc_u32[0]);
2207
2208         case IOC_LIBCFS_REGISTER_MYNID:
2209                 /* Ignore if this is a noop */
2210                 if (data->ioc_nid == ni->ni_nid)
2211                         return 0;
2212
2213                 CERROR("obsolete IOC_LIBCFS_REGISTER_MYNID: %s(%s)\n",
2214                        libcfs_nid2str(data->ioc_nid),
2215                        libcfs_nid2str(ni->ni_nid));
2216                 return -EINVAL;
2217
2218         case IOC_LIBCFS_PUSH_CONNECTION:
2219                 id.nid = data->ioc_nid;
2220                 id.pid = LNET_PID_ANY;
2221                 return ksocknal_push(ni, id);
2222
2223         default:
2224                 return -EINVAL;
2225         }
2226         /* not reached */
2227 }
2228
2229 void
2230 ksocknal_free_buffers (void)
2231 {
2232         LASSERT (cfs_atomic_read(&ksocknal_data.ksnd_nactive_txs) == 0);
2233
2234         if (ksocknal_data.ksnd_schedulers != NULL)
2235                 LIBCFS_FREE (ksocknal_data.ksnd_schedulers,
2236                              sizeof (ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2237
2238         LIBCFS_FREE (ksocknal_data.ksnd_peers,
2239                      sizeof (cfs_list_t) *
2240                      ksocknal_data.ksnd_peer_hash_size);
2241
2242         cfs_spin_lock(&ksocknal_data.ksnd_tx_lock);
2243
2244         if (!cfs_list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
2245                 cfs_list_t        zlist;
2246                 ksock_tx_t       *tx;
2247
2248                 cfs_list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
2249                 cfs_list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
2250                 cfs_spin_unlock(&ksocknal_data.ksnd_tx_lock);
2251
2252                 while(!cfs_list_empty(&zlist)) {
2253                         tx = cfs_list_entry(zlist.next, ksock_tx_t, tx_list);
2254                         cfs_list_del(&tx->tx_list);
2255                         LIBCFS_FREE(tx, tx->tx_desc_size);
2256                 }
2257         } else {
2258                 cfs_spin_unlock(&ksocknal_data.ksnd_tx_lock);
2259         }
2260 }
2261
2262 void
2263 ksocknal_base_shutdown (void)
2264 {
2265         ksock_sched_t *sched;
2266         int            i;
2267
2268         CDEBUG(D_MALLOC, "before NAL cleanup: kmem %d\n",
2269                cfs_atomic_read (&libcfs_kmemory));
2270         LASSERT (ksocknal_data.ksnd_nnets == 0);
2271
2272         switch (ksocknal_data.ksnd_init) {
2273         default:
2274                 LASSERT (0);
2275
2276         case SOCKNAL_INIT_ALL:
2277         case SOCKNAL_INIT_DATA:
2278                 LASSERT (ksocknal_data.ksnd_peers != NULL);
2279                 for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2280                         LASSERT (cfs_list_empty (&ksocknal_data.ksnd_peers[i]));
2281                 }
2282                 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_enomem_conns));
2283                 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_zombie_conns));
2284                 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_connd_connreqs));
2285                 LASSERT (cfs_list_empty (&ksocknal_data.ksnd_connd_routes));
2286
2287                 if (ksocknal_data.ksnd_schedulers != NULL)
2288                         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2289                                 ksock_sched_t *kss =
2290                                         &ksocknal_data.ksnd_schedulers[i];
2291
2292                                 LASSERT (cfs_list_empty (&kss->kss_tx_conns));
2293                                 LASSERT (cfs_list_empty (&kss->kss_rx_conns));
2294                                 LASSERT (cfs_list_empty (&kss-> \
2295                                                          kss_zombie_noop_txs));
2296                                 LASSERT (kss->kss_nconns == 0);
2297                         }
2298
2299                 /* flag threads to terminate; wake and wait for them to die */
2300                 ksocknal_data.ksnd_shuttingdown = 1;
2301                 cfs_waitq_broadcast (&ksocknal_data.ksnd_connd_waitq);
2302                 cfs_waitq_broadcast (&ksocknal_data.ksnd_reaper_waitq);
2303
2304                 if (ksocknal_data.ksnd_schedulers != NULL)
2305                         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2306                                 sched = &ksocknal_data.ksnd_schedulers[i];
2307                                 cfs_waitq_broadcast(&sched->kss_waitq);
2308                         }
2309
2310                 i = 4;
2311                 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2312                 while (ksocknal_data.ksnd_nthreads != 0) {
2313                         i++;
2314                         CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2315                                "waiting for %d threads to terminate\n",
2316                                 ksocknal_data.ksnd_nthreads);
2317                         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2318                         cfs_pause(cfs_time_seconds(1));
2319                         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2320                 }
2321                 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2322
2323                 ksocknal_free_buffers();
2324
2325                 ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
2326                 break;
2327         }
2328
2329         CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
2330                cfs_atomic_read (&libcfs_kmemory));
2331
2332         PORTAL_MODULE_UNUSE;
2333 }
2334
2335 __u64
2336 ksocknal_new_incarnation (void)
2337 {
2338         struct timeval tv;
2339
2340         /* The incarnation number is the time this module loaded and it
2341          * identifies this particular instance of the socknal.  Hopefully
2342          * we won't be able to reboot more frequently than 1MHz for the
2343          * forseeable future :) */
2344
2345         cfs_gettimeofday(&tv);
2346
2347         return (((__u64)tv.tv_sec) * 1000000) + tv.tv_usec;
2348 }
2349
2350 int
2351 ksocknal_base_startup (void)
2352 {
2353         int               rc;
2354         int               i;
2355
2356         LASSERT (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING);
2357         LASSERT (ksocknal_data.ksnd_nnets == 0);
2358
2359         memset (&ksocknal_data, 0, sizeof (ksocknal_data)); /* zero pointers */
2360
2361         ksocknal_data.ksnd_peer_hash_size = SOCKNAL_PEER_HASH_SIZE;
2362         LIBCFS_ALLOC (ksocknal_data.ksnd_peers,
2363                       sizeof (cfs_list_t) *
2364                       ksocknal_data.ksnd_peer_hash_size);
2365         if (ksocknal_data.ksnd_peers == NULL)
2366                 return -ENOMEM;
2367
2368         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++)
2369                 CFS_INIT_LIST_HEAD(&ksocknal_data.ksnd_peers[i]);
2370
2371         cfs_rwlock_init(&ksocknal_data.ksnd_global_lock);
2372
2373         cfs_spin_lock_init (&ksocknal_data.ksnd_reaper_lock);
2374         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_enomem_conns);
2375         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_zombie_conns);
2376         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_deathrow_conns);
2377         cfs_waitq_init(&ksocknal_data.ksnd_reaper_waitq);
2378
2379         cfs_spin_lock_init (&ksocknal_data.ksnd_connd_lock);
2380         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_connreqs);
2381         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_connd_routes);
2382         cfs_waitq_init(&ksocknal_data.ksnd_connd_waitq);
2383
2384         cfs_spin_lock_init (&ksocknal_data.ksnd_tx_lock);
2385         CFS_INIT_LIST_HEAD (&ksocknal_data.ksnd_idle_noop_txs);
2386
2387         /* NB memset above zeros whole of ksocknal_data, including
2388          * ksocknal_data.ksnd_irqinfo[all].ksni_valid */
2389
2390         /* flag lists/ptrs/locks initialised */
2391         ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
2392         PORTAL_MODULE_USE;
2393
2394         ksocknal_data.ksnd_nschedulers = ksocknal_nsched();
2395         LIBCFS_ALLOC(ksocknal_data.ksnd_schedulers,
2396                      sizeof(ksock_sched_t) * ksocknal_data.ksnd_nschedulers);
2397         if (ksocknal_data.ksnd_schedulers == NULL)
2398                 goto failed;
2399
2400         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2401                 ksock_sched_t *kss = &ksocknal_data.ksnd_schedulers[i];
2402
2403                 cfs_spin_lock_init (&kss->kss_lock);
2404                 CFS_INIT_LIST_HEAD (&kss->kss_rx_conns);
2405                 CFS_INIT_LIST_HEAD (&kss->kss_tx_conns);
2406                 CFS_INIT_LIST_HEAD (&kss->kss_zombie_noop_txs);
2407                 cfs_waitq_init (&kss->kss_waitq);
2408         }
2409
2410         for (i = 0; i < ksocknal_data.ksnd_nschedulers; i++) {
2411                 rc = ksocknal_thread_start (ksocknal_scheduler,
2412                                             &ksocknal_data.ksnd_schedulers[i]);
2413                 if (rc != 0) {
2414                         CERROR("Can't spawn socknal scheduler[%d]: %d\n",
2415                                i, rc);
2416                         goto failed;
2417                 }
2418         }
2419
2420         /* must have at least 2 connds to remain responsive to accepts while
2421          * connecting */
2422         if (*ksocknal_tunables.ksnd_nconnds < 2)
2423                 *ksocknal_tunables.ksnd_nconnds = 2;
2424
2425         for (i = 0; i < *ksocknal_tunables.ksnd_nconnds; i++) {
2426                 rc = ksocknal_thread_start (ksocknal_connd,
2427                                             (void *)((ulong_ptr_t)i));
2428                 if (rc != 0) {
2429                         CERROR("Can't spawn socknal connd: %d\n", rc);
2430                         goto failed;
2431                 }
2432         }
2433
2434         rc = ksocknal_thread_start (ksocknal_reaper, NULL);
2435         if (rc != 0) {
2436                 CERROR ("Can't spawn socknal reaper: %d\n", rc);
2437                 goto failed;
2438         }
2439
2440         /* flag everything initialised */
2441         ksocknal_data.ksnd_init = SOCKNAL_INIT_ALL;
2442
2443         return 0;
2444
2445  failed:
2446         ksocknal_base_shutdown();
2447         return -ENETDOWN;
2448 }
2449
2450 void
2451 ksocknal_debug_peerhash (lnet_ni_t *ni)
2452 {
2453         ksock_peer_t     *peer = NULL;
2454         cfs_list_t       *tmp;
2455         int               i;
2456
2457         cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2458
2459         for (i = 0; i < ksocknal_data.ksnd_peer_hash_size; i++) {
2460                 cfs_list_for_each (tmp, &ksocknal_data.ksnd_peers[i]) {
2461                         peer = cfs_list_entry (tmp, ksock_peer_t, ksnp_list);
2462
2463                         if (peer->ksnp_ni == ni) break;
2464
2465                         peer = NULL;
2466                 }
2467         }
2468
2469         if (peer != NULL) {
2470                 ksock_route_t *route;
2471                 ksock_conn_t  *conn;
2472
2473                 CWARN ("Active peer on shutdown: %s, ref %d, scnt %d, "
2474                        "closing %d, accepting %d, err %d, zcookie "LPU64", "
2475                        "txq %d, zc_req %d\n", libcfs_id2str(peer->ksnp_id),
2476                        cfs_atomic_read(&peer->ksnp_refcount),
2477                        peer->ksnp_sharecount, peer->ksnp_closing,
2478                        peer->ksnp_accepting, peer->ksnp_error,
2479                        peer->ksnp_zc_next_cookie,
2480                        !cfs_list_empty(&peer->ksnp_tx_queue),
2481                        !cfs_list_empty(&peer->ksnp_zc_req_list));
2482
2483                 cfs_list_for_each (tmp, &peer->ksnp_routes) {
2484                         route = cfs_list_entry(tmp, ksock_route_t, ksnr_list);
2485                         CWARN ("Route: ref %d, schd %d, conn %d, cnted %d, "
2486                                "del %d\n", cfs_atomic_read(&route->ksnr_refcount),
2487                                route->ksnr_scheduled, route->ksnr_connecting,
2488                                route->ksnr_connected, route->ksnr_deleted);
2489                 }
2490
2491                 cfs_list_for_each (tmp, &peer->ksnp_conns) {
2492                         conn = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
2493                         CWARN ("Conn: ref %d, sref %d, t %d, c %d\n",
2494                                cfs_atomic_read(&conn->ksnc_conn_refcount),
2495                                cfs_atomic_read(&conn->ksnc_sock_refcount),
2496                                conn->ksnc_type, conn->ksnc_closing);
2497                 }
2498         }
2499
2500         cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2501         return;
2502 }
2503
2504 void
2505 ksocknal_shutdown (lnet_ni_t *ni)
2506 {
2507         ksock_net_t      *net = ni->ni_data;
2508         int               i;
2509         lnet_process_id_t anyid = {0};
2510
2511         anyid.nid =  LNET_NID_ANY;
2512         anyid.pid =  LNET_PID_ANY;
2513
2514         LASSERT(ksocknal_data.ksnd_init == SOCKNAL_INIT_ALL);
2515         LASSERT(ksocknal_data.ksnd_nnets > 0);
2516
2517         cfs_spin_lock_bh (&net->ksnn_lock);
2518         net->ksnn_shutdown = 1;                 /* prevent new peers */
2519         cfs_spin_unlock_bh (&net->ksnn_lock);
2520
2521         /* Delete all peers */
2522         ksocknal_del_peer(ni, anyid, 0);
2523
2524         /* Wait for all peer state to clean up */
2525         i = 2;
2526         cfs_spin_lock_bh (&net->ksnn_lock);
2527         while (net->ksnn_npeers != 0) {
2528                 cfs_spin_unlock_bh (&net->ksnn_lock);
2529
2530                 i++;
2531                 CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
2532                        "waiting for %d peers to disconnect\n",
2533                        net->ksnn_npeers);
2534                 cfs_pause(cfs_time_seconds(1));
2535
2536                 ksocknal_debug_peerhash(ni);
2537
2538                 cfs_spin_lock_bh (&net->ksnn_lock);
2539         }
2540         cfs_spin_unlock_bh (&net->ksnn_lock);
2541
2542         for (i = 0; i < net->ksnn_ninterfaces; i++) {
2543                 LASSERT (net->ksnn_interfaces[i].ksni_npeers == 0);
2544                 LASSERT (net->ksnn_interfaces[i].ksni_nroutes == 0);
2545         }
2546
2547         LIBCFS_FREE(net, sizeof(*net));
2548
2549         ksocknal_data.ksnd_nnets--;
2550         if (ksocknal_data.ksnd_nnets == 0)
2551                 ksocknal_base_shutdown();
2552 }
2553
2554 int
2555 ksocknal_enumerate_interfaces(ksock_net_t *net)
2556 {
2557         char      **names;
2558         int         i;
2559         int         j;
2560         int         rc;
2561         int         n;
2562
2563         n = libcfs_ipif_enumerate(&names);
2564         if (n <= 0) {
2565                 CERROR("Can't enumerate interfaces: %d\n", n);
2566                 return n;
2567         }
2568
2569         for (i = j = 0; i < n; i++) {
2570                 int        up;
2571                 __u32      ip;
2572                 __u32      mask;
2573
2574                 if (!strcmp(names[i], "lo")) /* skip the loopback IF */
2575                         continue;
2576
2577                 rc = libcfs_ipif_query(names[i], &up, &ip, &mask);
2578                 if (rc != 0) {
2579                         CWARN("Can't get interface %s info: %d\n",
2580                               names[i], rc);
2581                         continue;
2582                 }
2583
2584                 if (!up) {
2585                         CWARN("Ignoring interface %s (down)\n",
2586                               names[i]);
2587                         continue;
2588                 }
2589
2590                 if (j == LNET_MAX_INTERFACES) {
2591                         CWARN("Ignoring interface %s (too many interfaces)\n",
2592                               names[i]);
2593                         continue;
2594                 }
2595
2596                 net->ksnn_interfaces[j].ksni_ipaddr = ip;
2597                 net->ksnn_interfaces[j].ksni_netmask = mask;
2598                 j++;
2599         }
2600
2601         libcfs_ipif_free_enumeration(names, n);
2602
2603         if (j == 0)
2604                 CERROR("Can't find any usable interfaces\n");
2605
2606         return j;
2607 }
2608
2609 int
2610 ksocknal_startup (lnet_ni_t *ni)
2611 {
2612         ksock_net_t  *net;
2613         int           rc;
2614         int           i;
2615
2616         LASSERT (ni->ni_lnd == &the_ksocklnd);
2617
2618         if (ksocknal_data.ksnd_init == SOCKNAL_INIT_NOTHING) {
2619                 rc = ksocknal_base_startup();
2620                 if (rc != 0)
2621                         return rc;
2622         }
2623
2624         LIBCFS_ALLOC(net, sizeof(*net));
2625         if (net == NULL)
2626                 goto fail_0;
2627
2628         memset(net, 0, sizeof(*net));
2629         cfs_spin_lock_init(&net->ksnn_lock);
2630         net->ksnn_incarnation = ksocknal_new_incarnation();
2631         ni->ni_data = net;
2632         ni->ni_peertimeout    = *ksocknal_tunables.ksnd_peertimeout;
2633         ni->ni_maxtxcredits   = *ksocknal_tunables.ksnd_credits;
2634         ni->ni_peertxcredits  = *ksocknal_tunables.ksnd_peertxcredits;
2635         ni->ni_peerrtrcredits = *ksocknal_tunables.ksnd_peerrtrcredits;
2636
2637         if (ni->ni_interfaces[0] == NULL) {
2638                 rc = ksocknal_enumerate_interfaces(net);
2639                 if (rc <= 0)
2640                         goto fail_1;
2641
2642                 net->ksnn_ninterfaces = 1;
2643         } else {
2644                 for (i = 0; i < LNET_MAX_INTERFACES; i++) {
2645                         int    up;
2646
2647                         if (ni->ni_interfaces[i] == NULL)
2648                                 break;
2649
2650                         rc = libcfs_ipif_query(
2651                                 ni->ni_interfaces[i], &up,
2652                                 &net->ksnn_interfaces[i].ksni_ipaddr,
2653                                 &net->ksnn_interfaces[i].ksni_netmask);
2654
2655                         if (rc != 0) {
2656                                 CERROR("Can't get interface %s info: %d\n",
2657                                        ni->ni_interfaces[i], rc);
2658                                 goto fail_1;
2659                         }
2660
2661                         if (!up) {
2662                                 CERROR("Interface %s is down\n",
2663                                        ni->ni_interfaces[i]);
2664                                 goto fail_1;
2665                         }
2666                 }
2667                 net->ksnn_ninterfaces = i;
2668         }
2669
2670         ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid),
2671                                 net->ksnn_interfaces[0].ksni_ipaddr);
2672
2673         ksocknal_data.ksnd_nnets++;
2674
2675         return 0;
2676
2677  fail_1:
2678         LIBCFS_FREE(net, sizeof(*net));
2679  fail_0:
2680         if (ksocknal_data.ksnd_nnets == 0)
2681                 ksocknal_base_shutdown();
2682
2683         return -ENETDOWN;
2684 }
2685
2686
2687 void __exit
2688 ksocknal_module_fini (void)
2689 {
2690         lnet_unregister_lnd(&the_ksocklnd);
2691         ksocknal_tunables_fini();
2692 }
2693
2694 int __init
2695 ksocknal_module_init (void)
2696 {
2697         int    rc;
2698
2699         /* check ksnr_connected/connecting field large enough */
2700         CLASSERT (SOCKLND_CONN_NTYPES <= 4);
2701         CLASSERT (SOCKLND_CONN_ACK == SOCKLND_CONN_BULK_IN);
2702
2703         /* initialize the_ksocklnd */
2704         the_ksocklnd.lnd_type     = SOCKLND;
2705         the_ksocklnd.lnd_startup  = ksocknal_startup;
2706         the_ksocklnd.lnd_shutdown = ksocknal_shutdown;
2707         the_ksocklnd.lnd_ctl      = ksocknal_ctl;
2708         the_ksocklnd.lnd_send     = ksocknal_send;
2709         the_ksocklnd.lnd_recv     = ksocknal_recv;
2710         the_ksocklnd.lnd_notify   = ksocknal_notify;
2711         the_ksocklnd.lnd_query    = ksocknal_query;
2712         the_ksocklnd.lnd_accept   = ksocknal_accept;
2713
2714         rc = ksocknal_tunables_init();
2715         if (rc != 0)
2716                 return rc;
2717
2718         lnet_register_lnd(&the_ksocklnd);
2719
2720         return 0;
2721 }
2722
2723 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2724 MODULE_DESCRIPTION("Kernel TCP Socket LND v3.0.0");
2725 MODULE_LICENSE("GPL");
2726
2727 cfs_module(ksocknal, "3.0.0", ksocknal_module_init, ksocknal_module_fini);