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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (C) 2013 DataDirect Networks, Inc.
25 * Copyright (c) 2014, 2016, Intel Corporation.
28 * lustre/ptlrpc/nrs_tbf.c
30 * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
34 #ifdef HAVE_SERVER_SUPPORT
41 #define DEBUG_SUBSYSTEM S_RPC
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <libcfs/libcfs.h>
45 #include <lustre_req_layout.h>
46 #include "ptlrpc_internal.h"
51 * Token Bucket Filter over client NIDs
56 #define NRS_POL_NAME_TBF "tbf"
58 static int tbf_jobid_cache_size = 8192;
59 module_param(tbf_jobid_cache_size, int, 0644);
60 MODULE_PARM_DESC(tbf_jobid_cache_size, "The size of jobid cache");
62 static int tbf_rate = 10000;
63 module_param(tbf_rate, int, 0644);
64 MODULE_PARM_DESC(tbf_rate, "Default rate limit in RPCs/s");
66 static int tbf_depth = 3;
67 module_param(tbf_depth, int, 0644);
68 MODULE_PARM_DESC(tbf_depth, "How many tokens that a client can save up");
70 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
72 struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
74 struct ptlrpc_nrs *nrs = head->th_res.res_policy->pol_nrs;
75 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
77 nrs->nrs_throttling = 0;
78 wake_up(&svcpt->scp_waitq);
80 return HRTIMER_NORESTART;
83 #define NRS_TBF_DEFAULT_RULE "default"
85 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
87 LASSERT(atomic_read(&rule->tr_ref) == 0);
88 LASSERT(list_empty(&rule->tr_cli_list));
89 LASSERT(list_empty(&rule->tr_linkage));
91 rule->tr_head->th_ops->o_rule_fini(rule);
96 * Decreases the rule's usage reference count, and stops the rule in case it
97 * was already stopping and have no more outstanding usage references (which
98 * indicates it has no more queued or started requests, and can be safely
101 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
103 if (atomic_dec_and_test(&rule->tr_ref))
104 nrs_tbf_rule_fini(rule);
108 * Increases the rule's usage reference count.
110 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
112 atomic_inc(&rule->tr_ref);
116 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
118 LASSERT(!list_empty(&cli->tc_linkage));
119 LASSERT(cli->tc_rule);
120 spin_lock(&cli->tc_rule->tr_rule_lock);
121 list_del_init(&cli->tc_linkage);
122 spin_unlock(&cli->tc_rule->tr_rule_lock);
123 nrs_tbf_rule_put(cli->tc_rule);
128 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
129 struct nrs_tbf_client *cli)
132 struct nrs_tbf_rule *rule = cli->tc_rule;
134 cli->tc_rpc_rate = rule->tr_rpc_rate;
135 cli->tc_nsecs = rule->tr_nsecs;
136 cli->tc_depth = rule->tr_depth;
137 cli->tc_ntoken = rule->tr_depth;
138 cli->tc_check_time = ktime_to_ns(ktime_get());
139 cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
140 cli->tc_rule_generation = rule->tr_generation;
143 cfs_binheap_relocate(head->th_binheap,
148 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
149 struct nrs_tbf_rule *rule,
150 struct nrs_tbf_client *cli)
152 spin_lock(&cli->tc_rule_lock);
153 if (cli->tc_rule != NULL && !list_empty(&cli->tc_linkage)) {
154 LASSERT(rule != cli->tc_rule);
155 nrs_tbf_cli_rule_put(cli);
157 LASSERT(cli->tc_rule == NULL);
158 LASSERT(list_empty(&cli->tc_linkage));
159 /* Rule's ref is added before called */
161 spin_lock(&rule->tr_rule_lock);
162 list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
163 spin_unlock(&rule->tr_rule_lock);
164 spin_unlock(&cli->tc_rule_lock);
165 nrs_tbf_cli_reset_value(head, cli);
169 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
171 return rule->tr_head->th_ops->o_rule_dump(rule, m);
175 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
177 struct nrs_tbf_rule *rule;
180 LASSERT(head != NULL);
181 spin_lock(&head->th_rule_lock);
182 /* List the rules from newest to oldest */
183 list_for_each_entry(rule, &head->th_list, tr_linkage) {
184 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
185 rc = nrs_tbf_rule_dump(rule, m);
191 spin_unlock(&head->th_rule_lock);
196 static struct nrs_tbf_rule *
197 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
200 struct nrs_tbf_rule *rule;
202 LASSERT(head != NULL);
203 list_for_each_entry(rule, &head->th_list, tr_linkage) {
204 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
205 if (strcmp(rule->tr_name, name) == 0) {
206 nrs_tbf_rule_get(rule);
213 static struct nrs_tbf_rule *
214 nrs_tbf_rule_find(struct nrs_tbf_head *head,
217 struct nrs_tbf_rule *rule;
219 LASSERT(head != NULL);
220 spin_lock(&head->th_rule_lock);
221 rule = nrs_tbf_rule_find_nolock(head, name);
222 spin_unlock(&head->th_rule_lock);
226 static struct nrs_tbf_rule *
227 nrs_tbf_rule_match(struct nrs_tbf_head *head,
228 struct nrs_tbf_client *cli)
230 struct nrs_tbf_rule *rule = NULL;
231 struct nrs_tbf_rule *tmp_rule;
233 spin_lock(&head->th_rule_lock);
234 /* Match the newest rule in the list */
235 list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
236 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
237 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
244 rule = head->th_rule;
246 nrs_tbf_rule_get(rule);
247 spin_unlock(&head->th_rule_lock);
252 nrs_tbf_cli_init(struct nrs_tbf_head *head,
253 struct nrs_tbf_client *cli,
254 struct ptlrpc_request *req)
256 struct nrs_tbf_rule *rule;
258 memset(cli, 0, sizeof(*cli));
259 cli->tc_in_heap = false;
260 head->th_ops->o_cli_init(cli, req);
261 INIT_LIST_HEAD(&cli->tc_list);
262 INIT_LIST_HEAD(&cli->tc_linkage);
263 spin_lock_init(&cli->tc_rule_lock);
264 atomic_set(&cli->tc_ref, 1);
265 rule = nrs_tbf_rule_match(head, cli);
266 nrs_tbf_cli_reset(head, rule, cli);
270 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
272 LASSERT(list_empty(&cli->tc_list));
273 LASSERT(!cli->tc_in_heap);
274 LASSERT(atomic_read(&cli->tc_ref) == 0);
275 spin_lock(&cli->tc_rule_lock);
276 nrs_tbf_cli_rule_put(cli);
277 spin_unlock(&cli->tc_rule_lock);
282 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
283 struct nrs_tbf_head *head,
284 struct nrs_tbf_cmd *start)
286 struct nrs_tbf_rule *rule;
287 struct nrs_tbf_rule *tmp_rule;
288 struct nrs_tbf_rule *next_rule;
289 char *next_name = start->u.tc_start.ts_next_name;
292 rule = nrs_tbf_rule_find(head, start->tc_name);
294 nrs_tbf_rule_put(rule);
298 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
302 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
303 rule->tr_rpc_rate = start->u.tc_start.ts_rpc_rate;
304 rule->tr_flags = start->u.tc_start.ts_rule_flags;
305 rule->tr_nsecs = NSEC_PER_SEC;
306 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
307 rule->tr_depth = tbf_depth;
308 atomic_set(&rule->tr_ref, 1);
309 INIT_LIST_HEAD(&rule->tr_cli_list);
310 INIT_LIST_HEAD(&rule->tr_nids);
311 INIT_LIST_HEAD(&rule->tr_linkage);
312 spin_lock_init(&rule->tr_rule_lock);
313 rule->tr_head = head;
315 rc = head->th_ops->o_rule_init(policy, rule, start);
321 /* Add as the newest rule */
322 spin_lock(&head->th_rule_lock);
323 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
325 spin_unlock(&head->th_rule_lock);
326 nrs_tbf_rule_put(tmp_rule);
327 nrs_tbf_rule_put(rule);
332 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
334 spin_unlock(&head->th_rule_lock);
335 nrs_tbf_rule_put(rule);
339 list_add(&rule->tr_linkage, next_rule->tr_linkage.prev);
340 nrs_tbf_rule_put(next_rule);
342 /* Add on the top of the rule list */
343 list_add(&rule->tr_linkage, &head->th_list);
345 spin_unlock(&head->th_rule_lock);
346 atomic_inc(&head->th_rule_sequence);
347 if (start->u.tc_start.ts_rule_flags & NTRS_DEFAULT) {
348 rule->tr_flags |= NTRS_DEFAULT;
349 LASSERT(head->th_rule == NULL);
350 head->th_rule = rule;
353 CDEBUG(D_RPCTRACE, "TBF starts rule@%p rate %llu gen %llu\n",
354 rule, rule->tr_rpc_rate, rule->tr_generation);
360 * Change the rank of a rule in the rule list
362 * The matched rule will be moved to the position right before another
365 * \param[in] policy the policy instance
366 * \param[in] head the TBF policy instance
367 * \param[in] name the rule name to be moved
368 * \param[in] next_name the rule name before which the matched rule will be
373 nrs_tbf_rule_change_rank(struct ptlrpc_nrs_policy *policy,
374 struct nrs_tbf_head *head,
378 struct nrs_tbf_rule *rule = NULL;
379 struct nrs_tbf_rule *next_rule = NULL;
382 LASSERT(head != NULL);
384 spin_lock(&head->th_rule_lock);
385 rule = nrs_tbf_rule_find_nolock(head, name);
387 GOTO(out, rc = -ENOENT);
389 if (strcmp(name, next_name) == 0)
392 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
394 GOTO(out_put, rc = -ENOENT);
396 list_move(&rule->tr_linkage, next_rule->tr_linkage.prev);
397 nrs_tbf_rule_put(next_rule);
399 nrs_tbf_rule_put(rule);
401 spin_unlock(&head->th_rule_lock);
406 nrs_tbf_rule_change_rate(struct ptlrpc_nrs_policy *policy,
407 struct nrs_tbf_head *head,
411 struct nrs_tbf_rule *rule;
413 assert_spin_locked(&policy->pol_nrs->nrs_lock);
415 rule = nrs_tbf_rule_find(head, name);
419 rule->tr_rpc_rate = rate;
420 rule->tr_nsecs = NSEC_PER_SEC;
421 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
422 rule->tr_generation++;
423 nrs_tbf_rule_put(rule);
429 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
430 struct nrs_tbf_head *head,
431 struct nrs_tbf_cmd *change)
433 __u64 rate = change->u.tc_change.tc_rpc_rate;
434 char *next_name = change->u.tc_change.tc_next_name;
438 rc = nrs_tbf_rule_change_rate(policy, head, change->tc_name,
445 rc = nrs_tbf_rule_change_rank(policy, head, change->tc_name,
455 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
456 struct nrs_tbf_head *head,
457 struct nrs_tbf_cmd *stop)
459 struct nrs_tbf_rule *rule;
461 assert_spin_locked(&policy->pol_nrs->nrs_lock);
463 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
466 rule = nrs_tbf_rule_find(head, stop->tc_name);
470 list_del_init(&rule->tr_linkage);
471 rule->tr_flags |= NTRS_STOPPING;
472 nrs_tbf_rule_put(rule);
473 nrs_tbf_rule_put(rule);
479 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
480 struct nrs_tbf_head *head,
481 struct nrs_tbf_cmd *cmd)
485 assert_spin_locked(&policy->pol_nrs->nrs_lock);
487 switch (cmd->tc_cmd) {
488 case NRS_CTL_TBF_START_RULE:
489 if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
492 spin_unlock(&policy->pol_nrs->nrs_lock);
493 rc = nrs_tbf_rule_start(policy, head, cmd);
494 spin_lock(&policy->pol_nrs->nrs_lock);
496 case NRS_CTL_TBF_CHANGE_RULE:
497 rc = nrs_tbf_rule_change(policy, head, cmd);
499 case NRS_CTL_TBF_STOP_RULE:
500 rc = nrs_tbf_rule_stop(policy, head, cmd);
501 /* Take it as a success, if not exists at all */
502 return rc == -ENOENT ? 0 : rc;
509 * Binary heap predicate.
511 * \param[in] e1 the first binheap node to compare
512 * \param[in] e2 the second binheap node to compare
518 tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
520 struct nrs_tbf_client *cli1;
521 struct nrs_tbf_client *cli2;
523 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
524 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
526 if (cli1->tc_deadline < cli2->tc_deadline)
528 else if (cli1->tc_deadline > cli2->tc_deadline)
531 if (cli1->tc_check_time < cli2->tc_check_time)
533 else if (cli1->tc_check_time > cli2->tc_check_time)
536 /* Maybe need more comparasion, e.g. request number in the rules */
541 * TBF binary heap operations
543 static struct cfs_binheap_ops nrs_tbf_heap_ops = {
546 .hop_compare = tbf_cli_compare,
549 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
552 return cfs_hash_djb2_hash(key, strlen(key), mask);
555 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
557 struct nrs_tbf_client *cli = hlist_entry(hnode,
558 struct nrs_tbf_client,
561 return (strcmp(cli->tc_jobid, key) == 0);
564 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
566 struct nrs_tbf_client *cli = hlist_entry(hnode,
567 struct nrs_tbf_client,
570 return cli->tc_jobid;
573 static void *nrs_tbf_hop_object(struct hlist_node *hnode)
575 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
578 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
580 struct nrs_tbf_client *cli = hlist_entry(hnode,
581 struct nrs_tbf_client,
584 atomic_inc(&cli->tc_ref);
587 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
589 struct nrs_tbf_client *cli = hlist_entry(hnode,
590 struct nrs_tbf_client,
593 atomic_dec(&cli->tc_ref);
597 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
600 struct nrs_tbf_client *cli = hlist_entry(hnode,
601 struct nrs_tbf_client,
604 LASSERT(atomic_read(&cli->tc_ref) == 0);
605 nrs_tbf_cli_fini(cli);
608 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
609 .hs_hash = nrs_tbf_jobid_hop_hash,
610 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
611 .hs_key = nrs_tbf_jobid_hop_key,
612 .hs_object = nrs_tbf_hop_object,
613 .hs_get = nrs_tbf_jobid_hop_get,
614 .hs_put = nrs_tbf_jobid_hop_put,
615 .hs_put_locked = nrs_tbf_jobid_hop_put,
616 .hs_exit = nrs_tbf_jobid_hop_exit,
619 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
620 CFS_HASH_NO_ITEMREF | \
623 static struct nrs_tbf_client *
624 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
625 struct cfs_hash_bd *bd,
628 struct hlist_node *hnode;
629 struct nrs_tbf_client *cli;
631 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)jobid);
635 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
636 if (!list_empty(&cli->tc_lru))
637 list_del_init(&cli->tc_lru);
641 #define NRS_TBF_JOBID_NULL ""
643 static struct nrs_tbf_client *
644 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
645 struct ptlrpc_request *req)
648 struct nrs_tbf_client *cli;
649 struct cfs_hash *hs = head->th_cli_hash;
650 struct cfs_hash_bd bd;
652 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
654 jobid = NRS_TBF_JOBID_NULL;
655 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
656 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
657 cfs_hash_bd_unlock(hs, &bd, 1);
662 static struct nrs_tbf_client *
663 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
664 struct nrs_tbf_client *cli)
667 struct nrs_tbf_client *ret;
668 struct cfs_hash *hs = head->th_cli_hash;
669 struct cfs_hash_bd bd;
671 jobid = cli->tc_jobid;
672 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
673 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
675 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
678 cfs_hash_bd_unlock(hs, &bd, 1);
684 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
685 struct nrs_tbf_client *cli)
687 struct cfs_hash_bd bd;
688 struct cfs_hash *hs = head->th_cli_hash;
689 struct nrs_tbf_bucket *bkt;
691 struct list_head zombies;
693 INIT_LIST_HEAD(&zombies);
694 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
695 bkt = cfs_hash_bd_extra_get(hs, &bd);
696 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
698 LASSERT(list_empty(&cli->tc_lru));
699 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
702 * Check and purge the LRU, there is at least one client in the LRU.
704 hw = tbf_jobid_cache_size >>
705 (hs->hs_cur_bits - hs->hs_bkt_bits);
706 while (cfs_hash_bd_count_get(&bd) > hw) {
707 if (unlikely(list_empty(&bkt->ntb_lru)))
709 cli = list_entry(bkt->ntb_lru.next,
710 struct nrs_tbf_client,
712 LASSERT(atomic_read(&cli->tc_ref) == 0);
713 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
714 list_move(&cli->tc_lru, &zombies);
716 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
718 while (!list_empty(&zombies)) {
719 cli = container_of0(zombies.next,
720 struct nrs_tbf_client, tc_lru);
721 list_del_init(&cli->tc_lru);
722 nrs_tbf_cli_fini(cli);
727 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
728 struct ptlrpc_request *req)
730 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
733 jobid = NRS_TBF_JOBID_NULL;
734 LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
735 INIT_LIST_HEAD(&cli->tc_lru);
736 memcpy(cli->tc_jobid, jobid, strlen(jobid));
739 static int nrs_tbf_jobid_hash_order(void)
743 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
749 #define NRS_TBF_JOBID_BKT_BITS 10
752 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
753 struct nrs_tbf_head *head)
755 struct nrs_tbf_cmd start;
756 struct nrs_tbf_bucket *bkt;
760 struct cfs_hash_bd bd;
762 bits = nrs_tbf_jobid_hash_order();
763 if (bits < NRS_TBF_JOBID_BKT_BITS)
764 bits = NRS_TBF_JOBID_BKT_BITS;
765 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
768 NRS_TBF_JOBID_BKT_BITS,
772 &nrs_tbf_jobid_hash_ops,
773 NRS_TBF_JOBID_HASH_FLAGS);
774 if (head->th_cli_hash == NULL)
777 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
778 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
779 INIT_LIST_HEAD(&bkt->ntb_lru);
782 memset(&start, 0, sizeof(start));
783 start.u.tc_start.ts_jobids_str = "*";
785 start.u.tc_start.ts_rpc_rate = tbf_rate;
786 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
787 start.tc_name = NRS_TBF_DEFAULT_RULE;
788 INIT_LIST_HEAD(&start.u.tc_start.ts_jobids);
789 rc = nrs_tbf_rule_start(policy, head, &start);
791 cfs_hash_putref(head->th_cli_hash);
792 head->th_cli_hash = NULL;
799 * Frees jobid of \a list.
803 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
805 struct nrs_tbf_jobid *jobid, *n;
807 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
808 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
809 list_del(&jobid->tj_linkage);
810 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
815 nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
817 struct nrs_tbf_jobid *jobid;
820 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
824 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
825 if (jobid->tj_id == NULL) {
826 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
830 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
831 ptr = lprocfs_strnstr(id->ls_str, "*", id->ls_len);
833 jobid->tj_match_flag = NRS_TBF_MATCH_FULL;
835 jobid->tj_match_flag = NRS_TBF_MATCH_WILDCARD;
837 list_add_tail(&jobid->tj_linkage, jobid_list);
842 cfs_match_wildcard(const char *pattern, const char *content)
844 if (*pattern == '\0' && *content == '\0')
847 if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
850 while (*pattern == *content) {
853 if (*pattern == '\0' && *content == '\0')
856 if (*pattern == '*' && *(pattern + 1) != '\0' &&
862 return (cfs_match_wildcard(pattern + 1, content) ||
863 cfs_match_wildcard(pattern, content + 1));
869 nrs_tbf_jobid_match(const struct nrs_tbf_jobid *jobid, const char *id)
871 if (jobid->tj_match_flag == NRS_TBF_MATCH_FULL)
872 return strcmp(jobid->tj_id, id) == 0;
874 if (jobid->tj_match_flag == NRS_TBF_MATCH_WILDCARD)
875 return cfs_match_wildcard(jobid->tj_id, id);
881 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
883 struct nrs_tbf_jobid *jobid;
885 list_for_each_entry(jobid, jobid_list, tj_linkage) {
886 if (nrs_tbf_jobid_match(jobid, id))
893 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
902 INIT_LIST_HEAD(jobid_list);
904 rc = cfs_gettok(&src, ' ', &res);
909 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
914 nrs_tbf_jobid_list_free(jobid_list);
918 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
920 if (!list_empty(&cmd->u.tc_start.ts_jobids))
921 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
922 if (cmd->u.tc_start.ts_jobids_str)
923 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
924 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
927 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
930 int keylen = strlen(key);
933 rc = cfs_gettok(src, '=', &res);
934 if (rc == 0 || res.ls_len != keylen ||
935 strncmp(res.ls_str, key, keylen) != 0 ||
936 src->ls_len <= 2 || src->ls_str[0] != '{' ||
937 src->ls_str[src->ls_len - 1] != '}')
940 /* Skip '{' and '}' */
946 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
952 src.ls_len = strlen(id);
953 rc = nrs_tbf_check_id_value(&src, "jobid");
957 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
958 if (cmd->u.tc_start.ts_jobids_str == NULL)
961 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
963 /* parse jobid list */
964 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
965 strlen(cmd->u.tc_start.ts_jobids_str),
966 &cmd->u.tc_start.ts_jobids);
968 nrs_tbf_jobid_cmd_fini(cmd);
973 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
974 struct nrs_tbf_rule *rule,
975 struct nrs_tbf_cmd *start)
979 LASSERT(start->u.tc_start.ts_jobids_str);
980 OBD_ALLOC(rule->tr_jobids_str,
981 strlen(start->u.tc_start.ts_jobids_str) + 1);
982 if (rule->tr_jobids_str == NULL)
985 memcpy(rule->tr_jobids_str,
986 start->u.tc_start.ts_jobids_str,
987 strlen(start->u.tc_start.ts_jobids_str));
989 INIT_LIST_HEAD(&rule->tr_jobids);
990 if (!list_empty(&start->u.tc_start.ts_jobids)) {
991 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
992 strlen(rule->tr_jobids_str),
995 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
998 OBD_FREE(rule->tr_jobids_str,
999 strlen(start->u.tc_start.ts_jobids_str) + 1);
1004 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1006 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1007 rule->tr_jobids_str, rule->tr_rpc_rate,
1008 atomic_read(&rule->tr_ref) - 1);
1013 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
1014 struct nrs_tbf_client *cli)
1016 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
1019 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
1021 if (!list_empty(&rule->tr_jobids))
1022 nrs_tbf_jobid_list_free(&rule->tr_jobids);
1023 LASSERT(rule->tr_jobids_str != NULL);
1024 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
1027 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
1028 .o_name = NRS_TBF_TYPE_JOBID,
1029 .o_startup = nrs_tbf_jobid_startup,
1030 .o_cli_find = nrs_tbf_jobid_cli_find,
1031 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
1032 .o_cli_put = nrs_tbf_jobid_cli_put,
1033 .o_cli_init = nrs_tbf_jobid_cli_init,
1034 .o_rule_init = nrs_tbf_jobid_rule_init,
1035 .o_rule_dump = nrs_tbf_jobid_rule_dump,
1036 .o_rule_match = nrs_tbf_jobid_rule_match,
1037 .o_rule_fini = nrs_tbf_jobid_rule_fini,
1041 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
1043 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
1044 * nrs_tbf_client objects.
1046 #define NRS_TBF_NID_BKT_BITS 8
1047 #define NRS_TBF_NID_BITS 16
1049 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1052 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1055 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1057 lnet_nid_t *nid = (lnet_nid_t *)key;
1058 struct nrs_tbf_client *cli = hlist_entry(hnode,
1059 struct nrs_tbf_client,
1062 return *nid == cli->tc_nid;
1065 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1067 struct nrs_tbf_client *cli = hlist_entry(hnode,
1068 struct nrs_tbf_client,
1071 return &cli->tc_nid;
1074 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1076 struct nrs_tbf_client *cli = hlist_entry(hnode,
1077 struct nrs_tbf_client,
1080 atomic_inc(&cli->tc_ref);
1083 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1085 struct nrs_tbf_client *cli = hlist_entry(hnode,
1086 struct nrs_tbf_client,
1089 atomic_dec(&cli->tc_ref);
1092 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1094 struct nrs_tbf_client *cli = hlist_entry(hnode,
1095 struct nrs_tbf_client,
1098 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1099 "Busy TBF object from client with NID %s, with %d refs\n",
1100 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1102 nrs_tbf_cli_fini(cli);
1105 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1106 .hs_hash = nrs_tbf_nid_hop_hash,
1107 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
1108 .hs_key = nrs_tbf_nid_hop_key,
1109 .hs_object = nrs_tbf_hop_object,
1110 .hs_get = nrs_tbf_nid_hop_get,
1111 .hs_put = nrs_tbf_nid_hop_put,
1112 .hs_put_locked = nrs_tbf_nid_hop_put,
1113 .hs_exit = nrs_tbf_nid_hop_exit,
1116 static struct nrs_tbf_client *
1117 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1118 struct ptlrpc_request *req)
1120 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
1123 static struct nrs_tbf_client *
1124 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1125 struct nrs_tbf_client *cli)
1127 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1132 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1133 struct nrs_tbf_client *cli)
1135 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1139 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1140 struct nrs_tbf_head *head)
1142 struct nrs_tbf_cmd start;
1145 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1148 NRS_TBF_NID_BKT_BITS, 0,
1151 &nrs_tbf_nid_hash_ops,
1152 CFS_HASH_RW_BKTLOCK);
1153 if (head->th_cli_hash == NULL)
1156 memset(&start, 0, sizeof(start));
1157 start.u.tc_start.ts_nids_str = "*";
1159 start.u.tc_start.ts_rpc_rate = tbf_rate;
1160 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1161 start.tc_name = NRS_TBF_DEFAULT_RULE;
1162 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1163 rc = nrs_tbf_rule_start(policy, head, &start);
1165 cfs_hash_putref(head->th_cli_hash);
1166 head->th_cli_hash = NULL;
1173 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1174 struct ptlrpc_request *req)
1176 cli->tc_nid = req->rq_peer.nid;
1179 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1180 struct nrs_tbf_rule *rule,
1181 struct nrs_tbf_cmd *start)
1183 LASSERT(start->u.tc_start.ts_nids_str);
1184 OBD_ALLOC(rule->tr_nids_str,
1185 strlen(start->u.tc_start.ts_nids_str) + 1);
1186 if (rule->tr_nids_str == NULL)
1189 memcpy(rule->tr_nids_str,
1190 start->u.tc_start.ts_nids_str,
1191 strlen(start->u.tc_start.ts_nids_str));
1193 INIT_LIST_HEAD(&rule->tr_nids);
1194 if (!list_empty(&start->u.tc_start.ts_nids)) {
1195 if (cfs_parse_nidlist(rule->tr_nids_str,
1196 strlen(rule->tr_nids_str),
1197 &rule->tr_nids) <= 0) {
1198 CERROR("nids {%s} illegal\n",
1200 OBD_FREE(rule->tr_nids_str,
1201 strlen(start->u.tc_start.ts_nids_str) + 1);
1209 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1211 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1212 rule->tr_nids_str, rule->tr_rpc_rate,
1213 atomic_read(&rule->tr_ref) - 1);
1218 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1219 struct nrs_tbf_client *cli)
1221 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1224 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1226 if (!list_empty(&rule->tr_nids))
1227 cfs_free_nidlist(&rule->tr_nids);
1228 LASSERT(rule->tr_nids_str != NULL);
1229 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1232 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1234 if (!list_empty(&cmd->u.tc_start.ts_nids))
1235 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1236 if (cmd->u.tc_start.ts_nids_str)
1237 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1238 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1241 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1243 struct cfs_lstr src;
1247 src.ls_len = strlen(id);
1248 rc = nrs_tbf_check_id_value(&src, "nid");
1252 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1253 if (cmd->u.tc_start.ts_nids_str == NULL)
1256 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1258 /* parse NID list */
1259 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1260 strlen(cmd->u.tc_start.ts_nids_str),
1261 &cmd->u.tc_start.ts_nids) <= 0) {
1262 nrs_tbf_nid_cmd_fini(cmd);
1269 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1270 .o_name = NRS_TBF_TYPE_NID,
1271 .o_startup = nrs_tbf_nid_startup,
1272 .o_cli_find = nrs_tbf_nid_cli_find,
1273 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1274 .o_cli_put = nrs_tbf_nid_cli_put,
1275 .o_cli_init = nrs_tbf_nid_cli_init,
1276 .o_rule_init = nrs_tbf_nid_rule_init,
1277 .o_rule_dump = nrs_tbf_nid_rule_dump,
1278 .o_rule_match = nrs_tbf_nid_rule_match,
1279 .o_rule_fini = nrs_tbf_nid_rule_fini,
1282 static unsigned nrs_tbf_hop_hash(struct cfs_hash *hs, const void *key,
1285 return cfs_hash_djb2_hash(key, strlen(key), mask);
1288 static int nrs_tbf_hop_keycmp(const void *key, struct hlist_node *hnode)
1290 struct nrs_tbf_client *cli = hlist_entry(hnode,
1291 struct nrs_tbf_client,
1294 return (strcmp(cli->tc_key, key) == 0);
1297 static void *nrs_tbf_hop_key(struct hlist_node *hnode)
1299 struct nrs_tbf_client *cli = hlist_entry(hnode,
1300 struct nrs_tbf_client,
1305 static void nrs_tbf_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1307 struct nrs_tbf_client *cli = hlist_entry(hnode,
1308 struct nrs_tbf_client,
1311 atomic_inc(&cli->tc_ref);
1314 static void nrs_tbf_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1316 struct nrs_tbf_client *cli = hlist_entry(hnode,
1317 struct nrs_tbf_client,
1320 atomic_dec(&cli->tc_ref);
1323 static void nrs_tbf_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1326 struct nrs_tbf_client *cli = hlist_entry(hnode,
1327 struct nrs_tbf_client,
1330 LASSERT(atomic_read(&cli->tc_ref) == 0);
1331 nrs_tbf_cli_fini(cli);
1334 static struct cfs_hash_ops nrs_tbf_hash_ops = {
1335 .hs_hash = nrs_tbf_hop_hash,
1336 .hs_keycmp = nrs_tbf_hop_keycmp,
1337 .hs_key = nrs_tbf_hop_key,
1338 .hs_object = nrs_tbf_hop_object,
1339 .hs_get = nrs_tbf_hop_get,
1340 .hs_put = nrs_tbf_hop_put,
1341 .hs_put_locked = nrs_tbf_hop_put,
1342 .hs_exit = nrs_tbf_hop_exit,
1345 #define NRS_TBF_GENERIC_BKT_BITS 10
1346 #define NRS_TBF_GENERIC_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
1347 CFS_HASH_NO_ITEMREF | \
1351 nrs_tbf_startup(struct ptlrpc_nrs_policy *policy, struct nrs_tbf_head *head)
1353 struct nrs_tbf_cmd start;
1354 struct nrs_tbf_bucket *bkt;
1358 struct cfs_hash_bd bd;
1360 bits = nrs_tbf_jobid_hash_order();
1361 if (bits < NRS_TBF_GENERIC_BKT_BITS)
1362 bits = NRS_TBF_GENERIC_BKT_BITS;
1363 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1365 NRS_TBF_GENERIC_BKT_BITS,
1368 NRS_TBF_GENERIC_HASH_FLAGS);
1369 if (head->th_cli_hash == NULL)
1372 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
1373 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
1374 INIT_LIST_HEAD(&bkt->ntb_lru);
1377 memset(&start, 0, sizeof(start));
1378 start.u.tc_start.ts_conds_str = "*";
1380 start.u.tc_start.ts_rpc_rate = tbf_rate;
1381 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1382 start.tc_name = NRS_TBF_DEFAULT_RULE;
1383 INIT_LIST_HEAD(&start.u.tc_start.ts_conds);
1384 rc = nrs_tbf_rule_start(policy, head, &start);
1386 cfs_hash_putref(head->th_cli_hash);
1391 static struct nrs_tbf_client *
1392 nrs_tbf_cli_hash_lookup(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1395 struct hlist_node *hnode;
1396 struct nrs_tbf_client *cli;
1398 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)key);
1402 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
1403 if (!list_empty(&cli->tc_lru))
1404 list_del_init(&cli->tc_lru);
1409 * ONLY opcode presented in this function will be checked in
1410 * nrs_tbf_id_cli_set(). That means, we can add or remove an
1411 * opcode to enable or disable requests handled in nrs_tbf
1413 static struct req_format *req_fmt(__u32 opcode)
1417 return &RQF_OST_GETATTR;
1419 return &RQF_OST_SETATTR;
1421 return &RQF_OST_BRW_READ;
1423 return &RQF_OST_BRW_WRITE;
1424 /* FIXME: OST_CREATE and OST_DESTROY comes from MDS
1425 * in most case. Should they be removed? */
1427 return &RQF_OST_CREATE;
1429 return &RQF_OST_DESTROY;
1431 return &RQF_OST_PUNCH;
1433 return &RQF_OST_SYNC;
1435 return &RQF_OST_LADVISE;
1437 return &RQF_MDS_GETATTR;
1438 case MDS_GETATTR_NAME:
1439 return &RQF_MDS_GETATTR_NAME;
1440 /* close is skipped to avoid LDLM cancel slowness */
1443 return &RQF_MDS_CLOSE;
1446 return &RQF_MDS_REINT;
1448 return &RQF_MDS_READPAGE;
1450 return &RQF_MDS_GET_ROOT;
1452 return &RQF_MDS_STATFS;
1454 return &RQF_MDS_SYNC;
1456 return &RQF_MDS_QUOTACTL;
1458 return &RQF_MDS_GETXATTR;
1460 return &RQF_MDS_GET_INFO;
1461 /* HSM op is skipped */
1463 case MDS_HSM_STATE_GET:
1464 return &RQF_MDS_HSM_STATE_GET;
1465 case MDS_HSM_STATE_SET:
1466 return &RQF_MDS_HSM_STATE_SET;
1467 case MDS_HSM_ACTION:
1468 return &RQF_MDS_HSM_ACTION;
1469 case MDS_HSM_CT_REGISTER:
1470 return &RQF_MDS_HSM_CT_REGISTER;
1471 case MDS_HSM_CT_UNREGISTER:
1472 return &RQF_MDS_HSM_CT_UNREGISTER;
1474 case MDS_SWAP_LAYOUTS:
1475 return &RQF_MDS_SWAP_LAYOUTS;
1477 return &RQF_LDLM_ENQUEUE;
1483 static struct req_format *intent_req_fmt(__u32 it_opc)
1485 if (it_opc & (IT_OPEN | IT_CREAT))
1486 return &RQF_LDLM_INTENT_OPEN;
1487 else if (it_opc & (IT_GETATTR | IT_LOOKUP))
1488 return &RQF_LDLM_INTENT_GETATTR;
1489 else if (it_opc & IT_GETXATTR)
1490 return &RQF_LDLM_INTENT_GETXATTR;
1491 else if (it_opc & (IT_GLIMPSE | IT_BRW))
1492 return &RQF_LDLM_INTENT;
1497 static int ost_tbf_id_cli_set(struct ptlrpc_request *req,
1500 struct ost_body *body;
1502 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1504 id->ti_uid = body->oa.o_uid;
1505 id->ti_gid = body->oa.o_gid;
1512 static void unpack_ugid_from_mdt_body(struct ptlrpc_request *req,
1515 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
1519 /* TODO: nodemaping feature converts {ug}id from individual
1520 * clients to the actual ones of the file system. Some work
1521 * may be needed to fix this. */
1522 id->ti_uid = b->mbo_uid;
1523 id->ti_gid = b->mbo_gid;
1526 static void unpack_ugid_from_mdt_rec_reint(struct ptlrpc_request *req,
1529 struct mdt_rec_reint *rec;
1531 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1532 LASSERT(rec != NULL);
1534 /* use the fs{ug}id as {ug}id of the process */
1535 id->ti_uid = rec->rr_fsuid;
1536 id->ti_gid = rec->rr_fsgid;
1539 static int mdt_tbf_id_cli_set(struct ptlrpc_request *req,
1542 u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1547 case MDS_GETATTR_NAME:
1552 case MDS_HSM_STATE_GET ... MDS_SWAP_LAYOUTS:
1553 unpack_ugid_from_mdt_body(req, id);
1557 unpack_ugid_from_mdt_rec_reint(req, id);
1566 static int ldlm_tbf_id_cli_set(struct ptlrpc_request *req,
1569 struct ldlm_intent *lit;
1570 struct req_format *fmt;
1572 if (req->rq_reqmsg->lm_bufcount <= DLM_INTENT_IT_OFF)
1575 req_capsule_extend(&req->rq_pill, &RQF_LDLM_INTENT_BASIC);
1576 lit = req_capsule_client_get(&req->rq_pill, &RMF_LDLM_INTENT);
1580 fmt = intent_req_fmt(lit->opc);
1584 req_capsule_extend(&req->rq_pill, fmt);
1586 if (lit->opc & (IT_GETXATTR | IT_GETATTR | IT_LOOKUP))
1587 unpack_ugid_from_mdt_body(req, id);
1588 else if (lit->opc & (IT_OPEN | IT_OPEN | IT_GLIMPSE | IT_BRW))
1589 unpack_ugid_from_mdt_rec_reint(req, id);
1595 static int nrs_tbf_id_cli_set(struct ptlrpc_request *req, struct tbf_id *id,
1596 enum nrs_tbf_flag ti_type)
1598 u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1599 struct req_format *fmt = req_fmt(opc);
1600 bool fmt_unset = false;
1603 memset(id, 0, sizeof(struct tbf_id));
1604 id->ti_type = ti_type;
1608 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1609 if (req->rq_pill.rc_fmt == NULL) {
1610 req_capsule_set(&req->rq_pill, fmt);
1614 if (opc < OST_LAST_OPC)
1615 rc = ost_tbf_id_cli_set(req, id);
1616 else if (opc >= MDS_FIRST_OPC && opc < MDS_LAST_OPC)
1617 rc = mdt_tbf_id_cli_set(req, id);
1618 else if (opc == LDLM_ENQUEUE)
1619 rc = ldlm_tbf_id_cli_set(req, id);
1623 /* restore it to the initialized state */
1625 req->rq_pill.rc_fmt = NULL;
1629 static inline void nrs_tbf_cli_gen_key(struct nrs_tbf_client *cli,
1630 struct ptlrpc_request *req,
1631 char *keystr, size_t keystr_sz)
1634 u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1637 nrs_tbf_id_cli_set(req, &id, NRS_TBF_FLAG_UID | NRS_TBF_FLAG_GID);
1638 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1640 jobid = NRS_TBF_JOBID_NULL;
1642 snprintf(keystr, keystr_sz, "%s_%s_%d_%u_%u", jobid,
1643 libcfs_nid2str(req->rq_peer.nid), opc, id.ti_uid,
1647 INIT_LIST_HEAD(&cli->tc_lru);
1648 strlcpy(cli->tc_key, keystr, sizeof(cli->tc_key));
1649 strlcpy(cli->tc_jobid, jobid, sizeof(cli->tc_jobid));
1650 cli->tc_nid = req->rq_peer.nid;
1651 cli->tc_opcode = opc;
1656 static struct nrs_tbf_client *
1657 nrs_tbf_cli_find(struct nrs_tbf_head *head, struct ptlrpc_request *req)
1659 struct nrs_tbf_client *cli;
1660 struct cfs_hash *hs = head->th_cli_hash;
1661 struct cfs_hash_bd bd;
1662 char keystr[NRS_TBF_KEY_LEN];
1664 nrs_tbf_cli_gen_key(NULL, req, keystr, sizeof(keystr));
1665 cfs_hash_bd_get_and_lock(hs, (void *)keystr, &bd, 1);
1666 cli = nrs_tbf_cli_hash_lookup(hs, &bd, keystr);
1667 cfs_hash_bd_unlock(hs, &bd, 1);
1672 static struct nrs_tbf_client *
1673 nrs_tbf_cli_findadd(struct nrs_tbf_head *head,
1674 struct nrs_tbf_client *cli)
1677 struct nrs_tbf_client *ret;
1678 struct cfs_hash *hs = head->th_cli_hash;
1679 struct cfs_hash_bd bd;
1682 cfs_hash_bd_get_and_lock(hs, (void *)key, &bd, 1);
1683 ret = nrs_tbf_cli_hash_lookup(hs, &bd, key);
1685 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
1688 cfs_hash_bd_unlock(hs, &bd, 1);
1694 nrs_tbf_cli_put(struct nrs_tbf_head *head, struct nrs_tbf_client *cli)
1696 struct cfs_hash_bd bd;
1697 struct cfs_hash *hs = head->th_cli_hash;
1698 struct nrs_tbf_bucket *bkt;
1700 struct list_head zombies;
1702 INIT_LIST_HEAD(&zombies);
1703 cfs_hash_bd_get(hs, &cli->tc_key, &bd);
1704 bkt = cfs_hash_bd_extra_get(hs, &bd);
1705 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
1707 LASSERT(list_empty(&cli->tc_lru));
1708 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
1711 * Check and purge the LRU, there is at least one client in the LRU.
1713 hw = tbf_jobid_cache_size >> (hs->hs_cur_bits - hs->hs_bkt_bits);
1714 while (cfs_hash_bd_count_get(&bd) > hw) {
1715 if (unlikely(list_empty(&bkt->ntb_lru)))
1717 cli = list_entry(bkt->ntb_lru.next,
1718 struct nrs_tbf_client,
1720 LASSERT(atomic_read(&cli->tc_ref) == 0);
1721 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
1722 list_move(&cli->tc_lru, &zombies);
1724 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
1726 while (!list_empty(&zombies)) {
1727 cli = container_of0(zombies.next,
1728 struct nrs_tbf_client, tc_lru);
1729 list_del_init(&cli->tc_lru);
1730 nrs_tbf_cli_fini(cli);
1735 nrs_tbf_generic_cli_init(struct nrs_tbf_client *cli,
1736 struct ptlrpc_request *req)
1738 char keystr[NRS_TBF_KEY_LEN];
1740 nrs_tbf_cli_gen_key(cli, req, keystr, sizeof(keystr));
1744 nrs_tbf_id_list_free(struct list_head *uid_list)
1746 struct nrs_tbf_id *nti_id, *n;
1748 list_for_each_entry_safe(nti_id, n, uid_list, nti_linkage) {
1749 list_del_init(&nti_id->nti_linkage);
1750 OBD_FREE_PTR(nti_id);
1755 nrs_tbf_expression_free(struct nrs_tbf_expression *expr)
1757 LASSERT(expr->te_field >= NRS_TBF_FIELD_NID &&
1758 expr->te_field < NRS_TBF_FIELD_MAX);
1759 switch (expr->te_field) {
1760 case NRS_TBF_FIELD_NID:
1761 cfs_free_nidlist(&expr->te_cond);
1763 case NRS_TBF_FIELD_JOBID:
1764 nrs_tbf_jobid_list_free(&expr->te_cond);
1766 case NRS_TBF_FIELD_OPCODE:
1767 CFS_FREE_BITMAP(expr->te_opcodes);
1769 case NRS_TBF_FIELD_UID:
1770 case NRS_TBF_FIELD_GID:
1771 nrs_tbf_id_list_free(&expr->te_cond);
1780 nrs_tbf_conjunction_free(struct nrs_tbf_conjunction *conjunction)
1782 struct nrs_tbf_expression *expression;
1783 struct nrs_tbf_expression *n;
1785 LASSERT(list_empty(&conjunction->tc_linkage));
1786 list_for_each_entry_safe(expression, n,
1787 &conjunction->tc_expressions,
1789 list_del_init(&expression->te_linkage);
1790 nrs_tbf_expression_free(expression);
1792 OBD_FREE_PTR(conjunction);
1796 nrs_tbf_conds_free(struct list_head *cond_list)
1798 struct nrs_tbf_conjunction *conjunction;
1799 struct nrs_tbf_conjunction *n;
1801 list_for_each_entry_safe(conjunction, n, cond_list, tc_linkage) {
1802 list_del_init(&conjunction->tc_linkage);
1803 nrs_tbf_conjunction_free(conjunction);
1808 nrs_tbf_generic_cmd_fini(struct nrs_tbf_cmd *cmd)
1810 if (!list_empty(&cmd->u.tc_start.ts_conds))
1811 nrs_tbf_conds_free(&cmd->u.tc_start.ts_conds);
1812 if (cmd->u.tc_start.ts_conds_str)
1813 OBD_FREE(cmd->u.tc_start.ts_conds_str,
1814 strlen(cmd->u.tc_start.ts_conds_str) + 1);
1817 #define NRS_TBF_DISJUNCTION_DELIM (',')
1818 #define NRS_TBF_CONJUNCTION_DELIM ('&')
1819 #define NRS_TBF_EXPRESSION_DELIM ('=')
1822 nrs_tbf_check_field(struct cfs_lstr *field, char *str)
1824 int len = strlen(str);
1826 return (field->ls_len == len &&
1827 strncmp(field->ls_str, str, len) == 0);
1831 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr);
1833 nrs_tbf_id_list_parse(char *str, int len, struct list_head *id_list,
1834 enum nrs_tbf_flag tif);
1837 nrs_tbf_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
1839 struct nrs_tbf_expression *expr;
1840 struct cfs_lstr field;
1843 OBD_ALLOC(expr, sizeof(struct nrs_tbf_expression));
1847 rc = cfs_gettok(src, NRS_TBF_EXPRESSION_DELIM, &field);
1848 if (rc == 0 || src->ls_len <= 2 || src->ls_str[0] != '{' ||
1849 src->ls_str[src->ls_len - 1] != '}')
1850 GOTO(out, rc = -EINVAL);
1852 /* Skip '{' and '}' */
1856 if (nrs_tbf_check_field(&field, "nid")) {
1857 if (cfs_parse_nidlist(src->ls_str,
1859 &expr->te_cond) <= 0)
1860 GOTO(out, rc = -EINVAL);
1861 expr->te_field = NRS_TBF_FIELD_NID;
1862 } else if (nrs_tbf_check_field(&field, "jobid")) {
1863 if (nrs_tbf_jobid_list_parse(src->ls_str,
1865 &expr->te_cond) < 0)
1866 GOTO(out, rc = -EINVAL);
1867 expr->te_field = NRS_TBF_FIELD_JOBID;
1868 } else if (nrs_tbf_check_field(&field, "opcode")) {
1869 if (nrs_tbf_opcode_list_parse(src->ls_str,
1871 &expr->te_opcodes) < 0)
1872 GOTO(out, rc = -EINVAL);
1873 expr->te_field = NRS_TBF_FIELD_OPCODE;
1874 } else if (nrs_tbf_check_field(&field, "uid")) {
1875 if (nrs_tbf_id_list_parse(src->ls_str,
1878 NRS_TBF_FLAG_UID) < 0)
1879 GOTO(out, rc = -EINVAL);
1880 expr->te_field = NRS_TBF_FIELD_UID;
1881 } else if (nrs_tbf_check_field(&field, "gid")) {
1882 if (nrs_tbf_id_list_parse(src->ls_str,
1885 NRS_TBF_FLAG_GID) < 0)
1886 GOTO(out, rc = -EINVAL);
1887 expr->te_field = NRS_TBF_FIELD_GID;
1889 GOTO(out, rc = -EINVAL);
1892 list_add_tail(&expr->te_linkage, cond_list);
1900 nrs_tbf_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
1902 struct nrs_tbf_conjunction *conjunction;
1903 struct cfs_lstr expr;
1906 OBD_ALLOC(conjunction, sizeof(struct nrs_tbf_conjunction));
1907 if (conjunction == NULL)
1910 INIT_LIST_HEAD(&conjunction->tc_expressions);
1911 list_add_tail(&conjunction->tc_linkage, cond_list);
1913 while (src->ls_str) {
1914 rc = cfs_gettok(src, NRS_TBF_CONJUNCTION_DELIM, &expr);
1919 rc = nrs_tbf_expression_parse(&expr,
1920 &conjunction->tc_expressions);
1928 nrs_tbf_conds_parse(char *str, int len, struct list_head *cond_list)
1930 struct cfs_lstr src;
1931 struct cfs_lstr res;
1936 INIT_LIST_HEAD(cond_list);
1937 while (src.ls_str) {
1938 rc = cfs_gettok(&src, NRS_TBF_DISJUNCTION_DELIM, &res);
1943 rc = nrs_tbf_conjunction_parse(&res, cond_list);
1951 nrs_tbf_generic_parse(struct nrs_tbf_cmd *cmd, const char *id)
1955 OBD_ALLOC(cmd->u.tc_start.ts_conds_str, strlen(id) + 1);
1956 if (cmd->u.tc_start.ts_conds_str == NULL)
1959 memcpy(cmd->u.tc_start.ts_conds_str, id, strlen(id));
1961 /* Parse hybird NID and JOBID conditions */
1962 rc = nrs_tbf_conds_parse(cmd->u.tc_start.ts_conds_str,
1963 strlen(cmd->u.tc_start.ts_conds_str),
1964 &cmd->u.tc_start.ts_conds);
1966 nrs_tbf_generic_cmd_fini(cmd);
1972 nrs_tbf_id_list_match(struct list_head *id_list, struct tbf_id id);
1975 nrs_tbf_expression_match(struct nrs_tbf_expression *expr,
1976 struct nrs_tbf_rule *rule,
1977 struct nrs_tbf_client *cli)
1979 switch (expr->te_field) {
1980 case NRS_TBF_FIELD_NID:
1981 return cfs_match_nid(cli->tc_nid, &expr->te_cond);
1982 case NRS_TBF_FIELD_JOBID:
1983 return nrs_tbf_jobid_list_match(&expr->te_cond, cli->tc_jobid);
1984 case NRS_TBF_FIELD_OPCODE:
1985 return cfs_bitmap_check(expr->te_opcodes, cli->tc_opcode);
1986 case NRS_TBF_FIELD_UID:
1987 case NRS_TBF_FIELD_GID:
1988 return nrs_tbf_id_list_match(&expr->te_cond, cli->tc_id);
1995 nrs_tbf_conjunction_match(struct nrs_tbf_conjunction *conjunction,
1996 struct nrs_tbf_rule *rule,
1997 struct nrs_tbf_client *cli)
1999 struct nrs_tbf_expression *expr;
2002 list_for_each_entry(expr, &conjunction->tc_expressions, te_linkage) {
2003 matched = nrs_tbf_expression_match(expr, rule, cli);
2012 nrs_tbf_cond_match(struct nrs_tbf_rule *rule, struct nrs_tbf_client *cli)
2014 struct nrs_tbf_conjunction *conjunction;
2017 list_for_each_entry(conjunction, &rule->tr_conds, tc_linkage) {
2018 matched = nrs_tbf_conjunction_match(conjunction, rule, cli);
2027 nrs_tbf_generic_rule_fini(struct nrs_tbf_rule *rule)
2029 if (!list_empty(&rule->tr_conds))
2030 nrs_tbf_conds_free(&rule->tr_conds);
2031 LASSERT(rule->tr_conds_str != NULL);
2032 OBD_FREE(rule->tr_conds_str, strlen(rule->tr_conds_str) + 1);
2036 nrs_tbf_rule_init(struct ptlrpc_nrs_policy *policy,
2037 struct nrs_tbf_rule *rule, struct nrs_tbf_cmd *start)
2041 LASSERT(start->u.tc_start.ts_conds_str);
2042 OBD_ALLOC(rule->tr_conds_str,
2043 strlen(start->u.tc_start.ts_conds_str) + 1);
2044 if (rule->tr_conds_str == NULL)
2047 memcpy(rule->tr_conds_str,
2048 start->u.tc_start.ts_conds_str,
2049 strlen(start->u.tc_start.ts_conds_str));
2051 INIT_LIST_HEAD(&rule->tr_conds);
2052 if (!list_empty(&start->u.tc_start.ts_conds)) {
2053 rc = nrs_tbf_conds_parse(rule->tr_conds_str,
2054 strlen(rule->tr_conds_str),
2058 nrs_tbf_generic_rule_fini(rule);
2064 nrs_tbf_generic_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2066 seq_printf(m, "%s %s %llu, ref %d\n", rule->tr_name,
2067 rule->tr_conds_str, rule->tr_rpc_rate,
2068 atomic_read(&rule->tr_ref) - 1);
2073 nrs_tbf_generic_rule_match(struct nrs_tbf_rule *rule,
2074 struct nrs_tbf_client *cli)
2076 return nrs_tbf_cond_match(rule, cli);
2079 static struct nrs_tbf_ops nrs_tbf_generic_ops = {
2080 .o_name = NRS_TBF_TYPE_GENERIC,
2081 .o_startup = nrs_tbf_startup,
2082 .o_cli_find = nrs_tbf_cli_find,
2083 .o_cli_findadd = nrs_tbf_cli_findadd,
2084 .o_cli_put = nrs_tbf_cli_put,
2085 .o_cli_init = nrs_tbf_generic_cli_init,
2086 .o_rule_init = nrs_tbf_rule_init,
2087 .o_rule_dump = nrs_tbf_generic_rule_dump,
2088 .o_rule_match = nrs_tbf_generic_rule_match,
2089 .o_rule_fini = nrs_tbf_generic_rule_fini,
2092 static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
2094 if (rule->tr_opcodes != NULL)
2095 CFS_FREE_BITMAP(rule->tr_opcodes);
2097 LASSERT(rule->tr_opcodes_str != NULL);
2098 OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
2101 static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
2104 return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2107 static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
2109 const __u32 *opc = key;
2110 struct nrs_tbf_client *cli = hlist_entry(hnode,
2111 struct nrs_tbf_client,
2114 return *opc == cli->tc_opcode;
2117 static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
2119 struct nrs_tbf_client *cli = hlist_entry(hnode,
2120 struct nrs_tbf_client,
2123 return &cli->tc_opcode;
2126 static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
2127 struct hlist_node *hnode)
2129 struct nrs_tbf_client *cli = hlist_entry(hnode,
2130 struct nrs_tbf_client,
2133 atomic_inc(&cli->tc_ref);
2136 static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
2137 struct hlist_node *hnode)
2139 struct nrs_tbf_client *cli = hlist_entry(hnode,
2140 struct nrs_tbf_client,
2143 atomic_dec(&cli->tc_ref);
2146 static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
2147 struct hlist_node *hnode)
2149 struct nrs_tbf_client *cli = hlist_entry(hnode,
2150 struct nrs_tbf_client,
2153 LASSERTF(atomic_read(&cli->tc_ref) == 0,
2154 "Busy TBF object from client with opcode %s, with %d refs\n",
2155 ll_opcode2str(cli->tc_opcode),
2156 atomic_read(&cli->tc_ref));
2158 nrs_tbf_cli_fini(cli);
2160 static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
2161 .hs_hash = nrs_tbf_opcode_hop_hash,
2162 .hs_keycmp = nrs_tbf_opcode_hop_keycmp,
2163 .hs_key = nrs_tbf_opcode_hop_key,
2164 .hs_object = nrs_tbf_hop_object,
2165 .hs_get = nrs_tbf_opcode_hop_get,
2166 .hs_put = nrs_tbf_opcode_hop_put,
2167 .hs_put_locked = nrs_tbf_opcode_hop_put,
2168 .hs_exit = nrs_tbf_opcode_hop_exit,
2172 nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
2173 struct nrs_tbf_head *head)
2175 struct nrs_tbf_cmd start = { 0 };
2178 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
2181 NRS_TBF_NID_BKT_BITS, 0,
2184 &nrs_tbf_opcode_hash_ops,
2185 CFS_HASH_RW_BKTLOCK);
2186 if (head->th_cli_hash == NULL)
2189 start.u.tc_start.ts_opcodes = NULL;
2190 start.u.tc_start.ts_opcodes_str = "*";
2192 start.u.tc_start.ts_rpc_rate = tbf_rate;
2193 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
2194 start.tc_name = NRS_TBF_DEFAULT_RULE;
2195 rc = nrs_tbf_rule_start(policy, head, &start);
2200 static struct nrs_tbf_client *
2201 nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
2202 struct ptlrpc_request *req)
2206 opc = lustre_msg_get_opc(req->rq_reqmsg);
2207 return cfs_hash_lookup(head->th_cli_hash, &opc);
2210 static struct nrs_tbf_client *
2211 nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
2212 struct nrs_tbf_client *cli)
2214 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
2219 nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
2220 struct ptlrpc_request *req)
2222 cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
2225 #define MAX_OPCODE_LEN 32
2227 nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, struct cfs_bitmap *opcodes)
2230 char opcode_str[MAX_OPCODE_LEN];
2232 if (id->ls_len + 1 > MAX_OPCODE_LEN)
2235 memcpy(opcode_str, id->ls_str, id->ls_len);
2236 opcode_str[id->ls_len] = '\0';
2238 op = ll_str2opcode(opcode_str);
2242 cfs_bitmap_set(opcodes, op);
2247 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr)
2249 struct cfs_bitmap *opcodes;
2250 struct cfs_lstr src;
2251 struct cfs_lstr res;
2255 opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
2256 if (opcodes == NULL)
2261 while (src.ls_str) {
2262 rc = cfs_gettok(&src, ' ', &res);
2267 rc = nrs_tbf_opcode_set_bit(&res, opcodes);
2273 *bitmaptr = opcodes;
2275 CFS_FREE_BITMAP(opcodes);
2280 static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
2282 if (cmd->u.tc_start.ts_opcodes)
2283 CFS_FREE_BITMAP(cmd->u.tc_start.ts_opcodes);
2285 if (cmd->u.tc_start.ts_opcodes_str)
2286 OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
2287 strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
2291 static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
2293 struct cfs_lstr src;
2297 src.ls_len = strlen(id);
2298 rc = nrs_tbf_check_id_value(&src, "opcode");
2302 OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
2303 if (cmd->u.tc_start.ts_opcodes_str == NULL)
2306 memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
2308 /* parse opcode list */
2309 rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
2310 strlen(cmd->u.tc_start.ts_opcodes_str),
2311 &cmd->u.tc_start.ts_opcodes);
2313 nrs_tbf_opcode_cmd_fini(cmd);
2319 nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
2320 struct nrs_tbf_client *cli)
2322 if (rule->tr_opcodes == NULL)
2325 return cfs_bitmap_check(rule->tr_opcodes, cli->tc_opcode);
2328 static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
2329 struct nrs_tbf_rule *rule,
2330 struct nrs_tbf_cmd *start)
2334 LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
2335 OBD_ALLOC(rule->tr_opcodes_str,
2336 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2337 if (rule->tr_opcodes_str == NULL)
2340 strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
2341 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2343 /* Default rule '*' */
2344 if (start->u.tc_start.ts_opcodes == NULL)
2347 rc = nrs_tbf_opcode_list_parse(rule->tr_opcodes_str,
2348 strlen(rule->tr_opcodes_str),
2351 OBD_FREE(rule->tr_opcodes_str,
2352 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2358 nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2360 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2361 rule->tr_opcodes_str, rule->tr_rpc_rate,
2362 atomic_read(&rule->tr_ref) - 1);
2367 struct nrs_tbf_ops nrs_tbf_opcode_ops = {
2368 .o_name = NRS_TBF_TYPE_OPCODE,
2369 .o_startup = nrs_tbf_opcode_startup,
2370 .o_cli_find = nrs_tbf_opcode_cli_find,
2371 .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
2372 .o_cli_put = nrs_tbf_nid_cli_put,
2373 .o_cli_init = nrs_tbf_opcode_cli_init,
2374 .o_rule_init = nrs_tbf_opcode_rule_init,
2375 .o_rule_dump = nrs_tbf_opcode_rule_dump,
2376 .o_rule_match = nrs_tbf_opcode_rule_match,
2377 .o_rule_fini = nrs_tbf_opcode_rule_fini,
2380 static unsigned nrs_tbf_id_hop_hash(struct cfs_hash *hs, const void *key,
2383 return cfs_hash_djb2_hash(key, sizeof(struct tbf_id), mask);
2386 static int nrs_tbf_id_hop_keycmp(const void *key, struct hlist_node *hnode)
2388 const struct tbf_id *opc = key;
2389 enum nrs_tbf_flag ntf;
2390 struct nrs_tbf_client *cli = hlist_entry(hnode, struct nrs_tbf_client,
2392 ntf = opc->ti_type & cli->tc_id.ti_type;
2393 if ((ntf & NRS_TBF_FLAG_UID) && opc->ti_uid != cli->tc_id.ti_uid)
2396 if ((ntf & NRS_TBF_FLAG_GID) && opc->ti_gid != cli->tc_id.ti_gid)
2402 static void *nrs_tbf_id_hop_key(struct hlist_node *hnode)
2404 struct nrs_tbf_client *cli = hlist_entry(hnode,
2405 struct nrs_tbf_client,
2410 static void nrs_tbf_id_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
2412 struct nrs_tbf_client *cli = hlist_entry(hnode,
2413 struct nrs_tbf_client,
2416 atomic_inc(&cli->tc_ref);
2419 static void nrs_tbf_id_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
2421 struct nrs_tbf_client *cli = hlist_entry(hnode,
2422 struct nrs_tbf_client,
2425 atomic_dec(&cli->tc_ref);
2429 nrs_tbf_id_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
2432 struct nrs_tbf_client *cli = hlist_entry(hnode,
2433 struct nrs_tbf_client,
2436 LASSERT(atomic_read(&cli->tc_ref) == 0);
2437 nrs_tbf_cli_fini(cli);
2440 static struct cfs_hash_ops nrs_tbf_id_hash_ops = {
2441 .hs_hash = nrs_tbf_id_hop_hash,
2442 .hs_keycmp = nrs_tbf_id_hop_keycmp,
2443 .hs_key = nrs_tbf_id_hop_key,
2444 .hs_object = nrs_tbf_hop_object,
2445 .hs_get = nrs_tbf_id_hop_get,
2446 .hs_put = nrs_tbf_id_hop_put,
2447 .hs_put_locked = nrs_tbf_id_hop_put,
2448 .hs_exit = nrs_tbf_id_hop_exit,
2452 nrs_tbf_id_startup(struct ptlrpc_nrs_policy *policy,
2453 struct nrs_tbf_head *head)
2455 struct nrs_tbf_cmd start;
2458 head->th_cli_hash = cfs_hash_create("nrs_tbf_id_hash",
2461 NRS_TBF_NID_BKT_BITS, 0,
2464 &nrs_tbf_id_hash_ops,
2465 CFS_HASH_RW_BKTLOCK);
2466 if (head->th_cli_hash == NULL)
2469 memset(&start, 0, sizeof(start));
2470 start.u.tc_start.ts_ids_str = "*";
2471 start.u.tc_start.ts_rpc_rate = tbf_rate;
2472 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
2473 start.tc_name = NRS_TBF_DEFAULT_RULE;
2474 INIT_LIST_HEAD(&start.u.tc_start.ts_ids);
2475 rc = nrs_tbf_rule_start(policy, head, &start);
2477 cfs_hash_putref(head->th_cli_hash);
2478 head->th_cli_hash = NULL;
2484 static struct nrs_tbf_client *
2485 nrs_tbf_id_cli_find(struct nrs_tbf_head *head,
2486 struct ptlrpc_request *req)
2490 LASSERT(head->th_type_flag == NRS_TBF_FLAG_UID ||
2491 head->th_type_flag == NRS_TBF_FLAG_GID);
2493 nrs_tbf_id_cli_set(req, &id, head->th_type_flag);
2494 return cfs_hash_lookup(head->th_cli_hash, &id);
2497 static struct nrs_tbf_client *
2498 nrs_tbf_id_cli_findadd(struct nrs_tbf_head *head,
2499 struct nrs_tbf_client *cli)
2501 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_id,
2506 nrs_tbf_uid_cli_init(struct nrs_tbf_client *cli,
2507 struct ptlrpc_request *req)
2509 nrs_tbf_id_cli_set(req, &cli->tc_id, NRS_TBF_FLAG_UID);
2513 nrs_tbf_gid_cli_init(struct nrs_tbf_client *cli,
2514 struct ptlrpc_request *req)
2516 nrs_tbf_id_cli_set(req, &cli->tc_id, NRS_TBF_FLAG_GID);
2520 nrs_tbf_id_list_match(struct list_head *id_list, struct tbf_id id)
2522 struct nrs_tbf_id *nti_id;
2523 enum nrs_tbf_flag flag;
2525 list_for_each_entry(nti_id, id_list, nti_linkage) {
2526 flag = id.ti_type & nti_id->nti_id.ti_type;
2530 if ((flag & NRS_TBF_FLAG_UID) &&
2531 (id.ti_uid != nti_id->nti_id.ti_uid))
2534 if ((flag & NRS_TBF_FLAG_GID) &&
2535 (id.ti_gid != nti_id->nti_id.ti_gid))
2544 nrs_tbf_id_rule_match(struct nrs_tbf_rule *rule,
2545 struct nrs_tbf_client *cli)
2547 return nrs_tbf_id_list_match(&rule->tr_ids, cli->tc_id);
2550 static void nrs_tbf_id_cmd_fini(struct nrs_tbf_cmd *cmd)
2552 nrs_tbf_id_list_free(&cmd->u.tc_start.ts_ids);
2554 if (cmd->u.tc_start.ts_ids_str)
2555 OBD_FREE(cmd->u.tc_start.ts_ids_str,
2556 strlen(cmd->u.tc_start.ts_ids_str) + 1);
2560 nrs_tbf_id_list_parse(char *str, int len, struct list_head *id_list,
2561 enum nrs_tbf_flag tif)
2563 struct cfs_lstr src;
2564 struct cfs_lstr res;
2566 struct tbf_id id = { 0 };
2569 if (tif != NRS_TBF_FLAG_UID && tif != NRS_TBF_FLAG_GID)
2574 INIT_LIST_HEAD(id_list);
2575 while (src.ls_str) {
2576 struct nrs_tbf_id *nti_id;
2578 if (cfs_gettok(&src, ' ', &res) == 0)
2579 GOTO(out, rc = -EINVAL);
2582 if (tif == NRS_TBF_FLAG_UID) {
2583 if (!cfs_str2num_check(res.ls_str, res.ls_len,
2584 &id.ti_uid, 0, (u32)~0U))
2585 GOTO(out, rc = -EINVAL);
2587 if (!cfs_str2num_check(res.ls_str, res.ls_len,
2588 &id.ti_gid, 0, (u32)~0U))
2589 GOTO(out, rc = -EINVAL);
2592 OBD_ALLOC_PTR(nti_id);
2594 GOTO(out, rc = -ENOMEM);
2596 nti_id->nti_id = id;
2597 list_add_tail(&nti_id->nti_linkage, id_list);
2601 nrs_tbf_id_list_free(id_list);
2605 static int nrs_tbf_ug_id_parse(struct nrs_tbf_cmd *cmd, char *id)
2607 struct cfs_lstr src;
2609 enum nrs_tbf_flag tif;
2611 tif = cmd->u.tc_start.ts_valid_type;
2614 src.ls_len = strlen(id);
2616 rc = nrs_tbf_check_id_value(&src,
2617 tif == NRS_TBF_FLAG_UID ? "uid" : "gid");
2621 OBD_ALLOC(cmd->u.tc_start.ts_ids_str, src.ls_len + 1);
2622 if (cmd->u.tc_start.ts_ids_str == NULL)
2625 strlcpy(cmd->u.tc_start.ts_ids_str, src.ls_str, src.ls_len + 1);
2627 rc = nrs_tbf_id_list_parse(cmd->u.tc_start.ts_ids_str,
2628 strlen(cmd->u.tc_start.ts_ids_str),
2629 &cmd->u.tc_start.ts_ids, tif);
2631 nrs_tbf_id_cmd_fini(cmd);
2637 nrs_tbf_id_rule_init(struct ptlrpc_nrs_policy *policy,
2638 struct nrs_tbf_rule *rule,
2639 struct nrs_tbf_cmd *start)
2641 struct nrs_tbf_head *head = rule->tr_head;
2643 enum nrs_tbf_flag tif = head->th_type_flag;
2644 int ids_len = strlen(start->u.tc_start.ts_ids_str) + 1;
2646 LASSERT(start->u.tc_start.ts_ids_str);
2647 INIT_LIST_HEAD(&rule->tr_ids);
2649 OBD_ALLOC(rule->tr_ids_str, ids_len);
2650 if (rule->tr_ids_str == NULL)
2653 strlcpy(rule->tr_ids_str, start->u.tc_start.ts_ids_str,
2656 if (!list_empty(&start->u.tc_start.ts_ids)) {
2657 rc = nrs_tbf_id_list_parse(rule->tr_ids_str,
2658 strlen(rule->tr_ids_str),
2659 &rule->tr_ids, tif);
2661 CERROR("%ss {%s} illegal\n",
2662 tif == NRS_TBF_FLAG_UID ? "uid" : "gid",
2666 OBD_FREE(rule->tr_ids_str, ids_len);
2667 rule->tr_ids_str = NULL;
2673 nrs_tbf_id_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2675 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2676 rule->tr_ids_str, rule->tr_rpc_rate,
2677 atomic_read(&rule->tr_ref) - 1);
2681 static void nrs_tbf_id_rule_fini(struct nrs_tbf_rule *rule)
2683 nrs_tbf_id_list_free(&rule->tr_ids);
2684 if (rule->tr_ids_str != NULL)
2685 OBD_FREE(rule->tr_ids_str, strlen(rule->tr_ids_str) + 1);
2688 struct nrs_tbf_ops nrs_tbf_uid_ops = {
2689 .o_name = NRS_TBF_TYPE_UID,
2690 .o_startup = nrs_tbf_id_startup,
2691 .o_cli_find = nrs_tbf_id_cli_find,
2692 .o_cli_findadd = nrs_tbf_id_cli_findadd,
2693 .o_cli_put = nrs_tbf_nid_cli_put,
2694 .o_cli_init = nrs_tbf_uid_cli_init,
2695 .o_rule_init = nrs_tbf_id_rule_init,
2696 .o_rule_dump = nrs_tbf_id_rule_dump,
2697 .o_rule_match = nrs_tbf_id_rule_match,
2698 .o_rule_fini = nrs_tbf_id_rule_fini,
2701 struct nrs_tbf_ops nrs_tbf_gid_ops = {
2702 .o_name = NRS_TBF_TYPE_GID,
2703 .o_startup = nrs_tbf_id_startup,
2704 .o_cli_find = nrs_tbf_id_cli_find,
2705 .o_cli_findadd = nrs_tbf_id_cli_findadd,
2706 .o_cli_put = nrs_tbf_nid_cli_put,
2707 .o_cli_init = nrs_tbf_gid_cli_init,
2708 .o_rule_init = nrs_tbf_id_rule_init,
2709 .o_rule_dump = nrs_tbf_id_rule_dump,
2710 .o_rule_match = nrs_tbf_id_rule_match,
2711 .o_rule_fini = nrs_tbf_id_rule_fini,
2714 static struct nrs_tbf_type nrs_tbf_types[] = {
2716 .ntt_name = NRS_TBF_TYPE_JOBID,
2717 .ntt_flag = NRS_TBF_FLAG_JOBID,
2718 .ntt_ops = &nrs_tbf_jobid_ops,
2721 .ntt_name = NRS_TBF_TYPE_NID,
2722 .ntt_flag = NRS_TBF_FLAG_NID,
2723 .ntt_ops = &nrs_tbf_nid_ops,
2726 .ntt_name = NRS_TBF_TYPE_OPCODE,
2727 .ntt_flag = NRS_TBF_FLAG_OPCODE,
2728 .ntt_ops = &nrs_tbf_opcode_ops,
2731 .ntt_name = NRS_TBF_TYPE_GENERIC,
2732 .ntt_flag = NRS_TBF_FLAG_GENERIC,
2733 .ntt_ops = &nrs_tbf_generic_ops,
2736 .ntt_name = NRS_TBF_TYPE_UID,
2737 .ntt_flag = NRS_TBF_FLAG_UID,
2738 .ntt_ops = &nrs_tbf_uid_ops,
2741 .ntt_name = NRS_TBF_TYPE_GID,
2742 .ntt_flag = NRS_TBF_FLAG_GID,
2743 .ntt_ops = &nrs_tbf_gid_ops,
2748 * Is called before the policy transitions into
2749 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
2750 * policy-specific private data structure.
2752 * \param[in] policy The policy to start
2754 * \retval -ENOMEM OOM error
2757 * \see nrs_policy_register()
2758 * \see nrs_policy_ctl()
2760 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
2762 struct nrs_tbf_head *head;
2763 struct nrs_tbf_ops *ops;
2771 name = NRS_TBF_TYPE_GENERIC;
2772 else if (strlen(arg) < NRS_TBF_TYPE_MAX_LEN)
2775 GOTO(out, rc = -EINVAL);
2777 for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
2778 if (strcmp(name, nrs_tbf_types[i].ntt_name) == 0) {
2779 ops = nrs_tbf_types[i].ntt_ops;
2780 type = nrs_tbf_types[i].ntt_flag;
2786 GOTO(out, rc = -ENOTSUPP);
2788 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
2790 GOTO(out, rc = -ENOMEM);
2792 memcpy(head->th_type, name, strlen(name));
2793 head->th_type[strlen(name)] = '\0';
2795 head->th_type_flag = type;
2797 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
2798 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
2799 nrs_pol2cptab(policy),
2800 nrs_pol2cptid(policy));
2801 if (head->th_binheap == NULL)
2802 GOTO(out_free_head, rc = -ENOMEM);
2804 atomic_set(&head->th_rule_sequence, 0);
2805 spin_lock_init(&head->th_rule_lock);
2806 INIT_LIST_HEAD(&head->th_list);
2807 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2808 head->th_timer.function = nrs_tbf_timer_cb;
2809 rc = head->th_ops->o_startup(policy, head);
2811 GOTO(out_free_heap, rc);
2813 policy->pol_private = head;
2816 cfs_binheap_destroy(head->th_binheap);
2824 * Is called before the policy transitions into
2825 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
2826 * private data structure.
2828 * \param[in] policy The policy to stop
2830 * \see nrs_policy_stop0()
2832 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
2834 struct nrs_tbf_head *head = policy->pol_private;
2835 struct ptlrpc_nrs *nrs = policy->pol_nrs;
2836 struct nrs_tbf_rule *rule, *n;
2838 LASSERT(head != NULL);
2839 LASSERT(head->th_cli_hash != NULL);
2840 hrtimer_cancel(&head->th_timer);
2841 /* Should cleanup hash first before free rules */
2842 cfs_hash_putref(head->th_cli_hash);
2843 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
2844 list_del_init(&rule->tr_linkage);
2845 nrs_tbf_rule_put(rule);
2847 LASSERT(list_empty(&head->th_list));
2848 LASSERT(head->th_binheap != NULL);
2849 LASSERT(cfs_binheap_is_empty(head->th_binheap));
2850 cfs_binheap_destroy(head->th_binheap);
2852 nrs->nrs_throttling = 0;
2853 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
2857 * Performs a policy-specific ctl function on TBF policy instances; similar
2860 * \param[in] policy the policy instance
2861 * \param[in] opc the opcode
2862 * \param[in,out] arg used for passing parameters and information
2864 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2865 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2867 * \retval 0 operation carried out successfully
2870 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
2871 enum ptlrpc_nrs_ctl opc,
2877 assert_spin_locked(&policy->pol_nrs->nrs_lock);
2879 switch ((enum nrs_ctl_tbf)opc) {
2884 * Read RPC rate size of a policy instance.
2886 case NRS_CTL_TBF_RD_RULE: {
2887 struct nrs_tbf_head *head = policy->pol_private;
2888 struct seq_file *m = (struct seq_file *) arg;
2889 struct ptlrpc_service_part *svcpt;
2891 svcpt = policy->pol_nrs->nrs_svcpt;
2892 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
2894 rc = nrs_tbf_rule_dump_all(head, m);
2899 * Write RPC rate of a policy instance.
2901 case NRS_CTL_TBF_WR_RULE: {
2902 struct nrs_tbf_head *head = policy->pol_private;
2903 struct nrs_tbf_cmd *cmd;
2905 cmd = (struct nrs_tbf_cmd *)arg;
2906 rc = nrs_tbf_command(policy,
2912 * Read the TBF policy type of a policy instance.
2914 case NRS_CTL_TBF_RD_TYPE_FLAG: {
2915 struct nrs_tbf_head *head = policy->pol_private;
2917 *(__u32 *)arg = head->th_type_flag;
2926 * Is called for obtaining a TBF policy resource.
2928 * \param[in] policy The policy on which the request is being asked for
2929 * \param[in] nrq The request for which resources are being taken
2930 * \param[in] parent Parent resource, unused in this policy
2931 * \param[out] resp Resources references are placed in this array
2932 * \param[in] moving_req Signifies limited caller context; unused in this
2936 * \see nrs_resource_get_safe()
2938 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
2939 struct ptlrpc_nrs_request *nrq,
2940 const struct ptlrpc_nrs_resource *parent,
2941 struct ptlrpc_nrs_resource **resp,
2944 struct nrs_tbf_head *head;
2945 struct nrs_tbf_client *cli;
2946 struct nrs_tbf_client *tmp;
2947 struct ptlrpc_request *req;
2949 if (parent == NULL) {
2950 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
2954 head = container_of(parent, struct nrs_tbf_head, th_res);
2955 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
2956 cli = head->th_ops->o_cli_find(head, req);
2958 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2959 LASSERT(cli->tc_rule);
2960 if (cli->tc_rule_sequence !=
2961 atomic_read(&head->th_rule_sequence) ||
2962 cli->tc_rule->tr_flags & NTRS_STOPPING) {
2963 struct nrs_tbf_rule *rule;
2966 "TBF class@%p rate %llu sequence %d, "
2967 "rule flags %d, head sequence %d\n",
2968 cli, cli->tc_rpc_rate,
2969 cli->tc_rule_sequence,
2970 cli->tc_rule->tr_flags,
2971 atomic_read(&head->th_rule_sequence));
2972 rule = nrs_tbf_rule_match(head, cli);
2973 if (rule != cli->tc_rule) {
2974 nrs_tbf_cli_reset(head, rule, cli);
2976 if (cli->tc_rule_generation != rule->tr_generation)
2977 nrs_tbf_cli_reset_value(head, cli);
2978 nrs_tbf_rule_put(rule);
2980 } else if (cli->tc_rule_generation !=
2981 cli->tc_rule->tr_generation) {
2982 nrs_tbf_cli_reset_value(head, cli);
2984 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2988 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
2989 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
2993 nrs_tbf_cli_init(head, cli, req);
2994 tmp = head->th_ops->o_cli_findadd(head, cli);
2996 atomic_dec(&cli->tc_ref);
2997 nrs_tbf_cli_fini(cli);
3001 *resp = &cli->tc_res;
3007 * Called when releasing references to the resource hierachy obtained for a
3008 * request for scheduling using the TBF policy.
3010 * \param[in] policy the policy the resource belongs to
3011 * \param[in] res the resource to be released
3013 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
3014 const struct ptlrpc_nrs_resource *res)
3016 struct nrs_tbf_head *head;
3017 struct nrs_tbf_client *cli;
3020 * Do nothing for freeing parent, nrs_tbf_net resources
3022 if (res->res_parent == NULL)
3025 cli = container_of(res, struct nrs_tbf_client, tc_res);
3026 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
3028 head->th_ops->o_cli_put(head, cli);
3032 * Called when getting a request from the TBF policy for handling, or just
3033 * peeking; removes the request from the policy when it is to be handled.
3035 * \param[in] policy The policy
3036 * \param[in] peek When set, signifies that we just want to examine the
3037 * request, and not handle it, so the request is not removed
3039 * \param[in] force Force the policy to return a request; unused in this
3042 * \retval The request to be handled; this is the next request in the TBF
3045 * \see ptlrpc_nrs_req_get_nolock()
3046 * \see nrs_request_get()
3049 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
3050 bool peek, bool force)
3052 struct nrs_tbf_head *head = policy->pol_private;
3053 struct ptlrpc_nrs_request *nrq = NULL;
3054 struct nrs_tbf_client *cli;
3055 struct cfs_binheap_node *node;
3057 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3059 if (!peek && policy->pol_nrs->nrs_throttling)
3062 node = cfs_binheap_root(head->th_binheap);
3063 if (unlikely(node == NULL))
3066 cli = container_of(node, struct nrs_tbf_client, tc_node);
3067 LASSERT(cli->tc_in_heap);
3069 nrq = list_entry(cli->tc_list.next,
3070 struct ptlrpc_nrs_request,
3073 struct nrs_tbf_rule *rule = cli->tc_rule;
3074 __u64 now = ktime_to_ns(ktime_get());
3078 __u64 old_resid = 0;
3080 deadline = cli->tc_check_time +
3082 LASSERT(now >= cli->tc_check_time);
3083 passed = now - cli->tc_check_time;
3084 ntoken = passed * cli->tc_rpc_rate;
3085 do_div(ntoken, NSEC_PER_SEC);
3087 ntoken += cli->tc_ntoken;
3088 if (rule->tr_flags & NTRS_REALTIME) {
3089 LASSERT(cli->tc_nsecs_resid < cli->tc_nsecs);
3090 old_resid = cli->tc_nsecs_resid;
3091 cli->tc_nsecs_resid += passed % cli->tc_nsecs;
3092 if (cli->tc_nsecs_resid > cli->tc_nsecs) {
3094 cli->tc_nsecs_resid -= cli->tc_nsecs;
3096 } else if (ntoken > cli->tc_depth)
3097 ntoken = cli->tc_depth;
3100 struct ptlrpc_request *req;
3101 nrq = list_entry(cli->tc_list.next,
3102 struct ptlrpc_nrs_request,
3104 req = container_of(nrq,
3105 struct ptlrpc_request,
3108 cli->tc_ntoken = ntoken;
3109 cli->tc_check_time = now;
3110 list_del_init(&nrq->nr_u.tbf.tr_list);
3111 if (list_empty(&cli->tc_list)) {
3112 cfs_binheap_remove(head->th_binheap,
3114 cli->tc_in_heap = false;
3116 if (!(rule->tr_flags & NTRS_REALTIME))
3117 cli->tc_deadline = now + cli->tc_nsecs;
3118 cfs_binheap_relocate(head->th_binheap,
3122 "TBF dequeues: class@%p rate %llu gen %llu "
3123 "token %llu, rule@%p rate %llu gen %llu\n",
3124 cli, cli->tc_rpc_rate,
3125 cli->tc_rule_generation, cli->tc_ntoken,
3126 cli->tc_rule, cli->tc_rule->tr_rpc_rate,
3127 cli->tc_rule->tr_generation);
3131 if (rule->tr_flags & NTRS_REALTIME) {
3132 cli->tc_deadline = deadline;
3133 cli->tc_nsecs_resid = old_resid;
3134 cfs_binheap_relocate(head->th_binheap,
3136 if (node != cfs_binheap_root(head->th_binheap))
3137 return nrs_tbf_req_get(policy,
3140 policy->pol_nrs->nrs_throttling = 1;
3141 head->th_deadline = deadline;
3142 time = ktime_set(0, 0);
3143 time = ktime_add_ns(time, deadline);
3144 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
3152 * Adds request \a nrq to \a policy's list of queued requests
3154 * \param[in] policy The policy
3155 * \param[in] nrq The request to add
3157 * \retval 0 success; nrs_request_enqueue() assumes this function will always
3160 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
3161 struct ptlrpc_nrs_request *nrq)
3163 struct nrs_tbf_head *head;
3164 struct nrs_tbf_client *cli;
3167 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3169 cli = container_of(nrs_request_resource(nrq),
3170 struct nrs_tbf_client, tc_res);
3171 head = container_of(nrs_request_resource(nrq)->res_parent,
3172 struct nrs_tbf_head, th_res);
3173 if (list_empty(&cli->tc_list)) {
3174 LASSERT(!cli->tc_in_heap);
3175 cli->tc_deadline = cli->tc_check_time + cli->tc_nsecs;
3176 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
3178 cli->tc_in_heap = true;
3179 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
3180 list_add_tail(&nrq->nr_u.tbf.tr_list,
3182 if (policy->pol_nrs->nrs_throttling) {
3183 __u64 deadline = cli->tc_deadline;
3184 if ((head->th_deadline > deadline) &&
3185 (hrtimer_try_to_cancel(&head->th_timer)
3188 head->th_deadline = deadline;
3189 time = ktime_set(0, 0);
3190 time = ktime_add_ns(time, deadline);
3191 hrtimer_start(&head->th_timer, time,
3197 LASSERT(cli->tc_in_heap);
3198 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
3199 list_add_tail(&nrq->nr_u.tbf.tr_list,
3205 "TBF enqueues: class@%p rate %llu gen %llu "
3206 "token %llu, rule@%p rate %llu gen %llu\n",
3207 cli, cli->tc_rpc_rate,
3208 cli->tc_rule_generation, cli->tc_ntoken,
3209 cli->tc_rule, cli->tc_rule->tr_rpc_rate,
3210 cli->tc_rule->tr_generation);
3216 * Removes request \a nrq from \a policy's list of queued requests.
3218 * \param[in] policy The policy
3219 * \param[in] nrq The request to remove
3221 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
3222 struct ptlrpc_nrs_request *nrq)
3224 struct nrs_tbf_head *head;
3225 struct nrs_tbf_client *cli;
3227 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3229 cli = container_of(nrs_request_resource(nrq),
3230 struct nrs_tbf_client, tc_res);
3231 head = container_of(nrs_request_resource(nrq)->res_parent,
3232 struct nrs_tbf_head, th_res);
3234 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
3235 list_del_init(&nrq->nr_u.tbf.tr_list);
3236 if (list_empty(&cli->tc_list)) {
3237 cfs_binheap_remove(head->th_binheap,
3239 cli->tc_in_heap = false;
3241 cfs_binheap_relocate(head->th_binheap,
3247 * Prints a debug statement right before the request \a nrq stops being
3250 * \param[in] policy The policy handling the request
3251 * \param[in] nrq The request being handled
3253 * \see ptlrpc_server_finish_request()
3254 * \see ptlrpc_nrs_req_stop_nolock()
3256 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
3257 struct ptlrpc_nrs_request *nrq)
3259 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
3262 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3264 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
3265 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
3266 nrq->nr_u.tbf.tr_sequence);
3274 * The maximum RPC rate.
3276 #define LPROCFS_NRS_RATE_MAX 65535
3279 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
3281 struct ptlrpc_service *svc = m->private;
3284 seq_printf(m, "regular_requests:\n");
3286 * Perform two separate calls to this as only one of the NRS heads'
3287 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
3288 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
3290 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
3292 NRS_CTL_TBF_RD_RULE,
3296 * -ENOSPC means buf in the parameter m is overflow, return 0
3297 * here to let upper layer function seq_read alloc a larger
3298 * memory area and do this process again.
3300 } else if (rc == -ENOSPC) {
3304 * Ignore -ENODEV as the regular NRS head's policy may be in the
3305 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
3307 } else if (rc != -ENODEV) {
3311 if (!nrs_svc_has_hp(svc))
3314 seq_printf(m, "high_priority_requests:\n");
3315 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
3317 NRS_CTL_TBF_RD_RULE,
3321 * -ENOSPC means buf in the parameter m is overflow, return 0
3322 * here to let upper layer function seq_read alloc a larger
3323 * memory area and do this process again.
3325 } else if (rc == -ENOSPC) {
3334 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
3339 switch (cmd->u.tc_start.ts_valid_type) {
3340 case NRS_TBF_FLAG_JOBID:
3341 rc = nrs_tbf_jobid_parse(cmd, token);
3343 case NRS_TBF_FLAG_NID:
3344 rc = nrs_tbf_nid_parse(cmd, token);
3346 case NRS_TBF_FLAG_OPCODE:
3347 rc = nrs_tbf_opcode_parse(cmd, token);
3349 case NRS_TBF_FLAG_GENERIC:
3350 rc = nrs_tbf_generic_parse(cmd, token);
3352 case NRS_TBF_FLAG_UID:
3353 case NRS_TBF_FLAG_GID:
3354 rc = nrs_tbf_ug_id_parse(cmd, token);
3363 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
3365 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
3366 switch (cmd->u.tc_start.ts_valid_type) {
3367 case NRS_TBF_FLAG_JOBID:
3368 nrs_tbf_jobid_cmd_fini(cmd);
3370 case NRS_TBF_FLAG_NID:
3371 nrs_tbf_nid_cmd_fini(cmd);
3373 case NRS_TBF_FLAG_OPCODE:
3374 nrs_tbf_opcode_cmd_fini(cmd);
3376 case NRS_TBF_FLAG_GENERIC:
3377 nrs_tbf_generic_cmd_fini(cmd);
3379 case NRS_TBF_FLAG_UID:
3380 case NRS_TBF_FLAG_GID:
3381 nrs_tbf_id_cmd_fini(cmd);
3384 CWARN("unknown NRS_TBF_FLAGS:0x%x\n",
3385 cmd->u.tc_start.ts_valid_type);
3390 static bool name_is_valid(const char *name)
3394 for (i = 0; i < strlen(name); i++) {
3395 if ((!isalnum(name[i])) &&
3403 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
3411 key = strsep(&val, "=");
3412 if (val == NULL || strlen(val) == 0)
3415 /* Key of the value pair */
3416 if (strcmp(key, "rate") == 0) {
3417 rc = kstrtoull(val, 10, &rate);
3421 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
3424 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
3425 cmd->u.tc_start.ts_rpc_rate = rate;
3426 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
3427 cmd->u.tc_change.tc_rpc_rate = rate;
3430 } else if (strcmp(key, "rank") == 0) {
3431 if (!name_is_valid(val))
3434 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
3435 cmd->u.tc_start.ts_next_name = val;
3436 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
3437 cmd->u.tc_change.tc_next_name = val;
3440 } else if (strcmp(key, "realtime") == 0) {
3441 unsigned long realtime;
3443 rc = kstrtoul(val, 10, &realtime);
3448 cmd->u.tc_start.ts_rule_flags |= NTRS_REALTIME;
3456 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
3463 while (val != NULL && strlen(val) != 0) {
3464 token = strsep(&val, " ");
3465 rc = nrs_tbf_parse_value_pair(cmd, token);
3470 switch (cmd->tc_cmd) {
3471 case NRS_CTL_TBF_START_RULE:
3472 if (cmd->u.tc_start.ts_rpc_rate == 0)
3473 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
3475 case NRS_CTL_TBF_CHANGE_RULE:
3476 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
3477 cmd->u.tc_change.tc_next_name == NULL)
3480 case NRS_CTL_TBF_STOP_RULE:
3488 static struct nrs_tbf_cmd *
3489 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
3491 static struct nrs_tbf_cmd *cmd;
3498 GOTO(out, rc = -ENOMEM);
3499 memset(cmd, 0, sizeof(*cmd));
3502 token = strsep(&val, " ");
3503 if (val == NULL || strlen(val) == 0)
3504 GOTO(out_free_cmd, rc = -EINVAL);
3506 /* Type of the command */
3507 if (strcmp(token, "start") == 0) {
3508 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
3509 cmd->u.tc_start.ts_valid_type = type_flag;
3510 } else if (strcmp(token, "stop") == 0)
3511 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
3512 else if (strcmp(token, "change") == 0)
3513 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
3515 GOTO(out_free_cmd, rc = -EINVAL);
3517 /* Name of the rule */
3518 token = strsep(&val, " ");
3519 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
3520 !name_is_valid(token))
3521 GOTO(out_free_cmd, rc = -EINVAL);
3522 cmd->tc_name = token;
3524 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
3528 val = strrchr(token, '}');
3530 GOTO(out_free_cmd, rc = -EINVAL);
3536 } else if (*val == ' ') {
3540 GOTO(out_free_cmd, rc = -EINVAL);
3542 rc = nrs_tbf_id_parse(cmd, token);
3544 GOTO(out_free_cmd, rc);
3547 rc = nrs_tbf_parse_value_pairs(cmd, val);
3549 GOTO(out_cmd_fini, rc = -EINVAL);
3552 nrs_tbf_cmd_fini(cmd);
3562 * Get the TBF policy type (nid, jobid, etc) preset by
3563 * proc entry 'nrs_policies' for command buffer parsing.
3565 * \param[in] svc the PTLRPC service
3566 * \param[in] queue the NRS queue type
3568 * \retval the preset TBF policy type flag
3571 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
3576 rc = ptlrpc_nrs_policy_control(svc, queue,
3578 NRS_CTL_TBF_RD_TYPE_FLAG,
3581 type = NRS_TBF_FLAG_INVALID;
3586 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
3588 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
3589 const char __user *buffer,
3590 size_t count, loff_t *off)
3592 struct seq_file *m = file->private_data;
3593 struct ptlrpc_service *svc = m->private;
3597 static struct nrs_tbf_cmd *cmd;
3598 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
3599 unsigned long length;
3602 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
3603 if (kernbuf == NULL)
3604 GOTO(out, rc = -ENOMEM);
3606 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
3607 GOTO(out_free_kernbuff, rc = -EINVAL);
3609 if (copy_from_user(kernbuf, buffer, count))
3610 GOTO(out_free_kernbuff, rc = -EFAULT);
3613 token = strsep(&val, " ");
3615 GOTO(out_free_kernbuff, rc = -EINVAL);
3617 if (strcmp(token, "reg") == 0) {
3618 queue = PTLRPC_NRS_QUEUE_REG;
3619 } else if (strcmp(token, "hp") == 0) {
3620 queue = PTLRPC_NRS_QUEUE_HP;
3622 kernbuf[strlen(token)] = ' ';
3625 length = strlen(val);
3628 GOTO(out_free_kernbuff, rc = -EINVAL);
3630 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
3631 GOTO(out_free_kernbuff, rc = -ENODEV);
3632 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
3633 queue = PTLRPC_NRS_QUEUE_REG;
3635 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
3637 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
3640 * Serialize NRS core lprocfs operations with policy registration/
3643 mutex_lock(&nrs_core.nrs_mutex);
3644 rc = ptlrpc_nrs_policy_control(svc, queue,
3646 NRS_CTL_TBF_WR_RULE,
3648 mutex_unlock(&nrs_core.nrs_mutex);
3650 nrs_tbf_cmd_fini(cmd);
3653 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
3655 return rc ? rc : count;
3658 LDEBUGFS_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
3661 * Initializes a TBF policy's lprocfs interface for service \a svc
3663 * \param[in] svc the service
3666 * \retval != 0 error
3668 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
3670 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
3671 { .name = "nrs_tbf_rule",
3672 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
3677 if (IS_ERR_OR_NULL(svc->srv_debugfs_entry))
3680 return ldebugfs_add_vars(svc->srv_debugfs_entry, nrs_tbf_lprocfs_vars,
3685 * TBF policy operations
3687 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
3688 .op_policy_start = nrs_tbf_start,
3689 .op_policy_stop = nrs_tbf_stop,
3690 .op_policy_ctl = nrs_tbf_ctl,
3691 .op_res_get = nrs_tbf_res_get,
3692 .op_res_put = nrs_tbf_res_put,
3693 .op_req_get = nrs_tbf_req_get,
3694 .op_req_enqueue = nrs_tbf_req_add,
3695 .op_req_dequeue = nrs_tbf_req_del,
3696 .op_req_stop = nrs_tbf_req_stop,
3697 .op_lprocfs_init = nrs_tbf_lprocfs_init,
3701 * TBF policy configuration
3703 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
3704 .nc_name = NRS_POL_NAME_TBF,
3705 .nc_ops = &nrs_tbf_ops,
3706 .nc_compat = nrs_policy_compat_all,
3713 #endif /* HAVE_SERVER_SUPPORT */