/*
* Copyright (C) 2013 DataDirect Networks, Inc.
*
- * Copyright (c) 2014, Intel Corporation.
+ * Copyright (c) 2014, 2016, Intel Corporation.
*/
/*
* lustre/ptlrpc/nrs_tbf.c
#define NRS_POL_NAME_TBF "tbf"
static int tbf_jobid_cache_size = 8192;
-CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
- "The size of jobid cache");
+module_param(tbf_jobid_cache_size, int, 0644);
+MODULE_PARM_DESC(tbf_jobid_cache_size, "The size of jobid cache");
static int tbf_rate = 10000;
-CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
- "Default rate limit in RPCs/s");
+module_param(tbf_rate, int, 0644);
+MODULE_PARM_DESC(tbf_rate, "Default rate limit in RPCs/s");
static int tbf_depth = 3;
-CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
- "How many tokens that a client can save up");
+module_param(tbf_depth, int, 0644);
+MODULE_PARM_DESC(tbf_depth, "How many tokens that a client can save up");
static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
{
{
LASSERT(!list_empty(&cli->tc_linkage));
LASSERT(cli->tc_rule);
+ spin_lock(&cli->tc_rule->tr_rule_lock);
list_del_init(&cli->tc_linkage);
+ spin_unlock(&cli->tc_rule->tr_rule_lock);
nrs_tbf_rule_put(cli->tc_rule);
cli->tc_rule = NULL;
}
struct nrs_tbf_rule *rule,
struct nrs_tbf_client *cli)
{
- if (!list_empty(&cli->tc_linkage)) {
+ spin_lock(&cli->tc_rule_lock);
+ if (cli->tc_rule != NULL && !list_empty(&cli->tc_linkage)) {
LASSERT(rule != cli->tc_rule);
nrs_tbf_cli_rule_put(cli);
}
LASSERT(list_empty(&cli->tc_linkage));
/* Rule's ref is added before called */
cli->tc_rule = rule;
+ spin_lock(&rule->tr_rule_lock);
list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
+ spin_unlock(&rule->tr_rule_lock);
+ spin_unlock(&cli->tc_rule_lock);
nrs_tbf_cli_reset_value(head, cli);
}
{
struct nrs_tbf_rule *rule;
+ memset(cli, 0, sizeof(*cli));
cli->tc_in_heap = false;
head->th_ops->o_cli_init(cli, req);
INIT_LIST_HEAD(&cli->tc_list);
INIT_LIST_HEAD(&cli->tc_linkage);
+ spin_lock_init(&cli->tc_rule_lock);
atomic_set(&cli->tc_ref, 1);
rule = nrs_tbf_rule_match(head, cli);
nrs_tbf_cli_reset(head, rule, cli);
LASSERT(list_empty(&cli->tc_list));
LASSERT(!cli->tc_in_heap);
LASSERT(atomic_read(&cli->tc_ref) == 0);
+ spin_lock(&cli->tc_rule_lock);
nrs_tbf_cli_rule_put(cli);
+ spin_unlock(&cli->tc_rule_lock);
OBD_FREE_PTR(cli);
}
struct nrs_tbf_head *head,
struct nrs_tbf_cmd *start)
{
- struct nrs_tbf_rule *rule, *tmp_rule;
- int rc;
+ struct nrs_tbf_rule *rule;
+ struct nrs_tbf_rule *tmp_rule;
+ struct nrs_tbf_rule *next_rule;
+ char *next_name = start->u.tc_start.ts_next_name;
+ int rc;
rule = nrs_tbf_rule_find(head, start->tc_name);
if (rule) {
return -ENOMEM;
memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
- rule->tr_rpc_rate = start->tc_rpc_rate;
- rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
+ rule->tr_rpc_rate = start->u.tc_start.ts_rpc_rate;
+ rule->tr_nsecs = NSEC_PER_SEC;
+ do_div(rule->tr_nsecs, rule->tr_rpc_rate);
rule->tr_depth = tbf_depth;
atomic_set(&rule->tr_ref, 1);
INIT_LIST_HEAD(&rule->tr_cli_list);
INIT_LIST_HEAD(&rule->tr_nids);
+ INIT_LIST_HEAD(&rule->tr_linkage);
+ spin_lock_init(&rule->tr_rule_lock);
+ rule->tr_head = head;
rc = head->th_ops->o_rule_init(policy, rule, start);
if (rc) {
nrs_tbf_rule_put(rule);
return -EEXIST;
}
- list_add(&rule->tr_linkage, &head->th_list);
- rule->tr_head = head;
+
+ if (next_name) {
+ next_rule = nrs_tbf_rule_find_nolock(head, next_name);
+ if (!next_rule) {
+ spin_unlock(&head->th_rule_lock);
+ nrs_tbf_rule_put(rule);
+ return -ENOENT;
+ }
+
+ list_add(&rule->tr_linkage, next_rule->tr_linkage.prev);
+ nrs_tbf_rule_put(next_rule);
+ } else {
+ /* Add on the top of the rule list */
+ list_add(&rule->tr_linkage, &head->th_list);
+ }
spin_unlock(&head->th_rule_lock);
atomic_inc(&head->th_rule_sequence);
- if (start->tc_rule_flags & NTRS_DEFAULT) {
+ if (start->u.tc_start.ts_rule_flags & NTRS_DEFAULT) {
rule->tr_flags |= NTRS_DEFAULT;
LASSERT(head->th_rule == NULL);
head->th_rule = rule;
return 0;
}
+/**
+ * Change the rank of a rule in the rule list
+ *
+ * The matched rule will be moved to the position right before another
+ * given rule.
+ *
+ * \param[in] policy the policy instance
+ * \param[in] head the TBF policy instance
+ * \param[in] name the rule name to be moved
+ * \param[in] next_name the rule name before which the matched rule will be
+ * moved
+ *
+ */
static int
-nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
- struct nrs_tbf_head *head,
- struct nrs_tbf_cmd *change)
+nrs_tbf_rule_change_rank(struct ptlrpc_nrs_policy *policy,
+ struct nrs_tbf_head *head,
+ char *name,
+ char *next_name)
+{
+ struct nrs_tbf_rule *rule = NULL;
+ struct nrs_tbf_rule *next_rule = NULL;
+ int rc = 0;
+
+ LASSERT(head != NULL);
+
+ spin_lock(&head->th_rule_lock);
+ rule = nrs_tbf_rule_find_nolock(head, name);
+ if (!rule)
+ GOTO(out, rc = -ENOENT);
+
+ if (strcmp(name, next_name) == 0)
+ GOTO(out_put, rc);
+
+ next_rule = nrs_tbf_rule_find_nolock(head, next_name);
+ if (!next_rule)
+ GOTO(out_put, rc = -ENOENT);
+
+ list_move(&rule->tr_linkage, next_rule->tr_linkage.prev);
+ nrs_tbf_rule_put(next_rule);
+out_put:
+ nrs_tbf_rule_put(rule);
+out:
+ spin_unlock(&head->th_rule_lock);
+ return rc;
+}
+
+static int
+nrs_tbf_rule_change_rate(struct ptlrpc_nrs_policy *policy,
+ struct nrs_tbf_head *head,
+ char *name,
+ __u64 rate)
{
struct nrs_tbf_rule *rule;
assert_spin_locked(&policy->pol_nrs->nrs_lock);
- rule = nrs_tbf_rule_find(head, change->tc_name);
+ rule = nrs_tbf_rule_find(head, name);
if (rule == NULL)
return -ENOENT;
- rule->tr_rpc_rate = change->tc_rpc_rate;
- rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
+ rule->tr_rpc_rate = rate;
+ rule->tr_nsecs = NSEC_PER_SEC;
+ do_div(rule->tr_nsecs, rule->tr_rpc_rate);
rule->tr_generation++;
nrs_tbf_rule_put(rule);
}
static int
+nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
+ struct nrs_tbf_head *head,
+ struct nrs_tbf_cmd *change)
+{
+ __u64 rate = change->u.tc_change.tc_rpc_rate;
+ char *next_name = change->u.tc_change.tc_next_name;
+ int rc;
+
+ if (rate != 0) {
+ rc = nrs_tbf_rule_change_rate(policy, head, change->tc_name,
+ rate);
+ if (rc)
+ return rc;
+ }
+
+ if (next_name) {
+ rc = nrs_tbf_rule_change_rank(policy, head, change->tc_name,
+ next_name);
+ if (rc)
+ return rc;
+ }
+
+ return 0;
+}
+
+static int
nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
struct nrs_tbf_head *head,
struct nrs_tbf_cmd *stop)
switch (cmd->tc_cmd) {
case NRS_CTL_TBF_START_RULE:
- if (!(cmd->tc_valid_types & head->th_type_flag))
+ if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
return -EINVAL;
spin_unlock(&policy->pol_nrs->nrs_lock);
rc = nrs_tbf_rule_start(policy, head, cmd);
spin_lock(&policy->pol_nrs->nrs_lock);
return rc;
- case NRS_CTL_TBF_CHANGE_RATE:
+ case NRS_CTL_TBF_CHANGE_RULE:
rc = nrs_tbf_rule_change(policy, head, cmd);
return rc;
case NRS_CTL_TBF_STOP_RULE:
* \retval 0 e1 > e2
* \retval 1 e1 < e2
*/
-static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
+static int
+tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
{
struct nrs_tbf_client *cli1;
struct nrs_tbf_client *cli2;
/**
* TBF binary heap operations
*/
-static cfs_binheap_ops_t nrs_tbf_heap_ops = {
+static struct cfs_binheap_ops nrs_tbf_heap_ops = {
.hop_enter = NULL,
.hop_exit = NULL,
.hop_compare = tbf_cli_compare,
}
memset(&start, 0, sizeof(start));
- start.tc_jobids_str = "*";
+ start.u.tc_start.ts_jobids_str = "*";
- start.tc_rpc_rate = tbf_rate;
- start.tc_rule_flags = NTRS_DEFAULT;
+ start.u.tc_start.ts_rpc_rate = tbf_rate;
+ start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
start.tc_name = NRS_TBF_DEFAULT_RULE;
- INIT_LIST_HEAD(&start.tc_jobids);
+ INIT_LIST_HEAD(&start.u.tc_start.ts_jobids);
rc = nrs_tbf_rule_start(policy, head, &start);
+ if (rc) {
+ cfs_hash_putref(head->th_cli_hash);
+ head->th_cli_hash = NULL;
+ }
return rc;
}
static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
{
- if (!list_empty(&cmd->tc_jobids))
- nrs_tbf_jobid_list_free(&cmd->tc_jobids);
- if (cmd->tc_jobids_str)
- OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
+ if (!list_empty(&cmd->u.tc_start.ts_jobids))
+ nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
+ if (cmd->u.tc_start.ts_jobids_str)
+ OBD_FREE(cmd->u.tc_start.ts_jobids_str,
+ strlen(cmd->u.tc_start.ts_jobids_str) + 1);
+}
+
+static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
+{
+ struct cfs_lstr res;
+ int keylen = strlen(key);
+ int rc;
+
+ rc = cfs_gettok(src, '=', &res);
+ if (rc == 0 || res.ls_len != keylen ||
+ strncmp(res.ls_str, key, keylen) != 0 ||
+ src->ls_len <= 2 || src->ls_str[0] != '{' ||
+ src->ls_str[src->ls_len - 1] != '}')
+ return -EINVAL;
+
+ /* Skip '{' and '}' */
+ src->ls_str++;
+ src->ls_len -= 2;
+ return 0;
}
-static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
+static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
{
+ struct cfs_lstr src;
int rc;
- OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
- if (cmd->tc_jobids_str == NULL)
+ src.ls_str = id;
+ src.ls_len = strlen(id);
+ rc = nrs_tbf_check_id_value(&src, "jobid");
+ if (rc)
+ return rc;
+
+ OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
+ if (cmd->u.tc_start.ts_jobids_str == NULL)
return -ENOMEM;
- memcpy(cmd->tc_jobids_str, id, strlen(id));
+ memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
/* parse jobid list */
- rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
- strlen(cmd->tc_jobids_str),
- &cmd->tc_jobids);
+ rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
+ strlen(cmd->u.tc_start.ts_jobids_str),
+ &cmd->u.tc_start.ts_jobids);
if (rc)
nrs_tbf_jobid_cmd_fini(cmd);
{
int rc = 0;
- LASSERT(start->tc_jobids_str);
+ LASSERT(start->u.tc_start.ts_jobids_str);
OBD_ALLOC(rule->tr_jobids_str,
- strlen(start->tc_jobids_str) + 1);
+ strlen(start->u.tc_start.ts_jobids_str) + 1);
if (rule->tr_jobids_str == NULL)
return -ENOMEM;
memcpy(rule->tr_jobids_str,
- start->tc_jobids_str,
- strlen(start->tc_jobids_str));
+ start->u.tc_start.ts_jobids_str,
+ strlen(start->u.tc_start.ts_jobids_str));
INIT_LIST_HEAD(&rule->tr_jobids);
- if (!list_empty(&start->tc_jobids)) {
+ if (!list_empty(&start->u.tc_start.ts_jobids)) {
rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
strlen(rule->tr_jobids_str),
&rule->tr_jobids);
}
if (rc)
OBD_FREE(rule->tr_jobids_str,
- strlen(start->tc_jobids_str) + 1);
+ strlen(start->u.tc_start.ts_jobids_str) + 1);
return rc;
}
static int
nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
{
- return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
- rule->tr_jobids_str, rule->tr_rpc_rate,
- atomic_read(&rule->tr_ref) - 1);
+ seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
+ rule->tr_jobids_str, rule->tr_rpc_rate,
+ atomic_read(&rule->tr_ref) - 1);
+ return 0;
}
static int
* This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
* nrs_tbf_client objects.
*/
-#define NRS_TBF_NID_BKT_BITS 8
-#define NRS_TBF_NID_BITS 16
+#define NRS_TBF_NID_BKT_BITS 8
+#define NRS_TBF_NID_BITS 16
static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
unsigned mask)
return -ENOMEM;
memset(&start, 0, sizeof(start));
- start.tc_nids_str = "*";
+ start.u.tc_start.ts_nids_str = "*";
- start.tc_rpc_rate = tbf_rate;
- start.tc_rule_flags = NTRS_DEFAULT;
+ start.u.tc_start.ts_rpc_rate = tbf_rate;
+ start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
start.tc_name = NRS_TBF_DEFAULT_RULE;
- INIT_LIST_HEAD(&start.tc_nids);
+ INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
rc = nrs_tbf_rule_start(policy, head, &start);
+ if (rc) {
+ cfs_hash_putref(head->th_cli_hash);
+ head->th_cli_hash = NULL;
+ }
return rc;
}
struct nrs_tbf_rule *rule,
struct nrs_tbf_cmd *start)
{
- LASSERT(start->tc_nids_str);
+ LASSERT(start->u.tc_start.ts_nids_str);
OBD_ALLOC(rule->tr_nids_str,
- strlen(start->tc_nids_str) + 1);
+ strlen(start->u.tc_start.ts_nids_str) + 1);
if (rule->tr_nids_str == NULL)
return -ENOMEM;
memcpy(rule->tr_nids_str,
- start->tc_nids_str,
- strlen(start->tc_nids_str));
+ start->u.tc_start.ts_nids_str,
+ strlen(start->u.tc_start.ts_nids_str));
INIT_LIST_HEAD(&rule->tr_nids);
- if (!list_empty(&start->tc_nids)) {
+ if (!list_empty(&start->u.tc_start.ts_nids)) {
if (cfs_parse_nidlist(rule->tr_nids_str,
strlen(rule->tr_nids_str),
&rule->tr_nids) <= 0) {
CERROR("nids {%s} illegal\n",
rule->tr_nids_str);
OBD_FREE(rule->tr_nids_str,
- strlen(start->tc_nids_str) + 1);
+ strlen(start->u.tc_start.ts_nids_str) + 1);
return -EINVAL;
}
}
static int
nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
{
- return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
- rule->tr_nids_str, rule->tr_rpc_rate,
- atomic_read(&rule->tr_ref) - 1);
+ seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
+ rule->tr_nids_str, rule->tr_rpc_rate,
+ atomic_read(&rule->tr_ref) - 1);
+ return 0;
}
static int
static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
{
- if (!list_empty(&cmd->tc_nids))
- cfs_free_nidlist(&cmd->tc_nids);
- if (cmd->tc_nids_str)
- OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
+ if (!list_empty(&cmd->u.tc_start.ts_nids))
+ cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
+ if (cmd->u.tc_start.ts_nids_str)
+ OBD_FREE(cmd->u.tc_start.ts_nids_str,
+ strlen(cmd->u.tc_start.ts_nids_str) + 1);
}
-static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
+static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
{
- OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
- if (cmd->tc_nids_str == NULL)
+ struct cfs_lstr src;
+ int rc;
+
+ src.ls_str = id;
+ src.ls_len = strlen(id);
+ rc = nrs_tbf_check_id_value(&src, "nid");
+ if (rc)
+ return rc;
+
+ OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
+ if (cmd->u.tc_start.ts_nids_str == NULL)
return -ENOMEM;
- memcpy(cmd->tc_nids_str, id, strlen(id));
+ memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
/* parse NID list */
- if (cfs_parse_nidlist(cmd->tc_nids_str,
- strlen(cmd->tc_nids_str),
- &cmd->tc_nids) <= 0) {
+ if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
+ strlen(cmd->u.tc_start.ts_nids_str),
+ &cmd->u.tc_start.ts_nids) <= 0) {
nrs_tbf_nid_cmd_fini(cmd);
return -EINVAL;
}
.o_rule_fini = nrs_tbf_nid_rule_fini,
};
+static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
+{
+ if (rule->tr_opcodes != NULL)
+ CFS_FREE_BITMAP(rule->tr_opcodes);
+
+ LASSERT(rule->tr_opcodes_str != NULL);
+ OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
+}
+
+static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
+ unsigned mask)
+{
+ return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
+}
+
+static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
+{
+ const __u32 *opc = key;
+ struct nrs_tbf_client *cli = hlist_entry(hnode,
+ struct nrs_tbf_client,
+ tc_hnode);
+
+ return *opc == cli->tc_opcode;
+}
+
+static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
+{
+ struct nrs_tbf_client *cli = hlist_entry(hnode,
+ struct nrs_tbf_client,
+ tc_hnode);
+
+ return &cli->tc_opcode;
+}
+
+static void *nrs_tbf_opcode_hop_object(struct hlist_node *hnode)
+{
+ return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
+}
+
+static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
+ struct hlist_node *hnode)
+{
+ struct nrs_tbf_client *cli = hlist_entry(hnode,
+ struct nrs_tbf_client,
+ tc_hnode);
+
+ atomic_inc(&cli->tc_ref);
+}
+
+static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
+ struct hlist_node *hnode)
+{
+ struct nrs_tbf_client *cli = hlist_entry(hnode,
+ struct nrs_tbf_client,
+ tc_hnode);
+
+ atomic_dec(&cli->tc_ref);
+}
+
+static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
+ struct hlist_node *hnode)
+{
+ struct nrs_tbf_client *cli = hlist_entry(hnode,
+ struct nrs_tbf_client,
+ tc_hnode);
+
+ LASSERTF(atomic_read(&cli->tc_ref) == 0,
+ "Busy TBF object from client with opcode %s, with %d refs\n",
+ ll_opcode2str(cli->tc_opcode),
+ atomic_read(&cli->tc_ref));
+
+ nrs_tbf_cli_fini(cli);
+}
+static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
+ .hs_hash = nrs_tbf_opcode_hop_hash,
+ .hs_keycmp = nrs_tbf_opcode_hop_keycmp,
+ .hs_key = nrs_tbf_opcode_hop_key,
+ .hs_object = nrs_tbf_opcode_hop_object,
+ .hs_get = nrs_tbf_opcode_hop_get,
+ .hs_put = nrs_tbf_opcode_hop_put,
+ .hs_put_locked = nrs_tbf_opcode_hop_put,
+ .hs_exit = nrs_tbf_opcode_hop_exit,
+};
+
+static int
+nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
+ struct nrs_tbf_head *head)
+{
+ struct nrs_tbf_cmd start = { 0 };
+ int rc;
+
+ head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
+ NRS_TBF_NID_BITS,
+ NRS_TBF_NID_BITS,
+ NRS_TBF_NID_BKT_BITS, 0,
+ CFS_HASH_MIN_THETA,
+ CFS_HASH_MAX_THETA,
+ &nrs_tbf_opcode_hash_ops,
+ CFS_HASH_RW_BKTLOCK);
+ if (head->th_cli_hash == NULL)
+ return -ENOMEM;
+
+ start.u.tc_start.ts_opcodes = NULL;
+ start.u.tc_start.ts_opcodes_str = "*";
+
+ start.u.tc_start.ts_rpc_rate = tbf_rate;
+ start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
+ start.tc_name = NRS_TBF_DEFAULT_RULE;
+ rc = nrs_tbf_rule_start(policy, head, &start);
+
+ return rc;
+}
+
+static struct nrs_tbf_client *
+nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
+ struct ptlrpc_request *req)
+{
+ __u32 opc;
+
+ opc = lustre_msg_get_opc(req->rq_reqmsg);
+ return cfs_hash_lookup(head->th_cli_hash, &opc);
+}
+
+static struct nrs_tbf_client *
+nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
+ struct nrs_tbf_client *cli)
+{
+ return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
+ &cli->tc_hnode);
+}
+
+static void
+nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
+ struct ptlrpc_request *req)
+{
+ cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
+}
+
+#define MAX_OPCODE_LEN 32
+static int
+nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, struct cfs_bitmap *opcodes)
+{
+ int op = 0;
+ char opcode_str[MAX_OPCODE_LEN];
+
+ if (id->ls_len + 1 > MAX_OPCODE_LEN)
+ return -EINVAL;
+
+ memcpy(opcode_str, id->ls_str, id->ls_len);
+ opcode_str[id->ls_len] = '\0';
+
+ op = ll_str2opcode(opcode_str);
+ if (op < 0)
+ return -EINVAL;
+
+ cfs_bitmap_set(opcodes, op);
+ return 0;
+}
+
+static int
+nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap *opcodes)
+{
+ struct cfs_lstr src;
+ struct cfs_lstr res;
+ int rc = 0;
+
+ ENTRY;
+
+ src.ls_str = str;
+ src.ls_len = len;
+ while (src.ls_str) {
+ rc = cfs_gettok(&src, ' ', &res);
+ if (rc == 0) {
+ rc = -EINVAL;
+ break;
+ }
+ rc = nrs_tbf_opcode_set_bit(&res, opcodes);
+ if (rc)
+ break;
+ }
+
+ RETURN(rc);
+}
+
+static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
+{
+ if (cmd->u.tc_start.ts_opcodes)
+ CFS_FREE_BITMAP(cmd->u.tc_start.ts_opcodes);
+
+ if (cmd->u.tc_start.ts_opcodes_str)
+ OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
+ strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
+
+}
+
+static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
+{
+ struct cfs_lstr src;
+ int rc;
+
+ cmd->u.tc_start.ts_opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
+ if (cmd->u.tc_start.ts_opcodes == NULL)
+ return -ENOMEM;
+
+ src.ls_str = id;
+ src.ls_len = strlen(id);
+ rc = nrs_tbf_check_id_value(&src, "opcode");
+ if (rc)
+ GOTO(out, rc);
+
+ OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
+ if (cmd->u.tc_start.ts_opcodes_str == NULL)
+ GOTO(out, rc = -ENOMEM);
+
+ memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
+
+ /* parse opcode list */
+ rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
+ strlen(cmd->u.tc_start.ts_opcodes_str),
+ cmd->u.tc_start.ts_opcodes);
+out:
+ if (rc != 0)
+ nrs_tbf_opcode_cmd_fini(cmd);
+
+ return rc;
+}
+
+static int
+nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
+ struct nrs_tbf_client *cli)
+{
+ if (rule->tr_opcodes == NULL)
+ return 0;
+
+ return cfs_bitmap_check(rule->tr_opcodes, cli->tc_opcode);
+}
+
+static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
+ struct nrs_tbf_rule *rule,
+ struct nrs_tbf_cmd *start)
+{
+ LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
+ OBD_ALLOC(rule->tr_opcodes_str,
+ strlen(start->u.tc_start.ts_opcodes_str) + 1);
+ if (rule->tr_opcodes_str == NULL)
+ return -ENOMEM;
+
+ strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
+ strlen(start->u.tc_start.ts_opcodes_str) + 1);
+
+ if (start->u.tc_start.ts_opcodes == NULL)
+ return 0;
+
+ rule->tr_opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
+ if (rule->tr_opcodes == NULL) {
+ OBD_FREE(rule->tr_opcodes_str,
+ strlen(start->u.tc_start.ts_opcodes_str) + 1);
+ return -ENOMEM;
+ }
+
+ cfs_bitmap_copy(rule->tr_opcodes, start->u.tc_start.ts_opcodes);
+
+ return 0;
+}
+
+static int
+nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
+{
+ seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
+ rule->tr_opcodes_str, rule->tr_rpc_rate,
+ atomic_read(&rule->tr_ref) - 1);
+ return 0;
+}
+
+
+struct nrs_tbf_ops nrs_tbf_opcode_ops = {
+ .o_name = NRS_TBF_TYPE_OPCODE,
+ .o_startup = nrs_tbf_opcode_startup,
+ .o_cli_find = nrs_tbf_opcode_cli_find,
+ .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
+ .o_cli_put = nrs_tbf_nid_cli_put,
+ .o_cli_init = nrs_tbf_opcode_cli_init,
+ .o_rule_init = nrs_tbf_opcode_rule_init,
+ .o_rule_dump = nrs_tbf_opcode_rule_dump,
+ .o_rule_match = nrs_tbf_opcode_rule_match,
+ .o_rule_fini = nrs_tbf_opcode_rule_fini,
+};
+
+static struct nrs_tbf_type nrs_tbf_types[] = {
+ {
+ .ntt_name = NRS_TBF_TYPE_JOBID,
+ .ntt_flag = NRS_TBF_FLAG_JOBID,
+ .ntt_ops = &nrs_tbf_jobid_ops,
+ },
+ {
+ .ntt_name = NRS_TBF_TYPE_NID,
+ .ntt_flag = NRS_TBF_FLAG_NID,
+ .ntt_ops = &nrs_tbf_nid_ops,
+ },
+ {
+ .ntt_name = NRS_TBF_TYPE_OPCODE,
+ .ntt_flag = NRS_TBF_FLAG_OPCODE,
+ .ntt_ops = &nrs_tbf_opcode_ops,
+ },
+};
+
/**
* Is called before the policy transitions into
* ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
struct nrs_tbf_head *head;
struct nrs_tbf_ops *ops;
__u32 type;
+ int found = 0;
+ int i;
int rc = 0;
if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
GOTO(out, rc = -EINVAL);
- if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
- ops = &nrs_tbf_nid_ops;
- type = NRS_TBF_FLAG_NID;
- } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
- ops = &nrs_tbf_jobid_ops;
- type = NRS_TBF_FLAG_JOBID;
- } else
+ for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
+ if (strcmp(arg, nrs_tbf_types[i].ntt_name) == 0) {
+ ops = nrs_tbf_types[i].ntt_ops;
+ type = nrs_tbf_types[i].ntt_flag;
+ found = 1;
+ break;
+ }
+ }
+ if (found == 0)
GOTO(out, rc = -ENOTSUPP);
OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
cmd);
}
break;
+ /**
+ * Read the TBF policy type of a policy instance.
+ */
+ case NRS_CTL_TBF_RD_TYPE_FLAG: {
+ struct nrs_tbf_head *head = policy->pol_private;
+
+ *(__u32 *)arg = head->th_type_flag;
+ }
+ break;
}
RETURN(rc);
sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
if (cli == NULL)
return -ENOMEM;
+
nrs_tbf_cli_init(head, cli, req);
tmp = head->th_ops->o_cli_findadd(head, cli);
if (tmp != cli) {
struct nrs_tbf_head *head = policy->pol_private;
struct ptlrpc_nrs_request *nrq = NULL;
struct nrs_tbf_client *cli;
- cfs_binheap_node_t *node;
+ struct cfs_binheap_node *node;
assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
} else {
__u64 now = ktime_to_ns(ktime_get());
__u64 passed;
- long ntoken;
+ __u64 ntoken;
__u64 deadline;
deadline = cli->tc_check_time +
cli->tc_nsecs;
LASSERT(now >= cli->tc_check_time);
passed = now - cli->tc_check_time;
- ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
+ ntoken = passed * cli->tc_rpc_rate;
+ do_div(ntoken, NSEC_PER_SEC);
ntoken += cli->tc_ntoken;
if (ntoken > cli->tc_depth)
ntoken = cli->tc_depth;
}
CDEBUG(D_RPCTRACE,
"NRS start %s request from %s, "
- "seq: "LPU64"\n",
+ "seq: %llu\n",
policy->pol_desc->pd_name,
libcfs_id2str(req->rq_peer),
nrq->nr_u.tbf.tr_sequence);
assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
- CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
+ CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
nrq->nr_u.tbf.tr_sequence);
}
return rc;
}
-static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
+static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
{
int rc;
- char *token;
- token = strsep(val, "}");
- if (*val == NULL)
- GOTO(out, rc = -EINVAL);
+ switch (cmd->u.tc_start.ts_valid_type) {
+ case NRS_TBF_FLAG_JOBID:
+ rc = nrs_tbf_jobid_parse(cmd, token);
+ break;
+ case NRS_TBF_FLAG_NID:
+ rc = nrs_tbf_nid_parse(cmd, token);
+ break;
+ case NRS_TBF_FLAG_OPCODE:
+ rc = nrs_tbf_opcode_parse(cmd, token);
+ break;
+ default:
+ RETURN(-EINVAL);
+ }
- if (strlen(token) <= 1 ||
- token[0] != '{')
- GOTO(out, rc = -EINVAL);
- /* Skip '{' */
- token++;
-
- /* Should be followed by ' ' or nothing */
- if ((*val)[0] == '\0')
- *val = NULL;
- else if ((*val)[0] == ' ')
- (*val)++;
- else
- GOTO(out, rc = -EINVAL);
+ return rc;
+}
- rc = nrs_tbf_jobid_parse(cmd, token);
- if (!rc)
- cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
+static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
+{
+ if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
+ if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_JOBID)
+ nrs_tbf_jobid_cmd_fini(cmd);
+ else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_NID)
+ nrs_tbf_nid_cmd_fini(cmd);
+ else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_OPCODE)
+ nrs_tbf_opcode_cmd_fini(cmd);
+ }
+}
- rc = nrs_tbf_nid_parse(cmd, token);
- if (!rc)
- cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
+static bool name_is_valid(const char *name)
+{
+ int i;
- if (!cmd->tc_valid_types)
- rc = -EINVAL;
- else
- rc = 0;
-out:
- return rc;
+ for (i = 0; i < strlen(name); i++) {
+ if ((!isalnum(name[i])) &&
+ (name[i] != '_'))
+ return false;
+ }
+ return true;
}
+static int
+nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
+{
+ char *key;
+ char *val;
+ int rc;
+ __u64 rate;
-static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
+ val = buffer;
+ key = strsep(&val, "=");
+ if (val == NULL || strlen(val) == 0)
+ return -EINVAL;
+
+ /* Key of the value pair */
+ if (strcmp(key, "rate") == 0) {
+ rc = kstrtoull(val, 10, &rate);
+ if (rc)
+ return rc;
+
+ if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
+ return -EINVAL;
+
+ if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
+ cmd->u.tc_start.ts_rpc_rate = rate;
+ else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
+ cmd->u.tc_change.tc_rpc_rate = rate;
+ else
+ return -EINVAL;
+ } else if (strcmp(key, "rank") == 0) {
+ if (!name_is_valid(val))
+ return -EINVAL;
+
+ if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
+ cmd->u.tc_start.ts_next_name = val;
+ else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
+ cmd->u.tc_change.tc_next_name = val;
+ else
+ return -EINVAL;
+ } else {
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int
+nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
{
- if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
- nrs_tbf_jobid_cmd_fini(cmd);
- if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
- nrs_tbf_nid_cmd_fini(cmd);
+ char *val;
+ char *token;
+ int rc;
+
+ val = buffer;
+ while (val != NULL && strlen(val) != 0) {
+ token = strsep(&val, " ");
+ rc = nrs_tbf_parse_value_pair(cmd, token);
+ if (rc)
+ return rc;
+ }
+
+ switch (cmd->tc_cmd) {
+ case NRS_CTL_TBF_START_RULE:
+ if (cmd->u.tc_start.ts_rpc_rate == 0)
+ cmd->u.tc_start.ts_rpc_rate = tbf_rate;
+ break;
+ case NRS_CTL_TBF_CHANGE_RULE:
+ if (cmd->u.tc_change.tc_rpc_rate == 0 &&
+ cmd->u.tc_change.tc_next_name == NULL)
+ return -EINVAL;
+ break;
+ case NRS_CTL_TBF_STOP_RULE:
+ break;
+ default:
+ return -EINVAL;
+ }
+ return 0;
}
static struct nrs_tbf_cmd *
-nrs_tbf_parse_cmd(char *buffer, unsigned long count)
+nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
{
- static struct nrs_tbf_cmd *cmd;
- char *token;
- char *val;
- int i;
- int rc = 0;
+ static struct nrs_tbf_cmd *cmd;
+ char *token;
+ char *val;
+ int rc = 0;
OBD_ALLOC_PTR(cmd);
if (cmd == NULL)
GOTO(out, rc = -ENOMEM);
+ memset(cmd, 0, sizeof(*cmd));
val = buffer;
token = strsep(&val, " ");
GOTO(out_free_cmd, rc = -EINVAL);
/* Type of the command */
- if (strcmp(token, "start") == 0)
+ if (strcmp(token, "start") == 0) {
cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
- else if (strcmp(token, "stop") == 0)
+ cmd->u.tc_start.ts_valid_type = type_flag;
+ } else if (strcmp(token, "stop") == 0)
cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
else if (strcmp(token, "change") == 0)
- cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
+ cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
else
GOTO(out_free_cmd, rc = -EINVAL);
/* Name of the rule */
token = strsep(&val, " ");
- if (val == NULL) {
- /**
- * Stop comand only need name argument,
- * But other commands need ID or rate argument.
- */
- if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
- GOTO(out_free_cmd, rc = -EINVAL);
- }
-
- for (i = 0; i < strlen(token); i++) {
- if ((!isalnum(token[i])) &&
- (token[i] != '_'))
- GOTO(out_free_cmd, rc = -EINVAL);
- }
+ if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
+ !name_is_valid(token))
+ GOTO(out_free_cmd, rc = -EINVAL);
cmd->tc_name = token;
if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
/* List of ID */
LASSERT(val);
- rc = nrs_tbf_id_parse(cmd, &val);
+ token = val;
+ val = strrchr(token, '}');
+ if (!val)
+ GOTO(out_free_cmd, rc = -EINVAL);
+
+ /* Skip '}' */
+ val++;
+ if (*val == '\0') {
+ val = NULL;
+ } else if (*val == ' ') {
+ *val = '\0';
+ val++;
+ } else
+ GOTO(out_free_cmd, rc = -EINVAL);
+
+ rc = nrs_tbf_id_parse(cmd, token);
if (rc)
GOTO(out_free_cmd, rc);
}
- if (val != NULL) {
- if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
- strlen(val) == 0 || !isdigit(val[0]))
- GOTO(out_free_nid, rc = -EINVAL);
-
- cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
- if (cmd->tc_rpc_rate <= 0 ||
- cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
- GOTO(out_free_nid, rc = -EINVAL);
- } else {
- if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
- GOTO(out_free_nid, rc = -EINVAL);
- /* No RPC rate given */
- cmd->tc_rpc_rate = tbf_rate;
- }
+ rc = nrs_tbf_parse_value_pairs(cmd, val);
+ if (rc)
+ GOTO(out_cmd_fini, rc = -EINVAL);
goto out;
-out_free_nid:
+out_cmd_fini:
nrs_tbf_cmd_fini(cmd);
out_free_cmd:
OBD_FREE_PTR(cmd);
return cmd;
}
+/**
+ * Get the TBF policy type (nid, jobid, etc) preset by
+ * proc entry 'nrs_policies' for command buffer parsing.
+ *
+ * \param[in] svc the PTLRPC service
+ * \param[in] queue the NRS queue type
+ *
+ * \retval the preset TBF policy type flag
+ */
+static __u32
+nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
+{
+ __u32 type;
+ int rc;
+
+ rc = ptlrpc_nrs_policy_control(svc, queue,
+ NRS_POL_NAME_TBF,
+ NRS_CTL_TBF_RD_TYPE_FLAG,
+ true, &type);
+ if (rc != 0)
+ type = NRS_TBF_FLAG_INVALID;
+
+ return type;
+}
+
extern struct nrs_core nrs_core;
#define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
static ssize_t
else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
queue = PTLRPC_NRS_QUEUE_REG;
- cmd = nrs_tbf_parse_cmd(val, length);
+ cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
if (IS_ERR(cmd))
GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));