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 "ptlrpc_internal.h"
50 * Token Bucket Filter over client NIDs
55 #define NRS_POL_NAME_TBF "tbf"
57 static int tbf_jobid_cache_size = 8192;
58 module_param(tbf_jobid_cache_size, int, 0644);
59 MODULE_PARM_DESC(tbf_jobid_cache_size, "The size of jobid cache");
61 static int tbf_rate = 10000;
62 module_param(tbf_rate, int, 0644);
63 MODULE_PARM_DESC(tbf_rate, "Default rate limit in RPCs/s");
65 static int tbf_depth = 3;
66 module_param(tbf_depth, int, 0644);
67 MODULE_PARM_DESC(tbf_depth, "How many tokens that a client can save up");
69 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
71 struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
73 struct ptlrpc_nrs *nrs = head->th_res.res_policy->pol_nrs;
74 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
76 nrs->nrs_throttling = 0;
77 wake_up(&svcpt->scp_waitq);
79 return HRTIMER_NORESTART;
82 #define NRS_TBF_DEFAULT_RULE "default"
84 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
86 LASSERT(atomic_read(&rule->tr_ref) == 0);
87 LASSERT(list_empty(&rule->tr_cli_list));
88 LASSERT(list_empty(&rule->tr_linkage));
90 rule->tr_head->th_ops->o_rule_fini(rule);
95 * Decreases the rule's usage reference count, and stops the rule in case it
96 * was already stopping and have no more outstanding usage references (which
97 * indicates it has no more queued or started requests, and can be safely
100 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
102 if (atomic_dec_and_test(&rule->tr_ref))
103 nrs_tbf_rule_fini(rule);
107 * Increases the rule's usage reference count.
109 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
111 atomic_inc(&rule->tr_ref);
115 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
117 LASSERT(!list_empty(&cli->tc_linkage));
118 LASSERT(cli->tc_rule);
119 spin_lock(&cli->tc_rule->tr_rule_lock);
120 list_del_init(&cli->tc_linkage);
121 spin_unlock(&cli->tc_rule->tr_rule_lock);
122 nrs_tbf_rule_put(cli->tc_rule);
127 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
128 struct nrs_tbf_client *cli)
131 struct nrs_tbf_rule *rule = cli->tc_rule;
133 cli->tc_rpc_rate = rule->tr_rpc_rate;
134 cli->tc_nsecs = rule->tr_nsecs;
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 cfs_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 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
302 rule->tr_rpc_rate = start->u.tc_start.ts_rpc_rate;
303 rule->tr_nsecs = NSEC_PER_SEC;
304 do_div(rule->tr_nsecs, 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;
355 * Change the rank of a rule in the rule list
357 * The matched rule will be moved to the position right before another
360 * \param[in] policy the policy instance
361 * \param[in] head the TBF policy instance
362 * \param[in] name the rule name to be moved
363 * \param[in] next_name the rule name before which the matched rule will be
368 nrs_tbf_rule_change_rank(struct ptlrpc_nrs_policy *policy,
369 struct nrs_tbf_head *head,
373 struct nrs_tbf_rule *rule = NULL;
374 struct nrs_tbf_rule *next_rule = NULL;
377 LASSERT(head != NULL);
379 spin_lock(&head->th_rule_lock);
380 rule = nrs_tbf_rule_find_nolock(head, name);
382 GOTO(out, rc = -ENOENT);
384 if (strcmp(name, next_name) == 0)
387 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
389 GOTO(out_put, rc = -ENOENT);
391 list_move(&rule->tr_linkage, next_rule->tr_linkage.prev);
392 nrs_tbf_rule_put(next_rule);
394 nrs_tbf_rule_put(rule);
396 spin_unlock(&head->th_rule_lock);
401 nrs_tbf_rule_change_rate(struct ptlrpc_nrs_policy *policy,
402 struct nrs_tbf_head *head,
406 struct nrs_tbf_rule *rule;
408 assert_spin_locked(&policy->pol_nrs->nrs_lock);
410 rule = nrs_tbf_rule_find(head, name);
414 rule->tr_rpc_rate = rate;
415 rule->tr_nsecs = NSEC_PER_SEC;
416 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
417 rule->tr_generation++;
418 nrs_tbf_rule_put(rule);
424 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
425 struct nrs_tbf_head *head,
426 struct nrs_tbf_cmd *change)
428 __u64 rate = change->u.tc_change.tc_rpc_rate;
429 char *next_name = change->u.tc_change.tc_next_name;
433 rc = nrs_tbf_rule_change_rate(policy, head, change->tc_name,
440 rc = nrs_tbf_rule_change_rank(policy, head, change->tc_name,
450 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
451 struct nrs_tbf_head *head,
452 struct nrs_tbf_cmd *stop)
454 struct nrs_tbf_rule *rule;
456 assert_spin_locked(&policy->pol_nrs->nrs_lock);
458 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
461 rule = nrs_tbf_rule_find(head, stop->tc_name);
465 list_del_init(&rule->tr_linkage);
466 rule->tr_flags |= NTRS_STOPPING;
467 nrs_tbf_rule_put(rule);
468 nrs_tbf_rule_put(rule);
474 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
475 struct nrs_tbf_head *head,
476 struct nrs_tbf_cmd *cmd)
480 assert_spin_locked(&policy->pol_nrs->nrs_lock);
482 switch (cmd->tc_cmd) {
483 case NRS_CTL_TBF_START_RULE:
484 if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
487 spin_unlock(&policy->pol_nrs->nrs_lock);
488 rc = nrs_tbf_rule_start(policy, head, cmd);
489 spin_lock(&policy->pol_nrs->nrs_lock);
491 case NRS_CTL_TBF_CHANGE_RULE:
492 rc = nrs_tbf_rule_change(policy, head, cmd);
494 case NRS_CTL_TBF_STOP_RULE:
495 rc = nrs_tbf_rule_stop(policy, head, cmd);
496 /* Take it as a success, if not exists at all */
497 return rc == -ENOENT ? 0 : rc;
504 * Binary heap predicate.
506 * \param[in] e1 the first binheap node to compare
507 * \param[in] e2 the second binheap node to compare
513 tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
515 struct nrs_tbf_client *cli1;
516 struct nrs_tbf_client *cli2;
518 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
519 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
521 if (cli1->tc_check_time + cli1->tc_nsecs <
522 cli2->tc_check_time + cli2->tc_nsecs)
524 else if (cli1->tc_check_time + cli1->tc_nsecs >
525 cli2->tc_check_time + cli2->tc_nsecs)
528 if (cli1->tc_check_time < cli2->tc_check_time)
530 else if (cli1->tc_check_time > cli2->tc_check_time)
533 /* Maybe need more comparasion, e.g. request number in the rules */
538 * TBF binary heap operations
540 static struct cfs_binheap_ops nrs_tbf_heap_ops = {
543 .hop_compare = tbf_cli_compare,
546 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
549 return cfs_hash_djb2_hash(key, strlen(key), mask);
552 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
554 struct nrs_tbf_client *cli = hlist_entry(hnode,
555 struct nrs_tbf_client,
558 return (strcmp(cli->tc_jobid, key) == 0);
561 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
563 struct nrs_tbf_client *cli = hlist_entry(hnode,
564 struct nrs_tbf_client,
567 return cli->tc_jobid;
570 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
572 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
575 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
577 struct nrs_tbf_client *cli = hlist_entry(hnode,
578 struct nrs_tbf_client,
581 atomic_inc(&cli->tc_ref);
584 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
586 struct nrs_tbf_client *cli = hlist_entry(hnode,
587 struct nrs_tbf_client,
590 atomic_dec(&cli->tc_ref);
594 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
597 struct nrs_tbf_client *cli = hlist_entry(hnode,
598 struct nrs_tbf_client,
601 LASSERT(atomic_read(&cli->tc_ref) == 0);
602 nrs_tbf_cli_fini(cli);
605 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
606 .hs_hash = nrs_tbf_jobid_hop_hash,
607 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
608 .hs_key = nrs_tbf_jobid_hop_key,
609 .hs_object = nrs_tbf_jobid_hop_object,
610 .hs_get = nrs_tbf_jobid_hop_get,
611 .hs_put = nrs_tbf_jobid_hop_put,
612 .hs_put_locked = nrs_tbf_jobid_hop_put,
613 .hs_exit = nrs_tbf_jobid_hop_exit,
616 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
617 CFS_HASH_NO_ITEMREF | \
620 static struct nrs_tbf_client *
621 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
622 struct cfs_hash_bd *bd,
625 struct hlist_node *hnode;
626 struct nrs_tbf_client *cli;
628 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)jobid);
632 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
633 if (!list_empty(&cli->tc_lru))
634 list_del_init(&cli->tc_lru);
638 #define NRS_TBF_JOBID_NULL ""
640 static struct nrs_tbf_client *
641 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
642 struct ptlrpc_request *req)
645 struct nrs_tbf_client *cli;
646 struct cfs_hash *hs = head->th_cli_hash;
647 struct cfs_hash_bd bd;
649 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
651 jobid = NRS_TBF_JOBID_NULL;
652 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
653 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
654 cfs_hash_bd_unlock(hs, &bd, 1);
659 static struct nrs_tbf_client *
660 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
661 struct nrs_tbf_client *cli)
664 struct nrs_tbf_client *ret;
665 struct cfs_hash *hs = head->th_cli_hash;
666 struct cfs_hash_bd bd;
668 jobid = cli->tc_jobid;
669 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
670 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
672 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
675 cfs_hash_bd_unlock(hs, &bd, 1);
681 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
682 struct nrs_tbf_client *cli)
684 struct cfs_hash_bd bd;
685 struct cfs_hash *hs = head->th_cli_hash;
686 struct nrs_tbf_bucket *bkt;
688 struct list_head zombies;
690 INIT_LIST_HEAD(&zombies);
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_of0(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);
807 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
812 nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
814 struct nrs_tbf_jobid *jobid;
818 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
822 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
823 if (jobid->tj_id == NULL) {
824 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
828 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
829 rc = cfs_gettok(id, '*', &res);
831 jobid->tj_match_flag = NRS_TBF_MATCH_FULL;
833 jobid->tj_match_flag = NRS_TBF_MATCH_WILDCARD;
835 list_add_tail(&jobid->tj_linkage, jobid_list);
840 cfs_match_wildcard(const char *pattern, const char *content)
842 if (*pattern == '\0' && *content == '\0')
845 if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
848 while (*pattern == *content) {
851 if (*pattern == '\0' && *content == '\0')
854 if (*pattern == '*' && *(pattern + 1) != '\0' &&
860 return (cfs_match_wildcard(pattern + 1, content) ||
861 cfs_match_wildcard(pattern, content + 1));
867 nrs_tbf_jobid_match(const struct nrs_tbf_jobid *jobid, const char *id)
869 if (jobid->tj_match_flag == NRS_TBF_MATCH_FULL)
870 return strcmp(jobid->tj_id, id) == 0;
872 if (jobid->tj_match_flag == NRS_TBF_MATCH_WILDCARD)
873 return cfs_match_wildcard(jobid->tj_id, id);
879 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
881 struct nrs_tbf_jobid *jobid;
883 list_for_each_entry(jobid, jobid_list, tj_linkage) {
884 if (nrs_tbf_jobid_match(jobid, id))
891 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
900 INIT_LIST_HEAD(jobid_list);
902 rc = cfs_gettok(&src, ' ', &res);
907 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
912 nrs_tbf_jobid_list_free(jobid_list);
916 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
918 if (!list_empty(&cmd->u.tc_start.ts_jobids))
919 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
920 if (cmd->u.tc_start.ts_jobids_str)
921 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
922 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
925 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
928 int keylen = strlen(key);
931 rc = cfs_gettok(src, '=', &res);
932 if (rc == 0 || res.ls_len != keylen ||
933 strncmp(res.ls_str, key, keylen) != 0 ||
934 src->ls_len <= 2 || src->ls_str[0] != '{' ||
935 src->ls_str[src->ls_len - 1] != '}')
938 /* Skip '{' and '}' */
944 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
950 src.ls_len = strlen(id);
951 rc = nrs_tbf_check_id_value(&src, "jobid");
955 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
956 if (cmd->u.tc_start.ts_jobids_str == NULL)
959 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
961 /* parse jobid list */
962 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
963 strlen(cmd->u.tc_start.ts_jobids_str),
964 &cmd->u.tc_start.ts_jobids);
966 nrs_tbf_jobid_cmd_fini(cmd);
971 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
972 struct nrs_tbf_rule *rule,
973 struct nrs_tbf_cmd *start)
977 LASSERT(start->u.tc_start.ts_jobids_str);
978 OBD_ALLOC(rule->tr_jobids_str,
979 strlen(start->u.tc_start.ts_jobids_str) + 1);
980 if (rule->tr_jobids_str == NULL)
983 memcpy(rule->tr_jobids_str,
984 start->u.tc_start.ts_jobids_str,
985 strlen(start->u.tc_start.ts_jobids_str));
987 INIT_LIST_HEAD(&rule->tr_jobids);
988 if (!list_empty(&start->u.tc_start.ts_jobids)) {
989 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
990 strlen(rule->tr_jobids_str),
993 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
996 OBD_FREE(rule->tr_jobids_str,
997 strlen(start->u.tc_start.ts_jobids_str) + 1);
1002 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1004 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1005 rule->tr_jobids_str, rule->tr_rpc_rate,
1006 atomic_read(&rule->tr_ref) - 1);
1011 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
1012 struct nrs_tbf_client *cli)
1014 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
1017 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
1019 if (!list_empty(&rule->tr_jobids))
1020 nrs_tbf_jobid_list_free(&rule->tr_jobids);
1021 LASSERT(rule->tr_jobids_str != NULL);
1022 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
1025 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
1026 .o_name = NRS_TBF_TYPE_JOBID,
1027 .o_startup = nrs_tbf_jobid_startup,
1028 .o_cli_find = nrs_tbf_jobid_cli_find,
1029 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
1030 .o_cli_put = nrs_tbf_jobid_cli_put,
1031 .o_cli_init = nrs_tbf_jobid_cli_init,
1032 .o_rule_init = nrs_tbf_jobid_rule_init,
1033 .o_rule_dump = nrs_tbf_jobid_rule_dump,
1034 .o_rule_match = nrs_tbf_jobid_rule_match,
1035 .o_rule_fini = nrs_tbf_jobid_rule_fini,
1039 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
1041 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
1042 * nrs_tbf_client objects.
1044 #define NRS_TBF_NID_BKT_BITS 8
1045 #define NRS_TBF_NID_BITS 16
1047 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1050 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1053 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1055 lnet_nid_t *nid = (lnet_nid_t *)key;
1056 struct nrs_tbf_client *cli = hlist_entry(hnode,
1057 struct nrs_tbf_client,
1060 return *nid == cli->tc_nid;
1063 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1065 struct nrs_tbf_client *cli = hlist_entry(hnode,
1066 struct nrs_tbf_client,
1069 return &cli->tc_nid;
1072 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
1074 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1077 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1079 struct nrs_tbf_client *cli = hlist_entry(hnode,
1080 struct nrs_tbf_client,
1083 atomic_inc(&cli->tc_ref);
1086 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1088 struct nrs_tbf_client *cli = hlist_entry(hnode,
1089 struct nrs_tbf_client,
1092 atomic_dec(&cli->tc_ref);
1095 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1097 struct nrs_tbf_client *cli = hlist_entry(hnode,
1098 struct nrs_tbf_client,
1101 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1102 "Busy TBF object from client with NID %s, with %d refs\n",
1103 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1105 nrs_tbf_cli_fini(cli);
1108 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1109 .hs_hash = nrs_tbf_nid_hop_hash,
1110 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
1111 .hs_key = nrs_tbf_nid_hop_key,
1112 .hs_object = nrs_tbf_nid_hop_object,
1113 .hs_get = nrs_tbf_nid_hop_get,
1114 .hs_put = nrs_tbf_nid_hop_put,
1115 .hs_put_locked = nrs_tbf_nid_hop_put,
1116 .hs_exit = nrs_tbf_nid_hop_exit,
1119 static struct nrs_tbf_client *
1120 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1121 struct ptlrpc_request *req)
1123 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
1126 static struct nrs_tbf_client *
1127 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1128 struct nrs_tbf_client *cli)
1130 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1135 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1136 struct nrs_tbf_client *cli)
1138 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1142 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1143 struct nrs_tbf_head *head)
1145 struct nrs_tbf_cmd start;
1148 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1151 NRS_TBF_NID_BKT_BITS, 0,
1154 &nrs_tbf_nid_hash_ops,
1155 CFS_HASH_RW_BKTLOCK);
1156 if (head->th_cli_hash == NULL)
1159 memset(&start, 0, sizeof(start));
1160 start.u.tc_start.ts_nids_str = "*";
1162 start.u.tc_start.ts_rpc_rate = tbf_rate;
1163 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1164 start.tc_name = NRS_TBF_DEFAULT_RULE;
1165 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1166 rc = nrs_tbf_rule_start(policy, head, &start);
1168 cfs_hash_putref(head->th_cli_hash);
1169 head->th_cli_hash = NULL;
1176 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1177 struct ptlrpc_request *req)
1179 cli->tc_nid = req->rq_peer.nid;
1182 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1183 struct nrs_tbf_rule *rule,
1184 struct nrs_tbf_cmd *start)
1186 LASSERT(start->u.tc_start.ts_nids_str);
1187 OBD_ALLOC(rule->tr_nids_str,
1188 strlen(start->u.tc_start.ts_nids_str) + 1);
1189 if (rule->tr_nids_str == NULL)
1192 memcpy(rule->tr_nids_str,
1193 start->u.tc_start.ts_nids_str,
1194 strlen(start->u.tc_start.ts_nids_str));
1196 INIT_LIST_HEAD(&rule->tr_nids);
1197 if (!list_empty(&start->u.tc_start.ts_nids)) {
1198 if (cfs_parse_nidlist(rule->tr_nids_str,
1199 strlen(rule->tr_nids_str),
1200 &rule->tr_nids) <= 0) {
1201 CERROR("nids {%s} illegal\n",
1203 OBD_FREE(rule->tr_nids_str,
1204 strlen(start->u.tc_start.ts_nids_str) + 1);
1212 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1214 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1215 rule->tr_nids_str, rule->tr_rpc_rate,
1216 atomic_read(&rule->tr_ref) - 1);
1221 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1222 struct nrs_tbf_client *cli)
1224 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1227 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1229 if (!list_empty(&rule->tr_nids))
1230 cfs_free_nidlist(&rule->tr_nids);
1231 LASSERT(rule->tr_nids_str != NULL);
1232 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1235 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1237 if (!list_empty(&cmd->u.tc_start.ts_nids))
1238 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1239 if (cmd->u.tc_start.ts_nids_str)
1240 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1241 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1244 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1246 struct cfs_lstr src;
1250 src.ls_len = strlen(id);
1251 rc = nrs_tbf_check_id_value(&src, "nid");
1255 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1256 if (cmd->u.tc_start.ts_nids_str == NULL)
1259 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1261 /* parse NID list */
1262 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1263 strlen(cmd->u.tc_start.ts_nids_str),
1264 &cmd->u.tc_start.ts_nids) <= 0) {
1265 nrs_tbf_nid_cmd_fini(cmd);
1272 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1273 .o_name = NRS_TBF_TYPE_NID,
1274 .o_startup = nrs_tbf_nid_startup,
1275 .o_cli_find = nrs_tbf_nid_cli_find,
1276 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1277 .o_cli_put = nrs_tbf_nid_cli_put,
1278 .o_cli_init = nrs_tbf_nid_cli_init,
1279 .o_rule_init = nrs_tbf_nid_rule_init,
1280 .o_rule_dump = nrs_tbf_nid_rule_dump,
1281 .o_rule_match = nrs_tbf_nid_rule_match,
1282 .o_rule_fini = nrs_tbf_nid_rule_fini,
1285 static unsigned nrs_tbf_hop_hash(struct cfs_hash *hs, const void *key,
1288 return cfs_hash_djb2_hash(key, strlen(key), mask);
1291 static int nrs_tbf_hop_keycmp(const void *key, struct hlist_node *hnode)
1293 struct nrs_tbf_client *cli = hlist_entry(hnode,
1294 struct nrs_tbf_client,
1297 return (strcmp(cli->tc_key, key) == 0);
1300 static void *nrs_tbf_hop_key(struct hlist_node *hnode)
1302 struct nrs_tbf_client *cli = hlist_entry(hnode,
1303 struct nrs_tbf_client,
1308 static void *nrs_tbf_hop_object(struct hlist_node *hnode)
1310 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1313 static void nrs_tbf_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1315 struct nrs_tbf_client *cli = hlist_entry(hnode,
1316 struct nrs_tbf_client,
1319 atomic_inc(&cli->tc_ref);
1322 static void nrs_tbf_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1324 struct nrs_tbf_client *cli = hlist_entry(hnode,
1325 struct nrs_tbf_client,
1328 atomic_dec(&cli->tc_ref);
1331 static void nrs_tbf_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1334 struct nrs_tbf_client *cli = hlist_entry(hnode,
1335 struct nrs_tbf_client,
1338 LASSERT(atomic_read(&cli->tc_ref) == 0);
1339 nrs_tbf_cli_fini(cli);
1342 static struct cfs_hash_ops nrs_tbf_hash_ops = {
1343 .hs_hash = nrs_tbf_hop_hash,
1344 .hs_keycmp = nrs_tbf_hop_keycmp,
1345 .hs_key = nrs_tbf_hop_key,
1346 .hs_object = nrs_tbf_hop_object,
1347 .hs_get = nrs_tbf_hop_get,
1348 .hs_put = nrs_tbf_hop_put,
1349 .hs_put_locked = nrs_tbf_hop_put,
1350 .hs_exit = nrs_tbf_hop_exit,
1353 #define NRS_TBF_GENERIC_BKT_BITS 10
1354 #define NRS_TBF_GENERIC_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
1355 CFS_HASH_NO_ITEMREF | \
1359 nrs_tbf_startup(struct ptlrpc_nrs_policy *policy, struct nrs_tbf_head *head)
1361 struct nrs_tbf_cmd start;
1362 struct nrs_tbf_bucket *bkt;
1366 struct cfs_hash_bd bd;
1368 bits = nrs_tbf_jobid_hash_order();
1369 if (bits < NRS_TBF_GENERIC_BKT_BITS)
1370 bits = NRS_TBF_GENERIC_BKT_BITS;
1371 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1373 NRS_TBF_GENERIC_BKT_BITS,
1376 NRS_TBF_GENERIC_HASH_FLAGS);
1377 if (head->th_cli_hash == NULL)
1380 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
1381 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
1382 INIT_LIST_HEAD(&bkt->ntb_lru);
1385 memset(&start, 0, sizeof(start));
1386 start.u.tc_start.ts_conds_str = "*";
1388 start.u.tc_start.ts_rpc_rate = tbf_rate;
1389 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1390 start.tc_name = NRS_TBF_DEFAULT_RULE;
1391 INIT_LIST_HEAD(&start.u.tc_start.ts_conds);
1392 rc = nrs_tbf_rule_start(policy, head, &start);
1394 cfs_hash_putref(head->th_cli_hash);
1399 static struct nrs_tbf_client *
1400 nrs_tbf_cli_hash_lookup(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1403 struct hlist_node *hnode;
1404 struct nrs_tbf_client *cli;
1406 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)key);
1410 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
1411 if (!list_empty(&cli->tc_lru))
1412 list_del_init(&cli->tc_lru);
1416 static struct nrs_tbf_client *
1417 nrs_tbf_cli_find(struct nrs_tbf_head *head, struct ptlrpc_request *req)
1419 struct nrs_tbf_client *cli;
1420 struct cfs_hash *hs = head->th_cli_hash;
1421 struct cfs_hash_bd bd;
1422 char keystr[NRS_TBF_KEY_LEN] = { '\0' };
1426 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1428 jobid = NRS_TBF_JOBID_NULL;
1429 opc = lustre_msg_get_opc(req->rq_reqmsg);
1430 snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1431 libcfs_nid2str(req->rq_peer.nid), opc);
1432 LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1433 cfs_hash_bd_get_and_lock(hs, (void *)keystr, &bd, 1);
1434 cli = nrs_tbf_cli_hash_lookup(hs, &bd, keystr);
1435 cfs_hash_bd_unlock(hs, &bd, 1);
1440 static struct nrs_tbf_client *
1441 nrs_tbf_cli_findadd(struct nrs_tbf_head *head,
1442 struct nrs_tbf_client *cli)
1445 struct nrs_tbf_client *ret;
1446 struct cfs_hash *hs = head->th_cli_hash;
1447 struct cfs_hash_bd bd;
1450 cfs_hash_bd_get_and_lock(hs, (void *)key, &bd, 1);
1451 ret = nrs_tbf_cli_hash_lookup(hs, &bd, key);
1453 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
1456 cfs_hash_bd_unlock(hs, &bd, 1);
1462 nrs_tbf_cli_put(struct nrs_tbf_head *head, struct nrs_tbf_client *cli)
1464 struct cfs_hash_bd bd;
1465 struct cfs_hash *hs = head->th_cli_hash;
1466 struct nrs_tbf_bucket *bkt;
1468 struct list_head zombies;
1470 INIT_LIST_HEAD(&zombies);
1471 cfs_hash_bd_get(hs, &cli->tc_key, &bd);
1472 bkt = cfs_hash_bd_extra_get(hs, &bd);
1473 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
1475 LASSERT(list_empty(&cli->tc_lru));
1476 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
1479 * Check and purge the LRU, there is at least one client in the LRU.
1481 hw = tbf_jobid_cache_size >> (hs->hs_cur_bits - hs->hs_bkt_bits);
1482 while (cfs_hash_bd_count_get(&bd) > hw) {
1483 if (unlikely(list_empty(&bkt->ntb_lru)))
1485 cli = list_entry(bkt->ntb_lru.next,
1486 struct nrs_tbf_client,
1488 LASSERT(atomic_read(&cli->tc_ref) == 0);
1489 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
1490 list_move(&cli->tc_lru, &zombies);
1492 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
1494 while (!list_empty(&zombies)) {
1495 cli = container_of0(zombies.next,
1496 struct nrs_tbf_client, tc_lru);
1497 list_del_init(&cli->tc_lru);
1498 nrs_tbf_cli_fini(cli);
1503 nrs_tbf_generic_cli_init(struct nrs_tbf_client *cli,
1504 struct ptlrpc_request *req)
1506 char keystr[NRS_TBF_KEY_LEN];
1510 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1512 jobid = NRS_TBF_JOBID_NULL;
1513 opc = lustre_msg_get_opc(req->rq_reqmsg);
1514 snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1515 libcfs_nid2str(req->rq_peer.nid), opc);
1517 LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1518 INIT_LIST_HEAD(&cli->tc_lru);
1519 memcpy(cli->tc_key, keystr, strlen(keystr));
1520 memcpy(cli->tc_jobid, jobid, strlen(jobid));
1521 cli->tc_nid = req->rq_peer.nid;
1522 cli->tc_opcode = opc;
1526 nrs_tbf_expression_free(struct nrs_tbf_expression *expr)
1528 LASSERT(expr->te_field >= NRS_TBF_FIELD_NID &&
1529 expr->te_field < NRS_TBF_FIELD_MAX);
1530 switch (expr->te_field) {
1531 case NRS_TBF_FIELD_NID:
1532 cfs_free_nidlist(&expr->te_cond);
1534 case NRS_TBF_FIELD_JOBID:
1535 nrs_tbf_jobid_list_free(&expr->te_cond);
1537 case NRS_TBF_FIELD_OPCODE:
1538 CFS_FREE_BITMAP(expr->te_opcodes);
1547 nrs_tbf_conjunction_free(struct nrs_tbf_conjunction *conjunction)
1549 struct nrs_tbf_expression *expression;
1550 struct nrs_tbf_expression *n;
1552 LASSERT(list_empty(&conjunction->tc_linkage));
1553 list_for_each_entry_safe(expression, n,
1554 &conjunction->tc_expressions,
1556 list_del_init(&expression->te_linkage);
1557 nrs_tbf_expression_free(expression);
1559 OBD_FREE_PTR(conjunction);
1563 nrs_tbf_conds_free(struct list_head *cond_list)
1565 struct nrs_tbf_conjunction *conjunction;
1566 struct nrs_tbf_conjunction *n;
1568 list_for_each_entry_safe(conjunction, n, cond_list, tc_linkage) {
1569 list_del_init(&conjunction->tc_linkage);
1570 nrs_tbf_conjunction_free(conjunction);
1575 nrs_tbf_generic_cmd_fini(struct nrs_tbf_cmd *cmd)
1577 if (!list_empty(&cmd->u.tc_start.ts_conds))
1578 nrs_tbf_conds_free(&cmd->u.tc_start.ts_conds);
1579 if (cmd->u.tc_start.ts_conds_str)
1580 OBD_FREE(cmd->u.tc_start.ts_conds_str,
1581 strlen(cmd->u.tc_start.ts_conds_str) + 1);
1584 #define NRS_TBF_DISJUNCTION_DELIM (',')
1585 #define NRS_TBF_CONJUNCTION_DELIM ('&')
1586 #define NRS_TBF_EXPRESSION_DELIM ('=')
1589 nrs_tbf_check_field(struct cfs_lstr *field, char *str)
1591 int len = strlen(str);
1593 return (field->ls_len == len &&
1594 strncmp(field->ls_str, str, len) == 0);
1598 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr);
1601 nrs_tbf_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
1603 struct nrs_tbf_expression *expr;
1604 struct cfs_lstr field;
1607 OBD_ALLOC(expr, sizeof(struct nrs_tbf_expression));
1611 rc = cfs_gettok(src, NRS_TBF_EXPRESSION_DELIM, &field);
1612 if (rc == 0 || src->ls_len <= 2 || src->ls_str[0] != '{' ||
1613 src->ls_str[src->ls_len - 1] != '}')
1614 GOTO(out, rc = -EINVAL);
1616 /* Skip '{' and '}' */
1620 if (nrs_tbf_check_field(&field, "nid")) {
1621 if (cfs_parse_nidlist(src->ls_str,
1623 &expr->te_cond) <= 0)
1624 GOTO(out, rc = -EINVAL);
1625 expr->te_field = NRS_TBF_FIELD_NID;
1626 } else if (nrs_tbf_check_field(&field, "jobid")) {
1627 if (nrs_tbf_jobid_list_parse(src->ls_str,
1629 &expr->te_cond) < 0)
1630 GOTO(out, rc = -EINVAL);
1631 expr->te_field = NRS_TBF_FIELD_JOBID;
1632 } else if (nrs_tbf_check_field(&field, "opcode")) {
1633 if (nrs_tbf_opcode_list_parse(src->ls_str,
1635 &expr->te_opcodes) < 0)
1636 GOTO(out, rc = -EINVAL);
1637 expr->te_field = NRS_TBF_FIELD_OPCODE;
1639 GOTO(out, rc = -EINVAL);
1641 list_add_tail(&expr->te_linkage, cond_list);
1649 nrs_tbf_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
1651 struct nrs_tbf_conjunction *conjunction;
1652 struct cfs_lstr expr;
1655 OBD_ALLOC(conjunction, sizeof(struct nrs_tbf_conjunction));
1656 if (conjunction == NULL)
1659 INIT_LIST_HEAD(&conjunction->tc_expressions);
1660 list_add_tail(&conjunction->tc_linkage, cond_list);
1662 while (src->ls_str) {
1663 rc = cfs_gettok(src, NRS_TBF_CONJUNCTION_DELIM, &expr);
1668 rc = nrs_tbf_expression_parse(&expr,
1669 &conjunction->tc_expressions);
1677 nrs_tbf_conds_parse(char *str, int len, struct list_head *cond_list)
1679 struct cfs_lstr src;
1680 struct cfs_lstr res;
1685 INIT_LIST_HEAD(cond_list);
1686 while (src.ls_str) {
1687 rc = cfs_gettok(&src, NRS_TBF_DISJUNCTION_DELIM, &res);
1692 rc = nrs_tbf_conjunction_parse(&res, cond_list);
1700 nrs_tbf_generic_parse(struct nrs_tbf_cmd *cmd, const char *id)
1704 OBD_ALLOC(cmd->u.tc_start.ts_conds_str, strlen(id) + 1);
1705 if (cmd->u.tc_start.ts_conds_str == NULL)
1708 memcpy(cmd->u.tc_start.ts_conds_str, id, strlen(id));
1710 /* Parse hybird NID and JOBID conditions */
1711 rc = nrs_tbf_conds_parse(cmd->u.tc_start.ts_conds_str,
1712 strlen(cmd->u.tc_start.ts_conds_str),
1713 &cmd->u.tc_start.ts_conds);
1715 nrs_tbf_generic_cmd_fini(cmd);
1721 nrs_tbf_expression_match(struct nrs_tbf_expression *expr,
1722 struct nrs_tbf_rule *rule,
1723 struct nrs_tbf_client *cli)
1725 switch (expr->te_field) {
1726 case NRS_TBF_FIELD_NID:
1727 return cfs_match_nid(cli->tc_nid, &expr->te_cond);
1728 case NRS_TBF_FIELD_JOBID:
1729 return nrs_tbf_jobid_list_match(&expr->te_cond, cli->tc_jobid);
1730 case NRS_TBF_FIELD_OPCODE:
1731 return cfs_bitmap_check(expr->te_opcodes, cli->tc_opcode);
1738 nrs_tbf_conjunction_match(struct nrs_tbf_conjunction *conjunction,
1739 struct nrs_tbf_rule *rule,
1740 struct nrs_tbf_client *cli)
1742 struct nrs_tbf_expression *expr;
1745 list_for_each_entry(expr, &conjunction->tc_expressions, te_linkage) {
1746 matched = nrs_tbf_expression_match(expr, rule, cli);
1755 nrs_tbf_cond_match(struct nrs_tbf_rule *rule, struct nrs_tbf_client *cli)
1757 struct nrs_tbf_conjunction *conjunction;
1760 list_for_each_entry(conjunction, &rule->tr_conds, tc_linkage) {
1761 matched = nrs_tbf_conjunction_match(conjunction, rule, cli);
1770 nrs_tbf_generic_rule_fini(struct nrs_tbf_rule *rule)
1772 if (!list_empty(&rule->tr_conds))
1773 nrs_tbf_conds_free(&rule->tr_conds);
1774 LASSERT(rule->tr_conds_str != NULL);
1775 OBD_FREE(rule->tr_conds_str, strlen(rule->tr_conds_str) + 1);
1779 nrs_tbf_rule_init(struct ptlrpc_nrs_policy *policy,
1780 struct nrs_tbf_rule *rule, struct nrs_tbf_cmd *start)
1784 LASSERT(start->u.tc_start.ts_conds_str);
1785 OBD_ALLOC(rule->tr_conds_str,
1786 strlen(start->u.tc_start.ts_conds_str) + 1);
1787 if (rule->tr_conds_str == NULL)
1790 memcpy(rule->tr_conds_str,
1791 start->u.tc_start.ts_conds_str,
1792 strlen(start->u.tc_start.ts_conds_str));
1794 INIT_LIST_HEAD(&rule->tr_conds);
1795 if (!list_empty(&start->u.tc_start.ts_conds)) {
1796 rc = nrs_tbf_conds_parse(rule->tr_conds_str,
1797 strlen(rule->tr_conds_str),
1801 nrs_tbf_generic_rule_fini(rule);
1807 nrs_tbf_generic_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1809 seq_printf(m, "%s %s %llu, ref %d\n", rule->tr_name,
1810 rule->tr_conds_str, rule->tr_rpc_rate,
1811 atomic_read(&rule->tr_ref) - 1);
1816 nrs_tbf_generic_rule_match(struct nrs_tbf_rule *rule,
1817 struct nrs_tbf_client *cli)
1819 return nrs_tbf_cond_match(rule, cli);
1822 static struct nrs_tbf_ops nrs_tbf_generic_ops = {
1823 .o_name = NRS_TBF_TYPE_GENERIC,
1824 .o_startup = nrs_tbf_startup,
1825 .o_cli_find = nrs_tbf_cli_find,
1826 .o_cli_findadd = nrs_tbf_cli_findadd,
1827 .o_cli_put = nrs_tbf_cli_put,
1828 .o_cli_init = nrs_tbf_generic_cli_init,
1829 .o_rule_init = nrs_tbf_rule_init,
1830 .o_rule_dump = nrs_tbf_generic_rule_dump,
1831 .o_rule_match = nrs_tbf_generic_rule_match,
1832 .o_rule_fini = nrs_tbf_generic_rule_fini,
1835 static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
1837 if (rule->tr_opcodes != NULL)
1838 CFS_FREE_BITMAP(rule->tr_opcodes);
1840 LASSERT(rule->tr_opcodes_str != NULL);
1841 OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
1844 static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
1847 return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
1850 static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
1852 const __u32 *opc = key;
1853 struct nrs_tbf_client *cli = hlist_entry(hnode,
1854 struct nrs_tbf_client,
1857 return *opc == cli->tc_opcode;
1860 static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
1862 struct nrs_tbf_client *cli = hlist_entry(hnode,
1863 struct nrs_tbf_client,
1866 return &cli->tc_opcode;
1869 static void *nrs_tbf_opcode_hop_object(struct hlist_node *hnode)
1871 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1874 static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
1875 struct hlist_node *hnode)
1877 struct nrs_tbf_client *cli = hlist_entry(hnode,
1878 struct nrs_tbf_client,
1881 atomic_inc(&cli->tc_ref);
1884 static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
1885 struct hlist_node *hnode)
1887 struct nrs_tbf_client *cli = hlist_entry(hnode,
1888 struct nrs_tbf_client,
1891 atomic_dec(&cli->tc_ref);
1894 static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
1895 struct hlist_node *hnode)
1897 struct nrs_tbf_client *cli = hlist_entry(hnode,
1898 struct nrs_tbf_client,
1901 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1902 "Busy TBF object from client with opcode %s, with %d refs\n",
1903 ll_opcode2str(cli->tc_opcode),
1904 atomic_read(&cli->tc_ref));
1906 nrs_tbf_cli_fini(cli);
1908 static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
1909 .hs_hash = nrs_tbf_opcode_hop_hash,
1910 .hs_keycmp = nrs_tbf_opcode_hop_keycmp,
1911 .hs_key = nrs_tbf_opcode_hop_key,
1912 .hs_object = nrs_tbf_opcode_hop_object,
1913 .hs_get = nrs_tbf_opcode_hop_get,
1914 .hs_put = nrs_tbf_opcode_hop_put,
1915 .hs_put_locked = nrs_tbf_opcode_hop_put,
1916 .hs_exit = nrs_tbf_opcode_hop_exit,
1920 nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
1921 struct nrs_tbf_head *head)
1923 struct nrs_tbf_cmd start = { 0 };
1926 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1929 NRS_TBF_NID_BKT_BITS, 0,
1932 &nrs_tbf_opcode_hash_ops,
1933 CFS_HASH_RW_BKTLOCK);
1934 if (head->th_cli_hash == NULL)
1937 start.u.tc_start.ts_opcodes = NULL;
1938 start.u.tc_start.ts_opcodes_str = "*";
1940 start.u.tc_start.ts_rpc_rate = tbf_rate;
1941 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1942 start.tc_name = NRS_TBF_DEFAULT_RULE;
1943 rc = nrs_tbf_rule_start(policy, head, &start);
1948 static struct nrs_tbf_client *
1949 nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
1950 struct ptlrpc_request *req)
1954 opc = lustre_msg_get_opc(req->rq_reqmsg);
1955 return cfs_hash_lookup(head->th_cli_hash, &opc);
1958 static struct nrs_tbf_client *
1959 nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
1960 struct nrs_tbf_client *cli)
1962 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
1967 nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
1968 struct ptlrpc_request *req)
1970 cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
1973 #define MAX_OPCODE_LEN 32
1975 nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, struct cfs_bitmap *opcodes)
1978 char opcode_str[MAX_OPCODE_LEN];
1980 if (id->ls_len + 1 > MAX_OPCODE_LEN)
1983 memcpy(opcode_str, id->ls_str, id->ls_len);
1984 opcode_str[id->ls_len] = '\0';
1986 op = ll_str2opcode(opcode_str);
1990 cfs_bitmap_set(opcodes, op);
1995 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr)
1997 struct cfs_bitmap *opcodes;
1998 struct cfs_lstr src;
1999 struct cfs_lstr res;
2003 opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
2004 if (opcodes == NULL)
2009 while (src.ls_str) {
2010 rc = cfs_gettok(&src, ' ', &res);
2015 rc = nrs_tbf_opcode_set_bit(&res, opcodes);
2021 *bitmaptr = opcodes;
2023 CFS_FREE_BITMAP(opcodes);
2028 static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
2030 if (cmd->u.tc_start.ts_opcodes)
2031 CFS_FREE_BITMAP(cmd->u.tc_start.ts_opcodes);
2033 if (cmd->u.tc_start.ts_opcodes_str)
2034 OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
2035 strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
2039 static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
2041 struct cfs_lstr src;
2045 src.ls_len = strlen(id);
2046 rc = nrs_tbf_check_id_value(&src, "opcode");
2050 OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
2051 if (cmd->u.tc_start.ts_opcodes_str == NULL)
2054 memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
2056 /* parse opcode list */
2057 rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
2058 strlen(cmd->u.tc_start.ts_opcodes_str),
2059 &cmd->u.tc_start.ts_opcodes);
2061 nrs_tbf_opcode_cmd_fini(cmd);
2067 nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
2068 struct nrs_tbf_client *cli)
2070 if (rule->tr_opcodes == NULL)
2073 return cfs_bitmap_check(rule->tr_opcodes, cli->tc_opcode);
2076 static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
2077 struct nrs_tbf_rule *rule,
2078 struct nrs_tbf_cmd *start)
2082 LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
2083 OBD_ALLOC(rule->tr_opcodes_str,
2084 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2085 if (rule->tr_opcodes_str == NULL)
2088 strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
2089 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2091 /* Default rule '*' */
2092 if (start->u.tc_start.ts_opcodes == NULL)
2095 rc = nrs_tbf_opcode_list_parse(rule->tr_opcodes_str,
2096 strlen(rule->tr_opcodes_str),
2099 OBD_FREE(rule->tr_opcodes_str,
2100 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2106 nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2108 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2109 rule->tr_opcodes_str, rule->tr_rpc_rate,
2110 atomic_read(&rule->tr_ref) - 1);
2115 struct nrs_tbf_ops nrs_tbf_opcode_ops = {
2116 .o_name = NRS_TBF_TYPE_OPCODE,
2117 .o_startup = nrs_tbf_opcode_startup,
2118 .o_cli_find = nrs_tbf_opcode_cli_find,
2119 .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
2120 .o_cli_put = nrs_tbf_nid_cli_put,
2121 .o_cli_init = nrs_tbf_opcode_cli_init,
2122 .o_rule_init = nrs_tbf_opcode_rule_init,
2123 .o_rule_dump = nrs_tbf_opcode_rule_dump,
2124 .o_rule_match = nrs_tbf_opcode_rule_match,
2125 .o_rule_fini = nrs_tbf_opcode_rule_fini,
2128 static struct nrs_tbf_type nrs_tbf_types[] = {
2130 .ntt_name = NRS_TBF_TYPE_JOBID,
2131 .ntt_flag = NRS_TBF_FLAG_JOBID,
2132 .ntt_ops = &nrs_tbf_jobid_ops,
2135 .ntt_name = NRS_TBF_TYPE_NID,
2136 .ntt_flag = NRS_TBF_FLAG_NID,
2137 .ntt_ops = &nrs_tbf_nid_ops,
2140 .ntt_name = NRS_TBF_TYPE_OPCODE,
2141 .ntt_flag = NRS_TBF_FLAG_OPCODE,
2142 .ntt_ops = &nrs_tbf_opcode_ops,
2145 .ntt_name = NRS_TBF_TYPE_GENERIC,
2146 .ntt_flag = NRS_TBF_FLAG_GENERIC,
2147 .ntt_ops = &nrs_tbf_generic_ops,
2152 * Is called before the policy transitions into
2153 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
2154 * policy-specific private data structure.
2156 * \param[in] policy The policy to start
2158 * \retval -ENOMEM OOM error
2161 * \see nrs_policy_register()
2162 * \see nrs_policy_ctl()
2164 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
2166 struct nrs_tbf_head *head;
2167 struct nrs_tbf_ops *ops;
2175 name = NRS_TBF_TYPE_GENERIC;
2176 else if (strlen(arg) < NRS_TBF_TYPE_MAX_LEN)
2179 GOTO(out, rc = -EINVAL);
2181 for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
2182 if (strcmp(name, nrs_tbf_types[i].ntt_name) == 0) {
2183 ops = nrs_tbf_types[i].ntt_ops;
2184 type = nrs_tbf_types[i].ntt_flag;
2190 GOTO(out, rc = -ENOTSUPP);
2192 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
2194 GOTO(out, rc = -ENOMEM);
2196 memcpy(head->th_type, name, strlen(name));
2197 head->th_type[strlen(name)] = '\0';
2199 head->th_type_flag = type;
2201 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
2202 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
2203 nrs_pol2cptab(policy),
2204 nrs_pol2cptid(policy));
2205 if (head->th_binheap == NULL)
2206 GOTO(out_free_head, rc = -ENOMEM);
2208 atomic_set(&head->th_rule_sequence, 0);
2209 spin_lock_init(&head->th_rule_lock);
2210 INIT_LIST_HEAD(&head->th_list);
2211 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2212 head->th_timer.function = nrs_tbf_timer_cb;
2213 rc = head->th_ops->o_startup(policy, head);
2215 GOTO(out_free_heap, rc);
2217 policy->pol_private = head;
2220 cfs_binheap_destroy(head->th_binheap);
2228 * Is called before the policy transitions into
2229 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
2230 * private data structure.
2232 * \param[in] policy The policy to stop
2234 * \see nrs_policy_stop0()
2236 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
2238 struct nrs_tbf_head *head = policy->pol_private;
2239 struct ptlrpc_nrs *nrs = policy->pol_nrs;
2240 struct nrs_tbf_rule *rule, *n;
2242 LASSERT(head != NULL);
2243 LASSERT(head->th_cli_hash != NULL);
2244 hrtimer_cancel(&head->th_timer);
2245 /* Should cleanup hash first before free rules */
2246 cfs_hash_putref(head->th_cli_hash);
2247 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
2248 list_del_init(&rule->tr_linkage);
2249 nrs_tbf_rule_put(rule);
2251 LASSERT(list_empty(&head->th_list));
2252 LASSERT(head->th_binheap != NULL);
2253 LASSERT(cfs_binheap_is_empty(head->th_binheap));
2254 cfs_binheap_destroy(head->th_binheap);
2256 nrs->nrs_throttling = 0;
2257 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
2261 * Performs a policy-specific ctl function on TBF policy instances; similar
2264 * \param[in] policy the policy instance
2265 * \param[in] opc the opcode
2266 * \param[in,out] arg used for passing parameters and information
2268 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2269 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2271 * \retval 0 operation carried out successfully
2274 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
2275 enum ptlrpc_nrs_ctl opc,
2281 assert_spin_locked(&policy->pol_nrs->nrs_lock);
2283 switch ((enum nrs_ctl_tbf)opc) {
2288 * Read RPC rate size of a policy instance.
2290 case NRS_CTL_TBF_RD_RULE: {
2291 struct nrs_tbf_head *head = policy->pol_private;
2292 struct seq_file *m = (struct seq_file *) arg;
2293 struct ptlrpc_service_part *svcpt;
2295 svcpt = policy->pol_nrs->nrs_svcpt;
2296 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
2298 rc = nrs_tbf_rule_dump_all(head, m);
2303 * Write RPC rate of a policy instance.
2305 case NRS_CTL_TBF_WR_RULE: {
2306 struct nrs_tbf_head *head = policy->pol_private;
2307 struct nrs_tbf_cmd *cmd;
2309 cmd = (struct nrs_tbf_cmd *)arg;
2310 rc = nrs_tbf_command(policy,
2316 * Read the TBF policy type of a policy instance.
2318 case NRS_CTL_TBF_RD_TYPE_FLAG: {
2319 struct nrs_tbf_head *head = policy->pol_private;
2321 *(__u32 *)arg = head->th_type_flag;
2330 * Is called for obtaining a TBF policy resource.
2332 * \param[in] policy The policy on which the request is being asked for
2333 * \param[in] nrq The request for which resources are being taken
2334 * \param[in] parent Parent resource, unused in this policy
2335 * \param[out] resp Resources references are placed in this array
2336 * \param[in] moving_req Signifies limited caller context; unused in this
2340 * \see nrs_resource_get_safe()
2342 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
2343 struct ptlrpc_nrs_request *nrq,
2344 const struct ptlrpc_nrs_resource *parent,
2345 struct ptlrpc_nrs_resource **resp,
2348 struct nrs_tbf_head *head;
2349 struct nrs_tbf_client *cli;
2350 struct nrs_tbf_client *tmp;
2351 struct ptlrpc_request *req;
2353 if (parent == NULL) {
2354 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
2358 head = container_of(parent, struct nrs_tbf_head, th_res);
2359 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
2360 cli = head->th_ops->o_cli_find(head, req);
2362 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2363 LASSERT(cli->tc_rule);
2364 if (cli->tc_rule_sequence !=
2365 atomic_read(&head->th_rule_sequence) ||
2366 cli->tc_rule->tr_flags & NTRS_STOPPING) {
2367 struct nrs_tbf_rule *rule;
2369 rule = nrs_tbf_rule_match(head, cli);
2370 if (rule != cli->tc_rule) {
2371 nrs_tbf_cli_reset(head, rule, cli);
2373 if (cli->tc_rule_generation != rule->tr_generation)
2374 nrs_tbf_cli_reset_value(head, cli);
2375 nrs_tbf_rule_put(rule);
2377 } else if (cli->tc_rule_generation !=
2378 cli->tc_rule->tr_generation) {
2379 nrs_tbf_cli_reset_value(head, cli);
2381 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2385 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
2386 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
2390 nrs_tbf_cli_init(head, cli, req);
2391 tmp = head->th_ops->o_cli_findadd(head, cli);
2393 atomic_dec(&cli->tc_ref);
2394 nrs_tbf_cli_fini(cli);
2398 *resp = &cli->tc_res;
2404 * Called when releasing references to the resource hierachy obtained for a
2405 * request for scheduling using the TBF policy.
2407 * \param[in] policy the policy the resource belongs to
2408 * \param[in] res the resource to be released
2410 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
2411 const struct ptlrpc_nrs_resource *res)
2413 struct nrs_tbf_head *head;
2414 struct nrs_tbf_client *cli;
2417 * Do nothing for freeing parent, nrs_tbf_net resources
2419 if (res->res_parent == NULL)
2422 cli = container_of(res, struct nrs_tbf_client, tc_res);
2423 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
2425 head->th_ops->o_cli_put(head, cli);
2429 * Called when getting a request from the TBF policy for handling, or just
2430 * peeking; removes the request from the policy when it is to be handled.
2432 * \param[in] policy The policy
2433 * \param[in] peek When set, signifies that we just want to examine the
2434 * request, and not handle it, so the request is not removed
2436 * \param[in] force Force the policy to return a request; unused in this
2439 * \retval The request to be handled; this is the next request in the TBF
2442 * \see ptlrpc_nrs_req_get_nolock()
2443 * \see nrs_request_get()
2446 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
2447 bool peek, bool force)
2449 struct nrs_tbf_head *head = policy->pol_private;
2450 struct ptlrpc_nrs_request *nrq = NULL;
2451 struct nrs_tbf_client *cli;
2452 struct cfs_binheap_node *node;
2454 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2456 if (!peek && policy->pol_nrs->nrs_throttling)
2459 node = cfs_binheap_root(head->th_binheap);
2460 if (unlikely(node == NULL))
2463 cli = container_of(node, struct nrs_tbf_client, tc_node);
2464 LASSERT(cli->tc_in_heap);
2466 nrq = list_entry(cli->tc_list.next,
2467 struct ptlrpc_nrs_request,
2470 __u64 now = ktime_to_ns(ktime_get());
2475 deadline = cli->tc_check_time +
2477 LASSERT(now >= cli->tc_check_time);
2478 passed = now - cli->tc_check_time;
2479 ntoken = passed * cli->tc_rpc_rate;
2480 do_div(ntoken, NSEC_PER_SEC);
2481 ntoken += cli->tc_ntoken;
2482 if (ntoken > cli->tc_depth)
2483 ntoken = cli->tc_depth;
2485 struct ptlrpc_request *req;
2486 nrq = list_entry(cli->tc_list.next,
2487 struct ptlrpc_nrs_request,
2489 req = container_of(nrq,
2490 struct ptlrpc_request,
2493 cli->tc_ntoken = ntoken;
2494 cli->tc_check_time = now;
2495 list_del_init(&nrq->nr_u.tbf.tr_list);
2496 if (list_empty(&cli->tc_list)) {
2497 cfs_binheap_remove(head->th_binheap,
2499 cli->tc_in_heap = false;
2501 cfs_binheap_relocate(head->th_binheap,
2505 "NRS start %s request from %s, "
2507 policy->pol_desc->pd_name,
2508 libcfs_id2str(req->rq_peer),
2509 nrq->nr_u.tbf.tr_sequence);
2513 policy->pol_nrs->nrs_throttling = 1;
2514 head->th_deadline = deadline;
2515 time = ktime_set(0, 0);
2516 time = ktime_add_ns(time, deadline);
2517 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
2525 * Adds request \a nrq to \a policy's list of queued requests
2527 * \param[in] policy The policy
2528 * \param[in] nrq The request to add
2530 * \retval 0 success; nrs_request_enqueue() assumes this function will always
2533 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
2534 struct ptlrpc_nrs_request *nrq)
2536 struct nrs_tbf_head *head;
2537 struct nrs_tbf_client *cli;
2540 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2542 cli = container_of(nrs_request_resource(nrq),
2543 struct nrs_tbf_client, tc_res);
2544 head = container_of(nrs_request_resource(nrq)->res_parent,
2545 struct nrs_tbf_head, th_res);
2546 if (list_empty(&cli->tc_list)) {
2547 LASSERT(!cli->tc_in_heap);
2548 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
2550 cli->tc_in_heap = true;
2551 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2552 list_add_tail(&nrq->nr_u.tbf.tr_list,
2554 if (policy->pol_nrs->nrs_throttling) {
2555 __u64 deadline = cli->tc_check_time +
2557 if ((head->th_deadline > deadline) &&
2558 (hrtimer_try_to_cancel(&head->th_timer)
2561 head->th_deadline = deadline;
2562 time = ktime_set(0, 0);
2563 time = ktime_add_ns(time, deadline);
2564 hrtimer_start(&head->th_timer, time,
2570 LASSERT(cli->tc_in_heap);
2571 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2572 list_add_tail(&nrq->nr_u.tbf.tr_list,
2579 * Removes request \a nrq from \a policy's list of queued requests.
2581 * \param[in] policy The policy
2582 * \param[in] nrq The request to remove
2584 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
2585 struct ptlrpc_nrs_request *nrq)
2587 struct nrs_tbf_head *head;
2588 struct nrs_tbf_client *cli;
2590 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2592 cli = container_of(nrs_request_resource(nrq),
2593 struct nrs_tbf_client, tc_res);
2594 head = container_of(nrs_request_resource(nrq)->res_parent,
2595 struct nrs_tbf_head, th_res);
2597 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
2598 list_del_init(&nrq->nr_u.tbf.tr_list);
2599 if (list_empty(&cli->tc_list)) {
2600 cfs_binheap_remove(head->th_binheap,
2602 cli->tc_in_heap = false;
2604 cfs_binheap_relocate(head->th_binheap,
2610 * Prints a debug statement right before the request \a nrq stops being
2613 * \param[in] policy The policy handling the request
2614 * \param[in] nrq The request being handled
2616 * \see ptlrpc_server_finish_request()
2617 * \see ptlrpc_nrs_req_stop_nolock()
2619 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
2620 struct ptlrpc_nrs_request *nrq)
2622 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
2625 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2627 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
2628 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
2629 nrq->nr_u.tbf.tr_sequence);
2632 #ifdef CONFIG_PROC_FS
2639 * The maximum RPC rate.
2641 #define LPROCFS_NRS_RATE_MAX 65535
2644 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
2646 struct ptlrpc_service *svc = m->private;
2649 seq_printf(m, "regular_requests:\n");
2651 * Perform two separate calls to this as only one of the NRS heads'
2652 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
2653 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
2655 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
2657 NRS_CTL_TBF_RD_RULE,
2661 * -ENOSPC means buf in the parameter m is overflow, return 0
2662 * here to let upper layer function seq_read alloc a larger
2663 * memory area and do this process again.
2665 } else if (rc == -ENOSPC) {
2669 * Ignore -ENODEV as the regular NRS head's policy may be in the
2670 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
2672 } else if (rc != -ENODEV) {
2676 if (!nrs_svc_has_hp(svc))
2679 seq_printf(m, "high_priority_requests:\n");
2680 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
2682 NRS_CTL_TBF_RD_RULE,
2686 * -ENOSPC means buf in the parameter m is overflow, return 0
2687 * here to let upper layer function seq_read alloc a larger
2688 * memory area and do this process again.
2690 } else if (rc == -ENOSPC) {
2699 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
2703 switch (cmd->u.tc_start.ts_valid_type) {
2704 case NRS_TBF_FLAG_JOBID:
2705 rc = nrs_tbf_jobid_parse(cmd, token);
2707 case NRS_TBF_FLAG_NID:
2708 rc = nrs_tbf_nid_parse(cmd, token);
2710 case NRS_TBF_FLAG_OPCODE:
2711 rc = nrs_tbf_opcode_parse(cmd, token);
2713 case NRS_TBF_FLAG_GENERIC:
2714 rc = nrs_tbf_generic_parse(cmd, token);
2723 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
2725 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2726 if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_JOBID)
2727 nrs_tbf_jobid_cmd_fini(cmd);
2728 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_NID)
2729 nrs_tbf_nid_cmd_fini(cmd);
2730 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_OPCODE)
2731 nrs_tbf_opcode_cmd_fini(cmd);
2732 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_GENERIC)
2733 nrs_tbf_generic_cmd_fini(cmd);
2737 static bool name_is_valid(const char *name)
2741 for (i = 0; i < strlen(name); i++) {
2742 if ((!isalnum(name[i])) &&
2750 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
2758 key = strsep(&val, "=");
2759 if (val == NULL || strlen(val) == 0)
2762 /* Key of the value pair */
2763 if (strcmp(key, "rate") == 0) {
2764 rc = kstrtoull(val, 10, &rate);
2768 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
2771 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2772 cmd->u.tc_start.ts_rpc_rate = rate;
2773 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2774 cmd->u.tc_change.tc_rpc_rate = rate;
2777 } else if (strcmp(key, "rank") == 0) {
2778 if (!name_is_valid(val))
2781 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2782 cmd->u.tc_start.ts_next_name = val;
2783 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2784 cmd->u.tc_change.tc_next_name = val;
2794 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
2801 while (val != NULL && strlen(val) != 0) {
2802 token = strsep(&val, " ");
2803 rc = nrs_tbf_parse_value_pair(cmd, token);
2808 switch (cmd->tc_cmd) {
2809 case NRS_CTL_TBF_START_RULE:
2810 if (cmd->u.tc_start.ts_rpc_rate == 0)
2811 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
2813 case NRS_CTL_TBF_CHANGE_RULE:
2814 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
2815 cmd->u.tc_change.tc_next_name == NULL)
2818 case NRS_CTL_TBF_STOP_RULE:
2826 static struct nrs_tbf_cmd *
2827 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
2829 static struct nrs_tbf_cmd *cmd;
2836 GOTO(out, rc = -ENOMEM);
2837 memset(cmd, 0, sizeof(*cmd));
2840 token = strsep(&val, " ");
2841 if (val == NULL || strlen(val) == 0)
2842 GOTO(out_free_cmd, rc = -EINVAL);
2844 /* Type of the command */
2845 if (strcmp(token, "start") == 0) {
2846 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
2847 cmd->u.tc_start.ts_valid_type = type_flag;
2848 } else if (strcmp(token, "stop") == 0)
2849 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
2850 else if (strcmp(token, "change") == 0)
2851 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
2853 GOTO(out_free_cmd, rc = -EINVAL);
2855 /* Name of the rule */
2856 token = strsep(&val, " ");
2857 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
2858 !name_is_valid(token))
2859 GOTO(out_free_cmd, rc = -EINVAL);
2860 cmd->tc_name = token;
2862 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2866 val = strrchr(token, '}');
2868 GOTO(out_free_cmd, rc = -EINVAL);
2874 } else if (*val == ' ') {
2878 GOTO(out_free_cmd, rc = -EINVAL);
2880 rc = nrs_tbf_id_parse(cmd, token);
2882 GOTO(out_free_cmd, rc);
2885 rc = nrs_tbf_parse_value_pairs(cmd, val);
2887 GOTO(out_cmd_fini, rc = -EINVAL);
2890 nrs_tbf_cmd_fini(cmd);
2900 * Get the TBF policy type (nid, jobid, etc) preset by
2901 * proc entry 'nrs_policies' for command buffer parsing.
2903 * \param[in] svc the PTLRPC service
2904 * \param[in] queue the NRS queue type
2906 * \retval the preset TBF policy type flag
2909 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
2914 rc = ptlrpc_nrs_policy_control(svc, queue,
2916 NRS_CTL_TBF_RD_TYPE_FLAG,
2919 type = NRS_TBF_FLAG_INVALID;
2924 extern struct nrs_core nrs_core;
2925 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
2927 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
2928 const char __user *buffer,
2929 size_t count, loff_t *off)
2931 struct seq_file *m = file->private_data;
2932 struct ptlrpc_service *svc = m->private;
2936 static struct nrs_tbf_cmd *cmd;
2937 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
2938 unsigned long length;
2941 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2942 if (kernbuf == NULL)
2943 GOTO(out, rc = -ENOMEM);
2945 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
2946 GOTO(out_free_kernbuff, rc = -EINVAL);
2948 if (copy_from_user(kernbuf, buffer, count))
2949 GOTO(out_free_kernbuff, rc = -EFAULT);
2952 token = strsep(&val, " ");
2954 GOTO(out_free_kernbuff, rc = -EINVAL);
2956 if (strcmp(token, "reg") == 0) {
2957 queue = PTLRPC_NRS_QUEUE_REG;
2958 } else if (strcmp(token, "hp") == 0) {
2959 queue = PTLRPC_NRS_QUEUE_HP;
2961 kernbuf[strlen(token)] = ' ';
2964 length = strlen(val);
2967 GOTO(out_free_kernbuff, rc = -EINVAL);
2969 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
2970 GOTO(out_free_kernbuff, rc = -ENODEV);
2971 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
2972 queue = PTLRPC_NRS_QUEUE_REG;
2974 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
2976 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
2979 * Serialize NRS core lprocfs operations with policy registration/
2982 mutex_lock(&nrs_core.nrs_mutex);
2983 rc = ptlrpc_nrs_policy_control(svc, queue,
2985 NRS_CTL_TBF_WR_RULE,
2987 mutex_unlock(&nrs_core.nrs_mutex);
2989 nrs_tbf_cmd_fini(cmd);
2992 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2994 return rc ? rc : count;
2996 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
2999 * Initializes a TBF policy's lprocfs interface for service \a svc
3001 * \param[in] svc the service
3004 * \retval != 0 error
3006 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
3008 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
3009 { .name = "nrs_tbf_rule",
3010 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
3015 if (svc->srv_procroot == NULL)
3018 return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
3022 * Cleans up a TBF policy's lprocfs interface for service \a svc
3024 * \param[in] svc the service
3026 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
3028 if (svc->srv_procroot == NULL)
3031 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
3034 #endif /* CONFIG_PROC_FS */
3037 * TBF policy operations
3039 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
3040 .op_policy_start = nrs_tbf_start,
3041 .op_policy_stop = nrs_tbf_stop,
3042 .op_policy_ctl = nrs_tbf_ctl,
3043 .op_res_get = nrs_tbf_res_get,
3044 .op_res_put = nrs_tbf_res_put,
3045 .op_req_get = nrs_tbf_req_get,
3046 .op_req_enqueue = nrs_tbf_req_add,
3047 .op_req_dequeue = nrs_tbf_req_del,
3048 .op_req_stop = nrs_tbf_req_stop,
3049 #ifdef CONFIG_PROC_FS
3050 .op_lprocfs_init = nrs_tbf_lprocfs_init,
3051 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
3056 * TBF policy configuration
3058 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
3059 .nc_name = NRS_POL_NAME_TBF,
3060 .nc_ops = &nrs_tbf_ops,
3061 .nc_compat = nrs_policy_compat_all,
3068 #endif /* HAVE_SERVER_SUPPORT */