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