Whamcloud - gitweb
LU-12446 tests: Fix use of undefined variable under sanityn.sh/16b
[fs/lustre-release.git] / lnet / lnet / net_fault.c
index 398864e..7fdd8df 100644 (file)
@@ -79,10 +79,12 @@ lnet_fault_nid_match(lnet_nid_t nid, lnet_nid_t msg_nid)
 
 static bool
 lnet_fault_attr_match(struct lnet_fault_attr *attr, lnet_nid_t src,
-                     lnet_nid_t dst, unsigned int type, unsigned int portal)
+                     lnet_nid_t local_nid, lnet_nid_t dst,
+                     unsigned int type, unsigned int portal)
 {
        if (!lnet_fault_nid_match(attr->fa_src, src) ||
-           !lnet_fault_nid_match(attr->fa_dst, dst))
+           !lnet_fault_nid_match(attr->fa_dst, dst) ||
+           !lnet_fault_nid_match(attr->fa_local_nid, local_nid))
                return false;
 
        if (!(attr->fa_msg_mask & (1 << type)))
@@ -294,23 +296,89 @@ lnet_drop_rule_reset(void)
        EXIT;
 }
 
+static void
+lnet_fault_match_health(enum lnet_msg_hstatus *hstatus, __u32 mask)
+{
+       unsigned int random;
+       int choice;
+       int delta;
+       int best_delta;
+       int i;
+
+       /* assign a random failure */
+       random = cfs_rand();
+       choice = random % (LNET_MSG_STATUS_END - LNET_MSG_STATUS_OK);
+       if (choice == 0)
+               choice++;
+
+       if (mask == HSTATUS_RANDOM) {
+               *hstatus = choice;
+               return;
+       }
+
+       if (mask & (1 << choice)) {
+               *hstatus = choice;
+               return;
+       }
+
+       /* round to the closest ON bit */
+       i = HSTATUS_END;
+       best_delta = HSTATUS_END;
+       while (i > 0) {
+               if (mask & (1 << i)) {
+                       delta = choice - i;
+                       if (delta < 0)
+                               delta *= -1;
+                       if (delta < best_delta) {
+                               best_delta = delta;
+                               choice = i;
+                       }
+               }
+               i--;
+       }
+
+       *hstatus = choice;
+}
+
 /**
  * check source/destination NID, portal, message type and drop rate,
  * decide whether should drop this message or not
  */
 static bool
 drop_rule_match(struct lnet_drop_rule *rule, lnet_nid_t src,
-               lnet_nid_t dst, unsigned int type, unsigned int portal)
+               lnet_nid_t local_nid, lnet_nid_t dst,
+               unsigned int type, unsigned int portal,
+               enum lnet_msg_hstatus *hstatus)
 {
        struct lnet_fault_attr  *attr = &rule->dr_attr;
        bool                     drop;
 
-       if (!lnet_fault_attr_match(attr, src, dst, type, portal))
+       if (!lnet_fault_attr_match(attr, src, local_nid, dst, type, portal))
+               return false;
+
+       if (attr->u.drop.da_drop_all) {
+               CDEBUG(D_NET, "set to drop all messages\n");
+               drop = true;
+               goto drop_matched;
+       }
+
+       /*
+        * if we're trying to match a health status error but it hasn't
+        * been set in the rule, then don't match
+        */
+       if ((hstatus && !attr->u.drop.da_health_error_mask) ||
+           (!hstatus && attr->u.drop.da_health_error_mask))
                return false;
 
        /* match this rule, check drop rate now */
        spin_lock(&rule->dr_lock);
-       if (rule->dr_drop_time != 0) { /* time based drop */
+       if (attr->u.drop.da_random) {
+               int value = cfs_rand() % attr->u.drop.da_interval;
+               if (value >= (attr->u.drop.da_interval / 2))
+                       drop = true;
+               else
+                       drop = false;
+       } else if (rule->dr_drop_time != 0) { /* time based drop */
                time64_t now = ktime_get_seconds();
 
                rule->dr_stat.fs_count++;
@@ -343,7 +411,12 @@ drop_rule_match(struct lnet_drop_rule *rule, lnet_nid_t src,
                }
        }
 
+drop_matched:
+
        if (drop) { /* drop this message, update counters */
+               if (hstatus)
+                       lnet_fault_match_health(hstatus,
+                               attr->u.drop.da_health_error_mask);
                lnet_fault_stat_inc(&rule->dr_stat, type);
                rule->dr_stat.u.drop.ds_dropped++;
        }
@@ -356,15 +429,17 @@ drop_rule_match(struct lnet_drop_rule *rule, lnet_nid_t src,
  * Check if message from \a src to \a dst can match any existed drop rule
  */
 bool
-lnet_drop_rule_match(struct lnet_hdr *hdr)
+lnet_drop_rule_match(struct lnet_hdr *hdr,
+                    lnet_nid_t local_nid,
+                    enum lnet_msg_hstatus *hstatus)
 {
-       struct lnet_drop_rule   *rule;
-       lnet_nid_t               src = le64_to_cpu(hdr->src_nid);
-       lnet_nid_t               dst = le64_to_cpu(hdr->dest_nid);
-       unsigned int             typ = le32_to_cpu(hdr->type);
-       unsigned int             ptl = -1;
-       bool                     drop = false;
-       int                      cpt;
+       lnet_nid_t src = le64_to_cpu(hdr->src_nid);
+       lnet_nid_t dst = le64_to_cpu(hdr->dest_nid);
+       unsigned int typ = le32_to_cpu(hdr->type);
+       struct lnet_drop_rule *rule;
+       unsigned int ptl = -1;
+       bool drop = false;
+       int cpt;
 
        /* NB: if Portal is specified, then only PUT and GET will be
         * filtered by drop rule */
@@ -375,12 +450,13 @@ lnet_drop_rule_match(struct lnet_hdr *hdr)
 
        cpt = lnet_net_lock_current();
        list_for_each_entry(rule, &the_lnet.ln_drop_rules, dr_link) {
-               drop = drop_rule_match(rule, src, dst, typ, ptl);
+               drop = drop_rule_match(rule, src, local_nid, dst, typ, ptl,
+                                      hstatus);
                if (drop)
                        break;
        }
-
        lnet_net_unlock(cpt);
+
        return drop;
 }
 
@@ -465,7 +541,8 @@ delay_rule_match(struct lnet_delay_rule *rule, lnet_nid_t src,
        struct lnet_fault_attr  *attr = &rule->dl_attr;
        bool                     delay;
 
-       if (!lnet_fault_attr_match(attr, src, dst, type, portal))
+       if (!lnet_fault_attr_match(attr, src, LNET_NID_ANY,
+                                  dst, type, portal))
                return false;
 
        /* match this rule, check delay rate now */
@@ -695,9 +772,9 @@ lnet_delay_rule_daemon(void *arg)
 }
 
 static void
-delay_timer_cb(unsigned long arg)
+delay_timer_cb(cfs_timer_cb_arg_t data)
 {
-       struct lnet_delay_rule *rule = (struct lnet_delay_rule *)arg;
+       struct lnet_delay_rule *rule = cfs_from_timer(rule, data, dl_timer);
 
        spin_lock_bh(&delay_dd.dd_lock);
        if (list_empty(&rule->dl_sched_link) && delay_dd.dd_running) {
@@ -760,9 +837,8 @@ lnet_delay_rule_add(struct lnet_fault_attr *attr)
                wait_event(delay_dd.dd_ctl_waitq, delay_dd.dd_running);
        }
 
-       init_timer(&rule->dl_timer);
-       rule->dl_timer.function = delay_timer_cb;
-       rule->dl_timer.data = (unsigned long)rule;
+       cfs_timer_setup(&rule->dl_timer, delay_timer_cb,
+                       (unsigned long)rule, 0);
 
        spin_lock_init(&rule->dl_lock);
        INIT_LIST_HEAD(&rule->dl_msg_list);