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