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(const struct cfs_lstr *id, struct list_head *jobid_list)
814 struct nrs_tbf_jobid *jobid;
816 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
820 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
821 if (jobid->tj_id == NULL) {
822 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
826 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
827 list_add_tail(&jobid->tj_linkage, jobid_list);
832 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
834 struct nrs_tbf_jobid *jobid;
836 list_for_each_entry(jobid, jobid_list, tj_linkage) {
837 if (strcmp(id, jobid->tj_id) == 0)
844 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
853 INIT_LIST_HEAD(jobid_list);
855 rc = cfs_gettok(&src, ' ', &res);
860 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
865 nrs_tbf_jobid_list_free(jobid_list);
869 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
871 if (!list_empty(&cmd->u.tc_start.ts_jobids))
872 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
873 if (cmd->u.tc_start.ts_jobids_str)
874 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
875 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
878 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
881 int keylen = strlen(key);
884 rc = cfs_gettok(src, '=', &res);
885 if (rc == 0 || res.ls_len != keylen ||
886 strncmp(res.ls_str, key, keylen) != 0 ||
887 src->ls_len <= 2 || src->ls_str[0] != '{' ||
888 src->ls_str[src->ls_len - 1] != '}')
891 /* Skip '{' and '}' */
897 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
903 src.ls_len = strlen(id);
904 rc = nrs_tbf_check_id_value(&src, "jobid");
908 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
909 if (cmd->u.tc_start.ts_jobids_str == NULL)
912 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
914 /* parse jobid list */
915 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
916 strlen(cmd->u.tc_start.ts_jobids_str),
917 &cmd->u.tc_start.ts_jobids);
919 nrs_tbf_jobid_cmd_fini(cmd);
924 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
925 struct nrs_tbf_rule *rule,
926 struct nrs_tbf_cmd *start)
930 LASSERT(start->u.tc_start.ts_jobids_str);
931 OBD_ALLOC(rule->tr_jobids_str,
932 strlen(start->u.tc_start.ts_jobids_str) + 1);
933 if (rule->tr_jobids_str == NULL)
936 memcpy(rule->tr_jobids_str,
937 start->u.tc_start.ts_jobids_str,
938 strlen(start->u.tc_start.ts_jobids_str));
940 INIT_LIST_HEAD(&rule->tr_jobids);
941 if (!list_empty(&start->u.tc_start.ts_jobids)) {
942 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
943 strlen(rule->tr_jobids_str),
946 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
949 OBD_FREE(rule->tr_jobids_str,
950 strlen(start->u.tc_start.ts_jobids_str) + 1);
955 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
957 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
958 rule->tr_jobids_str, rule->tr_rpc_rate,
959 atomic_read(&rule->tr_ref) - 1);
964 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
965 struct nrs_tbf_client *cli)
967 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
970 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
972 if (!list_empty(&rule->tr_jobids))
973 nrs_tbf_jobid_list_free(&rule->tr_jobids);
974 LASSERT(rule->tr_jobids_str != NULL);
975 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
978 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
979 .o_name = NRS_TBF_TYPE_JOBID,
980 .o_startup = nrs_tbf_jobid_startup,
981 .o_cli_find = nrs_tbf_jobid_cli_find,
982 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
983 .o_cli_put = nrs_tbf_jobid_cli_put,
984 .o_cli_init = nrs_tbf_jobid_cli_init,
985 .o_rule_init = nrs_tbf_jobid_rule_init,
986 .o_rule_dump = nrs_tbf_jobid_rule_dump,
987 .o_rule_match = nrs_tbf_jobid_rule_match,
988 .o_rule_fini = nrs_tbf_jobid_rule_fini,
992 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
994 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
995 * nrs_tbf_client objects.
997 #define NRS_TBF_NID_BKT_BITS 8
998 #define NRS_TBF_NID_BITS 16
1000 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1003 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1006 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1008 lnet_nid_t *nid = (lnet_nid_t *)key;
1009 struct nrs_tbf_client *cli = hlist_entry(hnode,
1010 struct nrs_tbf_client,
1013 return *nid == cli->tc_nid;
1016 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1018 struct nrs_tbf_client *cli = hlist_entry(hnode,
1019 struct nrs_tbf_client,
1022 return &cli->tc_nid;
1025 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
1027 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1030 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1032 struct nrs_tbf_client *cli = hlist_entry(hnode,
1033 struct nrs_tbf_client,
1036 atomic_inc(&cli->tc_ref);
1039 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1041 struct nrs_tbf_client *cli = hlist_entry(hnode,
1042 struct nrs_tbf_client,
1045 atomic_dec(&cli->tc_ref);
1048 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1050 struct nrs_tbf_client *cli = hlist_entry(hnode,
1051 struct nrs_tbf_client,
1054 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1055 "Busy TBF object from client with NID %s, with %d refs\n",
1056 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1058 nrs_tbf_cli_fini(cli);
1061 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1062 .hs_hash = nrs_tbf_nid_hop_hash,
1063 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
1064 .hs_key = nrs_tbf_nid_hop_key,
1065 .hs_object = nrs_tbf_nid_hop_object,
1066 .hs_get = nrs_tbf_nid_hop_get,
1067 .hs_put = nrs_tbf_nid_hop_put,
1068 .hs_put_locked = nrs_tbf_nid_hop_put,
1069 .hs_exit = nrs_tbf_nid_hop_exit,
1072 static struct nrs_tbf_client *
1073 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1074 struct ptlrpc_request *req)
1076 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
1079 static struct nrs_tbf_client *
1080 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1081 struct nrs_tbf_client *cli)
1083 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1088 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1089 struct nrs_tbf_client *cli)
1091 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1095 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1096 struct nrs_tbf_head *head)
1098 struct nrs_tbf_cmd start;
1101 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1104 NRS_TBF_NID_BKT_BITS, 0,
1107 &nrs_tbf_nid_hash_ops,
1108 CFS_HASH_RW_BKTLOCK);
1109 if (head->th_cli_hash == NULL)
1112 memset(&start, 0, sizeof(start));
1113 start.u.tc_start.ts_nids_str = "*";
1115 start.u.tc_start.ts_rpc_rate = tbf_rate;
1116 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1117 start.tc_name = NRS_TBF_DEFAULT_RULE;
1118 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1119 rc = nrs_tbf_rule_start(policy, head, &start);
1121 cfs_hash_putref(head->th_cli_hash);
1122 head->th_cli_hash = NULL;
1129 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1130 struct ptlrpc_request *req)
1132 cli->tc_nid = req->rq_peer.nid;
1135 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1136 struct nrs_tbf_rule *rule,
1137 struct nrs_tbf_cmd *start)
1139 LASSERT(start->u.tc_start.ts_nids_str);
1140 OBD_ALLOC(rule->tr_nids_str,
1141 strlen(start->u.tc_start.ts_nids_str) + 1);
1142 if (rule->tr_nids_str == NULL)
1145 memcpy(rule->tr_nids_str,
1146 start->u.tc_start.ts_nids_str,
1147 strlen(start->u.tc_start.ts_nids_str));
1149 INIT_LIST_HEAD(&rule->tr_nids);
1150 if (!list_empty(&start->u.tc_start.ts_nids)) {
1151 if (cfs_parse_nidlist(rule->tr_nids_str,
1152 strlen(rule->tr_nids_str),
1153 &rule->tr_nids) <= 0) {
1154 CERROR("nids {%s} illegal\n",
1156 OBD_FREE(rule->tr_nids_str,
1157 strlen(start->u.tc_start.ts_nids_str) + 1);
1165 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1167 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1168 rule->tr_nids_str, rule->tr_rpc_rate,
1169 atomic_read(&rule->tr_ref) - 1);
1174 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1175 struct nrs_tbf_client *cli)
1177 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1180 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1182 if (!list_empty(&rule->tr_nids))
1183 cfs_free_nidlist(&rule->tr_nids);
1184 LASSERT(rule->tr_nids_str != NULL);
1185 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1188 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1190 if (!list_empty(&cmd->u.tc_start.ts_nids))
1191 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1192 if (cmd->u.tc_start.ts_nids_str)
1193 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1194 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1197 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1199 struct cfs_lstr src;
1203 src.ls_len = strlen(id);
1204 rc = nrs_tbf_check_id_value(&src, "nid");
1208 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1209 if (cmd->u.tc_start.ts_nids_str == NULL)
1212 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1214 /* parse NID list */
1215 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1216 strlen(cmd->u.tc_start.ts_nids_str),
1217 &cmd->u.tc_start.ts_nids) <= 0) {
1218 nrs_tbf_nid_cmd_fini(cmd);
1225 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1226 .o_name = NRS_TBF_TYPE_NID,
1227 .o_startup = nrs_tbf_nid_startup,
1228 .o_cli_find = nrs_tbf_nid_cli_find,
1229 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1230 .o_cli_put = nrs_tbf_nid_cli_put,
1231 .o_cli_init = nrs_tbf_nid_cli_init,
1232 .o_rule_init = nrs_tbf_nid_rule_init,
1233 .o_rule_dump = nrs_tbf_nid_rule_dump,
1234 .o_rule_match = nrs_tbf_nid_rule_match,
1235 .o_rule_fini = nrs_tbf_nid_rule_fini,
1238 static unsigned nrs_tbf_hop_hash(struct cfs_hash *hs, const void *key,
1241 return cfs_hash_djb2_hash(key, strlen(key), mask);
1244 static int nrs_tbf_hop_keycmp(const void *key, struct hlist_node *hnode)
1246 struct nrs_tbf_client *cli = hlist_entry(hnode,
1247 struct nrs_tbf_client,
1250 return (strcmp(cli->tc_key, key) == 0);
1253 static void *nrs_tbf_hop_key(struct hlist_node *hnode)
1255 struct nrs_tbf_client *cli = hlist_entry(hnode,
1256 struct nrs_tbf_client,
1261 static void *nrs_tbf_hop_object(struct hlist_node *hnode)
1263 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1266 static void nrs_tbf_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1268 struct nrs_tbf_client *cli = hlist_entry(hnode,
1269 struct nrs_tbf_client,
1272 atomic_inc(&cli->tc_ref);
1275 static void nrs_tbf_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1277 struct nrs_tbf_client *cli = hlist_entry(hnode,
1278 struct nrs_tbf_client,
1281 atomic_dec(&cli->tc_ref);
1284 static void nrs_tbf_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1287 struct nrs_tbf_client *cli = hlist_entry(hnode,
1288 struct nrs_tbf_client,
1291 LASSERT(atomic_read(&cli->tc_ref) == 0);
1292 nrs_tbf_cli_fini(cli);
1295 static struct cfs_hash_ops nrs_tbf_hash_ops = {
1296 .hs_hash = nrs_tbf_hop_hash,
1297 .hs_keycmp = nrs_tbf_hop_keycmp,
1298 .hs_key = nrs_tbf_hop_key,
1299 .hs_object = nrs_tbf_hop_object,
1300 .hs_get = nrs_tbf_hop_get,
1301 .hs_put = nrs_tbf_hop_put,
1302 .hs_put_locked = nrs_tbf_hop_put,
1303 .hs_exit = nrs_tbf_hop_exit,
1306 #define NRS_TBF_GENERIC_BKT_BITS 10
1307 #define NRS_TBF_GENERIC_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
1308 CFS_HASH_NO_ITEMREF | \
1312 nrs_tbf_startup(struct ptlrpc_nrs_policy *policy, struct nrs_tbf_head *head)
1314 struct nrs_tbf_cmd start;
1315 struct nrs_tbf_bucket *bkt;
1319 struct cfs_hash_bd bd;
1321 bits = nrs_tbf_jobid_hash_order();
1322 if (bits < NRS_TBF_GENERIC_BKT_BITS)
1323 bits = NRS_TBF_GENERIC_BKT_BITS;
1324 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1326 NRS_TBF_GENERIC_BKT_BITS,
1329 NRS_TBF_GENERIC_HASH_FLAGS);
1330 if (head->th_cli_hash == NULL)
1333 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
1334 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
1335 INIT_LIST_HEAD(&bkt->ntb_lru);
1338 memset(&start, 0, sizeof(start));
1339 start.u.tc_start.ts_conds_str = "*";
1341 start.u.tc_start.ts_rpc_rate = tbf_rate;
1342 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1343 start.tc_name = NRS_TBF_DEFAULT_RULE;
1344 INIT_LIST_HEAD(&start.u.tc_start.ts_conds);
1345 rc = nrs_tbf_rule_start(policy, head, &start);
1347 cfs_hash_putref(head->th_cli_hash);
1352 static struct nrs_tbf_client *
1353 nrs_tbf_cli_hash_lookup(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1356 struct hlist_node *hnode;
1357 struct nrs_tbf_client *cli;
1359 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)key);
1363 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
1364 if (!list_empty(&cli->tc_lru))
1365 list_del_init(&cli->tc_lru);
1369 static struct nrs_tbf_client *
1370 nrs_tbf_cli_find(struct nrs_tbf_head *head, struct ptlrpc_request *req)
1372 struct nrs_tbf_client *cli;
1373 struct cfs_hash *hs = head->th_cli_hash;
1374 struct cfs_hash_bd bd;
1375 char keystr[NRS_TBF_KEY_LEN] = { '\0' };
1379 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1381 jobid = NRS_TBF_JOBID_NULL;
1382 opc = lustre_msg_get_opc(req->rq_reqmsg);
1383 snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1384 libcfs_nid2str(req->rq_peer.nid), opc);
1385 LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1386 cfs_hash_bd_get_and_lock(hs, (void *)keystr, &bd, 1);
1387 cli = nrs_tbf_cli_hash_lookup(hs, &bd, keystr);
1388 cfs_hash_bd_unlock(hs, &bd, 1);
1393 static struct nrs_tbf_client *
1394 nrs_tbf_cli_findadd(struct nrs_tbf_head *head,
1395 struct nrs_tbf_client *cli)
1398 struct nrs_tbf_client *ret;
1399 struct cfs_hash *hs = head->th_cli_hash;
1400 struct cfs_hash_bd bd;
1403 cfs_hash_bd_get_and_lock(hs, (void *)key, &bd, 1);
1404 ret = nrs_tbf_cli_hash_lookup(hs, &bd, key);
1406 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
1409 cfs_hash_bd_unlock(hs, &bd, 1);
1415 nrs_tbf_cli_put(struct nrs_tbf_head *head, struct nrs_tbf_client *cli)
1417 struct cfs_hash_bd bd;
1418 struct cfs_hash *hs = head->th_cli_hash;
1419 struct nrs_tbf_bucket *bkt;
1421 struct list_head zombies;
1423 INIT_LIST_HEAD(&zombies);
1424 cfs_hash_bd_get(hs, &cli->tc_key, &bd);
1425 bkt = cfs_hash_bd_extra_get(hs, &bd);
1426 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
1428 LASSERT(list_empty(&cli->tc_lru));
1429 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
1432 * Check and purge the LRU, there is at least one client in the LRU.
1434 hw = tbf_jobid_cache_size >> (hs->hs_cur_bits - hs->hs_bkt_bits);
1435 while (cfs_hash_bd_count_get(&bd) > hw) {
1436 if (unlikely(list_empty(&bkt->ntb_lru)))
1438 cli = list_entry(bkt->ntb_lru.next,
1439 struct nrs_tbf_client,
1441 LASSERT(atomic_read(&cli->tc_ref) == 0);
1442 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
1443 list_move(&cli->tc_lru, &zombies);
1445 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
1447 while (!list_empty(&zombies)) {
1448 cli = container_of0(zombies.next,
1449 struct nrs_tbf_client, tc_lru);
1450 list_del_init(&cli->tc_lru);
1451 nrs_tbf_cli_fini(cli);
1456 nrs_tbf_generic_cli_init(struct nrs_tbf_client *cli,
1457 struct ptlrpc_request *req)
1459 char keystr[NRS_TBF_KEY_LEN];
1463 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1465 jobid = NRS_TBF_JOBID_NULL;
1466 opc = lustre_msg_get_opc(req->rq_reqmsg);
1467 snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1468 libcfs_nid2str(req->rq_peer.nid), opc);
1470 LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1471 INIT_LIST_HEAD(&cli->tc_lru);
1472 memcpy(cli->tc_key, keystr, strlen(keystr));
1473 memcpy(cli->tc_jobid, jobid, strlen(jobid));
1474 cli->tc_nid = req->rq_peer.nid;
1475 cli->tc_opcode = opc;
1479 nrs_tbf_expression_free(struct nrs_tbf_expression *expr)
1481 LASSERT(expr->te_field >= NRS_TBF_FIELD_NID &&
1482 expr->te_field < NRS_TBF_FIELD_MAX);
1483 switch (expr->te_field) {
1484 case NRS_TBF_FIELD_NID:
1485 cfs_free_nidlist(&expr->te_cond);
1487 case NRS_TBF_FIELD_JOBID:
1488 nrs_tbf_jobid_list_free(&expr->te_cond);
1490 case NRS_TBF_FIELD_OPCODE:
1491 CFS_FREE_BITMAP(expr->te_opcodes);
1500 nrs_tbf_conjunction_free(struct nrs_tbf_conjunction *conjunction)
1502 struct nrs_tbf_expression *expression;
1503 struct nrs_tbf_expression *n;
1505 LASSERT(list_empty(&conjunction->tc_linkage));
1506 list_for_each_entry_safe(expression, n,
1507 &conjunction->tc_expressions,
1509 list_del_init(&expression->te_linkage);
1510 nrs_tbf_expression_free(expression);
1512 OBD_FREE_PTR(conjunction);
1516 nrs_tbf_conds_free(struct list_head *cond_list)
1518 struct nrs_tbf_conjunction *conjunction;
1519 struct nrs_tbf_conjunction *n;
1521 list_for_each_entry_safe(conjunction, n, cond_list, tc_linkage) {
1522 list_del_init(&conjunction->tc_linkage);
1523 nrs_tbf_conjunction_free(conjunction);
1528 nrs_tbf_generic_cmd_fini(struct nrs_tbf_cmd *cmd)
1530 if (!list_empty(&cmd->u.tc_start.ts_conds))
1531 nrs_tbf_conds_free(&cmd->u.tc_start.ts_conds);
1532 if (cmd->u.tc_start.ts_conds_str)
1533 OBD_FREE(cmd->u.tc_start.ts_conds_str,
1534 strlen(cmd->u.tc_start.ts_conds_str) + 1);
1537 #define NRS_TBF_DISJUNCTION_DELIM (',')
1538 #define NRS_TBF_CONJUNCTION_DELIM ('&')
1539 #define NRS_TBF_EXPRESSION_DELIM ('=')
1542 nrs_tbf_check_field(struct cfs_lstr *field, char *str)
1544 int len = strlen(str);
1546 return (field->ls_len == len &&
1547 strncmp(field->ls_str, str, len) == 0);
1551 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr);
1554 nrs_tbf_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
1556 struct nrs_tbf_expression *expr;
1557 struct cfs_lstr field;
1560 OBD_ALLOC(expr, sizeof(struct nrs_tbf_expression));
1564 rc = cfs_gettok(src, NRS_TBF_EXPRESSION_DELIM, &field);
1565 if (rc == 0 || src->ls_len <= 2 || src->ls_str[0] != '{' ||
1566 src->ls_str[src->ls_len - 1] != '}')
1567 GOTO(out, rc = -EINVAL);
1569 /* Skip '{' and '}' */
1573 if (nrs_tbf_check_field(&field, "nid")) {
1574 if (cfs_parse_nidlist(src->ls_str,
1576 &expr->te_cond) <= 0)
1577 GOTO(out, rc = -EINVAL);
1578 expr->te_field = NRS_TBF_FIELD_NID;
1579 } else if (nrs_tbf_check_field(&field, "jobid")) {
1580 if (nrs_tbf_jobid_list_parse(src->ls_str,
1582 &expr->te_cond) < 0)
1583 GOTO(out, rc = -EINVAL);
1584 expr->te_field = NRS_TBF_FIELD_JOBID;
1585 } else if (nrs_tbf_check_field(&field, "opcode")) {
1586 if (nrs_tbf_opcode_list_parse(src->ls_str,
1588 &expr->te_opcodes) < 0)
1589 GOTO(out, rc = -EINVAL);
1590 expr->te_field = NRS_TBF_FIELD_OPCODE;
1592 GOTO(out, rc = -EINVAL);
1594 list_add_tail(&expr->te_linkage, cond_list);
1602 nrs_tbf_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
1604 struct nrs_tbf_conjunction *conjunction;
1605 struct cfs_lstr expr;
1608 OBD_ALLOC(conjunction, sizeof(struct nrs_tbf_conjunction));
1609 if (conjunction == NULL)
1612 INIT_LIST_HEAD(&conjunction->tc_expressions);
1613 list_add_tail(&conjunction->tc_linkage, cond_list);
1615 while (src->ls_str) {
1616 rc = cfs_gettok(src, NRS_TBF_CONJUNCTION_DELIM, &expr);
1621 rc = nrs_tbf_expression_parse(&expr,
1622 &conjunction->tc_expressions);
1630 nrs_tbf_conds_parse(char *str, int len, struct list_head *cond_list)
1632 struct cfs_lstr src;
1633 struct cfs_lstr res;
1638 INIT_LIST_HEAD(cond_list);
1639 while (src.ls_str) {
1640 rc = cfs_gettok(&src, NRS_TBF_DISJUNCTION_DELIM, &res);
1645 rc = nrs_tbf_conjunction_parse(&res, cond_list);
1653 nrs_tbf_generic_parse(struct nrs_tbf_cmd *cmd, const char *id)
1657 OBD_ALLOC(cmd->u.tc_start.ts_conds_str, strlen(id) + 1);
1658 if (cmd->u.tc_start.ts_conds_str == NULL)
1661 memcpy(cmd->u.tc_start.ts_conds_str, id, strlen(id));
1663 /* Parse hybird NID and JOBID conditions */
1664 rc = nrs_tbf_conds_parse(cmd->u.tc_start.ts_conds_str,
1665 strlen(cmd->u.tc_start.ts_conds_str),
1666 &cmd->u.tc_start.ts_conds);
1668 nrs_tbf_generic_cmd_fini(cmd);
1674 nrs_tbf_expression_match(struct nrs_tbf_expression *expr,
1675 struct nrs_tbf_rule *rule,
1676 struct nrs_tbf_client *cli)
1678 switch (expr->te_field) {
1679 case NRS_TBF_FIELD_NID:
1680 return cfs_match_nid(cli->tc_nid, &expr->te_cond);
1681 case NRS_TBF_FIELD_JOBID:
1682 return nrs_tbf_jobid_list_match(&expr->te_cond, cli->tc_jobid);
1683 case NRS_TBF_FIELD_OPCODE:
1684 return cfs_bitmap_check(expr->te_opcodes, cli->tc_opcode);
1691 nrs_tbf_conjunction_match(struct nrs_tbf_conjunction *conjunction,
1692 struct nrs_tbf_rule *rule,
1693 struct nrs_tbf_client *cli)
1695 struct nrs_tbf_expression *expr;
1698 list_for_each_entry(expr, &conjunction->tc_expressions, te_linkage) {
1699 matched = nrs_tbf_expression_match(expr, rule, cli);
1708 nrs_tbf_cond_match(struct nrs_tbf_rule *rule, struct nrs_tbf_client *cli)
1710 struct nrs_tbf_conjunction *conjunction;
1713 list_for_each_entry(conjunction, &rule->tr_conds, tc_linkage) {
1714 matched = nrs_tbf_conjunction_match(conjunction, rule, cli);
1723 nrs_tbf_generic_rule_fini(struct nrs_tbf_rule *rule)
1725 if (!list_empty(&rule->tr_conds))
1726 nrs_tbf_conds_free(&rule->tr_conds);
1727 LASSERT(rule->tr_conds_str != NULL);
1728 OBD_FREE(rule->tr_conds_str, strlen(rule->tr_conds_str) + 1);
1732 nrs_tbf_rule_init(struct ptlrpc_nrs_policy *policy,
1733 struct nrs_tbf_rule *rule, struct nrs_tbf_cmd *start)
1737 LASSERT(start->u.tc_start.ts_conds_str);
1738 OBD_ALLOC(rule->tr_conds_str,
1739 strlen(start->u.tc_start.ts_conds_str) + 1);
1740 if (rule->tr_conds_str == NULL)
1743 memcpy(rule->tr_conds_str,
1744 start->u.tc_start.ts_conds_str,
1745 strlen(start->u.tc_start.ts_conds_str));
1747 INIT_LIST_HEAD(&rule->tr_conds);
1748 if (!list_empty(&start->u.tc_start.ts_conds)) {
1749 rc = nrs_tbf_conds_parse(rule->tr_conds_str,
1750 strlen(rule->tr_conds_str),
1754 nrs_tbf_generic_rule_fini(rule);
1760 nrs_tbf_generic_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1762 seq_printf(m, "%s %s %llu, ref %d\n", rule->tr_name,
1763 rule->tr_conds_str, rule->tr_rpc_rate,
1764 atomic_read(&rule->tr_ref) - 1);
1769 nrs_tbf_generic_rule_match(struct nrs_tbf_rule *rule,
1770 struct nrs_tbf_client *cli)
1772 return nrs_tbf_cond_match(rule, cli);
1775 static struct nrs_tbf_ops nrs_tbf_generic_ops = {
1776 .o_name = NRS_TBF_TYPE_GENERIC,
1777 .o_startup = nrs_tbf_startup,
1778 .o_cli_find = nrs_tbf_cli_find,
1779 .o_cli_findadd = nrs_tbf_cli_findadd,
1780 .o_cli_put = nrs_tbf_cli_put,
1781 .o_cli_init = nrs_tbf_generic_cli_init,
1782 .o_rule_init = nrs_tbf_rule_init,
1783 .o_rule_dump = nrs_tbf_generic_rule_dump,
1784 .o_rule_match = nrs_tbf_generic_rule_match,
1785 .o_rule_fini = nrs_tbf_generic_rule_fini,
1788 static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
1790 if (rule->tr_opcodes != NULL)
1791 CFS_FREE_BITMAP(rule->tr_opcodes);
1793 LASSERT(rule->tr_opcodes_str != NULL);
1794 OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
1797 static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
1800 return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
1803 static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
1805 const __u32 *opc = key;
1806 struct nrs_tbf_client *cli = hlist_entry(hnode,
1807 struct nrs_tbf_client,
1810 return *opc == cli->tc_opcode;
1813 static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
1815 struct nrs_tbf_client *cli = hlist_entry(hnode,
1816 struct nrs_tbf_client,
1819 return &cli->tc_opcode;
1822 static void *nrs_tbf_opcode_hop_object(struct hlist_node *hnode)
1824 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1827 static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
1828 struct hlist_node *hnode)
1830 struct nrs_tbf_client *cli = hlist_entry(hnode,
1831 struct nrs_tbf_client,
1834 atomic_inc(&cli->tc_ref);
1837 static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
1838 struct hlist_node *hnode)
1840 struct nrs_tbf_client *cli = hlist_entry(hnode,
1841 struct nrs_tbf_client,
1844 atomic_dec(&cli->tc_ref);
1847 static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
1848 struct hlist_node *hnode)
1850 struct nrs_tbf_client *cli = hlist_entry(hnode,
1851 struct nrs_tbf_client,
1854 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1855 "Busy TBF object from client with opcode %s, with %d refs\n",
1856 ll_opcode2str(cli->tc_opcode),
1857 atomic_read(&cli->tc_ref));
1859 nrs_tbf_cli_fini(cli);
1861 static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
1862 .hs_hash = nrs_tbf_opcode_hop_hash,
1863 .hs_keycmp = nrs_tbf_opcode_hop_keycmp,
1864 .hs_key = nrs_tbf_opcode_hop_key,
1865 .hs_object = nrs_tbf_opcode_hop_object,
1866 .hs_get = nrs_tbf_opcode_hop_get,
1867 .hs_put = nrs_tbf_opcode_hop_put,
1868 .hs_put_locked = nrs_tbf_opcode_hop_put,
1869 .hs_exit = nrs_tbf_opcode_hop_exit,
1873 nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
1874 struct nrs_tbf_head *head)
1876 struct nrs_tbf_cmd start = { 0 };
1879 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1882 NRS_TBF_NID_BKT_BITS, 0,
1885 &nrs_tbf_opcode_hash_ops,
1886 CFS_HASH_RW_BKTLOCK);
1887 if (head->th_cli_hash == NULL)
1890 start.u.tc_start.ts_opcodes = NULL;
1891 start.u.tc_start.ts_opcodes_str = "*";
1893 start.u.tc_start.ts_rpc_rate = tbf_rate;
1894 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1895 start.tc_name = NRS_TBF_DEFAULT_RULE;
1896 rc = nrs_tbf_rule_start(policy, head, &start);
1901 static struct nrs_tbf_client *
1902 nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
1903 struct ptlrpc_request *req)
1907 opc = lustre_msg_get_opc(req->rq_reqmsg);
1908 return cfs_hash_lookup(head->th_cli_hash, &opc);
1911 static struct nrs_tbf_client *
1912 nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
1913 struct nrs_tbf_client *cli)
1915 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
1920 nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
1921 struct ptlrpc_request *req)
1923 cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
1926 #define MAX_OPCODE_LEN 32
1928 nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, struct cfs_bitmap *opcodes)
1931 char opcode_str[MAX_OPCODE_LEN];
1933 if (id->ls_len + 1 > MAX_OPCODE_LEN)
1936 memcpy(opcode_str, id->ls_str, id->ls_len);
1937 opcode_str[id->ls_len] = '\0';
1939 op = ll_str2opcode(opcode_str);
1943 cfs_bitmap_set(opcodes, op);
1948 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr)
1950 struct cfs_bitmap *opcodes;
1951 struct cfs_lstr src;
1952 struct cfs_lstr res;
1956 opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
1957 if (opcodes == NULL)
1962 while (src.ls_str) {
1963 rc = cfs_gettok(&src, ' ', &res);
1968 rc = nrs_tbf_opcode_set_bit(&res, opcodes);
1974 *bitmaptr = opcodes;
1976 CFS_FREE_BITMAP(opcodes);
1981 static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
1983 if (cmd->u.tc_start.ts_opcodes)
1984 CFS_FREE_BITMAP(cmd->u.tc_start.ts_opcodes);
1986 if (cmd->u.tc_start.ts_opcodes_str)
1987 OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
1988 strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
1992 static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
1994 struct cfs_lstr src;
1998 src.ls_len = strlen(id);
1999 rc = nrs_tbf_check_id_value(&src, "opcode");
2003 OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
2004 if (cmd->u.tc_start.ts_opcodes_str == NULL)
2007 memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
2009 /* parse opcode list */
2010 rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
2011 strlen(cmd->u.tc_start.ts_opcodes_str),
2012 &cmd->u.tc_start.ts_opcodes);
2014 nrs_tbf_opcode_cmd_fini(cmd);
2020 nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
2021 struct nrs_tbf_client *cli)
2023 if (rule->tr_opcodes == NULL)
2026 return cfs_bitmap_check(rule->tr_opcodes, cli->tc_opcode);
2029 static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
2030 struct nrs_tbf_rule *rule,
2031 struct nrs_tbf_cmd *start)
2035 LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
2036 OBD_ALLOC(rule->tr_opcodes_str,
2037 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2038 if (rule->tr_opcodes_str == NULL)
2041 strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
2042 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2044 /* Default rule '*' */
2045 if (start->u.tc_start.ts_opcodes == NULL)
2048 rc = nrs_tbf_opcode_list_parse(rule->tr_opcodes_str,
2049 strlen(rule->tr_opcodes_str),
2052 OBD_FREE(rule->tr_opcodes_str,
2053 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2059 nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2061 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2062 rule->tr_opcodes_str, rule->tr_rpc_rate,
2063 atomic_read(&rule->tr_ref) - 1);
2068 struct nrs_tbf_ops nrs_tbf_opcode_ops = {
2069 .o_name = NRS_TBF_TYPE_OPCODE,
2070 .o_startup = nrs_tbf_opcode_startup,
2071 .o_cli_find = nrs_tbf_opcode_cli_find,
2072 .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
2073 .o_cli_put = nrs_tbf_nid_cli_put,
2074 .o_cli_init = nrs_tbf_opcode_cli_init,
2075 .o_rule_init = nrs_tbf_opcode_rule_init,
2076 .o_rule_dump = nrs_tbf_opcode_rule_dump,
2077 .o_rule_match = nrs_tbf_opcode_rule_match,
2078 .o_rule_fini = nrs_tbf_opcode_rule_fini,
2081 static struct nrs_tbf_type nrs_tbf_types[] = {
2083 .ntt_name = NRS_TBF_TYPE_JOBID,
2084 .ntt_flag = NRS_TBF_FLAG_JOBID,
2085 .ntt_ops = &nrs_tbf_jobid_ops,
2088 .ntt_name = NRS_TBF_TYPE_NID,
2089 .ntt_flag = NRS_TBF_FLAG_NID,
2090 .ntt_ops = &nrs_tbf_nid_ops,
2093 .ntt_name = NRS_TBF_TYPE_OPCODE,
2094 .ntt_flag = NRS_TBF_FLAG_OPCODE,
2095 .ntt_ops = &nrs_tbf_opcode_ops,
2098 .ntt_name = NRS_TBF_TYPE_GENERIC,
2099 .ntt_flag = NRS_TBF_FLAG_GENERIC,
2100 .ntt_ops = &nrs_tbf_generic_ops,
2105 * Is called before the policy transitions into
2106 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
2107 * policy-specific private data structure.
2109 * \param[in] policy The policy to start
2111 * \retval -ENOMEM OOM error
2114 * \see nrs_policy_register()
2115 * \see nrs_policy_ctl()
2117 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
2119 struct nrs_tbf_head *head;
2120 struct nrs_tbf_ops *ops;
2128 name = NRS_TBF_TYPE_GENERIC;
2129 else if (strlen(arg) < NRS_TBF_TYPE_MAX_LEN)
2132 GOTO(out, rc = -EINVAL);
2134 for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
2135 if (strcmp(name, nrs_tbf_types[i].ntt_name) == 0) {
2136 ops = nrs_tbf_types[i].ntt_ops;
2137 type = nrs_tbf_types[i].ntt_flag;
2143 GOTO(out, rc = -ENOTSUPP);
2145 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
2147 GOTO(out, rc = -ENOMEM);
2149 memcpy(head->th_type, name, strlen(name));
2150 head->th_type[strlen(name)] = '\0';
2152 head->th_type_flag = type;
2154 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
2155 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
2156 nrs_pol2cptab(policy),
2157 nrs_pol2cptid(policy));
2158 if (head->th_binheap == NULL)
2159 GOTO(out_free_head, rc = -ENOMEM);
2161 atomic_set(&head->th_rule_sequence, 0);
2162 spin_lock_init(&head->th_rule_lock);
2163 INIT_LIST_HEAD(&head->th_list);
2164 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2165 head->th_timer.function = nrs_tbf_timer_cb;
2166 rc = head->th_ops->o_startup(policy, head);
2168 GOTO(out_free_heap, rc);
2170 policy->pol_private = head;
2173 cfs_binheap_destroy(head->th_binheap);
2181 * Is called before the policy transitions into
2182 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
2183 * private data structure.
2185 * \param[in] policy The policy to stop
2187 * \see nrs_policy_stop0()
2189 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
2191 struct nrs_tbf_head *head = policy->pol_private;
2192 struct ptlrpc_nrs *nrs = policy->pol_nrs;
2193 struct nrs_tbf_rule *rule, *n;
2195 LASSERT(head != NULL);
2196 LASSERT(head->th_cli_hash != NULL);
2197 hrtimer_cancel(&head->th_timer);
2198 /* Should cleanup hash first before free rules */
2199 cfs_hash_putref(head->th_cli_hash);
2200 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
2201 list_del_init(&rule->tr_linkage);
2202 nrs_tbf_rule_put(rule);
2204 LASSERT(list_empty(&head->th_list));
2205 LASSERT(head->th_binheap != NULL);
2206 LASSERT(cfs_binheap_is_empty(head->th_binheap));
2207 cfs_binheap_destroy(head->th_binheap);
2209 nrs->nrs_throttling = 0;
2210 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
2214 * Performs a policy-specific ctl function on TBF policy instances; similar
2217 * \param[in] policy the policy instance
2218 * \param[in] opc the opcode
2219 * \param[in,out] arg used for passing parameters and information
2221 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2222 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2224 * \retval 0 operation carried out successfully
2227 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
2228 enum ptlrpc_nrs_ctl opc,
2234 assert_spin_locked(&policy->pol_nrs->nrs_lock);
2236 switch ((enum nrs_ctl_tbf)opc) {
2241 * Read RPC rate size of a policy instance.
2243 case NRS_CTL_TBF_RD_RULE: {
2244 struct nrs_tbf_head *head = policy->pol_private;
2245 struct seq_file *m = (struct seq_file *) arg;
2246 struct ptlrpc_service_part *svcpt;
2248 svcpt = policy->pol_nrs->nrs_svcpt;
2249 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
2251 rc = nrs_tbf_rule_dump_all(head, m);
2256 * Write RPC rate of a policy instance.
2258 case NRS_CTL_TBF_WR_RULE: {
2259 struct nrs_tbf_head *head = policy->pol_private;
2260 struct nrs_tbf_cmd *cmd;
2262 cmd = (struct nrs_tbf_cmd *)arg;
2263 rc = nrs_tbf_command(policy,
2269 * Read the TBF policy type of a policy instance.
2271 case NRS_CTL_TBF_RD_TYPE_FLAG: {
2272 struct nrs_tbf_head *head = policy->pol_private;
2274 *(__u32 *)arg = head->th_type_flag;
2283 * Is called for obtaining a TBF policy resource.
2285 * \param[in] policy The policy on which the request is being asked for
2286 * \param[in] nrq The request for which resources are being taken
2287 * \param[in] parent Parent resource, unused in this policy
2288 * \param[out] resp Resources references are placed in this array
2289 * \param[in] moving_req Signifies limited caller context; unused in this
2293 * \see nrs_resource_get_safe()
2295 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
2296 struct ptlrpc_nrs_request *nrq,
2297 const struct ptlrpc_nrs_resource *parent,
2298 struct ptlrpc_nrs_resource **resp,
2301 struct nrs_tbf_head *head;
2302 struct nrs_tbf_client *cli;
2303 struct nrs_tbf_client *tmp;
2304 struct ptlrpc_request *req;
2306 if (parent == NULL) {
2307 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
2311 head = container_of(parent, struct nrs_tbf_head, th_res);
2312 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
2313 cli = head->th_ops->o_cli_find(head, req);
2315 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2316 LASSERT(cli->tc_rule);
2317 if (cli->tc_rule_sequence !=
2318 atomic_read(&head->th_rule_sequence) ||
2319 cli->tc_rule->tr_flags & NTRS_STOPPING) {
2320 struct nrs_tbf_rule *rule;
2322 rule = nrs_tbf_rule_match(head, cli);
2323 if (rule != cli->tc_rule)
2324 nrs_tbf_cli_reset(head, rule, cli);
2326 nrs_tbf_rule_put(rule);
2327 } else if (cli->tc_rule_generation !=
2328 cli->tc_rule->tr_generation) {
2329 nrs_tbf_cli_reset_value(head, cli);
2331 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2335 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
2336 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
2340 nrs_tbf_cli_init(head, cli, req);
2341 tmp = head->th_ops->o_cli_findadd(head, cli);
2343 atomic_dec(&cli->tc_ref);
2344 nrs_tbf_cli_fini(cli);
2348 *resp = &cli->tc_res;
2354 * Called when releasing references to the resource hierachy obtained for a
2355 * request for scheduling using the TBF policy.
2357 * \param[in] policy the policy the resource belongs to
2358 * \param[in] res the resource to be released
2360 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
2361 const struct ptlrpc_nrs_resource *res)
2363 struct nrs_tbf_head *head;
2364 struct nrs_tbf_client *cli;
2367 * Do nothing for freeing parent, nrs_tbf_net resources
2369 if (res->res_parent == NULL)
2372 cli = container_of(res, struct nrs_tbf_client, tc_res);
2373 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
2375 head->th_ops->o_cli_put(head, cli);
2379 * Called when getting a request from the TBF policy for handling, or just
2380 * peeking; removes the request from the policy when it is to be handled.
2382 * \param[in] policy The policy
2383 * \param[in] peek When set, signifies that we just want to examine the
2384 * request, and not handle it, so the request is not removed
2386 * \param[in] force Force the policy to return a request; unused in this
2389 * \retval The request to be handled; this is the next request in the TBF
2392 * \see ptlrpc_nrs_req_get_nolock()
2393 * \see nrs_request_get()
2396 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
2397 bool peek, bool force)
2399 struct nrs_tbf_head *head = policy->pol_private;
2400 struct ptlrpc_nrs_request *nrq = NULL;
2401 struct nrs_tbf_client *cli;
2402 struct cfs_binheap_node *node;
2404 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2406 if (!peek && policy->pol_nrs->nrs_throttling)
2409 node = cfs_binheap_root(head->th_binheap);
2410 if (unlikely(node == NULL))
2413 cli = container_of(node, struct nrs_tbf_client, tc_node);
2414 LASSERT(cli->tc_in_heap);
2416 nrq = list_entry(cli->tc_list.next,
2417 struct ptlrpc_nrs_request,
2420 __u64 now = ktime_to_ns(ktime_get());
2425 deadline = cli->tc_check_time +
2427 LASSERT(now >= cli->tc_check_time);
2428 passed = now - cli->tc_check_time;
2429 ntoken = passed * cli->tc_rpc_rate;
2430 do_div(ntoken, NSEC_PER_SEC);
2431 ntoken += cli->tc_ntoken;
2432 if (ntoken > cli->tc_depth)
2433 ntoken = cli->tc_depth;
2435 struct ptlrpc_request *req;
2436 nrq = list_entry(cli->tc_list.next,
2437 struct ptlrpc_nrs_request,
2439 req = container_of(nrq,
2440 struct ptlrpc_request,
2443 cli->tc_ntoken = ntoken;
2444 cli->tc_check_time = now;
2445 list_del_init(&nrq->nr_u.tbf.tr_list);
2446 if (list_empty(&cli->tc_list)) {
2447 cfs_binheap_remove(head->th_binheap,
2449 cli->tc_in_heap = false;
2451 cfs_binheap_relocate(head->th_binheap,
2455 "NRS start %s request from %s, "
2457 policy->pol_desc->pd_name,
2458 libcfs_id2str(req->rq_peer),
2459 nrq->nr_u.tbf.tr_sequence);
2463 policy->pol_nrs->nrs_throttling = 1;
2464 head->th_deadline = deadline;
2465 time = ktime_set(0, 0);
2466 time = ktime_add_ns(time, deadline);
2467 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
2475 * Adds request \a nrq to \a policy's list of queued requests
2477 * \param[in] policy The policy
2478 * \param[in] nrq The request to add
2480 * \retval 0 success; nrs_request_enqueue() assumes this function will always
2483 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
2484 struct ptlrpc_nrs_request *nrq)
2486 struct nrs_tbf_head *head;
2487 struct nrs_tbf_client *cli;
2490 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2492 cli = container_of(nrs_request_resource(nrq),
2493 struct nrs_tbf_client, tc_res);
2494 head = container_of(nrs_request_resource(nrq)->res_parent,
2495 struct nrs_tbf_head, th_res);
2496 if (list_empty(&cli->tc_list)) {
2497 LASSERT(!cli->tc_in_heap);
2498 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
2500 cli->tc_in_heap = true;
2501 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2502 list_add_tail(&nrq->nr_u.tbf.tr_list,
2504 if (policy->pol_nrs->nrs_throttling) {
2505 __u64 deadline = cli->tc_check_time +
2507 if ((head->th_deadline > deadline) &&
2508 (hrtimer_try_to_cancel(&head->th_timer)
2511 head->th_deadline = deadline;
2512 time = ktime_set(0, 0);
2513 time = ktime_add_ns(time, deadline);
2514 hrtimer_start(&head->th_timer, time,
2520 LASSERT(cli->tc_in_heap);
2521 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2522 list_add_tail(&nrq->nr_u.tbf.tr_list,
2529 * Removes request \a nrq from \a policy's list of queued requests.
2531 * \param[in] policy The policy
2532 * \param[in] nrq The request to remove
2534 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
2535 struct ptlrpc_nrs_request *nrq)
2537 struct nrs_tbf_head *head;
2538 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);
2547 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
2548 list_del_init(&nrq->nr_u.tbf.tr_list);
2549 if (list_empty(&cli->tc_list)) {
2550 cfs_binheap_remove(head->th_binheap,
2552 cli->tc_in_heap = false;
2554 cfs_binheap_relocate(head->th_binheap,
2560 * Prints a debug statement right before the request \a nrq stops being
2563 * \param[in] policy The policy handling the request
2564 * \param[in] nrq The request being handled
2566 * \see ptlrpc_server_finish_request()
2567 * \see ptlrpc_nrs_req_stop_nolock()
2569 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
2570 struct ptlrpc_nrs_request *nrq)
2572 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
2575 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2577 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
2578 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
2579 nrq->nr_u.tbf.tr_sequence);
2582 #ifdef CONFIG_PROC_FS
2589 * The maximum RPC rate.
2591 #define LPROCFS_NRS_RATE_MAX 65535
2594 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
2596 struct ptlrpc_service *svc = m->private;
2599 seq_printf(m, "regular_requests:\n");
2601 * Perform two separate calls to this as only one of the NRS heads'
2602 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
2603 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
2605 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
2607 NRS_CTL_TBF_RD_RULE,
2611 * -ENOSPC means buf in the parameter m is overflow, return 0
2612 * here to let upper layer function seq_read alloc a larger
2613 * memory area and do this process again.
2615 } else if (rc == -ENOSPC) {
2619 * Ignore -ENODEV as the regular NRS head's policy may be in the
2620 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
2622 } else if (rc != -ENODEV) {
2626 if (!nrs_svc_has_hp(svc))
2629 seq_printf(m, "high_priority_requests:\n");
2630 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
2632 NRS_CTL_TBF_RD_RULE,
2636 * -ENOSPC means buf in the parameter m is overflow, return 0
2637 * here to let upper layer function seq_read alloc a larger
2638 * memory area and do this process again.
2640 } else if (rc == -ENOSPC) {
2649 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
2653 switch (cmd->u.tc_start.ts_valid_type) {
2654 case NRS_TBF_FLAG_JOBID:
2655 rc = nrs_tbf_jobid_parse(cmd, token);
2657 case NRS_TBF_FLAG_NID:
2658 rc = nrs_tbf_nid_parse(cmd, token);
2660 case NRS_TBF_FLAG_OPCODE:
2661 rc = nrs_tbf_opcode_parse(cmd, token);
2663 case NRS_TBF_FLAG_GENERIC:
2664 rc = nrs_tbf_generic_parse(cmd, token);
2673 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
2675 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2676 if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_JOBID)
2677 nrs_tbf_jobid_cmd_fini(cmd);
2678 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_NID)
2679 nrs_tbf_nid_cmd_fini(cmd);
2680 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_OPCODE)
2681 nrs_tbf_opcode_cmd_fini(cmd);
2682 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_GENERIC)
2683 nrs_tbf_generic_cmd_fini(cmd);
2687 static bool name_is_valid(const char *name)
2691 for (i = 0; i < strlen(name); i++) {
2692 if ((!isalnum(name[i])) &&
2700 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
2708 key = strsep(&val, "=");
2709 if (val == NULL || strlen(val) == 0)
2712 /* Key of the value pair */
2713 if (strcmp(key, "rate") == 0) {
2714 rc = kstrtoull(val, 10, &rate);
2718 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
2721 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2722 cmd->u.tc_start.ts_rpc_rate = rate;
2723 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2724 cmd->u.tc_change.tc_rpc_rate = rate;
2727 } else if (strcmp(key, "rank") == 0) {
2728 if (!name_is_valid(val))
2731 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2732 cmd->u.tc_start.ts_next_name = val;
2733 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2734 cmd->u.tc_change.tc_next_name = val;
2744 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
2751 while (val != NULL && strlen(val) != 0) {
2752 token = strsep(&val, " ");
2753 rc = nrs_tbf_parse_value_pair(cmd, token);
2758 switch (cmd->tc_cmd) {
2759 case NRS_CTL_TBF_START_RULE:
2760 if (cmd->u.tc_start.ts_rpc_rate == 0)
2761 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
2763 case NRS_CTL_TBF_CHANGE_RULE:
2764 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
2765 cmd->u.tc_change.tc_next_name == NULL)
2768 case NRS_CTL_TBF_STOP_RULE:
2776 static struct nrs_tbf_cmd *
2777 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
2779 static struct nrs_tbf_cmd *cmd;
2786 GOTO(out, rc = -ENOMEM);
2787 memset(cmd, 0, sizeof(*cmd));
2790 token = strsep(&val, " ");
2791 if (val == NULL || strlen(val) == 0)
2792 GOTO(out_free_cmd, rc = -EINVAL);
2794 /* Type of the command */
2795 if (strcmp(token, "start") == 0) {
2796 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
2797 cmd->u.tc_start.ts_valid_type = type_flag;
2798 } else if (strcmp(token, "stop") == 0)
2799 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
2800 else if (strcmp(token, "change") == 0)
2801 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
2803 GOTO(out_free_cmd, rc = -EINVAL);
2805 /* Name of the rule */
2806 token = strsep(&val, " ");
2807 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
2808 !name_is_valid(token))
2809 GOTO(out_free_cmd, rc = -EINVAL);
2810 cmd->tc_name = token;
2812 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2816 val = strrchr(token, '}');
2818 GOTO(out_free_cmd, rc = -EINVAL);
2824 } else if (*val == ' ') {
2828 GOTO(out_free_cmd, rc = -EINVAL);
2830 rc = nrs_tbf_id_parse(cmd, token);
2832 GOTO(out_free_cmd, rc);
2835 rc = nrs_tbf_parse_value_pairs(cmd, val);
2837 GOTO(out_cmd_fini, rc = -EINVAL);
2840 nrs_tbf_cmd_fini(cmd);
2850 * Get the TBF policy type (nid, jobid, etc) preset by
2851 * proc entry 'nrs_policies' for command buffer parsing.
2853 * \param[in] svc the PTLRPC service
2854 * \param[in] queue the NRS queue type
2856 * \retval the preset TBF policy type flag
2859 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
2864 rc = ptlrpc_nrs_policy_control(svc, queue,
2866 NRS_CTL_TBF_RD_TYPE_FLAG,
2869 type = NRS_TBF_FLAG_INVALID;
2874 extern struct nrs_core nrs_core;
2875 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
2877 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
2878 const char __user *buffer,
2879 size_t count, loff_t *off)
2881 struct seq_file *m = file->private_data;
2882 struct ptlrpc_service *svc = m->private;
2886 static struct nrs_tbf_cmd *cmd;
2887 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
2888 unsigned long length;
2891 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2892 if (kernbuf == NULL)
2893 GOTO(out, rc = -ENOMEM);
2895 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
2896 GOTO(out_free_kernbuff, rc = -EINVAL);
2898 if (copy_from_user(kernbuf, buffer, count))
2899 GOTO(out_free_kernbuff, rc = -EFAULT);
2902 token = strsep(&val, " ");
2904 GOTO(out_free_kernbuff, rc = -EINVAL);
2906 if (strcmp(token, "reg") == 0) {
2907 queue = PTLRPC_NRS_QUEUE_REG;
2908 } else if (strcmp(token, "hp") == 0) {
2909 queue = PTLRPC_NRS_QUEUE_HP;
2911 kernbuf[strlen(token)] = ' ';
2914 length = strlen(val);
2917 GOTO(out_free_kernbuff, rc = -EINVAL);
2919 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
2920 GOTO(out_free_kernbuff, rc = -ENODEV);
2921 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
2922 queue = PTLRPC_NRS_QUEUE_REG;
2924 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
2926 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
2929 * Serialize NRS core lprocfs operations with policy registration/
2932 mutex_lock(&nrs_core.nrs_mutex);
2933 rc = ptlrpc_nrs_policy_control(svc, queue,
2935 NRS_CTL_TBF_WR_RULE,
2937 mutex_unlock(&nrs_core.nrs_mutex);
2939 nrs_tbf_cmd_fini(cmd);
2942 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2944 return rc ? rc : count;
2946 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
2949 * Initializes a TBF policy's lprocfs interface for service \a svc
2951 * \param[in] svc the service
2954 * \retval != 0 error
2956 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
2958 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
2959 { .name = "nrs_tbf_rule",
2960 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
2965 if (svc->srv_procroot == NULL)
2968 return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
2972 * Cleans up a TBF policy's lprocfs interface for service \a svc
2974 * \param[in] svc the service
2976 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
2978 if (svc->srv_procroot == NULL)
2981 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
2984 #endif /* CONFIG_PROC_FS */
2987 * TBF policy operations
2989 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
2990 .op_policy_start = nrs_tbf_start,
2991 .op_policy_stop = nrs_tbf_stop,
2992 .op_policy_ctl = nrs_tbf_ctl,
2993 .op_res_get = nrs_tbf_res_get,
2994 .op_res_put = nrs_tbf_res_put,
2995 .op_req_get = nrs_tbf_req_get,
2996 .op_req_enqueue = nrs_tbf_req_add,
2997 .op_req_dequeue = nrs_tbf_req_del,
2998 .op_req_stop = nrs_tbf_req_stop,
2999 #ifdef CONFIG_PROC_FS
3000 .op_lprocfs_init = nrs_tbf_lprocfs_init,
3001 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
3006 * TBF policy configuration
3008 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
3009 .nc_name = NRS_POL_NAME_TBF,
3010 .nc_ops = &nrs_tbf_ops,
3011 .nc_compat = nrs_policy_compat_all,
3018 #endif /* HAVE_SERVER_SUPPORT */