Whamcloud - gitweb
37bc81126c6744ba1ec88a5237c63b9f2b6e07f4
[fs/lustre-release.git] / lnet / lnet / router.c
1 /*
2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2011, 2017, Intel Corporation.
5  *
6  *   This file is part of Lustre, https://wiki.whamcloud.com/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_LNET
24
25 #include <linux/random.h>
26 #include <lnet/lib-lnet.h>
27
28 #define LNET_NRB_TINY_MIN       512     /* min value for each CPT */
29 #define LNET_NRB_TINY           (LNET_NRB_TINY_MIN * 4)
30 #define LNET_NRB_SMALL_MIN      4096    /* min value for each CPT */
31 #define LNET_NRB_SMALL          (LNET_NRB_SMALL_MIN * 4)
32 #define LNET_NRB_SMALL_PAGES    1
33 #define LNET_NRB_LARGE_MIN      256     /* min value for each CPT */
34 #define LNET_NRB_LARGE          (LNET_NRB_LARGE_MIN * 4)
35 #define LNET_NRB_LARGE_PAGES    ((LNET_MTU + PAGE_SIZE - 1) >> \
36                                   PAGE_SHIFT)
37
38 static char *forwarding = "";
39 module_param(forwarding, charp, 0444);
40 MODULE_PARM_DESC(forwarding, "Explicitly enable/disable forwarding between networks");
41
42 static int tiny_router_buffers;
43 module_param(tiny_router_buffers, int, 0444);
44 MODULE_PARM_DESC(tiny_router_buffers, "# of 0 payload messages to buffer in the router");
45 static int small_router_buffers;
46 module_param(small_router_buffers, int, 0444);
47 MODULE_PARM_DESC(small_router_buffers, "# of small (1 page) messages to buffer in the router");
48 static int large_router_buffers;
49 module_param(large_router_buffers, int, 0444);
50 MODULE_PARM_DESC(large_router_buffers, "# of large messages to buffer in the router");
51 static int peer_buffer_credits;
52 module_param(peer_buffer_credits, int, 0444);
53 MODULE_PARM_DESC(peer_buffer_credits, "# router buffer credits per peer");
54
55 static int auto_down = 1;
56 module_param(auto_down, int, 0444);
57 MODULE_PARM_DESC(auto_down, "Automatically mark peers down on comms error");
58
59 int
60 lnet_peer_buffer_credits(struct lnet_net *net)
61 {
62         /* NI option overrides LNet default */
63         if (net->net_tunables.lct_peer_rtr_credits > 0)
64                 return net->net_tunables.lct_peer_rtr_credits;
65         if (peer_buffer_credits > 0)
66                 return peer_buffer_credits;
67
68         /* As an approximation, allow this peer the same number of router
69          * buffers as it is allowed outstanding sends */
70         return net->net_tunables.lct_peer_tx_credits;
71 }
72
73 static int check_routers_before_use;
74 module_param(check_routers_before_use, int, 0444);
75 MODULE_PARM_DESC(check_routers_before_use, "Assume routers are down and ping them before use");
76
77 int avoid_asym_router_failure = 1;
78 module_param(avoid_asym_router_failure, int, 0644);
79 MODULE_PARM_DESC(avoid_asym_router_failure, "Avoid asymmetrical router failures (0 to disable)");
80
81 static int dead_router_check_interval = 60;
82 module_param(dead_router_check_interval, int, 0644);
83 MODULE_PARM_DESC(dead_router_check_interval, "Seconds between dead router health checks (<= 0 to disable)");
84
85 static int live_router_check_interval = 60;
86 module_param(live_router_check_interval, int, 0644);
87 MODULE_PARM_DESC(live_router_check_interval, "Seconds between live router health checks (<= 0 to disable)");
88
89 static int router_ping_timeout = 50;
90 module_param(router_ping_timeout, int, 0644);
91 MODULE_PARM_DESC(router_ping_timeout, "Seconds to wait for the reply to a router health query");
92
93 /*
94  * A value between 0 and 100. 0 meaning that even if router's interfaces
95  * have the worse health still consider the gateway usable.
96  * 100 means that at least one interface on the route's remote net is 100%
97  * healthy to consider the route alive.
98  * The default is set to 100 to ensure we maintain the original behavior.
99  */
100 unsigned int router_sensitivity_percentage = 100;
101 static int rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp);
102 static struct kernel_param_ops param_ops_rtr_sensitivity = {
103         .set = rtr_sensitivity_set,
104         .get = param_get_int,
105 };
106 #define param_check_rtr_sensitivity(name, p) \
107                 __param_check(name, p, int)
108 #ifdef HAVE_KERNEL_PARAM_OPS
109 module_param(router_sensitivity_percentage, rtr_sensitivity, S_IRUGO|S_IWUSR);
110 #else
111 module_param_call(router_sensitivity_percentage, rtr_sensitivity_set, param_get_int,
112                   &router_sensitivity_percentage, S_IRUGO|S_IWUSR);
113 #endif
114 MODULE_PARM_DESC(router_sensitivity_percentage,
115                 "How healthy a gateway should be to be used in percent");
116
117 static int
118 rtr_sensitivity_set(const char *val, cfs_kernel_param_arg_t *kp)
119 {
120         int rc;
121         unsigned *sen = (unsigned *)kp->arg;
122         unsigned long value;
123
124         rc = kstrtoul(val, 0, &value);
125         if (rc) {
126                 CERROR("Invalid module parameter value for 'router_sensitivity_percentage'\n");
127                 return rc;
128         }
129
130         if (value < 0 || value > 100) {
131                 CERROR("Invalid value: %lu for 'router_sensitivity_percentage'\n", value);
132                 return -EINVAL;
133         }
134
135         /*
136          * The purpose of locking the api_mutex here is to ensure that
137          * the correct value ends up stored properly.
138          */
139         mutex_lock(&the_lnet.ln_api_mutex);
140
141         *sen = value;
142
143         mutex_unlock(&the_lnet.ln_api_mutex);
144
145         return 0;
146 }
147
148 int
149 lnet_peers_start_down(void)
150 {
151         return check_routers_before_use;
152 }
153
154 /*
155  * A net is alive if at least one gateway NI on the network is alive.
156  */
157 static bool
158 lnet_is_gateway_net_alive(struct lnet_peer_net *lpn)
159 {
160         struct lnet_peer_ni *lpni;
161
162         list_for_each_entry(lpni, &lpn->lpn_peer_nis, lpni_peer_nis) {
163                 if (lnet_is_peer_ni_alive(lpni))
164                         return true;
165         }
166
167         return false;
168 }
169
170 /*
171  * a gateway is alive only if all its nets are alive
172  * called with cpt lock held
173  */
174 bool lnet_is_gateway_alive(struct lnet_peer *gw)
175 {
176         struct lnet_peer_net *lpn;
177
178         list_for_each_entry(lpn, &gw->lp_peer_nets, lpn_peer_nets) {
179                 if (!lnet_is_gateway_net_alive(lpn))
180                         return false;
181         }
182
183         return true;
184 }
185
186 /*
187  * lnet_is_route_alive() needs to be called with cpt lock held
188  * A route is alive if the gateway can route between the local network and
189  * the remote network of the route.
190  * This means at least one NI is alive on each of the local and remote
191  * networks of the gateway.
192  */
193 bool lnet_is_route_alive(struct lnet_route *route)
194 {
195         struct lnet_peer *gw = route->lr_gateway;
196         struct lnet_peer_net *llpn;
197         struct lnet_peer_net *rlpn;
198         bool route_alive;
199
200         /*
201          * check the gateway's interfaces on the route rnet to make sure
202          * that the gateway is viable.
203          */
204         llpn = lnet_peer_get_net_locked(gw, route->lr_lnet);
205         if (!llpn)
206                 return false;
207
208         route_alive = lnet_is_gateway_net_alive(llpn);
209
210         if (avoid_asym_router_failure) {
211                 rlpn = lnet_peer_get_net_locked(gw, route->lr_net);
212                 if (!rlpn)
213                         return false;
214                 route_alive = route_alive &&
215                               lnet_is_gateway_net_alive(rlpn);
216         }
217
218         if (!route_alive)
219                 return route_alive;
220
221         spin_lock(&gw->lp_lock);
222         if (!(gw->lp_state & LNET_PEER_ROUTER_ENABLED)) {
223                 if (gw->lp_rtr_refcount > 0)
224                         CERROR("peer %s is being used as a gateway but routing feature is not turned on\n",
225                                libcfs_nid2str(gw->lp_primary_nid));
226                 route_alive = false;
227         }
228         spin_unlock(&gw->lp_lock);
229
230         return route_alive;
231 }
232
233 void
234 lnet_notify_locked(struct lnet_peer_ni *lp, int notifylnd, int alive,
235                    time64_t when)
236 {
237         if (lp->lpni_timestamp > when) { /* out of date information */
238                 CDEBUG(D_NET, "Out of date\n");
239                 return;
240         }
241
242         /*
243          * This function can be called with different cpt locks being
244          * held. lpni_alive_count modification needs to be properly protected.
245          * Significant reads to lpni_alive_count are also protected with
246          * the same lock
247          */
248         spin_lock(&lp->lpni_lock);
249
250         lp->lpni_timestamp = when; /* update timestamp */
251
252         /* got old news */
253         if (lp->lpni_alive_count != 0 &&
254             /* new date for old news */
255             (!lnet_is_peer_ni_alive(lp)) == (!alive)) {
256                 spin_unlock(&lp->lpni_lock);
257                 CDEBUG(D_NET, "Old news\n");
258                 return;
259         }
260
261         /* Flag that notification is outstanding */
262
263         lp->lpni_alive_count++;
264         lp->lpni_notify = 1;
265         lp->lpni_notifylnd = notifylnd;
266         if (lnet_is_peer_ni_alive(lp))
267                 lp->lpni_ping_feats = LNET_PING_FEAT_INVAL; /* reset */
268
269         spin_unlock(&lp->lpni_lock);
270
271         CDEBUG(D_NET, "set %s %d\n", libcfs_nid2str(lp->lpni_nid), alive);
272 }
273
274 /*
275  * This function will always be called with lp->lpni_cpt lock held.
276  */
277 static void
278 lnet_ni_notify_locked(struct lnet_ni *ni, struct lnet_peer_ni *lp)
279 {
280         int alive;
281         int notifylnd;
282
283         /* Notify only in 1 thread at any time to ensure ordered notification.
284          * NB individual events can be missed; the only guarantee is that you
285          * always get the most recent news */
286
287         spin_lock(&lp->lpni_lock);
288
289         if (lp->lpni_notifying || ni == NULL) {
290                 spin_unlock(&lp->lpni_lock);
291                 return;
292         }
293
294         lp->lpni_notifying = 1;
295
296         /*
297          * lp->lpni_notify needs to be protected because it can be set in
298          * lnet_notify_locked().
299          */
300         while (lp->lpni_notify) {
301                 alive     = lnet_is_peer_ni_alive(lp);
302                 notifylnd = lp->lpni_notifylnd;
303
304                 lp->lpni_notifylnd = 0;
305                 lp->lpni_notify    = 0;
306
307                 if (notifylnd && ni->ni_net->net_lnd->lnd_notify != NULL) {
308                         spin_unlock(&lp->lpni_lock);
309                         lnet_net_unlock(lp->lpni_cpt);
310
311                         /* A new notification could happen now; I'll handle it
312                          * when control returns to me */
313
314                         (ni->ni_net->net_lnd->lnd_notify)(ni, lp->lpni_nid,
315                                                           alive);
316
317                         lnet_net_lock(lp->lpni_cpt);
318                         spin_lock(&lp->lpni_lock);
319                 }
320         }
321
322         lp->lpni_notifying = 0;
323         spin_unlock(&lp->lpni_lock);
324 }
325
326 static void
327 lnet_rtr_addref_locked(struct lnet_peer *lp)
328 {
329         LASSERT(lp->lp_rtr_refcount >= 0);
330
331         /* lnet_net_lock must be exclusively locked */
332         lp->lp_rtr_refcount++;
333         if (lp->lp_rtr_refcount == 1) {
334                 list_add_tail(&lp->lp_rtr_list, &the_lnet.ln_routers);
335                 /* addref for the_lnet.ln_routers */
336                 lnet_peer_addref_locked(lp);
337                 the_lnet.ln_routers_version++;
338         }
339 }
340
341 static void
342 lnet_rtr_decref_locked(struct lnet_peer *lp)
343 {
344         LASSERT(atomic_read(&lp->lp_refcount) > 0);
345         LASSERT(lp->lp_rtr_refcount > 0);
346
347         /* lnet_net_lock must be exclusively locked */
348         lp->lp_rtr_refcount--;
349         if (lp->lp_rtr_refcount == 0) {
350                 LASSERT(list_empty(&lp->lp_routes));
351
352                 list_del(&lp->lp_rtr_list);
353                 /* decref for the_lnet.ln_routers */
354                 lnet_peer_decref_locked(lp);
355                 the_lnet.ln_routers_version++;
356         }
357 }
358
359 struct lnet_remotenet *
360 lnet_find_rnet_locked(__u32 net)
361 {
362         struct lnet_remotenet *rnet;
363         struct list_head *tmp;
364         struct list_head *rn_list;
365
366         LASSERT(the_lnet.ln_state == LNET_STATE_RUNNING);
367
368         rn_list = lnet_net2rnethash(net);
369         list_for_each(tmp, rn_list) {
370                 rnet = list_entry(tmp, struct lnet_remotenet, lrn_list);
371
372                 if (rnet->lrn_net == net)
373                         return rnet;
374         }
375         return NULL;
376 }
377
378 static void lnet_shuffle_seed(void)
379 {
380         static int seeded;
381         struct lnet_ni *ni = NULL;
382
383         if (seeded)
384                 return;
385
386         /* Nodes with small feet have little entropy
387          * the NID for this node gives the most entropy in the low bits */
388         while ((ni = lnet_get_next_ni_locked(NULL, ni)))
389                 add_device_randomness(&ni->ni_nid, sizeof(ni->ni_nid));
390
391         seeded = 1;
392         return;
393 }
394
395 /* NB expects LNET_LOCK held */
396 static void
397 lnet_add_route_to_rnet(struct lnet_remotenet *rnet, struct lnet_route *route)
398 {
399         unsigned int len = 0;
400         unsigned int offset = 0;
401         struct list_head *e;
402
403         lnet_shuffle_seed();
404
405         list_for_each(e, &rnet->lrn_routes)
406                 len++;
407
408         /*
409          * Randomly adding routes to the list is done to ensure that when
410          * different nodes are using the same list of routers, they end up
411          * preferring different routers.
412          */
413         offset = cfs_rand() % (len + 1);
414         list_for_each(e, &rnet->lrn_routes) {
415                 if (offset == 0)
416                         break;
417                 offset--;
418         }
419         list_add(&route->lr_list, e);
420         /*
421          * force a router check on the gateway to make sure the route is
422          * alive
423          */
424         route->lr_gateway->lp_rtrcheck_timestamp = 0;
425
426         the_lnet.ln_remote_nets_version++;
427
428         /* add the route on the gateway list */
429         list_add(&route->lr_gwlist, &route->lr_gateway->lp_routes);
430
431         /* take a router reference count on the gateway */
432         lnet_rtr_addref_locked(route->lr_gateway);
433 }
434
435 int
436 lnet_add_route(__u32 net, __u32 hops, lnet_nid_t gateway,
437                unsigned int priority)
438 {
439         struct list_head *route_entry;
440         struct lnet_remotenet *rnet;
441         struct lnet_remotenet *rnet2;
442         struct lnet_route *route;
443         struct lnet_peer_ni *lpni;
444         struct lnet_peer *gw;
445         int add_route;
446         int rc;
447
448         CDEBUG(D_NET, "Add route: remote net %s hops %d priority %u gw %s\n",
449                libcfs_net2str(net), hops, priority, libcfs_nid2str(gateway));
450
451         if (gateway == LNET_NID_ANY ||
452             LNET_NETTYP(LNET_NIDNET(gateway)) == LOLND ||
453             net == LNET_NIDNET(LNET_NID_ANY) ||
454             LNET_NETTYP(net) == LOLND ||
455             LNET_NIDNET(gateway) == net ||
456             (hops != LNET_UNDEFINED_HOPS && (hops < 1 || hops > 255)))
457                 return -EINVAL;
458
459         /* it's a local network */
460         if (lnet_islocalnet(net))
461                 return -EEXIST;
462
463         /* Assume net, route, all new */
464         LIBCFS_ALLOC(route, sizeof(*route));
465         LIBCFS_ALLOC(rnet, sizeof(*rnet));
466         if (route == NULL || rnet == NULL) {
467                 CERROR("Out of memory creating route %s %d %s\n",
468                        libcfs_net2str(net), hops, libcfs_nid2str(gateway));
469                 if (route != NULL)
470                         LIBCFS_FREE(route, sizeof(*route));
471                 if (rnet != NULL)
472                         LIBCFS_FREE(rnet, sizeof(*rnet));
473                 return -ENOMEM;
474         }
475
476         INIT_LIST_HEAD(&rnet->lrn_routes);
477         rnet->lrn_net = net;
478         /* store the local and remote net that the route represents */
479         route->lr_lnet = LNET_NIDNET(gateway);
480         route->lr_net = net;
481         route->lr_priority = priority;
482         route->lr_hops = hops;
483
484         lnet_net_lock(LNET_LOCK_EX);
485
486         /*
487          * lnet_nid2peerni_ex() grabs a ref on the lpni. We will need to
488          * lose that once we're done
489          */
490         lpni = lnet_nid2peerni_ex(gateway, LNET_LOCK_EX);
491         if (IS_ERR(lpni)) {
492                 lnet_net_unlock(LNET_LOCK_EX);
493
494                 LIBCFS_FREE(route, sizeof(*route));
495                 LIBCFS_FREE(rnet, sizeof(*rnet));
496
497                 rc = PTR_ERR(lpni);
498                 CERROR("Error %d creating route %s %d %s\n", rc,
499                         libcfs_net2str(net), hops,
500                         libcfs_nid2str(gateway));
501                 return rc;
502         }
503
504         LASSERT(lpni->lpni_peer_net && lpni->lpni_peer_net->lpn_peer);
505         gw = lpni->lpni_peer_net->lpn_peer;
506
507         route->lr_gateway = gw;
508
509         rnet2 = lnet_find_rnet_locked(net);
510         if (rnet2 == NULL) {
511                 /* new network */
512                 list_add_tail(&rnet->lrn_list, lnet_net2rnethash(net));
513                 rnet2 = rnet;
514         }
515
516         /* Search for a duplicate route (it's a NOOP if it is) */
517         add_route = 1;
518         list_for_each(route_entry, &rnet2->lrn_routes) {
519                 struct lnet_route *route2;
520
521                 route2 = list_entry(route_entry, struct lnet_route, lr_list);
522                 if (route2->lr_gateway == route->lr_gateway) {
523                         add_route = 0;
524                         break;
525                 }
526
527                 /* our lookups must be true */
528                 LASSERT(route2->lr_gateway->lp_primary_nid != gateway);
529         }
530
531         /*
532          * It is possible to add multiple routes through the same peer,
533          * but it'll be using a different NID of that peer. When the
534          * gateway is discovered, discovery will consolidate the different
535          * peers into one peer. In this case the discovery code will have
536          * to move the routes from the peer that's being deleted to the
537          * consolidated peer lp_routes list
538          */
539         if (add_route)
540                 lnet_add_route_to_rnet(rnet2, route);
541
542         /*
543          * get rid of the reference on the lpni.
544          */
545         lnet_peer_ni_decref_locked(lpni);
546         lnet_net_unlock(LNET_LOCK_EX);
547
548         rc = 0;
549
550         if (!add_route) {
551                 rc = -EEXIST;
552                 LIBCFS_FREE(route, sizeof(*route));
553         }
554
555         if (rnet != rnet2)
556                 LIBCFS_FREE(rnet, sizeof(*rnet));
557
558         /* kick start the monitor thread to handle the added route */
559         wake_up(&the_lnet.ln_mt_waitq);
560
561         return rc;
562 }
563
564 static void
565 lnet_del_route_from_rnet(lnet_nid_t gw_nid, struct list_head *route_list,
566                          struct list_head *zombies)
567 {
568         struct lnet_peer *gateway;
569         struct lnet_route *route;
570         struct lnet_route *tmp;
571
572         list_for_each_entry_safe(route, tmp, route_list, lr_list) {
573                 gateway = route->lr_gateway;
574                 if (gw_nid != LNET_NID_ANY &&
575                     gw_nid != gateway->lp_primary_nid)
576                         continue;
577
578                 /*
579                  * move to zombie to delete outside the lock
580                  * Note that this function is called with the
581                  * ln_api_mutex held as well as the exclusive net
582                  * lock. Adding to the remote net list happens
583                  * under the same conditions. Same goes for the
584                  * gateway router list
585                  */
586                 list_move(&route->lr_list, zombies);
587                 the_lnet.ln_remote_nets_version++;
588
589                 list_del(&route->lr_gwlist);
590                 lnet_rtr_decref_locked(gateway);
591         }
592 }
593
594 int
595 lnet_del_route(__u32 net, lnet_nid_t gw_nid)
596 {
597         struct list_head rnet_zombies;
598         struct lnet_remotenet *rnet;
599         struct lnet_remotenet *tmp;
600         struct list_head *rn_list;
601         struct lnet_peer_ni *lpni;
602         struct lnet_route *route;
603         struct list_head zombies;
604         struct lnet_peer *lp;
605         int i = 0;
606
607         INIT_LIST_HEAD(&rnet_zombies);
608         INIT_LIST_HEAD(&zombies);
609
610         CDEBUG(D_NET, "Del route: net %s : gw %s\n",
611                libcfs_net2str(net), libcfs_nid2str(gw_nid));
612
613         /* NB Caller may specify either all routes via the given gateway
614          * or a specific route entry actual NIDs) */
615
616         lnet_net_lock(LNET_LOCK_EX);
617
618         lpni = lnet_find_peer_ni_locked(gw_nid);
619         if (lpni) {
620                 lp = lpni->lpni_peer_net->lpn_peer;
621                 LASSERT(lp);
622                 gw_nid = lp->lp_primary_nid;
623                 lnet_peer_ni_decref_locked(lpni);
624         }
625
626         if (net != LNET_NIDNET(LNET_NID_ANY)) {
627                 rnet = lnet_find_rnet_locked(net);
628                 if (!rnet) {
629                         lnet_net_unlock(LNET_LOCK_EX);
630                         return -ENOENT;
631                 }
632                 lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
633                                          &zombies);
634                 if (list_empty(&rnet->lrn_routes))
635                         list_move(&rnet->lrn_list, &rnet_zombies);
636                 goto delete_zombies;
637         }
638
639         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
640                 rn_list = &the_lnet.ln_remote_nets_hash[i];
641
642                 list_for_each_entry_safe(rnet, tmp, rn_list, lrn_list) {
643                         lnet_del_route_from_rnet(gw_nid, &rnet->lrn_routes,
644                                                  &zombies);
645                         if (list_empty(&rnet->lrn_routes))
646                                 list_move(&rnet->lrn_list, &rnet_zombies);
647                 }
648         }
649
650 delete_zombies:
651         lnet_net_unlock(LNET_LOCK_EX);
652
653         while (!list_empty(&zombies)) {
654                 route = list_first_entry(&zombies, struct lnet_route, lr_list);
655                 list_del(&route->lr_list);
656                 LIBCFS_FREE(route, sizeof(*route));
657         }
658
659         while (!list_empty(&rnet_zombies)) {
660                 rnet = list_first_entry(&rnet_zombies, struct lnet_remotenet,
661                                         lrn_list);
662                 list_del(&rnet->lrn_list);
663                 LIBCFS_FREE(rnet, sizeof(*rnet));
664         }
665
666         return 0;
667 }
668
669 void
670 lnet_destroy_routes (void)
671 {
672         lnet_del_route(LNET_NIDNET(LNET_NID_ANY), LNET_NID_ANY);
673 }
674
675 int lnet_get_rtr_pool_cfg(int cpt, struct lnet_ioctl_pool_cfg *pool_cfg)
676 {
677         struct lnet_rtrbufpool *rbp;
678         int i, rc = -ENOENT, j;
679
680         if (the_lnet.ln_rtrpools == NULL)
681                 return rc;
682
683
684         cfs_percpt_for_each(rbp, i, the_lnet.ln_rtrpools) {
685                 if (i != cpt)
686                         continue;
687
688                 lnet_net_lock(i);
689                 for (j = 0; j < LNET_NRBPOOLS; j++) {
690                         pool_cfg->pl_pools[j].pl_npages = rbp[j].rbp_npages;
691                         pool_cfg->pl_pools[j].pl_nbuffers = rbp[j].rbp_nbuffers;
692                         pool_cfg->pl_pools[j].pl_credits = rbp[j].rbp_credits;
693                         pool_cfg->pl_pools[j].pl_mincredits = rbp[j].rbp_mincredits;
694                 }
695                 lnet_net_unlock(i);
696                 rc = 0;
697                 break;
698         }
699
700         lnet_net_lock(LNET_LOCK_EX);
701         pool_cfg->pl_routing = the_lnet.ln_routing;
702         lnet_net_unlock(LNET_LOCK_EX);
703
704         return rc;
705 }
706
707 int
708 lnet_get_route(int idx, __u32 *net, __u32 *hops,
709                lnet_nid_t *gateway, __u32 *alive, __u32 *priority)
710 {
711         struct list_head *e1;
712         struct list_head *e2;
713         struct lnet_remotenet *rnet;
714         struct lnet_route        *route;
715         int               cpt;
716         int               i;
717         struct list_head *rn_list;
718
719         cpt = lnet_net_lock_current();
720
721         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++) {
722                 rn_list = &the_lnet.ln_remote_nets_hash[i];
723                 list_for_each(e1, rn_list) {
724                         rnet = list_entry(e1, struct lnet_remotenet, lrn_list);
725
726                         list_for_each(e2, &rnet->lrn_routes) {
727                                 route = list_entry(e2, struct lnet_route,
728                                                    lr_list);
729
730                                 if (idx-- == 0) {
731                                         *net      = rnet->lrn_net;
732                                         *hops     = route->lr_hops;
733                                         *priority = route->lr_priority;
734                                         *gateway  = route->lr_gateway->lp_primary_nid;
735                                         *alive    = lnet_is_route_alive(route);
736                                         lnet_net_unlock(cpt);
737                                         return 0;
738                                 }
739                         }
740                 }
741         }
742
743         lnet_net_unlock(cpt);
744         return -ENOENT;
745 }
746
747 void
748 lnet_swap_pinginfo(struct lnet_ping_buffer *pbuf)
749 {
750         struct lnet_ni_status *stat;
751         int nnis;
752         int i;
753
754         __swab32s(&pbuf->pb_info.pi_magic);
755         __swab32s(&pbuf->pb_info.pi_features);
756         __swab32s(&pbuf->pb_info.pi_pid);
757         __swab32s(&pbuf->pb_info.pi_nnis);
758         nnis = pbuf->pb_info.pi_nnis;
759         if (nnis > pbuf->pb_nnis)
760                 nnis = pbuf->pb_nnis;
761         for (i = 0; i < nnis; i++) {
762                 stat = &pbuf->pb_info.pi_ni[i];
763                 __swab64s(&stat->ns_nid);
764                 __swab32s(&stat->ns_status);
765         }
766         return;
767 }
768
769 /**
770  * TODO: re-implement
771  */
772 static void
773 lnet_parse_rc_info(struct lnet_rc_data *rcd)
774 {
775         rcd = rcd;
776 }
777
778 static void
779 lnet_router_checker_event(struct lnet_event *event)
780 {
781         struct lnet_rc_data *rcd = event->md.user_ptr;
782         struct lnet_peer_ni *lp;
783
784         LASSERT(rcd != NULL);
785
786         if (event->unlinked) {
787                 LNetInvalidateMDHandle(&rcd->rcd_mdh);
788                 return;
789         }
790
791         LASSERT(event->type == LNET_EVENT_SEND ||
792                 event->type == LNET_EVENT_REPLY);
793
794         lp = rcd->rcd_gateway;
795         LASSERT(lp != NULL);
796
797          /* NB: it's called with holding lnet_res_lock, we have a few
798           * places need to hold both locks at the same time, please take
799           * care of lock ordering */
800         lnet_net_lock(lp->lpni_cpt);
801         if (!lnet_isrouter(lp) || lp->lpni_rcd != rcd) {
802                 /* ignore if no longer a router or rcd is replaced */
803                 goto out;
804         }
805
806         if (event->type == LNET_EVENT_SEND) {
807                 if (event->status == 0)
808                         goto out;
809         }
810
811         /* LNET_EVENT_REPLY */
812         /* A successful REPLY means the router is up.  If _any_ comms
813          * to the router fail I assume it's down (this will happen if
814          * we ping alive routers to try to detect router death before
815          * apps get burned). */
816
817         lnet_notify_locked(lp, 1, !event->status, ktime_get_seconds());
818         /* The router checker will wake up very shortly and do the
819          * actual notification.
820          * XXX If 'lp' stops being a router before then, it will still
821          * have the notification pending!!! */
822
823         if (avoid_asym_router_failure && event->status == 0)
824                 lnet_parse_rc_info(rcd);
825
826  out:
827         lnet_net_unlock(lp->lpni_cpt);
828 }
829
830 static void
831 lnet_wait_known_routerstate(void)
832 {
833         struct lnet_peer *rtr;
834         struct list_head *entry;
835         int all_known;
836
837         LASSERT(the_lnet.ln_mt_state == LNET_MT_STATE_RUNNING);
838
839         for (;;) {
840                 int cpt = lnet_net_lock_current();
841
842                 all_known = 1;
843                 list_for_each(entry, &the_lnet.ln_routers) {
844                         rtr = list_entry(entry, struct lnet_peer,
845                                          lp_rtr_list);
846
847                         spin_lock(&rtr->lp_lock);
848
849                         if ((rtr->lp_state & LNET_PEER_DISCOVERED) == 0) {
850                                 all_known = 0;
851                                 spin_unlock(&rtr->lp_lock);
852                                 break;
853                         }
854                         spin_unlock(&rtr->lp_lock);
855                 }
856
857                 lnet_net_unlock(cpt);
858
859                 if (all_known)
860                         return;
861
862                 set_current_state(TASK_UNINTERRUPTIBLE);
863                 schedule_timeout(cfs_time_seconds(1));
864         }
865 }
866
867 /* TODO: reimplement */
868 void
869 lnet_router_ni_update_locked(struct lnet_peer_ni *gw, __u32 net)
870 {
871         struct lnet_route *rte;
872         struct lnet_peer *lp;
873
874         if ((gw->lpni_ping_feats & LNET_PING_FEAT_NI_STATUS) != 0)
875                 lp = gw->lpni_peer_net->lpn_peer;
876         else
877                 return;
878
879         list_for_each_entry(rte, &lp->lp_routes, lr_gwlist) {
880                 if (rte->lr_net == net) {
881                         rte->lr_downis = 0;
882                         break;
883                 }
884         }
885 }
886
887 static void
888 lnet_update_ni_status_locked(void)
889 {
890         struct lnet_ni *ni = NULL;
891         time64_t now;
892         time64_t timeout;
893
894         LASSERT(the_lnet.ln_routing);
895
896         timeout = router_ping_timeout +
897                   MAX(live_router_check_interval, dead_router_check_interval);
898
899         now = ktime_get_real_seconds();
900         while ((ni = lnet_get_next_ni_locked(NULL, ni))) {
901                 if (ni->ni_net->net_lnd->lnd_type == LOLND)
902                         continue;
903
904                 if (now < ni->ni_last_alive + timeout)
905                         continue;
906
907                 lnet_ni_lock(ni);
908                 /* re-check with lock */
909                 if (now < ni->ni_last_alive + timeout) {
910                         lnet_ni_unlock(ni);
911                         continue;
912                 }
913
914                 LASSERT(ni->ni_status != NULL);
915
916                 if (ni->ni_status->ns_status != LNET_NI_STATUS_DOWN) {
917                         CDEBUG(D_NET, "NI(%s:%lld) status changed to down\n",
918                                libcfs_nid2str(ni->ni_nid), timeout);
919                         /* NB: so far, this is the only place to set
920                          * NI status to "down" */
921                         ni->ni_status->ns_status = LNET_NI_STATUS_DOWN;
922                 }
923                 lnet_ni_unlock(ni);
924         }
925 }
926
927 int lnet_router_pre_mt_start(void)
928 {
929         int rc;
930
931         if (check_routers_before_use &&
932             dead_router_check_interval <= 0) {
933                 LCONSOLE_ERROR_MSG(0x10a, "'dead_router_check_interval' must be"
934                                    " set if 'check_routers_before_use' is set"
935                                    "\n");
936                 return -EINVAL;
937         }
938
939         rc = LNetEQAlloc(0, lnet_router_checker_event, &the_lnet.ln_rc_eqh);
940         if (rc != 0) {
941                 CERROR("Can't allocate EQ(0): %d\n", rc);
942                 return -ENOMEM;
943         }
944
945         return 0;
946 }
947
948 void lnet_router_post_mt_start(void)
949 {
950         if (check_routers_before_use) {
951                 /* Note that a helpful side-effect of pinging all known routers
952                  * at startup is that it makes them drop stale connections they
953                  * may have to a previous instance of me. */
954                 lnet_wait_known_routerstate();
955         }
956 }
957
958 void
959 lnet_router_cleanup(void)
960 {
961         int rc;
962
963         rc = LNetEQFree(the_lnet.ln_rc_eqh);
964         LASSERT(rc == 0);
965         return;
966 }
967
968 void
969 lnet_prune_rc_data(int wait_unlink)
970 {
971         wait_unlink = wait_unlink;
972 }
973
974 /*
975  * This function is called from the monitor thread to check if there are
976  * any active routers that need to be checked.
977  */
978 inline bool
979 lnet_router_checker_active(void)
980 {
981         if (the_lnet.ln_mt_state != LNET_MT_STATE_RUNNING)
982                 return true;
983
984         /* Router Checker thread needs to run when routing is enabled in
985          * order to call lnet_update_ni_status_locked() */
986         if (the_lnet.ln_routing)
987                 return true;
988
989         /* if there are routers that need to be cleaned up then do so */
990         if (!list_empty(&the_lnet.ln_rcd_deathrow) ||
991             !list_empty(&the_lnet.ln_rcd_zombie))
992                 return true;
993
994         return !list_empty(&the_lnet.ln_routers) &&
995                 (live_router_check_interval > 0 ||
996                  dead_router_check_interval > 0);
997 }
998
999 void
1000 lnet_check_routers(void)
1001 {
1002         struct lnet_peer *rtr;
1003         struct list_head *entry;
1004         __u64   version;
1005         int     cpt;
1006
1007         cpt = lnet_net_lock_current();
1008 rescan:
1009         version = the_lnet.ln_routers_version;
1010
1011         list_for_each(entry, &the_lnet.ln_routers) {
1012                 rtr = list_entry(entry, struct lnet_peer,
1013                                  lp_rtr_list);
1014
1015                 /* TODO use discovery to determine if router is alive */
1016
1017                 /* NB dropped lock */
1018                 if (version != the_lnet.ln_routers_version) {
1019                         /* the routers list has changed */
1020                         goto rescan;
1021                 }
1022         }
1023
1024         if (the_lnet.ln_routing)
1025                 lnet_update_ni_status_locked();
1026
1027         lnet_net_unlock(cpt);
1028
1029         lnet_prune_rc_data(0); /* don't wait for UNLINK */
1030 }
1031
1032 void
1033 lnet_destroy_rtrbuf(struct lnet_rtrbuf *rb, int npages)
1034 {
1035         int sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1036
1037         while (--npages >= 0)
1038                 __free_page(rb->rb_kiov[npages].kiov_page);
1039
1040         LIBCFS_FREE(rb, sz);
1041 }
1042
1043 static struct lnet_rtrbuf *
1044 lnet_new_rtrbuf(struct lnet_rtrbufpool *rbp, int cpt)
1045 {
1046         int            npages = rbp->rbp_npages;
1047         int            sz = offsetof(struct lnet_rtrbuf, rb_kiov[npages]);
1048         struct page   *page;
1049         struct lnet_rtrbuf *rb;
1050         int            i;
1051
1052         LIBCFS_CPT_ALLOC(rb, lnet_cpt_table(), cpt, sz);
1053         if (rb == NULL)
1054                 return NULL;
1055
1056         rb->rb_pool = rbp;
1057
1058         for (i = 0; i < npages; i++) {
1059                 page = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1060                                           GFP_KERNEL | __GFP_ZERO);
1061                 if (page == NULL) {
1062                         while (--i >= 0)
1063                                 __free_page(rb->rb_kiov[i].kiov_page);
1064
1065                         LIBCFS_FREE(rb, sz);
1066                         return NULL;
1067                 }
1068
1069                 rb->rb_kiov[i].kiov_len = PAGE_SIZE;
1070                 rb->rb_kiov[i].kiov_offset = 0;
1071                 rb->rb_kiov[i].kiov_page = page;
1072         }
1073
1074         return rb;
1075 }
1076
1077 static void
1078 lnet_rtrpool_free_bufs(struct lnet_rtrbufpool *rbp, int cpt)
1079 {
1080         int npages = rbp->rbp_npages;
1081         struct lnet_rtrbuf *rb;
1082         struct list_head tmp;
1083
1084         if (rbp->rbp_nbuffers == 0) /* not initialized or already freed */
1085                 return;
1086
1087         INIT_LIST_HEAD(&tmp);
1088
1089         lnet_net_lock(cpt);
1090         list_splice_init(&rbp->rbp_msgs, &tmp);
1091         lnet_drop_routed_msgs_locked(&tmp, cpt);
1092         list_splice_init(&rbp->rbp_bufs, &tmp);
1093         rbp->rbp_req_nbuffers = 0;
1094         rbp->rbp_nbuffers = rbp->rbp_credits = 0;
1095         rbp->rbp_mincredits = 0;
1096         lnet_net_unlock(cpt);
1097
1098         /* Free buffers on the free list. */
1099         while (!list_empty(&tmp)) {
1100                 rb = list_entry(tmp.next, struct lnet_rtrbuf, rb_list);
1101                 list_del(&rb->rb_list);
1102                 lnet_destroy_rtrbuf(rb, npages);
1103         }
1104 }
1105
1106 static int
1107 lnet_rtrpool_adjust_bufs(struct lnet_rtrbufpool *rbp, int nbufs, int cpt)
1108 {
1109         struct list_head rb_list;
1110         struct lnet_rtrbuf *rb;
1111         int             num_rb;
1112         int             num_buffers = 0;
1113         int             old_req_nbufs;
1114         int             npages = rbp->rbp_npages;
1115
1116         lnet_net_lock(cpt);
1117         /* If we are called for less buffers than already in the pool, we
1118          * just lower the req_nbuffers number and excess buffers will be
1119          * thrown away as they are returned to the free list.  Credits
1120          * then get adjusted as well.
1121          * If we already have enough buffers allocated to serve the
1122          * increase requested, then we can treat that the same way as we
1123          * do the decrease. */
1124         num_rb = nbufs - rbp->rbp_nbuffers;
1125         if (nbufs <= rbp->rbp_req_nbuffers || num_rb <= 0) {
1126                 rbp->rbp_req_nbuffers = nbufs;
1127                 lnet_net_unlock(cpt);
1128                 return 0;
1129         }
1130         /* store the older value of rbp_req_nbuffers and then set it to
1131          * the new request to prevent lnet_return_rx_credits_locked() from
1132          * freeing buffers that we need to keep around */
1133         old_req_nbufs = rbp->rbp_req_nbuffers;
1134         rbp->rbp_req_nbuffers = nbufs;
1135         lnet_net_unlock(cpt);
1136
1137         INIT_LIST_HEAD(&rb_list);
1138
1139         /* allocate the buffers on a local list first.  If all buffers are
1140          * allocated successfully then join this list to the rbp buffer
1141          * list.  If not then free all allocated buffers. */
1142         while (num_rb-- > 0) {
1143                 rb = lnet_new_rtrbuf(rbp, cpt);
1144                 if (rb == NULL) {
1145                         CERROR("Failed to allocate %d route bufs of %d pages\n",
1146                                nbufs, npages);
1147
1148                         lnet_net_lock(cpt);
1149                         rbp->rbp_req_nbuffers = old_req_nbufs;
1150                         lnet_net_unlock(cpt);
1151
1152                         goto failed;
1153                 }
1154
1155                 list_add(&rb->rb_list, &rb_list);
1156                 num_buffers++;
1157         }
1158
1159         lnet_net_lock(cpt);
1160
1161         list_splice_tail(&rb_list, &rbp->rbp_bufs);
1162         rbp->rbp_nbuffers += num_buffers;
1163         rbp->rbp_credits += num_buffers;
1164         rbp->rbp_mincredits = rbp->rbp_credits;
1165         /* We need to schedule blocked msg using the newly
1166          * added buffers. */
1167         while (!list_empty(&rbp->rbp_bufs) &&
1168                !list_empty(&rbp->rbp_msgs))
1169                 lnet_schedule_blocked_locked(rbp);
1170
1171         lnet_net_unlock(cpt);
1172
1173         return 0;
1174
1175 failed:
1176         while (!list_empty(&rb_list)) {
1177                 rb = list_entry(rb_list.next, struct lnet_rtrbuf, rb_list);
1178                 list_del(&rb->rb_list);
1179                 lnet_destroy_rtrbuf(rb, npages);
1180         }
1181
1182         return -ENOMEM;
1183 }
1184
1185 static void
1186 lnet_rtrpool_init(struct lnet_rtrbufpool *rbp, int npages)
1187 {
1188         INIT_LIST_HEAD(&rbp->rbp_msgs);
1189         INIT_LIST_HEAD(&rbp->rbp_bufs);
1190
1191         rbp->rbp_npages = npages;
1192         rbp->rbp_credits = 0;
1193         rbp->rbp_mincredits = 0;
1194 }
1195
1196 void
1197 lnet_rtrpools_free(int keep_pools)
1198 {
1199         struct lnet_rtrbufpool *rtrp;
1200         int               i;
1201
1202         if (the_lnet.ln_rtrpools == NULL) /* uninitialized or freed */
1203                 return;
1204
1205         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1206                 lnet_rtrpool_free_bufs(&rtrp[LNET_TINY_BUF_IDX], i);
1207                 lnet_rtrpool_free_bufs(&rtrp[LNET_SMALL_BUF_IDX], i);
1208                 lnet_rtrpool_free_bufs(&rtrp[LNET_LARGE_BUF_IDX], i);
1209         }
1210
1211         if (!keep_pools) {
1212                 cfs_percpt_free(the_lnet.ln_rtrpools);
1213                 the_lnet.ln_rtrpools = NULL;
1214         }
1215 }
1216
1217 static int
1218 lnet_nrb_tiny_calculate(void)
1219 {
1220         int     nrbs = LNET_NRB_TINY;
1221
1222         if (tiny_router_buffers < 0) {
1223                 LCONSOLE_ERROR_MSG(0x10c,
1224                                    "tiny_router_buffers=%d invalid when "
1225                                    "routing enabled\n", tiny_router_buffers);
1226                 return -EINVAL;
1227         }
1228
1229         if (tiny_router_buffers > 0)
1230                 nrbs = tiny_router_buffers;
1231
1232         nrbs /= LNET_CPT_NUMBER;
1233         return max(nrbs, LNET_NRB_TINY_MIN);
1234 }
1235
1236 static int
1237 lnet_nrb_small_calculate(void)
1238 {
1239         int     nrbs = LNET_NRB_SMALL;
1240
1241         if (small_router_buffers < 0) {
1242                 LCONSOLE_ERROR_MSG(0x10c,
1243                                    "small_router_buffers=%d invalid when "
1244                                    "routing enabled\n", small_router_buffers);
1245                 return -EINVAL;
1246         }
1247
1248         if (small_router_buffers > 0)
1249                 nrbs = small_router_buffers;
1250
1251         nrbs /= LNET_CPT_NUMBER;
1252         return max(nrbs, LNET_NRB_SMALL_MIN);
1253 }
1254
1255 static int
1256 lnet_nrb_large_calculate(void)
1257 {
1258         int     nrbs = LNET_NRB_LARGE;
1259
1260         if (large_router_buffers < 0) {
1261                 LCONSOLE_ERROR_MSG(0x10c,
1262                                    "large_router_buffers=%d invalid when "
1263                                    "routing enabled\n", large_router_buffers);
1264                 return -EINVAL;
1265         }
1266
1267         if (large_router_buffers > 0)
1268                 nrbs = large_router_buffers;
1269
1270         nrbs /= LNET_CPT_NUMBER;
1271         return max(nrbs, LNET_NRB_LARGE_MIN);
1272 }
1273
1274 int
1275 lnet_rtrpools_alloc(int im_a_router)
1276 {
1277         struct lnet_rtrbufpool *rtrp;
1278         int     nrb_tiny;
1279         int     nrb_small;
1280         int     nrb_large;
1281         int     rc;
1282         int     i;
1283
1284         if (!strcmp(forwarding, "")) {
1285                 /* not set either way */
1286                 if (!im_a_router)
1287                         return 0;
1288         } else if (!strcmp(forwarding, "disabled")) {
1289                 /* explicitly disabled */
1290                 return 0;
1291         } else if (!strcmp(forwarding, "enabled")) {
1292                 /* explicitly enabled */
1293         } else {
1294                 LCONSOLE_ERROR_MSG(0x10b, "'forwarding' not set to either "
1295                                    "'enabled' or 'disabled'\n");
1296                 return -EINVAL;
1297         }
1298
1299         nrb_tiny = lnet_nrb_tiny_calculate();
1300         if (nrb_tiny < 0)
1301                 return -EINVAL;
1302
1303         nrb_small = lnet_nrb_small_calculate();
1304         if (nrb_small < 0)
1305                 return -EINVAL;
1306
1307         nrb_large = lnet_nrb_large_calculate();
1308         if (nrb_large < 0)
1309                 return -EINVAL;
1310
1311         the_lnet.ln_rtrpools = cfs_percpt_alloc(lnet_cpt_table(),
1312                                                 LNET_NRBPOOLS *
1313                                                 sizeof(struct lnet_rtrbufpool));
1314         if (the_lnet.ln_rtrpools == NULL) {
1315                 LCONSOLE_ERROR_MSG(0x10c,
1316                                    "Failed to initialize router buffe pool\n");
1317                 return -ENOMEM;
1318         }
1319
1320         cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1321                 lnet_rtrpool_init(&rtrp[LNET_TINY_BUF_IDX], 0);
1322                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1323                                               nrb_tiny, i);
1324                 if (rc != 0)
1325                         goto failed;
1326
1327                 lnet_rtrpool_init(&rtrp[LNET_SMALL_BUF_IDX],
1328                                   LNET_NRB_SMALL_PAGES);
1329                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1330                                               nrb_small, i);
1331                 if (rc != 0)
1332                         goto failed;
1333
1334                 lnet_rtrpool_init(&rtrp[LNET_LARGE_BUF_IDX],
1335                                   LNET_NRB_LARGE_PAGES);
1336                 rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1337                                               nrb_large, i);
1338                 if (rc != 0)
1339                         goto failed;
1340         }
1341
1342         lnet_net_lock(LNET_LOCK_EX);
1343         the_lnet.ln_routing = 1;
1344         lnet_net_unlock(LNET_LOCK_EX);
1345         wake_up(&the_lnet.ln_mt_waitq);
1346         return 0;
1347
1348  failed:
1349         lnet_rtrpools_free(0);
1350         return rc;
1351 }
1352
1353 static int
1354 lnet_rtrpools_adjust_helper(int tiny, int small, int large)
1355 {
1356         int nrb = 0;
1357         int rc = 0;
1358         int i;
1359         struct lnet_rtrbufpool *rtrp;
1360
1361         /* If the provided values for each buffer pool are different than the
1362          * configured values, we need to take action. */
1363         if (tiny >= 0) {
1364                 tiny_router_buffers = tiny;
1365                 nrb = lnet_nrb_tiny_calculate();
1366                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1367                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_TINY_BUF_IDX],
1368                                                       nrb, i);
1369                         if (rc != 0)
1370                                 return rc;
1371                 }
1372         }
1373         if (small >= 0) {
1374                 small_router_buffers = small;
1375                 nrb = lnet_nrb_small_calculate();
1376                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1377                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_SMALL_BUF_IDX],
1378                                                       nrb, i);
1379                         if (rc != 0)
1380                                 return rc;
1381                 }
1382         }
1383         if (large >= 0) {
1384                 large_router_buffers = large;
1385                 nrb = lnet_nrb_large_calculate();
1386                 cfs_percpt_for_each(rtrp, i, the_lnet.ln_rtrpools) {
1387                         rc = lnet_rtrpool_adjust_bufs(&rtrp[LNET_LARGE_BUF_IDX],
1388                                                       nrb, i);
1389                         if (rc != 0)
1390                                 return rc;
1391                 }
1392         }
1393
1394         return 0;
1395 }
1396
1397 int
1398 lnet_rtrpools_adjust(int tiny, int small, int large)
1399 {
1400         /* this function doesn't revert the changes if adding new buffers
1401          * failed.  It's up to the user space caller to revert the
1402          * changes. */
1403
1404         if (!the_lnet.ln_routing)
1405                 return 0;
1406
1407         return lnet_rtrpools_adjust_helper(tiny, small, large);
1408 }
1409
1410 int
1411 lnet_rtrpools_enable(void)
1412 {
1413         int rc = 0;
1414
1415         if (the_lnet.ln_routing)
1416                 return 0;
1417
1418         if (the_lnet.ln_rtrpools == NULL)
1419                 /* If routing is turned off, and we have never
1420                  * initialized the pools before, just call the
1421                  * standard buffer pool allocation routine as
1422                  * if we are just configuring this for the first
1423                  * time. */
1424                 rc = lnet_rtrpools_alloc(1);
1425         else
1426                 rc = lnet_rtrpools_adjust_helper(0, 0, 0);
1427         if (rc != 0)
1428                 return rc;
1429
1430         lnet_net_lock(LNET_LOCK_EX);
1431         the_lnet.ln_routing = 1;
1432
1433         the_lnet.ln_ping_target->pb_info.pi_features &=
1434                 ~LNET_PING_FEAT_RTE_DISABLED;
1435         lnet_net_unlock(LNET_LOCK_EX);
1436
1437         return rc;
1438 }
1439
1440 void
1441 lnet_rtrpools_disable(void)
1442 {
1443         if (!the_lnet.ln_routing)
1444                 return;
1445
1446         lnet_net_lock(LNET_LOCK_EX);
1447         the_lnet.ln_routing = 0;
1448         the_lnet.ln_ping_target->pb_info.pi_features |=
1449                 LNET_PING_FEAT_RTE_DISABLED;
1450
1451         tiny_router_buffers = 0;
1452         small_router_buffers = 0;
1453         large_router_buffers = 0;
1454         lnet_net_unlock(LNET_LOCK_EX);
1455         lnet_rtrpools_free(1);
1456 }
1457
1458 int
1459 lnet_notify(struct lnet_ni *ni, lnet_nid_t nid, int alive, time64_t when)
1460 {
1461         struct lnet_peer_ni *lp = NULL;
1462         time64_t now = ktime_get_seconds();
1463         int cpt = lnet_cpt_of_nid(nid, ni);
1464
1465         LASSERT (!in_interrupt ());
1466
1467         CDEBUG (D_NET, "%s notifying %s: %s\n",
1468                 (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1469                 libcfs_nid2str(nid),
1470                 alive ? "up" : "down");
1471
1472         if (ni != NULL &&
1473             LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid)) {
1474                 CWARN("Ignoring notification of %s %s by %s (different net)\n",
1475                       libcfs_nid2str(nid), alive ? "birth" : "death",
1476                       libcfs_nid2str(ni->ni_nid));
1477                 return -EINVAL;
1478         }
1479
1480         /* can't do predictions... */
1481         if (when > now) {
1482                 CWARN("Ignoring prediction from %s of %s %s "
1483                       "%lld seconds in the future\n",
1484                       (ni == NULL) ? "userspace" : libcfs_nid2str(ni->ni_nid),
1485                       libcfs_nid2str(nid), alive ? "up" : "down", when - now);
1486                 return -EINVAL;
1487         }
1488
1489         if (ni != NULL && !alive &&             /* LND telling me she's down */
1490             !auto_down) {                       /* auto-down disabled */
1491                 CDEBUG(D_NET, "Auto-down disabled\n");
1492                 return 0;
1493         }
1494
1495         lnet_net_lock(cpt);
1496
1497         if (the_lnet.ln_state != LNET_STATE_RUNNING) {
1498                 lnet_net_unlock(cpt);
1499                 return -ESHUTDOWN;
1500         }
1501
1502         lp = lnet_find_peer_ni_locked(nid);
1503         if (lp == NULL) {
1504                 /* nid not found */
1505                 lnet_net_unlock(cpt);
1506                 CDEBUG(D_NET, "%s not found\n", libcfs_nid2str(nid));
1507                 return 0;
1508         }
1509
1510         /*
1511          * It is possible for this function to be called for the same peer
1512          * but with different NIs. We want to synchronize the notification
1513          * between the different calls. So we will use the lpni_cpt to
1514          * grab the net lock.
1515          */
1516         if (lp->lpni_cpt != cpt) {
1517                 lnet_net_unlock(cpt);
1518                 cpt = lp->lpni_cpt;
1519                 lnet_net_lock(cpt);
1520         }
1521
1522         /* We can't fully trust LND on reporting exact peer last_alive
1523          * if he notifies us about dead peer. For example ksocklnd can
1524          * call us with when == _time_when_the_node_was_booted_ if
1525          * no connections were successfully established */
1526         if (ni != NULL && !alive && when < lp->lpni_last_alive)
1527                 when = lp->lpni_last_alive;
1528
1529         lnet_notify_locked(lp, ni == NULL, alive, when);
1530
1531         if (ni != NULL)
1532                 lnet_ni_notify_locked(ni, lp);
1533
1534         lnet_peer_ni_decref_locked(lp);
1535
1536         lnet_net_unlock(cpt);
1537         return 0;
1538 }
1539 EXPORT_SYMBOL(lnet_notify);