4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (C) 2013 DataDirect Networks, Inc.
25 * Copyright (c) 2014, Intel Corporation.
28 * lustre/ptlrpc/nrs_tbf.c
30 * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
34 #ifdef HAVE_SERVER_SUPPORT
41 #define DEBUG_SUBSYSTEM S_RPC
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <libcfs/libcfs.h>
45 #include "ptlrpc_internal.h"
50 * Token Bucket Filter over client NIDs
55 #define NRS_POL_NAME_TBF "tbf"
57 static int tbf_jobid_cache_size = 8192;
58 module_param(tbf_jobid_cache_size, int, 0644);
59 MODULE_PARM_DESC(tbf_jobid_cache_size, "The size of jobid cache");
61 static int tbf_rate = 10000;
62 module_param(tbf_rate, int, 0644);
63 MODULE_PARM_DESC(tbf_rate, "Default rate limit in RPCs/s");
65 static int tbf_depth = 3;
66 module_param(tbf_depth, int, 0644);
67 MODULE_PARM_DESC(tbf_depth, "How many tokens that a client can save up");
69 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
71 struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
73 struct ptlrpc_nrs *nrs = head->th_res.res_policy->pol_nrs;
74 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
76 nrs->nrs_throttling = 0;
77 wake_up(&svcpt->scp_waitq);
79 return HRTIMER_NORESTART;
82 #define NRS_TBF_DEFAULT_RULE "default"
84 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
86 LASSERT(atomic_read(&rule->tr_ref) == 0);
87 LASSERT(list_empty(&rule->tr_cli_list));
88 LASSERT(list_empty(&rule->tr_linkage));
90 rule->tr_head->th_ops->o_rule_fini(rule);
95 * Decreases the rule's usage reference count, and stops the rule in case it
96 * was already stopping and have no more outstanding usage references (which
97 * indicates it has no more queued or started requests, and can be safely
100 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
102 if (atomic_dec_and_test(&rule->tr_ref))
103 nrs_tbf_rule_fini(rule);
107 * Increases the rule's usage reference count.
109 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
111 atomic_inc(&rule->tr_ref);
115 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
117 LASSERT(!list_empty(&cli->tc_linkage));
118 LASSERT(cli->tc_rule);
119 spin_lock(&cli->tc_rule->tr_rule_lock);
120 list_del_init(&cli->tc_linkage);
121 spin_unlock(&cli->tc_rule->tr_rule_lock);
122 nrs_tbf_rule_put(cli->tc_rule);
127 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
128 struct nrs_tbf_client *cli)
131 struct nrs_tbf_rule *rule = cli->tc_rule;
133 cli->tc_rpc_rate = rule->tr_rpc_rate;
134 cli->tc_nsecs = rule->tr_nsecs;
135 cli->tc_depth = rule->tr_depth;
136 cli->tc_ntoken = rule->tr_depth;
137 cli->tc_check_time = ktime_to_ns(ktime_get());
138 cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
139 cli->tc_rule_generation = rule->tr_generation;
142 cfs_binheap_relocate(head->th_binheap,
147 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
148 struct nrs_tbf_rule *rule,
149 struct nrs_tbf_client *cli)
151 spin_lock(&cli->tc_rule_lock);
152 if (cli->tc_rule != NULL && !list_empty(&cli->tc_linkage)) {
153 LASSERT(rule != cli->tc_rule);
154 nrs_tbf_cli_rule_put(cli);
156 LASSERT(cli->tc_rule == NULL);
157 LASSERT(list_empty(&cli->tc_linkage));
158 /* Rule's ref is added before called */
160 spin_lock(&rule->tr_rule_lock);
161 list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
162 spin_unlock(&rule->tr_rule_lock);
163 spin_unlock(&cli->tc_rule_lock);
164 nrs_tbf_cli_reset_value(head, cli);
168 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
170 return rule->tr_head->th_ops->o_rule_dump(rule, m);
174 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
176 struct nrs_tbf_rule *rule;
179 LASSERT(head != NULL);
180 spin_lock(&head->th_rule_lock);
181 /* List the rules from newest to oldest */
182 list_for_each_entry(rule, &head->th_list, tr_linkage) {
183 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
184 rc = nrs_tbf_rule_dump(rule, m);
190 spin_unlock(&head->th_rule_lock);
195 static struct nrs_tbf_rule *
196 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
199 struct nrs_tbf_rule *rule;
201 LASSERT(head != NULL);
202 list_for_each_entry(rule, &head->th_list, tr_linkage) {
203 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
204 if (strcmp(rule->tr_name, name) == 0) {
205 nrs_tbf_rule_get(rule);
212 static struct nrs_tbf_rule *
213 nrs_tbf_rule_find(struct nrs_tbf_head *head,
216 struct nrs_tbf_rule *rule;
218 LASSERT(head != NULL);
219 spin_lock(&head->th_rule_lock);
220 rule = nrs_tbf_rule_find_nolock(head, name);
221 spin_unlock(&head->th_rule_lock);
225 static struct nrs_tbf_rule *
226 nrs_tbf_rule_match(struct nrs_tbf_head *head,
227 struct nrs_tbf_client *cli)
229 struct nrs_tbf_rule *rule = NULL;
230 struct nrs_tbf_rule *tmp_rule;
232 spin_lock(&head->th_rule_lock);
233 /* Match the newest rule in the list */
234 list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
235 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
236 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
243 rule = head->th_rule;
245 nrs_tbf_rule_get(rule);
246 spin_unlock(&head->th_rule_lock);
251 nrs_tbf_cli_init(struct nrs_tbf_head *head,
252 struct nrs_tbf_client *cli,
253 struct ptlrpc_request *req)
255 struct nrs_tbf_rule *rule;
257 cli->tc_in_heap = false;
258 head->th_ops->o_cli_init(cli, req);
259 INIT_LIST_HEAD(&cli->tc_list);
260 INIT_LIST_HEAD(&cli->tc_linkage);
261 spin_lock_init(&cli->tc_rule_lock);
262 atomic_set(&cli->tc_ref, 1);
263 rule = nrs_tbf_rule_match(head, cli);
264 nrs_tbf_cli_reset(head, rule, cli);
268 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
270 LASSERT(list_empty(&cli->tc_list));
271 LASSERT(!cli->tc_in_heap);
272 LASSERT(atomic_read(&cli->tc_ref) == 0);
273 spin_lock(&cli->tc_rule_lock);
274 nrs_tbf_cli_rule_put(cli);
275 spin_unlock(&cli->tc_rule_lock);
280 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
281 struct nrs_tbf_head *head,
282 struct nrs_tbf_cmd *start)
284 struct nrs_tbf_rule *rule, *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->u.tc_start.ts_rpc_rate;
299 rule->tr_nsecs = NSEC_PER_SEC;
300 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
301 rule->tr_depth = tbf_depth;
302 atomic_set(&rule->tr_ref, 1);
303 INIT_LIST_HEAD(&rule->tr_cli_list);
304 INIT_LIST_HEAD(&rule->tr_nids);
305 INIT_LIST_HEAD(&rule->tr_linkage);
306 spin_lock_init(&rule->tr_rule_lock);
307 rule->tr_head = head;
309 rc = head->th_ops->o_rule_init(policy, rule, start);
315 /* Add as the newest rule */
316 spin_lock(&head->th_rule_lock);
317 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
319 spin_unlock(&head->th_rule_lock);
320 nrs_tbf_rule_put(tmp_rule);
321 nrs_tbf_rule_put(rule);
324 list_add(&rule->tr_linkage, &head->th_list);
325 spin_unlock(&head->th_rule_lock);
326 atomic_inc(&head->th_rule_sequence);
327 if (start->u.tc_start.ts_rule_flags & NTRS_DEFAULT) {
328 rule->tr_flags |= NTRS_DEFAULT;
329 LASSERT(head->th_rule == NULL);
330 head->th_rule = rule;
337 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
338 struct nrs_tbf_head *head,
339 struct nrs_tbf_cmd *change)
341 struct nrs_tbf_rule *rule;
343 assert_spin_locked(&policy->pol_nrs->nrs_lock);
345 rule = nrs_tbf_rule_find(head, change->tc_name);
349 rule->tr_rpc_rate = change->u.tc_change.tc_rpc_rate;
350 rule->tr_nsecs = NSEC_PER_SEC;
351 do_div(rule->tr_nsecs, rule->tr_rpc_rate);
352 rule->tr_generation++;
353 nrs_tbf_rule_put(rule);
359 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
360 struct nrs_tbf_head *head,
361 struct nrs_tbf_cmd *stop)
363 struct nrs_tbf_rule *rule;
365 assert_spin_locked(&policy->pol_nrs->nrs_lock);
367 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
370 rule = nrs_tbf_rule_find(head, stop->tc_name);
374 list_del_init(&rule->tr_linkage);
375 rule->tr_flags |= NTRS_STOPPING;
376 nrs_tbf_rule_put(rule);
377 nrs_tbf_rule_put(rule);
383 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
384 struct nrs_tbf_head *head,
385 struct nrs_tbf_cmd *cmd)
389 assert_spin_locked(&policy->pol_nrs->nrs_lock);
391 switch (cmd->tc_cmd) {
392 case NRS_CTL_TBF_START_RULE:
393 if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
396 spin_unlock(&policy->pol_nrs->nrs_lock);
397 rc = nrs_tbf_rule_start(policy, head, cmd);
398 spin_lock(&policy->pol_nrs->nrs_lock);
400 case NRS_CTL_TBF_CHANGE_RULE:
401 rc = nrs_tbf_rule_change(policy, head, cmd);
403 case NRS_CTL_TBF_STOP_RULE:
404 rc = nrs_tbf_rule_stop(policy, head, cmd);
405 /* Take it as a success, if not exists at all */
406 return rc == -ENOENT ? 0 : rc;
413 * Binary heap predicate.
415 * \param[in] e1 the first binheap node to compare
416 * \param[in] e2 the second binheap node to compare
422 tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
424 struct nrs_tbf_client *cli1;
425 struct nrs_tbf_client *cli2;
427 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
428 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
430 if (cli1->tc_check_time + cli1->tc_nsecs <
431 cli2->tc_check_time + cli2->tc_nsecs)
433 else if (cli1->tc_check_time + cli1->tc_nsecs >
434 cli2->tc_check_time + cli2->tc_nsecs)
437 if (cli1->tc_check_time < cli2->tc_check_time)
439 else if (cli1->tc_check_time > cli2->tc_check_time)
442 /* Maybe need more comparasion, e.g. request number in the rules */
447 * TBF binary heap operations
449 static struct cfs_binheap_ops nrs_tbf_heap_ops = {
452 .hop_compare = tbf_cli_compare,
455 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
458 return cfs_hash_djb2_hash(key, strlen(key), mask);
461 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
463 struct nrs_tbf_client *cli = hlist_entry(hnode,
464 struct nrs_tbf_client,
467 return (strcmp(cli->tc_jobid, key) == 0);
470 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
472 struct nrs_tbf_client *cli = hlist_entry(hnode,
473 struct nrs_tbf_client,
476 return cli->tc_jobid;
479 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
481 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
484 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
486 struct nrs_tbf_client *cli = hlist_entry(hnode,
487 struct nrs_tbf_client,
490 atomic_inc(&cli->tc_ref);
493 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
495 struct nrs_tbf_client *cli = hlist_entry(hnode,
496 struct nrs_tbf_client,
499 atomic_dec(&cli->tc_ref);
503 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
506 struct nrs_tbf_client *cli = hlist_entry(hnode,
507 struct nrs_tbf_client,
510 LASSERT(atomic_read(&cli->tc_ref) == 0);
511 nrs_tbf_cli_fini(cli);
514 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
515 .hs_hash = nrs_tbf_jobid_hop_hash,
516 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
517 .hs_key = nrs_tbf_jobid_hop_key,
518 .hs_object = nrs_tbf_jobid_hop_object,
519 .hs_get = nrs_tbf_jobid_hop_get,
520 .hs_put = nrs_tbf_jobid_hop_put,
521 .hs_put_locked = nrs_tbf_jobid_hop_put,
522 .hs_exit = nrs_tbf_jobid_hop_exit,
525 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
526 CFS_HASH_NO_ITEMREF | \
529 static struct nrs_tbf_client *
530 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
531 struct cfs_hash_bd *bd,
534 struct hlist_node *hnode;
535 struct nrs_tbf_client *cli;
537 /* cfs_hash_bd_peek_locked is a somehow "internal" function
538 * of cfs_hash, it doesn't add refcount on object. */
539 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
543 cfs_hash_get(hs, hnode);
544 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
545 if (!list_empty(&cli->tc_lru))
546 list_del_init(&cli->tc_lru);
550 #define NRS_TBF_JOBID_NULL ""
552 static struct nrs_tbf_client *
553 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
554 struct ptlrpc_request *req)
557 struct nrs_tbf_client *cli;
558 struct cfs_hash *hs = head->th_cli_hash;
559 struct cfs_hash_bd bd;
561 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
563 jobid = NRS_TBF_JOBID_NULL;
564 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
565 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
566 cfs_hash_bd_unlock(hs, &bd, 1);
571 static struct nrs_tbf_client *
572 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
573 struct nrs_tbf_client *cli)
576 struct nrs_tbf_client *ret;
577 struct cfs_hash *hs = head->th_cli_hash;
578 struct cfs_hash_bd bd;
580 jobid = cli->tc_jobid;
581 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
582 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
584 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
587 cfs_hash_bd_unlock(hs, &bd, 1);
593 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
594 struct nrs_tbf_client *cli)
596 struct cfs_hash_bd bd;
597 struct cfs_hash *hs = head->th_cli_hash;
598 struct nrs_tbf_bucket *bkt;
600 struct list_head zombies;
602 INIT_LIST_HEAD(&zombies);
603 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
604 bkt = cfs_hash_bd_extra_get(hs, &bd);
605 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
607 LASSERT(list_empty(&cli->tc_lru));
608 list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
611 * Check and purge the LRU, there is at least one client in the LRU.
613 hw = tbf_jobid_cache_size >>
614 (hs->hs_cur_bits - hs->hs_bkt_bits);
615 while (cfs_hash_bd_count_get(&bd) > hw) {
616 if (unlikely(list_empty(&bkt->ntb_lru)))
618 cli = list_entry(bkt->ntb_lru.next,
619 struct nrs_tbf_client,
621 LASSERT(atomic_read(&cli->tc_ref) == 0);
622 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
623 list_move(&cli->tc_lru, &zombies);
625 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
627 while (!list_empty(&zombies)) {
628 cli = container_of0(zombies.next,
629 struct nrs_tbf_client, tc_lru);
630 list_del_init(&cli->tc_lru);
631 nrs_tbf_cli_fini(cli);
636 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
637 struct ptlrpc_request *req)
639 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
642 jobid = NRS_TBF_JOBID_NULL;
643 LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
644 INIT_LIST_HEAD(&cli->tc_lru);
645 memcpy(cli->tc_jobid, jobid, strlen(jobid));
648 static int nrs_tbf_jobid_hash_order(void)
652 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
658 #define NRS_TBF_JOBID_BKT_BITS 10
661 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
662 struct nrs_tbf_head *head)
664 struct nrs_tbf_cmd start;
665 struct nrs_tbf_bucket *bkt;
669 struct cfs_hash_bd bd;
671 bits = nrs_tbf_jobid_hash_order();
672 if (bits < NRS_TBF_JOBID_BKT_BITS)
673 bits = NRS_TBF_JOBID_BKT_BITS;
674 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
677 NRS_TBF_JOBID_BKT_BITS,
681 &nrs_tbf_jobid_hash_ops,
682 NRS_TBF_JOBID_HASH_FLAGS);
683 if (head->th_cli_hash == NULL)
686 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
687 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
688 INIT_LIST_HEAD(&bkt->ntb_lru);
691 memset(&start, 0, sizeof(start));
692 start.u.tc_start.ts_jobids_str = "*";
694 start.u.tc_start.ts_rpc_rate = tbf_rate;
695 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
696 start.tc_name = NRS_TBF_DEFAULT_RULE;
697 INIT_LIST_HEAD(&start.u.tc_start.ts_jobids);
698 rc = nrs_tbf_rule_start(policy, head, &start);
704 * Frees jobid of \a list.
708 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
710 struct nrs_tbf_jobid *jobid, *n;
712 list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
713 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
714 list_del(&jobid->tj_linkage);
715 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
720 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
722 struct nrs_tbf_jobid *jobid;
724 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
728 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
729 if (jobid->tj_id == NULL) {
730 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
734 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
735 list_add_tail(&jobid->tj_linkage, jobid_list);
740 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
742 struct nrs_tbf_jobid *jobid;
744 list_for_each_entry(jobid, jobid_list, tj_linkage) {
745 if (strcmp(id, jobid->tj_id) == 0)
752 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
761 INIT_LIST_HEAD(jobid_list);
763 rc = cfs_gettok(&src, ' ', &res);
768 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
773 nrs_tbf_jobid_list_free(jobid_list);
777 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
779 if (!list_empty(&cmd->u.tc_start.ts_jobids))
780 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
781 if (cmd->u.tc_start.ts_jobids_str)
782 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
783 strlen(cmd->u.tc_start.ts_jobids_str) + 1);
786 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
789 int keylen = strlen(key);
792 rc = cfs_gettok(src, '=', &res);
793 if (rc == 0 || res.ls_len != keylen ||
794 strncmp(res.ls_str, key, keylen) != 0 ||
795 src->ls_len <= 2 || src->ls_str[0] != '{' ||
796 src->ls_str[src->ls_len - 1] != '}')
799 /* Skip '{' and '}' */
805 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
811 src.ls_len = strlen(id);
812 rc = nrs_tbf_check_id_value(&src, "jobid");
816 OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
817 if (cmd->u.tc_start.ts_jobids_str == NULL)
820 memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
822 /* parse jobid list */
823 rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
824 strlen(cmd->u.tc_start.ts_jobids_str),
825 &cmd->u.tc_start.ts_jobids);
827 nrs_tbf_jobid_cmd_fini(cmd);
832 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
833 struct nrs_tbf_rule *rule,
834 struct nrs_tbf_cmd *start)
838 LASSERT(start->u.tc_start.ts_jobids_str);
839 OBD_ALLOC(rule->tr_jobids_str,
840 strlen(start->u.tc_start.ts_jobids_str) + 1);
841 if (rule->tr_jobids_str == NULL)
844 memcpy(rule->tr_jobids_str,
845 start->u.tc_start.ts_jobids_str,
846 strlen(start->u.tc_start.ts_jobids_str));
848 INIT_LIST_HEAD(&rule->tr_jobids);
849 if (!list_empty(&start->u.tc_start.ts_jobids)) {
850 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
851 strlen(rule->tr_jobids_str),
854 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
857 OBD_FREE(rule->tr_jobids_str,
858 strlen(start->u.tc_start.ts_jobids_str) + 1);
863 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
865 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
866 rule->tr_jobids_str, rule->tr_rpc_rate,
867 atomic_read(&rule->tr_ref) - 1);
872 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
873 struct nrs_tbf_client *cli)
875 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
878 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
880 if (!list_empty(&rule->tr_jobids))
881 nrs_tbf_jobid_list_free(&rule->tr_jobids);
882 LASSERT(rule->tr_jobids_str != NULL);
883 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
886 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
887 .o_name = NRS_TBF_TYPE_JOBID,
888 .o_startup = nrs_tbf_jobid_startup,
889 .o_cli_find = nrs_tbf_jobid_cli_find,
890 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
891 .o_cli_put = nrs_tbf_jobid_cli_put,
892 .o_cli_init = nrs_tbf_jobid_cli_init,
893 .o_rule_init = nrs_tbf_jobid_rule_init,
894 .o_rule_dump = nrs_tbf_jobid_rule_dump,
895 .o_rule_match = nrs_tbf_jobid_rule_match,
896 .o_rule_fini = nrs_tbf_jobid_rule_fini,
900 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
902 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
903 * nrs_tbf_client objects.
905 #define NRS_TBF_NID_BKT_BITS 8
906 #define NRS_TBF_NID_BITS 16
908 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
911 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
914 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
916 lnet_nid_t *nid = (lnet_nid_t *)key;
917 struct nrs_tbf_client *cli = hlist_entry(hnode,
918 struct nrs_tbf_client,
921 return *nid == cli->tc_nid;
924 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
926 struct nrs_tbf_client *cli = hlist_entry(hnode,
927 struct nrs_tbf_client,
933 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
935 return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
938 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
940 struct nrs_tbf_client *cli = hlist_entry(hnode,
941 struct nrs_tbf_client,
944 atomic_inc(&cli->tc_ref);
947 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
949 struct nrs_tbf_client *cli = hlist_entry(hnode,
950 struct nrs_tbf_client,
953 atomic_dec(&cli->tc_ref);
956 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
958 struct nrs_tbf_client *cli = hlist_entry(hnode,
959 struct nrs_tbf_client,
962 LASSERTF(atomic_read(&cli->tc_ref) == 0,
963 "Busy TBF object from client with NID %s, with %d refs\n",
964 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
966 nrs_tbf_cli_fini(cli);
969 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
970 .hs_hash = nrs_tbf_nid_hop_hash,
971 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
972 .hs_key = nrs_tbf_nid_hop_key,
973 .hs_object = nrs_tbf_nid_hop_object,
974 .hs_get = nrs_tbf_nid_hop_get,
975 .hs_put = nrs_tbf_nid_hop_put,
976 .hs_put_locked = nrs_tbf_nid_hop_put,
977 .hs_exit = nrs_tbf_nid_hop_exit,
980 static struct nrs_tbf_client *
981 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
982 struct ptlrpc_request *req)
984 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
987 static struct nrs_tbf_client *
988 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
989 struct nrs_tbf_client *cli)
991 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
996 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
997 struct nrs_tbf_client *cli)
999 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1003 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1004 struct nrs_tbf_head *head)
1006 struct nrs_tbf_cmd start;
1009 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1012 NRS_TBF_NID_BKT_BITS, 0,
1015 &nrs_tbf_nid_hash_ops,
1016 CFS_HASH_RW_BKTLOCK);
1017 if (head->th_cli_hash == NULL)
1020 memset(&start, 0, sizeof(start));
1021 start.u.tc_start.ts_nids_str = "*";
1023 start.u.tc_start.ts_rpc_rate = tbf_rate;
1024 start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1025 start.tc_name = NRS_TBF_DEFAULT_RULE;
1026 INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1027 rc = nrs_tbf_rule_start(policy, head, &start);
1033 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1034 struct ptlrpc_request *req)
1036 cli->tc_nid = req->rq_peer.nid;
1039 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1040 struct nrs_tbf_rule *rule,
1041 struct nrs_tbf_cmd *start)
1043 LASSERT(start->u.tc_start.ts_nids_str);
1044 OBD_ALLOC(rule->tr_nids_str,
1045 strlen(start->u.tc_start.ts_nids_str) + 1);
1046 if (rule->tr_nids_str == NULL)
1049 memcpy(rule->tr_nids_str,
1050 start->u.tc_start.ts_nids_str,
1051 strlen(start->u.tc_start.ts_nids_str));
1053 INIT_LIST_HEAD(&rule->tr_nids);
1054 if (!list_empty(&start->u.tc_start.ts_nids)) {
1055 if (cfs_parse_nidlist(rule->tr_nids_str,
1056 strlen(rule->tr_nids_str),
1057 &rule->tr_nids) <= 0) {
1058 CERROR("nids {%s} illegal\n",
1060 OBD_FREE(rule->tr_nids_str,
1061 strlen(start->u.tc_start.ts_nids_str) + 1);
1069 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1071 seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1072 rule->tr_nids_str, rule->tr_rpc_rate,
1073 atomic_read(&rule->tr_ref) - 1);
1078 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1079 struct nrs_tbf_client *cli)
1081 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1084 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1086 if (!list_empty(&rule->tr_nids))
1087 cfs_free_nidlist(&rule->tr_nids);
1088 LASSERT(rule->tr_nids_str != NULL);
1089 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1092 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1094 if (!list_empty(&cmd->u.tc_start.ts_nids))
1095 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1096 if (cmd->u.tc_start.ts_nids_str)
1097 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1098 strlen(cmd->u.tc_start.ts_nids_str) + 1);
1101 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1103 struct cfs_lstr src;
1107 src.ls_len = strlen(id);
1108 rc = nrs_tbf_check_id_value(&src, "nid");
1112 OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1113 if (cmd->u.tc_start.ts_nids_str == NULL)
1116 memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1118 /* parse NID list */
1119 if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1120 strlen(cmd->u.tc_start.ts_nids_str),
1121 &cmd->u.tc_start.ts_nids) <= 0) {
1122 nrs_tbf_nid_cmd_fini(cmd);
1129 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1130 .o_name = NRS_TBF_TYPE_NID,
1131 .o_startup = nrs_tbf_nid_startup,
1132 .o_cli_find = nrs_tbf_nid_cli_find,
1133 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1134 .o_cli_put = nrs_tbf_nid_cli_put,
1135 .o_cli_init = nrs_tbf_nid_cli_init,
1136 .o_rule_init = nrs_tbf_nid_rule_init,
1137 .o_rule_dump = nrs_tbf_nid_rule_dump,
1138 .o_rule_match = nrs_tbf_nid_rule_match,
1139 .o_rule_fini = nrs_tbf_nid_rule_fini,
1143 * Is called before the policy transitions into
1144 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1145 * policy-specific private data structure.
1147 * \param[in] policy The policy to start
1149 * \retval -ENOMEM OOM error
1152 * \see nrs_policy_register()
1153 * \see nrs_policy_ctl()
1155 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1157 struct nrs_tbf_head *head;
1158 struct nrs_tbf_ops *ops;
1162 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1163 GOTO(out, rc = -EINVAL);
1165 if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1166 ops = &nrs_tbf_nid_ops;
1167 type = NRS_TBF_FLAG_NID;
1168 } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1169 ops = &nrs_tbf_jobid_ops;
1170 type = NRS_TBF_FLAG_JOBID;
1172 GOTO(out, rc = -ENOTSUPP);
1174 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1176 GOTO(out, rc = -ENOMEM);
1178 memcpy(head->th_type, arg, strlen(arg));
1179 head->th_type[strlen(arg)] = '\0';
1181 head->th_type_flag = type;
1183 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1184 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1185 nrs_pol2cptab(policy),
1186 nrs_pol2cptid(policy));
1187 if (head->th_binheap == NULL)
1188 GOTO(out_free_head, rc = -ENOMEM);
1190 atomic_set(&head->th_rule_sequence, 0);
1191 spin_lock_init(&head->th_rule_lock);
1192 INIT_LIST_HEAD(&head->th_list);
1193 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1194 head->th_timer.function = nrs_tbf_timer_cb;
1195 rc = head->th_ops->o_startup(policy, head);
1197 GOTO(out_free_heap, rc);
1199 policy->pol_private = head;
1202 cfs_binheap_destroy(head->th_binheap);
1210 * Is called before the policy transitions into
1211 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1212 * private data structure.
1214 * \param[in] policy The policy to stop
1216 * \see nrs_policy_stop0()
1218 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1220 struct nrs_tbf_head *head = policy->pol_private;
1221 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1222 struct nrs_tbf_rule *rule, *n;
1224 LASSERT(head != NULL);
1225 LASSERT(head->th_cli_hash != NULL);
1226 hrtimer_cancel(&head->th_timer);
1227 /* Should cleanup hash first before free rules */
1228 cfs_hash_putref(head->th_cli_hash);
1229 list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1230 list_del_init(&rule->tr_linkage);
1231 nrs_tbf_rule_put(rule);
1233 LASSERT(list_empty(&head->th_list));
1234 LASSERT(head->th_binheap != NULL);
1235 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1236 cfs_binheap_destroy(head->th_binheap);
1238 nrs->nrs_throttling = 0;
1239 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1243 * Performs a policy-specific ctl function on TBF policy instances; similar
1246 * \param[in] policy the policy instance
1247 * \param[in] opc the opcode
1248 * \param[in,out] arg used for passing parameters and information
1250 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1251 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1253 * \retval 0 operation carried out successfully
1256 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
1257 enum ptlrpc_nrs_ctl opc,
1263 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1265 switch ((enum nrs_ctl_tbf)opc) {
1270 * Read RPC rate size of a policy instance.
1272 case NRS_CTL_TBF_RD_RULE: {
1273 struct nrs_tbf_head *head = policy->pol_private;
1274 struct seq_file *m = (struct seq_file *) arg;
1275 struct ptlrpc_service_part *svcpt;
1277 svcpt = policy->pol_nrs->nrs_svcpt;
1278 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1280 rc = nrs_tbf_rule_dump_all(head, m);
1285 * Write RPC rate of a policy instance.
1287 case NRS_CTL_TBF_WR_RULE: {
1288 struct nrs_tbf_head *head = policy->pol_private;
1289 struct nrs_tbf_cmd *cmd;
1291 cmd = (struct nrs_tbf_cmd *)arg;
1292 rc = nrs_tbf_command(policy,
1298 * Read the TBF policy type of a policy instance.
1300 case NRS_CTL_TBF_RD_TYPE_FLAG: {
1301 struct nrs_tbf_head *head = policy->pol_private;
1303 *(__u32 *)arg = head->th_type_flag;
1312 * Is called for obtaining a TBF policy resource.
1314 * \param[in] policy The policy on which the request is being asked for
1315 * \param[in] nrq The request for which resources are being taken
1316 * \param[in] parent Parent resource, unused in this policy
1317 * \param[out] resp Resources references are placed in this array
1318 * \param[in] moving_req Signifies limited caller context; unused in this
1322 * \see nrs_resource_get_safe()
1324 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1325 struct ptlrpc_nrs_request *nrq,
1326 const struct ptlrpc_nrs_resource *parent,
1327 struct ptlrpc_nrs_resource **resp,
1330 struct nrs_tbf_head *head;
1331 struct nrs_tbf_client *cli;
1332 struct nrs_tbf_client *tmp;
1333 struct ptlrpc_request *req;
1335 if (parent == NULL) {
1336 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1340 head = container_of(parent, struct nrs_tbf_head, th_res);
1341 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1342 cli = head->th_ops->o_cli_find(head, req);
1344 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1345 LASSERT(cli->tc_rule);
1346 if (cli->tc_rule_sequence !=
1347 atomic_read(&head->th_rule_sequence) ||
1348 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1349 struct nrs_tbf_rule *rule;
1351 rule = nrs_tbf_rule_match(head, cli);
1352 if (rule != cli->tc_rule)
1353 nrs_tbf_cli_reset(head, rule, cli);
1355 nrs_tbf_rule_put(rule);
1356 } else if (cli->tc_rule_generation !=
1357 cli->tc_rule->tr_generation) {
1358 nrs_tbf_cli_reset_value(head, cli);
1360 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1364 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1365 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1368 nrs_tbf_cli_init(head, cli, req);
1369 tmp = head->th_ops->o_cli_findadd(head, cli);
1371 atomic_dec(&cli->tc_ref);
1372 nrs_tbf_cli_fini(cli);
1376 *resp = &cli->tc_res;
1382 * Called when releasing references to the resource hierachy obtained for a
1383 * request for scheduling using the TBF policy.
1385 * \param[in] policy the policy the resource belongs to
1386 * \param[in] res the resource to be released
1388 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1389 const struct ptlrpc_nrs_resource *res)
1391 struct nrs_tbf_head *head;
1392 struct nrs_tbf_client *cli;
1395 * Do nothing for freeing parent, nrs_tbf_net resources
1397 if (res->res_parent == NULL)
1400 cli = container_of(res, struct nrs_tbf_client, tc_res);
1401 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1403 head->th_ops->o_cli_put(head, cli);
1407 * Called when getting a request from the TBF policy for handling, or just
1408 * peeking; removes the request from the policy when it is to be handled.
1410 * \param[in] policy The policy
1411 * \param[in] peek When set, signifies that we just want to examine the
1412 * request, and not handle it, so the request is not removed
1414 * \param[in] force Force the policy to return a request; unused in this
1417 * \retval The request to be handled; this is the next request in the TBF
1420 * \see ptlrpc_nrs_req_get_nolock()
1421 * \see nrs_request_get()
1424 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1425 bool peek, bool force)
1427 struct nrs_tbf_head *head = policy->pol_private;
1428 struct ptlrpc_nrs_request *nrq = NULL;
1429 struct nrs_tbf_client *cli;
1430 struct cfs_binheap_node *node;
1432 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1434 if (!peek && policy->pol_nrs->nrs_throttling)
1437 node = cfs_binheap_root(head->th_binheap);
1438 if (unlikely(node == NULL))
1441 cli = container_of(node, struct nrs_tbf_client, tc_node);
1442 LASSERT(cli->tc_in_heap);
1444 nrq = list_entry(cli->tc_list.next,
1445 struct ptlrpc_nrs_request,
1448 __u64 now = ktime_to_ns(ktime_get());
1453 deadline = cli->tc_check_time +
1455 LASSERT(now >= cli->tc_check_time);
1456 passed = now - cli->tc_check_time;
1457 ntoken = passed * cli->tc_rpc_rate;
1458 do_div(ntoken, NSEC_PER_SEC);
1459 ntoken += cli->tc_ntoken;
1460 if (ntoken > cli->tc_depth)
1461 ntoken = cli->tc_depth;
1463 struct ptlrpc_request *req;
1464 nrq = list_entry(cli->tc_list.next,
1465 struct ptlrpc_nrs_request,
1467 req = container_of(nrq,
1468 struct ptlrpc_request,
1471 cli->tc_ntoken = ntoken;
1472 cli->tc_check_time = now;
1473 list_del_init(&nrq->nr_u.tbf.tr_list);
1474 if (list_empty(&cli->tc_list)) {
1475 cfs_binheap_remove(head->th_binheap,
1477 cli->tc_in_heap = false;
1479 cfs_binheap_relocate(head->th_binheap,
1483 "NRS start %s request from %s, "
1485 policy->pol_desc->pd_name,
1486 libcfs_id2str(req->rq_peer),
1487 nrq->nr_u.tbf.tr_sequence);
1491 policy->pol_nrs->nrs_throttling = 1;
1492 head->th_deadline = deadline;
1493 time = ktime_set(0, 0);
1494 time = ktime_add_ns(time, deadline);
1495 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1503 * Adds request \a nrq to \a policy's list of queued requests
1505 * \param[in] policy The policy
1506 * \param[in] nrq The request to add
1508 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1511 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1512 struct ptlrpc_nrs_request *nrq)
1514 struct nrs_tbf_head *head;
1515 struct nrs_tbf_client *cli;
1518 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1520 cli = container_of(nrs_request_resource(nrq),
1521 struct nrs_tbf_client, tc_res);
1522 head = container_of(nrs_request_resource(nrq)->res_parent,
1523 struct nrs_tbf_head, th_res);
1524 if (list_empty(&cli->tc_list)) {
1525 LASSERT(!cli->tc_in_heap);
1526 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1528 cli->tc_in_heap = true;
1529 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1530 list_add_tail(&nrq->nr_u.tbf.tr_list,
1532 if (policy->pol_nrs->nrs_throttling) {
1533 __u64 deadline = cli->tc_check_time +
1535 if ((head->th_deadline > deadline) &&
1536 (hrtimer_try_to_cancel(&head->th_timer)
1539 head->th_deadline = deadline;
1540 time = ktime_set(0, 0);
1541 time = ktime_add_ns(time, deadline);
1542 hrtimer_start(&head->th_timer, time,
1548 LASSERT(cli->tc_in_heap);
1549 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1550 list_add_tail(&nrq->nr_u.tbf.tr_list,
1557 * Removes request \a nrq from \a policy's list of queued requests.
1559 * \param[in] policy The policy
1560 * \param[in] nrq The request to remove
1562 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1563 struct ptlrpc_nrs_request *nrq)
1565 struct nrs_tbf_head *head;
1566 struct nrs_tbf_client *cli;
1568 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1570 cli = container_of(nrs_request_resource(nrq),
1571 struct nrs_tbf_client, tc_res);
1572 head = container_of(nrs_request_resource(nrq)->res_parent,
1573 struct nrs_tbf_head, th_res);
1575 LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1576 list_del_init(&nrq->nr_u.tbf.tr_list);
1577 if (list_empty(&cli->tc_list)) {
1578 cfs_binheap_remove(head->th_binheap,
1580 cli->tc_in_heap = false;
1582 cfs_binheap_relocate(head->th_binheap,
1588 * Prints a debug statement right before the request \a nrq stops being
1591 * \param[in] policy The policy handling the request
1592 * \param[in] nrq The request being handled
1594 * \see ptlrpc_server_finish_request()
1595 * \see ptlrpc_nrs_req_stop_nolock()
1597 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1598 struct ptlrpc_nrs_request *nrq)
1600 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1603 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1605 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1606 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1607 nrq->nr_u.tbf.tr_sequence);
1610 #ifdef CONFIG_PROC_FS
1617 * The maximum RPC rate.
1619 #define LPROCFS_NRS_RATE_MAX 65535
1622 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1624 struct ptlrpc_service *svc = m->private;
1627 seq_printf(m, "regular_requests:\n");
1629 * Perform two separate calls to this as only one of the NRS heads'
1630 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1631 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1633 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1635 NRS_CTL_TBF_RD_RULE,
1639 * -ENOSPC means buf in the parameter m is overflow, return 0
1640 * here to let upper layer function seq_read alloc a larger
1641 * memory area and do this process again.
1643 } else if (rc == -ENOSPC) {
1647 * Ignore -ENODEV as the regular NRS head's policy may be in the
1648 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1650 } else if (rc != -ENODEV) {
1654 if (!nrs_svc_has_hp(svc))
1657 seq_printf(m, "high_priority_requests:\n");
1658 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1660 NRS_CTL_TBF_RD_RULE,
1664 * -ENOSPC means buf in the parameter m is overflow, return 0
1665 * here to let upper layer function seq_read alloc a larger
1666 * memory area and do this process again.
1668 } else if (rc == -ENOSPC) {
1677 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
1681 if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_JOBID)
1682 rc = nrs_tbf_jobid_parse(cmd, token);
1683 else if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_NID)
1684 rc = nrs_tbf_nid_parse(cmd, token);
1685 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_INVALID)
1694 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1696 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1697 if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_JOBID)
1698 nrs_tbf_jobid_cmd_fini(cmd);
1699 else if (cmd->u.tc_start.ts_valid_type & NRS_TBF_FLAG_NID)
1700 nrs_tbf_nid_cmd_fini(cmd);
1704 static bool name_is_valid(const char *name)
1708 for (i = 0; i < strlen(name); i++) {
1709 if ((!isalnum(name[i])) &&
1717 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
1725 key = strsep(&val, "=");
1726 if (val == NULL || strlen(val) == 0)
1729 /* Key of the value pair */
1730 if (strcmp(key, "rate") == 0) {
1731 rc = kstrtoull(val, 10, &rate);
1735 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
1738 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
1739 cmd->u.tc_start.ts_rpc_rate = rate;
1740 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
1741 cmd->u.tc_change.tc_rpc_rate = rate;
1751 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
1758 while (val != NULL && strlen(val) != 0) {
1759 token = strsep(&val, " ");
1760 rc = nrs_tbf_parse_value_pair(cmd, token);
1765 switch (cmd->tc_cmd) {
1766 case NRS_CTL_TBF_START_RULE:
1767 if (cmd->u.tc_start.ts_rpc_rate == 0)
1768 cmd->u.tc_start.ts_rpc_rate = tbf_rate;
1770 case NRS_CTL_TBF_CHANGE_RULE:
1771 if (cmd->u.tc_change.tc_rpc_rate == 0)
1774 case NRS_CTL_TBF_STOP_RULE:
1782 static struct nrs_tbf_cmd *
1783 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
1785 static struct nrs_tbf_cmd *cmd;
1792 GOTO(out, rc = -ENOMEM);
1793 memset(cmd, 0, sizeof(*cmd));
1796 token = strsep(&val, " ");
1797 if (val == NULL || strlen(val) == 0)
1798 GOTO(out_free_cmd, rc = -EINVAL);
1800 /* Type of the command */
1801 if (strcmp(token, "start") == 0) {
1802 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1803 cmd->u.tc_start.ts_valid_type = type_flag;
1804 } else if (strcmp(token, "stop") == 0)
1805 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1806 else if (strcmp(token, "change") == 0)
1807 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
1809 GOTO(out_free_cmd, rc = -EINVAL);
1811 /* Name of the rule */
1812 token = strsep(&val, " ");
1813 if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
1814 !name_is_valid(token))
1815 GOTO(out_free_cmd, rc = -EINVAL);
1816 cmd->tc_name = token;
1818 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1822 val = strrchr(token, '}');
1824 GOTO(out_free_cmd, rc = -EINVAL);
1830 } else if (*val == ' ') {
1834 GOTO(out_free_cmd, rc = -EINVAL);
1836 rc = nrs_tbf_id_parse(cmd, token);
1838 GOTO(out_free_cmd, rc);
1841 rc = nrs_tbf_parse_value_pairs(cmd, val);
1843 GOTO(out_cmd_fini, rc = -EINVAL);
1846 nrs_tbf_cmd_fini(cmd);
1856 * Get the TBF policy type (nid, jobid, etc) preset by
1857 * proc entry 'nrs_policies' for command buffer parsing.
1859 * \param[in] svc the PTLRPC service
1860 * \param[in] queue the NRS queue type
1862 * \retval the preset TBF policy type flag
1865 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
1870 rc = ptlrpc_nrs_policy_control(svc, queue,
1872 NRS_CTL_TBF_RD_TYPE_FLAG,
1875 type = NRS_TBF_FLAG_INVALID;
1880 extern struct nrs_core nrs_core;
1881 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1883 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
1884 const char __user *buffer,
1885 size_t count, loff_t *off)
1887 struct seq_file *m = file->private_data;
1888 struct ptlrpc_service *svc = m->private;
1892 static struct nrs_tbf_cmd *cmd;
1893 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1894 unsigned long length;
1897 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1898 if (kernbuf == NULL)
1899 GOTO(out, rc = -ENOMEM);
1901 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1902 GOTO(out_free_kernbuff, rc = -EINVAL);
1904 if (copy_from_user(kernbuf, buffer, count))
1905 GOTO(out_free_kernbuff, rc = -EFAULT);
1908 token = strsep(&val, " ");
1910 GOTO(out_free_kernbuff, rc = -EINVAL);
1912 if (strcmp(token, "reg") == 0) {
1913 queue = PTLRPC_NRS_QUEUE_REG;
1914 } else if (strcmp(token, "hp") == 0) {
1915 queue = PTLRPC_NRS_QUEUE_HP;
1917 kernbuf[strlen(token)] = ' ';
1920 length = strlen(val);
1923 GOTO(out_free_kernbuff, rc = -EINVAL);
1925 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1926 GOTO(out_free_kernbuff, rc = -ENODEV);
1927 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1928 queue = PTLRPC_NRS_QUEUE_REG;
1930 cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
1932 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1935 * Serialize NRS core lprocfs operations with policy registration/
1938 mutex_lock(&nrs_core.nrs_mutex);
1939 rc = ptlrpc_nrs_policy_control(svc, queue,
1941 NRS_CTL_TBF_WR_RULE,
1943 mutex_unlock(&nrs_core.nrs_mutex);
1945 nrs_tbf_cmd_fini(cmd);
1948 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1950 return rc ? rc : count;
1952 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1955 * Initializes a TBF policy's lprocfs interface for service \a svc
1957 * \param[in] svc the service
1960 * \retval != 0 error
1962 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1964 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
1965 { .name = "nrs_tbf_rule",
1966 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1971 if (svc->srv_procroot == NULL)
1974 return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
1978 * Cleans up a TBF policy's lprocfs interface for service \a svc
1980 * \param[in] svc the service
1982 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1984 if (svc->srv_procroot == NULL)
1987 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1990 #endif /* CONFIG_PROC_FS */
1993 * TBF policy operations
1995 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1996 .op_policy_start = nrs_tbf_start,
1997 .op_policy_stop = nrs_tbf_stop,
1998 .op_policy_ctl = nrs_tbf_ctl,
1999 .op_res_get = nrs_tbf_res_get,
2000 .op_res_put = nrs_tbf_res_put,
2001 .op_req_get = nrs_tbf_req_get,
2002 .op_req_enqueue = nrs_tbf_req_add,
2003 .op_req_dequeue = nrs_tbf_req_del,
2004 .op_req_stop = nrs_tbf_req_stop,
2005 #ifdef CONFIG_PROC_FS
2006 .op_lprocfs_init = nrs_tbf_lprocfs_init,
2007 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
2012 * TBF policy configuration
2014 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
2015 .nc_name = NRS_POL_NAME_TBF,
2016 .nc_ops = &nrs_tbf_ops,
2017 .nc_compat = nrs_policy_compat_all,
2024 #endif /* HAVE_SERVER_SUPPORT */