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 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
59 "The size of jobid cache");
61 static int tbf_rate = 10000;
62 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
63 "Default rate limit in RPCs/s");
65 static int tbf_depth = 3;
66 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
67 "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, *tmp_rule;
287 rule = nrs_tbf_rule_find(head, start->tc_name);
289 nrs_tbf_rule_put(rule);
293 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
297 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
298 rule->tr_rpc_rate = start->tc_rpc_rate;
299 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
300 rule->tr_depth = tbf_depth;
301 atomic_set(&rule->tr_ref, 1);
302 INIT_LIST_HEAD(&rule->tr_cli_list);
303 INIT_LIST_HEAD(&rule->tr_nids);
304 INIT_LIST_HEAD(&rule->tr_linkage);
305 spin_lock_init(&rule->tr_rule_lock);
306 rule->tr_head = head;
308 rc = head->th_ops->o_rule_init(policy, rule, start);
314 /* Add as the newest rule */
315 spin_lock(&head->th_rule_lock);
316 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
318 spin_unlock(&head->th_rule_lock);
319 nrs_tbf_rule_put(tmp_rule);
320 nrs_tbf_rule_put(rule);
323 list_add(&rule->tr_linkage, &head->th_list);
324 spin_unlock(&head->th_rule_lock);
325 atomic_inc(&head->th_rule_sequence);
326 if (start->tc_rule_flags & NTRS_DEFAULT) {
327 rule->tr_flags |= NTRS_DEFAULT;
328 LASSERT(head->th_rule == NULL);
329 head->th_rule = rule;
336 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
337 struct nrs_tbf_head *head,
338 struct nrs_tbf_cmd *change)
340 struct nrs_tbf_rule *rule;
342 assert_spin_locked(&policy->pol_nrs->nrs_lock);
344 rule = nrs_tbf_rule_find(head, change->tc_name);
348 rule->tr_rpc_rate = change->tc_rpc_rate;
349 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
350 rule->tr_generation++;
351 nrs_tbf_rule_put(rule);
357 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
358 struct nrs_tbf_head *head,
359 struct nrs_tbf_cmd *stop)
361 struct nrs_tbf_rule *rule;
363 assert_spin_locked(&policy->pol_nrs->nrs_lock);
365 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
368 rule = nrs_tbf_rule_find(head, stop->tc_name);
372 list_del_init(&rule->tr_linkage);
373 rule->tr_flags |= NTRS_STOPPING;
374 nrs_tbf_rule_put(rule);
375 nrs_tbf_rule_put(rule);
381 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
382 struct nrs_tbf_head *head,
383 struct nrs_tbf_cmd *cmd)
387 assert_spin_locked(&policy->pol_nrs->nrs_lock);
389 switch (cmd->tc_cmd) {
390 case NRS_CTL_TBF_START_RULE:
391 if (!(cmd->tc_valid_types & head->th_type_flag))
394 spin_unlock(&policy->pol_nrs->nrs_lock);
395 rc = nrs_tbf_rule_start(policy, head, cmd);
396 spin_lock(&policy->pol_nrs->nrs_lock);
398 case NRS_CTL_TBF_CHANGE_RATE:
399 rc = nrs_tbf_rule_change(policy, head, cmd);
401 case NRS_CTL_TBF_STOP_RULE:
402 rc = nrs_tbf_rule_stop(policy, head, cmd);
403 /* Take it as a success, if not exists at all */
404 return rc == -ENOENT ? 0 : rc;
411 * Binary heap predicate.
413 * \param[in] e1 the first binheap node to compare
414 * \param[in] e2 the second binheap node to compare
420 tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
422 struct nrs_tbf_client *cli1;
423 struct nrs_tbf_client *cli2;
425 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
426 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
428 if (cli1->tc_check_time + cli1->tc_nsecs <
429 cli2->tc_check_time + cli2->tc_nsecs)
431 else if (cli1->tc_check_time + cli1->tc_nsecs >
432 cli2->tc_check_time + cli2->tc_nsecs)
435 if (cli1->tc_check_time < cli2->tc_check_time)
437 else if (cli1->tc_check_time > cli2->tc_check_time)
440 /* Maybe need more comparasion, e.g. request number in the rules */
445 * TBF binary heap operations
447 static struct cfs_binheap_ops nrs_tbf_heap_ops = {
450 .hop_compare = tbf_cli_compare,
453 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
456 return cfs_hash_djb2_hash(key, strlen(key), mask);
459 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
461 struct nrs_tbf_client *cli = hlist_entry(hnode,
462 struct nrs_tbf_client,
465 return (strcmp(cli->tc_jobid, key) == 0);
468 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
470 struct nrs_tbf_client *cli = hlist_entry(hnode,
471 struct nrs_tbf_client,
474 return cli->tc_jobid;
477 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
479 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
482 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
484 struct nrs_tbf_client *cli = hlist_entry(hnode,
485 struct nrs_tbf_client,
488 atomic_inc(&cli->tc_ref);
491 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
493 struct nrs_tbf_client *cli = hlist_entry(hnode,
494 struct nrs_tbf_client,
497 atomic_dec(&cli->tc_ref);
501 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
504 struct nrs_tbf_client *cli = hlist_entry(hnode,
505 struct nrs_tbf_client,
508 LASSERT(atomic_read(&cli->tc_ref) == 0);
509 nrs_tbf_cli_fini(cli);
512 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
513 .hs_hash = nrs_tbf_jobid_hop_hash,
514 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
515 .hs_key = nrs_tbf_jobid_hop_key,
516 .hs_object = nrs_tbf_jobid_hop_object,
517 .hs_get = nrs_tbf_jobid_hop_get,
518 .hs_put = nrs_tbf_jobid_hop_put,
519 .hs_put_locked = nrs_tbf_jobid_hop_put,
520 .hs_exit = nrs_tbf_jobid_hop_exit,
523 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
524 CFS_HASH_NO_ITEMREF | \
527 static struct nrs_tbf_client *
528 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
529 struct cfs_hash_bd *bd,
532 struct hlist_node *hnode;
533 struct nrs_tbf_client *cli;
535 /* cfs_hash_bd_peek_locked is a somehow "internal" function
536 * of cfs_hash, it doesn't add refcount on object. */
537 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
541 cfs_hash_get(hs, hnode);
542 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
543 if (!list_empty(&cli->tc_lru))
544 list_del_init(&cli->tc_lru);
548 #define NRS_TBF_JOBID_NULL ""
550 static struct nrs_tbf_client *
551 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
552 struct ptlrpc_request *req)
555 struct nrs_tbf_client *cli;
556 struct cfs_hash *hs = head->th_cli_hash;
557 struct cfs_hash_bd bd;
559 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
561 jobid = NRS_TBF_JOBID_NULL;
562 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
563 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
564 cfs_hash_bd_unlock(hs, &bd, 1);
569 static struct nrs_tbf_client *
570 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
571 struct nrs_tbf_client *cli)
574 struct nrs_tbf_client *ret;
575 struct cfs_hash *hs = head->th_cli_hash;
576 struct cfs_hash_bd bd;
578 jobid = cli->tc_jobid;
579 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
580 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
582 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
585 cfs_hash_bd_unlock(hs, &bd, 1);
591 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
592 struct nrs_tbf_client *cli)
594 struct cfs_hash_bd bd;
595 struct cfs_hash *hs = head->th_cli_hash;
596 struct nrs_tbf_bucket *bkt;
598 struct list_head zombies;
600 INIT_LIST_HEAD(&zombies);
601 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
602 bkt = cfs_hash_bd_extra_get(hs, &bd);
603 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
605 LASSERT(list_empty(&cli->tc_lru));
606 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
609 * Check and purge the LRU, there is at least one client in the LRU.
611 hw = tbf_jobid_cache_size >>
612 (hs->hs_cur_bits - hs->hs_bkt_bits);
613 while (cfs_hash_bd_count_get(&bd) > hw) {
614 if (unlikely(list_empty(&bkt->ntb_lru)))
616 cli = list_entry(bkt->ntb_lru.next,
617 struct nrs_tbf_client,
619 LASSERT(atomic_read(&cli->tc_ref) == 0);
620 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
621 list_move(&cli->tc_lru, &zombies);
623 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
625 while (!list_empty(&zombies)) {
626 cli = container_of0(zombies.next,
627 struct nrs_tbf_client, tc_lru);
628 list_del_init(&cli->tc_lru);
629 nrs_tbf_cli_fini(cli);
634 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
635 struct ptlrpc_request *req)
637 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
640 jobid = NRS_TBF_JOBID_NULL;
641 LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
642 INIT_LIST_HEAD(&cli->tc_lru);
643 memcpy(cli->tc_jobid, jobid, strlen(jobid));
646 static int nrs_tbf_jobid_hash_order(void)
650 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
656 #define NRS_TBF_JOBID_BKT_BITS 10
659 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
660 struct nrs_tbf_head *head)
662 struct nrs_tbf_cmd start;
663 struct nrs_tbf_bucket *bkt;
667 struct cfs_hash_bd bd;
669 bits = nrs_tbf_jobid_hash_order();
670 if (bits < NRS_TBF_JOBID_BKT_BITS)
671 bits = NRS_TBF_JOBID_BKT_BITS;
672 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
675 NRS_TBF_JOBID_BKT_BITS,
679 &nrs_tbf_jobid_hash_ops,
680 NRS_TBF_JOBID_HASH_FLAGS);
681 if (head->th_cli_hash == NULL)
684 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
685 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
686 INIT_LIST_HEAD(&bkt->ntb_lru);
689 memset(&start, 0, sizeof(start));
690 start.tc_jobids_str = "*";
692 start.tc_rpc_rate = tbf_rate;
693 start.tc_rule_flags = NTRS_DEFAULT;
694 start.tc_name = NRS_TBF_DEFAULT_RULE;
695 INIT_LIST_HEAD(&start.tc_jobids);
696 rc = nrs_tbf_rule_start(policy, head, &start);
702 * Frees jobid of \a list.
706 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
708 struct nrs_tbf_jobid *jobid, *n;
710 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
711 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
712 list_del(&jobid->tj_linkage);
713 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
718 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
720 struct nrs_tbf_jobid *jobid;
722 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
726 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
727 if (jobid->tj_id == NULL) {
728 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
732 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
733 list_add_tail(&jobid->tj_linkage, jobid_list);
738 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
740 struct nrs_tbf_jobid *jobid;
742 list_for_each_entry(jobid, jobid_list, tj_linkage) {
743 if (strcmp(id, jobid->tj_id) == 0)
750 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
759 INIT_LIST_HEAD(jobid_list);
761 rc = cfs_gettok(&src, ' ', &res);
766 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
771 nrs_tbf_jobid_list_free(jobid_list);
775 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
777 if (!list_empty(&cmd->tc_jobids))
778 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
779 if (cmd->tc_jobids_str)
780 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
783 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
787 OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
788 if (cmd->tc_jobids_str == NULL)
791 memcpy(cmd->tc_jobids_str, id, strlen(id));
793 /* parse jobid list */
794 rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
795 strlen(cmd->tc_jobids_str),
798 nrs_tbf_jobid_cmd_fini(cmd);
803 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
804 struct nrs_tbf_rule *rule,
805 struct nrs_tbf_cmd *start)
809 LASSERT(start->tc_jobids_str);
810 OBD_ALLOC(rule->tr_jobids_str,
811 strlen(start->tc_jobids_str) + 1);
812 if (rule->tr_jobids_str == NULL)
815 memcpy(rule->tr_jobids_str,
816 start->tc_jobids_str,
817 strlen(start->tc_jobids_str));
819 INIT_LIST_HEAD(&rule->tr_jobids);
820 if (!list_empty(&start->tc_jobids)) {
821 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
822 strlen(rule->tr_jobids_str),
825 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
828 OBD_FREE(rule->tr_jobids_str,
829 strlen(start->tc_jobids_str) + 1);
834 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
836 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
837 rule->tr_jobids_str, rule->tr_rpc_rate,
838 atomic_read(&rule->tr_ref) - 1);
842 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
843 struct nrs_tbf_client *cli)
845 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
848 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
850 if (!list_empty(&rule->tr_jobids))
851 nrs_tbf_jobid_list_free(&rule->tr_jobids);
852 LASSERT(rule->tr_jobids_str != NULL);
853 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
856 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
857 .o_name = NRS_TBF_TYPE_JOBID,
858 .o_startup = nrs_tbf_jobid_startup,
859 .o_cli_find = nrs_tbf_jobid_cli_find,
860 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
861 .o_cli_put = nrs_tbf_jobid_cli_put,
862 .o_cli_init = nrs_tbf_jobid_cli_init,
863 .o_rule_init = nrs_tbf_jobid_rule_init,
864 .o_rule_dump = nrs_tbf_jobid_rule_dump,
865 .o_rule_match = nrs_tbf_jobid_rule_match,
866 .o_rule_fini = nrs_tbf_jobid_rule_fini,
870 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
872 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
873 * nrs_tbf_client objects.
875 #define NRS_TBF_NID_BKT_BITS 8
876 #define NRS_TBF_NID_BITS 16
878 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
881 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
884 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
886 lnet_nid_t *nid = (lnet_nid_t *)key;
887 struct nrs_tbf_client *cli = hlist_entry(hnode,
888 struct nrs_tbf_client,
891 return *nid == cli->tc_nid;
894 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
896 struct nrs_tbf_client *cli = hlist_entry(hnode,
897 struct nrs_tbf_client,
903 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
905 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
908 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
910 struct nrs_tbf_client *cli = hlist_entry(hnode,
911 struct nrs_tbf_client,
914 atomic_inc(&cli->tc_ref);
917 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
919 struct nrs_tbf_client *cli = hlist_entry(hnode,
920 struct nrs_tbf_client,
923 atomic_dec(&cli->tc_ref);
926 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
928 struct nrs_tbf_client *cli = hlist_entry(hnode,
929 struct nrs_tbf_client,
932 LASSERTF(atomic_read(&cli->tc_ref) == 0,
933 "Busy TBF object from client with NID %s, with %d refs\n",
934 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
936 nrs_tbf_cli_fini(cli);
939 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
940 .hs_hash = nrs_tbf_nid_hop_hash,
941 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
942 .hs_key = nrs_tbf_nid_hop_key,
943 .hs_object = nrs_tbf_nid_hop_object,
944 .hs_get = nrs_tbf_nid_hop_get,
945 .hs_put = nrs_tbf_nid_hop_put,
946 .hs_put_locked = nrs_tbf_nid_hop_put,
947 .hs_exit = nrs_tbf_nid_hop_exit,
950 static struct nrs_tbf_client *
951 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
952 struct ptlrpc_request *req)
954 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
957 static struct nrs_tbf_client *
958 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
959 struct nrs_tbf_client *cli)
961 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
966 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
967 struct nrs_tbf_client *cli)
969 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
973 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
974 struct nrs_tbf_head *head)
976 struct nrs_tbf_cmd start;
979 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
982 NRS_TBF_NID_BKT_BITS, 0,
985 &nrs_tbf_nid_hash_ops,
986 CFS_HASH_RW_BKTLOCK);
987 if (head->th_cli_hash == NULL)
990 memset(&start, 0, sizeof(start));
991 start.tc_nids_str = "*";
993 start.tc_rpc_rate = tbf_rate;
994 start.tc_rule_flags = NTRS_DEFAULT;
995 start.tc_name = NRS_TBF_DEFAULT_RULE;
996 INIT_LIST_HEAD(&start.tc_nids);
997 rc = nrs_tbf_rule_start(policy, head, &start);
1003 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1004 struct ptlrpc_request *req)
1006 cli->tc_nid = req->rq_peer.nid;
1009 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1010 struct nrs_tbf_rule *rule,
1011 struct nrs_tbf_cmd *start)
1013 LASSERT(start->tc_nids_str);
1014 OBD_ALLOC(rule->tr_nids_str,
1015 strlen(start->tc_nids_str) + 1);
1016 if (rule->tr_nids_str == NULL)
1019 memcpy(rule->tr_nids_str,
1021 strlen(start->tc_nids_str));
1023 INIT_LIST_HEAD(&rule->tr_nids);
1024 if (!list_empty(&start->tc_nids)) {
1025 if (cfs_parse_nidlist(rule->tr_nids_str,
1026 strlen(rule->tr_nids_str),
1027 &rule->tr_nids) <= 0) {
1028 CERROR("nids {%s} illegal\n",
1030 OBD_FREE(rule->tr_nids_str,
1031 strlen(start->tc_nids_str) + 1);
1039 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1041 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1042 rule->tr_nids_str, rule->tr_rpc_rate,
1043 atomic_read(&rule->tr_ref) - 1);
1047 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1048 struct nrs_tbf_client *cli)
1050 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1053 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1055 if (!list_empty(&rule->tr_nids))
1056 cfs_free_nidlist(&rule->tr_nids);
1057 LASSERT(rule->tr_nids_str != NULL);
1058 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1061 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1063 if (!list_empty(&cmd->tc_nids))
1064 cfs_free_nidlist(&cmd->tc_nids);
1065 if (cmd->tc_nids_str)
1066 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1069 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1071 OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1072 if (cmd->tc_nids_str == NULL)
1075 memcpy(cmd->tc_nids_str, id, strlen(id));
1077 /* parse NID list */
1078 if (cfs_parse_nidlist(cmd->tc_nids_str,
1079 strlen(cmd->tc_nids_str),
1080 &cmd->tc_nids) <= 0) {
1081 nrs_tbf_nid_cmd_fini(cmd);
1088 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1089 .o_name = NRS_TBF_TYPE_NID,
1090 .o_startup = nrs_tbf_nid_startup,
1091 .o_cli_find = nrs_tbf_nid_cli_find,
1092 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1093 .o_cli_put = nrs_tbf_nid_cli_put,
1094 .o_cli_init = nrs_tbf_nid_cli_init,
1095 .o_rule_init = nrs_tbf_nid_rule_init,
1096 .o_rule_dump = nrs_tbf_nid_rule_dump,
1097 .o_rule_match = nrs_tbf_nid_rule_match,
1098 .o_rule_fini = nrs_tbf_nid_rule_fini,
1102 * Is called before the policy transitions into
1103 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1104 * policy-specific private data structure.
1106 * \param[in] policy The policy to start
1108 * \retval -ENOMEM OOM error
1111 * \see nrs_policy_register()
1112 * \see nrs_policy_ctl()
1114 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1116 struct nrs_tbf_head *head;
1117 struct nrs_tbf_ops *ops;
1121 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1122 GOTO(out, rc = -EINVAL);
1124 if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1125 ops = &nrs_tbf_nid_ops;
1126 type = NRS_TBF_FLAG_NID;
1127 } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1128 ops = &nrs_tbf_jobid_ops;
1129 type = NRS_TBF_FLAG_JOBID;
1131 GOTO(out, rc = -ENOTSUPP);
1133 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1135 GOTO(out, rc = -ENOMEM);
1137 memcpy(head->th_type, arg, strlen(arg));
1138 head->th_type[strlen(arg)] = '\0';
1140 head->th_type_flag = type;
1142 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1143 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1144 nrs_pol2cptab(policy),
1145 nrs_pol2cptid(policy));
1146 if (head->th_binheap == NULL)
1147 GOTO(out_free_head, rc = -ENOMEM);
1149 atomic_set(&head->th_rule_sequence, 0);
1150 spin_lock_init(&head->th_rule_lock);
1151 INIT_LIST_HEAD(&head->th_list);
1152 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1153 head->th_timer.function = nrs_tbf_timer_cb;
1154 rc = head->th_ops->o_startup(policy, head);
1156 GOTO(out_free_heap, rc);
1158 policy->pol_private = head;
1161 cfs_binheap_destroy(head->th_binheap);
1169 * Is called before the policy transitions into
1170 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1171 * private data structure.
1173 * \param[in] policy The policy to stop
1175 * \see nrs_policy_stop0()
1177 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1179 struct nrs_tbf_head *head = policy->pol_private;
1180 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1181 struct nrs_tbf_rule *rule, *n;
1183 LASSERT(head != NULL);
1184 LASSERT(head->th_cli_hash != NULL);
1185 hrtimer_cancel(&head->th_timer);
1186 /* Should cleanup hash first before free rules */
1187 cfs_hash_putref(head->th_cli_hash);
1188 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1189 list_del_init(&rule->tr_linkage);
1190 nrs_tbf_rule_put(rule);
1192 LASSERT(list_empty(&head->th_list));
1193 LASSERT(head->th_binheap != NULL);
1194 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1195 cfs_binheap_destroy(head->th_binheap);
1197 nrs->nrs_throttling = 0;
1198 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1202 * Performs a policy-specific ctl function on TBF policy instances; similar
1205 * \param[in] policy the policy instance
1206 * \param[in] opc the opcode
1207 * \param[in,out] arg used for passing parameters and information
1209 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1210 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1212 * \retval 0 operation carried out successfully
1215 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
1216 enum ptlrpc_nrs_ctl opc,
1222 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1224 switch ((enum nrs_ctl_tbf)opc) {
1229 * Read RPC rate size of a policy instance.
1231 case NRS_CTL_TBF_RD_RULE: {
1232 struct nrs_tbf_head *head = policy->pol_private;
1233 struct seq_file *m = (struct seq_file *) arg;
1234 struct ptlrpc_service_part *svcpt;
1236 svcpt = policy->pol_nrs->nrs_svcpt;
1237 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1239 rc = nrs_tbf_rule_dump_all(head, m);
1244 * Write RPC rate of a policy instance.
1246 case NRS_CTL_TBF_WR_RULE: {
1247 struct nrs_tbf_head *head = policy->pol_private;
1248 struct nrs_tbf_cmd *cmd;
1250 cmd = (struct nrs_tbf_cmd *)arg;
1251 rc = nrs_tbf_command(policy,
1262 * Is called for obtaining a TBF policy resource.
1264 * \param[in] policy The policy on which the request is being asked for
1265 * \param[in] nrq The request for which resources are being taken
1266 * \param[in] parent Parent resource, unused in this policy
1267 * \param[out] resp Resources references are placed in this array
1268 * \param[in] moving_req Signifies limited caller context; unused in this
1272 * \see nrs_resource_get_safe()
1274 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1275 struct ptlrpc_nrs_request *nrq,
1276 const struct ptlrpc_nrs_resource *parent,
1277 struct ptlrpc_nrs_resource **resp,
1280 struct nrs_tbf_head *head;
1281 struct nrs_tbf_client *cli;
1282 struct nrs_tbf_client *tmp;
1283 struct ptlrpc_request *req;
1285 if (parent == NULL) {
1286 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1290 head = container_of(parent, struct nrs_tbf_head, th_res);
1291 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1292 cli = head->th_ops->o_cli_find(head, req);
1294 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1295 LASSERT(cli->tc_rule);
1296 if (cli->tc_rule_sequence !=
1297 atomic_read(&head->th_rule_sequence) ||
1298 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1299 struct nrs_tbf_rule *rule;
1301 rule = nrs_tbf_rule_match(head, cli);
1302 if (rule != cli->tc_rule)
1303 nrs_tbf_cli_reset(head, rule, cli);
1305 nrs_tbf_rule_put(rule);
1306 } else if (cli->tc_rule_generation !=
1307 cli->tc_rule->tr_generation) {
1308 nrs_tbf_cli_reset_value(head, cli);
1310 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1314 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1315 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1318 nrs_tbf_cli_init(head, cli, req);
1319 tmp = head->th_ops->o_cli_findadd(head, cli);
1321 atomic_dec(&cli->tc_ref);
1322 nrs_tbf_cli_fini(cli);
1326 *resp = &cli->tc_res;
1332 * Called when releasing references to the resource hierachy obtained for a
1333 * request for scheduling using the TBF policy.
1335 * \param[in] policy the policy the resource belongs to
1336 * \param[in] res the resource to be released
1338 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1339 const struct ptlrpc_nrs_resource *res)
1341 struct nrs_tbf_head *head;
1342 struct nrs_tbf_client *cli;
1345 * Do nothing for freeing parent, nrs_tbf_net resources
1347 if (res->res_parent == NULL)
1350 cli = container_of(res, struct nrs_tbf_client, tc_res);
1351 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1353 head->th_ops->o_cli_put(head, cli);
1357 * Called when getting a request from the TBF policy for handling, or just
1358 * peeking; removes the request from the policy when it is to be handled.
1360 * \param[in] policy The policy
1361 * \param[in] peek When set, signifies that we just want to examine the
1362 * request, and not handle it, so the request is not removed
1364 * \param[in] force Force the policy to return a request; unused in this
1367 * \retval The request to be handled; this is the next request in the TBF
1370 * \see ptlrpc_nrs_req_get_nolock()
1371 * \see nrs_request_get()
1374 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1375 bool peek, bool force)
1377 struct nrs_tbf_head *head = policy->pol_private;
1378 struct ptlrpc_nrs_request *nrq = NULL;
1379 struct nrs_tbf_client *cli;
1380 struct cfs_binheap_node *node;
1382 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1384 if (!peek && policy->pol_nrs->nrs_throttling)
1387 node = cfs_binheap_root(head->th_binheap);
1388 if (unlikely(node == NULL))
1391 cli = container_of(node, struct nrs_tbf_client, tc_node);
1392 LASSERT(cli->tc_in_heap);
1394 nrq = list_entry(cli->tc_list.next,
1395 struct ptlrpc_nrs_request,
1398 __u64 now = ktime_to_ns(ktime_get());
1403 deadline = cli->tc_check_time +
1405 LASSERT(now >= cli->tc_check_time);
1406 passed = now - cli->tc_check_time;
1407 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1408 ntoken += cli->tc_ntoken;
1409 if (ntoken > cli->tc_depth)
1410 ntoken = cli->tc_depth;
1412 struct ptlrpc_request *req;
1413 nrq = list_entry(cli->tc_list.next,
1414 struct ptlrpc_nrs_request,
1416 req = container_of(nrq,
1417 struct ptlrpc_request,
1420 cli->tc_ntoken = ntoken;
1421 cli->tc_check_time = now;
1422 list_del_init(&nrq->nr_u.tbf.tr_list);
1423 if (list_empty(&cli->tc_list)) {
1424 cfs_binheap_remove(head->th_binheap,
1426 cli->tc_in_heap = false;
1428 cfs_binheap_relocate(head->th_binheap,
1432 "NRS start %s request from %s, "
1434 policy->pol_desc->pd_name,
1435 libcfs_id2str(req->rq_peer),
1436 nrq->nr_u.tbf.tr_sequence);
1440 policy->pol_nrs->nrs_throttling = 1;
1441 head->th_deadline = deadline;
1442 time = ktime_set(0, 0);
1443 time = ktime_add_ns(time, deadline);
1444 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1452 * Adds request \a nrq to \a policy's list of queued requests
1454 * \param[in] policy The policy
1455 * \param[in] nrq The request to add
1457 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1460 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1461 struct ptlrpc_nrs_request *nrq)
1463 struct nrs_tbf_head *head;
1464 struct nrs_tbf_client *cli;
1467 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1469 cli = container_of(nrs_request_resource(nrq),
1470 struct nrs_tbf_client, tc_res);
1471 head = container_of(nrs_request_resource(nrq)->res_parent,
1472 struct nrs_tbf_head, th_res);
1473 if (list_empty(&cli->tc_list)) {
1474 LASSERT(!cli->tc_in_heap);
1475 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1477 cli->tc_in_heap = true;
1478 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1479 list_add_tail(&nrq->nr_u.tbf.tr_list,
1481 if (policy->pol_nrs->nrs_throttling) {
1482 __u64 deadline = cli->tc_check_time +
1484 if ((head->th_deadline > deadline) &&
1485 (hrtimer_try_to_cancel(&head->th_timer)
1488 head->th_deadline = deadline;
1489 time = ktime_set(0, 0);
1490 time = ktime_add_ns(time, deadline);
1491 hrtimer_start(&head->th_timer, time,
1497 LASSERT(cli->tc_in_heap);
1498 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1499 list_add_tail(&nrq->nr_u.tbf.tr_list,
1506 * Removes request \a nrq from \a policy's list of queued requests.
1508 * \param[in] policy The policy
1509 * \param[in] nrq The request to remove
1511 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1512 struct ptlrpc_nrs_request *nrq)
1514 struct nrs_tbf_head *head;
1515 struct nrs_tbf_client *cli;
1517 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1519 cli = container_of(nrs_request_resource(nrq),
1520 struct nrs_tbf_client, tc_res);
1521 head = container_of(nrs_request_resource(nrq)->res_parent,
1522 struct nrs_tbf_head, th_res);
1524 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1525 list_del_init(&nrq->nr_u.tbf.tr_list);
1526 if (list_empty(&cli->tc_list)) {
1527 cfs_binheap_remove(head->th_binheap,
1529 cli->tc_in_heap = false;
1531 cfs_binheap_relocate(head->th_binheap,
1537 * Prints a debug statement right before the request \a nrq stops being
1540 * \param[in] policy The policy handling the request
1541 * \param[in] nrq The request being handled
1543 * \see ptlrpc_server_finish_request()
1544 * \see ptlrpc_nrs_req_stop_nolock()
1546 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1547 struct ptlrpc_nrs_request *nrq)
1549 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1552 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1554 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1555 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1556 nrq->nr_u.tbf.tr_sequence);
1559 #ifdef CONFIG_PROC_FS
1566 * The maximum RPC rate.
1568 #define LPROCFS_NRS_RATE_MAX 65535
1571 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1573 struct ptlrpc_service *svc = m->private;
1576 seq_printf(m, "regular_requests:\n");
1578 * Perform two separate calls to this as only one of the NRS heads'
1579 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1580 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1582 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1584 NRS_CTL_TBF_RD_RULE,
1588 * -ENOSPC means buf in the parameter m is overflow, return 0
1589 * here to let upper layer function seq_read alloc a larger
1590 * memory area and do this process again.
1592 } else if (rc == -ENOSPC) {
1596 * Ignore -ENODEV as the regular NRS head's policy may be in the
1597 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1599 } else if (rc != -ENODEV) {
1603 if (!nrs_svc_has_hp(svc))
1606 seq_printf(m, "high_priority_requests:\n");
1607 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1609 NRS_CTL_TBF_RD_RULE,
1613 * -ENOSPC means buf in the parameter m is overflow, return 0
1614 * here to let upper layer function seq_read alloc a larger
1615 * memory area and do this process again.
1617 } else if (rc == -ENOSPC) {
1626 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1631 token = strsep(val, "}");
1633 GOTO(out, rc = -EINVAL);
1635 if (strlen(token) <= 1 ||
1637 GOTO(out, rc = -EINVAL);
1641 /* Should be followed by ' ' or nothing */
1642 if ((*val)[0] == '\0')
1644 else if ((*val)[0] == ' ')
1647 GOTO(out, rc = -EINVAL);
1649 rc = nrs_tbf_jobid_parse(cmd, token);
1651 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1653 rc = nrs_tbf_nid_parse(cmd, token);
1655 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1657 if (!cmd->tc_valid_types)
1666 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1668 if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1669 nrs_tbf_jobid_cmd_fini(cmd);
1670 if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1671 nrs_tbf_nid_cmd_fini(cmd);
1674 static struct nrs_tbf_cmd *
1675 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1677 static struct nrs_tbf_cmd *cmd;
1685 GOTO(out, rc = -ENOMEM);
1688 token = strsep(&val, " ");
1689 if (val == NULL || strlen(val) == 0)
1690 GOTO(out_free_cmd, rc = -EINVAL);
1692 /* Type of the command */
1693 if (strcmp(token, "start") == 0)
1694 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1695 else if (strcmp(token, "stop") == 0)
1696 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1697 else if (strcmp(token, "change") == 0)
1698 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1700 GOTO(out_free_cmd, rc = -EINVAL);
1702 /* Name of the rule */
1703 token = strsep(&val, " ");
1706 * Stop comand only need name argument,
1707 * But other commands need ID or rate argument.
1709 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1710 GOTO(out_free_cmd, rc = -EINVAL);
1713 for (i = 0; i < strlen(token); i++) {
1714 if ((!isalnum(token[i])) &&
1716 GOTO(out_free_cmd, rc = -EINVAL);
1718 cmd->tc_name = token;
1720 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1723 rc = nrs_tbf_id_parse(cmd, &val);
1725 GOTO(out_free_cmd, rc);
1729 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1730 strlen(val) == 0 || !isdigit(val[0]))
1731 GOTO(out_free_nid, rc = -EINVAL);
1733 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1734 if (cmd->tc_rpc_rate <= 0 ||
1735 cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1736 GOTO(out_free_nid, rc = -EINVAL);
1738 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1739 GOTO(out_free_nid, rc = -EINVAL);
1740 /* No RPC rate given */
1741 cmd->tc_rpc_rate = tbf_rate;
1745 nrs_tbf_cmd_fini(cmd);
1754 extern struct nrs_core nrs_core;
1755 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1757 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
1758 const char __user *buffer,
1759 size_t count, loff_t *off)
1761 struct seq_file *m = file->private_data;
1762 struct ptlrpc_service *svc = m->private;
1766 static struct nrs_tbf_cmd *cmd;
1767 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1768 unsigned long length;
1771 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1772 if (kernbuf == NULL)
1773 GOTO(out, rc = -ENOMEM);
1775 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1776 GOTO(out_free_kernbuff, rc = -EINVAL);
1778 if (copy_from_user(kernbuf, buffer, count))
1779 GOTO(out_free_kernbuff, rc = -EFAULT);
1782 token = strsep(&val, " ");
1784 GOTO(out_free_kernbuff, rc = -EINVAL);
1786 if (strcmp(token, "reg") == 0) {
1787 queue = PTLRPC_NRS_QUEUE_REG;
1788 } else if (strcmp(token, "hp") == 0) {
1789 queue = PTLRPC_NRS_QUEUE_HP;
1791 kernbuf[strlen(token)] = ' ';
1794 length = strlen(val);
1797 GOTO(out_free_kernbuff, rc = -EINVAL);
1799 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1800 GOTO(out_free_kernbuff, rc = -ENODEV);
1801 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1802 queue = PTLRPC_NRS_QUEUE_REG;
1804 cmd = nrs_tbf_parse_cmd(val, length);
1806 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1809 * Serialize NRS core lprocfs operations with policy registration/
1812 mutex_lock(&nrs_core.nrs_mutex);
1813 rc = ptlrpc_nrs_policy_control(svc, queue,
1815 NRS_CTL_TBF_WR_RULE,
1817 mutex_unlock(&nrs_core.nrs_mutex);
1819 nrs_tbf_cmd_fini(cmd);
1822 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1824 return rc ? rc : count;
1826 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1829 * Initializes a TBF policy's lprocfs interface for service \a svc
1831 * \param[in] svc the service
1834 * \retval != 0 error
1836 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1838 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
1839 { .name = "nrs_tbf_rule",
1840 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1845 if (svc->srv_procroot == NULL)
1848 return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
1852 * Cleans up a TBF policy's lprocfs interface for service \a svc
1854 * \param[in] svc the service
1856 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1858 if (svc->srv_procroot == NULL)
1861 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1864 #endif /* CONFIG_PROC_FS */
1867 * TBF policy operations
1869 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1870 .op_policy_start = nrs_tbf_start,
1871 .op_policy_stop = nrs_tbf_stop,
1872 .op_policy_ctl = nrs_tbf_ctl,
1873 .op_res_get = nrs_tbf_res_get,
1874 .op_res_put = nrs_tbf_res_put,
1875 .op_req_get = nrs_tbf_req_get,
1876 .op_req_enqueue = nrs_tbf_req_add,
1877 .op_req_dequeue = nrs_tbf_req_del,
1878 .op_req_stop = nrs_tbf_req_stop,
1879 #ifdef CONFIG_PROC_FS
1880 .op_lprocfs_init = nrs_tbf_lprocfs_init,
1881 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
1886 * TBF policy configuration
1888 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1889 .nc_name = NRS_POL_NAME_TBF,
1890 .nc_ops = &nrs_tbf_ops,
1891 .nc_compat = nrs_policy_compat_all,
1898 #endif /* HAVE_SERVER_SUPPORT */