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