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