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.
27 * lustre/ptlrpc/nrs_tbf.c
29 * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
33 #ifdef HAVE_SERVER_SUPPORT
40 #define DEBUG_SUBSYSTEM S_RPC
42 #include <liblustre.h>
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <libcfs/libcfs.h>
47 #include "ptlrpc_internal.h"
52 * Token Bucket Filter over client NIDs
57 #define NRS_POL_NAME_TBF "tbf"
59 int tbf_jobid_cache_size = 8192;
60 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
61 "The size of jobid cache");
64 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
65 "Default rate limit in RPCs/s");
68 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
69 "How many tokens that a client can save up");
71 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
73 struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
75 struct ptlrpc_nrs *nrs = head->th_res.res_policy->pol_nrs;
76 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
78 spin_lock(&nrs->nrs_lock);
79 nrs->nrs_throttling = 0;
80 spin_unlock(&nrs->nrs_lock);
81 wake_up(&svcpt->scp_waitq);
83 return HRTIMER_NORESTART;
86 #define NRS_TBF_DEFAULT_RULE "default"
88 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
90 LASSERT(atomic_read(&rule->tr_ref) == 0);
91 LASSERT(list_empty(&rule->tr_cli_list));
92 LASSERT(list_empty(&rule->tr_linkage));
94 rule->tr_head->th_ops->o_rule_fini(rule);
99 * Decreases the rule's usage reference count, and stops the rule in case it
100 * was already stopping and have no more outstanding usage references (which
101 * indicates it has no more queued or started requests, and can be safely
104 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
106 if (atomic_dec_and_test(&rule->tr_ref))
107 nrs_tbf_rule_fini(rule);
111 * Increases the rule's usage reference count.
113 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
115 atomic_inc(&rule->tr_ref);
119 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
121 LASSERT(!cfs_list_empty(&cli->tc_linkage));
122 LASSERT(cli->tc_rule);
123 cfs_list_del_init(&cli->tc_linkage);
124 nrs_tbf_rule_put(cli->tc_rule);
129 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
130 struct nrs_tbf_client *cli)
133 struct nrs_tbf_rule *rule = cli->tc_rule;
135 cli->tc_rpc_rate = rule->tr_rpc_rate;
136 cli->tc_nsecs = rule->tr_nsecs;
137 cli->tc_depth = rule->tr_depth;
138 cli->tc_ntoken = rule->tr_depth;
139 cli->tc_check_time = ktime_to_ns(ktime_get());
140 cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
141 cli->tc_rule_generation = rule->tr_generation;
144 cfs_binheap_relocate(head->th_binheap,
149 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
150 struct nrs_tbf_rule *rule,
151 struct nrs_tbf_client *cli)
153 if (!list_empty(&cli->tc_linkage)) {
154 LASSERT(rule != cli->tc_rule);
155 nrs_tbf_cli_rule_put(cli);
157 LASSERT(cli->tc_rule == NULL);
158 LASSERT(list_empty(&cli->tc_linkage));
159 /* Rule's ref is added before called */
161 list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
162 nrs_tbf_cli_reset_value(head, cli);
166 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
168 return rule->tr_head->th_ops->o_rule_dump(rule, m);
172 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
174 struct nrs_tbf_rule *rule;
177 LASSERT(head != NULL);
178 spin_lock(&head->th_rule_lock);
179 /* List the rules from newest to oldest */
180 list_for_each_entry(rule, &head->th_list, tr_linkage) {
181 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
182 rc = nrs_tbf_rule_dump(rule, m);
188 spin_unlock(&head->th_rule_lock);
193 static struct nrs_tbf_rule *
194 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
197 struct nrs_tbf_rule *rule;
199 LASSERT(head != NULL);
200 cfs_list_for_each_entry(rule, &head->th_list, tr_linkage) {
201 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
202 if (strcmp(rule->tr_name, name) == 0) {
203 nrs_tbf_rule_get(rule);
210 static struct nrs_tbf_rule *
211 nrs_tbf_rule_find(struct nrs_tbf_head *head,
214 struct nrs_tbf_rule *rule;
216 LASSERT(head != NULL);
217 spin_lock(&head->th_rule_lock);
218 rule = nrs_tbf_rule_find_nolock(head, name);
219 spin_unlock(&head->th_rule_lock);
223 static struct nrs_tbf_rule *
224 nrs_tbf_rule_match(struct nrs_tbf_head *head,
225 struct nrs_tbf_client *cli)
227 struct nrs_tbf_rule *rule = NULL;
228 struct nrs_tbf_rule *tmp_rule;
230 spin_lock(&head->th_rule_lock);
231 /* Match the newest rule in the list */
232 cfs_list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
233 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
234 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
241 rule = head->th_rule;
243 nrs_tbf_rule_get(rule);
244 spin_unlock(&head->th_rule_lock);
249 nrs_tbf_cli_init(struct nrs_tbf_head *head,
250 struct nrs_tbf_client *cli,
251 struct ptlrpc_request *req)
253 struct nrs_tbf_rule *rule;
255 cli->tc_in_heap = false;
256 head->th_ops->o_cli_init(cli, req);
257 CFS_INIT_LIST_HEAD(&cli->tc_list);
258 CFS_INIT_LIST_HEAD(&cli->tc_linkage);
259 atomic_set(&cli->tc_ref, 1);
260 rule = nrs_tbf_rule_match(head, cli);
261 nrs_tbf_cli_reset(head, rule, cli);
265 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
267 LASSERT(cfs_list_empty(&cli->tc_list));
268 LASSERT(!cli->tc_in_heap);
269 LASSERT(atomic_read(&cli->tc_ref) == 0);
270 nrs_tbf_cli_rule_put(cli);
275 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
276 struct nrs_tbf_head *head,
277 struct nrs_tbf_cmd *start)
279 struct nrs_tbf_rule *rule, *tmp_rule;
282 rule = nrs_tbf_rule_find(head, start->tc_name);
284 nrs_tbf_rule_put(rule);
288 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
292 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
293 rule->tr_rpc_rate = start->tc_rpc_rate;
294 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
295 rule->tr_depth = tbf_depth;
296 atomic_set(&rule->tr_ref, 1);
297 CFS_INIT_LIST_HEAD(&rule->tr_cli_list);
298 CFS_INIT_LIST_HEAD(&rule->tr_nids);
300 rc = head->th_ops->o_rule_init(policy, rule, start);
306 /* Add as the newest rule */
307 spin_lock(&head->th_rule_lock);
308 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
310 nrs_tbf_rule_put(tmp_rule);
311 nrs_tbf_rule_put(rule);
314 cfs_list_add(&rule->tr_linkage, &head->th_list);
315 rule->tr_head = head;
316 spin_unlock(&head->th_rule_lock);
317 atomic_inc(&head->th_rule_sequence);
318 if (start->tc_rule_flags & NTRS_DEFAULT) {
319 rule->tr_flags |= NTRS_DEFAULT;
320 LASSERT(head->th_rule == NULL);
321 head->th_rule = rule;
328 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
329 struct nrs_tbf_head *head,
330 struct nrs_tbf_cmd *change)
332 struct nrs_tbf_rule *rule;
334 assert_spin_locked(&policy->pol_nrs->nrs_lock);
336 rule = nrs_tbf_rule_find(head, change->tc_name);
340 rule->tr_rpc_rate = change->tc_rpc_rate;
341 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
342 rule->tr_generation++;
343 nrs_tbf_rule_put(rule);
349 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
350 struct nrs_tbf_head *head,
351 struct nrs_tbf_cmd *stop)
353 struct nrs_tbf_rule *rule;
355 assert_spin_locked(&policy->pol_nrs->nrs_lock);
357 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
360 rule = nrs_tbf_rule_find(head, stop->tc_name);
364 cfs_list_del_init(&rule->tr_linkage);
365 rule->tr_flags |= NTRS_STOPPING;
366 nrs_tbf_rule_put(rule);
367 nrs_tbf_rule_put(rule);
373 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
374 struct nrs_tbf_head *head,
375 struct nrs_tbf_cmd *cmd)
379 assert_spin_locked(&policy->pol_nrs->nrs_lock);
381 switch (cmd->tc_cmd) {
382 case NRS_CTL_TBF_START_RULE:
383 if (!(cmd->tc_valid_types & head->th_type_flag))
386 spin_unlock(&policy->pol_nrs->nrs_lock);
387 rc = nrs_tbf_rule_start(policy, head, cmd);
388 spin_lock(&policy->pol_nrs->nrs_lock);
390 case NRS_CTL_TBF_CHANGE_RATE:
391 rc = nrs_tbf_rule_change(policy, head, cmd);
393 case NRS_CTL_TBF_STOP_RULE:
394 rc = nrs_tbf_rule_stop(policy, head, cmd);
395 /* Take it as a success, if not exists at all */
396 return rc == -ENOENT ? 0 : rc;
403 * Binary heap predicate.
405 * \param[in] e1 the first binheap node to compare
406 * \param[in] e2 the second binheap node to compare
411 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
413 struct nrs_tbf_client *cli1;
414 struct nrs_tbf_client *cli2;
416 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
417 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
419 if (cli1->tc_check_time + cli1->tc_nsecs <
420 cli2->tc_check_time + cli2->tc_nsecs)
422 else if (cli1->tc_check_time + cli1->tc_nsecs >
423 cli2->tc_check_time + cli2->tc_nsecs)
426 if (cli1->tc_check_time < cli2->tc_check_time)
428 else if (cli1->tc_check_time > cli2->tc_check_time)
431 /* Maybe need more comparasion, e.g. request number in the rules */
436 * TBF binary heap operations
438 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
441 .hop_compare = tbf_cli_compare,
444 static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
447 return cfs_hash_djb2_hash(key, strlen(key), mask);
450 static int nrs_tbf_jobid_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
452 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
453 struct nrs_tbf_client,
456 return (strcmp(cli->tc_jobid, key) == 0);
459 static void *nrs_tbf_jobid_hop_key(cfs_hlist_node_t *hnode)
461 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
462 struct nrs_tbf_client,
465 return cli->tc_jobid;
468 static void *nrs_tbf_jobid_hop_object(cfs_hlist_node_t *hnode)
470 return cfs_hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
473 static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
475 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
476 struct nrs_tbf_client,
479 atomic_inc(&cli->tc_ref);
482 static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
484 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
485 struct nrs_tbf_client,
488 atomic_dec(&cli->tc_ref);
491 static void nrs_tbf_jobid_hop_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
494 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
495 struct nrs_tbf_client,
498 LASSERT(atomic_read(&cli->tc_ref) == 0);
499 nrs_tbf_cli_fini(cli);
502 static cfs_hash_ops_t nrs_tbf_jobid_hash_ops = {
503 .hs_hash = nrs_tbf_jobid_hop_hash,
504 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
505 .hs_key = nrs_tbf_jobid_hop_key,
506 .hs_object = nrs_tbf_jobid_hop_object,
507 .hs_get = nrs_tbf_jobid_hop_get,
508 .hs_put = nrs_tbf_jobid_hop_put,
509 .hs_put_locked = nrs_tbf_jobid_hop_put,
510 .hs_exit = nrs_tbf_jobid_hop_exit,
513 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
514 CFS_HASH_NO_ITEMREF | \
517 static struct nrs_tbf_client *
518 nrs_tbf_jobid_hash_lookup(cfs_hash_t *hs,
522 cfs_hlist_node_t *hnode;
523 struct nrs_tbf_client *cli;
525 /* cfs_hash_bd_peek_locked is a somehow "internal" function
526 * of cfs_hash, it doesn't add refcount on object. */
527 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
531 cfs_hash_get(hs, hnode);
532 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
533 if (!cfs_list_empty(&cli->tc_lru))
534 cfs_list_del_init(&cli->tc_lru);
538 #define NRS_TBF_JOBID_NULL ""
540 static struct nrs_tbf_client *
541 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
542 struct ptlrpc_request *req)
545 struct nrs_tbf_client *cli;
546 cfs_hash_t *hs = head->th_cli_hash;
549 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
551 jobid = NRS_TBF_JOBID_NULL;
552 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
553 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
554 cfs_hash_bd_unlock(hs, &bd, 1);
559 static struct nrs_tbf_client *
560 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
561 struct nrs_tbf_client *cli)
564 struct nrs_tbf_client *ret;
565 cfs_hash_t *hs = head->th_cli_hash;
568 jobid = cli->tc_jobid;
569 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
570 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
572 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
575 cfs_hash_bd_unlock(hs, &bd, 1);
581 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
582 struct nrs_tbf_client *cli)
585 cfs_hash_t *hs = head->th_cli_hash;
586 struct nrs_tbf_bucket *bkt;
588 CFS_LIST_HEAD (zombies);
590 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
591 bkt = cfs_hash_bd_extra_get(hs, &bd);
592 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
594 LASSERT(cfs_list_empty(&cli->tc_lru));
595 cfs_list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
598 * Check and purge the LRU, there is at least one client in the LRU.
600 hw = tbf_jobid_cache_size >>
601 (hs->hs_cur_bits - hs->hs_bkt_bits);
602 while (cfs_hash_bd_count_get(&bd) > hw) {
603 if (unlikely(cfs_list_empty(&bkt->ntb_lru)))
605 cli = cfs_list_entry(bkt->ntb_lru.next,
606 struct nrs_tbf_client,
608 LASSERT(atomic_read(&cli->tc_ref) == 0);
609 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
610 cfs_list_move(&cli->tc_lru, &zombies);
612 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
614 while (!cfs_list_empty(&zombies)) {
615 cli = container_of0(zombies.next,
616 struct nrs_tbf_client, tc_lru);
617 cfs_list_del_init(&cli->tc_lru);
618 nrs_tbf_cli_fini(cli);
623 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
624 struct ptlrpc_request *req)
626 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
629 jobid = NRS_TBF_JOBID_NULL;
630 LASSERT(strlen(jobid) < JOBSTATS_JOBID_SIZE);
631 CFS_INIT_LIST_HEAD(&cli->tc_lru);
632 memcpy(cli->tc_jobid, jobid, strlen(jobid));
635 static int nrs_tbf_jobid_hash_order(void)
639 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
645 #define NRS_TBF_JOBID_BKT_BITS 10
648 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
649 struct nrs_tbf_head *head)
651 struct nrs_tbf_cmd start;
652 struct nrs_tbf_bucket *bkt;
658 bits = nrs_tbf_jobid_hash_order();
659 if (bits < NRS_TBF_JOBID_BKT_BITS)
660 bits = NRS_TBF_JOBID_BKT_BITS;
661 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
664 NRS_TBF_JOBID_BKT_BITS,
668 &nrs_tbf_jobid_hash_ops,
669 NRS_TBF_JOBID_HASH_FLAGS);
670 if (head->th_cli_hash == NULL)
673 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
674 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
675 CFS_INIT_LIST_HEAD(&bkt->ntb_lru);
678 memset(&start, 0, sizeof(start));
679 start.tc_jobids_str = "*";
681 start.tc_rpc_rate = tbf_rate;
682 start.tc_rule_flags = NTRS_DEFAULT;
683 start.tc_name = NRS_TBF_DEFAULT_RULE;
684 CFS_INIT_LIST_HEAD(&start.tc_jobids);
685 rc = nrs_tbf_rule_start(policy, head, &start);
691 * Frees jobid of \a list.
695 nrs_tbf_jobid_list_free(cfs_list_t *jobid_list)
697 struct nrs_tbf_jobid *jobid, *n;
699 cfs_list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
700 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
701 cfs_list_del(&jobid->tj_linkage);
702 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
707 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, cfs_list_t *jobid_list)
709 struct nrs_tbf_jobid *jobid;
711 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
715 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
716 if (jobid->tj_id == NULL) {
717 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
721 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
722 cfs_list_add_tail(&jobid->tj_linkage, jobid_list);
727 nrs_tbf_jobid_list_match(cfs_list_t *jobid_list, char *id)
729 struct nrs_tbf_jobid *jobid;
731 cfs_list_for_each_entry(jobid, jobid_list, tj_linkage) {
732 if (strcmp(id, jobid->tj_id) == 0)
739 nrs_tbf_jobid_list_parse(char *str, int len, cfs_list_t *jobid_list)
748 CFS_INIT_LIST_HEAD(jobid_list);
750 rc = cfs_gettok(&src, ' ', &res);
755 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
760 nrs_tbf_jobid_list_free(jobid_list);
764 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
766 if (!cfs_list_empty(&cmd->tc_jobids))
767 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
768 if (cmd->tc_jobids_str)
769 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
772 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
776 OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
777 if (cmd->tc_jobids_str == NULL)
780 memcpy(cmd->tc_jobids_str, id, strlen(id));
782 /* parse jobid list */
783 rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
784 strlen(cmd->tc_jobids_str),
787 nrs_tbf_jobid_cmd_fini(cmd);
792 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
793 struct nrs_tbf_rule *rule,
794 struct nrs_tbf_cmd *start)
798 LASSERT(start->tc_jobids_str);
799 OBD_ALLOC(rule->tr_jobids_str,
800 strlen(start->tc_jobids_str) + 1);
801 if (rule->tr_jobids_str == NULL)
804 memcpy(rule->tr_jobids_str,
805 start->tc_jobids_str,
806 strlen(start->tc_jobids_str));
808 CFS_INIT_LIST_HEAD(&rule->tr_jobids);
809 if (!cfs_list_empty(&start->tc_jobids)) {
810 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
811 strlen(rule->tr_jobids_str),
814 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
817 OBD_FREE(rule->tr_jobids_str,
818 strlen(start->tc_jobids_str) + 1);
823 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
825 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
826 rule->tr_jobids_str, rule->tr_rpc_rate,
827 atomic_read(&rule->tr_ref) - 1);
831 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
832 struct nrs_tbf_client *cli)
834 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
837 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
839 if (!cfs_list_empty(&rule->tr_jobids))
840 nrs_tbf_jobid_list_free(&rule->tr_jobids);
841 LASSERT(rule->tr_jobids_str != NULL);
842 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
845 struct nrs_tbf_ops nrs_tbf_jobid_ops = {
846 .o_name = NRS_TBF_TYPE_JOBID,
847 .o_startup = nrs_tbf_jobid_startup,
848 .o_cli_find = nrs_tbf_jobid_cli_find,
849 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
850 .o_cli_put = nrs_tbf_jobid_cli_put,
851 .o_cli_init = nrs_tbf_jobid_cli_init,
852 .o_rule_init = nrs_tbf_jobid_rule_init,
853 .o_rule_dump = nrs_tbf_jobid_rule_dump,
854 .o_rule_match = nrs_tbf_jobid_rule_match,
855 .o_rule_fini = nrs_tbf_jobid_rule_fini,
859 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
861 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
862 * nrs_tbf_client objects.
864 #define NRS_TBF_NID_BKT_BITS 8
865 #define NRS_TBF_NID_BITS 16
867 static unsigned nrs_tbf_nid_hop_hash(cfs_hash_t *hs, const void *key,
870 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
873 static int nrs_tbf_nid_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
875 lnet_nid_t *nid = (lnet_nid_t *)key;
876 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
877 struct nrs_tbf_client,
880 return *nid == cli->tc_nid;
883 static void *nrs_tbf_nid_hop_key(cfs_hlist_node_t *hnode)
885 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
886 struct nrs_tbf_client,
892 static void *nrs_tbf_nid_hop_object(cfs_hlist_node_t *hnode)
894 return cfs_hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
897 static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
899 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
900 struct nrs_tbf_client,
903 atomic_inc(&cli->tc_ref);
906 static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
908 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
909 struct nrs_tbf_client,
912 atomic_dec(&cli->tc_ref);
915 static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
917 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
918 struct nrs_tbf_client,
921 LASSERTF(atomic_read(&cli->tc_ref) == 0,
922 "Busy TBF object from client with NID %s, with %d refs\n",
923 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
925 nrs_tbf_cli_fini(cli);
928 static cfs_hash_ops_t nrs_tbf_nid_hash_ops = {
929 .hs_hash = nrs_tbf_nid_hop_hash,
930 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
931 .hs_key = nrs_tbf_nid_hop_key,
932 .hs_object = nrs_tbf_nid_hop_object,
933 .hs_get = nrs_tbf_nid_hop_get,
934 .hs_put = nrs_tbf_nid_hop_put,
935 .hs_put_locked = nrs_tbf_nid_hop_put,
936 .hs_exit = nrs_tbf_nid_hop_exit,
939 static struct nrs_tbf_client *
940 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
941 struct ptlrpc_request *req)
943 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
946 static struct nrs_tbf_client *
947 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
948 struct nrs_tbf_client *cli)
950 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
955 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
956 struct nrs_tbf_client *cli)
958 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
962 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
963 struct nrs_tbf_head *head)
965 struct nrs_tbf_cmd start;
968 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
971 NRS_TBF_NID_BKT_BITS, 0,
974 &nrs_tbf_nid_hash_ops,
975 CFS_HASH_RW_BKTLOCK);
976 if (head->th_cli_hash == NULL)
979 memset(&start, 0, sizeof(start));
980 start.tc_nids_str = "*";
982 start.tc_rpc_rate = tbf_rate;
983 start.tc_rule_flags = NTRS_DEFAULT;
984 start.tc_name = NRS_TBF_DEFAULT_RULE;
985 CFS_INIT_LIST_HEAD(&start.tc_nids);
986 rc = nrs_tbf_rule_start(policy, head, &start);
992 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
993 struct ptlrpc_request *req)
995 cli->tc_nid = req->rq_peer.nid;
998 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
999 struct nrs_tbf_rule *rule,
1000 struct nrs_tbf_cmd *start)
1002 LASSERT(start->tc_nids_str);
1003 OBD_ALLOC(rule->tr_nids_str,
1004 strlen(start->tc_nids_str) + 1);
1005 if (rule->tr_nids_str == NULL)
1008 memcpy(rule->tr_nids_str,
1010 strlen(start->tc_nids_str));
1012 CFS_INIT_LIST_HEAD(&rule->tr_nids);
1013 if (!cfs_list_empty(&start->tc_nids)) {
1014 if (cfs_parse_nidlist(rule->tr_nids_str,
1015 strlen(rule->tr_nids_str),
1016 &rule->tr_nids) <= 0) {
1017 CERROR("nids {%s} illegal\n",
1019 OBD_FREE(rule->tr_nids_str,
1020 strlen(start->tc_nids_str) + 1);
1028 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1030 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1031 rule->tr_nids_str, rule->tr_rpc_rate,
1032 atomic_read(&rule->tr_ref) - 1);
1036 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1037 struct nrs_tbf_client *cli)
1039 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1042 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1044 if (!cfs_list_empty(&rule->tr_nids))
1045 cfs_free_nidlist(&rule->tr_nids);
1046 LASSERT(rule->tr_nids_str != NULL);
1047 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1050 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1052 if (!cfs_list_empty(&cmd->tc_nids))
1053 cfs_free_nidlist(&cmd->tc_nids);
1054 if (cmd->tc_nids_str)
1055 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1058 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1060 OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1061 if (cmd->tc_nids_str == NULL)
1064 memcpy(cmd->tc_nids_str, id, strlen(id));
1066 /* parse NID list */
1067 if (cfs_parse_nidlist(cmd->tc_nids_str,
1068 strlen(cmd->tc_nids_str),
1069 &cmd->tc_nids) <= 0) {
1070 nrs_tbf_nid_cmd_fini(cmd);
1077 struct nrs_tbf_ops nrs_tbf_nid_ops = {
1078 .o_name = NRS_TBF_TYPE_NID,
1079 .o_startup = nrs_tbf_nid_startup,
1080 .o_cli_find = nrs_tbf_nid_cli_find,
1081 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1082 .o_cli_put = nrs_tbf_nid_cli_put,
1083 .o_cli_init = nrs_tbf_nid_cli_init,
1084 .o_rule_init = nrs_tbf_nid_rule_init,
1085 .o_rule_dump = nrs_tbf_nid_rule_dump,
1086 .o_rule_match = nrs_tbf_nid_rule_match,
1087 .o_rule_fini = nrs_tbf_nid_rule_fini,
1091 * Is called before the policy transitions into
1092 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1093 * policy-specific private data structure.
1095 * \param[in] policy The policy to start
1097 * \retval -ENOMEM OOM error
1100 * \see nrs_policy_register()
1101 * \see nrs_policy_ctl()
1103 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1105 struct nrs_tbf_head *head;
1106 struct nrs_tbf_ops *ops;
1110 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1111 GOTO(out, rc = -EINVAL);
1113 if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1114 ops = &nrs_tbf_nid_ops;
1115 type = NRS_TBF_FLAG_NID;
1116 } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1117 ops = &nrs_tbf_jobid_ops;
1118 type = NRS_TBF_FLAG_JOBID;
1120 GOTO(out, rc = -ENOTSUPP);
1122 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1124 GOTO(out, rc = -ENOMEM);
1126 memcpy(head->th_type, arg, strlen(arg));
1127 head->th_type[strlen(arg)] = '\0';
1129 head->th_type_flag = type;
1131 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1132 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1133 nrs_pol2cptab(policy),
1134 nrs_pol2cptid(policy));
1135 if (head->th_binheap == NULL)
1136 GOTO(out_free_head, rc = -ENOMEM);
1138 atomic_set(&head->th_rule_sequence, 0);
1139 spin_lock_init(&head->th_rule_lock);
1140 CFS_INIT_LIST_HEAD(&head->th_list);
1141 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1142 head->th_timer.function = nrs_tbf_timer_cb;
1143 rc = head->th_ops->o_startup(policy, head);
1145 GOTO(out_free_heap, rc);
1147 policy->pol_private = head;
1150 cfs_binheap_destroy(head->th_binheap);
1158 * Is called before the policy transitions into
1159 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1160 * private data structure.
1162 * \param[in] policy The policy to stop
1164 * \see nrs_policy_stop0()
1166 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1168 struct nrs_tbf_head *head = policy->pol_private;
1169 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1170 struct nrs_tbf_rule *rule, *n;
1172 LASSERT(head != NULL);
1173 LASSERT(head->th_cli_hash != NULL);
1174 hrtimer_cancel(&head->th_timer);
1175 /* Should cleanup hash first before free rules */
1176 cfs_hash_putref(head->th_cli_hash);
1177 cfs_list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1178 cfs_list_del_init(&rule->tr_linkage);
1179 nrs_tbf_rule_put(rule);
1181 LASSERT(cfs_list_empty(&head->th_list));
1182 LASSERT(head->th_binheap != NULL);
1183 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1184 cfs_binheap_destroy(head->th_binheap);
1186 spin_lock(&nrs->nrs_lock);
1187 nrs->nrs_throttling = 0;
1188 spin_unlock(&nrs->nrs_lock);
1189 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1193 * Performs a policy-specific ctl function on TBF policy instances; similar
1196 * \param[in] policy the policy instance
1197 * \param[in] opc the opcode
1198 * \param[in,out] arg used for passing parameters and information
1200 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1201 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1203 * \retval 0 operation carried out successfully
1206 int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy, enum ptlrpc_nrs_ctl opc,
1212 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1214 switch ((enum nrs_ctl_tbf)opc) {
1219 * Read RPC rate size of a policy instance.
1221 case NRS_CTL_TBF_RD_RULE: {
1222 struct nrs_tbf_head *head = policy->pol_private;
1223 struct seq_file *m = (struct seq_file *) arg;
1224 struct ptlrpc_service_part *svcpt;
1226 svcpt = policy->pol_nrs->nrs_svcpt;
1227 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1229 rc = nrs_tbf_rule_dump_all(head, m);
1234 * Write RPC rate of a policy instance.
1236 case NRS_CTL_TBF_WR_RULE: {
1237 struct nrs_tbf_head *head = policy->pol_private;
1238 struct nrs_tbf_cmd *cmd;
1240 cmd = (struct nrs_tbf_cmd *)arg;
1241 rc = nrs_tbf_command(policy,
1252 * Is called for obtaining a TBF policy resource.
1254 * \param[in] policy The policy on which the request is being asked for
1255 * \param[in] nrq The request for which resources are being taken
1256 * \param[in] parent Parent resource, unused in this policy
1257 * \param[out] resp Resources references are placed in this array
1258 * \param[in] moving_req Signifies limited caller context; unused in this
1262 * \see nrs_resource_get_safe()
1264 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1265 struct ptlrpc_nrs_request *nrq,
1266 const struct ptlrpc_nrs_resource *parent,
1267 struct ptlrpc_nrs_resource **resp,
1270 struct nrs_tbf_head *head;
1271 struct nrs_tbf_client *cli;
1272 struct nrs_tbf_client *tmp;
1273 struct ptlrpc_request *req;
1275 if (parent == NULL) {
1276 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1280 head = container_of(parent, struct nrs_tbf_head, th_res);
1281 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1282 cli = head->th_ops->o_cli_find(head, req);
1284 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1285 LASSERT(cli->tc_rule);
1286 if (cli->tc_rule_sequence !=
1287 atomic_read(&head->th_rule_sequence) ||
1288 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1289 struct nrs_tbf_rule *rule;
1291 rule = nrs_tbf_rule_match(head, cli);
1292 if (rule != cli->tc_rule)
1293 nrs_tbf_cli_reset(head, rule, cli);
1295 nrs_tbf_rule_put(rule);
1296 } else if (cli->tc_rule_generation !=
1297 cli->tc_rule->tr_generation) {
1298 nrs_tbf_cli_reset_value(head, cli);
1300 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1304 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1305 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1308 nrs_tbf_cli_init(head, cli, req);
1309 tmp = head->th_ops->o_cli_findadd(head, cli);
1311 atomic_dec(&cli->tc_ref);
1312 nrs_tbf_cli_fini(cli);
1316 *resp = &cli->tc_res;
1322 * Called when releasing references to the resource hierachy obtained for a
1323 * request for scheduling using the TBF policy.
1325 * \param[in] policy the policy the resource belongs to
1326 * \param[in] res the resource to be released
1328 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1329 const struct ptlrpc_nrs_resource *res)
1331 struct nrs_tbf_head *head;
1332 struct nrs_tbf_client *cli;
1335 * Do nothing for freeing parent, nrs_tbf_net resources
1337 if (res->res_parent == NULL)
1340 cli = container_of(res, struct nrs_tbf_client, tc_res);
1341 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1343 head->th_ops->o_cli_put(head, cli);
1347 * Called when getting a request from the TBF policy for handling, or just
1348 * peeking; removes the request from the policy when it is to be handled.
1350 * \param[in] policy The policy
1351 * \param[in] peek When set, signifies that we just want to examine the
1352 * request, and not handle it, so the request is not removed
1354 * \param[in] force Force the policy to return a request; unused in this
1357 * \retval The request to be handled; this is the next request in the TBF
1360 * \see ptlrpc_nrs_req_get_nolock()
1361 * \see nrs_request_get()
1364 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1365 bool peek, bool force)
1367 struct nrs_tbf_head *head = policy->pol_private;
1368 struct ptlrpc_nrs_request *nrq = NULL;
1369 struct nrs_tbf_client *cli;
1370 cfs_binheap_node_t *node;
1372 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1374 if (!peek && policy->pol_nrs->nrs_throttling)
1377 node = cfs_binheap_root(head->th_binheap);
1378 if (unlikely(node == NULL))
1381 cli = container_of(node, struct nrs_tbf_client, tc_node);
1382 LASSERT(cli->tc_in_heap);
1384 nrq = cfs_list_entry(cli->tc_list.next,
1385 struct ptlrpc_nrs_request,
1388 __u64 now = ktime_to_ns(ktime_get());
1393 deadline = cli->tc_check_time +
1395 LASSERT(now >= cli->tc_check_time);
1396 passed = now - cli->tc_check_time;
1397 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1398 ntoken += cli->tc_ntoken;
1399 if (ntoken > cli->tc_depth)
1400 ntoken = cli->tc_depth;
1402 struct ptlrpc_request *req;
1403 nrq = cfs_list_entry(cli->tc_list.next,
1404 struct ptlrpc_nrs_request,
1406 req = container_of(nrq,
1407 struct ptlrpc_request,
1410 cli->tc_ntoken = ntoken;
1411 cli->tc_check_time = now;
1412 cfs_list_del_init(&nrq->nr_u.tbf.tr_list);
1413 if (cfs_list_empty(&cli->tc_list)) {
1414 cfs_binheap_remove(head->th_binheap,
1416 cli->tc_in_heap = false;
1418 cfs_binheap_relocate(head->th_binheap,
1422 "NRS start %s request from %s, "
1424 policy->pol_desc->pd_name,
1425 libcfs_id2str(req->rq_peer),
1426 nrq->nr_u.tbf.tr_sequence);
1430 spin_lock(&policy->pol_nrs->nrs_lock);
1431 policy->pol_nrs->nrs_throttling = 1;
1432 spin_unlock(&policy->pol_nrs->nrs_lock);
1433 head->th_deadline = deadline;
1434 time = ktime_set(0, 0);
1435 time = ktime_add_ns(time, deadline);
1436 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1444 * Adds request \a nrq to \a policy's list of queued requests
1446 * \param[in] policy The policy
1447 * \param[in] nrq The request to add
1449 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1452 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1453 struct ptlrpc_nrs_request *nrq)
1455 struct nrs_tbf_head *head;
1456 struct nrs_tbf_client *cli;
1459 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1461 cli = container_of(nrs_request_resource(nrq),
1462 struct nrs_tbf_client, tc_res);
1463 head = container_of(nrs_request_resource(nrq)->res_parent,
1464 struct nrs_tbf_head, th_res);
1465 if (cfs_list_empty(&cli->tc_list)) {
1466 LASSERT(!cli->tc_in_heap);
1467 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1469 cli->tc_in_heap = true;
1470 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1471 cfs_list_add_tail(&nrq->nr_u.tbf.tr_list,
1473 if (policy->pol_nrs->nrs_throttling) {
1474 __u64 deadline = cli->tc_check_time +
1476 if ((head->th_deadline > deadline) &&
1477 (hrtimer_try_to_cancel(&head->th_timer)
1480 head->th_deadline = deadline;
1481 time = ktime_set(0, 0);
1482 time = ktime_add_ns(time, deadline);
1483 hrtimer_start(&head->th_timer, time,
1489 LASSERT(cli->tc_in_heap);
1490 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1491 cfs_list_add_tail(&nrq->nr_u.tbf.tr_list,
1498 * Removes request \a nrq from \a policy's list of queued requests.
1500 * \param[in] policy The policy
1501 * \param[in] nrq The request to remove
1503 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1504 struct ptlrpc_nrs_request *nrq)
1506 struct nrs_tbf_head *head;
1507 struct nrs_tbf_client *cli;
1509 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1511 cli = container_of(nrs_request_resource(nrq),
1512 struct nrs_tbf_client, tc_res);
1513 head = container_of(nrs_request_resource(nrq)->res_parent,
1514 struct nrs_tbf_head, th_res);
1516 LASSERT(!cfs_list_empty(&nrq->nr_u.tbf.tr_list));
1517 cfs_list_del_init(&nrq->nr_u.tbf.tr_list);
1518 if (cfs_list_empty(&cli->tc_list)) {
1519 cfs_binheap_remove(head->th_binheap,
1521 cli->tc_in_heap = false;
1523 cfs_binheap_relocate(head->th_binheap,
1529 * Prints a debug statement right before the request \a nrq stops being
1532 * \param[in] policy The policy handling the request
1533 * \param[in] nrq The request being handled
1535 * \see ptlrpc_server_finish_request()
1536 * \see ptlrpc_nrs_req_stop_nolock()
1538 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1539 struct ptlrpc_nrs_request *nrq)
1541 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1544 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1546 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1547 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1548 nrq->nr_u.tbf.tr_sequence);
1558 * The maximum RPC rate.
1560 #define LPROCFS_NRS_RATE_MAX 65535
1563 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1565 struct ptlrpc_service *svc = m->private;
1568 seq_printf(m, "regular_requests:\n");
1570 * Perform two separate calls to this as only one of the NRS heads'
1571 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1572 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1574 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1576 NRS_CTL_TBF_RD_RULE,
1580 * Ignore -ENODEV as the regular NRS head's policy may be in the
1581 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1583 } else if (rc != -ENODEV) {
1587 if (!nrs_svc_has_hp(svc))
1590 seq_printf(m, "high_priority_requests:\n");
1591 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1593 NRS_CTL_TBF_RD_RULE,
1597 * Ignore -ENODEV as the high priority NRS head's policy may be
1598 * in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1600 } else if (rc != -ENODEV) {
1609 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1614 token = strsep(val, "}");
1616 GOTO(out, rc = -EINVAL);
1618 if (strlen(token) <= 1 ||
1620 GOTO(out, rc = -EINVAL);
1624 /* Should be followed by ' ' or nothing */
1625 if ((*val)[0] == '\0')
1627 else if ((*val)[0] == ' ')
1630 GOTO(out, rc = -EINVAL);
1632 rc = nrs_tbf_jobid_parse(cmd, token);
1634 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1636 rc = nrs_tbf_nid_parse(cmd, token);
1638 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1640 if (!cmd->tc_valid_types)
1649 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1651 if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1652 nrs_tbf_jobid_cmd_fini(cmd);
1653 if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1654 nrs_tbf_nid_cmd_fini(cmd);
1657 static struct nrs_tbf_cmd *
1658 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1660 static struct nrs_tbf_cmd *cmd;
1668 GOTO(out, rc = -ENOMEM);
1671 token = strsep(&val, " ");
1672 if (val == NULL || strlen(val) == 0)
1673 GOTO(out_free_cmd, rc = -EINVAL);
1675 /* Type of the command */
1676 if (strcmp(token, "start") == 0)
1677 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1678 else if (strcmp(token, "stop") == 0)
1679 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1680 else if (strcmp(token, "change") == 0)
1681 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1683 GOTO(out_free_cmd, rc = -EINVAL);
1685 /* Name of the rule */
1686 token = strsep(&val, " ");
1689 * Stop comand only need name argument,
1690 * But other commands need ID or rate argument.
1692 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1693 GOTO(out_free_cmd, rc = -EINVAL);
1696 for (i = 0; i < strlen(token); i++) {
1697 if ((!isalnum(token[i])) &&
1699 GOTO(out_free_cmd, rc = -EINVAL);
1701 cmd->tc_name = token;
1703 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1706 rc = nrs_tbf_id_parse(cmd, &val);
1708 GOTO(out_free_cmd, rc);
1712 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1713 strlen(val) == 0 || !isdigit(val[0]))
1714 GOTO(out_free_nid, rc = -EINVAL);
1716 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1717 if (cmd->tc_rpc_rate <= 0 ||
1718 cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1719 GOTO(out_free_nid, rc = -EINVAL);
1721 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1722 GOTO(out_free_nid, rc = -EINVAL);
1723 /* No RPC rate given */
1724 cmd->tc_rpc_rate = tbf_rate;
1728 nrs_tbf_cmd_fini(cmd);
1737 extern struct nrs_core nrs_core;
1738 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1740 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file, const char *buffer,
1741 size_t count, loff_t *off)
1743 struct seq_file *m = file->private_data;
1744 struct ptlrpc_service *svc = m->private;
1748 static struct nrs_tbf_cmd *cmd;
1749 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1750 unsigned long length;
1753 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1754 if (kernbuf == NULL)
1755 GOTO(out, rc = -ENOMEM);
1757 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1758 GOTO(out_free_kernbuff, rc = -EINVAL);
1760 if (copy_from_user(kernbuf, buffer, count))
1761 GOTO(out_free_kernbuff, rc = -EFAULT);
1764 token = strsep(&val, " ");
1766 GOTO(out_free_kernbuff, rc = -EINVAL);
1768 if (strcmp(token, "reg") == 0) {
1769 queue = PTLRPC_NRS_QUEUE_REG;
1770 } else if (strcmp(token, "hp") == 0) {
1771 queue = PTLRPC_NRS_QUEUE_HP;
1773 kernbuf[strlen(token)] = ' ';
1776 length = strlen(val);
1779 GOTO(out_free_kernbuff, rc = -EINVAL);
1781 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1782 GOTO(out_free_kernbuff, rc = -ENODEV);
1783 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1784 queue = PTLRPC_NRS_QUEUE_REG;
1786 cmd = nrs_tbf_parse_cmd(val, length);
1788 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1791 * Serialize NRS core lprocfs operations with policy registration/
1794 mutex_lock(&nrs_core.nrs_mutex);
1795 rc = ptlrpc_nrs_policy_control(svc, queue,
1797 NRS_CTL_TBF_WR_RULE,
1799 mutex_unlock(&nrs_core.nrs_mutex);
1801 nrs_tbf_cmd_fini(cmd);
1804 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1806 return rc ? rc : count;
1808 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1811 * Initializes a TBF policy's lprocfs interface for service \a svc
1813 * \param[in] svc the service
1816 * \retval != 0 error
1818 int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1820 struct lprocfs_seq_vars nrs_tbf_lprocfs_vars[] = {
1821 { .name = "nrs_tbf_rule",
1822 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1827 if (svc->srv_procroot == NULL)
1830 return lprocfs_seq_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars,
1835 * Cleans up a TBF policy's lprocfs interface for service \a svc
1837 * \param[in] svc the service
1839 void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1841 if (svc->srv_procroot == NULL)
1844 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1847 #endif /* LPROCFS */
1850 * TBF policy operations
1852 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1853 .op_policy_start = nrs_tbf_start,
1854 .op_policy_stop = nrs_tbf_stop,
1855 .op_policy_ctl = nrs_tbf_ctl,
1856 .op_res_get = nrs_tbf_res_get,
1857 .op_res_put = nrs_tbf_res_put,
1858 .op_req_get = nrs_tbf_req_get,
1859 .op_req_enqueue = nrs_tbf_req_add,
1860 .op_req_dequeue = nrs_tbf_req_del,
1861 .op_req_stop = nrs_tbf_req_stop,
1863 .op_lprocfs_init = nrs_tbf_lprocfs_init,
1864 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
1869 * TBF policy configuration
1871 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1872 .nc_name = NRS_POL_NAME_TBF,
1873 .nc_ops = &nrs_tbf_ops,
1874 .nc_compat = nrs_policy_compat_all,
1881 #endif /* HAVE_SERVER_SUPPORT */