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, 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 cli->tc_in_heap = false;
258 head->th_ops->o_cli_init(cli, req);
259 INIT_LIST_HEAD(&cli->tc_list);
260 INIT_LIST_HEAD(&cli->tc_linkage);
261 spin_lock_init(&cli->tc_rule_lock);
262 atomic_set(&cli->tc_ref, 1);
263 rule = nrs_tbf_rule_match(head, cli);
264 nrs_tbf_cli_reset(head, rule, cli);
268 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
270 LASSERT(list_empty(&cli->tc_list));
271 LASSERT(!cli->tc_in_heap);
272 LASSERT(atomic_read(&cli->tc_ref) == 0);
273 spin_lock(&cli->tc_rule_lock);
274 nrs_tbf_cli_rule_put(cli);
275 spin_unlock(&cli->tc_rule_lock);
280 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
281 struct nrs_tbf_head *head,
282 struct nrs_tbf_cmd *start)
284 struct nrs_tbf_rule *rule;
285 struct nrs_tbf_rule *tmp_rule;
286 struct nrs_tbf_rule *next_rule;
287 char *next_name = start->u.tc_start.ts_next_name;
290 rule = nrs_tbf_rule_find(head, start->tc_name);
292 nrs_tbf_rule_put(rule);
296 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
300 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
301 rule->tr_rpc_rate = start->u.tc_start.ts_rpc_rate;
302 rule->tr_nsecs = NSEC_PER_SEC;
303 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
304 rule->tr_depth = tbf_depth;
305 atomic_set(&rule->tr_ref, 1);
306 INIT_LIST_HEAD(&rule->tr_cli_list);
307 INIT_LIST_HEAD(&rule->tr_nids);
308 INIT_LIST_HEAD(&rule->tr_linkage);
309 spin_lock_init(&rule->tr_rule_lock);
310 rule->tr_head = head;
312 rc = head->th_ops->o_rule_init(policy, rule, start);
318 /* Add as the newest rule */
319 spin_lock(&head->th_rule_lock);
320 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
322 spin_unlock(&head->th_rule_lock);
323 nrs_tbf_rule_put(tmp_rule);
324 nrs_tbf_rule_put(rule);
329 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
331 spin_unlock(&head->th_rule_lock);
332 nrs_tbf_rule_put(rule);
336 list_add(&rule->tr_linkage, next_rule->tr_linkage.prev);
337 nrs_tbf_rule_put(next_rule);
339 /* Add on the top of the rule list */
340 list_add(&rule->tr_linkage, &head->th_list);
342 spin_unlock(&head->th_rule_lock);
343 atomic_inc(&head->th_rule_sequence);
344 if (start->u.tc_start.ts_rule_flags & NTRS_DEFAULT) {
345 rule->tr_flags |= NTRS_DEFAULT;
346 LASSERT(head->th_rule == NULL);
347 head->th_rule = rule;
354 * Change the rank of a rule in the rule list
356 * The matched rule will be moved to the position right before another
359 * \param[in] policy the policy instance
360 * \param[in] head the TBF policy instance
361 * \param[in] name the rule name to be moved
362 * \param[in] next_name the rule name before which the matched rule will be
367 nrs_tbf_rule_change_rank(struct ptlrpc_nrs_policy *policy,
368 struct nrs_tbf_head *head,
372 struct nrs_tbf_rule *rule = NULL;
373 struct nrs_tbf_rule *next_rule = NULL;
376 LASSERT(head != NULL);
378 spin_lock(&head->th_rule_lock);
379 rule = nrs_tbf_rule_find_nolock(head, name);
381 GOTO(out, rc = -ENOENT);
383 if (strcmp(name, next_name) == 0)
386 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
388 GOTO(out_put, rc = -ENOENT);
390 list_move(&rule->tr_linkage, next_rule->tr_linkage.prev);
391 nrs_tbf_rule_put(next_rule);
393 nrs_tbf_rule_put(rule);
395 spin_unlock(&head->th_rule_lock);
400 nrs_tbf_rule_change_rate(struct ptlrpc_nrs_policy *policy,
401 struct nrs_tbf_head *head,
405 struct nrs_tbf_rule *rule;
407 assert_spin_locked(&policy->pol_nrs->nrs_lock);
409 rule = nrs_tbf_rule_find(head, name);
413 rule->tr_rpc_rate = rate;
414 rule->tr_nsecs = NSEC_PER_SEC;
415 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
416 rule->tr_generation++;
417 nrs_tbf_rule_put(rule);
423 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
424 struct nrs_tbf_head *head,
425 struct nrs_tbf_cmd *change)
427 __u64 rate = change->u.tc_change.tc_rpc_rate;
428 char *next_name = change->u.tc_change.tc_next_name;
432 rc = nrs_tbf_rule_change_rate(policy, head, change->tc_name,
439 rc = nrs_tbf_rule_change_rank(policy, head, change->tc_name,
449 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
450 struct nrs_tbf_head *head,
451 struct nrs_tbf_cmd *stop)
453 struct nrs_tbf_rule *rule;
455 assert_spin_locked(&policy->pol_nrs->nrs_lock);
457 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
460 rule = nrs_tbf_rule_find(head, stop->tc_name);
464 list_del_init(&rule->tr_linkage);
465 rule->tr_flags |= NTRS_STOPPING;
466 nrs_tbf_rule_put(rule);
467 nrs_tbf_rule_put(rule);
473 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
474 struct nrs_tbf_head *head,
475 struct nrs_tbf_cmd *cmd)
479 assert_spin_locked(&policy->pol_nrs->nrs_lock);
481 switch (cmd->tc_cmd) {
482 case NRS_CTL_TBF_START_RULE:
483 if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
486 spin_unlock(&policy->pol_nrs->nrs_lock);
487 rc = nrs_tbf_rule_start(policy, head, cmd);
488 spin_lock(&policy->pol_nrs->nrs_lock);
490 case NRS_CTL_TBF_CHANGE_RULE:
491 rc = nrs_tbf_rule_change(policy, head, cmd);
493 case NRS_CTL_TBF_STOP_RULE:
494 rc = nrs_tbf_rule_stop(policy, head, cmd);
495 /* Take it as a success, if not exists at all */
496 return rc == -ENOENT ? 0 : rc;
503 * Binary heap predicate.
505 * \param[in] e1 the first binheap node to compare
506 * \param[in] e2 the second binheap node to compare
512 tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
514 struct nrs_tbf_client *cli1;
515 struct nrs_tbf_client *cli2;
517 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
518 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
520 if (cli1->tc_check_time + cli1->tc_nsecs <
521 cli2->tc_check_time + cli2->tc_nsecs)
523 else if (cli1->tc_check_time + cli1->tc_nsecs >
524 cli2->tc_check_time + cli2->tc_nsecs)
527 if (cli1->tc_check_time < cli2->tc_check_time)
529 else if (cli1->tc_check_time > cli2->tc_check_time)
532 /* Maybe need more comparasion, e.g. request number in the rules */
537 * TBF binary heap operations
539 static struct cfs_binheap_ops nrs_tbf_heap_ops = {
542 .hop_compare = tbf_cli_compare,
545 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
548 return cfs_hash_djb2_hash(key, strlen(key), mask);
551 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
553 struct nrs_tbf_client *cli = hlist_entry(hnode,
554 struct nrs_tbf_client,
557 return (strcmp(cli->tc_jobid, key) == 0);
560 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
562 struct nrs_tbf_client *cli = hlist_entry(hnode,
563 struct nrs_tbf_client,
566 return cli->tc_jobid;
569 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
571 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
574 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
576 struct nrs_tbf_client *cli = hlist_entry(hnode,
577 struct nrs_tbf_client,
580 atomic_inc(&cli->tc_ref);
583 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
585 struct nrs_tbf_client *cli = hlist_entry(hnode,
586 struct nrs_tbf_client,
589 atomic_dec(&cli->tc_ref);
593 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
596 struct nrs_tbf_client *cli = hlist_entry(hnode,
597 struct nrs_tbf_client,
600 LASSERT(atomic_read(&cli->tc_ref) == 0);
601 nrs_tbf_cli_fini(cli);
604 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
605 .hs_hash = nrs_tbf_jobid_hop_hash,
606 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
607 .hs_key = nrs_tbf_jobid_hop_key,
608 .hs_object = nrs_tbf_jobid_hop_object,
609 .hs_get = nrs_tbf_jobid_hop_get,
610 .hs_put = nrs_tbf_jobid_hop_put,
611 .hs_put_locked = nrs_tbf_jobid_hop_put,
612 .hs_exit = nrs_tbf_jobid_hop_exit,
615 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
616 CFS_HASH_NO_ITEMREF | \
619 static struct nrs_tbf_client *
620 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
621 struct cfs_hash_bd *bd,
624 struct hlist_node *hnode;
625 struct nrs_tbf_client *cli;
627 /* cfs_hash_bd_peek_locked is a somehow "internal" function
628 * of cfs_hash, it doesn't add refcount on object. */
629 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
633 cfs_hash_get(hs, hnode);
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);
794 * Frees jobid of \a list.
798 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
800 struct nrs_tbf_jobid *jobid, *n;
802 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
803 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
804 list_del(&jobid->tj_linkage);
805 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
810 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
812 struct nrs_tbf_jobid *jobid;
814 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
818 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
819 if (jobid->tj_id == NULL) {
820 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
824 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
825 list_add_tail(&jobid->tj_linkage, jobid_list);
830 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
832 struct nrs_tbf_jobid *jobid;
834 list_for_each_entry(jobid, jobid_list, tj_linkage) {
835 if (strcmp(id, jobid->tj_id) == 0)
842 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
851 INIT_LIST_HEAD(jobid_list);
853 rc = cfs_gettok(&src, ' ', &res);
858 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
863 nrs_tbf_jobid_list_free(jobid_list);
867 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
869 if (!list_empty(&cmd->u.tc_start.ts_jobids))
870 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
871 if (cmd->u.tc_start.ts_jobids_str)
872 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
873 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
876 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
879 int keylen = strlen(key);
882 rc = cfs_gettok(src, '=', &res);
883 if (rc == 0 || res.ls_len != keylen ||
884 strncmp(res.ls_str, key, keylen) != 0 ||
885 src->ls_len <= 2 || src->ls_str[0] != '{' ||
886 src->ls_str[src->ls_len - 1] != '}')
889 /* Skip '{' and '}' */
895 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
901 src.ls_len = strlen(id);
902 rc = nrs_tbf_check_id_value(&src, "jobid");
906 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
907 if (cmd->u.tc_start.ts_jobids_str == NULL)
910 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
912 /* parse jobid list */
913 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
914 strlen(cmd->u.tc_start.ts_jobids_str),
915 &cmd->u.tc_start.ts_jobids);
917 nrs_tbf_jobid_cmd_fini(cmd);
922 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
923 struct nrs_tbf_rule *rule,
924 struct nrs_tbf_cmd *start)
928 LASSERT(start->u.tc_start.ts_jobids_str);
929 OBD_ALLOC(rule->tr_jobids_str,
930 strlen(start->u.tc_start.ts_jobids_str) + 1);
931 if (rule->tr_jobids_str == NULL)
934 memcpy(rule->tr_jobids_str,
935 start->u.tc_start.ts_jobids_str,
936 strlen(start->u.tc_start.ts_jobids_str));
938 INIT_LIST_HEAD(&rule->tr_jobids);
939 if (!list_empty(&start->u.tc_start.ts_jobids)) {
940 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
941 strlen(rule->tr_jobids_str),
944 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
947 OBD_FREE(rule->tr_jobids_str,
948 strlen(start->u.tc_start.ts_jobids_str) + 1);
953 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
955 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
956 rule->tr_jobids_str, rule->tr_rpc_rate,
957 atomic_read(&rule->tr_ref) - 1);
962 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
963 struct nrs_tbf_client *cli)
965 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
968 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
970 if (!list_empty(&rule->tr_jobids))
971 nrs_tbf_jobid_list_free(&rule->tr_jobids);
972 LASSERT(rule->tr_jobids_str != NULL);
973 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
976 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
977 .o_name = NRS_TBF_TYPE_JOBID,
978 .o_startup = nrs_tbf_jobid_startup,
979 .o_cli_find = nrs_tbf_jobid_cli_find,
980 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
981 .o_cli_put = nrs_tbf_jobid_cli_put,
982 .o_cli_init = nrs_tbf_jobid_cli_init,
983 .o_rule_init = nrs_tbf_jobid_rule_init,
984 .o_rule_dump = nrs_tbf_jobid_rule_dump,
985 .o_rule_match = nrs_tbf_jobid_rule_match,
986 .o_rule_fini = nrs_tbf_jobid_rule_fini,
990 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
992 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
993 * nrs_tbf_client objects.
995 #define NRS_TBF_NID_BKT_BITS 8
996 #define NRS_TBF_NID_BITS 16
998 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1001 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1004 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1006 lnet_nid_t *nid = (lnet_nid_t *)key;
1007 struct nrs_tbf_client *cli = hlist_entry(hnode,
1008 struct nrs_tbf_client,
1011 return *nid == cli->tc_nid;
1014 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1016 struct nrs_tbf_client *cli = hlist_entry(hnode,
1017 struct nrs_tbf_client,
1020 return &cli->tc_nid;
1023 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
1025 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1028 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1030 struct nrs_tbf_client *cli = hlist_entry(hnode,
1031 struct nrs_tbf_client,
1034 atomic_inc(&cli->tc_ref);
1037 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1039 struct nrs_tbf_client *cli = hlist_entry(hnode,
1040 struct nrs_tbf_client,
1043 atomic_dec(&cli->tc_ref);
1046 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1048 struct nrs_tbf_client *cli = hlist_entry(hnode,
1049 struct nrs_tbf_client,
1052 LASSERTF(atomic_read(&cli->tc_ref) == 0,
1053 "Busy TBF object from client with NID %s, with %d refs\n",
1054 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1056 nrs_tbf_cli_fini(cli);
1059 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1060 .hs_hash = nrs_tbf_nid_hop_hash,
1061 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
1062 .hs_key = nrs_tbf_nid_hop_key,
1063 .hs_object = nrs_tbf_nid_hop_object,
1064 .hs_get = nrs_tbf_nid_hop_get,
1065 .hs_put = nrs_tbf_nid_hop_put,
1066 .hs_put_locked = nrs_tbf_nid_hop_put,
1067 .hs_exit = nrs_tbf_nid_hop_exit,
1070 static struct nrs_tbf_client *
1071 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1072 struct ptlrpc_request *req)
1074 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
1077 static struct nrs_tbf_client *
1078 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1079 struct nrs_tbf_client *cli)
1081 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1086 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1087 struct nrs_tbf_client *cli)
1089 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1093 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1094 struct nrs_tbf_head *head)
1096 struct nrs_tbf_cmd start;
1099 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1102 NRS_TBF_NID_BKT_BITS, 0,
1105 &nrs_tbf_nid_hash_ops,
1106 CFS_HASH_RW_BKTLOCK);
1107 if (head->th_cli_hash == NULL)
1110 memset(&start, 0, sizeof(start));
1111 start.u.tc_start.ts_nids_str = "*";
1113 start.u.tc_start.ts_rpc_rate = tbf_rate;
1114 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1115 start.tc_name = NRS_TBF_DEFAULT_RULE;
1116 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1117 rc = nrs_tbf_rule_start(policy, head, &start);
1123 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1124 struct ptlrpc_request *req)
1126 cli->tc_nid = req->rq_peer.nid;
1129 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1130 struct nrs_tbf_rule *rule,
1131 struct nrs_tbf_cmd *start)
1133 LASSERT(start->u.tc_start.ts_nids_str);
1134 OBD_ALLOC(rule->tr_nids_str,
1135 strlen(start->u.tc_start.ts_nids_str) + 1);
1136 if (rule->tr_nids_str == NULL)
1139 memcpy(rule->tr_nids_str,
1140 start->u.tc_start.ts_nids_str,
1141 strlen(start->u.tc_start.ts_nids_str));
1143 INIT_LIST_HEAD(&rule->tr_nids);
1144 if (!list_empty(&start->u.tc_start.ts_nids)) {
1145 if (cfs_parse_nidlist(rule->tr_nids_str,
1146 strlen(rule->tr_nids_str),
1147 &rule->tr_nids) <= 0) {
1148 CERROR("nids {%s} illegal\n",
1150 OBD_FREE(rule->tr_nids_str,
1151 strlen(start->u.tc_start.ts_nids_str) + 1);
1159 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1161 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1162 rule->tr_nids_str, rule->tr_rpc_rate,
1163 atomic_read(&rule->tr_ref) - 1);
1168 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1169 struct nrs_tbf_client *cli)
1171 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1174 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1176 if (!list_empty(&rule->tr_nids))
1177 cfs_free_nidlist(&rule->tr_nids);
1178 LASSERT(rule->tr_nids_str != NULL);
1179 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1182 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1184 if (!list_empty(&cmd->u.tc_start.ts_nids))
1185 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1186 if (cmd->u.tc_start.ts_nids_str)
1187 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1188 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1191 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1193 struct cfs_lstr src;
1197 src.ls_len = strlen(id);
1198 rc = nrs_tbf_check_id_value(&src, "nid");
1202 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1203 if (cmd->u.tc_start.ts_nids_str == NULL)
1206 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1208 /* parse NID list */
1209 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1210 strlen(cmd->u.tc_start.ts_nids_str),
1211 &cmd->u.tc_start.ts_nids) <= 0) {
1212 nrs_tbf_nid_cmd_fini(cmd);
1219 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1220 .o_name = NRS_TBF_TYPE_NID,
1221 .o_startup = nrs_tbf_nid_startup,
1222 .o_cli_find = nrs_tbf_nid_cli_find,
1223 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1224 .o_cli_put = nrs_tbf_nid_cli_put,
1225 .o_cli_init = nrs_tbf_nid_cli_init,
1226 .o_rule_init = nrs_tbf_nid_rule_init,
1227 .o_rule_dump = nrs_tbf_nid_rule_dump,
1228 .o_rule_match = nrs_tbf_nid_rule_match,
1229 .o_rule_fini = nrs_tbf_nid_rule_fini,
1233 * Is called before the policy transitions into
1234 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1235 * policy-specific private data structure.
1237 * \param[in] policy The policy to start
1239 * \retval -ENOMEM OOM error
1242 * \see nrs_policy_register()
1243 * \see nrs_policy_ctl()
1245 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1247 struct nrs_tbf_head *head;
1248 struct nrs_tbf_ops *ops;
1252 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1253 GOTO(out, rc = -EINVAL);
1255 if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1256 ops = &nrs_tbf_nid_ops;
1257 type = NRS_TBF_FLAG_NID;
1258 } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1259 ops = &nrs_tbf_jobid_ops;
1260 type = NRS_TBF_FLAG_JOBID;
1262 GOTO(out, rc = -ENOTSUPP);
1264 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1266 GOTO(out, rc = -ENOMEM);
1268 memcpy(head->th_type, arg, strlen(arg));
1269 head->th_type[strlen(arg)] = '\0';
1271 head->th_type_flag = type;
1273 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1274 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1275 nrs_pol2cptab(policy),
1276 nrs_pol2cptid(policy));
1277 if (head->th_binheap == NULL)
1278 GOTO(out_free_head, rc = -ENOMEM);
1280 atomic_set(&head->th_rule_sequence, 0);
1281 spin_lock_init(&head->th_rule_lock);
1282 INIT_LIST_HEAD(&head->th_list);
1283 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1284 head->th_timer.function = nrs_tbf_timer_cb;
1285 rc = head->th_ops->o_startup(policy, head);
1287 GOTO(out_free_heap, rc);
1289 policy->pol_private = head;
1292 cfs_binheap_destroy(head->th_binheap);
1300 * Is called before the policy transitions into
1301 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1302 * private data structure.
1304 * \param[in] policy The policy to stop
1306 * \see nrs_policy_stop0()
1308 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1310 struct nrs_tbf_head *head = policy->pol_private;
1311 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1312 struct nrs_tbf_rule *rule, *n;
1314 LASSERT(head != NULL);
1315 LASSERT(head->th_cli_hash != NULL);
1316 hrtimer_cancel(&head->th_timer);
1317 /* Should cleanup hash first before free rules */
1318 cfs_hash_putref(head->th_cli_hash);
1319 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1320 list_del_init(&rule->tr_linkage);
1321 nrs_tbf_rule_put(rule);
1323 LASSERT(list_empty(&head->th_list));
1324 LASSERT(head->th_binheap != NULL);
1325 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1326 cfs_binheap_destroy(head->th_binheap);
1328 nrs->nrs_throttling = 0;
1329 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1333 * Performs a policy-specific ctl function on TBF policy instances; similar
1336 * \param[in] policy the policy instance
1337 * \param[in] opc the opcode
1338 * \param[in,out] arg used for passing parameters and information
1340 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1341 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1343 * \retval 0 operation carried out successfully
1346 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
1347 enum ptlrpc_nrs_ctl opc,
1353 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1355 switch ((enum nrs_ctl_tbf)opc) {
1360 * Read RPC rate size of a policy instance.
1362 case NRS_CTL_TBF_RD_RULE: {
1363 struct nrs_tbf_head *head = policy->pol_private;
1364 struct seq_file *m = (struct seq_file *) arg;
1365 struct ptlrpc_service_part *svcpt;
1367 svcpt = policy->pol_nrs->nrs_svcpt;
1368 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1370 rc = nrs_tbf_rule_dump_all(head, m);
1375 * Write RPC rate of a policy instance.
1377 case NRS_CTL_TBF_WR_RULE: {
1378 struct nrs_tbf_head *head = policy->pol_private;
1379 struct nrs_tbf_cmd *cmd;
1381 cmd = (struct nrs_tbf_cmd *)arg;
1382 rc = nrs_tbf_command(policy,
1388 * Read the TBF policy type of a policy instance.
1390 case NRS_CTL_TBF_RD_TYPE_FLAG: {
1391 struct nrs_tbf_head *head = policy->pol_private;
1393 *(__u32 *)arg = head->th_type_flag;
1402 * Is called for obtaining a TBF policy resource.
1404 * \param[in] policy The policy on which the request is being asked for
1405 * \param[in] nrq The request for which resources are being taken
1406 * \param[in] parent Parent resource, unused in this policy
1407 * \param[out] resp Resources references are placed in this array
1408 * \param[in] moving_req Signifies limited caller context; unused in this
1412 * \see nrs_resource_get_safe()
1414 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1415 struct ptlrpc_nrs_request *nrq,
1416 const struct ptlrpc_nrs_resource *parent,
1417 struct ptlrpc_nrs_resource **resp,
1420 struct nrs_tbf_head *head;
1421 struct nrs_tbf_client *cli;
1422 struct nrs_tbf_client *tmp;
1423 struct ptlrpc_request *req;
1425 if (parent == NULL) {
1426 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1430 head = container_of(parent, struct nrs_tbf_head, th_res);
1431 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1432 cli = head->th_ops->o_cli_find(head, req);
1434 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1435 LASSERT(cli->tc_rule);
1436 if (cli->tc_rule_sequence !=
1437 atomic_read(&head->th_rule_sequence) ||
1438 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1439 struct nrs_tbf_rule *rule;
1441 rule = nrs_tbf_rule_match(head, cli);
1442 if (rule != cli->tc_rule)
1443 nrs_tbf_cli_reset(head, rule, cli);
1445 nrs_tbf_rule_put(rule);
1446 } else if (cli->tc_rule_generation !=
1447 cli->tc_rule->tr_generation) {
1448 nrs_tbf_cli_reset_value(head, cli);
1450 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1454 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1455 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1458 nrs_tbf_cli_init(head, cli, req);
1459 tmp = head->th_ops->o_cli_findadd(head, cli);
1461 atomic_dec(&cli->tc_ref);
1462 nrs_tbf_cli_fini(cli);
1466 *resp = &cli->tc_res;
1472 * Called when releasing references to the resource hierachy obtained for a
1473 * request for scheduling using the TBF policy.
1475 * \param[in] policy the policy the resource belongs to
1476 * \param[in] res the resource to be released
1478 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1479 const struct ptlrpc_nrs_resource *res)
1481 struct nrs_tbf_head *head;
1482 struct nrs_tbf_client *cli;
1485 * Do nothing for freeing parent, nrs_tbf_net resources
1487 if (res->res_parent == NULL)
1490 cli = container_of(res, struct nrs_tbf_client, tc_res);
1491 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1493 head->th_ops->o_cli_put(head, cli);
1497 * Called when getting a request from the TBF policy for handling, or just
1498 * peeking; removes the request from the policy when it is to be handled.
1500 * \param[in] policy The policy
1501 * \param[in] peek When set, signifies that we just want to examine the
1502 * request, and not handle it, so the request is not removed
1504 * \param[in] force Force the policy to return a request; unused in this
1507 * \retval The request to be handled; this is the next request in the TBF
1510 * \see ptlrpc_nrs_req_get_nolock()
1511 * \see nrs_request_get()
1514 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1515 bool peek, bool force)
1517 struct nrs_tbf_head *head = policy->pol_private;
1518 struct ptlrpc_nrs_request *nrq = NULL;
1519 struct nrs_tbf_client *cli;
1520 struct cfs_binheap_node *node;
1522 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1524 if (!peek && policy->pol_nrs->nrs_throttling)
1527 node = cfs_binheap_root(head->th_binheap);
1528 if (unlikely(node == NULL))
1531 cli = container_of(node, struct nrs_tbf_client, tc_node);
1532 LASSERT(cli->tc_in_heap);
1534 nrq = list_entry(cli->tc_list.next,
1535 struct ptlrpc_nrs_request,
1538 __u64 now = ktime_to_ns(ktime_get());
1543 deadline = cli->tc_check_time +
1545 LASSERT(now >= cli->tc_check_time);
1546 passed = now - cli->tc_check_time;
1547 ntoken = passed * cli->tc_rpc_rate;
1548 do_div(ntoken, NSEC_PER_SEC);
1549 ntoken += cli->tc_ntoken;
1550 if (ntoken > cli->tc_depth)
1551 ntoken = cli->tc_depth;
1553 struct ptlrpc_request *req;
1554 nrq = list_entry(cli->tc_list.next,
1555 struct ptlrpc_nrs_request,
1557 req = container_of(nrq,
1558 struct ptlrpc_request,
1561 cli->tc_ntoken = ntoken;
1562 cli->tc_check_time = now;
1563 list_del_init(&nrq->nr_u.tbf.tr_list);
1564 if (list_empty(&cli->tc_list)) {
1565 cfs_binheap_remove(head->th_binheap,
1567 cli->tc_in_heap = false;
1569 cfs_binheap_relocate(head->th_binheap,
1573 "NRS start %s request from %s, "
1575 policy->pol_desc->pd_name,
1576 libcfs_id2str(req->rq_peer),
1577 nrq->nr_u.tbf.tr_sequence);
1581 policy->pol_nrs->nrs_throttling = 1;
1582 head->th_deadline = deadline;
1583 time = ktime_set(0, 0);
1584 time = ktime_add_ns(time, deadline);
1585 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1593 * Adds request \a nrq to \a policy's list of queued requests
1595 * \param[in] policy The policy
1596 * \param[in] nrq The request to add
1598 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1601 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1602 struct ptlrpc_nrs_request *nrq)
1604 struct nrs_tbf_head *head;
1605 struct nrs_tbf_client *cli;
1608 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1610 cli = container_of(nrs_request_resource(nrq),
1611 struct nrs_tbf_client, tc_res);
1612 head = container_of(nrs_request_resource(nrq)->res_parent,
1613 struct nrs_tbf_head, th_res);
1614 if (list_empty(&cli->tc_list)) {
1615 LASSERT(!cli->tc_in_heap);
1616 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1618 cli->tc_in_heap = true;
1619 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1620 list_add_tail(&nrq->nr_u.tbf.tr_list,
1622 if (policy->pol_nrs->nrs_throttling) {
1623 __u64 deadline = cli->tc_check_time +
1625 if ((head->th_deadline > deadline) &&
1626 (hrtimer_try_to_cancel(&head->th_timer)
1629 head->th_deadline = deadline;
1630 time = ktime_set(0, 0);
1631 time = ktime_add_ns(time, deadline);
1632 hrtimer_start(&head->th_timer, time,
1638 LASSERT(cli->tc_in_heap);
1639 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1640 list_add_tail(&nrq->nr_u.tbf.tr_list,
1647 * Removes request \a nrq from \a policy's list of queued requests.
1649 * \param[in] policy The policy
1650 * \param[in] nrq The request to remove
1652 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1653 struct ptlrpc_nrs_request *nrq)
1655 struct nrs_tbf_head *head;
1656 struct nrs_tbf_client *cli;
1658 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1660 cli = container_of(nrs_request_resource(nrq),
1661 struct nrs_tbf_client, tc_res);
1662 head = container_of(nrs_request_resource(nrq)->res_parent,
1663 struct nrs_tbf_head, th_res);
1665 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1666 list_del_init(&nrq->nr_u.tbf.tr_list);
1667 if (list_empty(&cli->tc_list)) {
1668 cfs_binheap_remove(head->th_binheap,
1670 cli->tc_in_heap = false;
1672 cfs_binheap_relocate(head->th_binheap,
1678 * Prints a debug statement right before the request \a nrq stops being
1681 * \param[in] policy The policy handling the request
1682 * \param[in] nrq The request being handled
1684 * \see ptlrpc_server_finish_request()
1685 * \see ptlrpc_nrs_req_stop_nolock()
1687 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1688 struct ptlrpc_nrs_request *nrq)
1690 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1693 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1695 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
1696 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1697 nrq->nr_u.tbf.tr_sequence);
1700 #ifdef CONFIG_PROC_FS
1707 * The maximum RPC rate.
1709 #define LPROCFS_NRS_RATE_MAX 65535
1712 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1714 struct ptlrpc_service *svc = m->private;
1717 seq_printf(m, "regular_requests:\n");
1719 * Perform two separate calls to this as only one of the NRS heads'
1720 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1721 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1723 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1725 NRS_CTL_TBF_RD_RULE,
1729 * -ENOSPC means buf in the parameter m is overflow, return 0
1730 * here to let upper layer function seq_read alloc a larger
1731 * memory area and do this process again.
1733 } else if (rc == -ENOSPC) {
1737 * Ignore -ENODEV as the regular NRS head's policy may be in the
1738 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1740 } else if (rc != -ENODEV) {
1744 if (!nrs_svc_has_hp(svc))
1747 seq_printf(m, "high_priority_requests:\n");
1748 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1750 NRS_CTL_TBF_RD_RULE,
1754 * -ENOSPC means buf in the parameter m is overflow, return 0
1755 * here to let upper layer function seq_read alloc a larger
1756 * memory area and do this process again.
1758 } else if (rc == -ENOSPC) {
1767 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
1771 if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_JOBID)
1772 rc = nrs_tbf_jobid_parse(cmd, token);
1773 else if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_NID)
1774 rc = nrs_tbf_nid_parse(cmd, token);
1775 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_INVALID)
1784 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1786 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1787 if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_JOBID)
1788 nrs_tbf_jobid_cmd_fini(cmd);
1789 else if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_NID)
1790 nrs_tbf_nid_cmd_fini(cmd);
1794 static bool name_is_valid(const char *name)
1798 for (i = 0; i < strlen(name); i++) {
1799 if ((!isalnum(name[i])) &&
1807 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
1815 key = strsep(&val, "=");
1816 if (val == NULL || strlen(val) == 0)
1819 /* Key of the value pair */
1820 if (strcmp(key, "rate") == 0) {
1821 rc = kstrtoull(val, 10, &rate);
1825 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
1828 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
1829 cmd->u.tc_start.ts_rpc_rate = rate;
1830 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
1831 cmd->u.tc_change.tc_rpc_rate = rate;
1834 } else if (strcmp(key, "rank") == 0) {
1835 if (!name_is_valid(val))
1838 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
1839 cmd->u.tc_start.ts_next_name = val;
1840 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
1841 cmd->u.tc_change.tc_next_name = val;
1851 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
1858 while (val != NULL && strlen(val) != 0) {
1859 token = strsep(&val, " ");
1860 rc = nrs_tbf_parse_value_pair(cmd, token);
1865 switch (cmd->tc_cmd) {
1866 case NRS_CTL_TBF_START_RULE:
1867 if (cmd->u.tc_start.ts_rpc_rate == 0)
1868 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
1870 case NRS_CTL_TBF_CHANGE_RULE:
1871 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
1872 cmd->u.tc_change.tc_next_name == NULL)
1875 case NRS_CTL_TBF_STOP_RULE:
1883 static struct nrs_tbf_cmd *
1884 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
1886 static struct nrs_tbf_cmd *cmd;
1893 GOTO(out, rc = -ENOMEM);
1894 memset(cmd, 0, sizeof(*cmd));
1897 token = strsep(&val, " ");
1898 if (val == NULL || strlen(val) == 0)
1899 GOTO(out_free_cmd, rc = -EINVAL);
1901 /* Type of the command */
1902 if (strcmp(token, "start") == 0) {
1903 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1904 cmd->u.tc_start.ts_valid_type = type_flag;
1905 } else if (strcmp(token, "stop") == 0)
1906 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1907 else if (strcmp(token, "change") == 0)
1908 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
1910 GOTO(out_free_cmd, rc = -EINVAL);
1912 /* Name of the rule */
1913 token = strsep(&val, " ");
1914 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
1915 !name_is_valid(token))
1916 GOTO(out_free_cmd, rc = -EINVAL);
1917 cmd->tc_name = token;
1919 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1923 val = strrchr(token, '}');
1925 GOTO(out_free_cmd, rc = -EINVAL);
1931 } else if (*val == ' ') {
1935 GOTO(out_free_cmd, rc = -EINVAL);
1937 rc = nrs_tbf_id_parse(cmd, token);
1939 GOTO(out_free_cmd, rc);
1942 rc = nrs_tbf_parse_value_pairs(cmd, val);
1944 GOTO(out_cmd_fini, rc = -EINVAL);
1947 nrs_tbf_cmd_fini(cmd);
1957 * Get the TBF policy type (nid, jobid, etc) preset by
1958 * proc entry 'nrs_policies' for command buffer parsing.
1960 * \param[in] svc the PTLRPC service
1961 * \param[in] queue the NRS queue type
1963 * \retval the preset TBF policy type flag
1966 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
1971 rc = ptlrpc_nrs_policy_control(svc, queue,
1973 NRS_CTL_TBF_RD_TYPE_FLAG,
1976 type = NRS_TBF_FLAG_INVALID;
1981 extern struct nrs_core nrs_core;
1982 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1984 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
1985 const char __user *buffer,
1986 size_t count, loff_t *off)
1988 struct seq_file *m = file->private_data;
1989 struct ptlrpc_service *svc = m->private;
1993 static struct nrs_tbf_cmd *cmd;
1994 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1995 unsigned long length;
1998 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1999 if (kernbuf == NULL)
2000 GOTO(out, rc = -ENOMEM);
2002 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
2003 GOTO(out_free_kernbuff, rc = -EINVAL);
2005 if (copy_from_user(kernbuf, buffer, count))
2006 GOTO(out_free_kernbuff, rc = -EFAULT);
2009 token = strsep(&val, " ");
2011 GOTO(out_free_kernbuff, rc = -EINVAL);
2013 if (strcmp(token, "reg") == 0) {
2014 queue = PTLRPC_NRS_QUEUE_REG;
2015 } else if (strcmp(token, "hp") == 0) {
2016 queue = PTLRPC_NRS_QUEUE_HP;
2018 kernbuf[strlen(token)] = ' ';
2021 length = strlen(val);
2024 GOTO(out_free_kernbuff, rc = -EINVAL);
2026 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
2027 GOTO(out_free_kernbuff, rc = -ENODEV);
2028 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
2029 queue = PTLRPC_NRS_QUEUE_REG;
2031 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
2033 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
2036 * Serialize NRS core lprocfs operations with policy registration/
2039 mutex_lock(&nrs_core.nrs_mutex);
2040 rc = ptlrpc_nrs_policy_control(svc, queue,
2042 NRS_CTL_TBF_WR_RULE,
2044 mutex_unlock(&nrs_core.nrs_mutex);
2046 nrs_tbf_cmd_fini(cmd);
2049 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2051 return rc ? rc : count;
2053 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
2056 * Initializes a TBF policy's lprocfs interface for service \a svc
2058 * \param[in] svc the service
2061 * \retval != 0 error
2063 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
2065 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
2066 { .name = "nrs_tbf_rule",
2067 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
2072 if (svc->srv_procroot == NULL)
2075 return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
2079 * Cleans up a TBF policy's lprocfs interface for service \a svc
2081 * \param[in] svc the service
2083 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
2085 if (svc->srv_procroot == NULL)
2088 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
2091 #endif /* CONFIG_PROC_FS */
2094 * TBF policy operations
2096 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
2097 .op_policy_start = nrs_tbf_start,
2098 .op_policy_stop = nrs_tbf_stop,
2099 .op_policy_ctl = nrs_tbf_ctl,
2100 .op_res_get = nrs_tbf_res_get,
2101 .op_res_put = nrs_tbf_res_put,
2102 .op_req_get = nrs_tbf_req_get,
2103 .op_req_enqueue = nrs_tbf_req_add,
2104 .op_req_dequeue = nrs_tbf_req_del,
2105 .op_req_stop = nrs_tbf_req_stop,
2106 #ifdef CONFIG_PROC_FS
2107 .op_lprocfs_init = nrs_tbf_lprocfs_init,
2108 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
2113 * TBF policy configuration
2115 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
2116 .nc_name = NRS_POL_NAME_TBF,
2117 .nc_ops = &nrs_tbf_ops,
2118 .nc_compat = nrs_policy_compat_all,
2125 #endif /* HAVE_SERVER_SUPPORT */