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