4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (C) 2013 DataDirect Networks, Inc.
27 * lustre/ptlrpc/nrs_tbf.c
29 * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
33 #ifdef HAVE_SERVER_SUPPORT
40 #define DEBUG_SUBSYSTEM S_RPC
42 #include <liblustre.h>
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <libcfs/libcfs.h>
47 #include "ptlrpc_internal.h"
52 * Token Bucket Filter over client NIDs
57 #define NRS_POL_NAME_TBF "tbf"
59 int tbf_jobid_cache_size = 8192;
60 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
61 "The size of jobid cache");
64 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
65 "Default rate limit in RPCs/s");
68 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
69 "How many tokens that a client can save up");
71 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
73 struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
75 struct ptlrpc_nrs *nrs = head->th_res.res_policy->pol_nrs;
76 struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
78 spin_lock(&nrs->nrs_lock);
79 nrs->nrs_throttling = 0;
80 spin_unlock(&nrs->nrs_lock);
81 wake_up(&svcpt->scp_waitq);
83 return HRTIMER_NORESTART;
86 #define NRS_TBF_DEFAULT_RULE "default"
88 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
90 LASSERT(atomic_read(&rule->tr_ref) == 0);
91 LASSERT(list_empty(&rule->tr_cli_list));
92 LASSERT(list_empty(&rule->tr_linkage));
94 rule->tr_head->th_ops->o_rule_fini(rule);
99 * Decreases the rule's usage reference count, and stops the rule in case it
100 * was already stopping and have no more outstanding usage references (which
101 * indicates it has no more queued or started requests, and can be safely
104 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
106 if (atomic_dec_and_test(&rule->tr_ref))
107 nrs_tbf_rule_fini(rule);
111 * Increases the rule's usage reference count.
113 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
115 atomic_inc(&rule->tr_ref);
119 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
121 LASSERT(!cfs_list_empty(&cli->tc_linkage));
122 LASSERT(cli->tc_rule);
123 cfs_list_del_init(&cli->tc_linkage);
124 nrs_tbf_rule_put(cli->tc_rule);
129 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
130 struct nrs_tbf_client *cli)
133 struct nrs_tbf_rule *rule = cli->tc_rule;
135 cli->tc_rpc_rate = rule->tr_rpc_rate;
136 cli->tc_nsecs = rule->tr_nsecs;
137 cli->tc_depth = rule->tr_depth;
138 cli->tc_ntoken = rule->tr_depth;
139 cli->tc_check_time = ktime_to_ns(ktime_get());
140 cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
141 cli->tc_rule_generation = rule->tr_generation;
144 cfs_binheap_relocate(head->th_binheap,
149 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
150 struct nrs_tbf_rule *rule,
151 struct nrs_tbf_client *cli)
153 if (!list_empty(&cli->tc_linkage)) {
154 LASSERT(rule != cli->tc_rule);
155 nrs_tbf_cli_rule_put(cli);
157 LASSERT(cli->tc_rule == NULL);
158 LASSERT(list_empty(&cli->tc_linkage));
159 /* Rule's ref is added before called */
161 list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
162 nrs_tbf_cli_reset_value(head, cli);
166 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
168 return rule->tr_head->th_ops->o_rule_dump(rule, m);
172 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
174 struct nrs_tbf_rule *rule;
177 LASSERT(head != NULL);
178 spin_lock(&head->th_rule_lock);
179 /* List the rules from newest to oldest */
180 list_for_each_entry(rule, &head->th_list, tr_linkage) {
181 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
182 rc = nrs_tbf_rule_dump(rule, m);
188 spin_unlock(&head->th_rule_lock);
193 static struct nrs_tbf_rule *
194 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
197 struct nrs_tbf_rule *rule;
199 LASSERT(head != NULL);
200 cfs_list_for_each_entry(rule, &head->th_list, tr_linkage) {
201 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
202 if (strcmp(rule->tr_name, name) == 0) {
203 nrs_tbf_rule_get(rule);
210 static struct nrs_tbf_rule *
211 nrs_tbf_rule_find(struct nrs_tbf_head *head,
214 struct nrs_tbf_rule *rule;
216 LASSERT(head != NULL);
217 spin_lock(&head->th_rule_lock);
218 rule = nrs_tbf_rule_find_nolock(head, name);
219 spin_unlock(&head->th_rule_lock);
223 static struct nrs_tbf_rule *
224 nrs_tbf_rule_match(struct nrs_tbf_head *head,
225 struct nrs_tbf_client *cli)
227 struct nrs_tbf_rule *rule = NULL;
228 struct nrs_tbf_rule *tmp_rule;
230 spin_lock(&head->th_rule_lock);
231 /* Match the newest rule in the list */
232 cfs_list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
233 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
234 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
241 rule = head->th_rule;
243 nrs_tbf_rule_get(rule);
244 spin_unlock(&head->th_rule_lock);
249 nrs_tbf_cli_init(struct nrs_tbf_head *head,
250 struct nrs_tbf_client *cli,
251 struct ptlrpc_request *req)
253 struct nrs_tbf_rule *rule;
255 cli->tc_in_heap = false;
256 head->th_ops->o_cli_init(cli, req);
257 CFS_INIT_LIST_HEAD(&cli->tc_list);
258 CFS_INIT_LIST_HEAD(&cli->tc_linkage);
259 atomic_set(&cli->tc_ref, 1);
260 rule = nrs_tbf_rule_match(head, cli);
261 nrs_tbf_cli_reset(head, rule, cli);
265 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
267 LASSERT(cfs_list_empty(&cli->tc_list));
268 LASSERT(!cli->tc_in_heap);
269 LASSERT(atomic_read(&cli->tc_ref) == 0);
270 nrs_tbf_cli_rule_put(cli);
275 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
276 struct nrs_tbf_head *head,
277 struct nrs_tbf_cmd *start)
279 struct nrs_tbf_rule *rule, *tmp_rule;
282 rule = nrs_tbf_rule_find(head, start->tc_name);
284 nrs_tbf_rule_put(rule);
288 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
292 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
293 rule->tr_rpc_rate = start->tc_rpc_rate;
294 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
295 rule->tr_depth = tbf_depth;
296 atomic_set(&rule->tr_ref, 1);
297 CFS_INIT_LIST_HEAD(&rule->tr_cli_list);
298 CFS_INIT_LIST_HEAD(&rule->tr_nids);
300 rc = head->th_ops->o_rule_init(policy, rule, start);
306 /* Add as the newest rule */
307 spin_lock(&head->th_rule_lock);
308 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
310 spin_unlock(&head->th_rule_lock);
311 nrs_tbf_rule_put(tmp_rule);
312 nrs_tbf_rule_put(rule);
315 cfs_list_add(&rule->tr_linkage, &head->th_list);
316 rule->tr_head = head;
317 spin_unlock(&head->th_rule_lock);
318 atomic_inc(&head->th_rule_sequence);
319 if (start->tc_rule_flags & NTRS_DEFAULT) {
320 rule->tr_flags |= NTRS_DEFAULT;
321 LASSERT(head->th_rule == NULL);
322 head->th_rule = rule;
329 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
330 struct nrs_tbf_head *head,
331 struct nrs_tbf_cmd *change)
333 struct nrs_tbf_rule *rule;
335 assert_spin_locked(&policy->pol_nrs->nrs_lock);
337 rule = nrs_tbf_rule_find(head, change->tc_name);
341 rule->tr_rpc_rate = change->tc_rpc_rate;
342 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
343 rule->tr_generation++;
344 nrs_tbf_rule_put(rule);
350 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
351 struct nrs_tbf_head *head,
352 struct nrs_tbf_cmd *stop)
354 struct nrs_tbf_rule *rule;
356 assert_spin_locked(&policy->pol_nrs->nrs_lock);
358 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
361 rule = nrs_tbf_rule_find(head, stop->tc_name);
365 cfs_list_del_init(&rule->tr_linkage);
366 rule->tr_flags |= NTRS_STOPPING;
367 nrs_tbf_rule_put(rule);
368 nrs_tbf_rule_put(rule);
374 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
375 struct nrs_tbf_head *head,
376 struct nrs_tbf_cmd *cmd)
380 assert_spin_locked(&policy->pol_nrs->nrs_lock);
382 switch (cmd->tc_cmd) {
383 case NRS_CTL_TBF_START_RULE:
384 if (!(cmd->tc_valid_types & head->th_type_flag))
387 spin_unlock(&policy->pol_nrs->nrs_lock);
388 rc = nrs_tbf_rule_start(policy, head, cmd);
389 spin_lock(&policy->pol_nrs->nrs_lock);
391 case NRS_CTL_TBF_CHANGE_RATE:
392 rc = nrs_tbf_rule_change(policy, head, cmd);
394 case NRS_CTL_TBF_STOP_RULE:
395 rc = nrs_tbf_rule_stop(policy, head, cmd);
396 /* Take it as a success, if not exists at all */
397 return rc == -ENOENT ? 0 : rc;
404 * Binary heap predicate.
406 * \param[in] e1 the first binheap node to compare
407 * \param[in] e2 the second binheap node to compare
412 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
414 struct nrs_tbf_client *cli1;
415 struct nrs_tbf_client *cli2;
417 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
418 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
420 if (cli1->tc_check_time + cli1->tc_nsecs <
421 cli2->tc_check_time + cli2->tc_nsecs)
423 else if (cli1->tc_check_time + cli1->tc_nsecs >
424 cli2->tc_check_time + cli2->tc_nsecs)
427 if (cli1->tc_check_time < cli2->tc_check_time)
429 else if (cli1->tc_check_time > cli2->tc_check_time)
432 /* Maybe need more comparasion, e.g. request number in the rules */
437 * TBF binary heap operations
439 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
442 .hop_compare = tbf_cli_compare,
445 static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
448 return cfs_hash_djb2_hash(key, strlen(key), mask);
451 static int nrs_tbf_jobid_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
453 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
454 struct nrs_tbf_client,
457 return (strcmp(cli->tc_jobid, key) == 0);
460 static void *nrs_tbf_jobid_hop_key(cfs_hlist_node_t *hnode)
462 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
463 struct nrs_tbf_client,
466 return cli->tc_jobid;
469 static void *nrs_tbf_jobid_hop_object(cfs_hlist_node_t *hnode)
471 return cfs_hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
474 static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
476 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
477 struct nrs_tbf_client,
480 atomic_inc(&cli->tc_ref);
483 static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
485 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
486 struct nrs_tbf_client,
489 atomic_dec(&cli->tc_ref);
492 static void nrs_tbf_jobid_hop_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
495 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
496 struct nrs_tbf_client,
499 LASSERT(atomic_read(&cli->tc_ref) == 0);
500 nrs_tbf_cli_fini(cli);
503 static cfs_hash_ops_t nrs_tbf_jobid_hash_ops = {
504 .hs_hash = nrs_tbf_jobid_hop_hash,
505 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
506 .hs_key = nrs_tbf_jobid_hop_key,
507 .hs_object = nrs_tbf_jobid_hop_object,
508 .hs_get = nrs_tbf_jobid_hop_get,
509 .hs_put = nrs_tbf_jobid_hop_put,
510 .hs_put_locked = nrs_tbf_jobid_hop_put,
511 .hs_exit = nrs_tbf_jobid_hop_exit,
514 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
515 CFS_HASH_NO_ITEMREF | \
518 static struct nrs_tbf_client *
519 nrs_tbf_jobid_hash_lookup(cfs_hash_t *hs,
523 cfs_hlist_node_t *hnode;
524 struct nrs_tbf_client *cli;
526 /* cfs_hash_bd_peek_locked is a somehow "internal" function
527 * of cfs_hash, it doesn't add refcount on object. */
528 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
532 cfs_hash_get(hs, hnode);
533 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
534 if (!cfs_list_empty(&cli->tc_lru))
535 cfs_list_del_init(&cli->tc_lru);
539 #define NRS_TBF_JOBID_NULL ""
541 static struct nrs_tbf_client *
542 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
543 struct ptlrpc_request *req)
546 struct nrs_tbf_client *cli;
547 cfs_hash_t *hs = head->th_cli_hash;
550 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
552 jobid = NRS_TBF_JOBID_NULL;
553 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
554 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
555 cfs_hash_bd_unlock(hs, &bd, 1);
560 static struct nrs_tbf_client *
561 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
562 struct nrs_tbf_client *cli)
565 struct nrs_tbf_client *ret;
566 cfs_hash_t *hs = head->th_cli_hash;
569 jobid = cli->tc_jobid;
570 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
571 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
573 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
576 cfs_hash_bd_unlock(hs, &bd, 1);
582 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
583 struct nrs_tbf_client *cli)
586 cfs_hash_t *hs = head->th_cli_hash;
587 struct nrs_tbf_bucket *bkt;
589 CFS_LIST_HEAD (zombies);
591 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
592 bkt = cfs_hash_bd_extra_get(hs, &bd);
593 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
595 LASSERT(cfs_list_empty(&cli->tc_lru));
596 cfs_list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
599 * Check and purge the LRU, there is at least one client in the LRU.
601 hw = tbf_jobid_cache_size >>
602 (hs->hs_cur_bits - hs->hs_bkt_bits);
603 while (cfs_hash_bd_count_get(&bd) > hw) {
604 if (unlikely(cfs_list_empty(&bkt->ntb_lru)))
606 cli = cfs_list_entry(bkt->ntb_lru.next,
607 struct nrs_tbf_client,
609 LASSERT(atomic_read(&cli->tc_ref) == 0);
610 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
611 cfs_list_move(&cli->tc_lru, &zombies);
613 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
615 while (!cfs_list_empty(&zombies)) {
616 cli = container_of0(zombies.next,
617 struct nrs_tbf_client, tc_lru);
618 cfs_list_del_init(&cli->tc_lru);
619 nrs_tbf_cli_fini(cli);
624 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
625 struct ptlrpc_request *req)
627 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
630 jobid = NRS_TBF_JOBID_NULL;
631 LASSERT(strlen(jobid) < JOBSTATS_JOBID_SIZE);
632 CFS_INIT_LIST_HEAD(&cli->tc_lru);
633 memcpy(cli->tc_jobid, jobid, strlen(jobid));
636 static int nrs_tbf_jobid_hash_order(void)
640 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
646 #define NRS_TBF_JOBID_BKT_BITS 10
649 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
650 struct nrs_tbf_head *head)
652 struct nrs_tbf_cmd start;
653 struct nrs_tbf_bucket *bkt;
659 bits = nrs_tbf_jobid_hash_order();
660 if (bits < NRS_TBF_JOBID_BKT_BITS)
661 bits = NRS_TBF_JOBID_BKT_BITS;
662 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
665 NRS_TBF_JOBID_BKT_BITS,
669 &nrs_tbf_jobid_hash_ops,
670 NRS_TBF_JOBID_HASH_FLAGS);
671 if (head->th_cli_hash == NULL)
674 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
675 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
676 CFS_INIT_LIST_HEAD(&bkt->ntb_lru);
679 memset(&start, 0, sizeof(start));
680 start.tc_jobids_str = "*";
682 start.tc_rpc_rate = tbf_rate;
683 start.tc_rule_flags = NTRS_DEFAULT;
684 start.tc_name = NRS_TBF_DEFAULT_RULE;
685 CFS_INIT_LIST_HEAD(&start.tc_jobids);
686 rc = nrs_tbf_rule_start(policy, head, &start);
692 * Frees jobid of \a list.
696 nrs_tbf_jobid_list_free(cfs_list_t *jobid_list)
698 struct nrs_tbf_jobid *jobid, *n;
700 cfs_list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
701 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
702 cfs_list_del(&jobid->tj_linkage);
703 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
708 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, cfs_list_t *jobid_list)
710 struct nrs_tbf_jobid *jobid;
712 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
716 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
717 if (jobid->tj_id == NULL) {
718 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
722 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
723 cfs_list_add_tail(&jobid->tj_linkage, jobid_list);
728 nrs_tbf_jobid_list_match(cfs_list_t *jobid_list, char *id)
730 struct nrs_tbf_jobid *jobid;
732 cfs_list_for_each_entry(jobid, jobid_list, tj_linkage) {
733 if (strcmp(id, jobid->tj_id) == 0)
740 nrs_tbf_jobid_list_parse(char *str, int len, cfs_list_t *jobid_list)
749 CFS_INIT_LIST_HEAD(jobid_list);
751 rc = cfs_gettok(&src, ' ', &res);
756 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
761 nrs_tbf_jobid_list_free(jobid_list);
765 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
767 if (!cfs_list_empty(&cmd->tc_jobids))
768 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
769 if (cmd->tc_jobids_str)
770 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
773 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
777 OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
778 if (cmd->tc_jobids_str == NULL)
781 memcpy(cmd->tc_jobids_str, id, strlen(id));
783 /* parse jobid list */
784 rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
785 strlen(cmd->tc_jobids_str),
788 nrs_tbf_jobid_cmd_fini(cmd);
793 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
794 struct nrs_tbf_rule *rule,
795 struct nrs_tbf_cmd *start)
799 LASSERT(start->tc_jobids_str);
800 OBD_ALLOC(rule->tr_jobids_str,
801 strlen(start->tc_jobids_str) + 1);
802 if (rule->tr_jobids_str == NULL)
805 memcpy(rule->tr_jobids_str,
806 start->tc_jobids_str,
807 strlen(start->tc_jobids_str));
809 CFS_INIT_LIST_HEAD(&rule->tr_jobids);
810 if (!cfs_list_empty(&start->tc_jobids)) {
811 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
812 strlen(rule->tr_jobids_str),
815 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
818 OBD_FREE(rule->tr_jobids_str,
819 strlen(start->tc_jobids_str) + 1);
824 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
826 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
827 rule->tr_jobids_str, rule->tr_rpc_rate,
828 atomic_read(&rule->tr_ref) - 1);
832 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
833 struct nrs_tbf_client *cli)
835 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
838 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
840 if (!cfs_list_empty(&rule->tr_jobids))
841 nrs_tbf_jobid_list_free(&rule->tr_jobids);
842 LASSERT(rule->tr_jobids_str != NULL);
843 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
846 struct nrs_tbf_ops nrs_tbf_jobid_ops = {
847 .o_name = NRS_TBF_TYPE_JOBID,
848 .o_startup = nrs_tbf_jobid_startup,
849 .o_cli_find = nrs_tbf_jobid_cli_find,
850 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
851 .o_cli_put = nrs_tbf_jobid_cli_put,
852 .o_cli_init = nrs_tbf_jobid_cli_init,
853 .o_rule_init = nrs_tbf_jobid_rule_init,
854 .o_rule_dump = nrs_tbf_jobid_rule_dump,
855 .o_rule_match = nrs_tbf_jobid_rule_match,
856 .o_rule_fini = nrs_tbf_jobid_rule_fini,
860 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
862 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
863 * nrs_tbf_client objects.
865 #define NRS_TBF_NID_BKT_BITS 8
866 #define NRS_TBF_NID_BITS 16
868 static unsigned nrs_tbf_nid_hop_hash(cfs_hash_t *hs, const void *key,
871 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
874 static int nrs_tbf_nid_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
876 lnet_nid_t *nid = (lnet_nid_t *)key;
877 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
878 struct nrs_tbf_client,
881 return *nid == cli->tc_nid;
884 static void *nrs_tbf_nid_hop_key(cfs_hlist_node_t *hnode)
886 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
887 struct nrs_tbf_client,
893 static void *nrs_tbf_nid_hop_object(cfs_hlist_node_t *hnode)
895 return cfs_hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
898 static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
900 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
901 struct nrs_tbf_client,
904 atomic_inc(&cli->tc_ref);
907 static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
909 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
910 struct nrs_tbf_client,
913 atomic_dec(&cli->tc_ref);
916 static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
918 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
919 struct nrs_tbf_client,
922 LASSERTF(atomic_read(&cli->tc_ref) == 0,
923 "Busy TBF object from client with NID %s, with %d refs\n",
924 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
926 nrs_tbf_cli_fini(cli);
929 static cfs_hash_ops_t nrs_tbf_nid_hash_ops = {
930 .hs_hash = nrs_tbf_nid_hop_hash,
931 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
932 .hs_key = nrs_tbf_nid_hop_key,
933 .hs_object = nrs_tbf_nid_hop_object,
934 .hs_get = nrs_tbf_nid_hop_get,
935 .hs_put = nrs_tbf_nid_hop_put,
936 .hs_put_locked = nrs_tbf_nid_hop_put,
937 .hs_exit = nrs_tbf_nid_hop_exit,
940 static struct nrs_tbf_client *
941 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
942 struct ptlrpc_request *req)
944 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
947 static struct nrs_tbf_client *
948 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
949 struct nrs_tbf_client *cli)
951 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
956 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
957 struct nrs_tbf_client *cli)
959 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
963 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
964 struct nrs_tbf_head *head)
966 struct nrs_tbf_cmd start;
969 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
972 NRS_TBF_NID_BKT_BITS, 0,
975 &nrs_tbf_nid_hash_ops,
976 CFS_HASH_RW_BKTLOCK);
977 if (head->th_cli_hash == NULL)
980 memset(&start, 0, sizeof(start));
981 start.tc_nids_str = "*";
983 start.tc_rpc_rate = tbf_rate;
984 start.tc_rule_flags = NTRS_DEFAULT;
985 start.tc_name = NRS_TBF_DEFAULT_RULE;
986 CFS_INIT_LIST_HEAD(&start.tc_nids);
987 rc = nrs_tbf_rule_start(policy, head, &start);
993 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
994 struct ptlrpc_request *req)
996 cli->tc_nid = req->rq_peer.nid;
999 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1000 struct nrs_tbf_rule *rule,
1001 struct nrs_tbf_cmd *start)
1003 LASSERT(start->tc_nids_str);
1004 OBD_ALLOC(rule->tr_nids_str,
1005 strlen(start->tc_nids_str) + 1);
1006 if (rule->tr_nids_str == NULL)
1009 memcpy(rule->tr_nids_str,
1011 strlen(start->tc_nids_str));
1013 CFS_INIT_LIST_HEAD(&rule->tr_nids);
1014 if (!cfs_list_empty(&start->tc_nids)) {
1015 if (cfs_parse_nidlist(rule->tr_nids_str,
1016 strlen(rule->tr_nids_str),
1017 &rule->tr_nids) <= 0) {
1018 CERROR("nids {%s} illegal\n",
1020 OBD_FREE(rule->tr_nids_str,
1021 strlen(start->tc_nids_str) + 1);
1029 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1031 return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1032 rule->tr_nids_str, rule->tr_rpc_rate,
1033 atomic_read(&rule->tr_ref) - 1);
1037 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1038 struct nrs_tbf_client *cli)
1040 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1043 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1045 if (!cfs_list_empty(&rule->tr_nids))
1046 cfs_free_nidlist(&rule->tr_nids);
1047 LASSERT(rule->tr_nids_str != NULL);
1048 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1051 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1053 if (!cfs_list_empty(&cmd->tc_nids))
1054 cfs_free_nidlist(&cmd->tc_nids);
1055 if (cmd->tc_nids_str)
1056 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1059 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1061 OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1062 if (cmd->tc_nids_str == NULL)
1065 memcpy(cmd->tc_nids_str, id, strlen(id));
1067 /* parse NID list */
1068 if (cfs_parse_nidlist(cmd->tc_nids_str,
1069 strlen(cmd->tc_nids_str),
1070 &cmd->tc_nids) <= 0) {
1071 nrs_tbf_nid_cmd_fini(cmd);
1078 struct nrs_tbf_ops nrs_tbf_nid_ops = {
1079 .o_name = NRS_TBF_TYPE_NID,
1080 .o_startup = nrs_tbf_nid_startup,
1081 .o_cli_find = nrs_tbf_nid_cli_find,
1082 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1083 .o_cli_put = nrs_tbf_nid_cli_put,
1084 .o_cli_init = nrs_tbf_nid_cli_init,
1085 .o_rule_init = nrs_tbf_nid_rule_init,
1086 .o_rule_dump = nrs_tbf_nid_rule_dump,
1087 .o_rule_match = nrs_tbf_nid_rule_match,
1088 .o_rule_fini = nrs_tbf_nid_rule_fini,
1092 * Is called before the policy transitions into
1093 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1094 * policy-specific private data structure.
1096 * \param[in] policy The policy to start
1098 * \retval -ENOMEM OOM error
1101 * \see nrs_policy_register()
1102 * \see nrs_policy_ctl()
1104 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1106 struct nrs_tbf_head *head;
1107 struct nrs_tbf_ops *ops;
1111 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1112 GOTO(out, rc = -EINVAL);
1114 if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1115 ops = &nrs_tbf_nid_ops;
1116 type = NRS_TBF_FLAG_NID;
1117 } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1118 ops = &nrs_tbf_jobid_ops;
1119 type = NRS_TBF_FLAG_JOBID;
1121 GOTO(out, rc = -ENOTSUPP);
1123 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1125 GOTO(out, rc = -ENOMEM);
1127 memcpy(head->th_type, arg, strlen(arg));
1128 head->th_type[strlen(arg)] = '\0';
1130 head->th_type_flag = type;
1132 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1133 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1134 nrs_pol2cptab(policy),
1135 nrs_pol2cptid(policy));
1136 if (head->th_binheap == NULL)
1137 GOTO(out_free_head, rc = -ENOMEM);
1139 atomic_set(&head->th_rule_sequence, 0);
1140 spin_lock_init(&head->th_rule_lock);
1141 CFS_INIT_LIST_HEAD(&head->th_list);
1142 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1143 head->th_timer.function = nrs_tbf_timer_cb;
1144 rc = head->th_ops->o_startup(policy, head);
1146 GOTO(out_free_heap, rc);
1148 policy->pol_private = head;
1151 cfs_binheap_destroy(head->th_binheap);
1159 * Is called before the policy transitions into
1160 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1161 * private data structure.
1163 * \param[in] policy The policy to stop
1165 * \see nrs_policy_stop0()
1167 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1169 struct nrs_tbf_head *head = policy->pol_private;
1170 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1171 struct nrs_tbf_rule *rule, *n;
1173 LASSERT(head != NULL);
1174 LASSERT(head->th_cli_hash != NULL);
1175 hrtimer_cancel(&head->th_timer);
1176 /* Should cleanup hash first before free rules */
1177 cfs_hash_putref(head->th_cli_hash);
1178 cfs_list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1179 cfs_list_del_init(&rule->tr_linkage);
1180 nrs_tbf_rule_put(rule);
1182 LASSERT(cfs_list_empty(&head->th_list));
1183 LASSERT(head->th_binheap != NULL);
1184 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1185 cfs_binheap_destroy(head->th_binheap);
1187 spin_lock(&nrs->nrs_lock);
1188 nrs->nrs_throttling = 0;
1189 spin_unlock(&nrs->nrs_lock);
1190 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1194 * Performs a policy-specific ctl function on TBF policy instances; similar
1197 * \param[in] policy the policy instance
1198 * \param[in] opc the opcode
1199 * \param[in,out] arg used for passing parameters and information
1201 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1202 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1204 * \retval 0 operation carried out successfully
1207 int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy, enum ptlrpc_nrs_ctl opc,
1213 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1215 switch ((enum nrs_ctl_tbf)opc) {
1220 * Read RPC rate size of a policy instance.
1222 case NRS_CTL_TBF_RD_RULE: {
1223 struct nrs_tbf_head *head = policy->pol_private;
1224 struct seq_file *m = (struct seq_file *) arg;
1225 struct ptlrpc_service_part *svcpt;
1227 svcpt = policy->pol_nrs->nrs_svcpt;
1228 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1230 rc = nrs_tbf_rule_dump_all(head, m);
1235 * Write RPC rate of a policy instance.
1237 case NRS_CTL_TBF_WR_RULE: {
1238 struct nrs_tbf_head *head = policy->pol_private;
1239 struct nrs_tbf_cmd *cmd;
1241 cmd = (struct nrs_tbf_cmd *)arg;
1242 rc = nrs_tbf_command(policy,
1253 * Is called for obtaining a TBF policy resource.
1255 * \param[in] policy The policy on which the request is being asked for
1256 * \param[in] nrq The request for which resources are being taken
1257 * \param[in] parent Parent resource, unused in this policy
1258 * \param[out] resp Resources references are placed in this array
1259 * \param[in] moving_req Signifies limited caller context; unused in this
1263 * \see nrs_resource_get_safe()
1265 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1266 struct ptlrpc_nrs_request *nrq,
1267 const struct ptlrpc_nrs_resource *parent,
1268 struct ptlrpc_nrs_resource **resp,
1271 struct nrs_tbf_head *head;
1272 struct nrs_tbf_client *cli;
1273 struct nrs_tbf_client *tmp;
1274 struct ptlrpc_request *req;
1276 if (parent == NULL) {
1277 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1281 head = container_of(parent, struct nrs_tbf_head, th_res);
1282 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1283 cli = head->th_ops->o_cli_find(head, req);
1285 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1286 LASSERT(cli->tc_rule);
1287 if (cli->tc_rule_sequence !=
1288 atomic_read(&head->th_rule_sequence) ||
1289 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1290 struct nrs_tbf_rule *rule;
1292 rule = nrs_tbf_rule_match(head, cli);
1293 if (rule != cli->tc_rule)
1294 nrs_tbf_cli_reset(head, rule, cli);
1296 nrs_tbf_rule_put(rule);
1297 } else if (cli->tc_rule_generation !=
1298 cli->tc_rule->tr_generation) {
1299 nrs_tbf_cli_reset_value(head, cli);
1301 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1305 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1306 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1309 nrs_tbf_cli_init(head, cli, req);
1310 tmp = head->th_ops->o_cli_findadd(head, cli);
1312 atomic_dec(&cli->tc_ref);
1313 nrs_tbf_cli_fini(cli);
1317 *resp = &cli->tc_res;
1323 * Called when releasing references to the resource hierachy obtained for a
1324 * request for scheduling using the TBF policy.
1326 * \param[in] policy the policy the resource belongs to
1327 * \param[in] res the resource to be released
1329 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1330 const struct ptlrpc_nrs_resource *res)
1332 struct nrs_tbf_head *head;
1333 struct nrs_tbf_client *cli;
1336 * Do nothing for freeing parent, nrs_tbf_net resources
1338 if (res->res_parent == NULL)
1341 cli = container_of(res, struct nrs_tbf_client, tc_res);
1342 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1344 head->th_ops->o_cli_put(head, cli);
1348 * Called when getting a request from the TBF policy for handling, or just
1349 * peeking; removes the request from the policy when it is to be handled.
1351 * \param[in] policy The policy
1352 * \param[in] peek When set, signifies that we just want to examine the
1353 * request, and not handle it, so the request is not removed
1355 * \param[in] force Force the policy to return a request; unused in this
1358 * \retval The request to be handled; this is the next request in the TBF
1361 * \see ptlrpc_nrs_req_get_nolock()
1362 * \see nrs_request_get()
1365 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1366 bool peek, bool force)
1368 struct nrs_tbf_head *head = policy->pol_private;
1369 struct ptlrpc_nrs_request *nrq = NULL;
1370 struct nrs_tbf_client *cli;
1371 cfs_binheap_node_t *node;
1373 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1375 if (!peek && policy->pol_nrs->nrs_throttling)
1378 node = cfs_binheap_root(head->th_binheap);
1379 if (unlikely(node == NULL))
1382 cli = container_of(node, struct nrs_tbf_client, tc_node);
1383 LASSERT(cli->tc_in_heap);
1385 nrq = cfs_list_entry(cli->tc_list.next,
1386 struct ptlrpc_nrs_request,
1389 __u64 now = ktime_to_ns(ktime_get());
1394 deadline = cli->tc_check_time +
1396 LASSERT(now >= cli->tc_check_time);
1397 passed = now - cli->tc_check_time;
1398 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1399 ntoken += cli->tc_ntoken;
1400 if (ntoken > cli->tc_depth)
1401 ntoken = cli->tc_depth;
1403 struct ptlrpc_request *req;
1404 nrq = cfs_list_entry(cli->tc_list.next,
1405 struct ptlrpc_nrs_request,
1407 req = container_of(nrq,
1408 struct ptlrpc_request,
1411 cli->tc_ntoken = ntoken;
1412 cli->tc_check_time = now;
1413 cfs_list_del_init(&nrq->nr_u.tbf.tr_list);
1414 if (cfs_list_empty(&cli->tc_list)) {
1415 cfs_binheap_remove(head->th_binheap,
1417 cli->tc_in_heap = false;
1419 cfs_binheap_relocate(head->th_binheap,
1423 "NRS start %s request from %s, "
1425 policy->pol_desc->pd_name,
1426 libcfs_id2str(req->rq_peer),
1427 nrq->nr_u.tbf.tr_sequence);
1431 spin_lock(&policy->pol_nrs->nrs_lock);
1432 policy->pol_nrs->nrs_throttling = 1;
1433 spin_unlock(&policy->pol_nrs->nrs_lock);
1434 head->th_deadline = deadline;
1435 time = ktime_set(0, 0);
1436 time = ktime_add_ns(time, deadline);
1437 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1445 * Adds request \a nrq to \a policy's list of queued requests
1447 * \param[in] policy The policy
1448 * \param[in] nrq The request to add
1450 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1453 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1454 struct ptlrpc_nrs_request *nrq)
1456 struct nrs_tbf_head *head;
1457 struct nrs_tbf_client *cli;
1460 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1462 cli = container_of(nrs_request_resource(nrq),
1463 struct nrs_tbf_client, tc_res);
1464 head = container_of(nrs_request_resource(nrq)->res_parent,
1465 struct nrs_tbf_head, th_res);
1466 if (cfs_list_empty(&cli->tc_list)) {
1467 LASSERT(!cli->tc_in_heap);
1468 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1470 cli->tc_in_heap = true;
1471 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1472 cfs_list_add_tail(&nrq->nr_u.tbf.tr_list,
1474 if (policy->pol_nrs->nrs_throttling) {
1475 __u64 deadline = cli->tc_check_time +
1477 if ((head->th_deadline > deadline) &&
1478 (hrtimer_try_to_cancel(&head->th_timer)
1481 head->th_deadline = deadline;
1482 time = ktime_set(0, 0);
1483 time = ktime_add_ns(time, deadline);
1484 hrtimer_start(&head->th_timer, time,
1490 LASSERT(cli->tc_in_heap);
1491 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1492 cfs_list_add_tail(&nrq->nr_u.tbf.tr_list,
1499 * Removes request \a nrq from \a policy's list of queued requests.
1501 * \param[in] policy The policy
1502 * \param[in] nrq The request to remove
1504 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1505 struct ptlrpc_nrs_request *nrq)
1507 struct nrs_tbf_head *head;
1508 struct nrs_tbf_client *cli;
1510 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1512 cli = container_of(nrs_request_resource(nrq),
1513 struct nrs_tbf_client, tc_res);
1514 head = container_of(nrs_request_resource(nrq)->res_parent,
1515 struct nrs_tbf_head, th_res);
1517 LASSERT(!cfs_list_empty(&nrq->nr_u.tbf.tr_list));
1518 cfs_list_del_init(&nrq->nr_u.tbf.tr_list);
1519 if (cfs_list_empty(&cli->tc_list)) {
1520 cfs_binheap_remove(head->th_binheap,
1522 cli->tc_in_heap = false;
1524 cfs_binheap_relocate(head->th_binheap,
1530 * Prints a debug statement right before the request \a nrq stops being
1533 * \param[in] policy The policy handling the request
1534 * \param[in] nrq The request being handled
1536 * \see ptlrpc_server_finish_request()
1537 * \see ptlrpc_nrs_req_stop_nolock()
1539 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1540 struct ptlrpc_nrs_request *nrq)
1542 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1545 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1547 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1548 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1549 nrq->nr_u.tbf.tr_sequence);
1559 * The maximum RPC rate.
1561 #define LPROCFS_NRS_RATE_MAX 65535
1564 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1566 struct ptlrpc_service *svc = m->private;
1569 seq_printf(m, "regular_requests:\n");
1571 * Perform two separate calls to this as only one of the NRS heads'
1572 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1573 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1575 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1577 NRS_CTL_TBF_RD_RULE,
1581 * Ignore -ENODEV as the regular NRS head's policy may be in the
1582 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1584 } else if (rc != -ENODEV) {
1588 if (!nrs_svc_has_hp(svc))
1591 seq_printf(m, "high_priority_requests:\n");
1592 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1594 NRS_CTL_TBF_RD_RULE,
1598 * Ignore -ENODEV as the high priority NRS head's policy may be
1599 * in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1601 } else if (rc != -ENODEV) {
1610 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1615 token = strsep(val, "}");
1617 GOTO(out, rc = -EINVAL);
1619 if (strlen(token) <= 1 ||
1621 GOTO(out, rc = -EINVAL);
1625 /* Should be followed by ' ' or nothing */
1626 if ((*val)[0] == '\0')
1628 else if ((*val)[0] == ' ')
1631 GOTO(out, rc = -EINVAL);
1633 rc = nrs_tbf_jobid_parse(cmd, token);
1635 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1637 rc = nrs_tbf_nid_parse(cmd, token);
1639 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1641 if (!cmd->tc_valid_types)
1650 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1652 if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1653 nrs_tbf_jobid_cmd_fini(cmd);
1654 if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1655 nrs_tbf_nid_cmd_fini(cmd);
1658 static struct nrs_tbf_cmd *
1659 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1661 static struct nrs_tbf_cmd *cmd;
1669 GOTO(out, rc = -ENOMEM);
1672 token = strsep(&val, " ");
1673 if (val == NULL || strlen(val) == 0)
1674 GOTO(out_free_cmd, rc = -EINVAL);
1676 /* Type of the command */
1677 if (strcmp(token, "start") == 0)
1678 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1679 else if (strcmp(token, "stop") == 0)
1680 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1681 else if (strcmp(token, "change") == 0)
1682 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1684 GOTO(out_free_cmd, rc = -EINVAL);
1686 /* Name of the rule */
1687 token = strsep(&val, " ");
1690 * Stop comand only need name argument,
1691 * But other commands need ID or rate argument.
1693 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1694 GOTO(out_free_cmd, rc = -EINVAL);
1697 for (i = 0; i < strlen(token); i++) {
1698 if ((!isalnum(token[i])) &&
1700 GOTO(out_free_cmd, rc = -EINVAL);
1702 cmd->tc_name = token;
1704 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1707 rc = nrs_tbf_id_parse(cmd, &val);
1709 GOTO(out_free_cmd, rc);
1713 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1714 strlen(val) == 0 || !isdigit(val[0]))
1715 GOTO(out_free_nid, rc = -EINVAL);
1717 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1718 if (cmd->tc_rpc_rate <= 0 ||
1719 cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1720 GOTO(out_free_nid, rc = -EINVAL);
1722 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1723 GOTO(out_free_nid, rc = -EINVAL);
1724 /* No RPC rate given */
1725 cmd->tc_rpc_rate = tbf_rate;
1729 nrs_tbf_cmd_fini(cmd);
1738 extern struct nrs_core nrs_core;
1739 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1741 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file, const char *buffer,
1742 size_t count, loff_t *off)
1744 struct seq_file *m = file->private_data;
1745 struct ptlrpc_service *svc = m->private;
1749 static struct nrs_tbf_cmd *cmd;
1750 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1751 unsigned long length;
1754 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1755 if (kernbuf == NULL)
1756 GOTO(out, rc = -ENOMEM);
1758 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1759 GOTO(out_free_kernbuff, rc = -EINVAL);
1761 if (copy_from_user(kernbuf, buffer, count))
1762 GOTO(out_free_kernbuff, rc = -EFAULT);
1765 token = strsep(&val, " ");
1767 GOTO(out_free_kernbuff, rc = -EINVAL);
1769 if (strcmp(token, "reg") == 0) {
1770 queue = PTLRPC_NRS_QUEUE_REG;
1771 } else if (strcmp(token, "hp") == 0) {
1772 queue = PTLRPC_NRS_QUEUE_HP;
1774 kernbuf[strlen(token)] = ' ';
1777 length = strlen(val);
1780 GOTO(out_free_kernbuff, rc = -EINVAL);
1782 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1783 GOTO(out_free_kernbuff, rc = -ENODEV);
1784 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1785 queue = PTLRPC_NRS_QUEUE_REG;
1787 cmd = nrs_tbf_parse_cmd(val, length);
1789 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1792 * Serialize NRS core lprocfs operations with policy registration/
1795 mutex_lock(&nrs_core.nrs_mutex);
1796 rc = ptlrpc_nrs_policy_control(svc, queue,
1798 NRS_CTL_TBF_WR_RULE,
1800 mutex_unlock(&nrs_core.nrs_mutex);
1802 nrs_tbf_cmd_fini(cmd);
1805 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1807 return rc ? rc : count;
1809 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1812 * Initializes a TBF policy's lprocfs interface for service \a svc
1814 * \param[in] svc the service
1817 * \retval != 0 error
1819 int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1821 struct lprocfs_seq_vars nrs_tbf_lprocfs_vars[] = {
1822 { .name = "nrs_tbf_rule",
1823 .fops = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1828 if (svc->srv_procroot == NULL)
1831 return lprocfs_seq_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars,
1836 * Cleans up a TBF policy's lprocfs interface for service \a svc
1838 * \param[in] svc the service
1840 void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1842 if (svc->srv_procroot == NULL)
1845 lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1848 #endif /* LPROCFS */
1851 * TBF policy operations
1853 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1854 .op_policy_start = nrs_tbf_start,
1855 .op_policy_stop = nrs_tbf_stop,
1856 .op_policy_ctl = nrs_tbf_ctl,
1857 .op_res_get = nrs_tbf_res_get,
1858 .op_res_put = nrs_tbf_res_put,
1859 .op_req_get = nrs_tbf_req_get,
1860 .op_req_enqueue = nrs_tbf_req_add,
1861 .op_req_dequeue = nrs_tbf_req_del,
1862 .op_req_stop = nrs_tbf_req_stop,
1864 .op_lprocfs_init = nrs_tbf_lprocfs_init,
1865 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
1870 * TBF policy configuration
1872 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1873 .nc_name = NRS_POL_NAME_TBF,
1874 .nc_ops = &nrs_tbf_ops,
1875 .nc_compat = nrs_policy_compat_all,
1882 #endif /* HAVE_SERVER_SUPPORT */