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 /* cfs_hash_bd_peek_locked is a somehow "internal" function
629 * of cfs_hash, it doesn't add refcount on object. */
630 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
634 cfs_hash_get(hs, hnode);
635 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
636 if (!list_empty(&cli->tc_lru))
637 list_del_init(&cli->tc_lru);
641 #define NRS_TBF_JOBID_NULL ""
643 static struct nrs_tbf_client *
644 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
645 struct ptlrpc_request *req)
648 struct nrs_tbf_client *cli;
649 struct cfs_hash *hs = head->th_cli_hash;
650 struct cfs_hash_bd bd;
652 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
654 jobid = NRS_TBF_JOBID_NULL;
655 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
656 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
657 cfs_hash_bd_unlock(hs, &bd, 1);
662 static struct nrs_tbf_client *
663 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
664 struct nrs_tbf_client *cli)
667 struct nrs_tbf_client *ret;
668 struct cfs_hash *hs = head->th_cli_hash;
669 struct cfs_hash_bd bd;
671 jobid = cli->tc_jobid;
672 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
673 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
675 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
678 cfs_hash_bd_unlock(hs, &bd, 1);
684 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
685 struct nrs_tbf_client *cli)
687 struct cfs_hash_bd bd;
688 struct cfs_hash *hs = head->th_cli_hash;
689 struct nrs_tbf_bucket *bkt;
691 struct list_head zombies;
693 INIT_LIST_HEAD(&zombies);
694 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
695 bkt = cfs_hash_bd_extra_get(hs, &bd);
696 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
698 LASSERT(list_empty(&cli->tc_lru));
699 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
702 * Check and purge the LRU, there is at least one client in the LRU.
704 hw = tbf_jobid_cache_size >>
705 (hs->hs_cur_bits - hs->hs_bkt_bits);
706 while (cfs_hash_bd_count_get(&bd) > hw) {
707 if (unlikely(list_empty(&bkt->ntb_lru)))
709 cli = list_entry(bkt->ntb_lru.next,
710 struct nrs_tbf_client,
712 LASSERT(atomic_read(&cli->tc_ref) == 0);
713 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
714 list_move(&cli->tc_lru, &zombies);
716 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
718 while (!list_empty(&zombies)) {
719 cli = container_of0(zombies.next,
720 struct nrs_tbf_client, tc_lru);
721 list_del_init(&cli->tc_lru);
722 nrs_tbf_cli_fini(cli);
727 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
728 struct ptlrpc_request *req)
730 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
733 jobid = NRS_TBF_JOBID_NULL;
734 LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
735 INIT_LIST_HEAD(&cli->tc_lru);
736 memcpy(cli->tc_jobid, jobid, strlen(jobid));
739 static int nrs_tbf_jobid_hash_order(void)
743 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
749 #define NRS_TBF_JOBID_BKT_BITS 10
752 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
753 struct nrs_tbf_head *head)
755 struct nrs_tbf_cmd start;
756 struct nrs_tbf_bucket *bkt;
760 struct cfs_hash_bd bd;
762 bits = nrs_tbf_jobid_hash_order();
763 if (bits < NRS_TBF_JOBID_BKT_BITS)
764 bits = NRS_TBF_JOBID_BKT_BITS;
765 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
768 NRS_TBF_JOBID_BKT_BITS,
772 &nrs_tbf_jobid_hash_ops,
773 NRS_TBF_JOBID_HASH_FLAGS);
774 if (head->th_cli_hash == NULL)
777 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
778 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
779 INIT_LIST_HEAD(&bkt->ntb_lru);
782 memset(&start, 0, sizeof(start));
783 start.u.tc_start.ts_jobids_str = "*";
785 start.u.tc_start.ts_rpc_rate = tbf_rate;
786 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
787 start.tc_name = NRS_TBF_DEFAULT_RULE;
788 INIT_LIST_HEAD(&start.u.tc_start.ts_jobids);
789 rc = nrs_tbf_rule_start(policy, head, &start);
795 * Frees jobid of \a list.
799 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
801 struct nrs_tbf_jobid *jobid, *n;
803 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
804 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
805 list_del(&jobid->tj_linkage);
806 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
811 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
813 struct nrs_tbf_jobid *jobid;
815 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
819 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
820 if (jobid->tj_id == NULL) {
821 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
825 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
826 list_add_tail(&jobid->tj_linkage, jobid_list);
831 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
833 struct nrs_tbf_jobid *jobid;
835 list_for_each_entry(jobid, jobid_list, tj_linkage) {
836 if (strcmp(id, jobid->tj_id) == 0)
843 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
852 INIT_LIST_HEAD(jobid_list);
854 rc = cfs_gettok(&src, ' ', &res);
859 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
864 nrs_tbf_jobid_list_free(jobid_list);
868 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
870 if (!list_empty(&cmd->u.tc_start.ts_jobids))
871 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
872 if (cmd->u.tc_start.ts_jobids_str)
873 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
874 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
877 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
880 int keylen = strlen(key);
883 rc = cfs_gettok(src, '=', &res);
884 if (rc == 0 || res.ls_len != keylen ||
885 strncmp(res.ls_str, key, keylen) != 0 ||
886 src->ls_len <= 2 || src->ls_str[0] != '{' ||
887 src->ls_str[src->ls_len - 1] != '}')
890 /* Skip '{' and '}' */
896 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
902 src.ls_len = strlen(id);
903 rc = nrs_tbf_check_id_value(&src, "jobid");
907 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
908 if (cmd->u.tc_start.ts_jobids_str == NULL)
911 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
913 /* parse jobid list */
914 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
915 strlen(cmd->u.tc_start.ts_jobids_str),
916 &cmd->u.tc_start.ts_jobids);
918 nrs_tbf_jobid_cmd_fini(cmd);
923 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
924 struct nrs_tbf_rule *rule,
925 struct nrs_tbf_cmd *start)
929 LASSERT(start->u.tc_start.ts_jobids_str);
930 OBD_ALLOC(rule->tr_jobids_str,
931 strlen(start->u.tc_start.ts_jobids_str) + 1);
932 if (rule->tr_jobids_str == NULL)
935 memcpy(rule->tr_jobids_str,
936 start->u.tc_start.ts_jobids_str,
937 strlen(start->u.tc_start.ts_jobids_str));
939 INIT_LIST_HEAD(&rule->tr_jobids);
940 if (!list_empty(&start->u.tc_start.ts_jobids)) {
941 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
942 strlen(rule->tr_jobids_str),
945 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
948 OBD_FREE(rule->tr_jobids_str,
949 strlen(start->u.tc_start.ts_jobids_str) + 1);
954 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
956 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
957 rule->tr_jobids_str, rule->tr_rpc_rate,
958 atomic_read(&rule->tr_ref) - 1);
963 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
964 struct nrs_tbf_client *cli)
966 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
969 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
971 if (!list_empty(&rule->tr_jobids))
972 nrs_tbf_jobid_list_free(&rule->tr_jobids);
973 LASSERT(rule->tr_jobids_str != NULL);
974 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
977 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
978 .o_name = NRS_TBF_TYPE_JOBID,
979 .o_startup = nrs_tbf_jobid_startup,
980 .o_cli_find = nrs_tbf_jobid_cli_find,
981 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
982 .o_cli_put = nrs_tbf_jobid_cli_put,
983 .o_cli_init = nrs_tbf_jobid_cli_init,
984 .o_rule_init = nrs_tbf_jobid_rule_init,
985 .o_rule_dump = nrs_tbf_jobid_rule_dump,
986 .o_rule_match = nrs_tbf_jobid_rule_match,
987 .o_rule_fini = nrs_tbf_jobid_rule_fini,
991 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
993 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
994 * nrs_tbf_client objects.
996 #define NRS_TBF_NID_BKT_BITS 8
997 #define NRS_TBF_NID_BITS 16
999 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1002 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1005 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1007 lnet_nid_t *nid = (lnet_nid_t *)key;
1008 struct nrs_tbf_client *cli = hlist_entry(hnode,
1009 struct nrs_tbf_client,
1012 return *nid == cli->tc_nid;
1015 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1017 struct nrs_tbf_client *cli = hlist_entry(hnode,
1018 struct nrs_tbf_client,
1021 return &cli->tc_nid;
1024 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
1026 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1029 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1031 struct nrs_tbf_client *cli = hlist_entry(hnode,
1032 struct nrs_tbf_client,
1035 atomic_inc(&cli->tc_ref);
1038 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1040 struct nrs_tbf_client *cli = hlist_entry(hnode,
1041 struct nrs_tbf_client,
1044 atomic_dec(&cli->tc_ref);
1047 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1049 struct nrs_tbf_client *cli = hlist_entry(hnode,
1050 struct nrs_tbf_client,
1053 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1054 "Busy TBF object from client with NID %s, with %d refs\n",
1055 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1057 nrs_tbf_cli_fini(cli);
1060 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1061 .hs_hash = nrs_tbf_nid_hop_hash,
1062 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
1063 .hs_key = nrs_tbf_nid_hop_key,
1064 .hs_object = nrs_tbf_nid_hop_object,
1065 .hs_get = nrs_tbf_nid_hop_get,
1066 .hs_put = nrs_tbf_nid_hop_put,
1067 .hs_put_locked = nrs_tbf_nid_hop_put,
1068 .hs_exit = nrs_tbf_nid_hop_exit,
1071 static struct nrs_tbf_client *
1072 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1073 struct ptlrpc_request *req)
1075 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
1078 static struct nrs_tbf_client *
1079 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1080 struct nrs_tbf_client *cli)
1082 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1087 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1088 struct nrs_tbf_client *cli)
1090 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1094 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1095 struct nrs_tbf_head *head)
1097 struct nrs_tbf_cmd start;
1100 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1103 NRS_TBF_NID_BKT_BITS, 0,
1106 &nrs_tbf_nid_hash_ops,
1107 CFS_HASH_RW_BKTLOCK);
1108 if (head->th_cli_hash == NULL)
1111 memset(&start, 0, sizeof(start));
1112 start.u.tc_start.ts_nids_str = "*";
1114 start.u.tc_start.ts_rpc_rate = tbf_rate;
1115 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1116 start.tc_name = NRS_TBF_DEFAULT_RULE;
1117 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1118 rc = nrs_tbf_rule_start(policy, head, &start);
1124 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1125 struct ptlrpc_request *req)
1127 cli->tc_nid = req->rq_peer.nid;
1130 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1131 struct nrs_tbf_rule *rule,
1132 struct nrs_tbf_cmd *start)
1134 LASSERT(start->u.tc_start.ts_nids_str);
1135 OBD_ALLOC(rule->tr_nids_str,
1136 strlen(start->u.tc_start.ts_nids_str) + 1);
1137 if (rule->tr_nids_str == NULL)
1140 memcpy(rule->tr_nids_str,
1141 start->u.tc_start.ts_nids_str,
1142 strlen(start->u.tc_start.ts_nids_str));
1144 INIT_LIST_HEAD(&rule->tr_nids);
1145 if (!list_empty(&start->u.tc_start.ts_nids)) {
1146 if (cfs_parse_nidlist(rule->tr_nids_str,
1147 strlen(rule->tr_nids_str),
1148 &rule->tr_nids) <= 0) {
1149 CERROR("nids {%s} illegal\n",
1151 OBD_FREE(rule->tr_nids_str,
1152 strlen(start->u.tc_start.ts_nids_str) + 1);
1160 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1162 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1163 rule->tr_nids_str, rule->tr_rpc_rate,
1164 atomic_read(&rule->tr_ref) - 1);
1169 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1170 struct nrs_tbf_client *cli)
1172 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1175 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1177 if (!list_empty(&rule->tr_nids))
1178 cfs_free_nidlist(&rule->tr_nids);
1179 LASSERT(rule->tr_nids_str != NULL);
1180 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1183 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1185 if (!list_empty(&cmd->u.tc_start.ts_nids))
1186 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1187 if (cmd->u.tc_start.ts_nids_str)
1188 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1189 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1192 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1194 struct cfs_lstr src;
1198 src.ls_len = strlen(id);
1199 rc = nrs_tbf_check_id_value(&src, "nid");
1203 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1204 if (cmd->u.tc_start.ts_nids_str == NULL)
1207 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1209 /* parse NID list */
1210 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1211 strlen(cmd->u.tc_start.ts_nids_str),
1212 &cmd->u.tc_start.ts_nids) <= 0) {
1213 nrs_tbf_nid_cmd_fini(cmd);
1220 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1221 .o_name = NRS_TBF_TYPE_NID,
1222 .o_startup = nrs_tbf_nid_startup,
1223 .o_cli_find = nrs_tbf_nid_cli_find,
1224 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1225 .o_cli_put = nrs_tbf_nid_cli_put,
1226 .o_cli_init = nrs_tbf_nid_cli_init,
1227 .o_rule_init = nrs_tbf_nid_rule_init,
1228 .o_rule_dump = nrs_tbf_nid_rule_dump,
1229 .o_rule_match = nrs_tbf_nid_rule_match,
1230 .o_rule_fini = nrs_tbf_nid_rule_fini,
1233 static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
1235 if (rule->tr_opcodes != NULL)
1236 CFS_FREE_BITMAP(rule->tr_opcodes);
1238 LASSERT(rule->tr_opcodes_str != NULL);
1239 OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
1242 static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
1245 return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
1248 static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
1250 const __u32 *opc = key;
1251 struct nrs_tbf_client *cli = hlist_entry(hnode,
1252 struct nrs_tbf_client,
1255 return *opc == cli->tc_opcode;
1258 static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
1260 struct nrs_tbf_client *cli = hlist_entry(hnode,
1261 struct nrs_tbf_client,
1264 return &cli->tc_opcode;
1267 static void *nrs_tbf_opcode_hop_object(struct hlist_node *hnode)
1269 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1272 static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
1273 struct hlist_node *hnode)
1275 struct nrs_tbf_client *cli = hlist_entry(hnode,
1276 struct nrs_tbf_client,
1279 atomic_inc(&cli->tc_ref);
1282 static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
1283 struct hlist_node *hnode)
1285 struct nrs_tbf_client *cli = hlist_entry(hnode,
1286 struct nrs_tbf_client,
1289 atomic_dec(&cli->tc_ref);
1292 static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
1293 struct hlist_node *hnode)
1295 struct nrs_tbf_client *cli = hlist_entry(hnode,
1296 struct nrs_tbf_client,
1299 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1300 "Busy TBF object from client with opcode %s, with %d refs\n",
1301 ll_opcode2str(cli->tc_opcode),
1302 atomic_read(&cli->tc_ref));
1304 nrs_tbf_cli_fini(cli);
1306 static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
1307 .hs_hash = nrs_tbf_opcode_hop_hash,
1308 .hs_keycmp = nrs_tbf_opcode_hop_keycmp,
1309 .hs_key = nrs_tbf_opcode_hop_key,
1310 .hs_object = nrs_tbf_opcode_hop_object,
1311 .hs_get = nrs_tbf_opcode_hop_get,
1312 .hs_put = nrs_tbf_opcode_hop_put,
1313 .hs_put_locked = nrs_tbf_opcode_hop_put,
1314 .hs_exit = nrs_tbf_opcode_hop_exit,
1318 nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
1319 struct nrs_tbf_head *head)
1321 struct nrs_tbf_cmd start = { 0 };
1324 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1327 NRS_TBF_NID_BKT_BITS, 0,
1330 &nrs_tbf_opcode_hash_ops,
1331 CFS_HASH_RW_BKTLOCK);
1332 if (head->th_cli_hash == NULL)
1335 start.u.tc_start.ts_opcodes = NULL;
1336 start.u.tc_start.ts_opcodes_str = "*";
1338 start.u.tc_start.ts_rpc_rate = tbf_rate;
1339 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1340 start.tc_name = NRS_TBF_DEFAULT_RULE;
1341 rc = nrs_tbf_rule_start(policy, head, &start);
1346 static struct nrs_tbf_client *
1347 nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
1348 struct ptlrpc_request *req)
1352 opc = lustre_msg_get_opc(req->rq_reqmsg);
1353 return cfs_hash_lookup(head->th_cli_hash, &opc);
1356 static struct nrs_tbf_client *
1357 nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
1358 struct nrs_tbf_client *cli)
1360 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
1365 nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
1366 struct ptlrpc_request *req)
1368 cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
1371 #define MAX_OPCODE_LEN 32
1373 nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, struct cfs_bitmap *opcodes)
1376 char opcode_str[MAX_OPCODE_LEN];
1378 if (id->ls_len + 1 > MAX_OPCODE_LEN)
1381 memcpy(opcode_str, id->ls_str, id->ls_len);
1382 opcode_str[id->ls_len] = '\0';
1384 op = ll_str2opcode(opcode_str);
1388 cfs_bitmap_set(opcodes, op);
1393 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap *opcodes)
1395 struct cfs_lstr src;
1396 struct cfs_lstr res;
1403 while (src.ls_str) {
1404 rc = cfs_gettok(&src, ' ', &res);
1409 rc = nrs_tbf_opcode_set_bit(&res, opcodes);
1417 static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
1419 if (cmd->u.tc_start.ts_opcodes)
1420 CFS_FREE_BITMAP(cmd->u.tc_start.ts_opcodes);
1422 if (cmd->u.tc_start.ts_opcodes_str)
1423 OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
1424 strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
1428 static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
1430 struct cfs_lstr src;
1433 cmd->u.tc_start.ts_opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
1434 if (cmd->u.tc_start.ts_opcodes == NULL)
1438 src.ls_len = strlen(id);
1439 rc = nrs_tbf_check_id_value(&src, "opcode");
1443 OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
1444 if (cmd->u.tc_start.ts_opcodes_str == NULL)
1445 GOTO(out, rc = -ENOMEM);
1447 memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
1449 /* parse opcode list */
1450 rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
1451 strlen(cmd->u.tc_start.ts_opcodes_str),
1452 cmd->u.tc_start.ts_opcodes);
1455 nrs_tbf_opcode_cmd_fini(cmd);
1461 nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
1462 struct nrs_tbf_client *cli)
1464 if (rule->tr_opcodes == NULL)
1467 return cfs_bitmap_check(rule->tr_opcodes, cli->tc_opcode);
1470 static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
1471 struct nrs_tbf_rule *rule,
1472 struct nrs_tbf_cmd *start)
1474 LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
1475 OBD_ALLOC(rule->tr_opcodes_str,
1476 strlen(start->u.tc_start.ts_opcodes_str) + 1);
1477 if (rule->tr_opcodes_str == NULL)
1480 strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
1481 strlen(start->u.tc_start.ts_opcodes_str) + 1);
1483 if (start->u.tc_start.ts_opcodes == NULL)
1486 rule->tr_opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
1487 if (rule->tr_opcodes == NULL) {
1488 OBD_FREE(rule->tr_opcodes_str,
1489 strlen(start->u.tc_start.ts_opcodes_str) + 1);
1493 cfs_bitmap_copy(rule->tr_opcodes, start->u.tc_start.ts_opcodes);
1499 nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1501 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1502 rule->tr_opcodes_str, rule->tr_rpc_rate,
1503 atomic_read(&rule->tr_ref) - 1);
1508 struct nrs_tbf_ops nrs_tbf_opcode_ops = {
1509 .o_name = NRS_TBF_TYPE_OPCODE,
1510 .o_startup = nrs_tbf_opcode_startup,
1511 .o_cli_find = nrs_tbf_opcode_cli_find,
1512 .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
1513 .o_cli_put = nrs_tbf_nid_cli_put,
1514 .o_cli_init = nrs_tbf_opcode_cli_init,
1515 .o_rule_init = nrs_tbf_opcode_rule_init,
1516 .o_rule_dump = nrs_tbf_opcode_rule_dump,
1517 .o_rule_match = nrs_tbf_opcode_rule_match,
1518 .o_rule_fini = nrs_tbf_opcode_rule_fini,
1521 static struct nrs_tbf_type nrs_tbf_types[] = {
1523 .ntt_name = NRS_TBF_TYPE_JOBID,
1524 .ntt_flag = NRS_TBF_FLAG_JOBID,
1525 .ntt_ops = &nrs_tbf_jobid_ops,
1528 .ntt_name = NRS_TBF_TYPE_NID,
1529 .ntt_flag = NRS_TBF_FLAG_NID,
1530 .ntt_ops = &nrs_tbf_nid_ops,
1533 .ntt_name = NRS_TBF_TYPE_OPCODE,
1534 .ntt_flag = NRS_TBF_FLAG_OPCODE,
1535 .ntt_ops = &nrs_tbf_opcode_ops,
1540 * Is called before the policy transitions into
1541 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1542 * policy-specific private data structure.
1544 * \param[in] policy The policy to start
1546 * \retval -ENOMEM OOM error
1549 * \see nrs_policy_register()
1550 * \see nrs_policy_ctl()
1552 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1554 struct nrs_tbf_head *head;
1555 struct nrs_tbf_ops *ops;
1561 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1562 GOTO(out, rc = -EINVAL);
1564 for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
1565 if (strcmp(arg, nrs_tbf_types[i].ntt_name) == 0) {
1566 ops = nrs_tbf_types[i].ntt_ops;
1567 type = nrs_tbf_types[i].ntt_flag;
1573 GOTO(out, rc = -ENOTSUPP);
1575 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1577 GOTO(out, rc = -ENOMEM);
1579 memcpy(head->th_type, arg, strlen(arg));
1580 head->th_type[strlen(arg)] = '\0';
1582 head->th_type_flag = type;
1584 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1585 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1586 nrs_pol2cptab(policy),
1587 nrs_pol2cptid(policy));
1588 if (head->th_binheap == NULL)
1589 GOTO(out_free_head, rc = -ENOMEM);
1591 atomic_set(&head->th_rule_sequence, 0);
1592 spin_lock_init(&head->th_rule_lock);
1593 INIT_LIST_HEAD(&head->th_list);
1594 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1595 head->th_timer.function = nrs_tbf_timer_cb;
1596 rc = head->th_ops->o_startup(policy, head);
1598 GOTO(out_free_heap, rc);
1600 policy->pol_private = head;
1603 cfs_binheap_destroy(head->th_binheap);
1611 * Is called before the policy transitions into
1612 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1613 * private data structure.
1615 * \param[in] policy The policy to stop
1617 * \see nrs_policy_stop0()
1619 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1621 struct nrs_tbf_head *head = policy->pol_private;
1622 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1623 struct nrs_tbf_rule *rule, *n;
1625 LASSERT(head != NULL);
1626 LASSERT(head->th_cli_hash != NULL);
1627 hrtimer_cancel(&head->th_timer);
1628 /* Should cleanup hash first before free rules */
1629 cfs_hash_putref(head->th_cli_hash);
1630 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1631 list_del_init(&rule->tr_linkage);
1632 nrs_tbf_rule_put(rule);
1634 LASSERT(list_empty(&head->th_list));
1635 LASSERT(head->th_binheap != NULL);
1636 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1637 cfs_binheap_destroy(head->th_binheap);
1639 nrs->nrs_throttling = 0;
1640 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1644 * Performs a policy-specific ctl function on TBF policy instances; similar
1647 * \param[in] policy the policy instance
1648 * \param[in] opc the opcode
1649 * \param[in,out] arg used for passing parameters and information
1651 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1652 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1654 * \retval 0 operation carried out successfully
1657 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
1658 enum ptlrpc_nrs_ctl opc,
1664 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1666 switch ((enum nrs_ctl_tbf)opc) {
1671 * Read RPC rate size of a policy instance.
1673 case NRS_CTL_TBF_RD_RULE: {
1674 struct nrs_tbf_head *head = policy->pol_private;
1675 struct seq_file *m = (struct seq_file *) arg;
1676 struct ptlrpc_service_part *svcpt;
1678 svcpt = policy->pol_nrs->nrs_svcpt;
1679 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1681 rc = nrs_tbf_rule_dump_all(head, m);
1686 * Write RPC rate of a policy instance.
1688 case NRS_CTL_TBF_WR_RULE: {
1689 struct nrs_tbf_head *head = policy->pol_private;
1690 struct nrs_tbf_cmd *cmd;
1692 cmd = (struct nrs_tbf_cmd *)arg;
1693 rc = nrs_tbf_command(policy,
1699 * Read the TBF policy type of a policy instance.
1701 case NRS_CTL_TBF_RD_TYPE_FLAG: {
1702 struct nrs_tbf_head *head = policy->pol_private;
1704 *(__u32 *)arg = head->th_type_flag;
1713 * Is called for obtaining a TBF policy resource.
1715 * \param[in] policy The policy on which the request is being asked for
1716 * \param[in] nrq The request for which resources are being taken
1717 * \param[in] parent Parent resource, unused in this policy
1718 * \param[out] resp Resources references are placed in this array
1719 * \param[in] moving_req Signifies limited caller context; unused in this
1723 * \see nrs_resource_get_safe()
1725 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1726 struct ptlrpc_nrs_request *nrq,
1727 const struct ptlrpc_nrs_resource *parent,
1728 struct ptlrpc_nrs_resource **resp,
1731 struct nrs_tbf_head *head;
1732 struct nrs_tbf_client *cli;
1733 struct nrs_tbf_client *tmp;
1734 struct ptlrpc_request *req;
1736 if (parent == NULL) {
1737 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1741 head = container_of(parent, struct nrs_tbf_head, th_res);
1742 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1743 cli = head->th_ops->o_cli_find(head, req);
1745 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1746 LASSERT(cli->tc_rule);
1747 if (cli->tc_rule_sequence !=
1748 atomic_read(&head->th_rule_sequence) ||
1749 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1750 struct nrs_tbf_rule *rule;
1752 rule = nrs_tbf_rule_match(head, cli);
1753 if (rule != cli->tc_rule)
1754 nrs_tbf_cli_reset(head, rule, cli);
1756 nrs_tbf_rule_put(rule);
1757 } else if (cli->tc_rule_generation !=
1758 cli->tc_rule->tr_generation) {
1759 nrs_tbf_cli_reset_value(head, cli);
1761 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1765 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1766 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1770 nrs_tbf_cli_init(head, cli, req);
1771 tmp = head->th_ops->o_cli_findadd(head, cli);
1773 atomic_dec(&cli->tc_ref);
1774 nrs_tbf_cli_fini(cli);
1778 *resp = &cli->tc_res;
1784 * Called when releasing references to the resource hierachy obtained for a
1785 * request for scheduling using the TBF policy.
1787 * \param[in] policy the policy the resource belongs to
1788 * \param[in] res the resource to be released
1790 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1791 const struct ptlrpc_nrs_resource *res)
1793 struct nrs_tbf_head *head;
1794 struct nrs_tbf_client *cli;
1797 * Do nothing for freeing parent, nrs_tbf_net resources
1799 if (res->res_parent == NULL)
1802 cli = container_of(res, struct nrs_tbf_client, tc_res);
1803 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1805 head->th_ops->o_cli_put(head, cli);
1809 * Called when getting a request from the TBF policy for handling, or just
1810 * peeking; removes the request from the policy when it is to be handled.
1812 * \param[in] policy The policy
1813 * \param[in] peek When set, signifies that we just want to examine the
1814 * request, and not handle it, so the request is not removed
1816 * \param[in] force Force the policy to return a request; unused in this
1819 * \retval The request to be handled; this is the next request in the TBF
1822 * \see ptlrpc_nrs_req_get_nolock()
1823 * \see nrs_request_get()
1826 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1827 bool peek, bool force)
1829 struct nrs_tbf_head *head = policy->pol_private;
1830 struct ptlrpc_nrs_request *nrq = NULL;
1831 struct nrs_tbf_client *cli;
1832 struct cfs_binheap_node *node;
1834 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1836 if (!peek && policy->pol_nrs->nrs_throttling)
1839 node = cfs_binheap_root(head->th_binheap);
1840 if (unlikely(node == NULL))
1843 cli = container_of(node, struct nrs_tbf_client, tc_node);
1844 LASSERT(cli->tc_in_heap);
1846 nrq = list_entry(cli->tc_list.next,
1847 struct ptlrpc_nrs_request,
1850 __u64 now = ktime_to_ns(ktime_get());
1855 deadline = cli->tc_check_time +
1857 LASSERT(now >= cli->tc_check_time);
1858 passed = now - cli->tc_check_time;
1859 ntoken = passed * cli->tc_rpc_rate;
1860 do_div(ntoken, NSEC_PER_SEC);
1861 ntoken += cli->tc_ntoken;
1862 if (ntoken > cli->tc_depth)
1863 ntoken = cli->tc_depth;
1865 struct ptlrpc_request *req;
1866 nrq = list_entry(cli->tc_list.next,
1867 struct ptlrpc_nrs_request,
1869 req = container_of(nrq,
1870 struct ptlrpc_request,
1873 cli->tc_ntoken = ntoken;
1874 cli->tc_check_time = now;
1875 list_del_init(&nrq->nr_u.tbf.tr_list);
1876 if (list_empty(&cli->tc_list)) {
1877 cfs_binheap_remove(head->th_binheap,
1879 cli->tc_in_heap = false;
1881 cfs_binheap_relocate(head->th_binheap,
1885 "NRS start %s request from %s, "
1887 policy->pol_desc->pd_name,
1888 libcfs_id2str(req->rq_peer),
1889 nrq->nr_u.tbf.tr_sequence);
1893 policy->pol_nrs->nrs_throttling = 1;
1894 head->th_deadline = deadline;
1895 time = ktime_set(0, 0);
1896 time = ktime_add_ns(time, deadline);
1897 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1905 * Adds request \a nrq to \a policy's list of queued requests
1907 * \param[in] policy The policy
1908 * \param[in] nrq The request to add
1910 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1913 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1914 struct ptlrpc_nrs_request *nrq)
1916 struct nrs_tbf_head *head;
1917 struct nrs_tbf_client *cli;
1920 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1922 cli = container_of(nrs_request_resource(nrq),
1923 struct nrs_tbf_client, tc_res);
1924 head = container_of(nrs_request_resource(nrq)->res_parent,
1925 struct nrs_tbf_head, th_res);
1926 if (list_empty(&cli->tc_list)) {
1927 LASSERT(!cli->tc_in_heap);
1928 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1930 cli->tc_in_heap = true;
1931 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1932 list_add_tail(&nrq->nr_u.tbf.tr_list,
1934 if (policy->pol_nrs->nrs_throttling) {
1935 __u64 deadline = cli->tc_check_time +
1937 if ((head->th_deadline > deadline) &&
1938 (hrtimer_try_to_cancel(&head->th_timer)
1941 head->th_deadline = deadline;
1942 time = ktime_set(0, 0);
1943 time = ktime_add_ns(time, deadline);
1944 hrtimer_start(&head->th_timer, time,
1950 LASSERT(cli->tc_in_heap);
1951 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1952 list_add_tail(&nrq->nr_u.tbf.tr_list,
1959 * Removes request \a nrq from \a policy's list of queued requests.
1961 * \param[in] policy The policy
1962 * \param[in] nrq The request to remove
1964 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1965 struct ptlrpc_nrs_request *nrq)
1967 struct nrs_tbf_head *head;
1968 struct nrs_tbf_client *cli;
1970 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1972 cli = container_of(nrs_request_resource(nrq),
1973 struct nrs_tbf_client, tc_res);
1974 head = container_of(nrs_request_resource(nrq)->res_parent,
1975 struct nrs_tbf_head, th_res);
1977 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1978 list_del_init(&nrq->nr_u.tbf.tr_list);
1979 if (list_empty(&cli->tc_list)) {
1980 cfs_binheap_remove(head->th_binheap,
1982 cli->tc_in_heap = false;
1984 cfs_binheap_relocate(head->th_binheap,
1990 * Prints a debug statement right before the request \a nrq stops being
1993 * \param[in] policy The policy handling the request
1994 * \param[in] nrq The request being handled
1996 * \see ptlrpc_server_finish_request()
1997 * \see ptlrpc_nrs_req_stop_nolock()
1999 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
2000 struct ptlrpc_nrs_request *nrq)
2002 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
2005 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2007 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
2008 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
2009 nrq->nr_u.tbf.tr_sequence);
2012 #ifdef CONFIG_PROC_FS
2019 * The maximum RPC rate.
2021 #define LPROCFS_NRS_RATE_MAX 65535
2024 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
2026 struct ptlrpc_service *svc = m->private;
2029 seq_printf(m, "regular_requests:\n");
2031 * Perform two separate calls to this as only one of the NRS heads'
2032 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
2033 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
2035 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
2037 NRS_CTL_TBF_RD_RULE,
2041 * -ENOSPC means buf in the parameter m is overflow, return 0
2042 * here to let upper layer function seq_read alloc a larger
2043 * memory area and do this process again.
2045 } else if (rc == -ENOSPC) {
2049 * Ignore -ENODEV as the regular NRS head's policy may be in the
2050 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
2052 } else if (rc != -ENODEV) {
2056 if (!nrs_svc_has_hp(svc))
2059 seq_printf(m, "high_priority_requests:\n");
2060 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
2062 NRS_CTL_TBF_RD_RULE,
2066 * -ENOSPC means buf in the parameter m is overflow, return 0
2067 * here to let upper layer function seq_read alloc a larger
2068 * memory area and do this process again.
2070 } else if (rc == -ENOSPC) {
2079 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
2083 switch (cmd->u.tc_start.ts_valid_type) {
2084 case NRS_TBF_FLAG_JOBID:
2085 rc = nrs_tbf_jobid_parse(cmd, token);
2087 case NRS_TBF_FLAG_NID:
2088 rc = nrs_tbf_nid_parse(cmd, token);
2090 case NRS_TBF_FLAG_OPCODE:
2091 rc = nrs_tbf_opcode_parse(cmd, token);
2100 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
2102 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2103 if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_JOBID)
2104 nrs_tbf_jobid_cmd_fini(cmd);
2105 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_NID)
2106 nrs_tbf_nid_cmd_fini(cmd);
2107 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_OPCODE)
2108 nrs_tbf_opcode_cmd_fini(cmd);
2112 static bool name_is_valid(const char *name)
2116 for (i = 0; i < strlen(name); i++) {
2117 if ((!isalnum(name[i])) &&
2125 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
2133 key = strsep(&val, "=");
2134 if (val == NULL || strlen(val) == 0)
2137 /* Key of the value pair */
2138 if (strcmp(key, "rate") == 0) {
2139 rc = kstrtoull(val, 10, &rate);
2143 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
2146 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2147 cmd->u.tc_start.ts_rpc_rate = rate;
2148 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2149 cmd->u.tc_change.tc_rpc_rate = rate;
2152 } else if (strcmp(key, "rank") == 0) {
2153 if (!name_is_valid(val))
2156 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2157 cmd->u.tc_start.ts_next_name = val;
2158 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2159 cmd->u.tc_change.tc_next_name = val;
2169 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
2176 while (val != NULL && strlen(val) != 0) {
2177 token = strsep(&val, " ");
2178 rc = nrs_tbf_parse_value_pair(cmd, token);
2183 switch (cmd->tc_cmd) {
2184 case NRS_CTL_TBF_START_RULE:
2185 if (cmd->u.tc_start.ts_rpc_rate == 0)
2186 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
2188 case NRS_CTL_TBF_CHANGE_RULE:
2189 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
2190 cmd->u.tc_change.tc_next_name == NULL)
2193 case NRS_CTL_TBF_STOP_RULE:
2201 static struct nrs_tbf_cmd *
2202 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
2204 static struct nrs_tbf_cmd *cmd;
2211 GOTO(out, rc = -ENOMEM);
2212 memset(cmd, 0, sizeof(*cmd));
2215 token = strsep(&val, " ");
2216 if (val == NULL || strlen(val) == 0)
2217 GOTO(out_free_cmd, rc = -EINVAL);
2219 /* Type of the command */
2220 if (strcmp(token, "start") == 0) {
2221 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
2222 cmd->u.tc_start.ts_valid_type = type_flag;
2223 } else if (strcmp(token, "stop") == 0)
2224 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
2225 else if (strcmp(token, "change") == 0)
2226 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
2228 GOTO(out_free_cmd, rc = -EINVAL);
2230 /* Name of the rule */
2231 token = strsep(&val, " ");
2232 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
2233 !name_is_valid(token))
2234 GOTO(out_free_cmd, rc = -EINVAL);
2235 cmd->tc_name = token;
2237 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2241 val = strrchr(token, '}');
2243 GOTO(out_free_cmd, rc = -EINVAL);
2249 } else if (*val == ' ') {
2253 GOTO(out_free_cmd, rc = -EINVAL);
2255 rc = nrs_tbf_id_parse(cmd, token);
2257 GOTO(out_free_cmd, rc);
2260 rc = nrs_tbf_parse_value_pairs(cmd, val);
2262 GOTO(out_cmd_fini, rc = -EINVAL);
2265 nrs_tbf_cmd_fini(cmd);
2275 * Get the TBF policy type (nid, jobid, etc) preset by
2276 * proc entry 'nrs_policies' for command buffer parsing.
2278 * \param[in] svc the PTLRPC service
2279 * \param[in] queue the NRS queue type
2281 * \retval the preset TBF policy type flag
2284 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
2289 rc = ptlrpc_nrs_policy_control(svc, queue,
2291 NRS_CTL_TBF_RD_TYPE_FLAG,
2294 type = NRS_TBF_FLAG_INVALID;
2299 extern struct nrs_core nrs_core;
2300 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
2302 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
2303 const char __user *buffer,
2304 size_t count, loff_t *off)
2306 struct seq_file *m = file->private_data;
2307 struct ptlrpc_service *svc = m->private;
2311 static struct nrs_tbf_cmd *cmd;
2312 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
2313 unsigned long length;
2316 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2317 if (kernbuf == NULL)
2318 GOTO(out, rc = -ENOMEM);
2320 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
2321 GOTO(out_free_kernbuff, rc = -EINVAL);
2323 if (copy_from_user(kernbuf, buffer, count))
2324 GOTO(out_free_kernbuff, rc = -EFAULT);
2327 token = strsep(&val, " ");
2329 GOTO(out_free_kernbuff, rc = -EINVAL);
2331 if (strcmp(token, "reg") == 0) {
2332 queue = PTLRPC_NRS_QUEUE_REG;
2333 } else if (strcmp(token, "hp") == 0) {
2334 queue = PTLRPC_NRS_QUEUE_HP;
2336 kernbuf[strlen(token)] = ' ';
2339 length = strlen(val);
2342 GOTO(out_free_kernbuff, rc = -EINVAL);
2344 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
2345 GOTO(out_free_kernbuff, rc = -ENODEV);
2346 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
2347 queue = PTLRPC_NRS_QUEUE_REG;
2349 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
2351 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
2354 * Serialize NRS core lprocfs operations with policy registration/
2357 mutex_lock(&nrs_core.nrs_mutex);
2358 rc = ptlrpc_nrs_policy_control(svc, queue,
2360 NRS_CTL_TBF_WR_RULE,
2362 mutex_unlock(&nrs_core.nrs_mutex);
2364 nrs_tbf_cmd_fini(cmd);
2367 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2369 return rc ? rc : count;
2371 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
2374 * Initializes a TBF policy's lprocfs interface for service \a svc
2376 * \param[in] svc the service
2379 * \retval != 0 error
2381 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
2383 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
2384 { .name = "nrs_tbf_rule",
2385 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
2390 if (svc->srv_procroot == NULL)
2393 return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
2397 * Cleans up a TBF policy's lprocfs interface for service \a svc
2399 * \param[in] svc the service
2401 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
2403 if (svc->srv_procroot == NULL)
2406 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
2409 #endif /* CONFIG_PROC_FS */
2412 * TBF policy operations
2414 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
2415 .op_policy_start = nrs_tbf_start,
2416 .op_policy_stop = nrs_tbf_stop,
2417 .op_policy_ctl = nrs_tbf_ctl,
2418 .op_res_get = nrs_tbf_res_get,
2419 .op_res_put = nrs_tbf_res_put,
2420 .op_req_get = nrs_tbf_req_get,
2421 .op_req_enqueue = nrs_tbf_req_add,
2422 .op_req_dequeue = nrs_tbf_req_del,
2423 .op_req_stop = nrs_tbf_req_stop,
2424 #ifdef CONFIG_PROC_FS
2425 .op_lprocfs_init = nrs_tbf_lprocfs_init,
2426 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
2431 * TBF policy configuration
2433 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
2434 .nc_name = NRS_POL_NAME_TBF,
2435 .nc_ops = &nrs_tbf_ops,
2436 .nc_compat = nrs_policy_compat_all,
2443 #endif /* HAVE_SERVER_SUPPORT */