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_flags = start->u.tc_start.ts_rule_flags;
304 rule->tr_nsecs = NSEC_PER_SEC;
305 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
306 rule->tr_depth = tbf_depth;
307 atomic_set(&rule->tr_ref, 1);
308 INIT_LIST_HEAD(&rule->tr_cli_list);
309 INIT_LIST_HEAD(&rule->tr_nids);
310 INIT_LIST_HEAD(&rule->tr_linkage);
311 spin_lock_init(&rule->tr_rule_lock);
312 rule->tr_head = head;
314 rc = head->th_ops->o_rule_init(policy, rule, start);
320 /* Add as the newest rule */
321 spin_lock(&head->th_rule_lock);
322 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
324 spin_unlock(&head->th_rule_lock);
325 nrs_tbf_rule_put(tmp_rule);
326 nrs_tbf_rule_put(rule);
331 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
333 spin_unlock(&head->th_rule_lock);
334 nrs_tbf_rule_put(rule);
338 list_add(&rule->tr_linkage, next_rule->tr_linkage.prev);
339 nrs_tbf_rule_put(next_rule);
341 /* Add on the top of the rule list */
342 list_add(&rule->tr_linkage, &head->th_list);
344 spin_unlock(&head->th_rule_lock);
345 atomic_inc(&head->th_rule_sequence);
346 if (start->u.tc_start.ts_rule_flags & NTRS_DEFAULT) {
347 rule->tr_flags |= NTRS_DEFAULT;
348 LASSERT(head->th_rule == NULL);
349 head->th_rule = rule;
352 CDEBUG(D_RPCTRACE, "TBF starts rule@%p rate %llu gen %llu\n",
353 rule, rule->tr_rpc_rate, rule->tr_generation);
359 * Change the rank of a rule in the rule list
361 * The matched rule will be moved to the position right before another
364 * \param[in] policy the policy instance
365 * \param[in] head the TBF policy instance
366 * \param[in] name the rule name to be moved
367 * \param[in] next_name the rule name before which the matched rule will be
372 nrs_tbf_rule_change_rank(struct ptlrpc_nrs_policy *policy,
373 struct nrs_tbf_head *head,
377 struct nrs_tbf_rule *rule = NULL;
378 struct nrs_tbf_rule *next_rule = NULL;
381 LASSERT(head != NULL);
383 spin_lock(&head->th_rule_lock);
384 rule = nrs_tbf_rule_find_nolock(head, name);
386 GOTO(out, rc = -ENOENT);
388 if (strcmp(name, next_name) == 0)
391 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
393 GOTO(out_put, rc = -ENOENT);
395 list_move(&rule->tr_linkage, next_rule->tr_linkage.prev);
396 nrs_tbf_rule_put(next_rule);
398 nrs_tbf_rule_put(rule);
400 spin_unlock(&head->th_rule_lock);
405 nrs_tbf_rule_change_rate(struct ptlrpc_nrs_policy *policy,
406 struct nrs_tbf_head *head,
410 struct nrs_tbf_rule *rule;
412 assert_spin_locked(&policy->pol_nrs->nrs_lock);
414 rule = nrs_tbf_rule_find(head, name);
418 rule->tr_rpc_rate = rate;
419 rule->tr_nsecs = NSEC_PER_SEC;
420 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
421 rule->tr_generation++;
422 nrs_tbf_rule_put(rule);
428 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
429 struct nrs_tbf_head *head,
430 struct nrs_tbf_cmd *change)
432 __u64 rate = change->u.tc_change.tc_rpc_rate;
433 char *next_name = change->u.tc_change.tc_next_name;
437 rc = nrs_tbf_rule_change_rate(policy, head, change->tc_name,
444 rc = nrs_tbf_rule_change_rank(policy, head, change->tc_name,
454 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
455 struct nrs_tbf_head *head,
456 struct nrs_tbf_cmd *stop)
458 struct nrs_tbf_rule *rule;
460 assert_spin_locked(&policy->pol_nrs->nrs_lock);
462 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
465 rule = nrs_tbf_rule_find(head, stop->tc_name);
469 list_del_init(&rule->tr_linkage);
470 rule->tr_flags |= NTRS_STOPPING;
471 nrs_tbf_rule_put(rule);
472 nrs_tbf_rule_put(rule);
478 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
479 struct nrs_tbf_head *head,
480 struct nrs_tbf_cmd *cmd)
484 assert_spin_locked(&policy->pol_nrs->nrs_lock);
486 switch (cmd->tc_cmd) {
487 case NRS_CTL_TBF_START_RULE:
488 if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
491 spin_unlock(&policy->pol_nrs->nrs_lock);
492 rc = nrs_tbf_rule_start(policy, head, cmd);
493 spin_lock(&policy->pol_nrs->nrs_lock);
495 case NRS_CTL_TBF_CHANGE_RULE:
496 rc = nrs_tbf_rule_change(policy, head, cmd);
498 case NRS_CTL_TBF_STOP_RULE:
499 rc = nrs_tbf_rule_stop(policy, head, cmd);
500 /* Take it as a success, if not exists at all */
501 return rc == -ENOENT ? 0 : rc;
508 * Binary heap predicate.
510 * \param[in] e1 the first binheap node to compare
511 * \param[in] e2 the second binheap node to compare
517 tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
519 struct nrs_tbf_client *cli1;
520 struct nrs_tbf_client *cli2;
522 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
523 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
525 if (cli1->tc_deadline < cli2->tc_deadline)
527 else if (cli1->tc_deadline > cli2->tc_deadline)
530 if (cli1->tc_check_time < cli2->tc_check_time)
532 else if (cli1->tc_check_time > cli2->tc_check_time)
535 /* Maybe need more comparasion, e.g. request number in the rules */
540 * TBF binary heap operations
542 static struct cfs_binheap_ops nrs_tbf_heap_ops = {
545 .hop_compare = tbf_cli_compare,
548 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
551 return cfs_hash_djb2_hash(key, strlen(key), mask);
554 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
556 struct nrs_tbf_client *cli = hlist_entry(hnode,
557 struct nrs_tbf_client,
560 return (strcmp(cli->tc_jobid, key) == 0);
563 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
565 struct nrs_tbf_client *cli = hlist_entry(hnode,
566 struct nrs_tbf_client,
569 return cli->tc_jobid;
572 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
574 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
577 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
579 struct nrs_tbf_client *cli = hlist_entry(hnode,
580 struct nrs_tbf_client,
583 atomic_inc(&cli->tc_ref);
586 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
588 struct nrs_tbf_client *cli = hlist_entry(hnode,
589 struct nrs_tbf_client,
592 atomic_dec(&cli->tc_ref);
596 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
599 struct nrs_tbf_client *cli = hlist_entry(hnode,
600 struct nrs_tbf_client,
603 LASSERT(atomic_read(&cli->tc_ref) == 0);
604 nrs_tbf_cli_fini(cli);
607 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
608 .hs_hash = nrs_tbf_jobid_hop_hash,
609 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
610 .hs_key = nrs_tbf_jobid_hop_key,
611 .hs_object = nrs_tbf_jobid_hop_object,
612 .hs_get = nrs_tbf_jobid_hop_get,
613 .hs_put = nrs_tbf_jobid_hop_put,
614 .hs_put_locked = nrs_tbf_jobid_hop_put,
615 .hs_exit = nrs_tbf_jobid_hop_exit,
618 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
619 CFS_HASH_NO_ITEMREF | \
622 static struct nrs_tbf_client *
623 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
624 struct cfs_hash_bd *bd,
627 struct hlist_node *hnode;
628 struct nrs_tbf_client *cli;
630 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)jobid);
634 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
635 if (!list_empty(&cli->tc_lru))
636 list_del_init(&cli->tc_lru);
640 #define NRS_TBF_JOBID_NULL ""
642 static struct nrs_tbf_client *
643 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
644 struct ptlrpc_request *req)
647 struct nrs_tbf_client *cli;
648 struct cfs_hash *hs = head->th_cli_hash;
649 struct cfs_hash_bd bd;
651 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
653 jobid = NRS_TBF_JOBID_NULL;
654 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
655 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
656 cfs_hash_bd_unlock(hs, &bd, 1);
661 static struct nrs_tbf_client *
662 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
663 struct nrs_tbf_client *cli)
666 struct nrs_tbf_client *ret;
667 struct cfs_hash *hs = head->th_cli_hash;
668 struct cfs_hash_bd bd;
670 jobid = cli->tc_jobid;
671 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
672 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
674 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
677 cfs_hash_bd_unlock(hs, &bd, 1);
683 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
684 struct nrs_tbf_client *cli)
686 struct cfs_hash_bd bd;
687 struct cfs_hash *hs = head->th_cli_hash;
688 struct nrs_tbf_bucket *bkt;
690 struct list_head zombies;
692 INIT_LIST_HEAD(&zombies);
693 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
694 bkt = cfs_hash_bd_extra_get(hs, &bd);
695 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
697 LASSERT(list_empty(&cli->tc_lru));
698 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
701 * Check and purge the LRU, there is at least one client in the LRU.
703 hw = tbf_jobid_cache_size >>
704 (hs->hs_cur_bits - hs->hs_bkt_bits);
705 while (cfs_hash_bd_count_get(&bd) > hw) {
706 if (unlikely(list_empty(&bkt->ntb_lru)))
708 cli = list_entry(bkt->ntb_lru.next,
709 struct nrs_tbf_client,
711 LASSERT(atomic_read(&cli->tc_ref) == 0);
712 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
713 list_move(&cli->tc_lru, &zombies);
715 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
717 while (!list_empty(&zombies)) {
718 cli = container_of0(zombies.next,
719 struct nrs_tbf_client, tc_lru);
720 list_del_init(&cli->tc_lru);
721 nrs_tbf_cli_fini(cli);
726 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
727 struct ptlrpc_request *req)
729 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
732 jobid = NRS_TBF_JOBID_NULL;
733 LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
734 INIT_LIST_HEAD(&cli->tc_lru);
735 memcpy(cli->tc_jobid, jobid, strlen(jobid));
738 static int nrs_tbf_jobid_hash_order(void)
742 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
748 #define NRS_TBF_JOBID_BKT_BITS 10
751 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
752 struct nrs_tbf_head *head)
754 struct nrs_tbf_cmd start;
755 struct nrs_tbf_bucket *bkt;
759 struct cfs_hash_bd bd;
761 bits = nrs_tbf_jobid_hash_order();
762 if (bits < NRS_TBF_JOBID_BKT_BITS)
763 bits = NRS_TBF_JOBID_BKT_BITS;
764 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
767 NRS_TBF_JOBID_BKT_BITS,
771 &nrs_tbf_jobid_hash_ops,
772 NRS_TBF_JOBID_HASH_FLAGS);
773 if (head->th_cli_hash == NULL)
776 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
777 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
778 INIT_LIST_HEAD(&bkt->ntb_lru);
781 memset(&start, 0, sizeof(start));
782 start.u.tc_start.ts_jobids_str = "*";
784 start.u.tc_start.ts_rpc_rate = tbf_rate;
785 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
786 start.tc_name = NRS_TBF_DEFAULT_RULE;
787 INIT_LIST_HEAD(&start.u.tc_start.ts_jobids);
788 rc = nrs_tbf_rule_start(policy, head, &start);
790 cfs_hash_putref(head->th_cli_hash);
791 head->th_cli_hash = NULL;
798 * Frees jobid of \a list.
802 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
804 struct nrs_tbf_jobid *jobid, *n;
806 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
807 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
808 list_del(&jobid->tj_linkage);
809 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
814 nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
816 struct nrs_tbf_jobid *jobid;
820 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
824 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
825 if (jobid->tj_id == NULL) {
826 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
830 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
831 rc = cfs_gettok(id, '*', &res);
833 jobid->tj_match_flag = NRS_TBF_MATCH_FULL;
835 jobid->tj_match_flag = NRS_TBF_MATCH_WILDCARD;
837 list_add_tail(&jobid->tj_linkage, jobid_list);
842 cfs_match_wildcard(const char *pattern, const char *content)
844 if (*pattern == '\0' && *content == '\0')
847 if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
850 while (*pattern == *content) {
853 if (*pattern == '\0' && *content == '\0')
856 if (*pattern == '*' && *(pattern + 1) != '\0' &&
862 return (cfs_match_wildcard(pattern + 1, content) ||
863 cfs_match_wildcard(pattern, content + 1));
869 nrs_tbf_jobid_match(const struct nrs_tbf_jobid *jobid, const char *id)
871 if (jobid->tj_match_flag == NRS_TBF_MATCH_FULL)
872 return strcmp(jobid->tj_id, id) == 0;
874 if (jobid->tj_match_flag == NRS_TBF_MATCH_WILDCARD)
875 return cfs_match_wildcard(jobid->tj_id, id);
881 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
883 struct nrs_tbf_jobid *jobid;
885 list_for_each_entry(jobid, jobid_list, tj_linkage) {
886 if (nrs_tbf_jobid_match(jobid, id))
893 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
902 INIT_LIST_HEAD(jobid_list);
904 rc = cfs_gettok(&src, ' ', &res);
909 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
914 nrs_tbf_jobid_list_free(jobid_list);
918 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
920 if (!list_empty(&cmd->u.tc_start.ts_jobids))
921 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
922 if (cmd->u.tc_start.ts_jobids_str)
923 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
924 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
927 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
930 int keylen = strlen(key);
933 rc = cfs_gettok(src, '=', &res);
934 if (rc == 0 || res.ls_len != keylen ||
935 strncmp(res.ls_str, key, keylen) != 0 ||
936 src->ls_len <= 2 || src->ls_str[0] != '{' ||
937 src->ls_str[src->ls_len - 1] != '}')
940 /* Skip '{' and '}' */
946 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
952 src.ls_len = strlen(id);
953 rc = nrs_tbf_check_id_value(&src, "jobid");
957 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
958 if (cmd->u.tc_start.ts_jobids_str == NULL)
961 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
963 /* parse jobid list */
964 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
965 strlen(cmd->u.tc_start.ts_jobids_str),
966 &cmd->u.tc_start.ts_jobids);
968 nrs_tbf_jobid_cmd_fini(cmd);
973 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
974 struct nrs_tbf_rule *rule,
975 struct nrs_tbf_cmd *start)
979 LASSERT(start->u.tc_start.ts_jobids_str);
980 OBD_ALLOC(rule->tr_jobids_str,
981 strlen(start->u.tc_start.ts_jobids_str) + 1);
982 if (rule->tr_jobids_str == NULL)
985 memcpy(rule->tr_jobids_str,
986 start->u.tc_start.ts_jobids_str,
987 strlen(start->u.tc_start.ts_jobids_str));
989 INIT_LIST_HEAD(&rule->tr_jobids);
990 if (!list_empty(&start->u.tc_start.ts_jobids)) {
991 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
992 strlen(rule->tr_jobids_str),
995 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
998 OBD_FREE(rule->tr_jobids_str,
999 strlen(start->u.tc_start.ts_jobids_str) + 1);
1004 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1006 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1007 rule->tr_jobids_str, rule->tr_rpc_rate,
1008 atomic_read(&rule->tr_ref) - 1);
1013 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
1014 struct nrs_tbf_client *cli)
1016 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
1019 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
1021 if (!list_empty(&rule->tr_jobids))
1022 nrs_tbf_jobid_list_free(&rule->tr_jobids);
1023 LASSERT(rule->tr_jobids_str != NULL);
1024 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
1027 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
1028 .o_name = NRS_TBF_TYPE_JOBID,
1029 .o_startup = nrs_tbf_jobid_startup,
1030 .o_cli_find = nrs_tbf_jobid_cli_find,
1031 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
1032 .o_cli_put = nrs_tbf_jobid_cli_put,
1033 .o_cli_init = nrs_tbf_jobid_cli_init,
1034 .o_rule_init = nrs_tbf_jobid_rule_init,
1035 .o_rule_dump = nrs_tbf_jobid_rule_dump,
1036 .o_rule_match = nrs_tbf_jobid_rule_match,
1037 .o_rule_fini = nrs_tbf_jobid_rule_fini,
1041 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
1043 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
1044 * nrs_tbf_client objects.
1046 #define NRS_TBF_NID_BKT_BITS 8
1047 #define NRS_TBF_NID_BITS 16
1049 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1052 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1055 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1057 lnet_nid_t *nid = (lnet_nid_t *)key;
1058 struct nrs_tbf_client *cli = hlist_entry(hnode,
1059 struct nrs_tbf_client,
1062 return *nid == cli->tc_nid;
1065 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1067 struct nrs_tbf_client *cli = hlist_entry(hnode,
1068 struct nrs_tbf_client,
1071 return &cli->tc_nid;
1074 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
1076 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1079 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1081 struct nrs_tbf_client *cli = hlist_entry(hnode,
1082 struct nrs_tbf_client,
1085 atomic_inc(&cli->tc_ref);
1088 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1090 struct nrs_tbf_client *cli = hlist_entry(hnode,
1091 struct nrs_tbf_client,
1094 atomic_dec(&cli->tc_ref);
1097 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1099 struct nrs_tbf_client *cli = hlist_entry(hnode,
1100 struct nrs_tbf_client,
1103 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1104 "Busy TBF object from client with NID %s, with %d refs\n",
1105 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1107 nrs_tbf_cli_fini(cli);
1110 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1111 .hs_hash = nrs_tbf_nid_hop_hash,
1112 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
1113 .hs_key = nrs_tbf_nid_hop_key,
1114 .hs_object = nrs_tbf_nid_hop_object,
1115 .hs_get = nrs_tbf_nid_hop_get,
1116 .hs_put = nrs_tbf_nid_hop_put,
1117 .hs_put_locked = nrs_tbf_nid_hop_put,
1118 .hs_exit = nrs_tbf_nid_hop_exit,
1121 static struct nrs_tbf_client *
1122 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1123 struct ptlrpc_request *req)
1125 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
1128 static struct nrs_tbf_client *
1129 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1130 struct nrs_tbf_client *cli)
1132 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1137 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1138 struct nrs_tbf_client *cli)
1140 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1144 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1145 struct nrs_tbf_head *head)
1147 struct nrs_tbf_cmd start;
1150 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1153 NRS_TBF_NID_BKT_BITS, 0,
1156 &nrs_tbf_nid_hash_ops,
1157 CFS_HASH_RW_BKTLOCK);
1158 if (head->th_cli_hash == NULL)
1161 memset(&start, 0, sizeof(start));
1162 start.u.tc_start.ts_nids_str = "*";
1164 start.u.tc_start.ts_rpc_rate = tbf_rate;
1165 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1166 start.tc_name = NRS_TBF_DEFAULT_RULE;
1167 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1168 rc = nrs_tbf_rule_start(policy, head, &start);
1170 cfs_hash_putref(head->th_cli_hash);
1171 head->th_cli_hash = NULL;
1178 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1179 struct ptlrpc_request *req)
1181 cli->tc_nid = req->rq_peer.nid;
1184 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1185 struct nrs_tbf_rule *rule,
1186 struct nrs_tbf_cmd *start)
1188 LASSERT(start->u.tc_start.ts_nids_str);
1189 OBD_ALLOC(rule->tr_nids_str,
1190 strlen(start->u.tc_start.ts_nids_str) + 1);
1191 if (rule->tr_nids_str == NULL)
1194 memcpy(rule->tr_nids_str,
1195 start->u.tc_start.ts_nids_str,
1196 strlen(start->u.tc_start.ts_nids_str));
1198 INIT_LIST_HEAD(&rule->tr_nids);
1199 if (!list_empty(&start->u.tc_start.ts_nids)) {
1200 if (cfs_parse_nidlist(rule->tr_nids_str,
1201 strlen(rule->tr_nids_str),
1202 &rule->tr_nids) <= 0) {
1203 CERROR("nids {%s} illegal\n",
1205 OBD_FREE(rule->tr_nids_str,
1206 strlen(start->u.tc_start.ts_nids_str) + 1);
1214 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1216 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1217 rule->tr_nids_str, rule->tr_rpc_rate,
1218 atomic_read(&rule->tr_ref) - 1);
1223 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1224 struct nrs_tbf_client *cli)
1226 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1229 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1231 if (!list_empty(&rule->tr_nids))
1232 cfs_free_nidlist(&rule->tr_nids);
1233 LASSERT(rule->tr_nids_str != NULL);
1234 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1237 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1239 if (!list_empty(&cmd->u.tc_start.ts_nids))
1240 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1241 if (cmd->u.tc_start.ts_nids_str)
1242 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1243 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1246 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1248 struct cfs_lstr src;
1252 src.ls_len = strlen(id);
1253 rc = nrs_tbf_check_id_value(&src, "nid");
1257 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1258 if (cmd->u.tc_start.ts_nids_str == NULL)
1261 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1263 /* parse NID list */
1264 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1265 strlen(cmd->u.tc_start.ts_nids_str),
1266 &cmd->u.tc_start.ts_nids) <= 0) {
1267 nrs_tbf_nid_cmd_fini(cmd);
1274 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1275 .o_name = NRS_TBF_TYPE_NID,
1276 .o_startup = nrs_tbf_nid_startup,
1277 .o_cli_find = nrs_tbf_nid_cli_find,
1278 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1279 .o_cli_put = nrs_tbf_nid_cli_put,
1280 .o_cli_init = nrs_tbf_nid_cli_init,
1281 .o_rule_init = nrs_tbf_nid_rule_init,
1282 .o_rule_dump = nrs_tbf_nid_rule_dump,
1283 .o_rule_match = nrs_tbf_nid_rule_match,
1284 .o_rule_fini = nrs_tbf_nid_rule_fini,
1287 static unsigned nrs_tbf_hop_hash(struct cfs_hash *hs, const void *key,
1290 return cfs_hash_djb2_hash(key, strlen(key), mask);
1293 static int nrs_tbf_hop_keycmp(const void *key, struct hlist_node *hnode)
1295 struct nrs_tbf_client *cli = hlist_entry(hnode,
1296 struct nrs_tbf_client,
1299 return (strcmp(cli->tc_key, key) == 0);
1302 static void *nrs_tbf_hop_key(struct hlist_node *hnode)
1304 struct nrs_tbf_client *cli = hlist_entry(hnode,
1305 struct nrs_tbf_client,
1310 static void *nrs_tbf_hop_object(struct hlist_node *hnode)
1312 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1315 static void nrs_tbf_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1317 struct nrs_tbf_client *cli = hlist_entry(hnode,
1318 struct nrs_tbf_client,
1321 atomic_inc(&cli->tc_ref);
1324 static void nrs_tbf_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1326 struct nrs_tbf_client *cli = hlist_entry(hnode,
1327 struct nrs_tbf_client,
1330 atomic_dec(&cli->tc_ref);
1333 static void nrs_tbf_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1336 struct nrs_tbf_client *cli = hlist_entry(hnode,
1337 struct nrs_tbf_client,
1340 LASSERT(atomic_read(&cli->tc_ref) == 0);
1341 nrs_tbf_cli_fini(cli);
1344 static struct cfs_hash_ops nrs_tbf_hash_ops = {
1345 .hs_hash = nrs_tbf_hop_hash,
1346 .hs_keycmp = nrs_tbf_hop_keycmp,
1347 .hs_key = nrs_tbf_hop_key,
1348 .hs_object = nrs_tbf_hop_object,
1349 .hs_get = nrs_tbf_hop_get,
1350 .hs_put = nrs_tbf_hop_put,
1351 .hs_put_locked = nrs_tbf_hop_put,
1352 .hs_exit = nrs_tbf_hop_exit,
1355 #define NRS_TBF_GENERIC_BKT_BITS 10
1356 #define NRS_TBF_GENERIC_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
1357 CFS_HASH_NO_ITEMREF | \
1361 nrs_tbf_startup(struct ptlrpc_nrs_policy *policy, struct nrs_tbf_head *head)
1363 struct nrs_tbf_cmd start;
1364 struct nrs_tbf_bucket *bkt;
1368 struct cfs_hash_bd bd;
1370 bits = nrs_tbf_jobid_hash_order();
1371 if (bits < NRS_TBF_GENERIC_BKT_BITS)
1372 bits = NRS_TBF_GENERIC_BKT_BITS;
1373 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1375 NRS_TBF_GENERIC_BKT_BITS,
1378 NRS_TBF_GENERIC_HASH_FLAGS);
1379 if (head->th_cli_hash == NULL)
1382 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
1383 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
1384 INIT_LIST_HEAD(&bkt->ntb_lru);
1387 memset(&start, 0, sizeof(start));
1388 start.u.tc_start.ts_conds_str = "*";
1390 start.u.tc_start.ts_rpc_rate = tbf_rate;
1391 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1392 start.tc_name = NRS_TBF_DEFAULT_RULE;
1393 INIT_LIST_HEAD(&start.u.tc_start.ts_conds);
1394 rc = nrs_tbf_rule_start(policy, head, &start);
1396 cfs_hash_putref(head->th_cli_hash);
1401 static struct nrs_tbf_client *
1402 nrs_tbf_cli_hash_lookup(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1405 struct hlist_node *hnode;
1406 struct nrs_tbf_client *cli;
1408 hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)key);
1412 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
1413 if (!list_empty(&cli->tc_lru))
1414 list_del_init(&cli->tc_lru);
1418 static struct nrs_tbf_client *
1419 nrs_tbf_cli_find(struct nrs_tbf_head *head, struct ptlrpc_request *req)
1421 struct nrs_tbf_client *cli;
1422 struct cfs_hash *hs = head->th_cli_hash;
1423 struct cfs_hash_bd bd;
1424 char keystr[NRS_TBF_KEY_LEN] = { '\0' };
1428 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1430 jobid = NRS_TBF_JOBID_NULL;
1431 opc = lustre_msg_get_opc(req->rq_reqmsg);
1432 snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1433 libcfs_nid2str(req->rq_peer.nid), opc);
1434 LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1435 cfs_hash_bd_get_and_lock(hs, (void *)keystr, &bd, 1);
1436 cli = nrs_tbf_cli_hash_lookup(hs, &bd, keystr);
1437 cfs_hash_bd_unlock(hs, &bd, 1);
1442 static struct nrs_tbf_client *
1443 nrs_tbf_cli_findadd(struct nrs_tbf_head *head,
1444 struct nrs_tbf_client *cli)
1447 struct nrs_tbf_client *ret;
1448 struct cfs_hash *hs = head->th_cli_hash;
1449 struct cfs_hash_bd bd;
1452 cfs_hash_bd_get_and_lock(hs, (void *)key, &bd, 1);
1453 ret = nrs_tbf_cli_hash_lookup(hs, &bd, key);
1455 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
1458 cfs_hash_bd_unlock(hs, &bd, 1);
1464 nrs_tbf_cli_put(struct nrs_tbf_head *head, struct nrs_tbf_client *cli)
1466 struct cfs_hash_bd bd;
1467 struct cfs_hash *hs = head->th_cli_hash;
1468 struct nrs_tbf_bucket *bkt;
1470 struct list_head zombies;
1472 INIT_LIST_HEAD(&zombies);
1473 cfs_hash_bd_get(hs, &cli->tc_key, &bd);
1474 bkt = cfs_hash_bd_extra_get(hs, &bd);
1475 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
1477 LASSERT(list_empty(&cli->tc_lru));
1478 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
1481 * Check and purge the LRU, there is at least one client in the LRU.
1483 hw = tbf_jobid_cache_size >> (hs->hs_cur_bits - hs->hs_bkt_bits);
1484 while (cfs_hash_bd_count_get(&bd) > hw) {
1485 if (unlikely(list_empty(&bkt->ntb_lru)))
1487 cli = list_entry(bkt->ntb_lru.next,
1488 struct nrs_tbf_client,
1490 LASSERT(atomic_read(&cli->tc_ref) == 0);
1491 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
1492 list_move(&cli->tc_lru, &zombies);
1494 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
1496 while (!list_empty(&zombies)) {
1497 cli = container_of0(zombies.next,
1498 struct nrs_tbf_client, tc_lru);
1499 list_del_init(&cli->tc_lru);
1500 nrs_tbf_cli_fini(cli);
1505 nrs_tbf_generic_cli_init(struct nrs_tbf_client *cli,
1506 struct ptlrpc_request *req)
1508 char keystr[NRS_TBF_KEY_LEN];
1512 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1514 jobid = NRS_TBF_JOBID_NULL;
1515 opc = lustre_msg_get_opc(req->rq_reqmsg);
1516 snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1517 libcfs_nid2str(req->rq_peer.nid), opc);
1519 LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1520 INIT_LIST_HEAD(&cli->tc_lru);
1521 memcpy(cli->tc_key, keystr, strlen(keystr));
1522 memcpy(cli->tc_jobid, jobid, strlen(jobid));
1523 cli->tc_nid = req->rq_peer.nid;
1524 cli->tc_opcode = opc;
1528 nrs_tbf_expression_free(struct nrs_tbf_expression *expr)
1530 LASSERT(expr->te_field >= NRS_TBF_FIELD_NID &&
1531 expr->te_field < NRS_TBF_FIELD_MAX);
1532 switch (expr->te_field) {
1533 case NRS_TBF_FIELD_NID:
1534 cfs_free_nidlist(&expr->te_cond);
1536 case NRS_TBF_FIELD_JOBID:
1537 nrs_tbf_jobid_list_free(&expr->te_cond);
1539 case NRS_TBF_FIELD_OPCODE:
1540 CFS_FREE_BITMAP(expr->te_opcodes);
1549 nrs_tbf_conjunction_free(struct nrs_tbf_conjunction *conjunction)
1551 struct nrs_tbf_expression *expression;
1552 struct nrs_tbf_expression *n;
1554 LASSERT(list_empty(&conjunction->tc_linkage));
1555 list_for_each_entry_safe(expression, n,
1556 &conjunction->tc_expressions,
1558 list_del_init(&expression->te_linkage);
1559 nrs_tbf_expression_free(expression);
1561 OBD_FREE_PTR(conjunction);
1565 nrs_tbf_conds_free(struct list_head *cond_list)
1567 struct nrs_tbf_conjunction *conjunction;
1568 struct nrs_tbf_conjunction *n;
1570 list_for_each_entry_safe(conjunction, n, cond_list, tc_linkage) {
1571 list_del_init(&conjunction->tc_linkage);
1572 nrs_tbf_conjunction_free(conjunction);
1577 nrs_tbf_generic_cmd_fini(struct nrs_tbf_cmd *cmd)
1579 if (!list_empty(&cmd->u.tc_start.ts_conds))
1580 nrs_tbf_conds_free(&cmd->u.tc_start.ts_conds);
1581 if (cmd->u.tc_start.ts_conds_str)
1582 OBD_FREE(cmd->u.tc_start.ts_conds_str,
1583 strlen(cmd->u.tc_start.ts_conds_str) + 1);
1586 #define NRS_TBF_DISJUNCTION_DELIM (',')
1587 #define NRS_TBF_CONJUNCTION_DELIM ('&')
1588 #define NRS_TBF_EXPRESSION_DELIM ('=')
1591 nrs_tbf_check_field(struct cfs_lstr *field, char *str)
1593 int len = strlen(str);
1595 return (field->ls_len == len &&
1596 strncmp(field->ls_str, str, len) == 0);
1600 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr);
1603 nrs_tbf_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
1605 struct nrs_tbf_expression *expr;
1606 struct cfs_lstr field;
1609 OBD_ALLOC(expr, sizeof(struct nrs_tbf_expression));
1613 rc = cfs_gettok(src, NRS_TBF_EXPRESSION_DELIM, &field);
1614 if (rc == 0 || src->ls_len <= 2 || src->ls_str[0] != '{' ||
1615 src->ls_str[src->ls_len - 1] != '}')
1616 GOTO(out, rc = -EINVAL);
1618 /* Skip '{' and '}' */
1622 if (nrs_tbf_check_field(&field, "nid")) {
1623 if (cfs_parse_nidlist(src->ls_str,
1625 &expr->te_cond) <= 0)
1626 GOTO(out, rc = -EINVAL);
1627 expr->te_field = NRS_TBF_FIELD_NID;
1628 } else if (nrs_tbf_check_field(&field, "jobid")) {
1629 if (nrs_tbf_jobid_list_parse(src->ls_str,
1631 &expr->te_cond) < 0)
1632 GOTO(out, rc = -EINVAL);
1633 expr->te_field = NRS_TBF_FIELD_JOBID;
1634 } else if (nrs_tbf_check_field(&field, "opcode")) {
1635 if (nrs_tbf_opcode_list_parse(src->ls_str,
1637 &expr->te_opcodes) < 0)
1638 GOTO(out, rc = -EINVAL);
1639 expr->te_field = NRS_TBF_FIELD_OPCODE;
1641 GOTO(out, rc = -EINVAL);
1643 list_add_tail(&expr->te_linkage, cond_list);
1651 nrs_tbf_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
1653 struct nrs_tbf_conjunction *conjunction;
1654 struct cfs_lstr expr;
1657 OBD_ALLOC(conjunction, sizeof(struct nrs_tbf_conjunction));
1658 if (conjunction == NULL)
1661 INIT_LIST_HEAD(&conjunction->tc_expressions);
1662 list_add_tail(&conjunction->tc_linkage, cond_list);
1664 while (src->ls_str) {
1665 rc = cfs_gettok(src, NRS_TBF_CONJUNCTION_DELIM, &expr);
1670 rc = nrs_tbf_expression_parse(&expr,
1671 &conjunction->tc_expressions);
1679 nrs_tbf_conds_parse(char *str, int len, struct list_head *cond_list)
1681 struct cfs_lstr src;
1682 struct cfs_lstr res;
1687 INIT_LIST_HEAD(cond_list);
1688 while (src.ls_str) {
1689 rc = cfs_gettok(&src, NRS_TBF_DISJUNCTION_DELIM, &res);
1694 rc = nrs_tbf_conjunction_parse(&res, cond_list);
1702 nrs_tbf_generic_parse(struct nrs_tbf_cmd *cmd, const char *id)
1706 OBD_ALLOC(cmd->u.tc_start.ts_conds_str, strlen(id) + 1);
1707 if (cmd->u.tc_start.ts_conds_str == NULL)
1710 memcpy(cmd->u.tc_start.ts_conds_str, id, strlen(id));
1712 /* Parse hybird NID and JOBID conditions */
1713 rc = nrs_tbf_conds_parse(cmd->u.tc_start.ts_conds_str,
1714 strlen(cmd->u.tc_start.ts_conds_str),
1715 &cmd->u.tc_start.ts_conds);
1717 nrs_tbf_generic_cmd_fini(cmd);
1723 nrs_tbf_expression_match(struct nrs_tbf_expression *expr,
1724 struct nrs_tbf_rule *rule,
1725 struct nrs_tbf_client *cli)
1727 switch (expr->te_field) {
1728 case NRS_TBF_FIELD_NID:
1729 return cfs_match_nid(cli->tc_nid, &expr->te_cond);
1730 case NRS_TBF_FIELD_JOBID:
1731 return nrs_tbf_jobid_list_match(&expr->te_cond, cli->tc_jobid);
1732 case NRS_TBF_FIELD_OPCODE:
1733 return cfs_bitmap_check(expr->te_opcodes, cli->tc_opcode);
1740 nrs_tbf_conjunction_match(struct nrs_tbf_conjunction *conjunction,
1741 struct nrs_tbf_rule *rule,
1742 struct nrs_tbf_client *cli)
1744 struct nrs_tbf_expression *expr;
1747 list_for_each_entry(expr, &conjunction->tc_expressions, te_linkage) {
1748 matched = nrs_tbf_expression_match(expr, rule, cli);
1757 nrs_tbf_cond_match(struct nrs_tbf_rule *rule, struct nrs_tbf_client *cli)
1759 struct nrs_tbf_conjunction *conjunction;
1762 list_for_each_entry(conjunction, &rule->tr_conds, tc_linkage) {
1763 matched = nrs_tbf_conjunction_match(conjunction, rule, cli);
1772 nrs_tbf_generic_rule_fini(struct nrs_tbf_rule *rule)
1774 if (!list_empty(&rule->tr_conds))
1775 nrs_tbf_conds_free(&rule->tr_conds);
1776 LASSERT(rule->tr_conds_str != NULL);
1777 OBD_FREE(rule->tr_conds_str, strlen(rule->tr_conds_str) + 1);
1781 nrs_tbf_rule_init(struct ptlrpc_nrs_policy *policy,
1782 struct nrs_tbf_rule *rule, struct nrs_tbf_cmd *start)
1786 LASSERT(start->u.tc_start.ts_conds_str);
1787 OBD_ALLOC(rule->tr_conds_str,
1788 strlen(start->u.tc_start.ts_conds_str) + 1);
1789 if (rule->tr_conds_str == NULL)
1792 memcpy(rule->tr_conds_str,
1793 start->u.tc_start.ts_conds_str,
1794 strlen(start->u.tc_start.ts_conds_str));
1796 INIT_LIST_HEAD(&rule->tr_conds);
1797 if (!list_empty(&start->u.tc_start.ts_conds)) {
1798 rc = nrs_tbf_conds_parse(rule->tr_conds_str,
1799 strlen(rule->tr_conds_str),
1803 nrs_tbf_generic_rule_fini(rule);
1809 nrs_tbf_generic_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1811 seq_printf(m, "%s %s %llu, ref %d\n", rule->tr_name,
1812 rule->tr_conds_str, rule->tr_rpc_rate,
1813 atomic_read(&rule->tr_ref) - 1);
1818 nrs_tbf_generic_rule_match(struct nrs_tbf_rule *rule,
1819 struct nrs_tbf_client *cli)
1821 return nrs_tbf_cond_match(rule, cli);
1824 static struct nrs_tbf_ops nrs_tbf_generic_ops = {
1825 .o_name = NRS_TBF_TYPE_GENERIC,
1826 .o_startup = nrs_tbf_startup,
1827 .o_cli_find = nrs_tbf_cli_find,
1828 .o_cli_findadd = nrs_tbf_cli_findadd,
1829 .o_cli_put = nrs_tbf_cli_put,
1830 .o_cli_init = nrs_tbf_generic_cli_init,
1831 .o_rule_init = nrs_tbf_rule_init,
1832 .o_rule_dump = nrs_tbf_generic_rule_dump,
1833 .o_rule_match = nrs_tbf_generic_rule_match,
1834 .o_rule_fini = nrs_tbf_generic_rule_fini,
1837 static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
1839 if (rule->tr_opcodes != NULL)
1840 CFS_FREE_BITMAP(rule->tr_opcodes);
1842 LASSERT(rule->tr_opcodes_str != NULL);
1843 OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
1846 static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
1849 return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
1852 static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
1854 const __u32 *opc = key;
1855 struct nrs_tbf_client *cli = hlist_entry(hnode,
1856 struct nrs_tbf_client,
1859 return *opc == cli->tc_opcode;
1862 static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
1864 struct nrs_tbf_client *cli = hlist_entry(hnode,
1865 struct nrs_tbf_client,
1868 return &cli->tc_opcode;
1871 static void *nrs_tbf_opcode_hop_object(struct hlist_node *hnode)
1873 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1876 static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
1877 struct hlist_node *hnode)
1879 struct nrs_tbf_client *cli = hlist_entry(hnode,
1880 struct nrs_tbf_client,
1883 atomic_inc(&cli->tc_ref);
1886 static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
1887 struct hlist_node *hnode)
1889 struct nrs_tbf_client *cli = hlist_entry(hnode,
1890 struct nrs_tbf_client,
1893 atomic_dec(&cli->tc_ref);
1896 static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
1897 struct hlist_node *hnode)
1899 struct nrs_tbf_client *cli = hlist_entry(hnode,
1900 struct nrs_tbf_client,
1903 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1904 "Busy TBF object from client with opcode %s, with %d refs\n",
1905 ll_opcode2str(cli->tc_opcode),
1906 atomic_read(&cli->tc_ref));
1908 nrs_tbf_cli_fini(cli);
1910 static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
1911 .hs_hash = nrs_tbf_opcode_hop_hash,
1912 .hs_keycmp = nrs_tbf_opcode_hop_keycmp,
1913 .hs_key = nrs_tbf_opcode_hop_key,
1914 .hs_object = nrs_tbf_opcode_hop_object,
1915 .hs_get = nrs_tbf_opcode_hop_get,
1916 .hs_put = nrs_tbf_opcode_hop_put,
1917 .hs_put_locked = nrs_tbf_opcode_hop_put,
1918 .hs_exit = nrs_tbf_opcode_hop_exit,
1922 nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
1923 struct nrs_tbf_head *head)
1925 struct nrs_tbf_cmd start = { 0 };
1928 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1931 NRS_TBF_NID_BKT_BITS, 0,
1934 &nrs_tbf_opcode_hash_ops,
1935 CFS_HASH_RW_BKTLOCK);
1936 if (head->th_cli_hash == NULL)
1939 start.u.tc_start.ts_opcodes = NULL;
1940 start.u.tc_start.ts_opcodes_str = "*";
1942 start.u.tc_start.ts_rpc_rate = tbf_rate;
1943 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1944 start.tc_name = NRS_TBF_DEFAULT_RULE;
1945 rc = nrs_tbf_rule_start(policy, head, &start);
1950 static struct nrs_tbf_client *
1951 nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
1952 struct ptlrpc_request *req)
1956 opc = lustre_msg_get_opc(req->rq_reqmsg);
1957 return cfs_hash_lookup(head->th_cli_hash, &opc);
1960 static struct nrs_tbf_client *
1961 nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
1962 struct nrs_tbf_client *cli)
1964 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
1969 nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
1970 struct ptlrpc_request *req)
1972 cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
1975 #define MAX_OPCODE_LEN 32
1977 nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, struct cfs_bitmap *opcodes)
1980 char opcode_str[MAX_OPCODE_LEN];
1982 if (id->ls_len + 1 > MAX_OPCODE_LEN)
1985 memcpy(opcode_str, id->ls_str, id->ls_len);
1986 opcode_str[id->ls_len] = '\0';
1988 op = ll_str2opcode(opcode_str);
1992 cfs_bitmap_set(opcodes, op);
1997 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr)
1999 struct cfs_bitmap *opcodes;
2000 struct cfs_lstr src;
2001 struct cfs_lstr res;
2005 opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
2006 if (opcodes == NULL)
2011 while (src.ls_str) {
2012 rc = cfs_gettok(&src, ' ', &res);
2017 rc = nrs_tbf_opcode_set_bit(&res, opcodes);
2023 *bitmaptr = opcodes;
2025 CFS_FREE_BITMAP(opcodes);
2030 static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
2032 if (cmd->u.tc_start.ts_opcodes)
2033 CFS_FREE_BITMAP(cmd->u.tc_start.ts_opcodes);
2035 if (cmd->u.tc_start.ts_opcodes_str)
2036 OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
2037 strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
2041 static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
2043 struct cfs_lstr src;
2047 src.ls_len = strlen(id);
2048 rc = nrs_tbf_check_id_value(&src, "opcode");
2052 OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
2053 if (cmd->u.tc_start.ts_opcodes_str == NULL)
2056 memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
2058 /* parse opcode list */
2059 rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
2060 strlen(cmd->u.tc_start.ts_opcodes_str),
2061 &cmd->u.tc_start.ts_opcodes);
2063 nrs_tbf_opcode_cmd_fini(cmd);
2069 nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
2070 struct nrs_tbf_client *cli)
2072 if (rule->tr_opcodes == NULL)
2075 return cfs_bitmap_check(rule->tr_opcodes, cli->tc_opcode);
2078 static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
2079 struct nrs_tbf_rule *rule,
2080 struct nrs_tbf_cmd *start)
2084 LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
2085 OBD_ALLOC(rule->tr_opcodes_str,
2086 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2087 if (rule->tr_opcodes_str == NULL)
2090 strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
2091 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2093 /* Default rule '*' */
2094 if (start->u.tc_start.ts_opcodes == NULL)
2097 rc = nrs_tbf_opcode_list_parse(rule->tr_opcodes_str,
2098 strlen(rule->tr_opcodes_str),
2101 OBD_FREE(rule->tr_opcodes_str,
2102 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2108 nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2110 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2111 rule->tr_opcodes_str, rule->tr_rpc_rate,
2112 atomic_read(&rule->tr_ref) - 1);
2117 struct nrs_tbf_ops nrs_tbf_opcode_ops = {
2118 .o_name = NRS_TBF_TYPE_OPCODE,
2119 .o_startup = nrs_tbf_opcode_startup,
2120 .o_cli_find = nrs_tbf_opcode_cli_find,
2121 .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
2122 .o_cli_put = nrs_tbf_nid_cli_put,
2123 .o_cli_init = nrs_tbf_opcode_cli_init,
2124 .o_rule_init = nrs_tbf_opcode_rule_init,
2125 .o_rule_dump = nrs_tbf_opcode_rule_dump,
2126 .o_rule_match = nrs_tbf_opcode_rule_match,
2127 .o_rule_fini = nrs_tbf_opcode_rule_fini,
2130 static struct nrs_tbf_type nrs_tbf_types[] = {
2132 .ntt_name = NRS_TBF_TYPE_JOBID,
2133 .ntt_flag = NRS_TBF_FLAG_JOBID,
2134 .ntt_ops = &nrs_tbf_jobid_ops,
2137 .ntt_name = NRS_TBF_TYPE_NID,
2138 .ntt_flag = NRS_TBF_FLAG_NID,
2139 .ntt_ops = &nrs_tbf_nid_ops,
2142 .ntt_name = NRS_TBF_TYPE_OPCODE,
2143 .ntt_flag = NRS_TBF_FLAG_OPCODE,
2144 .ntt_ops = &nrs_tbf_opcode_ops,
2147 .ntt_name = NRS_TBF_TYPE_GENERIC,
2148 .ntt_flag = NRS_TBF_FLAG_GENERIC,
2149 .ntt_ops = &nrs_tbf_generic_ops,
2154 * Is called before the policy transitions into
2155 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
2156 * policy-specific private data structure.
2158 * \param[in] policy The policy to start
2160 * \retval -ENOMEM OOM error
2163 * \see nrs_policy_register()
2164 * \see nrs_policy_ctl()
2166 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
2168 struct nrs_tbf_head *head;
2169 struct nrs_tbf_ops *ops;
2177 name = NRS_TBF_TYPE_GENERIC;
2178 else if (strlen(arg) < NRS_TBF_TYPE_MAX_LEN)
2181 GOTO(out, rc = -EINVAL);
2183 for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
2184 if (strcmp(name, nrs_tbf_types[i].ntt_name) == 0) {
2185 ops = nrs_tbf_types[i].ntt_ops;
2186 type = nrs_tbf_types[i].ntt_flag;
2192 GOTO(out, rc = -ENOTSUPP);
2194 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
2196 GOTO(out, rc = -ENOMEM);
2198 memcpy(head->th_type, name, strlen(name));
2199 head->th_type[strlen(name)] = '\0';
2201 head->th_type_flag = type;
2203 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
2204 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
2205 nrs_pol2cptab(policy),
2206 nrs_pol2cptid(policy));
2207 if (head->th_binheap == NULL)
2208 GOTO(out_free_head, rc = -ENOMEM);
2210 atomic_set(&head->th_rule_sequence, 0);
2211 spin_lock_init(&head->th_rule_lock);
2212 INIT_LIST_HEAD(&head->th_list);
2213 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2214 head->th_timer.function = nrs_tbf_timer_cb;
2215 rc = head->th_ops->o_startup(policy, head);
2217 GOTO(out_free_heap, rc);
2219 policy->pol_private = head;
2222 cfs_binheap_destroy(head->th_binheap);
2230 * Is called before the policy transitions into
2231 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
2232 * private data structure.
2234 * \param[in] policy The policy to stop
2236 * \see nrs_policy_stop0()
2238 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
2240 struct nrs_tbf_head *head = policy->pol_private;
2241 struct ptlrpc_nrs *nrs = policy->pol_nrs;
2242 struct nrs_tbf_rule *rule, *n;
2244 LASSERT(head != NULL);
2245 LASSERT(head->th_cli_hash != NULL);
2246 hrtimer_cancel(&head->th_timer);
2247 /* Should cleanup hash first before free rules */
2248 cfs_hash_putref(head->th_cli_hash);
2249 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
2250 list_del_init(&rule->tr_linkage);
2251 nrs_tbf_rule_put(rule);
2253 LASSERT(list_empty(&head->th_list));
2254 LASSERT(head->th_binheap != NULL);
2255 LASSERT(cfs_binheap_is_empty(head->th_binheap));
2256 cfs_binheap_destroy(head->th_binheap);
2258 nrs->nrs_throttling = 0;
2259 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
2263 * Performs a policy-specific ctl function on TBF policy instances; similar
2266 * \param[in] policy the policy instance
2267 * \param[in] opc the opcode
2268 * \param[in,out] arg used for passing parameters and information
2270 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2271 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2273 * \retval 0 operation carried out successfully
2276 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
2277 enum ptlrpc_nrs_ctl opc,
2283 assert_spin_locked(&policy->pol_nrs->nrs_lock);
2285 switch ((enum nrs_ctl_tbf)opc) {
2290 * Read RPC rate size of a policy instance.
2292 case NRS_CTL_TBF_RD_RULE: {
2293 struct nrs_tbf_head *head = policy->pol_private;
2294 struct seq_file *m = (struct seq_file *) arg;
2295 struct ptlrpc_service_part *svcpt;
2297 svcpt = policy->pol_nrs->nrs_svcpt;
2298 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
2300 rc = nrs_tbf_rule_dump_all(head, m);
2305 * Write RPC rate of a policy instance.
2307 case NRS_CTL_TBF_WR_RULE: {
2308 struct nrs_tbf_head *head = policy->pol_private;
2309 struct nrs_tbf_cmd *cmd;
2311 cmd = (struct nrs_tbf_cmd *)arg;
2312 rc = nrs_tbf_command(policy,
2318 * Read the TBF policy type of a policy instance.
2320 case NRS_CTL_TBF_RD_TYPE_FLAG: {
2321 struct nrs_tbf_head *head = policy->pol_private;
2323 *(__u32 *)arg = head->th_type_flag;
2332 * Is called for obtaining a TBF policy resource.
2334 * \param[in] policy The policy on which the request is being asked for
2335 * \param[in] nrq The request for which resources are being taken
2336 * \param[in] parent Parent resource, unused in this policy
2337 * \param[out] resp Resources references are placed in this array
2338 * \param[in] moving_req Signifies limited caller context; unused in this
2342 * \see nrs_resource_get_safe()
2344 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
2345 struct ptlrpc_nrs_request *nrq,
2346 const struct ptlrpc_nrs_resource *parent,
2347 struct ptlrpc_nrs_resource **resp,
2350 struct nrs_tbf_head *head;
2351 struct nrs_tbf_client *cli;
2352 struct nrs_tbf_client *tmp;
2353 struct ptlrpc_request *req;
2355 if (parent == NULL) {
2356 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
2360 head = container_of(parent, struct nrs_tbf_head, th_res);
2361 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
2362 cli = head->th_ops->o_cli_find(head, req);
2364 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2365 LASSERT(cli->tc_rule);
2366 if (cli->tc_rule_sequence !=
2367 atomic_read(&head->th_rule_sequence) ||
2368 cli->tc_rule->tr_flags & NTRS_STOPPING) {
2369 struct nrs_tbf_rule *rule;
2372 "TBF class@%p rate %llu sequence %d, "
2373 "rule flags %d, head sequence %d\n",
2374 cli, cli->tc_rpc_rate,
2375 cli->tc_rule_sequence,
2376 cli->tc_rule->tr_flags,
2377 atomic_read(&head->th_rule_sequence));
2378 rule = nrs_tbf_rule_match(head, cli);
2379 if (rule != cli->tc_rule) {
2380 nrs_tbf_cli_reset(head, rule, cli);
2382 if (cli->tc_rule_generation != rule->tr_generation)
2383 nrs_tbf_cli_reset_value(head, cli);
2384 nrs_tbf_rule_put(rule);
2386 } else if (cli->tc_rule_generation !=
2387 cli->tc_rule->tr_generation) {
2388 nrs_tbf_cli_reset_value(head, cli);
2390 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2394 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
2395 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
2399 nrs_tbf_cli_init(head, cli, req);
2400 tmp = head->th_ops->o_cli_findadd(head, cli);
2402 atomic_dec(&cli->tc_ref);
2403 nrs_tbf_cli_fini(cli);
2407 *resp = &cli->tc_res;
2413 * Called when releasing references to the resource hierachy obtained for a
2414 * request for scheduling using the TBF policy.
2416 * \param[in] policy the policy the resource belongs to
2417 * \param[in] res the resource to be released
2419 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
2420 const struct ptlrpc_nrs_resource *res)
2422 struct nrs_tbf_head *head;
2423 struct nrs_tbf_client *cli;
2426 * Do nothing for freeing parent, nrs_tbf_net resources
2428 if (res->res_parent == NULL)
2431 cli = container_of(res, struct nrs_tbf_client, tc_res);
2432 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
2434 head->th_ops->o_cli_put(head, cli);
2438 * Called when getting a request from the TBF policy for handling, or just
2439 * peeking; removes the request from the policy when it is to be handled.
2441 * \param[in] policy The policy
2442 * \param[in] peek When set, signifies that we just want to examine the
2443 * request, and not handle it, so the request is not removed
2445 * \param[in] force Force the policy to return a request; unused in this
2448 * \retval The request to be handled; this is the next request in the TBF
2451 * \see ptlrpc_nrs_req_get_nolock()
2452 * \see nrs_request_get()
2455 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
2456 bool peek, bool force)
2458 struct nrs_tbf_head *head = policy->pol_private;
2459 struct ptlrpc_nrs_request *nrq = NULL;
2460 struct nrs_tbf_client *cli;
2461 struct cfs_binheap_node *node;
2463 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2465 if (!peek && policy->pol_nrs->nrs_throttling)
2468 node = cfs_binheap_root(head->th_binheap);
2469 if (unlikely(node == NULL))
2472 cli = container_of(node, struct nrs_tbf_client, tc_node);
2473 LASSERT(cli->tc_in_heap);
2475 nrq = list_entry(cli->tc_list.next,
2476 struct ptlrpc_nrs_request,
2479 struct nrs_tbf_rule *rule = cli->tc_rule;
2480 __u64 now = ktime_to_ns(ktime_get());
2484 __u64 old_resid = 0;
2486 deadline = cli->tc_check_time +
2488 LASSERT(now >= cli->tc_check_time);
2489 passed = now - cli->tc_check_time;
2490 ntoken = passed * cli->tc_rpc_rate;
2491 do_div(ntoken, NSEC_PER_SEC);
2493 ntoken += cli->tc_ntoken;
2494 if (rule->tr_flags & NTRS_REALTIME) {
2495 LASSERT(cli->tc_nsecs_resid < cli->tc_nsecs);
2496 old_resid = cli->tc_nsecs_resid;
2497 cli->tc_nsecs_resid += passed % cli->tc_nsecs;
2498 if (cli->tc_nsecs_resid > cli->tc_nsecs) {
2500 cli->tc_nsecs_resid -= cli->tc_nsecs;
2502 } else if (ntoken > cli->tc_depth)
2503 ntoken = cli->tc_depth;
2506 struct ptlrpc_request *req;
2507 nrq = list_entry(cli->tc_list.next,
2508 struct ptlrpc_nrs_request,
2510 req = container_of(nrq,
2511 struct ptlrpc_request,
2514 cli->tc_ntoken = ntoken;
2515 cli->tc_check_time = now;
2516 list_del_init(&nrq->nr_u.tbf.tr_list);
2517 if (list_empty(&cli->tc_list)) {
2518 cfs_binheap_remove(head->th_binheap,
2520 cli->tc_in_heap = false;
2522 if (!(rule->tr_flags & NTRS_REALTIME))
2523 cli->tc_deadline = now + cli->tc_nsecs;
2524 cfs_binheap_relocate(head->th_binheap,
2528 "TBF dequeues: class@%p rate %llu gen %llu "
2529 "token %llu, rule@%p rate %llu gen %llu\n",
2530 cli, cli->tc_rpc_rate,
2531 cli->tc_rule_generation, cli->tc_ntoken,
2532 cli->tc_rule, cli->tc_rule->tr_rpc_rate,
2533 cli->tc_rule->tr_generation);
2537 if (rule->tr_flags & NTRS_REALTIME) {
2538 cli->tc_deadline = deadline;
2539 cli->tc_nsecs_resid = old_resid;
2540 cfs_binheap_relocate(head->th_binheap,
2542 if (node != cfs_binheap_root(head->th_binheap))
2543 return nrs_tbf_req_get(policy,
2546 policy->pol_nrs->nrs_throttling = 1;
2547 head->th_deadline = deadline;
2548 time = ktime_set(0, 0);
2549 time = ktime_add_ns(time, deadline);
2550 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
2558 * Adds request \a nrq to \a policy's list of queued requests
2560 * \param[in] policy The policy
2561 * \param[in] nrq The request to add
2563 * \retval 0 success; nrs_request_enqueue() assumes this function will always
2566 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
2567 struct ptlrpc_nrs_request *nrq)
2569 struct nrs_tbf_head *head;
2570 struct nrs_tbf_client *cli;
2573 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2575 cli = container_of(nrs_request_resource(nrq),
2576 struct nrs_tbf_client, tc_res);
2577 head = container_of(nrs_request_resource(nrq)->res_parent,
2578 struct nrs_tbf_head, th_res);
2579 if (list_empty(&cli->tc_list)) {
2580 LASSERT(!cli->tc_in_heap);
2581 cli->tc_deadline = cli->tc_check_time + cli->tc_nsecs;
2582 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
2584 cli->tc_in_heap = true;
2585 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2586 list_add_tail(&nrq->nr_u.tbf.tr_list,
2588 if (policy->pol_nrs->nrs_throttling) {
2589 __u64 deadline = cli->tc_deadline;
2590 if ((head->th_deadline > deadline) &&
2591 (hrtimer_try_to_cancel(&head->th_timer)
2594 head->th_deadline = deadline;
2595 time = ktime_set(0, 0);
2596 time = ktime_add_ns(time, deadline);
2597 hrtimer_start(&head->th_timer, time,
2603 LASSERT(cli->tc_in_heap);
2604 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2605 list_add_tail(&nrq->nr_u.tbf.tr_list,
2611 "TBF enqueues: class@%p rate %llu gen %llu "
2612 "token %llu, rule@%p rate %llu gen %llu\n",
2613 cli, cli->tc_rpc_rate,
2614 cli->tc_rule_generation, cli->tc_ntoken,
2615 cli->tc_rule, cli->tc_rule->tr_rpc_rate,
2616 cli->tc_rule->tr_generation);
2622 * Removes request \a nrq from \a policy's list of queued requests.
2624 * \param[in] policy The policy
2625 * \param[in] nrq The request to remove
2627 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
2628 struct ptlrpc_nrs_request *nrq)
2630 struct nrs_tbf_head *head;
2631 struct nrs_tbf_client *cli;
2633 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2635 cli = container_of(nrs_request_resource(nrq),
2636 struct nrs_tbf_client, tc_res);
2637 head = container_of(nrs_request_resource(nrq)->res_parent,
2638 struct nrs_tbf_head, th_res);
2640 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
2641 list_del_init(&nrq->nr_u.tbf.tr_list);
2642 if (list_empty(&cli->tc_list)) {
2643 cfs_binheap_remove(head->th_binheap,
2645 cli->tc_in_heap = false;
2647 cfs_binheap_relocate(head->th_binheap,
2653 * Prints a debug statement right before the request \a nrq stops being
2656 * \param[in] policy The policy handling the request
2657 * \param[in] nrq The request being handled
2659 * \see ptlrpc_server_finish_request()
2660 * \see ptlrpc_nrs_req_stop_nolock()
2662 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
2663 struct ptlrpc_nrs_request *nrq)
2665 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
2668 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2670 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
2671 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
2672 nrq->nr_u.tbf.tr_sequence);
2675 #ifdef CONFIG_PROC_FS
2682 * The maximum RPC rate.
2684 #define LPROCFS_NRS_RATE_MAX 65535
2687 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
2689 struct ptlrpc_service *svc = m->private;
2692 seq_printf(m, "regular_requests:\n");
2694 * Perform two separate calls to this as only one of the NRS heads'
2695 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
2696 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
2698 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
2700 NRS_CTL_TBF_RD_RULE,
2704 * -ENOSPC means buf in the parameter m is overflow, return 0
2705 * here to let upper layer function seq_read alloc a larger
2706 * memory area and do this process again.
2708 } else if (rc == -ENOSPC) {
2712 * Ignore -ENODEV as the regular NRS head's policy may be in the
2713 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
2715 } else if (rc != -ENODEV) {
2719 if (!nrs_svc_has_hp(svc))
2722 seq_printf(m, "high_priority_requests:\n");
2723 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
2725 NRS_CTL_TBF_RD_RULE,
2729 * -ENOSPC means buf in the parameter m is overflow, return 0
2730 * here to let upper layer function seq_read alloc a larger
2731 * memory area and do this process again.
2733 } else if (rc == -ENOSPC) {
2742 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
2746 switch (cmd->u.tc_start.ts_valid_type) {
2747 case NRS_TBF_FLAG_JOBID:
2748 rc = nrs_tbf_jobid_parse(cmd, token);
2750 case NRS_TBF_FLAG_NID:
2751 rc = nrs_tbf_nid_parse(cmd, token);
2753 case NRS_TBF_FLAG_OPCODE:
2754 rc = nrs_tbf_opcode_parse(cmd, token);
2756 case NRS_TBF_FLAG_GENERIC:
2757 rc = nrs_tbf_generic_parse(cmd, token);
2766 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
2768 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2769 if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_JOBID)
2770 nrs_tbf_jobid_cmd_fini(cmd);
2771 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_NID)
2772 nrs_tbf_nid_cmd_fini(cmd);
2773 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_OPCODE)
2774 nrs_tbf_opcode_cmd_fini(cmd);
2775 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_GENERIC)
2776 nrs_tbf_generic_cmd_fini(cmd);
2780 static bool name_is_valid(const char *name)
2784 for (i = 0; i < strlen(name); i++) {
2785 if ((!isalnum(name[i])) &&
2793 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
2801 key = strsep(&val, "=");
2802 if (val == NULL || strlen(val) == 0)
2805 /* Key of the value pair */
2806 if (strcmp(key, "rate") == 0) {
2807 rc = kstrtoull(val, 10, &rate);
2811 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
2814 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2815 cmd->u.tc_start.ts_rpc_rate = rate;
2816 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2817 cmd->u.tc_change.tc_rpc_rate = rate;
2820 } else if (strcmp(key, "rank") == 0) {
2821 if (!name_is_valid(val))
2824 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2825 cmd->u.tc_start.ts_next_name = val;
2826 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2827 cmd->u.tc_change.tc_next_name = val;
2830 } else if (strcmp(key, "realtime") == 0) {
2831 unsigned long realtime;
2833 rc = kstrtoul(val, 10, &realtime);
2838 cmd->u.tc_start.ts_rule_flags |= NTRS_REALTIME;
2846 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
2853 while (val != NULL && strlen(val) != 0) {
2854 token = strsep(&val, " ");
2855 rc = nrs_tbf_parse_value_pair(cmd, token);
2860 switch (cmd->tc_cmd) {
2861 case NRS_CTL_TBF_START_RULE:
2862 if (cmd->u.tc_start.ts_rpc_rate == 0)
2863 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
2865 case NRS_CTL_TBF_CHANGE_RULE:
2866 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
2867 cmd->u.tc_change.tc_next_name == NULL)
2870 case NRS_CTL_TBF_STOP_RULE:
2878 static struct nrs_tbf_cmd *
2879 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
2881 static struct nrs_tbf_cmd *cmd;
2888 GOTO(out, rc = -ENOMEM);
2889 memset(cmd, 0, sizeof(*cmd));
2892 token = strsep(&val, " ");
2893 if (val == NULL || strlen(val) == 0)
2894 GOTO(out_free_cmd, rc = -EINVAL);
2896 /* Type of the command */
2897 if (strcmp(token, "start") == 0) {
2898 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
2899 cmd->u.tc_start.ts_valid_type = type_flag;
2900 } else if (strcmp(token, "stop") == 0)
2901 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
2902 else if (strcmp(token, "change") == 0)
2903 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
2905 GOTO(out_free_cmd, rc = -EINVAL);
2907 /* Name of the rule */
2908 token = strsep(&val, " ");
2909 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
2910 !name_is_valid(token))
2911 GOTO(out_free_cmd, rc = -EINVAL);
2912 cmd->tc_name = token;
2914 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2918 val = strrchr(token, '}');
2920 GOTO(out_free_cmd, rc = -EINVAL);
2926 } else if (*val == ' ') {
2930 GOTO(out_free_cmd, rc = -EINVAL);
2932 rc = nrs_tbf_id_parse(cmd, token);
2934 GOTO(out_free_cmd, rc);
2937 rc = nrs_tbf_parse_value_pairs(cmd, val);
2939 GOTO(out_cmd_fini, rc = -EINVAL);
2942 nrs_tbf_cmd_fini(cmd);
2952 * Get the TBF policy type (nid, jobid, etc) preset by
2953 * proc entry 'nrs_policies' for command buffer parsing.
2955 * \param[in] svc the PTLRPC service
2956 * \param[in] queue the NRS queue type
2958 * \retval the preset TBF policy type flag
2961 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
2966 rc = ptlrpc_nrs_policy_control(svc, queue,
2968 NRS_CTL_TBF_RD_TYPE_FLAG,
2971 type = NRS_TBF_FLAG_INVALID;
2976 extern struct nrs_core nrs_core;
2977 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
2979 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
2980 const char __user *buffer,
2981 size_t count, loff_t *off)
2983 struct seq_file *m = file->private_data;
2984 struct ptlrpc_service *svc = m->private;
2988 static struct nrs_tbf_cmd *cmd;
2989 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
2990 unsigned long length;
2993 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2994 if (kernbuf == NULL)
2995 GOTO(out, rc = -ENOMEM);
2997 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
2998 GOTO(out_free_kernbuff, rc = -EINVAL);
3000 if (copy_from_user(kernbuf, buffer, count))
3001 GOTO(out_free_kernbuff, rc = -EFAULT);
3004 token = strsep(&val, " ");
3006 GOTO(out_free_kernbuff, rc = -EINVAL);
3008 if (strcmp(token, "reg") == 0) {
3009 queue = PTLRPC_NRS_QUEUE_REG;
3010 } else if (strcmp(token, "hp") == 0) {
3011 queue = PTLRPC_NRS_QUEUE_HP;
3013 kernbuf[strlen(token)] = ' ';
3016 length = strlen(val);
3019 GOTO(out_free_kernbuff, rc = -EINVAL);
3021 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
3022 GOTO(out_free_kernbuff, rc = -ENODEV);
3023 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
3024 queue = PTLRPC_NRS_QUEUE_REG;
3026 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
3028 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
3031 * Serialize NRS core lprocfs operations with policy registration/
3034 mutex_lock(&nrs_core.nrs_mutex);
3035 rc = ptlrpc_nrs_policy_control(svc, queue,
3037 NRS_CTL_TBF_WR_RULE,
3039 mutex_unlock(&nrs_core.nrs_mutex);
3041 nrs_tbf_cmd_fini(cmd);
3044 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
3046 return rc ? rc : count;
3048 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
3051 * Initializes a TBF policy's lprocfs interface for service \a svc
3053 * \param[in] svc the service
3056 * \retval != 0 error
3058 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
3060 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
3061 { .name = "nrs_tbf_rule",
3062 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
3067 if (svc->srv_procroot == NULL)
3070 return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
3074 * Cleans up a TBF policy's lprocfs interface for service \a svc
3076 * \param[in] svc the service
3078 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
3080 if (svc->srv_procroot == NULL)
3083 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
3086 #endif /* CONFIG_PROC_FS */
3089 * TBF policy operations
3091 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
3092 .op_policy_start = nrs_tbf_start,
3093 .op_policy_stop = nrs_tbf_stop,
3094 .op_policy_ctl = nrs_tbf_ctl,
3095 .op_res_get = nrs_tbf_res_get,
3096 .op_res_put = nrs_tbf_res_put,
3097 .op_req_get = nrs_tbf_req_get,
3098 .op_req_enqueue = nrs_tbf_req_add,
3099 .op_req_dequeue = nrs_tbf_req_del,
3100 .op_req_stop = nrs_tbf_req_stop,
3101 #ifdef CONFIG_PROC_FS
3102 .op_lprocfs_init = nrs_tbf_lprocfs_init,
3103 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
3108 * TBF policy configuration
3110 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
3111 .nc_name = NRS_POL_NAME_TBF,
3112 .nc_ops = &nrs_tbf_ops,
3113 .nc_compat = nrs_policy_compat_all,
3120 #endif /* HAVE_SERVER_SUPPORT */