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
39 #define DEBUG_SUBSYSTEM S_RPC
40 #include <obd_support.h>
41 #include <obd_class.h>
42 #include <libcfs/libcfs.h>
43 #include <lustre_req_layout.h>
44 #include "ptlrpc_internal.h"
49 * Token Bucket Filter over client NIDs
54 #define NRS_POL_NAME_TBF "tbf"
56 static int tbf_jobid_cache_size = 8192;
57 module_param(tbf_jobid_cache_size, int, 0644);
58 MODULE_PARM_DESC(tbf_jobid_cache_size, "The size of jobid cache");
60 static int tbf_rate = 10000;
61 module_param(tbf_rate, int, 0644);
62 MODULE_PARM_DESC(tbf_rate, "Default rate limit in RPCs/s");
64 static int tbf_depth = 3;
65 module_param(tbf_depth, int, 0644);
66 MODULE_PARM_DESC(tbf_depth, "How many tokens that a client can save up");
68 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
70 struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
72 struct ptlrpc_nrs *nrs = head->th_res.res_policy->pol_nrs;
73 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
75 nrs->nrs_throttling = 0;
76 wake_up(&svcpt->scp_waitq);
78 return HRTIMER_NORESTART;
81 #define NRS_TBF_DEFAULT_RULE "default"
83 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
85 LASSERT(atomic_read(&rule->tr_ref) == 0);
86 LASSERT(list_empty(&rule->tr_cli_list));
87 LASSERT(list_empty(&rule->tr_linkage));
89 rule->tr_head->th_ops->o_rule_fini(rule);
94 * Decreases the rule's usage reference count, and stops the rule in case it
95 * was already stopping and have no more outstanding usage references (which
96 * indicates it has no more queued or started requests, and can be safely
99 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
101 if (atomic_dec_and_test(&rule->tr_ref))
102 nrs_tbf_rule_fini(rule);
106 * Increases the rule's usage reference count.
108 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
110 atomic_inc(&rule->tr_ref);
114 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
116 LASSERT(!list_empty(&cli->tc_linkage));
117 LASSERT(cli->tc_rule);
118 spin_lock(&cli->tc_rule->tr_rule_lock);
119 list_del_init(&cli->tc_linkage);
120 spin_unlock(&cli->tc_rule->tr_rule_lock);
121 nrs_tbf_rule_put(cli->tc_rule);
126 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
127 struct nrs_tbf_client *cli)
130 struct nrs_tbf_rule *rule = cli->tc_rule;
132 cli->tc_rpc_rate = rule->tr_rpc_rate;
133 cli->tc_nsecs = rule->tr_nsecs_per_rpc;
134 cli->tc_nsecs_resid = 0;
135 cli->tc_depth = rule->tr_depth;
136 cli->tc_ntoken = rule->tr_depth;
137 cli->tc_check_time = ktime_to_ns(ktime_get());
138 cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
139 cli->tc_rule_generation = rule->tr_generation;
142 binheap_relocate(head->th_binheap,
147 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
148 struct nrs_tbf_rule *rule,
149 struct nrs_tbf_client *cli)
151 spin_lock(&cli->tc_rule_lock);
152 if (cli->tc_rule != NULL && !list_empty(&cli->tc_linkage)) {
153 LASSERT(rule != cli->tc_rule);
154 nrs_tbf_cli_rule_put(cli);
156 LASSERT(cli->tc_rule == NULL);
157 LASSERT(list_empty(&cli->tc_linkage));
158 /* Rule's ref is added before called */
160 spin_lock(&rule->tr_rule_lock);
161 list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
162 spin_unlock(&rule->tr_rule_lock);
163 spin_unlock(&cli->tc_rule_lock);
164 nrs_tbf_cli_reset_value(head, cli);
168 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
170 return rule->tr_head->th_ops->o_rule_dump(rule, m);
174 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
176 struct nrs_tbf_rule *rule;
179 LASSERT(head != NULL);
180 spin_lock(&head->th_rule_lock);
181 /* List the rules from newest to oldest */
182 list_for_each_entry(rule, &head->th_list, tr_linkage) {
183 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
184 rc = nrs_tbf_rule_dump(rule, m);
190 spin_unlock(&head->th_rule_lock);
195 static struct nrs_tbf_rule *
196 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
199 struct nrs_tbf_rule *rule;
201 LASSERT(head != NULL);
202 list_for_each_entry(rule, &head->th_list, tr_linkage) {
203 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
204 if (strcmp(rule->tr_name, name) == 0) {
205 nrs_tbf_rule_get(rule);
212 static struct nrs_tbf_rule *
213 nrs_tbf_rule_find(struct nrs_tbf_head *head,
216 struct nrs_tbf_rule *rule;
218 LASSERT(head != NULL);
219 spin_lock(&head->th_rule_lock);
220 rule = nrs_tbf_rule_find_nolock(head, name);
221 spin_unlock(&head->th_rule_lock);
225 static struct nrs_tbf_rule *
226 nrs_tbf_rule_match(struct nrs_tbf_head *head,
227 struct nrs_tbf_client *cli)
229 struct nrs_tbf_rule *rule = NULL;
230 struct nrs_tbf_rule *tmp_rule;
232 spin_lock(&head->th_rule_lock);
233 /* Match the newest rule in the list */
234 list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
235 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
236 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
243 rule = head->th_rule;
245 nrs_tbf_rule_get(rule);
246 spin_unlock(&head->th_rule_lock);
251 nrs_tbf_cli_init(struct nrs_tbf_head *head,
252 struct nrs_tbf_client *cli,
253 struct ptlrpc_request *req)
255 struct nrs_tbf_rule *rule;
257 memset(cli, 0, sizeof(*cli));
258 cli->tc_in_heap = false;
259 head->th_ops->o_cli_init(cli, req);
260 INIT_LIST_HEAD(&cli->tc_list);
261 INIT_LIST_HEAD(&cli->tc_linkage);
262 spin_lock_init(&cli->tc_rule_lock);
263 atomic_set(&cli->tc_ref, 1);
264 rule = nrs_tbf_rule_match(head, cli);
265 nrs_tbf_cli_reset(head, rule, cli);
269 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
271 LASSERT(list_empty(&cli->tc_list));
272 LASSERT(!cli->tc_in_heap);
273 LASSERT(atomic_read(&cli->tc_ref) == 0);
274 spin_lock(&cli->tc_rule_lock);
275 nrs_tbf_cli_rule_put(cli);
276 spin_unlock(&cli->tc_rule_lock);
281 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
282 struct nrs_tbf_head *head,
283 struct nrs_tbf_cmd *start)
285 struct nrs_tbf_rule *rule;
286 struct nrs_tbf_rule *tmp_rule;
287 struct nrs_tbf_rule *next_rule;
288 char *next_name = start->u.tc_start.ts_next_name;
291 rule = nrs_tbf_rule_find(head, start->tc_name);
293 nrs_tbf_rule_put(rule);
297 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
301 strlcpy(rule->tr_name, start->tc_name, sizeof(rule->tr_name));
302 rule->tr_rpc_rate = start->u.tc_start.ts_rpc_rate;
303 rule->tr_flags = start->u.tc_start.ts_rule_flags;
304 rule->tr_nsecs_per_rpc = NSEC_PER_SEC / rule->tr_rpc_rate;
305 rule->tr_depth = tbf_depth;
306 atomic_set(&rule->tr_ref, 1);
307 INIT_LIST_HEAD(&rule->tr_cli_list);
308 INIT_LIST_HEAD(&rule->tr_nids);
309 INIT_LIST_HEAD(&rule->tr_linkage);
310 spin_lock_init(&rule->tr_rule_lock);
311 rule->tr_head = head;
313 rc = head->th_ops->o_rule_init(policy, rule, start);
319 /* Add as the newest rule */
320 spin_lock(&head->th_rule_lock);
321 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
323 spin_unlock(&head->th_rule_lock);
324 nrs_tbf_rule_put(tmp_rule);
325 nrs_tbf_rule_put(rule);
330 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
332 spin_unlock(&head->th_rule_lock);
333 nrs_tbf_rule_put(rule);
337 list_add(&rule->tr_linkage, next_rule->tr_linkage.prev);
338 nrs_tbf_rule_put(next_rule);
340 /* Add on the top of the rule list */
341 list_add(&rule->tr_linkage, &head->th_list);
343 spin_unlock(&head->th_rule_lock);
344 atomic_inc(&head->th_rule_sequence);
345 if (start->u.tc_start.ts_rule_flags & NTRS_DEFAULT) {
346 rule->tr_flags |= NTRS_DEFAULT;
347 LASSERT(head->th_rule == NULL);
348 head->th_rule = rule;
351 CDEBUG(D_RPCTRACE, "TBF starts rule@%p rate %llu gen %llu\n",
352 rule, rule->tr_rpc_rate, rule->tr_generation);
358 * Change the rank of a rule in the rule list
360 * The matched rule will be moved to the position right before another
363 * \param[in] policy the policy instance
364 * \param[in] head the TBF policy instance
365 * \param[in] name the rule name to be moved
366 * \param[in] next_name the rule name before which the matched rule will be
371 nrs_tbf_rule_change_rank(struct ptlrpc_nrs_policy *policy,
372 struct nrs_tbf_head *head,
376 struct nrs_tbf_rule *rule = NULL;
377 struct nrs_tbf_rule *next_rule = NULL;
380 LASSERT(head != NULL);
382 spin_lock(&head->th_rule_lock);
383 rule = nrs_tbf_rule_find_nolock(head, name);
385 GOTO(out, rc = -ENOENT);
387 if (strcmp(name, next_name) == 0)
390 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
392 GOTO(out_put, rc = -ENOENT);
394 /* rules may be adjacent in same list, so list_move() isn't safe here */
395 list_move_tail(&rule->tr_linkage, &next_rule->tr_linkage);
396 nrs_tbf_rule_put(next_rule);
398 nrs_tbf_rule_put(rule);
400 spin_unlock(&head->th_rule_lock);
405 nrs_tbf_rule_change_rate(struct ptlrpc_nrs_policy *policy,
406 struct nrs_tbf_head *head,
410 struct nrs_tbf_rule *rule;
412 assert_spin_locked(&policy->pol_nrs->nrs_lock);
414 rule = nrs_tbf_rule_find(head, name);
418 rule->tr_rpc_rate = rate;
419 rule->tr_nsecs_per_rpc = NSEC_PER_SEC / rule->tr_rpc_rate;
420 rule->tr_generation++;
421 nrs_tbf_rule_put(rule);
427 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
428 struct nrs_tbf_head *head,
429 struct nrs_tbf_cmd *change)
431 __u64 rate = change->u.tc_change.tc_rpc_rate;
432 char *next_name = change->u.tc_change.tc_next_name;
436 rc = nrs_tbf_rule_change_rate(policy, head, change->tc_name,
443 rc = nrs_tbf_rule_change_rank(policy, head, change->tc_name,
453 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
454 struct nrs_tbf_head *head,
455 struct nrs_tbf_cmd *stop)
457 struct nrs_tbf_rule *rule;
459 assert_spin_locked(&policy->pol_nrs->nrs_lock);
461 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
464 rule = nrs_tbf_rule_find(head, stop->tc_name);
468 list_del_init(&rule->tr_linkage);
469 rule->tr_flags |= NTRS_STOPPING;
470 nrs_tbf_rule_put(rule);
471 nrs_tbf_rule_put(rule);
477 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
478 struct nrs_tbf_head *head,
479 struct nrs_tbf_cmd *cmd)
483 assert_spin_locked(&policy->pol_nrs->nrs_lock);
485 switch (cmd->tc_cmd) {
486 case NRS_CTL_TBF_START_RULE:
487 if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
490 spin_unlock(&policy->pol_nrs->nrs_lock);
491 rc = nrs_tbf_rule_start(policy, head, cmd);
492 spin_lock(&policy->pol_nrs->nrs_lock);
494 case NRS_CTL_TBF_CHANGE_RULE:
495 rc = nrs_tbf_rule_change(policy, head, cmd);
497 case NRS_CTL_TBF_STOP_RULE:
498 rc = nrs_tbf_rule_stop(policy, head, cmd);
499 /* Take it as a success, if not exists at all */
500 return rc == -ENOENT ? 0 : rc;
507 * Binary heap predicate.
509 * \param[in] e1 the first binheap node to compare
510 * \param[in] e2 the second binheap node to compare
516 tbf_cli_compare(struct binheap_node *e1, struct binheap_node *e2)
518 struct nrs_tbf_client *cli1;
519 struct nrs_tbf_client *cli2;
521 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
522 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
524 if (cli1->tc_deadline < cli2->tc_deadline)
526 else if (cli1->tc_deadline > cli2->tc_deadline)
529 if (cli1->tc_check_time < cli2->tc_check_time)
531 else if (cli1->tc_check_time > cli2->tc_check_time)
534 /* Maybe need more comparasion, e.g. request number in the rules */
539 * TBF binary heap operations
541 static struct binheap_ops nrs_tbf_heap_ops = {
544 .hop_compare = tbf_cli_compare,
547 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
550 return cfs_hash_djb2_hash(key, strlen(key), mask);
553 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
555 struct nrs_tbf_client *cli = hlist_entry(hnode,
556 struct nrs_tbf_client,
559 return (strcmp(cli->tc_jobid, key) == 0);
562 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
564 struct nrs_tbf_client *cli = hlist_entry(hnode,
565 struct nrs_tbf_client,
568 return cli->tc_jobid;
571 static void *nrs_tbf_hop_object(struct hlist_node *hnode)
573 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
576 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
578 struct nrs_tbf_client *cli = hlist_entry(hnode,
579 struct nrs_tbf_client,
582 atomic_inc(&cli->tc_ref);
585 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
587 struct nrs_tbf_client *cli = hlist_entry(hnode,
588 struct nrs_tbf_client,
591 atomic_dec(&cli->tc_ref);
595 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
598 struct nrs_tbf_client *cli = hlist_entry(hnode,
599 struct nrs_tbf_client,
602 LASSERT(atomic_read(&cli->tc_ref) == 0);
603 nrs_tbf_cli_fini(cli);
606 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
607 .hs_hash = nrs_tbf_jobid_hop_hash,
608 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
609 .hs_key = nrs_tbf_jobid_hop_key,
610 .hs_object = nrs_tbf_hop_object,
611 .hs_get = nrs_tbf_jobid_hop_get,
612 .hs_put = nrs_tbf_jobid_hop_put,
613 .hs_put_locked = nrs_tbf_jobid_hop_put,
614 .hs_exit = nrs_tbf_jobid_hop_exit,
617 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
618 CFS_HASH_NO_ITEMREF | \
621 static struct nrs_tbf_client *
622 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
623 struct cfs_hash_bd *bd,
626 struct hlist_node *hnode;
627 struct nrs_tbf_client *cli;
629 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)jobid);
633 cli = container_of(hnode, struct nrs_tbf_client, tc_hnode);
634 if (!list_empty(&cli->tc_lru))
635 list_del_init(&cli->tc_lru);
639 #define NRS_TBF_JOBID_NULL ""
641 static struct nrs_tbf_client *
642 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
643 struct ptlrpc_request *req)
646 struct nrs_tbf_client *cli;
647 struct cfs_hash *hs = head->th_cli_hash;
648 struct cfs_hash_bd bd;
650 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
652 jobid = NRS_TBF_JOBID_NULL;
653 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
654 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
655 cfs_hash_bd_unlock(hs, &bd, 1);
660 static struct nrs_tbf_client *
661 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
662 struct nrs_tbf_client *cli)
665 struct nrs_tbf_client *ret;
666 struct cfs_hash *hs = head->th_cli_hash;
667 struct cfs_hash_bd bd;
669 jobid = cli->tc_jobid;
670 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
671 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
673 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
676 cfs_hash_bd_unlock(hs, &bd, 1);
682 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
683 struct nrs_tbf_client *cli)
685 struct cfs_hash_bd bd;
686 struct cfs_hash *hs = head->th_cli_hash;
687 struct nrs_tbf_bucket *bkt;
691 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
692 bkt = cfs_hash_bd_extra_get(hs, &bd);
693 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
695 LASSERT(list_empty(&cli->tc_lru));
696 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
699 * Check and purge the LRU, there is at least one client in the LRU.
701 hw = tbf_jobid_cache_size >>
702 (hs->hs_cur_bits - hs->hs_bkt_bits);
703 while (cfs_hash_bd_count_get(&bd) > hw) {
704 if (unlikely(list_empty(&bkt->ntb_lru)))
706 cli = list_entry(bkt->ntb_lru.next,
707 struct nrs_tbf_client,
709 LASSERT(atomic_read(&cli->tc_ref) == 0);
710 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
711 list_move(&cli->tc_lru, &zombies);
713 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
715 while (!list_empty(&zombies)) {
716 cli = container_of(zombies.next,
717 struct nrs_tbf_client, tc_lru);
718 list_del_init(&cli->tc_lru);
719 nrs_tbf_cli_fini(cli);
724 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
725 struct ptlrpc_request *req)
727 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
730 jobid = NRS_TBF_JOBID_NULL;
731 LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
732 INIT_LIST_HEAD(&cli->tc_lru);
733 memcpy(cli->tc_jobid, jobid, strlen(jobid));
736 static int nrs_tbf_jobid_hash_order(void)
740 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
746 #define NRS_TBF_JOBID_BKT_BITS 10
749 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
750 struct nrs_tbf_head *head)
752 struct nrs_tbf_cmd start;
753 struct nrs_tbf_bucket *bkt;
757 struct cfs_hash_bd bd;
759 bits = nrs_tbf_jobid_hash_order();
760 if (bits < NRS_TBF_JOBID_BKT_BITS)
761 bits = NRS_TBF_JOBID_BKT_BITS;
762 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
765 NRS_TBF_JOBID_BKT_BITS,
769 &nrs_tbf_jobid_hash_ops,
770 NRS_TBF_JOBID_HASH_FLAGS);
771 if (head->th_cli_hash == NULL)
774 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
775 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
776 INIT_LIST_HEAD(&bkt->ntb_lru);
779 memset(&start, 0, sizeof(start));
780 start.u.tc_start.ts_jobids_str = "*";
782 start.u.tc_start.ts_rpc_rate = tbf_rate;
783 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
784 start.tc_name = NRS_TBF_DEFAULT_RULE;
785 INIT_LIST_HEAD(&start.u.tc_start.ts_jobids);
786 rc = nrs_tbf_rule_start(policy, head, &start);
788 cfs_hash_putref(head->th_cli_hash);
789 head->th_cli_hash = NULL;
796 * Frees jobid of \a list.
800 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
802 struct nrs_tbf_jobid *jobid, *n;
804 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
805 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
806 list_del(&jobid->tj_linkage);
812 nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
814 struct nrs_tbf_jobid *jobid;
817 OBD_ALLOC_PTR(jobid);
821 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
822 if (jobid->tj_id == NULL) {
827 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
828 ptr = lprocfs_strnstr(id->ls_str, "*", id->ls_len);
830 jobid->tj_match_flag = NRS_TBF_MATCH_FULL;
832 jobid->tj_match_flag = NRS_TBF_MATCH_WILDCARD;
834 list_add_tail(&jobid->tj_linkage, jobid_list);
839 cfs_match_wildcard(const char *pattern, const char *content)
841 if (*pattern == '\0' && *content == '\0')
844 if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
847 while (*pattern == *content) {
850 if (*pattern == '\0' && *content == '\0')
853 if (*pattern == '*' && *(pattern + 1) != '\0' &&
859 return (cfs_match_wildcard(pattern + 1, content) ||
860 cfs_match_wildcard(pattern, content + 1));
866 nrs_tbf_jobid_match(const struct nrs_tbf_jobid *jobid, const char *id)
868 if (jobid->tj_match_flag == NRS_TBF_MATCH_FULL)
869 return strcmp(jobid->tj_id, id) == 0;
871 if (jobid->tj_match_flag == NRS_TBF_MATCH_WILDCARD)
872 return cfs_match_wildcard(jobid->tj_id, id);
878 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
880 struct nrs_tbf_jobid *jobid;
882 list_for_each_entry(jobid, jobid_list, tj_linkage) {
883 if (nrs_tbf_jobid_match(jobid, id))
890 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
899 INIT_LIST_HEAD(jobid_list);
901 rc = cfs_gettok(&src, ' ', &res);
906 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
911 nrs_tbf_jobid_list_free(jobid_list);
915 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
917 if (!list_empty(&cmd->u.tc_start.ts_jobids))
918 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
919 if (cmd->u.tc_start.ts_jobids_str)
920 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
921 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
924 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
927 int keylen = strlen(key);
930 rc = cfs_gettok(src, '=', &res);
931 if (rc == 0 || res.ls_len != keylen ||
932 strncmp(res.ls_str, key, keylen) != 0 ||
933 !src->ls_str || src->ls_len <= 2 ||
934 src->ls_str[0] != '{' || src->ls_str[src->ls_len - 1] != '}')
937 /* Skip '{' and '}' */
943 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
949 src.ls_len = strlen(id);
950 rc = nrs_tbf_check_id_value(&src, "jobid");
954 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
955 if (cmd->u.tc_start.ts_jobids_str == NULL)
958 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
960 /* parse jobid list */
961 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
962 strlen(cmd->u.tc_start.ts_jobids_str),
963 &cmd->u.tc_start.ts_jobids);
965 nrs_tbf_jobid_cmd_fini(cmd);
970 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
971 struct nrs_tbf_rule *rule,
972 struct nrs_tbf_cmd *start)
976 LASSERT(start->u.tc_start.ts_jobids_str);
977 OBD_ALLOC(rule->tr_jobids_str,
978 strlen(start->u.tc_start.ts_jobids_str) + 1);
979 if (rule->tr_jobids_str == NULL)
982 memcpy(rule->tr_jobids_str,
983 start->u.tc_start.ts_jobids_str,
984 strlen(start->u.tc_start.ts_jobids_str));
986 INIT_LIST_HEAD(&rule->tr_jobids);
987 if (!list_empty(&start->u.tc_start.ts_jobids)) {
988 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
989 strlen(rule->tr_jobids_str),
992 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
995 OBD_FREE(rule->tr_jobids_str,
996 strlen(start->u.tc_start.ts_jobids_str) + 1);
1001 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1003 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1004 rule->tr_jobids_str, rule->tr_rpc_rate,
1005 atomic_read(&rule->tr_ref) - 1);
1010 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
1011 struct nrs_tbf_client *cli)
1013 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
1016 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
1018 if (!list_empty(&rule->tr_jobids))
1019 nrs_tbf_jobid_list_free(&rule->tr_jobids);
1020 LASSERT(rule->tr_jobids_str != NULL);
1021 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
1024 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
1025 .o_name = NRS_TBF_TYPE_JOBID,
1026 .o_startup = nrs_tbf_jobid_startup,
1027 .o_cli_find = nrs_tbf_jobid_cli_find,
1028 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
1029 .o_cli_put = nrs_tbf_jobid_cli_put,
1030 .o_cli_init = nrs_tbf_jobid_cli_init,
1031 .o_rule_init = nrs_tbf_jobid_rule_init,
1032 .o_rule_dump = nrs_tbf_jobid_rule_dump,
1033 .o_rule_match = nrs_tbf_jobid_rule_match,
1034 .o_rule_fini = nrs_tbf_jobid_rule_fini,
1038 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
1040 * This uses ptlrpc_request::rq_peer.nid (as nid4) as its key, in order to hash
1041 * nrs_tbf_client objects.
1043 #define NRS_TBF_NID_BKT_BITS 8
1044 #define NRS_TBF_NID_BITS 16
1046 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1049 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1052 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1054 lnet_nid_t *nid = (lnet_nid_t *)key;
1055 struct nrs_tbf_client *cli = hlist_entry(hnode,
1056 struct nrs_tbf_client,
1059 return *nid == cli->tc_nid;
1062 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1064 struct nrs_tbf_client *cli = hlist_entry(hnode,
1065 struct nrs_tbf_client,
1068 return &cli->tc_nid;
1071 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1073 struct nrs_tbf_client *cli = hlist_entry(hnode,
1074 struct nrs_tbf_client,
1077 atomic_inc(&cli->tc_ref);
1080 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1082 struct nrs_tbf_client *cli = hlist_entry(hnode,
1083 struct nrs_tbf_client,
1086 atomic_dec(&cli->tc_ref);
1089 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1091 struct nrs_tbf_client *cli = hlist_entry(hnode,
1092 struct nrs_tbf_client,
1095 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1096 "Busy TBF object from client with NID %s, with %d refs\n",
1097 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1099 nrs_tbf_cli_fini(cli);
1102 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1103 .hs_hash = nrs_tbf_nid_hop_hash,
1104 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
1105 .hs_key = nrs_tbf_nid_hop_key,
1106 .hs_object = nrs_tbf_hop_object,
1107 .hs_get = nrs_tbf_nid_hop_get,
1108 .hs_put = nrs_tbf_nid_hop_put,
1109 .hs_put_locked = nrs_tbf_nid_hop_put,
1110 .hs_exit = nrs_tbf_nid_hop_exit,
1113 static struct nrs_tbf_client *
1114 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1115 struct ptlrpc_request *req)
1117 lnet_nid_t nid4 = lnet_nid_to_nid4(&req->rq_peer.nid);
1119 return cfs_hash_lookup(head->th_cli_hash, &nid4);
1122 static struct nrs_tbf_client *
1123 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1124 struct nrs_tbf_client *cli)
1126 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1131 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1132 struct nrs_tbf_client *cli)
1134 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1138 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1139 struct nrs_tbf_head *head)
1141 struct nrs_tbf_cmd start;
1144 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1147 NRS_TBF_NID_BKT_BITS, 0,
1150 &nrs_tbf_nid_hash_ops,
1151 CFS_HASH_RW_BKTLOCK);
1152 if (head->th_cli_hash == NULL)
1155 memset(&start, 0, sizeof(start));
1156 start.u.tc_start.ts_nids_str = "*";
1158 start.u.tc_start.ts_rpc_rate = tbf_rate;
1159 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1160 start.tc_name = NRS_TBF_DEFAULT_RULE;
1161 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1162 rc = nrs_tbf_rule_start(policy, head, &start);
1164 cfs_hash_putref(head->th_cli_hash);
1165 head->th_cli_hash = NULL;
1172 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1173 struct ptlrpc_request *req)
1175 cli->tc_nid = lnet_nid_to_nid4(&req->rq_peer.nid);
1178 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1179 struct nrs_tbf_rule *rule,
1180 struct nrs_tbf_cmd *start)
1182 LASSERT(start->u.tc_start.ts_nids_str);
1183 OBD_ALLOC(rule->tr_nids_str,
1184 strlen(start->u.tc_start.ts_nids_str) + 1);
1185 if (rule->tr_nids_str == NULL)
1188 memcpy(rule->tr_nids_str,
1189 start->u.tc_start.ts_nids_str,
1190 strlen(start->u.tc_start.ts_nids_str));
1192 INIT_LIST_HEAD(&rule->tr_nids);
1193 if (!list_empty(&start->u.tc_start.ts_nids)) {
1194 if (cfs_parse_nidlist(rule->tr_nids_str,
1195 strlen(rule->tr_nids_str),
1196 &rule->tr_nids) <= 0) {
1197 CERROR("nids {%s} illegal\n",
1199 OBD_FREE(rule->tr_nids_str,
1200 strlen(start->u.tc_start.ts_nids_str) + 1);
1208 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1210 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1211 rule->tr_nids_str, rule->tr_rpc_rate,
1212 atomic_read(&rule->tr_ref) - 1);
1217 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1218 struct nrs_tbf_client *cli)
1220 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1223 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1225 if (!list_empty(&rule->tr_nids))
1226 cfs_free_nidlist(&rule->tr_nids);
1227 LASSERT(rule->tr_nids_str != NULL);
1228 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1231 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1233 if (!list_empty(&cmd->u.tc_start.ts_nids))
1234 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1235 if (cmd->u.tc_start.ts_nids_str)
1236 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1237 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1240 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1242 struct cfs_lstr src;
1246 src.ls_len = strlen(id);
1247 rc = nrs_tbf_check_id_value(&src, "nid");
1251 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1252 if (cmd->u.tc_start.ts_nids_str == NULL)
1255 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1257 /* parse NID list */
1258 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1259 strlen(cmd->u.tc_start.ts_nids_str),
1260 &cmd->u.tc_start.ts_nids) <= 0) {
1261 nrs_tbf_nid_cmd_fini(cmd);
1268 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1269 .o_name = NRS_TBF_TYPE_NID,
1270 .o_startup = nrs_tbf_nid_startup,
1271 .o_cli_find = nrs_tbf_nid_cli_find,
1272 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1273 .o_cli_put = nrs_tbf_nid_cli_put,
1274 .o_cli_init = nrs_tbf_nid_cli_init,
1275 .o_rule_init = nrs_tbf_nid_rule_init,
1276 .o_rule_dump = nrs_tbf_nid_rule_dump,
1277 .o_rule_match = nrs_tbf_nid_rule_match,
1278 .o_rule_fini = nrs_tbf_nid_rule_fini,
1281 static unsigned nrs_tbf_hop_hash(struct cfs_hash *hs, const void *key,
1284 return cfs_hash_djb2_hash(key, strlen(key), mask);
1287 static int nrs_tbf_hop_keycmp(const void *key, struct hlist_node *hnode)
1289 struct nrs_tbf_client *cli = hlist_entry(hnode,
1290 struct nrs_tbf_client,
1293 return (strcmp(cli->tc_key, key) == 0);
1296 static void *nrs_tbf_hop_key(struct hlist_node *hnode)
1298 struct nrs_tbf_client *cli = hlist_entry(hnode,
1299 struct nrs_tbf_client,
1304 static void nrs_tbf_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1306 struct nrs_tbf_client *cli = hlist_entry(hnode,
1307 struct nrs_tbf_client,
1310 atomic_inc(&cli->tc_ref);
1313 static void nrs_tbf_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1315 struct nrs_tbf_client *cli = hlist_entry(hnode,
1316 struct nrs_tbf_client,
1319 atomic_dec(&cli->tc_ref);
1322 static void nrs_tbf_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1325 struct nrs_tbf_client *cli = hlist_entry(hnode,
1326 struct nrs_tbf_client,
1329 LASSERT(atomic_read(&cli->tc_ref) == 0);
1330 nrs_tbf_cli_fini(cli);
1333 static struct cfs_hash_ops nrs_tbf_hash_ops = {
1334 .hs_hash = nrs_tbf_hop_hash,
1335 .hs_keycmp = nrs_tbf_hop_keycmp,
1336 .hs_key = nrs_tbf_hop_key,
1337 .hs_object = nrs_tbf_hop_object,
1338 .hs_get = nrs_tbf_hop_get,
1339 .hs_put = nrs_tbf_hop_put,
1340 .hs_put_locked = nrs_tbf_hop_put,
1341 .hs_exit = nrs_tbf_hop_exit,
1344 #define NRS_TBF_GENERIC_BKT_BITS 10
1345 #define NRS_TBF_GENERIC_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
1346 CFS_HASH_NO_ITEMREF | \
1350 nrs_tbf_startup(struct ptlrpc_nrs_policy *policy, struct nrs_tbf_head *head)
1352 struct nrs_tbf_cmd start;
1353 struct nrs_tbf_bucket *bkt;
1357 struct cfs_hash_bd bd;
1359 bits = nrs_tbf_jobid_hash_order();
1360 if (bits < NRS_TBF_GENERIC_BKT_BITS)
1361 bits = NRS_TBF_GENERIC_BKT_BITS;
1362 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1364 NRS_TBF_GENERIC_BKT_BITS,
1367 NRS_TBF_GENERIC_HASH_FLAGS);
1368 if (head->th_cli_hash == NULL)
1371 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
1372 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
1373 INIT_LIST_HEAD(&bkt->ntb_lru);
1376 memset(&start, 0, sizeof(start));
1377 start.u.tc_start.ts_conds_str = "*";
1379 start.u.tc_start.ts_rpc_rate = tbf_rate;
1380 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1381 start.tc_name = NRS_TBF_DEFAULT_RULE;
1382 INIT_LIST_HEAD(&start.u.tc_start.ts_conds);
1383 rc = nrs_tbf_rule_start(policy, head, &start);
1385 cfs_hash_putref(head->th_cli_hash);
1390 static struct nrs_tbf_client *
1391 nrs_tbf_cli_hash_lookup(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1394 struct hlist_node *hnode;
1395 struct nrs_tbf_client *cli;
1397 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)key);
1401 cli = container_of(hnode, struct nrs_tbf_client, tc_hnode);
1402 if (!list_empty(&cli->tc_lru))
1403 list_del_init(&cli->tc_lru);
1408 * ONLY opcode presented in this function will be checked in
1409 * nrs_tbf_id_cli_set(). That means, we can add or remove an
1410 * opcode to enable or disable requests handled in nrs_tbf
1412 static struct req_format *req_fmt(__u32 opcode)
1416 return &RQF_OST_GETATTR;
1418 return &RQF_OST_SETATTR;
1420 return &RQF_OST_BRW_READ;
1422 return &RQF_OST_BRW_WRITE;
1423 /* FIXME: OST_CREATE and OST_DESTROY comes from MDS
1424 * in most case. Should they be removed? */
1426 return &RQF_OST_CREATE;
1428 return &RQF_OST_DESTROY;
1430 return &RQF_OST_PUNCH;
1432 return &RQF_OST_SYNC;
1434 return &RQF_OST_LADVISE;
1436 return &RQF_MDS_GETATTR;
1437 case MDS_GETATTR_NAME:
1438 return &RQF_MDS_GETATTR_NAME;
1439 /* close is skipped to avoid LDLM cancel slowness */
1442 return &RQF_MDS_CLOSE;
1445 return &RQF_MDS_REINT;
1447 return &RQF_MDS_READPAGE;
1449 return &RQF_MDS_GET_ROOT;
1451 return &RQF_MDS_STATFS;
1453 return &RQF_MDS_SYNC;
1455 return &RQF_MDS_QUOTACTL;
1457 return &RQF_MDS_GETXATTR;
1459 return &RQF_MDS_GET_INFO;
1460 /* HSM op is skipped */
1462 case MDS_HSM_STATE_GET:
1463 return &RQF_MDS_HSM_STATE_GET;
1464 case MDS_HSM_STATE_SET:
1465 return &RQF_MDS_HSM_STATE_SET;
1466 case MDS_HSM_ACTION:
1467 return &RQF_MDS_HSM_ACTION;
1468 case MDS_HSM_CT_REGISTER:
1469 return &RQF_MDS_HSM_CT_REGISTER;
1470 case MDS_HSM_CT_UNREGISTER:
1471 return &RQF_MDS_HSM_CT_UNREGISTER;
1473 case MDS_SWAP_LAYOUTS:
1474 return &RQF_MDS_SWAP_LAYOUTS;
1476 return &RQF_LDLM_ENQUEUE;
1482 static struct req_format *intent_req_fmt(__u32 it_opc)
1484 if (it_opc & (IT_OPEN | IT_CREAT))
1485 return &RQF_LDLM_INTENT_OPEN;
1486 else if (it_opc & (IT_GETATTR | IT_LOOKUP))
1487 return &RQF_LDLM_INTENT_GETATTR;
1488 else if (it_opc & IT_GETXATTR)
1489 return &RQF_LDLM_INTENT_GETXATTR;
1490 else if (it_opc & (IT_GLIMPSE | IT_BRW))
1491 return &RQF_LDLM_INTENT;
1496 static int ost_tbf_id_cli_set(struct ptlrpc_request *req,
1499 struct ost_body *body;
1501 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
1503 id->ti_uid = body->oa.o_uid;
1504 id->ti_gid = body->oa.o_gid;
1511 static void unpack_ugid_from_mdt_body(struct ptlrpc_request *req,
1514 struct mdt_body *b = req_capsule_client_get(&req->rq_pill,
1518 /* TODO: nodemaping feature converts {ug}id from individual
1519 * clients to the actual ones of the file system. Some work
1520 * may be needed to fix this. */
1521 id->ti_uid = b->mbo_uid;
1522 id->ti_gid = b->mbo_gid;
1525 static void unpack_ugid_from_mdt_rec_reint(struct ptlrpc_request *req,
1528 struct mdt_rec_reint *rec;
1530 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1531 LASSERT(rec != NULL);
1533 /* use the fs{ug}id as {ug}id of the process */
1534 id->ti_uid = rec->rr_fsuid;
1535 id->ti_gid = rec->rr_fsgid;
1538 static int mdt_tbf_id_cli_set(struct ptlrpc_request *req,
1541 u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1546 case MDS_GETATTR_NAME:
1551 case MDS_HSM_STATE_GET ... MDS_SWAP_LAYOUTS:
1552 unpack_ugid_from_mdt_body(req, id);
1556 unpack_ugid_from_mdt_rec_reint(req, id);
1565 static int ldlm_tbf_id_cli_set(struct ptlrpc_request *req,
1568 struct ldlm_intent *lit;
1569 struct req_format *fmt;
1571 if (req->rq_reqmsg->lm_bufcount <= DLM_INTENT_IT_OFF)
1574 req_capsule_extend(&req->rq_pill, &RQF_LDLM_INTENT_BASIC);
1575 lit = req_capsule_client_get(&req->rq_pill, &RMF_LDLM_INTENT);
1579 fmt = intent_req_fmt(lit->opc);
1583 req_capsule_extend(&req->rq_pill, fmt);
1585 if (lit->opc & (IT_GETXATTR | IT_GETATTR | IT_LOOKUP))
1586 unpack_ugid_from_mdt_body(req, id);
1587 else if (lit->opc & (IT_OPEN | IT_OPEN | IT_GLIMPSE | IT_BRW))
1588 unpack_ugid_from_mdt_rec_reint(req, id);
1594 static int nrs_tbf_id_cli_set(struct ptlrpc_request *req, struct tbf_id *id,
1595 enum nrs_tbf_flag ti_type)
1597 u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1598 struct req_format *fmt = req_fmt(opc);
1599 bool fmt_unset = false;
1602 memset(id, 0, sizeof(struct tbf_id));
1603 id->ti_type = ti_type;
1607 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1608 if (req->rq_pill.rc_fmt == NULL) {
1609 req_capsule_set(&req->rq_pill, fmt);
1613 if (opc < OST_LAST_OPC)
1614 rc = ost_tbf_id_cli_set(req, id);
1615 else if (opc >= MDS_FIRST_OPC && opc < MDS_LAST_OPC)
1616 rc = mdt_tbf_id_cli_set(req, id);
1617 else if (opc == LDLM_ENQUEUE)
1618 rc = ldlm_tbf_id_cli_set(req, id);
1622 /* restore it to the initialized state */
1624 req->rq_pill.rc_fmt = NULL;
1628 static inline void nrs_tbf_cli_gen_key(struct nrs_tbf_client *cli,
1629 struct ptlrpc_request *req,
1630 char *keystr, size_t keystr_sz)
1633 u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1636 nrs_tbf_id_cli_set(req, &id, NRS_TBF_FLAG_UID | NRS_TBF_FLAG_GID);
1637 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1639 jobid = NRS_TBF_JOBID_NULL;
1641 snprintf(keystr, keystr_sz, "%s_%s_%d_%u_%u", jobid,
1642 libcfs_nidstr(&req->rq_peer.nid), opc, id.ti_uid,
1646 INIT_LIST_HEAD(&cli->tc_lru);
1647 strlcpy(cli->tc_key, keystr, sizeof(cli->tc_key));
1648 strlcpy(cli->tc_jobid, jobid, sizeof(cli->tc_jobid));
1649 cli->tc_nid = lnet_nid_to_nid4(&req->rq_peer.nid);
1650 cli->tc_opcode = opc;
1655 static struct nrs_tbf_client *
1656 nrs_tbf_cli_find(struct nrs_tbf_head *head, struct ptlrpc_request *req)
1658 struct nrs_tbf_client *cli;
1659 struct cfs_hash *hs = head->th_cli_hash;
1660 struct cfs_hash_bd bd;
1661 char keystr[NRS_TBF_KEY_LEN];
1663 nrs_tbf_cli_gen_key(NULL, req, keystr, sizeof(keystr));
1664 cfs_hash_bd_get_and_lock(hs, (void *)keystr, &bd, 1);
1665 cli = nrs_tbf_cli_hash_lookup(hs, &bd, keystr);
1666 cfs_hash_bd_unlock(hs, &bd, 1);
1671 static struct nrs_tbf_client *
1672 nrs_tbf_cli_findadd(struct nrs_tbf_head *head,
1673 struct nrs_tbf_client *cli)
1676 struct nrs_tbf_client *ret;
1677 struct cfs_hash *hs = head->th_cli_hash;
1678 struct cfs_hash_bd bd;
1681 cfs_hash_bd_get_and_lock(hs, (void *)key, &bd, 1);
1682 ret = nrs_tbf_cli_hash_lookup(hs, &bd, key);
1684 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
1687 cfs_hash_bd_unlock(hs, &bd, 1);
1693 nrs_tbf_cli_put(struct nrs_tbf_head *head, struct nrs_tbf_client *cli)
1695 struct cfs_hash_bd bd;
1696 struct cfs_hash *hs = head->th_cli_hash;
1697 struct nrs_tbf_bucket *bkt;
1701 cfs_hash_bd_get(hs, &cli->tc_key, &bd);
1702 bkt = cfs_hash_bd_extra_get(hs, &bd);
1703 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
1705 LASSERT(list_empty(&cli->tc_lru));
1706 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
1709 * Check and purge the LRU, there is at least one client in the LRU.
1711 hw = tbf_jobid_cache_size >> (hs->hs_cur_bits - hs->hs_bkt_bits);
1712 while (cfs_hash_bd_count_get(&bd) > hw) {
1713 if (unlikely(list_empty(&bkt->ntb_lru)))
1715 cli = list_entry(bkt->ntb_lru.next,
1716 struct nrs_tbf_client,
1718 LASSERT(atomic_read(&cli->tc_ref) == 0);
1719 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
1720 list_move(&cli->tc_lru, &zombies);
1722 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
1724 while (!list_empty(&zombies)) {
1725 cli = container_of(zombies.next,
1726 struct nrs_tbf_client, tc_lru);
1727 list_del_init(&cli->tc_lru);
1728 nrs_tbf_cli_fini(cli);
1733 nrs_tbf_generic_cli_init(struct nrs_tbf_client *cli,
1734 struct ptlrpc_request *req)
1736 char keystr[NRS_TBF_KEY_LEN];
1738 nrs_tbf_cli_gen_key(cli, req, keystr, sizeof(keystr));
1742 nrs_tbf_id_list_free(struct list_head *uid_list)
1744 struct nrs_tbf_id *nti_id, *n;
1746 list_for_each_entry_safe(nti_id, n, uid_list, nti_linkage) {
1747 list_del_init(&nti_id->nti_linkage);
1748 OBD_FREE_PTR(nti_id);
1753 nrs_tbf_expression_free(struct nrs_tbf_expression *expr)
1755 LASSERT(expr->te_field >= NRS_TBF_FIELD_NID &&
1756 expr->te_field < NRS_TBF_FIELD_MAX);
1757 switch (expr->te_field) {
1758 case NRS_TBF_FIELD_NID:
1759 cfs_free_nidlist(&expr->te_cond);
1761 case NRS_TBF_FIELD_JOBID:
1762 nrs_tbf_jobid_list_free(&expr->te_cond);
1764 case NRS_TBF_FIELD_OPCODE:
1765 bitmap_free(expr->te_opcodes);
1767 case NRS_TBF_FIELD_UID:
1768 case NRS_TBF_FIELD_GID:
1769 nrs_tbf_id_list_free(&expr->te_cond);
1778 nrs_tbf_conjunction_free(struct nrs_tbf_conjunction *conjunction)
1780 struct nrs_tbf_expression *expression;
1781 struct nrs_tbf_expression *n;
1783 LASSERT(list_empty(&conjunction->tc_linkage));
1784 list_for_each_entry_safe(expression, n,
1785 &conjunction->tc_expressions,
1787 list_del_init(&expression->te_linkage);
1788 nrs_tbf_expression_free(expression);
1790 OBD_FREE_PTR(conjunction);
1794 nrs_tbf_conds_free(struct list_head *cond_list)
1796 struct nrs_tbf_conjunction *conjunction;
1797 struct nrs_tbf_conjunction *n;
1799 list_for_each_entry_safe(conjunction, n, cond_list, tc_linkage) {
1800 list_del_init(&conjunction->tc_linkage);
1801 nrs_tbf_conjunction_free(conjunction);
1806 nrs_tbf_generic_cmd_fini(struct nrs_tbf_cmd *cmd)
1808 if (!list_empty(&cmd->u.tc_start.ts_conds))
1809 nrs_tbf_conds_free(&cmd->u.tc_start.ts_conds);
1810 if (cmd->u.tc_start.ts_conds_str)
1811 OBD_FREE(cmd->u.tc_start.ts_conds_str,
1812 strlen(cmd->u.tc_start.ts_conds_str) + 1);
1815 #define NRS_TBF_DISJUNCTION_DELIM (',')
1816 #define NRS_TBF_CONJUNCTION_DELIM ('&')
1817 #define NRS_TBF_EXPRESSION_DELIM ('=')
1820 nrs_tbf_check_field(struct cfs_lstr *field, char *str)
1822 int len = strlen(str);
1824 return (field->ls_len == len &&
1825 strncmp(field->ls_str, str, len) == 0);
1829 nrs_tbf_opcode_list_parse(char *str, int len, unsigned long **bitmaptr);
1831 nrs_tbf_id_list_parse(char *str, int len, struct list_head *id_list,
1832 enum nrs_tbf_flag tif);
1835 nrs_tbf_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
1837 struct nrs_tbf_expression *expr;
1838 struct cfs_lstr field;
1841 OBD_ALLOC_PTR(expr);
1845 rc = cfs_gettok(src, NRS_TBF_EXPRESSION_DELIM, &field);
1846 if (rc == 0 || !src->ls_str || src->ls_len <= 2 ||
1847 src->ls_str[0] != '{' || src->ls_str[src->ls_len - 1] != '}')
1848 GOTO(out, rc = -EINVAL);
1850 /* Skip '{' and '}' */
1854 if (nrs_tbf_check_field(&field, "nid")) {
1855 if (cfs_parse_nidlist(src->ls_str,
1857 &expr->te_cond) <= 0)
1858 GOTO(out, rc = -EINVAL);
1859 expr->te_field = NRS_TBF_FIELD_NID;
1860 } else if (nrs_tbf_check_field(&field, "jobid")) {
1861 if (nrs_tbf_jobid_list_parse(src->ls_str,
1863 &expr->te_cond) < 0)
1864 GOTO(out, rc = -EINVAL);
1865 expr->te_field = NRS_TBF_FIELD_JOBID;
1866 } else if (nrs_tbf_check_field(&field, "opcode")) {
1867 if (nrs_tbf_opcode_list_parse(src->ls_str,
1869 &expr->te_opcodes) < 0)
1870 GOTO(out, rc = -EINVAL);
1871 expr->te_field = NRS_TBF_FIELD_OPCODE;
1872 } else if (nrs_tbf_check_field(&field, "uid")) {
1873 if (nrs_tbf_id_list_parse(src->ls_str,
1876 NRS_TBF_FLAG_UID) < 0)
1877 GOTO(out, rc = -EINVAL);
1878 expr->te_field = NRS_TBF_FIELD_UID;
1879 } else if (nrs_tbf_check_field(&field, "gid")) {
1880 if (nrs_tbf_id_list_parse(src->ls_str,
1883 NRS_TBF_FLAG_GID) < 0)
1884 GOTO(out, rc = -EINVAL);
1885 expr->te_field = NRS_TBF_FIELD_GID;
1887 GOTO(out, rc = -EINVAL);
1890 list_add_tail(&expr->te_linkage, cond_list);
1898 nrs_tbf_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
1900 struct nrs_tbf_conjunction *conjunction;
1901 struct cfs_lstr expr;
1904 OBD_ALLOC_PTR(conjunction);
1905 if (conjunction == NULL)
1908 INIT_LIST_HEAD(&conjunction->tc_expressions);
1909 list_add_tail(&conjunction->tc_linkage, cond_list);
1911 while (src->ls_str) {
1912 rc = cfs_gettok(src, NRS_TBF_CONJUNCTION_DELIM, &expr);
1917 rc = nrs_tbf_expression_parse(&expr,
1918 &conjunction->tc_expressions);
1926 nrs_tbf_conds_parse(char *str, int len, struct list_head *cond_list)
1928 struct cfs_lstr src;
1929 struct cfs_lstr res;
1934 INIT_LIST_HEAD(cond_list);
1935 while (src.ls_str) {
1936 rc = cfs_gettok(&src, NRS_TBF_DISJUNCTION_DELIM, &res);
1941 rc = nrs_tbf_conjunction_parse(&res, cond_list);
1949 nrs_tbf_generic_parse(struct nrs_tbf_cmd *cmd, const char *id)
1953 OBD_ALLOC(cmd->u.tc_start.ts_conds_str, strlen(id) + 1);
1954 if (cmd->u.tc_start.ts_conds_str == NULL)
1957 memcpy(cmd->u.tc_start.ts_conds_str, id, strlen(id));
1959 /* Parse hybird NID and JOBID conditions */
1960 rc = nrs_tbf_conds_parse(cmd->u.tc_start.ts_conds_str,
1961 strlen(cmd->u.tc_start.ts_conds_str),
1962 &cmd->u.tc_start.ts_conds);
1964 nrs_tbf_generic_cmd_fini(cmd);
1970 nrs_tbf_id_list_match(struct list_head *id_list, struct tbf_id id);
1973 nrs_tbf_expression_match(struct nrs_tbf_expression *expr,
1974 struct nrs_tbf_rule *rule,
1975 struct nrs_tbf_client *cli)
1977 switch (expr->te_field) {
1978 case NRS_TBF_FIELD_NID:
1979 return cfs_match_nid(cli->tc_nid, &expr->te_cond);
1980 case NRS_TBF_FIELD_JOBID:
1981 return nrs_tbf_jobid_list_match(&expr->te_cond, cli->tc_jobid);
1982 case NRS_TBF_FIELD_OPCODE:
1983 return test_bit(cli->tc_opcode, expr->te_opcodes);
1984 case NRS_TBF_FIELD_UID:
1985 case NRS_TBF_FIELD_GID:
1986 return nrs_tbf_id_list_match(&expr->te_cond, cli->tc_id);
1993 nrs_tbf_conjunction_match(struct nrs_tbf_conjunction *conjunction,
1994 struct nrs_tbf_rule *rule,
1995 struct nrs_tbf_client *cli)
1997 struct nrs_tbf_expression *expr;
2000 list_for_each_entry(expr, &conjunction->tc_expressions, te_linkage) {
2001 matched = nrs_tbf_expression_match(expr, rule, cli);
2010 nrs_tbf_cond_match(struct nrs_tbf_rule *rule, struct nrs_tbf_client *cli)
2012 struct nrs_tbf_conjunction *conjunction;
2015 list_for_each_entry(conjunction, &rule->tr_conds, tc_linkage) {
2016 matched = nrs_tbf_conjunction_match(conjunction, rule, cli);
2025 nrs_tbf_generic_rule_fini(struct nrs_tbf_rule *rule)
2027 if (!list_empty(&rule->tr_conds))
2028 nrs_tbf_conds_free(&rule->tr_conds);
2029 LASSERT(rule->tr_conds_str != NULL);
2030 OBD_FREE(rule->tr_conds_str, strlen(rule->tr_conds_str) + 1);
2034 nrs_tbf_rule_init(struct ptlrpc_nrs_policy *policy,
2035 struct nrs_tbf_rule *rule, struct nrs_tbf_cmd *start)
2039 LASSERT(start->u.tc_start.ts_conds_str);
2040 OBD_ALLOC(rule->tr_conds_str,
2041 strlen(start->u.tc_start.ts_conds_str) + 1);
2042 if (rule->tr_conds_str == NULL)
2045 memcpy(rule->tr_conds_str,
2046 start->u.tc_start.ts_conds_str,
2047 strlen(start->u.tc_start.ts_conds_str));
2049 INIT_LIST_HEAD(&rule->tr_conds);
2050 if (!list_empty(&start->u.tc_start.ts_conds)) {
2051 rc = nrs_tbf_conds_parse(rule->tr_conds_str,
2052 strlen(rule->tr_conds_str),
2056 nrs_tbf_generic_rule_fini(rule);
2062 nrs_tbf_generic_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2064 seq_printf(m, "%s %s %llu, ref %d\n", rule->tr_name,
2065 rule->tr_conds_str, rule->tr_rpc_rate,
2066 atomic_read(&rule->tr_ref) - 1);
2071 nrs_tbf_generic_rule_match(struct nrs_tbf_rule *rule,
2072 struct nrs_tbf_client *cli)
2074 return nrs_tbf_cond_match(rule, cli);
2077 static struct nrs_tbf_ops nrs_tbf_generic_ops = {
2078 .o_name = NRS_TBF_TYPE_GENERIC,
2079 .o_startup = nrs_tbf_startup,
2080 .o_cli_find = nrs_tbf_cli_find,
2081 .o_cli_findadd = nrs_tbf_cli_findadd,
2082 .o_cli_put = nrs_tbf_cli_put,
2083 .o_cli_init = nrs_tbf_generic_cli_init,
2084 .o_rule_init = nrs_tbf_rule_init,
2085 .o_rule_dump = nrs_tbf_generic_rule_dump,
2086 .o_rule_match = nrs_tbf_generic_rule_match,
2087 .o_rule_fini = nrs_tbf_generic_rule_fini,
2090 static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
2092 if (rule->tr_opcodes)
2093 bitmap_free(rule->tr_opcodes);
2095 LASSERT(rule->tr_opcodes_str != NULL);
2096 OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
2099 static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
2102 return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2105 static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
2107 const __u32 *opc = key;
2108 struct nrs_tbf_client *cli = hlist_entry(hnode,
2109 struct nrs_tbf_client,
2112 return *opc == cli->tc_opcode;
2115 static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
2117 struct nrs_tbf_client *cli = hlist_entry(hnode,
2118 struct nrs_tbf_client,
2121 return &cli->tc_opcode;
2124 static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
2125 struct hlist_node *hnode)
2127 struct nrs_tbf_client *cli = hlist_entry(hnode,
2128 struct nrs_tbf_client,
2131 atomic_inc(&cli->tc_ref);
2134 static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
2135 struct hlist_node *hnode)
2137 struct nrs_tbf_client *cli = hlist_entry(hnode,
2138 struct nrs_tbf_client,
2141 atomic_dec(&cli->tc_ref);
2144 static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
2145 struct hlist_node *hnode)
2147 struct nrs_tbf_client *cli = hlist_entry(hnode,
2148 struct nrs_tbf_client,
2151 LASSERTF(atomic_read(&cli->tc_ref) == 0,
2152 "Busy TBF object from client with opcode %s, with %d refs\n",
2153 ll_opcode2str(cli->tc_opcode),
2154 atomic_read(&cli->tc_ref));
2156 nrs_tbf_cli_fini(cli);
2158 static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
2159 .hs_hash = nrs_tbf_opcode_hop_hash,
2160 .hs_keycmp = nrs_tbf_opcode_hop_keycmp,
2161 .hs_key = nrs_tbf_opcode_hop_key,
2162 .hs_object = nrs_tbf_hop_object,
2163 .hs_get = nrs_tbf_opcode_hop_get,
2164 .hs_put = nrs_tbf_opcode_hop_put,
2165 .hs_put_locked = nrs_tbf_opcode_hop_put,
2166 .hs_exit = nrs_tbf_opcode_hop_exit,
2170 nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
2171 struct nrs_tbf_head *head)
2173 struct nrs_tbf_cmd start = { 0 };
2176 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
2179 NRS_TBF_NID_BKT_BITS, 0,
2182 &nrs_tbf_opcode_hash_ops,
2183 CFS_HASH_RW_BKTLOCK);
2184 if (head->th_cli_hash == NULL)
2187 start.u.tc_start.ts_opcodes_str = "*";
2189 start.u.tc_start.ts_rpc_rate = tbf_rate;
2190 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
2191 start.tc_name = NRS_TBF_DEFAULT_RULE;
2192 rc = nrs_tbf_rule_start(policy, head, &start);
2197 static struct nrs_tbf_client *
2198 nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
2199 struct ptlrpc_request *req)
2203 opc = lustre_msg_get_opc(req->rq_reqmsg);
2204 return cfs_hash_lookup(head->th_cli_hash, &opc);
2207 static struct nrs_tbf_client *
2208 nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
2209 struct nrs_tbf_client *cli)
2211 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
2216 nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
2217 struct ptlrpc_request *req)
2219 cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
2222 #define MAX_OPCODE_LEN 32
2224 nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, unsigned long *opcodes)
2227 char opcode_str[MAX_OPCODE_LEN];
2229 if (id->ls_len + 1 > MAX_OPCODE_LEN)
2232 memcpy(opcode_str, id->ls_str, id->ls_len);
2233 opcode_str[id->ls_len] = '\0';
2235 op = ll_str2opcode(opcode_str);
2239 set_bit(op, opcodes);
2244 nrs_tbf_opcode_list_parse(char *str, int len, unsigned long **bitmaptr)
2246 unsigned long *opcodes;
2247 struct cfs_lstr src;
2248 struct cfs_lstr res;
2252 opcodes = bitmap_zalloc(LUSTRE_MAX_OPCODES, GFP_KERNEL);
2258 while (src.ls_str) {
2259 rc = cfs_gettok(&src, ' ', &res);
2264 rc = nrs_tbf_opcode_set_bit(&res, opcodes);
2269 if (rc == 0 && bitmaptr)
2270 *bitmaptr = opcodes;
2272 bitmap_free(opcodes);
2277 static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
2279 if (cmd->u.tc_start.ts_opcodes_str)
2280 OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
2281 strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
2285 static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
2287 struct cfs_lstr src;
2291 src.ls_len = strlen(id);
2292 rc = nrs_tbf_check_id_value(&src, "opcode");
2296 OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
2297 if (cmd->u.tc_start.ts_opcodes_str == NULL)
2300 memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
2302 /* parse opcode list */
2303 rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
2304 strlen(cmd->u.tc_start.ts_opcodes_str),
2307 nrs_tbf_opcode_cmd_fini(cmd);
2313 nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
2314 struct nrs_tbf_client *cli)
2316 if (rule->tr_opcodes == NULL)
2319 return test_bit(cli->tc_opcode, rule->tr_opcodes);
2322 static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
2323 struct nrs_tbf_rule *rule,
2324 struct nrs_tbf_cmd *start)
2328 LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
2329 OBD_ALLOC(rule->tr_opcodes_str,
2330 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2331 if (rule->tr_opcodes_str == NULL)
2334 strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
2335 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2337 /* Default rule '*' */
2338 if (strcmp(start->u.tc_start.ts_opcodes_str, "*") == 0)
2341 rc = nrs_tbf_opcode_list_parse(rule->tr_opcodes_str,
2342 strlen(rule->tr_opcodes_str),
2345 OBD_FREE(rule->tr_opcodes_str,
2346 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2352 nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2354 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2355 rule->tr_opcodes_str, rule->tr_rpc_rate,
2356 atomic_read(&rule->tr_ref) - 1);
2361 struct nrs_tbf_ops nrs_tbf_opcode_ops = {
2362 .o_name = NRS_TBF_TYPE_OPCODE,
2363 .o_startup = nrs_tbf_opcode_startup,
2364 .o_cli_find = nrs_tbf_opcode_cli_find,
2365 .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
2366 .o_cli_put = nrs_tbf_nid_cli_put,
2367 .o_cli_init = nrs_tbf_opcode_cli_init,
2368 .o_rule_init = nrs_tbf_opcode_rule_init,
2369 .o_rule_dump = nrs_tbf_opcode_rule_dump,
2370 .o_rule_match = nrs_tbf_opcode_rule_match,
2371 .o_rule_fini = nrs_tbf_opcode_rule_fini,
2374 static unsigned nrs_tbf_id_hop_hash(struct cfs_hash *hs, const void *key,
2377 return cfs_hash_djb2_hash(key, sizeof(struct tbf_id), mask);
2380 static int nrs_tbf_id_hop_keycmp(const void *key, struct hlist_node *hnode)
2382 const struct tbf_id *opc = key;
2383 enum nrs_tbf_flag ntf;
2384 struct nrs_tbf_client *cli = hlist_entry(hnode, struct nrs_tbf_client,
2386 ntf = opc->ti_type & cli->tc_id.ti_type;
2387 if ((ntf & NRS_TBF_FLAG_UID) && opc->ti_uid != cli->tc_id.ti_uid)
2390 if ((ntf & NRS_TBF_FLAG_GID) && opc->ti_gid != cli->tc_id.ti_gid)
2396 static void *nrs_tbf_id_hop_key(struct hlist_node *hnode)
2398 struct nrs_tbf_client *cli = hlist_entry(hnode,
2399 struct nrs_tbf_client,
2404 static void nrs_tbf_id_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
2406 struct nrs_tbf_client *cli = hlist_entry(hnode,
2407 struct nrs_tbf_client,
2410 atomic_inc(&cli->tc_ref);
2413 static void nrs_tbf_id_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
2415 struct nrs_tbf_client *cli = hlist_entry(hnode,
2416 struct nrs_tbf_client,
2419 atomic_dec(&cli->tc_ref);
2423 nrs_tbf_id_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
2426 struct nrs_tbf_client *cli = hlist_entry(hnode,
2427 struct nrs_tbf_client,
2430 LASSERT(atomic_read(&cli->tc_ref) == 0);
2431 nrs_tbf_cli_fini(cli);
2434 static struct cfs_hash_ops nrs_tbf_id_hash_ops = {
2435 .hs_hash = nrs_tbf_id_hop_hash,
2436 .hs_keycmp = nrs_tbf_id_hop_keycmp,
2437 .hs_key = nrs_tbf_id_hop_key,
2438 .hs_object = nrs_tbf_hop_object,
2439 .hs_get = nrs_tbf_id_hop_get,
2440 .hs_put = nrs_tbf_id_hop_put,
2441 .hs_put_locked = nrs_tbf_id_hop_put,
2442 .hs_exit = nrs_tbf_id_hop_exit,
2446 nrs_tbf_id_startup(struct ptlrpc_nrs_policy *policy,
2447 struct nrs_tbf_head *head)
2449 struct nrs_tbf_cmd start;
2452 head->th_cli_hash = cfs_hash_create("nrs_tbf_id_hash",
2455 NRS_TBF_NID_BKT_BITS, 0,
2458 &nrs_tbf_id_hash_ops,
2459 CFS_HASH_RW_BKTLOCK);
2460 if (head->th_cli_hash == NULL)
2463 memset(&start, 0, sizeof(start));
2464 start.u.tc_start.ts_ids_str = "*";
2465 start.u.tc_start.ts_rpc_rate = tbf_rate;
2466 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
2467 start.tc_name = NRS_TBF_DEFAULT_RULE;
2468 INIT_LIST_HEAD(&start.u.tc_start.ts_ids);
2469 rc = nrs_tbf_rule_start(policy, head, &start);
2471 cfs_hash_putref(head->th_cli_hash);
2472 head->th_cli_hash = NULL;
2478 static struct nrs_tbf_client *
2479 nrs_tbf_id_cli_find(struct nrs_tbf_head *head,
2480 struct ptlrpc_request *req)
2484 LASSERT(head->th_type_flag == NRS_TBF_FLAG_UID ||
2485 head->th_type_flag == NRS_TBF_FLAG_GID);
2487 nrs_tbf_id_cli_set(req, &id, head->th_type_flag);
2488 return cfs_hash_lookup(head->th_cli_hash, &id);
2491 static struct nrs_tbf_client *
2492 nrs_tbf_id_cli_findadd(struct nrs_tbf_head *head,
2493 struct nrs_tbf_client *cli)
2495 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_id,
2500 nrs_tbf_uid_cli_init(struct nrs_tbf_client *cli,
2501 struct ptlrpc_request *req)
2503 nrs_tbf_id_cli_set(req, &cli->tc_id, NRS_TBF_FLAG_UID);
2507 nrs_tbf_gid_cli_init(struct nrs_tbf_client *cli,
2508 struct ptlrpc_request *req)
2510 nrs_tbf_id_cli_set(req, &cli->tc_id, NRS_TBF_FLAG_GID);
2514 nrs_tbf_id_list_match(struct list_head *id_list, struct tbf_id id)
2516 struct nrs_tbf_id *nti_id;
2517 enum nrs_tbf_flag flag;
2519 list_for_each_entry(nti_id, id_list, nti_linkage) {
2520 flag = id.ti_type & nti_id->nti_id.ti_type;
2524 if ((flag & NRS_TBF_FLAG_UID) &&
2525 (id.ti_uid != nti_id->nti_id.ti_uid))
2528 if ((flag & NRS_TBF_FLAG_GID) &&
2529 (id.ti_gid != nti_id->nti_id.ti_gid))
2538 nrs_tbf_id_rule_match(struct nrs_tbf_rule *rule,
2539 struct nrs_tbf_client *cli)
2541 return nrs_tbf_id_list_match(&rule->tr_ids, cli->tc_id);
2544 static void nrs_tbf_id_cmd_fini(struct nrs_tbf_cmd *cmd)
2546 nrs_tbf_id_list_free(&cmd->u.tc_start.ts_ids);
2548 if (cmd->u.tc_start.ts_ids_str)
2549 OBD_FREE(cmd->u.tc_start.ts_ids_str,
2550 strlen(cmd->u.tc_start.ts_ids_str) + 1);
2554 nrs_tbf_id_list_parse(char *str, int len, struct list_head *id_list,
2555 enum nrs_tbf_flag tif)
2557 struct cfs_lstr src;
2558 struct cfs_lstr res;
2560 struct tbf_id id = { 0 };
2563 if (tif != NRS_TBF_FLAG_UID && tif != NRS_TBF_FLAG_GID)
2568 INIT_LIST_HEAD(id_list);
2569 while (src.ls_str) {
2570 struct nrs_tbf_id *nti_id;
2572 if (cfs_gettok(&src, ' ', &res) == 0)
2573 GOTO(out, rc = -EINVAL);
2576 if (tif == NRS_TBF_FLAG_UID) {
2577 if (!cfs_str2num_check(res.ls_str, res.ls_len,
2578 &id.ti_uid, 0, (u32)~0U))
2579 GOTO(out, rc = -EINVAL);
2581 if (!cfs_str2num_check(res.ls_str, res.ls_len,
2582 &id.ti_gid, 0, (u32)~0U))
2583 GOTO(out, rc = -EINVAL);
2586 OBD_ALLOC_PTR(nti_id);
2588 GOTO(out, rc = -ENOMEM);
2590 nti_id->nti_id = id;
2591 list_add_tail(&nti_id->nti_linkage, id_list);
2595 nrs_tbf_id_list_free(id_list);
2599 static int nrs_tbf_ug_id_parse(struct nrs_tbf_cmd *cmd, char *id)
2601 struct cfs_lstr src;
2603 enum nrs_tbf_flag tif;
2605 tif = cmd->u.tc_start.ts_valid_type;
2608 src.ls_len = strlen(id);
2610 rc = nrs_tbf_check_id_value(&src,
2611 tif == NRS_TBF_FLAG_UID ? "uid" : "gid");
2615 OBD_ALLOC(cmd->u.tc_start.ts_ids_str, src.ls_len + 1);
2616 if (cmd->u.tc_start.ts_ids_str == NULL)
2619 strlcpy(cmd->u.tc_start.ts_ids_str, src.ls_str, src.ls_len + 1);
2621 rc = nrs_tbf_id_list_parse(cmd->u.tc_start.ts_ids_str,
2622 strlen(cmd->u.tc_start.ts_ids_str),
2623 &cmd->u.tc_start.ts_ids, tif);
2625 nrs_tbf_id_cmd_fini(cmd);
2631 nrs_tbf_id_rule_init(struct ptlrpc_nrs_policy *policy,
2632 struct nrs_tbf_rule *rule,
2633 struct nrs_tbf_cmd *start)
2635 struct nrs_tbf_head *head = rule->tr_head;
2637 enum nrs_tbf_flag tif = head->th_type_flag;
2638 int ids_len = strlen(start->u.tc_start.ts_ids_str) + 1;
2640 LASSERT(start->u.tc_start.ts_ids_str);
2641 INIT_LIST_HEAD(&rule->tr_ids);
2643 OBD_ALLOC(rule->tr_ids_str, ids_len);
2644 if (rule->tr_ids_str == NULL)
2647 strlcpy(rule->tr_ids_str, start->u.tc_start.ts_ids_str,
2650 if (!list_empty(&start->u.tc_start.ts_ids)) {
2651 rc = nrs_tbf_id_list_parse(rule->tr_ids_str,
2652 strlen(rule->tr_ids_str),
2653 &rule->tr_ids, tif);
2655 CERROR("%ss {%s} illegal\n",
2656 tif == NRS_TBF_FLAG_UID ? "uid" : "gid",
2660 OBD_FREE(rule->tr_ids_str, ids_len);
2661 rule->tr_ids_str = NULL;
2667 nrs_tbf_id_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2669 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2670 rule->tr_ids_str, rule->tr_rpc_rate,
2671 atomic_read(&rule->tr_ref) - 1);
2675 static void nrs_tbf_id_rule_fini(struct nrs_tbf_rule *rule)
2677 nrs_tbf_id_list_free(&rule->tr_ids);
2678 if (rule->tr_ids_str != NULL)
2679 OBD_FREE(rule->tr_ids_str, strlen(rule->tr_ids_str) + 1);
2682 struct nrs_tbf_ops nrs_tbf_uid_ops = {
2683 .o_name = NRS_TBF_TYPE_UID,
2684 .o_startup = nrs_tbf_id_startup,
2685 .o_cli_find = nrs_tbf_id_cli_find,
2686 .o_cli_findadd = nrs_tbf_id_cli_findadd,
2687 .o_cli_put = nrs_tbf_nid_cli_put,
2688 .o_cli_init = nrs_tbf_uid_cli_init,
2689 .o_rule_init = nrs_tbf_id_rule_init,
2690 .o_rule_dump = nrs_tbf_id_rule_dump,
2691 .o_rule_match = nrs_tbf_id_rule_match,
2692 .o_rule_fini = nrs_tbf_id_rule_fini,
2695 struct nrs_tbf_ops nrs_tbf_gid_ops = {
2696 .o_name = NRS_TBF_TYPE_GID,
2697 .o_startup = nrs_tbf_id_startup,
2698 .o_cli_find = nrs_tbf_id_cli_find,
2699 .o_cli_findadd = nrs_tbf_id_cli_findadd,
2700 .o_cli_put = nrs_tbf_nid_cli_put,
2701 .o_cli_init = nrs_tbf_gid_cli_init,
2702 .o_rule_init = nrs_tbf_id_rule_init,
2703 .o_rule_dump = nrs_tbf_id_rule_dump,
2704 .o_rule_match = nrs_tbf_id_rule_match,
2705 .o_rule_fini = nrs_tbf_id_rule_fini,
2708 static struct nrs_tbf_type nrs_tbf_types[] = {
2710 .ntt_name = NRS_TBF_TYPE_JOBID,
2711 .ntt_flag = NRS_TBF_FLAG_JOBID,
2712 .ntt_ops = &nrs_tbf_jobid_ops,
2715 .ntt_name = NRS_TBF_TYPE_NID,
2716 .ntt_flag = NRS_TBF_FLAG_NID,
2717 .ntt_ops = &nrs_tbf_nid_ops,
2720 .ntt_name = NRS_TBF_TYPE_OPCODE,
2721 .ntt_flag = NRS_TBF_FLAG_OPCODE,
2722 .ntt_ops = &nrs_tbf_opcode_ops,
2725 .ntt_name = NRS_TBF_TYPE_GENERIC,
2726 .ntt_flag = NRS_TBF_FLAG_GENERIC,
2727 .ntt_ops = &nrs_tbf_generic_ops,
2730 .ntt_name = NRS_TBF_TYPE_UID,
2731 .ntt_flag = NRS_TBF_FLAG_UID,
2732 .ntt_ops = &nrs_tbf_uid_ops,
2735 .ntt_name = NRS_TBF_TYPE_GID,
2736 .ntt_flag = NRS_TBF_FLAG_GID,
2737 .ntt_ops = &nrs_tbf_gid_ops,
2742 * Is called before the policy transitions into
2743 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
2744 * policy-specific private data structure.
2746 * \param[in] policy The policy to start
2748 * \retval -ENOMEM OOM error
2751 * \see nrs_policy_register()
2752 * \see nrs_policy_ctl()
2754 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
2756 struct nrs_tbf_head *head;
2757 struct nrs_tbf_ops *ops;
2765 name = NRS_TBF_TYPE_GENERIC;
2766 else if (strlen(arg) < NRS_TBF_TYPE_MAX_LEN)
2769 GOTO(out, rc = -EINVAL);
2771 for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
2772 if (strcmp(name, nrs_tbf_types[i].ntt_name) == 0) {
2773 ops = nrs_tbf_types[i].ntt_ops;
2774 type = nrs_tbf_types[i].ntt_flag;
2780 GOTO(out, rc = -ENOTSUPP);
2782 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
2784 GOTO(out, rc = -ENOMEM);
2786 memcpy(head->th_type, name, strlen(name));
2787 head->th_type[strlen(name)] = '\0';
2789 head->th_type_flag = type;
2791 head->th_binheap = binheap_create(&nrs_tbf_heap_ops,
2792 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
2793 nrs_pol2cptab(policy),
2794 nrs_pol2cptid(policy));
2795 if (head->th_binheap == NULL)
2796 GOTO(out_free_head, rc = -ENOMEM);
2798 atomic_set(&head->th_rule_sequence, 0);
2799 spin_lock_init(&head->th_rule_lock);
2800 INIT_LIST_HEAD(&head->th_list);
2801 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2802 head->th_timer.function = nrs_tbf_timer_cb;
2803 rc = head->th_ops->o_startup(policy, head);
2805 GOTO(out_free_heap, rc);
2807 policy->pol_private = head;
2810 binheap_destroy(head->th_binheap);
2818 * Is called before the policy transitions into
2819 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
2820 * private data structure.
2822 * \param[in] policy The policy to stop
2824 * \see nrs_policy_stop0()
2826 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
2828 struct nrs_tbf_head *head = policy->pol_private;
2829 struct ptlrpc_nrs *nrs = policy->pol_nrs;
2830 struct nrs_tbf_rule *rule, *n;
2832 LASSERT(head != NULL);
2833 LASSERT(head->th_cli_hash != NULL);
2834 hrtimer_cancel(&head->th_timer);
2835 /* Should cleanup hash first before free rules */
2836 cfs_hash_putref(head->th_cli_hash);
2837 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
2838 list_del_init(&rule->tr_linkage);
2839 nrs_tbf_rule_put(rule);
2841 LASSERT(list_empty(&head->th_list));
2842 LASSERT(head->th_binheap != NULL);
2843 LASSERT(binheap_is_empty(head->th_binheap));
2844 binheap_destroy(head->th_binheap);
2846 nrs->nrs_throttling = 0;
2847 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
2851 * Performs a policy-specific ctl function on TBF policy instances; similar
2854 * \param[in] policy the policy instance
2855 * \param[in] opc the opcode
2856 * \param[in,out] arg used for passing parameters and information
2858 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2859 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2861 * \retval 0 operation carried out successfully
2864 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
2865 enum ptlrpc_nrs_ctl opc,
2871 assert_spin_locked(&policy->pol_nrs->nrs_lock);
2873 switch ((enum nrs_ctl_tbf)opc) {
2878 * Read RPC rate size of a policy instance.
2880 case NRS_CTL_TBF_RD_RULE: {
2881 struct nrs_tbf_head *head = policy->pol_private;
2882 struct seq_file *m = arg;
2883 struct ptlrpc_service_part *svcpt;
2885 svcpt = policy->pol_nrs->nrs_svcpt;
2886 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
2888 rc = nrs_tbf_rule_dump_all(head, m);
2893 * Write RPC rate of a policy instance.
2895 case NRS_CTL_TBF_WR_RULE: {
2896 struct nrs_tbf_head *head = policy->pol_private;
2897 struct nrs_tbf_cmd *cmd;
2899 cmd = (struct nrs_tbf_cmd *)arg;
2900 rc = nrs_tbf_command(policy,
2906 * Read the TBF policy type of a policy instance.
2908 case NRS_CTL_TBF_RD_TYPE_FLAG: {
2909 struct nrs_tbf_head *head = policy->pol_private;
2911 *(__u32 *)arg = head->th_type_flag;
2920 * Is called for obtaining a TBF policy resource.
2922 * \param[in] policy The policy on which the request is being asked for
2923 * \param[in] nrq The request for which resources are being taken
2924 * \param[in] parent Parent resource, unused in this policy
2925 * \param[out] resp Resources references are placed in this array
2926 * \param[in] moving_req Signifies limited caller context; unused in this
2930 * \see nrs_resource_get_safe()
2932 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
2933 struct ptlrpc_nrs_request *nrq,
2934 const struct ptlrpc_nrs_resource *parent,
2935 struct ptlrpc_nrs_resource **resp,
2938 struct nrs_tbf_head *head;
2939 struct nrs_tbf_client *cli;
2940 struct nrs_tbf_client *tmp;
2941 struct ptlrpc_request *req;
2943 if (parent == NULL) {
2944 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
2948 head = container_of(parent, struct nrs_tbf_head, th_res);
2949 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
2950 cli = head->th_ops->o_cli_find(head, req);
2952 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2953 LASSERT(cli->tc_rule);
2954 if (cli->tc_rule_sequence !=
2955 atomic_read(&head->th_rule_sequence) ||
2956 cli->tc_rule->tr_flags & NTRS_STOPPING) {
2957 struct nrs_tbf_rule *rule;
2960 "TBF class@%p rate %llu sequence %d, "
2961 "rule flags %d, head sequence %d\n",
2962 cli, cli->tc_rpc_rate,
2963 cli->tc_rule_sequence,
2964 cli->tc_rule->tr_flags,
2965 atomic_read(&head->th_rule_sequence));
2966 rule = nrs_tbf_rule_match(head, cli);
2967 if (rule != cli->tc_rule) {
2968 nrs_tbf_cli_reset(head, rule, cli);
2970 if (cli->tc_rule_generation != rule->tr_generation)
2971 nrs_tbf_cli_reset_value(head, cli);
2972 nrs_tbf_rule_put(rule);
2974 } else if (cli->tc_rule_generation !=
2975 cli->tc_rule->tr_generation) {
2976 nrs_tbf_cli_reset_value(head, cli);
2978 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2982 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
2983 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
2987 nrs_tbf_cli_init(head, cli, req);
2988 tmp = head->th_ops->o_cli_findadd(head, cli);
2990 atomic_dec(&cli->tc_ref);
2991 nrs_tbf_cli_fini(cli);
2995 *resp = &cli->tc_res;
3001 * Called when releasing references to the resource hierachy obtained for a
3002 * request for scheduling using the TBF policy.
3004 * \param[in] policy the policy the resource belongs to
3005 * \param[in] res the resource to be released
3007 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
3008 const struct ptlrpc_nrs_resource *res)
3010 struct nrs_tbf_head *head;
3011 struct nrs_tbf_client *cli;
3014 * Do nothing for freeing parent, nrs_tbf_net resources
3016 if (res->res_parent == NULL)
3019 cli = container_of(res, struct nrs_tbf_client, tc_res);
3020 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
3022 head->th_ops->o_cli_put(head, cli);
3026 * Called when getting a request from the TBF policy for handling, or just
3027 * peeking; removes the request from the policy when it is to be handled.
3029 * \param[in] policy The policy
3030 * \param[in] peek When set, signifies that we just want to examine the
3031 * request, and not handle it, so the request is not removed
3033 * \param[in] force Force the policy to return a request
3035 * \retval The request to be handled; this is the next request in the TBF
3038 * \see ptlrpc_nrs_req_get_nolock()
3039 * \see nrs_request_get()
3042 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
3043 bool peek, bool force)
3045 struct nrs_tbf_head *head = policy->pol_private;
3046 struct ptlrpc_nrs_request *nrq = NULL;
3047 struct nrs_tbf_client *cli;
3048 struct binheap_node *node;
3050 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3052 if (likely(!peek && !force) && policy->pol_nrs->nrs_throttling)
3055 node = binheap_root(head->th_binheap);
3056 if (unlikely(node == NULL))
3059 cli = container_of(node, struct nrs_tbf_client, tc_node);
3060 LASSERT(cli->tc_in_heap);
3061 if (unlikely(peek)) {
3062 nrq = list_entry(cli->tc_list.next,
3063 struct ptlrpc_nrs_request,
3066 struct nrs_tbf_rule *rule = cli->tc_rule;
3067 __u64 now = ktime_to_ns(ktime_get());
3071 __u64 old_resid = 0;
3073 deadline = cli->tc_check_time +
3075 LASSERT(now >= cli->tc_check_time);
3076 passed = now - cli->tc_check_time;
3077 ntoken = passed * cli->tc_rpc_rate;
3078 do_div(ntoken, NSEC_PER_SEC);
3080 ntoken += cli->tc_ntoken;
3081 if (rule->tr_flags & NTRS_REALTIME) {
3082 LASSERT(cli->tc_nsecs_resid < cli->tc_nsecs);
3083 old_resid = cli->tc_nsecs_resid;
3084 cli->tc_nsecs_resid += passed % cli->tc_nsecs;
3085 if (cli->tc_nsecs_resid > cli->tc_nsecs) {
3087 cli->tc_nsecs_resid -= cli->tc_nsecs;
3089 } else if (ntoken > cli->tc_depth)
3090 ntoken = cli->tc_depth;
3092 /* give an extra token with force mode */
3093 if (unlikely(force) && ntoken == 0)
3097 struct ptlrpc_request *req;
3098 nrq = list_entry(cli->tc_list.next,
3099 struct ptlrpc_nrs_request,
3101 req = container_of(nrq,
3102 struct ptlrpc_request,
3105 cli->tc_ntoken = ntoken;
3106 cli->tc_check_time = now;
3107 list_del_init(&nrq->nr_u.tbf.tr_list);
3108 if (list_empty(&cli->tc_list)) {
3109 binheap_remove(head->th_binheap,
3111 cli->tc_in_heap = false;
3113 if (!(rule->tr_flags & NTRS_REALTIME))
3114 cli->tc_deadline = now + cli->tc_nsecs;
3115 binheap_relocate(head->th_binheap,
3119 "TBF dequeues: class@%p rate %llu gen %llu token %llu, rule@%p rate %llu gen %llu\n",
3120 cli, cli->tc_rpc_rate,
3121 cli->tc_rule_generation, cli->tc_ntoken,
3122 cli->tc_rule, cli->tc_rule->tr_rpc_rate,
3123 cli->tc_rule->tr_generation);
3127 if (rule->tr_flags & NTRS_REALTIME) {
3128 cli->tc_deadline = deadline;
3129 cli->tc_nsecs_resid = old_resid;
3130 binheap_relocate(head->th_binheap,
3132 if (node != binheap_root(head->th_binheap))
3133 return nrs_tbf_req_get(policy,
3136 policy->pol_nrs->nrs_throttling = 1;
3137 head->th_deadline = deadline;
3138 time = ktime_set(0, 0);
3139 time = ktime_add_ns(time, deadline);
3140 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
3148 * Adds request \a nrq to \a policy's list of queued requests
3150 * \param[in] policy The policy
3151 * \param[in] nrq The request to add
3153 * \retval 0 success; nrs_request_enqueue() assumes this function will always
3156 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
3157 struct ptlrpc_nrs_request *nrq)
3159 struct nrs_tbf_head *head;
3160 struct nrs_tbf_client *cli;
3163 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3165 cli = container_of(nrs_request_resource(nrq),
3166 struct nrs_tbf_client, tc_res);
3167 head = container_of(nrs_request_resource(nrq)->res_parent,
3168 struct nrs_tbf_head, th_res);
3169 if (list_empty(&cli->tc_list)) {
3170 LASSERT(!cli->tc_in_heap);
3171 cli->tc_deadline = cli->tc_check_time + cli->tc_nsecs;
3172 rc = binheap_insert(head->th_binheap, &cli->tc_node);
3174 cli->tc_in_heap = true;
3175 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
3176 list_add_tail(&nrq->nr_u.tbf.tr_list,
3178 if (policy->pol_nrs->nrs_throttling) {
3179 __u64 deadline = cli->tc_deadline;
3180 if ((head->th_deadline > deadline) &&
3181 (hrtimer_try_to_cancel(&head->th_timer)
3184 head->th_deadline = deadline;
3185 time = ktime_set(0, 0);
3186 time = ktime_add_ns(time, deadline);
3187 hrtimer_start(&head->th_timer, time,
3193 LASSERT(cli->tc_in_heap);
3194 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
3195 list_add_tail(&nrq->nr_u.tbf.tr_list,
3201 "TBF enqueues: class@%p rate %llu gen %llu token %llu, rule@%p rate %llu gen %llu\n",
3202 cli, cli->tc_rpc_rate,
3203 cli->tc_rule_generation, cli->tc_ntoken,
3204 cli->tc_rule, cli->tc_rule->tr_rpc_rate,
3205 cli->tc_rule->tr_generation);
3211 * Removes request \a nrq from \a policy's list of queued requests.
3213 * \param[in] policy The policy
3214 * \param[in] nrq The request to remove
3216 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
3217 struct ptlrpc_nrs_request *nrq)
3219 struct nrs_tbf_head *head;
3220 struct nrs_tbf_client *cli;
3222 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3224 cli = container_of(nrs_request_resource(nrq),
3225 struct nrs_tbf_client, tc_res);
3226 head = container_of(nrs_request_resource(nrq)->res_parent,
3227 struct nrs_tbf_head, th_res);
3229 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
3230 list_del_init(&nrq->nr_u.tbf.tr_list);
3231 if (list_empty(&cli->tc_list)) {
3232 binheap_remove(head->th_binheap,
3234 cli->tc_in_heap = false;
3236 binheap_relocate(head->th_binheap,
3242 * Prints a debug statement right before the request \a nrq stops being
3245 * \param[in] policy The policy handling the request
3246 * \param[in] nrq The request being handled
3248 * \see ptlrpc_server_finish_request()
3249 * \see ptlrpc_nrs_req_stop_nolock()
3251 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
3252 struct ptlrpc_nrs_request *nrq)
3254 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
3257 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
3259 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
3260 policy->pol_desc->pd_name, libcfs_idstr(&req->rq_peer),
3261 nrq->nr_u.tbf.tr_sequence);
3269 * The maximum RPC rate.
3271 #define LPROCFS_NRS_RATE_MAX 1000000ULL /* 1rpc/us */
3274 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
3276 struct ptlrpc_service *svc = m->private;
3279 seq_printf(m, "regular_requests:\n");
3281 * Perform two separate calls to this as only one of the NRS heads'
3282 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
3283 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
3285 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
3287 NRS_CTL_TBF_RD_RULE,
3291 * -ENOSPC means buf in the parameter m is overflow, return 0
3292 * here to let upper layer function seq_read alloc a larger
3293 * memory area and do this process again.
3295 } else if (rc == -ENOSPC) {
3299 * Ignore -ENODEV as the regular NRS head's policy may be in the
3300 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
3302 } else if (rc != -ENODEV) {
3306 if (!nrs_svc_has_hp(svc))
3309 seq_printf(m, "high_priority_requests:\n");
3310 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
3312 NRS_CTL_TBF_RD_RULE,
3316 * -ENOSPC means buf in the parameter m is overflow, return 0
3317 * here to let upper layer function seq_read alloc a larger
3318 * memory area and do this process again.
3320 } else if (rc == -ENOSPC) {
3329 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
3334 switch (cmd->u.tc_start.ts_valid_type) {
3335 case NRS_TBF_FLAG_JOBID:
3336 rc = nrs_tbf_jobid_parse(cmd, token);
3338 case NRS_TBF_FLAG_NID:
3339 rc = nrs_tbf_nid_parse(cmd, token);
3341 case NRS_TBF_FLAG_OPCODE:
3342 rc = nrs_tbf_opcode_parse(cmd, token);
3344 case NRS_TBF_FLAG_GENERIC:
3345 rc = nrs_tbf_generic_parse(cmd, token);
3347 case NRS_TBF_FLAG_UID:
3348 case NRS_TBF_FLAG_GID:
3349 rc = nrs_tbf_ug_id_parse(cmd, token);
3358 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
3360 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
3361 switch (cmd->u.tc_start.ts_valid_type) {
3362 case NRS_TBF_FLAG_JOBID:
3363 nrs_tbf_jobid_cmd_fini(cmd);
3365 case NRS_TBF_FLAG_NID:
3366 nrs_tbf_nid_cmd_fini(cmd);
3368 case NRS_TBF_FLAG_OPCODE:
3369 nrs_tbf_opcode_cmd_fini(cmd);
3371 case NRS_TBF_FLAG_GENERIC:
3372 nrs_tbf_generic_cmd_fini(cmd);
3374 case NRS_TBF_FLAG_UID:
3375 case NRS_TBF_FLAG_GID:
3376 nrs_tbf_id_cmd_fini(cmd);
3379 CWARN("unknown NRS_TBF_FLAGS:0x%x\n",
3380 cmd->u.tc_start.ts_valid_type);
3385 static int check_rule_name(const char *name)
3389 if (name[0] == '\0')
3392 for (i = 0; name[i] != '\0' && i < MAX_TBF_NAME; i++) {
3393 if (!isalnum(name[i]) && name[i] != '_')
3397 if (i == MAX_TBF_NAME)
3398 return -ENAMETOOLONG;
3404 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
3412 key = strsep(&val, "=");
3413 if (val == NULL || strlen(val) == 0)
3416 /* Key of the value pair */
3417 if (strcmp(key, "rate") == 0) {
3418 rc = kstrtoull(val, 10, &rate);
3422 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
3425 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
3426 cmd->u.tc_start.ts_rpc_rate = rate;
3427 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
3428 cmd->u.tc_change.tc_rpc_rate = rate;
3431 } else if (strcmp(key, "rank") == 0) {
3432 rc = check_rule_name(val);
3436 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
3437 cmd->u.tc_start.ts_next_name = val;
3438 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
3439 cmd->u.tc_change.tc_next_name = val;
3442 } else if (strcmp(key, "realtime") == 0) {
3443 unsigned long realtime;
3445 rc = kstrtoul(val, 10, &realtime);
3450 cmd->u.tc_start.ts_rule_flags |= NTRS_REALTIME;
3458 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
3465 while (val != NULL && strlen(val) != 0) {
3466 token = strsep(&val, " ");
3467 rc = nrs_tbf_parse_value_pair(cmd, token);
3472 switch (cmd->tc_cmd) {
3473 case NRS_CTL_TBF_START_RULE:
3474 if (cmd->u.tc_start.ts_rpc_rate == 0)
3475 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
3477 case NRS_CTL_TBF_CHANGE_RULE:
3478 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
3479 cmd->u.tc_change.tc_next_name == NULL)
3482 case NRS_CTL_TBF_STOP_RULE:
3490 static struct nrs_tbf_cmd *
3491 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
3493 struct nrs_tbf_cmd *cmd;
3500 GOTO(out, rc = -ENOMEM);
3501 memset(cmd, 0, sizeof(*cmd));
3504 token = strsep(&val, " ");
3505 if (val == NULL || strlen(val) == 0)
3506 GOTO(out_free_cmd, rc = -EINVAL);
3508 /* Type of the command */
3509 if (strcmp(token, "start") == 0) {
3510 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
3511 cmd->u.tc_start.ts_valid_type = type_flag;
3512 } else if (strcmp(token, "stop") == 0)
3513 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
3514 else if (strcmp(token, "change") == 0)
3515 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
3517 GOTO(out_free_cmd, rc = -EINVAL);
3519 /* Name of the rule */
3520 token = strsep(&val, " ");
3521 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE))
3522 GOTO(out_free_cmd, rc = -EINVAL);
3524 rc = check_rule_name(token);
3526 GOTO(out_free_cmd, rc);
3528 cmd->tc_name = token;
3530 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
3534 val = strrchr(token, '}');
3536 GOTO(out_free_cmd, rc = -EINVAL);
3542 } else if (*val == ' ') {
3546 GOTO(out_free_cmd, rc = -EINVAL);
3548 rc = nrs_tbf_id_parse(cmd, token);
3550 GOTO(out_free_cmd, rc);
3553 rc = nrs_tbf_parse_value_pairs(cmd, val);
3555 GOTO(out_cmd_fini, rc = -EINVAL);
3558 nrs_tbf_cmd_fini(cmd);
3568 * Get the TBF policy type (nid, jobid, etc) preset by
3569 * proc entry 'nrs_policies' for command buffer parsing.
3571 * \param[in] svc the PTLRPC service
3572 * \param[in] queue the NRS queue type
3574 * \retval the preset TBF policy type flag
3577 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
3582 rc = ptlrpc_nrs_policy_control(svc, queue,
3584 NRS_CTL_TBF_RD_TYPE_FLAG,
3587 type = NRS_TBF_FLAG_INVALID;
3592 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
3594 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
3595 const char __user *buffer,
3596 size_t count, loff_t *off)
3598 struct seq_file *m = file->private_data;
3599 struct ptlrpc_service *svc = m->private;
3603 struct nrs_tbf_cmd *cmd;
3604 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
3605 unsigned long length;
3608 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
3609 if (kernbuf == NULL)
3610 GOTO(out, rc = -ENOMEM);
3612 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
3613 GOTO(out_free_kernbuff, rc = -EINVAL);
3615 if (copy_from_user(kernbuf, buffer, count))
3616 GOTO(out_free_kernbuff, rc = -EFAULT);
3619 token = strsep(&val, " ");
3621 GOTO(out_free_kernbuff, rc = -EINVAL);
3623 if (strcmp(token, "reg") == 0) {
3624 queue = PTLRPC_NRS_QUEUE_REG;
3625 } else if (strcmp(token, "hp") == 0) {
3626 queue = PTLRPC_NRS_QUEUE_HP;
3628 kernbuf[strlen(token)] = ' ';
3631 length = strlen(val);
3634 GOTO(out_free_kernbuff, rc = -EINVAL);
3636 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
3637 GOTO(out_free_kernbuff, rc = -ENODEV);
3638 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
3639 queue = PTLRPC_NRS_QUEUE_REG;
3641 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
3643 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
3646 * Serialize NRS core lprocfs operations with policy registration/
3649 mutex_lock(&nrs_core.nrs_mutex);
3650 rc = ptlrpc_nrs_policy_control(svc, queue,
3652 NRS_CTL_TBF_WR_RULE,
3654 mutex_unlock(&nrs_core.nrs_mutex);
3656 nrs_tbf_cmd_fini(cmd);
3659 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
3661 return rc ? rc : count;
3664 LDEBUGFS_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
3667 * Initializes a TBF policy's lprocfs interface for service \a svc
3669 * \param[in] svc the service
3672 * \retval != 0 error
3674 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
3676 struct ldebugfs_vars nrs_tbf_lprocfs_vars[] = {
3677 { .name = "nrs_tbf_rule",
3678 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
3683 if (!svc->srv_debugfs_entry)
3686 ldebugfs_add_vars(svc->srv_debugfs_entry, nrs_tbf_lprocfs_vars, NULL);
3692 * TBF policy operations
3694 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
3695 .op_policy_start = nrs_tbf_start,
3696 .op_policy_stop = nrs_tbf_stop,
3697 .op_policy_ctl = nrs_tbf_ctl,
3698 .op_res_get = nrs_tbf_res_get,
3699 .op_res_put = nrs_tbf_res_put,
3700 .op_req_get = nrs_tbf_req_get,
3701 .op_req_enqueue = nrs_tbf_req_add,
3702 .op_req_dequeue = nrs_tbf_req_del,
3703 .op_req_stop = nrs_tbf_req_stop,
3704 .op_lprocfs_init = nrs_tbf_lprocfs_init,
3708 * TBF policy configuration
3710 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
3711 .nc_name = NRS_POL_NAME_TBF,
3712 .nc_ops = &nrs_tbf_ops,
3713 .nc_compat = nrs_policy_compat_all,