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
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <libcfs/libcfs.h>
44 #include "ptlrpc_internal.h"
49 * Token Bucket Filter over client NIDs
54 #define NRS_POL_NAME_TBF "tbf"
56 int tbf_jobid_cache_size = 8192;
57 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
58 "The size of jobid cache");
61 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
62 "Default rate limit in RPCs/s");
65 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
66 "How many tokens that a client can save up");
68 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
70 struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
72 struct ptlrpc_nrs *nrs = head->th_res.res_policy->pol_nrs;
73 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
75 spin_lock(&nrs->nrs_lock);
76 nrs->nrs_throttling = 0;
77 spin_unlock(&nrs->nrs_lock);
78 wake_up(&svcpt->scp_waitq);
80 return HRTIMER_NORESTART;
83 #define NRS_TBF_DEFAULT_RULE "default"
85 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
87 LASSERT(atomic_read(&rule->tr_ref) == 0);
88 LASSERT(list_empty(&rule->tr_cli_list));
89 LASSERT(list_empty(&rule->tr_linkage));
91 rule->tr_head->th_ops->o_rule_fini(rule);
96 * Decreases the rule's usage reference count, and stops the rule in case it
97 * was already stopping and have no more outstanding usage references (which
98 * indicates it has no more queued or started requests, and can be safely
101 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
103 if (atomic_dec_and_test(&rule->tr_ref))
104 nrs_tbf_rule_fini(rule);
108 * Increases the rule's usage reference count.
110 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
112 atomic_inc(&rule->tr_ref);
116 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
118 LASSERT(!list_empty(&cli->tc_linkage));
119 LASSERT(cli->tc_rule);
120 list_del_init(&cli->tc_linkage);
121 nrs_tbf_rule_put(cli->tc_rule);
126 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
127 struct nrs_tbf_client *cli)
130 struct nrs_tbf_rule *rule = cli->tc_rule;
132 cli->tc_rpc_rate = rule->tr_rpc_rate;
133 cli->tc_nsecs = rule->tr_nsecs;
134 cli->tc_depth = rule->tr_depth;
135 cli->tc_ntoken = rule->tr_depth;
136 cli->tc_check_time = ktime_to_ns(ktime_get());
137 cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
138 cli->tc_rule_generation = rule->tr_generation;
141 cfs_binheap_relocate(head->th_binheap,
146 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
147 struct nrs_tbf_rule *rule,
148 struct nrs_tbf_client *cli)
150 if (!list_empty(&cli->tc_linkage)) {
151 LASSERT(rule != cli->tc_rule);
152 nrs_tbf_cli_rule_put(cli);
154 LASSERT(cli->tc_rule == NULL);
155 LASSERT(list_empty(&cli->tc_linkage));
156 /* Rule's ref is added before called */
158 list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
159 nrs_tbf_cli_reset_value(head, cli);
163 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
165 return rule->tr_head->th_ops->o_rule_dump(rule, m);
169 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
171 struct nrs_tbf_rule *rule;
174 LASSERT(head != NULL);
175 spin_lock(&head->th_rule_lock);
176 /* List the rules from newest to oldest */
177 list_for_each_entry(rule, &head->th_list, tr_linkage) {
178 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
179 rc = nrs_tbf_rule_dump(rule, m);
185 spin_unlock(&head->th_rule_lock);
190 static struct nrs_tbf_rule *
191 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
194 struct nrs_tbf_rule *rule;
196 LASSERT(head != NULL);
197 list_for_each_entry(rule, &head->th_list, tr_linkage) {
198 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
199 if (strcmp(rule->tr_name, name) == 0) {
200 nrs_tbf_rule_get(rule);
207 static struct nrs_tbf_rule *
208 nrs_tbf_rule_find(struct nrs_tbf_head *head,
211 struct nrs_tbf_rule *rule;
213 LASSERT(head != NULL);
214 spin_lock(&head->th_rule_lock);
215 rule = nrs_tbf_rule_find_nolock(head, name);
216 spin_unlock(&head->th_rule_lock);
220 static struct nrs_tbf_rule *
221 nrs_tbf_rule_match(struct nrs_tbf_head *head,
222 struct nrs_tbf_client *cli)
224 struct nrs_tbf_rule *rule = NULL;
225 struct nrs_tbf_rule *tmp_rule;
227 spin_lock(&head->th_rule_lock);
228 /* Match the newest rule in the list */
229 list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
230 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
231 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
238 rule = head->th_rule;
240 nrs_tbf_rule_get(rule);
241 spin_unlock(&head->th_rule_lock);
246 nrs_tbf_cli_init(struct nrs_tbf_head *head,
247 struct nrs_tbf_client *cli,
248 struct ptlrpc_request *req)
250 struct nrs_tbf_rule *rule;
252 cli->tc_in_heap = false;
253 head->th_ops->o_cli_init(cli, req);
254 INIT_LIST_HEAD(&cli->tc_list);
255 INIT_LIST_HEAD(&cli->tc_linkage);
256 atomic_set(&cli->tc_ref, 1);
257 rule = nrs_tbf_rule_match(head, cli);
258 nrs_tbf_cli_reset(head, rule, cli);
262 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
264 LASSERT(list_empty(&cli->tc_list));
265 LASSERT(!cli->tc_in_heap);
266 LASSERT(atomic_read(&cli->tc_ref) == 0);
267 nrs_tbf_cli_rule_put(cli);
272 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
273 struct nrs_tbf_head *head,
274 struct nrs_tbf_cmd *start)
276 struct nrs_tbf_rule *rule, *tmp_rule;
279 rule = nrs_tbf_rule_find(head, start->tc_name);
281 nrs_tbf_rule_put(rule);
285 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
289 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
290 rule->tr_rpc_rate = start->tc_rpc_rate;
291 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
292 rule->tr_depth = tbf_depth;
293 atomic_set(&rule->tr_ref, 1);
294 INIT_LIST_HEAD(&rule->tr_cli_list);
295 INIT_LIST_HEAD(&rule->tr_nids);
297 rc = head->th_ops->o_rule_init(policy, rule, start);
303 /* Add as the newest rule */
304 spin_lock(&head->th_rule_lock);
305 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
307 spin_unlock(&head->th_rule_lock);
308 nrs_tbf_rule_put(tmp_rule);
309 nrs_tbf_rule_put(rule);
312 list_add(&rule->tr_linkage, &head->th_list);
313 rule->tr_head = head;
314 spin_unlock(&head->th_rule_lock);
315 atomic_inc(&head->th_rule_sequence);
316 if (start->tc_rule_flags & NTRS_DEFAULT) {
317 rule->tr_flags |= NTRS_DEFAULT;
318 LASSERT(head->th_rule == NULL);
319 head->th_rule = rule;
326 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
327 struct nrs_tbf_head *head,
328 struct nrs_tbf_cmd *change)
330 struct nrs_tbf_rule *rule;
332 assert_spin_locked(&policy->pol_nrs->nrs_lock);
334 rule = nrs_tbf_rule_find(head, change->tc_name);
338 rule->tr_rpc_rate = change->tc_rpc_rate;
339 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
340 rule->tr_generation++;
341 nrs_tbf_rule_put(rule);
347 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
348 struct nrs_tbf_head *head,
349 struct nrs_tbf_cmd *stop)
351 struct nrs_tbf_rule *rule;
353 assert_spin_locked(&policy->pol_nrs->nrs_lock);
355 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
358 rule = nrs_tbf_rule_find(head, stop->tc_name);
362 list_del_init(&rule->tr_linkage);
363 rule->tr_flags |= NTRS_STOPPING;
364 nrs_tbf_rule_put(rule);
365 nrs_tbf_rule_put(rule);
371 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
372 struct nrs_tbf_head *head,
373 struct nrs_tbf_cmd *cmd)
377 assert_spin_locked(&policy->pol_nrs->nrs_lock);
379 switch (cmd->tc_cmd) {
380 case NRS_CTL_TBF_START_RULE:
381 if (!(cmd->tc_valid_types & head->th_type_flag))
384 spin_unlock(&policy->pol_nrs->nrs_lock);
385 rc = nrs_tbf_rule_start(policy, head, cmd);
386 spin_lock(&policy->pol_nrs->nrs_lock);
388 case NRS_CTL_TBF_CHANGE_RATE:
389 rc = nrs_tbf_rule_change(policy, head, cmd);
391 case NRS_CTL_TBF_STOP_RULE:
392 rc = nrs_tbf_rule_stop(policy, head, cmd);
393 /* Take it as a success, if not exists at all */
394 return rc == -ENOENT ? 0 : rc;
401 * Binary heap predicate.
403 * \param[in] e1 the first binheap node to compare
404 * \param[in] e2 the second binheap node to compare
409 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
411 struct nrs_tbf_client *cli1;
412 struct nrs_tbf_client *cli2;
414 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
415 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
417 if (cli1->tc_check_time + cli1->tc_nsecs <
418 cli2->tc_check_time + cli2->tc_nsecs)
420 else if (cli1->tc_check_time + cli1->tc_nsecs >
421 cli2->tc_check_time + cli2->tc_nsecs)
424 if (cli1->tc_check_time < cli2->tc_check_time)
426 else if (cli1->tc_check_time > cli2->tc_check_time)
429 /* Maybe need more comparasion, e.g. request number in the rules */
434 * TBF binary heap operations
436 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
439 .hop_compare = tbf_cli_compare,
442 static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
445 return cfs_hash_djb2_hash(key, strlen(key), mask);
448 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
450 struct nrs_tbf_client *cli = hlist_entry(hnode,
451 struct nrs_tbf_client,
454 return (strcmp(cli->tc_jobid, key) == 0);
457 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
459 struct nrs_tbf_client *cli = hlist_entry(hnode,
460 struct nrs_tbf_client,
463 return cli->tc_jobid;
466 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
468 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
471 static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
473 struct nrs_tbf_client *cli = hlist_entry(hnode,
474 struct nrs_tbf_client,
477 atomic_inc(&cli->tc_ref);
480 static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
482 struct nrs_tbf_client *cli = hlist_entry(hnode,
483 struct nrs_tbf_client,
486 atomic_dec(&cli->tc_ref);
489 static void nrs_tbf_jobid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
492 struct nrs_tbf_client *cli = hlist_entry(hnode,
493 struct nrs_tbf_client,
496 LASSERT(atomic_read(&cli->tc_ref) == 0);
497 nrs_tbf_cli_fini(cli);
500 static cfs_hash_ops_t nrs_tbf_jobid_hash_ops = {
501 .hs_hash = nrs_tbf_jobid_hop_hash,
502 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
503 .hs_key = nrs_tbf_jobid_hop_key,
504 .hs_object = nrs_tbf_jobid_hop_object,
505 .hs_get = nrs_tbf_jobid_hop_get,
506 .hs_put = nrs_tbf_jobid_hop_put,
507 .hs_put_locked = nrs_tbf_jobid_hop_put,
508 .hs_exit = nrs_tbf_jobid_hop_exit,
511 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
512 CFS_HASH_NO_ITEMREF | \
515 static struct nrs_tbf_client *
516 nrs_tbf_jobid_hash_lookup(cfs_hash_t *hs,
520 struct hlist_node *hnode;
521 struct nrs_tbf_client *cli;
523 /* cfs_hash_bd_peek_locked is a somehow "internal" function
524 * of cfs_hash, it doesn't add refcount on object. */
525 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
529 cfs_hash_get(hs, hnode);
530 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
531 if (!list_empty(&cli->tc_lru))
532 list_del_init(&cli->tc_lru);
536 #define NRS_TBF_JOBID_NULL ""
538 static struct nrs_tbf_client *
539 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
540 struct ptlrpc_request *req)
543 struct nrs_tbf_client *cli;
544 cfs_hash_t *hs = head->th_cli_hash;
547 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
549 jobid = NRS_TBF_JOBID_NULL;
550 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
551 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
552 cfs_hash_bd_unlock(hs, &bd, 1);
557 static struct nrs_tbf_client *
558 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
559 struct nrs_tbf_client *cli)
562 struct nrs_tbf_client *ret;
563 cfs_hash_t *hs = head->th_cli_hash;
566 jobid = cli->tc_jobid;
567 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
568 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
570 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
573 cfs_hash_bd_unlock(hs, &bd, 1);
579 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
580 struct nrs_tbf_client *cli)
583 cfs_hash_t *hs = head->th_cli_hash;
584 struct nrs_tbf_bucket *bkt;
586 struct list_head zombies;
588 INIT_LIST_HEAD(&zombies);
589 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
590 bkt = cfs_hash_bd_extra_get(hs, &bd);
591 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
593 LASSERT(list_empty(&cli->tc_lru));
594 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
597 * Check and purge the LRU, there is at least one client in the LRU.
599 hw = tbf_jobid_cache_size >>
600 (hs->hs_cur_bits - hs->hs_bkt_bits);
601 while (cfs_hash_bd_count_get(&bd) > hw) {
602 if (unlikely(list_empty(&bkt->ntb_lru)))
604 cli = list_entry(bkt->ntb_lru.next,
605 struct nrs_tbf_client,
607 LASSERT(atomic_read(&cli->tc_ref) == 0);
608 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
609 list_move(&cli->tc_lru, &zombies);
611 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
613 while (!list_empty(&zombies)) {
614 cli = container_of0(zombies.next,
615 struct nrs_tbf_client, tc_lru);
616 list_del_init(&cli->tc_lru);
617 nrs_tbf_cli_fini(cli);
622 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
623 struct ptlrpc_request *req)
625 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
628 jobid = NRS_TBF_JOBID_NULL;
629 LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
630 INIT_LIST_HEAD(&cli->tc_lru);
631 memcpy(cli->tc_jobid, jobid, strlen(jobid));
634 static int nrs_tbf_jobid_hash_order(void)
638 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
644 #define NRS_TBF_JOBID_BKT_BITS 10
647 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
648 struct nrs_tbf_head *head)
650 struct nrs_tbf_cmd start;
651 struct nrs_tbf_bucket *bkt;
657 bits = nrs_tbf_jobid_hash_order();
658 if (bits < NRS_TBF_JOBID_BKT_BITS)
659 bits = NRS_TBF_JOBID_BKT_BITS;
660 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
663 NRS_TBF_JOBID_BKT_BITS,
667 &nrs_tbf_jobid_hash_ops,
668 NRS_TBF_JOBID_HASH_FLAGS);
669 if (head->th_cli_hash == NULL)
672 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
673 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
674 INIT_LIST_HEAD(&bkt->ntb_lru);
677 memset(&start, 0, sizeof(start));
678 start.tc_jobids_str = "*";
680 start.tc_rpc_rate = tbf_rate;
681 start.tc_rule_flags = NTRS_DEFAULT;
682 start.tc_name = NRS_TBF_DEFAULT_RULE;
683 INIT_LIST_HEAD(&start.tc_jobids);
684 rc = nrs_tbf_rule_start(policy, head, &start);
690 * Frees jobid of \a list.
694 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
696 struct nrs_tbf_jobid *jobid, *n;
698 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
699 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
700 list_del(&jobid->tj_linkage);
701 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
706 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
708 struct nrs_tbf_jobid *jobid;
710 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
714 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
715 if (jobid->tj_id == NULL) {
716 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
720 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
721 list_add_tail(&jobid->tj_linkage, jobid_list);
726 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
728 struct nrs_tbf_jobid *jobid;
730 list_for_each_entry(jobid, jobid_list, tj_linkage) {
731 if (strcmp(id, jobid->tj_id) == 0)
738 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
747 INIT_LIST_HEAD(jobid_list);
749 rc = cfs_gettok(&src, ' ', &res);
754 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
759 nrs_tbf_jobid_list_free(jobid_list);
763 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
765 if (!list_empty(&cmd->tc_jobids))
766 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
767 if (cmd->tc_jobids_str)
768 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
771 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
775 OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
776 if (cmd->tc_jobids_str == NULL)
779 memcpy(cmd->tc_jobids_str, id, strlen(id));
781 /* parse jobid list */
782 rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
783 strlen(cmd->tc_jobids_str),
786 nrs_tbf_jobid_cmd_fini(cmd);
791 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
792 struct nrs_tbf_rule *rule,
793 struct nrs_tbf_cmd *start)
797 LASSERT(start->tc_jobids_str);
798 OBD_ALLOC(rule->tr_jobids_str,
799 strlen(start->tc_jobids_str) + 1);
800 if (rule->tr_jobids_str == NULL)
803 memcpy(rule->tr_jobids_str,
804 start->tc_jobids_str,
805 strlen(start->tc_jobids_str));
807 INIT_LIST_HEAD(&rule->tr_jobids);
808 if (!list_empty(&start->tc_jobids)) {
809 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
810 strlen(rule->tr_jobids_str),
813 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
816 OBD_FREE(rule->tr_jobids_str,
817 strlen(start->tc_jobids_str) + 1);
822 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
824 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
825 rule->tr_jobids_str, rule->tr_rpc_rate,
826 atomic_read(&rule->tr_ref) - 1);
830 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
831 struct nrs_tbf_client *cli)
833 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
836 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
838 if (!list_empty(&rule->tr_jobids))
839 nrs_tbf_jobid_list_free(&rule->tr_jobids);
840 LASSERT(rule->tr_jobids_str != NULL);
841 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
844 struct nrs_tbf_ops nrs_tbf_jobid_ops = {
845 .o_name = NRS_TBF_TYPE_JOBID,
846 .o_startup = nrs_tbf_jobid_startup,
847 .o_cli_find = nrs_tbf_jobid_cli_find,
848 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
849 .o_cli_put = nrs_tbf_jobid_cli_put,
850 .o_cli_init = nrs_tbf_jobid_cli_init,
851 .o_rule_init = nrs_tbf_jobid_rule_init,
852 .o_rule_dump = nrs_tbf_jobid_rule_dump,
853 .o_rule_match = nrs_tbf_jobid_rule_match,
854 .o_rule_fini = nrs_tbf_jobid_rule_fini,
858 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
860 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
861 * nrs_tbf_client objects.
863 #define NRS_TBF_NID_BKT_BITS 8
864 #define NRS_TBF_NID_BITS 16
866 static unsigned nrs_tbf_nid_hop_hash(cfs_hash_t *hs, const void *key,
869 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
872 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
874 lnet_nid_t *nid = (lnet_nid_t *)key;
875 struct nrs_tbf_client *cli = hlist_entry(hnode,
876 struct nrs_tbf_client,
879 return *nid == cli->tc_nid;
882 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
884 struct nrs_tbf_client *cli = hlist_entry(hnode,
885 struct nrs_tbf_client,
891 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
893 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
896 static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
898 struct nrs_tbf_client *cli = hlist_entry(hnode,
899 struct nrs_tbf_client,
902 atomic_inc(&cli->tc_ref);
905 static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
907 struct nrs_tbf_client *cli = hlist_entry(hnode,
908 struct nrs_tbf_client,
911 atomic_dec(&cli->tc_ref);
914 static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
916 struct nrs_tbf_client *cli = hlist_entry(hnode,
917 struct nrs_tbf_client,
920 LASSERTF(atomic_read(&cli->tc_ref) == 0,
921 "Busy TBF object from client with NID %s, with %d refs\n",
922 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
924 nrs_tbf_cli_fini(cli);
927 static cfs_hash_ops_t nrs_tbf_nid_hash_ops = {
928 .hs_hash = nrs_tbf_nid_hop_hash,
929 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
930 .hs_key = nrs_tbf_nid_hop_key,
931 .hs_object = nrs_tbf_nid_hop_object,
932 .hs_get = nrs_tbf_nid_hop_get,
933 .hs_put = nrs_tbf_nid_hop_put,
934 .hs_put_locked = nrs_tbf_nid_hop_put,
935 .hs_exit = nrs_tbf_nid_hop_exit,
938 static struct nrs_tbf_client *
939 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
940 struct ptlrpc_request *req)
942 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
945 static struct nrs_tbf_client *
946 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
947 struct nrs_tbf_client *cli)
949 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
954 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
955 struct nrs_tbf_client *cli)
957 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
961 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
962 struct nrs_tbf_head *head)
964 struct nrs_tbf_cmd start;
967 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
970 NRS_TBF_NID_BKT_BITS, 0,
973 &nrs_tbf_nid_hash_ops,
974 CFS_HASH_RW_BKTLOCK);
975 if (head->th_cli_hash == NULL)
978 memset(&start, 0, sizeof(start));
979 start.tc_nids_str = "*";
981 start.tc_rpc_rate = tbf_rate;
982 start.tc_rule_flags = NTRS_DEFAULT;
983 start.tc_name = NRS_TBF_DEFAULT_RULE;
984 INIT_LIST_HEAD(&start.tc_nids);
985 rc = nrs_tbf_rule_start(policy, head, &start);
991 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
992 struct ptlrpc_request *req)
994 cli->tc_nid = req->rq_peer.nid;
997 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
998 struct nrs_tbf_rule *rule,
999 struct nrs_tbf_cmd *start)
1001 LASSERT(start->tc_nids_str);
1002 OBD_ALLOC(rule->tr_nids_str,
1003 strlen(start->tc_nids_str) + 1);
1004 if (rule->tr_nids_str == NULL)
1007 memcpy(rule->tr_nids_str,
1009 strlen(start->tc_nids_str));
1011 INIT_LIST_HEAD(&rule->tr_nids);
1012 if (!list_empty(&start->tc_nids)) {
1013 if (cfs_parse_nidlist(rule->tr_nids_str,
1014 strlen(rule->tr_nids_str),
1015 &rule->tr_nids) <= 0) {
1016 CERROR("nids {%s} illegal\n",
1018 OBD_FREE(rule->tr_nids_str,
1019 strlen(start->tc_nids_str) + 1);
1027 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1029 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1030 rule->tr_nids_str, rule->tr_rpc_rate,
1031 atomic_read(&rule->tr_ref) - 1);
1035 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1036 struct nrs_tbf_client *cli)
1038 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1041 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1043 if (!list_empty(&rule->tr_nids))
1044 cfs_free_nidlist(&rule->tr_nids);
1045 LASSERT(rule->tr_nids_str != NULL);
1046 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1049 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1051 if (!list_empty(&cmd->tc_nids))
1052 cfs_free_nidlist(&cmd->tc_nids);
1053 if (cmd->tc_nids_str)
1054 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1057 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1059 OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1060 if (cmd->tc_nids_str == NULL)
1063 memcpy(cmd->tc_nids_str, id, strlen(id));
1065 /* parse NID list */
1066 if (cfs_parse_nidlist(cmd->tc_nids_str,
1067 strlen(cmd->tc_nids_str),
1068 &cmd->tc_nids) <= 0) {
1069 nrs_tbf_nid_cmd_fini(cmd);
1076 struct nrs_tbf_ops nrs_tbf_nid_ops = {
1077 .o_name = NRS_TBF_TYPE_NID,
1078 .o_startup = nrs_tbf_nid_startup,
1079 .o_cli_find = nrs_tbf_nid_cli_find,
1080 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1081 .o_cli_put = nrs_tbf_nid_cli_put,
1082 .o_cli_init = nrs_tbf_nid_cli_init,
1083 .o_rule_init = nrs_tbf_nid_rule_init,
1084 .o_rule_dump = nrs_tbf_nid_rule_dump,
1085 .o_rule_match = nrs_tbf_nid_rule_match,
1086 .o_rule_fini = nrs_tbf_nid_rule_fini,
1090 * Is called before the policy transitions into
1091 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1092 * policy-specific private data structure.
1094 * \param[in] policy The policy to start
1096 * \retval -ENOMEM OOM error
1099 * \see nrs_policy_register()
1100 * \see nrs_policy_ctl()
1102 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1104 struct nrs_tbf_head *head;
1105 struct nrs_tbf_ops *ops;
1109 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1110 GOTO(out, rc = -EINVAL);
1112 if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1113 ops = &nrs_tbf_nid_ops;
1114 type = NRS_TBF_FLAG_NID;
1115 } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1116 ops = &nrs_tbf_jobid_ops;
1117 type = NRS_TBF_FLAG_JOBID;
1119 GOTO(out, rc = -ENOTSUPP);
1121 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1123 GOTO(out, rc = -ENOMEM);
1125 memcpy(head->th_type, arg, strlen(arg));
1126 head->th_type[strlen(arg)] = '\0';
1128 head->th_type_flag = type;
1130 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1131 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1132 nrs_pol2cptab(policy),
1133 nrs_pol2cptid(policy));
1134 if (head->th_binheap == NULL)
1135 GOTO(out_free_head, rc = -ENOMEM);
1137 atomic_set(&head->th_rule_sequence, 0);
1138 spin_lock_init(&head->th_rule_lock);
1139 INIT_LIST_HEAD(&head->th_list);
1140 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1141 head->th_timer.function = nrs_tbf_timer_cb;
1142 rc = head->th_ops->o_startup(policy, head);
1144 GOTO(out_free_heap, rc);
1146 policy->pol_private = head;
1149 cfs_binheap_destroy(head->th_binheap);
1157 * Is called before the policy transitions into
1158 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1159 * private data structure.
1161 * \param[in] policy The policy to stop
1163 * \see nrs_policy_stop0()
1165 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1167 struct nrs_tbf_head *head = policy->pol_private;
1168 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1169 struct nrs_tbf_rule *rule, *n;
1171 LASSERT(head != NULL);
1172 LASSERT(head->th_cli_hash != NULL);
1173 hrtimer_cancel(&head->th_timer);
1174 /* Should cleanup hash first before free rules */
1175 cfs_hash_putref(head->th_cli_hash);
1176 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1177 list_del_init(&rule->tr_linkage);
1178 nrs_tbf_rule_put(rule);
1180 LASSERT(list_empty(&head->th_list));
1181 LASSERT(head->th_binheap != NULL);
1182 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1183 cfs_binheap_destroy(head->th_binheap);
1185 spin_lock(&nrs->nrs_lock);
1186 nrs->nrs_throttling = 0;
1187 spin_unlock(&nrs->nrs_lock);
1188 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1192 * Performs a policy-specific ctl function on TBF policy instances; similar
1195 * \param[in] policy the policy instance
1196 * \param[in] opc the opcode
1197 * \param[in,out] arg used for passing parameters and information
1199 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1200 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1202 * \retval 0 operation carried out successfully
1205 int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy, enum ptlrpc_nrs_ctl opc,
1211 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1213 switch ((enum nrs_ctl_tbf)opc) {
1218 * Read RPC rate size of a policy instance.
1220 case NRS_CTL_TBF_RD_RULE: {
1221 struct nrs_tbf_head *head = policy->pol_private;
1222 struct seq_file *m = (struct seq_file *) arg;
1223 struct ptlrpc_service_part *svcpt;
1225 svcpt = policy->pol_nrs->nrs_svcpt;
1226 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1228 rc = nrs_tbf_rule_dump_all(head, m);
1233 * Write RPC rate of a policy instance.
1235 case NRS_CTL_TBF_WR_RULE: {
1236 struct nrs_tbf_head *head = policy->pol_private;
1237 struct nrs_tbf_cmd *cmd;
1239 cmd = (struct nrs_tbf_cmd *)arg;
1240 rc = nrs_tbf_command(policy,
1251 * Is called for obtaining a TBF policy resource.
1253 * \param[in] policy The policy on which the request is being asked for
1254 * \param[in] nrq The request for which resources are being taken
1255 * \param[in] parent Parent resource, unused in this policy
1256 * \param[out] resp Resources references are placed in this array
1257 * \param[in] moving_req Signifies limited caller context; unused in this
1261 * \see nrs_resource_get_safe()
1263 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1264 struct ptlrpc_nrs_request *nrq,
1265 const struct ptlrpc_nrs_resource *parent,
1266 struct ptlrpc_nrs_resource **resp,
1269 struct nrs_tbf_head *head;
1270 struct nrs_tbf_client *cli;
1271 struct nrs_tbf_client *tmp;
1272 struct ptlrpc_request *req;
1274 if (parent == NULL) {
1275 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1279 head = container_of(parent, struct nrs_tbf_head, th_res);
1280 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1281 cli = head->th_ops->o_cli_find(head, req);
1283 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1284 LASSERT(cli->tc_rule);
1285 if (cli->tc_rule_sequence !=
1286 atomic_read(&head->th_rule_sequence) ||
1287 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1288 struct nrs_tbf_rule *rule;
1290 rule = nrs_tbf_rule_match(head, cli);
1291 if (rule != cli->tc_rule)
1292 nrs_tbf_cli_reset(head, rule, cli);
1294 nrs_tbf_rule_put(rule);
1295 } else if (cli->tc_rule_generation !=
1296 cli->tc_rule->tr_generation) {
1297 nrs_tbf_cli_reset_value(head, cli);
1299 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1303 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1304 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1307 nrs_tbf_cli_init(head, cli, req);
1308 tmp = head->th_ops->o_cli_findadd(head, cli);
1310 atomic_dec(&cli->tc_ref);
1311 nrs_tbf_cli_fini(cli);
1315 *resp = &cli->tc_res;
1321 * Called when releasing references to the resource hierachy obtained for a
1322 * request for scheduling using the TBF policy.
1324 * \param[in] policy the policy the resource belongs to
1325 * \param[in] res the resource to be released
1327 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1328 const struct ptlrpc_nrs_resource *res)
1330 struct nrs_tbf_head *head;
1331 struct nrs_tbf_client *cli;
1334 * Do nothing for freeing parent, nrs_tbf_net resources
1336 if (res->res_parent == NULL)
1339 cli = container_of(res, struct nrs_tbf_client, tc_res);
1340 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1342 head->th_ops->o_cli_put(head, cli);
1346 * Called when getting a request from the TBF policy for handling, or just
1347 * peeking; removes the request from the policy when it is to be handled.
1349 * \param[in] policy The policy
1350 * \param[in] peek When set, signifies that we just want to examine the
1351 * request, and not handle it, so the request is not removed
1353 * \param[in] force Force the policy to return a request; unused in this
1356 * \retval The request to be handled; this is the next request in the TBF
1359 * \see ptlrpc_nrs_req_get_nolock()
1360 * \see nrs_request_get()
1363 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1364 bool peek, bool force)
1366 struct nrs_tbf_head *head = policy->pol_private;
1367 struct ptlrpc_nrs_request *nrq = NULL;
1368 struct nrs_tbf_client *cli;
1369 cfs_binheap_node_t *node;
1371 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1373 if (!peek && policy->pol_nrs->nrs_throttling)
1376 node = cfs_binheap_root(head->th_binheap);
1377 if (unlikely(node == NULL))
1380 cli = container_of(node, struct nrs_tbf_client, tc_node);
1381 LASSERT(cli->tc_in_heap);
1383 nrq = list_entry(cli->tc_list.next,
1384 struct ptlrpc_nrs_request,
1387 __u64 now = ktime_to_ns(ktime_get());
1392 deadline = cli->tc_check_time +
1394 LASSERT(now >= cli->tc_check_time);
1395 passed = now - cli->tc_check_time;
1396 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1397 ntoken += cli->tc_ntoken;
1398 if (ntoken > cli->tc_depth)
1399 ntoken = cli->tc_depth;
1401 struct ptlrpc_request *req;
1402 nrq = list_entry(cli->tc_list.next,
1403 struct ptlrpc_nrs_request,
1405 req = container_of(nrq,
1406 struct ptlrpc_request,
1409 cli->tc_ntoken = ntoken;
1410 cli->tc_check_time = now;
1411 list_del_init(&nrq->nr_u.tbf.tr_list);
1412 if (list_empty(&cli->tc_list)) {
1413 cfs_binheap_remove(head->th_binheap,
1415 cli->tc_in_heap = false;
1417 cfs_binheap_relocate(head->th_binheap,
1421 "NRS start %s request from %s, "
1423 policy->pol_desc->pd_name,
1424 libcfs_id2str(req->rq_peer),
1425 nrq->nr_u.tbf.tr_sequence);
1429 spin_lock(&policy->pol_nrs->nrs_lock);
1430 policy->pol_nrs->nrs_throttling = 1;
1431 spin_unlock(&policy->pol_nrs->nrs_lock);
1432 head->th_deadline = deadline;
1433 time = ktime_set(0, 0);
1434 time = ktime_add_ns(time, deadline);
1435 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1443 * Adds request \a nrq to \a policy's list of queued requests
1445 * \param[in] policy The policy
1446 * \param[in] nrq The request to add
1448 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1451 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1452 struct ptlrpc_nrs_request *nrq)
1454 struct nrs_tbf_head *head;
1455 struct nrs_tbf_client *cli;
1458 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1460 cli = container_of(nrs_request_resource(nrq),
1461 struct nrs_tbf_client, tc_res);
1462 head = container_of(nrs_request_resource(nrq)->res_parent,
1463 struct nrs_tbf_head, th_res);
1464 if (list_empty(&cli->tc_list)) {
1465 LASSERT(!cli->tc_in_heap);
1466 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1468 cli->tc_in_heap = true;
1469 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1470 list_add_tail(&nrq->nr_u.tbf.tr_list,
1472 if (policy->pol_nrs->nrs_throttling) {
1473 __u64 deadline = cli->tc_check_time +
1475 if ((head->th_deadline > deadline) &&
1476 (hrtimer_try_to_cancel(&head->th_timer)
1479 head->th_deadline = deadline;
1480 time = ktime_set(0, 0);
1481 time = ktime_add_ns(time, deadline);
1482 hrtimer_start(&head->th_timer, time,
1488 LASSERT(cli->tc_in_heap);
1489 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1490 list_add_tail(&nrq->nr_u.tbf.tr_list,
1497 * Removes request \a nrq from \a policy's list of queued requests.
1499 * \param[in] policy The policy
1500 * \param[in] nrq The request to remove
1502 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1503 struct ptlrpc_nrs_request *nrq)
1505 struct nrs_tbf_head *head;
1506 struct nrs_tbf_client *cli;
1508 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1510 cli = container_of(nrs_request_resource(nrq),
1511 struct nrs_tbf_client, tc_res);
1512 head = container_of(nrs_request_resource(nrq)->res_parent,
1513 struct nrs_tbf_head, th_res);
1515 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1516 list_del_init(&nrq->nr_u.tbf.tr_list);
1517 if (list_empty(&cli->tc_list)) {
1518 cfs_binheap_remove(head->th_binheap,
1520 cli->tc_in_heap = false;
1522 cfs_binheap_relocate(head->th_binheap,
1528 * Prints a debug statement right before the request \a nrq stops being
1531 * \param[in] policy The policy handling the request
1532 * \param[in] nrq The request being handled
1534 * \see ptlrpc_server_finish_request()
1535 * \see ptlrpc_nrs_req_stop_nolock()
1537 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1538 struct ptlrpc_nrs_request *nrq)
1540 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1543 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1545 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1546 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1547 nrq->nr_u.tbf.tr_sequence);
1557 * The maximum RPC rate.
1559 #define LPROCFS_NRS_RATE_MAX 65535
1562 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1564 struct ptlrpc_service *svc = m->private;
1567 seq_printf(m, "regular_requests:\n");
1569 * Perform two separate calls to this as only one of the NRS heads'
1570 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1571 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1573 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1575 NRS_CTL_TBF_RD_RULE,
1579 * -ENOSPC means buf in the parameter m is overflow, return 0
1580 * here to let upper layer function seq_read alloc a larger
1581 * memory area and do this process again.
1583 } else if (rc == -ENOSPC) {
1587 * Ignore -ENODEV as the regular NRS head's policy may be in the
1588 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1590 } else if (rc != -ENODEV) {
1594 if (!nrs_svc_has_hp(svc))
1597 seq_printf(m, "high_priority_requests:\n");
1598 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1600 NRS_CTL_TBF_RD_RULE,
1604 * -ENOSPC means buf in the parameter m is overflow, return 0
1605 * here to let upper layer function seq_read alloc a larger
1606 * memory area and do this process again.
1608 } else if (rc == -ENOSPC) {
1617 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1622 token = strsep(val, "}");
1624 GOTO(out, rc = -EINVAL);
1626 if (strlen(token) <= 1 ||
1628 GOTO(out, rc = -EINVAL);
1632 /* Should be followed by ' ' or nothing */
1633 if ((*val)[0] == '\0')
1635 else if ((*val)[0] == ' ')
1638 GOTO(out, rc = -EINVAL);
1640 rc = nrs_tbf_jobid_parse(cmd, token);
1642 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1644 rc = nrs_tbf_nid_parse(cmd, token);
1646 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1648 if (!cmd->tc_valid_types)
1657 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1659 if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1660 nrs_tbf_jobid_cmd_fini(cmd);
1661 if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1662 nrs_tbf_nid_cmd_fini(cmd);
1665 static struct nrs_tbf_cmd *
1666 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1668 static struct nrs_tbf_cmd *cmd;
1676 GOTO(out, rc = -ENOMEM);
1679 token = strsep(&val, " ");
1680 if (val == NULL || strlen(val) == 0)
1681 GOTO(out_free_cmd, rc = -EINVAL);
1683 /* Type of the command */
1684 if (strcmp(token, "start") == 0)
1685 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1686 else if (strcmp(token, "stop") == 0)
1687 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1688 else if (strcmp(token, "change") == 0)
1689 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1691 GOTO(out_free_cmd, rc = -EINVAL);
1693 /* Name of the rule */
1694 token = strsep(&val, " ");
1697 * Stop comand only need name argument,
1698 * But other commands need ID or rate argument.
1700 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1701 GOTO(out_free_cmd, rc = -EINVAL);
1704 for (i = 0; i < strlen(token); i++) {
1705 if ((!isalnum(token[i])) &&
1707 GOTO(out_free_cmd, rc = -EINVAL);
1709 cmd->tc_name = token;
1711 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1714 rc = nrs_tbf_id_parse(cmd, &val);
1716 GOTO(out_free_cmd, rc);
1720 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1721 strlen(val) == 0 || !isdigit(val[0]))
1722 GOTO(out_free_nid, rc = -EINVAL);
1724 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1725 if (cmd->tc_rpc_rate <= 0 ||
1726 cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1727 GOTO(out_free_nid, rc = -EINVAL);
1729 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1730 GOTO(out_free_nid, rc = -EINVAL);
1731 /* No RPC rate given */
1732 cmd->tc_rpc_rate = tbf_rate;
1736 nrs_tbf_cmd_fini(cmd);
1745 extern struct nrs_core nrs_core;
1746 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1748 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file, const char *buffer,
1749 size_t count, loff_t *off)
1751 struct seq_file *m = file->private_data;
1752 struct ptlrpc_service *svc = m->private;
1756 static struct nrs_tbf_cmd *cmd;
1757 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1758 unsigned long length;
1761 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1762 if (kernbuf == NULL)
1763 GOTO(out, rc = -ENOMEM);
1765 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1766 GOTO(out_free_kernbuff, rc = -EINVAL);
1768 if (copy_from_user(kernbuf, buffer, count))
1769 GOTO(out_free_kernbuff, rc = -EFAULT);
1772 token = strsep(&val, " ");
1774 GOTO(out_free_kernbuff, rc = -EINVAL);
1776 if (strcmp(token, "reg") == 0) {
1777 queue = PTLRPC_NRS_QUEUE_REG;
1778 } else if (strcmp(token, "hp") == 0) {
1779 queue = PTLRPC_NRS_QUEUE_HP;
1781 kernbuf[strlen(token)] = ' ';
1784 length = strlen(val);
1787 GOTO(out_free_kernbuff, rc = -EINVAL);
1789 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1790 GOTO(out_free_kernbuff, rc = -ENODEV);
1791 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1792 queue = PTLRPC_NRS_QUEUE_REG;
1794 cmd = nrs_tbf_parse_cmd(val, length);
1796 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1799 * Serialize NRS core lprocfs operations with policy registration/
1802 mutex_lock(&nrs_core.nrs_mutex);
1803 rc = ptlrpc_nrs_policy_control(svc, queue,
1805 NRS_CTL_TBF_WR_RULE,
1807 mutex_unlock(&nrs_core.nrs_mutex);
1809 nrs_tbf_cmd_fini(cmd);
1812 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1814 return rc ? rc : count;
1816 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1819 * Initializes a TBF policy's lprocfs interface for service \a svc
1821 * \param[in] svc the service
1824 * \retval != 0 error
1826 int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1828 struct lprocfs_seq_vars nrs_tbf_lprocfs_vars[] = {
1829 { .name = "nrs_tbf_rule",
1830 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1835 if (svc->srv_procroot == NULL)
1838 return lprocfs_seq_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars,
1843 * Cleans up a TBF policy's lprocfs interface for service \a svc
1845 * \param[in] svc the service
1847 void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1849 if (svc->srv_procroot == NULL)
1852 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1855 #endif /* LPROCFS */
1858 * TBF policy operations
1860 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1861 .op_policy_start = nrs_tbf_start,
1862 .op_policy_stop = nrs_tbf_stop,
1863 .op_policy_ctl = nrs_tbf_ctl,
1864 .op_res_get = nrs_tbf_res_get,
1865 .op_res_put = nrs_tbf_res_put,
1866 .op_req_get = nrs_tbf_req_get,
1867 .op_req_enqueue = nrs_tbf_req_add,
1868 .op_req_dequeue = nrs_tbf_req_del,
1869 .op_req_stop = nrs_tbf_req_stop,
1871 .op_lprocfs_init = nrs_tbf_lprocfs_init,
1872 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
1877 * TBF policy configuration
1879 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1880 .nc_name = NRS_POL_NAME_TBF,
1881 .nc_ops = &nrs_tbf_ops,
1882 .nc_compat = nrs_policy_compat_all,
1889 #endif /* HAVE_SERVER_SUPPORT */