4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2014, 2017, Intel Corporation.
27 * This file is part of Lustre, http://www.lustre.org/
29 * lnet/lnet/net_fault.c
31 * Lustre network fault simulation
33 * Author: liang.zhen@intel.com
36 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/random.h>
39 #include <lnet/lib-lnet.h>
40 #include <uapi/linux/lnet/lnetctl.h>
42 #define LNET_MSG_MASK (LNET_PUT_BIT | LNET_ACK_BIT | \
43 LNET_GET_BIT | LNET_REPLY_BIT)
45 struct lnet_drop_rule {
46 /** link chain on the_lnet.ln_drop_rules */
47 struct list_head dr_link;
48 /** attributes of this rule */
49 struct lnet_fault_attr dr_attr;
50 /** lock to protect \a dr_drop_at and \a dr_stat */
53 * the message sequence to drop, which means message is dropped when
54 * dr_stat.drs_count == dr_drop_at
56 unsigned long dr_drop_at;
58 * seconds to drop the next message, it's exclusive with dr_drop_at
60 time64_t dr_drop_time;
61 /** baseline to caculate dr_drop_time */
62 time64_t dr_time_base;
63 /** statistic of dropped messages */
64 struct lnet_fault_stat dr_stat;
68 lnet_fault_nid_match(lnet_nid_t nid, lnet_nid_t msg_nid)
70 if (nid == msg_nid || nid == LNET_NID_ANY)
73 if (LNET_NIDNET(nid) != LNET_NIDNET(msg_nid))
76 /* 255.255.255.255@net is wildcard for all addresses in a network */
77 return LNET_NIDADDR(nid) == LNET_NIDADDR(LNET_NID_ANY);
81 lnet_fault_attr_match(struct lnet_fault_attr *attr, lnet_nid_t src,
82 lnet_nid_t local_nid, lnet_nid_t dst,
83 unsigned int type, unsigned int portal)
85 if (!lnet_fault_nid_match(attr->fa_src, src) ||
86 !lnet_fault_nid_match(attr->fa_dst, dst) ||
87 !lnet_fault_nid_match(attr->fa_local_nid, local_nid))
90 if (!(attr->fa_msg_mask & BIT(type)))
93 /* NB: ACK and REPLY have no portal, but they should have been
94 * rejected by message mask */
95 if (attr->fa_ptl_mask != 0 && /* has portal filter */
96 !(attr->fa_ptl_mask & (1ULL << portal)))
103 lnet_fault_attr_validate(struct lnet_fault_attr *attr)
105 if (attr->fa_msg_mask == 0)
106 attr->fa_msg_mask = LNET_MSG_MASK; /* all message types */
108 if (attr->fa_ptl_mask == 0) /* no portal filter */
111 /* NB: only PUT and GET can be filtered if portal filter has been set */
112 attr->fa_msg_mask &= LNET_GET_BIT | LNET_PUT_BIT;
113 if (attr->fa_msg_mask == 0) {
114 CDEBUG(D_NET, "can't find valid message type bits %x\n",
122 lnet_fault_stat_inc(struct lnet_fault_stat *stat, unsigned int type)
124 /* NB: fs_counter is NOT updated by this function */
142 * LNet message drop simulation
146 * Add a new drop rule to LNet
147 * There is no check for duplicated drop rule, all rules will be checked for
151 lnet_drop_rule_add(struct lnet_fault_attr *attr)
153 struct lnet_drop_rule *rule;
156 if (!((attr->u.drop.da_rate == 0) ^ (attr->u.drop.da_interval == 0))) {
158 "please provide either drop rate or drop interval, "
159 "but not both at the same time %d/%d\n",
160 attr->u.drop.da_rate, attr->u.drop.da_interval);
164 if (lnet_fault_attr_validate(attr) != 0)
171 spin_lock_init(&rule->dr_lock);
173 rule->dr_attr = *attr;
174 if (attr->u.drop.da_interval != 0) {
175 rule->dr_time_base = ktime_get_seconds() + attr->u.drop.da_interval;
176 rule->dr_drop_time = ktime_get_seconds() +
177 prandom_u32_max(attr->u.drop.da_interval);
179 rule->dr_drop_at = prandom_u32_max(attr->u.drop.da_rate);
182 lnet_net_lock(LNET_LOCK_EX);
183 list_add(&rule->dr_link, &the_lnet.ln_drop_rules);
184 lnet_net_unlock(LNET_LOCK_EX);
186 CDEBUG(D_NET, "Added drop rule: src %s, dst %s, rate %d, interval %d\n",
187 libcfs_nid2str(attr->fa_src), libcfs_nid2str(attr->fa_src),
188 attr->u.drop.da_rate, attr->u.drop.da_interval);
193 * Remove matched drop rules from lnet, all rules that can match \a src and
194 * \a dst will be removed.
195 * If \a src is zero, then all rules have \a dst as destination will be remove
196 * If \a dst is zero, then all rules have \a src as source will be removed
197 * If both of them are zero, all rules will be removed
200 lnet_drop_rule_del(lnet_nid_t src, lnet_nid_t dst)
202 struct lnet_drop_rule *rule;
203 struct lnet_drop_rule *tmp;
208 lnet_net_lock(LNET_LOCK_EX);
209 list_for_each_entry_safe(rule, tmp, &the_lnet.ln_drop_rules, dr_link) {
210 if (rule->dr_attr.fa_src != src && src != 0)
213 if (rule->dr_attr.fa_dst != dst && dst != 0)
216 list_move(&rule->dr_link, &zombies);
218 lnet_net_unlock(LNET_LOCK_EX);
220 list_for_each_entry_safe(rule, tmp, &zombies, dr_link) {
221 CDEBUG(D_NET, "Remove drop rule: src %s->dst: %s (1/%d, %d)\n",
222 libcfs_nid2str(rule->dr_attr.fa_src),
223 libcfs_nid2str(rule->dr_attr.fa_dst),
224 rule->dr_attr.u.drop.da_rate,
225 rule->dr_attr.u.drop.da_interval);
227 list_del(&rule->dr_link);
236 * List drop rule at position of \a pos
239 lnet_drop_rule_list(int pos, struct lnet_fault_attr *attr,
240 struct lnet_fault_stat *stat)
242 struct lnet_drop_rule *rule;
248 cpt = lnet_net_lock_current();
249 list_for_each_entry(rule, &the_lnet.ln_drop_rules, dr_link) {
253 spin_lock(&rule->dr_lock);
254 *attr = rule->dr_attr;
255 *stat = rule->dr_stat;
256 spin_unlock(&rule->dr_lock);
261 lnet_net_unlock(cpt);
266 * reset counters for all drop rules
269 lnet_drop_rule_reset(void)
271 struct lnet_drop_rule *rule;
275 cpt = lnet_net_lock_current();
277 list_for_each_entry(rule, &the_lnet.ln_drop_rules, dr_link) {
278 struct lnet_fault_attr *attr = &rule->dr_attr;
280 spin_lock(&rule->dr_lock);
282 memset(&rule->dr_stat, 0, sizeof(rule->dr_stat));
283 if (attr->u.drop.da_rate != 0) {
284 rule->dr_drop_at = prandom_u32_max(attr->u.drop.da_rate);
286 rule->dr_drop_time = ktime_get_seconds() +
287 prandom_u32_max(attr->u.drop.da_interval);
288 rule->dr_time_base = ktime_get_seconds() + attr->u.drop.da_interval;
290 spin_unlock(&rule->dr_lock);
293 lnet_net_unlock(cpt);
298 lnet_fault_match_health(enum lnet_msg_hstatus *hstatus, __u32 mask)
305 /* assign a random failure */
306 choice = prandom_u32_max(LNET_MSG_STATUS_END - LNET_MSG_STATUS_OK);
310 if (mask == HSTATUS_RANDOM) {
315 if (mask & BIT(choice)) {
320 /* round to the closest ON bit */
322 best_delta = HSTATUS_END;
328 if (delta < best_delta) {
340 * check source/destination NID, portal, message type and drop rate,
341 * decide whether should drop this message or not
344 drop_rule_match(struct lnet_drop_rule *rule, lnet_nid_t src,
345 lnet_nid_t local_nid, lnet_nid_t dst,
346 unsigned int type, unsigned int portal,
347 enum lnet_msg_hstatus *hstatus)
349 struct lnet_fault_attr *attr = &rule->dr_attr;
352 if (!lnet_fault_attr_match(attr, src, local_nid, dst, type, portal))
355 if (attr->u.drop.da_drop_all) {
356 CDEBUG(D_NET, "set to drop all messages\n");
362 * if we're trying to match a health status error but it hasn't
363 * been set in the rule, then don't match
365 if ((hstatus && !attr->u.drop.da_health_error_mask) ||
366 (!hstatus && attr->u.drop.da_health_error_mask))
369 /* match this rule, check drop rate now */
370 spin_lock(&rule->dr_lock);
371 if (attr->u.drop.da_random) {
372 int value = prandom_u32_max(attr->u.drop.da_interval);
373 if (value >= (attr->u.drop.da_interval / 2))
377 } else if (rule->dr_drop_time != 0) { /* time based drop */
378 time64_t now = ktime_get_seconds();
380 rule->dr_stat.fs_count++;
381 drop = now >= rule->dr_drop_time;
383 if (now > rule->dr_time_base)
384 rule->dr_time_base = now;
386 rule->dr_drop_time = rule->dr_time_base +
387 prandom_u32_max(attr->u.drop.da_interval);
388 rule->dr_time_base += attr->u.drop.da_interval;
390 CDEBUG(D_NET, "Drop Rule %s->%s: next drop : %lld\n",
391 libcfs_nid2str(attr->fa_src),
392 libcfs_nid2str(attr->fa_dst),
396 } else { /* rate based drop */
399 drop = rule->dr_stat.fs_count++ == rule->dr_drop_at;
400 count = rule->dr_stat.fs_count;
401 if (do_div(count, attr->u.drop.da_rate) == 0) {
402 rule->dr_drop_at = rule->dr_stat.fs_count +
403 prandom_u32_max(attr->u.drop.da_rate);
404 CDEBUG(D_NET, "Drop Rule %s->%s: next drop: %lu\n",
405 libcfs_nid2str(attr->fa_src),
406 libcfs_nid2str(attr->fa_dst), rule->dr_drop_at);
412 if (drop) { /* drop this message, update counters */
414 lnet_fault_match_health(hstatus,
415 attr->u.drop.da_health_error_mask);
416 lnet_fault_stat_inc(&rule->dr_stat, type);
417 rule->dr_stat.u.drop.ds_dropped++;
420 spin_unlock(&rule->dr_lock);
425 * Check if message from \a src to \a dst can match any existed drop rule
428 lnet_drop_rule_match(struct lnet_hdr *hdr,
429 lnet_nid_t local_nid,
430 enum lnet_msg_hstatus *hstatus)
432 lnet_nid_t src = le64_to_cpu(hdr->src_nid);
433 lnet_nid_t dst = le64_to_cpu(hdr->dest_nid);
434 unsigned int typ = le32_to_cpu(hdr->type);
435 struct lnet_drop_rule *rule;
436 unsigned int ptl = -1;
440 /* NB: if Portal is specified, then only PUT and GET will be
441 * filtered by drop rule */
442 if (typ == LNET_MSG_PUT)
443 ptl = le32_to_cpu(hdr->msg.put.ptl_index);
444 else if (typ == LNET_MSG_GET)
445 ptl = le32_to_cpu(hdr->msg.get.ptl_index);
447 cpt = lnet_net_lock_current();
448 list_for_each_entry(rule, &the_lnet.ln_drop_rules, dr_link) {
449 drop = drop_rule_match(rule, src, local_nid, dst, typ, ptl,
454 lnet_net_unlock(cpt);
460 * LNet Delay Simulation
462 /** timestamp (second) to send delayed message */
463 #define msg_delay_send msg_ev.hdr_data
465 struct lnet_delay_rule {
466 /** link chain on the_lnet.ln_delay_rules */
467 struct list_head dl_link;
468 /** link chain on delay_dd.dd_sched_rules */
469 struct list_head dl_sched_link;
470 /** attributes of this rule */
471 struct lnet_fault_attr dl_attr;
472 /** lock to protect \a below members */
474 /** refcount of delay rule */
475 atomic_t dl_refcount;
477 * the message sequence to delay, which means message is delayed when
478 * dl_stat.fs_count == dl_delay_at
480 unsigned long dl_delay_at;
482 * seconds to delay the next message, it's exclusive with dl_delay_at
484 time64_t dl_delay_time;
485 /** baseline to caculate dl_delay_time */
486 time64_t dl_time_base;
487 /** seconds until we send the next delayed message */
488 time64_t dl_msg_send;
489 /** delayed message list */
490 struct list_head dl_msg_list;
491 /** statistic of delayed messages */
492 struct lnet_fault_stat dl_stat;
493 /** timer to wakeup delay_daemon */
494 struct timer_list dl_timer;
497 struct delay_daemon_data {
498 /** serialise rule add/remove */
499 struct mutex dd_mutex;
500 /** protect rules on \a dd_sched_rules */
502 /** scheduled delay rules (by timer) */
503 struct list_head dd_sched_rules;
504 /** deamon thread sleeps at here */
505 wait_queue_head_t dd_waitq;
506 /** controler (lctl command) wait at here */
507 wait_queue_head_t dd_ctl_waitq;
508 /** deamon is running */
509 unsigned int dd_running;
510 /** deamon stopped */
511 unsigned int dd_stopped;
514 static struct delay_daemon_data delay_dd;
517 delay_rule_decref(struct lnet_delay_rule *rule)
519 if (atomic_dec_and_test(&rule->dl_refcount)) {
520 LASSERT(list_empty(&rule->dl_sched_link));
521 LASSERT(list_empty(&rule->dl_msg_list));
522 LASSERT(list_empty(&rule->dl_link));
529 * check source/destination NID, portal, message type and delay rate,
530 * decide whether should delay this message or not
533 delay_rule_match(struct lnet_delay_rule *rule, lnet_nid_t src,
534 lnet_nid_t dst, unsigned int type, unsigned int portal,
535 struct lnet_msg *msg)
537 struct lnet_fault_attr *attr = &rule->dl_attr;
540 if (!lnet_fault_attr_match(attr, src, LNET_NID_ANY,
544 /* match this rule, check delay rate now */
545 spin_lock(&rule->dl_lock);
546 if (rule->dl_delay_time != 0) { /* time based delay */
547 time64_t now = ktime_get_seconds();
549 rule->dl_stat.fs_count++;
550 delay = now >= rule->dl_delay_time;
552 if (now > rule->dl_time_base)
553 rule->dl_time_base = now;
555 rule->dl_delay_time = rule->dl_time_base +
556 prandom_u32_max(attr->u.delay.la_interval);
557 rule->dl_time_base += attr->u.delay.la_interval;
559 CDEBUG(D_NET, "Delay Rule %s->%s: next delay : %lld\n",
560 libcfs_nid2str(attr->fa_src),
561 libcfs_nid2str(attr->fa_dst),
562 rule->dl_delay_time);
565 } else { /* rate based delay */
568 delay = rule->dl_stat.fs_count++ == rule->dl_delay_at;
569 /* generate the next random rate sequence */
570 count = rule->dl_stat.fs_count;
571 if (do_div(count, attr->u.delay.la_rate) == 0) {
572 rule->dl_delay_at = rule->dl_stat.fs_count +
573 prandom_u32_max(attr->u.delay.la_rate);
574 CDEBUG(D_NET, "Delay Rule %s->%s: next delay: %lu\n",
575 libcfs_nid2str(attr->fa_src),
576 libcfs_nid2str(attr->fa_dst), rule->dl_delay_at);
581 spin_unlock(&rule->dl_lock);
585 /* delay this message, update counters */
586 lnet_fault_stat_inc(&rule->dl_stat, type);
587 rule->dl_stat.u.delay.ls_delayed++;
589 list_add_tail(&msg->msg_list, &rule->dl_msg_list);
590 msg->msg_delay_send = ktime_get_seconds() + attr->u.delay.la_latency;
591 if (rule->dl_msg_send == -1) {
592 rule->dl_msg_send = msg->msg_delay_send;
593 mod_timer(&rule->dl_timer,
594 jiffies + cfs_time_seconds(rule->dl_msg_send));
597 spin_unlock(&rule->dl_lock);
602 * check if \a msg can match any Delay Rule, receiving of this message
603 * will be delayed if there is a match.
606 lnet_delay_rule_match_locked(struct lnet_hdr *hdr, struct lnet_msg *msg)
608 struct lnet_delay_rule *rule;
609 lnet_nid_t src = le64_to_cpu(hdr->src_nid);
610 lnet_nid_t dst = le64_to_cpu(hdr->dest_nid);
611 unsigned int typ = le32_to_cpu(hdr->type);
612 unsigned int ptl = -1;
614 /* NB: called with hold of lnet_net_lock */
616 /* NB: if Portal is specified, then only PUT and GET will be
617 * filtered by delay rule */
618 if (typ == LNET_MSG_PUT)
619 ptl = le32_to_cpu(hdr->msg.put.ptl_index);
620 else if (typ == LNET_MSG_GET)
621 ptl = le32_to_cpu(hdr->msg.get.ptl_index);
623 list_for_each_entry(rule, &the_lnet.ln_delay_rules, dl_link) {
624 if (delay_rule_match(rule, src, dst, typ, ptl, msg))
631 /** check out delayed messages for send */
633 delayed_msg_check(struct lnet_delay_rule *rule, bool all,
634 struct list_head *msg_list)
636 struct lnet_msg *msg;
637 struct lnet_msg *tmp;
638 time64_t now = ktime_get_seconds();
640 if (!all && cfs_time_seconds(rule->dl_msg_send) > now)
643 spin_lock(&rule->dl_lock);
644 list_for_each_entry_safe(msg, tmp, &rule->dl_msg_list, msg_list) {
645 if (!all && msg->msg_delay_send > now)
648 msg->msg_delay_send = 0;
649 list_move_tail(&msg->msg_list, msg_list);
652 if (list_empty(&rule->dl_msg_list)) {
653 del_timer(&rule->dl_timer);
654 rule->dl_msg_send = -1;
656 } else if (!list_empty(msg_list)) {
657 /* dequeued some timedout messages, update timer for the
658 * next delayed message on rule */
659 msg = list_entry(rule->dl_msg_list.next,
660 struct lnet_msg, msg_list);
661 rule->dl_msg_send = msg->msg_delay_send;
662 mod_timer(&rule->dl_timer,
663 jiffies + cfs_time_seconds(rule->dl_msg_send));
665 spin_unlock(&rule->dl_lock);
669 delayed_msg_process(struct list_head *msg_list, bool drop)
671 struct lnet_msg *msg;
673 while (!list_empty(msg_list)) {
678 msg = list_entry(msg_list->next, struct lnet_msg, msg_list);
679 LASSERT(msg->msg_rxpeer != NULL);
680 LASSERT(msg->msg_rxni != NULL);
683 cpt = msg->msg_rx_cpt;
685 list_del_init(&msg->msg_list);
689 } else if (!msg->msg_routing) {
690 rc = lnet_parse_local(ni, msg);
696 rc = lnet_parse_forward_locked(ni, msg);
697 lnet_net_unlock(cpt);
701 lnet_ni_recv(ni, msg->msg_private, msg, 0,
702 0, msg->msg_len, msg->msg_len);
704 case LNET_CREDIT_WAIT:
706 default: /* failures */
711 lnet_drop_message(ni, cpt, msg->msg_private, msg->msg_len,
713 lnet_finalize(msg, rc);
718 * Process delayed messages for scheduled rules
719 * This function can either be called by delay_rule_daemon, or by lnet_finalise
722 lnet_delay_rule_check(void)
724 struct lnet_delay_rule *rule;
728 if (list_empty(&delay_dd.dd_sched_rules))
731 spin_lock_bh(&delay_dd.dd_lock);
732 if (list_empty(&delay_dd.dd_sched_rules)) {
733 spin_unlock_bh(&delay_dd.dd_lock);
737 rule = list_entry(delay_dd.dd_sched_rules.next,
738 struct lnet_delay_rule, dl_sched_link);
739 list_del_init(&rule->dl_sched_link);
740 spin_unlock_bh(&delay_dd.dd_lock);
742 delayed_msg_check(rule, false, &msgs);
743 delay_rule_decref(rule); /* -1 for delay_dd.dd_sched_rules */
746 if (!list_empty(&msgs))
747 delayed_msg_process(&msgs, false);
750 /** deamon thread to handle delayed messages */
752 lnet_delay_rule_daemon(void *arg)
754 delay_dd.dd_running = 1;
755 wake_up(&delay_dd.dd_ctl_waitq);
757 while (delay_dd.dd_running) {
758 wait_event_interruptible(delay_dd.dd_waitq,
759 !delay_dd.dd_running ||
760 !list_empty(&delay_dd.dd_sched_rules));
761 lnet_delay_rule_check();
764 /* in case more rules have been enqueued after my last check */
765 lnet_delay_rule_check();
766 delay_dd.dd_stopped = 1;
767 wake_up(&delay_dd.dd_ctl_waitq);
773 delay_timer_cb(cfs_timer_cb_arg_t data)
775 struct lnet_delay_rule *rule = cfs_from_timer(rule, data, dl_timer);
777 spin_lock_bh(&delay_dd.dd_lock);
778 if (list_empty(&rule->dl_sched_link) && delay_dd.dd_running) {
779 atomic_inc(&rule->dl_refcount);
780 list_add_tail(&rule->dl_sched_link, &delay_dd.dd_sched_rules);
781 wake_up(&delay_dd.dd_waitq);
783 spin_unlock_bh(&delay_dd.dd_lock);
787 * Add a new delay rule to LNet
788 * There is no check for duplicated delay rule, all rules will be checked for
792 lnet_delay_rule_add(struct lnet_fault_attr *attr)
794 struct lnet_delay_rule *rule;
798 if (!((attr->u.delay.la_rate == 0) ^
799 (attr->u.delay.la_interval == 0))) {
801 "please provide either delay rate or delay interval, "
802 "but not both at the same time %d/%d\n",
803 attr->u.delay.la_rate, attr->u.delay.la_interval);
807 if (attr->u.delay.la_latency == 0) {
808 CDEBUG(D_NET, "delay latency cannot be zero\n");
812 if (lnet_fault_attr_validate(attr) != 0)
819 mutex_lock(&delay_dd.dd_mutex);
820 if (!delay_dd.dd_running) {
821 struct task_struct *task;
823 /* NB: although LND threads will process delayed message
824 * in lnet_finalize, but there is no guarantee that LND
825 * threads will be waken up if no other message needs to
827 * Only one daemon thread, performance is not the concern
828 * of this simualation module.
830 task = kthread_run(lnet_delay_rule_daemon, NULL, "lnet_dd");
835 wait_event(delay_dd.dd_ctl_waitq, delay_dd.dd_running);
838 cfs_timer_setup(&rule->dl_timer, delay_timer_cb,
839 (unsigned long)rule, 0);
841 spin_lock_init(&rule->dl_lock);
842 INIT_LIST_HEAD(&rule->dl_msg_list);
843 INIT_LIST_HEAD(&rule->dl_sched_link);
845 rule->dl_attr = *attr;
846 if (attr->u.delay.la_interval != 0) {
847 rule->dl_time_base = ktime_get_seconds() +
848 attr->u.delay.la_interval;
849 rule->dl_delay_time = ktime_get_seconds() +
850 prandom_u32_max(attr->u.delay.la_interval);
852 rule->dl_delay_at = prandom_u32_max(attr->u.delay.la_rate);
855 rule->dl_msg_send = -1;
857 lnet_net_lock(LNET_LOCK_EX);
858 atomic_set(&rule->dl_refcount, 1);
859 list_add(&rule->dl_link, &the_lnet.ln_delay_rules);
860 lnet_net_unlock(LNET_LOCK_EX);
862 CDEBUG(D_NET, "Added delay rule: src %s, dst %s, rate %d\n",
863 libcfs_nid2str(attr->fa_src), libcfs_nid2str(attr->fa_src),
864 attr->u.delay.la_rate);
866 mutex_unlock(&delay_dd.dd_mutex);
869 mutex_unlock(&delay_dd.dd_mutex);
875 * Remove matched Delay Rules from lnet, if \a shutdown is true or both \a src
876 * and \a dst are zero, all rules will be removed, otherwise only matched rules
878 * If \a src is zero, then all rules have \a dst as destination will be remove
879 * If \a dst is zero, then all rules have \a src as source will be removed
881 * When a delay rule is removed, all delayed messages of this rule will be
882 * processed immediately.
885 lnet_delay_rule_del(lnet_nid_t src, lnet_nid_t dst, bool shutdown)
887 struct lnet_delay_rule *rule;
888 struct lnet_delay_rule *tmp;
889 LIST_HEAD(rule_list);
898 mutex_lock(&delay_dd.dd_mutex);
899 lnet_net_lock(LNET_LOCK_EX);
901 list_for_each_entry_safe(rule, tmp, &the_lnet.ln_delay_rules, dl_link) {
902 if (rule->dl_attr.fa_src != src && src != 0)
905 if (rule->dl_attr.fa_dst != dst && dst != 0)
908 CDEBUG(D_NET, "Remove delay rule: src %s->dst: %s (1/%d, %d)\n",
909 libcfs_nid2str(rule->dl_attr.fa_src),
910 libcfs_nid2str(rule->dl_attr.fa_dst),
911 rule->dl_attr.u.delay.la_rate,
912 rule->dl_attr.u.delay.la_interval);
913 /* refcount is taken over by rule_list */
914 list_move(&rule->dl_link, &rule_list);
917 /* check if we need to shutdown delay_daemon */
918 cleanup = list_empty(&the_lnet.ln_delay_rules) &&
919 !list_empty(&rule_list);
920 lnet_net_unlock(LNET_LOCK_EX);
922 list_for_each_entry_safe(rule, tmp, &rule_list, dl_link) {
923 list_del_init(&rule->dl_link);
925 del_timer_sync(&rule->dl_timer);
926 delayed_msg_check(rule, true, &msg_list);
927 delay_rule_decref(rule); /* -1 for the_lnet.ln_delay_rules */
931 if (cleanup) { /* no more delay rule, shutdown delay_daemon */
932 LASSERT(delay_dd.dd_running);
933 delay_dd.dd_running = 0;
934 wake_up(&delay_dd.dd_waitq);
936 while (!delay_dd.dd_stopped)
937 wait_event(delay_dd.dd_ctl_waitq, delay_dd.dd_stopped);
939 mutex_unlock(&delay_dd.dd_mutex);
941 if (!list_empty(&msg_list))
942 delayed_msg_process(&msg_list, shutdown);
948 * List Delay Rule at position of \a pos
951 lnet_delay_rule_list(int pos, struct lnet_fault_attr *attr,
952 struct lnet_fault_stat *stat)
954 struct lnet_delay_rule *rule;
960 cpt = lnet_net_lock_current();
961 list_for_each_entry(rule, &the_lnet.ln_delay_rules, dl_link) {
965 spin_lock(&rule->dl_lock);
966 *attr = rule->dl_attr;
967 *stat = rule->dl_stat;
968 spin_unlock(&rule->dl_lock);
973 lnet_net_unlock(cpt);
978 * reset counters for all Delay Rules
981 lnet_delay_rule_reset(void)
983 struct lnet_delay_rule *rule;
987 cpt = lnet_net_lock_current();
989 list_for_each_entry(rule, &the_lnet.ln_delay_rules, dl_link) {
990 struct lnet_fault_attr *attr = &rule->dl_attr;
992 spin_lock(&rule->dl_lock);
994 memset(&rule->dl_stat, 0, sizeof(rule->dl_stat));
995 if (attr->u.delay.la_rate != 0) {
996 rule->dl_delay_at = prandom_u32_max(attr->u.delay.la_rate);
998 rule->dl_delay_time = ktime_get_seconds() +
999 prandom_u32_max(attr->u.delay.la_interval);
1000 rule->dl_time_base = ktime_get_seconds() +
1001 attr->u.delay.la_interval;
1003 spin_unlock(&rule->dl_lock);
1006 lnet_net_unlock(cpt);
1011 lnet_fault_ctl(int opc, struct libcfs_ioctl_data *data)
1013 struct lnet_fault_attr *attr;
1014 struct lnet_fault_stat *stat;
1016 attr = (struct lnet_fault_attr *)data->ioc_inlbuf1;
1022 case LNET_CTL_DROP_ADD:
1026 return lnet_drop_rule_add(attr);
1028 case LNET_CTL_DROP_DEL:
1032 data->ioc_count = lnet_drop_rule_del(attr->fa_src,
1036 case LNET_CTL_DROP_RESET:
1037 lnet_drop_rule_reset();
1040 case LNET_CTL_DROP_LIST:
1041 stat = (struct lnet_fault_stat *)data->ioc_inlbuf2;
1042 if (attr == NULL || stat == NULL)
1045 return lnet_drop_rule_list(data->ioc_count, attr, stat);
1047 case LNET_CTL_DELAY_ADD:
1051 return lnet_delay_rule_add(attr);
1053 case LNET_CTL_DELAY_DEL:
1057 data->ioc_count = lnet_delay_rule_del(attr->fa_src,
1058 attr->fa_dst, false);
1061 case LNET_CTL_DELAY_RESET:
1062 lnet_delay_rule_reset();
1065 case LNET_CTL_DELAY_LIST:
1066 stat = (struct lnet_fault_stat *)data->ioc_inlbuf2;
1067 if (attr == NULL || stat == NULL)
1070 return lnet_delay_rule_list(data->ioc_count, attr, stat);
1075 lnet_fault_init(void)
1077 BUILD_BUG_ON(LNET_PUT_BIT != BIT(LNET_MSG_PUT));
1078 BUILD_BUG_ON(LNET_ACK_BIT != BIT(LNET_MSG_ACK));
1079 BUILD_BUG_ON(LNET_GET_BIT != BIT(LNET_MSG_GET));
1080 BUILD_BUG_ON(LNET_REPLY_BIT != BIT(LNET_MSG_REPLY));
1082 mutex_init(&delay_dd.dd_mutex);
1083 spin_lock_init(&delay_dd.dd_lock);
1084 init_waitqueue_head(&delay_dd.dd_waitq);
1085 init_waitqueue_head(&delay_dd.dd_ctl_waitq);
1086 INIT_LIST_HEAD(&delay_dd.dd_sched_rules);
1092 lnet_fault_fini(void)
1094 lnet_drop_rule_del(0, 0);
1095 lnet_delay_rule_del(0, 0, true);
1097 LASSERT(list_empty(&the_lnet.ln_drop_rules));
1098 LASSERT(list_empty(&the_lnet.ln_delay_rules));
1099 LASSERT(list_empty(&delay_dd.dd_sched_rules));