Whamcloud - gitweb
LU-8236 nrs: Add wildcard support for JOBID TBF rule 23/24523/7
authorQian Yingjin <qian@ddn.com>
Thu, 12 Jan 2017 02:50:10 +0000 (10:50 +0800)
committerOleg Drokin <oleg.drokin@intel.com>
Thu, 6 Apr 2017 01:00:25 +0000 (01:00 +0000)
This patch adds wildcard support for JOBID TBF rule.
The following kind of wildcard matching rules can be
used:
start ruleName jobid={dd.*} rate=20
start ruleName jobid={*.500} rate=10
start ruleName jobid={prog*.10*} rate=15

Signed-off-by: Qian Yingjin <qian@ddn.com>
Change-Id: I0a4c44fd15533dfccd9f6ab7374a0a7a24b1403e
Reviewed-on: https://review.whamcloud.com/24523
Tested-by: Jenkins
Tested-by: Maloo <hpdd-maloo@intel.com>
Reviewed-by: Andreas Dilger <andreas.dilger@intel.com>
Reviewed-by: Wang Shilong <wshilong@ddn.com>
lustre/include/lustre_nrs_tbf.h
lustre/ptlrpc/nrs_tbf.c

index e991909..6e0c736 100644 (file)
 struct nrs_tbf_head;
 struct nrs_tbf_cmd;
 
+#define NRS_TBF_MATCH_FULL     0x0000001
+#define NRS_TBF_MATCH_WILDCARD 0x0000002
+
 struct nrs_tbf_jobid {
        char            *tj_id;
+       __u32            tj_match_flag;
        struct list_head tj_linkage;
 };
 
index 9fb43e4..d3c6e9e 100644 (file)
@@ -809,9 +809,11 @@ nrs_tbf_jobid_list_free(struct list_head *jobid_list)
 }
 
 static int
-nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
+nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
 {
        struct nrs_tbf_jobid *jobid;
+       struct cfs_lstr res;
+       int rc;
 
        OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
        if (jobid == NULL)
@@ -824,17 +826,62 @@ nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
        }
 
        memcpy(jobid->tj_id, id->ls_str, id->ls_len);
+       rc = cfs_gettok(id, '*', &res);
+       if (rc == 0)
+               jobid->tj_match_flag = NRS_TBF_MATCH_FULL;
+       else
+               jobid->tj_match_flag = NRS_TBF_MATCH_WILDCARD;
+
        list_add_tail(&jobid->tj_linkage, jobid_list);
        return 0;
 }
 
+static bool
+cfs_match_wildcard(const char *pattern, const char *content)
+{
+       if (*pattern == '\0' && *content == '\0')
+               return true;
+
+       if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
+               return false;
+
+       while (*pattern == *content) {
+               pattern++;
+               content++;
+               if (*pattern == '\0' && *content == '\0')
+                       return true;
+
+               if (*pattern == '*' && *(pattern + 1) != '\0' &&
+                   *content == '\0')
+                       return false;
+       }
+
+       if (*pattern == '*')
+               return (cfs_match_wildcard(pattern + 1, content) ||
+                       cfs_match_wildcard(pattern, content + 1));
+
+       return false;
+}
+
+static inline bool
+nrs_tbf_jobid_match(const struct nrs_tbf_jobid *jobid, const char *id)
+{
+       if (jobid->tj_match_flag == NRS_TBF_MATCH_FULL)
+               return strcmp(jobid->tj_id, id) == 0;
+
+       if (jobid->tj_match_flag == NRS_TBF_MATCH_WILDCARD)
+               return cfs_match_wildcard(jobid->tj_id, id);
+
+       return false;
+}
+
 static int
 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
 {
        struct nrs_tbf_jobid *jobid;
 
        list_for_each_entry(jobid, jobid_list, tj_linkage) {
-               if (strcmp(id, jobid->tj_id) == 0)
+               if (nrs_tbf_jobid_match(jobid, id))
                        return 1;
        }
        return 0;